Switch to fallback earlier if the input is not valid UTF-8 prefix

This commit is contained in:
Pavel Tyunin
2020-09-29 15:35:53 +03:00
parent bc838b4773
commit b3eff48e28
4 changed files with 25 additions and 4 deletions

View File

@@ -1446,6 +1446,26 @@ size_t wxMBConvUTF8::FromWChar(char *buf, size_t n,
return len;
}
// checks if the input can be the beginning of a valid UTF-8 string
bool wxIsUTF8Prefix(const char *src, size_t len)
{
unsigned char l;
for ( size_t i = 0; i < len; ++i )
{
l = tableUtf8Lengths[(unsigned char)src[i]];
if ( !l )
return false; // invalid leading byte
while ( --l )
{
if ( ++i == len )
return true; // truncated sequence
if ( (src[i] & 0xC0) != 0x80 )
return false; // invalid continuation byte
}
}
return true;
}
// ============================================================================
// UTF-16
// ============================================================================