ws-arch2-unix: archive/lz4 (LZ4 frame read: linked / independent blocks, block / content XXH32, content size, skippable + legacy frames, concatenation; write: independent blocks) on codecs/compress/Lz4 (merged from ws/roblox ba4b20e) + codecs/compress/Xxh32; containers/Archive.streamData (shared single-stream payload: buffer / one entry / tar of entries; used by z and lz4); tests vs the lz4 tool both ways; bench lines

containers/Archive: additive helper streamData (requires containers/Tar); nothing existing changes.
This commit is contained in:
4DBug committed 2026-10-31 14:40:26 -05:00
1 parent 197ddc561a
commit 411261470a
10 files changed
+705 -34

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
--!strict
--!native
--!optimize 2
-- codecs/compress/Xxh32: XXH32 of a buffer range (any seed), for the LZ4 frame format's header, block and content
-- checksums. 32 x 32 -> 32 products split one operand into 16-bit limbs so every partial product is exact in a double.
-- From the xxHash specification (Yann Collet, BSD-2); no code copied. Never throws (the range is clamped to the buffer).
--
-- local h = Xxh32.hash(buf, offset?, length?, seed?) -- a number in 0 .. 2^32 - 1
local Xxh32 = {}
local bxor, bor, lshift, rshift = bit32.bxor, bit32.bor, bit32.lshift, bit32.rshift
local readu8, readu32 = buffer.readu8, buffer.readu32
local T32 = 4294967296
local P1, P2, P3, P4, P5 = 0x9E3779B1, 0x85EBCA77, 0xC2B2AE3D, 0x27D4EB2F, 0x165667B1
-- a * b mod 2^32 (a, b below 2^32)
local function mul(a: number, b: number): number
local a0 = a % 65536
local a1 = (a - a0) / 65536
return (a0 * b + ((a1 * b) % 65536) * 65536) % T32
end
local function rotl(x: number, r: number): number
return bor(lshift(x, r), rshift(x, 32 - r))
end
local function round(acc: number, lane: number): number
return mul(rotl((acc + mul(lane, P2)) % T32, 13), P1)
end
-- hash(buf, offset?, length?, seed?) -> XXH32
function Xxh32.hash(b: buffer, offset: number?, length: number?, seed: number?): number
local n0 = buffer.len(b)
local o = math.clamp(offset or 0, 0, n0)
local n = math.clamp(length or (n0 - o), 0, n0 - o)
local s = (seed or 0) % T32
local p = o
local e = o + n
local h
if n >= 16 then
local v1 = (s + P1 + P2) % T32
local v2 = (s + P2) % T32
local v3 = s
local v4 = (s - P1) % T32
local last = e - 16
while p <= last do
v1 = round(v1, readu32(b, p))
v2 = round(v2, readu32(b, p + 4))
v3 = round(v3, readu32(b, p + 8))
v4 = round(v4, readu32(b, p + 12))
p += 16
end
h = (rotl(v1, 1) + rotl(v2, 7) + rotl(v3, 12) + rotl(v4, 18)) % T32
else
h = (s + P5) % T32
end
h = (h + n) % T32
while p + 4 <= e do
h = mul(rotl((h + mul(readu32(b, p), P3)) % T32, 17), P4)
p += 4
end
while p < e do
h = mul(rotl((h + readu8(b, p) * P5) % T32, 11), P1)
p += 1
end
h = mul(bxor(h, rshift(h, 15)), P2)
h = mul(bxor(h, rshift(h, 13)), P3)
return bxor(h, rshift(h, 16))
end
return Xxh32