MOS Source Code
Loading...
Searching...
No Matches
kutils.c
Go to the documentation of this file.
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}
void hexdump(const char *data, const size_t len)
Definition kutils.c:9
static const int HEXDUMP_COLS
Definition kutils.c:7
#define pr_info(fmt,...)
Definition printk.h:35
#define pr_cont(fmt,...)
Definition printk.h:41
#define PTR_FMT
Definition types.h:33
unsigned long ptr_t
Definition types.h:25