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