2020-01-23 00:21:03 +00:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
|
|
|
|
namespace BizHawk.Common
|
2011-01-11 02:55:51 +00:00
|
|
|
|
{
|
2017-04-26 13:44:52 +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
|
2020-01-21 19:49:18 +00:00
|
|
|
|
// TODO - why am I here? put me alongside hash_md5 and such
|
2011-08-14 23:13:31 +00:00
|
|
|
|
public static class CRC32
|
|
|
|
|
{
|
2011-01-11 02:55:51 +00:00
|
|
|
|
// Lookup table for speed.
|
2017-04-26 13:44:52 +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()
|
|
|
|
|
{
|
2017-04-26 13:44:52 +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
|
|
|
|
|
2017-04-26 13:44:52 +00:00
|
|
|
|
Crc32Table[i] = crc;
|
2011-01-17 07:41:01 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2017-04-26 13:44:52 +00:00
|
|
|
|
result = (result >> 8) ^ Crc32Table[b ^ (result & 0xFF)];
|
2017-04-24 12:41:55 +00:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|