1#include <iconv.h>
2#include <errno.h>
3#include <wchar.h>
4#include <string.h>
5#include <stdlib.h>
6#include <limits.h>
7#include <stdint.h>
8#include "locale_impl.h"
9
10#define UTF_32BE    0300
11#define UTF_16LE    0301
12#define UTF_16BE    0302
13#define UTF_32LE    0303
14#define UCS2BE      0304
15#define UCS2LE      0305
16#define WCHAR_T     0306
17#define US_ASCII    0307
18#define UTF_8       0310
19#define EUC_JP      0320
20#define SHIFT_JIS   0321
21#define GB18030     0330
22#define GBK         0331
23#define GB2312      0332
24#define BIG5        0340
25#define EUC_KR      0350
26
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). */
31
32static const unsigned char charmaps[] =
33"utf8\0char\0\0\310"
34"wchart\0\0\306"
35"ucs2\0ucs2be\0\0\304"
36"ucs2le\0\0\305"
37"utf16\0utf16be\0\0\302"
38"utf16le\0\0\301"
39"ucs4\0ucs4be\0utf32\0utf32be\0\0\300"
40"ucs4le\0utf32le\0\0\303"
41"ascii\0usascii\0iso646\0iso646us\0\0\307"
42"eucjp\0\0\320"
43"shiftjis\0sjis\0\0\321"
44"gb18030\0\0\330"
45"gbk\0\0\331"
46"gb2312\0\0\332"
47"big5\0bigfive\0cp950\0big5hkscs\0\0\340"
48"euckr\0ksc5601\0ksx1001\0cp949\0\0\350"
49#include "codepages.h"
50;
51
52static const unsigned short legacy_chars[] = {
53#include "legacychars.h"
54};
55
56static const unsigned short jis0208[84][94] = {
57#include "jis0208.h"
58};
59
60static const unsigned short gb18030[126][190] = {
61#include "gb18030.h"
62};
63
64static const unsigned short big5[89][157] = {
65#include "big5.h"
66};
67
68static const unsigned short hkscs[] = {
69#include "hkscs.h"
70};
71
72static const unsigned short ksc[93][94] = {
73#include "ksc.h"
74};
75
76static int fuzzycmp(const unsigned char *a, const unsigned char *b)
77{
78	for (; *a && *b; a++, b++) {
79		while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++;
80		if ((*a|32U) != *b) return 1;
81	}
82	return *a != *b;
83}
84
85static size_t find_charmap(const void *name)
86{
87	const unsigned char *s;
88	if (!*(char *)name) name=charmaps; /* "utf8" */
89	for (s=charmaps; *s; ) {
90		if (!fuzzycmp(name, s)) {
91			for (; *s; s+=strlen((void *)s)+1);
92			return s+1-charmaps;
93		}
94		s += strlen((void *)s)+1;
95		if (!*s) {
96			if (s[1] > 0200) s+=2;
97			else s+=2+(128U-s[1])/4*5;
98		}
99	}
100	return -1;
101}
102
103iconv_t iconv_open(const char *to, const char *from)
104{
105	size_t f, t;
106
107	if ((t = find_charmap(to))==-1
108	 || (f = find_charmap(from))==-1
109	 || (charmaps[t] >= 0320)) {
110		errno = EINVAL;
111		return (iconv_t)-1;
112	}
113
114	return (void *)(f<<16 | t);
115}
116
117int iconv_close(iconv_t cd)
118{
119	return 0;
120}
121
122static unsigned get_16(const unsigned char *s, int e)
123{
124	e &= 1;
125	return s[e]<<8 | s[1-e];
126}
127
128static void put_16(unsigned char *s, unsigned c, int e)
129{
130	e &= 1;
131	s[e] = c>>8;
132	s[1-e] = c;
133}
134
135static unsigned get_32(const unsigned char *s, int e)
136{
137	e &= 3;
138	return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
139}
140
141static void put_32(unsigned char *s, unsigned c, int e)
142{
143	e &= 3;
144	s[e^0] = c>>24;
145	s[e^1] = c>>16;
146	s[e^2] = c>>8;
147	s[e^3] = c;
148}
149
150/* Adapt as needed */
151#define mbrtowc_utf8 mbrtowc
152#define wctomb_utf8 wctomb
153
154size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb)
155{
156	size_t x=0;
157	unsigned long cd = (unsigned long)cd0;
158	unsigned to = cd & 0xffff;
159	unsigned from = cd >> 16;
160	const unsigned char *map = charmaps+from+1;
161	const unsigned char *tomap = charmaps+to+1;
162	mbstate_t st = {0};
163	wchar_t wc;
164	unsigned c, d;
165	size_t k, l;
166	int err;
167	unsigned char type = map[-1];
168	unsigned char totype = tomap[-1];
169	locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
170
171	if (!in || !*in || !*inb) return 0;
172
173	*ploc = UTF8_LOCALE;
174
175	for (; *inb; *in+=l, *inb-=l) {
176		c = *(unsigned char *)*in;
177		l = 1;
178
179		if (c >= 128 || type-UTF_32BE < 7U) switch (type) {
180		case UTF_8:
181			l = mbrtowc_utf8(&wc, *in, *inb, &st);
182			if (!l) l++;
183			else if (l == (size_t)-1) goto ilseq;
184			else if (l == (size_t)-2) goto starved;
185			c = wc;
186			break;
187		case US_ASCII:
188			goto ilseq;
189		case WCHAR_T:
190			l = sizeof(wchar_t);
191			if (*inb < l) goto starved;
192			c = *(wchar_t *)*in;
193			if (0) {
194		case UTF_32BE:
195		case UTF_32LE:
196			l = 4;
197			if (*inb < 4) goto starved;
198			c = get_32((void *)*in, type);
199			}
200			if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq;
201			break;
202		case UCS2BE:
203		case UCS2LE:
204		case UTF_16BE:
205		case UTF_16LE:
206			l = 2;
207			if (*inb < 2) goto starved;
208			c = get_16((void *)*in, type);
209			if ((unsigned)(c-0xdc00) < 0x400) goto ilseq;
210			if ((unsigned)(c-0xd800) < 0x400) {
211				if (type-UCS2BE < 2U) goto ilseq;
212				l = 4;
213				if (*inb < 4) goto starved;
214				d = get_16((void *)(*in + 2), type);
215				if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq;
216				c = ((c-0xd7c0)<<10) + (d-0xdc00);
217			}
218			break;
219		case SHIFT_JIS:
220			if (c-0xa1 <= 0xdf-0xa1) {
221				c += 0xff61-0xa1;
222				break;
223			}
224			l = 2;
225			if (*inb < 2) goto starved;
226			d = *((unsigned char *)*in + 1);
227			if (c-129 <= 159-129) c -= 129;
228			else if (c-224 <= 239-224) c -= 193;
229			else goto ilseq;
230			c *= 2;
231			if (d-64 <= 158-64) {
232				if (d==127) goto ilseq;
233				if (d>127) d--;
234				d -= 64;
235			} else if (d-159 <= 252-159) {
236				c++;
237				d -= 159;
238			}
239			c = jis0208[c][d];
240			if (!c) goto ilseq;
241			break;
242		case EUC_JP:
243			l = 2;
244			if (*inb < 2) goto starved;
245			d = *((unsigned char *)*in + 1);
246			if (c==0x8e) {
247				c = d;
248				if (c-0xa1 > 0xdf-0xa1) goto ilseq;
249				c += 0xff61 - 0xa1;
250				break;
251			}
252			c -= 0xa1;
253			d -= 0xa1;
254			if (c >= 84 || d >= 94) goto ilseq;
255			c = jis0208[c][d];
256			if (!c) goto ilseq;
257			break;
258		case GB2312:
259			if (c < 0xa1) goto ilseq;
260		case GBK:
261		case GB18030:
262			c -= 0x81;
263			if (c >= 126) goto ilseq;
264			l = 2;
265			if (*inb < 2) goto starved;
266			d = *((unsigned char *)*in + 1);
267			if (d < 0xa1 && type == GB2312) goto ilseq;
268			if (d-0x40>=191 || d==127) {
269				if (d-'0'>9 || type != GB18030)
270					goto ilseq;
271				l = 4;
272				if (*inb < 4) goto starved;
273				c = (10*c + d-'0') * 1260;
274				d = *((unsigned char *)*in + 2);
275				if (d-0x81>126) goto ilseq;
276				c += 10*(d-0x81);
277				d = *((unsigned char *)*in + 3);
278				if (d-'0'>9) goto ilseq;
279				c += d-'0';
280				c += 128;
281				for (d=0; d<=c; ) {
282					k = 0;
283					for (int i=0; i<126; i++)
284						for (int j=0; j<190; j++)
285							if (gb18030[i][j]-d <= c-d)
286								k++;
287					d = c+1;
288					c += k;
289				}
290				break;
291			}
292			d -= 0x40;
293			if (d>63) d--;
294			c = gb18030[c][d];
295			break;
296		case BIG5:
297			l = 2;
298			if (*inb < 2) goto starved;
299			d = *((unsigned char *)*in + 1);
300			if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq;
301			d -= 0x40;
302			if (d > 0x3e) d -= 0x22;
303			if (c-0xa1>=0xfa-0xa1) {
304				if (c-0x87>=0xff-0x87) goto ilseq;
305				if (c < 0xa1) c -= 0x87;
306				else c -= 0x87 + (0xfa-0xa1);
307				c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17
308					| hkscs[c*157+d];
309				/* A few HKSCS characters map to pairs of UCS
310				 * characters. These are mapped to surrogate
311				 * range in the hkscs table then hard-coded
312				 * here. Ugly, yes. */
313				if (c/256 == 0xdc) {
314					if (totype-0300U > 8) k = 2;
315					else k = "\10\4\4\10\4\4\10\2\4"[totype-0300];
316					if (k > *outb) goto toobig;
317					x += iconv((iconv_t)(uintptr_t)to,
318						&(char *){"\303\212\314\204"
319						"\303\212\314\214"
320						"\303\252\314\204"
321						"\303\252\314\214"
322						+c%256}, &(size_t){4},
323						out, outb);
324					continue;
325				}
326				if (!c) goto ilseq;
327				break;
328			}
329			c -= 0xa1;
330			c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17;
331			if (!c) goto ilseq;
332			break;
333		case EUC_KR:
334			l = 2;
335			if (*inb < 2) goto starved;
336			d = *((unsigned char *)*in + 1);
337			c -= 0xa1;
338			d -= 0xa1;
339			if (c >= 93 || d >= 94) {
340				c += (0xa1-0x81);
341				d += 0xa1;
342				if (c >= 93 || c>=0xc6-0x81 && d>0x52)
343					goto ilseq;
344				if (d-'A'<26) d = d-'A';
345				else if (d-'a'<26) d = d-'a'+26;
346				else if (d-0x81<0xff-0x81) d = d-0x81+52;
347				else goto ilseq;
348				if (c < 0x20) c = 178*c + d;
349				else c = 178*0x20 + 84*(c-0x20) + d;
350				c += 0xac00;
351				for (d=0xac00; d<=c; ) {
352					k = 0;
353					for (int i=0; i<93; i++)
354						for (int j=0; j<94; j++)
355							if (ksc[i][j]-d <= c-d)
356								k++;
357					d = c+1;
358					c += k;
359				}
360				break;
361			}
362			c = ksc[c][d];
363			if (!c) goto ilseq;
364			break;
365		default:
366			if (c < 128+type) break;
367			c -= 128+type;
368			c = legacy_chars[ map[c*5/4]>>2*c%8 |
369				map[c*5/4+1]<<8-2*c%8 & 1023 ];
370			if (!c) c = *(unsigned char *)*in;
371			if (c==1) goto ilseq;
372		}
373
374		switch (totype) {
375		case WCHAR_T:
376			if (*outb < sizeof(wchar_t)) goto toobig;
377			*(wchar_t *)*out = c;
378			*out += sizeof(wchar_t);
379			*outb -= sizeof(wchar_t);
380			break;
381		case UTF_8:
382			if (*outb < 4) {
383				char tmp[4];
384				k = wctomb_utf8(tmp, c);
385				if (*outb < k) goto toobig;
386				memcpy(*out, tmp, k);
387			} else k = wctomb_utf8(*out, c);
388			*out += k;
389			*outb -= k;
390			break;
391		case US_ASCII:
392			if (c > 0x7f) subst: x++, c='*';
393		default:
394			if (*outb < 1) goto toobig;
395			if (c < 128+totype) {
396			revout:
397				*(*out)++ = c;
398				*outb -= 1;
399				break;
400			}
401			d = c;
402			for (c=0; c<128-totype; c++) {
403				if (d == legacy_chars[ tomap[c*5/4]>>2*c%8 |
404					tomap[c*5/4+1]<<8-2*c%8 & 1023 ]) {
405					c += 128;
406					goto revout;
407				}
408			}
409			goto subst;
410		case UCS2BE:
411		case UCS2LE:
412		case UTF_16BE:
413		case UTF_16LE:
414			if (c < 0x10000 || type-UCS2BE < 2U) {
415				if (c >= 0x10000) c = 0xFFFD;
416				if (*outb < 2) goto toobig;
417				put_16((void *)*out, c, totype);
418				*out += 2;
419				*outb -= 2;
420				break;
421			}
422			if (*outb < 4) goto toobig;
423			c -= 0x10000;
424			put_16((void *)*out, (c>>10)|0xd800, totype);
425			put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype);
426			*out += 4;
427			*outb -= 4;
428			break;
429		case UTF_32BE:
430		case UTF_32LE:
431			if (*outb < 4) goto toobig;
432			put_32((void *)*out, c, totype);
433			*out += 4;
434			*outb -= 4;
435			break;
436		}
437	}
438	*ploc = loc;
439	return x;
440ilseq:
441	err = EILSEQ;
442	x = -1;
443	goto end;
444toobig:
445	err = E2BIG;
446	x = -1;
447	goto end;
448starved:
449	err = EINVAL;
450	x = -1;
451end:
452	errno = err;
453	*ploc = loc;
454	return x;
455}
456