perf: slice-by-8 MSB-first CRC-32 (Crc32.msb) for Ogg pages and bzip2 blocks; bzip2 checks each block's CRC over its output at once and moves long MTF shifts with table.move

This commit is contained in:
4DBug committed 2026-11-01 19:46:46 -06:00
1 parent 2dac8b2a14
commit 5b4ee7f937
5 files changed
+56 -33

No files matched your search

+8 -11
View File
@@ -11,9 +11,6 @@ local bcreate, bcopy, bfill, blen = buffer.create, buffer.copy, buffer.fill, buf
local Decode = {}
-- CRC-32/BZIP2 table (polynomial 0x04C11DB7, not reflected: core/Crc32)
local CT = Crc32.MSB_TABLE
Decode.crcTable = CT
local MASK = table.create(33, 0)
for i = 0, 32 do
@@ -413,8 +410,12 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
return nil, "block larger than its declared size", "corrupt"
end
local v = MTF[idx + 1]
for k = idx, 1, -1 do
MTF[k + 1] = MTF[k]
if idx > 8 then
table.move(MTF, 1, idx, 2)
else
for k = idx, 1, -1 do
MTF[k + 1] = MTF[k]
end
end
MTF[1] = v
local ch = SEQ[v + 1]
@@ -447,7 +448,7 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
out = grow(out, op, op + nblock + 4096)
end
local cap = blen(out)
local crc = 0xFFFFFFFF
local blockStart = op
local tPos = rshift(readu32(T, origPtr * 4), 8)
local last, run = -1, 0
local i = 0
@@ -468,9 +469,6 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
end
bfill(out, op, last, ch)
op += ch
for _ = 1, ch do
crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), last) + 1])
end
end
run = 0
last = -1
@@ -490,13 +488,12 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
end
writeu8(out, op, ch)
op += 1
crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), ch) + 1])
end
end
if op > maxBytes then
return nil, "output larger than maxBytes", "limit"
end
crc = bxor(crc, 0xFFFFFFFF)
local crc = bxor(Crc32.msb(out, blockStart, op - blockStart, 0xFFFFFFFF), 0xFFFFFFFF)
if crc ~= storedCrc then
return nil, string.format("block CRC mismatch (%08x, expected %08x)", crc, storedCrc), "checksum"
end
+3 -7
View File
@@ -3,6 +3,7 @@
-- Written from the format description (no third-party code); randomised blocks (bzip2 0.9.0 and older) are unsupported.
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Decode = require("@rosetta/codecs/compress/Bzip2/Decode")
local Bzip2 = {}
@@ -33,14 +34,9 @@ function Bzip2.decompress(buf: buffer | string, offset: number?, length: number?
end
function Bzip2.crc(buf: buffer, offset: number?, length: number?): number
local CT = Decode.crcTable
local o = offset or 0
local e = o + (length or (buffer.len(buf) - o))
local c = 0xFFFFFFFF
for i = o, e - 1 do
c = bit32.bxor(bit32.lshift(c, 8), CT[bit32.bxor(bit32.rshift(c, 24), buffer.readu8(buf, i)) + 1])
end
return bit32.bxor(c, 0xFFFFFFFF)
local n = length or (buffer.len(buf) - o)
return bit32.bxor(Crc32.msb(buf, o, n, 0xFFFFFFFF), 0xFFFFFFFF)
end
return Bzip2