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]>
116 lines
4.7 KiB
Luau
116 lines
4.7 KiB
Luau
-- codecs/compress/Zstd tests: every fixture case (the zstd CLI at levels 1..19, --ultra -22, --fast, --long, small windows,
|
|
-- with / without checksum and content size, multi-frame and skippable frames, empty input) decodes to its corpus;
|
|
-- offsets / consumedEnd; XXH64 vectors; checksum, limit and error codes; truncated / corrupted / random input never
|
|
-- throws. Fixtures: tools/fixtures/zstd.py.
|
|
local T = require("@tests/T")
|
|
local Zstd = require("@formats/codecs/compress/Zstd")
|
|
local Err = require("@formats/core/Err")
|
|
local Fx = require("@tests/gen/zstd")
|
|
|
|
local t = T.suite("zstd")
|
|
|
|
-- XXH64 reference values (xxhsum -H64)
|
|
do
|
|
local function hex(s: string): string
|
|
local hi, lo = Zstd.xxh64(buffer.fromstring(s))
|
|
return string.format("%08x%08x", hi, lo)
|
|
end
|
|
t:eq(hex(""), "ef46db3751d8e999", "xxh64 empty")
|
|
t:eq(hex("a"), "d24ec4f1a98c6e5b", "xxh64 a")
|
|
t:eq(hex("abc"), "44bc2cf5ad770999", "xxh64 abc")
|
|
t:eq(hex("Nobody inspects the spammish repetition"), "fbcea83c8a378bf1", "xxh64 sentence")
|
|
t:eq(hex(string.rep("0123456789", 10)), "f80e7b96315afffa", "xxh64 100 bytes")
|
|
end
|
|
|
|
-- every case decodes to its corpus
|
|
local nbytes = 0
|
|
for _, c in Fx.cases do
|
|
local want = Fx.corpora[c.corpus]
|
|
local b = buffer.fromstring(c.data)
|
|
local out, consumed = Zstd.decompress(b)
|
|
t:check(out ~= nil, c.name .. ": " .. tostring(consumed))
|
|
t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs")
|
|
t:eq(consumed, #c.data, c.name .. ": consumed")
|
|
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
|
|
|
|
-- at an offset inside a bigger buffer, and string input
|
|
do
|
|
local c = case("text/l3")
|
|
local big = buffer.create(#c.data + 20)
|
|
buffer.fill(big, 0, 0xAB)
|
|
buffer.writestring(big, 7, c.data)
|
|
local out, consumed = Zstd.decompress(big, 7, #c.data)
|
|
t:check(out and buffer.tostring(out) == Fx.corpora.text, "at an offset")
|
|
t:eq(consumed, 7 + #c.data, "consumed at an offset")
|
|
t:check(buffer.tostring((Zstd.decompress(c.data))) == Fx.corpora.text, "string input")
|
|
t:check(Zstd.isFrame(buffer.fromstring(c.data)), "isFrame")
|
|
t:check(not Zstd.isFrame(buffer.fromstring("abcd")), "isFrame on other data")
|
|
-- trailing garbage is an error, as zstd -d says
|
|
local _, e = Zstd.decompress(c.data .. "junk")
|
|
t:check(e and e.code == Err.CORRUPT, "trailing garbage: " .. tostring(e))
|
|
end
|
|
|
|
-- checksum mismatch (the last 4 bytes), and verify = false skips it
|
|
do
|
|
local c = case("text/l9")
|
|
local b = buffer.fromstring(c.data)
|
|
local n = buffer.len(b)
|
|
buffer.writeu8(b, n - 1, bit32.bxor(buffer.readu8(b, n - 1), 0x10))
|
|
local out, e = Zstd.decompress(b)
|
|
t:check(out == nil and Err.is(e) and e.code == Err.CHECKSUM, "checksum mismatch: " .. tostring(e))
|
|
local out2 = Zstd.decompress(b, nil, nil, { verify = false })
|
|
t:check(out2 and buffer.tostring(out2) == Fx.corpora.text, "verify = false")
|
|
end
|
|
|
|
-- limits and arguments
|
|
do
|
|
local c = case("text/l3")
|
|
local _, e = Zstd.decompress(c.data, nil, nil, { maxBytes = 1000 })
|
|
t:check(e and e.code == Err.LIMIT, "maxBytes with a content size: " .. tostring(e))
|
|
local s = case("text/stdin")
|
|
local _, e2 = Zstd.decompress(s.data, nil, nil, { maxBytes = 1000 })
|
|
t:check(e2 and e2.code == Err.LIMIT, "maxBytes without a content size: " .. tostring(e2))
|
|
local ok, r1, r2 = pcall(Zstd.decompress, 5 :: any)
|
|
t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG")
|
|
local _, e3 = Zstd.decompress("")
|
|
t:check(e3 and e3.code == Err.TRUNCATED, "empty input is TRUNCATED: " .. tostring(e3))
|
|
local _, e4 = Zstd.decompress("\40\181\47\253\0")
|
|
t:check(e4 and e4.code == Err.TRUNCATED, "header only: " .. tostring(e4))
|
|
-- a dictionary ID: FHD with Dictionary_ID_flag = 1, single segment, then a 1-byte dictionary id and 1-byte size
|
|
local _, e5 = Zstd.decompress("\40\181\47\253\33\7\0\1\0\0")
|
|
t:check(e5 and e5.code == Err.UNSUPPORTED, "dictionary: " .. tostring(e5))
|
|
-- reserved block type
|
|
local _, e6 = Zstd.decompress("\40\181\47\253\32\0\7\0\0")
|
|
t:check(e6 and e6.code == Err.CORRUPT, "reserved block type: " .. tostring(e6))
|
|
end
|
|
|
|
-- truncation, corruption and garbage never throw
|
|
do
|
|
local samples = {}
|
|
for i, c in Fx.cases do
|
|
if i % 5 == 0 and #c.data > 8 and #c.data < 40000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("decompress", function(b: buffer)
|
|
return Zstd.decompress(b)
|
|
end, samples, { mutations = 12, garbage = 2000 })
|
|
-- keep the frame header intact so the mutations reach the blocks
|
|
t:fuzz("decompress (blocks)", function(b: buffer)
|
|
return Zstd.decompress(b, nil, nil, { verify = false, maxBytes = 1e6 })
|
|
end, samples, { mutations = 40, garbage = 0, keepHeader = 12, seed = 99 })
|
|
end
|
|
|
|
t:done()
|