19 #define SHIFT_JIS 0321
26 /* FIXME: these are not implemented yet
28 * GBK: 81-FE 40-7E,80-FE
29 * Big5: A1-FE 40-7E,A1-FE
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). */
37 static const unsigned char charmaps[] =
40 "ucs2\0ucs2be\0\0\304"
42 "utf16\0utf16be\0\0\302"
44 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
45 "ucs4le\0utf32le\0\0\303"
46 "ascii\0usascii\0iso646\0iso646us\0\0\307"
48 "shiftjis\0sjis\0\0\321"
52 "big5\0bigfive\0cp950\0big5hkscs\0\0\340"
53 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
54 #include "codepages.h"
57 static const unsigned short legacy_chars[] = {
58 #include "legacychars.h"
61 static const unsigned short jis0208[84][94] = {
65 static const unsigned short gb18030[126][190] = {
69 static const unsigned short big5[89][157] = {
73 static const unsigned short hkscs[] = {
77 static const unsigned short ksc[93][94] = {
81 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
83 for (; *a && *b; a++, b++) {
84 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
85 if ((*a|32U) != *b) return 1;
90 static size_t find_charmap(const void *name)
92 const unsigned char *s;
93 if (!*(char *)name) name=charmaps; /* "utf8" */
94 for (s=charmaps; *s; ) {
95 if (!fuzzycmp(name, s)) {
96 for (; *s; s+=strlen((void *)s)+1);
99 s += strlen((void *)s)+1;
101 if (s[1] > 0200) s+=2;
102 else s+=2+(128U-s[1])/4*5;
108 iconv_t iconv_open(const char *to, const char *from)
112 if ((t = find_charmap(to))==-1
113 || (f = find_charmap(from))==-1
114 || (charmaps[t] >= 0320)) {
119 return (void *)(f<<16 | t);
122 int iconv_close(iconv_t cd)
127 static unsigned get_16(const unsigned char *s, int e)
130 return s[e]<<8 | s[1-e];
133 static void put_16(unsigned char *s, unsigned c, int e)
140 static unsigned get_32(const unsigned char *s, int e)
143 return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
146 static void put_32(unsigned char *s, unsigned c, int e)
155 /* Adapt as needed */
156 #define mbrtowc_utf8 mbrtowc
157 #define wctomb_utf8 wctomb
159 size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
162 unsigned long cd = (unsigned long)cd0;
163 unsigned to = cd & 0xffff;
164 unsigned from = cd >> 16;
165 const unsigned char *map = charmaps+from+1;
166 const unsigned char *tomap = charmaps+to+1;
172 unsigned char type = map[-1];
173 unsigned char totype = tomap[-1];
175 if (!in || !*in || !*inb) return 0;
177 for (; *inb; *in+=l, *inb-=l) {
178 c = *(unsigned char *)*in;
181 if (c >= 128 || type-UTF_32BE < 7U) switch (type) {
183 l = mbrtowc_utf8(&wc, *in, *inb, &st);
185 else if (l == (size_t)-1) goto ilseq;
186 else if (l == (size_t)-2) goto starved;
193 if (*inb < l) goto starved;
199 if (*inb < 4) goto starved;
200 c = get_32((void *)*in, type);
202 if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
209 if (*inb < 2) goto starved;
210 c = get_16((void *)*in, type);
211 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
212 if ((unsigned)(c-0xd800) < 0x400) {
213 if (type-UCS2BE < 2U) goto ilseq;
215 if (*inb < 4) goto starved;
216 d = get_16((void *)(*in + 2), type);
217 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
218 c = ((c-0xd7c0)<<10) + (d-0xdc00);
222 if (c-0xa1 <= 0xdf-0xa1) {
227 if (*inb < 2) goto starved;
228 d = *((unsigned char *)*in + 1);
229 if (c-129 <= 159-129) c -= 129;
230 else if (c-224 <= 239-224) c -= 193;
233 if (d-64 <= 158-64) {
234 if (d==127) goto ilseq;
237 } else if (d-159 <= 252-159) {
246 if (*inb < 2) goto starved;
247 d = *((unsigned char *)*in + 1);
250 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
256 if (c >= 84 || d >= 94) goto ilseq;
261 if (c < 0xa1) goto ilseq;
265 if (c >= 126) goto ilseq;
267 if (*inb < 2) goto starved;
268 d = *((unsigned char *)*in + 1);
269 if (d < 0xa1 && type == GB2312) goto ilseq;
270 if (d-0x40>=191 || d==127) {
271 if (d-'0'>9 || type != GB18030)
274 if (*inb < 4) goto starved;
275 c = (10*c + d-'0') * 1260;
276 d = *((unsigned char *)*in + 2);
277 if (d-0x81>126) goto ilseq;
279 d = *((unsigned char *)*in + 3);
280 if (d-'0'>9) goto ilseq;
285 for (int i=0; i<126; i++)
286 for (int j=0; j<190; j++)
287 if (gb18030[i][j]-d <= c-d)
300 if (*inb < 2) goto starved;
301 d = *((unsigned char *)*in + 1);
302 if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
304 if (d > 0x3e) d -= 0x22;
305 if (c-0xa1>=0xfa-0xa1) {
306 if (c-0x87>=0xff-0x87) goto ilseq;
307 if (c < 0xa1) c -= 0x87;
308 else c -= 0x87 + (0xfa-0xa1);
309 c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
311 /* A few HKSCS characters map to pairs of UCS
312 * characters. These are mapped to surrogate
313 * range in the hkscs table then hard-coded
314 * here. Ugly, yes. */
316 if (totype-0300U > 8) k = 2;
317 else k = "\10\4\4\10\4\4\10\2\4"[totype-0300];
318 if (k > *outb) goto toobig;
319 x += iconv((iconv_t)(uintptr_t)to,
320 &(char *){"\303\212\314\204"
324 +c%256}, &(size_t){4},
332 c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
337 if (*inb < 2) goto starved;
338 d = *((unsigned char *)*in + 1);
341 if (c >= 93 || d >= 94) {
344 if (c >= 93 || c>=0xc6-0x81 && d>0x52)
346 if (d-'A'<26) d = d-'A';
347 else if (d-'a'<26) d = d-'a'+26;
348 else if (d-0x81<0xff-0x81) d = d-0x81+52;
350 if (c < 0x20) c = 178*c + d;
351 else c = 178*0x20 + 84*(c-0x20) + d;
353 for (d=0xac00; d<=c; ) {
355 for (int i=0; i<93; i++)
356 for (int j=0; j<94; j++)
357 if (ksc[i][j]-d <= c-d)
368 if (c < 128+type) break;
370 c = legacy_chars[ map[c*5/4]>>2*c%8 |
371 map[c*5/4+1]<<8-2*c%8 & 1023 ];
372 if (!c) c = *(unsigned char *)*in;
373 if (c==1) goto ilseq;
378 if (*outb < sizeof(wchar_t)) goto toobig;
379 *(wchar_t *)*out = c;
380 *out += sizeof(wchar_t);
381 *outb -= sizeof(wchar_t);
386 k = wctomb_utf8(tmp, c);
387 if (*outb < k) goto toobig;
388 memcpy(*out, tmp, k);
389 } else k = wctomb_utf8(*out, c);
394 if (c > 0x7f) subst: x++, c='*';
396 if (*outb < 1) goto toobig;
397 if (c < 128+totype) {
404 for (c=0; c<128-totype; c++) {
405 if (d == legacy_chars[ tomap[c*5/4]>>2*c%8 |
406 tomap[c*5/4+1]<<8-2*c%8 & 1023 ]) {
416 if (c < 0x10000 || type-UCS2BE < 2U) {
417 if (c >= 0x10000) c = 0xFFFD;
418 if (*outb < 2) goto toobig;
419 put_16((void *)*out, c, totype);
424 if (*outb < 4) goto toobig;
426 put_16((void *)*out, (c>>10)|0xd800, totype);
427 put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
433 if (*outb < 4) goto toobig;
434 put_32((void *)*out, c, totype);