--!strict -- containers/Cpio: cpio archives, read and written. Reading: SVR4 "newc" (070701, hex fields, 4-byte alignment), -- "crc" (070702: newc plus a byte-sum of the data, verified), POSIX.1 "odc" (070707, octal fields, no padding) and the -- old binary format in both byte orders (magic 070707 as a little / big endian u16, 2-byte alignment), up to the -- TRAILER!!! entry. Hard links: newc / crc store the data once, on the last link of an inode (as GNU cpio and bsdtar -- write it); every link of the group reads that data, and the later links name the first one in `hardlink`. Symlinks -- hold their target as data (`link`). Writing: newc (default), crc or odc, padded to opts.block bytes (default 512, as -- GNU cpio). Written from the cpio(5) manual pages (libarchive, GNU cpio) and POSIX.1-1988 (odc); no code ported. The -- archive formats (archive/cpio, archive/rpm's payload) build on it. -- -- local entries, info = Cpio.read(buf, offset?, length?, opts?) -- { Entry } , { format = "newc" | ... } | nil, Err -- local out, err = Cpio.write({ { name = "a.txt", data = buf, mode = 0o100644, mtime = 0 } }, { format = "odc" }) -- -- Never throws: bad data returns nil, Err. local Err = require("@formats/core/Err") local Bytes = require("@formats/core/Bytes") local Cpio = {} export type Entry = { name: string, size: number, -- data bytes (a symlink's target length; a hard link: the shared data's size) mode: number, -- the whole st_mode (file type bits included) type: string, -- tar type letters: "0" file, "5" directory, "2" symlink, "3" char device, "4" block device, "6" fifo, "s" socket dir: boolean, uid: number, gid: number, nlink: number, mtime: number, ino: number, devmajor: number, devminor: number, rdevmajor: number, rdevminor: number, link: string?, -- symlink target hardlink: string?, -- the first entry of this entry's hard link group (for the later links) offset: number, -- absolute offset of the data in the buffer check: number?, -- crc format: the stored byte sum } export type ReadOptions = { maxEntries: number?, -- default 1e6 verify: boolean?, -- crc format: check the byte sums (default true) } export type Info = { format: string } -- "newc" | "crc" | "odc" | "bin" | "binbe" local S_IFMT = 0xF000 local TYPES: { [number]: string } = { [0x8000] = "0", [0x4000] = "5", [0xA000] = "2", [0x2000] = "3", [0x6000] = "4", [0x1000] = "6", [0xC000] = "s", } local MODES: { [string]: number } = {} for m, t in TYPES do MODES[t] = m end Cpio.TYPE_MODE = MODES -- type letter -> S_IFMT bits local TRAILER = "TRAILER!!!" -- format(buf, offset) -> "newc" | "crc" | "odc" | "bin" | "binbe" | nil: the format of the header at offset function Cpio.format(b: buffer, o: number): string? local n = buffer.len(b) if o + 6 <= n then local m = buffer.readstring(b, o, 6) if m == "070701" then return "newc" elseif m == "070702" then return "crc" elseif m == "070707" then return "odc" end end if o + 2 <= n then local v = buffer.readu16(b, o) if v == 0x71C7 then return "bin" elseif v == 0xC771 then return "binbe" end end return nil end -- a fixed-width number field: hex (newc) or octal (odc); nil when not all digits local function num(b: buffer, o: number, n: number, base: number): number? local s = buffer.readstring(b, o, n) if base == 16 then if not string.find(s, "^%x+$") then return nil end elseif not string.find(s, "^[0-7]+$") then return nil end return tonumber(s, base) end -- checksum(buf, offset, n) -> the crc format's sum of the data bytes (mod 2^32) function Cpio.checksum(b: buffer, o: number, n: number): number local s = 0 local i = o local e = o + n while i + 4 <= e do local v = buffer.readu32(b, i) s += bit32.band(v, 255) + bit32.band(bit32.rshift(v, 8), 255) + bit32.band(bit32.rshift(v, 16), 255) + bit32.rshift(v, 24) i += 4 end while i < e do s += buffer.readu8(b, i) i += 1 end return s % 4294967296 end -- one header at p -> entry fields, header end (the name's start), name size, data size | nil, Err local function header(b: buffer, p: number, e: number, fmt: string): (any, Err.Err?) local h: any = {} local nameAt if fmt == "newc" or fmt == "crc" then if p + 110 > e then return nil, Err.new(Err.TRUNCATED, "header past the end", p) end local f = {} :: { number } for i = 1, 13 do local v = num(b, p + 6 + (i - 1) * 8, 8, 16) if not v then return nil, Err.new(Err.CORRUPT, "bad header field", p + 6 + (i - 1) * 8) end f[i] = v end h.ino, h.mode, h.uid, h.gid, h.nlink, h.mtime, h.size = f[1], f[2], f[3], f[4], f[5], f[6], f[7] h.devmajor, h.devminor, h.rdevmajor, h.rdevminor, h.namesize, h.check = f[8], f[9], f[10], f[11], f[12], f[13] nameAt = p + 110 elseif fmt == "odc" then if p + 76 > e then return nil, Err.new(Err.TRUNCATED, "header past the end", p) end local widths = { 6, 6, 6, 6, 6, 6, 6, 11, 6, 11 } local f = {} :: { number } local q = p + 6 for i, w in widths do local v = num(b, q, w, 8) if not v then return nil, Err.new(Err.CORRUPT, "bad header field", q) end f[i] = v q += w end h.ino, h.mode, h.uid, h.gid, h.nlink, h.mtime, h.namesize, h.size = f[2], f[3], f[4], f[5], f[6], f[8], f[9], f[10] h.devmajor, h.devminor = bit32.rshift(f[1], 8), bit32.band(f[1], 255) h.rdevmajor, h.rdevminor = bit32.rshift(f[7], 8), bit32.band(f[7], 255) nameAt = p + 76 else if p + 26 > e then return nil, Err.new(Err.TRUNCATED, "header past the end", p) end local be = fmt == "binbe" local function u16(i: number): number local v = buffer.readu16(b, p + i * 2) return if be then bit32.bor(bit32.rshift(v, 8), bit32.band(bit32.lshift(v, 8), 0xFF00)) else v end local dev, rdev = u16(1), u16(7) h.ino, h.mode, h.uid, h.gid, h.nlink = u16(2), u16(3), u16(4), u16(5), u16(6) h.mtime = u16(8) * 65536 + u16(9) h.namesize = u16(10) h.size = u16(11) * 65536 + u16(12) h.devmajor, h.devminor = bit32.rshift(dev, 8), bit32.band(dev, 255) h.rdevmajor, h.rdevminor = bit32.rshift(rdev, 8), bit32.band(rdev, 255) nameAt = p + 26 end if h.namesize < 1 or nameAt + h.namesize > e then return nil, Err.new(if h.namesize < 1 then Err.CORRUPT else Err.TRUNCATED, "bad name size", nameAt) end h.nameAt = nameAt return h, nil end -- read(buf, offset?, length?, opts?) -> { Entry }, Info | nil, Err function Cpio.read(b: buffer, offset: number?, length: number?, opts: ReadOptions?): ({ Entry }?, any) local o = offset or 0 local e = math.min(buffer.len(b), o + (length or (buffer.len(b) - o))) local maxEntries = (opts and opts.maxEntries) or 1e6 local verify = not (opts and opts.verify == false) local fmt = Cpio.format(b, o) if not fmt or o >= e then return nil, Err.new(Err.CORRUPT, "not a cpio archive", o) end local align = if fmt == "newc" or fmt == "crc" then 4 elseif fmt == "odc" then 1 else 2 local out: { Entry } = {} local groups: { [string]: { Entry } } = {} local p = o while true do if p >= e then return nil, Err.new(Err.TRUNCATED, "no TRAILER!!! entry", p) end if Cpio.format(b, p) ~= fmt then return nil, Err.new(Err.CORRUPT, "bad header magic", p) end local h, herr = header(b, p, e, fmt) if not h then return nil, herr end local name = buffer.readstring(b, h.nameAt, h.namesize) local z = string.find(name, "\0", 1, true) if z then name = string.sub(name, 1, z - 1) end local data = h.nameAt + h.namesize data = o + (data - o + align - 1) // align * align if name == TRAILER then break end if data + h.size > e then return nil, Err.new(Err.TRUNCATED, "entry " .. name .. " data past the end", data) end local typ = TYPES[bit32.band(h.mode, S_IFMT)] or "0" local entry: Entry = { name = name, size = h.size, mode = h.mode, type = typ, dir = typ == "5", uid = h.uid, gid = h.gid, nlink = h.nlink, mtime = h.mtime, ino = h.ino, devmajor = h.devmajor, devminor = h.devminor, rdevmajor = h.rdevmajor, rdevminor = h.rdevminor, link = if typ == "2" then buffer.readstring(b, data, h.size) else nil, hardlink = nil, offset = data, check = if fmt == "crc" then h.check else nil, } if fmt == "crc" and verify and typ == "0" and Cpio.checksum(b, data, h.size) ~= h.check then return nil, Err.new(Err.CHECKSUM, "entry " .. name .. ": data checksum mismatch", data) end if typ == "0" and h.nlink > 1 then local key = h.devmajor .. ":" .. h.devminor .. ":" .. h.ino local g = groups[key] if not g then g = {} groups[key] = g else entry.hardlink = g[1].name end table.insert(g, entry) end table.insert(out, entry) if #out > maxEntries then return nil, Err.new(Err.LIMIT, "more than " .. maxEntries .. " entries") end p = data + h.size p = o + (p - o + align - 1) // align * align end -- hard links: the links stored without data read the group's data (newc / crc keep it on the last link) for _, g in groups do local holder: Entry? = nil for _, x in g do if x.size > 0 then holder = x end end if holder then for _, x in g do if x.size == 0 then x.size, x.offset = holder.size, holder.offset end end end end return out, { format = fmt } end -- ------------------------------------------------------------------------------------------------ writing export type WriteEntry = { name: string, data: buffer?, -- file data (a symlink: its target, else taken from link) mode: number?, -- st_mode; permission bits alone get the type bits from type / dir (default 0o644 / 0o755) type: string?, -- a type letter (see Entry.type); default "0" ("5" for dir) dir: boolean?, link: string?, -- symlink target uid: number?, gid: number?, nlink: number?, -- default 1 (2 for directories) mtime: number?, -- unix seconds (default 0) ino: number?, -- default: the entry's index (1, 2, ...); hard links share one devmajor: number?, devminor: number?, rdevmajor: number?, rdevminor: number?, } export type WriteOptions = { format: ("newc" | "crc" | "odc")?, -- default "newc" block: number?, -- pad the archive to a multiple of this many bytes (default 512; 1 = no padding) } local function hex8(v: number): string? if v < 0 or v % 1 ~= 0 or v >= 4294967296 then return nil end return string.format("%08X", v) end local function oct(v: number, w: number): string? if v < 0 or v % 1 ~= 0 or v >= 8 ^ w then return nil end local s = string.format("%o", v) return string.rep("0", w - #s) .. s end -- write(entries, opts?) -> buffer | nil, Err function Cpio.write(entries: { WriteEntry }, opts: WriteOptions?): (buffer?, Err.Err?) local fmt = (opts and opts.format) or "newc" if fmt ~= "newc" and fmt ~= "crc" and fmt ~= "odc" then return nil, Err.new(Err.ARG, "format must be newc, crc or odc") end local block = (opts and opts.block) or 512 if type(block) ~= "number" or block < 1 or block % 1 ~= 0 then return nil, Err.new(Err.ARG, "block must be a positive integer") end -- newc / crc: a hard link group's data goes on its last link only local last: { [string]: number } = {} local prepared: { any } = table.create(#entries) for i, e in entries do if type(e) ~= "table" or type(e.name) ~= "string" or e.name == "" then return nil, Err.new(Err.ARG, "entry " .. i .. ": needs a name") end local typ = e.type or (if e.dir then "5" else "0") local tbits = MODES[typ] if not tbits then return nil, Err.new(Err.ARG, "entry " .. e.name .. ": unknown type " .. tostring(typ)) end local mode = e.mode or (if typ == "5" then 0x1ED elseif typ == "2" then 0x1FF else 0x1A4) if bit32.band(mode, S_IFMT) == 0 then mode = bit32.bor(mode, tbits) end local data = e.data if typ == "2" and not data then data = buffer.fromstring(e.link or "") elseif typ ~= "0" and typ ~= "2" then data = nil end local nlink = e.nlink or (if typ == "5" then 2 else 1) local ino = e.ino or i local p = { e = e, mode = mode, data = data or buffer.create(0), nlink = nlink, ino = ino, typ = typ } prepared[i] = p if typ == "0" and nlink > 1 then last[(e.devmajor or 0) .. ":" .. (e.devminor or 0) .. ":" .. ino] = i end end local w = Bytes.writer(1024) local function put(name: string, p: any, data: buffer): Err.Err? local e = p.e local namesize = #name + 1 local size = buffer.len(data) if fmt == "odc" then local dev = (e.devmajor or 0) * 256 + (e.devminor or 0) local rdev = (e.rdevmajor or 0) * 256 + (e.rdevminor or 0) local fields = { oct(dev, 6), oct(p.ino, 6), oct(p.mode, 6), oct(e.uid or 0, 6), oct(e.gid or 0, 6), oct(p.nlink, 6), oct(rdev, 6), oct(math.floor(e.mtime or 0), 11), oct(namesize, 6), oct(size, 11), } for k = 1, 10 do if not fields[k] then return Err.new(Err.ARG, "entry " .. name .. ": a field does not fit the odc header") end end w:string("070707" .. table.concat(fields :: { string })) w:string(name) w:u8(0) w:bytes(data) else local check = if fmt == "crc" and p.typ == "0" then Cpio.checksum(data, 0, size) else 0 local fields = { hex8(p.ino), hex8(p.mode), hex8(e.uid or 0), hex8(e.gid or 0), hex8(p.nlink), hex8(math.floor(e.mtime or 0)), hex8(size), hex8(e.devmajor or 0), hex8(e.devminor or 0), hex8(e.rdevmajor or 0), hex8(e.rdevminor or 0), hex8(namesize), hex8(check), } for k = 1, 13 do if not fields[k] then return Err.new(Err.ARG, "entry " .. name .. ": a field does not fit the newc header") end end w:string((if fmt == "crc" then "070702" else "070701") .. table.concat(fields :: { string })) w:string(name) w:u8(0) w:fill(0, (4 - (110 + namesize) % 4) % 4) w:bytes(data) w:fill(0, (4 - size % 4) % 4) end return nil end for i, p in prepared do local data = p.data if fmt ~= "odc" and p.typ == "0" and p.nlink > 1 then local e = p.e if last[(e.devmajor or 0) .. ":" .. (e.devminor or 0) .. ":" .. p.ino] ~= i then data = buffer.create(0) end end local err = put(p.e.name, p, data) if err then return nil, err end end local err = put(TRAILER, { e = {}, ino = 0, mode = 0, nlink = 1 }, buffer.create(0)) if err then return nil, err end w:fill(0, (block - w.pos % block) % block) return w:finish(), nil end return Cpio