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_cp936.c
26  * by rui hirokawa <hirokawa@php.net> on 11 Aug 2011.
27  *
28  */
29 
30 #include "mbfilter.h"
31 #include "mbfilter_gb18030.h"
32 
33 #include "unicode_table_cp936.h"
34 #include "unicode_table_gb18030.h"
35 
36 static int mbfl_filt_conv_gb18030_wchar_flush(mbfl_convert_filter *filter);
37 static size_t mb_gb18030_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
38 static void mb_wchar_to_gb18030(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
39 
40 static const char *mbfl_encoding_gb18030_aliases[] = {"gb-18030", "gb-18030-2000", NULL};
41 
42 const mbfl_encoding mbfl_encoding_gb18030 = {
43 	mbfl_no_encoding_gb18030,
44 	"GB18030",
45 	"GB18030",
46 	mbfl_encoding_gb18030_aliases,
47 	NULL,
48 	MBFL_ENCTYPE_GL_UNSAFE,
49 	&vtbl_gb18030_wchar,
50 	&vtbl_wchar_gb18030,
51 	mb_gb18030_to_wchar,
52 	mb_wchar_to_gb18030,
53 	NULL
54 };
55 
56 const struct mbfl_convert_vtbl vtbl_gb18030_wchar = {
57 	mbfl_no_encoding_gb18030,
58 	mbfl_no_encoding_wchar,
59 	mbfl_filt_conv_common_ctor,
60 	NULL,
61 	mbfl_filt_conv_gb18030_wchar,
62 	mbfl_filt_conv_gb18030_wchar_flush,
63 	NULL,
64 };
65 
66 const struct mbfl_convert_vtbl vtbl_wchar_gb18030 = {
67 	mbfl_no_encoding_wchar,
68 	mbfl_no_encoding_gb18030,
69 	mbfl_filt_conv_common_ctor,
70 	NULL,
71 	mbfl_filt_conv_wchar_gb18030,
72 	mbfl_filt_conv_common_flush,
73 	NULL,
74 };
75 
76 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
77 
78 /* `tbl` contains inclusive ranges, each represented by a pair of unsigned shorts */
mbfl_bisec_srch(int w,const unsigned short * tbl,int n)79 int mbfl_bisec_srch(int w, const unsigned short *tbl, int n)
80 {
81 	int l = 0, r = n-1;
82 	while (l <= r) {
83 		int probe = (l + r) >> 1;
84 		unsigned short lo = tbl[2 * probe], hi = tbl[(2 * probe) + 1];
85 		if (w < lo) {
86 			r = probe - 1;
87 		} else if (w > hi) {
88 			l = probe + 1;
89 		} else {
90 			return probe;
91 		}
92 	}
93 	return -1;
94 }
95 
96 /* `tbl` contains single values, not ranges */
mbfl_bisec_srch2(int w,const unsigned short tbl[],int n)97 int mbfl_bisec_srch2(int w, const unsigned short tbl[], int n)
98 {
99 	int l = 0, r = n-1;
100 	while (l <= r) {
101 		int probe = (l + r) >> 1;
102 		unsigned short val = tbl[probe];
103 		if (w < val) {
104 			r = probe - 1;
105 		} else if (w > val) {
106 			l = probe + 1;
107 		} else {
108 			return probe;
109 		}
110 	}
111 	return -1;
112 }
113 
mbfl_filt_conv_gb18030_wchar(int c,mbfl_convert_filter * filter)114 int mbfl_filt_conv_gb18030_wchar(int c, mbfl_convert_filter *filter)
115 {
116 	int k;
117 	int c1, c2, c3, w = -1;
118 
119 	switch (filter->status) {
120 	case 0:
121 		if (c >= 0 && c < 0x80) { /* latin */
122 			CK((*filter->output_function)(c, filter->data));
123 		} else if (c > 0x80 && c < 0xff) { /* dbcs/qbcs lead byte */
124 			filter->status = 1;
125 			filter->cache = c;
126 		} else {
127 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
128 		}
129 		break;
130 
131 	case 1: /* dbcs/qbcs second byte */
132 		c1 = filter->cache;
133 		filter->status = 0;
134 
135 		if (c1 >= 0x81 && c1 <= 0x84 && c >= 0x30 && c <= 0x39) {
136 			/* 4 byte range: Unicode BMP */
137 			filter->status = 2;
138 			filter->cache = (c1 << 8) | c;
139 			return 0;
140 		} else if (c1 >= 0x90 && c1 <= 0xe3 && c >= 0x30 && c <= 0x39) {
141 			/* 4 byte range: Unicode 16 planes */
142 			filter->status = 2;
143 			filter->cache = (c1 << 8) | c;
144 			return 0;
145 		} else if (((c1 >= 0xaa && c1 <= 0xaf) || (c1 >= 0xf8 && c1 <= 0xfe)) && (c >= 0xa1 && c <= 0xfe)) {
146 			/* UDA part 1,2: U+E000-U+E4C5 */
147 			w = 94*(c1 >= 0xf8 ? c1 - 0xf2 : c1 - 0xaa) + (c - 0xa1) + 0xe000;
148 			CK((*filter->output_function)(w, filter->data));
149 		} else if (c1 >= 0xa1 && c1 <= 0xa7 && c >= 0x40 && c < 0xa1 && c != 0x7f) {
150 			/* UDA part3 : U+E4C6-U+E765*/
151 			w = 96*(c1 - 0xa1) + c - (c >= 0x80 ? 0x41 : 0x40) + 0xe4c6;
152 			CK((*filter->output_function)(w, filter->data));
153 		}
154 
155 		c2 = (c1 << 8) | c;
156 
157 		if (w <= 0 &&
158 			((c2 >= 0xa2ab && c2 <= 0xa9f0 + (0xe80f-0xe801)) ||
159 			 (c2 >= 0xd7fa && c2 <= 0xd7fa + (0xe814-0xe810)) ||
160 			 (c2 >= 0xfe50 && c2 <= 0xfe80 + (0xe864-0xe844)))) {
161 			for (k = 0; k < mbfl_gb18030_pua_tbl_max; k++) {
162 				if (c2 >= mbfl_gb18030_pua_tbl[k][2] && c2 <= mbfl_gb18030_pua_tbl[k][2] + mbfl_gb18030_pua_tbl[k][1] - mbfl_gb18030_pua_tbl[k][0]) {
163 					w = c2 - mbfl_gb18030_pua_tbl[k][2] + mbfl_gb18030_pua_tbl[k][0];
164 					CK((*filter->output_function)(w, filter->data));
165 					break;
166 				}
167 			}
168 		}
169 
170 		if (w <= 0) {
171 			if ((c1 >= 0xa1 && c1 <= 0xa9 && c >= 0xa1 && c <= 0xfe) ||
172 				(c1 >= 0xb0 && c1 <= 0xf7 && c >= 0xa1 && c <= 0xfe) ||
173 				(c1 >= 0x81 && c1 <= 0xa0 && c >= 0x40 && c <= 0xfe && c != 0x7f) ||
174 				(c1 >= 0xaa && c1 <= 0xfe && c >= 0x40 && c <= 0xa0 && c != 0x7f) ||
175 				(c1 >= 0xa8 && c1 <= 0xa9 && c >= 0x40 && c <= 0xa0 && c != 0x7f)) {
176 				w = (c1 - 0x81)*192 + c - 0x40;
177 				ZEND_ASSERT(w < cp936_ucs_table_size);
178 				CK((*filter->output_function)(cp936_ucs_table[w], filter->data));
179 			} else {
180 				CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
181 			}
182 		}
183 		break;
184 
185 	case 2: /* qbcs third byte */
186 		c1 = (filter->cache >> 8) & 0xff;
187 		c2 = filter->cache & 0xff;
188 		filter->status = filter->cache = 0;
189 		if (((c1 >= 0x81 && c1 <= 0x84) || (c1 >= 0x90 && c1 <= 0xe3)) && c2 >= 0x30 && c2 <= 0x39 && c >= 0x81 && c <= 0xfe) {
190 			filter->cache = (c1 << 16) | (c2 << 8) | c;
191 			filter->status = 3;
192 		} else {
193 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
194 		}
195  		break;
196 
197 	case 3: /* qbcs fourth byte */
198 		c1 = (filter->cache >> 16) & 0xff;
199 		c2 = (filter->cache >> 8) & 0xff;
200 		c3 = filter->cache & 0xff;
201 		filter->status = filter->cache = 0;
202 		if (((c1 >= 0x81 && c1 <= 0x84) || (c1 >= 0x90 && c1 <= 0xe3)) && c2 >= 0x30 && c2 <= 0x39 && c3 >= 0x81 && c3 <= 0xfe && c >= 0x30 && c <= 0x39) {
203 			if (c1 >= 0x90 && c1 <= 0xe3) {
204 				w = ((((c1 - 0x90)*10 + (c2 - 0x30))*126 + (c3 - 0x81)))*10 + (c - 0x30) + 0x10000;
205 				if (w > 0x10FFFF) {
206 					CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
207 					return 0;
208 				}
209 			} else { /* Unicode BMP */
210 				w = (((c1 - 0x81)*10 + (c2 - 0x30))*126 + (c3 - 0x81))*10 + (c - 0x30);
211 				if (w >= 0 && w <= 39419) {
212 					k = mbfl_bisec_srch(w, mbfl_gb2uni_tbl, mbfl_gb_uni_max);
213 					w += mbfl_gb_uni_ofst[k];
214 				} else {
215 					CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
216 					return 0;
217 				}
218 			}
219 			CK((*filter->output_function)(w, filter->data));
220 		} else {
221 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
222 		}
223  		break;
224 
225 		EMPTY_SWITCH_DEFAULT_CASE();
226 	}
227 
228 	return 0;
229 }
230 
mbfl_filt_conv_gb18030_wchar_flush(mbfl_convert_filter * filter)231 static int mbfl_filt_conv_gb18030_wchar_flush(mbfl_convert_filter *filter)
232 {
233 	if (filter->status) {
234 		/* multi-byte character was truncated */
235 		filter->status = 0;
236 		CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
237 	}
238 
239 	if (filter->flush_function) {
240 		(*filter->flush_function)(filter->data);
241 	}
242 
243 	return 0;
244 }
245 
mbfl_filt_conv_wchar_gb18030(int c,mbfl_convert_filter * filter)246 int mbfl_filt_conv_wchar_gb18030(int c, mbfl_convert_filter *filter)
247 {
248 	int k, k1, k2;
249 	int c1, s = 0, s1 = 0;
250 
251 	if (c >= ucs_a1_cp936_table_min && c < ucs_a1_cp936_table_max) {
252 		if (c == 0x01f9) {
253 			s = 0xa8bf;
254 		} else {
255 			s = ucs_a1_cp936_table[c - ucs_a1_cp936_table_min];
256 		}
257 	} else if (c >= ucs_a2_cp936_table_min && c < ucs_a2_cp936_table_max) {
258 		if (c == 0x20ac) { /* euro-sign */
259 			s = 0xa2e3;
260 		} else {
261 			s = ucs_a2_cp936_table[c - ucs_a2_cp936_table_min];
262 		}
263 	} else if (c >= ucs_a3_cp936_table_min && c < ucs_a3_cp936_table_max) {
264 		s = ucs_a3_cp936_table[c - ucs_a3_cp936_table_min];
265 	} else if (c >= ucs_i_cp936_table_min && c < ucs_i_cp936_table_max) {
266 		s = ucs_i_cp936_table[c - ucs_i_cp936_table_min];
267 	} else if (c >= ucs_ci_cp936_table_min && c < ucs_ci_cp936_table_max) {
268 		/* U+F900-FA2F CJK Compatibility Ideographs */
269 		if (c == 0xf92c) {
270 			s = 0xfd9c;
271 		} else if (c == 0xf979) {
272 			s = 0xfd9d;
273 		} else if (c == 0xf995) {
274 			s = 0xfd9e;
275 		} else if (c == 0xf9e7) {
276 			s = 0xfd9f;
277 		} else if (c == 0xf9f1) {
278 			s = 0xfda0;
279 		} else if (c >= 0xfa0c && c <= 0xfa29) {
280 			s = ucs_ci_s_cp936_table[c - 0xfa0c];
281 		}
282 	} else if (c >= ucs_cf_cp936_table_min && c < ucs_cf_cp936_table_max) {
283 		/* FE30h CJK Compatibility Forms  */
284 		s = ucs_cf_cp936_table[c - ucs_cf_cp936_table_min];
285 	} else if (c >= ucs_sfv_cp936_table_min && c < ucs_sfv_cp936_table_max) {
286 		/* U+FE50-FE6F Small Form Variants */
287 		s = ucs_sfv_cp936_table[c - ucs_sfv_cp936_table_min];
288 	} else if (c >= ucs_hff_cp936_table_min && c < ucs_hff_cp936_table_max) {
289 		/* U+FF00-FFFF HW/FW Forms */
290 		if (c == 0xff04) {
291 			s = 0xa1e7;
292 		} else if (c == 0xff5e) {
293 			s = 0xa1ab;
294 		} else if (c >= 0xff01 && c <= 0xff5d) {
295 			s = c - 0xff01 + 0xa3a1;
296 		} else if (c >= 0xffe0 && c <= 0xffe5) {
297 			s = ucs_hff_s_cp936_table[c-0xffe0];
298 		}
299 	}
300 
301 	/* While GB18030 and CP936 are very similar, some mappings are different between these encodings;
302 	 * do a binary search in a table of differing codepoints to see if we have one */
303 	if (s <= 0 && c >= mbfl_gb18030_c_tbl_key[0] && c <= mbfl_gb18030_c_tbl_key[mbfl_gb18030_c_tbl_max-1]) {
304 		k1 = mbfl_bisec_srch2(c, mbfl_gb18030_c_tbl_key, mbfl_gb18030_c_tbl_max);
305 		if (k1 >= 0) {
306 			s = mbfl_gb18030_c_tbl_val[k1];
307 		}
308 	}
309 
310 	if (c >= 0xe000 && c <= 0xe864) { /* PUA */
311 		if (c < 0xe766) {
312 			if (c < 0xe4c6) {
313 				c1 = c - 0xe000;
314 				s = (c1 % 94) + 0xa1;
315 				c1 /= 94;
316 				s |= (c1 < 0x06 ? c1 + 0xaa : c1 + 0xf2) << 8;
317 			} else {
318 				c1 = c - 0xe4c6;
319 				s = ((c1 / 96) + 0xa1) << 8;
320 				c1 %= 96;
321 				s |= c1 + (c1 >= 0x3f ? 0x41 : 0x40);
322 			}
323 		} else {
324 			/* U+E766..U+E864 */
325 			k1 = 0;
326 			k2 = mbfl_gb18030_pua_tbl_max;
327 			while (k1 < k2) {
328 				k = (k1 + k2) >> 1;
329 				if (c < mbfl_gb18030_pua_tbl[k][0]) {
330 					k2 = k;
331 				} else if (c > mbfl_gb18030_pua_tbl[k][1]) {
332 					k1 = k + 1;
333 				} else {
334 					s = c - mbfl_gb18030_pua_tbl[k][0] + mbfl_gb18030_pua_tbl[k][2];
335 					break;
336 				}
337 			}
338 		}
339 	}
340 
341 	/* If we have not yet found a suitable mapping for this codepoint, it requires a 4-byte code */
342 	if (s <= 0 && c >= 0x0080 && c <= 0xffff) {
343 		/* BMP */
344 		s = mbfl_bisec_srch(c, mbfl_uni2gb_tbl, mbfl_gb_uni_max);
345 		if (s >= 0) {
346 			c1 = c - mbfl_gb_uni_ofst[s];
347 			s = (c1 % 10) + 0x30;
348 			c1 /= 10;
349 			s |= ((c1 % 126) + 0x81) << 8;
350 			c1 /= 126;
351 			s |= ((c1 % 10) + 0x30) << 16;
352 			c1 /= 10;
353 			s1 = c1 + 0x81;
354 		}
355 	} else if (c >= 0x10000 && c <= 0x10ffff) {
356 		/* Code set 3: Unicode U+10000..U+10FFFF */
357 		c1 = c - 0x10000;
358 		s = (c1 % 10) + 0x30;
359 		c1 /= 10;
360 		s |= ((c1 % 126) + 0x81) << 8;
361 		c1 /= 126;
362 		s |= ((c1 % 10) + 0x30) << 16;
363 		c1 /= 10;
364 		s1 = c1 + 0x90;
365 	}
366 
367 	if (c == 0) {
368 		s = 0;
369 	} else if (s == 0) {
370 		s = -1;
371 	}
372 
373 	if (s >= 0) {
374 		if (s <= 0x80) { /* latin */
375 			CK((*filter->output_function)(s, filter->data));
376 		} else if (s1 > 0) { /* qbcs */
377 			CK((*filter->output_function)(s1 & 0xff, filter->data));
378 			CK((*filter->output_function)((s >> 16) & 0xff, filter->data));
379 			CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
380 			CK((*filter->output_function)(s & 0xff, filter->data));
381 		} else { /* dbcs */
382 			CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
383 			CK((*filter->output_function)(s & 0xff, filter->data));
384 		}
385 	} else {
386 		CK(mbfl_filt_conv_illegal_output(c, filter));
387 	}
388 
389 	return 0;
390 }
391 
mb_gb18030_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)392 static size_t mb_gb18030_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
393 {
394 	unsigned char *p = *in, *e = p + *in_len;
395 	uint32_t *out = buf, *limit = buf + bufsize;
396 
397 	while (p < e && out < limit) {
398 		unsigned char c = *p++;
399 
400 		if (c < 0x80) {
401 			*out++ = c;
402 		} else if (c > 0x80 && c < 0xFF && p < e) {
403 			unsigned char c2 = *p++;
404 			unsigned int s = (c << 8) | c2;
405 
406 			if (((c >= 0x81 && c <= 0x84) || (c >= 0x90 && c <= 0xE3)) && c2 >= 0x30 && c2 <= 0x39) {
407 				if (p >= e) {
408 					*out++ = MBFL_BAD_INPUT;
409 					break;
410 				}
411 				unsigned char c3 = *p++;
412 
413 				if (c3 >= 0x81 && c3 <= 0xFE && p < e) {
414 					unsigned char c4 = *p++;
415 
416 					if (c4 >= 0x30 && c4 <= 0x39) {
417 						if (c >= 0x90 && c <= 0xE3) {
418 							unsigned int w = ((((c - 0x90)*10 + (c2 - 0x30))*126 + (c3 - 0x81)))*10 + (c4 - 0x30) + 0x10000;
419 							*out++ = (w > 0x10FFFF) ? MBFL_BAD_INPUT : w;
420 						} else {
421 							/* Unicode BMP */
422 							unsigned int w = (((c - 0x81)*10 + (c2 - 0x30))*126 + (c3 - 0x81))*10 + (c4 - 0x30);
423 							if (w <= 39419) {
424 								*out++ = w + mbfl_gb_uni_ofst[mbfl_bisec_srch(w, mbfl_gb2uni_tbl, mbfl_gb_uni_max)];
425 							} else {
426 								*out++ = MBFL_BAD_INPUT;
427 							}
428 						}
429 					} else {
430 						*out++ = MBFL_BAD_INPUT;
431 					}
432 				} else {
433 					*out++ = MBFL_BAD_INPUT;
434 				}
435 			} else if (((c >= 0xAA && c <= 0xAF) || (c >= 0xF8 && c <= 0xFE)) && (c2 >= 0xA1 && c2 <= 0xFE)) {
436 				/* UDA part 1, 2: U+E000-U+E4C5 */
437 				*out++ = 94*(c >= 0xF8 ? c - 0xF2 : c - 0xAA) + (c2 - 0xA1) + 0xE000;
438 			} else if (c >= 0xA1 && c <= 0xA7 && c2 >= 0x40 && c2 < 0xA1 && c2 != 0x7F) {
439 				/* UDA part 3: U+E4C6-U+E765 */
440 				*out++ = 96*(c - 0xA1) + c2 - (c2 >= 0x80 ? 0x41 : 0x40) + 0xE4C6;
441 			} else {
442 				if ((s >= 0xA2AB && s <= 0xA9FE) || (s >= 0xD7FA && s <= 0xD7FE) || (s >= 0xFE50 && s <= 0xFEA0)) {
443 					for (int i = 0; i < mbfl_gb18030_pua_tbl_max; i++) {
444 						if (s >= mbfl_gb18030_pua_tbl[i][2] && s <= mbfl_gb18030_pua_tbl[i][2] + mbfl_gb18030_pua_tbl[i][1] - mbfl_gb18030_pua_tbl[i][0]) {
445 							*out++ = s - mbfl_gb18030_pua_tbl[i][2] + mbfl_gb18030_pua_tbl[i][0];
446 							goto next_iteration;
447 						}
448 					}
449 				}
450 
451 				if ((c >= 0xA1 && c <= 0xA9 && c2 >= 0xA1 && c2 <= 0xFE) ||
452 					(c >= 0xB0 && c <= 0xf7 && c2 >= 0xa1 && c2 <= 0xfe) ||
453 					(c >= 0x81 && c <= 0xa0 && c2 >= 0x40 && c2 <= 0xfe && c2 != 0x7f) ||
454 					(c >= 0xAA && c <= 0xfe && c2 >= 0x40 && c2 <= 0xa0 && c2 != 0x7f) ||
455 					(c >= 0xA8 && c <= 0xa9 && c2 >= 0x40 && c2 <= 0xa0 && c2 != 0x7F)) {
456 					unsigned int w = (c - 0x81)*192 + c2 - 0x40;
457 					ZEND_ASSERT(w < cp936_ucs_table_size);
458 					*out++ = cp936_ucs_table[w];
459 				} else {
460 					*out++ = MBFL_BAD_INPUT;
461 				}
462 			}
463 		} else {
464 			*out++ = MBFL_BAD_INPUT;
465 		}
466 next_iteration: ;
467 	}
468 
469 	*in_len = e - p;
470 	*in = p;
471 	return out - buf;
472 }
473 
mb_wchar_to_gb18030(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)474 static void mb_wchar_to_gb18030(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
475 {
476 	unsigned char *out, *limit;
477 	MB_CONVERT_BUF_LOAD(buf, out, limit);
478 	MB_CONVERT_BUF_ENSURE(buf, out, limit, len);
479 
480 	while (len--) {
481 		uint32_t w = *in++;
482 		unsigned int s = 0;
483 
484 		if (w == 0) {
485 			out = mb_convert_buf_add(out, 0);
486 			continue;
487 		} else if (w >= ucs_a1_cp936_table_min && w < ucs_a1_cp936_table_max) {
488 			if (w == 0x1F9) {
489 				s = 0xA8Bf;
490 			} else {
491 				s = ucs_a1_cp936_table[w - ucs_a1_cp936_table_min];
492 			}
493 		} else if (w >= ucs_a2_cp936_table_min && w < ucs_a2_cp936_table_max) {
494 			if (w == 0x20AC) { /* Euro sign */
495 				s = 0xA2E3;
496 			} else {
497 				s = ucs_a2_cp936_table[w - ucs_a2_cp936_table_min];
498 			}
499 		} else if (w >= ucs_a3_cp936_table_min && w < ucs_a3_cp936_table_max) {
500 			s = ucs_a3_cp936_table[w - ucs_a3_cp936_table_min];
501 		} else if (w >= ucs_i_cp936_table_min && w < ucs_i_cp936_table_max) {
502 			s = ucs_i_cp936_table[w - ucs_i_cp936_table_min];
503 		} else if (w >= ucs_ci_cp936_table_min && w < ucs_ci_cp936_table_max) {
504 			/* U+F900-U+FA2F CJK Compatibility Ideographs */
505 			if (w == 0xF92C) {
506 				s = 0xFD9C;
507 			} else if (w == 0xF979) {
508 				s = 0xFD9D;
509 			} else if (w == 0xF995) {
510 				s = 0xFD9E;
511 			} else if (w == 0xF9E7) {
512 				s = 0xFD9F;
513 			} else if (w == 0xF9F1) {
514 				s = 0xFDA0;
515 			} else if (w >= 0xFA0C && w <= 0xFA29) {
516 				s = ucs_ci_s_cp936_table[w - 0xFA0C];
517 			}
518 		} else if (w >= ucs_cf_cp936_table_min && w < ucs_cf_cp936_table_max) {
519 			/* CJK Compatibility Forms  */
520 			s = ucs_cf_cp936_table[w - ucs_cf_cp936_table_min];
521 		} else if (w >= ucs_sfv_cp936_table_min && w < ucs_sfv_cp936_table_max) {
522 			/* U+FE50-U+FE6F Small Form Variants */
523 			s = ucs_sfv_cp936_table[w - ucs_sfv_cp936_table_min];
524 		} else if (w >= ucs_hff_cp936_table_min && w < ucs_hff_cp936_table_max) {
525 			/* U+FF00-U+FFFF HW/FW Forms */
526 			if (w == 0xFF04) {
527 				s = 0xA1E7;
528 			} else if (w == 0xFF5E) {
529 				s = 0xA1AB;
530 			} else if (w >= 0xFF01 && w <= 0xFF5D) {
531 				s = w - 0xFF01 + 0xA3A1;
532 			} else if (w >= 0xFFE0 && w <= 0xFFE5) {
533 				s = ucs_hff_s_cp936_table[w - 0xFFE0];
534 			}
535 		} else if (w >= 0xE000 && w <= 0xE864) {
536 			/* PUA */
537 			if (w < 0xE766) {
538 				if (w < 0xE4C6) {
539 					unsigned int c1 = w - 0xE000;
540 					s = (c1 % 94) + 0xA1;
541 					c1 /= 94;
542 					s |= (c1 + (c1 < 0x06 ? 0xAA : 0xF2)) << 8;
543 				} else {
544 					unsigned int c1 = w - 0xE4C6;
545 					s = ((c1 / 96) + 0xA1) << 8;
546 					c1 %= 96;
547 					s |= c1 + (c1 >= 0x3F ? 0x41 : 0x40);
548 				}
549 			} else {
550 				/* U+E766-U+E864 */
551 				unsigned int k1 = 0, k2 = mbfl_gb18030_pua_tbl_max;
552 				while (k1 < k2) {
553 					unsigned int k = (k1 + k2) >> 1;
554 					if (w < mbfl_gb18030_pua_tbl[k][0]) {
555 						k2 = k;
556 					} else if (w > mbfl_gb18030_pua_tbl[k][1]) {
557 						k1 = k + 1;
558 					} else {
559 						s = w - mbfl_gb18030_pua_tbl[k][0] + mbfl_gb18030_pua_tbl[k][2];
560 						break;
561 					}
562 				}
563 			}
564 		}
565 
566 		/* While GB18030 and CP936 are very similar, some mappings are different between these encodings;
567 		 * do a binary search in a table of differing codepoints to see if we have one */
568 		if (!s && w >= mbfl_gb18030_c_tbl_key[0] && w <= mbfl_gb18030_c_tbl_key[mbfl_gb18030_c_tbl_max-1]) {
569 			int i = mbfl_bisec_srch2(w, mbfl_gb18030_c_tbl_key, mbfl_gb18030_c_tbl_max);
570 			if (i >= 0) {
571 				s = mbfl_gb18030_c_tbl_val[i];
572 			}
573 		}
574 
575 		/* If we have not yet found a suitable mapping for this codepoint, it requires a 4-byte code */
576 		if (!s && w >= 0x80 && w <= 0xFFFF) {
577 			/* BMP */
578 			int i = mbfl_bisec_srch(w, mbfl_uni2gb_tbl, mbfl_gb_uni_max);
579 			if (i >= 0) {
580 				unsigned int c1 = w - mbfl_gb_uni_ofst[i];
581 				s = (c1 % 10) + 0x30;
582 				c1 /= 10;
583 				s |= ((c1 % 126) + 0x81) << 8;
584 				c1 /= 126;
585 				s |= ((c1 % 10) + 0x30) << 16;
586 				c1 /= 10;
587 				s |= (c1 + 0x81) << 24;
588 			}
589 		} else if (w >= 0x10000 && w <= 0x10FFFF) {
590 			/* Code set 3: Unicode U+10000-U+10FFFF */
591 			unsigned int c1 = w - 0x10000;
592 			s = (c1 % 10) + 0x30;
593 			c1 /= 10;
594 			s |= ((c1 % 126) + 0x81) << 8;
595 			c1 /= 126;
596 			s |= ((c1 % 10) + 0x30) << 16;
597 			c1 /= 10;
598 			s |= (c1 + 0x90) << 24;
599 		}
600 
601 		if (!s) {
602 			MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_gb18030);
603 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len);
604 		} else if (s < 0x80) {
605 			out = mb_convert_buf_add(out, s);
606 		} else if (s > 0xFFFFFF) {
607 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len + 4);
608 			out = mb_convert_buf_add4(out, (s >> 24) & 0xFF, (s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
609 		} else {
610 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len + 2);
611 			out = mb_convert_buf_add2(out, (s >> 8) & 0xFF, s & 0xFF);
612 		}
613 	}
614 
615 	MB_CONVERT_BUF_STORE(buf, out, limit);
616 }
617