8 #include "locale_impl.h"
20 #define SHIFT_JIS 0321
21 #define ISO2022_JP 0322
28 /* Definitions of charmaps. Each charmap consists of:
29 * 1. Empty-string-terminated list of null-terminated aliases.
30 * 2. Special type code or number of elided entries.
31 * 3. Character table (size determined by field 2). */
33 static const unsigned char charmaps[] =
36 "ucs2\0ucs2be\0\0\304"
38 "utf16\0utf16be\0\0\302"
40 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
41 "ucs4le\0utf32le\0\0\303"
42 "ascii\0usascii\0iso646\0iso646us\0\0\307"
44 "shiftjis\0sjis\0\0\321"
49 "big5\0bigfive\0cp950\0big5hkscs\0\0\340"
50 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
51 #include "codepages.h"
54 static const unsigned short legacy_chars[] = {
55 #include "legacychars.h"
58 static const unsigned short jis0208[84][94] = {
62 static const unsigned short gb18030[126][190] = {
66 static const unsigned short big5[89][157] = {
70 static const unsigned short hkscs[] = {
74 static const unsigned short ksc[93][94] = {
78 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
80 for (; *a && *b; a++, b++) {
81 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
82 if ((*a|32U) != *b) return 1;
87 static size_t find_charmap(const void *name)
89 const unsigned char *s;
90 if (!*(char *)name) name=charmaps; /* "utf8" */
91 for (s=charmaps; *s; ) {
92 if (!fuzzycmp(name, s)) {
93 for (; *s; s+=strlen((void *)s)+1);
96 s += strlen((void *)s)+1;
98 if (s[1] > 0200) s+=2;
99 else s+=2+(128U-s[1])/4*5;
110 static iconv_t combine_to_from(size_t t, size_t f)
112 return (void *)(f<<16 | t<<1 | 1);
115 static size_t extract_from(iconv_t cd)
117 return (size_t)cd >> 16;
120 static size_t extract_to(iconv_t cd)
122 return (size_t)cd >> 1 & 0x7fff;
125 iconv_t iconv_open(const char *to, const char *from)
128 struct stateful_cd *scd;
130 if ((t = find_charmap(to))==-1
131 || (f = find_charmap(from))==-1
132 || (charmaps[t] >= 0320)) {
136 iconv_t cd = combine_to_from(t, f);
138 switch (charmaps[f]) {
140 scd = malloc(sizeof *scd);
141 if (!scd) return (iconv_t)-1;
150 static unsigned get_16(const unsigned char *s, int e)
153 return s[e]<<8 | s[1-e];
156 static void put_16(unsigned char *s, unsigned c, int e)
163 static unsigned get_32(const unsigned char *s, int e)
166 return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
169 static void put_32(unsigned char *s, unsigned c, int e)
178 /* Adapt as needed */
179 #define mbrtowc_utf8 mbrtowc
180 #define wctomb_utf8 wctomb
182 static unsigned legacy_map(const unsigned char *map, unsigned c)
184 unsigned x = c - 128 - map[-1];
185 x = legacy_chars[ map[x*5/4]>>2*x%8 |
186 map[x*5/4+1]<<8-2*x%8 & 1023 ];
190 size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
193 struct stateful_cd *scd=0;
194 if (!((size_t)cd & 1)) {
198 unsigned to = extract_to(cd);
199 unsigned from = extract_from(cd);
200 const unsigned char *map = charmaps+from+1;
201 const unsigned char *tomap = charmaps+to+1;
207 unsigned char type = map[-1];
208 unsigned char totype = tomap[-1];
209 locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
211 if (!in || !*in || !*inb) return 0;
215 for (; *inb; *in+=l, *inb-=l) {
216 c = *(unsigned char *)*in;
222 l = mbrtowc_utf8(&wc, *in, *inb, &st);
223 if (l == (size_t)-1) goto ilseq;
224 if (l == (size_t)-2) goto starved;
228 if (c >= 128) goto ilseq;
232 if (*inb < l) goto starved;
238 if (*inb < 4) goto starved;
239 c = get_32((void *)*in, type);
241 if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
248 if (*inb < 2) goto starved;
249 c = get_16((void *)*in, type);
250 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
251 if ((unsigned)(c-0xd800) < 0x400) {
252 if (type-UCS2BE < 2U) goto ilseq;
254 if (*inb < 4) goto starved;
255 d = get_16((void *)(*in + 2), type);
256 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
257 c = ((c-0xd7c0)<<10) + (d-0xdc00);
262 if (c-0xa1 <= 0xdf-0xa1) {
267 if (*inb < 2) goto starved;
268 d = *((unsigned char *)*in + 1);
269 if (c-129 <= 159-129) c -= 129;
270 else if (c-224 <= 239-224) c -= 193;
273 if (d-64 <= 158-64) {
274 if (d==127) goto ilseq;
277 } else if (d-159 <= 252-159) {
287 if (*inb < 2) goto starved;
288 d = *((unsigned char *)*in + 1);
291 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
297 if (c >= 84 || d >= 94) goto ilseq;
302 if (c >= 128) goto ilseq;
305 if (*inb < 3) goto starved;
306 c = *((unsigned char *)*in + 1);
307 d = *((unsigned char *)*in + 2);
308 if (c != '(' && c != '$') goto ilseq;
309 switch (128*(c=='$') + d) {
310 case 'B': scd->state=0; continue;
311 case 'J': scd->state=1; continue;
312 case 'I': scd->state=4; continue;
313 case 128+'@': scd->state=2; continue;
314 case 128+'B': scd->state=3; continue;
318 switch (scd->state) {
320 if (c=='\\') c = 0xa5;
321 if (c=='~') c = 0x203e;
326 if (*inb < 2) goto starved;
327 d = *((unsigned char *)*in + 1);
330 if (c >= 84 || d >= 94) goto ilseq;
335 if (c-0x60 < 0x1f) goto ilseq;
336 if (c-0x21 < 0x5e) c += 0xff61-0x21;
342 if (c < 0xa1) goto ilseq;
347 if (c >= 126) goto ilseq;
349 if (*inb < 2) goto starved;
350 d = *((unsigned char *)*in + 1);
351 if (d < 0xa1 && type == GB2312) goto ilseq;
352 if (d-0x40>=191 || d==127) {
353 if (d-'0'>9 || type != GB18030)
356 if (*inb < 4) goto starved;
357 c = (10*c + d-'0') * 1260;
358 d = *((unsigned char *)*in + 2);
359 if (d-0x81>126) goto ilseq;
361 d = *((unsigned char *)*in + 3);
362 if (d-'0'>9) goto ilseq;
367 for (int i=0; i<126; i++)
368 for (int j=0; j<190; j++)
369 if (gb18030[i][j]-d <= c-d)
383 if (*inb < 2) goto starved;
384 d = *((unsigned char *)*in + 1);
385 if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
387 if (d > 0x3e) d -= 0x22;
388 if (c-0xa1>=0xfa-0xa1) {
389 if (c-0x87>=0xff-0x87) goto ilseq;
390 if (c < 0xa1) c -= 0x87;
391 else c -= 0x87 + (0xfa-0xa1);
392 c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
394 /* A few HKSCS characters map to pairs of UCS
395 * characters. These are mapped to surrogate
396 * range in the hkscs table then hard-coded
397 * here. Ugly, yes. */
399 if (totype-0300U > 8) k = 2;
400 else k = "\10\4\4\10\4\4\10\2\4"[totype-0300];
401 if (k > *outb) goto toobig;
402 x += iconv(combine_to_from(to, 0),
403 &(char *){"\303\212\314\204"
407 +c%256}, &(size_t){4},
415 c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
421 if (*inb < 2) goto starved;
422 d = *((unsigned char *)*in + 1);
425 if (c >= 93 || d >= 94) {
428 if (c >= 93 || c>=0xc6-0x81 && d>0x52)
430 if (d-'A'<26) d = d-'A';
431 else if (d-'a'<26) d = d-'a'+26;
432 else if (d-0x81<0xff-0x81) d = d-0x81+52;
434 if (c < 0x20) c = 178*c + d;
435 else c = 178*0x20 + 84*(c-0x20) + d;
437 for (d=0xac00; d<=c; ) {
439 for (int i=0; i<93; i++)
440 for (int j=0; j<94; j++)
441 if (ksc[i][j]-d <= c-d)
452 if (c < 128+type) break;
453 c = legacy_map(map, c);
454 if (c==1) goto ilseq;
459 if (*outb < sizeof(wchar_t)) goto toobig;
460 *(wchar_t *)*out = c;
461 *out += sizeof(wchar_t);
462 *outb -= sizeof(wchar_t);
467 k = wctomb_utf8(tmp, c);
468 if (*outb < k) goto toobig;
469 memcpy(*out, tmp, k);
470 } else k = wctomb_utf8(*out, c);
475 if (c > 0x7f) subst: x++, c='*';
477 if (*outb < 1) goto toobig;
478 if (c < 128+totype || (c<256 && c==legacy_map(tomap, c))) {
485 for (c=128+totype; c<256; c++) {
486 if (d == legacy_map(tomap, c)) {
495 if (c < 0x10000 || type-UCS2BE < 2U) {
496 if (c >= 0x10000) c = 0xFFFD;
497 if (*outb < 2) goto toobig;
498 put_16((void *)*out, c, totype);
503 if (*outb < 4) goto toobig;
505 put_16((void *)*out, (c>>10)|0xd800, totype);
506 put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
512 if (*outb < 4) goto toobig;
513 put_32((void *)*out, c, totype);