]> 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 02d11a31d0794644918f729646d1e8ace662371c..6fddb305bb4d5288fc751c353192582e4df7f3cc 100644 (file)
@@ -47,7 +47,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.115 2008/02/11 22:12:24 rrt Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.116 2008/02/12 01:08:39 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 e0de8309ad7857f88904be58bce02cff38803269..bfcc4e2b9f4df6dfee5d1d6cf8e1140810efbc35 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.94 2008/02/07 00:58:52 christos Exp $
+ * @(#)$File: file.h,v 1.95 2008/02/12 16:30:48 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 30104f5bf330e12abe7907d6748a5000a27639b6..ee1cdbe6fedf5c94d0313aa36aa34b067f659bc6 100644 (file)
@@ -63,7 +63,7 @@
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: magic.c,v 1.47 2008/02/04 20:51:17 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.48 2008/02/07 00:58:52 christos 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 f0e00c028a56d7e4bb22e6b1c621febc2711e49d..063a35547cf464a994d2213c93b8c61c635e5086 100644 (file)
@@ -41,7 +41,7 @@
 #include <time.h>
 
 #ifndef lint
-FILE_RCSID("@(#)$File: print.c,v 1.61 2007/12/27 16:35:59 christos Exp $")
+FILE_RCSID("@(#)$File: print.c,v 1.62 2008/02/04 20:51:17 christos 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 6a7c38a5dbb0fde5b34d68471baee68e0f57a87a..5bff875f567878a86c5c90a943e0e4cfe15a3868 100644 (file)
@@ -38,7 +38,7 @@
 
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.108 2008/02/11 23:20:51 rrt Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.109 2008/02/12 15:28:39 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;