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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * apprentice - make one pass through /etc/magic, learning its secrets.
34 #include "patchlevel.h"
44 #include <sys/param.h>
50 FILE_RCSID("@(#)$File: apprentice.c,v 1.109 2007/12/27 20:52:36 christos Exp $")
53 #define EATAB {while (isascii((unsigned char) *l) && \
54 isspace((unsigned char) *l)) ++l;}
55 #define LOWCASE(l) (isupper((unsigned char) (l)) ? \
56 tolower((unsigned char) (l)) : (l))
58 * Work around a bug in headers on Digital Unix.
59 * At least confirmed for: OSF1 V4.0 878
61 #if defined(__osf__) && defined(__DECC)
68 #define MAP_FAILED (void *) -1
76 #define MAXPATHLEN 1024
85 int file_formats[FILE_NAMES_SIZE];
86 const size_t file_nformats = FILE_NAMES_SIZE;
87 const char *file_names[FILE_NAMES_SIZE];
88 const size_t file_nnames = FILE_NAMES_SIZE;
90 private int getvalue(struct magic_set *ms, struct magic *, const char **, int);
91 private int hextoint(int);
92 private const char *getstr(struct magic_set *, const char *, char *, int,
94 private int parse(struct magic_set *, struct magic_entry **, uint32_t *,
95 const char *, size_t, int);
96 private void eatsize(const char **);
97 private int apprentice_1(struct magic_set *, const char *, int, struct mlist *);
98 private size_t apprentice_magic_strength(const struct magic *);
99 private int apprentice_sort(const void *, const void *);
100 private int apprentice_file(struct magic_set *, struct magic **, uint32_t *,
102 private void byteswap(struct magic *, uint32_t);
103 private void bs1(struct magic *);
104 private uint16_t swap2(uint16_t);
105 private uint32_t swap4(uint32_t);
106 private uint64_t swap8(uint64_t);
107 private char *mkdbname(const char *, char *, size_t, int);
108 private int apprentice_map(struct magic_set *, struct magic **, uint32_t *,
110 private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
112 private int check_format_type(const char *, int);
113 private int check_format(struct magic_set *, struct magic *);
114 private int get_op(char);
116 private size_t maxmagic = 0;
117 private size_t magicsize = sizeof(struct magic);
122 int main(int, char *[]);
125 main(int argc, char *argv[])
128 struct magic_set *ms;
131 if ((progname = strrchr(argv[0], '/')) != NULL)
137 (void)fprintf(stderr, "Usage: %s file\n", progname);
141 if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
142 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
145 ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
147 (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
151 #endif /* COMPILE_ONLY */
153 static const struct type_tbl_s {
159 # define XX(s) s, (sizeof(s) - 1)
160 # define XX_NULL NULL, 0
161 { XX("byte"), FILE_BYTE, FILE_FMT_NUM },
162 { XX("short"), FILE_SHORT, FILE_FMT_NUM },
163 { XX("default"), FILE_DEFAULT, FILE_FMT_STR },
164 { XX("long"), FILE_LONG, FILE_FMT_NUM },
165 { XX("string"), FILE_STRING, FILE_FMT_STR },
166 { XX("date"), FILE_DATE, FILE_FMT_STR },
167 { XX("beshort"), FILE_BESHORT, FILE_FMT_NUM },
168 { XX("belong"), FILE_BELONG, FILE_FMT_NUM },
169 { XX("bedate"), FILE_BEDATE, FILE_FMT_STR },
170 { XX("leshort"), FILE_LESHORT, FILE_FMT_NUM },
171 { XX("lelong"), FILE_LELONG, FILE_FMT_NUM },
172 { XX("ledate"), FILE_LEDATE, FILE_FMT_STR },
173 { XX("pstring"), FILE_PSTRING, FILE_FMT_STR },
174 { XX("ldate"), FILE_LDATE, FILE_FMT_STR },
175 { XX("beldate"), FILE_BELDATE, FILE_FMT_STR },
176 { XX("leldate"), FILE_LELDATE, FILE_FMT_STR },
177 { XX("regex"), FILE_REGEX, FILE_FMT_STR },
178 { XX("bestring16"), FILE_BESTRING16, FILE_FMT_STR },
179 { XX("lestring16"), FILE_LESTRING16, FILE_FMT_STR },
180 { XX("search"), FILE_SEARCH, FILE_FMT_STR },
181 { XX("medate"), FILE_MEDATE, FILE_FMT_STR },
182 { XX("meldate"), FILE_MELDATE, FILE_FMT_STR },
183 { XX("melong"), FILE_MELONG, FILE_FMT_NUM },
184 { XX("quad"), FILE_QUAD, FILE_FMT_QUAD },
185 { XX("lequad"), FILE_LEQUAD, FILE_FMT_QUAD },
186 { XX("bequad"), FILE_BEQUAD, FILE_FMT_QUAD },
187 { XX("qdate"), FILE_QDATE, FILE_FMT_STR },
188 { XX("leqdate"), FILE_LEQDATE, FILE_FMT_STR },
189 { XX("beqdate"), FILE_BEQDATE, FILE_FMT_STR },
190 { XX("qldate"), FILE_QLDATE, FILE_FMT_STR },
191 { XX("leqldate"), FILE_LEQLDATE, FILE_FMT_STR },
192 { XX("beqldate"), FILE_BEQLDATE, FILE_FMT_STR },
193 { XX("float"), FILE_FLOAT, FILE_FMT_FLOAT },
194 { XX("befloat"), FILE_BEFLOAT, FILE_FMT_FLOAT },
195 { XX("lefloat"), FILE_LEFLOAT, FILE_FMT_FLOAT },
196 { XX("double"), FILE_DOUBLE, FILE_FMT_DOUBLE },
197 { XX("bedouble"), FILE_BEDOUBLE, FILE_FMT_DOUBLE },
198 { XX("ledouble"), FILE_LEDOUBLE, FILE_FMT_DOUBLE },
199 { XX_NULL, FILE_INVALID, FILE_FMT_NONE },
205 get_type(const char *l, const char **t)
207 const struct type_tbl_s *p;
209 for (p = type_tbl; p->name; p++) {
210 if (strncmp(l, p->name, p->len) == 0) {
220 init_file_tables(void)
223 const struct type_tbl_s *p;
229 for (p = type_tbl; p->name; p++) {
230 assert(p->type < FILE_NAMES_SIZE);
231 file_names[p->type] = p->name;
232 file_formats[p->type] = p->format;
240 apprentice_1(struct magic_set *ms, const char *fn, int action,
243 struct magic *magic = NULL;
249 if (magicsize != FILE_MAGICSIZE) {
250 file_error(ms, 0, "magic element size %lu != %lu",
251 (unsigned long)sizeof(*magic),
252 (unsigned long)FILE_MAGICSIZE);
256 if (action == FILE_COMPILE) {
257 rv = apprentice_file(ms, &magic, &nmagic, fn, action);
260 rv = apprentice_compile(ms, &magic, &nmagic, fn);
266 if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
267 if (ms->flags & MAGIC_CHECK)
268 file_magwarn(ms, "using regular magic file `%s'", fn);
269 rv = apprentice_file(ms, &magic, &nmagic, fn, action);
276 if (magic == NULL || nmagic == 0) {
277 file_delmagic(magic, mapped, nmagic);
281 if ((ml = malloc(sizeof(*ml))) == NULL) {
282 file_delmagic(magic, mapped, nmagic);
283 file_oomem(ms, sizeof(*ml));
291 mlist->prev->next = ml;
292 ml->prev = mlist->prev;
297 #endif /* COMPILE_ONLY */
301 file_delmagic(struct magic *p, int type, size_t entries)
309 (void)munmap((void *)p, sizeof(*p) * (entries + 1));
323 /* const char *fn: list of magic files */
324 protected struct mlist *
325 file_apprentice(struct magic_set *ms, const char *fn, int action)
327 char *p, *mfn, *afn = NULL;
328 int file_err, errs = -1;
330 static const char mime[] = ".mime";
335 fn = getenv("MAGIC");
339 if ((mfn = strdup(fn)) == NULL) {
340 file_oomem(ms, strlen(fn));
345 if ((mlist = malloc(sizeof(*mlist))) == NULL) {
347 file_oomem(ms, sizeof(*mlist));
350 mlist->next = mlist->prev = mlist;
353 p = strchr(fn, PATHSEP);
358 if (ms->flags & MAGIC_MIME) {
359 size_t len = strlen(fn) + sizeof(mime);
360 if ((afn = malloc(len)) == NULL) {
366 (void)strcpy(afn, fn);
367 (void)strcat(afn, mime);
370 file_err = apprentice_1(ms, fn, action, mlist);
383 file_error(ms, 0, "could not find any magic files!");
391 * Get weight of this magic entry, for sorting purposes.
394 apprentice_magic_strength(const struct magic *m)
397 size_t val = 2 * MULT; /* baseline strength */
400 case FILE_DEFAULT: /* make sure this sorts last */
422 val += m->vallen * MULT;
425 case FILE_BESTRING16:
426 case FILE_LESTRING16:
427 val += m->vallen * MULT / 2;
466 (void)fprintf(stderr, "Bad type %d\n", m->type);
471 case 'x': /* matches anything penalize */
476 case '=': /* Exact match, prefer */
481 case '<': /* comparison match reduce strength */
486 case '&': /* masking bits, we could count them too */
491 (void)fprintf(stderr, "Bad relation %c\n", m->reln);
495 if (val == 0) /* ensure we only return 0 for FILE_DEFAULT */
502 * Sort callback for sorting entries by "strength" (basically length)
505 apprentice_sort(const void *a, const void *b)
507 const struct magic_entry *ma = a;
508 const struct magic_entry *mb = b;
509 size_t sa = apprentice_magic_strength(ma->mp);
510 size_t sb = apprentice_magic_strength(mb->mp);
521 * const char *fn: name of magic file
524 apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
525 const char *fn, int action)
527 private const char hdr[] =
528 "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
532 struct magic_entry *marray;
533 uint32_t marraycount, i, mentrycount = 0;
536 ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
538 f = fopen(ms->file = fn, "r");
541 file_error(ms, errno, "cannot read magic file `%s'",
547 if ((marray = calloc(maxmagic, sizeof(*marray))) == NULL) {
549 file_oomem(ms, maxmagic * sizeof(*marray));
554 /* print silly verbose header for USG compat. */
555 if (action == FILE_CHECK)
556 (void)fprintf(stderr, "%s\n", hdr);
558 /* read and parse this file */
559 for (ms->line = 1; fgets(line, sizeof(line), f) != NULL; ms->line++) {
562 if (len == 0) /* null line, garbage, etc */
564 if (line[len - 1] == '\n') {
566 line[len - 1] = '\0'; /* delete newline */
568 if (line[0] == '\0') /* empty, do not parse */
570 if (line[0] == '#') /* comment, do not parse */
572 if (parse(ms, &marray, &marraycount, line, lineno, action) != 0)
581 qsort(marray, marraycount, sizeof(*marray), apprentice_sort);
583 * Make sure that any level 0 "default" line is last (if one exists).
585 for (i = 0; i < marraycount; i++) {
586 if (marray[i].mp->cont_level == 0 &&
587 marray[i].mp->type == FILE_DEFAULT) {
588 while (++i < marraycount)
589 if (marray[i].mp->cont_level == 0)
591 if (i != marraycount) {
592 ms->line = marray[i].mp->lineno; /* XXX - Ugh! */
594 "level 0 \"default\" did not sort last");
601 for (i = 0; i < marraycount; i++)
602 mentrycount += marray[i].cont_count;
604 if ((*magicp = malloc(sizeof(**magicp) * mentrycount)) == NULL) {
605 file_oomem(ms, sizeof(**magicp) * mentrycount);
611 for (i = 0; i < marraycount; i++) {
612 (void)memcpy(*magicp + mentrycount, marray[i].mp,
613 marray[i].cont_count * sizeof(**magicp));
614 mentrycount += marray[i].cont_count;
617 for (i = 0; i < marraycount; i++)
625 *nmagicp = mentrycount;
632 * extend the sign bit if the comparison is to be signed
635 file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
637 if (!(m->flag & UNSIGNED)) {
640 * Do not remove the casts below. They are
641 * vital. When later compared with the data,
642 * the sign extension must have happened.
685 case FILE_BESTRING16:
686 case FILE_LESTRING16:
692 if (ms->flags & MAGIC_CHECK)
693 file_magwarn(ms, "cannot happen: m->type=%d\n",
702 string_modifier_check(struct magic_set *ms, struct magic const *m)
704 if ((ms->flags & MAGIC_CHECK) == 0)
708 case FILE_BESTRING16:
709 case FILE_LESTRING16:
710 if (m->str_flags != 0) {
711 file_magwarn(ms, "no modifiers allowed for 16-bit strings\n");
717 if ((m->str_flags & REGEX_OFFSET_START) != 0) {
718 file_magwarn(ms, "'/%c' only allowed on regex and search\n",
719 CHAR_REGEX_OFFSET_START);
726 if ((m->str_flags & STRING_COMPACT_BLANK) != 0) {
727 file_magwarn(ms, "'/%c' not allowed on regex\n",
731 if ((m->str_flags & STRING_COMPACT_OPTIONAL_BLANK) != 0) {
732 file_magwarn(ms, "'/%c' not allowed on regex\n",
733 CHAR_COMPACT_OPTIONAL_BLANK);
738 file_magwarn(ms, "coding error: m->type=%d\n",
760 return FILE_OPMULTIPLY;
762 return FILE_OPDIVIDE;
764 return FILE_OPMODULO;
770 #ifdef ENABLE_CONDITIONALS
772 get_cond(const char *l, const char **t)
774 static struct cond_tbl_s {
779 { "if", 2, COND_IF },
780 { "elif", 4, COND_ELIF },
781 { "else", 4, COND_ELSE },
782 { NULL, 0, COND_NONE },
784 struct cond_tbl_s *p;
786 for (p = cond_tbl; p->name; p++) {
787 if (strncmp(l, p->name, p->len) == 0 &&
788 isspace((unsigned char)l[p->len])) {
798 check_cond(struct magic_set *ms, int cond, uint32_t cont_level)
801 last_cond = ms->c.li[cont_level].last_cond;
805 if (last_cond != COND_NONE && last_cond != COND_ELIF) {
806 if (ms->flags & MAGIC_CHECK)
807 file_magwarn(ms, "syntax error: `if'");
814 if (last_cond != COND_IF && last_cond != COND_ELIF) {
815 if (ms->flags & MAGIC_CHECK)
816 file_magwarn(ms, "syntax error: `elif'");
819 last_cond = COND_ELIF;
823 if (last_cond != COND_IF && last_cond != COND_ELIF) {
824 if (ms->flags & MAGIC_CHECK)
825 file_magwarn(ms, "syntax error: `else'");
828 last_cond = COND_NONE;
832 last_cond = COND_NONE;
836 ms->c.li[cont_level].last_cond = last_cond;
839 #endif /* ENABLE_CONDITIONALS */
842 * parse one line from magic file, put into magic[index++] if valid
845 parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
846 const char *line, size_t lineno, int action)
848 #ifdef ENABLE_CONDITIONALS
849 static uint32_t last_cont_level = 0;
852 struct magic_entry *me;
854 const char *l = line;
865 #ifdef ENABLE_CONDITIONALS
866 if (cont_level == 0 || cont_level > last_cont_level)
867 if (file_check_mem(ms, cont_level) == -1)
869 last_cont_level = cont_level;
872 #define ALLOC_CHUNK (size_t)10
873 #define ALLOC_INCR (size_t)200
875 if (cont_level != 0) {
876 if (*nmentryp == 0) {
877 file_error(ms, 0, "No current entry for continuation");
880 me = &(*mentryp)[*nmentryp - 1];
881 if (me->cont_count == me->max_count) {
883 size_t cnt = me->max_count + ALLOC_CHUNK;
884 if ((nm = realloc(me->mp, sizeof(*nm) * cnt)) == NULL) {
885 file_oomem(ms, sizeof(*nm) * cnt);
891 m = &me->mp[me->cont_count++];
892 (void)memset(m, 0, sizeof(*m));
893 m->cont_level = cont_level;
895 if (*nmentryp == maxmagic) {
896 struct magic_entry *mp;
898 maxmagic += ALLOC_INCR;
899 if ((mp = realloc(*mentryp, sizeof(*mp) * maxmagic)) ==
901 file_oomem(ms, sizeof(*mp) * maxmagic);
904 (void)memset(&mp[*nmentryp], 0, sizeof(*mp) *
908 me = &(*mentryp)[*nmentryp];
909 if (me->mp == NULL) {
910 if ((m = malloc(sizeof(*m) * ALLOC_CHUNK)) == NULL) {
911 file_oomem(ms, sizeof(*m) * ALLOC_CHUNK);
915 me->max_count = ALLOC_CHUNK;
918 (void)memset(m, 0, sizeof(*m));
924 if (*l == '&') { /* m->cont_level == 0 checked below. */
931 if (m->flag & OFFADD)
932 m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
934 if (*l == '&') { /* m->cont_level == 0 checked below */
939 /* Indirect offsets are not valid at level 0. */
940 if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD)))
941 if (ms->flags & MAGIC_CHECK)
942 file_magwarn(ms, "relative offset at level 0");
944 /* get offset, then skip over it */
945 m->offset = (uint32_t)strtoul(l, &t, 0);
947 if (ms->flags & MAGIC_CHECK)
948 file_magwarn(ms, "offset `%s' invalid", l);
951 if (m->flag & INDIR) {
952 m->in_type = FILE_LONG;
955 * read [.lbs][+-]nnnnn)
961 m->in_type = FILE_LELONG;
964 m->in_type = FILE_BELONG;
967 m->in_type = FILE_MELONG;
971 m->in_type = FILE_LESHORT;
975 m->in_type = FILE_BESHORT;
981 m->in_type = FILE_BYTE;
986 m->in_type = FILE_LEDOUBLE;
991 m->in_type = FILE_BEDOUBLE;
994 if (ms->flags & MAGIC_CHECK)
996 "indirect offset type `%c' invalid",
1005 m->in_op |= FILE_OPINVERSE;
1008 if ((op = get_op(*l)) != -1) {
1013 m->in_op |= FILE_OPINDIRECT;
1016 if (isdigit((unsigned char)*l) || *l == '-') {
1017 m->in_offset = (int32_t)strtol(l, &t, 0);
1019 if (ms->flags & MAGIC_CHECK)
1021 "in_offset `%s' invalid", l);
1025 ((m->in_op & FILE_OPINDIRECT) && *l++ != ')'))
1026 if (ms->flags & MAGIC_CHECK)
1028 "missing ')' in indirect offset");
1032 #ifdef ENABLE_CONDITIONALS
1033 m->cond = get_cond(l, &l);
1034 if (check_cond(ms, m->cond, cont_level) == -1)
1042 m->flag |= UNSIGNED;
1045 m->type = get_type(l, &l);
1046 if (m->type == FILE_INVALID) {
1047 if (ms->flags & MAGIC_CHECK)
1048 file_magwarn(ms, "type `%s' invalid", l);
1052 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
1053 /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
1057 if (!IS_STRING(m->type))
1058 m->mask_op |= FILE_OPINVERSE;
1059 else if (ms->flags & MAGIC_CHECK)
1060 file_magwarn(ms, "'~' invalid for string types");
1066 if ((op = get_op(*l)) != -1) {
1067 if (!IS_STRING(m->type)) {
1071 val = (uint64_t)strtoull(l, &t, 0);
1073 m->num_mask = file_signextend(ms, m, val);
1076 else if (op == FILE_OPDIVIDE) {
1078 while (!isspace((unsigned char)*++l)) {
1080 /* for portability avoid "case '0' ... '9':" */
1081 case '0': case '1': case '2':
1082 case '3': case '4': case '5':
1083 case '6': case '7': case '8':
1085 if (have_count && ms->flags & MAGIC_CHECK)
1089 m->str_count = strtoul(l, &t, 0);
1093 case CHAR_COMPACT_BLANK:
1094 m->str_flags |= STRING_COMPACT_BLANK;
1096 case CHAR_COMPACT_OPTIONAL_BLANK:
1098 STRING_COMPACT_OPTIONAL_BLANK;
1100 case CHAR_IGNORE_LOWERCASE:
1101 m->str_flags |= STRING_IGNORE_LOWERCASE;
1103 case CHAR_IGNORE_UPPERCASE:
1104 m->str_flags |= STRING_IGNORE_UPPERCASE;
1106 case CHAR_REGEX_OFFSET_START:
1107 m->str_flags |= REGEX_OFFSET_START;
1110 if (ms->flags & MAGIC_CHECK)
1112 "string extension `%c' invalid",
1116 /* allow multiple '/' for readability */
1117 if (l[1] == '/' && !isspace((unsigned char)l[2]))
1120 if (string_modifier_check(ms, m) == -1)
1124 if (ms->flags & MAGIC_CHECK)
1125 file_magwarn(ms, "invalid string op: %c", *t);
1130 * We used to set mask to all 1's here, instead let's just not do
1131 * anything if mask = 0 (unless you have a better idea)
1138 /* Old-style anding: "0 byte &0x80 dynamically linked" */
1145 /* HP compat: ignore &= etc. */
1154 m->reln = '='; /* the default relation */
1155 if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
1156 isspace((unsigned char)l[1])) || !l[1])) {
1163 * Grab the value part, except for an 'x' reln.
1165 if (m->reln != 'x' && getvalue(ms, m, &l, action))
1169 * TODO finish this macro and start using it!
1170 * #define offsetcheck {if (offset > HOWMANY-1)
1171 * magwarn("offset too big"); }
1175 * Now get last part - the description
1181 } else if ((l[0] == '\\') && (l[1] == 'b')) {
1187 for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); )
1189 if (i == sizeof(m->desc)) {
1190 m->desc[sizeof(m->desc) - 1] = '\0';
1191 if (ms->flags & MAGIC_CHECK)
1192 file_magwarn(ms, "description `%s' truncated", m->desc);
1196 * We only do this check while compiling, or if any of the magic
1197 * files were not compiled.
1199 if (ms->flags & MAGIC_CHECK) {
1200 if (check_format(ms, m) == -1)
1203 #ifndef COMPILE_ONLY
1204 if (action == FILE_CHECK) {
1208 if (m->cont_level == 0)
1209 ++(*nmentryp); /* make room for next */
1214 check_format_type(const char *ptr, int type)
1218 /* Missing format string; bad */
1231 while (isdigit((unsigned char)*ptr)) ptr++;
1234 while (isdigit((unsigned char)*ptr)) ptr++;
1286 case FILE_FMT_FLOAT:
1287 case FILE_FMT_DOUBLE:
1292 while (isdigit((unsigned char)*ptr)) ptr++;
1295 while (isdigit((unsigned char)*ptr)) ptr++;
1314 while (isdigit((unsigned char )*ptr))
1318 while (isdigit((unsigned char )*ptr))
1330 /* internal error */
1338 * Check that the optional printf format in description matches
1339 * the type of the magic.
1342 check_format(struct magic_set *ms, struct magic *m)
1346 for (ptr = m->desc; *ptr; ptr++)
1350 /* No format string; ok */
1354 assert(file_nformats == file_nnames);
1356 if (m->type >= file_nformats) {
1357 file_error(ms, 0, "Internal error inconsistency between "
1358 "m->type and format strings");
1361 if (file_formats[m->type] == FILE_FMT_NONE) {
1362 file_error(ms, 0, "No format string for `%s' with description "
1363 "`%s'", m->desc, file_names[m->type]);
1368 if (check_format_type(ptr, file_formats[m->type]) == -1) {
1370 * TODO: this error message is unhelpful if the format
1371 * string is not one character long
1373 file_error(ms, 0, "Printf format `%c' is not valid for type "
1374 " `%s' in description `%s'", *ptr,
1375 file_names[m->type], m->desc);
1379 for (; *ptr; ptr++) {
1382 "Too many format strings (should have at most one) "
1383 "for `%s' with description `%s'",
1384 file_names[m->type], m->desc);
1392 * Read a numeric value from a pointer, into the value union of a magic
1393 * pointer, according to the magic type. Update the string pointer to point
1394 * just after the number read. Return 0 for success, non-zero for failure.
1397 getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
1402 case FILE_BESTRING16:
1403 case FILE_LESTRING16:
1408 *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen, action);
1410 if (ms->flags & MAGIC_CHECK)
1411 file_magwarn(ms, "cannot get string from `%s'",
1420 if (m->reln != 'x') {
1423 m->value.f = strtof(*p, &ep);
1425 m->value.f = (float)strtod(*p, &ep);
1433 if (m->reln != 'x') {
1435 m->value.d = strtod(*p, &ep);
1440 if (m->reln != 'x') {
1442 m->value.q = file_signextend(ms, m,
1443 (uint64_t)strtoull(*p, &ep, 0));
1452 * Convert a string containing C character escapes. Stop at an unescaped
1454 * Copy the converted version to "p", returning its length in *slen.
1455 * Return updated scan pointer as function result.
1457 private const char *
1458 getstr(struct magic_set *ms, const char *s, char *p, int plen, int *slen, int action)
1460 const char *origs = s;
1462 char *pmax = p + plen - 1;
1466 while ((c = *s++) != '\0') {
1467 if (isspace((unsigned char) c))
1470 file_error(ms, 0, "string too long: `%s'", origs);
1477 if (action == FILE_COMPILE)
1478 file_magwarn(ms, "incomplete escape");
1482 if (action == FILE_COMPILE) {
1484 "escaped tab found, use \\t instead");
1489 if (action == FILE_COMPILE) {
1490 if (isprint((unsigned char)c))
1492 "no need to escape `%c'", c);
1495 "unknown escape sequence: \\%03o", c);
1498 /* space, perhaps force people to use \040? */
1502 * Other things people escape, but shouldn't need to,
1503 * so we disallow them
1516 /* and baskslash itself */
1549 /* \ and up to 3 octal digits */
1559 c = *s++; /* try for 2 */
1560 if (c >= '0' && c <= '7') {
1561 val = (val << 3) | (c - '0');
1562 c = *s++; /* try for 3 */
1563 if (c >= '0' && c <= '7')
1564 val = (val << 3) | (c-'0');
1573 /* \x and up to 2 hex digits */
1575 val = 'x'; /* Default if no digits */
1576 c = hextoint(*s++); /* Get next char */
1581 val = (val << 4) + c;
1599 /* Single hex char to int; -1 if not a hex char. */
1603 if (!isascii((unsigned char) c))
1605 if (isdigit((unsigned char) c))
1607 if ((c >= 'a') && (c <= 'f'))
1608 return c + 10 - 'a';
1609 if (( c>= 'A') && (c <= 'F'))
1610 return c + 10 - 'A';
1616 * Print a string containing C character escapes.
1619 file_showstr(FILE *fp, const char *s, size_t len)
1633 if (c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
1634 (void) fputc(c, fp);
1636 (void) fputc('\\', fp);
1639 (void) fputc('a', fp);
1643 (void) fputc('b', fp);
1647 (void) fputc('f', fp);
1651 (void) fputc('n', fp);
1655 (void) fputc('r', fp);
1659 (void) fputc('t', fp);
1663 (void) fputc('v', fp);
1667 (void) fprintf(fp, "%.3o", c & 0377);
1675 * eatsize(): Eat the size spec from a number [eg. 10UL]
1678 eatsize(const char **p)
1682 if (LOWCASE(*l) == 'u')
1685 switch (LOWCASE(*l)) {
1686 case 'l': /* long */
1687 case 's': /* short */
1688 case 'h': /* short */
1689 case 'b': /* char/byte */
1690 case 'c': /* char/byte */
1701 * handle a compiled file.
1704 apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
1712 char buf[MAXPATHLEN];
1713 char *dbname = mkdbname(fn, buf, sizeof(buf), 0);
1719 if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
1722 if (fstat(fd, &st) == -1) {
1723 file_error(ms, errno, "cannot stat `%s'", dbname);
1726 if (st.st_size < 16) {
1727 file_error(ms, 0, "file `%s' is too small", dbname);
1732 if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
1733 MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
1734 file_error(ms, errno, "cannot map `%s'", dbname);
1739 if ((mm = malloc((size_t)st.st_size)) == NULL) {
1740 file_oomem(ms, (size_t)st.st_size);
1743 if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
1752 ptr = (uint32_t *)(void *)*magicp;
1753 if (*ptr != MAGICNO) {
1754 if (swap4(*ptr) != MAGICNO) {
1755 file_error(ms, 0, "bad magic in `%s'");
1762 version = swap4(ptr[1]);
1765 if (version != VERSIONNO) {
1766 file_error(ms, 0, "File %d.%d supports only %d version magic "
1767 "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,
1768 VERSIONNO, dbname, version);
1771 *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic)) - 1;
1774 byteswap(*magicp, *nmagicp);
1782 (void)munmap((void *)mm, (size_t)st.st_size);
1793 private const uint32_t ar[] = {
1797 * handle an mmaped file.
1800 apprentice_compile(struct magic_set *ms, struct magic **magicp,
1801 uint32_t *nmagicp, const char *fn)
1804 char buf[MAXPATHLEN];
1805 char *dbname = mkdbname(fn, buf, sizeof(buf), 1);
1810 if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) {
1811 file_error(ms, errno, "cannot open `%s'", dbname);
1815 if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
1816 file_error(ms, errno, "error writing `%s'", dbname);
1820 if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
1821 != sizeof(struct magic)) {
1822 file_error(ms, errno, "error seeking `%s'", dbname);
1826 if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
1827 != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
1828 file_error(ms, errno, "error writing `%s'", dbname);
1836 private const char ext[] = ".mgc";
1841 mkdbname(const char *fn, char *buf, size_t bufsiz, int strip)
1845 if ((p = strrchr(fn, '/')) != NULL)
1849 (void)snprintf(buf, bufsiz, "%s%s", fn, ext);
1854 * Byteswap an mmap'ed file if needed
1857 byteswap(struct magic *magic, uint32_t nmagic)
1860 for (i = 0; i < nmagic; i++)
1871 uint8_t *s = (uint8_t *)(void *)&sv;
1872 uint8_t *d = (uint8_t *)(void *)&rv;
1885 uint8_t *s = (uint8_t *)(void *)&sv;
1886 uint8_t *d = (uint8_t *)(void *)&rv;
1901 uint8_t *s = (uint8_t *)(void *)&sv;
1902 uint8_t *d = (uint8_t *)(void *)&rv;
1915 * byteswap a single magic entry
1918 bs1(struct magic *m)
1920 m->cont_level = swap2(m->cont_level);
1921 m->offset = swap4((uint32_t)m->offset);
1922 m->in_offset = swap4((uint32_t)m->in_offset);
1923 m->lineno = swap4((uint32_t)m->lineno);
1924 if (IS_STRING(m->type)) {
1925 m->str_count = swap4(m->str_count);
1926 m->str_flags = swap4(m->str_flags);
1929 m->value.q = swap8(m->value.q);
1930 m->num_mask = swap8(m->num_mask);