ws-voxel: codecs/text/Nbt (NBT Java / Bedrock / network varint, SNBT parse + print, typed values), data/nbt, data/snbt
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.
This commit is contained in:
1 parent
de4f14a343
commit
998177e5ab
15 files changed
+2745
-26
No files matched your search
@@ -9,6 +9,7 @@
|
||||
-- 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
|
||||
-- local head = Deflate.peek(buf, nil, nil, 512) -- the first 512 inflated bytes of gzip / zlib / raw
|
||||
--
|
||||
-- Never throws: bad data and bad arguments return nil, Err (core/Err codes TRUNCATED / CORRUPT / CHECKSUM / ARG).
|
||||
local Err = require("@formats/core/Err")
|
||||
@@ -101,6 +102,36 @@ function Deflate.zlib(buf: buffer | string, offset: number?, length: number?, ex
|
||||
return out, consumed + 4
|
||||
end
|
||||
|
||||
-- gzipBody(b, p, e) -> the offset of a gzip member's deflate data (past FEXTRA / FNAME / FCOMMENT / FHCRC) | nil, Err
|
||||
local function gzipBody(b: buffer, p: number, e: number): (number?, Err.Err?)
|
||||
local flg = buffer.readu8(b, p + 3)
|
||||
if flg >= 0x20 then
|
||||
return nil, Err.new(Err.CORRUPT, "gzip: reserved flags set", p + 3)
|
||||
end
|
||||
local q = p + 10
|
||||
if bit32.band(flg, 4) ~= 0 then -- FEXTRA
|
||||
if q + 2 > e then
|
||||
return nil, Err.new(Err.TRUNCATED, "gzip: truncated header", q)
|
||||
end
|
||||
q += 2 + buffer.readu16(b, q)
|
||||
end
|
||||
for _, bit in { 8, 16 } do -- FNAME, FCOMMENT: zero-terminated
|
||||
if bit32.band(flg, bit) ~= 0 then
|
||||
while q < e and buffer.readu8(b, q) ~= 0 do
|
||||
q += 1
|
||||
end
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
if bit32.band(flg, 2) ~= 0 then -- FHCRC
|
||||
q += 2
|
||||
end
|
||||
if q >= e then
|
||||
return nil, Err.new(Err.TRUNCATED, "gzip: truncated header", q)
|
||||
end
|
||||
return q, nil
|
||||
end
|
||||
|
||||
-- gunzip(buf, offset?, length?) -> buffer | nil, Err. Members are concatenated; trailing data after a member that is
|
||||
-- not another gzip member (zero padding, garbage) is ignored, as gzip -d does.
|
||||
function Deflate.gunzip(buf: buffer | string, offset: number?, length: number?): (buffer?, Err.Err?)
|
||||
@@ -127,30 +158,9 @@ function Deflate.gunzip(buf: buffer | string, offset: number?, length: number?):
|
||||
if buffer.readu8(b, p + 2) ~= 8 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "gzip: unknown compression method", p + 2)
|
||||
end
|
||||
local flg = buffer.readu8(b, p + 3)
|
||||
if flg >= 0x20 then
|
||||
return nil, Err.new(Err.CORRUPT, "gzip: reserved flags set", p + 3)
|
||||
end
|
||||
local q = p + 10
|
||||
if bit32.band(flg, 4) ~= 0 then -- FEXTRA
|
||||
if q + 2 > e then
|
||||
return nil, Err.new(Err.TRUNCATED, "gzip: truncated header", q)
|
||||
end
|
||||
q += 2 + buffer.readu16(b, q)
|
||||
end
|
||||
for _, bit in { 8, 16 } do -- FNAME, FCOMMENT: zero-terminated
|
||||
if bit32.band(flg, bit) ~= 0 then
|
||||
while q < e and buffer.readu8(b, q) ~= 0 do
|
||||
q += 1
|
||||
end
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
if bit32.band(flg, 2) ~= 0 then -- FHCRC
|
||||
q += 2
|
||||
end
|
||||
if q >= e then
|
||||
return nil, Err.new(Err.TRUNCATED, "gzip: truncated header", q)
|
||||
local q, herr = gzipBody(b, p, e)
|
||||
if not q then
|
||||
return nil, herr
|
||||
end
|
||||
local out, consumed = Deflate.inflate(b, q, e - q)
|
||||
if not out then
|
||||
@@ -191,6 +201,38 @@ function Deflate.gunzip(buf: buffer | string, offset: number?, length: number?):
|
||||
return res, nil
|
||||
end
|
||||
|
||||
-- peek(buf, offset?, length?, n) -> buffer | nil, Err: the first n bytes (fewer when the stream is shorter, or up to
|
||||
-- 258 fewer at a match boundary) a gzip member, zlib stream or raw deflate stream inflates to, without inflating
|
||||
-- the rest or checking checksums: for sniffing compressed files cheaply.
|
||||
function Deflate.peek(buf: buffer | string, offset: number?, length: number?, n: number): (buffer?, Err.Err?)
|
||||
local b, o, l, aerr = args("Deflate.peek", buf, offset, length)
|
||||
if not b then
|
||||
return nil, aerr
|
||||
end
|
||||
if type(n) ~= "number" or n < 1 then
|
||||
return nil, Err.new(Err.ARG, "Deflate.peek: bad size " .. tostring(n))
|
||||
end
|
||||
local e = o + l
|
||||
local q = o
|
||||
if l >= 18 and buffer.readu8(b, o) == 0x1f and buffer.readu8(b, o + 1) == 0x8b and buffer.readu8(b, o + 2) == 8 then
|
||||
local herr
|
||||
q, herr = gzipBody(b, o, e)
|
||||
if not q then
|
||||
return nil, herr
|
||||
end
|
||||
elseif l >= 6 then
|
||||
local cmf, flg = buffer.readu8(b, o), buffer.readu8(b, o + 1)
|
||||
if cmf % 16 == 8 and cmf // 16 <= 7 and (cmf * 256 + flg) % 31 == 0 and bit32.band(flg, 0x20) == 0 then
|
||||
q = o + 2
|
||||
end
|
||||
end
|
||||
local out, k = Inflate.raw(b, q :: number, e, n, true, true)
|
||||
if not out then
|
||||
return nil, inflateErr(tostring(k), q)
|
||||
end
|
||||
return shrink(out, k), nil
|
||||
end
|
||||
|
||||
export type CompressOptions = {
|
||||
level: number?, -- 0 (stored) .. 9 (best); default 6
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user