--!strict -- codecs/text/KeyValues/Binary: binary KeyValues (KeyValues::ReadAsBinary; Steam shortcuts.vdf / appinfo values). -- Types: 0 block, 1 string, 2 int32, 3 float32, 4 pointer, 5 wide string (UTF-16LE), 6 color, 7 uint64, 8 end, -- 10 int64, 11 end (alternative). 64-bit values are numbers below 2^53, decimal strings above. local Err = require("@rosetta/core/Err") local Text = require("@rosetta/codecs/text/Text") local Binary = {} export type Pair = { key: string, value: any, -- string | number | Block cond: string?, -- text: the [$CONDITION] kept when not evaluated type: string?, -- binary: "int32" | "float32" | "pointer" | "wstring" | "color" | "uint64" | "int64" (nil: string / block) } export type Block = { Pair } local readu8, readu32, readi32, readf32 = buffer.readu8, buffer.readu32, buffer.readi32, buffer.readf32 local TYPES: { [number]: string } = { [2] = "int32", [3] = "float32", [4] = "pointer", [5] = "wstring", [6] = "color", [7] = "uint64", [10] = "int64" } local CODES: { [string]: number } = { int32 = 2, float32 = 3, pointer = 4, wstring = 5, color = 6, uint64 = 7, int64 = 10 } -- (hi, lo) u32 halves -> number below 2^53, else the decimal digits local function u64text(hi: number, lo: number): number | string if hi < 2 ^ 21 then return hi * 4294967296 + lo end local digits: { string } = {} -- base 10^6 long division over 16-bit limbs local limbs = { hi // 65536, hi % 65536, lo // 65536, lo % 65536 } while limbs[1] + limbs[2] + limbs[3] + limbs[4] > 0 do local rem = 0 for i = 1, 4 do local cur = rem * 65536 + limbs[i] limbs[i] = cur // 1000000 rem = cur % 1000000 end table.insert(digits, 1, string.format("%06d", rem)) end local s = string.gsub(table.concat(digits), "^0+", "") return if s == "" then 0 else s end -- number or decimal string -> (hi, lo), two's complement for negatives local function u64parts(v: any): (number?, number?) if type(v) == "number" then if v ~= v or math.abs(v) >= 2 ^ 63 then return nil, nil end v = math.floor(v) local m = math.abs(v) local hi = m // 4294967296 local lo = m - hi * 4294967296 if v < 0 then return (4294967295 - hi + (if lo == 0 then 1 else 0)) % 4294967296, (4294967296 - lo) % 4294967296 end return hi, lo elseif type(v) == "string" then local neg, digits = string.match(v, "^(%-?)(%d+)$") if not digits or #digits > 20 then return nil, nil end local limbs = { 0, 0, 0, 0 } -- 16-bit, most significant first for i = 1, #digits do local carry = tonumber(string.sub(digits, i, i)) :: number for k = 4, 1, -1 do local cur = limbs[k] * 10 + carry limbs[k] = cur % 65536 carry = cur // 65536 end if carry > 0 then return nil, nil end end if neg == "-" then local carry = 1 for k = 4, 1, -1 do local cur = (65535 - limbs[k]) + carry limbs[k] = cur % 65536 carry = cur // 65536 end end return limbs[1] * 65536 + limbs[2], limbs[3] * 65536 + limbs[4] end return nil, nil end function Binary.read(buf: buffer | string, offset: number?, length: number?, opts: { maxDepth: number? }?): (Block?, Err.Err?) local b: buffer = if type(buf) == "string" then buffer.fromstring(buf) else buf if type(b) ~= "buffer" then return nil, Err.new(Err.ARG, "KeyValues.readBinary: expected a buffer") end local o = offset or 0 local e = o + (length or (buffer.len(b) - o)) if o < 0 or e > buffer.len(b) or e < o then return nil, Err.new(Err.ARG, "KeyValues.readBinary: range out of the buffer") end local maxDepth = opts and opts.maxDepth or 256 local p = o local function cstr(): string? local q = p while q < e and readu8(b, q) ~= 0 do q += 1 end if q >= e then return nil end local s = buffer.readstring(b, p, q - p) p = q + 1 return s end local root: Block = {} local stack: { Block } = { root } while true do if p >= e then if #stack > 1 then return nil, Err.new(Err.TRUNCATED, "unclosed block", p - o) end break end local t = readu8(b, p) p += 1 if t == 8 or t == 11 then if #stack == 1 then break end table.remove(stack) else local cur = stack[#stack] local key = cstr() if not key then return nil, Err.new(Err.TRUNCATED, "key", p - o) end if t == 0 then if #stack >= maxDepth then return nil, Err.new(Err.LIMIT, "nesting deeper than maxDepth", p - o) end local sub: Block = {} table.insert(cur, { key = key, value = sub }) table.insert(stack, sub) elseif t == 1 then local v = cstr() if not v then return nil, Err.new(Err.TRUNCATED, "string value", p - o) end table.insert(cur, { key = key, value = v }) elseif t == 5 then local q = p while q + 1 < e and buffer.readu16(b, q) ~= 0 do q += 2 end if q + 1 >= e then return nil, Err.new(Err.TRUNCATED, "wide string value", p - o) end table.insert(cur, { key = key, value = Text.utf16(b, p, q - p), type = "wstring" }) p = q + 2 elseif t == 2 or t == 3 or t == 4 or t == 6 then if p + 4 > e then return nil, Err.new(Err.TRUNCATED, "value", p - o) end local v = if t == 2 then readi32(b, p) elseif t == 3 then readf32(b, p) else readu32(b, p) table.insert(cur, { key = key, value = v, type = TYPES[t] }) p += 4 elseif t == 7 or t == 10 then if p + 8 > e then return nil, Err.new(Err.TRUNCATED, "value", p - o) end local lo, hi = readu32(b, p), readu32(b, p + 4) local v: number | string if t == 10 and hi >= 2 ^ 31 then -- negative: magnitude of the two's complement local nlo = (4294967296 - lo) % 4294967296 local nhi = 4294967295 - hi + (if lo == 0 then 1 else 0) local m = u64text(nhi, nlo) v = if type(m) == "number" then -m else "-" .. m else v = u64text(hi, lo) end table.insert(cur, { key = key, value = v, type = TYPES[t] }) p += 8 else return nil, Err.new(Err.CORRUPT, "unknown value type " .. t, p - o - 1) end end end return root, nil end local function utf16(s: string): string local out: { string } = {} for _, c in utf8.codes(s) do if c >= 0x10000 then local v = c - 0x10000 local hs = 0xD800 + v // 1024 table.insert(out, string.char(hs % 256, hs // 256)) local lo = 0xDC00 + v % 1024 table.insert(out, string.char(lo % 256, lo // 256)) else table.insert(out, string.char(c % 256, c // 256)) end end return table.concat(out) end local function writeBlock(blk: Block, out: { string }, depth: number): string? if depth > 256 then return "nesting deeper than 256" end for _, pr in blk do if type(pr) ~= "table" or type(pr.key) ~= "string" or string.find(pr.key, "%z") then return "a pair needs a string key without NUL" end local v: any = pr.value local k = pr.key .. "\0" if type(v) == "table" then table.insert(out, "\0" .. k) local err = writeBlock(v, out, depth + 1) if err then return err end table.insert(out, "\8") else local code = if pr.type then CODES[pr.type] else (if type(v) == "number" then (if v % 1 == 0 and v >= -2 ^ 31 and v < 2 ^ 31 then 2 else 3) else 1) if not code then return "unknown value type " .. tostring(pr.type) end local w = buffer.create(8) if code == 1 then if type(v) ~= "string" or string.find(v, "%z") then return "string value expected for key " .. pr.key end table.insert(out, "\1" .. k .. v .. "\0") elseif code == 5 then if type(v) ~= "string" or not utf8.len(v) then return "UTF-8 string value expected for key " .. pr.key end table.insert(out, "\5" .. k .. utf16(v) .. "\0\0") elseif code == 7 or code == 10 then local hi, lo = u64parts(v) if not hi then return "64-bit value expected for key " .. pr.key end buffer.writeu32(w, 0, lo :: number) buffer.writeu32(w, 4, hi :: number) table.insert(out, string.char(code) .. k .. buffer.readstring(w, 0, 8)) else if type(v) ~= "number" then return "number value expected for key " .. pr.key end if code == 3 then buffer.writef32(w, 0, v) elseif code == 2 then if v % 1 ~= 0 or v < -2 ^ 31 or v >= 2 ^ 31 then return "int32 out of range for key " .. pr.key end buffer.writei32(w, 0, v) else if v % 1 ~= 0 or v < 0 or v >= 2 ^ 32 then return "u32 out of range for key " .. pr.key end buffer.writeu32(w, 0, v) end table.insert(out, string.char(code) .. k .. buffer.readstring(w, 0, 4)) end end end return nil end function Binary.write(blk: Block): (buffer?, Err.Err?) if type(blk) ~= "table" then return nil, Err.new(Err.ARG, "KeyValues.writeBinary: expected a pair list") end local out: { string } = {} local err = writeBlock(blk, out, 0) if err then return nil, Err.new(Err.ARG, err) end table.insert(out, "\8") return buffer.fromstring(table.concat(out)), nil end -- a type byte 0 / 1 / 2 then a printable NUL-terminated key function Binary.sniff(b: buffer, o: number, n: number): boolean if n < 3 then return false end local t = readu8(b, o) if t ~= 0 and t ~= 1 and t ~= 2 then return false end for i = 1, math.min(n - 1, 64) do local c = readu8(b, o + i) if c == 0 then return i > 1 elseif c < 32 then return false end end return false end return Binary