ws-arch2: archive/zoo (types 1 / 2, stored / LZW / lzh, CRC-16; read) and codecs/compress/Lzw 'zoo' variant (13-bit codes)

codecs/compress/Lzw: decode accepts variant "zoo" (gif layout, 8-bit symbols, up to 13-bit codes; its tables grow
to 8192 entries); gif / tiff behaviour unchanged.
This commit is contained in:
4DBug committed 2026-10-31 14:49:54 -05:00
1 parent d0b81f02c7
commit f1c2c6ed06
6 files changed
+466 -9

No files matched your search

+12
View File
@@ -746,6 +746,18 @@ local list: { Entry } = {
return require("@formats/archive/zip")
end,
},
{
name = "zoo",
category = "archive",
path = "archive/zoo",
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" } },
load = function()
return require("@formats/archive/zoo")
end,
},
{
name = "zst",
category = "archive",
+197
View File
@@ -0,0 +1,197 @@
--!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("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Crc16 = require("@formats/core/Crc16")
local Lzw = require("@formats/codecs/compress/Lzw")
local Lzh = require("@formats/codecs/compress/Lzh")
local Archive = require("@formats/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
+16 -9
View File
@@ -8,9 +8,11 @@
-- tiff MSB-first codes, 8-bit symbols, clear = 256, EOI = 257, "early change": the width grows one code
-- earlier (next free code = 2^width - 1). Old-style (pre-6.0, LSB-first, no early change) TIFF LZW is
-- detected by its first bytes (0x00 then an odd byte) and decoded too.
-- zoo the gif layout with 8-bit symbols and codes up to 13 bits (Zoo archives' method 1; decode only).
--
-- local n, err = Lzw.decode(src, off, len, out, outOff, maxOut, "gif", minCodeSize) -- bytes written
-- local n, err = Lzw.decode(src, off, len, out, outOff, maxOut, "tiff")
-- local n, err = Lzw.decode(src, off, len, out, outOff, maxOut, "zoo")
-- local codes = Lzw.encode(data, off, len, "gif", minCodeSize) -- buffer
-- local codes = Lzw.encode(data, off, len, "tiff")
--
@@ -24,13 +26,14 @@ local blen = buffer.len
local Lzw = {}
local MAX_CODES = 4096
local MAX_CODES = 4096 -- 12-bit codes (GIF, TIFF)
local TABLE = 8192 -- the tables hold 13-bit codes (Zoo)
-- per-call tables (allocated once: decode / encode are not re-entrant across coroutines, but never yield)
local prefix = buffer.create(MAX_CODES * 2) -- u16: the code this entry extends
local suffix = buffer.create(MAX_CODES) -- u8: its last byte
local first = buffer.create(MAX_CODES) -- u8: its first byte
local length = buffer.create(MAX_CODES * 2) -- u16: its length
local prefix = buffer.create(TABLE * 2) -- u16: the code this entry extends
local suffix = buffer.create(TABLE) -- u8: its last byte
local first = buffer.create(TABLE) -- u8: its first byte
local length = buffer.create(TABLE * 2) -- u16: its length
-- ------------------------------------------------------------------------------------------------ decode
-- writes the string of code c (length l) ending at out[o + l - 1]; clipped to [o, lim)
@@ -66,6 +69,10 @@ function Lzw.decode(src: buffer, off: number, len: number, out: buffer, outOff:
local msb = variant == "tiff"
local early = 0 -- 1 for TIFF's early change
local min = minCodeSize or 8
local maxCodes, maxBits = MAX_CODES, 12
if variant == "zoo" then
min, maxCodes, maxBits = 8, TABLE, 13
end
if msb then
min = 8
early = 1
@@ -94,7 +101,7 @@ function Lzw.decode(src: buffer, off: number, len: number, out: buffer, outOff:
local p = off
local acc, nb = 0, 0
while true do
-- refill (at most 12 + 7 bits held: below 2^20)
-- refill (at most 13 + 7 bits held: below 2^21)
while nb < size do
if p >= e then
return o - outOff, "truncated"
@@ -140,14 +147,14 @@ function Lzw.decode(src: buffer, off: number, len: number, out: buffer, outOff:
if c < nextc then
l = readu16(length, c * 2)
emit(out, o, c, l, lim)
if nextc < MAX_CODES then
if nextc < maxCodes then
writeu16(prefix, nextc * 2, prev)
writeu8(suffix, nextc, readu8(first, c))
writeu8(first, nextc, readu8(first, prev))
writeu16(length, nextc * 2, readu16(length, prev * 2) + 1)
nextc += 1
end
elseif c == nextc and nextc < MAX_CODES then
elseif c == nextc and nextc < maxCodes then
-- KwKwK: the new entry is prev + first(prev)
local f = readu8(first, prev)
writeu16(prefix, nextc * 2, prev)
@@ -163,7 +170,7 @@ function Lzw.decode(src: buffer, off: number, len: number, out: buffer, outOff:
o = math.min(o + l, lim)
prev = c
end
if nextc + early >= lshift(1, size) and size < 12 then
if nextc + early >= lshift(1, size) and size < maxBits then
size += 1
mask = lshift(1, size) - 1
end