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