MySQL 8.3.0
Source Code Documentation
hexify.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2023, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24#ifndef MYSQL_HARNESS_HEXIFY_INCLUDED
25#define MYSQL_HARNESS_HEXIFY_INCLUDED
26
27#include <algorithm> // copy
28#include <array>
29#include <charconv>
30#include <cstdint>
31#include <iterator> // back_inserter
32#include <string>
33#include <utility> // exchange
34
35namespace mysql_harness {
36
37/**
38 * hexdump into a string.
39 *
40 * converts the contents of continous container (has .data() and .size())
41 * as hex values in rows of 16 bytes.
42 *
43 * @param buf a container
44 * @return string containing the hexdump
45 */
46template <class T>
47inline std::string hexify(const T &buf) {
48 std::string out;
49
50 for (auto cur = reinterpret_cast<const uint8_t *>(buf.data()),
51 end = cur + buf.size();
52 cur != end;) {
53 size_t col{};
54 std::array<char, 16L * 3> hexline{
55 '.', '.', ' ', '.', '.', ' ', '.', '.', ' ', '.', '.', ' ',
56 '.', '.', ' ', '.', '.', ' ', '.', '.', ' ', '.', '.', ' ',
57 '.', '.', ' ', '.', '.', ' ', '.', '.', ' ', '.', '.', ' ',
58 '.', '.', ' ', '.', '.', ' ', '.', '.', ' ', '.', '.', ' '};
59 std::array<char, 16> printable;
60
61 for (auto *hexline_pos = hexline.data(); cur != end && col < 16;
62 ++cur, ++col, hexline_pos += 3) {
63 const auto ch = *cur;
64
65 const auto res = std::to_chars(hexline_pos, hexline_pos + 2, ch, 16);
66 if (res.ptr == hexline_pos + 1) {
67 // insert a leading zero, if only one digit was produced.
68 hexline_pos[1] = std::exchange(hexline_pos[0], '0');
69 }
70
71 printable[col] = std::isprint(ch) ? static_cast<char>(ch) : '.';
72 }
73
74 std::copy(hexline.begin(), hexline.end(), std::back_inserter(out));
75 out.append(" "); // separator between hexline and printable
76
77 // only append the chars actually used.
78 for (size_t ndx = 0; ndx < col; ++ndx) {
79 out.push_back(printable[ndx]);
80 }
81 out.append("\n");
82 }
83
84 return out;
85}
86} // namespace mysql_harness
87
88#endif
Definition: buf0block_hint.cc:29
Definition: common.h:41
std::string hexify(const T &buf)
hexdump into a string.
Definition: hexify.h:47