ws-arch: codecs/Brotli (RFC 7932 decoder: all meta-block kinds, context modes/maps, static dictionary + 121 transforms, large windows) and archive/br (single-stream archive); tools/brotli_static.py generates codecs/Brotli/Static from the brotli sources (MIT, licenses/brotli-LICENSE.txt); fixtures via python brotli + CLI

This commit is contained in:
4DBug committed 2026-10-31 08:36:16 -05:00
1 parent 0ca32501a4
commit 0bfd70e1ca
12 files changed
+2524

No files matched your search

+150
View File
@@ -0,0 +1,150 @@
--!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 Crc32 = require("@formats/core/Crc32")
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
local B64 = {}
do
local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
for i = 1, 64 do
B64[string.byte(alphabet, i)] = i - 1
end
end
local function unbase64(s: string): buffer
local n = #s
local pad = 0
if string.sub(s, -2) == "==" then
pad = 2
elseif string.sub(s, -1) == "=" then
pad = 1
end
local out = buffer.create(n // 4 * 3 - pad)
local o = 0
for i = 1, n, 4 do
local a, b, c, d = string.byte(s, i, i + 3)
local v = B64[a] * 262144 + B64[b] * 4096 + (B64[c] or 0) * 64 + (B64[d] or 0)
buffer.writeu8(out, o, v // 65536)
if o + 1 < buffer.len(out) then
buffer.writeu8(out, o + 1, (v // 256) % 256)
end
if o + 2 < buffer.len(out) then
buffer.writeu8(out, o + 2, v % 256)
end
o += 3
end
return out
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
local function toBuffer(b: any): buffer?
if type(b) == "string" then
return buffer.fromstring(b)
end
return if type(b) == "buffer" then b else nil
end
-- 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 = toBuffer(buf)
if not b then
return nil, Err.new(Err.ARG, "Brotli.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, "Brotli.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, "Brotli.decompress: bad length " .. tostring(length))
end
l = math.min(l, n - o)
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 msg = tostring(size)
local code = Err.CORRUPT
if string.find(msg, "truncated", 1, true) then
code = Err.TRUNCATED
elseif string.sub(msg, 1, 6) == "limit:" then
code = Err.LIMIT
elseif string.sub(msg, 1, 12) == "unsupported:" then
code = Err.UNSUPPORTED
end
return nil, Err.new(code, "brotli: " .. msg, o)
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