codecs/compress: comments to specs only
This commit is contained in:
1 parent
f1ad32105b
commit
f626fb4295
23 files changed
+123
-498
No files matched your search
@@ -1,15 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/compress/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("@rosetta/codecs/compress/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).
|
||||
-- codecs/compress/Zstd: Zstandard decompression (RFC 8878), written from the specification; no dictionaries.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Decode = require("@rosetta/codecs/compress/Zstd/Decode")
|
||||
@@ -17,14 +7,13 @@ local Decode = require("@rosetta/codecs/compress/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)
|
||||
maxBytes: number?,
|
||||
verify: boolean?,
|
||||
}
|
||||
|
||||
-- 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
|
||||
@@ -34,8 +23,7 @@ function Zstd.isFrame(buf: buffer, offset: number?): boolean
|
||||
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).
|
||||
-- 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
|
||||
@@ -58,7 +46,6 @@ function Zstd.decompress(buf: buffer | string, offset: number?, length: number?,
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user