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
|
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define PAGE 256
#define COLS 16
int main(int argc, char** argv)
{
char prompt_name[64];
const char* name;
if (argc >= 2)
name = argv[1];
else
{
printf("hexview, type a filename\nfile: ");
readline(prompt_name, 64);
printf("\n");
name = prompt_name;
}
int fd = open(name, O_RDONLY);
if (fd < 0)
{
printf("no such file\n");
return 1;
}
long size = lseek(fd, 0, SEEK_END);
unsigned long offset = 0;
char buffer[PAGE];
for (;;)
{
lseek(fd, (long)offset, SEEK_SET);
long count = read(fd, buffer, PAGE);
printf("--- %s @ 0x%08lx / 0x%08lx ---\n", name, offset, (unsigned long)size);
for (long row = 0; row < count; row += COLS)
{
printf("%08lx: ", offset + (unsigned long)row);
for (int column = 0; column < COLS; column++)
{
if (row + column < count)
printf("%02x ", (unsigned char)buffer[row + column]);
else
printf(" ");
}
printf(" ");
for (int column = 0; column < COLS && row + column < count; column++)
{
char character = buffer[row + column];
putchar((character >= 32 && character < 127) ? character : '.');
}
printf("\n");
}
printf("[n]ext [p]rev [q]uit (or up/down arrows) > ");
int key = getchar();
if (key == 27) // ESC
{
getchar();
int final = getchar();
if (final == 'A')
key = 'p';
else if (final == 'B')
key = 'n';
}
printf("\n");
if (key == 'q')
break;
if (key == 'n' && offset + PAGE < (unsigned long)size)
offset += PAGE;
if (key == 'p' && offset >= PAGE)
offset -= PAGE;
}
close(fd);
return 0;
}
|