forked from OpenVPN/openvpn3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexstr.hpp
248 lines (222 loc) · 5.86 KB
/
hexstr.hpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Technologies, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// A collection of functions for rendering and parsing hexadecimal strings
#ifndef OPENVPN_COMMON_HEXSTR_H
#define OPENVPN_COMMON_HEXSTR_H
#include <string>
#include <iomanip>
#include <sstream>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/string.hpp>
namespace openvpn {
inline char render_hex_char(const int c, const bool caps=false)
{
if (c < 10)
return '0' + c;
else if (c < 16)
return (caps ? 'A' : 'a') - 10 + c;
else
return '?';
}
inline int parse_hex_char(const char c)
{
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
else
return -1;
}
class RenderHexByte
{
public:
RenderHexByte(const unsigned char byte, const bool caps=false)
{
c[0] = render_hex_char(byte >> 4, caps);
c[1] = render_hex_char(byte & 0x0F, caps);
}
char char1() const { return c[0]; }
char char2() const { return c[1]; }
const char *str2() const { return c; } // Note: length=2, NOT null terminated
private:
char c[2];
};
inline std::string render_hex(const unsigned char *data, size_t size, const bool caps=false)
{
if (!data)
return "NULL";
std::string ret;
ret.reserve(size*2+1);
while (size--)
{
const RenderHexByte b(*data++, caps);
ret += b.char1();
ret += b.char2();
}
return ret;
}
inline std::string render_hex_sep(const unsigned char *data, size_t size, const char sep, const bool caps=false)
{
if (!data)
return "NULL";
std::string ret;
ret.reserve(size*3);
bool prsep = false;
while (size--)
{
if (prsep)
ret += sep;
const RenderHexByte b(*data++, caps);
ret += b.char1();
ret += b.char2();
prsep = true;
}
return ret;
}
template <typename V>
inline std::string render_hex_generic(const V& data, const bool caps=false)
{
std::string ret;
ret.reserve(data.size()*2+1);
for (size_t i = 0; i < data.size(); ++i)
{
const RenderHexByte b(data[i], caps);
ret += b.char1();
ret += b.char2();
}
return ret;
}
inline std::string dump_hex(const unsigned char *data, size_t size)
{
if (!data)
return "NULL\n";
const unsigned int mask = 0x0F; // N bytes per line - 1
std::ostringstream os;
os << std::hex;
std::string chars;
size_t i;
for (i = 0; i < size; ++i)
{
if (!(i & mask))
{
if (i)
{
os << " " << chars << std::endl;
chars.clear();
}
os << std::setfill(' ') << std::setw(8) << i << ":";
}
const unsigned char c = data[i];
os << ' ' << std::setfill('0') << std::setw(2) << (unsigned int)c;
if (string::is_printable(c))
chars += c;
else
chars += '.';
}
if (i)
os << string::spaces(2 + (((i-1) & mask) ^ mask) * 3) << chars << std::endl;
return os.str();
}
inline std::string dump_hex(const std::string& str)
{
return dump_hex((const unsigned char *)str.c_str(), str.length());
}
template <typename V>
inline std::string dump_hex(const V& data)
{
return dump_hex(data.c_data(), data.size());
}
OPENVPN_SIMPLE_EXCEPTION(parse_hex_error);
template <typename V>
inline void parse_hex(V& dest, const std::string& str)
{
const int len = int(str.length());
int i;
for (i = 0; i <= len - 2; i += 2)
{
const int high = parse_hex_char(str[i]);
const int low = parse_hex_char(str[i+1]);
if (high == -1 || low == -1)
throw parse_hex_error();
dest.push_back((high<<4) + low);
}
if (i != len)
throw parse_hex_error(); // straggler char
}
// note -- currently doesn't detect overflow
template <typename T>
inline bool parse_hex_number(const char *str, T& retval)
{
if (!str[0])
return false; // empty string
size_t i = 0;
T ret = T(0);
while (true)
{
const char c = str[i++];
const int hd = parse_hex_char(c);
if (hd >= 0)
{
ret *= T(16);
ret += T(hd);
}
else if (!c)
{
retval = ret;
return true;
}
else
return false; // non-hex-digit
}
}
template <typename T>
inline bool parse_hex_number(const std::string& str, T& retval)
{
return parse_hex_number(str.c_str(), retval);
}
template <typename T>
inline T parse_hex_number(const std::string& str)
{
T ret;
if (!parse_hex_number<T>(str.c_str(), ret))
throw parse_hex_error();
return ret;
}
template <typename T>
std::string render_hex_number(T value, const bool caps=false)
{
unsigned char buf[sizeof(T)];
for (size_t i = sizeof(T); i --> 0 ;)
{
buf[i] = value & 0xFF;
value >>= 8;
}
return render_hex(buf, sizeof(T), caps);
}
std::string render_hex_number(unsigned char uc, const bool caps=false)
{
RenderHexByte b(uc, caps);
return std::string(b.str2(), 2);
}
} // namespace openvpn
#endif // OPENVPN_COMMON_HEXSTR_H