
static int utf8_to_unicode(const char *str, uint32_t *chPtr){
	//reads the contents pointed to by str and outputs a unicode codepage into object pointed to by chPtre
	//return is the bytes to offset for the first char of a subsequent read
	uint32_t b0 = *(uint8_t *)str;
	uint32_t b1, b2, b3;

#ifdef JISX0208
        if (0xA1 <= b0 && b0 <= 0xFE) {
                int ku, ten;

                ku = (b0 & 0x7F) - 0x20;
                ten = (str[1] & 0x7F) - 0x20;
                if ((ku < 1 || ku > 92) || (ten < 1 || ten > 94)) {
                        *chPtr = b0;
                        return 1;
                }

                *chPtr = (Tcl_UniChar) UnicodeTbl[ku - 1][ten - 1];
                return 2;
        } else
#endif /* JISX0208 */
	if (b0 < 0xC0) {
		*chPtr = b0;
		return 1;
	} else if (b0 < 0xE0) {
		b1=(uint8_t)str[1];
		if ((b1 & 0xC0) == 0x80) {
			*chPtr = ((b0 & 0x1F) << 6) | (b1 & 0x3F);
			return 2;
		}
		*chPtr = b0;
		return 1;
	} else if (b0 < 0xF0) {
		b1=(uint8_t)str[1];
		b2=(uint8_t)str[2];
		if (((b1 & 0xC0) == 0x80) && ((b2 & 0xC0) == 0x80)) {
			*chPtr = ((b0 & 0x0F) << 12) | ((b1 & 0x3F) << 6) | (b2 & 0x3F);
			return 3;
		}
		*chPtr = b0;
		return 1;
	}else if (b0 < 0xF8) {
		b1=(uint8_t)str[1];
		b2=(uint8_t)str[2];
		b3=(uint8_t)str[3];
		if (  ((b1 & 0xC0) == 0x80) && ((b2 & 0xC0) == 0x80) && ((b3 & 0xC0) == 0x80)  ) {
			*chPtr = ((b0 & 0x7) << 18) | ((b1 & 0x3F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F);
			return 4;
		}
		*chPtr = b0;
		return 1;
	}else{
		printf("WARNING: utf-8 above 21-bit unicode range\n");
	}
	return 0;
}
