static int XConvertEucKrToUtf8(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 (c1 >= 0xa1 && c1 < 0xff) { unsigned char b[2]; b[0] = c - 0x80; b[1] = c1 - 0x80; if (ksc5601_mbtowc(NULL, &ucs, b, 2) < 1) { ucs = '?'; } } else { ucs = '?'; } i += 2; } else { ucs = '?'; i++; } l += XConvertUcsToUtf8(ucs, buffer_return + l); } free(buf); return l; }
int iso2022_kr_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n) { state_t state = conv->istate; SPLIT_STATE; int count = 0; unsigned char c; for (;;) { c = *s; if (c == ESC) { if (n < count+4) goto none; if (s[1] == '$') { if (s[2] == ')') { if (s[3] == 'C') { state2 = STATE2_DESIGNATED_KSC5601; s += 4; count += 4; if (n < count+1) goto none; continue; } } } return RET_ILSEQ; } if (c == SO) { if (state2 != STATE2_DESIGNATED_KSC5601) return RET_ILSEQ; state1 = STATE_TWOBYTE; s++; count++; if (n < count+1) goto none; continue; } if (c == SI) { state1 = STATE_ASCII; s++; count++; if (n < count+1) goto none; continue; } break; } switch (state1) { case STATE_ASCII: if (c < 0x80) { int ret = ascii_mbtowc(conv,pwc,s,1); if (ret == RET_ILSEQ) return RET_ILSEQ; if (ret != 1) return SIG_ABRT; #if 0 /* Accept ISO-2022-KR according to CJK.INF. */ if (*pwc == 0x000a || *pwc == 0x000d) state2 = STATE2_NONE; #endif COMBINE_STATE; conv->istate = state; return count+1; } else return RET_ILSEQ; case STATE_TWOBYTE: if (n < count+2) goto none; if (state2 != STATE2_DESIGNATED_KSC5601) return SIG_ABRT; if (s[0] < 0x80 && s[1] < 0x80) { int ret = ksc5601_mbtowc(conv,pwc,s,2); if (ret == RET_ILSEQ) return RET_ILSEQ; if (ret != 2) return SIG_ABRT; COMBINE_STATE; conv->istate = state; return count+2; } else return RET_ILSEQ; default: return SIG_ABRT; } none: COMBINE_STATE; conv->istate = state; return RET_TOOFEW(count); }