ws-arch: archive/xz: .xz / .lzma read as one-entry stream archives (codecs/Lzma)
This commit is contained in:
1 parent
b306a3bf5e
commit
0a56389b09
4 files changed
+193
No files matched your search
@@ -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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user