99 lines
3.2 KiB
Luau
99 lines
3.2 KiB
Luau
--!strict
|
|
-- archive/xz: .xz (xz file format 1.2) and .lzma ("LZMA alone") as single-stream archives (codecs/compress/Lzma).
|
|
-- Supports: concatenated streams, padding, checks none / CRC-32 / CRC-64 (SHA-256 unverified), LZMA2, delta, BCJ
|
|
-- x86 / PowerPC / ARM / ARM-Thumb / SPARC / ARM64. Not supported: writing, IA-64 / RISC-V filters.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Types = require("@rosetta/Types")
|
|
local Lzma = require("@rosetta/codecs/compress/Lzma")
|
|
local Archive = require("@rosetta/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/compress/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?, -- default 2^30
|
|
name: string?,
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ 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: DecodeOptions = 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
|