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