Factor out some common code and make the compression work as before.
authorChristos Zoulas <christos@zoulas.com>
Wed, 26 Mar 2003 16:25:25 +0000 (16:25 +0000)
committerChristos Zoulas <christos@zoulas.com>
Wed, 26 Mar 2003 16:25:25 +0000 (16:25 +0000)
src/apprentice.c
src/compress.c
src/file.h

index dd43094c3cfbb4797f6d0d5b2b7cc283fe3d39ef..7daed58e7806ddec464b84c9501f6f17a99a8512 100644 (file)
@@ -50,7 +50,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: apprentice.c,v 1.55 2003/03/26 15:35:30 christos Exp $")
+FILE_RCSID("@(#)$Id: apprentice.c,v 1.56 2003/03/26 16:25:25 christos Exp $")
 #endif /* lint */
 
 #define        EATAB {while (isascii((unsigned char) *l) && \
@@ -334,6 +334,7 @@ parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
        int i = 0;
        struct magic *m;
        char *t;
+       private const char *fops = FILE_OPS;
        uint32_t val;
 
 #define ALLOC_INCR     200
@@ -369,7 +370,7 @@ parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
         }
 
        /* get offset, then skip over it */
-       m->offset = (int) strtoul(l,&t,0);
+       m->offset = (int) strtoul(l, &t, 0);
         if (l == t)
                if (ms->flags & MAGIC_CHECK)
                        file_magwarn("offset %s invalid", l);
@@ -552,54 +553,13 @@ parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
                        m->mask_op = FILE_OPINVERSE;
                ++l;
        }
-       val = (uint32_t)strtoul(l, &l, 0);
-       switch (*l) {
-       case '&':
-               m->mask_op |= FILE_OPAND;
-               ++l;
-               m->mask = file_signextend(ms, m, val);
-               eatsize(&l);
-               break;
-       case '|':
-               m->mask_op |= FILE_OPOR;
-               ++l;
-               m->mask = file_signextend(ms, m, val);
-               eatsize(&l);
-               break;
-       case '^':
-               m->mask_op |= FILE_OPXOR;
-               ++l;
-               m->mask = file_signextend(ms, m, val);
-               eatsize(&l);
-               break;
-       case '+':
-               m->mask_op |= FILE_OPADD;
-               ++l;
-               m->mask = file_signextend(ms, m, val);
-               eatsize(&l);
-               break;
-       case '-':
-               m->mask_op |= FILE_OPMINUS;
-               ++l;
-               m->mask = file_signextend(ms, m, val);
-               eatsize(&l);
-               break;
-       case '*':
-               m->mask_op |= FILE_OPMULTIPLY;
-               ++l;
-               m->mask = file_signextend(ms, m, val);
-               eatsize(&l);
-               break;
-       case '%':
-               m->mask_op |= FILE_OPMODULO;
-               ++l;
-               m->mask = file_signextend(ms, m, val);
-               eatsize(&l);
-               break;
-       case '/':
-               if (FILE_STRING != m->type && FILE_PSTRING != m->type) {
-                       m->mask_op |= FILE_OPDIVIDE;
+       if ((t = strchr(fops,  *l)) != NULL) {
+               uint32_t op = (uint32_t)(t - fops);
+               if (op != FILE_OPDIVIDE ||
+                   (FILE_STRING != m->type && FILE_PSTRING != m->type)) {
                        ++l;
+                       m->mask_op |= op;
+                       val = (uint32_t)strtoul(l, &l, 0);
                        m->mask = file_signextend(ms, m, val);
                        eatsize(&l);
                } else {
@@ -625,10 +585,11 @@ parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
                                }
                        }
                }
-               break;
        }
