198 lines
6.7 KiB
Luau
198 lines
6.7 KiB
Luau
--!strict
|
|
-- archive/zoo: Zoo archives (Rahul Dhesi's zoo 1.x / 2.x) read into an Archive: the archive header, the chain of
|
|
-- directory entries (type 1, and type 2 with long names, directory names, Unix-style attributes and version numbers),
|
|
-- deleted entries skipped, entries stored (method 0), LZW-compressed (method 1: codecs/compress/Lzw "zoo", 13-bit
|
|
-- codes) or lzh-compressed (method 2: codecs/compress/Lzh "lh5"); every file's CRC-16 (ARC) is verified when read
|
|
-- (Err.CHECKSUM). MS-DOS dates read as UTC. Written from zoo 2.10's file format description (zoo.h / ar.h). Not
|
|
-- supported: writing, file generations beyond keeping the newest (older versions of a name get ";<version>").
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc16 = require("@rosetta/core/Crc16")
|
|
local Lzw = require("@rosetta/codecs/compress/Lzw")
|
|
local Lzh = require("@rosetta/codecs/compress/Lzh")
|
|
local Archive = require("@rosetta/containers/Archive")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Zoo = {
|
|
name = "zoo",
|
|
category = "archive",
|
|
extensions = { "zoo" },
|
|
mime = { "application/x-zoo" },
|
|
deps = { "core/Err", "core/Bytes", "core/Crc16", "codecs/compress/Lzw", "codecs/compress/Lzh", "containers/Archive" },
|
|
caps = { decode = true, encode = false, methods = { "store", "lzw", "lzh" } },
|
|
}
|
|
|
|
export type DecodeOptions = {
|
|
maxBytes: number?, -- refuse an entry larger than this (default 2^30)
|
|
maxEntries: number?, -- default 65536
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constants
|
|
local TAG = 0xFDC4A7DC
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Zoo.sniff(r: Bytes.Reader): number
|
|
local h = r:peek(24)
|
|
if #h < 24 then
|
|
return 0
|
|
end
|
|
local tag = string.byte(h, 21) + string.byte(h, 22) * 256 + string.byte(h, 23) * 65536 + string.byte(h, 24) * 16777216
|
|
if tag ~= TAG then
|
|
return 0
|
|
end
|
|
return if string.sub(h, 1, 4) == "ZOO " then 1 else 0.8
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function Zoo.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 h = r:sub(0, 34)
|
|
local text = h:string(20)
|
|
local tag, start = h:u32le(), h:u32le()
|
|
h:skip(4)
|
|
local major, minor = h:u8(), h:u8()
|
|
if h.err then
|
|
return nil, h.err
|
|
end
|
|
if tag ~= TAG then
|
|
return nil, Err.new(Err.CORRUPT, "no zoo tag")
|
|
end
|
|
local a = Archive.new("zoo")
|
|
a.meta.text = string.match(text, "^[^%z\26]*")
|
|
a.meta.source = { format = "zoo", version = major .. "." .. minor }
|
|
local p = start
|
|
local count = 0
|
|
local seen: { [number]: boolean } = {}
|
|
local size = r:size()
|
|
while true do
|
|
if seen[p] or p < 34 or p + 51 > size then
|
|
return nil, Err.new(if p + 51 > size then Err.TRUNCATED else Err.CORRUPT, "bad directory entry offset " .. p)
|
|
end
|
|
seen[p] = true
|
|
local e = r:sub(p, size - p)
|
|
local etag = e:u32le()
|
|
local etype, method = e:u8(), e:u8()
|
|
local nextP, dataAt = e:u32le(), e:u32le()
|
|
local date, time = e:u16le(), e:u16le()
|
|
local crc = e:u16le()
|
|
local orig, packed = e:u32le(), e:u32le()
|
|
e:skip(2)
|
|
local deleted = e:u8()
|
|
e:skip(1)
|
|
local commentAt, commentLen = e:u32le(), e:u16le()
|
|
local fname = e:fixed(13)
|
|
if e.err then
|
|
return nil, e.err
|
|
end
|
|
if etag ~= TAG then
|
|
return nil, Err.new(Err.CORRUPT, "bad directory entry tag", r.base + p)
|
|
end
|
|
if nextP == 0 then
|
|
break
|
|
end
|
|
count += 1
|
|
if count > maxEntries then
|
|
return nil, Err.new(Err.LIMIT, "more than maxEntries entries")
|
|
end
|
|
local name, dir = fname, ""
|
|
local entry: any = {}
|
|
if etype == 2 then
|
|
local varLen = e:u16le()
|
|
e:skip(3) -- tz, dir_crc
|
|
if varLen > 0 then
|
|
local v = e:slice(varLen)
|
|
local nlen, dlen = v:u8(), v:u8()
|
|
local lname = v:string(nlen)
|
|
dir = v:string(dlen)
|
|
if nlen > 0 then
|
|
name = string.match(lname, "^[^%z]*") or lname
|
|
end
|
|
if v:remaining() >= 2 then
|
|
entry.system = v:u16le()
|
|
end
|
|
if v:remaining() >= 3 then
|
|
local f0, f1, f2 = v:u8(), v:u8(), v:u8()
|
|
local attr = f0 + f1 * 256 + f2 * 65536
|
|
if bit32.band(attr, 0x400000) ~= 0 then
|
|
entry.mode = bit32.band(attr, 0xFFFF)
|
|
end
|
|
end
|
|
if v:remaining() >= 4 then
|
|
local vflag, ver = v:u16le(), v:u16le()
|
|
if bit32.band(vflag, 0x40) ~= 0 then
|
|
entry.version = ver
|
|
end
|
|
end
|
|
end
|
|
if e.err then
|
|
return nil, e.err
|
|
end
|
|
end
|
|
dir = string.match(dir, "^[^%z]*") or dir
|
|
dir = string.gsub(dir, "\\", "/")
|
|
local full = if dir ~= "" then (string.gsub(dir, "/+$", "")) .. "/" .. name else name
|
|
full = string.gsub(full, "^/+", "")
|
|
if deleted == 0 then
|
|
local key = full
|
|
if a._byName[key] then
|
|
key = full .. ";" .. tostring(entry.version or count)
|
|
end
|
|
entry.name = key
|
|
entry.size = orig
|
|
entry.compressedSize = packed
|
|
entry.method = if method == 0 then "store" elseif method == 1 then "lzw" elseif method == 2 then "lzh" else "method" .. method
|
|
entry.mtime = Archive.fromDos(date * 65536 + time)
|
|
entry.dir = false
|
|
if commentLen > 0 and commentAt + commentLen <= size then
|
|
entry.comment = r:sub(commentAt, commentLen):string(commentLen)
|
|
end
|
|
if dataAt + packed > size then
|
|
return nil, Err.new(Err.TRUNCATED, full .. ": data past the end")
|
|
end
|
|
if orig > maxBytes then
|
|
return nil, Err.new(Err.LIMIT, full .. ": larger than maxBytes")
|
|
end
|
|
if orig > 0 then
|
|
local buf, at = r.buf, r.base + dataAt
|
|
entry.load = function(): (buffer?, Err.Err?)
|
|
local out: buffer?, derr: string?
|
|
if method == 0 then
|
|
if packed < orig then
|
|
return nil, Err.new(Err.CORRUPT, "stored entry shorter than its size")
|
|
end
|
|
out = buffer.create(orig)
|
|
buffer.copy(out :: buffer, 0, buf, at, orig)
|
|
elseif method == 1 then
|
|
local ob = buffer.create(orig)
|
|
local n, lerr = Lzw.decode(buf, at, packed, ob, 0, orig, "zoo")
|
|
if n ~= orig then
|
|
return nil, Err.from(lerr or "corrupt: LZW data ends early")
|
|
end
|
|
out = ob
|
|
elseif method == 2 then
|
|
out, derr = Lzh.decode(buf, at, packed, orig, "lh5")
|
|
else
|
|
return nil, Err.new(Err.UNSUPPORTED, "packing method " .. method)
|
|
end
|
|
if not out then
|
|
return nil, Err.from(derr)
|
|
end
|
|
if Crc16.arc(out, 0, orig) ~= crc then
|
|
return nil, Err.new(Err.CHECKSUM, "CRC-16 mismatch")
|
|
end
|
|
return out, nil
|
|
end
|
|
end
|
|
Archive.put(a, entry)
|
|
end
|
|
p = nextP
|
|
end
|
|
return a, nil
|
|
end
|
|
|
|
return Zoo
|