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

+2 -18
View File
@@ -11,6 +11,7 @@
--
-- Native code must never throw (an error unwinding through native code crashes Roblox Studio), so every read is
-- bounds-checked by construction and bad input is skipped and counted in `stats`, never raised.
local Crc32 = require("@formats/core/Crc32")
local MpegTs = {}
@@ -103,25 +104,8 @@ local CODECS: { [number]: { string } } = {
}
-- CRC-32/MPEG-2 (poly 0x04C11DB7, init 0xFFFFFFFF, MSB first, no final xor): a section including its CRC sums to 0
local CRC = buffer.create(256 * 4)
for i = 0, 255 do
local c = lshift(i, 24)
for _ = 1, 8 do
if band(c, 0x80000000) ~= 0 then
c = bit32.bxor(lshift(c, 1), 0x04C11DB7)
else
c = lshift(c, 1)
end
end
buffer.writeu32(CRC, i * 4, c)
end
local function crc32(b: buffer, off: number, n: number): number
local c = 0xFFFFFFFF
for i = off, off + n - 1 do
c = bit32.bxor(lshift(c, 8), buffer.readu32(CRC, band(bit32.bxor(rshift(c, 24), readu8(b, i)), 255) * 4))
end
return c
return Crc32.msb(b, off, n, 0xFFFFFFFF)
end
MpegTs.crc32 = crc32
+3 -13
View File
@@ -8,6 +8,7 @@
-- A page header (27 bytes + segment table):
-- 0 "OggS" | 4 version (0) | 5 flags (1 continued, 2 bos, 4 eos) | 6 granule (i64 LE) | 14 serial (u32)
-- 18 sequence (u32) | 22 crc (u32) | 26 segment count | 27 lacing values | data
local Crc32 = require("@formats/core/Crc32")
local readu8, readu32, readi32 = buffer.readu8, buffer.readu32, buffer.readi32
local band, bxor, lshift, rshift = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift
@@ -27,19 +28,8 @@ export type Info = {
sequence: number,
}
-- CRC table, MSB-first
local CRC = table.create(256, 0)
for i = 0, 255 do
local r = lshift(i, 24)
for _ = 1, 8 do
if band(r, 0x80000000) ~= 0 then
r = bxor(lshift(r, 1), 0x04C11DB7)
else
r = lshift(r, 1)
end
end
CRC[i + 1] = r
end
-- CRC table, MSB-first (core/Crc32)
local CRC = Crc32.MSB_TABLE
Page.CRC_TABLE = CRC
-- CRC of bytes [from, to) (caller guarantees bounds), continuing from crc