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:
1 parent
d0b81f02c7
commit
f1c2c6ed06
6 files changed
+466
-9
No files matched your search
@@ -66,6 +66,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| archive | xz | .xz .txz .lzma | yes | - | stream: true, filters: "lzma2", "lzma", "delta", "x86", "powerpc", "arm", "armthumb", "sparc", "arm64" | `tests/archive/xz.luau` |
|
||||
| archive | yenc | .yenc .ync | yes | yes | multipart: true | `tests/archive/yenc.luau` |
|
||||
| archive | zip | .zip .wsz | yes | yes | methods: "store", "deflate", zip64: true | `tests/archive/zip.luau` |
|
||||
| archive | zoo | .zoo | yes | - | methods: "store", "lzw", "lzh" | `tests/archive/zoo.luau` |
|
||||
| archive | zst | .zst .tzst | yes | - | stream: true | `tests/archive/zst.luau` |
|
||||
| data | cbor | .cbor | yes | yes | binary: true, ordered: true, tags: true | `tests/data/cbor.luau` |
|
||||
| data | csv | .csv .tsv | yes | yes | header: true | `tests/data/csv.luau` |
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
-- archive/zoo tests: zoo archives assembled by tools/fixtures/zoo.py (type 1 / type 2 entries, long and directory
|
||||
-- names, a deleted entry, methods stored / LZW / lzh, an LZW stream with table resets) give the files The Unarchiver
|
||||
-- extracts (names, sizes, data CRCs); CRC errors; the fuzz pass. Fixtures: tools/fixtures/zoo.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Zoo = require("@formats/archive/zoo")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local E = require("@tests/archive/_entries")
|
||||
local Fx = require("@tests/gen/zoo")
|
||||
|
||||
local t = T.suite("zoo")
|
||||
t:eq(Zoo.name, "zoo", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
local a, err = Zoo.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
if a then
|
||||
E.check(t, a, c.entries, c.name)
|
||||
t:eq(a._byName["gone.txt"], nil, c.name .. ": deleted entry skipped")
|
||||
end
|
||||
t:eq(F.detect(c.data), "zoo", c.name .. ": detect")
|
||||
if #c.data < 60000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
|
||||
local data = Fx.cases[2].data -- lzw
|
||||
local function code(s: string): string?
|
||||
local _, e = Zoo.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code(string.sub(data, 1, 20)), Err.TRUNCATED, "short header")
|
||||
t:eq(code(string.rep("\0", 100)), Err.CORRUPT, "no tag")
|
||||
t:eq(code(string.sub(data, 1, 200)), Err.TRUNCATED, "cut")
|
||||
local mid = #data // 2
|
||||
local bad = Zoo.decode(Bytes.reader(buffer.fromstring(string.sub(data, 1, mid - 1) .. string.char(bit32.bxor(string.byte(data, mid), 4)) .. string.sub(data, mid + 1)))) :: any
|
||||
local failed = 0
|
||||
for _, e in bad:list() do
|
||||
local b, err = bad:read(e.name)
|
||||
if not b then
|
||||
failed += 1
|
||||
t:check(err.code == Err.CHECKSUM or err.code == Err.CORRUPT or err.code == Err.TRUNCATED, "flipped data: " .. tostring(err))
|
||||
end
|
||||
end
|
||||
t:eq(failed, 1, "one entry fails after a flipped data byte")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local r = Zoo.decode(Bytes.reader(b))
|
||||
if r then
|
||||
for _, e in r:list() do
|
||||
r:read(e.name)
|
||||
end
|
||||
end
|
||||
return r
|
||||
end, samples, { mutations = 6 })
|
||||
t:done()
|
||||
@@ -0,0 +1,182 @@
|
||||
# Zoo fixtures (tools/fixtures.py): no zoo archiver is packaged in nixpkgs, so the archives are assembled here per
|
||||
# zoo 2.10's zoo.h layout (type 1 and type 2 directory entries, long names, directory names, a deleted entry), with
|
||||
# data stored (method 0), LZW-compressed by the encoder below (method 1: 9..13-bit LSB-first codes, CLEAR 256, EOF 257)
|
||||
# and lzh-compressed (method 2: the -lh5- streams of LHa for UNIX archives); The Unarchiver (`lsar -j`, `unar`)
|
||||
# is the reference reader: its listing and extracted data are the expected entries.
|
||||
# tests/gen/zoo.luau { cases = { { name, data, entries = { { name, size, crc } } } } }
|
||||
import importlib.util, json, os, struct, zlib
|
||||
|
||||
_s = importlib.util.spec_from_file_location("_arch2", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_arch2.py"))
|
||||
A = importlib.util.module_from_spec(_s)
|
||||
_s.loader.exec_module(A)
|
||||
|
||||
INPUTS = ["tools/fixtures/_arch2.py"]
|
||||
TAG = 0xFDC4A7DC
|
||||
|
||||
|
||||
def crc16(data):
|
||||
c = 0
|
||||
for b in data:
|
||||
c ^= b
|
||||
for _ in range(8):
|
||||
c = (c >> 1) ^ 0xA001 if c & 1 else c >> 1
|
||||
return c
|
||||
|
||||
|
||||
def lzw(data):
|
||||
"""zoo method 1: codes sized as the decoder (lzd.c) reads them"""
|
||||
out, acc, nbits = bytearray(), 0, 0
|
||||
|
||||
def put(code, size):
|
||||
nonlocal acc, nbits
|
||||
acc |= code << nbits
|
||||
nbits += size
|
||||
while nbits >= 8:
|
||||
out.append(acc & 255)
|
||||
acc >>= 8
|
||||
nbits -= 8
|
||||
|
||||
def reset():
|
||||
return {bytes([i]): i for i in range(256)}, 258, 258, 9, True
|
||||
|
||||
table, enc_next, dec_next, size, first = reset()
|
||||
put(256, size)
|
||||
w = b""
|
||||
for b in data:
|
||||
wc = w + bytes([b])
|
||||
if wc in table:
|
||||
w = wc
|
||||
continue
|
||||
put(table[w], size)
|
||||
if not first:
|
||||
dec_next += 1
|
||||
if dec_next >= (1 << size) and size < 13:
|
||||
size += 1
|
||||
first = False
|
||||
if enc_next < 8192:
|
||||
table[wc] = enc_next
|
||||
enc_next += 1
|
||||
w = bytes([b])
|
||||
else:
|
||||
put(table[bytes([b])], size) # the last code before the clear
|
||||
put(256, size)
|
||||
table, enc_next, dec_next, size, first = reset()
|
||||
w = b""
|
||||
continue
|
||||
if w:
|
||||
put(table[w], size)
|
||||
if not first:
|
||||
dec_next += 1
|
||||
if dec_next >= (1 << size) and size < 13:
|
||||
size += 1
|
||||
put(257, size)
|
||||
if nbits:
|
||||
out.append(acc & 255)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def lh5_streams(ctx, files, tmp):
|
||||
"""{name: (packed bytes, crc)} from LHa for UNIX -o5 level 2 archives"""
|
||||
out = {}
|
||||
work = os.path.join(tmp, "lh5")
|
||||
A.tree(files, work)
|
||||
for name, data in files:
|
||||
if not data:
|
||||
continue
|
||||
arc = os.path.join(tmp, "one.lzh")
|
||||
if os.path.exists(arc):
|
||||
os.remove(arc)
|
||||
A.tool([], ["env", "NIXPKGS_ALLOW_UNFREE=1", "nix", "shell", "--impure", "nixpkgs#lha", "-c", "lha", "-a2o5q", arc, name], cwd=work)
|
||||
b = open(arc, "rb").read()
|
||||
total = struct.unpack_from("<H", b, 0)[0]
|
||||
method = b[2:7]
|
||||
packed = struct.unpack_from("<I", b, 7)[0]
|
||||
out[name] = (method, b[total:total + packed])
|
||||
return out
|
||||
|
||||
|
||||
def entry(name, dirname, method, packed_data, orig, crc, offset, nxt, typ=2, deleted=0, version=None):
|
||||
short = (os.path.basename(name)[:12]).encode()
|
||||
fixed = struct.pack("<IBBIIHHHIIBBBBIH13s", TAG, typ, method, nxt, offset, 0x5765, 0x6000, crc, orig, len(packed_data),
|
||||
2, 1, deleted, 0, 0, 0, short)
|
||||
if typ == 1:
|
||||
return fixed
|
||||
ln, dn = name.encode(), dirname.encode()
|
||||
var = struct.pack("<BB", len(ln), len(dn)) + ln + dn + struct.pack("<H", 0) + bytes([0o644 & 255, 0o644 >> 8, 0x40])
|
||||
var += struct.pack("<HH", 0x40 if version else 0, version or 0)
|
||||
head = fixed + struct.pack("<HBH", len(var), 127, 0) + var
|
||||
dcrc = crc16(head)
|
||||
return fixed + struct.pack("<HBH", len(var), 127, dcrc) + var
|
||||
|
||||
|
||||
def archive(items):
|
||||
"""items: [(name, dir, method, packed, orig, crc, type, deleted, version)] -> zoo bytes"""
|
||||
hdr = b"ZOO 2.10 Archive.\x1a".ljust(20, b"\0") + struct.pack("<IIiBB", TAG, 34, -34, 2, 0)
|
||||
out = bytearray(hdr)
|
||||
p = 34
|
||||
for (name, d, method, packed, orig, crc, typ, deleted, version) in items:
|
||||
size = len(entry(name, d, method, packed, orig, crc, 0, 0, typ, deleted, version))
|
||||
data_at = p + size
|
||||
nxt = data_at + len(packed)
|
||||
out += entry(name, d, method, packed, orig, crc, data_at, nxt, typ, deleted, version) + packed
|
||||
p = nxt
|
||||
out += entry("", "", 0, b"", 0, 0, 0, 0, 2) # the end marker: next = 0
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def unar_listing(path, tmp):
|
||||
j = json.loads(A.tool(["unar"], ["lsar", "-j", path]))
|
||||
out = os.path.join(tmp, "x")
|
||||
A.rmtree(out)
|
||||
A.tool(["unar"], ["unar", "-q", "-f", "-D", "-o", out, path])
|
||||
got = A.read_tree(out)
|
||||
ents = []
|
||||
for e in j["lsarContents"]:
|
||||
n = e["XADFileName"]
|
||||
if n in got:
|
||||
ents.append({"name": n, "size": e.get("XADFileSize", 0), "crc": got[n]})
|
||||
return ents
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(77)
|
||||
tmp = A.tmpdir()
|
||||
try:
|
||||
files = A.FILES + [("text.txt", b"It was the best of times, it was the worst of times, " * 300), ("rand.bin", rng.randbytes(30000)),
|
||||
("runs.bin", b"\0" * 20000 + bytes(range(256)) * 40)]
|
||||
lh5 = lh5_streams(ctx, files, tmp)
|
||||
cases = []
|
||||
for label, method in (("stored", 0), ("lzw", 1), ("lzh", 2)):
|
||||
items = []
|
||||
for name, data in files:
|
||||
if method == 2 and name in lh5 and lh5[name][0] == b"-lh5-":
|
||||
packed, m = lh5[name][1], 2
|
||||
elif method == 1:
|
||||
packed, m = lzw(data), 1
|
||||
else:
|
||||
packed, m = data, 0
|
||||
d = "dir/sub" if name == "text.txt" else ""
|
||||
items.append((name, d, m, packed, len(data), crc16(data), 2, 0, None))
|
||||
items.insert(1, ("gone.txt", "", 0, b"deleted", 7, crc16(b"deleted"), 2, 1, None))
|
||||
p = os.path.join(tmp, label + ".zoo")
|
||||
blob = archive(items)
|
||||
open(p, "wb").write(blob)
|
||||
ents = [e for e in unar_listing(p, tmp) if e["name"] != "gone.txt"] # unar lists deleted entries; zoo does not
|
||||
src = {("dir/sub/" if n == "text.txt" else "") + n: zlib.crc32(d) for n, d in files}
|
||||
assert len(ents) == len(files) and all(src[e["name"]] == e["crc"] for e in ents), (label, ents)
|
||||
cases.append({"name": label, "data": blob, "entries": ents})
|
||||
# type 1 entries (zoo 1.x: 8.3 names only)
|
||||
items = [(n.upper()[:12], "", 0, d, len(d), crc16(d), 1, 0, None) for n, d in A.FILES[:2]]
|
||||
blob = archive(items)
|
||||
p = os.path.join(tmp, "type1.zoo")
|
||||
open(p, "wb").write(blob)
|
||||
cases.append({"name": "type 1 entries", "data": blob, "entries": unar_listing(p, tmp)})
|
||||
# a big LZW file: several table resets
|
||||
big = rng.randbytes(20000) + b"abcabcabd" * 20000
|
||||
blob = archive([("big.bin", "", 1, lzw(big), len(big), crc16(big), 2, 0, None)])
|
||||
p = os.path.join(tmp, "big.zoo")
|
||||
open(p, "wb").write(blob)
|
||||
cases.append({"name": "lzw table resets", "data": blob, "entries": unar_listing(p, tmp)})
|
||||
ctx.emit("zoo", {"cases": cases})
|
||||
finally:
|
||||
A.rmtree(tmp)
|
||||
Reference in new issue
Block a user