Created
April 13, 2011 22:52
-
-
Save kovrov/918601 to your computer and use it in GitHub Desktop.
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
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