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 quads of entries.
31 * 3. Character table (size determined by field 2), consisting
32 * of 5 bytes for every 4 characters, interpreted as 10-bit
33 * indices into the legacy_chars table. */
35 static const unsigned char charmaps[] =
38 "ucs2\0ucs2be\0\0\304"
40 "utf16\0utf16be\0\0\302"
42 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
43 "ucs4le\0utf32le\0\0\303"
44 "ascii\0usascii\0iso646\0iso646us\0\0\307"
46 "shiftjis\0sjis\0\0\321"
51 "big5\0bigfive\0cp950\0big5hkscs\0\0\340"
52 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
53 #include "codepages.h"
56 /* Table of characters that appear in legacy 8-bit codepages,
57 * limited to 1024 slots (10 bit indices). The first 256 entries
58 * are elided since those characters are obviously all included. */
59 static const unsigned short legacy_chars[] = {
60 #include "legacychars.h"
63 static const unsigned short jis0208[84][94] = {
67 static const unsigned short gb18030[126][190] = {
71 static const unsigned short big5[89][157] = {
75 static const unsigned short hkscs[] = {
79 static const unsigned short ksc[93][94] = {
83 static const unsigned short rev_jis[] = {
87 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
89 for (; *a && *b; a++, b++) {
90 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
91 if ((*a|32U) != *b) return 1;
96 static size_t find_charmap(const void *name)
98 const unsigned char *s;
99 if (!*(char *)name) name=charmaps; /* "utf8" */
100 for (s=charmaps; *s; ) {
101 if (!fuzzycmp(name, s)) {
102 for (; *s; s+=strlen((void *)s)+1);
105 s += strlen((void *)s)+1;
107 if (s[1] > 0200) s+=2;
108 else s+=2+(64U-s[1])*5;
119 static iconv_t combine_to_from(size_t t, size_t f)
121 return (void *)(f<<16 | t<<1 | 1);
124 static size_t extract_from(iconv_t cd)
126 return (size_t)cd >> 16;
129 static size_t extract_to(iconv_t cd)
131 return (size_t)cd >> 1 & 0x7fff;
134 iconv_t iconv_open(const char *to, const char *from)
137 struct stateful_cd *scd;
139 if ((t = find_charmap(to))==-1
140 || (f = find_charmap(from))==-1
141 || (charmaps[t] >= 0330)) {
145 iconv_t cd = combine_to_from(t, f);
147 switch (charmaps[f]) {
149 scd = malloc(sizeof *scd);
150 if (!scd) return (iconv_t)-1;
159 static unsigned get_16(const unsigned char *s, int e)
162 return s[e]<<8 | s[1-e];
165 static void put_16(unsigned char *s, unsigned c, int e)
172 static unsigned get_32(const unsigned char *s, int e)
175 return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
178 static void put_32(unsigned char *s, unsigned c, int e)
187 /* Adapt as needed */
188 #define mbrtowc_utf8 mbrtowc
189 #define wctomb_utf8 wctomb
191 static unsigned legacy_map(const unsigned char *map, unsigned c)
193 if (c < 4*map[-1]) return c;
194 unsigned x = c - 4*map[-1];
195 x = map[x*5/4]>>2*x%8 | map[x*5/4+1]<<8-2*x%8 & 1023;
196 return x < 256 ? x : legacy_chars[x-256];
199 static unsigned uni_to_jis(unsigned c)
201 unsigned nel = sizeof rev_jis / sizeof *rev_jis;
202 unsigned d, j, i, b = 0;
206 d = jis0208[j/256][j%256];
207 if (d==c) return j + 0x2121;
208 else if (nel == 1) return 0;
218 size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
221 struct stateful_cd *scd=0;
222 if (!((size_t)cd & 1)) {
226 unsigned to = extract_to(cd);
227 unsigned from = extract_from(cd);
228 const unsigned char *map = charmaps+from+1;
229 const unsigned char *tomap = charmaps+to+1;
235 unsigned char type = map[-1];
236 unsigned char totype = tomap[-1];
237 locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
239 if (!in || !*in || !*inb) return 0;
243 for (; *inb; *in+=l, *inb-=l) {
244 c = *(unsigned char *)*in;
250 l = mbrtowc_utf8(&wc, *in, *inb, &st);
251 if (l == (size_t)-1) goto ilseq;
252 if (l == (size_t)-2) goto starved;
256 if (c >= 128) goto ilseq;
260 if (*inb < l) goto starved;
266 if (*inb < 4) goto starved;
267 c = get_32((void *)*in, type);
269 if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
276 if (*inb < 2) goto starved;
277 c = get_16((void *)*in, type);
278 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
279 if ((unsigned)(c-0xd800) < 0x400) {
280 if (type-UCS2BE < 2U) goto ilseq;
282 if (*inb < 4) goto starved;
283 d = get_16((void *)(*in + 2), type);
284 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
285 c = ((c-0xd7c0)<<10) + (d-0xdc00);
290 if (c-0xa1 <= 0xdf-0xa1) {
295 if (*inb < 2) goto starved;
296 d = *((unsigned char *)*in + 1);
297 if (c-129 <= 159-129) c -= 129;
298 else if (c-224 <= 239-224) c -= 193;
301 if (d-64 <= 158-64) {
302 if (d==127) goto ilseq;
305 } else if (d-159 <= 252-159) {
315 if (*inb < 2) goto starved;
316 d = *((unsigned char *)*in + 1);
319 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
325 if (c >= 84 || d >= 94) goto ilseq;
330 if (c >= 128) goto ilseq;
333 if (*inb < 3) goto starved;
334 c = *((unsigned char *)*in + 1);
335 d = *((unsigned char *)*in + 2);
336 if (c != '(' && c != '$') goto ilseq;
337 switch (128*(c=='$') + d) {
338 case 'B': scd->state=0; continue;
339 case 'J': scd->state=1; continue;
340 case 'I': scd->state=4; continue;
341 case 128+'@': scd->state=2; continue;
342 case 128+'B': scd->state=3; continue;
346 switch (scd->state) {
348 if (c=='\\') c = 0xa5;
349 if (c=='~') c = 0x203e;
354 if (*inb < 2) goto starved;
355 d = *((unsigned char *)*in + 1);
358 if (c >= 84 || d >= 94) goto ilseq;
363 if (c-0x60 < 0x1f) goto ilseq;
364 if (c-0x21 < 0x5e) c += 0xff61-0x21;
370 if (c < 0xa1) goto ilseq;
375 if (c >= 126) goto ilseq;
377 if (*inb < 2) goto starved;
378 d = *((unsigned char *)*in + 1);
379 if (d < 0xa1 && type == GB2312) goto ilseq;
380 if (d-0x40>=191 || d==127) {
381 if (d-'0'>9 || type != GB18030)
384 if (*inb < 4) goto starved;
385 c = (10*c + d-'0') * 1260;
386 d = *((unsigned char *)*in + 2);
387 if (d-0x81>126) goto ilseq;
389 d = *((unsigned char *)*in + 3);
390 if (d-'0'>9) goto ilseq;
395 for (int i=0; i<126; i++)
396 for (int j=0; j<190; j++)
397 if (gb18030[i][j]-d <= c-d)
411 if (*inb < 2) goto starved;
412 d = *((unsigned char *)*in + 1);
413 if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
415 if (d > 0x3e) d -= 0x22;
416 if (c-0xa1>=0xfa-0xa1) {
417 if (c-0x87>=0xff-0x87) goto ilseq;
418 if (c < 0xa1) c -= 0x87;
419 else c -= 0x87 + (0xfa-0xa1);
420 c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
422 /* A few HKSCS characters map to pairs of UCS
423 * characters. These are mapped to surrogate
424 * range in the hkscs table then hard-coded
425 * here. Ugly, yes. */
427 if (totype-0300U > 8) k = 2;
428 else k = "\10\4\4\10\4\4\10\2\4"[totype-0300];
429 if (k > *outb) goto toobig;
430 x += iconv(combine_to_from(to, 0),
431 &(char *){"\303\212\314\204"
435 +c%256}, &(size_t){4},
443 c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
449 if (*inb < 2) goto starved;
450 d = *((unsigned char *)*in + 1);
453 if (c >= 93 || d >= 94) {
456 if (c >= 93 || c>=0xc6-0x81 && d>0x52)
458 if (d-'A'<26) d = d-'A';
459 else if (d-'a'<26) d = d-'a'+26;
460 else if (d-0x81<0xff-0x81) d = d-0x81+52;
462 if (c < 0x20) c = 178*c + d;
463 else c = 178*0x20 + 84*(c-0x20) + d;
465 for (d=0xac00; d<=c; ) {
467 for (int i=0; i<93; i++)
468 for (int j=0; j<94; j++)
469 if (ksc[i][j]-d <= c-d)
481 c = legacy_map(map, c);
487 if (*outb < sizeof(wchar_t)) goto toobig;
488 *(wchar_t *)*out = c;
489 *out += sizeof(wchar_t);
490 *outb -= sizeof(wchar_t);
495 k = wctomb_utf8(tmp, c);
496 if (*outb < k) goto toobig;
497 memcpy(*out, tmp, k);
498 } else k = wctomb_utf8(*out, c);
503 if (c > 0x7f) subst: x++, c='*';
505 if (*outb < 1) goto toobig;
506 if (c<256 && c==legacy_map(tomap, c)) {
513 for (c=4*totype; c<256; c++) {
514 if (d == legacy_map(tomap, c)) {
520 if (c < 128) goto revout;
531 if (c-0xff61 <= 0xdf-0xa1) {
537 if (*outb < 2) goto toobig;
540 *(*out)++ = (c+1)/2 + (c<95 ? 112 : 176);
541 *(*out)++ = c%2 ? d + 31 + d/96 : d + 126;
545 if (c < 128) goto revout;
546 if (c-0xff61 <= 0xdf-0xa1) {
547 c += 0x0e00 + 0x21 - 0xff61;
552 if (*outb < 2) goto toobig;
553 *(*out)++ = c/256 + 0x80;
554 *(*out)++ = c%256 + 0x80;
558 if (c < 128) goto revout;
559 if (c-0xff61 <= 0xdf-0xa1 || c==0xa5 || c==0x203e) {
560 if (*outb < 7) goto toobig;
566 } else if (c==0x203e) {
571 *(*out)++ = c-0xff61+0x21;
581 if (*outb < 8) goto toobig;
596 if (c < 0x10000 || type-UCS2BE < 2U) {
597 if (c >= 0x10000) c = 0xFFFD;
598 if (*outb < 2) goto toobig;
599 put_16((void *)*out, c, totype);
604 if (*outb < 4) goto toobig;
606 put_16((void *)*out, (c>>10)|0xd800, totype);
607 put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
613 if (*outb < 4) goto toobig;
614 put_32((void *)*out, c, totype);