X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Flocale%2Ficonv.c;h=a0b0232015a2be6d4a3f42ea081b1eae1d055c95;hb=6fbdeff0e51f6afc38fbb1476a4db81322779da4;hp=fb982921ecd16e0fd89f9a603ce48c1e9a1c1e68;hpb=95a85e047e2353959735d93de91df717a4b90900;p=musl diff --git a/src/locale/iconv.c b/src/locale/iconv.c index fb982921..a0b02320 100644 --- a/src/locale/iconv.c +++ b/src/locale/iconv.c @@ -12,11 +12,16 @@ #define UTF_32LE 0303 #define UCS2BE 0304 #define UCS2LE 0305 -#define US_ASCII 0306 -#define WCHAR_T 0307 +#define WCHAR_T 0306 +#define US_ASCII 0307 #define UTF_8 0310 #define EUC_JP 0320 #define SHIFT_JIS 0321 +#define GB18030 0330 +#define GBK 0331 +#define GB2312 0332 +#define BIG5 0340 +#define EUC_KR 0350 /* FIXME: these are not implemented yet * EUC: A1-FE A1-FE @@ -31,16 +36,21 @@ static const unsigned char charmaps[] = "utf8\0\0\310" -"wchart\0\0\307" +"wchart\0\0\306" "ucs2\0ucs2be\0\0\304" "ucs2le\0\0\305" "utf16\0utf16be\0\0\302" "utf16le\0\0\301" "ucs4\0ucs4be\0utf32\0utf32be\0\0\300" "ucs4le\0utf32le\0\0\303" -"ascii\0usascii\0iso646\0iso646us\0\0\306" +"ascii\0usascii\0iso646\0iso646us\0\0\307" "eucjp\0\0\320" "shiftjis\0sjis\0\0\321" +"gb18030\0\0\330" +"gbk\0\0\331" +"gb2312\0\0\332" +"big5\0bigfive\0cp950\0big5hkscs\0\0\340" +"euckr\0ksc5601\0ksx1001\0cp949\0\0\350" #include "codepages.h" ; @@ -52,6 +62,22 @@ static const unsigned short jis0208[84][94] = { #include "jis0208.h" }; +static const unsigned short gb18030[126][190] = { +#include "gb18030.h" +}; + +static const unsigned short big5[89][157] = { +#include "big5.h" +}; + +static const unsigned short hkscs[] = { +#include "hkscs.h" +}; + +static const unsigned short ksc[93][94] = { +#include "ksc.h" +}; + static int fuzzycmp(const unsigned char *a, const unsigned char *b) { for (; *a && *b; a++, b++) { @@ -82,7 +108,9 @@ iconv_t iconv_open(const char *to, const char *from) { size_t f, t; - if ((t = find_charmap(to))==-1 || (f = find_charmap(from))==-1) { + if ((t = find_charmap(to))==-1 + || (f = find_charmap(from))==-1 + || (charmaps[t] >= 0320)) { errno = EINVAL; return (iconv_t)-1; } @@ -127,8 +155,7 @@ static void put_32(unsigned char *s, unsigned c, int e) #define mbrtowc_utf8 mbrtowc #define wctomb_utf8 wctomb -#include -size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb) +size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb) { size_t x=0; unsigned long cd = (unsigned long)cd0; @@ -150,7 +177,7 @@ size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb) c = *(unsigned char *)*in; l = 1; - if (c >= 128) switch (type) { + if (c >= 128 || type-UTF_32BE < 7U) switch (type) { case UTF_8: l = mbrtowc_utf8(&wc, *in, *inb, &st); if (!l) l++; @@ -185,9 +212,9 @@ size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb) if (type-UCS2BE < 2U) goto ilseq; l = 4; if (*inb < 4) goto starved; - d = get_16((void *)(*in + 2), from); - if ((unsigned)(c-0xdc00) >= 0x400) goto ilseq; - c = ((c-0xd800)<<10) | (d-0xdc00); + d = get_16((void *)(*in + 2), type); + if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq; + c = ((c-0xd7c0)<<10) + (d-0xdc00); } break; case SHIFT_JIS: @@ -229,6 +256,113 @@ size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb) c = jis0208[c][d]; if (!c) goto ilseq; break; + case GB2312: + if (c < 0xa1) goto ilseq; + case GBK: + case GB18030: + c -= 0x81; + if (c >= 126) goto ilseq; + l = 2; + if (*inb < 2) goto starved; + d = *((unsigned char *)*in + 1); + if (d < 0xa1 && type == GB2312) goto ilseq; + if (d-0x40>=191 || d==127) { + if (d-'0'>9 || type != GB18030) + goto ilseq; + l = 4; + if (*inb < 4) goto starved; + c = (10*c + d-'0') * 1260; + d = *((unsigned char *)*in + 2); + if (d-0x81>126) goto ilseq; + c += 10*(d-0x81); + d = *((unsigned char *)*in + 3); + if (d-'0'>9) goto ilseq; + c += d-'0'; + c += 128; + for (d=0; d<=c; ) { + k = 0; + for (int i=0; i<126; i++) + for (int j=0; j<190; j++) + if (gb18030[i][j]-d <= c-d) + k++; + d = c+1; + c += k; + } + break; + } + d -= 0x40; + if (d>63) d--; + c = gb18030[c][d]; + break; + case BIG5: + l = 2; + if (*inb < 2) goto starved; + d = *((unsigned char *)*in + 1); + if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq; + d -= 0x40; + if (d > 0x3e) d -= 0x22; + if (c-0xa1>=0xfa-0xa1) { + if (c-0x87>=0xff-0x87) goto ilseq; + if (c < 0xa1) c -= 0x87; + else c -= 0x87 + (0xfa-0xa1); + c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<17 + | hkscs[c*157+d]; + /* A few HKSCS characters map to pairs of UCS + * characters. These are mapped to surrogate + * range in the hkscs table then hard-coded + * here. Ugly, yes. */ + if (c/256 == 0xdc) { + if (totype-0300U > 8) k = 2; + else k = "\10\4\4\10\4\4\10\2\4"[totype-0300]; + if (k > *outb) goto toobig; + x += iconv((iconv_t)(uintptr_t)to, + &(char *){"\303\212\314\204" + "\303\212\314\214" + "\303\252\314\204" + "\303\252\314\214" + +c%256}, &(size_t){4}, + out, outb); + continue; + } + if (!c) goto ilseq; + break; + } + c -= 0xa1; + c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<17; + if (!c) goto ilseq; + break; + case EUC_KR: + l = 2; + if (*inb < 2) goto starved; + d = *((unsigned char *)*in + 1); + c -= 0xa1; + d -= 0xa1; + if (c >= 93 || d >= 94) { + c += (0xa1-0x81); + d += 0xa1; + if (c >= 93 || c>=0xc6-0x81 && d>0x52) + goto ilseq; + if (d-'A'<26) d = d-'A'; + else if (d-'a'<26) d = d-'a'+26; + else if (d-0x81<0xff-0x81) d = d-0x81+52; + else goto ilseq; + if (c < 0x20) c = 178*c + d; + else c = 178*0x20 + 84*(c-0x20) + d; + c += 0xac00; + for (d=0xac00; d<=c; ) { + k = 0; + for (int i=0; i<93; i++) + for (int j=0; j<94; j++) + if (ksc[i][j]-d <= c-d) + k++; + d = c+1; + c += k; + } + break; + } + c = ksc[c][d]; + if (!c) goto ilseq; + break; default: if (c < 128+type) break; c -= 128+type; @@ -267,8 +401,8 @@ size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb) } d = c; for (c=0; c<128-totype; c++) { - if (d == legacy_chars[ map[c*5/4]>>2*c%8 | - map[c*5/4+1]<<8-2*c%8 & 1023 ]) { + if (d == legacy_chars[ tomap[c*5/4]>>2*c%8 | + tomap[c*5/4+1]<<8-2*c%8 & 1023 ]) { c += 128; goto revout; } @@ -278,15 +412,16 @@ size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb) case UCS2LE: case UTF_16BE: case UTF_16LE: - if (c < 0x10000) { + if (c < 0x10000 || type-UCS2BE < 2U) { + if (c >= 0x10000) c = 0xFFFD; if (*outb < 2) goto toobig; put_16((void *)*out, c, totype); *out += 2; *outb -= 2; break; } - if (type-UCS2BE < 2U) goto ilseq; if (*outb < 4) goto toobig; + c -= 0x10000; put_16((void *)*out, (c>>10)|0xd800, totype); put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype); *out += 4;