ws-arch2-cab: codecs/compress/Lzx (CAB LZX: windows 2^15..2^21, verbatim / aligned / uncompressed blocks, delta-coded trees, R0..R2, E8 translation, 32 KB frames; written from the spec) + cab LZX folders, checked against cabextract on cabinets of a fixture LZX compressor (tools/fixtures/_lzx.py) and libmspack's samples; Deflate.mszip (generic MSZIP blocks: CK + deflate, dictionary carried across blocks; for cab and later model/x); cab bench lines

This commit is contained in:
4DBug committed 2026-10-31 15:46:28 -05:00
1 parent 825c45b03c
commit c54c8daff5
9 files changed
+990 -49

No files matched your search

+36 -1
View File
@@ -6,7 +6,8 @@
-- local Deflate = require("@formats/codecs/compress/Deflate")
-- local out, err = Deflate.inflate(buf) -- raw deflate -> buffer, consumedEnd | nil, Err
-- local out, err = Deflate.inflate(buf, off, len, size) -- a range, with the exact output size if known
-- local out, err = Deflate.inflate(buf, off, len, size, dict) -- with a preset dictionary (MSZIP blocks)
-- local out, err = Deflate.inflate(buf, off, len, size, dict) -- with a preset dictionary
-- local out, err = Deflate.mszip(buf, { { offset, length, size }, ... }) -- MSZIP blocks ("CK" + deflate, chained)
-- local out, err = Deflate.zlib(buf) -- checks the Adler-32
-- local out, err = Deflate.gunzip(buf) -- checks CRC-32 + ISIZE, concatenates members
-- local z = Deflate.compress(buf, { level = 6 }) -- raw deflate; also zlibCompress / gzip
@@ -308,6 +309,40 @@ function Deflate.gzip(buf: buffer | string, opts: CompressOptions?): (buffer?, E
return out, nil
end
-- mszip(buf, blocks) -> buffer | nil, Err: MSZIP data (Microsoft's blocked deflate, as in CAB folders and DirectX .x
-- files): blocks = { { offset, length, size } }, each buf[offset, offset + length) = "CK" + a raw deflate stream that
-- inflates to exactly size bytes using the previous block's output (up to 32 KB) as its dictionary. Returns the
-- blocks' output concatenated.
function Deflate.mszip(buf: buffer, blocks: { { offset: number, length: number, size: number } }): (buffer?, Err.Err?)
local total = 0
for _, b in blocks do
if b.offset < 0 or b.length < 0 or b.offset + b.length > buffer.len(buf) or b.size < 0 then
return nil, Err.new(Err.ARG, "Deflate.mszip: block out of range")
end
total += b.size
end
local out = buffer.create(total)
local o = 0
for _, b in blocks do
if b.length < 2 or buffer.readu16(buf, b.offset) ~= 0x4B43 then
return nil, Err.new(Err.CORRUPT, "mszip: block without its CK signature", b.offset)
end
local dict: buffer? = nil
if o > 0 then
local d = math.min(o, 32768)
dict = buffer.create(d)
buffer.copy(dict :: buffer, 0, out, o - d, d)
end
local part, err = Deflate.inflate(buf, b.offset + 2, b.length - 2, b.size, dict)
if not part then
return nil, err
end
buffer.copy(out, o, part, 0, b.size)
o += b.size
end
return out, nil
end
-- crc32 / adler32 (kept from deflate-luau's API; the same as core/Crc32.of / core/Adler32.of)
Deflate.crc32 = function(buf: buffer | string, offset: number?, length: number?, crc: number?): number
local b = toBuffer(buf) or buffer.create(0)