Files
rosetta/tests/codecs/bzip2.luau
T
4DBugandClaude Opus 5.5 434a304315 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 12:25:03 -05:00

104 lines
3.5 KiB
Luau

-- codecs/compress/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/compress/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()