codecs/compress: comments to specs only
This commit is contained in:
1 parent
f1ad32105b
commit
f626fb4295
23 files changed
+123
-498
No files matched your search
@@ -1,16 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/compress/Zstd/Decode: Zstandard frame decoder (RFC 8878), written from the specification (no code copied from the
|
||||
-- BSD reference implementation). Frames (any number, concatenated), skippable frames, raw / RLE / compressed blocks,
|
||||
-- Huffman literals (1 or 4 streams, new or repeated table), FSE sequence tables (predefined / RLE / compressed /
|
||||
-- repeat), repeat offsets, the XXH64 content checksum. Not supported: dictionaries (a non-zero dictionary ID).
|
||||
--
|
||||
-- The whole output lives in one buffer, so the window is the output itself (offsets are checked against the frame's
|
||||
-- start). Backward bitstreams are read through bit offsets: bits [o - n, o) of a stream come from one readu32 at
|
||||
-- byte (o - n) / 8, bits before the stream start read as zeros.
|
||||
--
|
||||
-- Nothing here throws on any input: every read is bounds-checked; failures return nil, code, message, offset.
|
||||
-- codecs/compress/Zstd/Decode: Zstandard frame decoder (RFC 8878), written from the specification (no reference code).
|
||||
-- Backward bitstreams: bits [o - n, o) come from one readu32 at byte (o - n) / 8; bits before the stream read as zeros.
|
||||
local Xxh64 = require("@rosetta/codecs/compress/Zstd/Xxh64")
|
||||
|
||||
local band, lshift, rshift = bit32.band, bit32.lshift, bit32.rshift
|
||||
@@ -22,7 +14,6 @@ local BLOCK_MAX = 131072
|
||||
|
||||
local Decode = {}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ tables
|
||||
-- literal length / match length codes: baseline, extra bits (index = code + 1)
|
||||
local LL_BASE = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24, 28, 32, 40, 48, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 }
|
||||
local LL_BITS = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
|
||||
@@ -41,7 +32,6 @@ local ML_DEFAULT = {
|
||||
}
|
||||
local OF_DEFAULT = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1 }
|
||||
|
||||
-- highest set bit of 1..2048
|
||||
local HB = table.create(2048, 0)
|
||||
do
|
||||
local v = 0
|
||||
@@ -53,8 +43,6 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ bit readers
|
||||
-- backward stream starting at byte s: bits [o - n, o) (n <= 25), bits below 0 are zeros
|
||||
local function peek(b: buffer, s: number, o: number, n: number): number
|
||||
if n <= 0 then
|
||||
return 0
|
||||
@@ -82,7 +70,6 @@ local function peek(b: buffer, s: number, o: number, n: number): number
|
||||
return lshift(peek(b, s, o, o), -lo)
|
||||
end
|
||||
|
||||
-- the bit length of a backward stream [s, e) (up to its end marker), or -1 when the last byte is 0 / it is empty
|
||||
local function streamBits(b: buffer, s: number, e: number): number
|
||||
if e <= s then
|
||||
return -1
|
||||
@@ -94,7 +81,6 @@ local function streamBits(b: buffer, s: number, e: number): number
|
||||
return (e - 1 - s) * 8 + HB[last]
|
||||
end
|
||||
|
||||
-- forward stream: n (<= 24) bits at bit position bp from byte p; bytes at or past e read as zeros
|
||||
local function getF(b: buffer, p: number, e: number, bp: number, n: number): number
|
||||
local byte = p + rshift(bp, 3)
|
||||
local v = 0
|
||||
@@ -108,11 +94,10 @@ local function getF(b: buffer, p: number, e: number, bp: number, n: number): num
|
||||
return band(rshift(v, band(bp, 7)), lshift(1, n) - 1)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ FSE
|
||||
-- FSE decoding table entries (u32): symbol | nbBits << 8 | newStateBase << 16
|
||||
local symTmp = bcreate(512)
|
||||
|
||||
-- build(norm, nsym, al, t) -> true | nil, message. norm: { [symbol + 1] = normalized count (-1 = "less than 1") }
|
||||
-- normalized counts: { [symbol + 1] = count }, -1 = "less than 1" probability
|
||||
local function buildFse(norm: { number }, nsym: number, al: number, t: buffer): (boolean?, string?)
|
||||
local size = lshift(1, al)
|
||||
local high = size - 1
|
||||
@@ -166,7 +151,6 @@ local function buildFse(norm: { number }, nsym: number, al: number, t: buffer):
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- ncount(b, p, e, maxSym, maxAL, norm) -> al, bytesUsed, nsym | nil, message: an FSE table description
|
||||
local function ncount(b: buffer, p: number, e: number, maxSym: number, maxAL: number, norm: { number }): (number?, any, number?)
|
||||
if p >= e then
|
||||
return nil, "truncated FSE table"
|
||||
@@ -251,8 +235,7 @@ local function predefined(dist: { number }, al: number): buffer
|
||||
end
|
||||
local LL_PRE, ML_PRE, OF_PRE = predefined(LL_DEFAULT, 6), predefined(ML_DEFAULT, 6), predefined(OF_DEFAULT, 5)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ Huffman
|
||||
-- hufTable(b, p, e, st) -> bytesUsed | nil, message: a Huffman tree description into st.huf (u16: sym | nbBits << 8)
|
||||
-- Huffman table entry (u16): sym | nbBits << 8
|
||||
local weights = bcreate(256)
|
||||
local wfse = bcreate(64 * 4)
|
||||
local function hufTable(b: buffer, p: number, e: number, st: any): (number?, string?)
|
||||
@@ -380,7 +363,6 @@ local function hufTable(b: buffer, p: number, e: number, st: any): (number?, str
|
||||
return used, nil
|
||||
end
|
||||
|
||||
-- one Huffman stream [s, e) -> cnt literals at lit[dp] | nil, message
|
||||
local function hufStream(b: buffer, s: number, e: number, t: buffer, mb: number, lit: buffer, dp: number, cnt: number): string?
|
||||
local off = streamBits(b, s, e)
|
||||
if off < 0 then
|
||||
@@ -411,8 +393,6 @@ local function hufStream(b: buffer, s: number, e: number, t: buffer, mb: number,
|
||||
return nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ frames
|
||||
-- grow st.out to hold need bytes (doubling, capped at st.maxBytes): true | false (over the limit)
|
||||
local function grow(st: any, need: number): boolean
|
||||
if need > st.maxBytes then
|
||||
return false
|
||||
@@ -424,7 +404,6 @@ local function grow(st: any, need: number): boolean
|
||||
return true
|
||||
end
|
||||
|
||||
-- a compressed block [p, e): nil (ok) | code, message
|
||||
local function block(b: buffer, p: number, e: number, st: any): (string?, string?)
|
||||
-- literals section
|
||||
local b0 = readu8(b, p)
|
||||
@@ -759,7 +738,6 @@ local function block(b: buffer, p: number, e: number, st: any): (string?, string
|
||||
bfill(out, op, readu8(out, from), ml)
|
||||
op += ml
|
||||
else
|
||||
-- overlapping: copy in chunks of `offset` bytes (each chunk's source precedes its destination)
|
||||
local left = ml
|
||||
while left > 0 do
|
||||
local n = if left < offset then left else offset
|
||||
@@ -790,7 +768,6 @@ local function block(b: buffer, p: number, e: number, st: any): (string?, string
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
-- decompress(b, start, stop, maxBytes, verify) -> out, length, consumedEnd | nil, code, message, offset
|
||||
function Decode.decompress(b: buffer, start: number, stop: number, maxBytes: number, verify: boolean): (buffer?, any, any, any)
|
||||
local st: any = {
|
||||
out = bcreate(math.min(maxBytes, math.max(4096, (stop - start) * 3))),
|
||||
@@ -903,7 +880,6 @@ function Decode.decompress(b: buffer, start: number, stop: number, maxBytes: num
|
||||
st.rep1, st.rep2, st.rep3 = 1, 4, 8
|
||||
st.hufBits = nil
|
||||
table.clear(st.tabs)
|
||||
-- blocks
|
||||
local last = false
|
||||
repeat
|
||||
if pos + 3 > stop then
|
||||
|
||||
Reference in new issue
Block a user