271 lines
8.3 KiB
Luau
271 lines
8.3 KiB
Luau
--!strict
|
|
-- archive/yenc: yEnc 1.3 (=ybegin / =ypart / =yend, byte + 42, "=" escapes; Jürgen Helbing's draft): parts in one text
|
|
-- joined, pcrc32 / crc32 / size verified; writes one 128-character-line part per file, escaping NUL, LF, CR, "=" and a
|
|
-- leading / trailing TAB, space or ".", as uuenview / yencode do. Not supported: parts spread over several files.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Archive = require("@rosetta/containers/Archive")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Yenc = {
|
|
name = "yenc",
|
|
category = "archive",
|
|
extensions = { "yenc", "ync" },
|
|
mime = { "application/x-yenc" },
|
|
deps = { "core/Err", "core/Bytes", "core/Crc32", "containers/Archive" },
|
|
caps = { decode = true, encode = true, multipart = true },
|
|
}
|
|
|
|
export type DecodeOptions = { maxBytes: number?, name: string? } -- default 2^30
|
|
export type EncodeOptions = {
|
|
line: number?, -- default 128
|
|
name: string?,
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Yenc.sniff(r: Bytes.Reader): number
|
|
local head = r:peek(4096)
|
|
if string.match(head, "^=ybegin ") then
|
|
return 0.95
|
|
end
|
|
return if string.find(head, "\n=ybegin [^\n]*name=") then 0.6 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- key=value fields of a keyword line; name= runs to the end of the line
|
|
local function fields(line: string): { [string]: string }
|
|
local f: { [string]: string } = {}
|
|
local s, name = string.match(line, "^(.-)name=(.*)$")
|
|
if s then
|
|
f.name = string.gsub(name :: string, "%s+$", "")
|
|
line = s
|
|
end
|
|
for k, v in string.gmatch(line, "(%w+)=(%S+)") do
|
|
f[k] = v
|
|
end
|
|
return f
|
|
end
|
|
|
|
local function decodeData(src: buffer, i: number, j: number, out: buffer, o: number): number
|
|
local lim = buffer.len(out)
|
|
local esc = false
|
|
for p = i, j - 1 do
|
|
local c = buffer.readu8(src, p)
|
|
if c == 13 or c == 10 then
|
|
continue
|
|
end
|
|
if c == 61 and not esc then
|
|
esc = true
|
|
continue
|
|
end
|
|
if esc then
|
|
c -= 64
|
|
esc = false
|
|
end
|
|
if o < lim then
|
|
buffer.writeu8(out, o, (c - 42) % 256)
|
|
end
|
|
o += 1
|
|
end
|
|
return o
|
|
end
|
|
|
|
local function countData(src: buffer, i: number, j: number): number
|
|
local n, esc = 0, false
|
|
for p = i, j - 1 do
|
|
local c = buffer.readu8(src, p)
|
|
if c ~= 13 and c ~= 10 then
|
|
if c == 61 and not esc then
|
|
esc = true
|
|
else
|
|
esc = false
|
|
n += 1
|
|
end
|
|
end
|
|
end
|
|
return n
|
|
end
|
|
|
|
type Part = { first: number, last: number, from: number, to: number, pcrc: number?, part: number? }
|
|
type File = { name: string, size: number, crc: number?, parts: { Part }, total: number? }
|
|
|
|
local function hex(s: string?): number?
|
|
return if s and string.match(s, "^%x+$") and #s <= 8 then tonumber(s, 16) else nil
|
|
end
|
|
|
|
local function parse(b: buffer, base: number, limit: number): ({ File }?, string?)
|
|
local files: { File } = {}
|
|
local byName: { [string]: File } = {}
|
|
local p = base
|
|
local cur: File? = nil
|
|
local part: Part? = nil
|
|
while p < limit do
|
|
local nl = p
|
|
while nl < limit and buffer.readu8(b, nl) ~= 10 do
|
|
nl += 1
|
|
end
|
|
local isKey = nl - p >= 2 and buffer.readu8(b, p) == 61 and buffer.readu8(b, p + 1) == 121 -- "=y"
|
|
if isKey then
|
|
local line = string.gsub(buffer.readstring(b, p, nl - p), "\r$", "")
|
|
local kw = string.match(line, "^=y(%a+)")
|
|
local f = fields(line)
|
|
if kw == "begin" then
|
|
local name = f.name
|
|
local size = tonumber(f.size or "")
|
|
if not name or name == "" or not size or size < 0 then
|
|
return nil, "=ybegin without name or size"
|
|
end
|
|
local file: File? = byName[name]
|
|
if not file then
|
|
local nf: File = { name = name :: string, size = size, parts = {}, total = tonumber(f.total or "") }
|
|
byName[name] = nf
|
|
table.insert(files, nf)
|
|
file = nf
|
|
end
|
|
cur = file
|
|
part = { first = 0, last = size, from = nl + 1, to = nl + 1, part = tonumber(f.part or "") }
|
|
elseif kw == "part" and part then
|
|
local first, last = tonumber(f.begin or ""), tonumber(f["end"] or "")
|
|
if not first or not last or first < 1 or last < first - 1 then
|
|
return nil, "bad =ypart range"
|
|
end
|
|
part.first, part.last = first - 1, last
|
|
part.from, part.to = nl + 1, nl + 1
|
|
elseif kw == "end" and part and cur then
|
|
part.pcrc = hex(f.pcrc32)
|
|
if f.crc32 then
|
|
cur.crc = hex(f.crc32)
|
|
end
|
|
local size = tonumber(f.size or "")
|
|
if size and size ~= part.last - part.first then
|
|
return nil, "=yend size " .. size .. " does not match the part (" .. (part.last - part.first) .. ")"
|
|
end
|
|
table.insert(cur.parts, part)
|
|
part = nil
|
|
end
|
|
elseif part then
|
|
part.to = nl + 1
|
|
end
|
|
p = nl + 1
|
|
end
|
|
if part then
|
|
return nil, "truncated: no =yend"
|
|
end
|
|
if #files == 0 then
|
|
return nil, "no =ybegin line"
|
|
end
|
|
return files, nil
|
|
end
|
|
|
|
local function encoder(a: any, o: any?): (buffer?, Err.Err?)
|
|
return Yenc.encode(a, o)
|
|
end
|
|
|
|
function Yenc.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
|
local o: DecodeOptions = opts or {}
|
|
local maxBytes = o.maxBytes or 2 ^ 30
|
|
local b = r.buf
|
|
local files, perr = parse(b, r.base, math.min(r.limit, buffer.len(b)))
|
|
if not files then
|
|
return nil, Err.from(perr)
|
|
end
|
|
local a = Archive.new("yenc", encoder)
|
|
local incomplete = {}
|
|
for _, f in files do
|
|
if f.size > maxBytes then
|
|
return nil, Err.new(Err.LIMIT, f.name .. ": size over maxBytes")
|
|
end
|
|
local out = buffer.create(f.size)
|
|
local covered = 0
|
|
for _, pt in f.parts do
|
|
local n = countData(b, pt.from, pt.to)
|
|
if n ~= pt.last - pt.first or pt.last > f.size then
|
|
return nil, Err.new(Err.CORRUPT, f.name .. ": part data is " .. n .. " bytes, the header says " .. (pt.last - pt.first))
|
|
end
|
|
local tmp = buffer.create(n)
|
|
decodeData(b, pt.from, pt.to, tmp, 0)
|
|
if pt.pcrc and Crc32.of(tmp) ~= pt.pcrc then
|
|
return nil, Err.new(Err.CHECKSUM, f.name .. ": part CRC mismatch")
|
|
end
|
|
buffer.copy(out, pt.first, tmp, 0, n)
|
|
covered += n
|
|
end
|
|
local whole = covered == f.size
|
|
if whole and f.crc and Crc32.of(out) ~= f.crc then
|
|
return nil, Err.new(Err.CHECKSUM, f.name .. ": CRC mismatch")
|
|
end
|
|
if not whole then
|
|
table.insert(incomplete, f.name)
|
|
end
|
|
Archive.put(a, { name = f.name, size = f.size, dir = false, method = "yenc", data = out, complete = whole })
|
|
end
|
|
a.meta.stream = #files == 1
|
|
a.meta.incomplete = if #incomplete > 0 then incomplete else nil
|
|
a.meta.source = { format = "yenc" }
|
|
return a, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Yenc.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
local o: EncodeOptions = opts or {}
|
|
local lineLen = o.line or 128
|
|
if type(lineLen) ~= "number" or lineLen < 2 or lineLen > 997 then
|
|
return nil, Err.new(Err.ARG, "line must be 2..997")
|
|
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 w = Bytes.writer()
|
|
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 data = f.data
|
|
local n = buffer.len(data)
|
|
w:string(string.format("=ybegin line=%d size=%d name=%s\r\n", lineLen, n, f.name))
|
|
local col = 0
|
|
for i = 0, n - 1 do
|
|
local c = (buffer.readu8(data, i) + 42) % 256
|
|
local critical = c == 0 or c == 10 or c == 13 or c == 61
|
|
if not critical and (c == 9 or c == 32) and (col == 0 or col >= lineLen - 1 or i == n - 1) then
|
|
critical = true
|
|
elseif not critical and c == 46 and col == 0 then
|
|
critical = true
|
|
end
|
|
if critical then
|
|
w:u8(61)
|
|
w:u8((c + 64) % 256)
|
|
col += 2
|
|
else
|
|
w:u8(c)
|
|
col += 1
|
|
end
|
|
if col >= lineLen and i < n - 1 then
|
|
w:string("\r\n")
|
|
col = 0
|
|
end
|
|
end
|
|
if n > 0 then
|
|
w:string("\r\n")
|
|
end
|
|
w:string(string.format("=yend size=%d crc32=%08x\r\n", n, Crc32.of(data)))
|
|
end
|
|
return w:finish(), nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ extras
|
|
function Yenc.new(): any
|
|
local a = Archive.new("yenc", encoder)
|
|
a.meta.source = { format = "yenc" }
|
|
return a
|
|
end
|
|
|
|
return Yenc
|