Skip to content

Commit

Permalink
Follow exact UTF-16 rules
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-fb-martin committed Jul 6, 2020
1 parent ff3e8ba commit 2e568ed
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions echttp_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,22 +255,22 @@ static const char *echttp_json_string (JsonContext context) {
l = hex2bin(from[4]);
if (h < 0 || l < 0) return "invalid unicode";
ucode += 16 * h + l;
if (from[5] != '\\' || from[6] != 'u') {
if (ucode < 0xd800 || ucode >= 0xe000) {
// Convert UTF-16 to UTF-8:
if (ucode < 0x80) {
*to++ = (char) (ucode & 0x7f);
} else if (ucode < 0x800) {
*to++ = 0xc0 + ((ucode >> 6) & 0x1f);
*to++ = 0x80 + (ucode & 0x3f);
} else if (ucode >= 0xD800) {
return "reserved UTF-16 character";
} else {
*to++ = 0xc0 + ((ucode >> 12) & 0x1f);
*to++ = 0x80 + ((ucode > 6) & 0x3f);
*to++ = 0x80 + (ucode & 0x3f);
}
from += 4;
} else {
if (from[5] != '\\' || from[6] != 'u')
return "missing 2nd half of surrogate pair";
// UTF-32 coded as a surrogate pair.
ucode -= 0xd800;
if (ucode < 0 || ucode > 0x3ff)
Expand Down

0 comments on commit 2e568ed

Please sign in to comment.