SNode.C
Loading...
Searching...
No Matches
hexdump.cpp
Go to the documentation of this file.
1/*
2 * snode.c - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "hexdump.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include "log/Logger.h"
25
26#include <cstddef>
27#include <cstdint>
28#include <iomanip>
29#include <sstream>
30
31#endif // DOXYGEN_SHOULD_SKIP_THIS
32
33namespace utils {
34
35 // From: https://gist.github.com/shreyasbharath/32a8092666303a916e24a81b18af146b
37 return hexDump(bytes.data(), bytes.size(), prefixLength, prefixAtFirstLine);
38 }
39
41 return hexDump(string.data(), string.length(), prefixLength, prefixAtFirstLine);
42 }
43
45 std::stringstream hexStream;
46
47 if (length > 0) {
48 uint8_t buff[17];
49 size_t i = 0;
50
51 hexStream << std::hex;
52
53 int currentPrefixLength = prefixAtFirstLine ? prefixLength : 0;
54
55 // Process every byte in the data.
56 for (i = 0; i < length; i++) {
57 // Multiple of 16 means new line (with line offset).
58
59 if ((i % 16) == 0) {
60 // Just don't print ASCII for the zeroth line.
61 if (i != 0) {
62 hexStream << " " << buff << std::endl;
63 }
64
65 // Output the offset.
66 hexStream << Color::Code::FG_BLUE;
67 hexStream << std::setw(currentPrefixLength) << std::setfill(' ') << ""
68 << ": " << std::setw(8) << std::setfill('0') << static_cast<unsigned int>(i);
69 hexStream << Color::Code::FG_DEFAULT << " ";
70 }
71
72 // Now the hex code for the specific character.
73 hexStream << Color::Code::FG_GREEN;
74 hexStream << " " << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(static_cast<unsigned char>(bytes[i]));
75 hexStream << Color::Code::FG_DEFAULT;
76
77 // And store a printable ASCII character for later.
78 if ((bytes[i] < 0x20) || (bytes[i] > 0x7e)) {
79 buff[i % 16] = '.';
80 } else {
81 buff[i % 16] = static_cast<uint8_t>(bytes[i]);
82 }
83 buff[(i % 16) + 1] = '\0';
84
85 currentPrefixLength = prefixLength;
86 }
87
88 hexStream << std::dec;
89
90 // Pad out last line if not exactly 16 characters.
91 while ((i % 16) != 0) {
92 hexStream << " ";
93 i++;
94 }
95
96 // And print the final ASCII bit.
97 hexStream << " " << buff;
98 }
99
100 return hexStream.str();
101 }
102
103} // namespace utils
Definition Config.h:37
std::string hexDump(const char *bytes, uint64_t length, int prefixLength, bool prefixAtFirstLine)
Definition hexdump.cpp:44
std::string hexDump(const std::vector< char > &bytes, int prefixLength, bool prefixAtFirstLine)
Definition hexdump.cpp:36