Files
rosetta/src/Formats/archive/gzip/init.luau
T

188 lines
6.1 KiB
Luau

--!strict
-- archive/gzip: gzip files (RFC 1952) as a single-stream Archive: one entry, named after the header's FNAME (else the
-- file name without .gz, else "data"), with the header's MTIME; concatenated members decode as one stream, as gzip -d
-- does. Writing: an Archive with one entry (or a buffer / string) becomes one member with FNAME and MTIME; an Archive
-- with several entries is written as a tar inside the gzip (a .tar.gz). The api unwraps a .tar.gz / .tgz on load (the
-- inner tar, meta.compression = "gzip"). Deflate is codecs/Deflate. Not supported: FHCRC verification.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Deflate = require("@formats/codecs/Deflate")
local Archive = require("@formats/containers/Archive")
local Tar = require("@formats/containers/Tar")
-- ------------------------------------------------------------------------------------------------ contract
local Gzip = {
name = "gzip",
category = "archive",
extensions = { "gz", "tgz", "gzip" },
mime = { "application/gzip" },
deps = { "core/Err", "core/Bytes", "core/Crc32", "codecs/Deflate", "containers/Archive", "containers/Tar" },
caps = { decode = true, encode = true, stream = true },
}
export type DecodeOptions = { name: string? } -- the file name (the api passes it): names the entry without FNAME
export type EncodeOptions = {
level: number?, -- 0..9, default 6
name: string?, -- FNAME (default: the entry's name; none for a buffer)
mtime: number?, -- MTIME (default: the entry's mtime, else 0)
}
-- ------------------------------------------------------------------------------------------------ sniff
function Gzip.sniff(r: Bytes.Reader): number
return if r:peek(3) == "\31\139\8" then 1 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
-- header(r) -> { name, comment, mtime, os } of the first member (nil fields when absent)
local function header(b: buffer, o: number, e: number): { [string]: any }
local h: { [string]: any } = {}
if o + 10 > e then
return h
end
local flg = buffer.readu8(b, o + 3)
h.mtime = buffer.readu32(b, o + 4)
h.os = buffer.readu8(b, o + 9)
local q = o + 10
if bit32.band(flg, 4) ~= 0 and q + 2 <= e then
q += 2 + buffer.readu16(b, q)
end
for _, f in { { 8, "name" }, { 16, "comment" } } do
if bit32.band(flg, f[1]) ~= 0 then
local s = q
while q < e and buffer.readu8(b, q) ~= 0 do
q += 1
end
if q < e then
h[f[2]] = buffer.readstring(b, s, q - s)
end
q += 1
end
end
return h
end
function Gzip.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local out, err = Deflate.gunzip(r.buf, r.base, r.limit - r.base)
if not out then
return nil, err
end
local h = header(r.buf, r.base, r.limit)
local a = Archive.new("gzip", function(arc: any, o: any?): (buffer?, Err.Err?)
return Gzip.encode(arc, o)
end)
a.meta.stream = true
a.meta.source = { format = "gzip", os = h.os }
a.meta.comment = h.comment
local name = h.name
if not name or name == "" or string.find(name, "[/\\]") then
name = Archive.streamName(opts, Gzip.extensions)
end
Archive.put(a, {
name = name,
size = buffer.len(out),
compressedSize = r.limit - r.base,
method = "deflate",
mtime = if h.mtime and h.mtime > 0 then h.mtime else nil,
dir = false,
data = out,
})
return a, nil
end
-- ------------------------------------------------------------------------------------------------ encode
function Gzip.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local o = opts or {}
local level = if o.level == nil then 6 else o.level
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 data: buffer
local name: string? = o.name
local mtime: number = o.mtime or 0
if type(value) == "buffer" then
data = value
elseif type(value) == "string" then
data = buffer.fromstring(value)
else
local items = Archive.entries(value)
if not items then
return nil, Err.new(Err.ARG, "expected an Archive, a buffer or a string")
end
local files = {}
for _, it in items do
if not it.entry.dir then
table.insert(files, it)
end
end
if #files == 1 and #items == 1 then
local b, err = files[1].read()
if not b then
return nil, err
end
data = b
name = name or files[1].entry.name
mtime = o.mtime or files[1].entry.mtime or 0
else
-- several entries: a tar inside
local list = {}
for _, it in items do
local e: any = it.entry
local b = nil
if not e.dir then
local rb, err = it.read()
if not rb then
return nil, err
end
b = rb
end
table.insert(list, { name = e.name, data = b, dir = e.dir, mtime = e.mtime, mode = e.mode })
end
local t, terr = Tar.write(list)
if not t then
return nil, terr
end
data = t
end
end
if type(mtime) ~= "number" or mtime < 0 or mtime >= 2 ^ 32 then
mtime = 0
end
local raw, cerr = Deflate.compress(data, { level = level })
if not raw then
return nil, cerr
end
local w = Bytes.writer(buffer.len(raw) + 32 + (if name then #name else 0))
w:string("\31\139\8")
w:u8(if name then 8 else 0)
w:u32le(math.floor(mtime))
w:u8(if level >= 9 then 2 elseif level <= 1 then 4 else 0)
w:u8(255)
if name then
local z = string.find(name, "\0", 1, true)
w:cstring(if z then string.sub(name, 1, z - 1) else name)
end
w:bytes(raw)
w:u32le(Crc32.raw(data, 0, buffer.len(data)))
w:u32le(buffer.len(data) % 4294967296)
return w:finish(), nil
end
-- ------------------------------------------------------------------------------------------------ extras
-- new(data?, name?) -> a gzip Archive holding one entry (add() replaces / adds; several entries write a .tar.gz)
function Gzip.new(data: (buffer | string)?, name: string?): any
local a = Archive.new("gzip", function(arc: any, o: any?): (buffer?, Err.Err?)
return Gzip.encode(arc, o)
end)
a.meta.stream = true
a.meta.source = { format = "gzip" }
if data then
a:add(name or "data", data)
end
return a
end
return Gzip