in crawl/filter_utf8.cc [62:94]
bool valid_utf8(uint8_t* str, size_t length)
{
uint8_t* end = str + length;
while (str < end) {
if (str[0] < 0x80) {
// 0.xxxxxxx
str += 1;
} else if ((str[0] & 0xe0) == 0xc0) {
// 110.xxxxx 10.xxxxxx
if (str + 1 >= end) return false;
if (!continuation(str + 1, 1)) return false;
if (overlong_2(str)) return false;
str += 2;
} else if ((str[0] & 0xf0) == 0xe0) {
// 1110.xxxx 10.xxxxxx 10.xxxxxx
if (str + 2 >= end) return false;
if (!continuation(str + 1, 2)) return false;
if (overlong_3(str)) return false;
if (surrogate(str)) return false;
str += 3;
} else if ((str[0] & 0xf8) == 0xf0) {
// 11110.xxx 10.xxxxxx 10.xxxxxx 10.xxxxxx
if (str + 3 >= end) return false;
if (!continuation(str + 1, 3)) return false;
if (overlong_4(str)) return false;
if (invalid(str)) return false;
str += 4;
} else {
return false;
}
}
return true;
}