ws-arch2-cab: codecs/compress/Lzx (CAB LZX: windows 2^15..2^21, verbatim / aligned / uncompressed blocks, delta-coded trees, R0..R2, E8 translation, 32 KB frames; written from the spec) + cab LZX folders, checked against cabextract on cabinets of a fixture LZX compressor (tools/fixtures/_lzx.py) and libmspack's samples; Deflate.mszip (generic MSZIP blocks: CK + deflate, dictionary carried across blocks; for cab and later model/x); cab bench lines
This commit is contained in:
1 parent
825c45b03c
commit
c54c8daff5
9 files changed
+990
-49
No files matched your search
@@ -800,8 +800,8 @@ local list: { Entry } = {
|
||||
path = "archive/cab",
|
||||
extensions = { "cab" },
|
||||
mime = { "application/vnd.ms-cab-compressed" },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/compress/Deflate", "containers/Archive" },
|
||||
caps = { decode = true, encode = true, methods = { "store", "mszip" } },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/compress/Deflate", "codecs/compress/Lzx", "containers/Archive" },
|
||||
caps = { decode = true, encode = true, methods = { "store", "mszip", "lzx" } },
|
||||
load = function()
|
||||
return require("@formats/archive/cab")
|
||||
end,
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Lzx = require("@formats/codecs/compress/Lzx")
|
||||
local Archive = require("@formats/containers/Archive")
|
||||
local Sum = require("@formats/archive/cab/Sum")
|
||||
|
||||
@@ -23,8 +24,8 @@ local Cab = {
|
||||
category = "archive",
|
||||
extensions = { "cab" },
|
||||
mime = { "application/vnd.ms-cab-compressed" },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/compress/Deflate", "containers/Archive" },
|
||||
caps = { decode = true, encode = true, methods = { "store", "mszip" } },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/compress/Deflate", "codecs/compress/Lzx", "containers/Archive" },
|
||||
caps = { decode = true, encode = true, methods = { "store", "mszip", "lzx" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
@@ -108,25 +109,28 @@ local function inflateFolder(buf: buffer, list: { Block }, total: number, typ: n
|
||||
end
|
||||
return out, nil
|
||||
elseif method == 1 then
|
||||
local spans: { { offset: number, length: number, size: number } } = {}
|
||||
for _, b in list do
|
||||
table.insert(spans, { offset = b.at, length = b.len, size = b.size })
|
||||
end
|
||||
return Deflate.mszip(buf, spans)
|
||||
elseif method == 3 then
|
||||
local bits = bit32.band(bit32.rshift(typ, 8), 31)
|
||||
local n = 0
|
||||
for _, b in list do
|
||||
n += b.len
|
||||
end
|
||||
local src = buffer.create(n)
|
||||
local o = 0
|
||||
for _, b in list do
|
||||
if b.len < 2 or buffer.readu16(buf, b.at) ~= 0x4B43 then
|
||||
return nil, Err.new(Err.CORRUPT, "MSZIP block without its CK signature", b.at)
|
||||
end
|
||||
local dict: buffer? = nil
|
||||
if o > 0 then
|
||||
local d = math.min(o, BLOCK)
|
||||
dict = buffer.create(d)
|
||||
buffer.copy(dict :: buffer, 0, out, o - d, d)
|
||||
end
|
||||
local part, err = Deflate.inflate(buf, b.at + 2, b.len - 2, b.size, dict)
|
||||
if not part then
|
||||
return nil, err
|
||||
end
|
||||
buffer.copy(out, o, part, 0, b.size)
|
||||
o += b.size
|
||||
buffer.copy(src, o, buf, b.at, b.len)
|
||||
o += b.len
|
||||
end
|
||||
return out, nil
|
||||
local res, err = Lzx.decode(src, 0, n, total, bits)
|
||||
if not res then
|
||||
return nil, Err.from(err)
|
||||
end
|
||||
return res, nil
|
||||
end
|
||||
return nil, Err.new(Err.UNSUPPORTED, "compression type " .. method)
|
||||
end
|
||||
@@ -199,6 +203,9 @@ function Cab.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
}
|
||||
end
|
||||
meta.folders = methods
|
||||
if cFiles == 0 or cFolders == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "a cabinet without files or folders")
|
||||
end
|
||||
if cFiles > maxEntries then
|
||||
return nil, Err.new(Err.LIMIT, "more than maxEntries files")
|
||||
end
|
||||
@@ -307,6 +314,9 @@ function Cab.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if not files then
|
||||
return nil, ferr
|
||||
end
|
||||
if #files == 0 then
|
||||
return nil, Err.new(Err.ARG, "a cabinet needs at least one file")
|
||||
end
|
||||
if #files > 65535 then
|
||||
return nil, Err.new(Err.ARG, "more than 65535 files")
|
||||
end
|
||||
@@ -373,7 +383,7 @@ function Cab.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
offset += buffer.len(f.data)
|
||||
end
|
||||
local fileTable = table_:finish()
|
||||
local nfolders = if #files > 0 then 1 else 0
|
||||
local nfolders = 1
|
||||
local coffFiles = 36 + 8 * nfolders
|
||||
local dataAt = coffFiles + buffer.len(fileTable)
|
||||
local size = dataAt + buffer.len(blob)
|
||||
@@ -391,11 +401,9 @@ function Cab.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
w:u16le(0)
|
||||
w:u16le(o.setId or 0)
|
||||
w:u16le(0)
|
||||
if nfolders == 1 then
|
||||
w:u32le(dataAt)
|
||||
w:u16le(nblocks)
|
||||
w:u16le(if compression == "mszip" then 1 else 0)
|
||||
end
|
||||
w:u32le(dataAt)
|
||||
w:u16le(nblocks)
|
||||
w:u16le(if compression == "mszip" then 1 else 0)
|
||||
w:bytes(fileTable)
|
||||
w:bytes(blob)
|
||||
return w:finish(), nil
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
-- local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
-- local out, err = Deflate.inflate(buf) -- raw deflate -> buffer, consumedEnd | nil, Err
|
||||
-- local out, err = Deflate.inflate(buf, off, len, size) -- a range, with the exact output size if known
|
||||
-- local out, err = Deflate.inflate(buf, off, len, size, dict) -- with a preset dictionary (MSZIP blocks)
|
||||
-- local out, err = Deflate.inflate(buf, off, len, size, dict) -- with a preset dictionary
|
||||
-- local out, err = Deflate.mszip(buf, { { offset, length, size }, ... }) -- MSZIP blocks ("CK" + deflate, chained)
|
||||
-- local out, err = Deflate.zlib(buf) -- checks the Adler-32
|
||||
-- local out, err = Deflate.gunzip(buf) -- checks CRC-32 + ISIZE, concatenates members
|
||||
-- local z = Deflate.compress(buf, { level = 6 }) -- raw deflate; also zlibCompress / gzip
|
||||
@@ -308,6 +309,40 @@ function Deflate.gzip(buf: buffer | string, opts: CompressOptions?): (buffer?, E
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- mszip(buf, blocks) -> buffer | nil, Err: MSZIP data (Microsoft's blocked deflate, as in CAB folders and DirectX .x
|
||||
-- files): blocks = { { offset, length, size } }, each buf[offset, offset + length) = "CK" + a raw deflate stream that
|
||||
-- inflates to exactly size bytes using the previous block's output (up to 32 KB) as its dictionary. Returns the
|
||||
-- blocks' output concatenated.
|
||||
function Deflate.mszip(buf: buffer, blocks: { { offset: number, length: number, size: number } }): (buffer?, Err.Err?)
|
||||
local total = 0
|
||||
for _, b in blocks do
|
||||
if b.offset < 0 or b.length < 0 or b.offset + b.length > buffer.len(buf) or b.size < 0 then
|
||||
return nil, Err.new(Err.ARG, "Deflate.mszip: block out of range")
|
||||
end
|
||||
total += b.size
|
||||
end
|
||||
local out = buffer.create(total)
|
||||
local o = 0
|
||||
for _, b in blocks do
|
||||
if b.length < 2 or buffer.readu16(buf, b.offset) ~= 0x4B43 then
|
||||
return nil, Err.new(Err.CORRUPT, "mszip: block without its CK signature", b.offset)
|
||||
end
|
||||
local dict: buffer? = nil
|
||||
if o > 0 then
|
||||
local d = math.min(o, 32768)
|
||||
dict = buffer.create(d)
|
||||
buffer.copy(dict :: buffer, 0, out, o - d, d)
|
||||
end
|
||||
local part, err = Deflate.inflate(buf, b.offset + 2, b.length - 2, b.size, dict)
|
||||
if not part then
|
||||
return nil, err
|
||||
end
|
||||
buffer.copy(out, o, part, 0, b.size)
|
||||
o += b.size
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- crc32 / adler32 (kept from deflate-luau's API; the same as core/Crc32.of / core/Adler32.of)
|
||||
Deflate.crc32 = function(buf: buffer | string, offset: number?, length: number?, crc: number?): number
|
||||
local b = toBuffer(buf) or buffer.create(0)
|
||||
|
||||
@@ -0,0 +1,449 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/compress/Lzx: the LZX decoder as Microsoft Cabinet folders use it: windows of 2^15 .. 2^21 bytes,
|
||||
-- verbatim, aligned offset and uncompressed blocks (block sizes of 24 bits, blocks spanning frames), the main and
|
||||
-- length trees sent as deltas of the previous block's code lengths through a 20-symbol pre-tree (runs 17 / 18 / 19),
|
||||
-- the three repeated offsets R0..R2, the Intel E8 call translation header (applied to the first 32768 frames, on the
|
||||
-- output only), and the stream cut into frames of 32768 output bytes, each 16-bit aligned at its end; trees, offsets
|
||||
-- and the window persist across the frames (the CFDATA blocks) of a folder. Written from the specification
|
||||
-- (Microsoft's LZX data format documentation in the Cabinet SDK and the LZX sections of [MS-PATCH]); no code from
|
||||
-- libmspack. Not supported: LZX DELTA reference data and reset intervals (CHM / WIM variants).
|
||||
--
|
||||
-- local out, err = Lzx.decode(src, offset, length, outSize, windowBits) -- buffer | nil, message
|
||||
--
|
||||
-- src[offset, offset + length) is the folder's compressed data (the CFDATA payloads concatenated), outSize its exact
|
||||
-- uncompressed size. Never throws: bad or truncated data returns nil, message.
|
||||
local Huffman = require("@formats/codecs/entropy/Huffman")
|
||||
|
||||
local band, bor, lshift, rshift = bit32.band, bit32.bor, bit32.lshift, bit32.rshift
|
||||
local readu8, readu16, readu32, readi32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi32
|
||||
local writeu8, writei32 = buffer.writeu8, buffer.writei32
|
||||
local bcreate, bcopy, blen = buffer.create, buffer.copy, buffer.len
|
||||
|
||||
local Lzx = {}
|
||||
|
||||
local FRAME = 32768
|
||||
local MAINP, LENP, ALIGNP, PREP = 10, 8, 7, 6
|
||||
local MAXMAIN = 256 + 8 * 50
|
||||
local NLEN = 249
|
||||
local SAFETY = 64 -- pre-tree runs may run past the end of a tree: room for them (as the format's reference decoder)
|
||||
|
||||
-- position slots: footer bits and base of each formatted offset slot
|
||||
local EXTRA = table.create(51, 0)
|
||||
local BASE = table.create(51, 0)
|
||||
do
|
||||
local b = 0
|
||||
for i = 0, 50 do
|
||||
local e = if i < 4 then 0 else math.min(i // 2 - 1, 17)
|
||||
EXTRA[i + 1] = e
|
||||
BASE[i + 1] = b
|
||||
b += lshift(1, e)
|
||||
end
|
||||
end
|
||||
local SLOTS: { [number]: number } = { [15] = 30, [16] = 32, [17] = 34, [18] = 36, [19] = 38, [20] = 42, [21] = 50 }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ bit reader
|
||||
-- 16-bit little-endian words read most significant bit first; bb holds cnt bits left aligned (cnt <= 32). Past the
|
||||
-- end, zero words are read and over counts their bits.
|
||||
type State = { src: buffer, pos: number, stop: number, bb: number, cnt: number, over: number }
|
||||
|
||||
local function refill(s: State)
|
||||
while s.cnt <= 16 do
|
||||
local p = s.pos
|
||||
local w = 0
|
||||
if p + 2 <= s.stop then
|
||||
w = readu16(s.src, p)
|
||||
s.pos = p + 2
|
||||
elseif p < s.stop then
|
||||
w = readu8(s.src, p)
|
||||
s.pos = p + 1
|
||||
s.over += 8
|
||||
else
|
||||
s.over += 16
|
||||
end
|
||||
s.bb = bor(s.bb, lshift(w, 16 - s.cnt))
|
||||
s.cnt += 16
|
||||
end
|
||||
end
|
||||
|
||||
-- bits(s, n) -> the next n (1..17) bits
|
||||
local function bits(s: State, n: number): number
|
||||
if s.cnt < n then
|
||||
refill(s)
|
||||
end
|
||||
local v = rshift(s.bb, 32 - n)
|
||||
s.bb = lshift(s.bb, n)
|
||||
s.cnt -= n
|
||||
return v
|
||||
end
|
||||
|
||||
-- sym(s, tab, P, S) -> the next symbol of an MSB Huffman table, -1 for an invalid code
|
||||
local function sym(s: State, tab: buffer, P: number, S: number): number
|
||||
if s.cnt < 16 then
|
||||
refill(s)
|
||||
end
|
||||
local e = readu32(tab, rshift(s.bb, 32 - P) * 4)
|
||||
local l = band(e, 31)
|
||||
if l == 0 then
|
||||
if e == 0 then
|
||||
return -1
|
||||
end
|
||||
e = readu32(tab, (rshift(e, 5) + band(rshift(s.bb, 32 - P - S), lshift(1, S) - 1)) * 4)
|
||||
l = band(e, 31)
|
||||
if l == 0 then
|
||||
return -1
|
||||
end
|
||||
end
|
||||
s.bb = lshift(s.bb, l)
|
||||
s.cnt -= l
|
||||
return rshift(e, 5)
|
||||
end
|
||||
|
||||
-- readLens: the code lengths lens[first, last) as deltas of their previous values, through a fresh pre-tree
|
||||
local function readLens(s: State, lens: buffer, first: number, last: number, pre: buffer, preLens: buffer): boolean
|
||||
for i = 0, 19 do
|
||||
writeu8(preLens, i, bits(s, 4))
|
||||
end
|
||||
local S = Huffman.buildMsb(preLens, 0, 20, pre, PREP)
|
||||
if S < 0 then
|
||||
return false
|
||||
end
|
||||
local cap = blen(lens)
|
||||
local x = first
|
||||
while x < last do
|
||||
local z = sym(s, pre, PREP, S)
|
||||
if z < 0 then
|
||||
return false
|
||||
end
|
||||
if z == 17 or z == 18 then
|
||||
local run = if z == 17 then bits(s, 4) + 4 else bits(s, 5) + 20
|
||||
for _ = 1, run do
|
||||
if x < cap then
|
||||
writeu8(lens, x, 0)
|
||||
end
|
||||
x += 1
|
||||
end
|
||||
elseif z == 19 then
|
||||
local run = bits(s, 1) + 4
|
||||
local d = sym(s, pre, PREP, S)
|
||||
if d < 0 or d > 16 then
|
||||
return false
|
||||
end
|
||||
local v = (readu8(lens, x) - d + 17) % 17
|
||||
for _ = 1, run do
|
||||
if x < cap then
|
||||
writeu8(lens, x, v)
|
||||
end
|
||||
x += 1
|
||||
end
|
||||
else
|
||||
writeu8(lens, x, (readu8(lens, x) - z + 17) % 17)
|
||||
x += 1
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- E8 translation of the decoded output, frame by frame (absolute call targets back to relative)
|
||||
local function translate(out: buffer, n: number, size: number)
|
||||
local base = 0
|
||||
local frame = 0
|
||||
while base < n and frame < 32768 do
|
||||
local fs = math.min(FRAME, n - base)
|
||||
if fs > 10 then
|
||||
local i = base
|
||||
local stop = base + fs - 10
|
||||
while i < stop do
|
||||
if readu8(out, i) == 0xE8 then
|
||||
local abs = readi32(out, i + 1)
|
||||
if abs >= -i and abs < size then
|
||||
writei32(out, i + 1, if abs >= 0 then abs - i else abs + size)
|
||||
end
|
||||
i += 5
|
||||
else
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
base += fs
|
||||
frame += 1
|
||||
end
|
||||
end
|
||||
|
||||
function Lzx.decode(src: buffer, offset: number, length: number, outSize: number, windowBits: number): (buffer?, string?)
|
||||
local nslots = SLOTS[windowBits]
|
||||
if not nslots then
|
||||
return nil, "unsupported: LZX window 2^" .. tostring(windowBits)
|
||||
end
|
||||
if offset < 0 or length < 0 or offset + length > blen(src) or outSize < 0 or outSize % 1 ~= 0 then
|
||||
return nil, "bad arguments"
|
||||
end
|
||||
local windowSize = lshift(1, windowBits)
|
||||
local mainN = 256 + 8 * nslots
|
||||
local out = bcreate(outSize)
|
||||
local mainLens, lenLens, alignLens, preLens = bcreate(MAXMAIN + SAFETY), bcreate(NLEN + SAFETY), bcreate(8), bcreate(20)
|
||||
local mainTab = bcreate(Huffman.capacity(MAXMAIN, MAINP, 16) * 4)
|
||||
local lenTab = bcreate(Huffman.capacity(NLEN, LENP, 16) * 4)
|
||||
local alignTab = bcreate(Huffman.capacity(8, ALIGNP, 16) * 4)
|
||||
local preTab = bcreate(Huffman.capacity(20, PREP, 16) * 4)
|
||||
local s: State = { src = src, pos = offset, stop = offset + length, bb = 0, cnt = 0, over = 0 }
|
||||
local R0: number, R1: number, R2: number = 1, 1, 1
|
||||
local intel = 0
|
||||
local started = false
|
||||
local btype, bremain, bsize = 0, 0, 0
|
||||
local mainS, lenS, alignS = 0, 0, 0
|
||||
local op = 0
|
||||
while op < outSize do
|
||||
local frameEnd = math.min(op + FRAME, outSize)
|
||||
while op < frameEnd do
|
||||
if bremain == 0 then
|
||||
-- a new block
|
||||
if btype == 3 and band(bsize, 1) == 1 then
|
||||
s.pos += 1
|
||||
end
|
||||
if not started then
|
||||
started = true
|
||||
if bits(s, 1) == 1 then
|
||||
local hi = bits(s, 16)
|
||||
intel = hi * 65536 + bits(s, 16)
|
||||
end
|
||||
end
|
||||
btype = bits(s, 3)
|
||||
local hi = bits(s, 16)
|
||||
bsize = hi * 256 + bits(s, 8)
|
||||
bremain = bsize
|
||||
if s.over > s.cnt then
|
||||
return nil, "truncated input"
|
||||
end
|
||||
if bsize == 0 then
|
||||
return nil, "empty block"
|
||||
end
|
||||
if btype == 2 then
|
||||
for i = 0, 7 do
|
||||
writeu8(alignLens, i, bits(s, 3))
|
||||
end
|
||||
alignS = Huffman.buildMsb(alignLens, 0, 8, alignTab, ALIGNP)
|
||||
if alignS < 0 then
|
||||
return nil, "bad aligned offset tree"
|
||||
end
|
||||
end
|
||||
if btype == 1 or btype == 2 then
|
||||
if not readLens(s, mainLens, 0, 256, preTab, preLens) or not readLens(s, mainLens, 256, mainN, preTab, preLens) then
|
||||
return nil, "bad main tree"
|
||||
end
|
||||
mainS = Huffman.buildMsb(mainLens, 0, mainN, mainTab, MAINP)
|
||||
if mainS < 0 then
|
||||
return nil, "bad main tree"
|
||||
end
|
||||
if not readLens(s, lenLens, 0, NLEN, preTab, preLens) then
|
||||
return nil, "bad length tree"
|
||||
end
|
||||
lenS = Huffman.buildMsb(lenLens, 0, NLEN, lenTab, LENP)
|
||||
if lenS < 0 then
|
||||
return nil, "bad length tree"
|
||||
end
|
||||
elseif btype == 3 then
|
||||
-- 1..16 bits of padding to a 16-bit boundary, then R0..R2 and the bytes
|
||||
local k = s.cnt % 16
|
||||
if k == 0 then
|
||||
k = 16
|
||||
end
|
||||
local p = s.pos - (s.cnt - k) // 8
|
||||
s.bb, s.cnt = 0, 0
|
||||
if s.over > 0 or p + 12 > s.stop then
|
||||
return nil, "truncated uncompressed block"
|
||||
end
|
||||
R0 = readu32(src, p)
|
||||
R1 = readu32(src, p + 4)
|
||||
R2 = readu32(src, p + 8)
|
||||
s.pos = p + 12
|
||||
else
|
||||
return nil, "bad block type " .. btype
|
||||
end
|
||||
if s.over > s.cnt then
|
||||
return nil, "truncated input"
|
||||
end
|
||||
end
|
||||
local runEnd = op + math.min(bremain, frameEnd - op)
|
||||
if btype == 3 then
|
||||
local n = runEnd - op
|
||||
local p = s.pos
|
||||
if p + n > s.stop then
|
||||
return nil, "truncated uncompressed block"
|
||||
end
|
||||
bcopy(out, op, src, p, n)
|
||||
s.pos = p + n
|
||||
op += n
|
||||
bremain -= n
|
||||
else
|
||||
local bb, cnt, pos, over = s.bb, s.cnt, s.pos, s.over
|
||||
local stop = s.stop
|
||||
local aligned = btype == 2
|
||||
local start = op
|
||||
while op < runEnd do
|
||||
-- refill to at least 17 bits
|
||||
while cnt <= 16 do
|
||||
local w = 0
|
||||
if pos + 2 <= stop then
|
||||
w = readu16(src, pos)
|
||||
pos += 2
|
||||
elseif pos < stop then
|
||||
w = readu8(src, pos)
|
||||
pos += 1
|
||||
over += 8
|
||||
else
|
||||
over += 16
|
||||
end
|
||||
bb = bor(bb, lshift(w, 16 - cnt))
|
||||
cnt += 16
|
||||
end
|
||||
local e = readu32(mainTab, rshift(bb, 32 - MAINP) * 4)
|
||||
local l = band(e, 31)
|
||||
if l == 0 then
|
||||
if e == 0 then
|
||||
return nil, "bad main code"
|
||||
end
|
||||
e = readu32(mainTab, (rshift(e, 5) + band(rshift(bb, 32 - MAINP - mainS), lshift(1, mainS) - 1)) * 4)
|
||||
l = band(e, 31)
|
||||
if l == 0 then
|
||||
return nil, "bad main code"
|
||||
end
|
||||
end
|
||||
bb = lshift(bb, l)
|
||||
cnt -= l
|
||||
local m = rshift(e, 5)
|
||||
if m < 256 then
|
||||
writeu8(out, op, m)
|
||||
op += 1
|
||||
else
|
||||
m -= 256
|
||||
local len = band(m, 7)
|
||||
if len == 7 then
|
||||
while cnt <= 16 do
|
||||
local w = 0
|
||||
if pos + 2 <= stop then
|
||||
w = readu16(src, pos)
|
||||
pos += 2
|
||||
else
|
||||
over += 16
|
||||
end
|
||||
bb = bor(bb, lshift(w, 16 - cnt))
|
||||
cnt += 16
|
||||
end
|
||||
local le = readu32(lenTab, rshift(bb, 32 - LENP) * 4)
|
||||
local ll = band(le, 31)
|
||||
if ll == 0 then
|
||||
if le == 0 then
|
||||
return nil, "bad length code"
|
||||
end
|
||||
le = readu32(lenTab, (rshift(le, 5) + band(rshift(bb, 32 - LENP - lenS), lshift(1, lenS) - 1)) * 4)
|
||||
ll = band(le, 31)
|
||||
if ll == 0 then
|
||||
return nil, "bad length code"
|
||||
end
|
||||
end
|
||||
bb = lshift(bb, ll)
|
||||
cnt -= ll
|
||||
len += rshift(le, 5)
|
||||
end
|
||||
len += 2
|
||||
local slot = rshift(m, 3)
|
||||
local dist: number
|
||||
if slot == 0 then
|
||||
dist = R0
|
||||
elseif slot == 1 then
|
||||
dist = R1
|
||||
R1 = R0
|
||||
R0 = dist
|
||||
elseif slot == 2 then
|
||||
dist = R2
|
||||
R2 = R0
|
||||
R0 = dist
|
||||
else
|
||||
local ex = EXTRA[slot + 1]
|
||||
dist = BASE[slot + 1] - 2
|
||||
if ex > 0 then
|
||||
while cnt <= 16 do
|
||||
local w = 0
|
||||
if pos + 2 <= stop then
|
||||
w = readu16(src, pos)
|
||||
pos += 2
|
||||
else
|
||||
over += 16
|
||||
end
|
||||
bb = bor(bb, lshift(w, 16 - cnt))
|
||||
cnt += 16
|
||||
end
|
||||
if aligned and ex >= 3 then
|
||||
if ex > 3 then
|
||||
dist += lshift(rshift(bb, 35 - ex), 3)
|
||||
bb = lshift(bb, ex - 3)
|
||||
cnt -= ex - 3
|
||||
while cnt <= 16 do
|
||||
local w = 0
|
||||
if pos + 2 <= stop then
|
||||
w = readu16(src, pos)
|
||||
pos += 2
|
||||
else
|
||||
over += 16
|
||||
end
|
||||
bb = bor(bb, lshift(w, 16 - cnt))
|
||||
cnt += 16
|
||||
end
|
||||
end
|
||||
local ae = readu32(alignTab, rshift(bb, 32 - ALIGNP) * 4)
|
||||
local al = band(ae, 31)
|
||||
if al == 0 then
|
||||
return nil, "bad aligned offset code"
|
||||
end
|
||||
bb = lshift(bb, al)
|
||||
cnt -= al
|
||||
dist += rshift(ae, 5)
|
||||
else
|
||||
dist += rshift(bb, 32 - ex)
|
||||
bb = lshift(bb, ex)
|
||||
cnt -= ex
|
||||
end
|
||||
end
|
||||
R2 = R1
|
||||
R1 = R0
|
||||
R0 = dist
|
||||
end
|
||||
if dist < 1 or dist > op or dist > windowSize then
|
||||
return nil, "match offset before the start of the data"
|
||||
end
|
||||
if op + len > runEnd then
|
||||
return nil, "match past the end of its block or frame"
|
||||
end
|
||||
local from = op - dist
|
||||
if dist >= len then
|
||||
bcopy(out, op, out, from, len)
|
||||
else
|
||||
for k = 0, len - 1 do
|
||||
writeu8(out, op + k, readu8(out, from + k))
|
||||
end
|
||||
end
|
||||
op += len
|
||||
end
|
||||
end
|
||||
s.bb, s.cnt, s.pos, s.over = bb, cnt, pos, over
|
||||
bremain -= op - start
|
||||
end
|
||||
end
|
||||
-- the end of a frame: realign to 16 bits
|
||||
local k = s.cnt % 16
|
||||
s.bb = lshift(s.bb, k)
|
||||
s.cnt -= k
|
||||
if s.over > s.cnt then
|
||||
return nil, "truncated input"
|
||||
end
|
||||
end
|
||||
if intel ~= 0 then
|
||||
translate(out, outSize, intel)
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
return Lzx
|
||||
Reference in new issue
Block a user