19 #define SHIFT_JIS 0321
26 /* Definitions of charmaps. Each charmap consists of:
27 * 1. Empty-string-terminated list of null-terminated aliases.
28 * 2. Special type code or number of elided entries.
29 * 3. Character table (size determined by field 2). */
31 static const unsigned char charmaps[] =
34 "ucs2\0ucs2be\0\0\304"
36 "utf16\0utf16be\0\0\302"
38 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
39 "ucs4le\0utf32le\0\0\303"
40 "ascii\0usascii\0iso646\0iso646us\0\0\307"
42 "shiftjis\0sjis\0\0\321"
46 "big5\0bigfive\0cp950\0big5hkscs\0\0\340"
47 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
48 #include "codepages.h"
51 static const unsigned short legacy_chars[] = {
52 #include "legacychars.h"
55 static const unsigned short jis0208[84][94] = {
59 static const unsigned short gb18030[126][190] = {
63 static const unsigned short big5[89][157] = {
67 static const unsigned short hkscs[] = {
71 static const unsigned short ksc[93][94] = {
75 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
77 for (; *a && *b; a++, b++) {
78 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
79 if ((*a|32U) != *b) return 1;
84 static size_t find_charmap(const void *name)
86 const unsigned char *s;
87 if (!*(char *)name) name=charmaps; /* "utf8" */
88 for (s=charmaps; *s; ) {
89 if (!fuzzycmp(name, s)) {
90 for (; *s; s+=strlen((void *)s)+1);
93 s += strlen((void *)s)+1;
95 if (s[1] > 0200) s+=2;
96 else s+=2+(128U-s[1])/4*5;
102 iconv_t iconv_open(const char *to, const char *from)
106 if ((t = find_charmap(to))==-1
107 || (f = find_charmap(from))==-1
108 || (charmaps[t] >= 0320)) {
113 return (void *)(f<<16 | t);
116 int iconv_close(iconv_t cd)
121 static unsigned get_16(const unsigned char *s, int e)
124 return s[e]<<8 | s[1-e];
127 static void put_16(unsigned char *s, unsigned c, int e)
134 static unsigned get_32(const unsigned char *s, int e)
137 return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
140 static void put_32(unsigned char *s, unsigned c, int e)
149 /* Adapt as needed */
150 #define mbrtowc_utf8 mbrtowc
151 #define wctomb_utf8 wctomb
153 size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
156 unsigned long cd = (unsigned long)cd0;
157 unsigned to = cd & 0xffff;
158 unsigned from = cd >> 16;
159 const unsigned char *map = charmaps+from+1;
160 const unsigned char *tomap = charmaps+to+1;
166 unsigned char type = map[-1];
167 unsigned char totype = tomap[-1];
169 if (!in || !*in || !*inb) return 0;
171 for (; *inb; *in+=l, *inb-=l) {
172 c = *(unsigned char *)*in;
175 if (c >= 128 || type-UTF_32BE < 7U) switch (type) {
177 l = mbrtowc_utf8(&wc, *in, *inb, &st);
179 else if (l == (size_t)-1) goto ilseq;
180 else if (l == (size_t)-2) goto starved;
187 if (*inb < l) goto starved;
193 if (*inb < 4) goto starved;
194 c = get_32((void *)*in, type);
196 if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
203 if (*inb < 2) goto starved;
204 c = get_16((void *)*in, type);
205 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
206 if ((unsigned)(c-0xd800) < 0x400) {
207 if (type-UCS2BE < 2U) goto ilseq;
209 if (*inb < 4) goto starved;
210 d = get_16((void *)(*in + 2), type);
211 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
212 c = ((c-0xd7c0)<<10) + (d-0xdc00);
216 if (c-0xa1 <= 0xdf-0xa1) {
221 if (*inb < 2) goto starved;
222 d = *((unsigned char *)*in + 1);
223 if (c-129 <= 159-129) c -= 129;
224 else if (c-224 <= 239-224) c -= 193;
227 if (d-64 <= 158-64) {
228 if (d==127) goto ilseq;
231 } else if (d-159 <= 252-159) {
240 if (*inb < 2) goto starved;
241 d = *((unsigned char *)*in + 1);
244 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
250 if (c >= 84 || d >= 94) goto ilseq;
255 if (c < 0xa1) goto ilseq;
259 if (c >= 126) goto ilseq;
261 if (*inb < 2) goto starved;
262 d = *((unsigned char *)*in + 1);
263 if (d < 0xa1 && type == GB2312) goto ilseq;
264 if (d-0x40>=191 || d==127) {
265 if (d-'0'>9 || type != GB18030)
268 if (*inb < 4) goto starved;
269 c = (10*c + d-'0') * 1260;
270 d = *((unsigned char *)*in + 2);
271 if (d-0x81>126) goto ilseq;
273 d = *((unsigned char *)*in + 3);
274 if (d-'0'>9) goto ilseq;
279 for (int i=0; i<126; i++)
280 for (int j=0; j<190; j++)
281 if (gb18030[i][j]-d <= c-d)
294 if (*inb < 2) goto starved;
295 d = *((unsigned char *)*in + 1);
296 if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
298 if (d > 0x3e) d -= 0x22;
299 if (c-0xa1>=0xfa-0xa1) {
300 if (c-0x87>=0xff-0x87) goto ilseq;
301 if (c < 0xa1) c -= 0x87;
302 else c -= 0x87 + (0xfa-0xa1);
303 c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
305 /* A few HKSCS characters map to pairs of UCS
306 * characters. These are mapped to surrogate
307 * range in the hkscs table then hard-coded
308 * here. Ugly, yes. */
310 if (totype-0300U > 8) k = 2;
311 else k = "\10\4\4\10\4\4\10\2\4"[totype-0300];
312 if (k > *outb) goto toobig;
313 x += iconv((iconv_t)(uintptr_t)to,
314 &(char *){"\303\212\314\204"
318 +c%256}, &(size_t){4},
326 c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
331 if (*inb < 2) goto starved;
332 d = *((unsigned char *)*in + 1);
335 if (c >= 93 || d >= 94) {
338 if (c >= 93 || c>=0xc6-0x81 && d>0x52)
340 if (d-'A'<26) d = d-'A';
341 else if (d-'a'<26) d = d-'a'+26;
342 else if (d-0x81<0xff-0x81) d = d-0x81+52;
344 if (c < 0x20) c = 178*c + d;
345 else c = 178*0x20 + 84*(c-0x20) + d;
347 for (d=0xac00; d<=c; ) {
349 for (int i=0; i<93; i++)
350 for (int j=0; j<94; j++)
351 if (ksc[i][j]-d <= c-d)
362 if (c < 128+type) break;
364 c = legacy_chars[ map[c*5/4]>>2*c%8 |
365 map[c*5/4+1]<<8-2*c%8 & 1023 ];
366 if (!c) c = *(unsigned char *)*in;
367 if (c==1) goto ilseq;
372 if (*outb < sizeof(wchar_t)) goto toobig;
373 *(wchar_t *)*out = c;
374 *out += sizeof(wchar_t);
375 *outb -= sizeof(wchar_t);
380 k = wctomb_utf8(tmp, c);
381 if (*outb < k) goto toobig;
382 memcpy(*out, tmp, k);
383 } else k = wctomb_utf8(*out, c);
388 if (c > 0x7f) subst: x++, c='*';
390 if (*outb < 1) goto toobig;
391 if (c < 128+totype) {
398 for (c=0; c<128-totype; c++) {
399 if (d == legacy_chars[ tomap[c*5/4]>>2*c%8 |
400 tomap[c*5/4+1]<<8-2*c%8 & 1023 ]) {
410 if (c < 0x10000 || type-UCS2BE < 2U) {
411 if (c >= 0x10000) c = 0xFFFD;
412 if (*outb < 2) goto toobig;
413 put_16((void *)*out, c, totype);
418 if (*outb < 4) goto toobig;
420 put_16((void *)*out, (c>>10)|0xd800, totype);
421 put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
427 if (*outb < 4) goto toobig;
428 put_32((void *)*out, c, totype);