997 lines
25 KiB
Luau
997 lines
25 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/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.
|
|
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
|
|
|
|
local Decode = {}
|
|
|
|
export type Static = {
|
|
dictionary: buffer, -- 122784 bytes
|
|
context: buffer, -- 2048 bytes: 4 modes x (256 for p1, 256 for p2)
|
|
prefixSuffix: buffer, -- length-prefixed strings
|
|
prefixSuffixMap: { number },
|
|
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 }
|
|
local BL_OFF = {
|
|
1, 5, 9, 13, 17, 25, 33, 41, 49, 65, 81, 97, 113, 145, 177, 209, 241, 305, 369, 497, 753, 1265, 2289, 4337, 8433, 16625,
|
|
}
|
|
local BL_BITS = { 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 11, 12, 13, 24 }
|
|
local INS_BASE = { 0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50, 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594 }
|
|
local INS_EXTRA = { 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 12, 14, 24 }
|
|
local CPY_BASE = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30, 38, 54, 70, 102, 134, 198, 326, 582, 1094, 2118 }
|
|
local CPY_EXTRA = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 24 }
|
|
local CELL_INS = { 0, 0, 0, 0, 8, 8, 0, 16, 8, 16, 16 }
|
|
local CELL_CPY = { 0, 8, 0, 8, 0, 8, 16, 0, 16, 8, 16 }
|
|
-- static dictionary: bits of the word index per word length 0..24 (index + 1), and the offsets
|
|
local NDBITS = { 0, 0, 0, 0, 10, 10, 11, 11, 10, 10, 10, 10, 10, 9, 9, 8, 7, 7, 8, 7, 7, 6, 6, 5, 5 }
|
|
local DOFF = table.create(25, 0)
|
|
do
|
|
local o = 0
|
|
for l = 4, 24 do
|
|
DOFF[l + 1] = o
|
|
o += l * 2 ^ NDBITS[l + 1]
|
|
end
|
|
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
|
|
for _ = 1, 8 do
|
|
r = r * 2 + v % 2
|
|
v = v // 2
|
|
end
|
|
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
|
|
end
|
|
local used = 0
|
|
for s = 0, n - 1 do
|
|
local l = readu8(lens, s)
|
|
if l > 0 then
|
|
count[l] += 1
|
|
used += 1
|
|
end
|
|
end
|
|
if used <= 1 then
|
|
return 256
|
|
end
|
|
local left = 1
|
|
for l = 1, 15 do
|
|
left = left * 2 - count[l]
|
|
if left < 0 then
|
|
return -1
|
|
end
|
|
end
|
|
-- canonical codes: the first code of length l is (first(l - 1) + count(l - 1)) * 2
|
|
local code = 0
|
|
for l = 1, 15 do
|
|
nextc[l] = code
|
|
code = (code + count[l]) * 2
|
|
end
|
|
for i = 1, 256 do
|
|
subBits[i] = 0
|
|
end
|
|
local nc = table.clone(nextc)
|
|
for s = 0, n - 1 do
|
|
local l = readu8(lens, s)
|
|
if l > 8 then
|
|
local c = nc[l]
|
|
nc[l] = c + 1
|
|
local pre = band(reverse(c, l), 255)
|
|
local k = l - 8
|
|
if k > subBits[pre + 1] then
|
|
subBits[pre + 1] = k
|
|
end
|
|
elseif l > 0 then
|
|
nc[l] += 1
|
|
end
|
|
end
|
|
local size = 256
|
|
for i = 1, 256 do
|
|
local k = subBits[i]
|
|
if k > 0 then
|
|
subOff[i] = size
|
|
size += lshift(1, k)
|
|
end
|
|
end
|
|
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
|
|
for s = 0, n - 1 do
|
|
if readu8(lens, s) > 0 then
|
|
used += 1
|
|
single = s
|
|
end
|
|
end
|
|
if used <= 1 then
|
|
local e = (if single < 0 then 0 else single) * 32
|
|
for i = 0, 255 do
|
|
writeu32(T, (at + i) * 4, e)
|
|
end
|
|
return
|
|
end
|
|
for i = 1, 256 do
|
|
local k = subBits[i]
|
|
if k > 0 then
|
|
writeu32(T, (at + i - 1) * 4, subOff[i] * 32 + 16 + k)
|
|
end
|
|
end
|
|
for s = 0, n - 1 do
|
|
local l = readu8(lens, s)
|
|
if l > 0 then
|
|
local c = nextc[l]
|
|
nextc[l] = c + 1
|
|
local rev = reverse(c, l)
|
|
local e = s * 32 + l
|
|
if l <= 8 then
|
|
local step = lshift(1, l)
|
|
local i = rev
|
|
while i < 256 do
|
|
writeu32(T, (at + i) * 4, e)
|
|
i += step
|
|
end
|
|
else
|
|
local pre = band(rev, 255)
|
|
local sb = subBits[pre + 1]
|
|
local base = at + subOff[pre + 1]
|
|
local step = lshift(1, l - 8)
|
|
local i = rshift(rev, 8)
|
|
local lim = lshift(1, sb)
|
|
while i < lim do
|
|
writeu32(T, (base + i) * 4, e)
|
|
i += step
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- the largest distance alphabet whose distances stay under MAX_ALLOWED_DISTANCE (large windows); after the reference
|
|
-- decoder's BrotliCalculateDistanceCodeLimit
|
|
local function distanceLimit(maxDistance: number, npostfix: number, ndirect: number): number
|
|
if maxDistance <= ndirect then
|
|
return maxDistance + 16
|
|
end
|
|
local offset = maxDistance + 1 - ndirect - 1
|
|
offset = rshift(offset, npostfix) + 4
|
|
local ndistbits = 0
|
|
local tmp = offset // 2
|
|
while tmp ~= 0 do
|
|
ndistbits += 1
|
|
tmp = rshift(tmp, 1)
|
|
end
|
|
ndistbits -= 1
|
|
local half = band(rshift(offset, ndistbits), 1)
|
|
local group = bor(lshift(ndistbits - 1, 1), half)
|
|
if group == 0 then
|
|
return ndirect + 16
|
|
end
|
|
group -= 1
|
|
local postfix = lshift(1, npostfix) - 1
|
|
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
|
|
local bb, bc = 0, 0
|
|
|
|
local function fill()
|
|
while bc <= 24 do
|
|
local byte = 0
|
|
if ip < lim then
|
|
byte = readu8(src, ip)
|
|
end
|
|
ip += 1
|
|
bb = bor(bb, lshift(byte, bc))
|
|
bc += 8
|
|
end
|
|
end
|
|
local function bits(n: number): number
|
|
if bc < n then
|
|
fill()
|
|
end
|
|
local v = band(bb, lshift(1, n) - 1)
|
|
bb = rshift(bb, n)
|
|
bc -= n
|
|
return v
|
|
end
|
|
local function bitsLong(n: number): number
|
|
if n <= 24 then
|
|
return bits(n)
|
|
end
|
|
local lo = bits(16)
|
|
return lo + bits(n - 16) * 65536
|
|
end
|
|
local function overrun(): boolean
|
|
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)
|
|
local CLL = bcreate(18)
|
|
local CLT = bcreate(256 * 4)
|
|
|
|
local function decodeSym(base: number): number
|
|
if bc < 15 then
|
|
fill()
|
|
end
|
|
local e = readu32(T, (base + band(bb, 255)) * 4)
|
|
local l = band(e, 31)
|
|
if l >= 16 then
|
|
e = readu32(T, (base + rshift(e, 5) + band(rshift(bb, 8), lshift(1, l - 16) - 1)) * 4)
|
|
l = band(e, 31)
|
|
end
|
|
bb = rshift(bb, l)
|
|
bc -= l
|
|
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
|
|
return -1
|
|
end
|
|
local need = (tpos + size) * 4
|
|
if need > blen(T) then
|
|
local nt = bcreate(math.max(blen(T) * 2, need))
|
|
bcopy(nt, 0, T, 0, tpos * 4)
|
|
T = nt
|
|
end
|
|
build(LENS, n, T, tpos)
|
|
local base = tpos
|
|
tpos += size
|
|
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)
|
|
if hskip == 1 then
|
|
local nsym = bits(2) + 1
|
|
local maxBits = 0
|
|
local x = alphabetMax - 1
|
|
while x > 0 do
|
|
maxBits += 1
|
|
x = rshift(x, 1)
|
|
end
|
|
local s1, s2, s3, s4 = -1, -1, -1, -1
|
|
for i = 1, nsym do
|
|
local v = bits(maxBits)
|
|
if v >= limit then
|
|
return -1
|
|
end
|
|
if i == 1 then
|
|
s1 = v
|
|
elseif i == 2 then
|
|
s2 = v
|
|
elseif i == 3 then
|
|
s3 = v
|
|
else
|
|
s4 = v
|
|
end
|
|
end
|
|
if nsym >= 2 and (s1 == s2 or s1 == s3 or s1 == s4 or (nsym >= 3 and (s2 == s3 or s2 == s4)) or (nsym == 4 and s3 == s4)) then
|
|
return -1
|
|
end
|
|
if nsym == 1 then
|
|
writeu8(LENS, s1, 1)
|
|
elseif nsym == 2 then
|
|
writeu8(LENS, s1, 1)
|
|
writeu8(LENS, s2, 1)
|
|
elseif nsym == 3 then
|
|
writeu8(LENS, s1, 1)
|
|
writeu8(LENS, s2, 2)
|
|
writeu8(LENS, s3, 2)
|
|
else
|
|
if bits(1) == 0 then
|
|
writeu8(LENS, s1, 2)
|
|
writeu8(LENS, s2, 2)
|
|
writeu8(LENS, s3, 2)
|
|
writeu8(LENS, s4, 2)
|
|
else
|
|
writeu8(LENS, s1, 1)
|
|
writeu8(LENS, s2, 2)
|
|
writeu8(LENS, s3, 3)
|
|
writeu8(LENS, s4, 3)
|
|
end
|
|
end
|
|
return install(limit)
|
|
end
|
|
-- complex: the code length code
|
|
bfill(CLL, 0, 0, 18)
|
|
local space, num = 32, 0
|
|
for i = hskip + 1, 18 do
|
|
if bc < 4 then
|
|
fill()
|
|
end
|
|
local ix = band(bb, 15)
|
|
local v = CLP_VAL[ix + 1]
|
|
local dl = CLP_LEN[ix + 1]
|
|
bb = rshift(bb, dl)
|
|
bc -= dl
|
|
writeu8(CLL, CL_ORDER[i], v)
|
|
if v ~= 0 then
|
|
space -= rshift(32, v)
|
|
num += 1
|
|
if space <= 0 then
|
|
break
|
|
end
|
|
end
|
|
end
|
|
if not (num == 1 or space == 0) then
|
|
return -1
|
|
end
|
|
-- its table (lengths <= 5)
|
|
do
|
|
local size = prepare(CLL, 18)
|
|
if size ~= 256 then
|
|
return -1
|
|
end
|
|
build(CLL, 18, CLT, 0)
|
|
end
|
|
-- the symbol code lengths
|
|
local sym, prev, rep, repLen = 0, 8, 0, 0
|
|
space = 32768
|
|
while sym < limit and space > 0 do
|
|
if bc < 15 then
|
|
fill()
|
|
end
|
|
local e = readu32(CLT, band(bb, 255) * 4)
|
|
local l = band(e, 31)
|
|
bb = rshift(bb, l)
|
|
bc -= l
|
|
local cl = rshift(e, 5)
|
|
if cl < 16 then
|
|
rep = 0
|
|
writeu8(LENS, sym, cl)
|
|
if cl ~= 0 then
|
|
prev = cl
|
|
space -= rshift(32768, cl)
|
|
end
|
|
sym += 1
|
|
else
|
|
local extra = if cl == 16 then 2 else 3
|
|
local d = bits(extra)
|
|
local newLen = if cl == 16 then prev else 0
|
|
if repLen ~= newLen then
|
|
rep = 0
|
|
repLen = newLen
|
|
end
|
|
local old = rep
|
|
if rep > 0 then
|
|
rep = (rep - 2) * lshift(1, extra)
|
|
end
|
|
rep += d + 3
|
|
local delta = rep - old
|
|
if sym + delta > limit then
|
|
return -1
|
|
end
|
|
if repLen ~= 0 then
|
|
bfill(LENS, sym, repLen, delta)
|
|
space -= delta * rshift(32768, repLen)
|
|
end
|
|
sym += delta
|
|
end
|
|
if overrun() then
|
|
return -1
|
|
end
|
|
end
|
|
if space ~= 0 then
|
|
return -1
|
|
end
|
|
return install(limit)
|
|
end
|
|
|
|
local function varLen8(): number
|
|
if bits(1) == 0 then
|
|
return 0
|
|
end
|
|
local n = bits(3)
|
|
if n == 0 then
|
|
return 1
|
|
end
|
|
return bits(n) + lshift(1, n)
|
|
end
|
|
|
|
local function blockLen(base: number): number
|
|
local code = decodeSym(base)
|
|
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)
|
|
if ntrees < 2 then
|
|
return ntrees, map
|
|
end
|
|
local rleMax = 0
|
|
if bits(1) == 1 then
|
|
rleMax = bits(4) + 1
|
|
end
|
|
local base = readCode(ntrees + rleMax, ntrees + rleMax)
|
|
if base < 0 then
|
|
return -1, map
|
|
end
|
|
local i = 0
|
|
while i < size do
|
|
local code = decodeSym(base)
|
|
if code == 0 then
|
|
i += 1
|
|
elseif code > rleMax then
|
|
writeu8(map, i, code - rleMax)
|
|
i += 1
|
|
else
|
|
local reps = lshift(1, code) + bits(code)
|
|
if i + reps > size then
|
|
return -1, map
|
|
end
|
|
i += reps -- zeros (map is zero-filled)
|
|
end
|
|
if overrun() then
|
|
return -1, map
|
|
end
|
|
end
|
|
if bits(1) == 1 then -- inverse move-to-front
|
|
local mtf = table.create(256, 0)
|
|
for k = 1, 256 do
|
|
mtf[k] = k - 1
|
|
end
|
|
for k = 0, size - 1 do
|
|
local idx = readu8(map, k)
|
|
local v = mtf[idx + 1]
|
|
writeu8(map, k, v)
|
|
if idx > 0 then
|
|
table.move(mtf, 1, idx, 2)
|
|
mtf[1] = v
|
|
end
|
|
end
|
|
end
|
|
return ntrees, map
|
|
end
|
|
|
|
-- ------------------------------------------------------------------ stream header
|
|
fill()
|
|
local wbits
|
|
local large = false
|
|
if bits(1) == 0 then
|
|
wbits = 16
|
|
else
|
|
local n = bits(3)
|
|
if n ~= 0 then
|
|
wbits = 17 + n
|
|
else
|
|
local m = bits(3)
|
|
if m == 1 then
|
|
if not largeWindow then
|
|
return nil, "unsupported: large-window brotli (pass opts.largeWindow = true)"
|
|
end
|
|
if bits(1) == 1 then
|
|
return nil, "bad window bits"
|
|
end
|
|
large = true
|
|
wbits = bits(6)
|
|
if wbits < 10 or wbits > 30 then
|
|
return nil, "bad large window bits"
|
|
end
|
|
elseif m ~= 0 then
|
|
wbits = 8 + m
|
|
else
|
|
wbits = 17
|
|
end
|
|
end
|
|
end
|
|
local maxBackward = 2 ^ wbits - 16
|
|
|
|
local dict = S.dictionary
|
|
local LUT = S.context
|
|
local PS = S.prefixSuffix
|
|
local PSMAP = S.prefixSuffixMap
|
|
local TRANS = S.transforms
|
|
local WORD = bcreate(64)
|
|
|
|
local out = bcreate(math.max(256, math.min(len * 4, maxBytes)))
|
|
local op = 0
|
|
-- distance ring buffer: last, second, third, fourth
|
|
local d0, d1, d2, d3 = 4, 11, 15, 16
|
|
|
|
while true do
|
|
-- ---------------------------------------------------------------- meta-block header
|
|
if overrun() then
|
|
return nil, "truncated stream"
|
|
end
|
|
local isLast = bits(1) == 1
|
|
if isLast and bits(1) == 1 then
|
|
break -- ISLASTEMPTY
|
|
end
|
|
local mn = bits(2)
|
|
local mlen = 0
|
|
if mn == 3 then
|
|
-- metadata
|
|
if bits(1) ~= 0 then
|
|
return nil, "reserved bit set"
|
|
end
|
|
local nbytes = bits(2)
|
|
for i = 0, nbytes - 1 do
|
|
local b = bits(8)
|
|
if i + 1 == nbytes and nbytes > 1 and b == 0 then
|
|
return nil, "exuberant metadata length byte"
|
|
end
|
|
mlen += b * 2 ^ (8 * i)
|
|
end
|
|
if nbytes > 0 then
|
|
mlen += 1
|
|
end
|
|
local pad = band(bc, 7)
|
|
if pad > 0 and bits(pad) ~= 0 then
|
|
return nil, "nonzero padding"
|
|
end
|
|
ip -= rshift(bc, 3)
|
|
bb, bc = 0, 0
|
|
if ip + mlen > lim then
|
|
return nil, "truncated metadata"
|
|
end
|
|
ip += mlen
|
|
if isLast then
|
|
break
|
|
end
|
|
continue
|
|
end
|
|
local nib = mn + 4
|
|
for i = 0, nib - 1 do
|
|
local v = bits(4)
|
|
if i + 1 == nib and nib > 4 and v == 0 then
|
|
return nil, "exuberant nibble"
|
|
end
|
|
mlen += v * 2 ^ (4 * i)
|
|
end
|
|
mlen += 1
|
|
local uncompressed = false
|
|
if not isLast then
|
|
uncompressed = bits(1) == 1
|
|
end
|
|
if op + mlen > maxBytes then
|
|
return nil, "limit: output over " .. maxBytes .. " bytes"
|
|
end
|
|
if op + mlen + 8 > blen(out) then
|
|
local nb = bcreate(math.min(math.max(blen(out) * 2, op + mlen + 8), maxBytes + 8))
|
|
bcopy(nb, 0, out, 0, op)
|
|
out = nb
|
|
end
|
|
if uncompressed then
|
|
local pad = band(bc, 7)
|
|
if pad > 0 and bits(pad) ~= 0 then
|
|
return nil, "nonzero padding"
|
|
end
|
|
ip -= rshift(bc, 3)
|
|
bb, bc = 0, 0
|
|
if ip + mlen > lim then
|
|
return nil, "truncated uncompressed block"
|
|
end
|
|
bcopy(out, op, src, ip, mlen)
|
|
ip += mlen
|
|
op += mlen
|
|
if isLast then
|
|
break
|
|
end
|
|
continue
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- compressed meta-block
|
|
tpos = 0
|
|
-- block types / counts: 1 = literals, 2 = commands, 3 = distances
|
|
local nbtL, nbtI, nbtD = 1, 1, 1
|
|
local btTreeL, btTreeI, btTreeD = 0, 0, 0
|
|
local blTreeL, blTreeI, blTreeD = 0, 0, 0
|
|
local cntL, cntI, cntD = 1073741824, 1073741824, 1073741824
|
|
for c = 1, 3 do
|
|
local nbt = varLen8() + 1
|
|
local btt, blt, cnt = 0, 0, 1073741824
|
|
if nbt >= 2 then
|
|
btt = readCode(nbt + 2, nbt + 2)
|
|
if btt < 0 then
|
|
return nil, "bad block type code"
|
|
end
|
|
blt = readCode(26, 26)
|
|
if blt < 0 then
|
|
return nil, "bad block count code"
|
|
end
|
|
cnt = blockLen(blt)
|
|
end
|
|
if c == 1 then
|
|
nbtL, btTreeL, blTreeL, cntL = nbt, btt, blt, cnt
|
|
elseif c == 2 then
|
|
nbtI, btTreeI, blTreeI, cntI = nbt, btt, blt, cnt
|
|
else
|
|
nbtD, btTreeD, blTreeD, cntD = nbt, btt, blt, cnt
|
|
end
|
|
end
|
|
if overrun() then
|
|
return nil, "truncated meta-block header"
|
|
end
|
|
local npostfix = bits(2)
|
|
local ndirect = lshift(bits(4), npostfix)
|
|
local postMask = lshift(1, npostfix) - 1
|
|
local modes = table.create(nbtL, 0)
|
|
for i = 1, nbtL do
|
|
modes[i] = bits(2)
|
|
end
|
|
local ntreesL, cmapL = readContextMap(64 * nbtL)
|
|
if ntreesL < 0 then
|
|
return nil, "bad literal context map"
|
|
end
|
|
local ntreesD, cmapD = readContextMap(4 * nbtD)
|
|
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)
|
|
if b < 0 then
|
|
return nil, "bad literal code"
|
|
end
|
|
litBase[i] = b
|
|
end
|
|
local cmdBase = table.create(nbtI, 0)
|
|
for i = 1, nbtI do
|
|
local b = readCode(704, 704)
|
|
if b < 0 then
|
|
return nil, "bad insert-and-copy code"
|
|
end
|
|
cmdBase[i] = b
|
|
end
|
|
local distMax = 16 + ndirect + lshift(if large then 62 else 24, npostfix + 1)
|
|
local distLimit = if large then distanceLimit(MAX_ALLOWED_DISTANCE, npostfix, ndirect) else distMax
|
|
local distBase = table.create(ntreesD, 0)
|
|
for i = 1, ntreesD do
|
|
local b = readCode(distMax, distLimit)
|
|
if b < 0 then
|
|
return nil, "bad distance code"
|
|
end
|
|
distBase[i] = b
|
|
end
|
|
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]
|
|
end
|
|
local distMap = table.create(4 * nbtD, 0)
|
|
for i = 0, 4 * nbtD - 1 do
|
|
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
|
|
local lutBase = modes[1] * 512
|
|
local litSlice = 0
|
|
local cmdTree = cmdBase[1]
|
|
local distSlice = 0
|
|
|
|
local remaining = mlen
|
|
while remaining > 0 do
|
|
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
|
|
t = typeI + 1
|
|
elseif t == 0 then
|
|
t = prevI
|
|
else
|
|
t -= 2
|
|
end
|
|
if t >= nbtI then
|
|
t -= nbtI
|
|
end
|
|
prevI, typeI = typeI, t
|
|
cntI = blockLen(blTreeI)
|
|
cmdTree = cmdBase[t + 1]
|
|
end
|
|
cntI -= 1
|
|
local cmd = decodeSym(cmdTree)
|
|
local cell = rshift(cmd, 6)
|
|
local insCode = CELL_INS[cell + 1] + band(rshift(cmd, 3), 7)
|
|
local cpyCode = CELL_CPY[cell + 1] + band(cmd, 7)
|
|
local insLen = INS_BASE[insCode + 1]
|
|
local ie = INS_EXTRA[insCode + 1]
|
|
if ie > 0 then
|
|
insLen += bits(ie)
|
|
end
|
|
local cpyLen = CPY_BASE[cpyCode + 1]
|
|
local ce = CPY_EXTRA[cpyCode + 1]
|
|
if ce > 0 then
|
|
cpyLen += bits(ce)
|
|
end
|
|
-- literals
|
|
if insLen > remaining then
|
|
return nil, "insert past the meta-block end"
|
|
end
|
|
if insLen > 0 then
|
|
local p1 = if op > 0 then readu8(out, op - 1) else 0
|
|
local p2 = if op > 1 then readu8(out, op - 2) else 0
|
|
for _ = 1, insLen do
|
|
if cntL == 0 then
|
|
local t = decodeSym(btTreeL)
|
|
if t == 1 then
|
|
t = typeL + 1
|
|
elseif t == 0 then
|
|
t = prevL
|
|
else
|
|
t -= 2
|
|
end
|
|
if t >= nbtL then
|
|
t -= nbtL
|
|
end
|
|
prevL, typeL = typeL, t
|
|
cntL = blockLen(blTreeL)
|
|
lutBase = modes[t + 1] * 512
|
|
litSlice = t * 64
|
|
end
|
|
cntL -= 1
|
|
local ctx = bor(readu8(LUT, lutBase + p1), readu8(LUT, lutBase + 256 + p2))
|
|
local base = litMap[litSlice + ctx + 1]
|
|
if bc < 16 then
|
|
if ip + 2 <= lim then
|
|
bb = bor(bb, lshift(readu16(src, ip), bc))
|
|
ip += 2
|
|
bc += 16
|
|
else
|
|
fill()
|
|
end
|
|
end
|
|
local e = readu32(T, (base + band(bb, 255)) * 4)
|
|
local l = band(e, 31)
|
|
if l >= 16 then
|
|
e = readu32(T, (base + rshift(e, 5) + band(rshift(bb, 8), lshift(1, l - 16) - 1)) * 4)
|
|
l = band(e, 31)
|
|
end
|
|
bb = rshift(bb, l)
|
|
bc -= l
|
|
local lit = rshift(e, 5)
|
|
writeu8(out, op, lit)
|
|
op += 1
|
|
p2 = p1
|
|
p1 = lit
|
|
end
|
|
remaining -= insLen
|
|
if remaining == 0 then
|
|
break
|
|
end
|
|
end
|
|
-- distance
|
|
local dist
|
|
local push = true
|
|
if cell < 2 then
|
|
dist = d0
|
|
push = false
|
|
else
|
|
if cntD == 0 then
|
|
local t = decodeSym(btTreeD)
|
|
if t == 1 then
|
|
t = typeD + 1
|
|
elseif t == 0 then
|
|
t = prevD
|
|
else
|
|
t -= 2
|
|
end
|
|
if t >= nbtD then
|
|
t -= nbtD
|
|
end
|
|
prevD, typeD = typeD, t
|
|
cntD = blockLen(blTreeD)
|
|
distSlice = t * 4
|
|
end
|
|
cntD -= 1
|
|
local dctx = if cpyLen > 4 then 3 else cpyLen - 2
|
|
local dcode = decodeSym(distMap[distSlice + dctx + 1])
|
|
if dcode < 16 then
|
|
if dcode == 0 then
|
|
dist = d0
|
|
push = false
|
|
elseif dcode == 1 then
|
|
dist = d1
|
|
elseif dcode == 2 then
|
|
dist = d2
|
|
elseif dcode == 3 then
|
|
dist = d3
|
|
elseif dcode < 10 then
|
|
local k = (dcode - 2) // 2 -- 4,5 -> 1; 6,7 -> 2; 8,9 -> 3
|
|
dist = if dcode % 2 == 0 then d0 - k else d0 + k
|
|
else
|
|
local k = (dcode - 8) // 2 -- 10,11 -> 1; ...
|
|
dist = if dcode % 2 == 0 then d1 - k else d1 + k
|
|
end
|
|
if dist <= 0 then
|
|
return nil, "bad distance"
|
|
end
|
|
elseif dcode < 16 + ndirect then
|
|
dist = dcode - 15
|
|
else
|
|
local x = dcode - ndirect - 16
|
|
local ndistbits = 1 + rshift(x, npostfix + 1)
|
|
local hcode = rshift(x, npostfix)
|
|
local lcode = band(x, postMask)
|
|
local offset = (2 + band(hcode, 1)) * 2 ^ ndistbits - 4
|
|
dist = (offset + bitsLong(ndistbits)) * 2 ^ npostfix + lcode + ndirect + 1
|
|
end
|
|
end
|
|
local maxDist = if op < maxBackward then op else maxBackward
|
|
if dist > maxDist then
|
|
-- a static dictionary word
|
|
if dist > MAX_ALLOWED_DISTANCE or cpyLen < 4 or cpyLen > 24 then
|
|
return nil, "bad dictionary reference"
|
|
end
|
|
local nb = NDBITS[cpyLen + 1]
|
|
local addr = dist - maxDist - 1
|
|
local widx = band(addr, lshift(1, nb) - 1)
|
|
local tidx = rshift(addr, nb)
|
|
if tidx >= NTRANSFORMS then
|
|
return nil, "bad dictionary transform"
|
|
end
|
|
local woff = DOFF[cpyLen + 1] + widx * cpyLen
|
|
-- transform into WORD
|
|
local pfx = PSMAP[TRANS[tidx * 3 + 1] + 1]
|
|
local ttype = TRANS[tidx * 3 + 2]
|
|
local sfx = PSMAP[TRANS[tidx * 3 + 3] + 1]
|
|
local w = 0
|
|
local pl = readu8(PS, pfx)
|
|
for k = 1, pl do
|
|
writeu8(WORD, w, readu8(PS, pfx + k))
|
|
w += 1
|
|
end
|
|
local wl = cpyLen
|
|
local wo = woff
|
|
if ttype <= 9 then
|
|
wl -= ttype
|
|
elseif ttype >= 12 and ttype <= 20 then
|
|
local skip = ttype - 11
|
|
wo += skip
|
|
wl -= skip
|
|
end
|
|
local ws = w
|
|
if wl > 0 then
|
|
bcopy(WORD, w, dict, wo, wl)
|
|
w += wl
|
|
end
|
|
if ttype == 10 then
|
|
-- uppercase the first character
|
|
local c = readu8(WORD, ws)
|
|
if c < 0xC0 then
|
|
if c >= 97 and c <= 122 then
|
|
writeu8(WORD, ws, c - 32)
|
|
end
|
|
elseif c < 0xE0 then
|
|
writeu8(WORD, ws + 1, bit32.bxor(readu8(WORD, ws + 1), 32))
|
|
else
|
|
writeu8(WORD, ws + 2, bit32.bxor(readu8(WORD, ws + 2), 5))
|
|
end
|
|
elseif ttype == 11 then
|
|
local p = ws
|
|
local left = wl
|
|
while left > 0 do
|
|
local c = readu8(WORD, p)
|
|
local step
|
|
if c < 0xC0 then
|
|
if c >= 97 and c <= 122 then
|
|
writeu8(WORD, p, c - 32)
|
|
end
|
|
step = 1
|
|
elseif c < 0xE0 then
|
|
writeu8(WORD, p + 1, bit32.bxor(readu8(WORD, p + 1), 32))
|
|
step = 2
|
|
else
|
|
writeu8(WORD, p + 2, bit32.bxor(readu8(WORD, p + 2), 5))
|
|
step = 3
|
|
end
|
|
p += step
|
|
left -= step
|
|
end
|
|
end
|
|
local sl = readu8(PS, sfx)
|
|
for k = 1, sl do
|
|
writeu8(WORD, w, readu8(PS, sfx + k))
|
|
w += 1
|
|
end
|
|
if w == 0 and dist <= 120 then
|
|
return nil, "empty dictionary word"
|
|
end
|
|
if w > remaining then
|
|
return nil, "dictionary word past the meta-block end"
|
|
end
|
|
bcopy(out, op, WORD, 0, w)
|
|
op += w
|
|
remaining -= w
|
|
else
|
|
if cpyLen > remaining then
|
|
return nil, "copy past the meta-block end"
|
|
end
|
|
if push then
|
|
d3, d2, d1, d0 = d2, d1, d0, dist
|
|
end
|
|
local from = op - dist
|
|
if dist >= cpyLen then
|
|
bcopy(out, op, out, from, cpyLen)
|
|
else
|
|
for k = 0, cpyLen - 1 do
|
|
writeu8(out, op + k, readu8(out, from + k))
|
|
end
|
|
end
|
|
op += cpyLen
|
|
remaining -= cpyLen
|
|
end
|
|
end
|
|
if isLast then
|
|
break
|
|
end
|
|
end
|
|
-- the final byte's padding bits must be zero
|
|
local pad = band(bc, 7)
|
|
if pad > 0 and bits(pad) ~= 0 then
|
|
return nil, "nonzero padding at the end"
|
|
end
|
|
ip -= rshift(bc, 3)
|
|
if ip > lim then
|
|
return nil, "truncated stream"
|
|
end
|
|
return out, op, ip
|
|
end
|
|
|
|
return Decode
|