cc278231aee0940521c7caf3de26726b9c5b0b63
[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 = 0;
30
31 char buffer[BUFFERSIZE] = {0};
32 FILE *fin = NULL;
33 unsigned long 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/|addr] [a hstr] [d nb|-] [i hstr] [p nb|-] [s/h1/h2/[g]]\n");
59 fprintf (fd, " addr: move to address (0... octal, [1-9]... deci, 0x... hexa)\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 unsigned int getnbdigits (unsigned 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 /* go to address function */
164
165 int gotoaddr (unsigned long int addr) {
166 char buffer[BUFFERSIZE] = {0};
167
168 if (addrfile > addr) {
169 return 1;
170 }
171
172 VERBOSE (DEBUG, printf ("look for address: 0x%04lx\n", addr));
173 while (!feof (fin)) {
174 int nbtoread = (addrfile + BUFFERSIZE > addr) ? addr - addrfile : BUFFERSIZE;
175 int nbread = fread (buffer, 1, nbtoread, fin);
176 writefile (buffer, nbread);
177 addrfile += nbread;
178 if (addrfile == addr) {
179 return 0;
180 }
181 }
182
183 return 1;
184 }
185
186 /* insert sequence function */
187
188 int insertseq (sequence_t *seq) {
189 char buffer[BUFFERSIZE] = {0};
190
191 VERBOSE (DEBUG, printf ("insert (%d): '%s'\n", offset, seq->sequence);
192 int i;
193 for (i = 0; i < seq->length; i++) {
194 char c = seq->bytes[i];
195 printf (" 0x%02x (%c)", c, ((c >= 32) && (c < 127)) ? c : '.');
196 };
197 printf ("\n"));
198 if (offset > 0) {
199 int nbread = fread (buffer, 1, offset, fin);
200 if (nbread != offset) {
201 return 1;
202 }
203 writefile (buffer, offset);
204 offset = 0;
205 }
206 writefile (seq->bytes, seq->length);
207
208 return 0;
209 }
210
211 /* hexadecimal dump function */
212
213 int hexdump (int len) {
214 char buffer[BUFFERSIZE] = {0};
215 int i;
216
217 char *pt = buffer;
218
219 int nb = 0;
220 while (!feof (fin)) {
221 int nbtoread = BUFFERSIZE - (pt - buffer);
222 if ((len > 0) && (nbtoread > len)) {
223 nbtoread = len;
224 }
225 int nbread = fread (pt, 1, nbtoread, fin);
226 if (len > 0) {
227 len -= nbread;
228 }
229 nb += nbread;
230 pt = buffer;
231
232 /* print line */
233 while ((nb - (int)(pt - buffer)) / nbcols > 0) {
234 printline (pt, nbcols, addrfile);
235 writefile (pt, nbcols);
236 addrfile += nbcols;
237 pt += nbcols;
238 }
239
240 /* copy end buffer */
241 nb -= pt - buffer;
242 for (i = 0; i < nb; i++) {
243 buffer[i] = pt[i];
244 }
245 pt = buffer + nb;
246
247 /* end partial reading */
248 if (len == 0) {
249 break;
250 }
251 }
252
253 /* last line */
254 if (nb > 0) {
255 printline (buffer, nb, addrfile);
256 writefile (buffer, nb);
257 addrfile += nb;
258 }
259
260 return 0;
261 }
262
263 /* parse octal string */
264
265 long int octal (char *s, int n) {
266 int i;
267 unsigned long int l = 0;
268 for (i = 0; i < n; i++) {
269 if ((s[i] >= '0') && (s[i] <= '9')) {
270 l = l * 8 + s[i] - '0';
271 } else {
272 return -1;
273 }
274 }
275 return l;
276 }
277
278 /* parse hexa string */
279
280 long int hexa (char *s, int n) {
281 int i;
282 unsigned long int l = 0;
283 for (i = 0; i < n; i++) {
284 l *= 16;
285 if ((s[i] >= '0') && (s[i] <= '9')) {
286 l += s[i] - '0';
287 } else if ((s[i] >= 'A') && (s[i] <= 'F')) {
288 l += s[i] + 10 - 'A';
289 } else if ((s[i] >= 'a') && (s[i] <= 'f')) {
290 l += s[i] + 10 - 'a';
291 } else {
292 return -1;
293 }
294 }
295 return l;
296 }
297
298 /* special character function */
299
300 int specialchar (char *s, char *b) {
301 int i = 0, j = 0;
302 while (s[i] != 0) {
303 if (j == SEQLEN) {
304 return 0;
305 }
306 if (s[i] != '\\') {
307 b[j++] = s[i++];
308 continue;
309 }
310
311 int l = -1;
312 switch (s[i + 1]) {
313 case 'a': l = 0x07; i += 2; break;
314 case 'b': l = 0x08; i += 2; break;
315 case 'e': l = 0x1b; i += 2; break;
316 case 'f': l = 0x0c; i += 2; break;
317 case 'n': l = 0x0a; i += 2; break;
318 case 'r': l = 0x0d; i += 2; break;
319 case 't': l = 0x09; i += 2; break;
320 case 'v': l = 0x0b; i += 2; break;
321 case '/': l = '/'; i += 2; break;
322 case '\\': l = '\\'; i += 2; break;
323 case '\'': l = '\''; i += 2; break;
324 case '"': l = '"'; i += 2; break;
325 case '0':
326 case '1':
327 case '2':
328 case '3':
329 l = octal (s + i + 1, 3);
330 if (l != -1) {
331 i += 4;
332 }
333 break;
334 case 'x':
335 l = hexa (s + i + 2, 2);
336 if (l != -1) {
337 i += 4;
338 }
339 break;
340 default:
341 break;
342 }
343 if (l != -1) {
344 VERBOSE (DEBUG, printf("l: 0x%02x '%c'\n", l, l));
345 }
346 b[j++] = (l != -1) ? l : s[i++];
347 }
348
349 return j;
350 }
351
352 /* main function */
353
354 int main (int argc, char *argv[])
355 {
356 int i, rc = 0;
357 char *input = NULL;
358 char *output = NULL;
359 char *commands = NULL;
360 int length = -1;
361 sequence_t seq = {0};
362 unsigned long int addr = 0;
363 char c;
364
365 /* get basename */
366 char *pt = progname = argv[0];
367 while (*pt) {
368 if ((*pt == '/') || (*pt == '\\')) {
369 progname = pt + 1;
370 }
371 pt++;
372 }
373
374 while (argc-- > 1) {
375 char *arg = *(++argv);
376 if (arg[0] != '-') {
377 VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- %s\n", progname, arg));
378 return usage (1);
379 }
380 char c = arg[1];
381 switch (c) {
382 case 'e':
383 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
384 if (arg) {
385 if (commands == NULL) {
386 commands = arg;
387 } else {
388 strcat (commands, " ");
389 strcat (commands, arg);
390 }
391 }
392 break;
393 case 'i':
394 input = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
395 break;
396 case 'n':
397 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
398 if (arg == NULL) {
399 VERBOSE (ERROR, fprintf (stderr, "%s: missing number of columns\n", progname));
400 return usage (1);
401 }
402 nbcols = atoi (arg);
403 break;
404 case 'o':
405 output = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
406 break;
407 case 'v':
408 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
409 if (arg == NULL) {
410 VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname));
411 return usage (1);
412 }
413 verbose = atoi (arg);
414 break;
415 case 'h':
416 default:
417 return usage (c != 'h');
418 }
419 }
420
421 /* check input */
422 if (input) {
423 fin = fopen (input, "rb");
424 if (!fin) {
425 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
426 return 1;
427 }
428 } else {
429 fin = stdin;
430 }
431
432 /* check output */
433 if (output) {
434 fout = fopen (output, "wb");
435 if (!fout) {
436 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
437 fclose (fin);
438 return 1;
439 }
440 } else {
441 //fout = stdout;
442 }
443
444 /* get file size */
445 if (fin != stdin) {
446 fseek (fin, 0 , SEEK_END);
447 unsigned long int filesize = ftell (fin);
448 fseek (fin, 0 , SEEK_SET);
449 nbdigits = getnbdigits (filesize);
450 }
451
452 if (commands == NULL) {
453 VERBOSE (DEBUG, printf ("no command\n"));
454 hexdump (-1);
455 } else {
456 VERBOSE (DEBUG, printf ("commands: %s\n", commands));
457 while ((*commands != '\0') && (rc == 0)) {
458 switch (c = *commands++) {
459 case ' ':
460 case '\t':
461 break;
462
463 case '/': /* read patern */
464 seq.sequence = commands;
465 seq.length = 0;
466 while (*commands) {
467 if ((*commands == '\\') &&
468 ((commands[1] == '/') || (commands[1] == '\\'))) {
469 commands++;
470 } else if (*commands == '/') {
471 *commands++ = 0;
472 break;
473 }
474 commands++;
475 }
476 seq.length = specialchar (seq.sequence, seq.bytes);
477 if (seq.length != 0) {
478 rc = searchseq (&seq);
479 } else {
480 VERBOSE (ERROR, fprintf (stderr, "incorrect sequence (%s)\n", seq.sequence));
481 rc = 1;
482 }
483 break;
484
485 case '0': /* read address */
486 if (*commands == 'x') {
487 commands++;
488 addr = strtol (commands, &commands, 16);
489 } else {
490 addr = strtol (commands, &commands, 8);
491 }
492 if (addr) {
493 rc = gotoaddr (addr);
494 } else {
495 VERBOSE (ERROR, fprintf (stderr, "erroneous address\n"));
496 rc = 1;
497 }
498 break;
499
500 case '1':
501 case '2':
502 case '3':
503 case '4':
504 case '5':
505 case '6':
506 case '7':
507 case '8':
508 case '9': /* read address */
509 commands--;
510 addr = strtol (commands, &commands, 10);
511 rc = gotoaddr (addr);
512 break;
513
514 case 'a': /* append mode */
515 offset = 0;
516 /* fall through */
517
518 case 'i': /* insert mode */
519 while (*commands) {
520 if ((*commands == ' ') || (*commands == '\t')) {
521 commands++;
522 } else {
523 break;
524 }
525 }
526 seq.sequence = commands;
527 seq.length = 0;
528 i = 0;
529 while (*commands) {
530 if ((*commands == ' ') || (*commands == '\t')) {
531 *commands++ = '\0';
532 break;
533 } else {
534 commands++;
535 i++;
536 if (i % 2 == 0) {
537 seq.bytes[seq.length] = hexa (seq.sequence + 2 * seq.length, 2);
538 if (seq.bytes[seq.length] == -1) {
539 rc = 1;
540 break;
541 }
542 seq.length++;
543 }
544 }
545 }
546 if ((seq.length > 0) && (rc == 0) && (i % 2 == 0)) {
547 rc = insertseq (&seq);
548 } else {
549 VERBOSE (ERROR, fprintf (stderr, "erroneous sequence '%s'\n", seq.sequence));
550 rc = 1;
551 }
552 break;
553
554 case 'd': /* delete mode */
555 /* fall through */
556
557 case 'p': /* print mode */
558 length = -1;
559 while (*commands != '\0') {
560 if ((*commands == ' ') || (*commands == '\t')) {
561 commands++;
562 } else if ((*commands >= '0') && (*commands <= '9')) {
563 length = strtol (commands, &commands, 10);
564 break;
565 } else if (*commands == '-') {
566 length = -1;
567 commands++;
568 break;
569 } else {
570 VERBOSE (ERROR, fprintf (stderr, "unknown length (%s)\n", commands));
571 rc = 1;
572 break;
573 }
574 }
575 if (rc == 0) {
576 switch (c) {
577 case 'd':
578 fseek (fin, length, SEEK_CUR);
579 break;
580 case 'p':
581 hexdump (length);
582 break;
583 }
584 }
585 break;
586
587 case 's': /* substitute mode */
588 break;
589
590 default:
591 VERBOSE (ERROR, fprintf (stderr, "unknown command (%c)\n", commands[-1]));
592 rc = 1;
593 }
594 }
595 }
596
597 /* end of file */
598 if ((rc == 0) && (fout != NULL)) {
599 while (!feof (fin)) {
600 int nbread = fread (buffer, 1, BUFFERSIZE, fin);
601 if (nbread) {
602 fwrite (buffer, 1, nbread, fout);
603 }
604 }
605 }
606
607 /* close all */
608 if (fin) fclose (fin);
609 if (fout) fclose (fout);
610
611 return rc;
612 }
613
614 // test: hexdump.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
615 // test: hexdump.exe foo 2>&1 | grep -q 'invalid option'
616 // test: hexdump.exe -n 2>&1 | grep -q 'missing number of columns'
617 // test: hexdump.exe -v 2>&1 | grep -q 'missing verbose level'
618 // test: hexdump.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
619 // test: hexdump.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
620 // test: hexdump.exe -i hexdump.c | grep -q '0x[0-9a-f]*: '
621 // test: hexdump.exe -i hexdump.ko 2>&1 | grep -q "can't open file"
622 // test: hexdump.exe -i hexdump.c -o ko/test.c 2>&1 | grep -q "can't open file"
623 // test: cat hexdump.c | hexdump.exe -n 3 | head -2 | tail -1 | grep -q '0x000003: 64 65 70 dep'
624 // test: hexdump.exe -i hexdump.c -n 3 | head -2 | tail -1 | grep -q '0x0003: 64 65 70 dep'
625 // test: hexdump.exe -i hexdump.c -o test.c -e 'p 200' | tail -1 | grep -q '0x00c0:'
626 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
627 // 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'
628 // 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'
629 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
630 // test: hexdump.exe -i hexdump.c -e ' /\n/ p 8' | grep -q '0x000d: 0a 2f 2a 20 63 66 6c 61 \./\* cfla'
631 // test: hexdump.exe -i hexdump.c -o test.c -e ' /\a\b\e\f\r\t\v/ p 8'; x=$?; test x$x = x1
632 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
633 // test: hexdump.exe -i hexdump.c -v 3 -e " /\'/" -e ' /\"/' -e ' /\\/' -e ' /\x2a/' -e ' s/\x3A/' | grep l: | wc -l | xargs test 5 =
634 // test: hexdump.exe -i hexdump.c -e ' /\n\/* vim:/ p -' | grep -q ': 74 3a 20 2a 2f 0a *t: \*\/\.'
635 // test: hexdump.exe -i hexdump.c -e 'p go_to_end' 2>&1 | grep -q 'unknown length'
636 // test: hexdump.exe -i hexdump.c -e ' //' 2>&1 | grep -q 'incorrect sequence'
637 // test: hexdump.exe -i hexdump.c -e 'foo' 2>&1 | grep -q 'unknown command'
638 // test: hexdump.exe -i hexdump.c -e '0x20 p 8 64 p 8 0200 p 16' | grep -q '0x0080:'
639 // test: hexdump.exe -i hexdump.c -e '0xg' 2>&1 | grep -q 'erroneous address'
640 // test: hexdump.exe -i hexdump.c -o test.c -e ' /cflags/ a 414e5a /link/ i 2F333B'
641 // test: grep -q ANZcflags test.c && grep -q 'link/3;er' test.c; x=$?; rm test.c; test x$x = x0
642 // test: hexdump.exe -i hexdump.c -e ' /cflags/ a 414e5' 2>&1 | grep 'erroneous sequence'
643 // test: hexdump.exe -i hexdump.c -o test.c -e ' /lags/ d 2'
644 // test: grep -q cfgs test.c; x=$?; rm test.c; test x$x = x0
645
646 /* vim: set ts=4 sw=4 et: */