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]>
122 lines
4.0 KiB
Luau
122 lines
4.0 KiB
Luau
--!strict
|
|
--!native
|
|
-- core/Crc32: CRC-32 (IEEE 802.3, the zlib / gzip / zip / PNG polynomial 0xEDB88320, reflected), slice-by-8.
|
|
-- Absorbed from deflate-luau src/Deflate/Checksum.luau (same author;
|
|
-- no third-party code). Never throws: the range is clamped.
|
|
--
|
|
-- Crc32.of(buf, offset?, length?, crc?) -> u32 (pass a previous result as crc to continue it)
|
|
-- Crc32.string(s, crc?) -> u32
|
|
-- 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)
|
|
local T4, T5, T6, T7 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
|
for n = 0, 255 do
|
|
local c = n
|
|
for _ = 1, 8 do
|
|
if band(c, 1) ~= 0 then
|
|
c = bxor(0xEDB88320, rshift(c, 1))
|
|
else
|
|
c = rshift(c, 1)
|
|
end
|
|
end
|
|
T0[n + 1] = c
|
|
end
|
|
for n = 0, 255 do
|
|
local c = T0[n + 1]
|
|
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
|
T1[n + 1] = c
|
|
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
|
T2[n + 1] = c
|
|
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
|
T3[n + 1] = c
|
|
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
|
T4[n + 1] = c
|
|
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
|
T5[n + 1] = c
|
|
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
|
T6[n + 1] = c
|
|
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
|
T7[n + 1] = c
|
|
end
|
|
|
|
-- CRC-32 of buf[off, off + len), continuing crc (default 0 = a new CRC). Values are finished CRCs (the inversion
|
|
-- happens inside), so crc(b, crc(a)) == the CRC of a .. b.
|
|
local function crc32(buf: buffer, off: number, len: number, crc: number?): number
|
|
local c = bxor(crc or 0, 0xFFFFFFFF)
|
|
local i, e = off, off + len
|
|
local e8 = e - 8
|
|
while i <= e8 do
|
|
c = bxor(c, readu32(buf, i))
|
|
local d = readu32(buf, i + 4)
|
|
c = bxor(
|
|
T7[band(c, 255) + 1],
|
|
T6[band(rshift(c, 8), 255) + 1],
|
|
T5[band(rshift(c, 16), 255) + 1],
|
|
T4[rshift(c, 24) + 1],
|
|
T3[band(d, 255) + 1],
|
|
T2[band(rshift(d, 8), 255) + 1],
|
|
T1[band(rshift(d, 16), 255) + 1],
|
|
T0[rshift(d, 24) + 1]
|
|
)
|
|
i += 8
|
|
end
|
|
local e4 = e - 4
|
|
while i <= e4 do
|
|
c = bxor(c, readu32(buf, i))
|
|
c = bxor(T3[band(c, 255) + 1], T2[band(rshift(c, 8), 255) + 1], T1[band(rshift(c, 16), 255) + 1], T0[rshift(c, 24) + 1])
|
|
i += 4
|
|
end
|
|
while i < e do
|
|
c = bxor(T0[band(bxor(c, readu8(buf, i)), 255) + 1], rshift(c, 8))
|
|
i += 1
|
|
end
|
|
return bxor(c, 0xFFFFFFFF)
|
|
end
|
|
|
|
-- clamp (offset, length) to the buffer
|
|
local function range(buf: buffer, offset: number?, length: number?): (number, number)
|
|
local n = buffer.len(buf)
|
|
local o = math.clamp(offset or 0, 0, n)
|
|
return o, math.clamp(length or (n - o), 0, n - o)
|
|
end
|
|
|
|
local Crc32 = {}
|
|
|
|
function Crc32.of(buf: buffer, offset: number?, length: number?, crc: number?): number
|
|
local o, l = range(buf, offset, length)
|
|
return crc32(buf, o, l, crc)
|
|
end
|
|
|
|
function Crc32.string(s: string, crc: number?): number
|
|
return crc32(buffer.fromstring(s), 0, #s, crc)
|
|
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
|