-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.c
80 lines (71 loc) · 1.74 KB
/
helper.c
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
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int offset(int a, int b)
{
if (a > b)
return a - b;
return b - a;
}
/* Borrowed from xpwn. */
void hexToInts(const char* hex, unsigned int** buffer, size_t* bytes)
{
*bytes = strlen(hex) / 2;
*buffer = (unsigned int*) malloc((*bytes) * sizeof(int));
size_t i;
for(i = 0; i < *bytes; i++)
{
sscanf(hex, "%2x", &((*buffer)[i]));
hex += 2;
}
}
#include <ctype.h>
#ifndef HEXDUMP_COLS
#define HEXDUMP_COLS 16
#endif
void hexdump(void *mem, unsigned int len)
{
unsigned int i, j;
for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
{
/* print offset */
if(i % HEXDUMP_COLS == 0)
{
printf("0x%06x: ", i);
}
if((i+8) % 16 == 0)
{
printf("- ");
}
/* print hex data */
if(i < len)
{
printf("%02x ", 0xFF & ((char*)mem)[i]);
}
else /* end of block, just aligning for ASCII dump */
{
printf(" ");
}
/* print ASCII dump */
if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
{
for(j = i - (HEXDUMP_COLS - 1); j <= i; j++)
{
if(j >= len) /* end of block, not really printing */
{
putchar(' ');
}
else if(isprint(((char*)mem)[j])) /* printable char */
{
putchar(0xFF & ((char*)mem)[j]);
}
else /* other char */
{
putchar('.');
}
}
putchar('\n');
}
}
}