2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include <debug_hex_dump.h>
16 // #pragma mark - HexDumpDataProvider
19 HexDumpDataProvider::~HexDumpDataProvider()
25 HexDumpDataProvider::GetAddressString(char* buffer
, size_t bufferSize
) const
31 // #pragma mark - HexDumpBufferDataProvider
34 HexDumpBufferDataProvider::HexDumpBufferDataProvider(const void* data
,
37 fData((const uint8
*)data
),
44 HexDumpBufferDataProvider::HasMoreData() const
51 HexDumpBufferDataProvider::NextByte()
62 HexDumpBufferDataProvider::GetAddressString(char* buffer
,
63 size_t bufferSize
) const
65 snprintf(buffer
, bufferSize
, "%p", fData
);
74 print_hex_dump(HexDumpDataProvider
& data
, size_t maxBytes
, uint32 flags
)
76 static const size_t kBytesPerBlock
= 4;
77 static const size_t kBytesPerLine
= 16;
80 for (; i
< maxBytes
&& data
.HasMoreData();) {
85 uint8 buffer
[kBytesPerLine
];
86 if ((flags
& HEX_DUMP_FLAG_OMIT_ADDRESS
) == 0
87 && data
.GetAddressString((char*)buffer
, sizeof(buffer
))) {
93 size_t bytesInLine
= 0;
94 for (; i
< maxBytes
&& bytesInLine
< kBytesPerLine
95 && data
.HasMoreData();
97 buffer
[bytesInLine
++] = data
.NextByte();
100 // print hex representation
101 for (size_t k
= 0; k
< bytesInLine
; k
++) {
102 if (k
> 0 && k
% kBytesPerBlock
== 0)
104 kprintf("%02x", buffer
[k
]);
107 // pad to align the text representation, if line is incomplete
108 if (bytesInLine
< kBytesPerLine
) {
109 int missingBytes
= int(kBytesPerLine
- bytesInLine
);
111 2 * missingBytes
+ int(missingBytes
/ kBytesPerBlock
), "");
114 // print character representation
116 for (size_t k
= 0; k
< bytesInLine
; k
++)
117 kprintf("%c", isprint(buffer
[k
]) ? buffer
[k
] : '.');
126 print_hex_dump(const void* data
, size_t maxBytes
, uint32 flags
)
128 HexDumpBufferDataProvider
dataProvider(data
, maxBytes
);
129 print_hex_dump(dataProvider
, maxBytes
, flags
);
133 } // namespace BKernel