-
Notifications
You must be signed in to change notification settings - Fork 4
/
Crc32.cpp
61 lines (49 loc) · 1.45 KB
/
Crc32.cpp
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
#include "Crc32.h"
#include "CrcParameters.h"
#include <iostream>
#include <iomanip>
#include <cstdint>
#include <array>
//*****************************************************************************
void Crc32::DisplayTable() const
{
int index = 0;
for (int i = 0; i < 16; ++i)
{
for (int j = 0; j < 16; ++j)
{
std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8) << crcTable[index++] << ", ";
}
std::cout << "\n";
}
}
//*****************************************************************************
void Crc32::GenerateTable(uint32_t polynomial, bool reflectIn, bool reflectOut)
{
for (int byte = 0; byte < 256; ++byte)
{
uint32_t crc = (reflectIn ? (Reverse(uint32_t(byte)) >> 24) : byte);
for (int bit = 32; bit > 0; --bit)
{
if (crc & 0x80000000)
{
crc = (crc << 1) ^ polynomial;
}
else
{
crc <<= 1;
}
}
crcTable[byte] = (reflectOut ? Reverse(crc) : crc);
}
}
//*****************************************************************************
uint32_t Crc32::Reverse(uint32_t value) const
{
value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1);
value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2);
value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4);
value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
value = (value >> 16) | (value << 16);
return value;
}