[BACK]Return to apprentice.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / file

Diff for /src/usr.bin/file/Attic/apprentice.c between version 1.18 and 1.19

version 1.18, 2003/06/13 18:31:14 version 1.19, 2004/05/19 02:32:35
Line 1 
Line 1 
 /*      $OpenBSD$       */  /*      $OpenBSD$ */
   
 /*  /*
  * apprentice - make one pass through /etc/magic, learning its secrets.  
  *  
  * Copyright (c) Ian F. Darwin 1986-1995.   * Copyright (c) Ian F. Darwin 1986-1995.
  * Software written by Ian F. Darwin and others;   * Software written by Ian F. Darwin and others;
  * maintained 1995-present by Christos Zoulas and others.   * maintained 1995-present by Christos Zoulas and others.
Line 29 
Line 26 
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.   * SUCH DAMAGE.
  */   */
   /*
    * apprentice - make one pass through /etc/magic, learning its secrets.
    */
   
 #include <stdio.h>  #include "file.h"
   #include "magic.h"
 #include <stdlib.h>  #include <stdlib.h>
   #ifdef HAVE_UNISTD_H
   #include <unistd.h>
   #endif
 #include <string.h>  #include <string.h>
 #include <ctype.h>  #include <ctype.h>
 #include <errno.h>  #include <fcntl.h>
 #include <err.h>  #include <sys/stat.h>
 #include "file.h"  #include <sys/param.h>
   #ifdef QUICK
   #include <sys/mman.h>
   #endif
   
 #ifndef lint  #ifndef lint
 static char *moduleid = "$OpenBSD$";  FILE_RCSID("@(#)$Id$")
 #endif  /* lint */  #endif  /* lint */
   
 #define EATAB {while (isascii((unsigned char) *l) && \  #define EATAB {while (isascii((unsigned char) *l) && \
                       isspace((unsigned char) *l))  ++l;}                        isspace((unsigned char) *l))  ++l;}
 #define LOWCASE(l) (isupper((unsigned char) (l)) ? \  #define LOWCASE(l) (isupper((unsigned char) (l)) ? \
                         tolower((unsigned char) (l)) : (l))                          tolower((unsigned char) (l)) : (l))
   /*
    * Work around a bug in headers on Digital Unix.
    * At least confirmed for: OSF1 V4.0 878
    */
   #if defined(__osf__) && defined(__DECC)
   #ifdef MAP_FAILED
   #undef MAP_FAILED
   #endif
   #endif
   
   #ifndef MAP_FAILED
   #define MAP_FAILED (void *) -1
   #endif
   
 static int getvalue(struct magic *, char **);  #ifndef MAP_FILE
 static int hextoint(int);  #define MAP_FILE 0
 static char *getstr(char *, char *, int, int *);  #endif
 static int parse(char *, int *, int);  
 static void eatsize(char **);  
   
 static int maxmagic = 0;  #ifndef MAXPATHLEN
 static int alloc_incr = 256;  #define MAXPATHLEN      1024
   #endif
   
 static int apprentice_1(char *, int);  private int getvalue(struct magic_set *ms, struct magic *, char **);
   private int hextoint(int);
   private char *getstr(struct magic_set *, char *, char *, int, int *);
   private int parse(struct magic_set *, struct magic **, uint32_t *, char *, int);
   private void eatsize(char **);
   private int apprentice_1(struct magic_set *, const char *, int, struct mlist *);
   private int apprentice_file(struct magic_set *, struct magic **, uint32_t *,
       const char *, int);
   private void byteswap(struct magic *, uint32_t);
   private void bs1(struct magic *);
   private uint16_t swap2(uint16_t);
   private uint32_t swap4(uint32_t);
   private char *mkdbname(const char *, char *, size_t);
   private int apprentice_map(struct magic_set *, struct magic **, uint32_t *,
       const char *);
   private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
       const char *);
   
   private size_t maxmagic = 0;
   private size_t magicsize = sizeof(struct magic);
   
   #ifdef COMPILE_ONLY
   const char *magicfile;
   char *progname;
   int lineno;
   
   int main(int, char *[]);
   
 int  int
 apprentice(fn, check)  main(int argc, char *argv[])
 char *fn;                       /* list of magic files */  
 int check;                      /* non-zero? checking-only run. */  
 {  {
         char *p, *mfn;          int ret;
         int file_err, errs = -1;  
         size_t len;  
   
         maxmagic = MAXMAGIS;          if ((progname = strrchr(argv[0], '/')) != NULL)
         magic = (struct magic *) calloc(maxmagic, sizeof(struct magic));                  progname++;
         len = strlen(fn)+1;          else
         mfn = malloc(len);                  progname = argv[0];
         if (magic == NULL || mfn == NULL) {  
                 warn("malloc");          if (argc != 2) {
                 if (check)                  (void)fprintf(stderr, "usage: %s file\n", progname);
                   exit(1);
           }
           magicfile = argv[1];
   
           exit(file_apprentice(magicfile, COMPILE, MAGIC_CHECK) == -1 ? 1 : 0);
   }
   #endif /* COMPILE_ONLY */
   
   
   /*
    * Handle one file.
    */
   private int
   apprentice_1(struct magic_set *ms, const char *fn, int action,
       struct mlist *mlist)
   {
           struct magic *magic = NULL;
           uint32_t nmagic = 0;
           struct mlist *ml;
           int rv = -1;
           int mapped;
   
           if (magicsize != FILE_MAGICSIZE) {
                   file_error(ms, 0, "magic element size %lu != %lu",
                       (unsigned long)sizeof(*magic),
                       (unsigned long)FILE_MAGICSIZE);
                   return -1;
           }
   
           if (action == FILE_COMPILE) {
                   rv = apprentice_file(ms, &magic, &nmagic, fn, action);
                   if (rv != 0)
                         return -1;                          return -1;
                 else                  rv = apprentice_compile(ms, &magic, &nmagic, fn);
                         exit(1);                  free(magic);
                   return rv;
         }          }
         strlcpy(mfn, fn, len);  #ifndef COMPILE_ONLY
         fn = mfn;          if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
                   if (ms->flags & MAGIC_CHECK)
                           file_magwarn("using regular magic file `%s'", fn);
                   rv = apprentice_file(ms, &magic, &nmagic, fn, action);
                   if (rv != 0)
                           return -1;
                   mapped = 0;
           }
   
           if (rv == -1)
                   return rv;
           mapped = rv;
   
           if (magic == NULL || nmagic == 0) {
                   file_delmagic(magic, mapped, nmagic);
                   return -1;
           }
   
           if ((ml = malloc(sizeof(*ml))) == NULL) {
                   file_delmagic(magic, mapped, nmagic);
                   file_oomem(ms);
                   return -1;
           }
   
           ml->magic = magic;
           ml->nmagic = nmagic;
           ml->mapped = mapped;
   
           mlist->prev->next = ml;
           ml->prev = mlist->prev;
           ml->next = mlist;
           mlist->prev = ml;
   
           return 0;
   #endif /* COMPILE_ONLY */
   }
   
   protected void
   file_delmagic(struct magic *p, int type, size_t entries)
   {
           if (p == NULL)
                   return;
           switch (type) {
           case 2:
                   p--;
                   (void)munmap((void *)p, sizeof(*p) * (entries + 1));
                   break;
           case 1:
                   p--;
           case 0:
                   free(p);
                   break;
           default:
                   abort();
           }
   }
   
   
   /* const char *fn: list of magic files */
   protected struct mlist *
   file_apprentice(struct magic_set *ms, const char *fn, int action)
   {
           char *p, *mfn, *afn = NULL;
           int file_err, errs = -1;
           struct mlist *mlist;
   
           if (fn == NULL)
                   fn = getenv("MAGIC");
           if (fn == NULL)
                   fn = MAGIC;
   
           if ((fn = mfn = strdup(fn)) == NULL) {
                   file_oomem(ms);
                   return NULL;
           }
   
           if ((mlist = malloc(sizeof(*mlist))) == NULL) {
                   free(mfn);
                   file_oomem(ms);
                   return NULL;
           }
           mlist->next = mlist->prev = mlist;
   
         while (fn) {          while (fn) {
                 p = strchr(fn, ':');                  p = strchr(fn, PATHSEP);
                 if (p)                  if (p)
                         *p++ = '\0';                          *p++ = '\0';
                 file_err = apprentice_1(fn, check);                  if (*fn == '\0')
                           break;
                   if (ms->flags & MAGIC_MIME) {
                           size_t len = strlen(fn) + 5 + 1;
                           if ((afn = malloc(len)) == NULL) {
                                   free(mfn);
                                   free(mlist);
                                   file_oomem(ms);
                                   return NULL;
                           }
                           (void)strlcpy(afn, fn, len);
                           (void)strlcat(afn, ".mime", len);
                           fn = afn;
                   }
                   file_err = apprentice_1(ms, fn, action, mlist);
                 if (file_err > errs)                  if (file_err > errs)
                         errs = file_err;                          errs = file_err;
                   if (afn) {
                           free(afn);
                           afn = NULL;
                   }
                 fn = p;                  fn = p;
         }          }
         if (errs == -1)          if (errs == -1) {
                 warnx("couldn't find any magic files!");                  free(mfn);
         if (!check && errs)                  free(mlist);
                 exit(1);                  mlist = NULL;
                   file_error(ms, 0, "could not find any magic files!");
                   return NULL;
           }
         free(mfn);          free(mfn);
         return errs;          return mlist;
 }  }
   
 static int  /*
 apprentice_1(fn, check)   * parse from a file
 char *fn;                       /* name of magic file */   * const char *fn: name of magic file
 int check;                      /* non-zero? checking-only run. */   */
   private int
   apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
       const char *fn, int action)
 {  {
         static const char hdr[] =          private const char hdr[] =
                 "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";                  "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
         FILE *f;          FILE *f;
         char line[BUFSIZ+1];          char line[BUFSIZ+1];
           int lineno;
         int errs = 0;          int errs = 0;
   
         f = fopen(fn, "r");          f = fopen(fn, "r");
         if (f==NULL) {          if (f == NULL) {
                 if (errno != ENOENT)                  if (errno != ENOENT)
                         warn("%s", fn);                          file_error(ms, errno, "cannot read magic file `%s'",
                               fn);
                 return -1;                  return -1;
         }          }
   
         /* parse it */          maxmagic = MAXMAGIS;
         if (check)      /* print silly verbose header for USG compat. */          *magicp = (struct magic *) calloc(maxmagic, sizeof(struct magic));
                 (void) printf("%s\n", hdr);          if (*magicp == NULL) {
                   (void)fclose(f);
                   file_oomem(ms);
                   return -1;
           }
   
         for (lineno = 1;fgets(line, BUFSIZ, f) != NULL; lineno++) {          /* print silly verbose header for USG compat. */
           if (action == FILE_CHECK)
                   (void)fprintf(stderr, "%s\n", hdr);
   
           /* parse it */
           for (lineno = 1; fgets(line, BUFSIZ, f) != NULL; lineno++) {
                 if (line[0]=='#')       /* comment, do not parse */                  if (line[0]=='#')       /* comment, do not parse */
                         continue;                          continue;
                 if (strlen(line) <= (unsigned)1) /* null line, garbage, etc */                  if (strlen(line) <= (unsigned)1) /* null line, garbage, etc */
                         continue;                          continue;
                 line[strlen(line)-1] = '\0'; /* delete newline */                  line[strlen(line)-1] = '\0'; /* delete newline */
                 if (parse(line, &nmagic, check) != 0)                  if (parse(ms, magicp, nmagicp, line, action) != 0)
                         errs = 1;                          errs = 1;
         }          }
   
         (void) fclose(f);          (void)fclose(f);
           if (errs) {
                   free(*magicp);
                   *magicp = NULL;
                   *nmagicp = 0;
           }
         return errs;          return errs;
 }  }
   
 /*  /*
  * extend the sign bit if the comparison is to be signed   * extend the sign bit if the comparison is to be signed
  */   */
 uint32_t  protected uint32_t
 signextend(m, v)  file_signextend(struct magic_set *ms, struct magic *m, uint32_t v)
 struct magic *m;  
 uint32_t v;  
 {  {
         if (!(m->flag & UNSIGNED))          if (!(m->flag & UNSIGNED))
                 switch(m->type) {                  switch(m->type) {
Line 151 
Line 342 
                  * vital.  When later compared with the data,                   * vital.  When later compared with the data,
                  * the sign extension must have happened.                   * the sign extension must have happened.
                  */                   */
                 case BYTE:                  case FILE_BYTE:
                         v = (char) v;                          v = (char) v;
                         break;                          break;
                 case SHORT:                  case FILE_SHORT:
                 case BESHORT:                  case FILE_BESHORT:
                 case LESHORT:                  case FILE_LESHORT:
                         v = (short) v;                          v = (short) v;
                         break;                          break;
                 case DATE:                  case FILE_DATE:
                 case BEDATE:                  case FILE_BEDATE:
                 case LEDATE:                  case FILE_LEDATE:
                 case LONG:                  case FILE_LDATE:
                 case BELONG:                  case FILE_BELDATE:
                 case LELONG:                  case FILE_LELDATE:
                   case FILE_LONG:
                   case FILE_BELONG:
                   case FILE_LELONG:
                         v = (int32_t) v;                          v = (int32_t) v;
                         break;                          break;
                 case STRING:                  case FILE_STRING:
                   case FILE_PSTRING:
                         break;                          break;
                   case FILE_REGEX:
                           break;
                 default:                  default:
                         warnx("can't happen: m->type=%d", m->type);                          if (ms->flags & MAGIC_CHECK)
                         return -1;                              file_magwarn("cannot happen: m->type=%d\n",
                                       m->type);
                           return ~0U;
                 }                  }
         return v;          return v;
 }  }
