tlx
Toggle main menu visibility
Loading...
Searching...
No Matches
hexdump.cpp
Go to the documentation of this file.
1
/*******************************************************************************
2
* tlx/string/hexdump.cpp
3
*
4
* Part of tlx - http://panthema.net/tlx
5
*
6
* Copyright (C) 2007-2017 Timo Bingmann <tb@panthema.net>
7
*
8
* All rights reserved. Published under the Boost Software License, Version 1.0
9
******************************************************************************/
10
11
#include <
tlx/string/hexdump.hpp
>
12
13
#include <cstdint>
14
#include <sstream>
15
#include <stdexcept>
16
17
namespace
tlx
{
18
19
/******************************************************************************/
20
// Uppercase Hexdump Methods
21
22
std::string
hexdump
(
const
void
*
const
data,
size_t
size) {
23
const
unsigned
char
*
const
cdata =
24
static_cast<
const
unsigned
char
*
>
(data);
25
26
std::string out;
27
out.resize(size * 2);
28
29
static
const
char
xdigits[16] = {
30
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
31
'8'
,
'9'
,
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
32
};
33
34
std::string::iterator oi = out.begin();
35
for
(
const
unsigned
char
* si = cdata; si != cdata + size; ++si) {
36
*oi++ = xdigits[(*si & 0xF0) >> 4];
37
*oi++ = xdigits[(*si & 0x0F)];
38
}
39
40
return
out;
41
}
42
43
std::string
hexdump
(
const
std::string& str) {
44
return
hexdump
(str.data(), str.size());
45
}
46
47
std::string
hexdump
(
const
std::vector<char>& data) {
48
return
hexdump
(data.data(), data.size());
49
}
50
51
std::string
hexdump
(
const
std::vector<std::uint8_t>& data) {
52
return
hexdump
(data.data(), data.size());
53
}
54
55
std::string
hexdump_sourcecode
(
56
const
std::string& str,
const
std::string& var_name) {
57
58
std::ostringstream header;
59
header <<
"const std::uint8_t "
<< var_name <<
"["
<< str.size() <<
"] = {\n"
;
60
61
static
const
int
perline = 16;
62
63
std::string out = header.str();
64
out.reserve(out.size() + (str.size() * 5) - 1 + (str.size() / 16) + 4);
65
66
static
const
char
xdigits[16] = {
67
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
68
'8'
,
'9'
,
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
69
};
70
71
std::string::size_type ci = 0;
72
73
for
(std::string::const_iterator si = str.begin(); si != str.end();
74
++si, ++ci) {
75
76
out +=
"0x"
;
77
out += xdigits[(*si & 0xF0) >> 4];
78
out += xdigits[(*si & 0x0F)];
79
80
if
(ci + 1 < str.size()) {
81
out +=
','
;
82
83
if
(ci % perline == perline - 1)
84
out +=
'\n'
;
85
}
86
}
87
88
out +=
"\n};\n"
;
89
90
return
out;
91
}
92
93
/******************************************************************************/
94
// Lowercase Hexdump Methods
95
96
std::string
hexdump_lc
(
const
void
*
const
data,
size_t
size) {
97
const
unsigned
char
*
const
cdata =
98
static_cast<
const
unsigned
char
*
>
(data);
99
100
std::string out;
101
out.resize(size * 2);
102
103
static
const
char
xdigits[16] = {
104
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
105
'8'
,
'9'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
106
};
107
108
std::string::iterator oi = out.begin();
109
for
(
const
unsigned
char
* si = cdata; si != cdata + size; ++si) {
110
*oi++ = xdigits[(*si & 0xF0) >> 4];
111
*oi++ = xdigits[(*si & 0x0F)];
112
}
113
114
return
out;
115
}
116
117
std::string
hexdump_lc
(
const
std::string& str) {
118
return
hexdump_lc
(str.data(), str.size());
119
}
120
121
std::string
hexdump_lc
(
const
std::vector<char>& data) {
122
return
hexdump_lc
(data.data(), data.size());
123
}
124
125
std::string
hexdump_lc
(
const
std::vector<std::uint8_t>& data) {
126
return
hexdump_lc
(data.data(), data.size());
127
}
128
129
/******************************************************************************/
130
// Parser for Hex Digit Sequence
131
132
std::string
parse_hexdump
(
const
std::string& str) {
133
std::string out;
134
135
for
(std::string::const_iterator si = str.begin(); si != str.end(); ++si) {
136
137
unsigned
char
c = 0;
138
139
// read first character of pair
140
switch
(*si) {
141
case
'0'
: c |= 0x00;
142
break
;
143
case
'1'
: c |= 0x10;
144
break
;
145
case
'2'
: c |= 0x20;
146
break
;
147
case
'3'
: c |= 0x30;
148
break
;
149
case
'4'
: c |= 0x40;
150
break
;
151
case
'5'
: c |= 0x50;
152
break
;
153
case
'6'
: c |= 0x60;
154
break
;
155
case
'7'
: c |= 0x70;
156
break
;
157
case
'8'
: c |= 0x80;
158
break
;
159
case
'9'
: c |= 0x90;
160
break
;
161
case
'A'
:
162
case
'a'
: c |= 0xA0;
163
break
;
164
case
'B'
:
165
case
'b'
: c |= 0xB0;
166
break
;
167
case
'C'
:
168
case
'c'
: c |= 0xC0;
169
break
;
170
case
'D'
:
171
case
'd'
: c |= 0xD0;
172
break
;
173
case
'E'
:
174
case
'e'
: c |= 0xE0;
175
break
;
176
case
'F'
:
177
case
'f'
: c |= 0xF0;
178
break
;
179
default
:
throw
std::runtime_error(
"Invalid string for hex conversion"
);
180
}
181
182
++si;
183
if
(si == str.end())
184
throw
std::runtime_error(
"Invalid string for hex conversion"
);
185
186
// read second character of pair
187
switch
(*si) {
188
case
'0'
: c |= 0x00;
189
break
;
190
case
'1'
: c |= 0x01;
191
break
;
192
case
'2'
: c |= 0x02;
193
break
;
194
case
'3'
: c |= 0x03;
195
break
;
196
case
'4'
: c |= 0x04;
197
break
;
198
case
'5'
: c |= 0x05;
199
break
;
200
case
'6'
: c |= 0x06;
201
break
;
202
case
'7'
: c |= 0x07;
203
break
;
204
case
'8'
: c |= 0x08;
205
break
;
206
case
'9'
: c |= 0x09;
207
break
;
208
case
'A'
:
209
case
'a'
: c |= 0x0A;
210
break
;
211
case
'B'
:
212
case
'b'
: c |= 0x0B;
213
break
;
214
case
'C'
:
215
case
'c'
: c |= 0x0C;
216
break
;
217
case
'D'
:
218
case
'd'
: c |= 0x0D;
219
break
;
220
case
'E'
:
221
case
'e'
: c |= 0x0E;
222
break
;
223
case
'F'
:
224
case
'f'
: c |= 0x0F;
225
break
;
226
default
:
throw
std::runtime_error(
"Invalid string for hex conversion"
);
227
}
228
229
out +=
static_cast<
char
>
(c);
230
}
231
232
return
out;
233
}
234
235
}
// namespace tlx
236
237
/******************************************************************************/
tlx::hexdump_lc
std::string hexdump_lc(const void *const data, size_t size)
Dump a (binary) string as a sequence of lowercase hexadecimal pairs.
Definition
hexdump.cpp:96
tlx::parse_hexdump
std::string parse_hexdump(const std::string &str)
Read a string as a sequence of hexadecimal pairs.
Definition
hexdump.cpp:132
tlx::hexdump_sourcecode
std::string hexdump_sourcecode(const std::string &str, const std::string &var_name)
Dump a (binary) string into a C source code snippet.
Definition
hexdump.cpp:55
tlx::hexdump
std::string hexdump(const void *const data, size_t size)
Dump a (binary) string as a sequence of uppercase hexadecimal pairs.
Definition
hexdump.cpp:22
hexdump.hpp
tlx
Definition
exclusive_scan.hpp:17
tlx
string
hexdump.cpp
Generated on
for tlx by
1.17.0