--!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