BizHawk/BizHawk.Emulation/Database/CRC32.cs

39 lines
804 B
C#
Raw Normal View History

2012-11-15 08:03:23 +00:00
//we could get a little list of crcs from here and make it clear which crc this class was for, and expose others
//http://www.ross.net/crc/download/crc_v3.txt
namespace BizHawk
2011-01-11 02:55:51 +00:00
{
public static class CRC32
{
2011-01-11 02:55:51 +00:00
// Lookup table for speed.
private static uint[] CRC32Table;
static CRC32()
{
2011-01-17 07:41:01 +00:00
CRC32Table = new uint[256];
for (uint i = 0; i < 256; ++i)
{
2011-01-17 07:41:01 +00:00
uint crc = i;
for (int j = 8; j > 0; --j)
{
2011-01-17 07:41:01 +00:00
if ((crc & 1) == 1)
crc = ((crc >> 1) ^ 0xEDB88320);
else
2011-01-17 07:41:01 +00:00
crc >>= 1;
2011-01-11 02:55:51 +00:00
}
2011-01-17 07:41:01 +00:00
CRC32Table[i] = crc;
}
2011-01-11 02:55:51 +00:00
}
public static int Calculate(byte[] data)
{
uint Result = 0xFFFFFFFF;
foreach (var b in data)
Result = (((Result) >> 8) ^ CRC32Table[b ^ ((Result) & 0xFF)]);
return (int) ~Result;
2011-01-11 02:55:51 +00:00
}
}
2011-01-11 02:55:51 +00:00
}