1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/hexdump.c Wed Apr 01 22:11:05 2009 +0100 1.3 @@ -0,0 +1,58 @@ 1.4 +#include <stdio.h> 1.5 +#include <ctype.h> 1.6 +#include <string.h> 1.7 + 1.8 +void hex_dump(void *data, int size) 1.9 +{ 1.10 + /* dumps size bytes of *data to stdout. Looks like: 1.11 + * [0000] 75 6E 6B 6E 6F 77 6E 20 1.12 + * 30 FF 00 00 00 00 39 00 unknown 0.....9. 1.13 + * (in a single line of course) 1.14 + */ 1.15 + 1.16 + unsigned char *p = data; 1.17 + unsigned char c; 1.18 + int n; 1.19 + char bytestr[4] = {0}; 1.20 + char addrstr[10] = {0}; 1.21 + char hexstr[ 16*3 + 5] = {0}; 1.22 + char charstr[16*1 + 5] = {0}; 1.23 + for(n=1;n<=size;n++) { 1.24 + if (n%16 == 1) { 1.25 + /* store address for this line */ 1.26 + snprintf(addrstr, sizeof(addrstr), "%.4x", 1.27 + ((unsigned int)p-(unsigned int)data) ); 1.28 + } 1.29 + 1.30 + c = *p; 1.31 + if (isalnum(c) == 0) { 1.32 + c = '.'; 1.33 + } 1.34 + 1.35 + /* store hex str (for left side) */ 1.36 + snprintf(bytestr, sizeof(bytestr), "%02X ", *p); 1.37 + strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1); 1.38 + 1.39 + /* store char str (for right side) */ 1.40 + snprintf(bytestr, sizeof(bytestr), "%c", c); 1.41 + strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1); 1.42 + 1.43 + if(n%16 == 0) { 1.44 + /* line completed */ 1.45 + printf("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr); 1.46 + hexstr[0] = 0; 1.47 + charstr[0] = 0; 1.48 + } else if(n%8 == 0) { 1.49 + /* half line: add whitespaces */ 1.50 + strncat(hexstr, " ", sizeof(hexstr)-strlen(hexstr)-1); 1.51 + strncat(charstr, " ", sizeof(charstr)-strlen(charstr)-1); 1.52 + } 1.53 + p++; /* next byte */ 1.54 + } 1.55 + 1.56 + if (strlen(hexstr) > 0) { 1.57 + /* print rest of buffer if not empty */ 1.58 + printf("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr); 1.59 + } 1.60 +} 1.61 +