ws-arch: codecs/Zstd (RFC 8878 decoder from the spec: frames, skippable frames, raw/RLE/compressed blocks, Huffman literals, FSE tables, repeat offsets, XXH64 checksum on 32-bit halves) + archive/zst (single-stream archive); tests against the zstd CLI on both runtimes

This commit is contained in:
4DBug committed 2026-10-31 08:36:24 -05:00
1 parent 0ca32501a4
commit 45cc667083
9 files changed
+1591

No files matched your search

+89
View File
@@ -0,0 +1,89 @@
--!strict
-- codecs/Zstd: Zstandard decompression (RFC 8878). A codec (layer 3): archive/zst and anything else carrying zstd
-- data build on it. Written from the specification. Supports every frame feature a dictionary-less stream uses:
-- concatenated and skippable frames, raw / RLE / compressed blocks, Huffman literals (1 / 4 streams, repeated tables),
-- predefined / RLE / compressed / repeated FSE tables, repeat offsets, the XXH64 content checksum (verified).
-- Not supported: dictionaries (Err.UNSUPPORTED), compression.
--
-- local Zstd = require("@formats/codecs/Zstd")
-- local out, err = Zstd.decompress(buf) -- -> buffer, consumedEnd | nil, Err
-- local out, err = Zstd.decompress(buf, off, len, { maxBytes = 2^24, verify = false })
--
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / CHECKSUM / UNSUPPORTED / LIMIT / ARG).
local Err = require("@formats/core/Err")
local Decode = require("@formats/codecs/Zstd/Decode")
local Zstd = {}
export type Options = {
maxBytes: number?, -- refuse output larger than this (default 2^30)
verify: boolean?, -- check the content checksum when a frame has one (default true)
}
local CODES: { [string]: string } = {
TRUNCATED = Err.TRUNCATED,
CORRUPT = Err.CORRUPT,
CHECKSUM = Err.CHECKSUM,
UNSUPPORTED = Err.UNSUPPORTED,
LIMIT = Err.LIMIT,
}
-- magic numbers: a zstd frame, and skippable frames (0x184D2A50 .. 0x184D2A5F)
Zstd.MAGIC = "\40\181\47\253"
-- isFrame(buf, offset?) -> true when a zstd (or skippable) frame starts there
function Zstd.isFrame(buf: buffer, offset: number?): boolean
local o = offset or 0
if type(buf) ~= "buffer" or o < 0 or o + 4 > buffer.len(buf) then
return false
end
local m = buffer.readu32(buf, o)
return m == 0xFD2FB528 or bit32.band(m, 0xFFFFFFF0) == 0x184D2A50
end
-- decompress(buf, offset?, length?, opts?) -> buffer, consumedEnd | nil, Err. Every frame in the range is decoded
-- and the outputs concatenated; data after the last frame that is not a frame is an error (as zstd -d does).
function Zstd.decompress(buf: buffer | string, offset: number?, length: number?, opts: Options?): (buffer?, any)
local b: buffer
if type(buf) == "string" then
b = buffer.fromstring(buf)
elseif type(buf) == "buffer" then
b = buf
else
return nil, Err.new(Err.ARG, "Zstd.decompress: expected a buffer or a string, got " .. typeof(buf))
end
local n = buffer.len(b)
local o = offset or 0
if type(o) ~= "number" or o < 0 or o > n or o % 1 ~= 0 then
return nil, Err.new(Err.ARG, "Zstd.decompress: bad offset " .. tostring(offset))
end
local l = length or (n - o)
if type(l) ~= "number" or l < 0 or l % 1 ~= 0 then
return nil, Err.new(Err.ARG, "Zstd.decompress: bad length " .. tostring(length))
end
l = math.min(l, n - o)
local maxBytes = if opts and opts.maxBytes ~= nil then opts.maxBytes else 2 ^ 30
if type(maxBytes) ~= "number" or maxBytes < 0 then
return nil, Err.new(Err.ARG, "Zstd.decompress: bad maxBytes " .. tostring(maxBytes))
end
local verify = not (opts and opts.verify == false)
local out, len, consumed, at = Decode.decompress(b, o, o + l, maxBytes, verify)
if not out then
return nil, Err.new(CODES[len] or Err.CORRUPT, tostring(consumed), at)
end
if buffer.len(out) ~= len then
local r = buffer.create(len)
buffer.copy(r, 0, out, 0, len)
out = r
end
return out, consumed
end
-- xxh64(buf, offset?, length?) -> hi, lo: the XXH64 hash (seed 0) as two unsigned 32-bit halves
function Zstd.xxh64(buf: buffer, offset: number?, length: number?): (number, number)
local o = math.clamp(offset or 0, 0, buffer.len(buf))
local l = math.clamp(length or (buffer.len(buf) - o), 0, buffer.len(buf) - o)
return Decode.hash(buf, o, l)
end
return Zstd