Exemplo n.º 1
0
size_t
strlen_sjis(const char *s)
{
    int i = 0, j = 0;
    while (s[i]) {
	if (is_sjis_lead_byte(s[i])) i++; /* skip */
	j++;
	i++;
    }
    return j;
}
Exemplo n.º 2
0
void
MultiByteAccumulate(BYTE ch, LPWSTR wstr, int * count)
{
    static char mbstr[4] = "";
    static int mbwait = 0;
    static int mbcount = 0;

    *count = 0;

    /* try to re-sync on control characters */
    /* works for utf8 and sjis */
    if (ch < 32) {
	mbwait = mbcount = 0;
	mbstr[0] = NUL;
    }

    if (encoding == S_ENC_UTF8) { /* combine UTF8 byte sequences */
	if (mbwait == 0) {
	    /* first byte */
	    mbcount = 0;
	    mbstr[mbcount] = ch;
	    if ((ch & 0xE0) == 0xC0) {
		// expect one more byte
		mbwait = 1;
	    } else if ((ch & 0xF0) == 0xE0) {
		// expect two more bytes
		mbwait = 2;
	    } else if ((ch & 0xF8) == 0xF0) {
		// expect three more bytes
		mbwait = 3;
	    }
	} else {
	    /* subsequent byte */
	    /*assert((ch & 0xC0) == 0x80);*/
	    if ((ch & 0xC0) == 0x80) {
		mbcount++;
		mbwait--;
	    } else {
		/* invalid sequence */
		mbcount = 0;
		mbwait = 0;
	    }
	    mbstr[mbcount] = ch;
	}
	if (mbwait == 0) {
	    *count = MultiByteToWideChar(CP_UTF8, 0, mbstr, mbcount + 1, wstr, 2);
	}
    } else if (encoding == S_ENC_SJIS) { /* combine S-JIS sequences */
	if (mbwait == 0) {
	    /* first or single byte */
	    mbcount = 0;
	    mbstr[mbcount] = ch;
	    if (is_sjis_lead_byte(ch)) {
		/* first byte */
		mbwait = 1;
	    }
	} else {
	    if ((ch >= 0x40) && (ch <= 0xfc)) {
		/* valid */
		mbcount++;
	    } else {
		/* invalid */
		mbcount = 0;
	    }
	    mbwait = 0; /* max. double byte sequences */
	    mbstr[mbcount] = ch;
	}
	if (mbwait == 0) {
	    *count = MultiByteToWideChar(932, 0, mbstr, mbcount + 1, wstr, 2);
	}
    } else {
	mbcount = 0;
	mbwait = 0;
	mbstr[0] = (char) ch;
	*count = MultiByteToWideChar(WinGetCodepage(encoding), 0, mbstr, mbcount + 1, wstr, 2);
    }
}