containers/Archive: additive helper streamData (requires containers/Tar); nothing existing changes.
99 lines
3.8 KiB
Luau
99 lines
3.8 KiB
Luau
--!strict
|
|
-- archive/z: Unix compress files (.Z: LZW, magic 1F 9D, 9..16-bit codes, block mode) as a single-stream Archive: one
|
|
-- entry, named after the file without .Z (.taz / .tZ -> .tar), else "data"; meta.stream = true, so the api unwraps a
|
|
-- .tar.Z into the tar. Writing: an Archive with one file entry (or a buffer / string) becomes that file compressed,
|
|
-- byte for byte what ncompress writes; an Archive with several entries is written as a tar inside (a .tar.Z). The LZW is
|
|
-- codecs/compress/UnixCompress. Not supported: pack (.z, 1F 1E) and compact (1F FF) files.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local UnixCompress = require("@formats/codecs/compress/UnixCompress")
|
|
local Archive = require("@formats/containers/Archive")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Z = {
|
|
name = "z",
|
|
category = "archive",
|
|
extensions = { "z", "taz", "tz" },
|
|
mime = { "application/x-compress" },
|
|
deps = { "core/Err", "core/Bytes", "codecs/compress/UnixCompress", "containers/Archive" },
|
|
caps = { decode = true, encode = true, stream = true, bits = { 9, 16 } },
|
|
}
|
|
|
|
export type DecodeOptions = {
|
|
maxBytes: number?, -- refuse output larger than this (default 2^30)
|
|
name: string?, -- the file name (the api passes it): names the entry
|
|
}
|
|
export type EncodeOptions = {
|
|
maxBits: number?, -- 9..16 (compress -b), default 16
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Z.sniff(r: Bytes.Reader): number
|
|
if r:peek(2) ~= "\31\157" then
|
|
return 0
|
|
end
|
|
local f = r:peekU8(2)
|
|
return if f >= 0 and bit32.band(f, 0x60) == 0 and bit32.band(f, 0x1F) >= 9 and bit32.band(f, 0x1F) <= 16 then 1 else 0.3
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- the entry name: the file name without .Z; .taz / .tz name a tar
|
|
local function entryName(opts: DecodeOptions): string
|
|
local name = Archive.streamName(opts, { "z" })
|
|
local stem, ext = string.match(name, "^(.+)%.(%w+)$")
|
|
if stem and ext and (string.lower(ext) == "taz" or string.lower(ext) == "tz") then
|
|
return stem .. ".tar"
|
|
end
|
|
return name
|
|
end
|
|
|
|
local function encode(value: any, opts: any?): (buffer?, Err.Err?)
|
|
return Z.encode(value, opts)
|
|
end
|
|
|
|
function Z.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
|
local o: DecodeOptions = opts or {}
|
|
local n = r.limit - r.base
|
|
local out, err = UnixCompress.decompress(r.buf, r.base, n, o)
|
|
if not out then
|
|
return nil, err
|
|
end
|
|
local a = Archive.new("z", encode)
|
|
a.meta.stream = true
|
|
a.meta.source = { format = "z", maxBits = bit32.band(buffer.readu8(r.buf, r.base + 2), 0x1F), blockMode = bit32.band(buffer.readu8(r.buf, r.base + 2), 0x80) ~= 0 }
|
|
Archive.put(a, {
|
|
name = entryName(o),
|
|
size = buffer.len(out),
|
|
compressedSize = n,
|
|
method = "lzw",
|
|
dir = false,
|
|
data = out,
|
|
})
|
|
return a, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Z.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
local data, err = Archive.streamData(value)
|
|
if not data then
|
|
return nil, err
|
|
end
|
|
return UnixCompress.compress(data, 0, buffer.len(data), { maxBits = opts and opts.maxBits })
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ extras
|
|
-- new(data?, name?) -> a .Z Archive holding one entry
|
|
function Z.new(data: (buffer | string)?, name: string?): any
|
|
local a = Archive.new("z", encode)
|
|
a.meta.stream = true
|
|
a.meta.source = { format = "z" }
|
|
if data then
|
|
a:add(name or "data", data)
|
|
end
|
|
return a
|
|
end
|
|
|
|
return Z
|