- fixed utf8_decode.

This function was written for already validated UTF-8 but not for text that can be mixed with ISO-8859-1.
To handle that properly it needs to do a bit more validation to avoid mangling its output and instead reject invalid input.
This commit is contained in:
Christoph Oelckers 2019-02-24 13:55:08 +01:00
parent 0676a98efc
commit b670d5f372

View file

@ -94,7 +94,9 @@ int utf8_decode(const uint8_t *src, int *size)
return c; return c;
} }
int c1 = src[1] & 0x3f; int c1 = src[1];
if (c1 < 0x80 || c1 >= 0xc0) return -1;
c1 &= 0x3f;
if ((c & 0xE0) == 0xC0) if ((c & 0xE0) == 0xC0)
{ {
@ -107,7 +109,9 @@ int utf8_decode(const uint8_t *src, int *size)
return -1; return -1;
} }
int c2 = src[2] & 0x3f; int c2 = src[2];
if (c2 < 0x80 || c2 >= 0xc0) return -1;
c2 &= 0x3f;
if ((c & 0xF0) == 0xE0) if ((c & 0xF0) == 0xE0)
{ {
@ -120,7 +124,9 @@ int utf8_decode(const uint8_t *src, int *size)
return -1; return -1;
} }
int c3 = src[3] & 0x3f; int c3 = src[3];
if (c3 < 0x80 || c1 >= 0xc0) return -1;
c3 &= 0x3f;
if ((c & 0xF8) == 0xF0) if ((c & 0xF8) == 0xF0)
{ {