From: Pavel Charvat Date: Sat, 9 Mar 2024 22:49:44 +0000 (+0000) Subject: JSON: Don't generate corrupted output for string with surrogates. X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fdev-json;p=libucw.git JSON: Don't generate corrupted output for string with surrogates. --- diff --git a/ucw-json/format.c b/ucw-json/format.c index 227a26b5..790fa15a 100644 --- a/ucw-json/format.c +++ b/ucw-json/format.c @@ -50,14 +50,16 @@ static void write_string(struct json_context *js, const char *p) bprintf(fb, "\\u%04x", u); } } - else if (u >= 0x007f && (js->format_options & JSON_FORMAT_ESCAPE_NONASCII)) + else if (u < 0x7f) + bputc(fb, u); + else if (u >= 0x110000 || u >= 0xd800 && u <= 0xdfff) + bprintf(fb, "\\u%04x", UNI_REPLACEMENT); + else if (js->format_options & JSON_FORMAT_ESCAPE_NONASCII) { if (u < 0x10000) bprintf(fb, "\\u%04x", u); - else if (u < 0x110000) - bprintf(fb, "\\u%04x\\u%04x", 0xd800 + ((u - 0x10000) >> 10), 0xdc00 + (u & 0x3ff)); else - bprintf(fb, "\\u%04x", UNI_REPLACEMENT); + bprintf(fb, "\\u%04x\\u%04x", 0xd800 + ((u - 0x10000) >> 10), 0xdc00 + (u & 0x3ff)); } else bput_utf8_32(fb, u);