ws-arch2-cab: archive/cab (MSCF read: stored / MSZIP folders with the carried-over deflate dictionary, reserve areas, checksums, attributes, UTF-8 names, spanned files UNSUPPORTED; write: one stored / MSZIP folder) vs cabextract / gcab / 7z; Deflate.inflate gains an optional preset dictionary (5th argument, Inflate.raw 7th; backward compatible)
This commit is contained in:
1 parent
53b41c7275
commit
825c45b03c
10 files changed
+851
-4
No files matched your search
@@ -794,6 +794,18 @@ local list: { Entry } = {
|
||||
return require("@formats/archive/bz2")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "cab",
|
||||
category = "archive",
|
||||
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" } },
|
||||
load = function()
|
||||
return require("@formats/archive/cab")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "cpio",
|
||||
category = "archive",
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- archive/cab/Sum: the cabinet data block checksum (the XOR of the data's little-endian 32-bit words, the last 1..3
|
||||
-- bytes packed most significant first, then the block's cbData / cbUncomp word). Written from the CAB format
|
||||
-- description ([MS-CAB] 2.4, CFDATA.csum).
|
||||
|
||||
local readu8, readu32, blen = buffer.readu8, buffer.readu32, buffer.len
|
||||
local bxor, bor, lshift = bit32.bxor, bit32.bor, bit32.lshift
|
||||
|
||||
local Sum = {}
|
||||
|
||||
-- of(buf, off, len, seed) -> the checksum of buf[off, off + len) continuing from seed (0 for a new sum)
|
||||
function Sum.of(buf: buffer, off: number, len: number, seed: number): number
|
||||
if off < 0 or len <= 0 or off + len > blen(buf) then
|
||||
return seed
|
||||
end
|
||||
local s = seed
|
||||
local p = off
|
||||
local stop = off + len - len % 4
|
||||
while p < stop do
|
||||
s = bxor(s, readu32(buf, p))
|
||||
p += 4
|
||||
end
|
||||
local rem = len % 4
|
||||
local tail = 0
|
||||
if rem == 3 then
|
||||
tail = bor(lshift(readu8(buf, p), 16), lshift(readu8(buf, p + 1), 8), readu8(buf, p + 2))
|
||||
elseif rem == 2 then
|
||||
tail = bor(lshift(readu8(buf, p), 8), readu8(buf, p + 1))
|
||||
elseif rem == 1 then
|
||||
tail = readu8(buf, p)
|
||||
end
|
||||
return bxor(s, tail)
|
||||
end
|
||||
|
||||
-- block(buf, dataAt, cbData, cbUncomp) -> the csum field of a CFDATA block with these sizes over buf[dataAt, +cbData)
|
||||
function Sum.block(buf: buffer, dataAt: number, cbData: number, cbUncomp: number): number
|
||||
return bxor(Sum.of(buf, dataAt, cbData, 0), cbData + cbUncomp * 65536)
|
||||
end
|
||||
|
||||
return Sum
|
||||
@@ -0,0 +1,412 @@
|
||||
--!strict
|
||||
-- archive/cab: Microsoft Cabinet files (MSCF 1.3) read into an Archive: the header with its reserve areas and
|
||||
-- cabinet set fields, folders (CFFOLDER) compressed with none, MSZIP (each CFDATA block "CK" + raw deflate whose
|
||||
-- dictionary carries over from the block before), LZX (codecs/compress/Lzx, windows 2^15..2^21) or Quantum
|
||||
-- (codecs/compress/Quantum, windows 2^10..2^21), the files (CFFILE) with their MS-DOS attributes, DOS times (local
|
||||
-- time without a zone, read as UTC) and the UTF-8 name flag; every data block's checksum is verified (Err.CHECKSUM)
|
||||
-- when a file of its folder is read. A folder is decompressed once per read and kept for the next file of the same
|
||||
-- folder. Writes one folder, stored or MSZIP (independent 32 KB deflate blocks), from any Archive. Written from the
|
||||
-- Microsoft Cabinet format specification ([MS-CAB]). Not supported: files continued from / to another cabinet of a
|
||||
-- set (Err.UNSUPPORTED for those entries, and for the first folder of a cabinet continued from a previous one),
|
||||
-- signed / self-extracting stubs before the header.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Archive = require("@formats/containers/Archive")
|
||||
local Sum = require("@formats/archive/cab/Sum")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Cab = {
|
||||
name = "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" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse a folder that decompresses to more than this (default 2^30)
|
||||
maxEntries: number?, -- default 65535
|
||||
verify: boolean?, -- check the data block checksums (default true)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
compression: ("mszip" | "none")?, -- default "mszip"
|
||||
level: number?, -- deflate level 0..9 (default 6)
|
||||
setId: number?, -- the cabinet set id (default 0)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local FLAG_PREV, FLAG_NEXT, FLAG_RESERVE = 0x0001, 0x0002, 0x0004
|
||||
local ATTR_UTF8 = 0x80
|
||||
local METHOD = { [0] = "store", "mszip", "quantum", "lzx" }
|
||||
local BLOCK = 32768
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Cab.sniff(r: Bytes.Reader): number
|
||||
if r:peek(8) == "MSCF\0\0\0\0" then
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
type Folder = {
|
||||
dataAt: number, -- offset of the first CFDATA (relative to the reader)
|
||||
blocks: number,
|
||||
type: number, -- typeCompress
|
||||
index: number,
|
||||
}
|
||||
type Block = { at: number, len: number, size: number }
|
||||
|
||||
-- the CFDATA blocks of a folder (checksums checked), their total output and whether the folder runs into the next
|
||||
-- cabinet (a block with cbUncomp 0)
|
||||
local function blocks(r: Bytes.Reader, f: Folder, dataReserve: number, verify: boolean, maxBytes: number): ({ Block }?, any, boolean?)
|
||||
local list: { Block } = {}
|
||||
local p = f.dataAt
|
||||
local total = 0
|
||||
for i = 1, f.blocks do
|
||||
local h = r:sub(p, 8)
|
||||
local csum, cbData, cbUncomp = h:u32le(), h:u16le(), h:u16le()
|
||||
if h.err then
|
||||
return nil, Err.new(Err.TRUNCATED, "data block header past the end", r.base + p)
|
||||
end
|
||||
local at = p + 8 + dataReserve
|
||||
if at + cbData > r:size() then
|
||||
return nil, Err.new(Err.TRUNCATED, "data block past the end", r.base + p)
|
||||
end
|
||||
if cbUncomp == 0 then
|
||||
return list, total, true
|
||||
end
|
||||
if verify and csum ~= 0 and Sum.block(r.buf, r.base + at, cbData, cbUncomp) ~= csum then
|
||||
return nil, Err.new(Err.CHECKSUM, "data block " .. i .. " checksum mismatch", r.base + p)
|
||||
end
|
||||
total += cbUncomp
|
||||
if total > maxBytes then
|
||||
return nil, Err.new(Err.LIMIT, "folder larger than maxBytes")
|
||||
end
|
||||
table.insert(list, { at = r.base + at, len = cbData, size = cbUncomp })
|
||||
p = at + cbData
|
||||
end
|
||||
return list, total, false
|
||||
end
|
||||
|
||||
-- the decompressed data of a folder's blocks
|
||||
local function inflateFolder(buf: buffer, list: { Block }, total: number, typ: number): (buffer?, Err.Err?)
|
||||
local method = bit32.band(typ, 15)
|
||||
local out = buffer.create(total)
|
||||
if method == 0 then
|
||||
local o = 0
|
||||
for _, b in list do
|
||||
if b.len ~= b.size then
|
||||
return nil, Err.new(Err.CORRUPT, "stored block with different sizes", b.at)
|
||||
end
|
||||
buffer.copy(out, o, buf, b.at, b.len)
|
||||
o += b.len
|
||||
end
|
||||
return out, nil
|
||||
elseif method == 1 then
|
||||
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
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
return nil, Err.new(Err.UNSUPPORTED, "compression type " .. method)
|
||||
end
|
||||
|
||||
local function encode(archive: any, opts: any?): (buffer?, Err.Err?)
|
||||
return Cab.encode(archive, opts)
|
||||
end
|
||||
|
||||
function Cab.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local o: DecodeOptions = opts or {}
|
||||
local maxBytes = o.maxBytes or 2 ^ 30
|
||||
local maxEntries = o.maxEntries or 65535
|
||||
local verify = o.verify ~= false
|
||||
if not r:match("MSCF") then
|
||||
return nil, Err.new(Err.CORRUPT, "no MSCF signature", r.base)
|
||||
end
|
||||
r:skip(4)
|
||||
local cbCabinet = r:u32le()
|
||||
r:skip(4)
|
||||
local coffFiles = r:u32le()
|
||||
r:skip(4)
|
||||
local minor, major = r:u8(), r:u8()
|
||||
local cFolders, cFiles, flags = r:u16le(), r:u16le(), r:u16le()
|
||||
local setId, iCabinet = r:u16le(), r:u16le()
|
||||
local headerReserve, folderReserve, dataReserve = 0, 0, 0
|
||||
if bit32.band(flags, FLAG_RESERVE) ~= 0 then
|
||||
headerReserve, folderReserve, dataReserve = r:u16le(), r:u8(), r:u8()
|
||||
end
|
||||
local a = Archive.new("cab", encode)
|
||||
local meta: any = a.meta
|
||||
if headerReserve > 0 then
|
||||
meta.reserve = r:bytes(headerReserve)
|
||||
end
|
||||
if bit32.band(flags, FLAG_PREV) ~= 0 then
|
||||
meta.prevCabinet, meta.prevDisk = r:cstring(256), r:cstring(256)
|
||||
end
|
||||
if bit32.band(flags, FLAG_NEXT) ~= 0 then
|
||||
meta.nextCabinet, meta.nextDisk = r:cstring(256), r:cstring(256)
|
||||
end
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if major ~= 1 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "cabinet version " .. major .. "." .. minor)
|
||||
end
|
||||
meta.setId, meta.cabinet = setId, iCabinet
|
||||
meta.source = {
|
||||
format = "cab",
|
||||
version = major .. "." .. minor,
|
||||
size = cbCabinet,
|
||||
flags = flags,
|
||||
reserve = { header = headerReserve, folder = folderReserve, data = dataReserve },
|
||||
}
|
||||
-- folders
|
||||
local folders: { Folder } = {}
|
||||
local methods = {}
|
||||
for i = 1, cFolders do
|
||||
local dataAt, nblocks, typ = r:u32le(), r:u16le(), r:u16le()
|
||||
r:skip(folderReserve)
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
folders[i] = { dataAt = dataAt, blocks = nblocks, type = typ, index = i }
|
||||
local m = METHOD[bit32.band(typ, 15)]
|
||||
methods[i] = {
|
||||
method = m or ("type " .. bit32.band(typ, 15)),
|
||||
window = if m == "lzx" or m == "quantum" then bit32.band(bit32.rshift(typ, 8), 31) else nil,
|
||||
level = if m == "quantum" then bit32.band(bit32.rshift(typ, 4), 15) else nil,
|
||||
blocks = nblocks,
|
||||
}
|
||||
end
|
||||
meta.folders = methods
|
||||
if cFiles > maxEntries then
|
||||
return nil, Err.new(Err.LIMIT, "more than maxEntries files")
|
||||
end
|
||||
-- the folder cache: the last decompressed folder
|
||||
local cachedIndex, cachedData, cachedRuns = 0, nil :: buffer?, false
|
||||
local function folderData(f: Folder): (buffer?, Err.Err?, boolean?)
|
||||
if cachedIndex == f.index then
|
||||
return cachedData, nil, cachedRuns
|
||||
end
|
||||
local list, total, runs = blocks(r, f, dataReserve, verify, maxBytes)
|
||||
if not list then
|
||||
return nil, total, false
|
||||
end
|
||||
local data, err = inflateFolder(r.buf, list, total, f.type)
|
||||
if not data then
|
||||
return nil, err, false
|
||||
end
|
||||
cachedIndex, cachedData, cachedRuns = f.index, data, runs == true
|
||||
return data, nil, runs
|
||||
end
|
||||
-- files
|
||||
local fr = r:sub(coffFiles)
|
||||
for _ = 1, cFiles do
|
||||
local size, offset, iFolder = fr:u32le(), fr:u32le(), fr:u16le()
|
||||
local date, time, attribs = fr:u16le(), fr:u16le(), fr:u16le()
|
||||
local raw = fr:cstring(65536)
|
||||
if fr.err then
|
||||
return nil, fr.err
|
||||
end
|
||||
local name = string.gsub(raw, "\\", "/")
|
||||
local spanned: string? = nil
|
||||
local folder: Folder?
|
||||
if iFolder == 0xFFFD or iFolder == 0xFFFF then
|
||||
spanned = "a file continued from the previous cabinet"
|
||||
folder = folders[1]
|
||||
elseif iFolder == 0xFFFE then
|
||||
spanned = "a file continued in the next cabinet"
|
||||
folder = folders[#folders]
|
||||
else
|
||||
folder = folders[iFolder + 1]
|
||||
if iFolder == 0 and bit32.band(flags, FLAG_PREV) ~= 0 then
|
||||
spanned = "a folder continued from the previous cabinet"
|
||||
end
|
||||
end
|
||||
if not folder then
|
||||
return nil, Err.new(Err.CORRUPT, "file " .. name .. " in a missing folder " .. iFolder)
|
||||
end
|
||||
local fo = folder :: Folder
|
||||
if size > maxBytes then
|
||||
return nil, Err.new(Err.LIMIT, name .. ": larger than maxBytes")
|
||||
end
|
||||
local e: any = {
|
||||
name = name,
|
||||
size = size,
|
||||
method = METHOD[bit32.band(fo.type, 15)] or "unknown",
|
||||
mtime = Archive.fromDos(date * 65536 + time),
|
||||
dir = false,
|
||||
attributes = bit32.band(attribs, 0xFF7F),
|
||||
utf8 = bit32.band(attribs, ATTR_UTF8) ~= 0,
|
||||
folder = fo.index,
|
||||
offset = offset,
|
||||
readonly = bit32.band(attribs, 0x01) ~= 0,
|
||||
executable = bit32.band(attribs, 0x40) ~= 0,
|
||||
}
|
||||
if spanned then
|
||||
e.spanned = true
|
||||
end
|
||||
if size > 0 or spanned then
|
||||
e.load = function(): (buffer?, Err.Err?)
|
||||
if spanned then
|
||||
return nil, Err.new(Err.UNSUPPORTED, spanned :: string)
|
||||
end
|
||||
local data, err, runs = folderData(fo)
|
||||
if not data then
|
||||
return nil, err
|
||||
end
|
||||
if offset + size > buffer.len(data) then
|
||||
if runs then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "a file continued in the next cabinet")
|
||||
end
|
||||
return nil, Err.new(Err.CORRUPT, "file past the end of its folder")
|
||||
end
|
||||
local out = buffer.create(size)
|
||||
buffer.copy(out, 0, data, offset, size)
|
||||
return out, nil
|
||||
end
|
||||
end
|
||||
Archive.put(a, e)
|
||||
end
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- encode(archive, opts?) -> a cabinet with one folder holding every file (directories are implied by the names)
|
||||
function Cab.encode(archive: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local o: EncodeOptions = opts or {}
|
||||
local compression = o.compression or "mszip"
|
||||
if compression ~= "mszip" and compression ~= "none" then
|
||||
return nil, Err.new(Err.ARG, "compression must be \"mszip\" or \"none\"")
|
||||
end
|
||||
local level = o.level or 6
|
||||
if type(level) ~= "number" or level < 0 or level > 9 or level % 1 ~= 0 then
|
||||
return nil, Err.new(Err.ARG, "level must be 0..9")
|
||||
end
|
||||
local files, ferr = Archive.files(archive)
|
||||
if not files then
|
||||
return nil, ferr
|
||||
end
|
||||
if #files > 65535 then
|
||||
return nil, Err.new(Err.ARG, "more than 65535 files")
|
||||
end
|
||||
local total = 0
|
||||
for _, f in files do
|
||||
total += buffer.len(f.data)
|
||||
end
|
||||
if total > 0x7FFF8000 then
|
||||
return nil, Err.new(Err.ARG, "more than 2 GB of data")
|
||||
end
|
||||
-- the folder data, in 32 KB blocks
|
||||
local folder = buffer.create(total)
|
||||
local p = 0
|
||||
for _, f in files do
|
||||
buffer.copy(folder, p, f.data, 0, buffer.len(f.data))
|
||||
p += buffer.len(f.data)
|
||||
end
|
||||
local data = Bytes.writer(total // 2 + 64)
|
||||
local nblocks = 0
|
||||
local s = 0
|
||||
while s < total do
|
||||
local n = math.min(BLOCK, total - s)
|
||||
local chunk = buffer.create(n)
|
||||
buffer.copy(chunk, 0, folder, s, n)
|
||||
local payload: buffer
|
||||
if compression == "mszip" then
|
||||
local z, zerr = Deflate.compress(chunk, { level = level })
|
||||
if not z then
|
||||
return nil, zerr
|
||||
end
|
||||
payload = buffer.create(buffer.len(z) + 2)
|
||||
buffer.writeu16(payload, 0, 0x4B43)
|
||||
buffer.copy(payload, 2, z, 0, buffer.len(z))
|
||||
else
|
||||
payload = chunk
|
||||
end
|
||||
local len = buffer.len(payload)
|
||||
data:u32le(Sum.block(payload, 0, len, n))
|
||||
data:u16le(len)
|
||||
data:u16le(n)
|
||||
data:bytes(payload)
|
||||
nblocks += 1
|
||||
s += n
|
||||
end
|
||||
local blob = data:finish()
|
||||
-- names and the file table
|
||||
local table_ = Bytes.writer(64 + #files * 32)
|
||||
local offset = 0
|
||||
for _, f in files do
|
||||
local e: any = f.entry
|
||||
local name = string.gsub(f.name, "/", "\\")
|
||||
local attribs = if type(e.attributes) == "number" then bit32.band(e.attributes, 0x7F) else 0x20
|
||||
if string.find(name, "[\128-\255]") then
|
||||
attribs = bit32.bor(attribs, ATTR_UTF8)
|
||||
end
|
||||
local stamp = Archive.toDos(e.mtime)
|
||||
table_:u32le(buffer.len(f.data))
|
||||
table_:u32le(offset)
|
||||
table_:u16le(0)
|
||||
table_:u16le(stamp // 65536)
|
||||
table_:u16le(stamp % 65536)
|
||||
table_:u16le(attribs)
|
||||
table_:cstring(name)
|
||||
offset += buffer.len(f.data)
|
||||
end
|
||||
local fileTable = table_:finish()
|
||||
local nfolders = if #files > 0 then 1 else 0
|
||||
local coffFiles = 36 + 8 * nfolders
|
||||
local dataAt = coffFiles + buffer.len(fileTable)
|
||||
local size = dataAt + buffer.len(blob)
|
||||
local w = Bytes.writer(size)
|
||||
w:string("MSCF")
|
||||
w:u32le(0)
|
||||
w:u32le(size)
|
||||
w:u32le(0)
|
||||
w:u32le(coffFiles)
|
||||
w:u32le(0)
|
||||
w:u8(3)
|
||||
w:u8(1)
|
||||
w:u16le(nfolders)
|
||||
w:u16le(#files)
|
||||
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:bytes(fileTable)
|
||||
w:bytes(blob)
|
||||
return w:finish(), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- new() -> an empty cab Archive: a:add(name, data, { mtime }); a:write({ compression = "none" | "mszip" })
|
||||
function Cab.new(): any
|
||||
local a = Archive.new("cab", encode)
|
||||
a.meta.source = { format = "cab" }
|
||||
return a
|
||||
end
|
||||
|
||||
return Cab
|
||||
@@ -64,10 +64,19 @@ 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).
|
||||
-- dict: a preset dictionary (the data before the stream, e.g. MSZIP's previous block): out starts with a copy of it,
|
||||
-- distances may reach into it, and outLen includes it (cap does not).
|
||||
-- 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)
|
||||
function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, exact: boolean, prefix: boolean?, dict: buffer?): (buffer?, any, number?)
|
||||
local op = 0
|
||||
if dict then
|
||||
op = blen(dict)
|
||||
cap += op
|
||||
end
|
||||
local out = bcreate(cap)
|
||||
if dict then
|
||||
bcopy(out, 0, dict, 0, op)
|
||||
end
|
||||
local pos = start
|
||||
local bitbuf, bitcnt, over = 0, 0, 0
|
||||
local lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
-- 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.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
|
||||
@@ -43,7 +44,9 @@ end
|
||||
-- inflate(buf, offset?, length?, expectedSize?) -> buffer, consumedEnd | nil, Err. Raw deflate.
|
||||
-- expectedSize: the exact uncompressed size when known (a zip entry): the output is allocated once, and a stream
|
||||
-- that inflates to a different size is an error. consumedEnd is the input offset just past the final block.
|
||||
function Deflate.inflate(buf: buffer | string, offset: number?, length: number?, expectedSize: number?): (buffer?, any)
|
||||
-- dict: a preset dictionary (up to 32 KB of data before the stream, as MSZIP's previous block): back references may
|
||||
-- reach into it; the result holds only the new bytes.
|
||||
function Deflate.inflate(buf: buffer | string, offset: number?, length: number?, expectedSize: number?, dict: buffer?): (buffer?, any)
|
||||
local b, o, l, aerr = args("Deflate.inflate", buf, offset, length)
|
||||
if not b then
|
||||
return nil, aerr
|
||||
@@ -58,10 +61,20 @@ function Deflate.inflate(buf: buffer | string, offset: number?, length: number?,
|
||||
else
|
||||
cap = math.max(1024, l * 4)
|
||||
end
|
||||
local out, n, consumed = Inflate.raw(b, o, o + l, cap :: number, exact)
|
||||
if dict ~= nil and type(dict) ~= "buffer" then
|
||||
return nil, Err.new(Err.ARG, "Deflate.inflate: dict must be a buffer")
|
||||
end
|
||||
local out, n, consumed = Inflate.raw(b, o, o + l, cap :: number, exact, false, dict)
|
||||
if not out then
|
||||
return nil, inflateErr(tostring(n), o)
|
||||
end
|
||||
if dict then
|
||||
local d = buffer.len(dict)
|
||||
n -= d
|
||||
local res = buffer.create(n)
|
||||
buffer.copy(res, 0, out, d, n)
|
||||
out = res
|
||||
end
|
||||
if exact and n ~= expectedSize then
|
||||
return nil, Err.new(Err.CORRUPT, string.format("inflate: output is %d bytes, expected %d", n, expectedSize :: number))
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user