19 #define SHIFT_JIS 0321
21 /* FIXME: these are not implemented yet
23 * GBK: 81-FE 40-7E,80-FE
24 * Big5: A1-FE 40-7E,A1-FE
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\306"
43 "shiftjis\0sjis\0\0\321"
44 #include "codepages.h"
47 static const unsigned short legacy_chars[] = {
48 #include "legacychars.h"
51 static const unsigned short jis0208[84][94] = {
55 static int fuzzycmp(const unsigned char *a, const unsigned char *b)
57 for (; *a && *b; a++, b++) {
58 while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
59 if ((*a|32U) != *b) return 1;
64 static size_t find_charmap(const void *name)
66 const unsigned char *s;
67 for (s=charmaps; *s; ) {
68 if (!fuzzycmp(name, s)) {
69 for (; *s; s+=strlen((void *)s)+1);
72 s += strlen((void *)s)+1;
74 if (s[1] > 0200) s+=2;
75 else s+=2+(128U-s[1])/4*5;
81 iconv_t iconv_open(const char *to, const char *from)
85 if ((t = find_charmap(to))==-1 || (f = find_charmap(from))==-1) {
90 return (void *)(f<<16 | t);
93 int iconv_close(iconv_t cd)
98 static unsigned get_16(const unsigned char *s, int e)
101 return s[e]<<8 | s[1-e];
104 static void put_16(unsigned char *s, unsigned c, int e)
111 static unsigned get_32(const unsigned char *s, int e)
114 return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
117 static void put_32(unsigned char *s, unsigned c, int e)
126 /* Adapt as needed */
127 #define mbrtowc_utf8 mbrtowc
128 #define wctomb_utf8 wctomb
131 size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb)
134 unsigned long cd = (unsigned long)cd0;
135 unsigned to = cd & 0xffff;
136 unsigned from = cd >> 16;
137 const unsigned char *map = charmaps+from+1;
138 const unsigned char *tomap = charmaps+to+1;
144 unsigned char type = map[-1];
145 unsigned char totype = tomap[-1];
147 if (!in || !*in || !*inb) return 0;
149 for (; *inb; *in+=l, *inb-=l) {
150 c = *(unsigned char *)*in;
153 if (c >= 128) switch (type) {
155 l = mbrtowc_utf8(&wc, *in, *inb, &st);
157 else if (l == (size_t)-1) goto ilseq;
158 else if (l == (size_t)-2) goto starved;
165 if (*inb < l) goto starved;
171 if (*inb < 4) goto starved;
172 c = get_32((void *)*in, type);
174 if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
181 if (*inb < 2) goto starved;
182 c = get_16((void *)*in, type);
183 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
184 if ((unsigned)(c-0xd800) < 0x400) {
185 if (type-UCS2BE < 2U) goto ilseq;
187 if (*inb < 4) goto starved;
188 d = get_16((void *)(*in + 2), from);
189 if ((unsigned)(c-0xdc00) >= 0x400) goto ilseq;
190 c = ((c-0xd800)<<10) | (d-0xdc00);
194 if (c-0xa1 <= 0xdf-0xa1) {
199 if (*inb < 2) goto starved;
200 d = *((unsigned char *)*in + 1);
201 if (c-129 <= 159-129) c -= 129;
202 else if (c-224 <= 239-224) c -= 193;
205 if (d-64 <= 158-64) {
206 if (d==127) goto ilseq;
209 } else if (d-159 <= 252-159) {
218 if (*inb < 2) goto starved;
219 d = *((unsigned char *)*in + 1);
222 if (c-0xa1 > 0xdf-0xa1) goto ilseq;
228 if (c >= 84 || d >= 94) goto ilseq;
233 if (c < 128+type) break;
235 c = legacy_chars[ map[c*5/4]>>2*c%8 |
236 map[c*5/4+1]<<8-2*c%8 & 1023 ];
237 if (!c) c = *(unsigned char *)*in;
238 if (c==1) goto ilseq;
243 if (*outb < sizeof(wchar_t)) goto toobig;
244 *(wchar_t *)*out = c;
245 *out += sizeof(wchar_t);
246 *outb -= sizeof(wchar_t);
251 k = wctomb_utf8(tmp, c);
252 if (*outb < k) goto toobig;
253 memcpy(*out, tmp, k);
254 } else k = wctomb_utf8(*out, c);
259 if (c > 0x7f) subst: x++, c='*';
261 if (*outb < 1) goto toobig;
262 if (c < 128+totype) {
269 for (c=0; c<128-totype; c++) {
270 if (d == legacy_chars[ map[c*5/4]>>2*c%8 |
271 map[c*5/4+1]<<8-2*c%8 & 1023 ]) {
282 if (*outb < 2) goto toobig;
283 put_16((void *)*out, c, totype);
288 if (type-UCS2BE < 2U) goto ilseq;
289 if (*outb < 4) goto toobig;
290 put_16((void *)*out, (c>>10)|0xd800, totype);
291 put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
297 if (*outb < 4) goto toobig;
298 put_32((void *)*out, c, totype);