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