legacy japanese charset support in iconv (only from, not to)
[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 US_ASCII    0306
16 #define WCHAR_T     0307
17 #define UTF_8       0310
18 #define EUC_JP      0320
19 #define SHIFT_JIS   0321
20
21 /* FIXME: these are not implemented yet
22  * EUC:   A1-FE A1-FE
23  * GBK:   81-FE 40-7E,80-FE
24  * Big5:  A1-FE 40-7E,A1-FE
25  */
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\0\0\310"
34 "wchart\0\0\307"
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\306"
42 "eucjp\0\0\320"
43 "shiftjis\0sjis\0\0\321"
44 #include "codepages.h"
45 ;
46
47 static const unsigned short legacy_chars[] = {
48 #include "legacychars.h"
49 };
50
51 static const unsigned short jis0208[84][94] = {
52 #include "jis0208.h"
53 };
54
55 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
56 {
57         for (; *a && *b; a++, b++) {
58                 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
59                 if ((*a|32U) != *b) return 1;
60         }
61         return *a != *b;
62 }
63
64 static size_t find_charmap(const void *name)
65 {
66         const unsigned char *s;
67         for (s=charmaps; *s; ) {
68                 if (!fuzzycmp(name, s)) {
69                         for (; *s; s+=strlen((void *)s)+1);
70                         return s+1-charmaps;
71                 }
72                 s += strlen((void *)s)+1;
73                 if (!*s) {
74                         if (s[1] > 0200) s+=2;
75                         else s+=2+(128U-s[1])/4*5;
76                 }
77         }
78         return -1;
79 }
80
81 iconv_t iconv_open(const char *to, const char *from)
82 {
83         size_t f, t;
84
85         if ((t = find_charmap(to))==-1 || (f = find_charmap(from))==-1) {
86                 errno = EINVAL;
87                 return (iconv_t)-1;
88         }
89
90         return (void *)(f<<16 | t);
91 }
92
93 int iconv_close(iconv_t cd)
94 {
95         return 0;
96 }
97
98 static unsigned get_16(const unsigned char *s, int e)
99 {
100         e &= 1;
101         return s[e]<<8 | s[1-e];
102 }
103
104 static void put_16(unsigned char *s, unsigned c, int e)
105 {
106         e &= 1;
107         s[e] = c>>8;
108         s[1-e] = c;
109 }
110
111 static unsigned get_32(const unsigned char *s, int e)
112 {
113         e &= 3;
114         return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
115 }
116
117 static void put_32(unsigned char *s, unsigned c, int e)
118 {
119         e &= 3;
120         s[e^0] = c>>24;
121         s[e^1] = c>>16;
122         s[e^2] = c>>8;
123         s[e^3] = c;
124 }
125
126 /* Adapt as needed */
127 #define mbrtowc_utf8 mbrtowc
128 #define wctomb_utf8 wctomb
129
130 #include <stdio.h>
131 size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb)
132 {
133         size_t x=0;
134         unsigned long cd = (unsigned long)cd0;
135         unsigned to = cd & 0xffff;
136         unsigned from = cd >> 16;
137         const unsigned char *map = charmaps+from+1;
138         const unsigned char *tomap = charmaps+to+1;
139         mbstate_t st = {0};
140         wchar_t wc;
141         unsigned c, d;
142         size_t k, l;
143         int err;
144         unsigned char type = map[-1];
145         unsigned char totype = tomap[-1];
146
147         if (!in || !*in || !*inb) return 0;
148
149         for (; *inb; *in+=l, *inb-=l) {
150                 c = *(unsigned char *)*in;
151                 l = 1;
152
153                 if (c >= 128) switch (type) {
154                 case UTF_8:
155                         l = mbrtowc_utf8(&wc, *in, *inb, &st);
156                         if (!l) l++;
157                         else if (l == (size_t)-1) goto ilseq;
158                         else if (l == (size_t)-2) goto starved;
159                         c = wc;
160                         break;
161                 case US_ASCII:
162                         goto ilseq;
163                 case WCHAR_T:
164                         l = sizeof(wchar_t);
165                         if (*inb < l) goto starved;
166                         c = *(wchar_t *)*in;
167                         if (0) {
168                 case UTF_32BE:
169                 case UTF_32LE:
170                         l = 4;
171                         if (*inb < 4) goto starved;
172                         c = get_32((void *)*in, type);
173                         }
174                         if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
175                         break;
176                 case UCS2BE:
177                 case UCS2LE:
178                 case UTF_16BE:
179                 case UTF_16LE:
180                         l = 2;
181                         if (*inb < 2) goto starved;
182                         c = get_16((void *)*in, type);
183                         if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
184                         if ((unsigned)(c-0xd800) < 0x400) {
185                                 if (type-UCS2BE < 2U) goto ilseq;
186                                 l = 4;
187                                 if (*inb < 4) goto starved;
188                                 d = get_16((void *)(*in + 2), from);
189                                 if ((unsigned)(c-0xdc00) >= 0x400) goto ilseq;
190                                 c = ((c-0xd800)<<10) | (d-0xdc00);
191                         }
192                         break;
193                 case SHIFT_JIS:
194                         if (c-0xa1 <= 0xdf-0xa1) {
195                                 c += 0xff61-0xa1;
196                                 break;
197                         }
198                         l = 2;
199                         if (*inb < 2) goto starved;
200                         d = *((unsigned char *)*in + 1);
201                         if (c-129 <= 159-129) c -= 129;
202                         else if (c-224 <= 239-224) c -= 193;
203                         else goto ilseq;
204                         c *= 2;
205                         if (d-64 <= 158-64) {
206                                 if (d==127) goto ilseq;
207                                 if (d>127) d--;
208                                 d -= 64;
209                         } else if (d-159 <= 252-159) {
210                                 c++;
211                                 d -= 159;
212                         }
213                         c = jis0208[c][d];
214                         if (!c) goto ilseq;
215                         break;
216                 case EUC_JP:
217                         l = 2;
218                         if (*inb < 2) goto starved;
219                         d = *((unsigned char *)*in + 1);
220                         if (c==0x8e) {
221                                 c = d;
222                                 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
223                                 c += 0xff61 - 0xa1;
224                                 break;
225                         }
226                         c -= 0xa1;
227                         d -= 0xa1;
228                         if (c >= 84 || d >= 94) goto ilseq;
229                         c = jis0208[c][d];
230                         if (!c) goto ilseq;
231                         break;
232                 default:
233                         if (c < 128+type) break;
234                         c -= 128+type;
235                         c = legacy_chars[ map[c*5/4]>>2*c%8 |
236                                 map[c*5/4+1]<<8-2*c%8 & 1023 ];
237                         if (!c) c = *(unsigned char *)*in;
238                         if (c==1) goto ilseq;
239                 }
240
241                 switch (totype) {
242                 case WCHAR_T:
243                         if (*outb < sizeof(wchar_t)) goto toobig;
244                         *(wchar_t *)*out = c;
245                         *out += sizeof(wchar_t);
246                         *outb -= sizeof(wchar_t);
247                         break;
248                 case UTF_8:
249                         if (*outb < 4) {
250                                 char tmp[4];
251                                 k = wctomb_utf8(tmp, c);
252                                 if (*outb < k) goto toobig;
253                                 memcpy(*out, tmp, k);
254                         } else k = wctomb_utf8(*out, c);
255                         *out += k;
256                         *outb -= k;
257                         break;
258                 case US_ASCII:
259                         if (c > 0x7f) subst: x++, c='*';
260                 default:
261                         if (*outb < 1) goto toobig;
262                         if (c < 128+totype) {
263                         revout:
264                                 *(*out)++ = c;
265                                 *outb -= 1;
266                                 break;
267                         }
268                         d = c;
269                         for (c=0; c<128-totype; c++) {
270                                 if (d == legacy_chars[ map[c*5/4]>>2*c%8 |
271                                         map[c*5/4+1]<<8-2*c%8 & 1023 ]) {
272                                         c += 128;
273                                         goto revout;
274                                 }
275                         }
276                         goto subst;
277                 case UCS2BE:
278                 case UCS2LE:
279                 case UTF_16BE:
280                 case UTF_16LE:
281                         if (c < 0x10000) {
282                                 if (*outb < 2) goto toobig;
283                                 put_16((void *)*out, c, totype);
284                                 *out += 2;
285                                 *outb -= 2;
286                                 break;
287                         }
288                         if (type-UCS2BE < 2U) goto ilseq;
289                         if (*outb < 4) goto toobig;
290                         put_16((void *)*out, (c>>10)|0xd800, totype);
291                         put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
292                         *out += 4;
293                         *outb -= 4;
294                         break;
295                 case UTF_32BE:
296                 case UTF_32LE:
297                         if (*outb < 4) goto toobig;
298                         put_32((void *)*out, c, totype);
299                         *out += 4;
300                         *outb -= 4;
301                         break;
302                 }
303         }
304         return x;
305 ilseq:
306         err = EILSEQ;
307         x = -1;
308         goto end;
309 toobig:
310         err = E2BIG;
311         x = -1;
312         goto end;
313 starved:
314         err = EINVAL;
315         x = -1;
316 end:
317         errno = err;
318         return x;
319 }