• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <iconv.h>
2 #include <errno.h>
3 #include <wchar.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <limits.h>
7 #include <stdint.h>
8 #include "locale_impl.h"
9 
10 #define UTF_32BE    0300
11 #define UTF_16LE    0301
12 #define UTF_16BE    0302
13 #define UTF_32LE    0303
14 #define UCS2BE      0304
15 #define UCS2LE      0305
16 #define WCHAR_T     0306
17 #define US_ASCII    0307
18 #define UTF_8       0310
19 #define UTF_16      0312
20 #define UTF_32      0313
21 #define UCS2        0314
22 #define EUC_JP      0320
23 #define SHIFT_JIS   0321
24 #define ISO2022_JP  0322
25 #define GB18030     0330
26 #define GBK         0331
27 #define GB2312      0332
28 #define BIG5        0340
29 #define EUC_KR      0350
30 
31 /* Definitions of charmaps. Each charmap consists of:
32  * 1. Empty-string-terminated list of null-terminated aliases.
33  * 2. Special type code or number of elided quads of entries.
34  * 3. Character table (size determined by field 2), consisting
35  *    of 5 bytes for every 4 characters, interpreted as 10-bit
36  *    indices into the legacy_chars table. */
37 
38 static const unsigned char charmaps[] =
39 "utf8\0char\0\0\310"
40 "wchart\0\0\306"
41 "ucs2be\0\0\304"
42 "ucs2le\0\0\305"
43 "utf16be\0\0\302"
44 "utf16le\0\0\301"
45 "ucs4be\0utf32be\0\0\300"
46 "ucs4le\0utf32le\0\0\303"
47 "ascii\0usascii\0iso646\0iso646us\0\0\307"
48 "utf16\0\0\312"
49 "ucs4\0utf32\0\0\313"
50 "ucs2\0\0\314"
51 "eucjp\0\0\320"
52 "shiftjis\0sjis\0\0\321"
53 "iso2022jp\0\0\322"
54 "gb18030\0\0\330"
55 "gbk\0\0\331"
56 "gb2312\0\0\332"
57 "big5\0bigfive\0cp950\0big5hkscs\0\0\340"
58 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
59 #include "codepages.h"
60 ;
61 
62 /* Table of characters that appear in legacy 8-bit codepages,
63  * limited to 1024 slots (10 bit indices). The first 256 entries
64  * are elided since those characters are obviously all included. */
65 static const unsigned short legacy_chars[] = {
66 #include "legacychars.h"
67 };
68 
69 static const unsigned short jis0208[84][94] = {
70 #include "jis0208.h"
71 };
72 
73 static const unsigned short gb18030[126][190] = {
74 #include "gb18030.h"
75 };
76 
77 static const unsigned short big5[89][157] = {
78 #include "big5.h"
79 };
80 
81 static const unsigned short hkscs[] = {
82 #include "hkscs.h"
83 };
84 
85 static const unsigned short ksc[93][94] = {
86 #include "ksc.h"
87 };
88 
89 static const unsigned short rev_jis[] = {
90 #include "revjis.h"
91 };
92 
fuzzycmp(const unsigned char * a,const unsigned char * b)93 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
94 {
95 	for (; *a && *b; a++, b++) {
96 		while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
97 		if ((*a|32U) != *b) return 1;
98 	}
99 	return *a != *b;
100 }
101 
find_charmap(const void * name)102 static size_t find_charmap(const void *name)
103 {
104 	const unsigned char *s;
105 	if (!*(char *)name) name=charmaps; /* "utf8" */
106 	for (s=charmaps; *s; ) {
107 		if (!fuzzycmp(name, s)) {
108 			for (; *s; s+=strlen((void *)s)+1);
109 			return s+1-charmaps;
110 		}
111 		s += strlen((void *)s)+1;
112 		if (!*s) {
113 			if (s[1] > 0200) s+=2;
114 			else s+=2+(64U-s[1])*5;
115 		}
116 	}
117 	return -1;
118 }
119 
120 struct stateful_cd {
121 	iconv_t base_cd;
122 	unsigned state;
123 };
124 
combine_to_from(size_t t,size_t f)125 static iconv_t combine_to_from(size_t t, size_t f)
126 {
127 	return (void *)(f<<16 | t<<1 | 1);
128 }
129 
extract_from(iconv_t cd)130 static size_t extract_from(iconv_t cd)
131 {
132 	return (size_t)cd >> 16;
133 }
134 
extract_to(iconv_t cd)135 static size_t extract_to(iconv_t cd)
136 {
137 	return (size_t)cd >> 1 & 0x7fff;
138 }
139 
iconv_open(const char * to,const char * from)140 iconv_t iconv_open(const char *to, const char *from)
141 {
142 	size_t f, t;
143 	struct stateful_cd *scd;
144 
145 	if ((t = find_charmap(to))==-1
146 	 || (f = find_charmap(from))==-1
147 	 || (charmaps[t] >= 0330)) {
148 		errno = EINVAL;
149 		return (iconv_t)-1;
150 	}
151 	iconv_t cd = combine_to_from(t, f);
152 
153 	switch (charmaps[f]) {
154 	case UTF_16:
155 	case UTF_32:
156 	case UCS2:
157 	case ISO2022_JP:
158 		scd = malloc(sizeof *scd);
159 		if (!scd) return (iconv_t)-1;
160 		scd->base_cd = cd;
161 		scd->state = 0;
162 		cd = (iconv_t)scd;
163 	}
164 
165 	return cd;
166 }
167 
get_16(const unsigned char * s,int e)168 static unsigned get_16(const unsigned char *s, int e)
169 {
170 	e &= 1;
171 	return s[e]<<8 | s[1-e];
172 }
173 
put_16(unsigned char * s,unsigned c,int e)174 static void put_16(unsigned char *s, unsigned c, int e)
175 {
176 	e &= 1;
177 	s[e] = c>>8;
178 	s[1-e] = c;
179 }
180 
get_32(const unsigned char * s,int e)181 static unsigned get_32(const unsigned char *s, int e)
182 {
183 	e &= 3;
184 	return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
185 }
186 
put_32(unsigned char * s,unsigned c,int e)187 static void put_32(unsigned char *s, unsigned c, int e)
188 {
189 	e &= 3;
190 	s[e^0] = c>>24;
191 	s[e^1] = c>>16;
192 	s[e^2] = c>>8;
193 	s[e^3] = c;
194 }
195 
196 /* Adapt as needed */
197 #define mbrtowc_utf8 mbrtowc
198 #define wctomb_utf8 wctomb
199 
legacy_map(const unsigned char * map,unsigned c)200 static unsigned legacy_map(const unsigned char *map, unsigned c)
201 {
202 	if (c < 4*map[-1]) return c;
203 	unsigned x = c - 4*map[-1];
204 	x = map[x*5/4]>>2*x%8 | map[x*5/4+1]<<8-2*x%8 & 1023;
205 	return x < 256 ? x : legacy_chars[x-256];
206 }
207 
uni_to_jis(unsigned c)208 static unsigned uni_to_jis(unsigned c)
209 {
210 	unsigned nel = sizeof rev_jis / sizeof *rev_jis;
211 	unsigned d, j, i, b = 0;
212 	for (;;) {
213 		i = nel/2;
214 		j = rev_jis[b+i];
215 		d = jis0208[j/256][j%256];
216 		if (d==c) return j + 0x2121;
217 		else if (nel == 1) return 0;
218 		else if (c < d)
219 			nel /= 2;
220 		else {
221 			b += i;
222 			nel -= nel/2;
223 		}
224 	}
225 }
226 
iconv(iconv_t cd,char ** restrict in,size_t * restrict inb,char ** restrict out,size_t * restrict outb)227 size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
228 {
229 	size_t x=0;
230 	struct stateful_cd *scd=0;
231 	if (!((size_t)cd & 1)) {
232 		scd = (void *)cd;
233 		cd = scd->base_cd;
234 	}
235 	unsigned to = extract_to(cd);
236 	unsigned from = extract_from(cd);
237 	const unsigned char *map = charmaps+from+1;
238 	const unsigned char *tomap = charmaps+to+1;
239 	mbstate_t st = {0};
240 	wchar_t wc;
241 	unsigned c, d;
242 	size_t k, l;
243 	int err;
244 	unsigned char type = map[-1];
245 	unsigned char totype = tomap[-1];
246 	locale_t locale = CURRENT_LOCALE;
247 	locale_t *ploc = &locale, loc = *ploc;
248 
249 	if (!in || !*in || !*inb) return 0;
250 
251 	*ploc = UTF8_LOCALE;
252 
253 	for (; *inb; *in+=l, *inb-=l) {
254 		c = *(unsigned char *)*in;
255 		l = 1;
256 
257 		switch (type) {
258 		case UTF_8:
259 			if (c < 128) break;
260 			l = mbrtowc_utf8(&wc, *in, *inb, &st);
261 			if (l == (size_t)-1) goto ilseq;
262 			if (l == (size_t)-2) goto starved;
263 			c = wc;
264 			break;
265 		case US_ASCII:
266 			if (c >= 128) goto ilseq;
267 			break;
268 		case WCHAR_T:
269 			l = sizeof(wchar_t);
270 			if (*inb < l) goto starved;
271 			c = *(wchar_t *)*in;
272 			if (0) {
273 		case UTF_32BE:
274 		case UTF_32LE:
275 			l = 4;
276 			if (*inb < 4) goto starved;
277 			c = get_32((void *)*in, type);
278 			}
279 			if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
280 			break;
281 		case UCS2BE:
282 		case UCS2LE:
283 		case UTF_16BE:
284 		case UTF_16LE:
285 			l = 2;
286 			if (*inb < 2) goto starved;
287 			c = get_16((void *)*in, type);
288 			if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
289 			if ((unsigned)(c-0xd800) < 0x400) {
290 				if (type-UCS2BE < 2U) goto ilseq;
291 				l = 4;
292 				if (*inb < 4) goto starved;
293 				d = get_16((void *)(*in + 2), type);
294 				if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
295 				c = ((c-0xd7c0)<<10) + (d-0xdc00);
296 			}
297 			break;
298 		case UCS2:
299 		case UTF_16:
300 			l = 0;
301 			if (!scd->state) {
302 				if (*inb < 2) goto starved;
303 				c = get_16((void *)*in, 0);
304 				scd->state = type==UCS2
305 					? c==0xfffe ? UCS2LE : UCS2BE
306 					: c==0xfffe ? UTF_16LE : UTF_16BE;
307 				if (c == 0xfffe || c == 0xfeff)
308 					l = 2;
309 			}
310 			type = scd->state;
311 			continue;
312 		case UTF_32:
313 			l = 0;
314 			if (!scd->state) {
315 				if (*inb < 4) goto starved;
316 				c = get_32((void *)*in, 0);
317 				scd->state = c==0xfffe0000 ? UTF_32LE : UTF_32BE;
318 				if (c == 0xfffe0000 || c == 0xfeff)
319 					l = 4;
320 			}
321 			type = scd->state;
322 			continue;
323 		case SHIFT_JIS:
324 			if (c < 128) break;
325 			if (c-0xa1 <= 0xdf-0xa1) {
326 				c += 0xff61-0xa1;
327 				break;
328 			}
329 			l = 2;
330 			if (*inb < 2) goto starved;
331 			d = *((unsigned char *)*in + 1);
332 			if (c-129 <= 159-129) c -= 129;
333 			else if (c-224 <= 239-224) c -= 193;
334 			else goto ilseq;
335 			c *= 2;
336 			if (d-64 <= 158-64) {
337 				if (d==127) goto ilseq;
338 				if (d>127) d--;
339 				d -= 64;
340 			} else if (d-159 <= 252-159) {
341 				c++;
342 				d -= 159;
343 			}
344 			c = jis0208[c][d];
345 			if (!c) goto ilseq;
346 			break;
347 		case EUC_JP:
348 			if (c < 128) break;
349 			l = 2;
350 			if (*inb < 2) goto starved;
351 			d = *((unsigned char *)*in + 1);
352 			if (c==0x8e) {
353 				c = d;
354 				if (c-0xa1 > 0xdf-0xa1) goto ilseq;
355 				c += 0xff61 - 0xa1;
356 				break;
357 			}
358 			c -= 0xa1;
359 			d -= 0xa1;
360 			if (c >= 84 || d >= 94) goto ilseq;
361 			c = jis0208[c][d];
362 			if (!c) goto ilseq;
363 			break;
364 		case ISO2022_JP:
365 			if (c >= 128) goto ilseq;
366 			if (c == '\033') {
367 				l = 3;
368 				if (*inb < 3) goto starved;
369 				c = *((unsigned char *)*in + 1);
370 				d = *((unsigned char *)*in + 2);
371 				if (c != '(' && c != '$') goto ilseq;
372 				switch (128*(c=='$') + d) {
373 				case 'B': scd->state=0; continue;
374 				case 'J': scd->state=1; continue;
375 				case 'I': scd->state=4; continue;
376 				case 128+'@': scd->state=2; continue;
377 				case 128+'B': scd->state=3; continue;
378 				}
379 				goto ilseq;
380 			}
381 			switch (scd->state) {
382 			case 1:
383 				if (c=='\\') c = 0xa5;
384 				if (c=='~') c = 0x203e;
385 				break;
386 			case 2:
387 			case 3:
388 				l = 2;
389 				if (*inb < 2) goto starved;
390 				d = *((unsigned char *)*in + 1);
391 				c -= 0x21;
392 				d -= 0x21;
393 				if (c >= 84 || d >= 94) goto ilseq;
394 				c = jis0208[c][d];
395 				if (!c) goto ilseq;
396 				break;
397 			case 4:
398 				if (c-0x60 < 0x1f) goto ilseq;
399 				if (c-0x21 < 0x5e) c += 0xff61-0x21;
400 				break;
401 			}
402 			break;
403 		case GB2312:
404 			if (c < 128) break;
405 			if (c < 0xa1) goto ilseq;
406 		case GBK:
407 		case GB18030:
408 			if (c < 128) break;
409 			c -= 0x81;
410 			if (c >= 126) goto ilseq;
411 			l = 2;
412 			if (*inb < 2) goto starved;
413 			d = *((unsigned char *)*in + 1);
414 			if (d < 0xa1 && type == GB2312) goto ilseq;
415 			if (d-0x40>=191 || d==127) {
416 				if (d-'0'>9 || type != GB18030)
417 					goto ilseq;
418 				l = 4;
419 				if (*inb < 4) goto starved;
420 				c = (10*c + d-'0') * 1260;
421 				d = *((unsigned char *)*in + 2);
422 				if (d-0x81>126) goto ilseq;
423 				c += 10*(d-0x81);
424 				d = *((unsigned char *)*in + 3);
425 				if (d-'0'>9) goto ilseq;
426 				c += d-'0';
427 				c += 128;
428 				for (d=0; d<=c; ) {
429 					k = 0;
430 					for (int i=0; i<126; i++)
431 						for (int j=0; j<190; j++)
432 							if (gb18030[i][j]-d <= c-d)
433 								k++;
434 					d = c+1;
435 					c += k;
436 				}
437 				break;
438 			}
439 			d -= 0x40;
440 			if (d>63) d--;
441 			c = gb18030[c][d];
442 			break;
443 		case BIG5:
444 			if (c < 128) break;
445 			l = 2;
446 			if (*inb < 2) goto starved;
447 			d = *((unsigned char *)*in + 1);
448 			if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
449 			d -= 0x40;
450 			if (d > 0x3e) d -= 0x22;
451 			if (c-0xa1>=0xfa-0xa1) {
452 				if (c-0x87>=0xff-0x87) goto ilseq;
453 				if (c < 0xa1) c -= 0x87;
454 				else c -= 0x87 + (0xfa-0xa1);
455 				c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
456 					| hkscs[c*157+d];
457 				/* A few HKSCS characters map to pairs of UCS
458 				 * characters. These are mapped to surrogate
459 				 * range in the hkscs table then hard-coded
460 				 * here. Ugly, yes. */
461 				if (c/256 == 0xdc) {
462 					union {
463 						char c[8];
464 						wchar_t wc[2];
465 					} tmp;
466 					char *ptmp = tmp.c;
467 					size_t tmpx = iconv(combine_to_from(to, find_charmap("utf8")),
468 						&(char *){"\303\212\314\204"
469 						"\303\212\314\214"
470 						"\303\252\314\204"
471 						"\303\252\314\214"
472 						+c%256}, &(size_t){4},
473 						&ptmp, &(size_t){sizeof tmp});
474 					size_t tmplen = ptmp - tmp.c;
475 					if (tmplen > *outb) goto toobig;
476 					if (tmpx) x++;
477 					memcpy(*out, &tmp, tmplen);
478 					*out += tmplen;
479 					*outb -= tmplen;
480 					continue;
481 				}
482 				if (!c) goto ilseq;
483 				break;
484 			}
485 			c -= 0xa1;
486 			c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
487 			if (!c) goto ilseq;
488 			break;
489 		case EUC_KR:
490 			if (c < 128) break;
491 			l = 2;
492 			if (*inb < 2) goto starved;
493 			d = *((unsigned char *)*in + 1);
494 			c -= 0xa1;
495 			d -= 0xa1;
496 			if (c >= 93 || d >= 94) {
497 				c += (0xa1-0x81);
498 				d += 0xa1;
499 				if (c >= 93 || c>=0xc6-0x81 && d>0x52)
500 					goto ilseq;
501 				if (d-'A'<26) d = d-'A';
502 				else if (d-'a'<26) d = d-'a'+26;
503 				else if (d-0x81<0xff-0x81) d = d-0x81+52;
504 				else goto ilseq;
505 				if (c < 0x20) c = 178*c + d;
506 				else c = 178*0x20 + 84*(c-0x20) + d;
507 				c += 0xac00;
508 				for (d=0xac00; d<=c; ) {
509 					k = 0;
510 					for (int i=0; i<93; i++)
511 						for (int j=0; j<94; j++)
512 							if (ksc[i][j]-d <= c-d)
513 								k++;
514 					d = c+1;
515 					c += k;
516 				}
517 				break;
518 			}
519 			c = ksc[c][d];
520 			if (!c) goto ilseq;
521 			break;
522 		default:
523 			if (!c) break;
524 			c = legacy_map(map, c);
525 			if (!c) goto ilseq;
526 		}
527 
528 		switch (totype) {
529 		case WCHAR_T:
530 			if (*outb < sizeof(wchar_t)) goto toobig;
531 			*(wchar_t *)*out = c;
532 			*out += sizeof(wchar_t);
533 			*outb -= sizeof(wchar_t);
534 			break;
535 		case UTF_8:
536 			if (*outb < 4) {
537 				char tmp[4];
538 				k = wctomb_utf8(tmp, c);
539 				if (*outb < k) goto toobig;
540 				memcpy(*out, tmp, k);
541 			} else k = wctomb_utf8(*out, c);
542 			*out += k;
543 			*outb -= k;
544 			break;
545 		case US_ASCII:
546 			if (c > 0x7f) subst: x++, c='*';
547 		default:
548 			if (*outb < 1) goto toobig;
549 			if (c<256 && c==legacy_map(tomap, c)) {
550 			revout:
551 				if (*outb < 1) goto toobig;
552 				*(*out)++ = c;
553 				*outb -= 1;
554 				break;
555 			}
556 			d = c;
557 			for (c=4*totype; c<256; c++) {
558 				if (d == legacy_map(tomap, c)) {
559 					goto revout;
560 				}
561 			}
562 			goto subst;
563 		case SHIFT_JIS:
564 			if (c < 128) goto revout;
565 			if (c == 0xa5) {
566 				x++;
567 				c = '\\';
568 				goto revout;
569 			}
570 			if (c == 0x203e) {
571 				x++;
572 				c = '~';
573 				goto revout;
574 			}
575 			if (c-0xff61 <= 0xdf-0xa1) {
576 				c += 0xa1 - 0xff61;
577 				goto revout;
578 			}
579 			c = uni_to_jis(c);
580 			if (!c) goto subst;
581 			if (*outb < 2) goto toobig;
582 			d = c%256;
583 			c = c/256;
584 			*(*out)++ = (c+1)/2 + (c<95 ? 112 : 176);
585 			*(*out)++ = c%2 ? d + 31 + d/96 : d + 126;
586 			*outb -= 2;
587 			break;
588 		case EUC_JP:
589 			if (c < 128) goto revout;
590 			if (c-0xff61 <= 0xdf-0xa1) {
591 				c += 0x0e00 + 0x21 - 0xff61;
592 			} else {
593 				c = uni_to_jis(c);
594 			}
595 			if (!c) goto subst;
596 			if (*outb < 2) goto toobig;
597 			*(*out)++ = c/256 + 0x80;
598 			*(*out)++ = c%256 + 0x80;
599 			*outb -= 2;
600 			break;
601 		case ISO2022_JP:
602 			if (c < 128) goto revout;
603 			if (c-0xff61 <= 0xdf-0xa1 || c==0xa5 || c==0x203e) {
604 				if (*outb < 7) goto toobig;
605 				*(*out)++ = '\033';
606 				*(*out)++ = '(';
607 				if (c==0xa5) {
608 					*(*out)++ = 'J';
609 					*(*out)++ = '\\';
610 				} else if (c==0x203e) {
611 					*(*out)++ = 'J';
612 					*(*out)++ = '~';
613 				} else {
614 					*(*out)++ = 'I';
615 					*(*out)++ = c-0xff61+0x21;
616 				}
617 				*(*out)++ = '\033';
618 				*(*out)++ = '(';
619 				*(*out)++ = 'B';
620 				*outb -= 7;
621 				break;
622 			}
623 			c = uni_to_jis(c);
624 			if (!c) goto subst;
625 			if (*outb < 8) goto toobig;
626 			*(*out)++ = '\033';
627 			*(*out)++ = '$';
628 			*(*out)++ = 'B';
629 			*(*out)++ = c/256;
630 			*(*out)++ = c%256;
631 			*(*out)++ = '\033';
632 			*(*out)++ = '(';
633 			*(*out)++ = 'B';
634 			*outb -= 8;
635 			break;
636 		case UCS2:
637 			totype = UCS2BE;
638 		case UCS2BE:
639 		case UCS2LE:
640 		case UTF_16:
641 		case UTF_16BE:
642 		case UTF_16LE:
643 			if (c < 0x10000 || totype-UCS2BE < 2U) {
644 				if (c >= 0x10000) c = 0xFFFD;
645 				if (*outb < 2) goto toobig;
646 				put_16((void *)*out, c, totype);
647 				*out += 2;
648 				*outb -= 2;
649 				break;
650 			}
651 			if (*outb < 4) goto toobig;
652 			c -= 0x10000;
653 			put_16((void *)*out, (c>>10)|0xd800, totype);
654 			put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
655 			*out += 4;
656 			*outb -= 4;
657 			break;
658 		case UTF_32:
659 			totype = UTF_32BE;
660 		case UTF_32BE:
661 		case UTF_32LE:
662 			if (*outb < 4) goto toobig;
663 			put_32((void *)*out, c, totype);
664 			*out += 4;
665 			*outb -= 4;
666 			break;
667 		}
668 	}
669 	*ploc = loc;
670 	return x;
671 ilseq:
672 	err = EILSEQ;
673 	x = -1;
674 	goto end;
675 toobig:
676 	err = E2BIG;
677 	x = -1;
678 	goto end;
679 starved:
680 	err = EINVAL;
681 	x = -1;
682 end:
683 	errno = err;
684 	*ploc = loc;
685 	return x;
686 }
687