refactor iconv conversion descriptor encoding/decoding
[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 static iconv_t combine_to_from(size_t t, size_t f)
104 {
105         return (void *)(f<<16 | t);
106 }
107
108 static size_t extract_from(iconv_t cd)
109 {
110         return (size_t)cd >> 16;
111 }
112
113 static size_t extract_to(iconv_t cd)
114 {
115         return (size_t)cd & 0xffff;
116 }
117
118 iconv_t iconv_open(const char *to, const char *from)
119 {
120         size_t f, t;
121
122         if ((t = find_charmap(to))==-1
123          || (f = find_charmap(from))==-1
124          || (charmaps[t] >= 0320)) {
125                 errno = EINVAL;
126                 return (iconv_t)-1;
127         }
128
129         return combine_to_from(t, f);
130 }
131
132 int iconv_close(iconv_t cd)
133 {
134         return 0;
135 }
136
137 static unsigned get_16(const unsigned char *s, int e)
138 {
139         e &= 1;
140         return s[e]<<8 | s[1-e];
141 }
142
143 static void put_16(unsigned char *s, unsigned c, int e)
144 {
145         e &= 1;
146         s[e] = c>>8;
147         s[1-e] = c;
148 }
149
150 static unsigned get_32(const unsigned char *s, int e)
151 {
152         e &= 3;
153         return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
154 }
155
156 static void put_32(unsigned char *s, unsigned c, int e)
157 {
158         e &= 3;
159         s[e^0] = c>>24;
160         s[e^1] = c>>16;
161         s[e^2] = c>>8;
162         s[e^3] = c;
163 }
164
165 /* Adapt as needed */
166 #define mbrtowc_utf8 mbrtowc
167 #define wctomb_utf8 wctomb
168
169 static unsigned legacy_map(const unsigned char *map, unsigned c)
170 {
171         unsigned x = c - 128 - map[-1];
172         x = legacy_chars[ map[x*5/4]>>2*x%8 |
173                 map[x*5/4+1]<<8-2*x%8 & 1023 ];
174         return x ? x : c;
175 }
176
177 size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
178 {
179         size_t x=0;
180         unsigned to = extract_to(cd);
181         unsigned from = extract_from(cd);
182         const unsigned char *map = charmaps+from+1;
183         const unsigned char *tomap = charmaps+to+1;
184         mbstate_t st = {0};
185         wchar_t wc;
186         unsigned c, d;
187         size_t k, l;
188         int err;
189         unsigned char type = map[-1];
190         unsigned char totype = tomap[-1];
191         locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
192
193         if (!in || !*in || !*inb) return 0;
194
195         *ploc = UTF8_LOCALE;
196
197         for (; *inb; *in+=l, *inb-=l) {
198                 c = *(unsigned char *)*in;
199                 l = 1;
200
201                 if (c >= 128 || type-UTF_32BE < 7U) switch (type) {
202                 case UTF_8:
203                         l = mbrtowc_utf8(&wc, *in, *inb, &st);
204                         if (!l) l++;
205                         else if (l == (size_t)-1) goto ilseq;
206                         else if (l == (size_t)-2) goto starved;
207                         c = wc;
208                         break;
209                 case US_ASCII:
210                         goto ilseq;
211                 case WCHAR_T:
212                         l = sizeof(wchar_t);
213                         if (*inb < l) goto starved;
214                         c = *(wchar_t *)*in;
215                         if (0) {
216                 case UTF_32BE:
217                 case UTF_32LE:
218                         l = 4;
219                         if (*inb < 4) goto starved;
220                         c = get_32((void *)*in, type);
221                         }
222                         if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
223                         break;
224                 case UCS2BE:
225                 case UCS2LE:
226                 case UTF_16BE:
227                 case UTF_16LE:
228                         l = 2;
229                         if (*inb < 2) goto starved;
230                         c = get_16((void *)*in, type);
231                         if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
232                         if ((unsigned)(c-0xd800) < 0x400) {
233                                 if (type-UCS2BE < 2U) goto ilseq;
234                                 l = 4;
235                                 if (*inb < 4) goto starved;
236                                 d = get_16((void *)(*in + 2), type);
237                                 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
238                                 c = ((c-0xd7c0)<<10) + (d-0xdc00);
239                         }
240                         break;
241                 case SHIFT_JIS:
242                         if (c-0xa1 <= 0xdf-0xa1) {
243                                 c += 0xff61-0xa1;
244                                 break;
245                         }
246                         l = 2;
247                         if (*inb < 2) goto starved;
248                         d = *((unsigned char *)*in + 1);
249                         if (c-129 <= 159-129) c -= 129;
250                         else if (c-224 <= 239-224) c -= 193;
251                         else goto ilseq;
252                         c *= 2;
253                         if (d-64 <= 158-64) {
254                                 if (d==127) goto ilseq;
255                                 if (d>127) d--;
256                                 d -= 64;
257                         } else if (d-159 <= 252-159) {
258                                 c++;
259                                 d -= 159;
260                         }
261                         c = jis0208[c][d];
262                         if (!c) goto ilseq;
263                         break;
264                 case EUC_JP:
265                         l = 2;
266                         if (*inb < 2) goto starved;
267                         d = *((unsigned char *)*in + 1);
268                         if (c==0x8e) {
269                                 c = d;
270                                 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
271                                 c += 0xff61 - 0xa1;
272                                 break;
273                         }
274                         c -= 0xa1;
275                         d -= 0xa1;
276                         if (c >= 84 || d >= 94) goto ilseq;
277                         c = jis0208[c][d];
278                         if (!c) goto ilseq;
279                         break;
280                 case GB2312:
281                         if (c < 0xa1) goto ilseq;
282                 case GBK:
283                 case GB18030:
284                         c -= 0x81;
285                         if (c >= 126) goto ilseq;
286                         l = 2;
287                         if (*inb < 2) goto starved;
288                         d = *((unsigned char *)*in + 1);
289                         if (d < 0xa1 && type == GB2312) goto ilseq;
290                         if (d-0x40>=191 || d==127) {
291                                 if (d-'0'>9 || type != GB18030)
292                                         goto ilseq;
293                                 l = 4;
294                                 if (*inb < 4) goto starved;
295                                 c = (10*c + d-'0') * 1260;
296                                 d = *((unsigned char *)*in + 2);
297                                 if (d-0x81>126) goto ilseq;
298                                 c += 10*(d-0x81);
299                                 d = *((unsigned char *)*in + 3);
300                                 if (d-'0'>9) goto ilseq;
301                                 c += d-'0';
302                                 c += 128;
303                                 for (d=0; d<=c; ) {
304                                         k = 0;
305                                         for (int i=0; i<126; i++)
306                                                 for (int j=0; j<190; j++)
307                                                         if (gb18030[i][j]-d <= c-d)
308                                                                 k++;
309                                         d = c+1;
310                                         c += k;
311                                 }
312                                 break;
313                         }
314                         d -= 0x40;
315                         if (d>63) d--;
316                         c = gb18030[c][d];
317                         break;
318                 case BIG5:
319                         l = 2;
320                         if (*inb < 2) goto starved;
321                         d = *((unsigned char *)*in + 1);
322                         if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
323                         d -= 0x40;
324                         if (d > 0x3e) d -= 0x22;
325                         if (c-0xa1>=0xfa-0xa1) {
326                                 if (c-0x87>=0xff-0x87) goto ilseq;
327                                 if (c < 0xa1) c -= 0x87;
328                                 else c -= 0x87 + (0xfa-0xa1);
329                                 c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
330                                         | hkscs[c*157+d];
331                                 /* A few HKSCS characters map to pairs of UCS
332                                  * characters. These are mapped to surrogate
333                                  * range in the hkscs table then hard-coded
334                                  * here. Ugly, yes. */
335                                 if (c/256 == 0xdc) {
336                                         if (totype-0300U > 8) k = 2;
337                                         else k = "\10\4\4\10\4\4\10\2\4"[totype-0300];
338                                         if (k > *outb) goto toobig;
339                                         x += iconv(combine_to_from(to, 0),
340                                                 &(char *){"\303\212\314\204"
341                                                 "\303\212\314\214"
342                                                 "\303\252\314\204"
343                                                 "\303\252\314\214"
344                                                 +c%256}, &(size_t){4},
345                                                 out, outb);
346                                         continue;
347                                 }
348                                 if (!c) goto ilseq;
349                                 break;
350                         }
351                         c -= 0xa1;
352                         c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
353                         if (!c) goto ilseq;
354                         break;
355                 case EUC_KR:
356                         l = 2;
357                         if (*inb < 2) goto starved;
358                         d = *((unsigned char *)*in + 1);
359                         c -= 0xa1;
360                         d -= 0xa1;
361                         if (c >= 93 || d >= 94) {
362                                 c += (0xa1-0x81);
363                                 d += 0xa1;
364                                 if (c >= 93 || c>=0xc6-0x81 && d>0x52)
365                                         goto ilseq;
366                                 if (d-'A'<26) d = d-'A';
367                                 else if (d-'a'<26) d = d-'a'+26;
368                                 else if (d-0x81<0xff-0x81) d = d-0x81+52;
369                                 else goto ilseq;
370                                 if (c < 0x20) c = 178*c + d;
371                                 else c = 178*0x20 + 84*(c-0x20) + d;
372                                 c += 0xac00;
373                                 for (d=0xac00; d<=c; ) {
374                                         k = 0;
375                                         for (int i=0; i<93; i++)
376                                                 for (int j=0; j<94; j++)
377                                                         if (ksc[i][j]-d <= c-d)
378                                                                 k++;
379                                         d = c+1;
380                                         c += k;
381                                 }
382                                 break;
383                         }
384                         c = ksc[c][d];
385                         if (!c) goto ilseq;
386                         break;
387                 default:
388                         if (c < 128+type) break;
389                         c = legacy_map(map, c);
390                         if (c==1) goto ilseq;
391                 }
392
393                 switch (totype) {
394                 case WCHAR_T:
395                         if (*outb < sizeof(wchar_t)) goto toobig;
396                         *(wchar_t *)*out = c;
397                         *out += sizeof(wchar_t);
398                         *outb -= sizeof(wchar_t);
399                         break;
400                 case UTF_8:
401                         if (*outb < 4) {
402                                 char tmp[4];
403                                 k = wctomb_utf8(tmp, c);
404                                 if (*outb < k) goto toobig;
405                                 memcpy(*out, tmp, k);
406                         } else k = wctomb_utf8(*out, c);
407                         *out += k;
408                         *outb -= k;
409                         break;
410                 case US_ASCII:
411                         if (c > 0x7f) subst: x++, c='*';
412                 default:
413                         if (*outb < 1) goto toobig;
414                         if (c < 128+totype || (c<256 && c==legacy_map(tomap, c))) {
415                         revout:
416                                 *(*out)++ = c;
417                                 *outb -= 1;
418                                 break;
419                         }
420                         d = c;
421                         for (c=128+totype; c<256; c++) {
422                                 if (d == legacy_map(tomap, c)) {
423                                         goto revout;
424                                 }
425                         }
426                         goto subst;
427                 case UCS2BE:
428                 case UCS2LE:
429                 case UTF_16BE:
430                 case UTF_16LE:
431                         if (c < 0x10000 || type-UCS2BE < 2U) {
432                                 if (c >= 0x10000) c = 0xFFFD;
433                                 if (*outb < 2) goto toobig;
434                                 put_16((void *)*out, c, totype);
435                                 *out += 2;
436                                 *outb -= 2;
437                                 break;
438                         }
439                         if (*outb < 4) goto toobig;
440                         c -= 0x10000;
441                         put_16((void *)*out, (c>>10)|0xd800, totype);
442                         put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
443                         *out += 4;
444                         *outb -= 4;
445                         break;
446                 case UTF_32BE:
447                 case UTF_32LE:
448                         if (*outb < 4) goto toobig;
449                         put_32((void *)*out, c, totype);
450                         *out += 4;
451                         *outb -= 4;
452                         break;
453                 }
454         }
455         *ploc = loc;
456         return x;
457 ilseq:
458         err = EILSEQ;
459         x = -1;
460         goto end;
461 toobig:
462         err = E2BIG;
463         x = -1;
464         goto end;
465 starved:
466         err = EINVAL;
467         x = -1;
468 end:
469         errno = err;
470         *ploc = loc;
471         return x;
472 }