add Big5 charset support to iconv
[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 /* FIXME: these are not implemented yet
27  * EUC:   A1-FE A1-FE
28  * GBK:   81-FE 40-7E,80-FE
29  * Big5:  A1-FE 40-7E,A1-FE
30  */
31
32 /* Definitions of charmaps. Each charmap consists of:
33  * 1. Empty-string-terminated list of null-terminated aliases.
34  * 2. Special type code or number of elided entries.
35  * 3. Character table (size determined by field 2). */
36
37 static const unsigned char charmaps[] =
38 "utf8\0\0\310"
39 "wchart\0\0\306"
40 "ucs2\0ucs2be\0\0\304"
41 "ucs2le\0\0\305"
42 "utf16\0utf16be\0\0\302"
43 "utf16le\0\0\301"
44 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
45 "ucs4le\0utf32le\0\0\303"
46 "ascii\0usascii\0iso646\0iso646us\0\0\307"
47 "eucjp\0\0\320"
48 "shiftjis\0sjis\0\0\321"
49 "gb18030\0\0\330"
50 "gbk\0\0\331"
51 "gb2312\0\0\332"
52 "big5\0bigfive\0cp950\0\0\340"
53 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
54 #include "codepages.h"
55 ;
56
57 static const unsigned short legacy_chars[] = {
58 #include "legacychars.h"
59 };
60
61 static const unsigned short jis0208[84][94] = {
62 #include "jis0208.h"
63 };
64
65 static const unsigned short gb18030[126][190] = {
66 #include "gb18030.h"
67 };
68
69 static const unsigned short big5[89][157] = {
70 #include "big5.h"
71 };
72
73 static const unsigned short ksc[93][94] = {
74 #include "ksc.h"
75 };
76
77 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
78 {
79         for (; *a && *b; a++, b++) {
80                 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
81                 if ((*a|32U) != *b) return 1;
82         }
83         return *a != *b;
84 }
85
86 static size_t find_charmap(const void *name)
87 {
88         const unsigned char *s;
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 size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
155 {
156         size_t x=0;
157         unsigned long cd = (unsigned long)cd0;
158         unsigned to = cd & 0xffff;
159         unsigned from = cd >> 16;
160         const unsigned char *map = charmaps+from+1;
161         const unsigned char *tomap = charmaps+to+1;
162         mbstate_t st = {0};
163         wchar_t wc;
164         unsigned c, d;
165         size_t k, l;
166         int err;
167         unsigned char type = map[-1];
168         unsigned char totype = tomap[-1];
169
170         if (!in || !*in || !*inb) return 0;
171
172         for (; *inb; *in+=l, *inb-=l) {
173                 c = *(unsigned char *)*in;
174                 l = 1;
175
176                 if (c >= 128 || type-UTF_32BE < 7U) switch (type) {
177                 case UTF_8:
178                         l = mbrtowc_utf8(&wc, *in, *inb, &st);
179                         if (!l) l++;
180                         else if (l == (size_t)-1) goto ilseq;
181                         else if (l == (size_t)-2) goto starved;
182                         c = wc;
183                         break;
184                 case US_ASCII:
185                         goto ilseq;
186                 case WCHAR_T:
187                         l = sizeof(wchar_t);
188                         if (*inb < l) goto starved;
189                         c = *(wchar_t *)*in;
190                         if (0) {
191                 case UTF_32BE:
192                 case UTF_32LE:
193                         l = 4;
194                         if (*inb < 4) goto starved;
195                         c = get_32((void *)*in, type);
196                         }
197                         if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
198                         break;
199                 case UCS2BE:
200                 case UCS2LE:
201                 case UTF_16BE:
202                 case UTF_16LE:
203                         l = 2;
204                         if (*inb < 2) goto starved;
205                         c = get_16((void *)*in, type);
206                         if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
207                         if ((unsigned)(c-0xd800) < 0x400) {
208                                 if (type-UCS2BE < 2U) goto ilseq;
209                                 l = 4;
210                                 if (*inb < 4) goto starved;
211                                 d = get_16((void *)(*in + 2), type);
212                                 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
213                                 c = ((c-0xd7c0)<<10) + (d-0xdc00);
214                         }
215                         break;
216                 case SHIFT_JIS:
217                         if (c-0xa1 <= 0xdf-0xa1) {
218                                 c += 0xff61-0xa1;
219                                 break;
220                         }
221                         l = 2;
222                         if (*inb < 2) goto starved;
223                         d = *((unsigned char *)*in + 1);
224                         if (c-129 <= 159-129) c -= 129;
225                         else if (c-224 <= 239-224) c -= 193;
226                         else goto ilseq;
227                         c *= 2;
228                         if (d-64 <= 158-64) {
229                                 if (d==127) goto ilseq;
230                                 if (d>127) d--;
231                                 d -= 64;
232                         } else if (d-159 <= 252-159) {
233                                 c++;
234                                 d -= 159;
235                         }
236                         c = jis0208[c][d];
237                         if (!c) goto ilseq;
238                         break;
239                 case EUC_JP:
240                         l = 2;
241                         if (*inb < 2) goto starved;
242                         d = *((unsigned char *)*in + 1);
243                         if (c==0x8e) {
244                                 c = d;
245                                 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
246                                 c += 0xff61 - 0xa1;
247                                 break;
248                         }
249                         c -= 0xa1;
250                         d -= 0xa1;
251                         if (c >= 84 || d >= 94) goto ilseq;
252                         c = jis0208[c][d];
253                         if (!c) goto ilseq;
254                         break;
255                 case GB2312:
256                         if (c < 0xa1) goto ilseq;
257                 case GBK:
258                 case GB18030:
259                         c -= 0x81;
260                         if (c >= 126) goto ilseq;
261                         l = 2;
262                         if (*inb < 2) goto starved;
263                         d = *((unsigned char *)*in + 1);
264                         if (d < 0xa1 && type == GB2312) goto ilseq;
265                         if (d-0x40>=191 || d==127) {
266                                 if (d-'0'>9 || type != GB18030)
267                                         goto ilseq;
268                                 l = 4;
269                                 if (*inb < 4) goto starved;
270                                 c = (10*c + d-'0') * 1260;
271                                 d = *((unsigned char *)*in + 2);
272                                 if (d-0x81>126) goto ilseq;
273                                 c += 10*(d-0x81);
274                                 d = *((unsigned char *)*in + 3);
275                                 if (d-'0'>9) goto ilseq;
276                                 c += d-'0';
277                                 c += 128;
278                                 for (d=0; d<=c; ) {
279                                         k = 0;
280                                         for (int i=0; i<126; i++)
281                                                 for (int j=0; j<190; j++)
282                                                         if (gb18030[i][j]-d <= c-d)
283                                                                 k++;
284                                         d = c+1;
285                                         c += k;
286                                 }
287                                 break;
288                         }
289                         d -= 0x40;
290                         if (d>63) d--;
291                         c = gb18030[c][d];
292                         break;
293                 case BIG5:
294                         l = 2;
295                         if (*inb < 2) goto starved;
296                         d = *((unsigned char *)*in + 1);
297                         if (c-0xa1>=0xfa-0xa1) goto ilseq;
298                         c -= 0xa1;
299                         if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
300                         d -= 0x40;
301                         if (d > 0x3e) d -= 0x22;
302                         c = big5[c][d];
303                         if (!c) goto ilseq;
304                         break;
305                 case EUC_KR:
306                         l = 2;
307                         if (*inb < 2) goto starved;
308                         d = *((unsigned char *)*in + 1);
309                         c -= 0xa1;
310                         d -= 0xa1;
311                         if (c >= 93 || d >= 94) {
312                                 c += (0xa1-0x81);
313                                 d += 0xa1;
314                                 if (c >= 93 || c>=0xc6-0x81 && d>0x52)
315                                         goto ilseq;
316                                 if (d-'A'<26) d = d-'A';
317                                 else if (d-'a'<26) d = d-'a'+26;
318                                 else if (d-0x81<0xff-0x81) d = d-0x81+52;
319                                 else goto ilseq;
320                                 if (c < 0x20) c = 178*c + d;
321                                 else c = 178*0x20 + 84*(c-0x20) + d;
322                                 c += 0xac00;
323                                 for (d=0xac00; d<=c; ) {
324                                         k = 0;
325                                         for (int i=0; i<93; i++)
326                                                 for (int j=0; j<94; j++)
327                                                         if (ksc[i][j]-d <= c-d)
328                                                                 k++;
329                                         d = c+1;
330                                         c += k;
331                                 }
332                                 break;
333                         }
334                         c = ksc[c][d];
335                         if (!c) goto ilseq;
336                         break;
337                 default:
338                         if (c < 128+type) break;
339                         c -= 128+type;
340                         c = legacy_chars[ map[c*5/4]>>2*c%8 |
341                                 map[c*5/4+1]<<8-2*c%8 & 1023 ];
342                         if (!c) c = *(unsigned char *)*in;
343                         if (c==1) goto ilseq;
344                 }
345
346                 switch (totype) {
347                 case WCHAR_T:
348                         if (*outb < sizeof(wchar_t)) goto toobig;
349                         *(wchar_t *)*out = c;
350                         *out += sizeof(wchar_t);
351                         *outb -= sizeof(wchar_t);
352                         break;
353                 case UTF_8:
354                         if (*outb < 4) {
355                                 char tmp[4];
356                                 k = wctomb_utf8(tmp, c);
357                                 if (*outb < k) goto toobig;
358                                 memcpy(*out, tmp, k);
359                         } else k = wctomb_utf8(*out, c);
360                         *out += k;
361                         *outb -= k;
362                         break;
363                 case US_ASCII:
364                         if (c > 0x7f) subst: x++, c='*';
365                 default:
366                         if (*outb < 1) goto toobig;
367                         if (c < 128+totype) {
368                         revout:
369                                 *(*out)++ = c;
370                                 *outb -= 1;
371                                 break;
372                         }
373                         d = c;
374                         for (c=0; c<128-totype; c++) {
375                                 if (d == legacy_chars[ tomap[c*5/4]>>2*c%8 |
376                                         tomap[c*5/4+1]<<8-2*c%8 & 1023 ]) {
377                                         c += 128;
378                                         goto revout;
379                                 }
380                         }
381                         goto subst;
382                 case UCS2BE:
383                 case UCS2LE:
384                 case UTF_16BE:
385                 case UTF_16LE:
386                         if (c < 0x10000 || type-UCS2BE < 2U) {
387                                 if (c >= 0x10000) c = 0xFFFD;
388                                 if (*outb < 2) goto toobig;
389                                 put_16((void *)*out, c, totype);
390                                 *out += 2;
391                                 *outb -= 2;
392                                 break;
393                         }
394                         if (*outb < 4) goto toobig;
395                         c -= 0x10000;
396                         put_16((void *)*out, (c>>10)|0xd800, totype);
397                         put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
398                         *out += 4;
399                         *outb -= 4;
400                         break;
401                 case UTF_32BE:
402                 case UTF_32LE:
403                         if (*outb < 4) goto toobig;
404                         put_32((void *)*out, c, totype);
405                         *out += 4;
406                         *outb -= 4;
407                         break;
408                 }
409         }
410         return x;
411 ilseq:
412         err = EILSEQ;
413         x = -1;
414         goto end;
415 toobig:
416         err = E2BIG;
417         x = -1;
418         goto end;
419 starved:
420         err = EINVAL;
421         x = -1;
422 end:
423         errno = err;
424         return x;
425 }