-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrc32.cpp
More file actions
96 lines (81 loc) · 2.09 KB
/
Copy pathCrc32.cpp
File metadata and controls
96 lines (81 loc) · 2.09 KB
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
/*
This file is part of fsdump, a tool for dumping drives into image files.
Copyright (C) 2020 Simon Gander.
FSDump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
FSDump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with fsdump. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Crc32.h"
Crc32::Crc32(bool reflect, uint32_t poly)
{
unsigned int i;
uint32_t r;
unsigned int b;
m_reflect = reflect;
m_crc = 0;
if (reflect) {
poly = ((poly << 16) & 0xFFFF0000) | ((poly >> 16) & 0x0000FFFF);
poly = ((poly << 8) & 0xFF00FF00) | ((poly >> 8) & 0x00FF00FF);
poly = ((poly << 4) & 0xF0F0F0F0) | ((poly >> 4) & 0x0F0F0F0F);
poly = ((poly << 2) & 0xCCCCCCCC) | ((poly >> 2) & 0x33333333);
poly = ((poly << 1) & 0xAAAAAAAA) | ((poly >> 1) & 0x55555555);
for (i = 0; i < 256; i++) {
r = i;
for (b = 0; b < 8; b++) {
if (r & 1)
r = (r >> 1) ^ poly;
else
r = (r >> 1);
}
m_table[i] = r;
}
}
else {
for (i = 0; i < 256; i++) {
r = (i << 24);
for (b = 0; b < 8; b++) {
if (r & 0x80000000)
r = (r << 1) ^ poly;
else
r = (r << 1);
}
m_table[i] = r;
}
}
}
Crc32::~Crc32()
{
}
void Crc32::Calc(const uint8_t *data, size_t size)
{
size_t i;
for (i = 0; i < size; i++) {
if (m_reflect) {
CalcLE(data[i]);
}
else {
CalcBE(data[i]);
}
}
}
void Crc32::CalcLE(uint8_t b)
{
m_crc = m_table[b ^ (m_crc & 0xFF)] ^ (m_crc >> 8);
}
void Crc32::CalcBE(uint8_t b)
{
m_crc = m_table[b ^ ((m_crc >> 24) & 0xFF)] ^ (m_crc << 8);
}
uint32_t Crc32::GetDataCRC(const uint8_t *data, size_t size, uint32_t initialXor, uint32_t finalXor)
{
m_crc = initialXor;
Calc(data, size);
return m_crc ^ finalXor;
}