data: data/plist (XML read/write byte-identical to plistlib, binary bplist00 read incl. UIDs / shared refs / cycle guard); plistlib fixtures. Array detection in Json / msgpack / cbor encoders no longer trusts next(t, #t)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
2d02b78f65
commit
4d90831cf7
10 files changed
+830
-9
No files matched your search
@@ -86,6 +86,18 @@ local list: { Entry } = {
|
||||
return require("@formats/data/msgpack")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "plist",
|
||||
category = "data",
|
||||
path = "data/plist",
|
||||
extensions = { "plist" },
|
||||
mime = { "application/x-plist", "application/x-bplist" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "codecs/Json" },
|
||||
caps = { decode = true, encode = true, binary = "read", ordered = true },
|
||||
load = function()
|
||||
return require("@formats/data/plist")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "toml",
|
||||
category = "data",
|
||||
|
||||
@@ -126,9 +126,6 @@ local function kind(t: { [any]: any }, emptyObject: boolean): string
|
||||
end
|
||||
return "object"
|
||||
end
|
||||
if next(t, n) ~= nil then
|
||||
return "object"
|
||||
end
|
||||
local count = 0
|
||||
for _ in next, t do
|
||||
count += 1
|
||||
|
||||
@@ -472,9 +472,6 @@ local function isArray(t: any, emptyMap: boolean): boolean
|
||||
if n == 0 then
|
||||
return next(t) == nil and not emptyMap
|
||||
end
|
||||
if next(t, n) ~= nil then
|
||||
return false
|
||||
end
|
||||
local c = 0
|
||||
for _ in next, t do
|
||||
c += 1
|
||||
|
||||
@@ -341,9 +341,6 @@ local function isArray(t: any, emptyMap: boolean): boolean
|
||||
if n == 0 then
|
||||
return next(t) == nil and not emptyMap
|
||||
end
|
||||
if next(t, n) ~= nil then
|
||||
return false
|
||||
end
|
||||
local c = 0
|
||||
for _ in next, t do
|
||||
c += 1
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- data/plist/Base64: RFC 4648 base64 for plist <data> (decode skips whitespace; encode wraps lines). Never throws.
|
||||
local Base64 = {}
|
||||
|
||||
local ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
local DEC: { [number]: number } = {}
|
||||
for i = 1, 64 do
|
||||
DEC[string.byte(ALPHA, i)] = i - 1
|
||||
end
|
||||
local ENC: { string } = {}
|
||||
for i = 1, 64 do
|
||||
ENC[i - 1] = string.sub(ALPHA, i, i)
|
||||
end
|
||||
|
||||
-- decode(s) -> buffer | nil, message (whitespace ignored; padding optional)
|
||||
function Base64.decode(s: string): (buffer?, string?)
|
||||
local clean = string.gsub(s, "[ \t\r\n]", "")
|
||||
clean = string.gsub(clean, "=+$", "")
|
||||
local n = #clean
|
||||
if n % 4 == 1 then
|
||||
return nil, "bad base64 length"
|
||||
end
|
||||
local out = buffer.create(n * 3 // 4)
|
||||
local o = 0
|
||||
local acc, bits = 0, 0
|
||||
for i = 1, n do
|
||||
local v = DEC[string.byte(clean, i)]
|
||||
if v == nil then
|
||||
return nil, "bad base64 character"
|
||||
end
|
||||
acc = acc * 64 + v
|
||||
bits += 6
|
||||
if bits >= 8 then
|
||||
bits -= 8
|
||||
local b = acc // (2 ^ bits)
|
||||
buffer.writeu8(out, o, b)
|
||||
o += 1
|
||||
acc -= b * 2 ^ bits
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- encode(b, lineBytes?) -> base64 text; lineBytes > 0 puts "\n" after every lineBytes input bytes (and at the end)
|
||||
function Base64.encode(b: buffer, lineBytes: number?): string
|
||||
local n = buffer.len(b)
|
||||
local parts = table.create(n // 3 + 2)
|
||||
local per = lineBytes or 0
|
||||
local k = 0
|
||||
local i = 0
|
||||
while i < n do
|
||||
local a = buffer.readu8(b, i)
|
||||
local c1 = if i + 1 < n then buffer.readu8(b, i + 1) else 0
|
||||
local c2 = if i + 2 < n then buffer.readu8(b, i + 2) else 0
|
||||
local v = a * 65536 + c1 * 256 + c2
|
||||
local q = ENC[v // 262144] .. ENC[(v // 4096) % 64]
|
||||
q ..= if i + 1 < n then ENC[(v // 64) % 64] else "="
|
||||
q ..= if i + 2 < n then ENC[v % 64] else "="
|
||||
k += 1
|
||||
parts[k] = q
|
||||
i += 3
|
||||
if per > 0 and (i % per == 0 or i >= n) then
|
||||
k += 1
|
||||
parts[k] = "\n"
|
||||
end
|
||||
end
|
||||
return table.concat(parts)
|
||||
end
|
||||
|
||||
return Base64
|
||||
@@ -0,0 +1,271 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- data/plist/Binary: the binary property list reader ("bplist00", CoreFoundation's format as documented in
|
||||
-- CFBinaryPList.c and implemented by Python's plistlib). Objects: null / bool / fill, ints (1, 2, 4, 8 bytes; 16-byte
|
||||
-- ints are read from their low 8 bytes), reals (4 / 8 bytes), dates, data, ASCII / UTF-16BE / UTF-8 strings, UIDs,
|
||||
-- arrays, sets (as arrays), dicts (Types.object, key order kept). Shared references decode to the same table; cycles
|
||||
-- are errors. Never throws: errors return nil, message.
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Binary = {}
|
||||
|
||||
local readu8 = buffer.readu8
|
||||
local TWO32 = 4294967296
|
||||
local EPOCH_2001 = 978307200
|
||||
|
||||
local UidMeta = { __tostring = function(u: any): string
|
||||
return "UID(" .. u.uid .. ")"
|
||||
end }
|
||||
function Binary.uid(n: number): { uid: number }
|
||||
return setmetatable({ uid = n }, UidMeta) :: any
|
||||
end
|
||||
function Binary.isUid(v: any): boolean
|
||||
return type(v) == "table" and getmetatable(v) == UidMeta
|
||||
end
|
||||
|
||||
local function be(b: buffer, o: number, n: number): number
|
||||
local v = 0
|
||||
for k = 0, n - 1 do
|
||||
v = v * 256 + readu8(b, o + k)
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
local function utf8char(c: number): string
|
||||
if c < 0x80 then
|
||||
return string.char(c)
|
||||
elseif c < 0x800 then
|
||||
return string.char(0xC0 + c // 64, 0x80 + c % 64)
|
||||
elseif c < 0x10000 then
|
||||
return string.char(0xE0 + c // 4096, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
||||
end
|
||||
return string.char(0xF0 + c // 262144, 0x80 + (c // 4096) % 64, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
||||
end
|
||||
|
||||
export type Options = { bigInts: ("number" | "string")?, maxDepth: number? }
|
||||
|
||||
-- decode(buf, base, len, opts) -> value | nil, message
|
||||
function Binary.decode(b: buffer, base: number, len: number, opts: Options?): (any, string?)
|
||||
if len < 40 then
|
||||
return nil, "truncated: shorter than header + trailer"
|
||||
end
|
||||
local t = base + len - 32
|
||||
local offSize, refSize = readu8(b, t + 6), readu8(b, t + 7)
|
||||
local numObjects = be(b, t + 8, 8)
|
||||
local top = be(b, t + 16, 8)
|
||||
local tableOff = be(b, t + 24, 8)
|
||||
if offSize < 1 or offSize > 8 or refSize < 1 or refSize > 8 then
|
||||
return nil, "bad trailer sizes"
|
||||
end
|
||||
if numObjects < 1 or top >= numObjects or tableOff < 8 or tableOff + numObjects * offSize > len - 32 then
|
||||
return nil, "bad trailer"
|
||||
end
|
||||
local maxDepth = (opts and opts.maxDepth) or 512
|
||||
local cache: { [number]: any } = {}
|
||||
local busy: { [number]: boolean } = {}
|
||||
|
||||
local object: (idx: number, depth: number) -> (any, string?)
|
||||
|
||||
-- a length nibble: 0..14, or 15 = an int object follows
|
||||
local function count(o: number, nib: number): (number?, number)
|
||||
if nib < 15 then
|
||||
return nib, o + 1
|
||||
end
|
||||
if o + 2 > len - 32 then
|
||||
return nil, 0
|
||||
end
|
||||
local m = readu8(b, base + o + 1)
|
||||
if m // 16 ~= 1 then
|
||||
return nil, 0
|
||||
end
|
||||
local w = 2 ^ (m % 16)
|
||||
if w > 8 or o + 2 + w > len - 32 then
|
||||
return nil, 0
|
||||
end
|
||||
return be(b, base + o + 2, w), o + 2 + w
|
||||
end
|
||||
|
||||
local function ref(at: number): number
|
||||
return be(b, base + at, refSize)
|
||||
end
|
||||
|
||||
function object(idx: number, depth: number): (any, string?)
|
||||
local cached = cache[idx]
|
||||
if cached ~= nil then
|
||||
return cached, nil
|
||||
end
|
||||
if busy[idx] then
|
||||
return nil, "object references form a cycle"
|
||||
end
|
||||
if depth > maxDepth then
|
||||
return nil, "nested deeper than " .. maxDepth
|
||||
end
|
||||
if idx >= numObjects then
|
||||
return nil, "object reference out of range"
|
||||
end
|
||||
local o = be(b, base + tableOff + idx * offSize, offSize)
|
||||
if o < 8 or o >= len - 32 then
|
||||
return nil, "object offset out of range"
|
||||
end
|
||||
local marker = readu8(b, base + o)
|
||||
local hi, nib = marker // 16, marker % 16
|
||||
local v: any
|
||||
if marker == 0x00 then
|
||||
v = Types.null
|
||||
elseif marker == 0x08 then
|
||||
v = false
|
||||
elseif marker == 0x09 then
|
||||
v = true
|
||||
elseif hi == 1 then
|
||||
local w = 2 ^ nib
|
||||
if w > 16 or o + 1 + w > len - 32 then
|
||||
return nil, "bad integer"
|
||||
end
|
||||
if w == 16 then
|
||||
o += 8
|
||||
w = 8
|
||||
end
|
||||
if w == 8 then
|
||||
local h, l = be(b, base + o + 1, 4), be(b, base + o + 5, 4)
|
||||
if h >= 0x80000000 and nib == 3 then
|
||||
v = (h - TWO32) * TWO32 + l
|
||||
elseif opts and opts.bigInts == "string" and h >= 0x200000 then
|
||||
-- exact decimal text of h * 2^32 + l
|
||||
local digits = {}
|
||||
local limbs = { h // 65536, h % 65536, l // 65536, l % 65536 }
|
||||
while limbs[1] + limbs[2] + limbs[3] + limbs[4] > 0 do
|
||||
local rem = 0
|
||||
for k = 1, 4 do
|
||||
local cur = rem * 65536 + limbs[k]
|
||||
limbs[k] = cur // 10
|
||||
rem = cur % 10
|
||||
end
|
||||
table.insert(digits, 1, string.char(48 + rem))
|
||||
end
|
||||
v = table.concat(digits)
|
||||
else
|
||||
v = h * TWO32 + l
|
||||
end
|
||||
else
|
||||
v = be(b, base + o + 1, w)
|
||||
end
|
||||
elseif hi == 2 then
|
||||
if nib == 2 and o + 5 <= len - 32 then
|
||||
local s = buffer.create(4)
|
||||
for k = 0, 3 do
|
||||
buffer.writeu8(s, k, readu8(b, base + o + 4 - k))
|
||||
end
|
||||
v = buffer.readf32(s, 0)
|
||||
elseif nib == 3 and o + 9 <= len - 32 then
|
||||
local s = buffer.create(8)
|
||||
for k = 0, 7 do
|
||||
buffer.writeu8(s, k, readu8(b, base + o + 8 - k))
|
||||
end
|
||||
v = buffer.readf64(s, 0)
|
||||
else
|
||||
return nil, "bad real"
|
||||
end
|
||||
elseif marker == 0x33 then
|
||||
if o + 9 > len - 32 then
|
||||
return nil, "truncated date"
|
||||
end
|
||||
local s = buffer.create(8)
|
||||
for k = 0, 7 do
|
||||
buffer.writeu8(s, k, readu8(b, base + o + 8 - k))
|
||||
end
|
||||
local secs = buffer.readf64(s, 0)
|
||||
if secs ~= secs or math.abs(secs) > 1e14 then
|
||||
return nil, "bad date"
|
||||
end
|
||||
v = Types.Date.fromUnix(secs + EPOCH_2001)
|
||||
elseif hi == 4 or hi == 5 or hi == 6 or hi == 7 then
|
||||
local n, p = count(o, nib)
|
||||
if not n then
|
||||
return nil, "bad length"
|
||||
end
|
||||
local bytes = if hi == 6 then n * 2 else n
|
||||
if p + bytes > len - 32 then
|
||||
return nil, "string / data past the end"
|
||||
end
|
||||
if hi == 4 then
|
||||
local d = buffer.create(bytes)
|
||||
buffer.copy(d, 0, b, base + p, bytes)
|
||||
v = d
|
||||
elseif hi == 6 then
|
||||
local parts = table.create(n)
|
||||
local k = 0
|
||||
while k < n do
|
||||
local u = be(b, base + p + k * 2, 2)
|
||||
k += 1
|
||||
if u >= 0xD800 and u <= 0xDBFF and k < n then
|
||||
local lo = be(b, base + p + k * 2, 2)
|
||||
if lo >= 0xDC00 and lo <= 0xDFFF then
|
||||
u = 0x10000 + (u - 0xD800) * 1024 + (lo - 0xDC00)
|
||||
k += 1
|
||||
end
|
||||
end
|
||||
table.insert(parts, utf8char(u))
|
||||
end
|
||||
v = table.concat(parts)
|
||||
else
|
||||
v = buffer.readstring(b, base + p, bytes)
|
||||
end
|
||||
elseif hi == 8 then
|
||||
if o + 2 + nib > len - 32 then
|
||||
return nil, "bad UID"
|
||||
end
|
||||
v = Binary.uid(be(b, base + o + 1, nib + 1))
|
||||
elseif hi == 0xA or hi == 0xC or hi == 0xD then
|
||||
local n, p = count(o, nib)
|
||||
if not n then
|
||||
return nil, "bad length"
|
||||
end
|
||||
local refs = if hi == 0xD then n * 2 else n
|
||||
if p + refs * refSize > len - 32 then
|
||||
return nil, "collection past the end"
|
||||
end
|
||||
busy[idx] = true
|
||||
if hi == 0xD then
|
||||
local t2 = {}
|
||||
local order = table.create(n)
|
||||
for k = 0, n - 1 do
|
||||
local key, ke = object(ref(p + k * refSize), depth + 1)
|
||||
if ke then
|
||||
return nil, ke
|
||||
end
|
||||
if type(key) ~= "string" then
|
||||
return nil, "dict key is not a string"
|
||||
end
|
||||
local val, ve = object(ref(p + (n + k) * refSize), depth + 1)
|
||||
if ve then
|
||||
return nil, ve
|
||||
end
|
||||
if t2[key] == nil then
|
||||
table.insert(order, key)
|
||||
end
|
||||
t2[key] = val
|
||||
end
|
||||
v = Types.object(t2, order)
|
||||
else
|
||||
local arr = table.create(n)
|
||||
for k = 0, n - 1 do
|
||||
local item, ie = object(ref(p + k * refSize), depth + 1)
|
||||
if ie then
|
||||
return nil, ie
|
||||
end
|
||||
arr[k + 1] = item
|
||||
end
|
||||
v = arr
|
||||
end
|
||||
busy[idx] = nil
|
||||
else
|
||||
return nil, string.format("unknown object marker 0x%02x", marker)
|
||||
end
|
||||
cache[idx] = v
|
||||
return v, nil
|
||||
end
|
||||
|
||||
return object(top, 0)
|
||||
end
|
||||
|
||||
return Binary
|
||||
@@ -0,0 +1,306 @@
|
||||
--!strict
|
||||
-- data/plist: Apple property lists. New code. Supports: XML plists (read and write, through codecs/Xml; the write is
|
||||
-- byte-identical to Python's plistlib for the same key order) and binary plists ("bplist00", read). Values: dict ->
|
||||
-- Types.object (key order kept), array, string, integer / real -> number, true / false, date -> Types.Date, data ->
|
||||
-- buffer, UID (binary) -> Plist.uid(n). Not supported: binary write, the old OpenStep text format, integers past
|
||||
-- 2^63. Note: Luau has one number type, so a real with an integral value is written back as <integer>.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
local Json = require("@formats/codecs/Json")
|
||||
local Base64 = require("@formats/data/plist/Base64")
|
||||
local Binary = require("@formats/data/plist/Binary")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Plist = {
|
||||
name = "plist",
|
||||
category = "data",
|
||||
extensions = { "plist" },
|
||||
mime = { "application/x-plist", "application/x-bplist" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "codecs/Json" },
|
||||
caps = { decode = true, encode = true, binary = "read", ordered = true },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
bigInts: ("number" | "string")?, -- integers past 2^53: "string" keeps exact decimal text
|
||||
maxDepth: number?, -- default 512
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
sortKeys: boolean?, -- plain tables' keys sorted (default true; Types.object keeps its order)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Plist.sniff(r: Bytes.Reader): number
|
||||
local head = r:peek(256)
|
||||
if string.sub(head, 1, 6) == "bplist" then
|
||||
return 1
|
||||
end
|
||||
if string.find(head, "<!DOCTYPE plist", 1, true) or string.find(head, "<plist", 1, true) then
|
||||
return 0.9
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function xmlValue(node: any, opts: DecodeOptions?, depth: number): (any, string?)
|
||||
if depth > ((opts and opts.maxDepth) or 512) then
|
||||
return nil, "nested too deeply"
|
||||
end
|
||||
local name = node.name
|
||||
if name == "dict" then
|
||||
local out = Types.object()
|
||||
local kids = Xml.children(node)
|
||||
local i = 1
|
||||
while i <= #kids do
|
||||
local k = kids[i]
|
||||
if k.name ~= "key" then
|
||||
return nil, "dict: expected <key>, got <" .. k.name .. ">"
|
||||
end
|
||||
local v = kids[i + 1]
|
||||
if not v then
|
||||
return nil, "dict: <key> without a value"
|
||||
end
|
||||
local val, e = xmlValue(v, opts, depth + 1)
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
Types.set(out, Xml.text(k), val)
|
||||
i += 2
|
||||
end
|
||||
return out, nil
|
||||
elseif name == "array" then
|
||||
local out = {}
|
||||
for _, ch in Xml.children(node) do
|
||||
local val, e = xmlValue(ch, opts, depth + 1)
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
table.insert(out, val)
|
||||
end
|
||||
return out, nil
|
||||
elseif name == "string" then
|
||||
return Xml.text(node), nil
|
||||
elseif name == "integer" then
|
||||
local t = string.match(Xml.text(node), "^%s*(.-)%s*$") :: string
|
||||
local neg, hex = string.match(t, "^([%-%+]?)0[xX](%x+)$")
|
||||
local n: number?
|
||||
if hex then
|
||||
n = tonumber(hex, 16)
|
||||
if n and neg == "-" then
|
||||
n = -n
|
||||
end
|
||||
elseif string.match(t, "^[%-%+]?%d+$") then
|
||||
n = tonumber(t)
|
||||
if n and math.abs(n) >= 2 ^ 53 and opts and opts.bigInts == "string" then
|
||||
return (string.gsub(t, "^%+", "")), nil
|
||||
end
|
||||
end
|
||||
if not n then
|
||||
return nil, "bad integer " .. string.sub(t, 1, 30)
|
||||
end
|
||||
return n, nil
|
||||
elseif name == "real" then
|
||||
local t = string.lower(string.match(Xml.text(node), "^%s*(.-)%s*$") :: string)
|
||||
local n = tonumber(t)
|
||||
if not n then
|
||||
if t == "nan" or t == "+nan" or t == "-nan" then
|
||||
n = 0 / 0
|
||||
elseif t == "inf" or t == "+inf" or t == "infinity" or t == "+infinity" then
|
||||
n = math.huge
|
||||
elseif t == "-inf" or t == "-infinity" then
|
||||
n = -math.huge
|
||||
end
|
||||
end
|
||||
if not n then
|
||||
return nil, "bad real " .. string.sub(t, 1, 30)
|
||||
end
|
||||
return n, nil
|
||||
elseif name == "true" then
|
||||
return true, nil
|
||||
elseif name == "false" then
|
||||
return false, nil
|
||||
elseif name == "date" then
|
||||
local d = Types.Date.parse((string.match(Xml.text(node), "^%s*(.-)%s*$")))
|
||||
if not d then
|
||||
return nil, "bad date"
|
||||
end
|
||||
return d, nil
|
||||
elseif name == "data" then
|
||||
local b, e = Base64.decode(Xml.text(node))
|
||||
if not b then
|
||||
return nil, e
|
||||
end
|
||||
return b, nil
|
||||
end
|
||||
return nil, "unknown element <" .. tostring(name) .. ">"
|
||||
end
|
||||
|
||||
function Plist.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local len = r.limit - r.base
|
||||
if len >= 8 and buffer.readstring(r.buf, r.base, 6) == "bplist" then
|
||||
local v, e = Binary.decode(r.buf, r.base, len, opts)
|
||||
if e then
|
||||
return nil, Err.new(if string.find(e, "truncated", 1, true) or string.find(e, "past the end", 1, true) then Err.TRUNCATED else Err.CORRUPT, e)
|
||||
end
|
||||
return v, nil
|
||||
end
|
||||
local doc, err = Xml.decode(buffer.readstring(r.buf, r.base, len), { whitespace = true })
|
||||
if not doc then
|
||||
return nil, err
|
||||
end
|
||||
local root = (doc :: any).root
|
||||
if root.name == "plist" then
|
||||
local kids = Xml.children(root)
|
||||
if #kids ~= 1 then
|
||||
return nil, Err.new(Err.CORRUPT, "<plist> must hold exactly one value")
|
||||
end
|
||||
root = kids[1]
|
||||
end
|
||||
local v, e = xmlValue(root, opts, 0)
|
||||
if e then
|
||||
return nil, Err.new(Err.CORRUPT, e)
|
||||
end
|
||||
return v, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local HEADER = '<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n<plist version="1.0">\n'
|
||||
|
||||
local function esc(s: string): (string?, string?)
|
||||
if string.find(s, "[%z\1-\8\11\12\14-\31]") then
|
||||
return nil, "strings cannot contain control characters"
|
||||
end
|
||||
s = string.gsub(string.gsub(s, "\r\n", "\n"), "\r", "\n")
|
||||
return (string.gsub(s, "[&<>]", { ["&"] = "&", ["<"] = "<", [">"] = ">" })), nil
|
||||
end
|
||||
|
||||
local function keyLess(a: any, b: any): boolean
|
||||
return a < b
|
||||
end
|
||||
|
||||
function Plist.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local sort = not (opts and opts.sortKeys == false)
|
||||
local out: { string } = { HEADER }
|
||||
local err: string? = nil
|
||||
local function line(level: number, s: string)
|
||||
table.insert(out, string.rep("\t", level) .. s .. "\n")
|
||||
end
|
||||
local write: (v: any, level: number, depth: number) -> boolean
|
||||
function write(v: any, level: number, depth: number): boolean
|
||||
if depth > 512 then
|
||||
err = "nested deeper than 512 (a cycle?)"
|
||||
return false
|
||||
end
|
||||
local tv = type(v)
|
||||
if tv == "string" then
|
||||
local s, e = esc(v)
|
||||
if not s then
|
||||
err = e
|
||||
return false
|
||||
end
|
||||
line(level, "<string>" .. s .. "</string>")
|
||||
elseif tv == "boolean" then
|
||||
line(level, if v then "<true/>" else "<false/>")
|
||||
elseif tv == "number" then
|
||||
if v == math.floor(v) and math.abs(v) < 2 ^ 63 then
|
||||
line(level, "<integer>" .. string.format("%d", v) .. "</integer>")
|
||||
else
|
||||
local s = Json.number(v)
|
||||
if not s then
|
||||
s = if v ~= v then "nan" elseif v > 0 then "inf" else "-inf"
|
||||
end
|
||||
line(level, "<real>" .. s .. "</real>")
|
||||
end
|
||||
elseif tv == "buffer" then
|
||||
line(level, "<data>")
|
||||
local maxLine = math.max(16, 76 - 8 * level)
|
||||
for l in string.gmatch(Base64.encode(v, maxLine // 4 * 3), "[^\n]+") do
|
||||
line(level, l)
|
||||
end
|
||||
line(level, "</data>")
|
||||
elseif tv == "table" and Types.Date.is(v) then
|
||||
local d = Types.Date.fromUnix(math.floor(Types.Date.toUnix(v)))
|
||||
line(level, string.format("<date>%04d-%02d-%02dT%02d:%02d:%02dZ</date>", d.year :: number, d.month :: number, d.day :: number, d.hour :: number, d.minute :: number, d.second :: number))
|
||||
elseif tv == "table" and Binary.isUid(v) then
|
||||
err = "UIDs can only be written to binary plists"
|
||||
return false
|
||||
elseif tv == "table" and v ~= Types.null then
|
||||
local n = #v
|
||||
local isArray = false
|
||||
if not Types.isObject(v) then
|
||||
local c = 0
|
||||
for _ in next, v do
|
||||
c += 1
|
||||
end
|
||||
isArray = c == n
|
||||
end
|
||||
if isArray then
|
||||
if n == 0 then
|
||||
line(level, "<array/>")
|
||||
return true
|
||||
end
|
||||
line(level, "<array>")
|
||||
for k = 1, n do
|
||||
if not write(v[k], level + 1, depth + 1) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
line(level, "</array>")
|
||||
else
|
||||
local keys: { any }
|
||||
if Types.isObject(v) then
|
||||
keys = Types.keys(v)
|
||||
else
|
||||
keys = {}
|
||||
for k in next, v do
|
||||
table.insert(keys, k)
|
||||
end
|
||||
end
|
||||
for _, k in keys do
|
||||
if type(k) ~= "string" then
|
||||
err = "dict keys must be strings"
|
||||
return false
|
||||
end
|
||||
end
|
||||
if sort and not Types.isObject(v) then
|
||||
table.sort(keys, keyLess)
|
||||
end
|
||||
if #keys == 0 then
|
||||
line(level, "<dict/>")
|
||||
return true
|
||||
end
|
||||
line(level, "<dict>")
|
||||
for _, k in keys do
|
||||
local ks, e = esc(k)
|
||||
if not ks then
|
||||
err = e
|
||||
return false
|
||||
end
|
||||
line(level + 1, "<key>" .. ks .. "</key>")
|
||||
if not write(v[k], level + 1, depth + 1) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
line(level, "</dict>")
|
||||
end
|
||||
else
|
||||
err = "cannot write " .. (if v == Types.null then "null" else "a " .. tv) .. " in a plist"
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
if not write(value, 0, 0) then
|
||||
return nil, Err.new(Err.ARG, err or "cannot encode")
|
||||
end
|
||||
table.insert(out, "</plist>\n")
|
||||
return buffer.fromstring(table.concat(out)), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
Plist.uid = Binary.uid -- uid(n) -> a UID value (binary plists, NSKeyedArchiver)
|
||||
Plist.isUid = Binary.isUid
|
||||
|
||||
return Plist
|
||||
Reference in new issue
Block a user