Files
rosetta/src/Rosetta/core/Crc32.luau
T

111 lines
3.2 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.
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
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
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
Crc32.raw = crc32
-- MSB-first CRC-32 (polynomial 0x04C11DB7, not reflected, raw register, no inversions): Ogg pages (init 0),
-- MPEG-2 PSI sections and bzip2 blocks (init 0xFFFFFFFF)
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
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