--!strict -- archive/uu: uuencode (POSIX), xxencode and GNU "begin-base64" blocks, one entry each (variant detected per block); -- written as GNU sharutils does (45 bytes a line, "`" for zero; base64 60 characters + "===="). Not supported: postings -- split over several files, yEnc (archive/yenc), BinHex (archive/binhex). -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") local Base64 = require("@rosetta/core/Base64") local Archive = require("@rosetta/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? } -- default 2^30 export type EncodeOptions = { variant: ("uu" | "xx" | "base64")?, -- default "uu" mode: number?, -- default 0o644 name: string?, } -- ------------------------------------------------------------------------------------------------ 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 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 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 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 function Uu.new(): any local a = Archive.new("uu", encoder) a.meta.source = { format = "uu" } return a end return Uu