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