Shared change: codecs/compress/Deflate gains Deflate.peek(buf, off, len, n) (the first n inflated bytes of a gzip / zlib / raw stream, for sniffing gzip-wrapped NBT) through a backward-compatible prefix flag on Inflate.raw; gunzip's header parsing moved into a local helper both use.
446 lines
13 KiB
Luau
446 lines
13 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/compress/Deflate/Inflate: raw DEFLATE (RFC 1951) decoder: stored, fixed-Huffman and dynamic-Huffman blocks.
|
|
-- Absorbed from deflate-luau src/Deflate/Inflate.luau (same author); the table builder moved to codecs/entropy/Huffman, whose
|
|
-- entries are symbol * 32 + length (5 length bits instead of 4).
|
|
--
|
|
-- Bits are read LSB first into a 32-bit bit buffer (refilled 4 bytes at a time with one readu32 while the input
|
|
-- lasts). Huffman codes decode through a codecs/entropy/Huffman LSB table indexed by the next LITBITS / DISTBITS bits; a longer
|
|
-- code's primary slot links to a subtable, so a symbol is one or two table reads.
|
|
--
|
|
-- Nothing in this module throws on any input: every buffer read and write is bounds-checked, and corrupt data
|
|
-- returns nil, message. (Native code must never throw: it crashes Roblox Studio.)
|
|
local Huffman = require("@formats/codecs/entropy/Huffman")
|
|
|
|
local band, bor, bxor, lshift, rshift = bit32.band, bit32.bor, bit32.bxor, bit32.lshift, bit32.rshift
|
|
local readu8, readu16, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu32
|
|
local bcreate, bcopy, bfill, blen = buffer.create, buffer.copy, buffer.fill, buffer.len
|
|
|
|
local LITBITS, DISTBITS, CLBITS = 10, 8, 7
|
|
local LITMASK, DISTMASK, CLMASK = 1023, 255, 127
|
|
|
|
-- length codes 257..285 and distance codes 0..29: base value, extra bits
|
|
local LBASE = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 }
|
|
local LEXT = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 }
|
|
local DBASE = {
|
|
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097,
|
|
6145, 8193, 12289, 16385, 24577,
|
|
}
|
|
local DEXT = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 }
|
|
-- the order code-length code lengths are sent in
|
|
local CLORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }
|
|
|
|
-- table capacities (entries): primary + one subtable per code longer than the primary width, worst case
|
|
local LITCAP = Huffman.capacity(288, LITBITS, 15)
|
|
local DISTCAP = Huffman.capacity(32, DISTBITS, 15)
|
|
local CLCAP = Huffman.capacity(19, CLBITS, 7)
|
|
|
|
-- fixed Huffman tables
|
|
local FLIT, FDIST = bcreate(LITCAP * 4), bcreate(DISTCAP * 4)
|
|
local FLITS: number, FDISTS: number
|
|
do
|
|
local l = bcreate(288)
|
|
bfill(l, 0, 8, 144)
|
|
bfill(l, 144, 9, 112)
|
|
bfill(l, 256, 7, 24)
|
|
bfill(l, 280, 8, 8)
|
|
FLITS = Huffman.buildLsb(l, 0, 288, FLIT, LITBITS)
|
|
local d = bcreate(30)
|
|
bfill(d, 0, 5, 30)
|
|
FDISTS = Huffman.buildLsb(d, 0, 30, FDIST, DISTBITS)
|
|
end
|
|
|
|
local Inflate = {}
|
|
|
|
-- Grow out (capacity cap) to hold at least need bytes. Returns the new buffer and capacity.
|
|
local function grow(out: buffer, op: number, need: number): (buffer, number)
|
|
local cap = math.max(blen(out) * 2, need, 1024)
|
|
local nb = bcreate(cap)
|
|
bcopy(nb, 0, out, 0, op)
|
|
return nb, cap
|
|
end
|
|
|
|
-- Inflate src[start, endpos) into a buffer. cap: initial output capacity; exact: true = the output must fit in
|
|
-- cap bytes (an error otherwise), false = grow as needed. prefix (with exact): stop at cap instead of failing and
|
|
-- return what was inflated so far (at most cap bytes; Deflate.peek, for sniffing).
|
|
-- Returns out, outLen, consumedEnd (the input offset just past the final block) or nil, message.
|
|
function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, exact: boolean, prefix: boolean?): (buffer?, any, number?)
|
|
local out = bcreate(cap)
|
|
local op = 0
|
|
local pos = start
|
|
local bitbuf, bitcnt, over = 0, 0, 0
|
|
local lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer
|
|
local clt = bcreate(CLCAP * 4)
|
|
local lens = bcreate(320)
|
|
local final = 0
|
|
repeat
|
|
-- block header (3 bits); also up to 14 bits of a dynamic header
|
|
if bitcnt < 17 then
|
|
if pos + 4 <= endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt))
|
|
local nb = rshift(32 - bitcnt, 3)
|
|
pos += nb
|
|
bitcnt += nb * 8
|
|
else
|
|
while bitcnt <= 24 do
|
|
if pos < endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
|
|
pos += 1
|
|
else
|
|
over += 1
|
|
if over > 4 then
|
|
return nil, "truncated input"
|
|
end
|
|
end
|
|
bitcnt += 8
|
|
end
|
|
end
|
|
end
|
|
final = band(bitbuf, 1)
|
|
local btype = band(rshift(bitbuf, 1), 3)
|
|
bitbuf = rshift(bitbuf, 3)
|
|
bitcnt -= 3
|
|
if btype == 0 then
|
|
-- stored: skip to the byte boundary, give back the whole bytes still in the bit buffer
|
|
local k = band(bitcnt, 7)
|
|
bitcnt -= k
|
|
local real = rshift(bitcnt, 3) - over
|
|
if real < 0 then
|
|
return nil, "truncated input"
|
|
end
|
|
pos -= real
|
|
bitbuf, bitcnt, over = 0, 0, 0
|
|
if pos + 4 > endpos then
|
|
return nil, "truncated input"
|
|
end
|
|
local n = readu16(src, pos)
|
|
if n ~= bxor(readu16(src, pos + 2), 0xFFFF) then
|
|
return nil, "stored block length mismatch"
|
|
end
|
|
pos += 4
|
|
if pos + n > endpos then
|
|
return nil, "truncated input"
|
|
end
|
|
if op + n > cap then
|
|
if exact then
|
|
if prefix then
|
|
return out, op, pos
|
|
end
|
|
return nil, "output larger than expected"
|
|
end
|
|
out, cap = grow(out, op, op + n)
|
|
end
|
|
bcopy(out, op, src, pos, n)
|
|
op += n
|
|
pos += n
|
|
continue
|
|
end
|
|
local lsub: number, dsub: number
|
|
if btype == 1 then
|
|
lt, dt = FLIT, FDIST
|
|
lsub, dsub = FLITS, FDISTS
|
|
elseif btype == 2 then
|
|
if lt == FLIT then
|
|
lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer
|
|
end
|
|
local hlit = band(bitbuf, 31) + 257
|
|
local hdist = band(rshift(bitbuf, 5), 31) + 1
|
|
local hclen = band(rshift(bitbuf, 10), 15) + 4
|
|
bitbuf = rshift(bitbuf, 14)
|
|
bitcnt -= 14
|
|
if hlit > 286 or hdist > 30 then
|
|
return nil, "too many length or distance symbols"
|
|
end
|
|
bfill(lens, 0, 0, 19)
|
|
for i = 1, hclen do
|
|
if bitcnt < 3 then
|
|
if pos + 4 <= endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt))
|
|
local nb = rshift(32 - bitcnt, 3)
|
|
pos += nb
|
|
bitcnt += nb * 8
|
|
else
|
|
while bitcnt <= 24 do
|
|
if pos < endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
|
|
pos += 1
|
|
else
|
|
over += 1
|
|
if over > 4 then
|
|
return nil, "truncated input"
|
|
end
|
|
end
|
|
bitcnt += 8
|
|
end
|
|
end
|
|
end
|
|
writeu8(lens, CLORDER[i], band(bitbuf, 7))
|
|
bitbuf = rshift(bitbuf, 3)
|
|
bitcnt -= 3
|
|
end
|
|
if Huffman.buildLsb(lens, 0, 19, clt, CLBITS) < 0 then
|
|
return nil, "invalid code lengths set"
|
|
end
|
|
local total = hlit + hdist
|
|
local i = 0
|
|
while i < total do
|
|
if bitcnt < 14 then
|
|
if pos + 4 <= endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt))
|
|
local nb = rshift(32 - bitcnt, 3)
|
|
pos += nb
|
|
bitcnt += nb * 8
|
|
else
|
|
while bitcnt <= 24 do
|
|
if pos < endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
|
|
pos += 1
|
|
else
|
|
over += 1
|
|
if over > 4 then
|
|
return nil, "truncated input"
|
|
end
|
|
end
|
|
bitcnt += 8
|
|
end
|
|
end
|
|
end
|
|
local e = readu32(clt, band(bitbuf, CLMASK) * 4)
|
|
local l = band(e, 31)
|
|
if l == 0 then
|
|
return nil, "invalid code lengths set"
|
|
end
|
|
bitbuf = rshift(bitbuf, l)
|
|
bitcnt -= l
|
|
local sym = rshift(e, 5)
|
|
if sym < 16 then
|
|
writeu8(lens, i, sym)
|
|
i += 1
|
|
else
|
|
local rep, val
|
|
if sym == 16 then
|
|
if i == 0 then
|
|
return nil, "repeat with no first length"
|
|
end
|
|
val = readu8(lens, i - 1)
|
|
rep = 3 + band(bitbuf, 3)
|
|
bitbuf = rshift(bitbuf, 2)
|
|
bitcnt -= 2
|
|
elseif sym == 17 then
|
|
val = 0
|
|
rep = 3 + band(bitbuf, 7)
|
|
bitbuf = rshift(bitbuf, 3)
|
|
bitcnt -= 3
|
|
else
|
|
val = 0
|
|
rep = 11 + band(bitbuf, 127)
|
|
bitbuf = rshift(bitbuf, 7)
|
|
bitcnt -= 7
|
|
end
|
|
if i + rep > total then
|
|
return nil, "too many code lengths"
|
|
end
|
|
bfill(lens, i, val, rep)
|
|
i += rep
|
|
end
|
|
end
|
|
if readu8(lens, 256) == 0 then
|
|
return nil, "missing end-of-block code"
|
|
end
|
|
lsub = Huffman.buildLsb(lens, 0, hlit, lt, LITBITS)
|
|
dsub = Huffman.buildLsb(lens, hlit, hdist, dt, DISTBITS)
|
|
if lsub < 0 or dsub < 0 then
|
|
return nil, "invalid literal/length or distance code set"
|
|
end
|
|
else
|
|
return nil, "invalid block type"
|
|
end
|
|
local lsmask = lshift(1, lsub) - 1
|
|
local dsmask = lshift(1, dsub) - 1
|
|
-- the symbol loop
|
|
while true do
|
|
if bitcnt < 15 then
|
|
if pos + 4 <= endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt))
|
|
local nb = rshift(32 - bitcnt, 3)
|
|
pos += nb
|
|
bitcnt += nb * 8
|
|
else
|
|
while bitcnt <= 24 do
|
|
if pos < endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
|
|
pos += 1
|
|
else
|
|
over += 1
|
|
if over > 4 then
|
|
return nil, "truncated input"
|
|
end
|
|
end
|
|
bitcnt += 8
|
|
end
|
|
end
|
|
end
|
|
local e = readu32(lt, band(bitbuf, LITMASK) * 4)
|
|
local l = band(e, 31)
|
|
if l == 0 then
|
|
if e == 0 then
|
|
return nil, "invalid literal/length code"
|
|
end
|
|
e = readu32(lt, (rshift(e, 5) + band(rshift(bitbuf, LITBITS), lsmask)) * 4)
|
|
l = band(e, 31)
|
|
if l == 0 then
|
|
return nil, "invalid literal/length code"
|
|
end
|
|
end
|
|
bitbuf = rshift(bitbuf, l)
|
|
bitcnt -= l
|
|
local sym = rshift(e, 5)
|
|
if sym < 256 then
|
|
if op >= cap then
|
|
if exact then
|
|
if prefix then
|
|
return out, op, pos
|
|
end
|
|
return nil, "output larger than expected"
|
|
end
|
|
out, cap = grow(out, op, op + 1)
|
|
end
|
|
writeu8(out, op, sym)
|
|
op += 1
|
|
elseif sym == 256 then
|
|
break
|
|
else
|
|
sym -= 256
|
|
if sym > 29 then
|
|
return nil, "invalid literal/length code"
|
|
end
|
|
-- up to 5 length extra bits + a 15-bit distance code
|
|
if bitcnt < 20 then
|
|
if pos + 4 <= endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt))
|
|
local nb = rshift(32 - bitcnt, 3)
|
|
pos += nb
|
|
bitcnt += nb * 8
|
|
else
|
|
while bitcnt <= 24 do
|
|
if pos < endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
|
|
pos += 1
|
|
else
|
|
over += 1
|
|
if over > 4 then
|
|
return nil, "truncated input"
|
|
end
|
|
end
|
|
bitcnt += 8
|
|
end
|
|
end
|
|
end
|
|
local len = LBASE[sym]
|
|
local xb = LEXT[sym]
|
|
if xb > 0 then
|
|
len += band(bitbuf, lshift(1, xb) - 1)
|
|
bitbuf = rshift(bitbuf, xb)
|
|
bitcnt -= xb
|
|
end
|
|
e = readu32(dt, band(bitbuf, DISTMASK) * 4)
|
|
l = band(e, 31)
|
|
if l == 0 then
|
|
if e == 0 then
|
|
return nil, "invalid distance code"
|
|
end
|
|
e = readu32(dt, (rshift(e, 5) + band(rshift(bitbuf, DISTBITS), dsmask)) * 4)
|
|
l = band(e, 31)
|
|
if l == 0 then
|
|
return nil, "invalid distance code"
|
|
end
|
|
end
|
|
bitbuf = rshift(bitbuf, l)
|
|
bitcnt -= l
|
|
local dsym = rshift(e, 5) + 1
|
|
if dsym > 30 then
|
|
return nil, "invalid distance code"
|
|
end
|
|
local dist = DBASE[dsym]
|
|
xb = DEXT[dsym]
|
|
if xb > 0 then
|
|
if bitcnt < 13 then
|
|
if pos + 4 <= endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu32(src, pos), bitcnt))
|
|
local nb = rshift(32 - bitcnt, 3)
|
|
pos += nb
|
|
bitcnt += nb * 8
|
|
else
|
|
while bitcnt <= 24 do
|
|
if pos < endpos then
|
|
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
|
|
pos += 1
|
|
else
|
|
over += 1
|
|
if over > 4 then
|
|
return nil, "truncated input"
|
|
end
|
|
end
|
|
bitcnt += 8
|
|
end
|
|
end
|
|
end
|
|
dist += band(bitbuf, lshift(1, xb) - 1)
|
|
bitbuf = rshift(bitbuf, xb)
|
|
bitcnt -= xb
|
|
end
|
|
if dist > op then
|
|
return nil, "invalid distance too far back"
|
|
end
|
|
if op + len > cap then
|
|
if exact then
|
|
if prefix then
|
|
return out, op, pos
|
|
end
|
|
return nil, "output larger than expected"
|
|
end
|
|
out, cap = grow(out, op, op + len)
|
|
end
|
|
local s = op - dist
|
|
if dist >= len then
|
|
if len <= 16 and op + 16 <= cap then
|
|
-- straight-line 16-byte copy (the bytes past len are rewritten later): no inner loop, which
|
|
-- keeps LuaJIT's traces simple; chunks read only bytes before op or already copied ones
|
|
writeu32(out, op, readu32(out, s))
|
|
writeu32(out, op + 4, readu32(out, s + 4))
|
|
writeu32(out, op + 8, readu32(out, s + 8))
|
|
writeu32(out, op + 12, readu32(out, s + 12))
|
|
elseif len <= 16 then
|
|
for k = 0, len - 1 do
|
|
writeu8(out, op + k, readu8(out, s + k))
|
|
end
|
|
else
|
|
bcopy(out, op, out, s, len)
|
|
end
|
|
op += len
|
|
elseif dist == 1 then
|
|
bfill(out, op, readu8(out, s), len)
|
|
op += len
|
|
else
|
|
-- overlapping: the pattern of period dist, copied in growing non-overlapping chunks
|
|
local rem = len
|
|
while rem > 0 do
|
|
local k = math.min(op - s, rem)
|
|
bcopy(out, op, out, s, k)
|
|
op += k
|
|
rem -= k
|
|
end
|
|
end
|
|
end
|
|
end
|
|
until final == 1
|
|
local consumed = pos + over - rshift(bitcnt, 3)
|
|
if consumed > endpos then
|
|
return nil, "truncated input"
|
|
end
|
|
return out, op, consumed
|
|
end
|
|
|
|
return Inflate
|