X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Flocale%2Ficonv.c;h=508d322e5e38927da68e31e0ab137f2299415086;hb=b439c051c7eee4eb4b93fc382f993aa6305ce530;hp=a7d4fd9f5d345770cfc934996a046894d4615172;hpb=0e2331c9b6e0c0b4f24019d4062f4c655d28cbaf;p=musl diff --git a/src/locale/iconv.c b/src/locale/iconv.c index a7d4fd9f..508d322e 100644 --- a/src/locale/iconv.c +++ b/src/locale/iconv.c @@ -12,8 +12,8 @@ #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 @@ -34,14 +34,14 @@ 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" @@ -94,7 +94,7 @@ iconv_t iconv_open(const char *to, const char *from) if ((t = find_charmap(to))==-1 || (f = find_charmap(from))==-1 - || (t >= 0320)) { + || (charmaps[t] >= 0320)) { errno = EINVAL; return (iconv_t)-1; } @@ -161,7 +161,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++; @@ -196,9 +196,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: @@ -327,15 +327,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;