-       /* We used to set mask to all 1's here, instead let's just not do anything 
-          if mask = 0 (unless you have a better idea) */
+       /*
+        * We used to set mask to all 1's here, instead let's just not do
+        * anything if mask = 0 (unless you have a better idea)
+        */
        EATAB;
   
        switch (*l) {
index 11f2773eef7b7187d5adcf62d7ac5283953557c9..e498c64b5a9f21d5c04487ad816cdabec367994d 100644 (file)
 #ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
 #endif
+#undef HAVE_LIBZ
 #ifdef HAVE_LIBZ
 #include <zlib.h>
 #endif
 
 #ifndef lint
-FILE_RCSID("@(#)$Id: compress.c,v 1.30 2003/03/26 15:35:30 christos Exp $")
+FILE_RCSID("@(#)$Id: compress.c,v 1.31 2003/03/26 16:25:25 christos Exp $")
 #endif
 
 
@@ -92,8 +93,9 @@ private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
 protected int
 file_zmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
 {
-       unsigned char *newbuf;
-       size_t i;
+       unsigned char *newbuf = NULL;
+       size_t i, nsz;
+       int rv = 0;
 
        if ((ms->flags & MAGIC_COMPRESS) == 0)
                return 0;
@@ -102,23 +104,26 @@ file_zmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
                if (nbytes < compr[i].maglen)
                        continue;
                if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
-                   uncompressbuf(ms, i, buf, &newbuf, nbytes) != 0) {
-                       free(newbuf);
-                       if (file_printf(ms, " (") == -1)
-                               return -1;
+                   (nsz = uncompressbuf(ms, i, buf, &newbuf, nbytes)) != 0) {
                        ms->flags &= ~MAGIC_COMPRESS;
-                       if (file_buffer(ms, buf, nbytes) == -1) {
-                               ms->flags |= MAGIC_COMPRESS;
-                               return -1;
-                       }
-                       ms->flags |= MAGIC_COMPRESS;
+                       rv = -1;
+                       if (file_buffer(ms, newbuf, nsz) == -1)
+                               goto error;
+                       if (file_printf(ms, " (") == -1)
+                               goto error;
+                       if (file_buffer(ms, buf, nbytes) == -1)
+                               goto error;
                        if (file_printf(ms, ")") == -1)
-                               return -1;
-                       return 1;
+                               goto error;
+                       rv = 1;
+                       break;
                }
        }
-
-       return 0;
+error:
+       if (newbuf)
+               free(newbuf);
+       ms->flags |= MAGIC_COMPRESS;
+       return rv;
 }
 
 /*
index da8a66b6f4eb81357f375ba7a2814fd535d0dd78..82cdcaf5faca13ba68fe01eab9a4348c040c9059 100644 (file)
@@ -32,7 +32,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.49 2003/03/26 15:35:30 christos Exp $
+ * @(#)$Id: file.h,v 1.50 2003/03/26 16:25:25 christos Exp $
  */
 
 #ifndef __file_h__
@@ -80,7 +80,7 @@
 #define FILE_COMPILE   2
 
 #ifndef __GNUC__
-#ifndef __attribute__
+#ifndef __lint__
 #define __attribute__(a)
 #endif
 #endif
@@ -114,14 +114,15 @@ struct magic {
 #define                                FILE_REGEX      17
        uint8_t in_op;          /* operator for indirection */
        uint8_t mask_op;        /* operator for mask */
-#define                                FILE_OPAND      1
-#define                                FILE_OPOR       2
-#define                                FILE_OPXOR      3
-#define                                FILE_OPADD      4
-#define                                FILE_OPMINUS    5
-#define                                FILE_OPMULTIPLY 6
-#define                                FILE_OPDIVIDE   7
-#define                                FILE_OPMODULO   8
+#define                                FILE_OPS        "&|^+-*%/"
+#define                                FILE_OPAND      0
+#define                                FILE_OPOR       1
+#define                                FILE_OPXOR      2
+#define                                FILE_OPADD      3
+#define                                FILE_OPMINUS    4
+#define                                FILE_OPMULTIPLY 5
+#define                                FILE_OPDIVIDE   6
+#define                                FILE_OPMODULO   7
 #define                                FILE_OPINVERSE  0x80
        int32_t offset;         /* offset to magic number */
        int32_t in_offset;      /* offset from indirection */