From 966bf6d2f2fbddb7eb70ac0fd18e0ba8157dd1cb Mon Sep 17 00:00:00 2001 From: waker Date: Mon, 24 Jan 2011 19:44:45 +0100 Subject: adding shift-jis->utf8 converter WIP --- junklib.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'junklib.c') diff --git a/junklib.c b/junklib.c index 799bb11a..479fd9ac 100644 --- a/junklib.c +++ b/junklib.c @@ -32,6 +32,9 @@ #else #define DDB_RECODE #include "ConvertUTF/ConvertUTF.h" +uint16_t sj_to_unicode[] = { + #include "sj_to_unicode.h" +}; #endif #include #include @@ -354,6 +357,54 @@ int ddb_iconv (const char *cs_out, const char *cs_in, char *out, int outlen, con len = target - out; } } + else if (!strcasecmp (cs_in, "SHIFT-JIS")) { + int len = 0; + while (inlen > 0 && len < outlen) { + if (*in > 0) { + *out++ = *in++; + inlen--; + len++; + } + else if (inlen < 2) { + return -1; + } + else { + // find character in table + uint16_t c = (((uint8_t*)in)[0] << 8) | ((uint8_t*)in)[1]; + for (int i = 0; sj_to_unicode[i]; i += 2) { + if (c == sj_to_unicode[i]) { + break; + } + } + if (sj_to_unicode[i]) { + // slow conversion! + char unicode_val[2] = { (sj_to_unicode[i+1] & 0xff00) >> 8, sj_to_unicode[i+1] & 0xff }; + char utf8_val[5]; + char *src = unicode_val, *dst = utf8_val; + + ConversionResult res = ConvertUTF16toUTF32 ((UTF16**)&src, src+2, (UTF8**)&dst, dst+5, strictConversion); + if (res == conversionOK) { + if (src - utf8_val < outlen-len) { + memcpy (out, utf8_val, src - utf8_val); + out += src - utf8_val; + len += src - utf8_val; + inlen -= 2; + in += 2; + } + else { + return -1; + } + } + else { + return -1; + } + } + else { + return -1; // error + } + } + } + } else { fprintf (stderr, "invalid conversion request: %s -> %s\n", cs_in, cs_out); } -- cgit v1.2.3