fix iconv dest utf-16: unavailable chars must be replaced; EILSEQ is wrong
[musl] / src / locale / iconv.c
index a7d4fd9..78c215e 100644 (file)
@@ -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;
        }
@@ -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;