116 lines
4.1 KiB
Luau
116 lines
4.1 KiB
Luau
--!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
|