microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
hexdump.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2//
3// SPDX-License-Identifier: BSD-2-Clause
4
5#pragma once
6
9#include "../microfmt.hpp"
10#include <algorithm>
11#include <cstddef>
12#include <cstdint>
13#include <string_view>
14
15namespace microfmt {
16
17// ============================================================================
18// Checked Memory Reader Callback Definition
19// ============================================================================
20
31using memory_reader_fn_t = size_t (*)(void *ctx, uintptr_t src_addr, uint8_t *dst_buf, size_t max_len) noexcept;
32
33// ============================================================================
34// Hex Dump Configuration & Descriptor
35// ============================================================================
36
37struct MICROFMT_API_CLASS RELOCO_POINTER hexdump_view {
38 uintptr_t base_addr{0};
39 span<const uint8_t> direct_data{};
40 void *reader_ctx{nullptr};
41 memory_reader_fn_t reader_fn{nullptr};
42 size_t total_len{0};
43 uint8_t bytes_per_line{16};
44 bool show_ascii{true};
45 bool show_address{true};
46 bool uppercase{false};
47};
48
52[[nodiscard]] constexpr hexdump_view hexdump(span<const uint8_t> data, uintptr_t base_addr = 0,
53 uint8_t bytes_per_line = 16, bool show_ascii = true) noexcept {
54 return hexdump_view{base_addr, data, nullptr,
55 nullptr, data.size(), bytes_per_line == 0 ? static_cast<uint8_t>(16) : bytes_per_line,
56 show_ascii, true, false};
57}
58
62// low-level formatting: raw character pointers and integer width conversions bounded by the buffer sizes checked here.
63RELOCO_BEGIN_UNSAFE_CODE
64[[nodiscard]] constexpr hexdump_view hexdump_checked(uintptr_t base_addr, size_t total_len, memory_reader_fn_t reader,
65 void *ctx = nullptr, uint8_t bytes_per_line = 16,
66 bool show_ascii = true) noexcept {
67 return hexdump_view{base_addr, {}, ctx,
68 reader, total_len, bytes_per_line == 0 ? static_cast<uint8_t>(16) : bytes_per_line,
69 show_ascii, true, false};
70}
72
73// ============================================================================
74// Formatter Specialization for hexdump_view
75// ============================================================================
76
90MICROFMT_API void format_hexdump(const hexdump_view &h, span<uint8_t> line_buffer, const sink &out) noexcept;
91
92#if MICROFMT_SHARED_PROVIDE_DEFINITIONS
93#include "hexdump.ipp"
94#endif
95
96template <> struct formatter<hexdump_view> {
97 constexpr void parse(format_parse_context &ctx) noexcept { (void)ctx; }
98
99 void format(const hexdump_view &h, const sink &out) const noexcept {
100 // NOLINTNEXTLINE(reloco-no-c-array): fixed-size buffer/table for span APIs
101 uint8_t line_buffer[32] = {}; // Bounded scratch buffer on stack
102 format_hexdump(h, {line_buffer, sizeof(line_buffer)}, out);
103 }
104};
105
106inline void hexdump_to(const sink &out, span<const uint8_t> data, uintptr_t base_address = 0) noexcept {
107 const formatter<hexdump_view> f = {};
108 f.format(hexdump(data, base_address), out);
109}
110
111} // namespace microfmt
Definition microfmt.hpp:881
#define MICROFMT_API_CLASS
Definition compat.hpp:181
#define MICROFMT_API
Definition compat.hpp:135
Definition assert.hpp:19
void hexdump_to(const sink &out, span< const uint8_t > data, uintptr_t base_address=0) noexcept
Definition hexdump.hpp:106
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
RELOCO_BEGIN_UNSAFE_CODE constexpr hexdump_view hexdump_checked(uintptr_t base_addr, size_t total_len, memory_reader_fn_t reader, void *ctx=nullptr, uint8_t bytes_per_line=16, bool show_ascii=true) noexcept
Helper for dumping memory via a fault-checked accessor probe.
Definition hexdump.hpp:64
RELOCO_END_UNSAFE_CODE MICROFMT_API void format_hexdump(const hexdump_view &h, span< uint8_t > line_buffer, const sink &out) noexcept
Formats a hex dump using caller-provided line storage.
constexpr hexdump_view hexdump(span< const uint8_t > data, uintptr_t base_addr=0, uint8_t bytes_per_line=16, bool show_ascii=true) noexcept
Helper for dumping safe, direct in-memory buffers (zero overhead).
Definition hexdump.hpp:52
size_t(*)(void *ctx, uintptr_t src_addr, uint8_t *dst_buf, size_t max_len) noexcept memory_reader_fn_t
Function signature for a safe/probed memory reader.
Definition hexdump.hpp:31
RELOCO_END_UNSAFE_CODE
Definition remote_object.hpp:132
constexpr void parse(format_parse_context &ctx) noexcept
Definition hexdump.hpp:97
void format(const hexdump_view &h, const sink &out) const noexcept
Definition hexdump.hpp:99
Definition microfmt.hpp:985
Definition hexdump.hpp:37