Files
rosetta/src/Formats/codecs/Lzma/Crc64.luau
T

38 lines
1.1 KiB
Luau

--!strict
--!native
--!optimize 2
-- codecs/Lzma/Crc64: CRC-64/XZ (ECMA-182 polynomial, reflected: 0xC96C5795D7870F42), the xz check type 4. The 64-bit
-- value is kept as two u32 halves (hi, lo). Written from the xz file format specification. Never throws.
local band, bxor, rshift, lshift, bor = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift, bit32.bor
local readu8 = buffer.readu8
local THI, TLO = table.create(256, 0), table.create(256, 0)
for n = 0, 255 do
local hi, lo = 0, n
for _ = 1, 8 do
local low = band(lo, 1)
lo = bor(rshift(lo, 1), lshift(band(hi, 1), 31))
hi = rshift(hi, 1)
if low ~= 0 then
hi = bxor(hi, 0xC96C5795)
lo = bxor(lo, 0xD7870F42)
end
end
THI[n + 1], TLO[n + 1] = hi, lo
end
local Crc64 = {}
-- of(buf, offset, length) -> hi, lo
function Crc64.of(b: buffer, o: number, n: number): (number, number)
local hi, lo = 0xFFFFFFFF, 0xFFFFFFFF
for i = o, o + n - 1 do
local idx = band(bxor(lo, readu8(b, i)), 255) + 1
lo = bxor(TLO[idx], bor(rshift(lo, 8), lshift(band(hi, 255), 24)))
hi = bxor(THI[idx], rshift(hi, 8))
end
return bxor(hi, 0xFFFFFFFF), bxor(lo, 0xFFFFFFFF)
end
return Crc64