Last active
September 17, 2022 14:32
-
-
Save jaames/3ac7c82a59967d28c73947f6d4ecc2a4 to your computer and use it in GitHub Desktop.
crc32 function for playdate lua. only works on strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local CRC32_LUT = nil | |
local function crc32_init() | |
if not CRC32_LUT then | |
CRC32_LUT = table.create(255, 0) | |
local rem | |
for i = 0, 255 do | |
rem = i | |
for j = 1, 8 do | |
if (rem & 1 == 1) then | |
rem = rem >> 1 | |
rem = rem ~ 0xEDB88320 | |
else | |
rem = rem >> 1 | |
end | |
end | |
CRC32_LUT[i] = rem | |
end | |
end | |
end | |
function crc32(str) | |
crc32_init() | |
local size = #str | |
local crc = 0xFFFFFFFF | |
local c | |
for x = 1, size do | |
c = str:byte(x) | |
crc = (crc >> 8) ~ CRC32_LUT[(crc & 0xFF) ~ c] | |
end | |
return crc ~ 0xFFFFFFFF | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment