- 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.
102 lines
3.9 KiB
Luau
102 lines
3.9 KiB
Luau
--!strict
|
|
-- codecs/Brotli: Brotli (RFC 7932) decompression. A codec (layer 3): archive/br and font/woff2 build on it. Written
|
|
-- from the RFC (decoder in codecs/Brotli/Decode); the static dictionary, context table and word transforms come from
|
|
-- google/brotli (MIT, licenses/brotli-LICENSE.txt) through codecs/Brotli/Static (tools/brotli_static.py).
|
|
-- Supports every RFC 7932 stream (window sizes 10..24, metadata / uncompressed / compressed meta-blocks, the static
|
|
-- dictionary with all 121 transforms) and the large-window extension (opts.largeWindow). Not supported: compression,
|
|
-- shared / compound dictionaries (draft "shared brotli").
|
|
--
|
|
-- local Brotli = require("@formats/codecs/Brotli")
|
|
-- local out, consumedEnd = Brotli.decompress(buf) -- buffer | string
|
|
-- local out, err = Brotli.decompress(buf, offset, length, { maxBytes = 2^24 })
|
|
--
|
|
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / LIMIT / UNSUPPORTED / ARG).
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Crc32 = require("@formats/core/Crc32")
|
|
local Base64 = require("@formats/core/Base64")
|
|
local Deflate = require("@formats/codecs/Deflate")
|
|
local Decode = require("@formats/codecs/Brotli/Decode")
|
|
local StaticData = require("@formats/codecs/Brotli/Static")
|
|
|
|
local Brotli = {}
|
|
|
|
export type Options = {
|
|
maxBytes: number?, -- refuse output larger than this (default 2^30)
|
|
largeWindow: boolean?, -- accept the large-window extension (windows up to 2^30; default true)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ static data
|
|
-- the tables are base64 text (trusted, generated by tools/brotli_static.py)
|
|
local function unbase64(s: string): buffer
|
|
return Base64.decode(s) or buffer.create(0)
|
|
end
|
|
|
|
local static: Decode.Static? = nil
|
|
|
|
-- tables() -> the decoded static data (inflated once, on first use)
|
|
local function tables(): (Decode.Static?, Err.Err?)
|
|
if static then
|
|
return static, nil
|
|
end
|
|
local z = unbase64(table.concat(StaticData.dictionary))
|
|
local dict, err = Deflate.zlib(z, 0, nil, StaticData.dictionarySize)
|
|
if not dict then
|
|
return nil, err
|
|
end
|
|
if Crc32.of(dict) ~= StaticData.dictionaryCrc then
|
|
return nil, Err.new(Err.CHECKSUM, "brotli: static dictionary damaged")
|
|
end
|
|
static = {
|
|
dictionary = dict,
|
|
context = unbase64(table.concat(StaticData.context)),
|
|
prefixSuffix = unbase64(StaticData.prefixSuffix),
|
|
prefixSuffixMap = StaticData.prefixSuffixMap,
|
|
transforms = StaticData.transforms,
|
|
}
|
|
return static, nil
|
|
end
|
|
|
|
-- dictionary() -> the 122784-byte RFC 7932 static dictionary (for tests and encoders)
|
|
function Brotli.dictionary(): buffer?
|
|
local s = tables()
|
|
return if s then s.dictionary else nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decompress
|
|
|
|
-- decompress(buf, offset?, length?, opts?) -> buffer, consumedEnd | nil, Err. consumedEnd: the input offset just past
|
|
-- the stream (trailing bytes are ignored).
|
|
function Brotli.decompress(buf: buffer | string, offset: number?, length: number?, opts: Options?): (buffer?, any)
|
|
local b, o, l, aerr = Bytes.span("Brotli.decompress", buf, offset, length)
|
|
if not b then
|
|
return nil, aerr
|
|
end
|
|
local maxBytes = if opts and opts.maxBytes then opts.maxBytes else 2 ^ 30
|
|
if type(maxBytes) ~= "number" or maxBytes < 0 then
|
|
return nil, Err.new(Err.ARG, "Brotli.decompress: bad maxBytes")
|
|
end
|
|
local large = not (opts and opts.largeWindow == false)
|
|
if l == 0 then
|
|
return nil, Err.new(Err.TRUNCATED, "brotli: empty input", o)
|
|
end
|
|
local S, serr = tables()
|
|
if not S then
|
|
return nil, serr
|
|
end
|
|
local out, size, consumed = Decode.decompress(b, o, l, maxBytes, S, large)
|
|
if not out then
|
|
local e = Err.from(size, o)
|
|
e.message = "brotli: " .. e.message
|
|
return nil, e
|
|
end
|
|
local res = out
|
|
if buffer.len(out) ~= size then
|
|
res = buffer.create(size)
|
|
buffer.copy(res, 0, out, 0, size)
|
|
end
|
|
return res, consumed
|
|
end
|
|
|
|
return Brotli
|