forked from ILightburn/KLM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqcrc32.cpp
73 lines (59 loc) · 1.3 KB
/
qcrc32.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
63
64
65
66
67
68
69
70
71
72
73
#include "qcrc32.h"
#include <QFile>
Crc32::Crc32()
{
quint32 crc;
// initialize CRC table
for (int i = 0; i < 256; i++)
{
crc = i;
for (int j = 0; j < 8; j++)
crc = crc & 1 ? (crc >> 1) ^ 0xEDB88320UL : crc >> 1;
crc_table[i] = crc;
}
}
quint32 Crc32::calculateFromFile(const QString& filename)
{
quint32 crc;
QFile file;
char buffer[16000];
uint64_t len, i;
crc = 0xFFFFFFFFUL;
file.setFileName(filename);
if (file.open(QIODevice::ReadOnly))
{
while (!file.atEnd())
{
len = file.read(buffer, 16000);
for (i = 0; i < len; i++)
crc = crc_table[(crc ^ buffer[i]) & 0xFF] ^ (crc >> 8);
}
file.close();
}
return crc ^ 0xFFFFFFFFUL;
}
void Crc32::initInstance(int i)
{
instances[i] = 0xFFFFFFFFUL;
}
void Crc32::pushData(int i, const char *data, int len)
{
quint32 crc = instances[i];
if (crc)
{
for (int j = 0; j < len; j++)
crc = crc_table[(crc ^ data[j]) & 0xFF] ^ (crc >> 8);
instances[i] = crc;
}
}
quint32 Crc32::releaseInstance(int i)
{
quint32 crc32 = instances[i];
if (crc32) {
instances.remove(i);
return crc32 ^ 0xFFFFFFFFUL;
}
else {
return 0;
}
}