1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/misc/kutils.h"
4
5#include "mos/syslog/printk.h"
6
7static const int HEXDUMP_COLS = 16;
8
9void hexdump(const char *data, const size_t len)
10{
11 if (len)
12 pr_info(" " PTR_FMT ": ", (ptr_t) data);
13
14 for (size_t i = 0; i < len; i++)
15 {
16 pr_cont("%02hhx ", (char) data[i]);
17 if ((i + 1) % HEXDUMP_COLS == 0)
18 {
19 for (size_t j = i - (HEXDUMP_COLS - 1); j <= i; j++)
20 {
21 const char c = data[j];
22 pr_cont("%c", c >= 32 && c <= 126 ? c : '.');
23 }
24
25 if (i + 1 < len)
26 pr_info(" " PTR_FMT ": ", (ptr_t) (data + i + 1));
27 }
28 }
29
30 if (len % HEXDUMP_COLS != 0)
31 {
32 const size_t spaces = (HEXDUMP_COLS - (len % HEXDUMP_COLS)) * 3;
33 pr_cont("%*c", (int) spaces, ' ');
34 for (size_t i = len - (len % HEXDUMP_COLS); i < len; i++)
35 {
36 const char c = data[i];
37 pr_cont("%c", c >= 32 && c <= 126 ? c : '.');
38 }
39 }
40
41 pr_info("");
42}
43