ws-arch: codecs/Bzip2 (bzip2 decompressor: Huffman groups, MTF/RUNA/RUNB, inverse BWT, RLE1, block + stream CRCs, concatenated streams) + archive/bz2 (single-stream archive); tests vs Python bz2 (libbzip2) on both runtimes

This commit is contained in:
4DBug committed 2026-10-31 08:27:55 -05:00
1 parent 0ca32501a4
commit b7018b6bfb
8 files changed
+921

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
-- archive/bz2 tests: the contract, sniff, a .bz2 as a one-entry single-stream archive (entry named after the file),
-- the api path (F.load by sniff, .tar.bz2 unwrap is the api's: covered once tar exists), errors, the fuzz pass.
-- Fixtures: tools/fixtures/bzip2.py.
local T = require("@tests/T")
local Bz2 = require("@formats/archive/bz2")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local F = require("@formats")
local Fx = require("@tests/gen/bzip2")
local t = T.suite("bz2")
t:eq(Bz2.name, "bz2", "name")
t:eq(Bz2.category, "archive", "category")
t:check(Bz2.caps.decode and not Bz2.caps.encode, "caps")
for _, c in Fx.cases do
t:eq(Bz2.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
end
t:eq(Bz2.sniff(Bytes.reader(buffer.fromstring("BZh9hello"))), 0, "sniff: no block magic")
t:eq(Bz2.sniff(Bytes.reader(buffer.fromstring("\x1f\x8b\x08\x00"))), 0, "sniff: gzip")
do
local c = Fx.cases[1]
for _, x in Fx.cases do
if x.name == "text/l9" then
c = x
end
end
local a, err = Bz2.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "dir/notes.txt.bz2" })
t:check(a, "decode: " .. tostring(err))
t:check(a.kind == "archive" and a.format == "bz2" and a.meta.stream, "a single-stream archive")
local list = a:list()
t:eq(#list, 1, "one entry")
t:eq(list[1].name, "notes.txt", "entry named after the file")
t:eq(list[1].size, #Fx.corpora.text, "entry size")
t:eq(list[1].compressedSize, #c.data, "compressed size")
t:check(buffer.tostring((a:read("notes.txt"))) == Fx.corpora.text, "read")
local _, re = a:read("nope")
t:check(re and re.code == Err.ARG, "read of a missing entry")
local _, we = a:write()
t:check(we and we.code == Err.UNSUPPORTED, "read-only")
local a2 = Bz2.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "x.tbz2" })
t:eq(a2:list()[1].name, "x.tar", ".tbz2 -> .tar")
local a3 = Bz2.decode(Bytes.reader(buffer.fromstring(c.data)))
t:eq(a3:list()[1].name, "data", "no name -> data")
-- through the api
local v, ferr = F.load(c.data)
t:check(v and v.kind == "archive" and v.format == "bz2", "F.load sniffs bz2: " .. tostring(ferr))
t:eq(F.detect(c.data), "bz2", "F.detect")
local _, de = Bz2.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 100))))
t:check(de and de.code == Err.TRUNCATED, "truncated: " .. tostring(de))
end
local samples = {}
for i, c in Fx.cases do
if i % 6 == 0 and #c.data < 30000 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return Bz2.decode(Bytes.reader(b))
end, samples, { mutations = 6, garbage = 300 })
t:done()
+103
View File
@@ -0,0 +1,103 @@
-- codecs/Bzip2 tests: every fixture stream (Python bz2 = libbzip2, block sizes 1..9, RLE1 run edges, block-boundary
-- runs, random / skewed data, concatenated streams with trailing garbage) decodes exactly; offsets / consumedEnd;
-- maxBytes; CRC mismatches are Err.CHECKSUM; truncations are Err.TRUNCATED; the fuzz no-throw pass.
-- Fixtures: tools/fixtures/bzip2.py.
local T = require("@tests/T")
local Bzip2 = require("@formats/codecs/Bzip2")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/bzip2")
local t = T.suite("bzip2")
t:eq(Bzip2.crc(buffer.fromstring("123456789")), 0xFC891918, "CRC-32/BZIP2 check value")
local nbytes = 0
for _, c in Fx.cases do
local want = Fx.corpora[c.corpus]
local out, consumed = Bzip2.decompress(c.data)
t:check(out ~= nil, c.name .. ": " .. tostring(consumed))
t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs")
if c.name ~= "multi/trailing" then
t:eq(consumed, #c.data, c.name .. ": consumedEnd")
end
nbytes += #want
end
t:note("%d fixture streams, %.1f MB decoded", #Fx.cases, nbytes / 1e6)
local function case(name: string): any
for _, c in Fx.cases do
if c.name == name then
return c
end
end
error("no case " .. name)
end
-- inside a bigger buffer, at an offset
do
local c = case("text/l9")
local big = buffer.create(#c.data + 20)
buffer.fill(big, 0, 0xAB)
buffer.writestring(big, 7, c.data)
local out, consumed = Bzip2.decompress(big, 7, #c.data + 5)
t:check(out and buffer.tostring(out) == Fx.corpora.text, "at an offset")
t:eq(consumed, 7 + #c.data, "consumedEnd at an offset")
end
-- maxBytes
do
local c = case("zeros/l9")
local _, e = Bzip2.decompress(c.data, nil, nil, { maxBytes = 1000 })
t:check(e and e.code == Err.LIMIT, "maxBytes: " .. tostring(e))
local ok = Bzip2.decompress(c.data, nil, nil, { maxBytes = 250000 })
t:check(ok ~= nil, "maxBytes = exact size")
end
-- block CRC / stream CRC mismatches
do
local c = case("text/l9")
local b = buffer.fromstring(c.data)
-- block CRC sits at bytes 10..13 (after "BZh9" + 6 magic bytes)
buffer.writeu8(b, 11, bit32.bxor(buffer.readu8(b, 11), 0x10))
local _, e = Bzip2.decompress(b)
t:check(e and e.code == Err.CHECKSUM, "block CRC mismatch: " .. tostring(e))
local s = case("one/l9")
local b2 = buffer.fromstring(s.data)
local n = buffer.len(b2)
buffer.writeu8(b2, n - 2, bit32.bxor(buffer.readu8(b2, n - 2), 0x01))
local _, e2 = Bzip2.decompress(b2)
t:check(e2 and (e2.code == Err.CHECKSUM or e2.code == Err.CORRUPT), "stream CRC mismatch: " .. tostring(e2))
end
-- truncation -> TRUNCATED, bad magic -> CORRUPT, bad args -> ARG
do
local c = case("text/l1")
for _, cut in { 3, 10, 40, #c.data // 2, #c.data - 3 } do
local _, e = Bzip2.decompress(string.sub(c.data, 1, cut))
t:check(e and e.code == Err.TRUNCATED, "truncated at " .. cut .. ": " .. tostring(e))
end
local _, e = Bzip2.decompress("BZx9abcdefgh")
t:check(e and e.code == Err.CORRUPT, "bad magic")
local _, e2 = Bzip2.decompress(5 :: any)
t:check(e2 and e2.code == Err.ARG, "non-buffer")
local _, e3 = Bzip2.decompress("BZh9", -1)
t:check(e3 and e3.code == Err.ARG, "bad offset")
end
-- fuzz
do
local samples = {}
for i, c in Fx.cases do
if i % 4 == 0 and #c.data < 40000 then
table.insert(samples, c.data)
end
end
t:fuzz("decompress", function(b: buffer)
return Bzip2.decompress(b)
end, samples, { mutations = 12, garbage = 1000, keepHeader = 4 })
t:fuzz("decompress (no header kept)", function(b: buffer)
return Bzip2.decompress(b)
end, samples, { mutations = 4, garbage = 200 })
end
t:done()