forked from jancarlsson/snarkfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexUtil.cpp
166 lines (131 loc) · 3.63 KB
/
HexUtil.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "HexUtil.hpp"
using namespace std;
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// convert from hexadecimal ASCII to binary
//
uint8_t asciiHexToNibble(const char c) {
switch (c) {
case ('0') : return 0;
case ('1') : return 1;
case ('2') : return 2;
case ('3') : return 3;
case ('4') : return 4;
case ('5') : return 5;
case ('6') : return 6;
case ('7') : return 7;
case ('8') : return 8;
case ('9') : return 9;
case ('a') :
case ('A') :
return 10;
case ('b') :
case ('B') :
return 11;
case ('c') :
case ('C') :
return 12;
case ('d') :
case ('D') :
return 13;
case ('e') :
case ('E') :
return 14;
case ('f') :
case ('F') :
return 15;
}
return 0xff;
}
uint8_t asciiHexToOctet(const char high, const char low, bool& status) {
const uint8_t
highNibble = asciiHexToNibble(high),
lowNibble = asciiHexToNibble(low);
if (0xff == highNibble || 0xff == lowNibble)
status = false;
return (highNibble << 4) | lowNibble;
}
////////////////////////////////////////////////////////////////////////////////
// print as hex digits
//
PrintHex::PrintHex(ostream& out, const bool trailingSpace)
: m_trailingSpace(trailingSpace),
m_nibbles{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'},
m_out(out)
{}
void PrintHex::pushOctet(const uint8_t a) {
m_out << m_nibbles[a >> CHAR_BIT / 2]
<< m_nibbles[a & 0xf];
if (m_trailingSpace) m_out << ' ';
}
////////////////////////////////////////////////////////////////////////////////
// print as text characters
//
PrintText::PrintText(ostream& out)
: m_out(out)
{}
void PrintText::pushOctet(const uint8_t a) {
if (isprint(a) && !isspace(a))
m_out << a;
else
m_out << '.';
}
////////////////////////////////////////////////////////////////////////////////
// print messages in hexdump format
//
HexDumper::HexDumper(ostream& out)
: m_hex(out, true),
m_text(out),
m_out(out)
{}
void HexDumper::print(const vector<uint8_t>& msg) {
const size_t octetsPerLine = 16;
size_t lineIdx = 0;
while (lineIdx < msg.size()) {
// index to beginning of line
m_out << lineIdx << "\t";
// hex numbers
size_t i = lineIdx;
while (i < msg.size() && (i - lineIdx) < octetsPerLine) {
m_hex.push8(msg[i]);
if (7 == i - lineIdx) {
m_out << ' ';
}
++i;
}
// final line may need more whitespace to make columns line up
if (i >= msg.size()) {
while (i - lineIdx < octetsPerLine) {
m_out << " "; // three spaces for uint8_t in hex format
if (7 == i - lineIdx) {
m_out << ' ';
}
++i;
}
}
// start of ASCII
m_out << " |";
// ASCII text
i = lineIdx;
while (i < msg.size() && (i - lineIdx) < octetsPerLine) {
m_text.push8(msg[i]);
++i;
}
// final line may need more whitespace to make columns line up
if (i >= msg.size()) {
while (i - lineIdx < octetsPerLine) {
m_out << ' ';
++i;
}
}
// end of ASCII
m_out << "|" << endl;
// next line
lineIdx += octetsPerLine;
}
}
void HexDumper::print(const DataBufferStream& a) {
print(a.data());
}
} // namespace snarkfront