ws-arch: archive/xz: .xz / .lzma read as one-entry stream archives (codecs/Lzma)

This commit is contained in:
4DBug committed 2026-10-31 08:32:50 -05:00
1 parent 3dc80be5ba
commit 6ea28b9aad
4 files changed
+193

No files matched your search

+2
View File
@@ -6,7 +6,9 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| Category | Format | Extensions | Read | Write | Capabilities | Test |
|---|---|---|---|---|---|---|
| image | png | .png .apng | yes | yes | animation: false, alpha: true, depths: 1, 2, 4, 8, 16 | `tests/image/png.luau` |
| archive | xz | .xz .txz .lzma | yes | - | stream: true, filters: "lzma2", "lzma", "delta", "x86", "powerpc", "arm", "armthumb", "sparc", "arm64" | `tests/archive/xz.luau` |
Codecs (layer 1, shared by formats; not loadable on their own):
- codecs/Deflate: DEFLATE (RFC 1951) decoder and compressor, with the zlib (RFC 1950) and gzip (RFC 1952) wrappers.
- codecs/Lzma: LZMA and LZMA2 decoders, the .lzma ("LZMA alone") wrapper and the .xz container. A codec (layer 3):
+12
View File
@@ -26,6 +26,18 @@ local list: { Entry } = {
return require("@formats/image/png")
end,
},
{
name = "xz",
category = "archive",
path = "archive/xz",
extensions = { "xz", "txz", "lzma" },
mime = { "application/x-xz", "application/x-lzma" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Lzma", "containers/Archive" },
caps = { decode = true, encode = false, stream = true, filters = { "lzma2", "lzma", "delta", "x86", "powerpc", "arm", "armthumb", "sparc", "arm64" } },
load = function()
return require("@formats/archive/xz")
end,
},
}
return list
+101
View File
@@ -0,0 +1,101 @@
--!strict
-- archive/xz: .xz (xz file format 1.2) and .lzma ("LZMA alone") files, read as single-stream archives holding one
-- entry (the decompressed data). Built on codecs/Lzma. Supports: concatenated streams, stream padding, every block
-- layout, checks none / CRC-32 / CRC-64 (verified; SHA-256 is not verified), filters LZMA2, delta and the BCJ filters
-- x86 / PowerPC / ARM / ARM-Thumb / SPARC / ARM64. Not supported: writing, IA-64 / RISC-V filters.
-- The entry is named after the file (opts.name without .xz / .lzma; .txz -> .tar), else "data". meta.stream = true:
-- the api unwraps an inner archive (a .tar.xz loads as the tar).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Lzma = require("@formats/codecs/Lzma")
local Archive = require("@formats/containers/Archive")
-- ------------------------------------------------------------------------------------------------ contract
local Xz = {
name = "xz",
category = "archive",
extensions = { "xz", "txz", "lzma" },
mime = { "application/x-xz", "application/x-lzma" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Lzma", "containers/Archive" },
caps = { decode = true, encode = false, stream = true, filters = { "lzma2", "lzma", "delta", "x86", "powerpc", "arm", "armthumb", "sparc", "arm64" } },
}
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
}
-- ------------------------------------------------------------------------------------------------ constants
local XZ_MAGIC = "\253" .. "7zXZ\0"
-- ------------------------------------------------------------------------------------------------ sniff
-- .lzma files have no magic: props byte < 225 (0x5D for every preset), a dictionary size of 2^n or 2^n + 2^(n-1),
-- a size of -1 or under 2^40, and a 0 range coder byte
local function aloneScore(r: Bytes.Reader): number
local h = r:peek(14)
if #h < 14 then
return 0
end
local d = string.byte(h, 1)
if d >= 225 or string.byte(h, 14) ~= 0 then
return 0
end
local dict = string.byte(h, 2) + string.byte(h, 3) * 256 + string.byte(h, 4) * 65536 + string.byte(h, 5) * 16777216
local okDict = false
for n = 12, 31 do
if dict == 2 ^ n or (n < 31 and dict == 2 ^ n + 2 ^ (n - 1)) then
okDict = true
break
end
end
if not okDict then
return 0
end
local s = string.sub(h, 6, 13)
if s ~= string.rep("\255", 8) and (string.byte(h, 11) ~= 0 or string.byte(h, 12) ~= 0 or string.byte(h, 13) ~= 0) then
return 0
end
return if d == 0x5D then 0.4 else 0.2
end
function Xz.sniff(r: Bytes.Reader): number
if r:peek(6) == XZ_MAGIC then
return 1
end
return aloneScore(r)
end
-- ------------------------------------------------------------------------------------------------ decode
function Xz.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?, Err.Err?)
local o = opts or {}
local n = r.limit - r.base
local out, res
local method
if r:peek(6) == XZ_MAGIC then
out, res = Lzma.xz(r.buf, r.base, n, o)
method = "lzma2"
else
out, res = Lzma.alone(r.buf, r.base, n, o)
method = "lzma"
end
if not out then
return nil, res
end
local a = Archive.new("xz")
a.meta.stream = true
a.meta.container = if method == "lzma" then "lzma" else "xz"
Archive.put(a, {
name = Archive.streamName(o, Xz.extensions),
size = buffer.len(out),
compressedSize = n,
method = method,
dir = false,
data = out,
})
return a, nil
end
return Xz
+78
View File
@@ -0,0 +1,78 @@
-- archive/xz tests: the contract, sniffing (.xz magic, conservative .lzma), every xz / .lzma fixture of
-- tools/fixtures/lzma.py read as a one-entry archive (entry names from opts.name), F.load through the registry, errors,
-- and the fuzz no-throw pass.
local T = require("@tests/T")
local Xz = require("@formats/archive/xz")
local F = require("@formats")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/lzma")
local t = T.suite("xz")
t:eq(Xz.name, "xz", "name")
t:eq(Xz.category, "archive", "category")
t:check(Xz.caps.decode and not Xz.caps.encode, "caps")
local function reader(s: string): Bytes.Reader
return Bytes.reader(buffer.fromstring(s))
end
local n = 0
for _, c in Fx.cases do
if c.kind == "xz" or c.kind == "alone" then
local want = Fx.corpora[c.corpus]
t:check(Xz.sniff(reader(c.data)) > 0, c.name .. ": sniff")
local a, err = Xz.decode(reader(c.data), { name = "dir/file.txt." .. (if c.kind == "xz" then "xz" else "lzma") })
t:check(a, c.name .. ": " .. tostring(err))
local list = a:list()
t:eq(#list, 1, c.name .. ": one entry")
t:eq(list[1].name, "file.txt", c.name .. ": entry name")
t:eq(list[1].size, #want, c.name .. ": size")
t:check(a.meta.stream == true, c.name .. ": meta.stream")
local b = a:read("file.txt")
t:check(b and buffer.tostring(b) == want, c.name .. ": data")
n += 1
end
end
t:note("%d xz / lzma files", n)
-- sniff: not xz
t:eq(Xz.sniff(reader("PK\3\4 hello")), 0, "zip is not xz")
t:eq(Xz.sniff(reader("")), 0, "empty")
t:eq(Xz.sniff(reader(string.rep("\0", 40))), 0, "zeros are not .lzma")
-- through the api
do
local c = Fx.cases[1]
for _, x in Fx.cases do
if x.name == "text/xz-6" then
c = x
end
end
local a, err = F.load(c.data, { name = "notes.txz" })
t:check(a and a.kind == "archive" and a.format == "xz", "F.load: " .. tostring(err))
t:eq(a:list()[1].name, "notes.tar", "txz -> tar entry name")
t:eq(F.detect(c.data), "xz", "F.detect")
local a2 = F.archive.load(c.data)
t:eq(a2:list()[1].name, "data", "unnamed entry")
local _, e2 = F.save(a2, nil, { format = "xz" })
t:check(e2 and e2.code == Err.UNSUPPORTED, "xz is read-only: " .. tostring(e2))
local _, e3 = F.load(string.sub(c.data, 1, 100), { format = "xz" })
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
end
-- fuzz
do
local samples = {}
for i, c in Fx.cases do
if (c.kind == "xz" or c.kind == "alone") and i % 5 == 0 and #c.data < 30000 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return Xz.decode(Bytes.reader(b), { maxBytes = 2 ^ 20 })
end, samples, { mutations = 6, garbage = 400 })
end
t:done()