-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc32.cpp
108 lines (85 loc) · 1.75 KB
/
crc32.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
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
#include "crc32.h"
#include <QFile>
Crc32::Crc32(QStringList data) : data(data)
{
mutex.lock();
_id = this->data.at(0);
id = this->data.at(0);
nRow = this->data.at(1).toInt();
filepath = this->data.at(2);
mutex.unlock();
initTable();
}
Crc32::~Crc32()
{
}
void Crc32::initTable()
{
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;
}
}
void Crc32::calculateFromFile()
{
quint32 crc;
QFile file;
char buffer[16000];
int len, i;
crc = 0xFFFFFFFFUL;
file.setFileName(filepath);
if(!file.exists())
{
emit finished();
return;
}
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();
}
QString returnCRC32 = QString::number(crc ^ 0xFFFFFFFFUL, 16);
emit updateCRC32data(id, nRow, returnCRC32);
emit finished();
}
void Crc32::initInstance(int i)
{
instances[i] = 0xFFFFFFFFUL;
}
void Crc32::pushData(int i, 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;
}
}