Do not require 4byte alignment in isValidUtf8

This commit is contained in:
Alex Hultman
2020-01-01 17:37:16 +01:00
parent 736e50bdc8
commit 9038988a48
+8 -3
View File
@@ -105,9 +105,15 @@ T cond_byte_swap(T value) {
static bool isValidUtf8(unsigned char *s, size_t length) static bool isValidUtf8(unsigned char *s, size_t length)
{ {
for (unsigned char *e = s + length; s != e; ) { for (unsigned char *e = s + length; s != e; ) {
if (s + 4 <= e && ((*(uint32_t *) s) & 0x80808080) == 0) { if (s + 4 <= e) {
uint32_t tmp;
memcpy(&tmp, s, 4);
if ((tmp & 0x80808080) == 0) {
s += 4; s += 4;
} else { continue;
}
}
while (!(*s & 0x80)) { while (!(*s & 0x80)) {
if (++s == e) { if (++s == e) {
return true; return true;
@@ -135,7 +141,6 @@ static bool isValidUtf8(unsigned char *s, size_t length)
return false; return false;
} }
} }
}
return true; return true;
} }