formats-luau foundation: build (darklua + token passes) for Roblox and LuaJIT, compat prelude, core, Types, host adapters, API + registry, lint, tests on both runtimes; absorb deflate (codecs/Deflate + new compressor) and png (image/png + new encoder)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
commit
ccb06775c5
226 files changed
+8409
No files matched your search
@@ -0,0 +1,98 @@
|
||||
--!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: the range is clamped.
|
||||
--
|
||||
-- Crc32.of(buf, offset?, length?, crc?) -> u32 (pass a previous result as crc to continue it)
|
||||
-- Crc32.string(s, crc?) -> u32
|
||||
local band, bxor, rshift = bit32.band, bit32.bxor, bit32.rshift
|
||||
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
|
||||
|
||||
-- CRC-32 of buf[off, off + len), continuing crc (default 0 = a new CRC). Values are finished CRCs (the inversion
|
||||
-- happens inside), so crc(b, crc(a)) == the CRC of a .. b.
|
||||
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
|
||||
|
||||
-- clamp (offset, length) to the buffer
|
||||
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
|
||||
|
||||
-- raw(buf, off, len, crc?): no clamping, for callers that already checked the range (hot paths)
|
||||
Crc32.raw = crc32
|
||||
|
||||
return Crc32
|
||||
Reference in new issue
Block a user