ws-arch2: archive/uu (uuencode / xxencode / base64 blocks, read + write), core/Crc16 (ccitt / arc), Archive.files helper, shared fixture helpers (_arch2.py / _arch2_ours.luau) and tests/archive/_entries.luau

core/Crc16 is new (CRC-16/XMODEM for BinHex / MacBinary, CRC-16/ARC for LHA / Zoo). containers/Archive gains
Archive.files(value, name?) (additive).
This commit is contained in:
4DBug committed 2026-10-31 13:58:54 -05:00
1 parent 51117f9732
commit 8cdb65367a
10 files changed
+679

No files matched your search

+12
View File
@@ -602,6 +602,18 @@ local list: { Entry } = {
return require("@formats/archive/tar")
end,
},
{
name = "uu",
category = "archive",
path = "archive/uu",
extensions = { "uu", "uue", "xxe", "b64" },
mime = { "text/x-uuencode" },
deps = { "core/Err", "core/Bytes", "core/Base64", "containers/Archive" },
caps = { decode = true, encode = true, variants = { "uu", "xx", "base64" } },
load = function()
return require("@formats/archive/uu")
end,
},
{
name = "xz",
category = "archive",
+276
View File
@@ -0,0 +1,276 @@
--!strict
-- archive/uu: uuencoded, xxencoded and GNU "begin-base64" text as an Archive: every "begin <mode> <name>" block of a
-- text (mail, news, shar parts: the text around the blocks is ignored) is one entry (entry.mode = the octal mode);
-- one block makes a single-stream archive (meta.stream: the api unwraps a uuencoded .tar.gz). The variant of each
-- block (uu / xx / base64) is detected from its characters and line lengths and kept in entry.variant and
-- meta.source.variant. Written from the POSIX uuencode description and the xxencode alphabet; written as GNU sharutils
-- does (45 bytes a line, "`" for zero, base64 lines of 60 characters + "===="). Not supported: multi-part postings
-- split over several files (concatenate the parts first), yEnc (archive/yenc), BinHex (archive/binhex).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Base64 = require("@formats/core/Base64")
local Archive = require("@formats/containers/Archive")
-- ------------------------------------------------------------------------------------------------ contract
local Uu = {
name = "uu",
category = "archive",
extensions = { "uu", "uue", "xxe", "b64" },
mime = { "text/x-uuencode" },
deps = { "core/Err", "core/Bytes", "core/Base64", "containers/Archive" },
caps = { decode = true, encode = true, variants = { "uu", "xx", "base64" } },
}
export type DecodeOptions = { maxBytes: number?, name: string? } -- maxBytes: default 2^30
export type EncodeOptions = {
variant: ("uu" | "xx" | "base64")?, -- default "uu"
mode: number?, -- file mode when the entry has none (default 0o644)
name: string?, -- the name of a buffer / string value (default "data")
}
-- ------------------------------------------------------------------------------------------------ constants
local XX = "+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
local XX_VALUE: { [number]: number } = {}
for i = 1, 64 do
XX_VALUE[string.byte(XX, i)] = i - 1
end
-- ------------------------------------------------------------------------------------------------ sniff
function Uu.sniff(r: Bytes.Reader): number
local head = r:peek(4096)
if string.match(head, "^begin%-base64 [0-7]+ [^\r\n]") or string.match(head, "^begin [0-7]+ [^\r\n]") then
return 0.9
end
if string.match(head, "\nbegin%-base64 [0-7]+ [^\r\n]") or string.match(head, "\nbegin [0-7][0-7][0-7]+ [^\r\n]") then
return 0.5
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
-- the value of character c in a variant, or nil
local function value(c: number, xx: boolean): number?
if xx then
return XX_VALUE[c]
end
if c >= 32 and c <= 96 then
return bit32.band(c - 32, 63)
end
return nil
end
-- does a data line fit the variant: every character valid and the length matching the count character
local function fits(line: string, xx: boolean): boolean
local n = value(string.byte(line, 1) or 0, xx)
if not n then
return false
end
local need = math.ceil(n / 3) * 4
if #line - 1 < need or #line - 1 > need + 2 then
return false
end
for i = 2, need + 1 do
if not value(string.byte(line, i), xx) then
return false
end
end
return true
end
-- decode the uu / xx lines of a block into a buffer (nil, message on a bad line)
local function decodeLines(lines: { string }, xx: boolean, maxBytes: number): (buffer?, string?)
local total = 0
for _, l in lines do
total += value(string.byte(l, 1) or 0, xx) or 0
end
if total > maxBytes then
return nil, "limit: data larger than maxBytes"
end
local out = buffer.create(total)
local o = 0
for li, l in lines do
local n = value(string.byte(l, 1) or 0, xx) or 0
local p = 2
local left = n
while left > 0 do
local a = value(string.byte(l, p) or 0, xx)
local b = value(string.byte(l, p + 1) or 0, xx)
local c = value(string.byte(l, p + 2) or 0, xx) or 0
local d = value(string.byte(l, p + 3) or 0, xx) or 0
if not a or not b then
return nil, "bad character in line " .. li
end
local v = a * 262144 + b * 4096 + c * 64 + d
for k = 2, 0, -1 do
if left > 0 then
buffer.writeu8(out, o, bit32.band(bit32.rshift(v, k * 8), 255))
o += 1
left -= 1
end
end
p += 4
end
end
return out, nil
end
type Block = { name: string, mode: number, variant: string, data: buffer }
local function parse(text: string, maxBytes: number): ({ Block }?, string?)
local lines = string.split(text, "\n")
local blocks: { Block } = {}
local i = 1
while i <= #lines do
local line = string.gsub(lines[i], "\r$", "")
local b64, mode, name = string.match(line, "^begin(%-base64) ([0-7]+) (.+)$")
if not b64 then
mode, name = string.match(line, "^begin ([0-7]+) (.+)$")
end
if mode and name then
local body: { string } = {}
local closed = false
i += 1
while i <= #lines do
local l = string.gsub(lines[i], "\r$", "")
if (b64 and l == "====") or (not b64 and l == "end") then
closed = true
break
end
table.insert(body, l)
i += 1
end
if not closed then
return nil, "truncated: block " .. name .. " has no end line"
end
local data, variant
if b64 then
local d, err = Base64.decode(table.concat(body))
if not d then
return nil, "block " .. name .. ": " .. tostring(err)
end
data, variant = d, "base64"
else
-- drop the empty-line terminator ("`" / " " in uu, "+" in xx) and blank lines
local data_lines: { string } = {}
for _, l in body do
if l ~= "" then
table.insert(data_lines, l)
end
end
local uuOk, xxOk = true, true
for _, l in data_lines do
uuOk = uuOk and fits(l, false)
xxOk = xxOk and fits(l, true)
end
local xx = xxOk and not uuOk
if not uuOk and not xxOk then
return nil, "block " .. name .. ": lines are neither uuencode nor xxencode"
end
local d, err = decodeLines(data_lines, xx, maxBytes)
if not d then
return nil, err
end
data, variant = d, if xx then "xx" else "uu"
end
table.insert(blocks, { name = name, mode = tonumber(mode, 8) or 0, variant = variant, data = data })
end
i += 1
end
if #blocks == 0 then
return nil, "no begin line"
end
return blocks, nil
end
local function encoder(a: any, o: any?): (buffer?, Err.Err?)
return Uu.encode(a, o)
end
function Uu.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local o: DecodeOptions = opts or {}
local blocks, err = parse(buffer.readstring(r.buf, r.base, r.limit - r.base), o.maxBytes or 2 ^ 30)
if not blocks then
return nil, Err.from(err)
end
local a = Archive.new("uu", encoder)
for _, b in blocks do
Archive.put(a, { name = b.name, size = buffer.len(b.data), dir = false, mode = b.mode, variant = b.variant, method = b.variant, data = b.data })
end
a.meta.stream = #a:list() == 1
a.meta.source = { format = "uu", variant = blocks[1].variant }
return a, nil
end
-- ------------------------------------------------------------------------------------------------ encode
local function encodeLines(data: buffer, xx: boolean, out: { string })
local n = buffer.len(data)
local function ch(v: number): string
if xx then
return string.sub(XX, v + 1, v + 1)
end
return if v == 0 then "`" else string.char(v + 32)
end
local p = 0
while p < n do
local len = math.min(45, n - p)
local parts = { ch(len) }
for q = p, p + len - 1, 3 do
local b0 = buffer.readu8(data, q)
local b1 = if q + 1 < p + len then buffer.readu8(data, q + 1) else 0
local b2 = if q + 2 < p + len then buffer.readu8(data, q + 2) else 0
local v = b0 * 65536 + b1 * 256 + b2
table.insert(parts, ch(bit32.rshift(v, 18)) .. ch(bit32.band(bit32.rshift(v, 12), 63)) .. ch(bit32.band(bit32.rshift(v, 6), 63)) .. ch(bit32.band(v, 63)))
end
table.insert(out, table.concat(parts))
p += len
end
table.insert(out, ch(0))
end
function Uu.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local o: EncodeOptions = opts or {}
local variant = o.variant or "uu"
if variant ~= "uu" and variant ~= "xx" and variant ~= "base64" then
return nil, Err.new(Err.ARG, "variant must be uu, xx or base64")
end
local files, err = Archive.files(value, o.name)
if not files then
return nil, err
end
if #files == 0 then
return nil, Err.new(Err.ARG, "nothing to encode")
end
local out: { string } = {}
for _, f in files do
if string.find(f.name, "[\r\n]") then
return nil, Err.new(Err.ARG, "file names may not contain line breaks")
end
local mode = if type(f.entry.mode) == "number" then f.entry.mode else (o.mode or 0x1A4)
local m = string.format("%o", bit32.band(mode, 0xFFF))
if variant == "base64" then
table.insert(out, "begin-base64 " .. m .. " " .. f.name)
local s = Base64.encode(f.data)
for p = 1, #s, 60 do
table.insert(out, string.sub(s, p, p + 59))
end
table.insert(out, "====")
else
table.insert(out, "begin " .. m .. " " .. f.name)
encodeLines(f.data, variant == "xx", out)
table.insert(out, "end")
end
end
return buffer.fromstring(table.concat(out, "\n") .. "\n"), nil
end
-- ------------------------------------------------------------------------------------------------ extras
-- new() -> an empty uu Archive: a:add(name, data, { mode }); a:write({ variant })
function Uu.new(): any
local a = Archive.new("uu", encoder)
a.meta.source = { format = "uu" }
return a
end
return Uu
+24
View File
@@ -152,6 +152,30 @@ function Archive.entries(value: any): { { entry: Types.ArchiveEntry, read: () ->
return out
end
-- files(value, name?) -> { { name, data, entry } } | nil, Err: the files (directories skipped) of any Archive value,
-- read; a buffer or a string is one file named name (default "data"). What single-file and encoding writers take.
function Archive.files(value: any, name: string?): ({ { name: string, data: buffer, entry: any } }?, Err.Err?)
if type(value) == "buffer" or type(value) == "string" then
local b = if type(value) == "string" then buffer.fromstring(value) else value
return { { name = name or "data", data = b, entry = { name = name or "data", size = buffer.len(b), dir = false } } }, nil
end
local items = Archive.entries(value)
if not items then
return nil, Err.new(Err.ARG, "expected an Archive, a buffer or a string")
end
local out = {}
for _, it in items do
if not it.entry.dir then
local b, err = it.read()
if not b then
return nil, err
end
table.insert(out, { name = it.entry.name, data = b, entry = it.entry })
end
end
return out, nil
end
-- streamName(opts, ext) -> the entry name for a single-stream archive: opts.name without its compression extension
-- (".tgz" -> ".tar"), else "data"
function Archive.streamName(opts: any?, exts: { string }): string
+52
View File
@@ -0,0 +1,52 @@
--!strict
--!native
-- core/Crc16: the two 16-bit CRCs old archive and encoding formats use, table driven. New code.
-- Crc16.ccitt(buf, off, len, crc?) CRC-16/XMODEM: polynomial 0x1021, MSB first, init 0, no final XOR (BinHex 4.0,
-- MacBinary II / III headers)
-- Crc16.arc(buf, off, len, crc?) CRC-16/ARC: polynomial 0x8005 reflected (0xA001), init 0 (LHA, Zoo, ARC)
-- Pass a previous result as crc to continue it. Never throws: the range is clamped to the buffer.
local band, bxor, rshift, lshift = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift
local readu8 = buffer.readu8
local MSB = table.create(256, 0)
local ARC = table.create(256, 0)
for n = 0, 255 do
local c = lshift(n, 8)
for _ = 1, 8 do
c = if band(c, 0x8000) ~= 0 then bxor(band(lshift(c, 1), 0xFFFF), 0x1021) else band(lshift(c, 1), 0xFFFF)
end
MSB[n + 1] = c
local r = n
for _ = 1, 8 do
r = if band(r, 1) ~= 0 then bxor(rshift(r, 1), 0xA001) else rshift(r, 1)
end
ARC[n + 1] = r
end
local Crc16 = {}
local function clamp(buf: buffer, off: number, len: number): (number, number)
local n = buffer.len(buf)
local o = math.clamp(math.floor(off), 0, n)
return o, math.clamp(math.floor(len), 0, n - o)
end
function Crc16.ccitt(buf: buffer, off: number, len: number, crc: number?): number
local o, l = clamp(buf, off, len)
local c = crc or 0
for i = o, o + l - 1 do
c = bxor(band(lshift(c, 8), 0xFFFF), MSB[bxor(rshift(c, 8), readu8(buf, i)) + 1])
end
return c
end
function Crc16.arc(buf: buffer, off: number, len: number, crc: number?): number
local o, l = clamp(buf, off, len)
local c = crc or 0
for i = o, o + l - 1 do
c = bxor(rshift(c, 8), ARC[bxor(band(c, 0xFF), readu8(buf, i)) + 1])
end
return c
end
return Crc16