missing x bit
[hexdump.git] / hexdump.c
1 /* depend: */
2 /* cflags: */
3 /* linker: debug.o */
4
5 #include <assert.h>
6 #include <malloc.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "debug.h"
12
13 /* macros */
14
15 #define CEIL(x, y) (((x) + (y) - 1) / (y))
16 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
17 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
18
19 //#define BUFFERSIZE 4096
20 #define BUFFERSIZE 256
21 #define NBCOLS 8
22 #define NBDIGITS 6
23 #define SEQLEN 32
24
25 /* gobal variables */
26
27 int nbcols = NBCOLS;
28 int nbdigits = NBDIGITS;
29 int offset = 1;
30
31 char buffer[BUFFERSIZE] = {0};
32 FILE *fin = NULL;
33 int addrfile = 0;
34 FILE *fout = NULL;
35 char *progname = NULL;
36
37 /* type definitions */
38
39 typedef struct {
40 char *sequence;
41 char bytes[SEQLEN];
42 int length;
43 } sequence_t;
44
45 /* help function */
46
47 int usage (int ret)
48 {
49 FILE *fd = ret ? stderr : stdout;
50 fprintf (fd, "usage: %s [-i file] [-h] [-n nbcols] [-o file] [-v]\n", progname);
51 fprintf (fd, " -i: input file\n");
52 fprintf (fd, " -h: help message\n");
53 fprintf (fd, " -n: number of columns\n");
54 fprintf (fd, " -e: commands\n");
55 fprintf (fd, " -o: output file\n");
56 fprintf (fd, " -v: verbose level (%d)\n", verbose);
57 fprintf (fd, "\n");
58 fprintf (fd, "commands: [/hstr/|0xaddr] [a hstr] [d nb|-] [i hstr] [p nb|-] [s/h1/h2/[g]]\n");
59 fprintf (fd, " 0x: move to address addr\n");
60 fprintf (fd, " //: move to hexa stringi hstr\n");
61 fprintf (fd, " a : append hexa string hstr to current address\n");
62 fprintf (fd, " d : delete nb bytes (- until end file)\n");
63 fprintf (fd, " i : insert hexa string hstr to current address\n");
64 fprintf (fd, " p : print nb bytes (- until end file)\n");
65 fprintf (fd, " s : substitute h1 by h2 (g for globally)\n");
66
67 return ret;
68 }
69
70 /* get number of digits */
71
72 int getnbdigits (long int l) {
73 int n = 0;
74 while (l) {
75 n += 2;
76 l /= 256;
77 }
78 return n;
79 }
80
81 /* print a line */
82
83 void printline (char *buffer, int nb, int addr) {
84 int i;
85
86 printf ("0x%0*x:", nbdigits, addr);
87 for (i = 0; i < nb; i++) {
88 printf (" %02x", buffer[i]);
89 }
90 for (i = nb; i < nbcols; i++) {
91 printf (" ");
92 }
93 printf (" ");
94 for (i = 0; i < nb; i++) {
95 char c = buffer[i];
96 printf ("%c", (c > 31) && (c < 127) ? c : '.');
97 }
98 printf ("\n");
99 }
100
101 /* write file function */
102
103 int writefile (char *pt, int nb) {
104 if (fout) {
105 fwrite (pt, 1, nb, fout);
106 }
107 return 1;
108 }
109
110 /* search sequence function */
111
112 int searchseq (sequence_t *seq) {
113 char *pt = buffer;
114 int nb = 0;
115 int i, j;
116 int valid = 0;
117
118 VERBOSE (DEBUG, printf ("search sequence: %s\n", seq->sequence));
119
120 while (!feof (fin)) {
121 int nbread = fread (pt, 1, BUFFERSIZE - (pt - buffer), fin);
122 nb += nbread;
123 pt = buffer;
124 for (i = 0; i < nb - seq->length; i++) {
125 valid = 1;
126 for (j = 0; (j < seq->length) && (valid); j++) {
127 if (pt[i + j] != seq->bytes[j]) {
128 valid = 0;
129 }
130 }
131 if (valid) {
132 break;
133 }
134 }
135
136 if (!valid) {
137 writefile (buffer, nb - seq->length);
138 offset = 0;
139 addrfile += nb - seq->length;
140 for (i = 0; i < seq->length; i++) {
141 buffer[i] = buffer[nb - seq->length + i];
142 }
143 pt = buffer + seq->length;
144 nb = seq->length;
145 } else {
146 writefile (buffer, i);
147 offset = seq->length;
148 addrfile += i;
149 fseek (fin, i - nb, SEEK_CUR);
150 VERBOSE (DEBUG, printf ("found sequence (%d)\n", i - nb));
151 return 0;
152 }
153 }
154
155 if (!valid) {
156 writefile (buffer, nb);
157 addrfile += seq->length;
158 }
159
160 return 1;
161 }
162
163 /* hexadecimal dump function */
164
165 int hexdump (int len) {
166 char buffer[BUFFERSIZE] = {0};
167 int i;
168
169 char *pt = buffer;
170
171 int nb = 0;
172 while (!feof (fin)) {
173 int nbtoread = BUFFERSIZE - (pt - buffer);
174 if ((len > 0) && (nbtoread > len)) {
175 nbtoread = len;
176 }
177 int nbread = fread (pt, 1, nbtoread, fin);
178 if (len > 0) {
179 len -= nbread;
180 }
181 nb += nbread;
182 pt = buffer;
183
184 /* print line */
185 while ((nb - (int)(pt - buffer)) / nbcols > 0) {
186 printline (pt, nbcols, addrfile);
187 writefile (pt, nbcols);
188 addrfile += nbcols;
189 pt += nbcols;
190 }
191
192 /* copy end buffer */
193 nb -= pt - buffer;
194 for (i = 0; i < nb; i++) {
195 buffer[i] = pt[i];
196 }
197 pt = buffer + nb;
198
199 /* end partial reading */
200 if (len == 0) {
201 break;
202 }
203 }
204
205 /* last line */
206 if (nb > 0) {
207 printline (buffer, nb, addrfile);
208 writefile (buffer, nb);
209 addrfile += nb;
210 }
211
212 return 0;
213 }
214
215 /* parse octal string */
216
217 long int octal (char *s, int n) {
218 int i;
219 long int l = 0;
220 for (i = 0; i < n; i++) {
221 if ((s[i] >= '0') && (s[i] <= '9')) {
222 l = l * 8 + s[i] - '0';
223 } else {
224 return -1;
225 }
226 }
227 return l;
228 }
229
230 /* parse hexa string */
231
232 long int hexa (char *s, int n) {
233 int i;
234 long int l = 0;
235 for (i = 0; i < n; i++) {
236 l *= 16;
237 if ((s[i] >= '0') && (s[i] <= '9')) {
238 l += s[i] - '0';
239 } else if ((s[i] >= 'A') && (s[i] <= 'F')) {
240 l += s[i] + 10 - 'A';
241 } else if ((s[i] >= 'a') && (s[i] <= 'f')) {
242 l += s[i] + 10 - 'a';
243 } else {
244 return -1;
245 }
246 }
247 return l;
248 }
249
250 /* special character function */
251
252 int specialchar (char *s, char *b) {
253 int i = 0, j = 0;
254 while (s[i] != 0) {
255 if (j == SEQLEN) {
256 return 0;
257 }
258 if (s[i] != '\\') {
259 b[j++] = s[i++];
260 continue;
261 }
262
263 int l = -1;
264 switch (s[i + 1]) {
265 case 'a': l = 0x07; i += 2; break;
266 case 'b': l = 0x08; i += 2; break;
267 case 'e': l = 0x1b; i += 2; break;
268 case 'f': l = 0x0c; i += 2; break;
269 case 'n': l = 0x0a; i += 2; break;
270 case 'r': l = 0x0d; i += 2; break;
271 case 't': l = 0x09; i += 2; break;
272 case 'v': l = 0x0b; i += 2; break;
273 case '/': l = '/'; i += 2; break;
274 case '\\': l = '\\'; i += 2; break;
275 case '\'': l = '\''; i += 2; break;
276 case '"': l = '"'; i += 2; break;
277 case '0':
278 case '1':
279 case '2':
280 case '3':
281 l = octal (s + i + 1, 3);
282 if (l != -1) {
283 i += 4;
284 }
285 break;
286 case 'x':
287 l = hexa (s + i + 2, 2);
288 if (l != -1) {
289 i += 4;
290 }
291 break;
292 default:
293 }
294 if (l != -1) {
295 VERBOSE (DEBUG, printf("l: 0x%02x '%c'\n", l, l));
296 }
297 b[j++] = (l != -1) ? l : s[i++];
298 }
299
300 return j;
301 }
302
303 /* main function */
304
305 int main (int argc, char *argv[])
306 {
307 int rc = 0;
308 char *input = NULL;
309 char *output = NULL;
310 char *commands = NULL;
311 int printlen = -1;
312 sequence_t seq = {0};
313 char *addr = NULL;
314
315 /* get basename */
316 char *pt = progname = argv[0];
317 while (*pt) {
318 if ((*pt == '/') || (*pt == '\\')) {
319 progname = pt + 1;
320 }
321 pt++;
322 }
323
324 while (argc-- > 1) {
325 char *arg = *(++argv);
326 if (arg[0] != '-') {
327 VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- %s\n", progname, arg));
328 return usage (1);
329 }
330 char c = arg[1];
331 switch (c) {
332 case 'e':
333 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
334 if (arg) {
335 if (commands == NULL) {
336 commands = arg;
337 } else {
338 strcat (commands, " ");
339 strcat (commands, arg);
340 }
341 }
342 break;
343 case 'i':
344 input = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
345 break;
346 case 'n':
347 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
348 if (arg == NULL) {
349 VERBOSE (ERROR, fprintf (stderr, "%s: missing number of columns\n", progname));
350 return usage (1);
351 }
352 nbcols = atoi (arg);
353 break;
354 case 'o':
355 output = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
356 break;
357 case 'v':
358 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
359 if (arg == NULL) {
360 VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname));
361 return usage (1);
362 }
363 verbose = atoi (arg);
364 break;
365 case 'h':
366 default:
367 return usage (c != 'h');
368 }
369 }
370
371 /* check input */
372 if (input) {
373 fin = fopen (input, "rb");
374 if (!fin) {
375 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
376 return 1;
377 }
378 } else {
379 fin = stdin;
380 }
381
382 /* check output */
383 if (output) {
384 fout = fopen (output, "wb");
385 if (!fout) {
386 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
387 fclose (fin);
388 return 1;
389 }
390 } else {
391 //fout = stdout;
392 }
393
394 /* get file size */
395 if (fin != stdin) {
396 fseek (fin, 0 , SEEK_END);
397 long int filesize = ftell (fin);
398 fseek (fin, 0 , SEEK_SET);
399 nbdigits = getnbdigits (filesize);
400 }
401
402 if (commands == NULL) {
403 hexdump (-1);
404 } else {
405 VERBOSE (DEBUG, printf ("commands: %s\n", commands));
406 while ((*commands != '\0') && (rc == 0)) {
407 switch (*commands++) {
408 case ' ':
409 case '\t':
410 break;
411
412 case '/': /* read patern */
413 seq.sequence = commands;
414 while (*commands) {
415 if ((*commands == '\\') &&
416 ((commands[1] == '/') || (commands[1] == '\\'))) {
417 commands++;
418 } else if (*commands == '/') {
419 *commands++ = 0;
420 break;
421 }
422 commands++;
423 }
424 seq.length = specialchar (seq.sequence, seq.bytes);
425 if (seq.length != 0) {
426 rc = searchseq (&seq);
427 } else {
428 VERBOSE (ERROR, fprintf (stderr, "incorrect sequence (%s)\n", seq.sequence));
429 rc = 1;
430 }
431 break;
432
433 case '0': /* read address */
434 break;
435
436 case 'a': /* append mode */
437 break;
438
439 case 'd': /* delete mode */
440 break;
441
442 case 'i': /* insert mode */
443 break;
444
445 case 'p': /* print mode */
446 printlen = -1;
447 while (*commands != '\0') {
448 if ((*commands == ' ') || (*commands == '\t')) {
449 commands++;
450 } else if ((*commands >= '0') && (*commands <= '9')) {
451 printlen = strtol (commands, &commands, 10);
452 break;
453 } else if (*commands == '-') {
454 printlen = -1;
455 commands++;
456 break;
457 } else {
458 VERBOSE (ERROR, fprintf (stderr, "unknown print length (%s)\n", commands));
459 rc = 1;
460 break;
461 }
462 }
463 if (rc == 0) {
464 hexdump (printlen);
465 }
466 break;
467
468 case 's': /* substitute mode */
469 break;
470
471 default:
472 VERBOSE (ERROR, fprintf (stderr, "unknown command (%c)\n", commands[-1]));
473 rc = 1;
474 }
475 }
476 }
477
478 /* end of file */
479 if ((rc == 0) && (fout != NULL)) {
480 while (!feof (fin)) {
481 int nbread = fread (buffer, 1, BUFFERSIZE, fin);
482 if (nbread) {
483 fwrite (buffer, 1, nbread, fout);
484 }
485 }
486 }
487
488 /* close all */
489 if (fin) fclose (fin);
490 if (fout) fclose (fout);
491
492 return rc;
493 }
494
495 // test: hexdump.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
496 // test: hexdump.exe foo 2>&1 | grep -q 'invalid option'
497 // test: hexdump.exe -n 2>&1 | grep -q 'missing number of columns'
498 // test: hexdump.exe -v 2>&1 | grep -q 'missing verbose level'
499 // test: hexdump.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
500 // test: hexdump.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
501 // test: hexdump.exe -i hexdump.c | grep -q '0x[0-9a-f]*: '
502 // test: hexdump.exe -i hexdump.ko 2>&1 | grep -q "can't open file"
503 // test: hexdump.exe -i hexdump.c -o ko/test.c 2>&1 | grep -q "can't open file"
504 // test: cat hexdump.c | hexdump.exe -n 3 | head -2 | tail -1 | grep -q '0x000003: 64 65 70 dep'
505 // test: hexdump.exe -i hexdump.c -n 3 | head -2 | tail -1 | grep -q '0x0003: 64 65 70 dep'
506 // test: hexdump.exe -i hexdump.c -o test.c -e 'p 200' | tail -1 | grep -q '0x00c0:'
507 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
508 // test: hexdump.exe -i hexdump.c -e ' /cflags/ p 17 /debug/ p 8' | grep -q '0x0019: 2a 2f 0a 2f 2a 20 6c 69 \*\/\./\* li'
509 // test: hexdump.exe -i hexdump.c -o test.c -e ' /cfl\x61gs/ p 16 /d\145bug/ p 8' | grep -q '0x0027: 64 65 62 75 67 2e 6f 20 debug.o'
510 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
511 // test: hexdump.exe -i hexdump.c -e ' /\n/ p 8' | grep -q '0x000d: 0a 2f 2a 20 63 66 6c 61 \./\* cfla'
512 // test: hexdump.exe -i hexdump.c -o test.c -e ' /\a\b\e\f\r\t\v/ p 8'; x=$?; test x$x = x1
513 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
514 // test: hexdump.exe -i hexdump.c -v 3 -e " /\'/" -e ' /\"/' -e ' /\\/' -e ' /\x2a/' -e ' s/\x3A/' | grep l: | wc -l | xargs test 5 =
515 // test: hexdump.exe -i hexdump.c -e ' /\n\/* vim:/ p -' | grep -q ': 74 3a 20 2a 2f 0a *t: \*\/\.'
516 // test: hexdump.exe -i hexdump.c -e 'p go_to_end' 2>&1 | grep -q 'unknown print length'
517 // test: hexdump.exe -i hexdump.c -e '//' 2>&1 | grep -q 'incorrect sequence'
518 // test: hexdump.exe -i hexdump.c -e 'foo' 2>&1 | grep -q 'unknown command'
519
520 /* vim: set ts=4 sw=4 et: */