-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoshash.cpp
More file actions
54 lines (43 loc) · 1.25 KB
/
Copy pathoshash.cpp
File metadata and controls
54 lines (43 loc) · 1.25 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
/*
* OpenSubtitles Hash (OSHash) - C++ implementation.
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdint>
#include <algorithm>
#define CHUNK_SIZE 65536
uint64_t compute_hash(std::ifstream &f) {
uint64_t hash, fsize;
f.seekg(0, std::ios::end);
fsize = f.tellg();
f.seekg(0, std::ios::beg);
hash = fsize;
uint64_t tmp = 0;
for (uint64_t i = 0; i < CHUNK_SIZE / sizeof(tmp) && f.read(reinterpret_cast<char*>(&tmp), sizeof(tmp)); i++) {
hash += tmp;
tmp = 0;
}
f.seekg(std::max<int64_t>(0, fsize - CHUNK_SIZE), std::ios::beg);
tmp = 0;
for (uint64_t i = 0; i < CHUNK_SIZE / sizeof(tmp) && f.read(reinterpret_cast<char*>(&tmp), sizeof(tmp)); i++) {
hash += tmp;
tmp = 0;
}
return hash;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <file>" << std::endl;
return 1;
}
std::ifstream f(argv[1], std::ios::in | std::ios::binary);
if (!f.is_open()) {
std::cerr << "Error opening file" << std::endl;
return 1;
}
uint64_t myhash = compute_hash(f);
std::cout << std::setw(16) << std::setfill('0') << std::hex << myhash << std::endl;
f.close();
return 0;
}