From 6226ec41e4ec3fbcfa14c6bd9a531a19f8402f5d Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 08:55:24 -0500 Subject: [PATCH] ws-arch: archive/7z reads BZip2 (codecs/Bzip2) and Zstd (codecs/Zstd) coders; fixtures test BZip2 solid + BCJ --- src/Formats/Registry.luau | 2 +- src/Formats/archive/7z/init.luau | 25 +++++++++++++++++++++---- tests/archive/7z.luau | 2 +- tools/fixtures/7z.py | 5 +++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index 068e540..c0070ac 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -80,7 +80,7 @@ local list: { Entry } = { path = "archive/7z", extensions = { "7z" }, mime = { "application/x-7z-compressed" }, - deps = { "core/Err", "core/Bytes", "core/Crc32", "Types", "codecs/Lzma", "codecs/Deflate", "containers/Archive" }, + deps = { "core/Err", "core/Bytes", "core/Crc32", "Types", "codecs/Lzma", "codecs/Deflate", "codecs/Bzip2", "codecs/Zstd", "containers/Archive" }, caps = { decode = true, encode = false, methods = { "copy", "lzma", "lzma2", "deflate", "bcj", "delta" }, encrypted = false }, load = function() return require("@formats/archive/7z") diff --git a/src/Formats/archive/7z/init.luau b/src/Formats/archive/7z/init.luau index 6d56872..39097fd 100644 --- a/src/Formats/archive/7z/init.luau +++ b/src/Formats/archive/7z/init.luau @@ -1,11 +1,12 @@ --!strict -- archive/7z: 7z archives (7zFormat.txt from the LZMA SDK, Igor Pavlov, public domain), read as an Archive whose -- entries decode lazily (a folder is decoded once on first read, then sliced). Supports: plain and encoded (compressed) --- headers, solid and non-solid folders, coder chains of copy / LZMA / LZMA2 / Deflate with BCJ (x86, PowerPC, ARM, +-- headers, solid and non-solid folders, coder chains of copy / LZMA / LZMA2 / Deflate / BZip2 / +-- Zstd (7-Zip-zstd's plain frames; untested: stock 7-Zip cannot write it) with BCJ (x86, PowerPC, ARM, -- ARM-Thumb, SPARC, ARM64) and delta filters, empty files and directories, anti items (skipped), UTF-16 names, mtimes, -- Windows / Unix attributes, folder and file CRC-32s (verified on read). --- Not supported (Err.UNSUPPORTED naming the method, on read of the affected entries): BZip2, PPMd, BCJ2, Deflate64, --- Zstd and other plug-in codecs, AES encryption, multi-volume archives, external header references; writing. +-- Not supported (Err.UNSUPPORTED naming the method, on read of the affected entries): PPMd, BCJ2, Deflate64, other +-- plug-in codecs (Brotli, LZ4, ...), AES encryption, multi-volume archives, external header references; writing. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@formats/core/Err") @@ -14,6 +15,8 @@ local Crc32 = require("@formats/core/Crc32") local Types = require("@formats/Types") local Lzma = require("@formats/codecs/Lzma") local Deflate = require("@formats/codecs/Deflate") +local Bzip2 = require("@formats/codecs/Bzip2") +local Zstd = require("@formats/codecs/Zstd") local Archive = require("@formats/containers/Archive") local Header = require("@formats/archive/7z/Header") @@ -23,7 +26,7 @@ local SevenZip = { category = "archive", extensions = { "7z" }, mime = { "application/x-7z-compressed" }, - deps = { "core/Err", "core/Bytes", "core/Crc32", "Types", "codecs/Lzma", "codecs/Deflate", "containers/Archive" }, + deps = { "core/Err", "core/Bytes", "core/Crc32", "Types", "codecs/Lzma", "codecs/Deflate", "codecs/Bzip2", "codecs/Zstd", "containers/Archive" }, caps = { decode = true, encode = false, methods = { "copy", "lzma", "lzma2", "deflate", "bcj", "delta" }, encrypted = false }, } @@ -179,6 +182,20 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf elseif id == "\4\1\8" then local b, e = Deflate.inflate(src, so, sl, size) return b, if b then nil else e + elseif id == "\4\2\2" or id == "\4\247\17\1" then -- BZip2, Zstd (7-Zip-zstd: plain zstd frames) + local b, e + if id == "\4\2\2" then + b, e = Bzip2.decompress(src, so, sl, opts) + else + b, e = Zstd.decompress(src, so, sl, opts) + end + if not b then + return nil, e + end + if buffer.len(b) ~= size then + return nil, Err.new(Err.CORRUPT, methodName(id) .. " stream decodes to the wrong size") + end + return b elseif FILTERS[id] then if sl ~= size then return nil, Err.new(Err.CORRUPT, "filter size mismatch") diff --git a/tests/archive/7z.luau b/tests/archive/7z.luau index 2d8a3d0..c10d3b0 100644 --- a/tests/archive/7z.luau +++ b/tests/archive/7z.luau @@ -1,7 +1,7 @@ -- archive/7z tests: the contract and sniff; every archive 7-Zip wrote in tools/fixtures/7z.py (LZMA / LZMA2 / copy / -- Deflate, solid and not, BCJ / ARM64 / ARM / PPC + SPARC / delta chains, plain and encoded headers) lists exactly the -- source tree (names incl. UTF-16 surrogates, directories, empty files, mtimes) and reads every file back; unsupported --- methods (BZip2, PPMd, BCJ2, AES) list but fail on read with Err.UNSUPPORTED naming the method; corrupt CRCs; F.load; +-- methods (PPMd, BCJ2, AES) list but fail on read with Err.UNSUPPORTED naming the method; corrupt CRCs; F.load; -- the fuzz no-throw pass. local T = require("@tests/T") local SevenZip = require("@formats/archive/7z") diff --git a/tools/fixtures/7z.py b/tools/fixtures/7z.py index c3567d3..aa4d0c4 100644 --- a/tools/fixtures/7z.py +++ b/tools/fixtures/7z.py @@ -1,7 +1,7 @@ # 7z fixtures (tools/fixtures.py): a small tree (text, x86 machine code, runs, an empty file, empty and nested # directories, UTF-16 names with accents and a surrogate pair) archived by 7-Zip 2x (nix _7zz) with many method chains: # LZMA / LZMA2 / copy / Deflate, solid and non-solid, BCJ / ARM64 / delta filters, plain and compressed headers; plus -# archives whose methods are not supported (BZip2, PPMd, BCJ2, AES) and an empty archive. +# BZip2 archives (codecs/Bzip2), archives whose methods are not supported (PPMd, BCJ2, AES) and an empty archive. # tests/gen/7z.luau { files = { { name, dir, data?, mtime } }, cases = { { name, data, unsupported? } } } import os, random, subprocess, sys, tempfile @@ -85,7 +85,8 @@ def generate(ctx): make("lzma-lc-lp", ["-m0=LZMA:lc=0:lp=2:pb=0", "-ms=e"]) make("auto", ["-mx=9"]) make("fast", ["-mx=1"]) - make("bzip2", ["-m0=BZip2", "-mhc=off"], unsupported="bzip2") + make("bzip2", ["-m0=BZip2", "-mhc=off"]) + make("bzip2-solid-bcj", ["-m0=BCJ", "-m1=BZip2", "-ms=on", "-mhc=on"]) make("ppmd", ["-m0=PPMd", "-mhc=off"], unsupported="ppmd") make("bcj2", ["-m0=BCJ2", "-m1=LZMA", "-m2=LZMA", "-m3=LZMA", "-mb0s0:1", "-mb0s1:2", "-mb0s2:3", "-mhc=off"], unsupported="bcj2")