Files
rosetta/tests/containers/chunks.luau
T
4DBug 51b08575c4 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.
2026-10-31 12:25:03 -05:00

77 lines
3.4 KiB
Luau

-- containers/Chunks tests: RIFF (padding, an odd chunk without its pad byte, open-ended data), IFF big-endian, PNG
-- (size first + CRC trailer, truncation), CAF 64-bit sizes, resize hooks, find, and a no-throw pass over garbage.
local T = require("@tests/T")
local Chunks = require("@formats/containers/Chunks")
local t = T.suite("chunks")
local function le32(n: number): string
return string.char(n % 256, (n // 256) % 256, (n // 65536) % 256, (n // 16777216) % 256)
end
local function be32(n: number): string
return string.char((n // 16777216) % 256, (n // 65536) % 256, (n // 256) % 256, n % 256)
end
-- RIFF: an odd chunk with its pad byte, then one without
do
local s = "abcd" .. le32(3) .. "xyz\0" .. "efgh" .. le32(1) .. "Q" .. "ijkl" .. le32(2) .. "12"
local list = Chunks.walk(buffer.fromstring(s), 0, #s, Chunks.RIFF)
t:eq(#list, 3, "riff: three chunks")
t:check(list[1].id == "abcd" and list[1].offset == 8 and list[1].size == 3, "riff: first")
t:check(list[2].id == "efgh" and list[2].size == 1, "riff: an odd chunk")
t:check(list[3].id == "ijkl" and list[3].offset == 29 and list[3].avail == 2, "riff: missing pad byte detected")
t:eq((Chunks.find(list, "efgh") :: any).start, 12, "find")
-- open-ended data with size 0, and a declared size past the end
local o = "fmt " .. le32(2) .. "ab" .. "data" .. le32(0) .. "123456"
local l2 = Chunks.walk(buffer.fromstring(o), 0, #o, { pad = 2, openEnded = { data = true } })
t:check(l2[2].streamed and l2[2].size == 6, "open-ended data")
local tr = "data" .. le32(100) .. "abc"
local l3 = Chunks.walk(buffer.fromstring(tr), 0, #tr, Chunks.RIFF)
t:check(l3[1].truncated and l3[1].avail == 3 and l3[1].size == 100, "truncated chunk keeps what is there")
local l4 = Chunks.walk(buffer.fromstring(o), 0, #o, { pad = 2, resize = function(id: string, n: number)
return if id == "fmt " then 1 else nil
end })
t:eq(l4[1].size, 1, "resize hook")
end
-- IFF (big-endian sizes)
do
local s = "COMM" .. be32(2) .. "ab" .. "SSND" .. be32(3) .. "xyz\0"
local list = Chunks.walk(buffer.fromstring(s), 0, #s, Chunks.IFF)
t:check(#list == 2 and list[2].id == "SSND" and list[2].size == 3, "iff")
end
-- PNG: size, type, data, CRC
do
local s = be32(2) .. "IHDR" .. "ab" .. "CRC!" .. be32(0) .. "IEND" .. "CRC?"
local list = Chunks.walk(buffer.fromstring(s), 0, #s, Chunks.PNG)
t:check(#list == 2 and list[1].id == "IHDR" and list[1].offset == 8 and list[2].id == "IEND" and list[2].start == 14, "png")
local cut = string.sub(s, 1, 12)
local l2 = Chunks.walk(buffer.fromstring(cut), 0, #cut, Chunks.PNG)
t:check(l2[1].truncated, "png: a missing CRC is a truncation")
end
-- CAF: 64-bit big-endian sizes
do
local s = "desc" .. be32(0) .. be32(4) .. "abcd" .. "data" .. be32(0) .. be32(2) .. "xy"
local list = Chunks.walk(buffer.fromstring(s), 0, #s, Chunks.CAF)
t:check(#list == 2 and list[2].offset == 28 and list[2].size == 2, "caf")
end
-- an offset range inside a bigger buffer
do
local s = "junk" .. "abcd" .. le32(1) .. "x\0" .. "tail"
local list = Chunks.walk(buffer.fromstring(s), 4, 14, Chunks.RIFF)
t:check(#list == 1 and list[1].offset == 12, "a sub-range")
end
local samples = { "abcd" .. le32(3) .. "xyz\0efgh" .. le32(1) .. "Q", be32(2) .. "IHDRabCRC!" }
t:fuzz("walk", function(b: buffer)
local n = 0
for _, layout in { Chunks.RIFF, Chunks.IFF, Chunks.PNG, Chunks.CAF } do
n += #Chunks.walk(b, 0, buffer.len(b), layout)
end
return n
end, samples)
t:done()