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,18 +1,9 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/compress/Brotli/Decode: a Brotli (RFC 7932) decoder over a whole in-memory stream. Written from the RFC, with the
|
||||
-- reference decoder (google/brotli c/dec/decode.c, MIT) as the authority on edge cases (simple prefix code alphabet
|
||||
-- widths, repeat codes, padding checks, the large-window extension). Supports every meta-block kind (compressed,
|
||||
-- uncompressed, metadata), block types / counts, the four literal context modes, context maps with RLE and inverse
|
||||
-- move-to-front, NPOSTFIX / NDIRECT distances, the static dictionary with all 121 transforms, window sizes 10..24 and
|
||||
-- large windows (10..30). The whole output is kept, so the window is only used to tell copies from dictionary words.
|
||||
--
|
||||
-- Bits are read LSB first into a bit buffer of up to 32 bits. Prefix codes decode through tables indexed by the next
|
||||
-- 8 bits; entries are symbol * 32 + length (length < 16), or a link offset * 32 + 16 + subtable bits.
|
||||
--
|
||||
-- Nothing here throws: every buffer access is checked or in range by construction (the output buffer is sized for each
|
||||
-- meta-block before it is decoded); bad data returns nil, message.
|
||||
-- codecs/compress/Brotli/Decode: a Brotli (RFC 7932) decoder, with the large-window extension (windows 10..30).
|
||||
-- Written from the RFC, with google/brotli c/dec/decode.c (MIT) as the authority on edge cases.
|
||||
-- Prefix table entry: symbol * 32 + length (length < 16), or a link offset * 32 + 16 + subtable bits (8-bit primary).
|
||||
local band, bor, lshift, rshift = bit32.band, bit32.bor, bit32.lshift, bit32.rshift
|
||||
local readu8, readu16, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
local bcreate, bcopy, blen, bfill = buffer.create, buffer.copy, buffer.len, buffer.fill
|
||||
@@ -27,7 +18,6 @@ export type Static = {
|
||||
transforms: { number }, -- 121 x (prefix id, type, suffix id)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local CL_ORDER = { 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
|
||||
local CLP_LEN = { 2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4 }
|
||||
local CLP_VAL = { 0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5 }
|
||||
@@ -54,7 +44,6 @@ end
|
||||
local NTRANSFORMS = 121
|
||||
local MAX_ALLOWED_DISTANCE = 0x7FFFFFFC
|
||||
|
||||
-- 8-bit reversal
|
||||
local REV8 = bcreate(256)
|
||||
for i = 0, 255 do
|
||||
local r, v = 0, i
|
||||
@@ -65,19 +54,15 @@ for i = 0, 255 do
|
||||
writeu8(REV8, i, r)
|
||||
end
|
||||
local function reverse(code: number, len: number): number
|
||||
-- len <= 15
|
||||
local r = readu8(REV8, band(code, 255)) * 256 + readu8(REV8, band(rshift(code, 8), 255))
|
||||
return rshift(r, 16 - len)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ prefix tables
|
||||
local count = table.create(16, 0)
|
||||
local nextc = table.create(16, 0)
|
||||
local subBits = table.create(256, 0)
|
||||
local subOff = table.create(256, 0)
|
||||
|
||||
-- tableSize(lens, n) -> the entries build() will write (256 + subtables), or -1 for an over-subscribed set.
|
||||
-- Also leaves count / nextc / subBits ready for build().
|
||||
local function prepare(lens: buffer, n: number): number
|
||||
for l = 1, 16 do
|
||||
count[l] = 0
|
||||
@@ -135,7 +120,6 @@ local function prepare(lens: buffer, n: number): number
|
||||
return size
|
||||
end
|
||||
|
||||
-- build(lens, n, T, at): the table prepared by prepare() at entry offset `at` of T (T must have room)
|
||||
local function build(lens: buffer, n: number, T: buffer, at: number)
|
||||
local single = -1
|
||||
local used = 0
|
||||
@@ -188,8 +172,7 @@ local function build(lens: buffer, n: number, T: buffer, at: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- the largest distance alphabet whose distances stay under MAX_ALLOWED_DISTANCE (large windows); after the reference
|
||||
-- decoder's BrotliCalculateDistanceCodeLimit
|
||||
-- largest distance alphabet under MAX_ALLOWED_DISTANCE, as the reference's BrotliCalculateDistanceCodeLimit
|
||||
local function distanceLimit(maxDistance: number, npostfix: number, ndirect: number): number
|
||||
if maxDistance <= ndirect then
|
||||
return maxDistance + 16
|
||||
@@ -213,8 +196,6 @@ local function distanceLimit(maxDistance: number, npostfix: number, ndirect: num
|
||||
return bor(lshift(group, npostfix), postfix) + ndirect + 16 + 1
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decoder
|
||||
-- decompress(src, off, len, maxBytes, S, largeWindow) -> out, n, consumedEnd | nil, message
|
||||
function Decode.decompress(src: buffer, off: number, len: number, maxBytes: number, S: Static, largeWindow: boolean): (buffer?, any, number?)
|
||||
local lim = off + len
|
||||
local ip = off
|
||||
@@ -251,7 +232,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
return (ip - lim) * 8 > bc
|
||||
end
|
||||
|
||||
-- prefix tables of the current meta-block: T (u32 entries), tpos entries used
|
||||
local T = bcreate(4096 * 4)
|
||||
local tpos = 0
|
||||
local LENS = bcreate(1128)
|
||||
@@ -273,7 +253,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
return rshift(e, 5)
|
||||
end
|
||||
|
||||
-- install(n) -> base: the table for LENS[0 .. n - 1] appended to T, or -1
|
||||
local function install(n: number): number
|
||||
local size = prepare(LENS, n)
|
||||
if size < 0 then
|
||||
@@ -291,7 +270,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
return base
|
||||
end
|
||||
|
||||
-- readCode(alphabetMax, limit) -> table base | -1 (corrupt)
|
||||
local function readCode(alphabetMax: number, limit: number): number
|
||||
bfill(LENS, 0, 0, limit)
|
||||
local hskip = bits(2)
|
||||
@@ -447,7 +425,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
return BL_OFF[code + 1] + bits(BL_BITS[code + 1])
|
||||
end
|
||||
|
||||
-- readContextMap(size) -> ntrees, map buffer | -1
|
||||
local function readContextMap(size: number): (number, buffer)
|
||||
local ntrees = varLen8() + 1
|
||||
local map = bcreate(size)
|
||||
@@ -475,7 +452,7 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
if i + reps > size then
|
||||
return -1, map
|
||||
end
|
||||
i += reps -- zeros (map is zero-filled)
|
||||
i += reps
|
||||
end
|
||||
if overrun() then
|
||||
return -1, map
|
||||
@@ -499,7 +476,7 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
return ntrees, map
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------ stream header
|
||||
-- RFC 7932 9.1: stream header
|
||||
fill()
|
||||
local wbits
|
||||
local large = false
|
||||
@@ -545,7 +522,7 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
local d0, d1, d2, d3 = 4, 11, 15, 16
|
||||
|
||||
while true do
|
||||
-- ---------------------------------------------------------------- meta-block header
|
||||
-- RFC 7932 9.2: meta-block header
|
||||
if overrun() then
|
||||
return nil, "truncated stream"
|
||||
end
|
||||
@@ -626,7 +603,7 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
continue
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------- compressed meta-block
|
||||
-- RFC 7932 9.3: compressed meta-block data
|
||||
tpos = 0
|
||||
-- block types / counts: 1 = literals, 2 = commands, 3 = distances
|
||||
local nbtL, nbtI, nbtD = 1, 1, 1
|
||||
@@ -673,7 +650,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
if ntreesD < 0 then
|
||||
return nil, "bad distance context map"
|
||||
end
|
||||
-- the tree groups
|
||||
local litBase = table.create(ntreesL, 0)
|
||||
for i = 1, ntreesL do
|
||||
local b = readCode(256, 256)
|
||||
@@ -703,7 +679,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
if overrun() then
|
||||
return nil, "truncated meta-block header"
|
||||
end
|
||||
-- context map slices resolved to table bases
|
||||
local litMap = table.create(64 * nbtL, 0)
|
||||
for i = 0, 64 * nbtL - 1 do
|
||||
litMap[i + 1] = litBase[readu8(cmapL, i) + 1]
|
||||
@@ -713,7 +688,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
distMap[i + 1] = distBase[readu8(cmapD, i) + 1]
|
||||
end
|
||||
|
||||
-- block state: current type, previous type
|
||||
local typeL, prevL = 0, 1
|
||||
local typeI, prevI = 0, 1
|
||||
local typeD, prevD = 0, 1
|
||||
@@ -727,7 +701,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
if ip > lim + 4 then
|
||||
return nil, "truncated stream"
|
||||
end
|
||||
-- command block switch
|
||||
if cntI == 0 then
|
||||
local t = decodeSym(btTreeI)
|
||||
if t == 1 then
|
||||
@@ -759,7 +732,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
if ce > 0 then
|
||||
cpyLen += bits(ce)
|
||||
end
|
||||
-- literals
|
||||
if insLen > remaining then
|
||||
return nil, "insert past the meta-block end"
|
||||
end
|
||||
@@ -815,7 +787,6 @@ function Decode.decompress(src: buffer, off: number, len: number, maxBytes: numb
|
||||
break
|
||||
end
|
||||
end
|
||||
-- distance
|
||||
local dist
|
||||
local push = true
|
||||
if cell < 2 then
|
||||
|
||||
Reference in new issue
Block a user