19 #define SHIFT_JIS 0321
25 /* FIXME: these are not implemented yet
27 * GBK: 81-FE 40-7E,80-FE
28 * Big5: A1-FE 40-7E,A1-FE
31 /* Definitions of charmaps. Each charmap consists of:
32 * 1. Empty-string-terminated list of null-terminated aliases.
33 * 2. Special type code or number of elided entries.
34 * 3. Character table (size determined by field 2). */
36 static const unsigned char charmaps[] =
39 "ucs2\0ucs2be\0\0\304"
41 "utf16\0utf16be\0\0\302"
43 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
44 "ucs4le\0utf32le\0\0\303"
45 "ascii\0usascii\0iso646\0iso646us\0\0\307"
47 "shiftjis\0sjis\0\0\321"
51 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
52 #include "codepages.h"
55 static const unsigned short legacy_chars[] = {
56 #include "legacychars.h"
59 static const unsigned short jis0208[84][94] = {
63 static const unsigned short gb18030[126][190] = {
67 static const unsigned short ksc[93][94] = {
71 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
73 for (; *a && *b; a++, b++) {
74 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
75 if ((*a|32U) != *b) return 1;
80 static size_t find_charmap(const void *name)
82 const unsigned char *s;
83 for (s=charmaps; *s; ) {
84 if (!fuzzycmp(name, s)) {
85 for (; *s; s+=strlen((void *)s)+1);
88 s += strlen((void *)s)+1;
90 if (s[1] > 0200) s+=2;
91 else s+=2+(128U-s[1])/4*5;
97 iconv_t iconv_open(const char *to, const char *from)
101 if ((t = find_charmap(to))==-1
102 || (f = find_charmap(from))==-1
103 || (charmaps[t] >= 0320)) {
108 return (void *)(f<<16 | t);
111 int iconv_close(iconv_t cd)
116 static unsigned get_16(const unsigned char *s, int e)
119 return s[e]<<8 | s[1-e];
122 static void put_16(unsigned char *s, unsigned c, int e)
129 static unsigned get_32(const unsigned char *s, int e)
132 return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
135 static void put_32(unsigned char *s, unsigned c, int e)
144 /* Adapt as needed */
145 #define mbrtowc_utf8 mbrtowc
146 #define wctomb_utf8 wctomb
148 size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
151 unsigned long cd = (unsigned long)cd0;
152 unsigned to = cd & 0xffff;
153 unsigned from = cd >> 16;
154 const unsigned char *map = charmaps+from+1;
155 const unsigned char *tomap = charmaps+to+1;
161 unsigned char type = map[-1];
162 unsigned char totype = tomap[-1];
164 if (!in || !*in || !*inb) return 0;
166 for (; *inb; *in+=l, *inb-=l) {
167 c = *(unsigned char *)*in;
170 if (c >= 128 || type-UTF_32BE < 7U) switch (type) {
172 l = mbrtowc_utf8(&wc, *in, *inb, &st);
174 else if (l == (size_t)-1) goto ilseq;
175 else if (l == (size_t)-2) goto starved;
182 if (*inb < l) goto starved;
188 if (*inb < 4) goto starved;
189 c = get_32((void *)*in, type);
191 if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
198 if (*inb < 2) goto starved;
199 c = get_16((void *)*in, type);
200 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
201 if ((unsigned)(c-0xd800) < 0x400) {
202 if (type-UCS2BE < 2U) goto ilseq;
204 if (*inb < 4) goto starved;
205 d = get_16((void *)(*in + 2), type);
206 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
207 c = ((c-0xd7c0)<<10) + (d-0xdc00);
211 if (c-0xa1 <= 0xdf-0xa1) {
216 if (*inb < 2) goto starved;
217 d = *((unsigned char *)*in + 1);
218 if (c-129 <= 159-129) c -= 129;
219 else if (c-224 <= 239-224) c -= 193;
222 if (d-64 <= 158-64) {
223 if (d==127) goto ilseq;
226 } else if (d-159 <= 252-159) {
235 if (*inb < 2) goto starved;
236 d = *((unsigned char *)*in + 1);
239 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
245 if (c >= 84 || d >= 94) goto ilseq;
250 if (c < 0xa1) goto ilseq;
254 if (c >= 126) goto ilseq;
256 if (*inb < 2) goto starved;
257 d = *((unsigned char *)*in + 1);
258 if (d < 0xa1 && type == GB2312) goto ilseq;
259 if (d-0x40>=191 || d==127) {
260 if (d-'0'>9 || type != GB18030)
263 if (*inb < 4) goto starved;
264 c = (10*c + d-'0') * 1260;
265 d = *((unsigned char *)*in + 2);
266 if (d-0x81>126) goto ilseq;
268 d = *((unsigned char *)*in + 3);
269 if (d-'0'>9) goto ilseq;
274 for (int i=0; i<126; i++)
275 for (int j=0; j<190; j++)
276 if (gb18030[i][j]-d <= c-d)
289 if (*inb < 2) goto starved;
290 d = *((unsigned char *)*in + 1);
293 if (c >= 93 || d >= 94) {
296 if (c >= 93 || c>=0xc6-0x81 && d>0x52)
298 if (d-'A'<26) d = d-'A';
299 else if (d-'a'<26) d = d-'a'+26;
300 else if (d-0x81<0xff-0x81) d = d-0x81+52;
302 if (c < 0x20) c = 178*c + d;
303 else c = 178*0x20 + 84*(c-0x20) + d;
305 for (d=0xac00; d<=c; ) {
307 for (int i=0; i<93; i++)
308 for (int j=0; j<94; j++)
309 if (ksc[i][j]-d <= c-d)
320 if (c < 128+type) break;
322 c = legacy_chars[ map[c*5/4]>>2*c%8 |
323 map[c*5/4+1]<<8-2*c%8 & 1023 ];
324 if (!c) c = *(unsigned char *)*in;
325 if (c==1) goto ilseq;
330 if (*outb < sizeof(wchar_t)) goto toobig;
331 *(wchar_t *)*out = c;
332 *out += sizeof(wchar_t);
333 *outb -= sizeof(wchar_t);
338 k = wctomb_utf8(tmp, c);
339 if (*outb < k) goto toobig;
340 memcpy(*out, tmp, k);
341 } else k = wctomb_utf8(*out, c);
346 if (c > 0x7f) subst: x++, c='*';
348 if (*outb < 1) goto toobig;
349 if (c < 128+totype) {
356 for (c=0; c<128-totype; c++) {
357 if (d == legacy_chars[ tomap[c*5/4]>>2*c%8 |
358 tomap[c*5/4+1]<<8-2*c%8 & 1023 ]) {
368 if (c < 0x10000 || type-UCS2BE < 2U) {
369 if (c >= 0x10000) c = 0xFFFD;
370 if (*outb < 2) goto toobig;
371 put_16((void *)*out, c, totype);
376 if (*outb < 4) goto toobig;
378 put_16((void *)*out, (c>>10)|0xd800, totype);
379 put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
385 if (*outb < 4) goto toobig;
386 put_32((void *)*out, c, totype);