1 /*
2 * "streamable kanji code filter and converter"
3 * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
4 *
5 * LICENSE NOTICES
6 *
7 * This file is part of "streamable kanji code filter and converter",
8 * which is distributed under the terms of GNU Lesser General Public
9 * License (version 2) as published by the Free Software Foundation.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with "streamable kanji code filter and converter";
18 * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 * Suite 330, Boston, MA 02111-1307 USA
20 *
21 * The author of this file:
22 *
23 */
24 /*
25 * the source code included in this files was separated from mbfilter_cn.c
26 * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
27 *
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "mbfilter.h"
35 #include "mbfilter_cp936.h"
36
37 #include "unicode_table_cp936.h"
38
39 static int mbfl_filt_ident_cp936(int c, mbfl_identify_filter *filter);
40
41 static const unsigned char mblen_table_cp936[] = { /* 0x81-0xFE */
42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
53 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
54 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
55 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
56 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
57 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
58 };
59
60 static const char *mbfl_encoding_cp936_aliases[] = {"CP-936", "GBK", NULL};
61
62 const mbfl_encoding mbfl_encoding_cp936 = {
63 mbfl_no_encoding_cp936,
64 "CP936",
65 "CP936",
66 (const char *(*)[])&mbfl_encoding_cp936_aliases,
67 mblen_table_cp936,
68 MBFL_ENCTYPE_MBCS
69 };
70
71 const struct mbfl_identify_vtbl vtbl_identify_cp936 = {
72 mbfl_no_encoding_cp936,
73 mbfl_filt_ident_common_ctor,
74 mbfl_filt_ident_common_dtor,
75 mbfl_filt_ident_cp936
76 };
77
78 const struct mbfl_convert_vtbl vtbl_cp936_wchar = {
79 mbfl_no_encoding_cp936,
80 mbfl_no_encoding_wchar,
81 mbfl_filt_conv_common_ctor,
82 mbfl_filt_conv_common_dtor,
83 mbfl_filt_conv_cp936_wchar,
84 mbfl_filt_conv_common_flush
85 };
86
87 const struct mbfl_convert_vtbl vtbl_wchar_cp936 = {
88 mbfl_no_encoding_wchar,
89 mbfl_no_encoding_cp936,
90 mbfl_filt_conv_common_ctor,
91 mbfl_filt_conv_common_dtor,
92 mbfl_filt_conv_wchar_cp936,
93 mbfl_filt_conv_common_flush
94 };
95
96
97 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
98
99 /*
100 * CP936 => wchar
101 */
102 int
mbfl_filt_conv_cp936_wchar(int c,mbfl_convert_filter * filter)103 mbfl_filt_conv_cp936_wchar(int c, mbfl_convert_filter *filter)
104 {
105 int c1, w;
106
107 switch (filter->status) {
108 case 0:
109 if (c >= 0 && c < 0x80) { /* latin */
110 CK((*filter->output_function)(c, filter->data));
111 } else if (c == 0x80) { /* euro sign */
112 CK((*filter->output_function)(0x20ac, filter->data));
113 } else if (c > 0x80 && c < 0xff) { /* dbcs lead byte */
114 filter->status = 1;
115 filter->cache = c;
116 } else {
117 w = c & MBFL_WCSGROUP_MASK;
118 w |= MBFL_WCSGROUP_THROUGH;
119 CK((*filter->output_function)(w, filter->data));
120 }
121 break;
122
123 case 1: /* dbcs second byte */
124 filter->status = 0;
125 c1 = filter->cache;
126 if ( c1 < 0xff && c1 > 0x80 && c > 0x39 && c < 0xff && c != 0x7f) {
127 w = (c1 - 0x81)*192 + (c - 0x40);
128 if (w >= 0 && w < cp936_ucs_table_size) {
129 w = cp936_ucs_table[w];
130 } else {
131 w = 0;
132 }
133 if (w <= 0) {
134 w = (c1 << 8) | c;
135 w &= MBFL_WCSPLANE_MASK;
136 w |= MBFL_WCSPLANE_WINCP936;
137 }
138 CK((*filter->output_function)(w, filter->data));
139 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
140 CK((*filter->output_function)(c, filter->data));
141 } else {
142 w = (c1 << 8) | c;
143 w &= MBFL_WCSGROUP_MASK;
144 w |= MBFL_WCSGROUP_THROUGH;
145 CK((*filter->output_function)(w, filter->data));
146 }
147 break;
148
149 default:
150 filter->status = 0;
151 break;
152 }
153
154 return c;
155 }
156
157 /*
158 * wchar => CP936
159 */
160 int
mbfl_filt_conv_wchar_cp936(int c,mbfl_convert_filter * filter)161 mbfl_filt_conv_wchar_cp936(int c, mbfl_convert_filter *filter)
162 {
163 int c1, s;
164
165 s = 0;
166 if (c >= ucs_a1_cp936_table_min && c < ucs_a1_cp936_table_max) {
167 s = ucs_a1_cp936_table[c - ucs_a1_cp936_table_min];
168 } else if (c >= ucs_a2_cp936_table_min && c < ucs_a2_cp936_table_max) {
169 s = ucs_a2_cp936_table[c - ucs_a2_cp936_table_min];
170 } else if (c >= ucs_a3_cp936_table_min && c < ucs_a3_cp936_table_max) {
171 s = ucs_a3_cp936_table[c - ucs_a3_cp936_table_min];
172 } else if (c >= ucs_i_cp936_table_min && c < ucs_i_cp936_table_max) {
173 s = ucs_i_cp936_table[c - ucs_i_cp936_table_min];
174 } else if (c >= ucs_ci_cp936_table_min && c < ucs_ci_cp936_table_max) {
175 s = ucs_ci_cp936_table[c - ucs_ci_cp936_table_min];
176 } else if (c >= ucs_cf_cp936_table_min && c < ucs_cf_cp936_table_max) {
177 s = ucs_cf_cp936_table[c - ucs_cf_cp936_table_min];
178 } else if (c >= ucs_sfv_cp936_table_min && c < ucs_sfv_cp936_table_max) {
179 s = ucs_sfv_cp936_table[c - ucs_sfv_cp936_table_min];
180 } else if (c >= ucs_hff_cp936_table_min && c < ucs_hff_cp936_table_max) {
181 s = ucs_hff_cp936_table[c - ucs_hff_cp936_table_min];
182 }
183 if (s <= 0) {
184 c1 = c & ~MBFL_WCSPLANE_MASK;
185 if (c1 == MBFL_WCSPLANE_WINCP936) {
186 s = c & MBFL_WCSPLANE_MASK;
187 }
188 if (c == 0) {
189 s = 0;
190 } else if (s <= 0) {
191 s = -1;
192 }
193 }
194 if (s >= 0) {
195 if (s <= 0x80) { /* latin */
196 CK((*filter->output_function)(s, filter->data));
197 } else {
198 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
199 CK((*filter->output_function)(s & 0xff, filter->data));
200 }
201 } else {
202 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
203 CK(mbfl_filt_conv_illegal_output(c, filter));
204 }
205 }
206
207 return c;
208 }
209
mbfl_filt_ident_cp936(int c,mbfl_identify_filter * filter)210 static int mbfl_filt_ident_cp936(int c, mbfl_identify_filter *filter)
211 {
212 if (filter->status) { /* kanji second char */
213 if (c < 0x40 || c > 0xfe || c == 0x7f) { /* bad */
214 filter->flag = 1;
215 }
216 filter->status = 0;
217 } else if (c >= 0 && c < 0x80) { /* latin ok */
218 ;
219 } else if (c > 0x80 && c < 0xff) { /* DBCS lead byte */
220 filter->status = 1;
221 } else { /* bad */
222 filter->flag = 1;
223 }
224
225 return c;
226 }
227
228
229