Line 179 
Line 378 
 /*  /*
  * parse one line from magic file, put into magic[index++] if valid   * parse one line from magic file, put into magic[index++] if valid
  */   */
 static int  private int
 parse(l, ndx, check)  parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
 char *l;      int action)
 int *ndx, check;  
 {  {
         int i = 0, nd = *ndx;          int i = 0;
         struct magic *m;          struct magic *m;
         char *t, *s;          char *t;
           private const char *fops = FILE_OPS;
           uint32_t val;
   
         if (nd+1 >= maxmagic){  #define ALLOC_INCR      200
             struct magic *mtmp;          if (*nmagicp + 1 >= maxmagic){
                   maxmagic += ALLOC_INCR;
             maxmagic += alloc_incr;                  if ((m = (struct magic *) realloc(*magicp,
             if ((mtmp = (struct magic *) realloc(magic,                      sizeof(struct magic) * maxmagic)) == NULL) {
                                                   sizeof(struct magic) *                          file_oomem(ms);
                                                   maxmagic)) == NULL) {                          if (*magicp)
                 warn("malloc");                                  free(*magicp);
                 if (check) {  
                         if (magic)  
                                 free(magic);  
                         return -1;                          return -1;
                 } else                  }
                         exit(1);                  *magicp = m;
             }                  memset(&(*magicp)[*nmagicp], 0, sizeof(struct magic)
             magic = mtmp;                      * ALLOC_INCR);
             memset(&magic[*ndx], 0, sizeof(struct magic) * alloc_incr);  
             alloc_incr *= 2;  
         }          }
         m = &magic[*ndx];          m = &(*magicp)[*nmagicp];
         m->flag = 0;          m->flag = 0;
         m->cont_level = 0;          m->cont_level = 0;
   
Line 222 
Line 417 
         }          }
         if (m->cont_level != 0 && *l == '&') {          if (m->cont_level != 0 && *l == '&') {
                 ++l;            /* step over */                  ++l;            /* step over */
                 m->flag |= ADD;                  m->flag |= OFFADD;
         }          }
   
         /* get offset, then skip over it */          /* get offset, then skip over it */
         m->offset = (int) strtoul(l,&t,0);          m->offset = (uint32_t)strtoul(l, &t, 0);
         if (l == t)          if (l == t)
                 warnx("offset %s invalid", l);                  if (ms->flags & MAGIC_CHECK)
                           file_magwarn("offset %s invalid", l);
         l = t;          l = t;
   
         if (m->flag & INDIR) {          if (m->flag & INDIR) {
                 m->in.type = LONG;                  m->in_type = FILE_LONG;
                 m->in.offset = 0;                  m->in_offset = 0;
                 /*                  /*
                  * read [.lbs][+-]nnnnn)                   * read [.lbs][+-]nnnnn)
                  */                   */
                 if (*l == '.') {                  if (*l == '.') {
                         l++;                          l++;
                         switch (LOWCASE(*l)) {                          switch (*l) {
                         case 'l':                          case 'l':
                                 m->in.type = LONG;                                  m->in_type = FILE_LELONG;
                                 break;                                  break;
                           case 'L':
                                   m->in_type = FILE_BELONG;
                                   break;
                         case 'h':                          case 'h':
                         case 's':                          case 's':
                                 m->in.type = SHORT;                                  m->in_type = FILE_LESHORT;
                                 break;                                  break;
                           case 'H':
                           case 'S':
                                   m->in_type = FILE_BESHORT;
                                   break;
                         case 'c':                          case 'c':
                         case 'b':                          case 'b':
                                 m->in.type = BYTE;                          case 'C':
                           case 'B':
                                   m->in_type = FILE_BYTE;
                                 break;                                  break;
                         default:                          default:
                                 warnx("indirect offset type %c invalid", *l);                                  if (ms->flags & MAGIC_CHECK)
                                           file_magwarn(
                                               "indirect offset type %c invalid",
                                               *l);
                                 break;                                  break;
                         }                          }
                         l++;                          l++;
                 }                  }
                 s = l;                  if (*l == '~') {
                 if (*l == '+' || *l == '-') l++;                          m->in_op = FILE_OPINVERSE;
                 if (isdigit((unsigned char)*l)) {                          l++;
                         m->in.offset = strtoul(l, &t, 0);  
                         if (*s == '-') m->in.offset = - m->in.offset;  
                 }                  }
                   switch (*l) {
                   case '&':
                           m->in_op |= FILE_OPAND;
                           l++;
                           break;
                   case '|':
                           m->in_op |= FILE_OPOR;
                           l++;
                           break;
                   case '^':
                           m->in_op |= FILE_OPXOR;
                           l++;
                           break;
                   case '+':
                           m->in_op |= FILE_OPADD;
                           l++;
                           break;
                   case '-':
                           m->in_op |= FILE_OPMINUS;
                           l++;
                           break;
                   case '*':
                           m->in_op |= FILE_OPMULTIPLY;
                           l++;
                           break;
                   case '/':
                           m->in_op |= FILE_OPDIVIDE;
                           l++;
                           break;
                   case '%':
                           m->in_op |= FILE_OPMODULO;
                           l++;
                           break;
                   }
                   if (isdigit((unsigned char)*l))
                           m->in_offset = (uint32_t)strtoul(l, &t, 0);
                 else                  else
                         t = l;                          t = l;
                 if (*t++ != ')')                  if (*t++ != ')')
                         warnx("missing ')' in indirect offset");                          if (ms->flags & MAGIC_CHECK)
                                   file_magwarn("missing ')' in indirect offset");
                 l = t;                  l = t;
         }          }
   
