2017-04-14 17:28: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
|
2013-11-04 01:06:36 +00:00
|
|
|
|
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
|
2011-08-14 23:13:31 +00:00
|
|
|
|
public static class CRC32
|
|
|
|
|
{
|
2011-01-11 02:55:51 +00:00
|
|
|
|
// Lookup table for speed.
|
2013-11-04 03:12:50 +00:00
|
|
|
|
private static readonly uint[] CRC32Table;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
|
2011-08-14 23:13:31 +00:00
|
|
|
|
static CRC32()
|
|
|
|
|
{
|
2011-01-17 07:41:01 +00:00
|
|
|
|
CRC32Table = new uint[256];
|
2011-08-14 23:13:31 +00:00
|
|
|
|
for (uint i = 0; i < 256; ++i)
|
|
|
|
|
{
|
2011-01-17 07:41:01 +00:00
|
|
|
|
uint crc = i;
|
2011-08-14 23:13:31 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2011-08-14 23:13:31 +00:00
|
|
|
|
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
|
|
|
|
}
|
2017-04-14 17:28:23 +00:00
|
|
|
|
|
2011-01-17 07:41:01 +00:00
|
|
|
|
CRC32Table[i] = crc;
|
|
|
|
|
}
|
2011-01-11 02:55:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-14 23:13:31 +00:00
|
|
|
|
public static int Calculate(byte[] data)
|
|
|
|
|
{
|
2017-04-24 12:41:55 +00:00
|
|
|
|
uint result = 0xFFFFFFFF;
|
2011-08-14 23:13:31 +00:00
|
|
|
|
foreach (var b in data)
|
2017-04-24 12:41:55 +00:00
|
|
|
|
{
|
|
|
|
|
result = (result >> 8) ^ CRC32Table[b ^ (result & 0xFF)];
|
|
|
|
|
}
|
2017-04-14 17:28:23 +00:00
|
|
|
|
|
2017-04-24 12:41:55 +00:00
|
|
|
|
return (int)~result;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|