ws-arch2: archive/arj (methods 0-4 via codecs/compress/Lzh, CRC-32 checks; read)
This commit is contained in:
1 parent
572cd42f8a
commit
70cc54f7c8
5 files changed
+333
No files matched your search
@@ -578,6 +578,18 @@ local list: { Entry } = {
|
||||
return require("@formats/archive/applesingle")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "arj",
|
||||
category = "archive",
|
||||
path = "archive/arj",
|
||||
extensions = { "arj" },
|
||||
mime = { "application/x-arj" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Crc32", "codecs/compress/Lzh", "containers/Archive" },
|
||||
caps = { decode = true, encode = false, methods = { "store", "arj1", "arj2", "arj3", "arj4" } },
|
||||
load = function()
|
||||
return require("@formats/archive/arj")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "binhex",
|
||||
category = "archive",
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
--!strict
|
||||
-- archive/arj: ARJ archives (Robert Jung) read into an Archive: the main header (archive name, comment, host OS,
|
||||
-- dates) and every local header (CRC-32 checked), files stored (method 0), compressed with methods 1..3 (the LHA-style
|
||||
-- static Huffman LZSS of codecs/compress/Lzh, "lh6" tables) or 4 (codecs/compress/Lzh "arj4"), directories (file type
|
||||
-- 3), DOS times (read as UTC), file modes, comments; each file's CRC-32 is verified when read (Err.CHECKSUM).
|
||||
-- Written from the ARJ technical information (arj's technote.txt). Not supported: garbled (encrypted) files,
|
||||
-- multi-volume archives (entries continued to / from another volume), chapters, writing (Err.UNSUPPORTED for those
|
||||
-- entries / calls).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Lzh = require("@formats/codecs/compress/Lzh")
|
||||
local Archive = require("@formats/containers/Archive")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Arj = {
|
||||
name = "arj",
|
||||
category = "archive",
|
||||
extensions = { "arj" },
|
||||
mime = { "application/x-arj" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Crc32", "codecs/compress/Lzh", "containers/Archive" },
|
||||
caps = { decode = true, encode = false, methods = { "store", "arj1", "arj2", "arj3", "arj4" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse an entry larger than this (default 2^30)
|
||||
maxEntries: number?, -- default 65536
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local OS = { [0] = "msdos", "primos", "unix", "amiga", "macos", "os2", "appleiigs", "atarist", "next", "vaxvms", "win95", "win32" }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Arj.sniff(r: Bytes.Reader): number
|
||||
local h = r:peek(4)
|
||||
if #h == 4 and string.byte(h, 1) == 0x60 and string.byte(h, 2) == 0xEA then
|
||||
local size = string.byte(h, 3) + string.byte(h, 4) * 256
|
||||
return if size > 0 and size <= 2600 then 0.9 else 0
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function dosTime(v: number): number?
|
||||
local time, date = v % 65536, v // 65536
|
||||
if date == 0 then
|
||||
return nil
|
||||
end
|
||||
local y, m, d = 1980 + bit32.rshift(date, 9), bit32.band(bit32.rshift(date, 5), 15), bit32.band(date, 31)
|
||||
if m < 1 or m > 12 or d < 1 then
|
||||
return nil
|
||||
end
|
||||
local yy = if m <= 2 then y - 1 else y
|
||||
local era = yy // 400
|
||||
local yoe = yy - era * 400
|
||||
local doy = (153 * (if m > 2 then m - 3 else m + 9) + 2) // 5 + d - 1
|
||||
local days = era * 146097 + yoe * 365 + yoe // 4 - yoe // 100 + doy - 719468
|
||||
return days * 86400 + bit32.rshift(time, 11) * 3600 + bit32.band(bit32.rshift(time, 5), 63) * 60 + bit32.band(time, 31) * 2
|
||||
end
|
||||
|
||||
type Header = { fields: Bytes.Reader, name: string, comment: string, size: number, at: number, next: number }
|
||||
|
||||
-- the header block at p: 60 EA, size, the basic header, its CRC-32, extended headers; nil at the end marker
|
||||
local function block(r: Bytes.Reader, p: number): (Header?, string?)
|
||||
local h = r:sub(p, 4)
|
||||
local id1, id2, size = h:u8(), h:u8(), h:u16le()
|
||||
if h.err then
|
||||
return nil, "truncated header"
|
||||
end
|
||||
if id1 ~= 0x60 or id2 ~= 0xEA then
|
||||
return nil, "bad header id"
|
||||
end
|
||||
if size == 0 then
|
||||
return nil, nil
|
||||
end
|
||||
if size > 2600 then
|
||||
return nil, "header too large"
|
||||
end
|
||||
if p + 4 + size + 4 > r:size() then
|
||||
return nil, "truncated header"
|
||||
end
|
||||
local crc = r:sub(p + 4 + size, 4):u32le()
|
||||
if Crc32.of(r.buf, r.base + p + 4, size) ~= crc then
|
||||
return nil, "checksum: header CRC-32 mismatch"
|
||||
end
|
||||
local f = r:sub(p + 4, size)
|
||||
local first = f:u8()
|
||||
if first < 30 or first > size then
|
||||
return nil, "bad first header size"
|
||||
end
|
||||
f:seek(first)
|
||||
local name = f:cstring()
|
||||
local comment = f:cstring()
|
||||
-- extended headers
|
||||
local q = p + 4 + size + 4
|
||||
local guard = 0
|
||||
while true do
|
||||
local e = r:sub(q, 2)
|
||||
local n = e:u16le()
|
||||
if e.err then
|
||||
return nil, "truncated extended header"
|
||||
end
|
||||
q += 2
|
||||
if n == 0 then
|
||||
break
|
||||
end
|
||||
q += n + 4
|
||||
guard += 1
|
||||
if guard > 256 or q > r:size() then
|
||||
return nil, "truncated extended header"
|
||||
end
|
||||
end
|
||||
f:seek(0)
|
||||
return { fields = f, name = name, comment = comment, size = size, at = p, next = q }, nil
|
||||
end
|
||||
|
||||
function Arj.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local o: DecodeOptions = opts or {}
|
||||
local maxBytes = o.maxBytes or 2 ^ 30
|
||||
local maxEntries = o.maxEntries or 65536
|
||||
local main, err = block(r, 0)
|
||||
if not main then
|
||||
return nil, Err.from(err or "no main header", r.base)
|
||||
end
|
||||
local a = Archive.new("arj")
|
||||
local mf = main.fields
|
||||
mf:skip(1)
|
||||
local version, minVersion, host = mf:u8(), mf:u8(), mf:u8()
|
||||
local flags = mf:u8()
|
||||
mf:skip(3)
|
||||
local created = mf:u32le()
|
||||
a.meta.archive = main.name
|
||||
a.meta.comment = if main.comment ~= "" then main.comment else nil
|
||||
a.meta.os = OS[host]
|
||||
a.meta.created = dosTime(created)
|
||||
a.meta.source = { format = "arj", version = version, minVersion = minVersion, flags = flags }
|
||||
local p = main.next
|
||||
local count = 0
|
||||
while true do
|
||||
local h, herr = block(r, p)
|
||||
if herr then
|
||||
return nil, Err.from(herr, r.base + p)
|
||||
end
|
||||
if not h then
|
||||
break
|
||||
end
|
||||
count += 1
|
||||
if count > maxEntries then
|
||||
return nil, Err.new(Err.LIMIT, "more than maxEntries entries")
|
||||
end
|
||||
local f = h.fields
|
||||
f:skip(1)
|
||||
f:skip(2)
|
||||
local hostOs = f:u8()
|
||||
local fl = f:u8()
|
||||
local method = f:u8()
|
||||
local ftype = f:u8()
|
||||
f:skip(1)
|
||||
local stamp = f:u32le()
|
||||
local packed, size, crc = f:u32le(), f:u32le(), f:u32le()
|
||||
f:skip(2)
|
||||
local mode = f:u16le()
|
||||
local dataAt = h.next
|
||||
if dataAt + packed > r:size() then
|
||||
return nil, Err.new(Err.TRUNCATED, "entry " .. h.name .. " data past the end", r.base + dataAt)
|
||||
end
|
||||
local name = string.gsub(h.name, "\\", "/")
|
||||
local isDir = ftype == 3
|
||||
if isDir and string.sub(name, -1) ~= "/" then
|
||||
name ..= "/"
|
||||
end
|
||||
local e: any = {
|
||||
name = name,
|
||||
size = if isDir then 0 else size,
|
||||
compressedSize = packed,
|
||||
method = if method == 0 then "store" else "arj" .. method,
|
||||
mtime = dosTime(stamp),
|
||||
dir = isDir,
|
||||
mode = mode,
|
||||
os = OS[hostOs],
|
||||
comment = if h.comment ~= "" then h.comment else nil,
|
||||
fileType = ftype,
|
||||
}
|
||||
if ftype ~= 4 then -- 4: a volume label, not a file
|
||||
if not isDir and size > 0 then
|
||||
if size > maxBytes then
|
||||
return nil, Err.new(Err.LIMIT, name .. ": larger than maxBytes")
|
||||
end
|
||||
local buf, at = r.buf, r.base + dataAt
|
||||
e.load = function(): (buffer?, Err.Err?)
|
||||
if bit32.band(fl, 0x01) ~= 0 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "garbled (encrypted) entries")
|
||||
end
|
||||
if bit32.band(fl, 0x0C) ~= 0 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "an entry split across volumes")
|
||||
end
|
||||
local out: buffer?, derr: string?
|
||||
if method == 0 then
|
||||
if packed < size then
|
||||
return nil, Err.new(Err.CORRUPT, "stored entry shorter than its size")
|
||||
end
|
||||
out = buffer.create(size)
|
||||
buffer.copy(out :: buffer, 0, buf, at, size)
|
||||
elseif method >= 1 and method <= 3 then
|
||||
out, derr = Lzh.decode(buf, at, packed, size, "lh6")
|
||||
elseif method == 4 then
|
||||
out, derr = Lzh.decode(buf, at, packed, size, "arj4")
|
||||
else
|
||||
return nil, Err.new(Err.UNSUPPORTED, "method " .. method)
|
||||
end
|
||||
if not out then
|
||||
return nil, Err.from(derr)
|
||||
end
|
||||
if Crc32.of(out) ~= crc then
|
||||
return nil, Err.new(Err.CHECKSUM, "CRC-32 mismatch")
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
end
|
||||
Archive.put(a, e)
|
||||
end
|
||||
p = dataAt + packed
|
||||
end
|
||||
return a, nil
|
||||
end
|
||||
|
||||
return Arj
|
||||
Reference in new issue
Block a user