--- np2/codecnv/utf8ucs2.c 2005/02/12 12:13:56 1.1 +++ np2/codecnv/utf8ucs2.c 2005/04/01 15:35:48 1.2 @@ -5,53 +5,71 @@ UINT codecnv_utf8toucs2(UINT16 *dst, UINT dcnt, const char *src, UINT scnt) { UINT orgdcnt; + BOOL stringmode; UINT c; - (void)scnt; if (src == NULL) { return(0); } - - orgdcnt = dcnt; if (dcnt == 0) { dst = NULL; + dcnt = (UINT)-1; + } + orgdcnt = dcnt; + stringmode = (((SINT)scnt) < 0); + if (stringmode) { + dcnt--; } - dcnt--; - while(dcnt) { - if (src[0] == '\0') { + while(scnt > 0) { + if ((src[0] == '\0') && (stringmode)) { break; } else if (!(src[0] & 0x80)) { c = src[0]; src += 1; + scnt -= 1; } else if ((src[0] & 0xe0) == 0xc0) { - if ((src[1] & 0xc0) != 0x80) { + if ((scnt < 2) || + ((src[1] & 0xc0) != 0x80)) { break; } c = ((src[0] & 0x1f) << 6) + (src[1] & 0x3f); src += 2; + scnt -= 2; } else if ((src[0] & 0xf0) == 0xe0) { - if (((src[1] & 0xc0) != 0x80) || + if ((scnt < 3) || + ((src[1] & 0xc0) != 0x80) || ((src[2] & 0xc0) != 0x80)) { break; } c = ((src[0] & 0x0f) << 12) + ((src[1] & 0x3f) << 6) + (src[2] & 0x3f); src += 3; + scnt -= 3; } else { break; } + if (dcnt == 0) { + break; + } dcnt--; if (dst) { dst[0] = (UINT16)c; dst += 1; } } - if (dst) { - dst[0] = '\0'; + if (dst != NULL) { + if (stringmode) { + *dst = '\0'; + } +#if 1 // 一応互換の為に NULLつける + else if (dcnt) { + *dst = '\0'; + } +#endif } return((UINT)(orgdcnt - dcnt)); }