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

@@ -1,11 +1,8 @@
--!strict
--!native
--!optimize 2
-- codecs/compress/Deflate/Compress: a DEFLATE (RFC 1951) compressor. LZ77 over a 32 KB window with hash chains (chain depth
-- and lazy matching by level), then per block the cheapest of stored / fixed-Huffman / dynamic-Huffman (codecs/entropy/Huffman
-- length-limited codes). Level 0 = stored blocks only.
--
-- raw(buf, off, len, level) -> buffer. Never throws (the caller validated the arguments).
-- codecs/compress/Deflate/Compress: a DEFLATE (RFC 1951) compressor: hash-chain LZ77 over a 32 KB window, then per
-- block the cheapest of stored / fixed / dynamic Huffman, as zlib's levels.
local Huffman = require("@rosetta/codecs/entropy/Huffman")
local readu8, writeu8, writeu16 = buffer.readu8, buffer.writeu8, buffer.writeu16
@@ -15,11 +12,10 @@ local floor = math.floor
local WSIZE = 32768
local HSIZE = 65536
local MAXMATCH = 258
local BLOCKSYMS = 16384 -- symbols per block
local BLOCKSYMS = 16384
-- per level (zlib's configuration_table): GOOD = a match this long quarters the chain for the lazy search, LAZY = no
-- lazy search after a match this long, NICE = stop searching at this length, CHAIN = max chain steps. Levels 1-3 are
-- greedy (no lazy matching).
-- zlib's configuration_table: GOOD quarters the chain for the lazy search, LAZY = no lazy search after a match this
-- long, NICE = stop searching at this length, CHAIN = max chain steps; levels 1-3 are greedy.
local GOOD = { 4, 4, 4, 4, 8, 8, 8, 32, 32 }
local LAZY = { 0, 0, 0, 4, 16, 16, 32, 128, 258 }
local NICE = { 8, 16, 32, 16, 32, 128, 128, 258, 258 }
@@ -66,14 +62,12 @@ end
local CLORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }
-- fixed codes
local FIXLEN = table.create(288, 0)
for i = 1, 288 do
FIXLEN[i] = if i <= 144 then 8 elseif i <= 256 then 9 elseif i <= 280 then 7 else 8
end
local FIXDLEN = table.create(30, 5)
-- reversed (LSB-first) canonical codes for lengths
local function revCodes(lens: { number }): { number }
local codes = Huffman.codes(lens)
for i, l in lens do
@@ -91,7 +85,6 @@ for i = 0, 32 do
POW[i + 1] = 2 ^ i
end
-- ------------------------------------------------------------------------------------------------ bit output
type Out = { buf: buffer, op: number, acc: number, cnt: number }
local function ensure(o: Out, n: number)
@@ -102,7 +95,6 @@ local function ensure(o: Out, n: number)
end
end
-- put(v, n): n <= 24 bits, LSB first
local function put(o: Out, v: number, n: number)
local acc = o.acc + v * POW[o.cnt + 1]
local cnt = o.cnt + n
@@ -127,8 +119,6 @@ local function alignOut(o: Out)
end
end
-- ------------------------------------------------------------------------------------------------ blocks
-- syms: L[i] = literal 0..255 or 256 + match length; D[i] = distance (0 for literals)
local function storedBlock(o: Out, src: buffer, s: number, e: number, final: boolean)
local p = s
repeat
@@ -209,7 +199,7 @@ local function block(o: Out, src: buffer, s: number, e: number, L: { number }, D
for i = 1, hdist do
all[hlit + i] = dlen[i]
end
local cs, cx = {}, {} -- symbol, extra value
local cs, cx = {}, {}
local cf = table.create(19, 0)
local i = 1
local total = hlit + hdist
@@ -252,7 +242,6 @@ local function block(o: Out, src: buffer, s: number, e: number, L: { number }, D
while hclen > 4 and clen[CLORDER[hclen] + 1] == 0 do
hclen -= 1
end
-- costs in bits
local dyn = 17 + 3 * hclen + extra
for k = 1, 19 do
dyn += cf[k] * clen[k]
@@ -299,7 +288,6 @@ local function block(o: Out, src: buffer, s: number, e: number, L: { number }, D
emitSymbols(o, L, D, nsym, revCodes(llen), llen, revCodes(dlen), dlen)
end
-- ------------------------------------------------------------------------------------------------ LZ77
local Compress = {}
function Compress.raw(src: buffer, off: number, len: number, level: number): buffer
@@ -336,7 +324,6 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
prev[p % WSIZE + 1] = head[h]
head[h] = p
end
-- longest match at p among earlier positions: len (0 if < 3), dist
local function find(p: number, steps: number): (number, number)
local maxLen = math.min(MAXMATCH, e - p)
if maxLen < 3 then
@@ -360,7 +347,7 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
end
local nc = prev[c % WSIZE + 1]
if nc >= c then
break -- the slot was reused by a newer position: the chain ends here
break
end
c = nc
steps -= 1
@@ -377,7 +364,7 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
end
local p = off
local stop3 = e - 3 -- positions with 3 bytes ahead can be hashed
local stop3 = e - 3
while p < e do
local mlen, mdist = 0, 0
if p <= stop3 then
@@ -386,7 +373,6 @@ function Compress.raw(src: buffer, off: number, len: number, level: number): buf
if mlen >= 3 and mlen < lazyMax and p + 1 <= stop3 then
local l2, d2 = find(p + 1, if mlen >= good then maxChain // 4 else maxChain)
if l2 > mlen then
-- a literal now, the longer match from p + 1
nsym += 1
L[nsym] = readu8(src, p)
D[nsym] = 0