Commit | Line | Data |
---|---|---|
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 |
27 | int nbcols = NBCOLS; |
28 | int nbdigits = NBDIGITS; | |
29 | int offset = 1; | |
30 | ||
31 | char buffer[BUFFERSIZE] = {0}; | |
ce305529 | 32 | FILE *fin = NULL; |
4c4a10dd | 33 | int addrfile = 0; |
ce305529 | 34 | FILE *fout = NULL; |
a1ab98f9 LM |
35 | char *progname = NULL; |
36 | ||
5bcbfcca LM |
37 | /* type definitions */ |
38 | ||
39 | typedef struct { | |
40 | char *sequence; | |
41 | char bytes[SEQLEN]; | |
42 | int length; | |
43 | } sequence_t; | |
44 | ||
a1ab98f9 LM |
45 | /* help function */ |
46 | ||
4c4a10dd | 47 | int 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"); | |
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"); | |
a1ab98f9 | 66 | |
4c4a10dd | 67 | return ret; |
a1ab98f9 LM |
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 | ||
4c4a10dd | 83 | void 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 |
103 | int 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 | 112 | int 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 | ||
ce305529 LM |
163 | /* hexadecimal dump function */ |
164 | ||
4c4a10dd | 165 | int hexdump (int len) { |
a1ab98f9 LM |
166 | char buffer[BUFFERSIZE] = {0}; |
167 | int i; | |
168 | ||
169 | char *pt = buffer; | |
170 | ||
a1ab98f9 LM |
171 | int nb = 0; |
172 | while (!feof (fin)) { | |
5272fae8 LM |
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; | |
a1ab98f9 LM |
182 | pt = buffer; |
183 | ||
184 | /* print line */ | |
185 | while ((nb - (int)(pt - buffer)) / nbcols > 0) { | |
4c4a10dd | 186 | printline (pt, nbcols, addrfile); |
ce305529 | 187 | writefile (pt, nbcols); |
4c4a10dd | 188 | addrfile += nbcols; |
a1ab98f9 | 189 | pt += nbcols; |
a1ab98f9 LM |
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; | |
5272fae8 LM |
198 | |
199 | /* end partial reading */ | |
200 | if (len == 0) { | |
201 | break; | |
202 | } | |
a1ab98f9 LM |
203 | } |
204 | ||
205 | /* last line */ | |
206 | if (nb > 0) { | |
4c4a10dd LM |
207 | printline (buffer, nb, addrfile); |
208 | writefile (buffer, nb); | |
209 | addrfile += nb; | |
a1ab98f9 LM |
210 | } |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
47db4fc7 LM |
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 | ||
4c4a10dd LM |
250 | /* special character function */ |
251 | ||
5bcbfcca | 252 | int specialchar (char *s, char *b) { |
4c4a10dd LM |
253 | int i = 0, j = 0; |
254 | while (s[i] != 0) { | |
5bcbfcca LM |
255 | if (j == SEQLEN) { |
256 | return 0; | |
257 | } | |
4c4a10dd | 258 | if (s[i] != '\\') { |
5bcbfcca | 259 | b[j++] = s[i++]; |
4c4a10dd LM |
260 | continue; |
261 | } | |
262 | ||
47db4fc7 | 263 | int l = -1; |
4c4a10dd LM |
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; | |
f975557c | 273 | case '/': l = '/'; i += 2; break; |
4c4a10dd LM |
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': | |
5bcbfcca | 281 | l = octal (s + i + 1, 3); |
47db4fc7 | 282 | if (l != -1) { |
4c4a10dd LM |
283 | i += 4; |
284 | } | |
285 | break; | |
286 | case 'x': | |
5bcbfcca | 287 | l = hexa (s + i + 2, 2); |
47db4fc7 | 288 | if (l != -1) { |
4c4a10dd LM |
289 | i += 4; |
290 | } | |
291 | break; | |
292 | default: | |
293 | } | |
5bcbfcca LM |
294 | if (l != -1) { |
295 | VERBOSE (DEBUG, printf("l: 0x%02x '%c'\n", l, l)); | |
296 | } | |
297 | b[j++] = (l != -1) ? l : s[i++]; | |
4c4a10dd | 298 | } |
4c4a10dd | 299 | |
5bcbfcca | 300 | return j; |
4c4a10dd LM |
301 | } |
302 | ||
a1ab98f9 LM |
303 | /* main function */ |
304 | ||
4c4a10dd | 305 | int main (int argc, char *argv[]) |
a1ab98f9 | 306 | { |
5272fae8 | 307 | int rc = 0; |
a1ab98f9 LM |
308 | char *input = NULL; |
309 | char *output = NULL; | |
5272fae8 LM |
310 | char *commands = NULL; |
311 | int printlen = -1; | |
5bcbfcca | 312 | sequence_t seq = {0}; |
4c4a10dd LM |
313 | char *addr = NULL; |
314 | ||
a1ab98f9 LM |
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 | ||
4c4a10dd LM |
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]; | |
a1ab98f9 | 331 | switch (c) { |
5272fae8 | 332 | case 'e': |
4c4a10dd LM |
333 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
334 | if (arg) { | |
4c4a10dd LM |
335 | if (commands == NULL) { |
336 | commands = arg; | |
337 | } else { | |
338 | strcat (commands, " "); | |
339 | strcat (commands, arg); | |
340 | } | |
5272fae8 LM |
341 | } |
342 | break; | |
4c4a10dd LM |
343 | case 'i': |
344 | input = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; | |
345 | break; | |
a1ab98f9 | 346 | case 'n': |
4c4a10dd LM |
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); | |
a1ab98f9 LM |
353 | break; |
354 | case 'o': | |
4c4a10dd | 355 | output = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; |
a1ab98f9 LM |
356 | break; |
357 | case 'v': | |
4c4a10dd LM |
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); | |
a1ab98f9 LM |
364 | break; |
365 | case 'h': | |
366 | default: | |
4c4a10dd | 367 | return usage (c != 'h'); |
a1ab98f9 LM |
368 | } |
369 | } | |
a1ab98f9 LM |
370 | |
371 | /* check input */ | |
a1ab98f9 LM |
372 | if (input) { |
373 | fin = fopen (input, "rb"); | |
374 | if (!fin) { | |
375 | VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input)); | |
5272fae8 | 376 | return 1; |
a1ab98f9 LM |
377 | } |
378 | } else { | |
379 | fin = stdin; | |
380 | } | |
381 | ||
382 | /* check output */ | |
a1ab98f9 | 383 | if (output) { |
ce305529 | 384 | fout = fopen (output, "wb"); |
a1ab98f9 LM |
385 | if (!fout) { |
386 | VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output)); | |
387 | fclose (fin); | |
5272fae8 | 388 | return 1; |
a1ab98f9 LM |
389 | } |
390 | } else { | |
ce305529 | 391 | //fout = stdout; |
a1ab98f9 LM |
392 | } |
393 | ||
4c4a10dd LM |
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 | ||
5272fae8 | 402 | if (commands == NULL) { |
4c4a10dd | 403 | hexdump (-1); |
5272fae8 LM |
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 */ | |
5bcbfcca | 413 | seq.sequence = commands; |
4c4a10dd | 414 | while (*commands) { |
f975557c LM |
415 | if ((*commands == '\\') && |
416 | ((commands[1] == '/') || (commands[1] == '\\'))) { | |
417 | commands++; | |
418 | } else if (*commands == '/') { | |
4c4a10dd LM |
419 | *commands++ = 0; |
420 | break; | |
421 | } | |
422 | commands++; | |
423 | } | |
5bcbfcca LM |
424 | seq.length = specialchar (seq.sequence, seq.bytes); |
425 | if (seq.length != 0) { | |
426 | rc = searchseq (&seq); | |
4c4a10dd | 427 | } else { |
5bcbfcca | 428 | VERBOSE (ERROR, fprintf (stderr, "incorrect sequence (%s)\n", seq.sequence)); |
4c4a10dd LM |
429 | rc = 1; |
430 | } | |
5272fae8 LM |
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 { | |
f975557c | 458 | VERBOSE (ERROR, fprintf (stderr, "unknown print length (%s)\n", commands)); |
5272fae8 LM |
459 | rc = 1; |
460 | break; | |
461 | } | |
462 | } | |
4c4a10dd LM |
463 | if (rc == 0) { |
464 | hexdump (printlen); | |
465 | } | |
5272fae8 LM |
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 | } | |
a1ab98f9 | 477 | |
ce305529 LM |
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 | ||
a1ab98f9 | 488 | /* close all */ |
5272fae8 LM |
489 | if (fin) fclose (fin); |
490 | if (fout) fclose (fout); | |
a1ab98f9 | 491 | |
5272fae8 | 492 | return rc; |
a1ab98f9 LM |
493 | } |
494 | ||
a1ab98f9 | 495 | // test: hexdump.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' |
f975557c LM |
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' | |
a1ab98f9 LM |
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]*: ' | |
f975557c LM |
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' | |
ce305529 LM |
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:' | |
5bcbfcca | 507 | // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0 |
f975557c | 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' |
5bcbfcca LM |
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 | |
f975557c LM |
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' | |
a1ab98f9 LM |
519 | |
520 | /* vim: set ts=4 sw=4 et: */ |