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:
1 parent
2dac8b2a14
commit
5b4ee7f937
5 files changed
+56
-33
No files matched your search
@@ -11,9 +11,6 @@ local bcreate, bcopy, bfill, blen = buffer.create, buffer.copy, buffer.fill, buf
|
|||||||
|
|
||||||
local Decode = {}
|
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)
|
local MASK = table.create(33, 0)
|
||||||
for i = 0, 32 do
|
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"
|
return nil, "block larger than its declared size", "corrupt"
|
||||||
end
|
end
|
||||||
local v = MTF[idx + 1]
|
local v = MTF[idx + 1]
|
||||||
for k = idx, 1, -1 do
|
if idx > 8 then
|
||||||
MTF[k + 1] = MTF[k]
|
table.move(MTF, 1, idx, 2)
|
||||||
|
else
|
||||||
|
for k = idx, 1, -1 do
|
||||||
|
MTF[k + 1] = MTF[k]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
MTF[1] = v
|
MTF[1] = v
|
||||||
local ch = SEQ[v + 1]
|
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)
|
out = grow(out, op, op + nblock + 4096)
|
||||||
end
|
end
|
||||||
local cap = blen(out)
|
local cap = blen(out)
|
||||||
local crc = 0xFFFFFFFF
|
local blockStart = op
|
||||||
local tPos = rshift(readu32(T, origPtr * 4), 8)
|
local tPos = rshift(readu32(T, origPtr * 4), 8)
|
||||||
local last, run = -1, 0
|
local last, run = -1, 0
|
||||||
local i = 0
|
local i = 0
|
||||||
@@ -468,9 +469,6 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
|||||||
end
|
end
|
||||||
bfill(out, op, last, ch)
|
bfill(out, op, last, ch)
|
||||||
op += ch
|
op += ch
|
||||||
for _ = 1, ch do
|
|
||||||
crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), last) + 1])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
run = 0
|
run = 0
|
||||||
last = -1
|
last = -1
|
||||||
@@ -490,13 +488,12 @@ function Decode.decode(b: buffer, o: number, e: number, maxBytes: number): (buff
|
|||||||
end
|
end
|
||||||
writeu8(out, op, ch)
|
writeu8(out, op, ch)
|
||||||
op += 1
|
op += 1
|
||||||
crc = bxor(lshift(crc, 8), CT[bxor(rshift(crc, 24), ch) + 1])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if op > maxBytes then
|
if op > maxBytes then
|
||||||
return nil, "output larger than maxBytes", "limit"
|
return nil, "output larger than maxBytes", "limit"
|
||||||
end
|
end
|
||||||
crc = bxor(crc, 0xFFFFFFFF)
|
local crc = bxor(Crc32.msb(out, blockStart, op - blockStart, 0xFFFFFFFF), 0xFFFFFFFF)
|
||||||
if crc ~= storedCrc then
|
if crc ~= storedCrc then
|
||||||
return nil, string.format("block CRC mismatch (%08x, expected %08x)", crc, storedCrc), "checksum"
|
return nil, string.format("block CRC mismatch (%08x, expected %08x)", crc, storedCrc), "checksum"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
-- Written from the format description (no third-party code); randomised blocks (bzip2 0.9.0 and older) are unsupported.
|
-- 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 Err = require("@rosetta/core/Err")
|
||||||
local Bytes = require("@rosetta/core/Bytes")
|
local Bytes = require("@rosetta/core/Bytes")
|
||||||
|
local Crc32 = require("@rosetta/core/Crc32")
|
||||||
local Decode = require("@rosetta/codecs/compress/Bzip2/Decode")
|
local Decode = require("@rosetta/codecs/compress/Bzip2/Decode")
|
||||||
|
|
||||||
local Bzip2 = {}
|
local Bzip2 = {}
|
||||||
@@ -33,14 +34,9 @@ function Bzip2.decompress(buf: buffer | string, offset: number?, length: number?
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Bzip2.crc(buf: buffer, offset: number?, length: number?): number
|
function Bzip2.crc(buf: buffer, offset: number?, length: number?): number
|
||||||
local CT = Decode.crcTable
|
|
||||||
local o = offset or 0
|
local o = offset or 0
|
||||||
local e = o + (length or (buffer.len(buf) - o))
|
local n = length or (buffer.len(buf) - o)
|
||||||
local c = 0xFFFFFFFF
|
return bit32.bxor(Crc32.msb(buf, o, n, 0xFFFFFFFF), 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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return Bzip2
|
return Bzip2
|
||||||
@@ -8,11 +8,9 @@
|
|||||||
-- of a multiplexed file come first when every stream's first packet is written (with flush) before any other.
|
-- of a multiplexed file come first when every stream's first packet is written (with flush) before any other.
|
||||||
-- Never throws: a bad argument returns false, Err.
|
-- Never throws: a bad argument returns false, Err.
|
||||||
local Err = require("@rosetta/core/Err")
|
local Err = require("@rosetta/core/Err")
|
||||||
local Page = require("@rosetta/containers/Ogg/Page")
|
local Crc32 = require("@rosetta/core/Crc32")
|
||||||
|
|
||||||
local CRC = Page.CRC_TABLE
|
local writeu8, writeu32, writei32 = buffer.writeu8, buffer.writeu32, buffer.writei32
|
||||||
local readu8, writeu8, writeu32, writei32 = buffer.readu8, buffer.writeu8, buffer.writeu32, buffer.writei32
|
|
||||||
local bxor, lshift, rshift = bit32.bxor, bit32.lshift, bit32.rshift
|
|
||||||
|
|
||||||
local Mux = {}
|
local Mux = {}
|
||||||
Mux.__index = Mux
|
Mux.__index = Mux
|
||||||
@@ -63,11 +61,7 @@ end
|
|||||||
|
|
||||||
-- the CRC of a page (the crc field written as zero)
|
-- the CRC of a page (the crc field written as zero)
|
||||||
local function crc(b: buffer, n: number): number
|
local function crc(b: buffer, n: number): number
|
||||||
local c = 0
|
return Crc32.msb(b, 0, n, 0)
|
||||||
for i = 0, n - 1 do
|
|
||||||
c = bxor(lshift(c, 8), CRC[bxor(rshift(c, 24), readu8(b, i)) + 1])
|
|
||||||
end
|
|
||||||
return c
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Stream._flush(self: any, eos: boolean)
|
function Stream._flush(self: any, eos: boolean)
|
||||||
|
|||||||
@@ -32,10 +32,7 @@ local CRC = Crc32.MSB_TABLE
|
|||||||
Page.CRC_TABLE = CRC
|
Page.CRC_TABLE = CRC
|
||||||
|
|
||||||
local function crcRange(b: buffer, from: number, to: number, crc: number): number
|
local function crcRange(b: buffer, from: number, to: number, crc: number): number
|
||||||
for i = from, to - 1 do
|
return Crc32.msb(b, from, to - from, crc)
|
||||||
crc = bxor(lshift(crc, 8), CRC[bxor(rshift(crc, 24), readu8(b, i)) + 1])
|
|
||||||
end
|
|
||||||
return crc
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- the Ogg CRC-32 of arbitrary data (the crc field is not zeroed)
|
-- the Ogg CRC-32 of arbitrary data (the crc field is not zeroed)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
--!native
|
--!native
|
||||||
-- core/Crc32: CRC-32 (IEEE 802.3, the zlib / gzip / zip / PNG polynomial 0xEDB88320, reflected), slice-by-8.
|
-- 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.
|
-- 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 band, bxor, rshift, lshift, byteswap = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift, bit32.byteswap
|
||||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
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 T0, T1, T2, T3 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||||
@@ -99,10 +99,49 @@ for n = 0, 255 do
|
|||||||
end
|
end
|
||||||
Crc32.MSB_TABLE = MSB
|
Crc32.MSB_TABLE = MSB
|
||||||
|
|
||||||
|
-- slice-by-8 for the MSB-first CRC: M[k][n] = the CRC register after byte n and k zero bytes
|
||||||
|
local M1, M2, M3 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||||
|
local M4, M5, M6, M7 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||||
|
for n = 0, 255 do
|
||||||
|
local c = MSB[n + 1]
|
||||||
|
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||||
|
M1[n + 1] = c
|
||||||
|
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||||
|
M2[n + 1] = c
|
||||||
|
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||||
|
M3[n + 1] = c
|
||||||
|
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||||
|
M4[n + 1] = c
|
||||||
|
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||||
|
M5[n + 1] = c
|
||||||
|
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||||
|
M6[n + 1] = c
|
||||||
|
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
|
||||||
|
M7[n + 1] = c
|
||||||
|
end
|
||||||
|
|
||||||
function Crc32.msb(buf: buffer, off: number, len: number, init: number?): number
|
function Crc32.msb(buf: buffer, off: number, len: number, init: number?): number
|
||||||
local c = init or 0
|
local c = init or 0
|
||||||
for i = off, off + len - 1 do
|
local i, e = off, off + len
|
||||||
|
local e8 = e - 8
|
||||||
|
while i <= e8 do
|
||||||
|
c = bxor(c, byteswap(readu32(buf, i)))
|
||||||
|
local d = byteswap(readu32(buf, i + 4))
|
||||||
|
c = bxor(
|
||||||
|
M7[rshift(c, 24) + 1],
|
||||||
|
M6[band(rshift(c, 16), 255) + 1],
|
||||||
|
M5[band(rshift(c, 8), 255) + 1],
|
||||||
|
M4[band(c, 255) + 1],
|
||||||
|
M3[rshift(d, 24) + 1],
|
||||||
|
M2[band(rshift(d, 16), 255) + 1],
|
||||||
|
M1[band(rshift(d, 8), 255) + 1],
|
||||||
|
MSB[band(d, 255) + 1]
|
||||||
|
)
|
||||||
|
i += 8
|
||||||
|
end
|
||||||
|
while i < e do
|
||||||
c = bxor(lshift(c, 8), MSB[bxor(rshift(c, 24), readu8(buf, i)) + 1])
|
c = bxor(lshift(c, 8), MSB[bxor(rshift(c, 24), readu8(buf, i)) + 1])
|
||||||
|
i += 1
|
||||||
end
|
end
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in new issue
Block a user