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