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 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "mbfilter.h"
35 #include "mbfilter_cp936.h"
36 #define UNICODE_TABLE_CP936_DEF
37 #include "unicode_table_cp936.h"
38 
39 static int mbfl_filt_ident_cp936(int c, mbfl_identify_filter *filter);
40 
41 static const unsigned char mblen_table_cp936[] = { /* 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_cp936_aliases[] = {"CP-936", "GBK", NULL};
61 
62 const mbfl_encoding mbfl_encoding_cp936 = {
63 	mbfl_no_encoding_cp936,
64 	"CP936",
65 	"CP936",
66 	(const char *(*)[])&mbfl_encoding_cp936_aliases,
67 	mblen_table_cp936,
68 	MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE,
69 	&vtbl_cp936_wchar,
70 	&vtbl_wchar_cp936
71 };
72 
73 const struct mbfl_identify_vtbl vtbl_identify_cp936 = {
74 	mbfl_no_encoding_cp936,
75 	mbfl_filt_ident_common_ctor,
76 	mbfl_filt_ident_common_dtor,
77 	mbfl_filt_ident_cp936
78 };
79 
80 const struct mbfl_convert_vtbl vtbl_cp936_wchar = {
81 	mbfl_no_encoding_cp936,
82 	mbfl_no_encoding_wchar,
83 	mbfl_filt_conv_common_ctor,
84 	mbfl_filt_conv_common_dtor,
85 	mbfl_filt_conv_cp936_wchar,
86 	mbfl_filt_conv_common_flush
87 };
88 
89 const struct mbfl_convert_vtbl vtbl_wchar_cp936 = {
90 	mbfl_no_encoding_wchar,
91 	mbfl_no_encoding_cp936,
92 	mbfl_filt_conv_common_ctor,
93 	mbfl_filt_conv_common_dtor,
94 	mbfl_filt_conv_wchar_cp936,
95 	mbfl_filt_conv_common_flush
96 };
97 
98 
99 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
100 
101 /*
102  * CP936 => wchar
103  */
104 int
mbfl_filt_conv_cp936_wchar(int c,mbfl_convert_filter * filter)105 mbfl_filt_conv_cp936_wchar(int c, mbfl_convert_filter *filter)
106 {
107 	int k;
108 	int c1, c2, w = -1;
109 
110 	switch (filter->status) {
111 	case 0:
112 		if (c >= 0 && c < 0x80) {	/* latin */
113 			CK((*filter->output_function)(c, filter->data));
114 		} else if (c == 0x80) {	/* euro sign */
115 			CK((*filter->output_function)(0x20ac, filter->data));
116 		} else if (c < 0xff) {	/* dbcs lead byte */
117 			filter->status = 1;
118 			filter->cache = c;
119 		} else { /* 0xff */
120 			CK((*filter->output_function)(0xf8f5, filter->data));
121 		}
122 		break;
123 
124 	case 1:		/* dbcs second byte */
125 		filter->status = 0;
126 		c1 = filter->cache;
127 
128 		if (((c1 >= 0xaa && c1 <= 0xaf) || (c1 >= 0xf8 && c1 <= 0xfe)) &&
129 			(c >= 0xa1 && c <= 0xfe)) {
130 			/* UDA part1,2: U+E000-U+E4C5 */
131 			w = 94*(c1 >= 0xf8 ? c1 - 0xf2 : c1 - 0xaa) + (c - 0xa1) + 0xe000;
132 			CK((*filter->output_function)(w, filter->data));
133 		} else if (c1 >= 0xa1 && c1 <= 0xa7 && c >= 0x40 && c < 0xa1 && c != 0x7f) {
134 			/* UDA part3 : U+E4C6-U+E765*/
135 			w = 96*(c1 - 0xa1) + c - (c >= 0x80 ? 0x41 : 0x40) + 0xe4c6;
136 			CK((*filter->output_function)(w, filter->data));
137 		}
138 
139 		c2 = (c1 << 8) | c;
140 
141 		if (w <= 0 &&
142 			((c2 >= 0xa2ab && c2 <= 0xa9f0 + (0xe80f-0xe801)) ||
143 			 (c2 >= 0xd7fa && c2 <= 0xd7fa + (0xe814-0xe810)) ||
144 			 (c2 >= 0xfe50 && c2 <= 0xfe80 + (0xe864-0xe844)))) {
145 			for (k = 0; k < mbfl_cp936_pua_tbl_max; k++) {
146 				if (c2 >= mbfl_cp936_pua_tbl[k][2] &&
147 					c2 <= mbfl_cp936_pua_tbl[k][2] +
148 					mbfl_cp936_pua_tbl[k][1] -  mbfl_cp936_pua_tbl[k][0]) {
149 					w = c2 -  mbfl_cp936_pua_tbl[k][2] + mbfl_cp936_pua_tbl[k][0];
150 					CK((*filter->output_function)(w, filter->data));
151 					break;
152 				}
153 			}
154 		}
155 
156 		if (w <= 0) {
157 			if (c1 < 0xff && c1 > 0x80 && c > 0x39 && c < 0xff && c != 0x7f) {
158 				w = (c1 - 0x81)*192 + (c - 0x40);
159 				if (w >= 0 && w < cp936_ucs_table_size) {
160 					w = cp936_ucs_table[w];
161 				} else {
162 					w = 0;
163 				}
164 				if (w <= 0) {
165 					w = (c1 << 8) | c;
166 					w &= MBFL_WCSPLANE_MASK;
167 					w |= MBFL_WCSPLANE_WINCP936;
168 				}
169 				CK((*filter->output_function)(w, filter->data));
170 			} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
171 				CK((*filter->output_function)(c, filter->data));
172 			} else {
173 				w = (c1 << 8) | c;
174 				w &= MBFL_WCSGROUP_MASK;
175 				w |= MBFL_WCSGROUP_THROUGH;
176 				CK((*filter->output_function)(w, filter->data));
177 			}
178 		}
179 		break;
180 
181 	default:
182 		filter->status = 0;
183 		break;
184 	}
185 
186 	return c;
187 }
188 
189 /*
190  * wchar => CP936
191  */
192 int
mbfl_filt_conv_wchar_cp936(int c,mbfl_convert_filter * filter)193 mbfl_filt_conv_wchar_cp936(int c, mbfl_convert_filter *filter)
194 {
195 	int k, k1, k2;
196 	int c1, s = 0;
197 
198 	if (c >= ucs_a1_cp936_table_min && c < ucs_a1_cp936_table_max) {
199 		/* U+0000 - U+0451 */
200 		s = ucs_a1_cp936_table[c - ucs_a1_cp936_table_min];
201 	} else if (c >= ucs_a2_cp936_table_min && c < ucs_a2_cp936_table_max) {
202 		/* U+2000 - U+26FF */
203 		if (c == 0x203e) {
204 			s = 0xa3fe;
205 		} else if (c == 0x2218) {
206 			s = 0xa1e3;
207 		} else if (c == 0x223c) {
208 			s = 0xa1ab;
209 		} else {
210 			s = ucs_a2_cp936_table[c - ucs_a2_cp936_table_min];
211 		}
212 	} else if (c >= ucs_a3_cp936_table_min && c < ucs_a3_cp936_table_max) {
213 		/* U+2F00 - U+33FF */
214 		s = ucs_a3_cp936_table[c - ucs_a3_cp936_table_min];
215 	} else if (c >= ucs_i_cp936_table_min && c < ucs_i_cp936_table_max) {
216 		/* U+4D00-9FFF CJK Unified Ideographs (+ Extension A) */
217 		s = ucs_i_cp936_table[c - ucs_i_cp936_table_min];
218 	} else if (c >= 0xe000 && c <= 0xe864) { /* PUA */
219 		if (c < 0xe766) {
220 			if (c < 0xe4c6) {
221 				c1 = c - 0xe000;
222 				s = (c1 % 94) + 0xa1; c1 /= 94;
223 				s |= (c1 < 0x06 ? c1 + 0xaa : c1 + 0xf2) << 8;
224 			} else {
225 				c1 = c - 0xe4c6;
226 				s = ((c1 / 96) + 0xa1) << 8; c1 %= 96;
227 				s |= c1 + (c1 >= 0x3f ? 0x41 : 0x40);
228 			}
229 		} else {
230 			/* U+E766..U+E864 */
231 			k1 = 0; k2 = mbfl_cp936_pua_tbl_max;
232 			while (k1 < k2) {
233 				k = (k1 + k2) >> 1;
234 				if (c < mbfl_cp936_pua_tbl[k][0]) {
235 					k2 = k;
236 				} else if (c > mbfl_cp936_pua_tbl[k][1]) {
237 					k1 = k + 1;
238 				} else {
239 					s = c - mbfl_cp936_pua_tbl[k][0] + mbfl_cp936_pua_tbl[k][2];
240 					break;
241 				}
242 			}
243 		}
244 	} else if (c == 0xf8f5) {
245 		s = 0xff;
246 	} else if (c >= ucs_ci_cp936_table_min && c < ucs_ci_cp936_table_max) {
247 		/* U+F900-FA2F CJK Compatibility Ideographs */
248 		s = ucs_ci_cp936_table[c - ucs_ci_cp936_table_min];
249 	} else if (c >= ucs_cf_cp936_table_min && c < ucs_cf_cp936_table_max) {
250 		s = ucs_cf_cp936_table[c - ucs_cf_cp936_table_min];
251 	} else if (c >= ucs_sfv_cp936_table_min && c < ucs_sfv_cp936_table_max) {
252 		s = ucs_sfv_cp936_table[c - ucs_sfv_cp936_table_min]; /* U+FE50-FE6F Small Form Variants */
253 	} else if (c >= ucs_hff_cp936_table_min && c < ucs_hff_cp936_table_max) {
254 		/* U+FF00-FFFF HW/FW Forms */
255 		if (c == 0xff04) {
256 			s = 0xa1e7;
257 		} else if (c == 0xff5e) {
258 			s = 0xa1ab;
259 		} else if (c >= 0xff01 && c <= 0xff5d) {
260 			s = c - 0xff01 + 0xa3a1;
261 		} else if (c >= 0xffe0 && c <= 0xffe5) {
262 			s = ucs_hff_s_cp936_table[c-0xffe0];
263 		}
264 	}
265 	if (s <= 0) {
266 		c1 = c & ~MBFL_WCSPLANE_MASK;
267 		if (c1 == MBFL_WCSPLANE_WINCP936) {
268 			s = c & MBFL_WCSPLANE_MASK;
269 		}
270 		if (c == 0) {
271 			s = 0;
272 		} else if (s <= 0) {
273 			s = -1;
274 		}
275 	}
276 	if (s >= 0) {
277 		if (s <= 0x80 || s == 0xff) {	/* latin */
278 			CK((*filter->output_function)(s, filter->data));
279 		} else {
280 			CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
281 			CK((*filter->output_function)(s & 0xff, filter->data));
282 		}
283 	} else {
284 		if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
285 			CK(mbfl_filt_conv_illegal_output(c, filter));
286 		}
287 	}
288 
289 	return c;
290 }
291 
mbfl_filt_ident_cp936(int c,mbfl_identify_filter * filter)292 static int mbfl_filt_ident_cp936(int c, mbfl_identify_filter *filter)
293 {
294 	if (filter->status) {		/* kanji second char */
295 		if (c < 0x40 || c > 0xfe || c == 0x7f) {	/* bad */
296 		    filter->flag = 1;
297 		}
298 		filter->status = 0;
299 	} else if (c >= 0 && c < 0x80) {	/* latin  ok */
300 		;
301 	} else if (c > 0x80 && c < 0xff) {	/* DBCS lead byte */
302 		filter->status = 1;
303 	} else {							/* bad */
304 		filter->flag = 1;
305 	}
306 
307 	return c;
308 }
309