Files
rosetta/src/Formats/codecs/Zstd/init.luau
T
4DBug 763f4a7c04 ws-cleanup: shared plumbing: Err.from, core/Sniff, Pcm.drain, Bits.msbPut; DUP lint; tools/move.py
- core/Err.from(message, offset?, kind?): the private-submodule -> Err plumbing (a kind, or a 'code: ' prefix /
  'truncated' / 'past the end' in the message) that Brotli, Bzip2, Zstd, Jpeg, WOFF2, FBX, plist, msgpack and
  bmfont each wrote.
- core/Sniff (new): Sniff.text(r, n, trim?) (the start of a text file without its BOM), Sniff.dropBom(s),
  Sniff.magic(r, s); 13 text formats' sniff / decode BOM handling goes through it.
- codecs/Pcm.drain(dec, source?, maxFrames?): a format's decode() through its open(); the collect-the-Decoder
  loops of aac, aiff, au, caf, flac, m4a, mp3, opus, vorbis and wav are one call each.
- core/Bits.msbPut: the MSB-first bit writer the MP3 / MP2 / FLAC encoders each copied.
- tools/lint_rules.py DUP: a function body of 12+ code lines repeated token for token in another module fails.
- tools/move.py: moves a module and rewrites every reference (requires, deps, headers, fixture INPUTS, docs).
- tools/fixtures/zstd.py: the incompressible corpus is smaller (zstd.luau 15.1 -> 13.4 MB; every fixture module is
  now under 14.3 MB).
- tools/bench.luau: MPEG-1 also runs with the api's LuaJIT trace limits; the sections that compare tuned / default
  limits set them explicitly (jitTune), so the order of sections no longer leaks the tuning.
2026-10-31 12:00:59 -05:00

69 lines
3.0 KiB
Luau

--!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 Bytes = require("@formats/core/Bytes")
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)
}
-- 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, o, l, aerr = Bytes.span("Zstd.decompress", buf, offset, length)
if not b then
return nil, aerr
end
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.from(consumed, at, len)
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