-
Notifications
You must be signed in to change notification settings - Fork 9
/
OggCrc2.cs
47 lines (45 loc) · 1.41 KB
/
OggCrc2.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
namespace NSpeex
{
/// <summary>
/// Calculates the CRC checksum for Ogg packets.
/// Ogg uses the same generator polynomial as ethernet, although with an unreflected alg and an init/final of 0, not 0xffffffff.
/// </summary>
public class OggCrc2
{
private static int[] crc_lookup;
static OggCrc2()
{
crc_lookup = new int[256];
for (int i = 0; i < crc_lookup.Length; i++)
{
int r = i << 24;
for (int j = 0; j < 8; j++)
{
if ((r & 0x80000000) != 0)
{
/* The same as the ethernet generator polynomial, although we use an
unreflected alg and an init/final of 0, not 0xffffffff */
r = (r << 1) ^ 0x04c11db7;
}
else
{
r <<= 1;
}
}
crc_lookup[i] = (int)(r & 0xffffffff);
}
}
public static int checksum(int crc,
byte[] data,
int offset,
int length)
{
int end = offset + length;
for (; offset < end; offset++)
{
crc = (crc << 8) ^ crc_lookup[((crc >> 24) & 0xff) ^ (data[offset] & 0xff)];
}
return crc;
}
}
}