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