xref: /aosp_15_r20/external/musl/src/locale/iconv.c (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
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\0cp932\0\0\321"
53 "iso2022jp\0\0\322"
54 "gb18030\0\0\330"
55 "gbk\0cp936\0windows936\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 *ploc = &CURRENT_LOCALE, loc = *ploc;
247 
248 	if (!in || !*in || !*inb) return 0;
249 
250 	*ploc = UTF8_LOCALE;
251 
252 	for (; *inb; *in+=l, *inb-=l) {
253 		c = *(unsigned char *)*in;
254 		l = 1;
255 
256 		switch (type) {
257 		case UTF_8:
258 			if (c < 128) break;
259 			l = mbrtowc_utf8(&wc, *in, *inb, &st);
260 			if (l == (size_t)-1) goto ilseq;
261 			if (l == (size_t)-2) goto starved;
262 			c = wc;
263 			break;
264 		case US_ASCII:
265 			if (c >= 128) goto ilseq;
266 			break;
267 		case WCHAR_T:
268 			l = sizeof(wchar_t);
269 			if (*inb < l) goto starved;
270 			c = *(wchar_t *)*in;
271 			if (0) {
272 		case UTF_32BE:
273 		case UTF_32LE:
274 			l = 4;
275 			if (*inb < 4) goto starved;
276 			c = get_32((void *)*in, type);
277 			}
278 			if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
279 			break;
280 		case UCS2BE:
281 		case UCS2LE:
282 		case UTF_16BE:
283 		case UTF_16LE:
284 			l = 2;
285 			if (*inb < 2) goto starved;
286 			c = get_16((void *)*in, type);
287 			if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
288 			if ((unsigned)(c-0xd800) < 0x400) {
289 				if (type-UCS2BE < 2U) goto ilseq;
290 				l = 4;
291 				if (*inb < 4) goto starved;
292 				d = get_16((void *)(*in + 2), type);
293 				if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
294 				c = ((c-0xd7c0)<<10) + (d-0xdc00);
295 			}
296 			break;
297 		case UCS2:
298 		case UTF_16:
299 			l = 0;
300 			if (!scd->state) {
301 				if (*inb < 2) goto starved;
302 				c = get_16((void *)*in, 0);
303 				scd->state = type==UCS2
304 					? c==0xfffe ? UCS2LE : UCS2BE
305 					: c==0xfffe ? UTF_16LE : UTF_16BE;
306 				if (c == 0xfffe || c == 0xfeff)
307 					l = 2;
308 			}
309 			type = scd->state;
310 			continue;
311 		case UTF_32:
312 			l = 0;
313 			if (!scd->state) {
314 				if (*inb < 4) goto starved;
315 				c = get_32((void *)*in, 0);
316 				scd->state = c==0xfffe0000 ? UTF_32LE : UTF_32BE;
317 				if (c == 0xfffe0000 || c == 0xfeff)
318 					l = 4;
319 			}
320 			type = scd->state;
321 			continue;
322 		case SHIFT_JIS:
323 			if (c < 128) break;
324 			if (c-0xa1 <= 0xdf-0xa1) {
325 				c += 0xff61-0xa1;
326 				break;
327 			}
328 			l = 2;
329 			if (*inb < 2) goto starved;
330 			d = *((unsigned char *)*in + 1);
331 			if (c-129 <= 159-129) c -= 129;
332 			else if (c-224 <= 239-224) c -= 193;
333 			else goto ilseq;
334 			c *= 2;
335 			if (d-64 <= 158-64) {
336 				if (d==127) goto ilseq;
337 				if (d>127) d--;
338 				d -= 64;
339 			} else if (d-159 <= 252-159) {
340 				c++;
341 				d -= 159;
342 			}
343 			if (c>=84) goto ilseq;
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 			if (c == 128) {
408 				c = 0x20ac;
409 				break;
410 			}
411 		case GB18030:
412 			if (c < 128) break;
413 			c -= 0x81;
414 			if (c >= 126) goto ilseq;
415 			l = 2;
416 			if (*inb < 2) goto starved;
417 			d = *((unsigned char *)*in + 1);
418 			if (d < 0xa1 && type == GB2312) goto ilseq;
419 			if (d-0x40>=191 || d==127) {
420 				if (d-'0'>9 || type != GB18030)
421 					goto ilseq;
422 				l = 4;
423 				if (*inb < 4) goto starved;
424 				c = (10*c + d-'0') * 1260;
425 				d = *((unsigned char *)*in + 2);
426 				if (d-0x81>126) goto ilseq;
427 				c += 10*(d-0x81);
428 				d = *((unsigned char *)*in + 3);
429 				if (d-'0'>9) goto ilseq;
430 				c += d-'0';
431 				c += 128;
432 				for (d=0; d<=c; ) {
433 					k = 0;
434 					for (int i=0; i<126; i++)
435 						for (int j=0; j<190; j++)
436 							if (gb18030[i][j]-d <= c-d)
437 								k++;
438 					d = c+1;
439 					c += k;
440 				}
441 				break;
442 			}
443 			d -= 0x40;
444 			if (d>63) d--;
445 			c = gb18030[c][d];
446 			break;
447 		case BIG5:
448 			if (c < 128) break;
449 			l = 2;
450 			if (*inb < 2) goto starved;
451 			d = *((unsigned char *)*in + 1);
452 			if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
453 			d -= 0x40;
454 			if (d > 0x3e) d -= 0x22;
455 			if (c-0xa1>=0xfa-0xa1) {
456 				if (c-0x87>=0xff-0x87) goto ilseq;
457 				if (c < 0xa1) c -= 0x87;
458 				else c -= 0x87 + (0xfa-0xa1);
459 				c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
460 					| hkscs[c*157+d];
461 				/* A few HKSCS characters map to pairs of UCS
462 				 * characters. These are mapped to surrogate
463 				 * range in the hkscs table then hard-coded
464 				 * here. Ugly, yes. */
465 				if (c/256 == 0xdc) {
466 					union {
467 						char c[8];
468 						wchar_t wc[2];
469 					} tmp;
470 					char *ptmp = tmp.c;
471 					size_t tmpx = iconv(combine_to_from(to, find_charmap("utf8")),
472 						&(char *){"\303\212\314\204"
473 						"\303\212\314\214"
474 						"\303\252\314\204"
475 						"\303\252\314\214"
476 						+c%256}, &(size_t){4},
477 						&ptmp, &(size_t){sizeof tmp});
478 					size_t tmplen = ptmp - tmp.c;
479 					if (tmplen > *outb) goto toobig;
480 					if (tmpx) x++;
481 					memcpy(*out, &tmp, tmplen);
482 					*out += tmplen;
483 					*outb -= tmplen;
484 					continue;
485 				}
486 				if (!c) goto ilseq;
487 				break;
488 			}
489 			c -= 0xa1;
490 			c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
491 			if (!c) goto ilseq;
492 			break;
493 		case EUC_KR:
494 			if (c < 128) break;
495 			l = 2;
496 			if (*inb < 2) goto starved;
497 			d = *((unsigned char *)*in + 1);
498 			c -= 0xa1;
499 			d -= 0xa1;
500 			if (c >= 93 || d >= 94) {
501 				c += (0xa1-0x81);
502 				d += 0xa1;
503 				if (c >= 93 || c>=0xc6-0x81 && d>0x52)
504 					goto ilseq;
505 				if (d-'A'<26) d = d-'A';
506 				else if (d-'a'<26) d = d-'a'+26;
507 				else if (d-0x81<0xff-0x81) d = d-0x81+52;
508 				else goto ilseq;
509 				if (c < 0x20) c = 178*c + d;
510 				else c = 178*0x20 + 84*(c-0x20) + d;
511 				c += 0xac00;
512 				for (d=0xac00; d<=c; ) {
513 					k = 0;
514 					for (int i=0; i<93; i++)
515 						for (int j=0; j<94; j++)
516 							if (ksc[i][j]-d <= c-d)
517 								k++;
518 					d = c+1;
519 					c += k;
520 				}
521 				break;
522 			}
523 			c = ksc[c][d];
524 			if (!c) goto ilseq;
525 			break;
526 		default:
527 			if (!c) break;
528 			c = legacy_map(map, c);
529 			if (!c) goto ilseq;
530 		}
531 
532 		switch (totype) {
533 		case WCHAR_T:
534 			if (*outb < sizeof(wchar_t)) goto toobig;
535 			*(wchar_t *)*out = c;
536 			*out += sizeof(wchar_t);
537 			*outb -= sizeof(wchar_t);
538 			break;
539 		case UTF_8:
540 			if (*outb < 4) {
541 				char tmp[4];
542 				k = wctomb_utf8(tmp, c);
543 				if (*outb < k) goto toobig;
544 				memcpy(*out, tmp, k);
545 			} else k = wctomb_utf8(*out, c);
546 			*out += k;
547 			*outb -= k;
548 			break;
549 		case US_ASCII:
550 			if (c > 0x7f) subst: x++, c='*';
551 		default:
552 			if (*outb < 1) goto toobig;
553 			if (c<256 && c==legacy_map(tomap, c)) {
554 			revout:
555 				if (*outb < 1) goto toobig;
556 				*(*out)++ = c;
557 				*outb -= 1;
558 				break;
559 			}
560 			d = c;
561 			for (c=4*totype; c<256; c++) {
562 				if (d == legacy_map(tomap, c)) {
563 					goto revout;
564 				}
565 			}
566 			goto subst;
567 		case SHIFT_JIS:
568 			if (c < 128) goto revout;
569 			if (c == 0xa5) {
570 				x++;
571 				c = '\\';
572 				goto revout;
573 			}
574 			if (c == 0x203e) {
575 				x++;
576 				c = '~';
577 				goto revout;
578 			}
579 			if (c-0xff61 <= 0xdf-0xa1) {
580 				c += 0xa1 - 0xff61;
581 				goto revout;
582 			}
583 			c = uni_to_jis(c);
584 			if (!c) goto subst;
585 			if (*outb < 2) goto toobig;
586 			d = c%256;
587 			c = c/256;
588 			*(*out)++ = (c+1)/2 + (c<95 ? 112 : 176);
589 			*(*out)++ = c%2 ? d + 31 + d/96 : d + 126;
590 			*outb -= 2;
591 			break;
592 		case EUC_JP:
593 			if (c < 128) goto revout;
594 			if (c-0xff61 <= 0xdf-0xa1) {
595 				c += 0x0e00 + 0x21 - 0xff61;
596 			} else {
597 				c = uni_to_jis(c);
598 			}
599 			if (!c) goto subst;
600 			if (*outb < 2) goto toobig;
601 			*(*out)++ = c/256 + 0x80;
602 			*(*out)++ = c%256 + 0x80;
603 			*outb -= 2;
604 			break;
605 		case ISO2022_JP:
606 			if (c < 128) goto revout;
607 			if (c-0xff61 <= 0xdf-0xa1 || c==0xa5 || c==0x203e) {
608 				if (*outb < 7) goto toobig;
609 				*(*out)++ = '\033';
610 				*(*out)++ = '(';
611 				if (c==0xa5) {
612 					*(*out)++ = 'J';
613 					*(*out)++ = '\\';
614 				} else if (c==0x203e) {
615 					*(*out)++ = 'J';
616 					*(*out)++ = '~';
617 				} else {
618 					*(*out)++ = 'I';
619 					*(*out)++ = c-0xff61+0x21;
620 				}
621 				*(*out)++ = '\033';
622 				*(*out)++ = '(';
623 				*(*out)++ = 'B';
624 				*outb -= 7;
625 				break;
626 			}
627 			c = uni_to_jis(c);
628 			if (!c) goto subst;
629 			if (*outb < 8) goto toobig;
630 			*(*out)++ = '\033';
631 			*(*out)++ = '$';
632 			*(*out)++ = 'B';
633 			*(*out)++ = c/256;
634 			*(*out)++ = c%256;
635 			*(*out)++ = '\033';
636 			*(*out)++ = '(';
637 			*(*out)++ = 'B';
638 			*outb -= 8;
639 			break;
640 		case UCS2:
641 			totype = UCS2BE;
642 		case UCS2BE:
643 		case UCS2LE:
644 		case UTF_16:
645 		case UTF_16BE:
646 		case UTF_16LE:
647 			if (c < 0x10000 || totype-UCS2BE < 2U) {
648 				if (c >= 0x10000) c = 0xFFFD;
649 				if (*outb < 2) goto toobig;
650 				put_16((void *)*out, c, totype);
651 				*out += 2;
652 				*outb -= 2;
653 				break;
654 			}
655 			if (*outb < 4) goto toobig;
656 			c -= 0x10000;
657 			put_16((void *)*out, (c>>10)|0xd800, totype);
658 			put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
659 			*out += 4;
660 			*outb -= 4;
661 			break;
662 		case UTF_32:
663 			totype = UTF_32BE;
664 		case UTF_32BE:
665 		case UTF_32LE:
666 			if (*outb < 4) goto toobig;
667 			put_32((void *)*out, c, totype);
668 			*out += 4;
669 			*outb -= 4;
670 			break;
671 		}
672 	}
673 	*ploc = loc;
674 	return x;
675 ilseq:
676 	err = EILSEQ;
677 	x = -1;
678 	goto end;
679 toobig:
680 	err = E2BIG;
681 	x = -1;
682 	goto end;
683 starved:
684 	err = EINVAL;
685 	x = -1;
686 end:
687 	errno = err;
688 	*ploc = loc;
689 	return x;
690 }
691