ws-game: archive/mpq (MPQ v1-4 read: hash / block tables, encryption, listfile names, zlib / implode / bzip2 / LZMA / sparse sectors; Huffman / ADPCM UNSUPPORTED); codecs/compress/Explode (PKWARE DCL implode decompressor, binary + ASCII, tested against StormLib); licenses: blast (format reference)

This commit is contained in:
4DBug committed 2026-10-31 14:21:34 -05:00
1 parent 34d0600109
commit b02e59809f
8 files changed
+1063

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
-- archive/mpq tests: archives written by StormLib (tools/fixtures/mpq.py; header versions 1-4) list every file and
-- read back its exact bytes: zlib, PKWARE implode, bzip2, LZMA and sparse sectors, imploded files, single-unit,
-- stored, encrypted (plain and fix-key) and sector-CRC files; a prefixed archive; an archive without a listfile
-- (names by block index, opts.names); Huffman sectors are UNSUPPORTED; the hash helper; errors; the fuzz pass.
local T = require("@tests/T")
local F = require("@formats")
local Mpq = require("@formats/archive/mpq")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/mpq")
local t = T.suite("mpq")
t:eq(Mpq.name, "mpq", "name")
-- the classic test vector: the hash table key
t:eq(Mpq.hash("(hash table)", 3), 0xC3AF3770, "hash of (hash table)")
t:eq(Mpq.hash("(block table)", 3), 0xEC83B3A3, "hash of (block table)")
for _, c in Fx.archives do
local a, err = Mpq.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(a, c.name .. ": " .. tostring(err))
t:eq(F.detect(c.data), "mpq", c.name .. ": F.detect")
local by = {}
for _, e in a:list() do
by[e.name] = e
end
local ok = 0
for _, w in c.files do
if c.unnamed then
continue
end
local e = by[w.name]
t:check(e, c.name .. ": has " .. w.name)
if w.size > 0 then
local b, rerr = a:read(w.name)
if w.huffman then
t:check(rerr and rerr.code == Err.UNSUPPORTED, c.name .. ": " .. w.name .. ": Huffman is unsupported: " .. tostring(rerr))
else
t:check(b, c.name .. ": read " .. w.name .. ": " .. tostring(rerr))
t:eq(e.size, w.size, c.name .. ": size " .. w.name)
t:eq(Crc32.of(b), w.crc, c.name .. ": data " .. w.name)
ok += 1
end
end
end
if c.unnamed then
-- no listfile: unnamed entries; unencrypted ones read by block index, names through opts.names
local n = 0
for _, e in a:list() do
if string.match(e.name, "^File%d+%.xxx$") then
n += 1
local b, rerr = a:read(e.name)
if (e :: any).encrypted then
t:check(rerr and rerr.code == Err.UNSUPPORTED, c.name .. ": encrypted without a name")
else
t:check(b, c.name .. ": read " .. e.name .. ": " .. tostring(rerr))
end
end
end
t:check(n > 0, c.name .. ": entries named by block index")
local names = {}
for _, w in c.files do
table.insert(names, w.name)
end
local b = Mpq.decode(Bytes.reader(buffer.fromstring(c.data)), { names = names }) :: any
for _, w in c.files do
if w.size > 0 then
t:eq(Crc32.of((b:read(w.name))), w.crc, c.name .. ": opts.names " .. w.name)
end
end
end
t:note("%-9s v%d, %2d entries, %2d files checked", c.name, a.meta.source.version, #a:list(), ok)
end
-- errors
do
local c = Fx.archives[1]
local _, e1 = Mpq.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 200))))
t:check(e1 and (e1.code == Err.TRUNCATED or e1.code == Err.CORRUPT), "truncated: " .. tostring(e1))
local _, e2 = Mpq.decode(Bytes.reader(buffer.fromstring(string.rep("x", 64))))
t:check(e2 and e2.code == Err.CORRUPT, "no header")
local a = Mpq.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
local _, e3 = a:write()
t:check(e3 and e3.code == Err.UNSUPPORTED, "read-only")
end
local samples = {}
for _, c in Fx.archives do
if c.name == "v1" or c.name == "nolist" or c.name == "v4" then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
local a = Mpq.decode(Bytes.reader(b))
if a then
for _, e in a:list() do
a:read(e.name)
end
end
return a
end, samples, { mutations = 60 })
do
-- mutations inside the file data (the tables intact)
local c = Fx.archives[1]
local rand = T.rng(5)
local threw = 0
for _ = 1, 80 do
local b = buffer.fromstring(c.data)
for _ = 1, 1 + rand(8) do
buffer.writeu8(b, 32 + rand(#c.data // 2), rand(256))
end
local ok = pcall(function()
local a = Mpq.decode(Bytes.reader(b))
if a then
for _, e in a:list() do
a:read(e.name)
end
end
end)
if not ok then
threw += 1
end
end
t:eq(threw, 0, "mutated file data never throws")
end
t:done()
+33
View File
@@ -0,0 +1,33 @@
-- codecs/compress/Explode tests: PKWARE DCL streams written by StormLib's SCompImplode (tools/fixtures/mpq.py:
-- text, noise, a short repeat, zeros) decompress to the source bytes; maxOut stops early; hand-made streams with
-- coded literals; bad headers and truncations; the fuzz pass.
local T = require("@tests/T")
local Explode = require("@formats/codecs/compress/Explode")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/mpq")
local t = T.suite("codecs/explode")
local samples = {}
for _, c in Fx.implode do
local out, err = Explode.decompress(c.data)
t:check(out, c.name .. ": " .. tostring(err))
t:eq(buffer.len(out), c.size, c.name .. ": size")
t:eq(Crc32.of(out), c.crc, c.name .. ": data")
t:note("%-6s %6d -> %6d bytes (header %d %d)", c.name, #c.data, c.size, string.byte(c.data, 1), string.byte(c.data, 2))
local part = Explode.decompress(c.data, nil, nil, 7)
t:eq(buffer.len(part), math.min(7, c.size), c.name .. ": maxOut")
table.insert(samples, c.data)
end
-- a stream with coded literals: header 1 (Huffman literals), 4 (1 KB); then only the end code would need the tables:
-- check the header checks instead
local _, e1 = Explode.decompress("\2\4\0")
t:check(e1 and e1.code == Err.CORRUPT, "literal mode 2")
local _, e2 = Explode.decompress("\0\7\0")
t:check(e2 and e2.code == Err.CORRUPT, "dictionary 7")
local _, e3 = Explode.decompress(string.sub(Fx.implode[1].data, 1, 20))
t:check(e3 and e3.code == Err.TRUNCATED, "truncated")
t:fuzz("decompress", function(b: buffer)
return Explode.decompress(b)
end, samples, { mutations = 40 })
t:done()