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};
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/|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");
70 /* get number of digits */
72 int getnbdigits (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 /* hexadecimal dump function */
165 int hexdump (int len
) {
166 char buffer
[BUFFERSIZE
] = {0};
172 while (!feof (fin
)) {
173 int nbtoread
= BUFFERSIZE
- (pt
- buffer
);
174 if ((len
> 0) && (nbtoread
> len
)) {
177 int nbread
= fread (pt
, 1, nbtoread
, fin
);
185 while ((nb
- (int)(pt
- buffer
)) / nbcols
> 0) {
186 printline (pt
, nbcols
, addrfile
);
187 writefile (pt
, nbcols
);
192 /* copy end buffer */
194 for (i
= 0; i
< nb
; i
++) {
199 /* end partial reading */
207 printline (buffer
, nb
, addrfile
);
208 writefile (buffer
, nb
);
215 /* parse octal string */
217 long int octal (char *s
, int n
) {
220 for (i
= 0; i
< n
; i
++) {
221 if ((s
[i
] >= '0') && (s
[i
] <= '9')) {
222 l
= l
* 8 + s
[i
] - '0';
230 /* parse hexa string */
232 long int hexa (char *s
, int n
) {
235 for (i
= 0; i
< n
; i
++) {
237 if ((s
[i
] >= '0') && (s
[i
] <= '9')) {
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';
250 /* special character function */
252 int specialchar (char *s
, char *b
) {
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;
280 l
= octal (s
+ i
+ 1, 3);
286 l
= hexa (s
+ i
+ 2, 2);
294 VERBOSE (DEBUG
, printf("l: 0x%02x '%c'\n", l
, l
));
296 b
[j
++] = (l
!= -1) ? l
: s
[i
++];
304 int main (int argc
, char *argv
[])
309 char *commands
= NULL
;
311 sequence_t seq
= {0};
315 char *pt
= progname
= argv
[0];
317 if ((*pt
== '/') || (*pt
== '\\')) {
324 char *arg
= *(++argv
);
326 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid option -- %s\n", progname
, arg
));
332 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
334 if (commands
== NULL
) {
337 strcat (commands
, " ");
338 strcat (commands
, arg
);
343 input
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
346 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
348 VERBOSE (ERROR
, fprintf (stderr
, "%s: missing number of columns\n", progname
));
354 output
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
357 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
359 VERBOSE (ERROR
, fprintf (stderr
, "%s: missing verbose level\n", progname
));
362 verbose
= atoi (arg
);
366 return usage (c
!= 'h');
372 fin
= fopen (input
, "rb");
374 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", input
));
383 fout
= fopen (output
, "wb");
385 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", output
));
395 fseek (fin
, 0 , SEEK_END
);
396 long int filesize
= ftell (fin
);
397 fseek (fin
, 0 , SEEK_SET
);
398 nbdigits
= getnbdigits (filesize
);
401 if (commands
== NULL
) {
404 VERBOSE (DEBUG
, printf ("commands: %s\n", commands
));
405 while ((*commands
!= '\0') && (rc
== 0)) {
406 switch (*commands
++) {
411 case '/': /* read patern */
412 seq
.sequence
= commands
;
414 if (*commands
== '/') {
420 seq
.length
= specialchar (seq
.sequence
, seq
.bytes
);
421 if (seq
.length
!= 0) {
422 rc
= searchseq (&seq
);
424 VERBOSE (ERROR
, fprintf (stderr
, "incorrect sequence (%s)\n", seq
.sequence
));
429 case '0': /* read address */
432 case 'a': /* append mode */
435 case 'd': /* delete mode */
438 case 'i': /* insert mode */
441 case 'p': /* print mode */
443 while (*commands
!= '\0') {
444 if ((*commands
== ' ') || (*commands
== '\t')) {
446 } else if ((*commands
>= '0') && (*commands
<= '9')) {
447 printlen
= strtol (commands
, &commands
, 10);
449 } else if (*commands
== '-') {
454 VERBOSE (ERROR
, fprintf (stderr
, "unkown print length (%s)\n", commands
));
464 case 's': /* substitute mode */
468 VERBOSE (ERROR
, fprintf (stderr
, "unknown command (%c)\n", commands
[-1]));
475 if ((rc
== 0) && (fout
!= NULL
)) {
476 while (!feof (fin
)) {
477 int nbread
= fread (buffer
, 1, BUFFERSIZE
, fin
);
479 fwrite (buffer
, 1, nbread
, fout
);
485 if (fin
) fclose (fin
);
486 if (fout
) fclose (fout
);
491 // test: hexdump.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
492 // test: hexdump.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
493 // test: hexdump.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
494 // test: hexdump.exe -i hexdump.c | grep -q '0x[0-9a-f]*: '
495 // test: hexdump.exe -i hexdump.c -n 3 | head -2 | tail -1 | grep -q '0x0003: 64 65 70 dep'
496 // test: hexdump.exe -i hexdump.c -o test.c -e 'p 200' | tail -1 | grep -q '0x00c0:'
497 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
498 // 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'
499 // 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'
500 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
501 // test: hexdump.exe -i hexdump.c -e ' /\n/ p 8' | grep -q '0x000d: 0a 2f 2a 20 63 66 6c 61 \./\* cfla'
502 // test: hexdump.exe -i hexdump.c -o test.c -e ' /\a\b\e\f\r\t\v/ p 8'; x=$?; test x$x = x1
503 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
504 // test: hexdump.exe -i hexdump.c -e ' /\"/' -e " /\\'/" -e ' /\\/'
506 /* vim: set ts=4 sw=4 et: */