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.
183 lines
7.2 KiB
Python
183 lines
7.2 KiB
Python
# 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)
|