ws-arch2-unix: archive/rpm (lead, signature + main headers, file list from dirnames / basenames, payload gzip / bzip2 / xz / lzma / zstd / none decompressed lazily via codecs/compress, cpio via containers/Cpio; meta.rpm), tests vs rpmbuild / rpm -qp / rpm2cpio; containers/Cpio.TYPE_OF
This commit is contained in:
1 parent
07f70f70d5
commit
8b94584d0f
6 files changed
+549
No files matched your search
@@ -614,6 +614,18 @@ local list: { Entry } = {
|
||||
return require("@formats/archive/gzip")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "rpm",
|
||||
category = "archive",
|
||||
path = "archive/rpm",
|
||||
extensions = { "rpm" },
|
||||
mime = { "application/x-rpm" },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/compress/Deflate", "codecs/compress/Lzma", "codecs/compress/Zstd", "codecs/compress/Bzip2", "containers/Archive", "containers/Cpio" },
|
||||
caps = { decode = true, encode = false, compressors = { "gzip", "bzip2", "xz", "lzma", "zstd" } },
|
||||
load = function()
|
||||
return require("@formats/archive/rpm")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "tar",
|
||||
category = "archive",
|
||||
|
||||
@@ -0,0 +1,333 @@
|
||||
--!strict
|
||||
-- archive/rpm: RPM packages (format v3 / v4), read into the standard Archive value. The 96-byte lead, the signature
|
||||
-- header (padded to 8 bytes) and the main header are parsed (every tag into meta.rpm.tags; the common ones by name in
|
||||
-- meta.rpm); the file list comes from the header (dirnames / basenames / dirindexes, or the old filenames tag, with
|
||||
-- sizes / long sizes, modes, mtimes, link targets, owners) and lists without touching the payload. The payload
|
||||
-- (gzip / bzip2 / xz / lzma / zstd or none, through codecs/compress/*) is decompressed on the first read and parsed
|
||||
-- as a cpio archive (containers/Cpio); entry names drop the leading "./" and "/". Ghost files (%ghost) are not
|
||||
-- listed (they are not in the payload). Written from the RPM file format documentation (rpm.org, the LSB package
|
||||
-- format chapter); no code ported. Not supported: writing, signature / digest verification, the "stripped cpio"
|
||||
-- payload of packages with files over 4 GiB (Err.UNSUPPORTED on read), delta rpms.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Lzma = require("@formats/codecs/compress/Lzma")
|
||||
local Zstd = require("@formats/codecs/compress/Zstd")
|
||||
local Bzip2 = require("@formats/codecs/compress/Bzip2")
|
||||
local Archive = require("@formats/containers/Archive")
|
||||
local Cpio = require("@formats/containers/Cpio")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Rpm = {
|
||||
name = "rpm",
|
||||
category = "archive",
|
||||
extensions = { "rpm" },
|
||||
mime = { "application/x-rpm" },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/compress/Deflate", "codecs/compress/Lzma", "codecs/compress/Zstd", "codecs/compress/Bzip2", "containers/Archive", "containers/Cpio" },
|
||||
caps = { decode = true, encode = false, compressors = { "gzip", "bzip2", "xz", "lzma", "zstd" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- the largest decompressed payload (default 2^30)
|
||||
maxEntries: number?, -- default 1e6
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local LEAD_MAGIC = "\237\171\238\219"
|
||||
local HEADER_MAGIC = "\142\173\232\1"
|
||||
local RPMFILE_GHOST = 64
|
||||
|
||||
-- the tags meta.rpm names
|
||||
local NAMES: { [number]: string } = {
|
||||
[1000] = "name",
|
||||
[1001] = "version",
|
||||
[1002] = "release",
|
||||
[1003] = "epoch",
|
||||
[1004] = "summary",
|
||||
[1005] = "description",
|
||||
[1006] = "buildtime",
|
||||
[1007] = "buildhost",
|
||||
[1009] = "size",
|
||||
[1010] = "distribution",
|
||||
[1011] = "vendor",
|
||||
[1014] = "license",
|
||||
[1015] = "packager",
|
||||
[1016] = "group",
|
||||
[1020] = "url",
|
||||
[1021] = "os",
|
||||
[1022] = "arch",
|
||||
[1044] = "sourcerpm",
|
||||
[1047] = "provides",
|
||||
[1049] = "requires",
|
||||
[1124] = "payloadFormat",
|
||||
[1125] = "payloadCompressor",
|
||||
[1126] = "payloadFlags",
|
||||
[5009] = "longsize",
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Rpm.sniff(r: Bytes.Reader): number
|
||||
return if r:peek(4) == LEAD_MAGIC then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- a header structure at p -> { [tag] = value }, end offset | nil, Err
|
||||
local function header(b: buffer, p: number, e: number): (any, any)
|
||||
if p + 16 > e then
|
||||
return nil, Err.new(Err.TRUNCATED, "header past the end", p)
|
||||
end
|
||||
if buffer.readstring(b, p, 4) ~= HEADER_MAGIC then
|
||||
return nil, Err.new(Err.CORRUPT, "bad header magic", p)
|
||||
end
|
||||
local n = bit32.byteswap(buffer.readu32(b, p + 8))
|
||||
local hsize = bit32.byteswap(buffer.readu32(b, p + 12))
|
||||
local index = p + 16
|
||||
local store = index + n * 16
|
||||
if n > 0x10000 or hsize > 0x10000000 then
|
||||
return nil, Err.new(Err.LIMIT, "header too large", p)
|
||||
end
|
||||
if store + hsize > e then
|
||||
return nil, Err.new(Err.TRUNCATED, "header data past the end", p)
|
||||
end
|
||||
local tags: { [number]: any } = {}
|
||||
local se = store + hsize
|
||||
for i = 0, n - 1 do
|
||||
local q = index + i * 16
|
||||
local tag = bit32.byteswap(buffer.readu32(b, q))
|
||||
local typ = bit32.byteswap(buffer.readu32(b, q + 4))
|
||||
local off = bit32.byteswap(buffer.readu32(b, q + 8))
|
||||
local count = bit32.byteswap(buffer.readu32(b, q + 12))
|
||||
local d = store + off
|
||||
if off > hsize then
|
||||
return nil, Err.new(Err.CORRUPT, "tag data outside the header", q)
|
||||
end
|
||||
local width = if typ == 2 or typ == 1 then 1 elseif typ == 3 then 2 elseif typ == 4 then 4 elseif typ == 5 then 8 else 0
|
||||
if width > 0 then
|
||||
if d + count * width > se then
|
||||
return nil, Err.new(Err.CORRUPT, "tag data outside the header", q)
|
||||
end
|
||||
local v = table.create(count)
|
||||
for k = 0, count - 1 do
|
||||
local x = d + k * width
|
||||
v[k + 1] = if width == 1 then buffer.readu8(b, x)
|
||||
elseif width == 2 then bit32.bor(bit32.lshift(buffer.readu8(b, x), 8), buffer.readu8(b, x + 1))
|
||||
elseif width == 4 then bit32.byteswap(buffer.readu32(b, x))
|
||||
else bit32.byteswap(buffer.readu32(b, x)) * 4294967296 + bit32.byteswap(buffer.readu32(b, x + 4))
|
||||
end
|
||||
tags[tag] = v
|
||||
elseif typ == 7 then
|
||||
if d + count > se then
|
||||
return nil, Err.new(Err.CORRUPT, "tag data outside the header", q)
|
||||
end
|
||||
tags[tag] = buffer.readstring(b, d, count)
|
||||
elseif typ == 6 or typ == 8 or typ == 9 then
|
||||
local v = {}
|
||||
local s = d
|
||||
local want = if typ == 6 then 1 else count
|
||||
if want > hsize then
|
||||
return nil, Err.new(Err.CORRUPT, "bad string count", q)
|
||||
end
|
||||
for _ = 1, want do
|
||||
local z = s
|
||||
while z < se and buffer.readu8(b, z) ~= 0 do
|
||||
z += 1
|
||||
end
|
||||
if z >= se then
|
||||
return nil, Err.new(Err.CORRUPT, "unterminated string", s)
|
||||
end
|
||||
table.insert(v, buffer.readstring(b, s, z - s))
|
||||
s = z + 1
|
||||
end
|
||||
tags[tag] = if typ == 6 then v[1] else v
|
||||
end
|
||||
end
|
||||
return tags, se
|
||||
end
|
||||
|
||||
-- the file list of the header: { name, size, mode, mtime, link, user, group, flags }
|
||||
local function files(tags: { [number]: any }): { any }
|
||||
local names: { string } = {}
|
||||
local bn, dn, di = tags[1117], tags[1118], tags[1116]
|
||||
if type(bn) == "table" and type(dn) == "table" and type(di) == "table" then
|
||||
for i, base in bn do
|
||||
local d = dn[(di[i] or 0) + 1]
|
||||
names[i] = (if type(d) == "string" then d else "") .. base
|
||||
end
|
||||
elseif type(tags[1027]) == "table" then
|
||||
names = tags[1027]
|
||||
end
|
||||
local sizes = tags[5008] or tags[1028] or {}
|
||||
local modes, mtimes, links = tags[1030] or {}, tags[1034] or {}, tags[1036] or {}
|
||||
local users, groups, flags = tags[1039] or {}, tags[1040] or {}, tags[1037] or {}
|
||||
local out = {}
|
||||
for i, n in names do
|
||||
table.insert(out, {
|
||||
name = n,
|
||||
size = sizes[i] or 0,
|
||||
mode = modes[i] or 0,
|
||||
mtime = mtimes[i] or 0,
|
||||
link = if links[i] and links[i] ~= "" then links[i] else nil,
|
||||
user = users[i],
|
||||
group = groups[i],
|
||||
flags = flags[i] or 0,
|
||||
})
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function strip(name: string): string
|
||||
return (string.gsub(name, "^%.?/+", ""))
|
||||
end
|
||||
|
||||
-- the payload: decompressed, then its cpio entries by stripped name
|
||||
local function payload(b: buffer, p: number, e: number, compressor: string?, o: DecodeOptions): (any, Err.Err?)
|
||||
local n = e - p
|
||||
local opts: any = { maxBytes = o.maxBytes or 2 ^ 30 }
|
||||
local raw: buffer?, err: any
|
||||
local m = if n >= 6 then buffer.readstring(b, p, 6) else ""
|
||||
if string.sub(m, 1, 2) == "\31\139" then
|
||||
raw, err = Deflate.gunzip(b, p, n)
|
||||
elseif m == "\253" .. "7zXZ\0" then
|
||||
raw, err = Lzma.xz(b, p, n, opts)
|
||||
elseif string.sub(m, 1, 3) == "BZh" then
|
||||
raw, err = Bzip2.decompress(b, p, n, opts)
|
||||
elseif string.sub(m, 1, 4) == "\40\181\47\253" then
|
||||
raw, err = Zstd.decompress(b, p, n, opts)
|
||||
elseif compressor == "lzma" then
|
||||
raw, err = Lzma.alone(b, p, n, opts)
|
||||
elseif string.sub(m, 1, 5) == "07070" then
|
||||
raw = buffer.create(n)
|
||||
buffer.copy(raw :: buffer, 0, b, p, n)
|
||||
else
|
||||
return nil, Err.new(Err.UNSUPPORTED, "payload compressor " .. tostring(compressor), p)
|
||||
end
|
||||
if not raw then
|
||||
return nil, err
|
||||
end
|
||||
if buffer.len(raw) >= 6 and buffer.readstring(raw, 0, 6) == "07070X" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "stripped cpio payload (files over 4 GiB)")
|
||||
end
|
||||
local list, cerr = Cpio.read(raw, 0, nil, { maxEntries = o.maxEntries })
|
||||
if not list then
|
||||
return nil, cerr
|
||||
end
|
||||
local by = {}
|
||||
for _, ent in list do
|
||||
by[strip(ent.name)] = ent
|
||||
end
|
||||
return { buf = raw, by = by }, nil
|
||||
end
|
||||
|
||||
function Rpm.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local o: DecodeOptions = opts or {}
|
||||
local b, base, e = r.buf, r.base, r.limit
|
||||
if e - base < 96 then
|
||||
return nil, Err.new(Err.TRUNCATED, "lead past the end", base)
|
||||
end
|
||||
if buffer.readstring(b, base, 4) ~= LEAD_MAGIC then
|
||||
return nil, Err.new(Err.CORRUPT, "bad lead magic", base)
|
||||
end
|
||||
local major, minor = buffer.readu8(b, base + 4), buffer.readu8(b, base + 5)
|
||||
local kind = bit32.bor(bit32.lshift(buffer.readu8(b, base + 6), 8), buffer.readu8(b, base + 7))
|
||||
local leadName = buffer.readstring(b, base + 10, 66)
|
||||
leadName = string.match(leadName, "^[^%z]*") :: string
|
||||
if major < 3 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "rpm format version " .. major, base + 4)
|
||||
end
|
||||
local sig, sigEnd = header(b, base + 96, e)
|
||||
if not sig then
|
||||
return nil, sigEnd
|
||||
end
|
||||
local hp = base + (sigEnd - base + 7) // 8 * 8
|
||||
local tags, hEnd = header(b, hp, e)
|
||||
if not tags then
|
||||
return nil, hEnd
|
||||
end
|
||||
local a = Archive.new("rpm")
|
||||
local info: { [string]: any } = { tags = tags, signature = sig, type = if kind == 1 then "source" else "binary", lead = { major = major, minor = minor, name = leadName } }
|
||||
for tag, key in NAMES do
|
||||
local v = tags[tag]
|
||||
if type(v) == "table" and tag ~= 1047 and tag ~= 1049 then -- numbers and i18n strings: the first value
|
||||
v = v[1]
|
||||
end
|
||||
info[key] = v
|
||||
end
|
||||
a.meta.rpm = info
|
||||
a.meta.source = { format = "rpm", compressor = info.payloadCompressor }
|
||||
if info.payloadFormat and info.payloadFormat ~= "cpio" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "payload format " .. tostring(info.payloadFormat), hEnd)
|
||||
end
|
||||
local cache: any = nil
|
||||
local cacheErr: Err.Err? = nil
|
||||
local function entry(name: string): (any, Err.Err?)
|
||||
if not cache and not cacheErr then
|
||||
cache, cacheErr = payload(b, hEnd, e, info.payloadCompressor, o)
|
||||
end
|
||||
if not cache then
|
||||
return nil, cacheErr
|
||||
end
|
||||
local ent = cache.by[name]
|
||||
if not ent then
|
||||
return nil, Err.new(Err.CORRUPT, "file " .. name .. " is not in the payload")
|
||||
end
|
||||
return ent, nil
|
||||
end
|
||||
local list = files(tags)
|
||||
if #list > (o.maxEntries or 1e6) then
|
||||
return nil, Err.new(Err.LIMIT, "too many files")
|
||||
end
|
||||
for _, f in list do
|
||||
if bit32.band(f.flags, RPMFILE_GHOST) ~= 0 then
|
||||
continue
|
||||
end
|
||||
local name = strip(f.name)
|
||||
local typ = Cpio.TYPE_OF[bit32.band(f.mode, 0xF000)] or "0"
|
||||
local data = typ == "0" and f.size > 0
|
||||
Archive.put(a, {
|
||||
name = name,
|
||||
size = if data then f.size else 0,
|
||||
dir = typ == "5",
|
||||
mtime = f.mtime,
|
||||
mode = bit32.band(f.mode, 0xFFF),
|
||||
type = typ,
|
||||
link = if typ == "2" then f.link else nil,
|
||||
uname = f.user,
|
||||
gname = f.group,
|
||||
method = if info.payloadCompressor then info.payloadCompressor else "store",
|
||||
load = if data
|
||||
then function(): (buffer?, Err.Err?)
|
||||
local ent, err = entry(name)
|
||||
if not ent then
|
||||
return nil, err
|
||||
end
|
||||
local out = buffer.create(ent.size)
|
||||
buffer.copy(out, 0, cache.buf, ent.offset, ent.size)
|
||||
return out, nil
|
||||
end
|
||||
else nil,
|
||||
})
|
||||
end
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- cpio(r, opts?) -> the decompressed payload (a cpio archive, as rpm2cpio prints it) | nil, Err
|
||||
function Rpm.cpio(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?)
|
||||
local a, err = Rpm.decode(r, opts)
|
||||
if not a then
|
||||
return nil, err
|
||||
end
|
||||
local p = r.base + 96
|
||||
local _, sigEnd = header(r.buf, p, r.limit)
|
||||
local hp = r.base + (sigEnd - r.base + 7) // 8 * 8
|
||||
local _, hEnd = header(r.buf, hp, r.limit)
|
||||
local pl, perr = payload(r.buf, hEnd, r.limit, a.meta.rpm.payloadCompressor, opts or {})
|
||||
if not pl then
|
||||
return nil, perr
|
||||
end
|
||||
return pl.buf, nil
|
||||
end
|
||||
|
||||
return Rpm
|
||||
@@ -58,6 +58,7 @@ for m, t in TYPES do
|
||||
MODES[t] = m
|
||||
end
|
||||
Cpio.TYPE_MODE = MODES -- type letter -> S_IFMT bits
|
||||
Cpio.TYPE_OF = TYPES -- S_IFMT bits -> type letter
|
||||
|
||||
local TRAILER = "TRAILER!!!"
|
||||
|
||||
|
||||
Reference in new issue
Block a user