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).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 10:58:31 -05:00
1 parent 6b68c2ffab
commit 8158acf3d5
22 files changed
+152 -311

No files matched your search

+30
View File
@@ -13,6 +13,8 @@
--
-- local w = Bytes.writer() -- or Bytes.writer(initialCapacity)
-- w:string("abcd"); w:u32be(width); local out = w:finish()
--
-- local b, o, n, err = Bytes.span("Deflate.inflate", input, offset?, length?) -- codec argument checking
local Err = require("@formats/core/Err")
local readu8, readi8, readu16, readi16, readu32, readi32 = buffer.readu8, buffer.readi8, buffer.readu16, buffer.readi16, buffer.readu32, buffer.readi32
@@ -534,4 +536,32 @@ end
Bytes.Reader = Reader
Bytes.Writer = Writer
-- ------------------------------------------------------------------------------------------------ arguments
-- toBuffer(v) -> v as a buffer (strings are copied) | nil for anything else
function Bytes.toBuffer(v: any): buffer?
if type(v) == "string" then
return buffer.fromstring(v)
end
return if type(v) == "buffer" then v else nil
end
-- span(what, input, offset?, length?) -> buffer, offset, length | nil, 0, 0, Err.ARG: a codec's (buffer | string,
-- offset, length) arguments checked (whole numbers, offset inside the data; the length is clamped to the end)
function Bytes.span(what: string, input: any, offset: number?, length: number?): (buffer?, number, number, Err.Err?)
local b = Bytes.toBuffer(input)
if not b then
return nil, 0, 0, Err.new(Err.ARG, what .. ": expected a buffer or a string, got " .. typeof(input))
end
local n = buffer.len(b)
local o = offset or 0
local l = length or (n - (if type(o) == "number" then o else 0))
if type(o) ~= "number" or o < 0 or o > n or o % 1 ~= 0 or type(l) ~= "number" or l < 0 or l % 1 ~= 0 then
return nil, 0, 0, Err.new(Err.ARG, what .. ": bad offset / length " .. tostring(offset) .. ", " .. tostring(length))
end
if o + l > n then
l = n - o
end
return b, o, l, nil
end
return Bytes
+24 -1
View File
@@ -6,7 +6,10 @@
--
-- Crc32.of(buf, offset?, length?, crc?) -> u32 (pass a previous result as crc to continue it)
-- Crc32.string(s, crc?) -> u32
local band, bxor, rshift = bit32.band, bit32.bxor, bit32.rshift
-- Crc32.msb(buf, offset, length, init?) -> u32 the MSB-first (unreflected) 0x04C11DB7 CRC, raw register: no
-- inversions (Ogg: init 0; MPEG-2 sections / bzip2: 0xFFFFFFFF)
-- Crc32.MSB_TABLE its 1-based byte table, for codecs that inline it (bzip2)
local band, bxor, rshift, lshift = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift
local readu8, readu32 = buffer.readu8, buffer.readu32
local T0, T1, T2, T3 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
@@ -95,4 +98,24 @@ end
-- raw(buf, off, len, crc?): no clamping, for callers that already checked the range (hot paths)
Crc32.raw = crc32
-- the MSB-first CRC-32 (polynomial 0x04C11DB7, not reflected): Ogg pages, MPEG-2 PSI sections, bzip2 blocks
local MSB = table.create(256, 0)
for n = 0, 255 do
local c = lshift(n, 24)
for _ = 1, 8 do
c = if band(c, 0x80000000) ~= 0 then bxor(lshift(c, 1), 0x04C11DB7) else lshift(c, 1)
end
MSB[n + 1] = c
end
Crc32.MSB_TABLE = MSB
-- msb(buf, off, len, init?) -> the register after buf[off, off + len) (no inversions; the caller checked the range)
function Crc32.msb(buf: buffer, off: number, len: number, init: number?): number
local c = init or 0
for i = off, off + len - 1 do
c = bxor(lshift(c, 8), MSB[bxor(rshift(c, 24), readu8(buf, i)) + 1])
end
return c
end
return Crc32