-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
91 lines (77 loc) · 2.54 KB
/
main.cpp
File metadata and controls
91 lines (77 loc) · 2.54 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
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include <boost/program_options.hpp>
// consider using this implementation
// https://github.com/noloader/SHA-Intrinsics
#include "DBXHash.h"
namespace po = boost::program_options;
void po_keyless(po::basic_parsed_options<char>& opts, std::vector<string>& keyless) {
for (int j=0; j<opts.options.size(); j++) {
po::basic_option<char> a = opts.options.at(j);
if (a.string_key == "") {
for (int i=0; i<a.value.size(); i++) {
keyless.push_back(a.value[i]);
}
}
}
}
int main(int argc, char** argv) {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("threads", po::value<string>(), "'all', 'half', or specify the number of threads to use");
po::variables_map vm;
po::basic_parsed_options<char> x = po::parse_command_line(argc, argv, desc);
po::store(x, vm);
po::notify(vm);
std::vector<string> keyless;
po_keyless(x, keyless);
if (vm.count("help")) {
cout << desc << endl;
return 0;
}
// file list
bool read_from_stdin = false;
for (auto& v : keyless) {
if (v == "-") {
read_from_stdin = true;
break;
}
}
if (read_from_stdin and keyless.size() > 1) {
cerr << "reading from stdin must be the only file argument if it is present" << endl;
return 1;
}
if (keyless.empty()) read_from_stdin = true;
// number of threads to use
int all = (int) boost::thread::hardware_concurrency();
int threads = all;
if (vm.count("threads")) {
string arg_threads = vm["threads"].as<string>();
if (arg_threads == "all") threads = all;
else if (arg_threads == "half") threads = all/2;
else {
int amount = boost::lexical_cast<int>(arg_threads);
threads = std::min(std::max(amount, 1), all);
}
}
byte hash[DIGEST_SIZE];
if (read_from_stdin) {
DBXHash(threads).process(std::cin, hash);
cout << hexify(hash) << " -" << endl;
} else {
for (string& path: keyless) {
fs::ifstream file((fs::path(path)));
if (! file.is_open()) {
cerr << "failed to open file" << endl;
return 2;
}
DBXHash(threads).process(file, hash);
cout << hexify(hash) << " " << path << endl;
file.close();
}
}
return 0;
}