Repository navigation
Expand file tree
/
Copy pathcrc32.cpp
More file actions
80 lines (71 loc) · 2.27 KB
/
Copy pathcrc32.cpp
File metadata and controls
80 lines (71 loc) · 2.27 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
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <boost/crc.hpp>
#include <boost/regex.hpp>
using namespace std;
const string colorReset = "\x1b[39m",
colorGreen = "\x1b[32m",
colorRed = "\x1b[31m",
colorMagenta = "\x1b[35m";
unsigned long calculateCrc32(ifstream &inf) // {{{1
{
const size_t maxRead = 1048576;
char buffer[maxRead];
boost::crc_32_type chksum;
while (inf) {
inf.read(buffer, maxRead);
chksum.process_bytes(buffer, inf.gcount());
}
return chksum.checksum();
} // 1}}}
unsigned long getCrc32FromFilename(const string &file) // {{{1
{
const boost::regex crcRegex("([\\[\\(]+)([a-fA-F0-9]{8})([\\]\\)]+)");
boost::match_results<string::const_iterator> what;
if (boost::regex_search(file.begin(), file.end(), what, crcRegex)) {
unsigned long crc;
// 0: complete match, 1: first match, 2: second match, 3: third match
istringstream(what[2]) >> hex >> crc; // convert hex string into dec ulong
return crc;
}
return 0; // nothing found
} // 1}}}
int main(int argc, const char *argv[])
{
if (argc < 2) {
cout << argv[0] << " file [file2 [ ..." << endl;
return 1;
}
ifstream inf;
bool errorOccurred = false;
#pragma omp parallel for private(inf) shared(errorOccurred)
for (int arg = 1; arg < argc; arg++) {
inf.open(argv[arg], ios::binary);
if (inf) {
unsigned long calcCrc = calculateCrc32(inf),
wantedCrc = getCrc32FromFilename(argv[arg]);
string col;
if (calcCrc == wantedCrc) {
col = colorGreen;
} else if (wantedCrc == 0) { // no crc found
col = colorMagenta;
} else {
col = colorRed;
}
#pragma omp critical
{
cout << col << std::setw(8) << hex << calcCrc << colorReset << " " << argv[arg] << endl;
}
} else {
#pragma omp critical
{
errorOccurred = true;
cout << colorRed << " fail" << colorReset << " " << argv[arg] << endl;
}
}
inf.close();
}
return errorOccurred;
}