Enrik Berkhan wrote: Never post before doing at least _some_ testing :)
static int valid_utf8(const unsigned char *p, size_t len) { if (len >= 1 && p[0] < 0x7f) { return 1; } else if (len >= 2 && p[0] >= 0xC0 && p[0] < 0xE0 && (p[1]&0x80) == 0x80) { Of course, all the masks should be 0xC0 instead of 0x80.
return 2; } else if (len >= 3 && p[0] >= 0xE0 && p[0] < 0xF0 && (p[1]&0x80) == 0x80 && (p[2]&0x80) == 0x80) { return 3; } else if (len >= 4 && p[0] >= 0xF0 && p[0] < 0xF8 && (p[1]&0x80) == 0x80 && (p[2]&0x80) == 0x80 && (p[3]&0x80) == 0x80) { return 4; } else if (len >= 5 && p[0] >= 0xF8 && p[0] < 0xFC && (p[1]&0x80) == 0x80 && (p[2]&0x80) == 0x80 && (p[3]&0x80) == 0x80 && (p[4]&0x80) == 0x80) { return 5; } else if (len >= 6 && p[0] >= 0xFC && p[0] < 0xFE && (p[1]&0x80) == 0x80 && (p[2]&0x80) == 0x80 && (p[3]&0x80) == 0x80 && (p[4]&0x80) == 0x80 && (p[5]&0x80) == 0x80) { return 6; } return 0; }
And the code still allows non shortest possible sequences. Enrik