From eac069f7dc7999fbe340711ce954f23ab1e5aaf3 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 13:56:30 -0500 Subject: [PATCH] ws-game: archive/grp (Build engine GRP read + write) --- docs/FORMATS.md | 1 + src/Formats/Registry.luau | 12 ++++ src/Formats/archive/grp/init.luau | 115 ++++++++++++++++++++++++++++++ tests/archive/grp.luau | 64 +++++++++++++++++ tools/fixtures/grp.py | 35 +++++++++ 5 files changed, 227 insertions(+) create mode 100644 src/Formats/archive/grp/init.luau create mode 100644 tests/archive/grp.luau create mode 100644 tools/fixtures/grp.py diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 91a5232..ff0f55e 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -52,6 +52,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | archive | 7z | .7z | yes | - | methods: "copy", "lzma", "lzma2", "deflate", "bcj", "delta", encrypted: false | `tests/archive/7z.luau` | | archive | br | .br | yes | - | stream: true | `tests/archive/br.luau` | | archive | bz2 | .bz2 .tbz2 .tbz | yes | - | stream: true | `tests/archive/bz2.luau` | +| archive | grp | .grp | yes | yes | | `tests/archive/grp.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` | diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index 5000567..156b1d9 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -578,6 +578,18 @@ local list: { Entry } = { return require("@formats/archive/bz2") end, }, + { + name = "grp", + category = "archive", + path = "archive/grp", + extensions = { "grp" }, + mime = { "application/x-build-grp" }, + deps = { "core/Err", "core/Bytes", "core/Sniff", "containers/Archive" }, + caps = { decode = true, encode = true }, + load = function() + return require("@formats/archive/grp") + end, + }, { name = "gzip", category = "archive", diff --git a/src/Formats/archive/grp/init.luau b/src/Formats/archive/grp/init.luau new file mode 100644 index 0000000..a7ad03e --- /dev/null +++ b/src/Formats/archive/grp/init.luau @@ -0,0 +1,115 @@ +--!strict +-- archive/grp: Build engine GRP archives (Duke Nukem 3D, Shadow Warrior, Blood: "KenSilverman", a u32 file count, +-- 16-byte entries of a 12-byte name and a u32 size, then the files back to back), read into the standard Archive +-- (entries read lazily) and written from any Archive value (names upper-cased to 8.3 by the caller's choice: they +-- are stored as given, at most 12 bytes). Written from Ken Silverman's GRP description (kenbuild); no code taken. +-- Not supported: the "GRP 2" / RFF / SSI variants. + +-- ------------------------------------------------------------------------------------------------ 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 Grp = { + name = "grp", + category = "archive", + extensions = { "grp" }, + mime = { "application/x-build-grp" }, + deps = { "core/Err", "core/Bytes", "core/Sniff", "containers/Archive" }, + caps = { decode = true, encode = true }, +} + +-- ------------------------------------------------------------------------------------------------ types +export type DecodeOptions = { maxEntries: number? } -- default 1e6 +export type EncodeOptions = { upper: boolean? } -- upper-case the names (Build looks them up case-insensitively; default false) + +-- ------------------------------------------------------------------------------------------------ constants +local MAGIC = "KenSilverman" + +-- ------------------------------------------------------------------------------------------------ sniff +function Grp.sniff(r: Bytes.Reader): number + return Sniff.magic(r, MAGIC) +end + +-- ------------------------------------------------------------------------------------------------ decode +local function encode(archive: any, opts: any?): (buffer?, Err.Err?) + return Grp.encode(archive, opts) +end + +function Grp.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?) + if not r:match(MAGIC) then + return nil, Err.new(Err.CORRUPT, "bad magic", 0) + end + local count = r:u32le() + if r.err then + return nil, r.err + end + if count > (opts and opts.maxEntries or 1e6) then + return nil, Err.new(Err.LIMIT, "too many entries: " .. count) + end + if count * 16 > r:remaining() then + return nil, Err.new(Err.TRUNCATED, "directory past the end", 12) + end + local a = Archive.new("grp", encode) + local at = 16 + count * 16 + local size = r.limit - r.base + for i = 0, count - 1 do + local name = r:fixed(12) + local n = r:u32le() + if at + n > size then + return nil, Err.new(Err.TRUNCATED, "entry " .. name .. " past the end", 16 + i * 16) + end + Archive.put(a, { name = name, size = n, dir = false, method = "store", offset = at, load = Archive.slice(r.buf, r.base + at, n) }) + at += n + end + if r.err then + return nil, r.err + end + a.meta.source = { format = "grp", entries = count } + return a, nil +end + +-- ------------------------------------------------------------------------------------------------ encode +function Grp.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?) + local items, err = Archive.collect(archive) + if not items then + return nil, err + end + local files: { { name: string, data: buffer } } = {} + for _, it in items do + if not it.entry.dir then + local name = it.entry.name + if opts and opts.upper then + name = string.upper(name) + end + if #name > 12 then + return nil, Err.new(Err.ARG, "name longer than 12 bytes: " .. name) + end + table.insert(files, { name = name, data = it.data }) + end + end + local w = Bytes.writer() + w:string(MAGIC) + w:u32le(#files) + for _, f in files do + w:string(f.name) + w:fill(0, 12 - #f.name) + w:u32le(buffer.len(f.data)) + end + for _, f in files do + w:bytes(f.data) + end + return w:finish(), nil +end + +-- ------------------------------------------------------------------------------------------------ extras +-- new() -> an empty GRP Archive: a:add(name, data); a:write() +function Grp.new(): any + local a = Archive.new("grp", encode) + a.meta.source = { format = "grp" } + return a +end + +return Grp diff --git a/tests/archive/grp.luau b/tests/archive/grp.luau new file mode 100644 index 0000000..0a11a97 --- /dev/null +++ b/tests/archive/grp.luau @@ -0,0 +1,64 @@ +-- archive/grp tests: Build GRP archives written by tools/fixtures/grp.py list the entries an independent Python +-- reader sees (names, sizes, data CRCs); our writer produces the same bytes as the Python writer and round-trips; +-- conversion from a zip; errors; the fuzz pass. Fixtures: tools/fixtures/grp.py. +local T = require("@tests/T") +local F = require("@formats") +local Grp = require("@formats/archive/grp") +local Bytes = require("@formats/core/Bytes") +local Crc32 = require("@formats/core/Crc32") +local Err = require("@formats/core/Err") +local Fx = require("@tests/gen/grp") + +local t = T.suite("grp") +t:eq(Grp.name, "grp", "name") + +for _, c in Fx do + local a, err = Grp.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(F.detect(c.data), "grp", c.name .. ": F.detect") + local n = Grp.new() + for _, f in c.files do + n:add(f.name, f.data) + end + t:check(buffer.tostring(n:write() :: buffer) == c.data, c.name .. ": bytes identical to the Python writer") + t:check(buffer.tostring(F.save(a) :: buffer) == c.data, c.name .. ": decode + F.save is lossless") +end + +do + local zip = F.format("zip").create() + zip:add("readme.txt", "hi") + local g = F.load(F.save(zip, nil, { format = "grp", upper = true }) :: buffer) + t:check(g.format == "grp" and g:list()[1].name == "README.TXT", "zip -> grp, upper-cased") + local c = Fx[1] + local _, e1 = Grp.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 1)))) + t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1)) + local _, e2 = Grp.decode(Bytes.reader(buffer.fromstring("KenSilverman\255\255\255\255"))) + t:check(e2 and e2.code == Err.LIMIT, "count limit: " .. tostring(e2)) + local long = Grp.new() + long:add("THIRTEEN.CHAR", "x") + local _, e3 = long:write() + t:check(e3 and e3.code == Err.ARG, "a 13-byte name does not fit") +end + +local samples = {} +for _, c in Fx do + table.insert(samples, c.data) +end +t:fuzz("decode", function(b: buffer) + local a = Grp.decode(Bytes.reader(b)) + if a then + for _, e in a:list() do + a:read(e.name) + end + end + return a +end, samples, { mutations = 40 }) + +t:done() diff --git a/tools/fixtures/grp.py b/tools/fixtures/grp.py new file mode 100644 index 0000000..fab40f8 --- /dev/null +++ b/tools/fixtures/grp.py @@ -0,0 +1,35 @@ +# Build GRP fixtures (tools/fixtures.py): GRP files written here from Ken Silverman's layout ("KenSilverman", count, +# 12-byte names + u32 sizes, files back to back), with the entries an independent Python reader sees. +# tests/gen/grp.luau { { name, data, entries = { { name, size, crc } }, files = { { name, data } } } } +import struct, zlib + + +def write(files): + out = bytearray(b"KenSilverman" + struct.pack("