]> granicus.if.org Git - file/commitdiff
Enforce range in search (use default non-zero value if not given in
authorReuben Thomas <rrt@sc3d.org>
Sun, 17 Feb 2008 19:28:54 +0000 (19:28 +0000)
committerReuben Thomas <rrt@sc3d.org>
Sun, 17 Feb 2008 19:28:54 +0000 (19:28 +0000)
magic file).

Rename str_count to str_range for clarity.

Improve exegetical comment in file_strncmp.

src/apprentice.c
src/file.h
src/magic.c
src/print.c
src/softmagic.c

index d0734a57690fa441e1c204320dc5c498b516308e..0d65d84421ed79b08b3594c9e4e916e663999c6e 100644 (file)
@@ -47,7 +47,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.116 2008/02/12 01:08:39 rrt Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.117 2008/02/17 19:28:54 rrt Exp $")
 #endif /* lint */
 
 #define        EATAB {while (isascii((unsigned char) *l) && \
@@ -697,7 +697,7 @@ file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
 }
 
 private int
-string_modifier_check(struct magic_set *ms, struct magic const *m)
+string_modifier_check(struct magic_set *ms, struct magic *m)
 {
        if ((ms->flags & MAGIC_CHECK) == 0)
                return 0;
@@ -721,6 +721,13 @@ string_modifier_check(struct magic_set *ms, struct magic const *m)
                }
                break;
        case FILE_SEARCH:
+               if (m->str_range == 0) {
+                       file_magwarn(ms,
+                           "missing range; defaulting to %d\n",
+                            STRING_DEFAULT_RANGE);
+                       m->str_range = STRING_DEFAULT_RANGE;
+                       return -1;
+               }
                break;
        case FILE_REGEX:
                if ((m->str_flags & STRING_COMPACT_BLANK) != 0) {
@@ -1060,7 +1067,7 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
                        file_magwarn(ms, "'~' invalid for string types");
                ++l;
        }
