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 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "mbfilter.h"
35 #include "mbfilter_big5.h"
36
37 #include "unicode_table_big5.h"
38
39 static int mbfl_filt_ident_big5(int c, mbfl_identify_filter *filter);
40
41 static const unsigned char mblen_table_big5[] = { /* 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_big5_aliases[] = {"CN-BIG5", "BIG-FIVE", "BIGFIVE", NULL};
61
62 const mbfl_encoding mbfl_encoding_big5 = {
63 mbfl_no_encoding_big5,
64 "BIG-5",
65 "BIG5",
66 (const char *(*)[])&mbfl_encoding_big5_aliases,
67 mblen_table_big5,
68 MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE
69 };
70
71 const mbfl_encoding mbfl_encoding_cp950 = {
72 mbfl_no_encoding_cp950,
73 "CP950",
74 "BIG5",
75 NULL,
76 mblen_table_big5,
77 MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE
78 };
79
80 const struct mbfl_identify_vtbl vtbl_identify_big5 = {
81 mbfl_no_encoding_big5,
82 mbfl_filt_ident_common_ctor,
83 mbfl_filt_ident_common_dtor,
84 mbfl_filt_ident_big5
85 };
86
87 const struct mbfl_identify_vtbl vtbl_identify_cp950 = {
88 mbfl_no_encoding_cp950,
89 mbfl_filt_ident_common_ctor,
90 mbfl_filt_ident_common_dtor,
91 mbfl_filt_ident_big5
92 };
93
94 const struct mbfl_convert_vtbl vtbl_big5_wchar = {
95 mbfl_no_encoding_big5,
96 mbfl_no_encoding_wchar,
97 mbfl_filt_conv_common_ctor,
98 mbfl_filt_conv_common_dtor,
99 mbfl_filt_conv_big5_wchar,
100 mbfl_filt_conv_common_flush
101 };
102
103 const struct mbfl_convert_vtbl vtbl_wchar_big5 = {
104 mbfl_no_encoding_wchar,
105 mbfl_no_encoding_big5,
106 mbfl_filt_conv_common_ctor,
107 mbfl_filt_conv_common_dtor,
108 mbfl_filt_conv_wchar_big5,
109 mbfl_filt_conv_common_flush
110 };
111
112 const struct mbfl_convert_vtbl vtbl_cp950_wchar = {
113 mbfl_no_encoding_cp950,
114 mbfl_no_encoding_wchar,
115 mbfl_filt_conv_common_ctor,
116 mbfl_filt_conv_common_dtor,
117 mbfl_filt_conv_big5_wchar,
118 mbfl_filt_conv_common_flush
119 };
120
121 const struct mbfl_convert_vtbl vtbl_wchar_cp950 = {
122 mbfl_no_encoding_wchar,
123 mbfl_no_encoding_cp950,
124 mbfl_filt_conv_common_ctor,
125 mbfl_filt_conv_common_dtor,
126 mbfl_filt_conv_wchar_big5,
127 mbfl_filt_conv_common_flush
128 };
129
130 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
131
132 /* 63 + 94 = 157 or 94 */
133 static unsigned short cp950_pua_tbl[][4] = {
134 {0xe000,0xe310,0xfa40,0xfefe},
135 {0xe311,0xeeb7,0x8e40,0xa0fe},
136 {0xeeb8,0xf6b0,0x8140,0x8dfe},
137 {0xf6b1,0xf70e,0xc6a1,0xc6fe},
138 {0xf70f,0xf848,0xc740,0xc8fe},
139 };
140
is_in_cp950_pua(int c1,int c)141 static inline int is_in_cp950_pua(int c1, int c) {
142 if ((c1 >= 0xfa && c1 <= 0xfe) || (c1 >= 0x8e && c1 <= 0xa0) ||
143 (c1 >= 0x81 && c1 <= 0x8d) || (c1 >= 0xc7 && c1 <= 0xc8)) {
144 return (c >=0x40 && c <= 0x7e) || (c >= 0xa1 && c <= 0xfe);
145 }
146 if (c1 == 0xc6) {
147 return c >= 0xa1 && c <= 0xfe;
148 }
149 return 0;
150 }
151
152 /*
153 * Big5 => wchar
154 */
155 int
mbfl_filt_conv_big5_wchar(int c,mbfl_convert_filter * filter)156 mbfl_filt_conv_big5_wchar(int c, mbfl_convert_filter *filter)
157 {
158 int k;
159 int c1, w, c2;
160
161 switch (filter->status) {
162 case 0:
163 if (filter->from->no_encoding == mbfl_no_encoding_cp950) {
164 c1 = 0x80;
165 } else {
166 c1 = 0xa0;
167 }
168
169 if (c >= 0 && c <= 0x80) { /* latin */
170 CK((*filter->output_function)(c, filter->data));
171 } else if (c == 0xff) {
172 CK((*filter->output_function)(0xf8f8, filter->data));
173 } else if (c > c1 && c < 0xff) { /* dbcs lead byte */
174 filter->status = 1;
175 filter->cache = c;
176 } else {
177 w = c & MBFL_WCSGROUP_MASK;
178 w |= MBFL_WCSGROUP_THROUGH;
179 CK((*filter->output_function)(w, filter->data));
180 }
181 break;
182
183 case 1: /* dbcs second byte */
184 filter->status = 0;
185 c1 = filter->cache;
186 if ((c > 0x39 && c < 0x7f) | (c > 0xa0 && c < 0xff)) {
187 if (c < 0x7f){
188 w = (c1 - 0xa1)*157 + (c - 0x40);
189 } else {
190 w = (c1 - 0xa1)*157 + (c - 0xa1) + 0x3f;
191 }
192 if (w >= 0 && w < big5_ucs_table_size) {
193 w = big5_ucs_table[w];
194 } else {
195 w = 0;
196 }
197
198 if (filter->from->no_encoding == mbfl_no_encoding_cp950) {
199 /* PUA for CP950 */
200 if (w <= 0 && is_in_cp950_pua(c1, c)) {
201 c2 = c1 << 8 | c;
202 for (k = 0; k < sizeof(cp950_pua_tbl)/(sizeof(unsigned short)*4); k++) {
203 if (c2 >= cp950_pua_tbl[k][2] && c2 <= cp950_pua_tbl[k][3]) {
204 break;
205 }
206 }
207
208 if ((cp950_pua_tbl[k][2] & 0xff) == 0x40) {
209 w = 157*(c1 - (cp950_pua_tbl[k][2]>>8)) + c - (c >= 0xa1 ? 0x62 : 0x40)
210 + cp950_pua_tbl[k][0];
211 } else {
212 w = c2 - cp950_pua_tbl[k][2] + cp950_pua_tbl[k][0];
213 }
214 }
215 }
216
217 if (w <= 0) {
218 w = (c1 << 8) | c;
219 w &= MBFL_WCSPLANE_MASK;
220 w |= MBFL_WCSPLANE_BIG5;
221 }
222 CK((*filter->output_function)(w, filter->data));
223 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
224 CK((*filter->output_function)(c, filter->data));
225 } else {
226 w = (c1 << 8) | c;
227 w &= MBFL_WCSGROUP_MASK;
228 w |= MBFL_WCSGROUP_THROUGH;
229 CK((*filter->output_function)(w, filter->data));
230 }
231 break;
232
233 default:
234 filter->status = 0;
235 break;
236 }
237
238 return c;
239 }
240
241 /*
242 * wchar => Big5
243 */
244 int
mbfl_filt_conv_wchar_big5(int c,mbfl_convert_filter * filter)245 mbfl_filt_conv_wchar_big5(int c, mbfl_convert_filter *filter)
246 {
247 int k;
248 int c1, s, c2;
249
250 s = 0;
251 if (c >= ucs_a1_big5_table_min && c < ucs_a1_big5_table_max) {
252 s = ucs_a1_big5_table[c - ucs_a1_big5_table_min];
253 } else if (c >= ucs_a2_big5_table_min && c < ucs_a2_big5_table_max) {
254 s = ucs_a2_big5_table[c - ucs_a2_big5_table_min];
255 } else if (c >= ucs_a3_big5_table_min && c < ucs_a3_big5_table_max) {
256 s = ucs_a3_big5_table[c - ucs_a3_big5_table_min];
257 } else if (c >= ucs_i_big5_table_min && c < ucs_i_big5_table_max) {
258 s = ucs_i_big5_table[c - ucs_i_big5_table_min];
259 } else if (c >= ucs_pua_big5_table_min && c < ucs_pua_big5_table_max) {
260 s = ucs_pua_big5_table[c - ucs_pua_big5_table_min];
261 } else if (c >= ucs_r1_big5_table_min && c < ucs_r1_big5_table_max) {
262 s = ucs_r1_big5_table[c - ucs_r1_big5_table_min];
263 } else if (c >= ucs_r2_big5_table_min && c < ucs_r2_big5_table_max) {
264 s = ucs_r2_big5_table[c - ucs_r2_big5_table_min];
265 }
266
267 if (filter->to->no_encoding == mbfl_no_encoding_cp950) {
268 if (c >= 0xe000 && c <= 0xf848) { /* PUA for CP950 */
269 for (k = 0; k < sizeof(cp950_pua_tbl)/(sizeof(unsigned short)*4); k++) {
270 if (c <= cp950_pua_tbl[k][1]) {
271 break;
272 }
273 }
274 c1 = c - cp950_pua_tbl[k][0];
275 if ((cp950_pua_tbl[k][2] & 0xff) == 0x40) {
276 c2 = cp950_pua_tbl[k][2] >> 8;
277 s = ((c1 / 157) + c2) << 8; c1 %= 157;
278 s |= c1 + (c1 >= 0x3f ? 0x62 : 0x40);
279 } else {
280 s = c1 + cp950_pua_tbl[k][2];
281 }
282 }
283
284 if (c == 0x80) {
285 s = 0x80;
286 } else if (c == 0xf8f8) {
287 s = 0xff;
288 } else if (c == 0x256d) {
289 s = 0xa27e;
290 } else if (c == 0x256e) {
291 s = 0xa2a1;
292 } else if (c == 0x256f) {
293 s = 0xa2a3;
294 } else if (c == 0x2570) {
295 s = 0xa2a2;
296 }
297 }
298
299 if (s <= 0) {
300 c1 = c & ~MBFL_WCSPLANE_MASK;
301 if (c1 == MBFL_WCSPLANE_BIG5) {
302 s = c & MBFL_WCSPLANE_MASK;
303 }
304 if (c == 0) {
305 s = 0;
306 } else if (s <= 0) {
307 s = -1;
308 }
309 }
310 if (s >= 0) {
311 if (s <= 0x80 || s == 0xff) { /* latin */
312 CK((*filter->output_function)(s, filter->data));
313 } else {
314 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
315 CK((*filter->output_function)(s & 0xff, filter->data));
316 }
317 } else {
318 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
319 CK(mbfl_filt_conv_illegal_output(c, filter));
320 }
321 }
322
323 return c;
324 }
325
mbfl_filt_ident_big5(int c,mbfl_identify_filter * filter)326 static int mbfl_filt_ident_big5(int c, mbfl_identify_filter *filter)
327 {
328 int c1;
329 if (filter->encoding->no_encoding == mbfl_no_encoding_cp950) {
330 c1 = 0x80;
331 } else {
332 c1 = 0xa0;
333 }
334
335 if (filter->status) { /* kanji second char */
336 if (c < 0x40 || (c > 0x7e && c < 0xa1) ||c > 0xfe) { /* bad */
337 filter->flag = 1;
338 }
339 filter->status = 0;
340 } else if (c >= 0 && c < 0x80) { /* latin ok */
341 ;
342 } else if (c > c1 && c < 0xff) { /* DBCS lead byte */
343 filter->status = 1;
344 } else { /* bad */
345 filter->flag = 1;
346 }
347
348 return c;
349 }
350