better search sequence management
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 12 Jan 2023 10:59:51 +0000 (11:59 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 12 Jan 2023 10:59:51 +0000 (11:59 +0100)
hexdump.c

index d118c066a904cc22691f5c856f896093fe48bfd9..cc294143cd78005ab46908d515a2595104351d50 100644 (file)
--- a/hexdump.c
+++ b/hexdump.c
@@ -20,6 +20,7 @@
 #define BUFFERSIZE 256
 #define NBCOLS 8
 #define NBDIGITS 6
+#define SEQLEN 32
 
 /* gobal variables */
 
@@ -33,6 +34,14 @@ int addrfile = 0;
 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)
@@ -100,23 +109,22 @@ int writefile (char *pt, int nb) {
 
 /* 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;
                 }
             }
@@ -126,17 +134,17 @@ int searchseq (char *seq) {
         }
 
         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));
@@ -146,7 +154,7 @@ int searchseq (char *seq) {
 
     if (!valid) {
         writefile (buffer, nb);
-        addrfile += len;
+        addrfile += seq->length;
     }
 
     return 1;
@@ -241,11 +249,14 @@ long int hexa (char *s, int n) {
 
 /* 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;
         }
 
@@ -266,25 +277,26 @@ char *specialchar (char *s) {
         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 */
@@ -296,7 +308,7 @@ int main (int argc, char *argv[])
     char *output = NULL;
     char *commands = NULL;
     int printlen = -1;
-    char *seq = NULL;
+    sequence_t seq = {0};
     char *addr = NULL;
 
     /* get basename */
@@ -319,7 +331,6 @@ int main (int argc, char *argv[])
         case 'e':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg) {
-                arg = specialchar (arg);
                 if (commands == NULL) {
                     commands = arg;
                 } else {
@@ -398,7 +409,7 @@ int main (int argc, char *argv[])
                 break;
 
             case '/': /* read patern */
-                seq = commands;
+                seq.sequence = commands;
                 while (*commands) {
                     if (*commands == '/') {
                         *commands++ = 0;
@@ -406,10 +417,11 @@ int main (int argc, char *argv[])
                     }
                     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;
@@ -439,7 +451,7 @@ int main (int argc, char *argv[])
                         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;
                     }
@@ -482,11 +494,13 @@ int main (int argc, char *argv[])
 // 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: */