16 /* builtin charmaps */
19 /* only 0-7 are valid as dest charset */
29 /* additional charsets with algorithmic conversion */
34 /* some programs like php need this */
35 int _libiconv_version = _LIBICONV_VERSION;
37 /* these must match the constants above */
38 static const unsigned char charsets[] =
41 "\000" "UTF-16BE" "\0"
42 "\001" "UTF-16LE" "\0"
43 "\002" "UTF-32BE" "\0"
44 "\003" "UTF-32LE" "\0"
46 "\006" "US-ASCII" "\0"
47 "\007" "ISO-8859-1" "\0"
49 "\010" "ISO-8859-15""\0"
51 "\011" "ISO-8859-11""\0"
53 "\012" "JIS-0201" "\0"
56 /* separate identifiers for sbcs/dbcs/etc map type */
65 /* FIXME: these are not implemented yet
67 // GBK: 81-FE 40-7E,80-FE
68 // Big5: A1-FE 40-7E,A1-FE
71 static const unsigned short maplen[] = {
72 [UCS2_8BIT] = 4+ 2* 128,
73 [UCS3_8BIT] = 4+ 3* 128,
75 [SHIFT_JIS] = 4+ 2* 94*94,
76 [BIG5] = 4+ 2* 94*157,
77 [GBK] = 4+ 2* 126*190,
78 [EUC_TW] = 4+ 2* 2*94*94,
81 static int find_charmap(const char *name)
84 for (i = 0; i < (sizeof(charmaps) / sizeof(charmaps[0])); i++)
85 if (!strcasecmp(charmaps[i].name, name))
90 static int find_charset(const char *name)
92 const unsigned char *s;
93 for (s=charsets; *s<0xff && strcasecmp(s+1, name); s+=strlen(s)+1);
97 iconv_t iconv_open(const char *to, const char *from)
102 if ((t = find_charset(to)) > 8)
105 if ((f = find_charset(from)) < 255)
106 return 0 | (t<<1) | (f<<8);
108 if ((m = find_charmap(from)) > -1)
109 return 1 | (t<<1) | (m<<8);
114 int iconv_close(iconv_t cd)
119 static inline wchar_t get_16(const unsigned char *s, int endian)
122 return s[endian]<<8 | s[endian^1];
125 static inline void put_16(unsigned char *s, wchar_t c, int endian)
132 static inline int utf8enc_wchar(char *outb, wchar_t c)
138 else if (c <= 0x7FF) {
139 *outb++ = ((c >> 6) & 0x1F) | 0xC0;
140 *outb++ = ( c & 0x3F) | 0x80;
143 else if (c <= 0xFFFF) {
144 *outb++ = ((c >> 12) & 0x0F) | 0xE0;
145 *outb++ = ((c >> 6) & 0x3F) | 0x80;
146 *outb++ = ( c & 0x3F) | 0x80;
149 else if (c <= 0x10FFFF) {
150 *outb++ = ((c >> 18) & 0x07) | 0xF0;
151 *outb++ = ((c >> 12) & 0x3F) | 0x80;
152 *outb++ = ((c >> 6) & 0x3F) | 0x80;
153 *outb++ = ( c & 0x3F) | 0x80;
162 static inline int utf8seq_is_overlong(char *s, int n)
167 /* 1100000x (10xxxxxx) */
168 return (((*s >> 1) == 0x60) &&
169 ((*(s+1) >> 6) == 0x02));
172 /* 11100000 100xxxxx (10xxxxxx) */
173 return ((*s == 0xE0) &&
174 ((*(s+1) >> 5) == 0x04) &&
175 ((*(s+2) >> 6) == 0x02));
178 /* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */
179 return ((*s == 0xF0) &&
180 ((*(s+1) >> 4) == 0x08) &&
181 ((*(s+2) >> 6) == 0x02) &&
182 ((*(s+3) >> 6) == 0x02));
188 static inline int utf8seq_is_surrogate(char *s, int n)
190 return ((n == 3) && (*s == 0xED) && (*(s+1) >= 0xA0) && (*(s+1) <= 0xBF));
193 static inline int utf8seq_is_illegal(char *s, int n)
195 return ((n == 3) && (*s == 0xEF) && (*(s+1) == 0xBF) &&
196 (*(s+2) >= 0xBE) && (*(s+2) <= 0xBF));
199 static inline int utf8dec_wchar(wchar_t *c, unsigned char *in, size_t inb)
210 /* find utf8 sequence length */
211 if ((*in & 0xE0) == 0xC0) n = 2;
212 else if ((*in & 0xF0) == 0xE0) n = 3;
213 else if ((*in & 0xF8) == 0xF0) n = 4;
214 else if ((*in & 0xFC) == 0xF8) n = 5;
215 else if ((*in & 0xFE) == 0xFC) n = 6;
222 if (n > 1 && n < 5) {
223 /* reject invalid sequences */
224 if (utf8seq_is_overlong(in, n) ||
225 utf8seq_is_surrogate(in, n) ||
226 utf8seq_is_illegal(in, n))
230 *c = (char)(*in++ & (0x7F >> n));
232 for (i = 1; i < n; i++) {
233 /* illegal continuation byte */
234 if (*in < 0x80 || *in > 0xBF)
237 *c = (*c << 6) | (*in++ & 0x3F);
243 /* unmapped sequence (> 4) */
247 static inline char latin9_translit(wchar_t c)
249 /* a number of trivial iso-8859-15 <> utf-8 transliterations */
251 case 0x20AC: return 0xA4; /* Euro */
252 case 0x0160: return 0xA6; /* S caron */
253 case 0x0161: return 0xA8; /* s caron */
254 case 0x017D: return 0xB4; /* Z caron */
255 case 0x017E: return 0xB8; /* z caron */
256 case 0x0152: return 0xBC; /* OE */
257 case 0x0153: return 0xBD; /* oe */
258 case 0x0178: return 0xBE; /* Y diaeresis */
263 size_t iconv(iconv_t cd, char **in, size_t *inb, char **out, size_t *outb)
266 unsigned char to = (cd>>1)&127;
267 unsigned char from = 255;
268 const unsigned char *map = 0;
269 char tmp[MB_LEN_MAX];
274 if (!in || !*in || !*inb) return 0;
277 map = charmaps[cd>>8].map;
281 for (; *inb; *in+=l, *inb-=l) {
282 c = *(unsigned char *)*in;
284 if (from >= UTF_8 && c < 0x80) goto charok;
288 if (*inb < l) goto starved;
292 l = utf8dec_wchar(&c, *in, *inb);
294 else if (l == (size_t)-1) goto ilseq;
295 else if (l == (size_t)-2) goto starved;
300 if ((unsigned)c - 0xa4 <= 0xbe - 0xa4) {
301 static const unsigned char map[] = {
302 0, 0x60, 0, 0x61, 0, 0, 0, 0, 0, 0, 0,
303 0, 0, 0, 0, 0x7d, 0, 0, 0, 0x7e, 0, 0, 0,
306 if (c == 0xa4) c = 0x20ac;
307 else if (map[c-0xa5]) c = 0x100 | map[c-0xa5];
312 if (c >= 0xa1) c += 0x0e01-0xa1;
316 if (c <= 0xdf) c += 0xff61-0xa1;
323 if (*inb < 2) goto starved;
324 c = get_16(*in, from);
325 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
326 if ((unsigned)(c-0xd800) < 0x400) {
328 if (*inb < 4) goto starved;
329 d = get_16(*in + 2, from);
330 if ((unsigned)(c-0xdc00) >= 0x400) goto ilseq;
331 c = ((c-0xd800)<<10) | (d-0xdc00);
337 if (*inb < 4) goto starved;
339 // c = get_32(*in, from);
342 /* only support ascii supersets */
349 if ((unsigned)c - 0xa1 >= 94) goto ilseq;
350 if ((unsigned)in[0][1] - 0xa1 >= 94) goto ilseq;
351 c = (c-0xa1)*94 + (in[0][1]-0xa1);
355 if ((unsigned)c - 0xa1 <= 0xdf-0xa1) {
365 c = get_16(map + 4 + 2*c, 0);
366 if (c == 0xffff) goto ilseq;
370 if ((unsigned)c - 0xd800 < 0x800 || (unsigned)c >= 0x110000)
375 if (*outb < sizeof(wchar_t)) goto toobig;
376 *(wchar_t *)*out = c;
377 *out += sizeof(wchar_t);
378 *outb -= sizeof(wchar_t);
382 k = utf8enc_wchar(tmp, c);
383 if (*outb < k) goto toobig;
384 memcpy(*out, tmp, k);
385 } else k = utf8enc_wchar(*out, c);
390 if (c > 0x7f) c = 0xfffd;
391 /* fall thru and count replacement in latin1 case */
393 if (c >= 0x100 && c != 0xfffd)
394 c = latin9_translit(c);
397 if (!*outb) goto toobig;
398 if (c < 0x100) **out = c;
399 else x++, **out = '*'; //FIXME: translit?
406 if (*outb < 2) goto toobig;
412 if (*outb < 4) goto toobig;
413 put_16(*out, (c>>10)|0xd800, to);
414 put_16(*out + 2, (c&0x3ff)|0xdc00, to);