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
@@ -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