[packages] libiconv: add ASCII as alias to US-ASCII, fixes various glib applications
[openwrt/svn-archive/archive.git] / libs / libiconv / src / iconv.c
1 #include <iconv.h>
2 #include <errno.h>
3 #include <wchar.h>
4 #include <string.h>
5 #include <strings.h>
6 #include <stdlib.h>
7 #include <limits.h>
8
9 #include <dirent.h>
10 #include <fcntl.h>
11 #include <sys/mman.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <stdint.h>
15
16 /* builtin charmaps */
17 #include "charmaps.h"
18
19 /* only 0-7 are valid as dest charset */
20 #define UTF_16BE 000
21 #define UTF_16LE 001
22 #define UTF_32BE 002
23 #define UTF_32LE 003
24 #define WCHAR_T 004
25 #define UTF_8 005
26 #define US_ASCII 006
27 #define LATIN_1 007
28
29 /* additional charsets with algorithmic conversion */
30 #define LATIN_9 010
31 #define TIS_620 011
32 #define JIS_0201 012
33
34 /* some programs like php need this */
35 int _libiconv_version = _LIBICONV_VERSION;
36
37 /* these must match the constants above */
38 static const unsigned char charsets[] =
39 "\005" "UTF-8" "\0"
40 "\004" "WCHAR_T" "\0"
41 "\000" "UTF-16BE" "\0"
42 "\001" "UTF-16LE" "\0"
43 "\002" "UTF-32BE" "\0"
44 "\003" "UTF-32LE" "\0"
45 "\006" "ASCII" "\0"
46 "\006" "US-ASCII" "\0"
47 "\007" "ISO-8859-1" "\0"
48 "\007" "LATIN1" "\0"
49 "\010" "ISO-8859-15""\0"
50 "\010" "LATIN9" "\0"
51 "\011" "ISO-8859-11""\0"
52 "\011" "TIS-620" "\0"
53 "\012" "JIS-0201" "\0"
54 "\377";
55
56 /* separate identifiers for sbcs/dbcs/etc map type */
57 #define UCS2_8BIT 000
58 #define UCS3_8BIT 001
59 #define EUC 002
60 #define EUC_TW 003
61 #define SHIFT_JIS 004
62 #define BIG5 005
63 #define GBK 006
64
65 /* FIXME: these are not implemented yet
66 // EUC: A1-FE A1-FE
67 // GBK: 81-FE 40-7E,80-FE
68 // Big5: A1-FE 40-7E,A1-FE
69 */
70
71 static const unsigned short maplen[] = {
72 [UCS2_8BIT] = 4+ 2* 128,
73 [UCS3_8BIT] = 4+ 3* 128,
74 [EUC] = 4+ 2* 94*94,
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,
79 };
80
81 static int find_charmap(const char *name)
82 {
83 int i;
84 for (i = 0; i < (sizeof(charmaps) / sizeof(charmaps[0])); i++)
85 if (!strcasecmp(charmaps[i].name, name))
86 return i;
87 return -1;
88 }
89
90 static int find_charset(const char *name)
91 {
92 const unsigned char *s;
93 for (s=charsets; *s<0xff && strcasecmp(s+1, name); s+=strlen(s)+1);
94 return *s;
95 }
96
97 iconv_t iconv_open(const char *to, const char *from)
98 {
99 unsigned f, t;
100 int m;
101
102 if ((t = find_charset(to)) >= 8)
103 return -1;
104
105 if ((f = find_charset(from)) < 255)
106 return 0 | (t<<1) | (f<<4);
107
108 if ((m = find_charmap(from)) > -1)
109 return 1 | (t<<1) | (m<<4);
110
111 return -1;
112 }
113
114 int iconv_close(iconv_t cd)
115 {
116 return 0;
117 }
118
119 static inline wchar_t get_16(const unsigned char *s, int endian)
120 {
121 endian &= 1;
122 return s[endian]<<8 | s[endian^1];
123 }
124
125 static inline void put_16(unsigned char *s, wchar_t c, int endian)
126 {
127 endian &= 1;
128 s[endian] = c>>8;
129 s[endian^1] = c;
130 }
131
132 size_t iconv(iconv_t cd, char **in, size_t *inb, char **out, size_t *outb)
133 {
134 size_t x=0;
135 unsigned char to = (cd>>1)&7;
136 unsigned char from = 255;
137 const unsigned char *map = 0;
138 mbstate_t st = {0};
139 char tmp[MB_LEN_MAX];
140 wchar_t c, d;
141 size_t k, l;
142 int err;
143
144 if (!in || !*in || !*inb) return 0;
145
146 if (cd & 1)
147 map = charmaps[cd>>4].map;
148 else
149 from = cd>>4;
150
151 for (; *inb; *in+=l, *inb-=l) {
152 c = *(unsigned char *)*in;
153 l = 1;
154 if (from >= UTF_8 && c < 0x80) goto charok;
155 switch (from) {
156 case WCHAR_T:
157 l = sizeof(wchar_t);
158 if (*inb < l) goto starved;
159 c = *(wchar_t *)*in;
160 break;
161 case UTF_8:
162 l = mbrtowc(&c, *in, *inb, &st);
163 if (!l) l++;
164 else if (l == (size_t)-1) goto ilseq;
165 else if (l == (size_t)-2) goto starved;
166 break;
167 case US_ASCII:
168 goto ilseq;
169 case LATIN_9:
170 if ((unsigned)c - 0xa4 <= 0xbe - 0xa4) {
171 static const unsigned char map[] = {
172 0, 0x60, 0, 0x61, 0, 0, 0, 0, 0, 0, 0,
173 0, 0, 0, 0, 0x7d, 0, 0, 0, 0x7e, 0, 0, 0,
174 0x52, 0x53, 0x78
175 };
176 if (c == 0xa4) c = 0x20ac;
177 else if (map[c-0xa5]) c = 0x100 | map[c-0xa5];
178 }
179 case LATIN_1:
180 goto charok;
181 case TIS_620:
182 if (c >= 0xa1) c += 0x0e01-0xa1;
183 goto charok;
184 case JIS_0201:
185 if (c >= 0xa1) {
186 if (c <= 0xdf) c += 0xff61-0xa1;
187 else goto ilseq;
188 }
189 goto charok;
190 case UTF_16BE:
191 case UTF_16LE:
192 l = 2;
193 if (*inb < 2) goto starved;
194 c = get_16(*in, from);
195 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
196 if ((unsigned)(c-0xd800) < 0x400) {
197 l = 4;
198 if (*inb < 4) goto starved;
199 d = get_16(*in + 2, from);
200 if ((unsigned)(c-0xdc00) >= 0x400) goto ilseq;
201 c = ((c-0xd800)<<10) | (d-0xdc00);
202 }
203 break;
204 case UTF_32BE:
205 case UTF_32LE:
206 l = 4;
207 if (*inb < 4) goto starved;
208 // FIXME
209 // c = get_32(*in, from);
210 break;
211 default:
212 /* only support ascii supersets */
213 if (c < 0x80) break;
214 switch (map[0]) {
215 case UCS2_8BIT:
216 c -= 0x80;
217 break;
218 case EUC:
219 if ((unsigned)c - 0xa1 >= 94) goto ilseq;
220 if ((unsigned)in[0][1] - 0xa1 >= 94) goto ilseq;
221 c = (c-0xa1)*94 + (in[0][1]-0xa1);
222 l = 2;
223 break;
224 case SHIFT_JIS:
225 if ((unsigned)c - 0xa1 <= 0xdf-0xa1) {
226 c += 0xff61-0xa1;
227 goto charok;
228 }
229 // FIXME...
230 l = 2;
231 break;
232 default:
233 goto badf;
234 }
235 c = get_16(map + 4 + 2*c, 0);
236 if (c == 0xffff) goto ilseq;
237 goto charok;
238 }
239
240 if ((unsigned)c - 0xd800 < 0x800 || (unsigned)c >= 0x110000)
241 goto ilseq;
242 charok:
243 switch (to) {
244 case WCHAR_T:
245 if (*outb < sizeof(wchar_t)) goto toobig;
246 *(wchar_t *)*out = c;
247 *out += sizeof(wchar_t);
248 *outb -= sizeof(wchar_t);
249 break;
250 case UTF_8:
251 if (*outb < 4) {
252 k = wctomb(tmp, c);
253 if (*outb < k) goto toobig;
254 memcpy(*out, tmp, k);
255 } else k = wctomb(*out, c);
256 *out += k;
257 *outb -= k;
258 break;
259 case US_ASCII:
260 if (c > 0x7f) c = 0xfffd;
261 /* fall thru and count replacement in latin1 case */
262 case LATIN_1:
263 if (!*outb) goto toobig;
264 if (c < 0x100) **out = c;
265 else x++, **out = '*'; //FIXME: translit?
266 ++*out;
267 --*outb;
268 break;
269 case UTF_16BE:
270 case UTF_16LE:
271 if (c < 0x10000) {
272 if (*outb < 2) goto toobig;
273 put_16(*out, c, to);
274 *out += 2;
275 *outb -= 2;
276 break;
277 }
278 if (*outb < 4) goto toobig;
279 put_16(*out, (c>>10)|0xd800, to);
280 put_16(*out + 2, (c&0x3ff)|0xdc00, to);
281 *out += 4;
282 *outb -= 4;
283 break;
284 default:
285 goto badf;
286 }
287 }
288 return x;
289 ilseq:
290 err = EILSEQ;
291 x = -1;
292 goto end;
293 badf:
294 err = EBADF;
295 x = -1;
296 goto end;
297 toobig:
298 err = E2BIG;
299 goto end;
300 starved:
301 err = EINVAL;
302 end:
303 errno = err;
304 return x;
305 }