int sjis_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n) { unsigned char c = *s; if (c < 0x80 || (c >= 0xa1 && c <= 0xdf)) return jisx0201_mbtowc(conv,pwc,s,n); else { unsigned char s1, s2; s1 = c; if ((s1 >= 0x81 && s1 <= 0x9f) || (s1 >= 0xe0 && s1 <= 0xea)) { if (n < 2) return RET_TOOFEW(0); s2 = s[1]; if ((s2 >= 0x40 && s2 <= 0x7e) || (s2 >= 0x80 && s2 <= 0xfc)) { unsigned char t1 = (s1 < 0xe0 ? s1-0x81 : s1-0xc1); unsigned char t2 = (s2 < 0x80 ? s2-0x40 : s2-0x41); unsigned char buf[2]; buf[0] = 2*t1 + (t2 < 0x5e ? 0 : 1) + 0x21; buf[1] = (t2 < 0x5e ? t2 : t2-0x5e) + 0x21; return jisx0208_mbtowc(conv,pwc,buf,2); } } else if (s1 >= 0xf0 && s1 <= 0xf9) { /* User-defined range. See * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */ if (n < 2) return RET_TOOFEW(0); s2 = s[1]; if ((s2 >= 0x40 && s2 <= 0x7e) || (s2 >= 0x80 && s2 <= 0xfc)) { *pwc = 0xe000 + 188*(s1 - 0xf0) + (s2 < 0x80 ? s2-0x40 : s2-0x41); return 2; } } return RET_ILSEQ; } }
static int XConvertEucJpToUtf8(char* buffer_return, int len) { int i = 0, l = 0; char *buf; if (len < 1) return 0; buf = (char*) malloc((unsigned)len); memcpy(buf, buffer_return, (unsigned)len); while (i < len) { unsigned int ucs; unsigned char c, c1; c = (unsigned char) buf[i]; if (c < 0x80) { ucs = c; i++; } else if (c >= 0xA1 && c < 0xFF && len - i > 1) { c1 = (unsigned char) buf[i + 1]; if (c < 0xF5 && c1 >= 0xa1) { unsigned char b[2]; b[0] = c - 0x80; b[1] = c1 - 0x80; if (jisx0208_mbtowc(NULL, &ucs, b, 2) < 1) { ucs = '?'; } } else if (c1 >= 0xA1 && c1 < 0xFF) { ucs = 0xE000 + 94 * (c - 0xF5) + (c1 - 0xA1); } else { ucs = '?'; } i += 2; } else if (c == 0x8E && len - i > 1) { c1 = (unsigned char) buf[i + 1]; if (c1 >= 0xa1 && c1 <= 0xe0) { if (jisx0201_mbtowc(NULL, &ucs, &c1, 1) != 1) { ucs = '?'; } } else { ucs = '?'; } i += 2; } else if (c == 0x8F && len - i > 2) { c = (unsigned char) buf[i + 1]; c1 = (unsigned char) buf[i + 2]; if (c >= 0xa1 && c < 0xff) { if (c < 0xf5 && c1 >= 0xa1 && c1 < 0xff) { unsigned char b[2]; b[0] = c - 0x80; b[1] = c1 - 0x80; if (jisx0212_mbtowc(NULL, &ucs, b, 2) < 1) { ucs = '?'; } } else { ucs = '?'; } } else { if (c1 >= 0xa1 && c1 < 0xff) { ucs = 0xe3ac + 94 * (c - 0xF5) + (c1 - 0xA1); } else { ucs = '?'; } } i += 3; } else { ucs = '?'; i++; } l += XConvertUcsToUtf8(ucs, buffer_return + l); } free(buf); return l; }