1 /*******************************************************************************
2 * tlx/string/hexdump.hpp
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 #ifndef TLX_STRING_HEXDUMP_HEADER
12 #define TLX_STRING_HEXDUMP_HEADER
13
14 #include <cstdint>
15 #include <string>
16 #include <vector>
17
18 namespace tlx {
19
20 //! \addtogroup tlx_string
21 //! \{
22 //! \name Hexdump Methods
23 //! \{
24
25 /******************************************************************************/
26 // Uppercase Hexdump Methods
27
28 /*!
29 * Dump a (binary) string as a sequence of uppercase hexadecimal pairs.
30 *
31 * \param data binary data to output in hex
32 * \param size length of binary data
33 * \return string of hexadecimal pairs
34 */
35 std::string hexdump(const void* const data, size_t size);
36
37 /*!
38 * Dump a (binary) string as a sequence of uppercase hexadecimal pairs.
39 *
40 * \param str binary data to output in hex
41 * \return string of hexadecimal pairs
42 */
43 std::string hexdump(const std::string& str);
44
45 /*!
46 * Dump a (binary) item as a sequence of uppercase hexadecimal pairs.
47 *
48 * \param t binary data to output in hex
49 * \return string of hexadecimal pairs
50 */
51 template <typename Type>
hexdump_type(const Type & t)52 std::string hexdump_type(const Type& t) {
53 return hexdump(&t, sizeof(t));
54 }
55
56 /*!
57 * Dump a char vector as a sequence of uppercase hexadecimal pairs.
58 *
59 * \param data binary data to output in hex
60 * \return string of hexadecimal pairs
61 */
62 std::string hexdump(const std::vector<char>& data);
63
64 /*!
65 * Dump a uint8_t vector as a sequence of uppercase hexadecimal pairs.
66 *
67 * \param data binary data to output in hex
68 * \return string of hexadecimal pairs
69 */
70 std::string hexdump(const std::vector<uint8_t>& data);
71
72 /*!
73 * Dump a (binary) string into a C source code snippet. The snippet defines an
74 * array of const uint8_t* holding the data of the string.
75 *
76 * \param str string to output as C source array
77 * \param var_name name of the array variable in the outputted code snippet
78 * \return string holding C source snippet
79 */
80 std::string hexdump_sourcecode(
81 const std::string& str, const std::string& var_name = "name");
82
83 /******************************************************************************/
84 // Lowercase Hexdump Methods
85
86 /*!
87 * Dump a (binary) string as a sequence of lowercase hexadecimal pairs.
88 *
89 * \param data binary data to output in hex
90 * \param size length of binary data
91 * \return string of hexadecimal pairs
92 */
93 std::string hexdump_lc(const void* const data, size_t size);
94
95 /*!
96 * Dump a (binary) string as a sequence of lowercase hexadecimal pairs.
97 *
98 * \param str binary data to output in hex
99 * \return string of hexadecimal pairs
100 */
101 std::string hexdump_lc(const std::string& str);
102
103 /*!
104 * Dump a (binary) item as a sequence of lowercase hexadecimal pairs.
105 *
106 * \param t binary data to output in hex
107 * \return string of hexadecimal pairs
108 */
109 template <typename Type>
hexdump_lc_type(const Type & t)110 std::string hexdump_lc_type(const Type& t) {
111 return hexdump_lc(&t, sizeof(t));
112 }
113
114 /*!
115 * Dump a char vector as a sequence of lowercase hexadecimal pairs.
116 *
117 * \param data binary data to output in hex
118 * \return string of hexadecimal pairs
119 */
120 std::string hexdump_lc(const std::vector<char>& data);
121
122 /*!
123 * Dump a uint8_t vector as a sequence of lowercase hexadecimal pairs.
124 *
125 * \param data binary data to output in hex
126 * \return string of hexadecimal pairs
127 */
128 std::string hexdump_lc(const std::vector<uint8_t>& data);
129
130 /******************************************************************************/
131 // Parser for Hex Digit Sequence
132
133 /*!
134 * Read a string as a sequence of hexadecimal pairs. Converts each pair of
135 * hexadecimal digits into a byte of the output string. Throws
136 * std::runtime_error() if an unknown letter is encountered.
137 *
138 * \param str string to parse as hex digits
139 * \return string of read bytes
140 */
141 std::string parse_hexdump(const std::string& str);
142
143 //! \}
144 //! \}
145
146 } // namespace tlx
147
148 #endif // !TLX_STRING_HEXDUMP_HEADER
149
150 /******************************************************************************/
151