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 "\006" "ISO646-US" "\0"
48 "\006" "ISO_646.IRV:1991" "\0"
49 "\006" "ISO-IR-6" "\0"
50 "\006" "ANSI_X3.4-1968" "\0"
51 "\006" "ANSI_X3.4-1986" "\0"
56 "\007" "ISO-8859-1" "\0"
58 "\010" "ISO-8859-15""\0"
60 "\011" "ISO-8859-11""\0"
62 "\012" "JIS-0201" "\0"
65 /* separate identifiers for sbcs/dbcs/etc map type */
74 /* FIXME: these are not implemented yet
76 // GBK: 81-FE 40-7E,80-FE
77 // Big5: A1-FE 40-7E,A1-FE
80 static const unsigned short maplen
[] = {
81 [UCS2_8BIT
] = 4+ 2* 128,
82 [UCS3_8BIT
] = 4+ 3* 128,
84 [SHIFT_JIS
] = 4+ 2* 94*94,
85 [BIG5
] = 4+ 2* 94*157,
86 [GBK
] = 4+ 2* 126*190,
87 [EUC_TW
] = 4+ 2* 2*94*94,
90 static int find_charmap(const char *name
)
93 for (i
= 0; i
< (sizeof(charmaps
) / sizeof(charmaps
[0])); i
++)
94 if (!strcasecmp(charmaps
[i
].name
, name
))
99 static int find_charset(const char *name
)
101 const unsigned char *s
;
102 for (s
=charsets
; *s
<0xff && strcasecmp(s
+1, name
); s
+=strlen(s
)+1);
106 iconv_t
iconv_open(const char *to
, const char *from
)
111 if ((t
= find_charset(to
)) > 8)
114 if ((f
= find_charset(from
)) < 255)
115 return 0 | (t
<<1) | (f
<<8);
117 if ((m
= find_charmap(from
)) > -1)
118 return 1 | (t
<<1) | (m
<<8);
123 int iconv_close(iconv_t cd
)
128 static inline wchar_t get_16(const unsigned char *s
, int endian
)
131 return s
[endian
]<<8 | s
[endian
^1];
134 static inline void put_16(unsigned char *s
, wchar_t c
, int endian
)
141 static inline int utf8enc_wchar(char *outb
, wchar_t c
)
147 else if (c
<= 0x7FF) {
148 *outb
++ = ((c
>> 6) & 0x1F) | 0xC0;
149 *outb
++ = ( c
& 0x3F) | 0x80;
152 else if (c
<= 0xFFFF) {
153 *outb
++ = ((c
>> 12) & 0x0F) | 0xE0;
154 *outb
++ = ((c
>> 6) & 0x3F) | 0x80;
155 *outb
++ = ( c
& 0x3F) | 0x80;
158 else if (c
<= 0x10FFFF) {
159 *outb
++ = ((c
>> 18) & 0x07) | 0xF0;
160 *outb
++ = ((c
>> 12) & 0x3F) | 0x80;
161 *outb
++ = ((c
>> 6) & 0x3F) | 0x80;
162 *outb
++ = ( c
& 0x3F) | 0x80;
171 static inline int utf8seq_is_overlong(char *s
, int n
)
176 /* 1100000x (10xxxxxx) */
177 return (((*s
>> 1) == 0x60) &&
178 ((*(s
+1) >> 6) == 0x02));
181 /* 11100000 100xxxxx (10xxxxxx) */
182 return ((*s
== 0xE0) &&
183 ((*(s
+1) >> 5) == 0x04) &&
184 ((*(s
+2) >> 6) == 0x02));
187 /* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */
188 return ((*s
== 0xF0) &&
189 ((*(s
+1) >> 4) == 0x08) &&
190 ((*(s
+2) >> 6) == 0x02) &&
191 ((*(s
+3) >> 6) == 0x02));
197 static inline int utf8seq_is_surrogate(char *s
, int n
)
199 return ((n
== 3) && (*s
== 0xED) && (*(s
+1) >= 0xA0) && (*(s
+1) <= 0xBF));
202 static inline int utf8seq_is_illegal(char *s
, int n
)
204 return ((n
== 3) && (*s
== 0xEF) && (*(s
+1) == 0xBF) &&
205 (*(s
+2) >= 0xBE) && (*(s
+2) <= 0xBF));
208 static inline int utf8dec_wchar(wchar_t *c
, unsigned char *in
, size_t inb
)
219 /* find utf8 sequence length */
220 if ((*in
& 0xE0) == 0xC0) n
= 2;
221 else if ((*in
& 0xF0) == 0xE0) n
= 3;
222 else if ((*in
& 0xF8) == 0xF0) n
= 4;
223 else if ((*in
& 0xFC) == 0xF8) n
= 5;
224 else if ((*in
& 0xFE) == 0xFC) n
= 6;
231 if (n
> 1 && n
< 5) {
232 /* reject invalid sequences */
233 if (utf8seq_is_overlong(in
, n
) ||
234 utf8seq_is_surrogate(in
, n
) ||
235 utf8seq_is_illegal(in
, n
))
239 *c
= (char)(*in
++ & (0x7F >> n
));
241 for (i
= 1; i
< n
; i
++) {
242 /* illegal continuation byte */
243 if (*in
< 0x80 || *in
> 0xBF)
246 *c
= (*c
<< 6) | (*in
++ & 0x3F);
252 /* unmapped sequence (> 4) */
256 static inline wchar_t latin9_translit(wchar_t c
)
258 /* a number of trivial iso-8859-15 <> utf-8 transliterations */
260 case 0x20AC: return 0xA4; /* Euro */
261 case 0x0160: return 0xA6; /* S caron */
262 case 0x0161: return 0xA8; /* s caron */
263 case 0x017D: return 0xB4; /* Z caron */
264 case 0x017E: return 0xB8; /* z caron */
265 case 0x0152: return 0xBC; /* OE */
266 case 0x0153: return 0xBD; /* oe */
267 case 0x0178: return 0xBE; /* Y diaeresis */
268 default: return 0xFFFD; /* cannot translate */
272 size_t iconv(iconv_t cd
, char **in
, size_t *inb
, char **out
, size_t *outb
)
275 unsigned char to
= (cd
>>1)&127;
276 unsigned char from
= 255;
277 const unsigned char *map
= 0;
278 char tmp
[MB_LEN_MAX
];
283 if (!in
|| !*in
|| !*inb
) return 0;
286 map
= charmaps
[cd
>>8].map
;
290 for (; *inb
; *in
+=l
, *inb
-=l
) {
291 c
= *(unsigned char *)*in
;
293 if (from
>= UTF_8
&& c
< 0x80) goto charok
;
297 if (*inb
< l
) goto starved
;
301 l
= utf8dec_wchar(&c
, *in
, *inb
);
303 else if (l
== (size_t)-1) goto ilseq
;
304 else if (l
== (size_t)-2) goto starved
;
309 if ((unsigned)c
- 0xa4 <= 0xbe - 0xa4) {
310 static const unsigned char map
[] = {
311 0, 0x60, 0, 0x61, 0, 0, 0, 0, 0, 0, 0,
312 0, 0, 0, 0, 0x7d, 0, 0, 0, 0x7e, 0, 0, 0,
315 if (c
== 0xa4) c
= 0x20ac;
316 else if (map
[c
-0xa5]) c
= 0x100 | map
[c
-0xa5];
321 if (c
>= 0xa1) c
+= 0x0e01-0xa1;
325 if (c
<= 0xdf) c
+= 0xff61-0xa1;
332 if (*inb
< 2) goto starved
;
333 c
= get_16(*in
, from
);
334 if ((unsigned)(c
-0xdc00) < 0x400) goto ilseq
;
335 if ((unsigned)(c
-0xd800) < 0x400) {
337 if (*inb
< 4) goto starved
;
338 d
= get_16(*in
+ 2, from
);
339 if ((unsigned)(c
-0xdc00) >= 0x400) goto ilseq
;
340 c
= ((c
-0xd800)<<10) | (d
-0xdc00);
346 if (*inb
< 4) goto starved
;
348 // c = get_32(*in, from);
351 /* only support ascii supersets */
358 if ((unsigned)c
- 0xa1 >= 94) goto ilseq
;
359 if ((unsigned)in
[0][1] - 0xa1 >= 94) goto ilseq
;
360 c
= (c
-0xa1)*94 + (in
[0][1]-0xa1);
364 if ((unsigned)c
- 0xa1 <= 0xdf-0xa1) {
374 c
= get_16(map
+ 4 + 2*c
, 0);
375 if (c
== 0xffff) goto ilseq
;
379 if ((unsigned)c
- 0xd800 < 0x800 || (unsigned)c
>= 0x110000)
384 if (*outb
< sizeof(wchar_t)) goto toobig
;
385 *(wchar_t *)*out
= c
;
386 *out
+= sizeof(wchar_t);
387 *outb
-= sizeof(wchar_t);
391 k
= utf8enc_wchar(tmp
, c
);
392 if (*outb
< k
) goto toobig
;
393 memcpy(*out
, tmp
, k
);
394 } else k
= utf8enc_wchar(*out
, c
);
399 if (c
> 0x7f) c
= 0xfffd;
400 /* fall thru and count replacement in latin1 case */
402 if (c
>= 0x100 && c
!= 0xfffd)
403 c
= latin9_translit(c
);
406 if (c
> 0xff) goto ilseq
;
407 if (!*outb
) goto toobig
;
415 if (*outb
< 2) goto toobig
;
421 if (*outb
< 4) goto toobig
;
422 put_16(*out
, (c
>>10)|0xd800, to
);
423 put_16(*out
+ 2, (c
&0x3ff)|0xdc00, to
);