aboutsummaryrefslogtreecommitdiffstats
path: root/util/json.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2018-03-27 13:44:40 +0100
committerMatthew Wild <mwild1@gmail.com>2018-03-27 13:44:40 +0100
commit14b0dd8bdf55d5d282221d42352364ec0862c834 (patch)
treef2348fb972cd370fd1ae2721973fb28b109dc931 /util/json.lua
parent479ea4dd26f5efd207249f417c27f4e4bda91316 (diff)
downloadprosody-14b0dd8bdf55d5d282221d42352364ec0862c834.tar.gz
prosody-14b0dd8bdf55d5d282221d42352364ec0862c834.zip
util.json: Unescape surrogate pairs
Diffstat (limited to 'util/json.lua')
-rw-r--r--util/json.lua9
1 files changed, 5 insertions, 4 deletions
diff --git a/util/json.lua b/util/json.lua
index c88d4c09..05af709a 100644
--- a/util/json.lua
+++ b/util/json.lua
@@ -246,7 +246,7 @@ local function _readarray(json, index)
end
end
local _unescape_error;
-local function _unescape_surrogate_func(x) -- luacheck: ignore
+local function _unescape_surrogate_func(x)
local lead, trail = tonumber(x:sub(3, 6), 16), tonumber(x:sub(9, 12), 16);
local codepoint = lead * 0x400 + trail - 0x35FDC00;
local a = codepoint % 64;
@@ -260,8 +260,9 @@ end
local function _unescape_func(x)
x = x:match("%x%x%x%x", 3);
if x then
- --if x >= 0xD800 and x <= 0xDFFF then _unescape_error = true; end -- bad surrogate pair
- return codepoint_to_utf8(tonumber(x, 16));
+ local codepoint = tonumber(x, 16)
+ if codepoint >= 0xD800 and codepoint <= 0xDFFF then _unescape_error = true; end -- bad surrogate pair
+ return codepoint_to_utf8(codepoint);
end
_unescape_error = true;
end
@@ -273,7 +274,7 @@ function _readstring(json, index)
--if s:find("[%z-\31]") then return nil, "control char in string"; end
-- FIXME handle control characters
_unescape_error = nil;
- --s = s:gsub("\\u[dD][89abAB]%x%x\\u[dD][cdefCDEF]%x%x", _unescape_surrogate_func);
+ s = s:gsub("\\u[dD][89abAB]%x%x\\u[dD][cdefCDEF]%x%x", _unescape_surrogate_func);
-- FIXME handle escapes beyond BMP
s = s:gsub("\\u.?.?.?.?", _unescape_func);
if _unescape_error then return nil, "invalid escape"; end