1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 #include <stdlib.h> 5 #include <stdio.h> 6 #include <errno.h> 7 #include <stdint.h> 8 #include <rte_hexdump.h> 9 #include <rte_string_fns.h> 10 11 #define LINE_LEN 128 12 13 void 14 rte_hexdump(FILE *f, const char *title, const void *buf, unsigned int len) 15 { 16 unsigned int i, out, ofs; 17 const unsigned char *data = buf; 18 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */ 19 20 fprintf(f, "%s at [%p], len=%u\n", 21 title ? : " Dump data", data, len); 22 ofs = 0; 23 while (ofs < len) { 24 /* format the line in the buffer */ 25 out = snprintf(line, LINE_LEN, "%08X:", ofs); 26 for (i = 0; i < 16; i++) { 27 if (ofs + i < len) 28 snprintf(line + out, LINE_LEN - out, 29 " %02X", (data[ofs + i] & 0xff)); 30 else 31 strcpy(line + out, " "); 32 out += 3; 33 } 34 35 36 for (; i <= 16; i++) 37 out += snprintf(line + out, LINE_LEN - out, " | "); 38 39 for (i = 0; ofs < len && i < 16; i++, ofs++) { 40 unsigned char c = data[ofs]; 41 42 if (c < ' ' || c > '~') 43 c = '.'; 44 out += snprintf(line + out, LINE_LEN - out, "%c", c); 45 } 46 fprintf(f, "%s\n", line); 47 } 48 fflush(f); 49 } 50 51 void 52 rte_memdump(FILE *f, const char *title, const void *buf, unsigned int len) 53 { 54 unsigned int i, out; 55 const unsigned char *data = buf; 56 char line[LINE_LEN]; 57 58 if (title) 59 fprintf(f, "%s: ", title); 60 61 line[0] = '\0'; 62 for (i = 0, out = 0; i < len; i++) { 63 /* Make sure we do not overrun the line buffer length. */ 64 if (out >= LINE_LEN - 4) { 65 fprintf(f, "%s", line); 66 out = 0; 67 line[out] = '\0'; 68 } 69 out += snprintf(line + out, LINE_LEN - out, "%02x%s", 70 (data[i] & 0xff), ((i + 1) < len) ? ":" : ""); 71 } 72 if (out > 0) 73 fprintf(f, "%s", line); 74 fprintf(f, "\n"); 75 76 fflush(f); 77 } 78