0696b5556a4d0f68d5c76d39da51db018b3dc7aa
[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 GB18030     0330
22 #define GBK         0331
23 #define GB2312      0332
24 #define BIG5        0340
25 #define EUC_KR      0350
26
27 /* Definitions of charmaps. Each charmap consists of:
28  * 1. Empty-string-terminated list of null-terminated aliases.
29  * 2. Special type code or number of elided entries.
30  * 3. Character table (size determined by field 2). */
31
32 static const unsigned char charmaps[] =
33 "utf8\0char\0\0\310"
34 "wchart\0\0\306"
35 "ucs2\0ucs2be\0\0\304"
36 "ucs2le\0\0\305"
37 "utf16\0utf16be\0\0\302"
38 "utf16le\0\0\301"
39 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
40 "ucs4le\0utf32le\0\0\303"
41 "ascii\0usascii\0iso646\0iso646us\0\0\307"
42 "eucjp\0\0\320"
43 "shiftjis\0sjis\0\0\321"
44 "gb18030\0\0\330"
45 "gbk\0\0\331"
46 "gb2312\0\0\332"
47 "big5\0bigfive\0cp950\0big5hkscs\0\0\340"
48 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
49 #include "codepages.h"
50 ;
51
52 static const unsigned short legacy_chars[] = {
53 #include "legacychars.h"
54 };
55
56 static const unsigned short jis0208[84][94] = {
57 #include "jis0208.h"
58 };
59
60 static const unsigned short gb18030[126][190] = {
61 #include "gb18030.h"
62 };
63
64 static const unsigned short big5[89][157] = {
65 #include "big5.h"
66 };
67
68 static const unsigned short hkscs[] = {
69 #include "hkscs.h"
70 };
71
72 static const unsigned short ksc[93][94] = {
73 #include "ksc.h"
74 };
75
76 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
77 {
78         for (; *a && *b; a++, b++) {
79                 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
80                 if ((*a|32U) != *b) return 1;
81         }
82         return *a != *b;
83 }
84
85 static size_t find_charmap(const void *name)
86 {
87         const unsigned char *s;
88         if (!*(char *)name) name=charmaps; /* "utf8" */
89         for (s=charmaps; *s; ) {
90                 if (!fuzzycmp(name, s)) {
91                         for (; *s; s+=strlen((void *)s)+1);
92                         return s+1-charmaps;
93                 }
94                 s += strlen((void *)s)+1;
95                 if (!*s) {
96                         if (s[1] > 0200) s+=2;
97                         else s+=2+(128U-s[1])/4*5;
98                 }
99         }
100         return -1;
101 }
102
103 struct stateful_cd {
104         iconv_t base_cd;
105         unsigned state;
106 };
107
108 static iconv_t combine_to_from(size_t t, size_t f)
109 {
110         return (void *)(f<<16 | t<<1 | 1);
111 }
112
113 static size_t extract_from(iconv_t cd)
114 {
115         return (size_t)cd >> 16;
116 }
117
118 static size_t extract_to(iconv_t cd)
119 {
120         return (size_t)cd >> 1 & 0x7fff;
121 }
122
123 iconv_t iconv_open(const char *to, const char *from)
124 {
125         size_t f, t;
126
127         if ((t = find_charmap(to))==-1
128          || (f = find_charmap(from))==-1
129          || (charmaps[t] >= 0320)) {
130                 errno = EINVAL;
131                 return (iconv_t)-1;
132         }
133         iconv_t cd = combine_to_from(t, f);
134
135         if (0) {
136                 struct stateful_cd *scd = malloc(sizeof *scd);
137                 if (!scd) return (iconv_t)-1;
138                 scd->base_cd = cd;
139                 scd->state = 0;
140                 cd = (iconv_t)scd;
141         }
142
143         return cd;
144 }
145
146 static unsigned get_16(const unsigned char *s, int e)
147 {
148         e &= 1;
149         return s[e]<<8 | s[1-e];
150 }
151
152 static void put_16(unsigned char *s, unsigned c, int e)
153 {
154         e &= 1;
155         s[e] = c>>8;
156         s[1-e] = c;
157 }
158
159 static unsigned get_32(const unsigned char *s, int e)
160 {
161         e &= 3;
162         return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
163 }
164
165 static void put_32(unsigned char *s, unsigned c, int e)
166 {
167         e &= 3;
168         s[e^0] = c>>24;
169         s[e^1] = c>>16;
170         s[e^2] = c>>8;
171         s[e^3] = c;
172 }
173
174 /* Adapt as needed */
175 #define mbrtowc_utf8 mbrtowc
176 #define wctomb_utf8 wctomb
177
178 static unsigned legacy_map(const unsigned char *map, unsigned c)
179 {
180         unsigned x = c - 128 - map[-1];
181         x = legacy_chars[ map[x*5/4]>>2*x%8 |
182                 map[x*5/4+1]<<8-2*x%8 & 1023 ];
183         return x ? x : c;
184 }
185
186 size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
187 {
188         size_t x=0;
189         struct stateful_cd *scd=0;
190         if (!((size_t)cd & 1)) {
191                 scd = (void *)cd;
192                 cd = scd->base_cd;
193         }
194         unsigned to = extract_to(cd);
195         unsigned from = extract_from(cd);
196         const unsigned char *map = charmaps+from+1;
197         const unsigned char *tomap = charmaps+to+1;
198         mbstate_t st = {0};
199         wchar_t wc;
200         unsigned c, d;
201         size_t k, l;
202         int err;
203         unsigned char type = map[-1];
204         unsigned char totype = tomap[-1];
205         locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
206
207         if (!in || !*in || !*inb) return 0;
208
209         *ploc = UTF8_LOCALE;
210
211         for (; *inb; *in+=l, *inb-=l) {
212                 c = *(unsigned char *)*in;
213                 l = 1;
214
215                 switch (type) {
216                 case UTF_8:
217                         if (c < 128) break;
218                         l = mbrtowc_utf8(&wc, *in, *inb, &st);
219                         if (l == (size_t)-1) goto ilseq;
220                         if (l == (size_t)-2) goto starved;
221                         c = wc;
222                         break;
223                 case US_ASCII:
224                         if (c >= 128) goto ilseq;
225                         break;
226                 case WCHAR_T:
227                         l = sizeof(wchar_t);
228                         if (*inb < l) goto starved;
229                         c = *(wchar_t *)*in;
230                         if (0) {
231                 case UTF_32BE:
232                 case UTF_32LE:
233                         l = 4;
234                         if (*inb < 4) goto starved;
235                         c = get_32((void *)*in, type);
236                         }
237                         if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
238                         break;
239                 case UCS2BE:
240                 case UCS2LE:
241                 case UTF_16BE:
242                 case UTF_16LE:
243                         l = 2;
244                         if (*inb < 2) goto starved;
245                         c = get_16((void *)*in, type);
246                         if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
247                         if ((unsigned)(c-0xd800) < 0x400) {
248                                 if (type-UCS2BE < 2U) goto ilseq;
249                                 l = 4;
250                                 if (*inb < 4) goto starved;
251                                 d = get_16((void *)(*in + 2), type);
252                                 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
253                                 c = ((c-0xd7c0)<<10) + (d-0xdc00);
254                         }
255                         break;
256                 case SHIFT_JIS:
257                         if (c < 128) break;
258                         if (c-0xa1 <= 0xdf-0xa1) {
259                                 c += 0xff61-0xa1;
260                                 break;
261                         }
262                         l = 2;
263                         if (*inb < 2) goto starved;
264                         d = *((unsigned char *)*in + 1);
265                         if (c-129 <= 159-129) c -= 129;
266                         else if (c-224 <= 239-224) c -= 193;
267                         else goto ilseq;
268                         c *= 2;
269                         if (d-64 <= 158-64) {
270                                 if (d==127) goto ilseq;
271                                 if (d>127) d--;
272                                 d -= 64;
273                         } else if (d-159 <= 252-159) {
274                                 c++;
275                                 d -= 159;
276                         }
277                         c = jis0208[c][d];
278                         if (!c) goto ilseq;
279                         break;
280                 case EUC_JP:
281                         if (c < 128) break;
282                         l = 2;
283                         if (*inb < 2) goto starved;
284                         d = *((unsigned char *)*in + 1);
285                         if (c==0x8e) {
286                                 c = d;
287                                 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
288                                 c += 0xff61 - 0xa1;
289                                 break;
290                         }
291                         c -= 0xa1;
292                         d -= 0xa1;
293                         if (c >= 84 || d >= 94) goto ilseq;
294                         c = jis0208[c][d];
295                         if (!c) goto ilseq;
296                         break;
297                 case GB2312:
298                         if (c < 128) break;
299                         if (c < 0xa1) goto ilseq;
300                 case GBK:
301                 case GB18030:
302                         if (c < 128) break;
303                         c -= 0x81;
304                         if (c >= 126) goto ilseq;
305                         l = 2;
306                         if (*inb < 2) goto starved;
307                         d = *((unsigned char *)*in + 1);
308                         if (d < 0xa1 && type == GB2312) goto ilseq;
309                         if (d-0x40>=191 || d==127) {
310                                 if (d-'0'>9 || type != GB18030)
311                                         goto ilseq;
312                                 l = 4;
313                                 if (*inb < 4) goto starved;
314                                 c = (10*c + d-'0') * 1260;
315                                 d = *((unsigned char *)*in + 2);
316                                 if (d-0x81>126) goto ilseq;
317                                 c += 10*(d-0x81);
318                                 d = *((unsigned char *)*in + 3);
319                                 if (d-'0'>9) goto ilseq;
320                                 c += d-'0';
321                                 c += 128;
322                                 for (d=0; d<=c; ) {
323                                         k = 0;
324                                         for (int i=0; i<126; i++)
325                                                 for (int j=0; j<190; j++)
326                                                         if (gb18030[i][j]-d <= c-d)
327                                                                 k++;
328                                         d = c+1;
329                                         c += k;
330                                 }
331                                 break;
332                         }
333                         d -= 0x40;
334                         if (d>63) d--;
335                         c = gb18030[c][d];
336                         break;
337                 case BIG5:
338                         if (c < 128) break;
339                         l = 2;
340                         if (*inb < 2) goto starved;
341                         d = *((unsigned char *)*in + 1);
342                         if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
343                         d -= 0x40;
344                         if (d > 0x3e) d -= 0x22;
345                         if (c-0xa1>=0xfa-0xa1) {
346                                 if (c-0x87>=0xff-0x87) goto ilseq;
347                                 if (c < 0xa1) c -= 0x87;
348                                 else c -= 0x87 + (0xfa-0xa1);
349                                 c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
350                                         | hkscs[c*157+d];
351                                 /* A few HKSCS characters map to pairs of UCS
352                                  * characters. These are mapped to surrogate
353                                  * range in the hkscs table then hard-coded
354                                  * here. Ugly, yes. */
355                                 if (c/256 == 0xdc) {
356                                         if (totype-0300U > 8) k = 2;
357                                         else k = "\10\4\4\10\4\4\10\2\4"[totype-0300];
358                                         if (k > *outb) goto toobig;
359                                         x += iconv(combine_to_from(to, 0),
360                                                 &(char *){"\303\212\314\204"
361                                                 "\303\212\314\214"
362                                                 "\303\252\314\204"
363                                                 "\303\252\314\214"
364                                                 +c%256}, &(size_t){4},
365                                                 out, outb);
366                                         continue;
367                                 }
368                                 if (!c) goto ilseq;
369                                 break;
370                         }
371                         c -= 0xa1;
372                         c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
373                         if (!c) goto ilseq;
374                         break;
375                 case EUC_KR:
376                         if (c < 128) break;
377                         l = 2;
378                         if (*inb < 2) goto starved;
379                         d = *((unsigned char *)*in + 1);
380                         c -= 0xa1;
381                         d -= 0xa1;
382                         if (c >= 93 || d >= 94) {
383                                 c += (0xa1-0x81);
384                                 d += 0xa1;
385                                 if (c >= 93 || c>=0xc6-0x81 && d>0x52)
386                                         goto ilseq;
387                                 if (d-'A'<26) d = d-'A';
388                                 else if (d-'a'<26) d = d-'a'+26;
389                                 else if (d-0x81<0xff-0x81) d = d-0x81+52;
390                                 else goto ilseq;
391                                 if (c < 0x20) c = 178*c + d;
392                                 else c = 178*0x20 + 84*(c-0x20) + d;
393                                 c += 0xac00;
394                                 for (d=0xac00; d<=c; ) {
395                                         k = 0;
396                                         for (int i=0; i<93; i++)
397                                                 for (int j=0; j<94; j++)
398                                                         if (ksc[i][j]-d <= c-d)
399                                                                 k++;
400                                         d = c+1;
401                                         c += k;
402                                 }
403                                 break;
404                         }
405                         c = ksc[c][d];
406                         if (!c) goto ilseq;
407                         break;
408                 default:
409                         if (c < 128+type) break;
410                         c = legacy_map(map, c);
411                         if (c==1) goto ilseq;
412                 }
413
414                 switch (totype) {
415                 case WCHAR_T:
416                         if (*outb < sizeof(wchar_t)) goto toobig;
417                         *(wchar_t *)*out = c;
418                         *out += sizeof(wchar_t);
419                         *outb -= sizeof(wchar_t);
420                         break;
421                 case UTF_8:
422                         if (*outb < 4) {
423                                 char tmp[4];
424                                 k = wctomb_utf8(tmp, c);
425                                 if (*outb < k) goto toobig;
426                                 memcpy(*out, tmp, k);
427                         } else k = wctomb_utf8(*out, c);
428                         *out += k;
429                         *outb -= k;
430                         break;
431                 case US_ASCII:
432                         if (c > 0x7f) subst: x++, c='*';
433                 default:
434                         if (*outb < 1) goto toobig;
435                         if (c < 128+totype || (c<256 && c==legacy_map(tomap, c))) {
436                         revout:
437                                 *(*out)++ = c;
438                                 *outb -= 1;
439                                 break;
440                         }
441                         d = c;
442                         for (c=128+totype; c<256; c++) {
443                                 if (d == legacy_map(tomap, c)) {
444                                         goto revout;
445                                 }
446                         }
447                         goto subst;
448                 case UCS2BE:
449                 case UCS2LE:
450                 case UTF_16BE:
451                 case UTF_16LE:
452                         if (c < 0x10000 || type-UCS2BE < 2U) {
453                                 if (c >= 0x10000) c = 0xFFFD;
454                                 if (*outb < 2) goto toobig;
455                                 put_16((void *)*out, c, totype);
456                                 *out += 2;
457                                 *outb -= 2;
458                                 break;
459                         }
460                         if (*outb < 4) goto toobig;
461                         c -= 0x10000;
462                         put_16((void *)*out, (c>>10)|0xd800, totype);
463                         put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
464                         *out += 4;
465                         *outb -= 4;
466                         break;
467                 case UTF_32BE:
468                 case UTF_32LE:
469                         if (*outb < 4) goto toobig;
470                         put_32((void *)*out, c, totype);
471                         *out += 4;
472                         *outb -= 4;
473                         break;
474                 }
475         }
476         *ploc = loc;
477         return x;
478 ilseq:
479         err = EILSEQ;
480         x = -1;
481         goto end;
482 toobig:
483         err = E2BIG;
484         x = -1;
485         goto end;
486 starved:
487         err = EINVAL;
488         x = -1;
489 end:
490         errno = err;
491         *ploc = loc;
492         return x;
493 }