libs: Fixed serialization stuff

This commit is contained in:
Steven Barth 2008-07-26 17:24:46 +00:00
parent 47774572e0
commit e5df13e80e
3 changed files with 12 additions and 28 deletions

View file

@ -375,28 +375,15 @@ function clone(object, deep)
return copy return copy
end end
-- Test whether the given table value is a numerically indexed table.
function _is_numeric_table(t)
local k = pairs(t)(t)
return ( tonumber(k) ~= nil )
end
-- Serialize the contents of a table value. -- Serialize the contents of a table value.
function _serialize_table(t) function _serialize_table(t)
local data = "" local data = ""
if _is_numeric_table(t) then
for i, v in ipairs(t) do
v = serialize_data(v)
data = data .. ( #data > 0 and ", " or "" ) .. v
end
else
for k, v in pairs(t) do for k, v in pairs(t) do
k = serialize_data(k) k = serialize_data(k)
v = serialize_data(v) v = serialize_data(v)
data = data .. ( #data > 0 and "; " or "" ) .. data = data .. ( #data > 0 and ", " or "" ) ..
'[' .. k .. '] = ' .. v '[' .. k .. '] = ' .. v
end end
end
return data return data
end end
@ -410,15 +397,13 @@ function serialize_data(val)
if val == nil then if val == nil then
return "nil" return "nil"
elseif type(val) == "number" then elseif type(val) == "number" then
return tostring(val) return val
elseif type(val) == "string" then elseif type(val) == "string" then
val = val:gsub("\\", "\\\\") return string.format("%q", val)
:gsub("\r", "\\r")
:gsub("\n", "\\n")
:gsub('"','\\"')
return '"' .. val .. '"'
elseif type(val) == "boolean" then elseif type(val) == "boolean" then
return val and "true" or "false" return val and "true" or "false"
elseif type(val) == "function" then
return string.format("loadstring(%q)", get_bytecode(val))
elseif type(val) == "table" then elseif type(val) == "table" then
return "{ " .. _serialize_table(val) .. " }" return "{ " .. _serialize_table(val) .. " }"
else else

View file

@ -14,6 +14,7 @@ $Id$
]]-- ]]--
module("luci.sauth", package.seeall) module("luci.sauth", package.seeall)
require("luci.fs") require("luci.fs")
require("luci.util")
require("luci.config") require("luci.config")

View file

@ -68,10 +68,8 @@ function compile(template)
template = template:gsub("(%s*)<%%(%-?)(.-)(%-?)%%>(%s*)", expr_add) template = template:gsub("(%s*)<%%(%-?)(.-)(%-?)%%>(%s*)", expr_add)
local function sanitize(s) local function sanitize(s)
s = luci.util.escape(s) s = string.format("%q", s)
s = luci.util.escape(s, "'") return s:sub(2, #s-1)
s = luci.util.escape(s, "\n")
return s
end end
-- Escape and sanitize all the template (all non-expressions) -- Escape and sanitize all the template (all non-expressions)