xref: /dpdk/lib/eal/common/eal_common_hexdump.c (revision 6d49cb526ef822bcad659537644b1a900414b863)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson #include <stdio.h>
599a2dd95SBruce Richardson #include <rte_hexdump.h>
699a2dd95SBruce Richardson #include <rte_string_fns.h>
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson #define LINE_LEN 128
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson void
rte_hexdump(FILE * f,const char * title,const void * buf,unsigned int len)1199a2dd95SBruce Richardson rte_hexdump(FILE *f, const char *title, const void *buf, unsigned int len)
1299a2dd95SBruce Richardson {
1399a2dd95SBruce Richardson 	unsigned int i, out, ofs;
1499a2dd95SBruce Richardson 	const unsigned char *data = buf;
1599a2dd95SBruce Richardson 	char line[LINE_LEN];	/* space needed 8+16*3+3+16 == 75 */
1699a2dd95SBruce Richardson 
1799a2dd95SBruce Richardson 	fprintf(f, "%s at [%p], len=%u\n",
18*6d49cb52STyler Retzlaff 		title != NULL ? title : "  Dump data", data, len);
1999a2dd95SBruce Richardson 	ofs = 0;
2099a2dd95SBruce Richardson 	while (ofs < len) {
2199a2dd95SBruce Richardson 		/* format the line in the buffer */
2299a2dd95SBruce Richardson 		out = snprintf(line, LINE_LEN, "%08X:", ofs);
2399a2dd95SBruce Richardson 		for (i = 0; i < 16; i++) {
2499a2dd95SBruce Richardson 			if (ofs + i < len)
2599a2dd95SBruce Richardson 				snprintf(line + out, LINE_LEN - out,
2699a2dd95SBruce Richardson 					 " %02X", (data[ofs + i] & 0xff));
2799a2dd95SBruce Richardson 			else
2899a2dd95SBruce Richardson 				strcpy(line + out, "   ");
2999a2dd95SBruce Richardson 			out += 3;
3099a2dd95SBruce Richardson 		}
3199a2dd95SBruce Richardson 
3299a2dd95SBruce Richardson 
3399a2dd95SBruce Richardson 		for (; i <= 16; i++)
3499a2dd95SBruce Richardson 			out += snprintf(line + out, LINE_LEN - out, " | ");
3599a2dd95SBruce Richardson 
3699a2dd95SBruce Richardson 		for (i = 0; ofs < len && i < 16; i++, ofs++) {
3799a2dd95SBruce Richardson 			unsigned char c = data[ofs];
3899a2dd95SBruce Richardson 
3999a2dd95SBruce Richardson 			if (c < ' ' || c > '~')
4099a2dd95SBruce Richardson 				c = '.';
4199a2dd95SBruce Richardson 			out += snprintf(line + out, LINE_LEN - out, "%c", c);
4299a2dd95SBruce Richardson 		}
4399a2dd95SBruce Richardson 		fprintf(f, "%s\n", line);
4499a2dd95SBruce Richardson 	}
4599a2dd95SBruce Richardson 	fflush(f);
4699a2dd95SBruce Richardson }
4799a2dd95SBruce Richardson 
4899a2dd95SBruce Richardson void
rte_memdump(FILE * f,const char * title,const void * buf,unsigned int len)4999a2dd95SBruce Richardson rte_memdump(FILE *f, const char *title, const void *buf, unsigned int len)
5099a2dd95SBruce Richardson {
5199a2dd95SBruce Richardson 	unsigned int i, out;
5299a2dd95SBruce Richardson 	const unsigned char *data = buf;
5399a2dd95SBruce Richardson 	char line[LINE_LEN];
5499a2dd95SBruce Richardson 
5599a2dd95SBruce Richardson 	if (title)
5699a2dd95SBruce Richardson 		fprintf(f, "%s: ", title);
5799a2dd95SBruce Richardson 
5899a2dd95SBruce Richardson 	line[0] = '\0';
5999a2dd95SBruce Richardson 	for (i = 0, out = 0; i < len; i++) {
6099a2dd95SBruce Richardson 		/* Make sure we do not overrun the line buffer length. */
6199a2dd95SBruce Richardson 		if (out >= LINE_LEN - 4) {
6299a2dd95SBruce Richardson 			fprintf(f, "%s", line);
6399a2dd95SBruce Richardson 			out = 0;
6499a2dd95SBruce Richardson 			line[out] = '\0';
6599a2dd95SBruce Richardson 		}
6699a2dd95SBruce Richardson 		out += snprintf(line + out, LINE_LEN - out, "%02x%s",
6799a2dd95SBruce Richardson 				(data[i] & 0xff), ((i + 1) < len) ? ":" : "");
6899a2dd95SBruce Richardson 	}
6999a2dd95SBruce Richardson 	if (out > 0)
7099a2dd95SBruce Richardson 		fprintf(f, "%s", line);
7199a2dd95SBruce Richardson 	fprintf(f, "\n");
7299a2dd95SBruce Richardson 
7399a2dd95SBruce Richardson 	fflush(f);
7499a2dd95SBruce Richardson }
75