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.
35 FILE_RCSID("@(#)$File: apprentice.c,v 1.165 2011/01/16 19:30:36 rrt Exp $")
39 #include "patchlevel.h"
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 *, struct magic *, const char *,
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 void apprentice_list(struct mlist *, int );
101 private int apprentice_load(struct magic_set *, struct magic **, uint32_t *,
103 private void byteswap(struct magic *, uint32_t);
104 private void bs1(struct magic *);
105 private uint16_t swap2(uint16_t);
106 private uint32_t swap4(uint32_t);
107 private uint64_t swap8(uint64_t);
108 private char *mkdbname(struct magic_set *, const char *, int);
109 private int apprentice_map(struct magic_set *, struct magic **, uint32_t *,
111 private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
113 private int check_format_type(const char *, int);
114 private int check_format(struct magic_set *, struct magic *);
115 private int get_op(char);
116 private int parse_mime(struct magic_set *, struct magic_entry *, const char *);
117 private int parse_strength(struct magic_set *, struct magic_entry *, const char *);
118 private int parse_apple(struct magic_set *, struct magic_entry *, const char *);
121 private size_t maxmagic = 0;
122 private size_t magicsize = sizeof(struct magic);
124 private const char usg_hdr[] = "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
129 int (*fun)(struct magic_set *, struct magic_entry *, const char *);
131 #define DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name }
133 DECLARE_FIELD(apple),
134 DECLARE_FIELD(strength),
141 int main(int, char *[]);
144 main(int argc, char *argv[])
147 struct magic_set *ms;
150 if ((progname = strrchr(argv[0], '/')) != NULL)
156 (void)fprintf(stderr, "Usage: %s file\n", progname);
160 if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
161 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
164 ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
166 (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
170 #endif /* COMPILE_ONLY */
172 static const struct type_tbl_s {
178 # define XX(s) s, (sizeof(s) - 1)
179 # define XX_NULL "", 0
180 { XX("byte"), FILE_BYTE, FILE_FMT_NUM },
181 { XX("short"), FILE_SHORT, FILE_FMT_NUM },
182 { XX("default"), FILE_DEFAULT, FILE_FMT_STR },
183 { XX("long"), FILE_LONG, FILE_FMT_NUM },
184 { XX("string"), FILE_STRING, FILE_FMT_STR },
185 { XX("date"), FILE_DATE, FILE_FMT_STR },
186 { XX("beshort"), FILE_BESHORT, FILE_FMT_NUM },
187 { XX("belong"), FILE_BELONG, FILE_FMT_NUM },
188 { XX("bedate"), FILE_BEDATE, FILE_FMT_STR },
189 { XX("leshort"), FILE_LESHORT, FILE_FMT_NUM },
190 { XX("lelong"), FILE_LELONG, FILE_FMT_NUM },
191 { XX("ledate"), FILE_LEDATE, FILE_FMT_STR },
192 { XX("pstring"), FILE_PSTRING, FILE_FMT_STR },
193 { XX("ldate"), FILE_LDATE, FILE_FMT_STR },
194 { XX("beldate"), FILE_BELDATE, FILE_FMT_STR },
195 { XX("leldate"), FILE_LELDATE, FILE_FMT_STR },
196 { XX("regex"), FILE_REGEX, FILE_FMT_STR },
197 { XX("bestring16"), FILE_BESTRING16, FILE_FMT_STR },
198 { XX("lestring16"), FILE_LESTRING16, FILE_FMT_STR },
199 { XX("search"), FILE_SEARCH, FILE_FMT_STR },
200 { XX("medate"), FILE_MEDATE, FILE_FMT_STR },
201 { XX("meldate"), FILE_MELDATE, FILE_FMT_STR },
202 { XX("melong"), FILE_MELONG, FILE_FMT_NUM },
203 { XX("quad"), FILE_QUAD, FILE_FMT_QUAD },
204 { XX("lequad"), FILE_LEQUAD, FILE_FMT_QUAD },
205 { XX("bequad"), FILE_BEQUAD, FILE_FMT_QUAD },
206 { XX("qdate"), FILE_QDATE, FILE_FMT_STR },
207 { XX("leqdate"), FILE_LEQDATE, FILE_FMT_STR },
208 { XX("beqdate"), FILE_BEQDATE, FILE_FMT_STR },
209 { XX("qldate"), FILE_QLDATE, FILE_FMT_STR },
210 { XX("leqldate"), FILE_LEQLDATE, FILE_FMT_STR },
211 { XX("beqldate"), FILE_BEQLDATE, FILE_FMT_STR },
212 { XX("float"), FILE_FLOAT, FILE_FMT_FLOAT },
213 { XX("befloat"), FILE_BEFLOAT, FILE_FMT_FLOAT },
214 { XX("lefloat"), FILE_LEFLOAT, FILE_FMT_FLOAT },
215 { XX("double"), FILE_DOUBLE, FILE_FMT_DOUBLE },
216 { XX("bedouble"), FILE_BEDOUBLE, FILE_FMT_DOUBLE },
217 { XX("ledouble"), FILE_LEDOUBLE, FILE_FMT_DOUBLE },
218 { XX("leid3"), FILE_LEID3, FILE_FMT_NUM },
219 { XX("beid3"), FILE_BEID3, FILE_FMT_NUM },
220 { XX("indirect"), FILE_INDIRECT, FILE_FMT_NONE },
221 { XX_NULL, FILE_INVALID, FILE_FMT_NONE },
227 get_type(const char *l, const char **t)
229 const struct type_tbl_s *p;
231 for (p = type_tbl; p->len; p++) {
232 if (strncmp(l, p->name, p->len) == 0) {
242 init_file_tables(void)
245 const struct type_tbl_s *p;
251 for (p = type_tbl; p->len; p++) {
252 assert(p->type < FILE_NAMES_SIZE);
253 file_names[p->type] = p->name;
254 file_formats[p->type] = p->format;
259 * Handle one file or directory.
262 apprentice_1(struct magic_set *ms, const char *fn, int action,
265 struct magic *magic = NULL;
271 if (magicsize != FILE_MAGICSIZE) {
272 file_error(ms, 0, "magic element size %lu != %lu",
273 (unsigned long)sizeof(*magic),
274 (unsigned long)FILE_MAGICSIZE);
278 if (action == FILE_COMPILE) {
279 rv = apprentice_load(ms, &magic, &nmagic, fn, action);
282 rv = apprentice_compile(ms, &magic, &nmagic, fn);
288 if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
289 if (ms->flags & MAGIC_CHECK)
290 file_magwarn(ms, "using regular magic file `%s'", fn);
291 rv = apprentice_load(ms, &magic, &nmagic, fn, action);
299 file_delmagic(magic, mapped, nmagic);
303 if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL) {
304 file_delmagic(magic, mapped, nmagic);
305 file_oomem(ms, sizeof(*ml));
313 mlist->prev->next = ml;
314 ml->prev = mlist->prev;
318 if (action == FILE_LIST) {
319 printf("Binary patterns:\n");
320 apprentice_list(mlist, BINTEST);
321 printf("Text patterns:\n");
322 apprentice_list(mlist, TEXTTEST);
326 #endif /* COMPILE_ONLY */
330 file_delmagic(struct magic *p, int type, size_t entries)
338 (void)munmap((void *)p, sizeof(*p) * (entries + 1));
356 /* const char *fn: list of magic files and directories */
357 protected struct mlist *
358 file_apprentice(struct magic_set *ms, const char *fn, int action)
361 int file_err, errs = -1;
364 if ((fn = magic_getpath(fn, action)) == NULL)
369 if ((mfn = strdup(fn)) == NULL) {
370 file_oomem(ms, strlen(fn));
375 if ((mlist = CAST(struct mlist *, malloc(sizeof(*mlist)))) == NULL) {
377 file_oomem(ms, sizeof(*mlist));
380 mlist->next = mlist->prev = mlist;
383 p = strchr(fn, PATHSEP);
388 file_err = apprentice_1(ms, fn, action, mlist);
389 errs = MAX(errs, file_err);
396 file_error(ms, 0, "could not find any magic files!");
404 * Get weight of this magic entry, for sorting purposes.
407 apprentice_magic_strength(const struct magic *m)
410 size_t val = 2 * MULT; /* baseline strength */
413 case FILE_DEFAULT: /* make sure this sorts last */
414 if (m->factor_op != FILE_FACTOR_OP_NONE)
437 val += m->vallen * MULT;
440 case FILE_BESTRING16:
441 case FILE_LESTRING16:
442 val += m->vallen * MULT / 2;
447 val += m->vallen * MAX(MULT / m->vallen, 1);
481 (void)fprintf(stderr, "Bad type %d\n", m->type);
486 case 'x': /* matches anything penalize */
487 case '!': /* matches almost anything penalize */
491 case '=': /* Exact match, prefer */
496 case '<': /* comparison match reduce strength */
501 case '&': /* masking bits, we could count them too */
506 (void)fprintf(stderr, "Bad relation %c\n", m->reln);
510 if (val == 0) /* ensure we only return 0 for FILE_DEFAULT */
513 switch (m->factor_op) {
514 case FILE_FACTOR_OP_NONE:
516 case FILE_FACTOR_OP_PLUS:
519 case FILE_FACTOR_OP_MINUS:
522 case FILE_FACTOR_OP_TIMES:
525 case FILE_FACTOR_OP_DIV:
533 * Magic entries with no description get a bonus because they depend
534 * on subsequent magic entries to print something.
536 if (m->desc[0] == '\0')
542 * Sort callback for sorting entries by "strength" (basically length)
545 apprentice_sort(const void *a, const void *b)
547 const struct magic_entry *ma = CAST(const struct magic_entry *, a);
548 const struct magic_entry *mb = CAST(const struct magic_entry *, b);
549 size_t sa = apprentice_magic_strength(ma->mp);
550 size_t sb = apprentice_magic_strength(mb->mp);
560 * Shows sorted patterns list in the order which is used for the matching
563 apprentice_list(struct mlist *mlist, int mode)
565 uint32_t magindex = 0;
567 for (ml = mlist->next; ml != mlist; ml = ml->next) {
568 for (magindex = 0; magindex < ml->nmagic; magindex++) {
569 struct magic *m = &ml->magic[magindex];
570 if ((m->flag & mode) != mode) {
572 while (magindex + 1 < ml->nmagic &&
573 ml->magic[magindex + 1].cont_level != 0)
575 continue; /* Skip to next top-level test*/
579 * Try to iterate over the tree until we find item with
580 * description/mimetype.
582 while (magindex + 1 < ml->nmagic &&
583 ml->magic[magindex + 1].cont_level != 0 &&
584 *ml->magic[magindex].desc == '\0' &&
585 *ml->magic[magindex].mimetype == '\0')
588 printf("Strength = %3" SIZE_T_FORMAT "u : %s [%s]\n",
589 apprentice_magic_strength(m),
590 ml->magic[magindex].desc,
591 ml->magic[magindex].mimetype);
597 set_test_type(struct magic *mstart, struct magic *m)
633 case FILE_BESTRING16:
634 case FILE_LESTRING16:
635 /* Allow text overrides */
636 if (mstart->str_flags & STRING_TEXTTEST)
637 mstart->flag |= TEXTTEST;
639 mstart->flag |= BINTEST;
643 /* Check for override */
644 if (mstart->str_flags & STRING_BINTEST)
645 mstart->flag |= BINTEST;
646 if (mstart->str_flags & STRING_TEXTTEST)
647 mstart->flag |= TEXTTEST;
649 if (mstart->flag & (TEXTTEST|BINTEST))
652 /* binary test if pattern is not text */
653 if (file_looks_utf8(m->value.us, (size_t)m->vallen, NULL,
655 mstart->flag |= BINTEST;
657 mstart->flag |= TEXTTEST;
660 /* can't deduce anything; we shouldn't see this at the
665 /* invalid search type, but no need to complain here */
671 * Load and parse one file.
674 load_1(struct magic_set *ms, int action, const char *fn, int *errs,
675 struct magic_entry **marray, uint32_t *marraycount)
679 FILE *f = fopen(ms->file = fn, "r");
682 file_error(ms, errno, "cannot read magic file `%s'",
686 /* read and parse this file */
688 fgets(line, CAST(int, sizeof(line)), f) != NULL;
692 if (len == 0) /* null line, garbage, etc */
694 if (line[len - 1] == '\n') {
696 line[len - 1] = '\0'; /* delete newline */
698 if (line[0] == '\0') /* empty, do not parse */
700 if (line[0] == '#') /* comment, do not parse */
702 if (line[0] == '!' && line[1] == ':') {
705 for (i = 0; bang[i].name != NULL; i++) {
706 if (len - 2 > bang[i].len &&
707 memcmp(bang[i].name, line + 2,
711 if (bang[i].name == NULL) {
713 "Unknown !: entry `%s'", line);
717 if (*marraycount == 0) {
719 "No current entry for :!%s type",
724 if ((*bang[i].fun)(ms,
725 &(*marray)[*marraycount - 1],
726 line + bang[i].len + 2) != 0) {
732 if (parse(ms, marray, marraycount, line, lineno,
742 * parse a file or directory of files
743 * const char *fn: name of magic file or directory
746 cmpstrp(const void *p1, const void *p2)
748 return strcmp(*(char *const *)p1, *(char *const *)p2);
752 apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
753 const char *fn, int action)
756 struct magic_entry *marray;
757 uint32_t marraycount, i, mentrycount = 0, starttest;
758 size_t slen, files = 0, maxfiles = 0;
759 char subfn[MAXPATHLEN], **filearr = NULL, *mfn;
764 ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
767 if ((marray = CAST(struct magic_entry *, calloc(maxmagic,
768 sizeof(*marray)))) == NULL) {
769 file_oomem(ms, maxmagic * sizeof(*marray));
774 /* print silly verbose header for USG compat. */
775 if (action == FILE_CHECK)
776 (void)fprintf(stderr, "%s\n", usg_hdr);
778 /* load directory or file */
779 if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
785 while ((d = readdir(dir)) != NULL) {
786 (void)snprintf(subfn, sizeof(subfn), "%s/%s",
788 if (stat(subfn, &st) == -1 || !S_ISREG(st.st_mode))
790 if ((mfn = strdup(subfn)) == NULL) {
791 file_oomem(ms, strlen(subfn));
795 if (files >= maxfiles) {
797 maxfiles = (maxfiles + 1) * 2;
798 mlen = maxfiles * sizeof(*filearr);
799 if ((filearr = CAST(char **,
800 realloc(filearr, mlen))) == NULL) {
801 file_oomem(ms, mlen);
806 filearr[files++] = mfn;
809 qsort(filearr, files, sizeof(*filearr), cmpstrp);
810 for (i = 0; i < files; i++) {
811 load_1(ms, action, filearr[i], &errs, &marray,
817 load_1(ms, action, fn, &errs, &marray, &marraycount);
821 /* Set types of tests */
822 for (i = 0; i < marraycount; ) {
823 if (marray[i].mp->cont_level != 0) {
830 static const char text[] = "text";
831 static const char binary[] = "binary";
832 static const size_t len = sizeof(text);
833 set_test_type(marray[starttest].mp, marray[i].mp);
834 if ((ms->flags & MAGIC_DEBUG) == 0)
836 (void)fprintf(stderr, "%s%s%s: %s\n",
837 marray[i].mp->mimetype,
838 marray[i].mp->mimetype[0] == '\0' ? "" : "; ",
839 marray[i].mp->desc[0] ? marray[i].mp->desc :
841 marray[i].mp->flag & BINTEST ? binary : text);
842 if (marray[i].mp->flag & BINTEST) {
843 char *p = strstr(marray[i].mp->desc, text);
844 if (p && (p == marray[i].mp->desc ||
845 isspace((unsigned char)p[-1])) &&
846 (p + len - marray[i].mp->desc ==
847 MAXstring || (p[len] == '\0' ||
848 isspace((unsigned char)p[len]))))
849 (void)fprintf(stderr, "*** Possible "
850 "binary test for text type\n");
852 } while (++i < marraycount && marray[i].mp->cont_level != 0);
855 qsort(marray, marraycount, sizeof(*marray), apprentice_sort);
858 * Make sure that any level 0 "default" line is last (if one exists).
860 for (i = 0; i < marraycount; i++) {
861 if (marray[i].mp->cont_level == 0 &&
862 marray[i].mp->type == FILE_DEFAULT) {
863 while (++i < marraycount)
864 if (marray[i].mp->cont_level == 0)
866 if (i != marraycount) {
868 ms->line = marray[i].mp->lineno;
870 "level 0 \"default\" did not sort last");
876 for (i = 0; i < marraycount; i++)
877 mentrycount += marray[i].cont_count;
879 slen = sizeof(**magicp) * mentrycount;
880 if ((*magicp = CAST(struct magic *, malloc(slen))) == NULL) {
881 file_oomem(ms, slen);
887 for (i = 0; i < marraycount; i++) {
888 (void)memcpy(*magicp + mentrycount, marray[i].mp,
889 marray[i].cont_count * sizeof(**magicp));
890 mentrycount += marray[i].cont_count;
893 for (i = 0; i < marraycount; i++)
901 *nmagicp = mentrycount;
908 * extend the sign bit if the comparison is to be signed
911 file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
913 if (!(m->flag & UNSIGNED)) {
916 * Do not remove the casts below. They are
917 * vital. When later compared with the data,
918 * the sign extension must have happened.
961 case FILE_BESTRING16:
962 case FILE_LESTRING16:
969 if (ms->flags & MAGIC_CHECK)
970 file_magwarn(ms, "cannot happen: m->type=%d\n",
979 string_modifier_check(struct magic_set *ms, struct magic *m)
981 if ((ms->flags & MAGIC_CHECK) == 0)
984 if (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0) {
986 "'/BHhLl' modifiers are only allowed for pascal strings\n");
990 case FILE_BESTRING16:
991 case FILE_LESTRING16:
992 if (m->str_flags != 0) {
994 "no modifiers allowed for 16-bit strings\n");
1000 if ((m->str_flags & REGEX_OFFSET_START) != 0) {
1002 "'/%c' only allowed on regex and search\n",
1003 CHAR_REGEX_OFFSET_START);
1008 if (m->str_range == 0) {
1010 "missing range; defaulting to %d\n",
1011 STRING_DEFAULT_RANGE);
1012 m->str_range = STRING_DEFAULT_RANGE;
1017 if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) {
1018 file_magwarn(ms, "'/%c' not allowed on regex\n",
1019 CHAR_COMPACT_WHITESPACE);
1022 if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) {
1023 file_magwarn(ms, "'/%c' not allowed on regex\n",
1024 CHAR_COMPACT_OPTIONAL_WHITESPACE);
1029 file_magwarn(ms, "coding error: m->type=%d\n",
1049 return FILE_OPMINUS;
1051 return FILE_OPMULTIPLY;
1053 return FILE_OPDIVIDE;
1055 return FILE_OPMODULO;
1061 #ifdef ENABLE_CONDITIONALS
1063 get_cond(const char *l, const char **t)
1065 static const struct cond_tbl_s {
1070 { "if", 2, COND_IF },
1071 { "elif", 4, COND_ELIF },
1072 { "else", 4, COND_ELSE },
1073 { "", 0, COND_NONE },
1075 const struct cond_tbl_s *p;
1077 for (p = cond_tbl; p->len; p++) {
1078 if (strncmp(l, p->name, p->len) == 0 &&
1079 isspace((unsigned char)l[p->len])) {
1089 check_cond(struct magic_set *ms, int cond, uint32_t cont_level)
1092 last_cond = ms->c.li[cont_level].last_cond;
1096 if (last_cond != COND_NONE && last_cond != COND_ELIF) {
1097 if (ms->flags & MAGIC_CHECK)
1098 file_magwarn(ms, "syntax error: `if'");
1101 last_cond = COND_IF;
1105 if (last_cond != COND_IF && last_cond != COND_ELIF) {
1106 if (ms->flags & MAGIC_CHECK)
1107 file_magwarn(ms, "syntax error: `elif'");
1110 last_cond = COND_ELIF;
1114 if (last_cond != COND_IF && last_cond != COND_ELIF) {
1115 if (ms->flags & MAGIC_CHECK)
1116 file_magwarn(ms, "syntax error: `else'");
1119 last_cond = COND_NONE;
1123 last_cond = COND_NONE;
1127 ms->c.li[cont_level].last_cond = last_cond;
1130 #endif /* ENABLE_CONDITIONALS */
1133 * parse one line from magic file, put into magic[index++] if valid
1136 parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
1137 const char *line, size_t lineno, int action)
1139 #ifdef ENABLE_CONDITIONALS
1140 static uint32_t last_cont_level = 0;
1143 struct magic_entry *me;
1145 const char *l = line;
1148 uint32_t cont_level;
1153 ++l; /* step over */
1156 #ifdef ENABLE_CONDITIONALS
1157 if (cont_level == 0 || cont_level > last_cont_level)
1158 if (file_check_mem(ms, cont_level) == -1)
1160 last_cont_level = cont_level;
1163 #define ALLOC_CHUNK (size_t)10
1164 #define ALLOC_INCR (size_t)200
1166 if (cont_level != 0) {
1167 if (*nmentryp == 0) {
1168 file_error(ms, 0, "No current entry for continuation");
1171 me = &(*mentryp)[*nmentryp - 1];
1172 if (me->cont_count == me->max_count) {
1174 size_t cnt = me->max_count + ALLOC_CHUNK;
1175 if ((nm = CAST(struct magic *, realloc(me->mp,
1176 sizeof(*nm) * cnt))) == NULL) {
1177 file_oomem(ms, sizeof(*nm) * cnt);
1181 me->max_count = CAST(uint32_t, cnt);
1183 m = &me->mp[me->cont_count++];
1184 (void)memset(m, 0, sizeof(*m));
1185 m->cont_level = cont_level;
1187 if (*nmentryp == maxmagic) {
1188 struct magic_entry *mp;
1190 maxmagic += ALLOC_INCR;
1191 if ((mp = CAST(struct magic_entry *,
1192 realloc(*mentryp, sizeof(*mp) * maxmagic))) ==
1194 file_oomem(ms, sizeof(*mp) * maxmagic);
1197 (void)memset(&mp[*nmentryp], 0, sizeof(*mp) *
1201 me = &(*mentryp)[*nmentryp];
1202 if (me->mp == NULL) {
1203 size_t len = sizeof(*m) * ALLOC_CHUNK;
1204 if ((m = CAST(struct magic *, malloc(len))) == NULL) {
1205 file_oomem(ms, len);
1209 me->max_count = ALLOC_CHUNK;
1212 (void)memset(m, 0, sizeof(*m));
1213 m->factor_op = FILE_FACTOR_OP_NONE;
1217 m->lineno = CAST(uint32_t, lineno);
1219 if (*l == '&') { /* m->cont_level == 0 checked below. */
1220 ++l; /* step over */
1224 ++l; /* step over */
1226 if (m->flag & OFFADD)
1227 m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
1229 if (*l == '&') { /* m->cont_level == 0 checked below */
1230 ++l; /* step over */
1234 /* Indirect offsets are not valid at level 0. */
1235 if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD)))
1236 if (ms->flags & MAGIC_CHECK)
1237 file_magwarn(ms, "relative offset at level 0");
1239 /* get offset, then skip over it */
1240 m->offset = (uint32_t)strtoul(l, &t, 0);
1242 if (ms->flags & MAGIC_CHECK)
1243 file_magwarn(ms, "offset `%s' invalid", l);
1246 if (m->flag & INDIR) {
1247 m->in_type = FILE_LONG;
1250 * read [.lbs][+-]nnnnn)
1256 m->in_type = FILE_LELONG;
1259 m->in_type = FILE_BELONG;
1262 m->in_type = FILE_MELONG;
1266 m->in_type = FILE_LESHORT;
1270 m->in_type = FILE_BESHORT;
1276 m->in_type = FILE_BYTE;
1281 m->in_type = FILE_LEDOUBLE;
1286 m->in_type = FILE_BEDOUBLE;
1289 m->in_type = FILE_LEID3;
1292 m->in_type = FILE_BEID3;
1295 if (ms->flags & MAGIC_CHECK)
1297 "indirect offset type `%c' invalid",
1306 m->in_op |= FILE_OPINVERSE;
1309 if ((op = get_op(*l)) != -1) {
1314 m->in_op |= FILE_OPINDIRECT;
1317 if (isdigit((unsigned char)*l) || *l == '-') {
1318 m->in_offset = (int32_t)strtol(l, &t, 0);
1320 if (ms->flags & MAGIC_CHECK)
1322 "in_offset `%s' invalid", l);
1326 ((m->in_op & FILE_OPINDIRECT) && *l++ != ')'))
1327 if (ms->flags & MAGIC_CHECK)
1329 "missing ')' in indirect offset");
1333 #ifdef ENABLE_CONDITIONALS
1334 m->cond = get_cond(l, &l);
1335 if (check_cond(ms, m->cond, cont_level) == -1)
1343 m->flag |= UNSIGNED;
1346 m->type = get_type(l, &l);
1347 if (m->type == FILE_INVALID) {
1348 if (ms->flags & MAGIC_CHECK)
1349 file_magwarn(ms, "type `%s' invalid", l);
1353 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
1354 /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
1358 if (!IS_STRING(m->type))
1359 m->mask_op |= FILE_OPINVERSE;
1360 else if (ms->flags & MAGIC_CHECK)
1361 file_magwarn(ms, "'~' invalid for string types");
1365 m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
1366 if ((op = get_op(*l)) != -1) {
1367 if (!IS_STRING(m->type)) {
1371 val = (uint64_t)strtoull(l, &t, 0);
1373 m->num_mask = file_signextend(ms, m, val);
1376 else if (op == FILE_OPDIVIDE) {
1378 while (!isspace((unsigned char)*++l)) {
1380 case '0': case '1': case '2':
1381 case '3': case '4': case '5':
1382 case '6': case '7': case '8':
1385 (ms->flags & MAGIC_CHECK))
1389 m->str_range = CAST(uint32_t,
1391 if (m->str_range == 0)
1396 case CHAR_COMPACT_WHITESPACE:
1398 STRING_COMPACT_WHITESPACE;
1400 case CHAR_COMPACT_OPTIONAL_WHITESPACE:
1402 STRING_COMPACT_OPTIONAL_WHITESPACE;
1404 case CHAR_IGNORE_LOWERCASE:
1405 m->str_flags |= STRING_IGNORE_LOWERCASE;
1407 case CHAR_IGNORE_UPPERCASE:
1408 m->str_flags |= STRING_IGNORE_UPPERCASE;
1410 case CHAR_REGEX_OFFSET_START:
1411 m->str_flags |= REGEX_OFFSET_START;
1414 m->str_flags |= STRING_BINTEST;
1417 m->str_flags |= STRING_TEXTTEST;
1419 case CHAR_PSTRING_1_LE:
1420 if (m->type != FILE_PSTRING)
1422 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_1_LE;
1424 case CHAR_PSTRING_2_BE:
1425 if (m->type != FILE_PSTRING)
1427 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_BE;
1429 case CHAR_PSTRING_2_LE:
1430 if (m->type != FILE_PSTRING)
1432 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_LE;
1434 case CHAR_PSTRING_4_BE:
1435 if (m->type != FILE_PSTRING)
1437 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_BE;
1439 case CHAR_PSTRING_4_LE:
1440 if (m->type != FILE_PSTRING)
1442 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_LE;
1444 case CHAR_PSTRING_LENGTH_INCLUDES_ITSELF:
1445 if (m->type != FILE_PSTRING)
1447 m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF;
1451 if (ms->flags & MAGIC_CHECK)
1453 "string extension `%c' "
1457 /* allow multiple '/' for readability */
1459 !isspace((unsigned char)l[2]))
1462 if (string_modifier_check(ms, m) == -1)
1466 if (ms->flags & MAGIC_CHECK)
1467 file_magwarn(ms, "invalid string op: %c", *t);
1472 * We used to set mask to all 1's here, instead let's just not do
1473 * anything if mask = 0 (unless you have a better idea)
1483 if (ms->flags & MAGIC_CHECK) {
1484 file_magwarn(ms, "%c= not supported",
1491 /* Old-style anding: "0 byte &0x80 dynamically linked" */
1498 /* HP compat: ignore &= etc. */
1507 m->reln = '='; /* the default relation */
1508 if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
1509 isspace((unsigned char)l[1])) || !l[1])) {
1516 * Grab the value part, except for an 'x' reln.
1518 if (m->reln != 'x' && getvalue(ms, m, &l, action))
1522 * TODO finish this macro and start using it!
1523 * #define offsetcheck {if (offset > HOWMANY-1)
1524 * magwarn("offset too big"); }
1528 * Now get last part - the description
1534 } else if ((l[0] == '\\') && (l[1] == 'b')) {
1539 for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); )
1541 if (i == sizeof(m->desc)) {
1542 m->desc[sizeof(m->desc) - 1] = '\0';
1543 if (ms->flags & MAGIC_CHECK)
1544 file_magwarn(ms, "description `%s' truncated", m->desc);
1548 * We only do this check while compiling, or if any of the magic
1549 * files were not compiled.
1551 if (ms->flags & MAGIC_CHECK) {
1552 if (check_format(ms, m) == -1)
1555 #ifndef COMPILE_ONLY
1556 if (action == FILE_CHECK) {
1560 m->mimetype[0] = '\0'; /* initialise MIME type to none */
1561 if (m->cont_level == 0)
1562 ++(*nmentryp); /* make room for next */
1567 * parse a STRENGTH annotation line from magic file, put into magic[index - 1]
1571 parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line)
1573 const char *l = line;
1575 unsigned long factor;
1576 struct magic *m = &me->mp[0];
1578 if (m->factor_op != FILE_FACTOR_OP_NONE) {
1580 "Current entry already has a strength type: %c %d",
1581 m->factor_op, m->factor);
1586 case FILE_FACTOR_OP_NONE:
1587 case FILE_FACTOR_OP_PLUS:
1588 case FILE_FACTOR_OP_MINUS:
1589 case FILE_FACTOR_OP_TIMES:
1590 case FILE_FACTOR_OP_DIV:
1591 m->factor_op = *l++;
1594 file_magwarn(ms, "Unknown factor op `%c'", *l);
1598 factor = strtoul(l, &el, 0);
1600 file_magwarn(ms, "Too large factor `%lu'", factor);
1603 if (*el && !isspace((unsigned char)*el)) {
1604 file_magwarn(ms, "Bad factor `%s'", l);
1607 m->factor = (uint8_t)factor;
1608 if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) {
1609 file_magwarn(ms, "Cannot have factor op `%c' and factor %u",
1610 m->factor_op, m->factor);
1615 m->factor_op = FILE_FACTOR_OP_NONE;
1621 * Parse an Apple CREATOR/TYPE annotation from magic file and put it into
1625 parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line)
1628 const char *l = line;
1629 struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
1631 if (m->apple[0] != '\0') {
1632 file_magwarn(ms, "Current entry already has a APPLE type "
1633 "`%.8s', new type `%s'", m->mimetype, l);
1638 for (i = 0; *l && ((isascii((unsigned char)*l) &&
1639 isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
1640 i < sizeof(m->apple); m->apple[i++] = *l++)
1642 if (i == sizeof(m->apple) && *l) {
1643 /* We don't need to NUL terminate here, printing handles it */
1644 if (ms->flags & MAGIC_CHECK)
1645 file_magwarn(ms, "APPLE type `%s' truncated %"
1646 SIZE_T_FORMAT "u", line, i);
1656 * parse a MIME annotation line from magic file, put into magic[index - 1]
1660 parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line)
1663 const char *l = line;
1664 struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
1666 if (m->mimetype[0] != '\0') {
1667 file_magwarn(ms, "Current entry already has a MIME type `%s',"
1668 " new type `%s'", m->mimetype, l);
1673 for (i = 0; *l && ((isascii((unsigned char)*l) &&
1674 isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
1675 i < sizeof(m->mimetype); m->mimetype[i++] = *l++)
1677 if (i == sizeof(m->mimetype)) {
1678 m->mimetype[sizeof(m->mimetype) - 1] = '\0';
1679 if (ms->flags & MAGIC_CHECK)
1680 file_magwarn(ms, "MIME type `%s' truncated %"
1681 SIZE_T_FORMAT "u", m->mimetype, i);
1683 m->mimetype[i] = '\0';
1692 check_format_type(const char *ptr, int type)
1696 /* Missing format string; bad */
1709 while (isdigit((unsigned char)*ptr)) ptr++;
1712 while (isdigit((unsigned char)*ptr)) ptr++;
1764 case FILE_FMT_FLOAT:
1765 case FILE_FMT_DOUBLE:
1770 while (isdigit((unsigned char)*ptr)) ptr++;
1773 while (isdigit((unsigned char)*ptr)) ptr++;
1792 while (isdigit((unsigned char )*ptr))
1796 while (isdigit((unsigned char )*ptr))
1808 /* internal error */
1816 * Check that the optional printf format in description matches
1817 * the type of the magic.
1820 check_format(struct magic_set *ms, struct magic *m)
1824 for (ptr = m->desc; *ptr; ptr++)
1828 /* No format string; ok */
1832 assert(file_nformats == file_nnames);
1834 if (m->type >= file_nformats) {
1835 file_magwarn(ms, "Internal error inconsistency between "
1836 "m->type and format strings");
1839 if (file_formats[m->type] == FILE_FMT_NONE) {
1840 file_magwarn(ms, "No format string for `%s' with description "
1841 "`%s'", m->desc, file_names[m->type]);
1846 if (check_format_type(ptr, file_formats[m->type]) == -1) {
1848 * TODO: this error message is unhelpful if the format
1849 * string is not one character long
1851 file_magwarn(ms, "Printf format `%c' is not valid for type "
1852 "`%s' in description `%s'", *ptr ? *ptr : '?',
1853 file_names[m->type], m->desc);
1857 for (; *ptr; ptr++) {
1860 "Too many format strings (should have at most one) "
1861 "for `%s' with description `%s'",
1862 file_names[m->type], m->desc);
1870 * Read a numeric value from a pointer, into the value union of a magic
1871 * pointer, according to the magic type. Update the string pointer to point
1872 * just after the number read. Return 0 for success, non-zero for failure.
1875 getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
1878 case FILE_BESTRING16:
1879 case FILE_LESTRING16:
1884 *p = getstr(ms, m, *p, action == FILE_COMPILE);
1886 if (ms->flags & MAGIC_CHECK)
1887 file_magwarn(ms, "cannot get string from `%s'",
1895 if (m->reln != 'x') {
1898 m->value.f = strtof(*p, &ep);
1900 m->value.f = (float)strtod(*p, &ep);
1908 if (m->reln != 'x') {
1910 m->value.d = strtod(*p, &ep);
1915 if (m->reln != 'x') {
1917 m->value.q = file_signextend(ms, m,
1918 (uint64_t)strtoull(*p, &ep, 0));
1927 * Convert a string containing C character escapes. Stop at an unescaped
1929 * Copy the converted version to "m->value.s", and the length in m->vallen.
1930 * Return updated scan pointer as function result. Warn if set.
1932 private const char *
1933 getstr(struct magic_set *ms, struct magic *m, const char *s, int warn)
1935 const char *origs = s;
1936 char *p = m->value.s;
1937 size_t plen = sizeof(m->value.s);
1939 char *pmax = p + plen - 1;
1943 while ((c = *s++) != '\0') {
1944 if (isspace((unsigned char) c))
1947 file_error(ms, 0, "string too long: `%s'", origs);
1955 file_magwarn(ms, "incomplete escape");
1961 "escaped tab found, use \\t instead");
1962 warn = 0; /* already did */
1967 if (isprint((unsigned char)c)) {
1968 /* Allow escaping of
1970 if (strchr("<>&^=!", c) == NULL
1971 && (m->type != FILE_REGEX ||
1972 strchr("[]().*?^$|{}", c)
1974 file_magwarn(ms, "no "
1980 "unknown escape sequence: "
1985 /* space, perhaps force people to use \040? */
1989 * Other things people escape, but shouldn't need to,
1990 * so we disallow them
2003 /* and baskslash itself */
2036 /* \ and up to 3 octal digits */
2046 c = *s++; /* try for 2 */
2047 if (c >= '0' && c <= '7') {
2048 val = (val << 3) | (c - '0');
2049 c = *s++; /* try for 3 */
2050 if (c >= '0' && c <= '7')
2051 val = (val << 3) | (c-'0');
2060 /* \x and up to 2 hex digits */
2062 val = 'x'; /* Default if no digits */
2063 c = hextoint(*s++); /* Get next char */
2068 val = (val << 4) + c;
2081 m->vallen = CAST(unsigned char, (p - origp));
2082 if (m->type == FILE_PSTRING)
2083 m->vallen += file_pstring_length_size(m);
2088 /* Single hex char to int; -1 if not a hex char. */
2092 if (!isascii((unsigned char) c))
2094 if (isdigit((unsigned char) c))
2096 if ((c >= 'a') && (c <= 'f'))
2097 return c + 10 - 'a';
2098 if (( c>= 'A') && (c <= 'F'))
2099 return c + 10 - 'A';
2105 * Print a string containing C character escapes.
2108 file_showstr(FILE *fp, const char *s, size_t len)
2123 if (c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
2124 (void) fputc(c, fp);
2126 (void) fputc('\\', fp);
2129 (void) fputc('a', fp);
2133 (void) fputc('b', fp);
2137 (void) fputc('f', fp);
2141 (void) fputc('n', fp);
2145 (void) fputc('r', fp);
2149 (void) fputc('t', fp);
2153 (void) fputc('v', fp);
2157 (void) fprintf(fp, "%.3o", c & 0377);
2165 * eatsize(): Eat the size spec from a number [eg. 10UL]
2168 eatsize(const char **p)
2172 if (LOWCASE(*l) == 'u')
2175 switch (LOWCASE(*l)) {
2176 case 'l': /* long */
2177 case 's': /* short */
2178 case 'h': /* short */
2179 case 'b': /* char/byte */
2180 case 'c': /* char/byte */
2191 * handle a compiled file.
2194 apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
2202 char *dbname = NULL;
2205 dbname = mkdbname(ms, fn, 0);
2209 if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
2212 if (fstat(fd, &st) == -1) {
2213 file_error(ms, errno, "cannot stat `%s'", dbname);
2216 if (st.st_size < 8) {
2217 file_error(ms, 0, "file `%s' is too small", dbname);
2222 if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
2223 MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
2224 file_error(ms, errno, "cannot map `%s'", dbname);
2229 if ((mm = CAST(void *, malloc((size_t)st.st_size))) == NULL) {
2230 file_oomem(ms, (size_t)st.st_size);
2233 if (read(fd, mm, (size_t)st.st_size) != (ssize_t)st.st_size) {
2239 *magicp = CAST(struct magic *, mm);
2242 ptr = (uint32_t *)(void *)*magicp;
2243 if (*ptr != MAGICNO) {
2244 if (swap4(*ptr) != MAGICNO) {
2245 file_error(ms, 0, "bad magic in `%s'", dbname);
2252 version = swap4(ptr[1]);
2255 if (version != VERSIONNO) {
2256 file_error(ms, 0, "File %d.%d supports only version %d magic "
2257 "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,
2258 VERSIONNO, dbname, version);
2261 *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic));
2266 byteswap(*magicp, *nmagicp);
2275 (void)munmap((void *)mm, (size_t)st.st_size);
2288 private const uint32_t ar[] = {
2292 * handle an mmaped file.
2295 apprentice_compile(struct magic_set *ms, struct magic **magicp,
2296 uint32_t *nmagicp, const char *fn)
2302 dbname = mkdbname(ms, fn, 1);
2307 if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) {
2308 file_error(ms, errno, "cannot open `%s'", dbname);
2312 if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
2313 file_error(ms, errno, "error writing `%s'", dbname);
2317 if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
2318 != sizeof(struct magic)) {
2319 file_error(ms, errno, "error seeking `%s'", dbname);
2323 if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
2324 != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
2325 file_error(ms, errno, "error writing `%s'", dbname);
2336 private const char ext[] = ".mgc";
2341 mkdbname(struct magic_set *ms, const char *fn, int strip)
2347 if ((p = strrchr(fn, '/')) != NULL)
2351 for (q = fn; *q; q++)
2354 for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--)
2358 /* Did not find .mgc, restore q */
2364 /* Compatibility with old code that looked in .mime */
2365 if (ms->flags & MAGIC_MIME) {
2366 asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext);
2367 if (access(buf, R_OK) != -1) {
2368 ms->flags &= MAGIC_MIME_TYPE;
2373 asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext);
2375 /* Compatibility with old code that looked in .mime */
2376 if (strstr(p, ".mime") != NULL)
2377 ms->flags &= MAGIC_MIME_TYPE;
2382 * Byteswap an mmap'ed file if needed
2385 byteswap(struct magic *magic, uint32_t nmagic)
2388 for (i = 0; i < nmagic; i++)
2399 uint8_t *s = (uint8_t *)(void *)&sv;
2400 uint8_t *d = (uint8_t *)(void *)&rv;
2413 uint8_t *s = (uint8_t *)(void *)&sv;
2414 uint8_t *d = (uint8_t *)(void *)&rv;
2429 uint8_t *s = (uint8_t *)(void *)&sv;
2430 uint8_t *d = (uint8_t *)(void *)&rv;
2454 * byteswap a single magic entry
2457 bs1(struct magic *m)
2459 m->cont_level = swap2(m->cont_level);
2460 m->offset = swap4((uint32_t)m->offset);
2461 m->in_offset = swap4((uint32_t)m->in_offset);
2462 m->lineno = swap4((uint32_t)m->lineno);
2463 if (IS_STRING(m->type)) {
2464 if (m->type == FILE_PSTRING)
2465 printf("flags! %d\n", m->str_flags);
2466 m->str_range = swap4(m->str_range);
2467 m->str_flags = swap4(m->str_flags);
2470 m->value.q = swap8(m->value.q);
2471 m->num_mask = swap8(m->num_mask);
2476 file_pstring_length_size(const struct magic *m)
2478 switch (m->str_flags & PSTRING_LEN) {
2488 abort(); /* Impossible */
2493 file_pstring_get_length(const struct magic *m, const char *s)
2497 switch (m->str_flags & PSTRING_LEN) {
2502 len = (s[1] << 8) | s[0];
2505 len = (s[0] << 8) | s[1];
2508 len = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0];
2511 len = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
2514 abort(); /* Impossible */
2517 if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
2518 len -= file_pstring_length_size(m);