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