cc278231aee0940521c7caf3de26726b9c5b0b63
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))
19 //#define BUFFERSIZE 4096
20 #define BUFFERSIZE 256
28 int nbdigits
= NBDIGITS
;
31 char buffer
[BUFFERSIZE
] = {0};
33 unsigned long int addrfile
= 0;
35 char *progname
= NULL
;
37 /* type definitions */
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
);
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");
70 /* get number of digits */
72 unsigned int getnbdigits (unsigned long int l
) {
83 void printline (char *buffer
, int nb
, int addr
) {
86 printf ("0x%0*x:", nbdigits
, addr
);
87 for (i
= 0; i
< nb
; i
++) {
88 printf (" %02x", buffer
[i
]);
90 for (i
= nb
; i
< nbcols
; i
++) {
94 for (i
= 0; i
< nb
; i
++) {
96 printf ("%c", (c
> 31) && (c
< 127) ? c
: '.');
101 /* write file function */
103 int writefile (char *pt
, int nb
) {
105 fwrite (pt
, 1, nb
, fout
);
110 /* search sequence function */
112 int searchseq (sequence_t
*seq
) {
118 VERBOSE (DEBUG
, printf ("search sequence: %s\n", seq
->sequence
));
120 while (!feof (fin
)) {
121 int nbread
= fread (pt
, 1, BUFFERSIZE
- (pt
- buffer
), fin
);
124 for (i
= 0; i
< nb
- seq
->length
; i
++) {
126 for (j
= 0; (j
< seq
->length
) && (valid
); j
++) {
127 if (pt
[i
+ j
] != seq
->bytes
[j
]) {
137 writefile (buffer
, nb
- seq
->length
);
139 addrfile
+= nb
- seq
->length
;
140 for (i
= 0; i
< seq
->length
; i
++) {
141 buffer
[i
] = buffer
[nb
- seq
->length
+ i
];
143 pt
= buffer
+ seq
->length
;
146 writefile (buffer
, i
);
147 offset
= seq
->length
;
149 fseek (fin
, i
- nb
, SEEK_CUR
);
150 VERBOSE (DEBUG
, printf ("found sequence (%d)\n", i
- nb
));
156 writefile (buffer
, nb
);
157 addrfile
+= seq
->length
;
163 /* go to address function */
165 int gotoaddr (unsigned long int addr
) {
166 char buffer
[BUFFERSIZE
] = {0};
168 if (addrfile
> addr
) {
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
);
178 if (addrfile
== addr
) {
186 /* insert sequence function */
188 int insertseq (sequence_t
*seq
) {
189 char buffer
[BUFFERSIZE
] = {0};
191 VERBOSE (DEBUG
, printf ("insert (%d): '%s'\n", offset
, seq
->sequence
);
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
: '.');
199 int nbread
= fread (buffer
, 1, offset
, fin
);
200 if (nbread
!= offset
) {
203 writefile (buffer
, offset
);
206 writefile (seq
->bytes
, seq
->length
);
211 /* hexadecimal dump function */
213 int hexdump (int len
) {
214 char buffer
[BUFFERSIZE
] = {0};
220 while (!feof (fin
)) {
221 int nbtoread
= BUFFERSIZE
- (pt
- buffer
);
222 if ((len
> 0) && (nbtoread
> len
)) {
225 int nbread
= fread (pt
, 1, nbtoread
, fin
);
233 while ((nb
- (int)(pt
- buffer
)) / nbcols
> 0) {
234 printline (pt
, nbcols
, addrfile
);
235 writefile (pt
, nbcols
);
240 /* copy end buffer */
242 for (i
= 0; i
< nb
; i
++) {
247 /* end partial reading */
255 printline (buffer
, nb
, addrfile
);
256 writefile (buffer
, nb
);
263 /* parse octal string */
265 long int octal (char *s
, int n
) {
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';
278 /* parse hexa string */
280 long int hexa (char *s
, int n
) {
282 unsigned long int l
= 0;
283 for (i
= 0; i
< n
; i
++) {
285 if ((s
[i
] >= '0') && (s
[i
] <= '9')) {
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';
298 /* special character function */
300 int specialchar (char *s
, char *b
) {
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;
329 l
= octal (s
+ i
+ 1, 3);
335 l
= hexa (s
+ i
+ 2, 2);
344 VERBOSE (DEBUG
, printf("l: 0x%02x '%c'\n", l
, l
));
346 b
[j
++] = (l
!= -1) ? l
: s
[i
++];
354 int main (int argc
, char *argv
[])
359 char *commands
= NULL
;
361 sequence_t seq
= {0};
362 unsigned long int addr
= 0;
366 char *pt
= progname
= argv
[0];
368 if ((*pt
== '/') || (*pt
== '\\')) {
375 char *arg
= *(++argv
);
377 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid option -- %s\n", progname
, arg
));
383 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
385 if (commands
== NULL
) {
388 strcat (commands
, " ");
389 strcat (commands
, arg
);
394 input
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
397 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
399 VERBOSE (ERROR
, fprintf (stderr
, "%s: missing number of columns\n", progname
));
405 output
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
408 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
410 VERBOSE (ERROR
, fprintf (stderr
, "%s: missing verbose level\n", progname
));
413 verbose
= atoi (arg
);
417 return usage (c
!= 'h');
423 fin
= fopen (input
, "rb");
425 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", input
));
434 fout
= fopen (output
, "wb");
436 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", output
));
446 fseek (fin
, 0 , SEEK_END
);
447 unsigned long int filesize
= ftell (fin
);
448 fseek (fin
, 0 , SEEK_SET
);
449 nbdigits
= getnbdigits (filesize
);
452 if (commands
== NULL
) {
453 VERBOSE (DEBUG
, printf ("no command\n"));
456 VERBOSE (DEBUG
, printf ("commands: %s\n", commands
));
457 while ((*commands
!= '\0') && (rc
== 0)) {
458 switch (c
= *commands
++) {
463 case '/': /* read patern */
464 seq
.sequence
= commands
;
467 if ((*commands
== '\\') &&
468 ((commands
[1] == '/') || (commands
[1] == '\\'))) {
470 } else if (*commands
== '/') {
476 seq
.length
= specialchar (seq
.sequence
, seq
.bytes
);
477 if (seq
.length
!= 0) {
478 rc
= searchseq (&seq
);
480 VERBOSE (ERROR
, fprintf (stderr
, "incorrect sequence (%s)\n", seq
.sequence
));
485 case '0': /* read address */
486 if (*commands
== 'x') {
488 addr
= strtol (commands
, &commands
, 16);
490 addr
= strtol (commands
, &commands
, 8);
493 rc
= gotoaddr (addr
);
495 VERBOSE (ERROR
, fprintf (stderr
, "erroneous address\n"));
508 case '9': /* read address */
510 addr
= strtol (commands
, &commands
, 10);
511 rc
= gotoaddr (addr
);
514 case 'a': /* append mode */
518 case 'i': /* insert mode */
520 if ((*commands
== ' ') || (*commands
== '\t')) {
526 seq
.sequence
= commands
;
530 if ((*commands
== ' ') || (*commands
== '\t')) {
537 seq
.bytes
[seq
.length
] = hexa (seq
.sequence
+ 2 * seq
.length
, 2);
538 if (seq
.bytes
[seq
.length
] == -1) {
546 if ((seq
.length
> 0) && (rc
== 0) && (i
% 2 == 0)) {
547 rc
= insertseq (&seq
);
549 VERBOSE (ERROR
, fprintf (stderr
, "erroneous sequence '%s'\n", seq
.sequence
));
554 case 'd': /* delete mode */
557 case 'p': /* print mode */
559 while (*commands
!= '\0') {
560 if ((*commands
== ' ') || (*commands
== '\t')) {
562 } else if ((*commands
>= '0') && (*commands
<= '9')) {
563 length
= strtol (commands
, &commands
, 10);
565 } else if (*commands
== '-') {
570 VERBOSE (ERROR
, fprintf (stderr
, "unknown length (%s)\n", commands
));
578 fseek (fin
, length
, SEEK_CUR
);
587 case 's': /* substitute mode */
591 VERBOSE (ERROR
, fprintf (stderr
, "unknown command (%c)\n", commands
[-1]));
598 if ((rc
== 0) && (fout
!= NULL
)) {
599 while (!feof (fin
)) {
600 int nbread
= fread (buffer
, 1, BUFFERSIZE
, fin
);
602 fwrite (buffer
, 1, nbread
, fout
);
608 if (fin
) fclose (fin
);
609 if (fout
) fclose (fout
);
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
646 /* vim: set ts=4 sw=4 et: */