-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRC32.cs
77 lines (65 loc) · 2.14 KB
/
CRC32.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
namespace CrcAppender
{
/// <summary>
/// Functions for CRC32 calculation
/// </summary>
/// <remarks>
/// The internals of this class originate from Crc32.NET, see https://github.com/force-net/Crc32.NET
/// </remarks>
public static class CRC32
{
private const uint Poly = 0xedb88320u;
private static readonly uint[] _table = new uint[16 * 256];
static CRC32()
{
var table = _table;
for (uint i = 0; i < 256; i++)
{
uint res = i;
for (int t = 0; t < 16; t++)
{
for (int k = 0; k < 8; k++) res = (res & 1) == 1 ? Poly ^ (res >> 1) : (res >> 1);
table[(t * 256) + i] = res;
}
}
}
/// <summary>
/// Calculate the CRC32 checksum for the given byte span
/// </summary>
/// <param name="buffer">Input data</param>
/// <returns>CRC32 checksum</returns>
public static uint Calculate(Span<byte> buffer)
{
uint crc = 0;
int length = buffer.Length, offset = 0;
uint crcLocal = uint.MaxValue ^ crc;
uint[] table = _table;
while (length >= 16)
{
var a = table[(3 * 256) + buffer[offset + 12]]
^ table[(2 * 256) + buffer[offset + 13]]
^ table[(1 * 256) + buffer[offset + 14]]
^ table[(0 * 256) + buffer[offset + 15]];
var b = table[(7 * 256) + buffer[offset + 8]]
^ table[(6 * 256) + buffer[offset + 9]]
^ table[(5 * 256) + buffer[offset + 10]]
^ table[(4 * 256) + buffer[offset + 11]];
var c = table[(11 * 256) + buffer[offset + 4]]
^ table[(10 * 256) + buffer[offset + 5]]
^ table[(9 * 256) + buffer[offset + 6]]
^ table[(8 * 256) + buffer[offset + 7]];
var d = table[(15 * 256) + ((byte)crcLocal ^ buffer[offset])]
^ table[(14 * 256) + ((byte)(crcLocal >> 8) ^ buffer[offset + 1])]
^ table[(13 * 256) + ((byte)(crcLocal >> 16) ^ buffer[offset + 2])]
^ table[(12 * 256) + ((crcLocal >> 24) ^ buffer[offset + 3])];
crcLocal = d ^ c ^ b ^ a;
offset += 16;
length -= 16;
}
while (--length >= 0)
crcLocal = table[(byte)(crcLocal ^ buffer[offset++])] ^ crcLocal >> 8;
return crcLocal ^ uint.MaxValue;
}
}
}