-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhm_lookup.cc
76 lines (64 loc) · 2.18 KB
/
hm_lookup.cc
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
/* HmSearch hash library - lookup tool
*
* Copyright 2014 Commons Machinery http://commonsmachinery.se/
* Distributed under an MIT license, please see LICENSE in the top dir.
*/
#include <stdio.h>
#include <iostream>
#include <memory>
#include "hmsearch.h"
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Usage: %s path [hexhash...]\n", argv[0]);
return 1;
}
const char *path = argv[1];
std::string error_msg;
std::auto_ptr<HmSearch> db(HmSearch::open(path, HmSearch::READONLY, &error_msg));
if (!db.get()) {
fprintf(stderr, "%s: error opening %s: %s\n", argv[0], path, error_msg.c_str());
return 1;
}
if (argc > 2) {
// Lookup hashes from command line
for (int i = 2; i < argc; i++) {
const char *hexhash = argv[i];
HmSearch::LookupResultList matches;
if (!db->lookup(HmSearch::parse_hexhash(hexhash), matches, -1, &error_msg)) {
fprintf(stderr, "%s: cannot lookup hash: %s (%s)\n",
argv[0], error_msg.c_str(), hexhash);
return 1;
}
for (HmSearch::LookupResultList::const_iterator i = matches.begin();
i != matches.end();
++i) {
std::cout << HmSearch::format_hexhash(i->hash) << " " << i->distance << std::endl;
}
}
}
else {
// Read hashes from stdin
std::string hexhash;
while (std::cin >> hexhash) {
HmSearch::LookupResultList matches;
if (!db->lookup(HmSearch::parse_hexhash(hexhash), matches, -1, &error_msg)) {
fprintf(stderr, "%s: cannot lookup hash: %s (%s)\n",
argv[0], error_msg.c_str(), hexhash.c_str());
return 1;
}
for (HmSearch::LookupResultList::const_iterator i = matches.begin();
i != matches.end();
++i) {
std::cout << HmSearch::format_hexhash(i->hash) << " " << i->distance << std::endl;
}
}
}
return 0;
}
/*
Local Variables:
c-file-style: "stroustrup"
indent-tabs-mode:nil
End:
*/