-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
76 lines (63 loc) · 1.31 KB
/
utils.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
#include <sstream>
#include <iomanip>
static const char* digits = "0123456789ABCDEF";
std::string cleanify(const std::string& input) {
std::string out = input;
for(char& c : out) {
if (c < 32 || c > 127) {
c = '.';
}
}
return out;
}
std::string hexify(const std::string& input) {
std::string out;
for(uint8_t c : input) {
out += digits[c >> 4];
out += digits[c & 0xf];
}
return out;
}
// TODO: Where did I get this hexdump?
std::string hexdump(const std::string& input)
{
std::istringstream is(input);
std::stringstream ss;
size_t address = 0;
ss << std::hex << std::setfill('0');
while (is.good()) {
int nread;
char buf[16];
for (nread = 0; nread < 16 && is.get(buf[nread]); nread++);
if (nread == 0) {
break;
}
// Show the address
ss << std::setw(8) << address;
// Show the hex codes
for (int i = 0; i < 16; i++) {
if (i % 8 == 0) {
ss << ' ';
}
if (i < nread) {
ss << ' ' << std::setw(2) << ((unsigned)buf[i] & 0xff);
}
else {
ss << " ";
}
}
// Show printable characters
ss << " ";
for (int i = 0; i < nread; i++) {
if (buf[i] < 32) {
ss << '.';
}
else {
ss << buf[i];
}
}
ss << "\n";
address += 16;
}
return ss.str();
}