-
Notifications
You must be signed in to change notification settings - Fork 31
/
schifra_crc.hpp
172 lines (138 loc) · 4.62 KB
/
schifra_crc.hpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
(**************************************************************************)
(* *)
(* Schifra *)
(* Reed-Solomon Error Correcting Code Library *)
(* *)
(* Release Version 0.0.1 *)
(* http://www.schifra.com *)
(* Copyright (c) 2000-2020 Arash Partow, All Rights Reserved. *)
(* *)
(* The Schifra Reed-Solomon error correcting code library and all its *)
(* components are supplied under the terms of the General Schifra License *)
(* agreement. The contents of the Schifra Reed-Solomon error correcting *)
(* code library and all its components may not be copied or disclosed *)
(* except in accordance with the terms of that agreement. *)
(* *)
(* URL: http://www.schifra.com/license.html *)
(* *)
(**************************************************************************)
*/
#ifndef INCLUDE_SCHIFRA_CRC_HPP
#define INCLUDE_SCHIFRA_CRC_HPP
#include <iostream>
#include <string>
namespace schifra
{
class crc32
{
public:
typedef std::size_t crc32_t;
crc32(const crc32_t& _key, const crc32_t& _state = 0x00)
: key(_key),
state(_state),
initial_state(_state)
{
initialize_crc32_table();
}
void reset()
{
state = initial_state;
}
void update_1byte(const unsigned char data)
{
state = (state >> 8) ^ table[data];
}
void update(const unsigned char data[], const std::size_t& count)
{
for (std::size_t i = 0; i < count; ++i)
{
update_1byte(data[i]);
}
}
void update(char data[], const std::size_t& count)
{
for (std::size_t i = 0; i < count; ++i)
{
update_1byte(static_cast<unsigned char>(data[i]));
}
}
void update(const std::string& data)
{
for (std::size_t i = 0; i < data.size(); ++i)
{
update_1byte(static_cast<unsigned char>(data[i]));
}
}
void update(const std::size_t& data)
{
update_1byte(static_cast<unsigned char>((data ) & 0xFF));
update_1byte(static_cast<unsigned char>((data >> 8) & 0xFF));
update_1byte(static_cast<unsigned char>((data >> 16) & 0xFF));
update_1byte(static_cast<unsigned char>((data >> 24) & 0xFF));
}
crc32_t crc()
{
return state;
}
private:
crc32& operator=(const crc32&);
void initialize_crc32_table()
{
for (std::size_t i = 0; i < 0xFF; ++i)
{
crc32_t reg = i;
for (int j = 0; j < 0x08; ++j)
{
reg = ((reg & 1) ? (reg >> 1) ^ key : reg >> 1);
}
table[i] = reg;
}
}
protected:
crc32_t key;
crc32_t state;
const crc32_t initial_state;
crc32_t table[256];
};
class schifra_crc : public crc32
{
public:
schifra_crc(const crc32_t _key)
: crc32(_key,0xAAAAAAAA)
{}
void update(const unsigned char& data)
{
state = ((state >> 8) ^ table[data]) ^ ((state << 8) ^ table[~data]);
}
void update(const unsigned char data[], const std::size_t& count)
{
for (std::size_t i = 0; i < count; ++i)
{
update_1byte(data[i]);
}
}
void update(const char data[], const std::size_t& count)
{
for (std::size_t i = 0; i < count; ++i)
{
update_1byte(static_cast<unsigned char>(data[i]));
}
}
void update(const std::string& data)
{
for (std::size_t i = 0; i < data.size(); ++i)
{
update_1byte(static_cast<unsigned char>(data[i]));
}
}
void update(const std::size_t& data)
{
update_1byte(static_cast<unsigned char>((data ) & 0xFF));
update_1byte(static_cast<unsigned char>((data >> 8) & 0xFF));
update_1byte(static_cast<unsigned char>((data >> 16) & 0xFF));
update_1byte(static_cast<unsigned char>((data >> 24) & 0xFF));
}
};
} // namespace schifra
#endif