Merge from vendor branch HEIMDAL:
[dragonfly.git] / contrib / file-4.12 / src / apprentice.c
1 /*
2  * Copyright (c) Ian F. Darwin 1986-1995.
3  * Software written by Ian F. Darwin and others;
4  * maintained 1995-present by Christos Zoulas and others.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *  
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /*
29  * apprentice - make one pass through /etc/magic, learning its secrets.
30  */
31
32 #include "file.h"
33 #include "magic.h"
34 #include <stdlib.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #include <string.h>
39 #include <ctype.h>
40 #include <fcntl.h>
41 #include <sys/stat.h>
42 #include <sys/param.h>
43 #ifdef QUICK
44 #include <sys/mman.h>
45 #endif
46
47 #ifndef lint
48 FILE_RCSID("@(#)$Id: apprentice.c,v 1.82 2004/11/24 18:56:04 christos Exp $")
49 #endif  /* lint */
50
51 #define EATAB {while (isascii((unsigned char) *l) && \
52                       isspace((unsigned char) *l))  ++l;}
53 #define LOWCASE(l) (isupper((unsigned char) (l)) ? \
54                         tolower((unsigned char) (l)) : (l))
55 /*
56  * Work around a bug in headers on Digital Unix.
57  * At least confirmed for: OSF1 V4.0 878
58  */
59 #if defined(__osf__) && defined(__DECC)
60 #ifdef MAP_FAILED
61 #undef MAP_FAILED
62 #endif
63 #endif
64
65 #ifndef MAP_FAILED
66 #define MAP_FAILED (void *) -1
67 #endif
68
69 #ifndef MAP_FILE
70 #define MAP_FILE 0
71 #endif
72
73 #ifndef MAXPATHLEN
74 #define MAXPATHLEN      1024
75 #endif
76
77 #define IS_STRING(t) ((t) == FILE_STRING || (t) == FILE_PSTRING || \
78     (t) == FILE_BESTRING16 || (t) == FILE_LESTRING16)
79
80 private int getvalue(struct magic_set *ms, struct magic *, char **);
81 private int hextoint(int);
82 private char *getstr(struct magic_set *, char *, char *, int, int *);
83 private int parse(struct magic_set *, struct magic **, uint32_t *, char *, int);
84 private void eatsize(char **);
85 private int apprentice_1(struct magic_set *, const char *, int, struct mlist *);
86 private int apprentice_file(struct magic_set *, struct magic **, uint32_t *,
87     const char *, int);
88 private void byteswap(struct magic *, uint32_t);
89 private void bs1(struct magic *);
90 private uint16_t swap2(uint16_t);
91 private uint32_t swap4(uint32_t);
92 private char *mkdbname(const char *, char *, size_t, int);
93 private int apprentice_map(struct magic_set *, struct magic **, uint32_t *,
94     const char *);
95 private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
96     const char *);
97 private int check_format(struct magic_set *, struct magic *);
98
99 private size_t maxmagic = 0;
100 private size_t magicsize = sizeof(struct magic);
101
102 #ifdef COMPILE_ONLY
103
104 int main(int, char *[]);
105
106 int
107 main(int argc, char *argv[])
108 {
109         int ret;
110         struct magic_set *ms;
111         char *progname;
112
113         if ((progname = strrchr(argv[0], '/')) != NULL)
114                 progname++;
115         else
116                 progname = argv[0];
117
118         if (argc != 2) {
119                 (void)fprintf(stderr, "Usage: %s file\n", progname);
120                 return 1;
121         }
122
123         if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
124                 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
125                 return 1;
126         }
127         ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
128         if (ret == 1)
129                 (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
130         magic_close(ms);
131         return ret;
132 }
133 #endif /* COMPILE_ONLY */
134
135
136 /*
137  * Handle one file.
138  */
139 private int
140 apprentice_1(struct magic_set *ms, const char *fn, int action,
141     struct mlist *mlist)
142 {
143         struct magic *magic = NULL;
144         uint32_t nmagic = 0;
145         struct mlist *ml;
146         int rv = -1;
147         int mapped;
148
149         if (magicsize != FILE_MAGICSIZE) {
150                 file_error(ms, 0, "magic element size %lu != %lu",
151                     (unsigned long)sizeof(*magic),
152                     (unsigned long)FILE_MAGICSIZE);
153                 return -1;
154         }
155
156         if (action == FILE_COMPILE) {
157                 rv = apprentice_file(ms, &magic, &nmagic, fn, action);
158                 if (rv != 0)
159                         return -1;
160                 rv = apprentice_compile(ms, &magic, &nmagic, fn);
161                 free(magic);
162                 return rv;
163         }
164 #ifndef COMPILE_ONLY
165         if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
166                 if (ms->flags & MAGIC_CHECK)
167                         file_magwarn(ms, "using regular magic file `%s'", fn);
168                 rv = apprentice_file(ms, &magic, &nmagic, fn, action);
169                 if (rv != 0)
170                         return -1;
171                 mapped = 0;
172         }
173
174         if (rv == -1)
175                 return rv;
176         mapped = rv;
177              
178         if (magic == NULL || nmagic == 0) {
179                 file_delmagic(magic, mapped, nmagic);
180                 return -1;
181         }
182
183         if ((ml = malloc(sizeof(*ml))) == NULL) {
184                 file_delmagic(magic, mapped, nmagic);
185                 file_oomem(ms);
186                 return -1;
187         }
188
189         ml->magic = magic;
190         ml->nmagic = nmagic;
191         ml->mapped = mapped;
192
193         mlist->prev->next = ml;
194         ml->prev = mlist->prev;
195         ml->next = mlist;
196         mlist->prev = ml;
197
198         return 0;
199 #endif /* COMPILE_ONLY */
200 }
201
202 protected void
203 file_delmagic(struct magic *p, int type, size_t entries)
204 {
205         if (p == NULL)
206                 return;
207         switch (type) {
208         case 2:
209                 p--;
210                 (void)munmap((void *)p, sizeof(*p) * (entries + 1));
211                 break;
212         case 1:
213                 p--;
214                 /*FALLTHROUGH*/
215         case 0:
216                 free(p);
217                 break;
218         default:
219                 abort();
220         }
221 }
222
223
224 /* const char *fn: list of magic files */
225 protected struct mlist *
226 file_apprentice(struct magic_set *ms, const char *fn, int action)
227 {
228         char *p, *mfn, *afn = NULL;
229         int file_err, errs = -1;
230         struct mlist *mlist;
231
232         if (fn == NULL)
233                 fn = getenv("MAGIC");
234         if (fn == NULL)
235                 fn = MAGIC;
236
237         if ((fn = mfn = strdup(fn)) == NULL) {
238                 file_oomem(ms);
239                 return NULL;
240         }
241
242         if ((mlist = malloc(sizeof(*mlist))) == NULL) {
243                 free(mfn);
244                 file_oomem(ms);
245                 return NULL;
246         }
247         mlist->next = mlist->prev = mlist;
248
249         while (fn) {
250                 p = strchr(fn, PATHSEP);
251                 if (p)
252                         *p++ = '\0';
253                 if (*fn == '\0')
254                         break;
255                 if (ms->flags & MAGIC_MIME) {
256                         if ((afn = malloc(strlen(fn) + 5 + 1)) == NULL) {
257                                 free(mfn);
258                                 free(mlist);
259                                 file_oomem(ms);
260                                 return NULL;
261                         }
262                         (void)strcpy(afn, fn);
263                         (void)strcat(afn, ".mime");
264                         fn = afn;
265                 }
266                 file_err = apprentice_1(ms, fn, action, mlist);
267                 if (file_err > errs)
268                         errs = file_err;
269                 if (afn) {
270                         free(afn);
271                         afn = NULL;
272                 }
273                 fn = p;
274         }
275         if (errs == -1) {
276                 free(mfn);
277                 free(mlist);
278                 mlist = NULL;
279                 file_error(ms, 0, "could not find any magic files!");
280                 return NULL;
281         }
282         free(mfn);
283         return mlist;
284 }
285
286 /*
287  * parse from a file
288  * const char *fn: name of magic file
289  */
290 private int
291 apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
292     const char *fn, int action)
293 {
294         private const char hdr[] =
295                 "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
296         FILE *f;
297         char line[BUFSIZ+1];
298         int errs = 0;
299
300         f = fopen(ms->file = fn, "r");
301         if (f == NULL) {
302                 if (errno != ENOENT)
303                         file_error(ms, errno, "cannot read magic file `%s'",
304                             fn);
305                 return -1;
306         }
307
308         maxmagic = MAXMAGIS;
309         *magicp = (struct magic *) calloc(maxmagic, sizeof(struct magic));
310         if (*magicp == NULL) {
311                 (void)fclose(f);
312                 file_oomem(ms);
313                 return -1;
314         }
315
316         /* print silly verbose header for USG compat. */
317         if (action == FILE_CHECK)
318                 (void)fprintf(stderr, "%s\n", hdr);
319
320         /* parse it */
321         for (ms->line = 1; fgets(line, BUFSIZ, f) != NULL; ms->line++) {
322                 size_t len;
323                 if (line[0]=='#')       /* comment, do not parse */
324                         continue;
325                 len = strlen(line);
326                 if (len < 2) /* null line, garbage, etc */
327                         continue;
328                 line[len - 1] = '\0'; /* delete newline */
329                 if (parse(ms, magicp, nmagicp, line, action) != 0)
330                         errs = 1;
331         }
332
333         (void)fclose(f);
334         if (errs) {
335                 free(*magicp);
336                 *magicp = NULL;
337                 *nmagicp = 0;
338         }
339         return errs;
340 }
341
342 /*
343  * extend the sign bit if the comparison is to be signed
344  */
345 protected uint32_t
346 file_signextend(struct magic_set *ms, struct magic *m, uint32_t v)
347 {
348         if (!(m->flag & UNSIGNED))
349                 switch(m->type) {
350                 /*
351                  * Do not remove the casts below.  They are
352                  * vital.  When later compared with the data,
353                  * the sign extension must have happened.
354                  */
355                 case FILE_BYTE:
356                         v = (char) v;
357                         break;
358                 case FILE_SHORT:
359                 case FILE_BESHORT:
360                 case FILE_LESHORT:
361                         v = (short) v;
362                         break;
363                 case FILE_DATE:
364                 case FILE_BEDATE:
365                 case FILE_LEDATE:
366                 case FILE_LDATE:
367                 case FILE_BELDATE:
368                 case FILE_LELDATE:
369                 case FILE_LONG:
370                 case FILE_BELONG:
371                 case FILE_LELONG:
372                         v = (int32_t) v;
373                         break;
374                 case FILE_STRING:
375                 case FILE_PSTRING:
376                 case FILE_BESTRING16:
377                 case FILE_LESTRING16:
378                         break;
379                 case FILE_REGEX:
380                         break;
381                 default:
382                         if (ms->flags & MAGIC_CHECK)
383                             file_magwarn(ms, "cannot happen: m->type=%d\n",
384                                     m->type);
385                         return ~0U;
386                 }
387         return v;
388 }
389
390 /*
391  * parse one line from magic file, put into magic[index++] if valid
392  */
393 private int
394 parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
395     int action)
396 {
397         int i = 0;
398         struct magic *m;
399         char *t;
400         private const char *fops = FILE_OPS;
401         uint32_t val;
402
403 #define ALLOC_INCR      200
404         if (*nmagicp + 1 >= maxmagic){
405                 maxmagic += ALLOC_INCR;
406                 if ((m = (struct magic *) realloc(*magicp,
407                     sizeof(struct magic) * maxmagic)) == NULL) {
408                         file_oomem(ms);
409                         if (*magicp)
410                                 free(*magicp);
411                         return -1;
412                 }
413                 *magicp = m;
414                 memset(&(*magicp)[*nmagicp], 0, sizeof(struct magic)
415                     * ALLOC_INCR);
416         }
417         m = &(*magicp)[*nmagicp];
418         m->flag = 0;
419         m->cont_level = 0;
420
421         while (*l == '>') {
422                 ++l;            /* step over */
423                 m->cont_level++; 
424         }
425
426         if (m->cont_level != 0 && *l == '(') {
427                 ++l;            /* step over */
428                 m->flag |= INDIR;
429         }
430         if (m->cont_level != 0 && *l == '&') {
431                 ++l;            /* step over */
432                 m->flag |= OFFADD;
433         }
434
435         /* get offset, then skip over it */
436         m->offset = (uint32_t)strtoul(l, &t, 0);
437         if (l == t)
438                 if (ms->flags & MAGIC_CHECK)
439                         file_magwarn(ms, "offset `%s' invalid", l);
440         l = t;
441
442         if (m->flag & INDIR) {
443                 m->in_type = FILE_LONG;
444                 m->in_offset = 0;
445                 /*
446                  * read [.lbs][+-]nnnnn)
447                  */
448                 if (*l == '.') {
449                         l++;
450                         switch (*l) {
451                         case 'l':
452                                 m->in_type = FILE_LELONG;
453                                 break;
454                         case 'L':
455                                 m->in_type = FILE_BELONG;
456                                 break;
457                         case 'h':
458                         case 's':
459                                 m->in_type = FILE_LESHORT;
460                                 break;
461                         case 'H':
462                         case 'S':
463                                 m->in_type = FILE_BESHORT;
464                                 break;
465                         case 'c':
466                         case 'b':
467                         case 'C':
468                         case 'B':
469                                 m->in_type = FILE_BYTE;
470                                 break;
471                         default:
472                                 if (ms->flags & MAGIC_CHECK)
473                                         file_magwarn(ms,
474                                             "indirect offset type `%c' invalid",
475                                             *l);
476                                 break;
477                         }
478                         l++;
479                 }
480                 if (*l == '~') {
481                         m->in_op = FILE_OPINVERSE;
482                         l++;
483                 }
484                 switch (*l) {
485                 case '&':
486                         m->in_op |= FILE_OPAND;
487                         l++;
488                         break;
489                 case '|':
490                         m->in_op |= FILE_OPOR;
491                         l++;
492                         break;
493                 case '^':
494                         m->in_op |= FILE_OPXOR;
495                         l++;
496                         break;
497                 case '+':
498                         m->in_op |= FILE_OPADD;
499                         l++;
500                         break;
501                 case '-':
502                         m->in_op |= FILE_OPMINUS;
503                         l++;
504                         break;
505                 case '*':
506                         m->in_op |= FILE_OPMULTIPLY;
507                         l++;
508                         break;
509                 case '/':
510                         m->in_op |= FILE_OPDIVIDE;
511                         l++;
512                         break;
513                 case '%':
514                         m->in_op |= FILE_OPMODULO;
515                         l++;
516                         break;
517                 }
518                 if (isdigit((unsigned char)*l)) 
519                         m->in_offset = (uint32_t)strtoul(l, &t, 0);
520                 else
521                         t = l;
522                 if (*t++ != ')') 
523                         if (ms->flags & MAGIC_CHECK)
524                                 file_magwarn(ms,
525                                     "missing ')' in indirect offset");
526                 l = t;
527         }
528
529
530         while (isascii((unsigned char)*l) && isdigit((unsigned char)*l))
531                 ++l;
532         EATAB;
533
534 #define NBYTE           4
535 #define NSHORT          5
536 #define NLONG           4
537 #define NSTRING         6
538 #define NDATE           4
539 #define NBESHORT        7
540 #define NBELONG         6
541 #define NBEDATE         6
542 #define NLESHORT        7
543 #define NLELONG         6
544 #define NLEDATE         6
545 #define NPSTRING        7
546 #define NLDATE          5
547 #define NBELDATE        7
548 #define NLELDATE        7
549 #define NREGEX          5
550 #define NBESTRING16     10
551 #define NLESTRING16     10
552
553         if (*l == 'u') {
554                 ++l;
555                 m->flag |= UNSIGNED;
556         }
557
558         /* get type, skip it */
559         if (strncmp(l, "char", NBYTE)==0) {     /* HP/UX compat */
560                 m->type = FILE_BYTE;
561                 l += NBYTE;
562         } else if (strncmp(l, "byte", NBYTE)==0) {
563                 m->type = FILE_BYTE;
564                 l += NBYTE;
565         } else if (strncmp(l, "short", NSHORT)==0) {
566                 m->type = FILE_SHORT;
567                 l += NSHORT;
568         } else if (strncmp(l, "long", NLONG)==0) {
569                 m->type = FILE_LONG;
570                 l += NLONG;
571         } else if (strncmp(l, "string", NSTRING)==0) {
572                 m->type = FILE_STRING;
573                 l += NSTRING;
574         } else if (strncmp(l, "date", NDATE)==0) {
575                 m->type = FILE_DATE;
576                 l += NDATE;
577         } else if (strncmp(l, "beshort", NBESHORT)==0) {
578                 m->type = FILE_BESHORT;
579                 l += NBESHORT;
580         } else if (strncmp(l, "belong", NBELONG)==0) {
581                 m->type = FILE_BELONG;
582                 l += NBELONG;
583         } else if (strncmp(l, "bedate", NBEDATE)==0) {
584                 m->type = FILE_BEDATE;
585                 l += NBEDATE;
586         } else if (strncmp(l, "leshort", NLESHORT)==0) {
587                 m->type = FILE_LESHORT;
588                 l += NLESHORT;
589         } else if (strncmp(l, "lelong", NLELONG)==0) {
590                 m->type = FILE_LELONG;
591                 l += NLELONG;
592         } else if (strncmp(l, "ledate", NLEDATE)==0) {
593                 m->type = FILE_LEDATE;
594                 l += NLEDATE;
595         } else if (strncmp(l, "pstring", NPSTRING)==0) {
596                 m->type = FILE_PSTRING;
597                 l += NPSTRING;
598         } else if (strncmp(l, "ldate", NLDATE)==0) {
599                 m->type = FILE_LDATE;
600                 l += NLDATE;
601         } else if (strncmp(l, "beldate", NBELDATE)==0) {
602                 m->type = FILE_BELDATE;
603                 l += NBELDATE;
604         } else if (strncmp(l, "leldate", NLELDATE)==0) {
605                 m->type = FILE_LELDATE;
606                 l += NLELDATE;
607         } else if (strncmp(l, "regex", NREGEX)==0) {
608                 m->type = FILE_REGEX;
609                 l += NREGEX;
610         } else if (strncmp(l, "bestring16", NBESTRING16)==0) {
611                 m->type = FILE_BESTRING16;
612                 l += NBESTRING16;
613         } else if (strncmp(l, "lestring16", NLESTRING16)==0) {
614                 m->type = FILE_LESTRING16;
615                 l += NLESTRING16;
616         } else {
617                 if (ms->flags & MAGIC_CHECK)
618                         file_magwarn(ms, "type `%s' invalid", l);
619                 return -1;
620         }
621         /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
622         /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
623         if (*l == '~') {
624                 if (!IS_STRING(m->type))
625                         m->mask_op = FILE_OPINVERSE;
626                 ++l;
627         }
628         if ((t = strchr(fops,  *l)) != NULL) {
629                 uint32_t op = (uint32_t)(t - fops);
630                 if (op != FILE_OPDIVIDE || !IS_STRING(m->type)) {
631                         ++l;
632                         m->mask_op |= op;
633                         val = (uint32_t)strtoul(l, &l, 0);
634                         m->mask = file_signextend(ms, m, val);
635                         eatsize(&l);
636                 } else {
637                         m->mask = 0L;
638                         while (!isspace((unsigned char)*++l)) {
639                                 switch (*l) {
640                                 case CHAR_IGNORE_LOWERCASE:
641                                         m->mask |= STRING_IGNORE_LOWERCASE;
642                                         break;
643                                 case CHAR_COMPACT_BLANK:
644                                         m->mask |= STRING_COMPACT_BLANK;
645                                         break;
646                                 case CHAR_COMPACT_OPTIONAL_BLANK:
647                                         m->mask |=
648                                             STRING_COMPACT_OPTIONAL_BLANK;
649                                         break;
650                                 default:
651                                         if (ms->flags & MAGIC_CHECK)
652                                                 file_magwarn(ms,
653                                                 "string extension `%c' invalid",
654                                                 *l);
655                                         return -1;
656                                 }
657                         }
658                 }
659         }
660         /*
661          * We used to set mask to all 1's here, instead let's just not do
662          * anything if mask = 0 (unless you have a better idea)
663          */
664         EATAB;
665   
666         switch (*l) {
667         case '>':
668         case '<':
669         /* Old-style anding: "0 byte &0x80 dynamically linked" */
670         case '&':
671         case '^':
672         case '=':
673                 m->reln = *l;
674                 ++l;
675                 if (*l == '=') {
676                    /* HP compat: ignore &= etc. */
677                    ++l;
678                 }
679                 break;
680         case '!':
681                 if (!IS_STRING(m->type)) {
682                         m->reln = *l;
683                         ++l;
684                         break;
685                 }
686                 /*FALLTHROUGH*/
687         default:
688                 if (*l == 'x' && isascii((unsigned char)l[1]) && 
689                     isspace((unsigned char)l[1])) {
690                         m->reln = *l;
691                         ++l;
692                         goto GetDesc;   /* Bill The Cat */
693                 }
694                 m->reln = '=';
695                 break;
696         }
697         EATAB;
698   
699         if (getvalue(ms, m, &l))
700                 return -1;
701         /*
702          * TODO finish this macro and start using it!
703          * #define offsetcheck {if (offset > HOWMANY-1) 
704          *      magwarn("offset too big"); }
705          */
706
707         /*
708          * now get last part - the description
709          */
710 GetDesc:
711         EATAB;
712         if (l[0] == '\b') {
713                 ++l;
714                 m->nospflag = 1;
715         } else if ((l[0] == '\\') && (l[1] == 'b')) {
716                 ++l;
717                 ++l;
718                 m->nospflag = 1;
719         } else
720                 m->nospflag = 0;
721         while ((m->desc[i++] = *l++) != '\0' && i < MAXDESC)
722                 /* NULLBODY */;
723
724         if (ms->flags & MAGIC_CHECK) {
725                 if (!check_format(ms, m))
726                         return -1;
727         }
728 #ifndef COMPILE_ONLY
729         if (action == FILE_CHECK) {
730                 file_mdump(m);
731         }
732 #endif
733         ++(*nmagicp);           /* make room for next */
734         return 0;
735 }
736
737 /*
738  * Check that the optional printf format in description matches
739  * the type of the magic.
740  */
741 private int
742 check_format(struct magic_set *ms, struct magic *m)
743 {
744         static const char *formats[] = { FILE_FORMAT_STRING };
745         static const char *names[] = { FILE_FORMAT_NAME };
746         char *ptr;
747
748         for (ptr = m->desc; *ptr; ptr++)
749                 if (*ptr == '%')
750                         break;
751         if (*ptr == '\0') {
752                 /* No format string; ok */
753                 return 1;
754         }
755         if (m->type >= sizeof(formats)/sizeof(formats[0])) {
756                 file_magwarn(ms, "Internal error inconsistency between m->type"
757                     " and format strings");
758                 return 0;
759         }
760         if (formats[m->type] == NULL) {
761                 file_magwarn(ms, "No format string for `%s' with description "
762                     "`%s'", m->desc, names[m->type]);
763                 return 0;
764         }
765         for (; *ptr; ptr++) {
766                 if (*ptr == 'l' || *ptr == 'h') {
767                         /* XXX: we should really fix this one day */
768                         continue;
769                 }
770                 if (islower((unsigned char)*ptr) || *ptr == 'X')
771                         break;
772         }
773         if (*ptr == '\0') {
774                 /* Missing format string; bad */
775                 file_magwarn(ms, "Invalid format `%s' for type `%s'",
776                         m->desc, names[m->type]);
777                 return 0;
778         }
779         if (strchr(formats[m->type], *ptr) == NULL) {
780                 file_magwarn(ms, "Printf format `%c' is not valid for type `%s'"
781                     " in description `%s'",
782                         *ptr, names[m->type], m->desc);
783                 return 0;
784         }
785         return 1;
786 }
787
788 /* 
789  * Read a numeric value from a pointer, into the value union of a magic 
790  * pointer, according to the magic type.  Update the string pointer to point 
791  * just after the number read.  Return 0 for success, non-zero for failure.
792  */
793 private int
794 getvalue(struct magic_set *ms, struct magic *m, char **p)
795 {
796         int slen;
797
798         switch (m->type) {
799         case FILE_BESTRING16:
800         case FILE_LESTRING16:
801         case FILE_STRING:
802         case FILE_PSTRING:
803         case FILE_REGEX:
804                 *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen);
805                 if (*p == NULL) {
806                         if (ms->flags & MAGIC_CHECK)
807                                 file_magwarn(ms, "cannot get string from `%s'",
808                                     m->value.s);
809                         return -1;
810                 }
811                 m->vallen = slen;
812                 return 0;
813         default:
814                 if (m->reln != 'x') {
815                         m->value.l = file_signextend(ms, m,
816                             (uint32_t)strtoul(*p, p, 0));
817                         eatsize(p);
818                 }
819                 return 0;
820         }
821 }
822
823 /*
824  * Convert a string containing C character escapes.  Stop at an unescaped
825  * space or tab.
826  * Copy the converted version to "p", returning its length in *slen.
827  * Return updated scan pointer as function result.
828  */
829 private char *
830 getstr(struct magic_set *ms, char *s, char *p, int plen, int *slen)
831 {
832         char    *origs = s, *origp = p;
833         char    *pmax = p + plen - 1;
834         int     c;
835         int     val;
836
837         while ((c = *s++) != '\0') {
838                 if (isspace((unsigned char) c))
839                         break;
840                 if (p >= pmax) {
841                         file_error(ms, 0, "string too long: `%s'", origs);
842                         return NULL;
843                 }
844                 if(c == '\\') {
845                         switch(c = *s++) {
846
847                         case '\0':
848                                 goto out;
849
850                         default:
851                                 *p++ = (char) c;
852                                 break;
853
854                         case 'n':
855                                 *p++ = '\n';
856                                 break;
857
858                         case 'r':
859                                 *p++ = '\r';
860                                 break;
861
862                         case 'b':
863                                 *p++ = '\b';
864                                 break;
865
866                         case 't':
867                                 *p++ = '\t';
868                                 break;
869
870                         case 'f':
871                                 *p++ = '\f';
872                                 break;
873
874                         case 'v':
875                                 *p++ = '\v';
876                                 break;
877
878                         /* \ and up to 3 octal digits */
879                         case '0':
880                         case '1':
881                         case '2':
882                         case '3':
883                         case '4':
884                         case '5':
885                         case '6':
886                         case '7':
887                                 val = c - '0';
888                                 c = *s++;  /* try for 2 */
889                                 if(c >= '0' && c <= '7') {
890                                         val = (val<<3) | (c - '0');
891                                         c = *s++;  /* try for 3 */
892                                         if(c >= '0' && c <= '7')
893                                                 val = (val<<3) | (c-'0');
894                                         else
895                                                 --s;
896                                 }
897                                 else
898                                         --s;
899                                 *p++ = (char)val;
900                                 break;
901
902                         /* \x and up to 2 hex digits */
903                         case 'x':
904                                 val = 'x';      /* Default if no digits */
905                                 c = hextoint(*s++);     /* Get next char */
906                                 if (c >= 0) {
907                                         val = c;
908                                         c = hextoint(*s++);
909                                         if (c >= 0)
910                                                 val = (val << 4) + c;
911                                         else
912                                                 --s;
913                                 } else
914                                         --s;
915                                 *p++ = (char)val;
916                                 break;
917                         }
918                 } else
919                         *p++ = (char)c;
920         }
921 out:
922         *p = '\0';
923         *slen = p - origp;
924         return s;
925 }
926
927
928 /* Single hex char to int; -1 if not a hex char. */
929 private int
930 hextoint(int c)
931 {
932         if (!isascii((unsigned char) c))
933                 return -1;
934         if (isdigit((unsigned char) c))
935                 return c - '0';
936         if ((c >= 'a')&&(c <= 'f'))
937                 return c + 10 - 'a';
938         if (( c>= 'A')&&(c <= 'F'))
939                 return c + 10 - 'A';
940         return -1;
941 }
942
943
944 /*
945  * Print a string containing C character escapes.
946  */
947 protected void
948 file_showstr(FILE *fp, const char *s, size_t len)
949 {
950         char    c;
951
952         for (;;) {
953                 c = *s++;
954                 if (len == ~0U) {
955                         if (c == '\0')
956                                 break;
957                 }
958                 else  {
959                         if (len-- == 0)
960                                 break;
961                 }
962                 if(c >= 040 && c <= 0176)       /* TODO isprint && !iscntrl */
963                         (void) fputc(c, fp);
964                 else {
965                         (void) fputc('\\', fp);
966                         switch (c) {
967                         
968                         case '\n':
969                                 (void) fputc('n', fp);
970                                 break;
971
972                         case '\r':
973                                 (void) fputc('r', fp);
974                                 break;
975
976                         case '\b':
977                                 (void) fputc('b', fp);
978                                 break;
979
980                         case '\t':
981                                 (void) fputc('t', fp);
982                                 break;
983
984                         case '\f':
985                                 (void) fputc('f', fp);
986                                 break;
987
988                         case '\v':
989                                 (void) fputc('v', fp);
990                                 break;
991
992                         default:
993                                 (void) fprintf(fp, "%.3o", c & 0377);
994                                 break;
995                         }
996                 }
997         }
998 }
999
1000 /*
1001  * eatsize(): Eat the size spec from a number [eg. 10UL]
1002  */
1003 private void
1004 eatsize(char **p)
1005 {
1006         char *l = *p;
1007
1008         if (LOWCASE(*l) == 'u') 
1009                 l++;
1010
1011         switch (LOWCASE(*l)) {
1012         case 'l':    /* long */
1013         case 's':    /* short */
1014         case 'h':    /* short */
1015         case 'b':    /* char/byte */
1016         case 'c':    /* char/byte */
1017                 l++;
1018                 /*FALLTHROUGH*/
1019         default:
1020                 break;
1021         }
1022
1023         *p = l;
1024 }
1025
1026 /*
1027  * handle a compiled file.
1028  */
1029 private int
1030 apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
1031     const char *fn)
1032 {
1033         int fd;
1034         struct stat st;
1035         uint32_t *ptr;
1036         uint32_t version;
1037         int needsbyteswap;
1038         char buf[MAXPATHLEN];
1039         char *dbname = mkdbname(fn, buf, sizeof(buf), 0);
1040         void *mm = NULL;
1041
1042         if (dbname == NULL)
1043                 return -1;
1044
1045         if ((fd = open(dbname, O_RDONLY)) == -1)
1046                 return -1;
1047
1048         if (fstat(fd, &st) == -1) {
1049                 file_error(ms, errno, "cannot stat `%s'", dbname);
1050                 goto error;
1051         }
1052         if (st.st_size < 16) {
1053                 file_error(ms, 0, "file `%s' is too small", dbname);
1054                 goto error;
1055         }
1056
1057 #ifdef QUICK
1058         if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
1059             MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
1060                 file_error(ms, errno, "cannot map `%s'", dbname);
1061                 goto error;
1062         }
1063 #define RET     2
1064 #else
1065         if ((mm = malloc((size_t)st.st_size)) == NULL) {
1066                 file_oomem(ms);
1067                 goto error;
1068         }
1069         if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
1070                 file_badread(ms);
1071                 goto error;
1072         }
1073 #define RET     1
1074 #endif
1075         *magicp = mm;
1076         (void)close(fd);
1077         fd = -1;
1078         ptr = (uint32_t *)(void *)*magicp;
1079         if (*ptr != MAGICNO) {
1080                 if (swap4(*ptr) != MAGICNO) {
1081                         file_error(ms, 0, "bad magic in `%s'");
1082                         goto error;
1083                 }
1084                 needsbyteswap = 1;
1085         } else
1086                 needsbyteswap = 0;
1087         if (needsbyteswap)
1088                 version = swap4(ptr[1]);
1089         else
1090                 version = ptr[1];
1091         if (version != VERSIONNO) {
1092                 file_error(ms, 0, "version mismatch (%d != %d) in `%s'",
1093                     version, VERSIONNO, dbname);
1094                 goto error;
1095         }
1096         *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic)) - 1;
1097         (*magicp)++;
1098         if (needsbyteswap)
1099                 byteswap(*magicp, *nmagicp);
1100         return RET;
1101
1102 error:
1103         if (fd != -1)
1104                 (void)close(fd);
1105         if (mm) {
1106 #ifdef QUICK
1107                 (void)munmap((void *)mm, (size_t)st.st_size);
1108 #else
1109                 free(mm);
1110 #endif
1111         } else {
1112                 *magicp = NULL;
1113                 *nmagicp = 0;
1114         }
1115         return -1;
1116 }
1117
1118 private const uint32_t ar[] = {
1119     MAGICNO, VERSIONNO
1120 };
1121 /*
1122  * handle an mmaped file.
1123  */
1124 private int
1125 apprentice_compile(struct magic_set *ms, struct magic **magicp,
1126     uint32_t *nmagicp, const char *fn)
1127 {
1128         int fd;
1129         char buf[MAXPATHLEN];
1130         char *dbname = mkdbname(fn, buf, sizeof(buf), 1);
1131
1132         if (dbname == NULL) 
1133                 return -1;
1134
1135         if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
1136                 file_error(ms, errno, "cannot open `%s'", dbname);
1137                 return -1;
1138         }
1139
1140         if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
1141                 file_error(ms, errno, "error writing `%s'", dbname);
1142                 return -1;
1143         }
1144
1145         if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
1146             != sizeof(struct magic)) {
1147                 file_error(ms, errno, "error seeking `%s'", dbname);
1148                 return -1;
1149         }
1150
1151         if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp)) 
1152             != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
1153                 file_error(ms, errno, "error writing `%s'", dbname);
1154                 return -1;
1155         }
1156
1157         (void)close(fd);
1158         return 0;
1159 }
1160
1161 private const char ext[] = ".mgc";
1162 /*
1163  * make a dbname
1164  */
1165 private char *
1166 mkdbname(const char *fn, char *buf, size_t bufsiz, int strip)
1167 {
1168         if (strip) {
1169                 const char *p;
1170                 if ((p = strrchr(fn, '/')) != NULL)
1171                         fn = ++p;
1172         }
1173
1174         (void)snprintf(buf, bufsiz, "%s%s", fn, ext);
1175         return buf;
1176 }
1177
1178 /*
1179  * Byteswap an mmap'ed file if needed
1180  */
1181 private void
1182 byteswap(struct magic *magic, uint32_t nmagic)
1183 {
1184         uint32_t i;
1185         for (i = 0; i < nmagic; i++)
1186                 bs1(&magic[i]);
1187 }
1188
1189 /*
1190  * swap a short
1191  */
1192 private uint16_t
1193 swap2(uint16_t sv)
1194 {
1195         uint16_t rv;
1196         uint8_t *s = (uint8_t *)(void *)&sv; 
1197         uint8_t *d = (uint8_t *)(void *)&rv; 
1198         d[0] = s[1];
1199         d[1] = s[0];
1200         return rv;
1201 }
1202
1203 /*
1204  * swap an int
1205  */
1206 private uint32_t
1207 swap4(uint32_t sv)
1208 {
1209         uint32_t rv;
1210         uint8_t *s = (uint8_t *)(void *)&sv; 
1211         uint8_t *d = (uint8_t *)(void *)&rv; 
1212         d[0] = s[3];
1213         d[1] = s[2];
1214         d[2] = s[1];
1215         d[3] = s[0];
1216         return rv;
1217 }
1218
1219 /*
1220  * byteswap a single magic entry
1221  */
1222 private void
1223 bs1(struct magic *m)
1224 {
1225         m->cont_level = swap2(m->cont_level);
1226         m->offset = swap4((uint32_t)m->offset);
1227         m->in_offset = swap4((uint32_t)m->in_offset);
1228         if (IS_STRING(m->type))
1229                 m->value.l = swap4(m->value.l);
1230         m->mask = swap4(m->mask);
1231 }