Line 286 
Line 529 
 #define NLESHORT        7  #define NLESHORT        7
 #define NLELONG         6  #define NLELONG         6
 #define NLEDATE         6  #define NLEDATE         6
   #define NPSTRING        7
   #define NLDATE          5
   #define NBELDATE        7
   #define NLELDATE        7
   #define NREGEX          5
   
         if (*l == 'u') {          if (*l == 'u') {
                 ++l;                  ++l;
Line 293 
Line 541 
         }          }
   
         /* get type, skip it */          /* get type, skip it */
         if (strncmp(l, "byte", NBYTE)==0) {          if (strncmp(l, "char", NBYTE)==0) {     /* HP/UX compat */
                 m->type = BYTE;                  m->type = FILE_BYTE;
                 l += NBYTE;                  l += NBYTE;
           } else if (strncmp(l, "byte", NBYTE)==0) {
                   m->type = FILE_BYTE;
                   l += NBYTE;
         } else if (strncmp(l, "short", NSHORT)==0) {          } else if (strncmp(l, "short", NSHORT)==0) {
                 m->type = SHORT;                  m->type = FILE_SHORT;
                 l += NSHORT;                  l += NSHORT;
         } else if (strncmp(l, "long", NLONG)==0) {          } else if (strncmp(l, "long", NLONG)==0) {
                 m->type = LONG;                  m->type = FILE_LONG;
                 l += NLONG;                  l += NLONG;
         } else if (strncmp(l, "string", NSTRING)==0) {          } else if (strncmp(l, "string", NSTRING)==0) {
                 m->type = STRING;                  m->type = FILE_STRING;
                 l += NSTRING;                  l += NSTRING;
         } else if (strncmp(l, "date", NDATE)==0) {          } else if (strncmp(l, "date", NDATE)==0) {
                 m->type = DATE;                  m->type = FILE_DATE;
                 l += NDATE;                  l += NDATE;
         } else if (strncmp(l, "beshort", NBESHORT)==0) {          } else if (strncmp(l, "beshort", NBESHORT)==0) {
                 m->type = BESHORT;                  m->type = FILE_BESHORT;
                 l += NBESHORT;                  l += NBESHORT;
         } else if (strncmp(l, "belong", NBELONG)==0) {          } else if (strncmp(l, "belong", NBELONG)==0) {
                 m->type = BELONG;                  m->type = FILE_BELONG;
                 l += NBELONG;                  l += NBELONG;
         } else if (strncmp(l, "bedate", NBEDATE)==0) {          } else if (strncmp(l, "bedate", NBEDATE)==0) {
                 m->type = BEDATE;                  m->type = FILE_BEDATE;
                 l += NBEDATE;                  l += NBEDATE;
         } else if (strncmp(l, "leshort", NLESHORT)==0) {          } else if (strncmp(l, "leshort", NLESHORT)==0) {
                 m->type = LESHORT;                  m->type = FILE_LESHORT;
                 l += NLESHORT;                  l += NLESHORT;
         } else if (strncmp(l, "lelong", NLELONG)==0) {          } else if (strncmp(l, "lelong", NLELONG)==0) {
                 m->type = LELONG;                  m->type = FILE_LELONG;
                 l += NLELONG;                  l += NLELONG;
         } else if (strncmp(l, "ledate", NLEDATE)==0) {          } else if (strncmp(l, "ledate", NLEDATE)==0) {
                 m->type = LEDATE;                  m->type = FILE_LEDATE;
                 l += NLEDATE;                  l += NLEDATE;
           } else if (strncmp(l, "pstring", NPSTRING)==0) {
                   m->type = FILE_PSTRING;
                   l += NPSTRING;
           } else if (strncmp(l, "ldate", NLDATE)==0) {
                   m->type = FILE_LDATE;
                   l += NLDATE;
           } else if (strncmp(l, "beldate", NBELDATE)==0) {
                   m->type = FILE_BELDATE;
                   l += NBELDATE;
           } else if (strncmp(l, "leldate", NLELDATE)==0) {
                   m->type = FILE_LELDATE;
                   l += NLELDATE;
           } else if (strncmp(l, "regex", NREGEX)==0) {
                   m->type = FILE_REGEX;
                   l += sizeof("regex");
         } else {          } else {
                 warnx("type %s invalid", l);                  if (ms->flags & MAGIC_CHECK)
                           file_magwarn("type %s invalid", l);
                 return -1;                  return -1;
         }          }
         /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */          /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
         if (*l == '&') {          /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
           if (*l == '~') {
                   if (FILE_STRING != m->type && FILE_PSTRING != m->type)
                           m->mask_op = FILE_OPINVERSE;
                 ++l;                  ++l;
                 m->mask = signextend(m, strtoul(l, &l, 0));          }
                 eatsize(&l);          if ((t = strchr(fops,  *l)) != NULL) {
         } else                  uint32_t op = (uint32_t)(t - fops);
                 m->mask = ~0L;                  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 {
                           m->mask = 0L;
                           while (!isspace((unsigned char)*++l)) {
                                   switch (*l) {
                                   case CHAR_IGNORE_LOWERCASE:
                                           m->mask |= STRING_IGNORE_LOWERCASE;
                                           break;
                                   case CHAR_COMPACT_BLANK:
                                           m->mask |= STRING_COMPACT_BLANK;
                                           break;
                                   case CHAR_COMPACT_OPTIONAL_BLANK:
                                           m->mask |=
                                               STRING_COMPACT_OPTIONAL_BLANK;
                                           break;
                                   default:
                                           if (ms->flags & MAGIC_CHECK)
                                                   file_magwarn(
                                                   "string extension %c invalid",
                                                   *l);
                                           return -1;
                                   }
                           }
                   }
           }
           /*
            * 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;          EATAB;
   
         switch (*l) {          switch (*l) {
Line 348 
Line 652 
         case '=':          case '=':
                 m->reln = *l;                  m->reln = *l;
                 ++l;                  ++l;
                   if (*l == '=') {
                      /* HP compat: ignore &= etc. */
                      ++l;
                   }
                 break;                  break;
         case '!':          case '!':
                 if (m->type != STRING) {                  if (m->type != FILE_STRING && m->type != FILE_PSTRING) {
                         m->reln = *l;                          m->reln = *l;
                         ++l;                          ++l;
                         break;                          break;
                 }                  }
                 /* FALL THROUGH */                  /*FALLTHROUGH*/
         default:          default:
                 if (*l == 'x' && isascii((unsigned char)l[1]) &&                  if (*l == 'x' && isascii((unsigned char)l[1]) &&
                     isspace((unsigned char)l[1])) {                      isspace((unsigned char)l[1])) {
Line 368 
Line 676 
         }          }
         EATAB;          EATAB;
   
         if (getvalue(m, &l))          if (getvalue(ms, m, &l))
                 return -1;                  return -1;
         /*          /*
          * TODO finish this macro and start using it!           * TODO finish this macro and start using it!
          * #define offsetcheck {if (offset > HOWMANY-1)           * #define offsetcheck {if (offset > HOWMANY-1)
          *      warnx("offset too big"); }           *      magwarn("offset too big"); }
          */           */
   
         /*          /*
Line 390 
Line 698 
                 m->nospflag = 1;                  m->nospflag = 1;
         } else          } else
                 m->nospflag = 0;                  m->nospflag = 0;
         while ((m->desc[i++] = *l++) != '\0' && i<MAXDESC)          while ((m->desc[i++] = *l++) != '\0' && i < MAXDESC)
                 /* NULLBODY */;                  /* NULLBODY */;
   
         if (check) {  #ifndef COMPILE_ONLY
                 mdump(m);          if (action == FILE_CHECK) {
                   file_mdump(m);
         }          }
         ++(*ndx);               /* make room for next */  #endif
           ++(*nmagicp);           /* make room for next */
         return 0;          return 0;
 }  }
   
