From eb869811aa8f325e1baff4eee0dfc1b4b4296d44 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 13:52:17 -0500 Subject: [PATCH] ws-game: archive/pak (Quake PACK / SiN SPAK read + write); containers/Archive: slice() lazy loader and collect() for simple archive writers (additive) --- docs/FORMATS.md | 1 + src/Formats/Registry.luau | 12 +++ src/Formats/archive/pak/init.luau | 135 ++++++++++++++++++++++++++++ src/Formats/containers/Archive.luau | 31 +++++++ tests/archive/pak.luau | 87 ++++++++++++++++++ tools/fixtures/pak.py | 63 +++++++++++++ 6 files changed, 329 insertions(+) create mode 100644 src/Formats/archive/pak/init.luau create mode 100644 tests/archive/pak.luau create mode 100644 tools/fixtures/pak.py diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 0a41a66..2c0a5a1 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -53,6 +53,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | archive | br | .br | yes | - | stream: true | `tests/archive/br.luau` | | archive | bz2 | .bz2 .tbz2 .tbz | yes | - | stream: true | `tests/archive/bz2.luau` | | archive | gzip | .gz .tgz .gzip | yes | yes | stream: true | `tests/archive/gzip.luau` | +| archive | pak | .pak .sin | yes | yes | variants: "quake", "sin" | `tests/archive/pak.luau` | | archive | tar | .tar | yes | yes | variants: "ustar", "pax", "gnu" | `tests/archive/tar.luau` | | archive | xz | .xz .txz .lzma | yes | - | stream: true, filters: "lzma2", "lzma", "delta", "x86", "powerpc", "arm", "armthumb", "sparc", "arm64" | `tests/archive/xz.luau` | | archive | zip | .zip .wsz | yes | yes | methods: "store", "deflate", zip64: true | `tests/archive/zip.luau` | diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index 6c0344c..5000567 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -590,6 +590,18 @@ local list: { Entry } = { return require("@formats/archive/gzip") end, }, + { + name = "pak", + category = "archive", + path = "archive/pak", + extensions = { "pak", "sin" }, + mime = { "application/x-pak" }, + deps = { "core/Err", "core/Bytes", "core/Sniff", "containers/Archive" }, + caps = { decode = true, encode = true, variants = { "quake", "sin" } }, + load = function() + return require("@formats/archive/pak") + end, + }, { name = "tar", category = "archive", diff --git a/src/Formats/archive/pak/init.luau b/src/Formats/archive/pak/init.luau new file mode 100644 index 0000000..cd48140 --- /dev/null +++ b/src/Formats/archive/pak/init.luau @@ -0,0 +1,135 @@ +--!strict +-- archive/pak: Quake / Quake II / Half-Life PAK archives ("PACK": 56-byte names, 64-byte directory entries) and the +-- SiN variant ("SPAK": 120-byte names), read into the standard Archive (entries read lazily: a copy of the stored +-- bytes) and written from any Archive value. Written from the format description in the Quake source release +-- (common.c: dpackheader_t / dpackfile_t) and the Quake specs; no code taken. +-- Not supported: Daikatana's compressed PAK entries. + +-- ------------------------------------------------------------------------------------------------ requires +local Err = require("@formats/core/Err") +local Bytes = require("@formats/core/Bytes") +local Sniff = require("@formats/core/Sniff") +local Archive = require("@formats/containers/Archive") + +-- ------------------------------------------------------------------------------------------------ contract +local Pak = { + name = "pak", + category = "archive", + extensions = { "pak", "sin" }, + mime = { "application/x-pak" }, + deps = { "core/Err", "core/Bytes", "core/Sniff", "containers/Archive" }, + caps = { decode = true, encode = true, variants = { "quake", "sin" } }, +} + +-- ------------------------------------------------------------------------------------------------ types +export type DecodeOptions = { maxEntries: number? } -- default 1e6 +export type EncodeOptions = { variant: ("quake" | "sin")? } -- default: the source variant, else "quake" + +-- ------------------------------------------------------------------------------------------------ constants +local VARIANTS = { quake = { magic = "PACK", nameLen = 56 }, sin = { magic = "SPAK", nameLen = 120 } } + +-- ------------------------------------------------------------------------------------------------ sniff +function Pak.sniff(r: Bytes.Reader): number + if r:remaining() < 12 then + return 0 + end + return math.max(Sniff.magic(r, "PACK", 0, 0.9), Sniff.magic(r, "SPAK", 0, 0.9)) +end + +-- ------------------------------------------------------------------------------------------------ decode +local function encode(archive: any, opts: any?): (buffer?, Err.Err?) + return Pak.encode(archive, opts) +end + +function Pak.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?) + local magic = r:string(4) + local dirOfs, dirLen = r:u32le(), r:u32le() + if r.err then + return nil, r.err + end + local variant = if magic == "PACK" then "quake" elseif magic == "SPAK" then "sin" else nil + if not variant then + return nil, Err.new(Err.CORRUPT, "bad magic", 0) + end + local nameLen = VARIANTS[variant].nameLen + local entrySize = nameLen + 8 + local size = r.limit - r.base + if dirOfs + dirLen > size then + return nil, Err.new(Err.TRUNCATED, "directory past the end", 4) + end + if dirLen % entrySize ~= 0 then + return nil, Err.new(Err.CORRUPT, "directory size is not a multiple of " .. entrySize, 8) + end + local count = dirLen // entrySize + if count > (opts and opts.maxEntries or 1e6) then + return nil, Err.new(Err.LIMIT, "too many entries: " .. count) + end + local a = Archive.new("pak", encode) + local d = r:sub(dirOfs, dirLen) + for i = 0, count - 1 do + local name = d:fixed(nameLen) + local at, n = d:u32le(), d:u32le() + if d.err then + return nil, d.err + end + if at + n > size then + return nil, Err.new(Err.TRUNCATED, "entry " .. name .. " past the end", dirOfs + i * entrySize) + end + Archive.put(a, { name = name, size = n, dir = false, method = "store", offset = at, load = Archive.slice(r.buf, r.base + at, n) }) + end + a.meta.source = { format = "pak", variant = variant, entries = count } + return a, nil +end + +-- ------------------------------------------------------------------------------------------------ encode +function Pak.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?) + local items, err = Archive.collect(archive) + if not items then + return nil, err + end + local src = type(archive) == "table" and type(archive.meta) == "table" and archive.meta.source + local variant = opts and opts.variant or (if type(src) == "table" and src.format == "pak" then src.variant else nil) or "quake" + local v = VARIANTS[variant] + if not v then + return nil, Err.new(Err.ARG, "unknown variant " .. tostring(variant)) + end + local files: { { name: string, data: buffer } } = {} + for _, it in items do + if not it.entry.dir then + local name = string.gsub(it.entry.name, "\\", "/") + if #name >= v.nameLen then + return nil, Err.new(Err.ARG, "name longer than " .. (v.nameLen - 1) .. " bytes: " .. name) + end + table.insert(files, { name = name, data = it.data }) + end + end + local w = Bytes.writer() + w:string(v.magic) + w:u32le(0) + w:u32le(0) + local offsets: { number } = {} + for i, f in files do + offsets[i] = w:tell() + w:bytes(f.data) + end + local dirOfs = w:tell() + for i, f in files do + w:string(f.name) + w:fill(0, v.nameLen - #f.name) + w:u32le(offsets[i]) + w:u32le(buffer.len(f.data)) + end + w:setU32le(4, dirOfs) + w:setU32le(8, w:tell() - dirOfs) + return w:finish(), nil +end + +-- ------------------------------------------------------------------------------------------------ extras +-- new(variant?) -> an empty PAK Archive: a:add(name, data); a:write() +function Pak.new(variant: ("quake" | "sin")?): any + local a = Archive.new("pak", encode) + a.meta.source = { format = "pak", variant = variant or "quake" } + return a +end + +return Pak diff --git a/src/Formats/containers/Archive.luau b/src/Formats/containers/Archive.luau index ef9a63d..53817b3 100644 --- a/src/Formats/containers/Archive.luau +++ b/src/Formats/containers/Archive.luau @@ -152,6 +152,37 @@ function Archive.entries(value: any): { { entry: Types.ArchiveEntry, read: () -> return out end +-- slice(buf, at, n) -> a loader for Entry.load: a copy of buf[at, at + n) (stored entries of uncompressed archives) +function Archive.slice(buf: buffer, at: number, n: number): () -> (buffer?, Err.Err?) + return function(): (buffer?, Err.Err?) + local out = buffer.create(n) + buffer.copy(out, 0, buf, at, n) + return out, nil + end +end + +-- collect(value) -> { { entry, data } } | nil, Err: every entry of an Archive-shaped value with its bytes read +-- (directories: data = an empty buffer), for writers of simple archives (PAK, WAD, GRP, VPK, ...) +function Archive.collect(value: any): ({ { entry: Types.ArchiveEntry, data: buffer } }?, Err.Err?) + local items = Archive.entries(value) + if not items then + return nil, Err.new(Err.ARG, "expected an Archive") + end + local out: { { entry: Types.ArchiveEntry, data: buffer } } = {} + for _, it in items do + local b: buffer = buffer.create(0) + if not it.entry.dir then + local d, err = it.read() + if not d then + return nil, err or Err.new(Err.CORRUPT, "entry " .. it.entry.name .. ": read failed") + end + b = d + end + table.insert(out, { entry = it.entry, data = b }) + end + return out, nil +end + -- streamName(opts, ext) -> the entry name for a single-stream archive: opts.name without its compression extension -- (".tgz" -> ".tar"), else "data" function Archive.streamName(opts: any?, exts: { string }): string diff --git a/tests/archive/pak.luau b/tests/archive/pak.luau new file mode 100644 index 0000000..6e03040 --- /dev/null +++ b/tests/archive/pak.luau @@ -0,0 +1,87 @@ +-- archive/pak tests: Quake ("PACK") and SiN ("SPAK") archives written by tools/fixtures/pak.py list the entries an +-- independent Python reader sees (names, sizes, data CRCs); our writer produces the same bytes as the Python writer +-- for the same files and round-trips; conversion from a tar; errors; the fuzz pass. Fixtures: tools/fixtures/pak.py. +local T = require("@tests/T") +local F = require("@formats") +local Pak = require("@formats/archive/pak") +local Bytes = require("@formats/core/Bytes") +local Crc32 = require("@formats/core/Crc32") +local Err = require("@formats/core/Err") +local Fx = require("@tests/gen/pak") + +local t = T.suite("pak") +t:eq(Pak.name, "pak", "name") +t:check(Pak.caps.decode and Pak.caps.encode, "caps") + +for _, c in Fx do + local a, err = Pak.decode(Bytes.reader(buffer.fromstring(c.data))) + t:check(a, c.name .. ": " .. tostring(err)) + local list = a:list() + t:eq(#list, #c.entries, c.name .. ": entry count") + for i, w in c.entries do + t:eq(list[i].name, w.name, c.name .. ": name " .. i) + t:eq(list[i].size, w.size, c.name .. ": size " .. w.name) + t:eq(Crc32.of((a:read(w.name))), w.crc, c.name .. ": data " .. w.name) + end + t:eq(a.meta.source.variant, c.variant, c.name .. ": variant") + if #c.data > 12 then + t:eq(F.detect(c.data), "pak", c.name .. ": F.detect") + end + -- the writer: same layout as the Python writer + local n = Pak.new(c.variant) + for _, f in c.files do + n:add(f.name, f.data) + end + local out = n:write() :: buffer + if c.layoutDefault then + t:check(buffer.tostring(out) == c.data, c.name .. ": bytes identical to the Python writer") + end + local back = Pak.decode(Bytes.reader(out)) + t:eq(#back:list(), #c.files, c.name .. ": round trip count") + -- rewriting a decoded archive keeps its variant + local again = F.save(a) :: buffer + t:eq(buffer.readstring(again, 0, 4), if c.variant == "sin" then "SPAK" else "PACK", c.name .. ": F.save keeps the variant") +end + +-- a sub-range reader, conversion from another archive, errors +do + local c = Fx[1] + local big = buffer.create(#c.data + 7) + buffer.writestring(big, 7, c.data) + local a = Pak.decode(Bytes.reader(big, 7, #c.data)) + t:check(a and Crc32.of((a:read(c.entries[2].name))) == c.entries[2].crc, "decode from a sub-range") + local tar = F.format("tar").new() + tar:add("dir/", "", { dir = true }) + tar:add("dir/x.txt", "x") + local p = F.load(F.save(tar, nil, { format = "pak" }) :: buffer) + t:check(p.format == "pak" and #p:list() == 1 and buffer.tostring(p:read("dir/x.txt")) == "x", "tar -> pak (directories dropped)") + local _, e1 = Pak.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 10)))) + t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1)) + local bad = buffer.fromstring(c.data) + buffer.writeu32(bad, 8, 65) + local _, e2 = Pak.decode(Bytes.reader(bad)) + t:check(e2 and e2.code == Err.CORRUPT, "directory size: " .. tostring(e2)) + local long = Pak.new() + long:add(string.rep("n", 56), "x") + local _, e3 = long:write() + t:check(e3 and e3.code == Err.ARG, "a 56-byte name does not fit") + local _, e4 = Pak.encode({ kind = "image" }) + t:check(e4 and e4.code == Err.ARG, "encode a non-archive") +end + +local samples = {} +for _, c in Fx do + table.insert(samples, c.data) +end +t:fuzz("decode", function(b: buffer) + local a = Pak.decode(Bytes.reader(b)) + if a then + for _, e in a:list() do + a:read(e.name) + end + a:write() + end + return a +end, samples, { mutations = 40 }) + +t:done() diff --git a/tools/fixtures/pak.py b/tools/fixtures/pak.py new file mode 100644 index 0000000..dca7da8 --- /dev/null +++ b/tools/fixtures/pak.py @@ -0,0 +1,63 @@ +# Quake PAK fixtures (tools/fixtures.py): PAK files written here from the Quake source's dpackheader_t / dpackfile_t +# layout (header, file data in order, directory at the end: the layout id's qpak and most tools write), in the Quake +# ("PACK", 56-byte names) and SiN ("SPAK", 120-byte names) variants, plus one with the directory first and padding +# between files; with the expected entries read back by an independent Python reader here. +# tests/gen/pak.luau { { name, variant, data, entries = { { name, size, crc } }, files = { { name, data } } } } +import struct, zlib + +VARIANTS = {"quake": (b"PACK", 56), "sin": (b"SPAK", 120)} + + +def write(files, variant="quake", dir_first=False, pad=0): + magic, nl = VARIANTS[variant] + dirlen = len(files) * (nl + 8) + out = bytearray(magic + b"\0" * 8) + if dir_first: + dirofs = 12 + out += b"\0" * dirlen + offs = [] + for name, data in files: + out += b"\xee" * pad + offs.append(len(out)) + out += data + if not dir_first: + dirofs = len(out) + out += b"\0" * dirlen + for i, (name, data) in enumerate(files): + struct.pack_into("<%dsII" % nl, out, dirofs + i * (nl + 8), name.encode(), offs[i], len(data)) + struct.pack_into("