8 #include "locale_impl.h"
20 #define SHIFT_JIS 0321
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). */
32 static const unsigned char charmaps[] =
35 "ucs2\0ucs2be\0\0\304"
37 "utf16\0utf16be\0\0\302"
39 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
40 "ucs4le\0utf32le\0\0\303"
41 "ascii\0usascii\0iso646\0iso646us\0\0\307"
43 "shiftjis\0sjis\0\0\321"
47 "big5\0bigfive\0cp950\0big5hkscs\0\0\340"
48 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
49 #include "codepages.h"
52 static const unsigned short legacy_chars[] = {
53 #include "legacychars.h"
56 static const unsigned short jis0208[84][94] = {
60 static const unsigned short gb18030[126][190] = {
64 static const unsigned short big5[89][157] = {
68 static const unsigned short hkscs[] = {
72 static const unsigned short ksc[93][94] = {
76 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
78 for (; *a && *b; a++, b++) {
79 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
80 if ((*a|32U) != *b) return 1;
85 static size_t find_charmap(const void *name)
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);
94 s += strlen((void *)s)+1;
96 if (s[1] > 0200) s+=2;
97 else s+=2+(128U-s[1])/4*5;
103 static iconv_t combine_to_from(size_t t, size_t f)
105 return (void *)(f<<16 | t);
108 static size_t extract_from(iconv_t cd)
110 return (size_t)cd >> 16;
113 static size_t extract_to(iconv_t cd)
115 return (size_t)cd & 0xffff;
118 iconv_t iconv_open(const char *to, const char *from)
122 if ((t = find_charmap(to))==-1
123 || (f = find_charmap(from))==-1
124 || (charmaps[t] >= 0320)) {
129 return combine_to_from(t, f);
132 static unsigned get_16(const unsigned char *s, int e)
135 return s[e]<<8 | s[1-e];
138 static void put_16(unsigned char *s, unsigned c, int e)
145 static unsigned get_32(const unsigned char *s, int e)
148 return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
151 static void put_32(unsigned char *s, unsigned c, int e)
160 /* Adapt as needed */
161 #define mbrtowc_utf8 mbrtowc
162 #define wctomb_utf8 wctomb
164 static unsigned legacy_map(const unsigned char *map, unsigned c)
166 unsigned x = c - 128 - map[-1];
167 x = legacy_chars[ map[x*5/4]>>2*x%8 |
168 map[x*5/4+1]<<8-2*x%8 & 1023 ];
172 size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
175 unsigned to = extract_to(cd);
176 unsigned from = extract_from(cd);
177 const unsigned char *map = charmaps+from+1;
178 const unsigned char *tomap = charmaps+to+1;
184 unsigned char type = map[-1];
185 unsigned char totype = tomap[-1];
186 locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
188 if (!in || !*in || !*inb) return 0;
192 for (; *inb; *in+=l, *inb-=l) {
193 c = *(unsigned char *)*in;
199 l = mbrtowc_utf8(&wc, *in, *inb, &st);
200 if (l == (size_t)-1) goto ilseq;
201 if (l == (size_t)-2) goto starved;
205 if (c >= 128) goto ilseq;
209 if (*inb < l) goto starved;
215 if (*inb < 4) goto starved;
216 c = get_32((void *)*in, type);
218 if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
225 if (*inb < 2) goto starved;
226 c = get_16((void *)*in, type);
227 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
228 if ((unsigned)(c-0xd800) < 0x400) {
229 if (type-UCS2BE < 2U) goto ilseq;
231 if (*inb < 4) goto starved;
232 d = get_16((void *)(*in + 2), type);
233 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
234 c = ((c-0xd7c0)<<10) + (d-0xdc00);
239 if (c-0xa1 <= 0xdf-0xa1) {
244 if (*inb < 2) goto starved;
245 d = *((unsigned char *)*in + 1);
246 if (c-129 <= 159-129) c -= 129;
247 else if (c-224 <= 239-224) c -= 193;
250 if (d-64 <= 158-64) {
251 if (d==127) goto ilseq;
254 } else if (d-159 <= 252-159) {
264 if (*inb < 2) goto starved;
265 d = *((unsigned char *)*in + 1);
268 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
274 if (c >= 84 || d >= 94) goto ilseq;
280 if (c < 0xa1) goto ilseq;
285 if (c >= 126) goto ilseq;
287 if (*inb < 2) goto starved;
288 d = *((unsigned char *)*in + 1);
289 if (d < 0xa1 && type == GB2312) goto ilseq;
290 if (d-0x40>=191 || d==127) {
291 if (d-'0'>9 || type != GB18030)
294 if (*inb < 4) goto starved;
295 c = (10*c + d-'0') * 1260;
296 d = *((unsigned char *)*in + 2);
297 if (d-0x81>126) goto ilseq;
299 d = *((unsigned char *)*in + 3);
300 if (d-'0'>9) goto ilseq;
305 for (int i=0; i<126; i++)
306 for (int j=0; j<190; j++)
307 if (gb18030[i][j]-d <= c-d)
321 if (*inb < 2) goto starved;
322 d = *((unsigned char *)*in + 1);
323 if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
325 if (d > 0x3e) d -= 0x22;
326 if (c-0xa1>=0xfa-0xa1) {
327 if (c-0x87>=0xff-0x87) goto ilseq;
328 if (c < 0xa1) c -= 0x87;
329 else c -= 0x87 + (0xfa-0xa1);
330 c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
332 /* A few HKSCS characters map to pairs of UCS
333 * characters. These are mapped to surrogate
334 * range in the hkscs table then hard-coded
335 * here. Ugly, yes. */
337 if (totype-0300U > 8) k = 2;
338 else k = "\10\4\4\10\4\4\10\2\4"[totype-0300];
339 if (k > *outb) goto toobig;
340 x += iconv(combine_to_from(to, 0),
341 &(char *){"\303\212\314\204"
345 +c%256}, &(size_t){4},
353 c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
359 if (*inb < 2) goto starved;
360 d = *((unsigned char *)*in + 1);
363 if (c >= 93 || d >= 94) {
366 if (c >= 93 || c>=0xc6-0x81 && d>0x52)
368 if (d-'A'<26) d = d-'A';
369 else if (d-'a'<26) d = d-'a'+26;
370 else if (d-0x81<0xff-0x81) d = d-0x81+52;
372 if (c < 0x20) c = 178*c + d;
373 else c = 178*0x20 + 84*(c-0x20) + d;
375 for (d=0xac00; d<=c; ) {
377 for (int i=0; i<93; i++)
378 for (int j=0; j<94; j++)
379 if (ksc[i][j]-d <= c-d)
390 if (c < 128+type) break;
391 c = legacy_map(map, c);
392 if (c==1) goto ilseq;
397 if (*outb < sizeof(wchar_t)) goto toobig;
398 *(wchar_t *)*out = c;
399 *out += sizeof(wchar_t);
400 *outb -= sizeof(wchar_t);
405 k = wctomb_utf8(tmp, c);
406 if (*outb < k) goto toobig;
407 memcpy(*out, tmp, k);
408 } else k = wctomb_utf8(*out, c);
413 if (c > 0x7f) subst: x++, c='*';
415 if (*outb < 1) goto toobig;
416 if (c < 128+totype || (c<256 && c==legacy_map(tomap, c))) {
423 for (c=128+totype; c<256; c++) {
424 if (d == legacy_map(tomap, c)) {
433 if (c < 0x10000 || type-UCS2BE < 2U) {
434 if (c >= 0x10000) c = 0xFFFD;
435 if (*outb < 2) goto toobig;
436 put_16((void *)*out, c, totype);
441 if (*outb < 4) goto toobig;
443 put_16((void *)*out, (c>>10)|0xd800, totype);
444 put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
450 if (*outb < 4) goto toobig;
451 put_32((void *)*out, c, totype);