Skip to content

Instantly share code, notes, and snippets.

@jaames
Last active September 17, 2022 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaames/3ac7c82a59967d28c73947f6d4ecc2a4 to your computer and use it in GitHub Desktop.
Save jaames/3ac7c82a59967d28c73947f6d4ecc2a4 to your computer and use it in GitHub Desktop.
crc32 function for playdate lua. only works on strings.
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