Skip to content

Instantly share code, notes, and snippets.

@kovrov
Created April 13, 2011 22:52
Show Gist options
  • Save kovrov/918601 to your computer and use it in GitHub Desktop.
Save kovrov/918601 to your computer and use it in GitHub Desktop.
immutable(uint[256]) crc32_lookup_table =
{
uint[] table;
foreach (ref i; 0 .. 256)
{
uint crc = i;
foreach (_; 0 .. 8)
crc = crc & 1 ? (crc >> 1) ^ 0xEDB88320 : crc >> 1;
table ~= crc;
}
return table[];
}();
pure uint crc32(T)(T[] packet) if (T.sizeof == 1)
{
uint crc = 0xFFFFFFFF;
foreach (ref const T element; packet)
crc = crc32_lookup_table[(crc ^ element) & 0xFF] ^ (crc >> 8);
return ~crc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment