-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhexdump.h
executable file
·87 lines (72 loc) · 2.63 KB
/
hexdump.h
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
#include <ctype.h>
#define HEXDUMP_COLS (16)
void hexdump_log(void *mem, unsigned int len) {
unsigned int i, j;
char buffer[256] = {0};
char str[80] = {0};
printf("[*] Dumping %08x bytes at address %llx:\n", len, (unsigned long long) mem);
for (i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++) {
if(i % HEXDUMP_COLS == 0) {
sprintf(str, "[*] 0x%016llx (+0x%04x): ", (unsigned long long) (i + mem), i);
strcat(buffer, str);
}
if(i < len) {
sprintf(str, "%02x ", 0xFF & ((char*)mem)[i]);
strcat(buffer, str);
} else {
sprintf(str, " ");
strcat(buffer, str);
}
if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1)) {
for(j = i - (HEXDUMP_COLS - 1); j <= i; j++) {
if(j >= len) {
sprintf(str, " ");
strcat(buffer, str);
} else if (isprint (((char*) mem)[j])) {
sprintf(str, "%c", 0xFF & ((char*)mem)[j]);
strcat(buffer,str);
} else {
sprintf(str, ".");
strcat(buffer,str);
}
}
printf("%s\n", buffer);
memset(&buffer[0], 0, sizeof(buffer));
}
}
}
void hexdump_log_base(void *mem, unsigned int len, unsigned int base) {
unsigned int i, j;
char buffer[256] = {0};
char str[80] = {0};
printf("[*] Dumping %08x bytes at address %llx:\n", len, (unsigned long long) mem);
for (i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++) {
if(i % HEXDUMP_COLS == 0) {
sprintf(str, "[*] 0x%08x (+0x%04x): ", (unsigned int) (i + base), i);
strcat(buffer, str);
}
if(i < len) {
sprintf(str, "%02x ", 0xFF & ((char*)mem)[i]);
strcat(buffer, str);
} else {
sprintf(str, " ");
strcat(buffer, str);
}
if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1)) {
for(j = i - (HEXDUMP_COLS - 1); j <= i; j++) {
if(j >= len) {
sprintf(str, " ");
strcat(buffer, str);
} else if (isprint (((char*) mem)[j])) {
sprintf(str, "%c", 0xFF & ((char*)mem)[j]);
strcat(buffer,str);
} else {
sprintf(str, ".");
strcat(buffer,str);
}
}
printf("%s\n", buffer);
memset(&buffer[0], 0, sizeof(buffer));
}
}
}