BizHawk/BizHawk.Emulation.Common/Database/CRC32.cs

45 lines
959 B
C#
Raw Normal View History

// 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.Emulation.Common
2011-01-11 02:55:51 +00:00
{
2017-04-24 12:41:55 +00:00
// TODO - why am I here? put me alongside hash_md5 and such in a non-emulation-related class
public static class CRC32
{
2011-01-11 02:55:51 +00:00
// Lookup table for speed.
private static readonly uint[] CRC32Table;
2011-01-11 02:55:51 +00:00
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)
2017-04-24 12:41:55 +00:00
{
crc = (crc >> 1) ^ 0xEDB88320;
}
else
2017-04-24 12:41:55 +00:00
{
2011-01-17 07:41:01 +00:00
crc >>= 1;
2017-04-24 12:41:55 +00:00
}
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)
{
2017-04-24 12:41:55 +00:00
uint result = 0xFFFFFFFF;
foreach (var b in data)
2017-04-24 12:41:55 +00:00
{
result = (result >> 8) ^ CRC32Table[b ^ (result & 0xFF)];
}
2017-04-24 12:41:55 +00:00
return (int)~result;
2011-01-11 02:55:51 +00:00
}
}
}