/
debug.c
188 lines (144 loc) · 3.44 KB
/
debug.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* debug.c
*
* Copyright 2014 Hayden Kroepfl
*
*************************/
#include <stdio.h>
#define HEXDUMP_BYTES_PER_LINE 0x10
int hexdump_file(FILE *fp, unsigned int bytes, unsigned int start);
void hexdump_buffer(char *buffer, unsigned int bytes, unsigned int start);
int hex_magnitude( unsigned int num );
int ilog16(int num);
int main(int argc, char **argv)
{
FILE *fp;
int size;
if(argc < 2)
{
fprintf(stderr,"Usage: %s <filename>\n", argv[0]);
return 1;
}
// Open for binary reading
fp = fopen(argv[1], "rb");
if( fp == NULL )
{
fprintf(stderr,"Unable to open file %s for reading\n", argv[1]);
return 1;
}
// Get the file size
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
// Try and read the whole file
hexdump_file(fp, size, 0);
fclose(fp);
return 0;
}
int hexdump_file(FILE *fp, unsigned int bytes, unsigned int start)
{
unsigned int byte_count;
unsigned int prefix_mag;
int i;
int file_remaining;
char linebuf[HEXDUMP_BYTES_PER_LINE];
prefix_mag = hex_magnitude(bytes + start);
// Seek where we need to start in the file
if(fseek(fp, start, SEEK_SET) != 0)
{
// Unable to seek to the start location
return 1;
}
// Step through our input in BYTES_PER_LINE steps
for( byte_count = 0; bytes > byte_count; byte_count += HEXDUMP_BYTES_PER_LINE )
{
printf("%0*X: ", prefix_mag, start + byte_count); // Line prefix
// Read in our "line"
file_remaining = fread(linebuf, sizeof(char), HEXDUMP_BYTES_PER_LINE, fp);
// Now our hex bytes
for(i = 0; i < HEXDUMP_BYTES_PER_LINE; i++)
{
if( i < (bytes - byte_count) && i < file_remaining )
printf("%02X ", (unsigned char)linebuf[i]);
else
printf(" ");
}
putchar('|');
// Now the ascii representation
for(i = 0; i < HEXDUMP_BYTES_PER_LINE; i++)
{
if( i < (bytes - byte_count) && i < file_remaining)
{
// Check if it's an unprintable ASCII character
if( linebuf[i] < 32 )
putchar('.');
else
putchar(linebuf[i]);
}
else
putchar(' ');
}
putchar('|');
putchar('\n');
if(file_remaining < HEXDUMP_BYTES_PER_LINE)
{
// We hit the end of the file
break;
}
}
return 0;
}
void hexdump_buffer(char *buffer, unsigned int bytes, unsigned int start)
{
unsigned int byte_count;
unsigned int prefix_mag;
int i;
prefix_mag = hex_magnitude(bytes + start);
// Step through our input in BYTES_PER_LINE steps
for( byte_count = 0; bytes > byte_count; byte_count += HEXDUMP_BYTES_PER_LINE )
{
printf("%*X: ", prefix_mag, start + byte_count); // Line prefix
// Now our hex bytes
for(i = 0; i < HEXDUMP_BYTES_PER_LINE; i++)
{
if( i < (bytes - byte_count) )
printf("%02X ", (unsigned char)buffer[ start + byte_count + i ]);
else
printf(" ");
}
putchar('|');
// Now the ascii representation
for(i = 0; i < HEXDUMP_BYTES_PER_LINE; i++)
{
if( i < (bytes - byte_count) )
{
// Check if it's an unprintable ASCII character
if( buffer[ start + byte_count + i ] < 32 )
putchar('.');
else
putchar(buffer[ start + byte_count + i ]);
}
else
putchar(' ');
}
putchar('|');
putchar('\n');
}
}
int hex_magnitude( unsigned int num )
{
return ilog16(num) + 1;
}
/* ilog16
* Finds the integer logarithm (base 16) of num
* O(n), there are faster algorithms, but this is fine
****************/
int ilog16(int num)
{
int log = 0;
while( num >>= 4 )
{
log++;
}
return log;
}