/
NrpeCrc.cs
63 lines (54 loc) · 1.92 KB
/
NrpeCrc.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
using System;
namespace FloodSensor
{
/// <summary>
/// Ported from https://github.com/KristianLyng/nrpe/blob/master/src/utils.c
/// I am not sure if this was strictly necessary, but then I could not seem to get Utility.ComputeCRC
/// (http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k%28Microsoft.SPOT.Hardware.Utility.ComputeCRC%29;k%28TargetFrameworkMoniker-.NETMicroFramework)
/// to return the same result as this function, no matter what seed I tried with it.
/// </summary>
class NrpeCrc
{
private const int CrcTableLength = 256;
static private readonly UInt32[] Crc32Table = new UInt32[CrcTableLength];
public NrpeCrc()
{
generateCrc32Table();
}
// Build the crc table - must be called before calculating the crc value
private void generateCrc32Table()
{
const uint poly = 0xEDB88320;
for (int i = 0; i < 256; i++)
{
var crc = (UInt32)i;
for (int j = 8; j > 0; j--)
{
if ((crc & (UInt32)1) > 0)
{
crc = (crc >> 1) ^ poly;
}
else
{
crc >>= 1;
}
}
Crc32Table[i] = crc;
}
}
/// <summary>
/// Calculates the CRC 32 value for a buffer
/// </summary>
public UInt32 CalculateCrc32(byte[] buffer, int bufferSize)
{
int currentIndex;
uint crc = 0xFFFFFFFF;
for (currentIndex = 0; currentIndex < bufferSize; currentIndex++)
{
int thisChar = buffer[currentIndex];
crc = ((crc >> 8) & 0x00FFFFFF) ^ Crc32Table[(crc ^ thisChar) & 0xFF];
}
return (crc ^ 0xFFFFFFFF);
}
}
}