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: Rui Hirokawa <hirokawa@php.net>
22 *
23 */
24 /*
25 * The source code included in this files was separated from mbfilter_tw.c
26 * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
27 *
28 */
29
30 #include "mbfilter.h"
31 #include "mbfilter_big5.h"
32
33 #include "unicode_table_big5.h"
34
35 static int mbfl_filt_conv_big5_wchar_flush(mbfl_convert_filter *filter);
36
37 static const unsigned char mblen_table_big5[] = { /* 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_big5_aliases[] = {"CN-BIG5", "BIG-FIVE", "BIGFIVE", NULL};
57
58 const mbfl_encoding mbfl_encoding_big5 = {
59 mbfl_no_encoding_big5,
60 "BIG-5",
61 "BIG5",
62 mbfl_encoding_big5_aliases,
63 mblen_table_big5,
64 MBFL_ENCTYPE_GL_UNSAFE,
65 &vtbl_big5_wchar,
66 &vtbl_wchar_big5,
67 NULL
68 };
69
70 const mbfl_encoding mbfl_encoding_cp950 = {
71 mbfl_no_encoding_cp950,
72 "CP950",
73 "BIG5",
74 NULL,
75 mblen_table_big5,
76 MBFL_ENCTYPE_GL_UNSAFE,
77 &vtbl_cp950_wchar,
78 &vtbl_wchar_cp950,
79 NULL
80 };
81
82 const struct mbfl_convert_vtbl vtbl_big5_wchar = {
83 mbfl_no_encoding_big5,
84 mbfl_no_encoding_wchar,
85 mbfl_filt_conv_common_ctor,
86 NULL,
87 mbfl_filt_conv_big5_wchar,
88 mbfl_filt_conv_big5_wchar_flush,
89 NULL,
90 };
91
92 const struct mbfl_convert_vtbl vtbl_wchar_big5 = {
93 mbfl_no_encoding_wchar,
94 mbfl_no_encoding_big5,
95 mbfl_filt_conv_common_ctor,
96 NULL,
97 mbfl_filt_conv_wchar_big5,
98 mbfl_filt_conv_common_flush,
99 NULL
100 };
101
102 const struct mbfl_convert_vtbl vtbl_cp950_wchar = {
103 mbfl_no_encoding_cp950,
104 mbfl_no_encoding_wchar,
105 mbfl_filt_conv_common_ctor,
106 NULL,
107 mbfl_filt_conv_big5_wchar,
108 mbfl_filt_conv_big5_wchar_flush,
109 NULL,
110 };
111
112 const struct mbfl_convert_vtbl vtbl_wchar_cp950 = {
113 mbfl_no_encoding_wchar,
114 mbfl_no_encoding_cp950,
115 mbfl_filt_conv_common_ctor,
116 NULL,
117 mbfl_filt_conv_wchar_big5,
118 mbfl_filt_conv_common_flush,
119 NULL,
120 };
121
122 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
123
124 /* 63 + 94 = 157 or 94 */
125 static unsigned short cp950_pua_tbl[][4] = {
126 {0xe000, 0xe310, 0xfa40, 0xfefe},
127 {0xe311, 0xeeb7, 0x8e40, 0xa0fe},
128 {0xeeb8, 0xf6b0, 0x8140, 0x8dfe},
129 {0xf6b1, 0xf70e, 0xc6a1, 0xc6fe},
130 {0xf70f, 0xf848, 0xc740, 0xc8fe},
131 };
132
is_in_cp950_pua(int c1,int c)133 static inline int is_in_cp950_pua(int c1, int c)
134 {
135 if ((c1 >= 0xfa && c1 <= 0xfe) || (c1 >= 0x8e && c1 <= 0xa0) ||
136 (c1 >= 0x81 && c1 <= 0x8d) || (c1 >= 0xc7 && c1 <= 0xc8)) {
137 return (c >= 0x40 && c <= 0x7e) || (c >= 0xa1 && c <= 0xfe);
138 } else if (c1 == 0xc6) {
139 return c >= 0xa1 && c <= 0xfe;
140 }
141 return 0;
142 }
143
mbfl_filt_conv_big5_wchar(int c,mbfl_convert_filter * filter)144 int mbfl_filt_conv_big5_wchar(int c, mbfl_convert_filter *filter)
145 {
146 int k, c1, w;
147
148 switch (filter->status) {
149 case 0:
150 if (c >= 0 && c < 0x80) { /* latin */
151 CK((*filter->output_function)(c, filter->data));
152 } else if (filter->from->no_encoding != mbfl_no_encoding_cp950 && c > 0xA0 && c <= 0xF9 && c != 0xC8) {
153 filter->status = 1;
154 filter->cache = c;
155 } else if (filter->from->no_encoding == mbfl_no_encoding_cp950 && c > 0x80 && c <= 0xFE) {
156 filter->status = 1;
157 filter->cache = c;
158 } else {
159 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
160 }
161 break;
162
163 case 1: /* dbcs second byte */
164 filter->status = 0;
165 c1 = filter->cache;
166 if ((c > 0x3f && c < 0x7f) || (c > 0xa0 && c < 0xff)) {
167 if (c < 0x7f) {
168 w = (c1 - 0xa1)*157 + (c - 0x40);
169 } else {
170 w = (c1 - 0xa1)*157 + (c - 0xa1) + 0x3f;
171 }
172 if (w >= 0 && w < big5_ucs_table_size) {
173 w = big5_ucs_table[w];
174 } else {
175 w = 0;
176 }
177
178 if (filter->from->no_encoding == mbfl_no_encoding_cp950) {
179 /* PUA for CP950 */
180 if (is_in_cp950_pua(c1, c)) {
181 int c2 = (c1 << 8) | c;
182
183 for (k = 0; k < sizeof(cp950_pua_tbl) / (sizeof(unsigned short)*4); k++) {
184 if (c2 >= cp950_pua_tbl[k][2] && c2 <= cp950_pua_tbl[k][3]) {
185 break;
186 }
187 }
188
189 if ((cp950_pua_tbl[k][2] & 0xff) == 0x40) {
190 w = 157*(c1 - (cp950_pua_tbl[k][2]>>8)) + c - (c >= 0xa1 ? 0x62 : 0x40) + cp950_pua_tbl[k][0];
191 } else {
192 w = c2 - cp950_pua_tbl[k][2] + cp950_pua_tbl[k][0];
193 }
194 } else if (c1 == 0xA1) {
195 if (c == 0x45) {
196 w = 0x2027;
197 } else if (c == 0x4E) {
198 w = 0xFE51;
199 } else if (c == 0x5A) {
200 w = 0x2574;
201 } else if (c == 0xC2) {
202 w = 0x00AF;
203 } else if (c == 0xC3) {
204 w = 0xFFE3;
205 } else if (c == 0xC5) {
206 w = 0x02CD;
207 } else if (c == 0xE3) {
208 w = 0xFF5E;
209 } else if (c == 0xF2) {
210 w = 0x2295;
211 } else if (c == 0xF3) {
212 w = 0x2299;
213 } else if (c == 0xFE) {
214 w = 0xFF0F;
215 }
216 } else if (c1 == 0xA2) {
217 if (c == 0x40) {
218 w = 0xFF3C;
219 } else if (c == 0x41) {
220 w = 0x2215;
221 } else if (c == 0x42) {
222 w = 0xFE68;
223 } else if (c == 0x46) {
224 w = 0xFFE0;
225 } else if (c == 0x47) {
226 w = 0xFFE1;
227 } else if (c == 0xCC) {
228 w = 0x5341;
229 } else if (c == 0xCE) {
230 w = 0x5345;
231 }
232 }
233 }
234
235 if (w <= 0) {
236 w = MBFL_BAD_INPUT;
237 }
238 CK((*filter->output_function)(w, filter->data));
239 } else {
240 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
241 }
242 break;
243
244 default:
245 filter->status = 0;
246 break;
247 }
248
249 return 0;
250 }
251
mbfl_filt_conv_big5_wchar_flush(mbfl_convert_filter * filter)252 static int mbfl_filt_conv_big5_wchar_flush(mbfl_convert_filter *filter)
253 {
254 if (filter->status == 1) {
255 /* 2-byte character was truncated */
256 filter->status = 0;
257 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
258 }
259
260 if (filter->flush_function) {
261 (*filter->flush_function)(filter->data);
262 }
263
264 return 0;
265 }
266
mbfl_filt_conv_wchar_big5(int c,mbfl_convert_filter * filter)267 int mbfl_filt_conv_wchar_big5(int c, mbfl_convert_filter *filter)
268 {
269 int k, s = 0;
270
271 if (c >= ucs_a1_big5_table_min && c < ucs_a1_big5_table_max) {
272 s = ucs_a1_big5_table[c - ucs_a1_big5_table_min];
273 } else if (c >= ucs_a2_big5_table_min && c < ucs_a2_big5_table_max) {
274 s = ucs_a2_big5_table[c - ucs_a2_big5_table_min];
275 } else if (c >= ucs_a3_big5_table_min && c < ucs_a3_big5_table_max) {
276 s = ucs_a3_big5_table[c - ucs_a3_big5_table_min];
277 } else if (c >= ucs_i_big5_table_min && c < ucs_i_big5_table_max) {
278 s = ucs_i_big5_table[c - ucs_i_big5_table_min];
279 } else if (c >= ucs_r1_big5_table_min && c < ucs_r1_big5_table_max) {
280 s = ucs_r1_big5_table[c - ucs_r1_big5_table_min];
281 } else if (c >= ucs_r2_big5_table_min && c < ucs_r2_big5_table_max) {
282 s = ucs_r2_big5_table[c - ucs_r2_big5_table_min];
283 }
284
285 if (filter->to->no_encoding == mbfl_no_encoding_cp950) {
286 if (c >= 0xe000 && c <= 0xf848) { /* PUA for CP950 */
287 for (k = 0; k < sizeof(cp950_pua_tbl) / (sizeof(unsigned short)*4); k++) {
288 if (c <= cp950_pua_tbl[k][1]) {
289 break;
290 }
291 }
292
293 int c1 = c - cp950_pua_tbl[k][0];
294 if ((cp950_pua_tbl[k][2] & 0xff) == 0x40) {
295 int c2 = cp950_pua_tbl[k][2] >> 8;
296 s = ((c1 / 157) + c2) << 8;
297 c1 %= 157;
298 s |= c1 + (c1 >= 0x3f ? 0x62 : 0x40);
299 } else {
300 s = c1 + cp950_pua_tbl[k][2];
301 }
302 } else if (c == 0x00A2) {
303 s = 0;
304 } else if (c == 0x00A3) {
305 s = 0;
306 } else if (c == 0x00AF) {
307 s = 0xA1C2;
308 } else if (c == 0x02CD) {
309 s = 0xA1C5;
310 } else if (c == 0x0401) {
311 s = 0;
312 } else if (c >= 0x0414 && c <= 0x041C) {
313 s = 0;
314 } else if (c >= 0x0423 && c <= 0x044F) {
315 s = 0;
316 } else if (c == 0x0451) {
317 s = 0;
318 } else if (c == 0x2022) {
319 s = 0;
320 } else if (c == 0x2027) {
321 s = 0xA145;
322 } else if (c == 0x203E) {
323 s = 0;
324 } else if (c == 0x2215) {
325 s = 0xA241;
326 } else if (c == 0x223C) {
327 s = 0;
328 } else if (c == 0x2295) {
329 s = 0xA1F2;
330 } else if (c == 0x2299) {
331 s = 0xA1F3;
332 } else if (c >= 0x2460 && c <= 0x247D) {
333 s = 0;
334 } else if (c == 0x2574) {
335 s = 0xA15A;
336 } else if (c == 0x2609) {
337 s = 0;
338 } else if (c == 0x2641) {
339 s = 0;
340 } else if (c == 0x3005 || (c >= 0x302A && c <= 0x30FF)) {
341 s = 0;
342 } else if (c == 0xFE51) {
343 s = 0xA14E;
344 } else if (c == 0xFE68) {
345 s = 0xA242;
346 } else if (c == 0xFF3C) {
347 s = 0xA240;
348 } else if (c == 0xFF5E) {
349 s = 0xA1E3;
350 } else if (c == 0xFF64) {
351 s = 0;
352 } else if (c == 0xFFE0) {
353 s = 0xA246;
354 } else if (c == 0xFFE1) {
355 s = 0xA247;
356 } else if (c == 0xFFE3) {
357 s = 0xA1C3;
358 } else if (c == 0xFF0F) {
359 s = 0xA1FE;
360 }
361 }
362
363 if (s <= 0) {
364 if (c == 0) {
365 s = 0;
366 } else {
367 s = -1;
368 }
369 }
370
371 if (s >= 0) {
372 if (s <= 0x80) { /* latin */
373 CK((*filter->output_function)(s, filter->data));
374 } else {
375 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
376 CK((*filter->output_function)(s & 0xff, filter->data));
377 }
378 } else {
379 CK(mbfl_filt_conv_illegal_output(c, filter));
380 }
381
382 return 0;
383 }
384