summaryrefslogtreecommitdiff
path: root/include/hexdump.h
blob: 6f3d5fa569faecebaf26b15c53ebc5625bf16647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: BSD-2-Clause-Patent

#ifndef STATIC_HEXDUMP_H
#define STATIC_HEXDUMP_H

#include "shim.h"
#include "include/console.h"

static inline unsigned long UNUSED
prepare_hex(const void *data, size_t size, char *buf, unsigned int position)
{
	char hexchars[] = "0123456789abcdef";
	int offset = 0;
	unsigned long i;
	unsigned long j;
	unsigned long ret;

	unsigned long before = (position % 16);
	unsigned long after = (before+size >= 16) ? 0 : 16 - (before+size);

	for (i = 0; i < before; i++) {
		buf[offset++] = 'X';
		buf[offset++] = 'X';
		buf[offset++] = ' ';
		if (i == 7)
			buf[offset++] = ' ';
	}
	for (j = 0; j < 16 - after - before; j++) {
		uint8_t d = ((uint8_t *)data)[j];
		buf[offset++] = hexchars[(d & 0xf0) >> 4];
		buf[offset++] = hexchars[(d & 0x0f)];
		if (i+j != 15)
			buf[offset++] = ' ';
		if (i+j == 7)
			buf[offset++] = ' ';
	}
	ret = 16 - after - before;
	j += i;
	for (i = 0; i < after; i++) {
		buf[offset++] = 'X';
		buf[offset++] = 'X';
		if (i+j != 15)
			buf[offset++] = ' ';
		if (i+j == 7)
			buf[offset++] = ' ';
	}
	buf[offset] = '\0';
	return ret;
}

static inline void UNUSED
prepare_text(const void *data, size_t size, char *buf, unsigned int position)
{
	int offset = 0;
	unsigned long i;
	unsigned long j;

	unsigned long before = position % 16;
	unsigned long after = (before+size > 16) ? 0 : 16 - (before+size);

	if (size == 0) {
		buf[0] = '\0';
		return;
	}
	for (i = 0; i < before; i++)
		buf[offset++] = 'X';
	buf[offset++] = '|';
	for (j = 0; j < 16 - after - before; j++) {
		if (isprint(((uint8_t *)data)[j]))
			buf[offset++] = ((uint8_t *)data)[j];
		else
			buf[offset++] = '.';
	}
	buf[offset++] = '|';
	buf[offset] = '\0';
}

/*
 * variadic hexdump formatted
 * think of it as: printf("%s%s\n", vformat(fmt, ap), hexdump(data,size));
 */
static inline void UNUSED EFIAPI
vhexdumpf(const char *file, int line, const char *func, const CHAR16 *const fmt,
          const void *data, unsigned long size, size_t at, ms_va_list ap)
{
	unsigned long display_offset = at;
	unsigned long offset = 0;

	if (verbose == 0)
		return;

	if (!data) {
		dprint(L"hexdump of a NULL pointer!\n");
		return;
	}
	if (!size) {
		dprint(L"hexdump of a 0 size region!\n");
		return;
	}

	while (offset < size) {
		char hexbuf[49];
		char txtbuf[19];
		unsigned long sz;

		sz = prepare_hex(data+offset, size-offset, hexbuf,
				 (unsigned long)data+offset);
		if (sz == 0)
			return;

		prepare_text(data+offset, size-offset, txtbuf,
			     (unsigned long)data+offset);
		if (fmt && fmt[0] != 0)
			vdprint_(fmt, file, line, func, ap);
		dprint_(L"%a:%d:%a() %08lx  %a  %a\n", file, line, func, display_offset, hexbuf, txtbuf);

		display_offset += sz;
		offset += sz;
	}
}

/*
 * hexdump formatted
 * think of it as: printf("%s%s", format(fmt, ...), hexdump(data,size)[lineN]);
 */
static inline void UNUSED EFIAPI
hexdumpf(const char *file, int line, const char *func, const CHAR16 *const fmt,
         const void *data, unsigned long size, size_t at, ...)
{
	ms_va_list ap;

	ms_va_start(ap, at);
	vhexdumpf(file, line, func, fmt, data, size, at, ap);
	ms_va_end(ap);
}

static inline void UNUSED
hexdump(const char *file, int line, const char *func, const void *data, unsigned long size)
{
	hexdumpf(file, line, func, L"", data, size, (intptr_t)data);
}

static inline void UNUSED
hexdumpat(const char *file, int line, const char *func, const void *data, unsigned long size, size_t at)
{
	hexdumpf(file, line, func, L"", data, size, at);
}

#if defined(SHIM_UNIT_TEST)
#define LogHexDump(data, ...)
#define dhexdump(data, ...)
#define dhexdumpat(data, ...)
#define dhexdumpf(fmt, ...)
#else
#define LogHexdump(data, sz) LogHexdump_(__FILE__, __LINE__, __func__, data, sz)
#define dhexdump(data, sz)   hexdump(__FILE__, __LINE__, __func__, data, sz)
#define dhexdumpat(data, sz, at) \
	hexdumpat(__FILE__, __LINE__ - 1, __func__, data, sz, at)
#define dhexdumpf(fmt, data, sz, at, ...) \
	hexdumpf(__FILE__, __LINE__ - 1, __func__, fmt, data, sz, at, ##__VA_ARGS__)
#endif

#endif /* STATIC_HEXDUMP_H */
// vim:fenc=utf-8:tw=75:noet