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