-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcrc24.cpp
63 lines (51 loc) · 1.2 KB
/
crc24.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
60
61
62
#include "crc24.hpp"
namespace {
const std::uint32_t REDUCE_POLY = 0x1864CFB;
const std::uint32_t INIT_VALUE = 0xB704CE;
struct table {
std::uint32_t arr[256];
table();
};
table::table()
{
for (std::uint32_t bite=0; bite<256; ++bite) {
auto temp = bite << 16;
for (int k=0; k<8; ++k) {
temp = (temp << 1) ^ ((-(temp >> 23)) & REDUCE_POLY);
}
arr[bite] = temp;
}
}
table const& get_table()
{
static const table t;
return t;
}
inline std::uint32_t process_byte_impl(std::uint32_t state,
unsigned char bite,
table const& t)
{
return (state << 8) ^ t.arr[((state >> 16) ^ bite) & 0xFF];
}
} // namespace
crc24hasher::crc24hasher() : state(INIT_VALUE) {}
void crc24hasher::process_byte(unsigned char bite)
{
auto& tab = get_table();
state = process_byte_impl(state, bite, tab) & 0xFFFFFF;
}
void crc24hasher::process_block(slice<const unsigned char> data)
{
if (!data) return;
auto& tab = get_table();
auto temp = this->state;
do {
unsigned bite = data.shift();
temp = process_byte_impl(temp, bite, tab);
} while (data);
this->state = temp & 0xFFFFFF;
}
std::uint32_t crc24hasher::finish() const
{
return state;
}