mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 14:58:30 +00:00
isutf8: implement RFC 3629
reject surrogate pairs (U+D800 to U+DFFF) reject greater than U+10FFFF
This commit is contained in:
parent
8a8fc9a65f
commit
a39caefc8b
1 changed files with 15 additions and 3 deletions
|
@ -27,8 +27,8 @@ static const char kUtf8Dispatch[] = {
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, // 0320
|
1, 1, 1, 1, 1, 1, 1, 1, // 0320
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, // 0330
|
1, 1, 1, 1, 1, 1, 1, 1, // 0330
|
||||||
2, 3, 3, 3, 3, 3, 3, 3, // 0340 utf8-3
|
2, 3, 3, 3, 3, 3, 3, 3, // 0340 utf8-3
|
||||||
3, 3, 3, 3, 3, 3, 3, 3, // 0350
|
3, 3, 3, 3, 3, 4, 3, 3, // 0350
|
||||||
4, 5, 5, 5, 5, 0, 0, 0, // 0360 utf8-4
|
5, 6, 6, 6, 7, 0, 0, 0, // 0360 utf8-4
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, // 0370
|
0, 0, 0, 0, 0, 0, 0, 0, // 0370
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -93,6 +93,7 @@ bool32 isutf8(const void *data, size_t size) {
|
||||||
return false; // overlong
|
return false; // overlong
|
||||||
}
|
}
|
||||||
// fallthrough
|
// fallthrough
|
||||||
|
case 3:
|
||||||
case3:
|
case3:
|
||||||
if (p + 2 <= e && //
|
if (p + 2 <= e && //
|
||||||
(p[0] & 0300) == 0200 && //
|
(p[0] & 0300) == 0200 && //
|
||||||
|
@ -103,11 +104,17 @@ bool32 isutf8(const void *data, size_t size) {
|
||||||
return false; // missing cont
|
return false; // missing cont
|
||||||
}
|
}
|
||||||
case 4:
|
case 4:
|
||||||
|
if (p < e && (*p & 040)) {
|
||||||
|
return false; // utf-16 surrogate
|
||||||
|
}
|
||||||
|
goto case3;
|
||||||
|
case 5:
|
||||||
if (p < e && (*p & 0377) < 0220) {
|
if (p < e && (*p & 0377) < 0220) {
|
||||||
return false; // overlong
|
return false; // overlong
|
||||||
}
|
}
|
||||||
// fallthrough
|
// fallthrough
|
||||||
case 5:
|
case 6:
|
||||||
|
case6:
|
||||||
if (p + 3 <= e && //
|
if (p + 3 <= e && //
|
||||||
(((uint32_t)(p[+2] & 0377) << 030 | //
|
(((uint32_t)(p[+2] & 0377) << 030 | //
|
||||||
(uint32_t)(p[+1] & 0377) << 020 | //
|
(uint32_t)(p[+1] & 0377) << 020 | //
|
||||||
|
@ -119,6 +126,11 @@ bool32 isutf8(const void *data, size_t size) {
|
||||||
} else {
|
} else {
|
||||||
return false; // missing cont
|
return false; // missing cont
|
||||||
}
|
}
|
||||||
|
case 7:
|
||||||
|
if (p < e && (*p & 0x3F) > 0xF) {
|
||||||
|
return false; // over limit
|
||||||
|
}
|
||||||
|
goto case6;
|
||||||
default:
|
default:
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue