ws-cleanup: shared helpers instead of copies (CRCs, codec arguments, utf8char, keyLess / isArray)

A sweep for duplicated function bodies, CRC tables and base64 across src/Formats:
- core/Crc32.msb / MSB_TABLE: the MSB-first 0x04C11DB7 CRC-32 that containers/Ogg (Page + Mux), containers/MpegTs
  and codecs/Bzip2 each built (test vectors CRC-32/MPEG-2 and /BZIP2 in tests/core/bytes).
- audio/flac/Encode uses audio/flac/Frame's CRC-8 / CRC-16 tables (Frame.crc8 added) instead of its own copy.
- core/Bytes.toBuffer / Bytes.span: the (buffer | string, offset, length) argument checking codecs/Deflate,
  codecs/Lzma and codecs/Brotli each carried.
- codecs/Text.utf8char: one copy instead of four (codecs/Json, codecs/Xml, data/yaml, data/toml).
- Types.keyLess / Types.isArray (small additions to Types, backward compatible): data/msgpack, data/cbor and
  data/yaml/Emit had identical copies.
- playlist/pls: dead copies of m3u's attributes / extinf removed.
Left on purpose: per-codec bit readers / writers in hot loops (STYLE.md: inner loops keep their own bit buffers),
same-folder helpers (Alac signExtend, Vorbis ilog), Zip's DOS-date civil() (a different epoch helper).
This commit is contained in:
4DBug committed 2026-10-31 10:58:31 -05:00
1 parent 7169f2cc2c
commit f784b4eb9b
22 files changed
+152 -311

No files matched your search

+3 -18
View File
@@ -12,6 +12,7 @@
--
-- 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")
@@ -63,30 +64,14 @@ function Brotli.dictionary(): buffer?
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)
local b, o, l, aerr = Bytes.span("Brotli.decompress", buf, offset, length)
if not b then
return nil, Err.new(Err.ARG, "Brotli.decompress: expected a buffer or a string, got " .. typeof(buf))
return nil, aerr
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")