Pigweed
 
Loading...
Searching...
No Matches
hex_dump.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#pragma once
16
17#include <cstdint>
18
19#include "pw_bytes/span.h"
20#include "pw_span/span.h"
21#include "pw_status/status.h"
22
23namespace pw::dump {
24
33inline constexpr const size_t kHexAddrStringSize = sizeof(uintptr_t) * 2 + 2;
34
82 public:
83 enum AddressMode {
84 kDisabled = 0,
85 kOffset = 1,
86 kAbsolute = 2,
87 };
88
89 struct Flags {
91 uint8_t bytes_per_line : 8;
92
100 uint8_t group_every : 8;
101
103 bool show_ascii : 1;
104
106 bool show_header : 1;
107
109 AddressMode prefix_mode : 2;
110 };
111
112 Flags flags = {.bytes_per_line = 16,
113 .group_every = 1,
114 .show_ascii = true,
115 .show_header = true,
116 .prefix_mode = AddressMode::kOffset};
117
118 FormattedHexDumper() = default;
119 FormattedHexDumper(span<char> dest) {
120 SetLineBuffer(dest)
121 .IgnoreError(); // TODO: b/242598609 - Handle Status properly
122 }
123 FormattedHexDumper(span<char> dest, Flags config_flags)
124 : flags(config_flags) {
125 SetLineBuffer(dest)
126 .IgnoreError(); // TODO: b/242598609 - Handle Status properly
127 }
128
129 // TODO: b/234892215 - Add iterator support.
130
144 Status SetLineBuffer(span<char> dest);
145
161 Status BeginDump(ConstByteSpan data);
162
189
190 private:
191 Status ValidateBufferSize();
192 Status PrintFormatHeader();
193
194 size_t current_offset_;
195 span<char> dest_;
196 ConstByteSpan source_data_;
197};
198
226Status DumpAddr(span<char> dest, uintptr_t addr);
227inline Status DumpAddr(span<char> dest, const void* ptr) {
228 uintptr_t addr = reinterpret_cast<uintptr_t>(ptr);
229 return DumpAddr(dest, addr);
230}
231
232} // namespace pw::dump
Definition: status.h:85
constexpr void IgnoreError() const
Definition: status.h:222
Definition: hex_dump.h:81
Status BeginDump(ConstByteSpan data)
Status SetLineBuffer(span< char > dest)
Definition: hex_dump.h:89
bool show_ascii
Show or hide ascii interpretation of binary data.
Definition: hex_dump.h:103
uint8_t bytes_per_line
Sets the number of source data bytes to print in each formatted line.
Definition: hex_dump.h:91
bool show_header
Show descriptive column headers.
Definition: hex_dump.h:106
AddressMode prefix_mode
Prefix each line of the dump with an offset or absolute address.
Definition: hex_dump.h:109
uint8_t group_every
Definition: hex_dump.h:100