#define BUFFERSIZE 256
#define NBCOLS 8
#define NBDIGITS 6
+#define SEQLEN 32
/* gobal variables */
FILE *fout = NULL;
char *progname = NULL;
+/* type definitions */
+
+typedef struct {
+ char *sequence;
+ char bytes[SEQLEN];
+ int length;
+} sequence_t;
+
/* help function */
int usage (int ret)
/* search sequence function */
-int searchseq (char *seq) {
+int searchseq (sequence_t *seq) {
char *pt = buffer;
int nb = 0;
int i, j;
int valid = 0;
- int len = strlen (seq);
- VERBOSE (DEBUG, printf ("search sequence: %s\n", seq));
+ VERBOSE (DEBUG, printf ("search sequence: %s\n", seq->sequence));
while (!feof (fin)) {
int nbread = fread (pt, 1, BUFFERSIZE - (pt - buffer), fin);
nb += nbread;
pt = buffer;
- for (i = 0; i < nb - len; i++) {
+ for (i = 0; i < nb - seq->length; i++) {
valid = 1;
- for (j = 0; (j < len) && (valid); j++) {
- if (pt[i + j] != seq[j]) {
+ for (j = 0; (j < seq->length) && (valid); j++) {
+ if (pt[i + j] != seq->bytes[j]) {
valid = 0;
}
}
}
if (!valid) {
- writefile (buffer, nb - len);
+ writefile (buffer, nb - seq->length);
offset = 0;
- addrfile += nb - len;
- for (i = 0; i < len; i++) {
- buffer[i] = buffer[nb - len + i];
+ addrfile += nb - seq->length;
+ for (i = 0; i < seq->length; i++) {
+ buffer[i] = buffer[nb - seq->length + i];
}
- pt = buffer + len;
- nb = len;
+ pt = buffer + seq->length;
+ nb = seq->length;
} else {
writefile (buffer, i);
- offset = len;
+ offset = seq->length;
addrfile += i;
fseek (fin, i - nb, SEEK_CUR);
VERBOSE (DEBUG, printf ("found sequence (%d)\n", i - nb));
if (!valid) {
writefile (buffer, nb);
- addrfile += len;
+ addrfile += seq->length;
}
return 1;
/* special character function */
-char *specialchar (char *s) {
+int specialchar (char *s, char *b) {
int i = 0, j = 0;
while (s[i] != 0) {
+ if (j == SEQLEN) {
+ return 0;
+ }
if (s[i] != '\\') {
- s[j++] = s[i++];
+ b[j++] = s[i++];
continue;
}
case '1':
case '2':
case '3':
- l = octal (s + 1, 3);
+ l = octal (s + i + 1, 3);
if (l != -1) {
i += 4;
}
break;
case 'x':
- l = hexa (s + 2, 2);
+ l = hexa (s + i + 2, 2);
if (l != -1) {
i += 4;
}
break;
default:
}
- VERBOSE (DEBUG, printf("l: 0x%02x '%c'\n", l, l));
- s[j++] = (l != -1) ? l : s[i++];
+ if (l != -1) {
+ VERBOSE (DEBUG, printf("l: 0x%02x '%c'\n", l, l));
+ }
+ b[j++] = (l != -1) ? l : s[i++];
}
- s[j] = '\0';
- return s;
+ return j;
}
/* main function */
char *output = NULL;
char *commands = NULL;
int printlen = -1;
- char *seq = NULL;
+ sequence_t seq = {0};
char *addr = NULL;
/* get basename */
case 'e':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg) {
- arg = specialchar (arg);
if (commands == NULL) {
commands = arg;
} else {
break;
case '/': /* read patern */
- seq = commands;
+ seq.sequence = commands;
while (*commands) {
if (*commands == '/') {
*commands++ = 0;
}
commands++;
}
- if (*seq != 0) {
- rc = searchseq (seq);
+ seq.length = specialchar (seq.sequence, seq.bytes);
+ if (seq.length != 0) {
+ rc = searchseq (&seq);
} else {
- VERBOSE (ERROR, fprintf (stderr, "no sequence definied\n"));
+ VERBOSE (ERROR, fprintf (stderr, "incorrect sequence (%s)\n", seq.sequence));
rc = 1;
}
break;
commands++;
break;
} else {
- VERBOSE (ERROR, fprintf (stderr, "unkown print lenght (%s)\n", commands));
+ VERBOSE (ERROR, fprintf (stderr, "unkown print length (%s)\n", commands));
rc = 1;
break;
}
// test: hexdump.exe -i hexdump.c | grep -q '0x[0-9a-f]*: '
// test: hexdump.exe -i hexdump.c -n 3 | head -2 | tail -1 | grep -q '0x0003: 64 65 70 dep'
// test: hexdump.exe -i hexdump.c -o test.c -e 'p 200' | tail -1 | grep -q '0x00c0:'
-// test: cmp hexdump.c test.c
-// test: rm test.c
-// test: hexdump.exe -i hexdump.c -e ' /cflags/ p 16 /debug/ p 8' | grep -q '0x0019: 2a 2f 0a 2f 2a 20 6c 69 \*/\./\* li'
-// test: hexdump.exe -i hexdump.c -o test.c -e ' /cflags/ p 16 /debug/ p 8' | grep -q '0x0027: 64 65 62 75 67 2e 6f 20 debug.o'
-// test: cmp hexdump.c test.c
-// test: rm test.c
+// test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
+// 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'
+// 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'
+// test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
+// test: hexdump.exe -i hexdump.c -e ' /\n/ p 8' | grep -q '0x000d: 0a 2f 2a 20 63 66 6c 61 \./\* cfla'
+// test: hexdump.exe -i hexdump.c -o test.c -e ' /\a\b\e\f\r\t\v/ p 8'; x=$?; test x$x = x1
+// test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
+// test: hexdump.exe -i hexdump.c -e ' /\"/' -e " /\\'/" -e ' /\\/'
/* vim: set ts=4 sw=4 et: */