--!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