Line 405 
Line 715 
  * pointer, according to the magic type.  Update the string pointer to point   * pointer, according to the magic type.  Update the string pointer to point
  * just after the number read.  Return 0 for success, non-zero for failure.   * just after the number read.  Return 0 for success, non-zero for failure.
  */   */
 static int  private int
 getvalue(m, p)  getvalue(struct magic_set *ms, struct magic *m, char **p)
 struct magic *m;  
 char **p;  
 {  {
         int slen;          int slen;
   
         if (m->type == STRING) {          switch (m->type) {
                 *p = getstr(*p, m->value.s, sizeof(m->value.s), &slen);          case FILE_STRING:
           case FILE_PSTRING:
           case FILE_REGEX:
                   *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen);
                   if (*p == NULL) {
                           if (ms->flags & MAGIC_CHECK)
                                   file_magwarn("cannot get string from `%s'",
                                       m->value.s);
                           return -1;
                   }
                 m->vallen = slen;                  m->vallen = slen;
         } else                  return 0;
           default:
                 if (m->reln != 'x') {                  if (m->reln != 'x') {
                         m->value.l = signextend(m, strtoul(*p, p, 0));                          m->value.l = file_signextend(ms, m,
                               (uint32_t)strtoul(*p, p, 0));
                         eatsize(p);                          eatsize(p);
                 }                  }
         return 0;                  return 0;
           }
 }  }
   
 /*  /*
Line 429 
Line 749 
  * Copy the converted version to "p", returning its length in *slen.   * Copy the converted version to "p", returning its length in *slen.
  * Return updated scan pointer as function result.   * Return updated scan pointer as function result.
  */   */
 static char *  private char *
 getstr(s, p, plen, slen)  getstr(struct magic_set *ms, char *s, char *p, int plen, int *slen)
 char    *s;  
 char    *p;  
 int     plen, *slen;  
 {  {
         char    *origs = s, *origp = p;          char    *origs = s, *origp = p;
         char    *pmax = p + plen - 1;          char    *pmax = p + plen - 1;
Line 444 
Line 761 
                 if (isspace((unsigned char) c))                  if (isspace((unsigned char) c))
                         break;                          break;
                 if (p >= pmax) {                  if (p >= pmax) {
                         fprintf(stderr, "String too long: %s\n", origs);                          file_error(ms, 0, "string too long: `%s'", origs);
                         break;                          return NULL;
                 }                  }
                 if(c == '\\') {                  if(c == '\\') {
                         switch(c = *s++) {                          switch(c = *s++) {
Line 532 
Line 849 
   
   
 /* Single hex char to int; -1 if not a hex char. */  /* Single hex char to int; -1 if not a hex char. */
 static int  private int
 hextoint(c)  hextoint(int c)
 int c;  
 {  {
         if (!isascii((unsigned char) c))        return -1;          if (!isascii((unsigned char) c))
         if (isdigit((unsigned char) c))         return c - '0';                  return -1;
         if ((c>='a')&&(c<='f')) return c + 10 - 'a';          if (isdigit((unsigned char) c))
         if ((c>='A')&&(c<='F')) return c + 10 - 'A';                  return c - '0';
                                 return -1;          if ((c >= 'a')&&(c <= 'f'))
                   return c + 10 - 'a';
           if (( c>= 'A')&&(c <= 'F'))
                   return c + 10 - 'A';
           return -1;
 }  }
   
   
 /*  /*
  * Print a string containing C character escapes.   * Print a string containing C character escapes.
  */   */
 void  protected void
 showstr(fp, s, len)  file_showstr(FILE *fp, const char *s, size_t len)
 FILE *fp;  
 const char *s;  
 int len;  
 {  {
         char    c;          char    c;
   
         for (;;) {          for (;;) {
                 c = *s++;                  c = *s++;
                 if (len == -1) {                  if (len == ~0U) {
                         if (c == '\0')                          if (c == '\0')
                                 break;                                  break;
                 }                  }
Line 606 
Line 923 
 /*  /*
  * eatsize(): Eat the size spec from a number [eg. 10UL]   * eatsize(): Eat the size spec from a number [eg. 10UL]
  */   */
 static void  private void
 eatsize(p)  eatsize(char **p)
 char **p;  
 {  {
         char *l = *p;          char *l = *p;
   
Line 628 
Line 944 
         }          }
   
         *p = l;          *p = l;
   }
   
   /*
    * handle a compiled file.
    */
   private int
   apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
       const char *fn)
   {
           int fd;
           struct stat st;
           uint32_t *ptr;
           uint32_t version;
           int needsbyteswap;
           char buf[MAXPATHLEN];
           char *dbname = mkdbname(fn, buf, sizeof(buf));
           void *mm = NULL;
   
           if (dbname == NULL)
                   return -1;
   
           if ((fd = open(dbname, O_RDONLY)) == -1)
                   return -1;
   
           if (fstat(fd, &st) == -1) {
                   file_error(ms, errno, "cannot stat `%s'", dbname);
                   goto error;
           }
           if (st.st_size < 16) {
                   file_error(ms, 0, "file `%s' is too small", dbname);
                   goto error;
           }
   
   #ifdef QUICK
           if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
               MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
                   file_error(ms, errno, "cannot map `%s'", dbname);
                   goto error;
           }
   #define RET     2
   #else
           if ((mm = malloc((size_t)st.st_size)) == NULL) {
                   file_oomem(ms);
                   goto error;
           }
           if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
                   file_badread(ms);
                   goto error;
           }
   #define RET     1
   #endif
           *magicp = mm;
           (void)close(fd);
           fd = -1;
           ptr = (uint32_t *)(void *)*magicp;
           if (*ptr != MAGICNO) {
                   if (swap4(*ptr) != MAGICNO) {
                           file_error(ms, 0, "bad magic in `%s'");
                           goto error;
                   }
                   needsbyteswap = 1;
           } else
                   needsbyteswap = 0;
           if (needsbyteswap)
                   version = swap4(ptr[1]);
           else
                   version = ptr[1];
           if (version != VERSIONNO) {
                   file_error(ms, 0, "version mismatch (%d != %d) in `%s'",
                       version, VERSIONNO, dbname);
                   goto error;
           }
           *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic)) - 1;
           (*magicp)++;
           if (needsbyteswap)
                   byteswap(*magicp, *nmagicp);
           return RET;
   
   error:
           if (fd != -1)
                   (void)close(fd);
           if (mm) {
   #ifdef QUICK
                   (void)munmap((void *)mm, (size_t)st.st_size);
   #else
                   free(mm);
   #endif
           } else {
                   *magicp = NULL;
                   *nmagicp = 0;
           }
           return -1;
   }
   
   private const uint32_t ar[] = {
       MAGICNO, VERSIONNO
   };
   /*
    * handle an mmaped file.
    */
   private int
   apprentice_compile(struct magic_set *ms, struct magic **magicp,
       uint32_t *nmagicp, const char *fn)
   {
           int fd;
           char buf[MAXPATHLEN];
           char *dbname = mkdbname(fn, buf, sizeof(buf));
   
           if (dbname == NULL)
                   return -1;
   
           if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
                   file_error(ms, errno, "cannot open `%s'", dbname);
                   return -1;
           }
   
           if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
                   file_error(ms, errno, "error writing `%s'", dbname);
                   return -1;
           }
   
           if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
               != sizeof(struct magic)) {
                   file_error(ms, errno, "error seeking `%s'", dbname);
                   return -1;
           }
   
           if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
               != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
                   file_error(ms, errno, "error writing `%s'", dbname);
                   return -1;
           }
   
           (void)close(fd);
           return 0;
   }
   
   private const char ext[] = ".mgc";
   /*
    * make a dbname
    */
   private char *
   mkdbname(const char *fn, char *buf, size_t bufsiz)
   {
   #ifdef notdef
           const char *p;
           if ((p = strrchr(fn, '/')) != NULL)
                   fn = ++p;
   #endif
           (void)snprintf(buf, bufsiz, "%s%s", fn, ext);
           return buf;
   }
   
   /*
    * Byteswap an mmap'ed file if needed
    */
   private void
   byteswap(struct magic *magic, uint32_t nmagic)
   {
           uint32_t i;
           for (i = 0; i < nmagic; i++)
                   bs1(&magic[i]);
   }
   
   /*
    * swap a short
    */
   private uint16_t
   swap2(uint16_t sv)
   {
           uint16_t rv;
           uint8_t *s = (uint8_t *)(void *)&sv;
           uint8_t *d = (uint8_t *)(void *)&rv;
           d[0] = s[1];
           d[1] = s[0];
           return rv;
   }
   
   /*
    * swap an int
    */
   private uint32_t
   swap4(uint32_t sv)
   {
           uint32_t rv;
           uint8_t *s = (uint8_t *)(void *)&sv;
           uint8_t *d = (uint8_t *)(void *)&rv;
           d[0] = s[3];
           d[1] = s[2];
           d[2] = s[1];
           d[3] = s[0];
           return rv;
   }
   
   /*
    * byteswap a single magic entry
    */
   private void
   bs1(struct magic *m)
   {
           m->cont_level = swap2(m->cont_level);
           m->offset = swap4((uint32_t)m->offset);
           m->in_offset = swap4((uint32_t)m->in_offset);
           if (m->type != FILE_STRING)
                   m->value.l = swap4(m->value.l);
           m->mask = swap4(m->mask);
 }  }

Legend:
Removed from v.1.18  
changed lines
  Added in v.1.19