-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashworker.cpp
196 lines (175 loc) · 5.3 KB
/
hashworker.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "hashworker.h"
#include "settings.h"
#include "customqtreewidgetitem.h"
#include <QDebug>
#include <QCryptographicHash>
#include <QFile>
#include <QThread>
#include <QElapsedTimer>
#include <QtZlib/zlib.h>
HashWorker::HashWorker()
= default;
HashWorker::~HashWorker()
= default;
void HashWorker::doWork()
{
qDebug() << "HashWorker::Started";
switch (hashType) {
case 0:
doMD5SHA1(QCryptographicHash::Md5);
break;
case 1:
doMD5SHA1(QCryptographicHash::Sha1);
break;
case 2:
doCRC32();
break;
default:
return;
}
emit finished();
}
void HashWorker::doMD5SHA1(QCryptographicHash::Algorithm algo)
{
QCryptographicHash hasher(algo);
int bufferSize = Settings::bufferSize();
QByteArray buffer;
qint64 fileSize;
qint64 totalBytesRead = 0;
int hashLength = algo == QCryptographicHash::Md5 ? 32 : 40;
qint64 *tSize = verifyMode ? &fileSize : &totalSize;
for(int i = 0; i < tree->topLevelItemCount(); i++)
{
if(selected)
{
if(!tree->topLevelItem(i)->isSelected()) continue;
}
else
{
if(tree->topLevelItem(i)->text(3).length() == hashLength)
{
totalBytesRead += static_cast<CustomQTreeWidgetItem*>(tree->topLevelItem(i))->size;
continue;
}
}
emit next(i);
QFile file(verifyMode ? rootPath + tree->topLevelItem(i)->text(0) : tree->topLevelItem(i)->text(0));
if(!file.exists())
{
emit error("File doesn't exist", i);
continue;
}
if(!file.open(QFile::ReadOnly))
{
emit error("Access denied", i);
continue;
}
fileSize = file.size();
if(verifyMode)
totalBytesRead = 0;
else if(auto *item = static_cast<CustomQTreeWidgetItem*>(tree->topLevelItem(i)); fileSize != item->size)
{
qDebug() << "File size mismatch!";
qint64 diff = fileSize - item->size;
item->size = fileSize;
emit newSize(diff, i);
totalSize += diff;
}
bool readError = false;
qint64 totalBytesReadAfter = totalBytesRead + fileSize;
while(!file.atEnd()){
if(thread()->isInterruptionRequested())
return;
buffer = file.read(bufferSize);
if(file.error())
{
readError = true;
emit error("Access denied", i);
break;
}
hasher.addData(buffer);
totalBytesRead += buffer.length();
emit progress((totalBytesRead * 100 / *tSize), i);
}
file.close();
if(readError) {
if(!verifyMode)
totalBytesRead = totalBytesReadAfter;
continue;
}
emit hashReady(i, hasher.result().toHex());
hasher.reset();
}
}
void HashWorker::doCRC32()
{
QByteArray buffer;
qint64 fileSize;
qint64 totalBytesRead = 0;
int bufferSize = Settings::bufferSize();
int hashLength = 8;
qint64 *tSize = verifyMode ? &fileSize : &totalSize;
for(int i = 0; i < tree->topLevelItemCount(); i++)
{
if(selected)
{
if(!tree->topLevelItem(i)->isSelected()) continue;
}
else
{
if(tree->topLevelItem(i)->text(3).length() == hashLength)
{
totalBytesRead += static_cast<CustomQTreeWidgetItem*>(tree->topLevelItem(i))->size;
continue;
}
}
emit next(i);
QFile file(verifyMode ? rootPath + tree->topLevelItem(i)->text(0) : tree->topLevelItem(i)->text(0));
if(!file.exists())
{
emit error("File doesn't exist", i);
continue;
}
if(!file.open(QFile::ReadOnly))
{
emit error("Access denied", i);
continue;
}
fileSize = file.size();
if(verifyMode)
totalBytesRead = 0;
else if(auto *item = static_cast<CustomQTreeWidgetItem*>(tree->topLevelItem(i)); fileSize != item->size)
{
qDebug() << "File size mismatch!";
qint64 diff = fileSize - item->size;
item->size = fileSize;
emit newSize(diff, i);
totalSize += diff;
}
unsigned long crc = crc32(0L, nullptr, 0);
bool readError = false;
qint64 totalBytesReadAfter = totalBytesRead + fileSize;
while(!file.atEnd())
{
if(thread()->isInterruptionRequested())
return;
buffer = file.read(bufferSize);
if(file.error())
{
readError = true;
emit error("Access denied", i);
break;
}
crc = crc32(crc, (unsigned char*)buffer.data(), z_uInt(buffer.length()));
totalBytesRead += buffer.length();
emit progress((totalBytesRead * 100 / *tSize), i);
}
file.close();
if(readError) {
if(!verifyMode)
totalBytesRead = totalBytesReadAfter;
continue;
}
emit hashReady(i, QByteArray::number(qulonglong(crc),16).rightJustified(8, '0'));
}
}