--!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 . -- ------------------------------------------------------------------------------------------------ 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/core/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", "core/Base64", "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, " ((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 , got <" .. k.name .. ">" end local v = kids[i + 1] if not v then return nil, "dict: 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, " 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 = '\n\n\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, "" .. s .. "") elseif tv == "boolean" then line(level, if v then "" else "") elseif tv == "number" then if v == math.floor(v) and math.abs(v) < 2 ^ 63 then line(level, "" .. string.format("%d", v) .. "") 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, "" .. s .. "") end elseif tv == "buffer" then line(level, "") local maxLine = math.max(16, 76 - 8 * level) for l in string.gmatch(Base64.encode(v, { wrap = maxLine // 4 * 4 }), "[^\n]+") do line(level, l) end line(level, "") elseif tv == "table" and Types.Date.is(v) then local d = Types.Date.fromUnix(math.floor(Types.Date.toUnix(v))) line(level, string.format("%04d-%02d-%02dT%02d:%02d:%02dZ", 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, "") return true end line(level, "") for k = 1, n do if not write(v[k], level + 1, depth + 1) then return false end end line(level, "") 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, "") return true end line(level, "") for _, k in keys do local ks, e = esc(k) if not ks then err = e return false end line(level + 1, "" .. ks .. "") if not write(v[k], level + 1, depth + 1) then return false end end line(level, "") 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, "\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