123456789101112131415161718192021222324252627282930313233343536373839404142434445
bool IsUTF8(const void* pBuffer, long size) { bool IsUTF8 = true; unsigned char* start = (unsigned char*)pBuffer; unsigned char* end = (unsigned char*)pBuffer + size; while (start < end) { if (*start < 0x80) // (10000000)[output][/output] { start++; } else if (*start < (0xC0)) // (11000000) { IsUTF8 = false; break; } { if (start >= end - 1) break; if ((start[1] & (0xC0)) != 0x80) { IsUTF8 = false; break; } start += 2; } else if (*start < (0xF0)) // (11110000) { if (start >= end - 2) break; if ((start[1] & (0xC0)) != 0x80 || (start[2] & (0xC0)) != 0x80) { IsUTF8 = false; break; } start += 3; } else { IsUTF8 = false; break; } } return IsUTF8; }