-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc32.cpp
executable file
·72 lines (66 loc) · 1.45 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
#include "crc32.h"
#include <QtGui>
#include "enumTypes.h"
void Crc32::crc_cycle(char * buf, size_t cnt)
{
crc32 = ~crc32;
for (size_t i = 0; i < cnt; i++)
{
crc32 = crc_table[(crc32^buf[i]) & 0xff] ^ (crc32 >> 8);
}
crc32 = ~crc32;
}
void Crc32::crc_init()
{
unsigned long t;
for (int i = 0; i < 256; i++)
{
t = i;
for (int j = 8; j>0; j--)
{
if (t & 1)
t = (t >> 1) ^ 0xedb88320;
else
t >>= 1;
}
crc_table[i] = t;
}
}
QString Crc32::getHashString()
{
QString hash;
hash.sprintf("%08lX", crc32);
return hash;
}
Crc32::Crc32(): crc32(0)
{
crc_init();
}
int Crc32::calculateFile(QString path)
{
QFile file(path);
if(file.open(QIODevice::ReadOnly))
{
QDataStream in(&file);
int ok;
char* buf = new char[4096];
while(!in.atEnd())
{
ok = in.readRawData(buf, 4096);
if(in.status() == QDataStream::Ok)
{
crc_cycle(buf, ok);
qApp->processEvents();
}
else break;
}
delete [] buf;
file.close();
if(in.status() == QDataStream::Ok)
return ErrorType::noError;
else
return ErrorType::corruptData;
}
else
return ErrorType::notOpened;
}