codecs/compress: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:57:58 -05:00
1 parent f1ad32105b
commit f626fb4295
23 files changed
+123 -498

No files matched your search

+4 -20
View File
@@ -1,20 +1,9 @@
--!strict
--!native
--!optimize 2
-- codecs/compress/Quantum: the Quantum decoder (David Stafford's LZ77 with an adaptive arithmetic coder) as Microsoft
-- Cabinet folders use it: windows of 2^10 .. 2^21 bytes; a 16-bit arithmetic decoder (H / L / C registers with
-- underflow handling) over a most-significant-bit-first byte stream; eight adaptive frequency models (the
-- 7-symbol selector, four 64-symbol literal models, the offset models for matches of 3, 4 and 5..259 bytes and the
-- 27-symbol length model) whose cumulative frequencies grow by 8 per symbol and are rescaled above 3800 (halved, and
-- every 50th time rebuilt from the frequencies and re-sorted); position and length footers read as raw bits from the
-- same stream. Each CFDATA block is one frame (at most 32768 output bytes) that restarts the arithmetic coder; the
-- models and the window persist across the frames of a folder. Written from the public descriptions of the format
-- (the Quantum notes by Matthew Russotto and the cabinet documentation); no code from libmspack.
--
-- local out, err = Quantum.decode(src, blocks, outSize, windowBits) -- buffer | nil, message
--
-- blocks = { { at, len, size } }: each block's compressed bytes src[at, at + len) and its output size; outSize is
-- their total. Never throws: bad or truncated data returns nil, message.
-- codecs/compress/Quantum: the Quantum decoder (David Stafford's LZ77 + adaptive arithmetic coder) for Cabinet folders.
-- Written from Matthew Russotto's Quantum notes and the cabinet documentation; no code from libmspack. Frequencies grow
-- by 8 and rescale above 3800 (halved; every 50th time rebuilt and re-sorted); each CFDATA frame restarts the coder.
local band, bor, bxor, lshift, rshift = bit32.band, bit32.bor, bit32.bxor, bit32.lshift, bit32.rshift
local readu8, writeu8 = buffer.readu8, buffer.writeu8
@@ -45,8 +34,7 @@ do
end
end
-- ------------------------------------------------------------------------------------------------ models
-- an adaptive model: symbols sym[1..n] in their current order, cumulative frequencies cum[1..n + 1] (cum[n + 1] = 0)
-- model: sym[1..n] in their current order, cumulative frequencies cum[1..n + 1] (cum[n + 1] = 0)
type Model = { n: number, sym: { number }, cum: { number }, shifts: number }
local function model(first: number, n: number): Model
@@ -92,8 +80,6 @@ local function rescale(m: Model)
end
end
-- ------------------------------------------------------------------------------------------------ decode
-- the coder and bit reader of one frame
type Coder = { src: buffer, pos: number, stop: number, acc: number, cnt: number, over: number, H: number, L: number, C: number, bad: boolean }
local function bit(c: Coder): number
@@ -112,7 +98,6 @@ local function bit(c: Coder): number
return band(rshift(c.acc, c.cnt), 1)
end
-- raw(c, n) -> the next n (0..19) bits, most significant first
local function raw(c: Coder, n: number): number
local v = 0
for _ = 1, n do
@@ -127,7 +112,6 @@ local function symbol(c: Coder, m: Model): number
local range = H - L + 1
local total = cum[1]
if range <= 0 or C < L or C > H then
-- only corrupt registers get here (the decoder keeps L <= C <= H)
c.bad = true
return 0
end