19 /* FIXME: these are not implemented yet
21 * GBK: 81-FE 40-7E,80-FE
22 * Big5: A1-FE 40-7E,A1-FE
25 /* Definitions of charmaps. Each charmap consists of:
26 * 1. Empty-string-terminated list of null-terminated aliases.
27 * 2. Special type code or number of elided entries.
28 * 3. Character table (size determined by field 2). */
30 static const unsigned char charmaps[] =
33 "ucs2\0ucs2be\0\0\304"
35 "utf16\0utf16be\0\0\302"
37 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
38 "ucs4le\0utf32le\0\0\303"
39 "ascii\0usascii\0iso646\0iso646us\0\0\306"
40 #include "codepages.h"
43 static const unsigned short legacy_chars[] = {
44 #include "legacychars.h"
47 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
49 for (; *a && *b; a++, b++) {
50 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
51 if ((*a|32U) != *b) return 1;
56 static size_t find_charmap(const void *name)
58 const unsigned char *s;
59 for (s=charmaps; *s; ) {
60 if (!fuzzycmp(name, s)) {
61 for (; *s; s+=strlen((void *)s)+1);
64 s += strlen((void *)s)+1;
66 if (s[1] > 0200) s+=2;
67 else s+=2+(128U-s[1])/4*5;
73 iconv_t iconv_open(const char *to, const char *from)
77 if ((t = find_charmap(to))==-1 || (f = find_charmap(from))==-1) {
82 return (void *)(f<<16 | t);
85 int iconv_close(iconv_t cd)
90 static unsigned get_16(const unsigned char *s, int e)
93 return s[e]<<8 | s[1-e];
96 static void put_16(unsigned char *s, unsigned c, int e)
103 static unsigned get_32(const unsigned char *s, int e)
106 return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
109 static void put_32(unsigned char *s, unsigned c, int e)
118 /* Adapt as needed */
119 #define mbrtowc_utf8 mbrtowc
120 #define wctomb_utf8 wctomb
123 size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb)
126 unsigned long cd = (unsigned long)cd0;
127 unsigned to = cd & 0xffff;
128 unsigned from = cd >> 16;
129 const unsigned char *map = charmaps+from+1;
130 const unsigned char *tomap = charmaps+to+1;
136 unsigned char type = map[-1];
137 unsigned char totype = tomap[-1];
139 if (!in || !*in || !*inb) return 0;
141 for (; *inb; *in+=l, *inb-=l) {
142 c = *(unsigned char *)*in;
145 if (c >= 128) switch (type) {
147 l = mbrtowc_utf8(&wc, *in, *inb, &st);
149 else if (l == (size_t)-1) goto ilseq;
150 else if (l == (size_t)-2) goto starved;
157 if (*inb < l) goto starved;
163 if (*inb < 4) goto starved;
164 c = get_32((void *)*in, type);
166 if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
173 if (*inb < 2) goto starved;
174 c = get_16((void *)*in, type);
175 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
176 if ((unsigned)(c-0xd800) < 0x400) {
177 if (type-UCS2BE < 2U) goto ilseq;
179 if (*inb < 4) goto starved;
180 d = get_16((void *)(*in + 2), from);
181 if ((unsigned)(c-0xdc00) >= 0x400) goto ilseq;
182 c = ((c-0xd800)<<10) | (d-0xdc00);
186 if (c < 128+type) break;
188 c = legacy_chars[ map[c*5/4]>>2*c%8 |
189 map[c*5/4+1]<<8-2*c%8 & 1023 ];
190 if (!c) c = *(unsigned char *)*in;
191 if (c==1) goto ilseq;
196 if (*outb < sizeof(wchar_t)) goto toobig;
197 *(wchar_t *)*out = c;
198 *out += sizeof(wchar_t);
199 *outb -= sizeof(wchar_t);
204 k = wctomb_utf8(tmp, c);
205 if (*outb < k) goto toobig;
206 memcpy(*out, tmp, k);
207 } else k = wctomb_utf8(*out, c);
212 if (c > 0x7f) subst: x++, c='*';
214 if (*outb < 1) goto toobig;
215 if (c < 128+totype) {
222 for (c=0; c<128-totype; c++) {
223 if (d == legacy_chars[ map[c*5/4]>>2*c%8 |
224 map[c*5/4+1]<<8-2*c%8 & 1023 ]) {
235 if (*outb < 2) goto toobig;
236 put_16((void *)*out, c, totype);
241 if (type-UCS2BE < 2U) goto ilseq;
242 if (*outb < 4) goto toobig;
243 put_16((void *)*out, (c>>10)|0xd800, totype);
244 put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
250 if (*outb < 4) goto toobig;
251 put_32((void *)*out, c, totype);