/* * The one text conversion fmptools asks iconv for: MacRoman ("MACINTOSH", * the text of .fp3 and .fp5 files) to UTF-8. Emscripten's musl iconv has no * MacRoman table, so iconv_open would fail and those files would not open; * these three functions take the place of libc's. The table is glibc's * MACINTOSH table (0xDB is the euro sign, 0xF0 the Apple logo in the private * use area), so the text comes out as the fmptools command-line tools give it * on Linux. Like iconv, a conversion stops when the output is full, never in * the middle of a character. */ #include #include #include #include #include static const uint16_t high[128] = { 0x00c4, 0x00c5, 0x00c7, 0x00c9, 0x00d1, 0x00d6, 0x00dc, 0x00e1, 0x00e0, 0x00e2, 0x00e4, 0x00e3, 0x00e5, 0x00e7, 0x00e9, 0x00e8, 0x00ea, 0x00eb, 0x00ed, 0x00ec, 0x00ee, 0x00ef, 0x00f1, 0x00f3, 0x00f2, 0x00f4, 0x00f6, 0x00f5, 0x00fa, 0x00f9, 0x00fb, 0x00fc, 0x2020, 0x00b0, 0x00a2, 0x00a3, 0x00a7, 0x2022, 0x00b6, 0x00df, 0x00ae, 0x00a9, 0x2122, 0x00b4, 0x00a8, 0x2260, 0x00c6, 0x00d8, 0x221e, 0x00b1, 0x2264, 0x2265, 0x00a5, 0x00b5, 0x2202, 0x2211, 0x220f, 0x03c0, 0x222b, 0x00aa, 0x00ba, 0x03a9, 0x00e6, 0x00f8, 0x00bf, 0x00a1, 0x00ac, 0x221a, 0x0192, 0x2248, 0x0394, 0x00ab, 0x00bb, 0x2026, 0x00a0, 0x00c0, 0x00c3, 0x00d5, 0x0152, 0x0153, 0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x00f7, 0x25ca, 0x00ff, 0x0178, 0x2044, 0x20ac, 0x2039, 0x203a, 0xfb01, 0xfb02, 0x2021, 0x00b7, 0x201a, 0x201e, 0x2030, 0x00c2, 0x00ca, 0x00c1, 0x00cb, 0x00c8, 0x00cd, 0x00ce, 0x00cf, 0x00cc, 0x00d3, 0x00d4, 0xe01e, 0x00d2, 0x00da, 0x00db, 0x00d9, 0x0131, 0x02c6, 0x02dc, 0x00af, 0x02d8, 0x02d9, 0x02da, 0x00b8, 0x02dd, 0x02db, 0x02c7, }; static int macroman_to_utf8; iconv_t iconv_open(const char *to, const char *from) { if (to && from && strcasecmp(to, "UTF-8") == 0 && (strcasecmp(from, "MACINTOSH") == 0 || strcasecmp(from, "MACROMAN") == 0)) return (iconv_t)¯oman_to_utf8; errno = EINVAL; return (iconv_t)-1; } size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inleft, char **restrict out, size_t *restrict outleft) { if (cd != (iconv_t)¯oman_to_utf8) { errno = EBADF; return (size_t)-1; } if (!in || !*in) return 0; while (*inleft) { unsigned char c = (unsigned char)**in; uint16_t u = c < 0x80 ? c : high[c - 0x80]; char buf[3]; size_t n; if (u < 0x80) { buf[0] = (char)u; n = 1; } else if (u < 0x800) { buf[0] = (char)(0xC0 | (u >> 6)); buf[1] = (char)(0x80 | (u & 0x3F)); n = 2; } else { buf[0] = (char)(0xE0 | (u >> 12)); buf[1] = (char)(0x80 | ((u >> 6) & 0x3F)); buf[2] = (char)(0x80 | (u & 0x3F)); n = 3; } if (n > *outleft) { errno = E2BIG; return (size_t)-1; } memcpy(*out, buf, n); *out += n; *outleft -= n; (*in)++; (*inleft)--; } return 0; } int iconv_close(iconv_t cd) { return 0; }