blob: 6623166dc35cf6b8eee9eb16bdd39600578959b6 (
plain)
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
|
#include "crc32.h"
namespace Multihash {
int* crc_table = 0;
void generate_table() {
int crc;
crc_table = new int[256];
for(int i = 0; i < 256; i++) {
crc = i << 24;
for(int j = 0; j < 8; j++) {
if(crc & 0x80000000) {
crc = (crc << 1) ^ 0x04c11db7;
} else {
crc = crc << 1;
}
}
crc_table[i] = crc;
}
}
int reflect(int data, int bits) {
int x = 0;
for(int i = 0; i < bits; i++) {
x = x << 1;
x |= data & 1;
data = data >> 1;
}
return x;
}
CRC32::CRC32(Hash* n) : Hash(n) {
if(!crc_table) {
generate_table();
}
crc_ctx = 0;
}
void CRC32::hash_update(const char* data, int length) {
int crc = ~reflect(crc_ctx, 32);
for (int i = 0; i < length; i++) {
crc = (crc << 8) ^ crc_table[((crc >> 24) ^ reflect(data[i], 8)) & 0xff];
}
crc_ctx = ~reflect(crc, 32);
}
std::string CRC32::hash_digest() {
return Hex::hex(crc_ctx);
}
}
|