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 #include "mbfilter.h"
31 #include "mbfilter_cp936.h"
32 #define UNICODE_TABLE_CP936_DEF
33 #include "unicode_table_cp936.h"
34
35 static int mbfl_filt_conv_cp936_wchar_flush(mbfl_convert_filter *filter);
36
37 static const unsigned char mblen_table_cp936[] = { /* 0x81-0xFE */
38 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
47 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
49 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
50 2, 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, 1
54 };
55
56 static const char *mbfl_encoding_cp936_aliases[] = {"CP-936", "GBK", NULL};
57
58 const mbfl_encoding mbfl_encoding_cp936 = {
59 mbfl_no_encoding_cp936,
60 "CP936",
61 "CP936",
62 mbfl_encoding_cp936_aliases,
63 mblen_table_cp936,
64 MBFL_ENCTYPE_GL_UNSAFE,
65 &vtbl_cp936_wchar,
66 &vtbl_wchar_cp936,
67 NULL
68 };
69
70 const struct mbfl_convert_vtbl vtbl_cp936_wchar = {
71 mbfl_no_encoding_cp936,
72 mbfl_no_encoding_wchar,
73 mbfl_filt_conv_common_ctor,
74 NULL,
75 mbfl_filt_conv_cp936_wchar,
76 mbfl_filt_conv_cp936_wchar_flush,
77 NULL,
78 };
79
80 const struct mbfl_convert_vtbl vtbl_wchar_cp936 = {
81 mbfl_no_encoding_wchar,
82 mbfl_no_encoding_cp936,
83 mbfl_filt_conv_common_ctor,
84 NULL,
85 mbfl_filt_conv_wchar_cp936,
86 mbfl_filt_conv_common_flush,
87 NULL,
88 };
89
90
91 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
92
mbfl_filt_conv_cp936_wchar(int c,mbfl_convert_filter * filter)93 int mbfl_filt_conv_cp936_wchar(int c, mbfl_convert_filter *filter)
94 {
95 int k;
96 int c1, c2, w = -1;
97
98 switch (filter->status) {
99 case 0:
100 if (c >= 0 && c < 0x80) { /* latin */
101 CK((*filter->output_function)(c, filter->data));
102 } else if (c == 0x80) { /* euro sign */
103 CK((*filter->output_function)(0x20ac, filter->data));
104 } else if (c < 0xff) { /* dbcs lead byte */
105 filter->status = 1;
106 filter->cache = c;
107 } else { /* 0xff */
108 CK((*filter->output_function)(0xf8f5, filter->data));
109 }
110 break;
111
112 case 1: /* dbcs second byte */
113 filter->status = 0;
114 c1 = filter->cache;
115
116 if (((c1 >= 0xaa && c1 <= 0xaf) || (c1 >= 0xf8 && c1 <= 0xfe)) &&
117 (c >= 0xa1 && c <= 0xfe)) {
118 /* UDA part1,2: U+E000-U+E4C5 */
119 w = 94*(c1 >= 0xf8 ? c1 - 0xf2 : c1 - 0xaa) + (c - 0xa1) + 0xe000;
120 CK((*filter->output_function)(w, filter->data));
121 } else if (c1 >= 0xa1 && c1 <= 0xa7 && c >= 0x40 && c < 0xa1 && c != 0x7f) {
122 /* UDA part3 : U+E4C6-U+E765*/
123 w = 96*(c1 - 0xa1) + c - (c >= 0x80 ? 0x41 : 0x40) + 0xe4c6;
124 CK((*filter->output_function)(w, filter->data));
125 }
126
127 c2 = (c1 << 8) | c;
128
129 if (w <= 0 &&
130 ((c2 >= 0xa2ab && c2 <= 0xa9f0 + (0xe80f-0xe801)) ||
131 (c2 >= 0xd7fa && c2 <= 0xd7fa + (0xe814-0xe810)) ||
132 (c2 >= 0xfe50 && c2 <= 0xfe80 + (0xe864-0xe844)))) {
133 for (k = 0; k < mbfl_cp936_pua_tbl_max; k++) {
134 if (c2 >= mbfl_cp936_pua_tbl[k][2] &&
135 c2 <= mbfl_cp936_pua_tbl[k][2] +
136 mbfl_cp936_pua_tbl[k][1] - mbfl_cp936_pua_tbl[k][0]) {
137 w = c2 - mbfl_cp936_pua_tbl[k][2] + mbfl_cp936_pua_tbl[k][0];
138 CK((*filter->output_function)(w, filter->data));
139 break;
140 }
141 }
142 }
143
144 if (w <= 0) {
145 if (c1 < 0xff && c1 > 0x80 && c >= 0x40 && c < 0xff && c != 0x7f) {
146 w = (c1 - 0x81)*192 + (c - 0x40);
147 if (w >= 0 && w < cp936_ucs_table_size) {
148 w = cp936_ucs_table[w];
149 if (!w)
150 w = MBFL_BAD_INPUT;
151 } else {
152 w = MBFL_BAD_INPUT;
153 }
154 CK((*filter->output_function)(w, filter->data));
155 } else {
156 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
157 }
158 }
159 break;
160
161 default:
162 filter->status = 0;
163 break;
164 }
165
166 return 0;
167 }
168
mbfl_filt_conv_cp936_wchar_flush(mbfl_convert_filter * filter)169 static int mbfl_filt_conv_cp936_wchar_flush(mbfl_convert_filter *filter)
170 {
171 if (filter->status) {
172 /* 2-byte character was truncated */
173 filter->status = 0;
174 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
175 }
176
177 if (filter->flush_function) {
178 (*filter->flush_function)(filter->data);
179 }
180
181 return 0;
182 }
183
mbfl_filt_conv_wchar_cp936(int c,mbfl_convert_filter * filter)184 int mbfl_filt_conv_wchar_cp936(int c, mbfl_convert_filter *filter)
185 {
186 int k, k1, k2;
187 int c1, s = 0;
188
189 if (c >= ucs_a1_cp936_table_min && c < ucs_a1_cp936_table_max) {
190 /* U+0000 - U+0451 */
191 s = ucs_a1_cp936_table[c - ucs_a1_cp936_table_min];
192 } else if (c >= ucs_a2_cp936_table_min && c < ucs_a2_cp936_table_max) {
193 /* U+2000 - U+26FF */
194 if (c == 0x203e) {
195 s = 0xa3fe;
196 } else if (c == 0x2218) {
197 s = 0xa1e3;
198 } else if (c == 0x223c) {
199 s = 0xa1ab;
200 } else {
201 s = ucs_a2_cp936_table[c - ucs_a2_cp936_table_min];
202 }
203 } else if (c >= ucs_a3_cp936_table_min && c < ucs_a3_cp936_table_max) {
204 /* U+2F00 - U+33FF */
205 s = ucs_a3_cp936_table[c - ucs_a3_cp936_table_min];
206 } else if (c >= ucs_i_cp936_table_min && c < ucs_i_cp936_table_max) {
207 /* U+4D00-9FFF CJK Unified Ideographs (+ Extension A) */
208 s = ucs_i_cp936_table[c - ucs_i_cp936_table_min];
209 } else if (c >= 0xe000 && c <= 0xe864) { /* PUA */
210 if (c < 0xe766) {
211 if (c < 0xe4c6) {
212 c1 = c - 0xe000;
213 s = (c1 % 94) + 0xa1; c1 /= 94;
214 s |= (c1 < 0x06 ? c1 + 0xaa : c1 + 0xf2) << 8;
215 } else {
216 c1 = c - 0xe4c6;
217 s = ((c1 / 96) + 0xa1) << 8; c1 %= 96;
218 s |= c1 + (c1 >= 0x3f ? 0x41 : 0x40);
219 }
220 } else {
221 /* U+E766..U+E864 */
222 k1 = 0; k2 = mbfl_cp936_pua_tbl_max;
223 while (k1 < k2) {
224 k = (k1 + k2) >> 1;
225 if (c < mbfl_cp936_pua_tbl[k][0]) {
226 k2 = k;
227 } else if (c > mbfl_cp936_pua_tbl[k][1]) {
228 k1 = k + 1;
229 } else {
230 s = c - mbfl_cp936_pua_tbl[k][0] + mbfl_cp936_pua_tbl[k][2];
231 break;
232 }
233 }
234 }
235 } else if (c == 0xf8f5) {
236 s = 0xff;
237 } else if (c >= ucs_ci_cp936_table_min && c < ucs_ci_cp936_table_max) {
238 /* U+F900-FA2F CJK Compatibility Ideographs */
239 s = ucs_ci_cp936_table[c - ucs_ci_cp936_table_min];
240 } else if (c >= ucs_cf_cp936_table_min && c < ucs_cf_cp936_table_max) {
241 s = ucs_cf_cp936_table[c - ucs_cf_cp936_table_min];
242 } else if (c >= ucs_sfv_cp936_table_min && c < ucs_sfv_cp936_table_max) {
243 s = ucs_sfv_cp936_table[c - ucs_sfv_cp936_table_min]; /* U+FE50-FE6F Small Form Variants */
244 } else if (c >= ucs_hff_cp936_table_min && c < ucs_hff_cp936_table_max) {
245 /* U+FF00-FFFF HW/FW Forms */
246 if (c == 0xff04) {
247 s = 0xa1e7;
248 } else if (c == 0xff5e) {
249 s = 0xa1ab;
250 } else if (c >= 0xff01 && c <= 0xff5d) {
251 s = c - 0xff01 + 0xa3a1;
252 } else if (c >= 0xffe0 && c <= 0xffe5) {
253 s = ucs_hff_s_cp936_table[c-0xffe0];
254 }
255 }
256
257 if (s <= 0) {
258 if (c == 0) {
259 s = 0;
260 } else if (s <= 0) {
261 s = -1;
262 }
263 }
264
265 if (s >= 0) {
266 if (s <= 0x80 || s == 0xff) { /* latin */
267 CK((*filter->output_function)(s, filter->data));
268 } else {
269 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
270 CK((*filter->output_function)(s & 0xff, filter->data));
271 }
272 } else {
273 CK(mbfl_filt_conv_illegal_output(c, filter));
274 }
275
276 return 0;
277 }
278