ws-arch2: codecs/compress/Lzh (lh1 / lh4-lh7 / lhx / lzs / lz5 / ARJ 4; ported from lhasa, ISC) + archive/lha (levels 0-3, extended headers, symlinks, DOS-name case, read; level-2 lh0 write); bench lines

This commit is contained in:
4DBug committed 2026-10-31 14:40:25 -05:00
1 parent 94d79313d4
commit 572cd42f8a
10 files changed
+1303

No files matched your search

+22
View File
@@ -1078,3 +1078,25 @@ do
jitTune(false)
end
end
-- ws-arch2 archives: the LHA family (codecs/compress/Lzh) over tools/fixtures/lha.py archives (MB/s of output)
do
local Lha = require("@formats/archive/lha")
local LF = require("@tests/gen/lha")
for _, want in { "lha114i -h2 -5", "lha114i -h2 -7", "lengths/lh1-1m.lzh", "lengths/lz5-1m.lzs", "larc333/long.lzs" } do
for _, c in LF.cases do
if c.name == want then
local a = Lha.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
local total = 0
for _, e in a:list() do
total += e.size
end
bench("lha " .. want, 3, total, function()
for _, e in a:list() do
a:read(e.name)
end
end)
end
end
end
end
+10
View File
@@ -61,3 +61,13 @@ def read_tree(root):
def tmpdir():
return tempfile.mkdtemp(prefix="arch2-")
def rmtree(root):
"""shutil.rmtree that first makes extracted directories writable (archives store read-only modes)"""
for d, ds, _ in os.walk(root):
for x in ds:
p = os.path.join(d, x)
if not os.path.islink(p):
os.chmod(p, 0o755)
shutil.rmtree(root, ignore_errors=True)
+75
View File
@@ -0,0 +1,75 @@
# LHA / LZH fixtures (tools/fixtures.py): the test archives of lhasa 0.6.0's source (nixpkgs#lhasa.src, ISC; read
# from the nix store at fixture time, not committed: archives by LHarc 1.13 (lh1), LArc 3.33 (lzs / lz5 / lz4), LHA
# 2.13 / 2.55e / Amiga / OS-9 / OSK / Unix 1.14i / ExpLZH (lh0 / lh5 / lh6 / lh7, header levels 0 / 1 / 2, subdirectories,
# symlinks) and lhasa's lh1 / lz5 length tests) and archives made here by LHa for UNIX 1.14i (unfree, run as a tool:
# -o5 / -o6 / -o7 / -z, header levels 0 / 1 / 2); the reference data is what lhasa extracts (`lha -xw=`). Our writer's
# output (tools/fixtures/_arch2_ours.luau) is extracted by lhasa.
# tests/gen/lha.luau { cases = { { name, data, entries = { { name, size, crc } }, links = { { name, link } } } },
# ours = { ok, listing } }
import glob, importlib.util, os, shutil, subprocess, tarfile, 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", "tools/fixtures/_arch2_ours.luau", "src/Formats/archive/lha"]
DIRS = ["lharc113", "larc333", "lha213", "lha255e", "lha_amiga_122", "lha_amiga_212", "lha_os9_211c", "lha_osk_201",
"lha_unix114i", "explzh_723", "lengths", "generated/lzs", "lha_x68k_213", "lh2_222", "lhmelt_16536"]
def lhasa(args, cwd):
return A.tool(["lhasa"], ["lha"] + args, cwd=cwd).decode(errors="replace")
def expected(path, tmp):
out = os.path.join(tmp, "x")
A.rmtree(out)
os.makedirs(out)
lhasa(["-xqfw=" + out, path], tmp)
ents, links = [], []
for d, _, fs in os.walk(out):
for f in fs:
p = os.path.join(d, f)
rel = os.path.relpath(p, out)
if os.path.islink(p):
links.append({"name": rel, "link": os.readlink(p)})
else:
data = open(p, "rb").read()
ents.append({"name": rel, "size": len(data), "crc": zlib.crc32(data)})
return ents, links
def generate(ctx):
src = subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "nixpkgs#lhasa.src"], capture_output=True, check=True, text=True).stdout.strip()
tmp = A.tmpdir()
try:
with tarfile.open(src) as t:
t.extractall(tmp, filter="data")
root = glob.glob(os.path.join(tmp, "lhasa-*", "test", "archives"))[0]
cases = []
for d in DIRS:
for p in sorted(glob.glob(os.path.join(root, d, "*.lz[hs]"))):
try:
ents, links = expected(p, tmp)
except RuntimeError:
continue # lhasa cannot extract it here (read-only directory attributes from OS/2)
cases.append({"name": os.path.relpath(p, root), "data": open(p, "rb").read(), "entries": ents, "links": links})
# LHa for UNIX
work = os.path.join(tmp, "w")
rng = ctx.rng(5)
A.tree(A.FILES + [("dir/sub/text.txt", b"the quick brown fox " * 300), ("dir/rand.bin", rng.randbytes(70000))], work)
names = [n for n, _ in A.FILES] + ["dir"]
for level in (0, 1, 2):
for m in ("5", "6", "7", "z"):
arc = os.path.join(tmp, "u%d%s.lzh" % (level, m))
opt = ("-a%dz" % level) if m == "z" else ("-a%do%s" % (level, m))
A.tool([], ["env", "NIXPKGS_ALLOW_UNFREE=1", "nix", "shell", "--impure", "nixpkgs#lha", "-c", "lha", opt + "q", arc] + names, cwd=work)
ents, links = expected(arc, tmp)
cases.append({"name": "lha114i -h%d -%s" % (level, m), "data": open(arc, "rb").read(), "entries": ents, "links": links})
arc = os.path.join(tmp, "ours.lzh")
open(arc, "wb").write(A.ours(ctx, "lha"))
got = {e["name"]: e["crc"] for e in expected(arc, tmp)[0]}
ok = all(got.get(n) == zlib.crc32(d) for n, d in A.FILES)
ctx.emit("lha", {"cases": cases, "ours": {"ok": ok, "listing": lhasa(["-v", arc], tmp)}})
finally:
A.rmtree(tmp)