-
Notifications
You must be signed in to change notification settings - Fork 2
/
dump.c
133 lines (106 loc) · 2.55 KB
/
dump.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
unsigned char *dumpAsBytes(unsigned char *p, unsigned long size, unsigned long base)
{
register unsigned long i, r, total, count;
register unsigned char *op = p;
count = size / 16;
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
for (r=0; r < count; r++)
{
printf("%08X ", (p - op) + base);
for (total = 0, i=0; i < 16; i++, total++)
{
printf(" %02X", (unsigned char) p[i]);
}
printf(" ");
for (i=0; i < total; i++)
{
if (p[i] < 32 || p[i] > 126)
printf(".");
else printf("%c", p[i]);
}
printf("\n");
p = (void *)((unsigned long) p + (unsigned long) total);
}
return p;
}
unsigned char *dumpAsLong(unsigned char *p, unsigned long size, unsigned long base)
{
register int i, r, count;
register unsigned char *op = p;
unsigned long *lp;
count = size / 16;
lp = (unsigned long *) p;
for (r=0; r < count; r++)
{
printf("%08X ", (p - op) + base);
for (i=0; i < (16 / 4); i++)
{
printf(" %08X", (unsigned long) lp[i]);
}
printf(" ");
for (i=0; i < 16; i++)
{
if (p[i] < 32 || p[i] > 126) printf(".");
else printf("%c", p[i]);
}
printf("\n");
p = (void *)((unsigned long) p + (unsigned long) 16);
lp = (unsigned long *) p;
}
return p;
}
int getch(void)
{
return getc(stdin);
}
unsigned long pause(void)
{
extern int getch(void);
register unsigned long key;
printf(" --- More --- ");
key = getch();
printf("%c %c", '\r', '\r');
if (key == 0x1B) // ESCAPE
return 1;
else
return 0;
}
unsigned char buffer[512];
int main(int argc, char *argv[])
{
long rc, total = 0, offset = 0;
FILE *fp;
if (argc < 2)
{
printf("USAGE: dump <filename.ext> <offset>\n");
return 1;
}
if (argc == 3)
offset = atol(argv[2]);
fp = fopen(argv[1], "rb");
if (!fp)
{
printf("error opening file [%s]\n", argv[1]);
return 1;
}
rc = fseek(fp, offset, SEEK_SET);
if (rc == -1)
{
printf("error seeking file offset %d\n", offset);
return 1;
}
total = offset;
while (!feof(fp))
{
rc = fread(buffer, 256, 1, fp);
dumpAsBytes(buffer, 256, total);
total += 256;
if (pause())
break;
}
fclose(fp);
return 0;
}