-       m->str_count = 0;
+       m->str_range = 0;
        m->str_flags = 0;
        m->num_mask = 0;
        if ((op = get_op(*l)) != -1) {
@@ -1074,23 +1081,24 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
                        eatsize(&l);
                }
                else if (op == FILE_OPDIVIDE) {
-                       int have_count = 0;
+                       int have_range = 0;
                        while (!isspace((unsigned char)*++l)) {
                                switch (*l) {
-                               /* for portability avoid "case '0' ... '9':" */
                                case '0':  case '1':  case '2':
                                case '3':  case '4':  case '5':
                                case '6':  case '7':  case '8':
-                               case '9': {
-                                       if (have_count &&
+                               case '9':
+                                       if (have_range &&
                                            (ms->flags & MAGIC_CHECK))
                                                file_magwarn(ms,
-                                                   "multiple counts");
-                                       have_count = 1;
-                                       m->str_count = strtoul(l, &t, 0);
+                                                   "multiple ranges");
+                                       have_range = 1;
+                                       m->str_range = strtoul(l, &t, 0);
+                                       if (m->str_range == 0)
+                                               file_magwarn(ms,
+                                                   "zero range");
                                        l = t - 1;
                                        break;
-                               }
                                case CHAR_COMPACT_BLANK:
                                        m->str_flags |= STRING_COMPACT_BLANK;
                                        break;
@@ -1985,7 +1993,7 @@ bs1(struct magic *m)
        m->in_offset = swap4((uint32_t)m->in_offset);
        m->lineno = swap4((uint32_t)m->lineno);
        if (IS_STRING(m->type)) {
-               m->str_count = swap4(m->str_count);
+               m->str_range = swap4(m->str_range);
                m->str_flags = swap4(m->str_flags);
        }
        else {
index 675e63e64afcef1c710af2af3aac6880db879caf..c8dc123fb911023c5a49741c3a6a14411e01be35 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.95 2008/02/12 16:30:48 rrt Exp $
+ * @(#)$File: file.h,v 1.96 2008/02/17 19:28:54 rrt Exp $
  */
 
 #ifndef __file_h__
@@ -219,7 +219,7 @@ struct magic {
                } _s;           /* for use with string types */
        } _u;
 #define num_mask _u._mask
-#define str_count _u._s._count
+#define str_range _u._s._count
 #define str_flags _u._s._flags
 
        /* Words 9-16 */
@@ -253,6 +253,7 @@ struct magic {
 #define CHAR_IGNORE_UPPERCASE          'C'
 #define CHAR_REGEX_OFFSET_START                's'
 #define STRING_IGNORE_CASE             (STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
+#define STRING_DEFAULT_RANGE           100
 
 
 /* list of magic entries */
index 68b0b17064d497c099cd496a084feb0c5ffc68dd..1ecd177c6e617e8a01dc45d712362e5b7e0b8a75 100644 (file)
@@ -63,7 +63,7 @@
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: magic.c,v 1.48 2008/02/07 00:58:52 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.49 2008/02/17 19:28:54 rrt Exp $")
 #endif /* lint */
 
 #ifndef PIPE_BUF 
@@ -312,6 +312,7 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
                        (void)strcat(strcpy(tmp, inname), ".exe");
                        if ((fd = open(tmp, flags)) < 0) {
 #endif
+                               fprintf(stderr, "couldn't open file\n");
                                if (info_from_stat(ms, sb.st_mode) == -1)
                                        goto done;
                                rv = 0;
index fd2a8b84ebd45a7b13e176110460380c0a4bb634..c41e71eac534a920a0c48681966f6e3f94031018 100644 (file)
@@ -41,7 +41,7 @@
 #include <time.h>
 
 #ifndef lint
-FILE_RCSID("@(#)$File: print.c,v 1.62 2008/02/04 20:51:17 christos Exp $")
+FILE_RCSID("@(#)$File: print.c,v 1.63 2008/02/17 19:28:54 rrt Exp $")
 #endif  /* lint */
 
 #define SZOF(a)        (sizeof(a) / sizeof(a[0]))
@@ -89,8 +89,8 @@ file_mdump(struct magic *m)
                        if (m->str_flags & REGEX_OFFSET_START) 
                                (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
                }
-               if (m->str_count)
-                       (void) fprintf(stderr, "/%u", m->str_count);
+               if (m->str_range)
+                       (void) fprintf(stderr, "/%u", m->str_range);
        }
        else {
                if ((m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
index 5b53f5791182f379f121ef144580a201bc3d90b4..55ab23fc266616e7f14d8d38d47189cb6cb132ee 100644 (file)
@@ -38,7 +38,7 @@
 
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.109 2008/02/12 15:28:39 rrt Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.110 2008/02/17 19:28:54 rrt Exp $")
 #endif /* lint */
 
 private int match(struct magic_set *, struct magic *, uint32_t,
@@ -884,7 +884,7 @@ mget(struct magic_set *ms, const unsigned char *s,
     struct magic *m, size_t nbytes, unsigned int cont_level)
 {
        uint32_t offset = ms->offset;
-       uint32_t count = m->str_count;
+       uint32_t count = m->str_range;
        union VALUETYPE *p = &ms->ms_value;
 
        if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
@@ -1448,10 +1448,8 @@ file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
        uint64_t v;
 
        /*
-        * What we want here is:
-        * v = strncmp(m->value.s, p->s, m->vallen);
-        * but ignoring any nulls.  bcmp doesn't give -/+/0
-        * and isn't universally available anyway.
+        * What we want here is v = strncmp(s1, s2, len),
+        * but ignoring any nulls.
         */
        v = 0;
        if (0L == flags) { /* normal string: do it fast */
@@ -1653,7 +1651,7 @@ magiccheck(struct magic_set *ms, struct magic *m)
                v = 0;
                ms->search.offset = m->offset;
 
-               for (idx = 0; m->str_count == 0 || idx < m->str_count; idx++) {
+               for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
                        if (slen + idx > ms->search.s_len)
                                break;