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;
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;
661 /* can't deduce anything; we shouldn't see this at the
666 /* invalid search type, but no need to complain here */
672 * Load and parse one file.
675 load_1(struct magic_set *ms, int action, const char *fn, int *errs,
676 struct magic_entry **marray, uint32_t *marraycount)
680 FILE *f = fopen(ms->file = fn, "r");
683 file_error(ms, errno, "cannot read magic file `%s'",
687 /* read and parse this file */
689 fgets(line, CAST(int, sizeof(line)), f) != NULL;
693 if (len == 0) /* null line, garbage, etc */
695 if (line[len - 1] == '\n') {
697 line[len - 1] = '\0'; /* delete newline */
699 if (line[0] == '\0') /* empty, do not parse */
701 if (line[0] == '#') /* comment, do not parse */
703 if (line[0] == '!' && line[1] == ':') {
706 for (i = 0; bang[i].name != NULL; i++) {
707 if (len - 2 > bang[i].len &&
708 memcmp(bang[i].name, line + 2,
712 if (bang[i].name == NULL) {
714 "Unknown !: entry `%s'", line);
718 if (*marraycount == 0) {
720 "No current entry for :!%s type",
725 if ((*bang[i].fun)(ms,
726 &(*marray)[*marraycount - 1],
727 line + bang[i].len + 2) != 0) {
733 if (parse(ms, marray, marraycount, line, lineno,
743 * parse a file or directory of files
744 * const char *fn: name of magic file or directory
747 cmpstrp(const void *p1, const void *p2)
749 return strcmp(*(char *const *)p1, *(char *const *)p2);
753 apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
754 const char *fn, int action)
757 struct magic_entry *marray;
758 uint32_t marraycount, i, mentrycount = 0, starttest;
759 size_t slen, files = 0, maxfiles = 0;
760 char subfn[MAXPATHLEN], **filearr = NULL, *mfn;
765 ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
768 if ((marray = CAST(struct magic_entry *, calloc(maxmagic,
769 sizeof(*marray)))) == NULL) {
770 file_oomem(ms, maxmagic * sizeof(*marray));
775 /* print silly verbose header for USG compat. */
776 if (action == FILE_CHECK)
777 (void)fprintf(stderr, "%s\n", usg_hdr);
779 /* load directory or file */
780 if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
786 while ((d = readdir(dir)) != NULL) {
787 (void)snprintf(subfn, sizeof(subfn), "%s/%s",
789 if (stat(subfn, &st) == -1 || !S_ISREG(st.st_mode))
791 if ((mfn = strdup(subfn)) == NULL) {
792 file_oomem(ms, strlen(subfn));
796 if (files >= maxfiles) {
798 maxfiles = (maxfiles + 1) * 2;
799 mlen = maxfiles * sizeof(*filearr);
800 if ((filearr = CAST(char **,
801 realloc(filearr, mlen))) == NULL) {
802 file_oomem(ms, mlen);
807 filearr[files++] = mfn;
810 qsort(filearr, files, sizeof(*filearr), cmpstrp);
811 for (i = 0; i < files; i++) {
812 load_1(ms, action, filearr[i], &errs, &marray,
818 load_1(ms, action, fn, &errs, &marray, &marraycount);
822 /* Set types of tests */
823 for (i = 0; i < marraycount; ) {
824 if (marray[i].mp->cont_level != 0) {
831 static const char text[] = "text";
832 static const char binary[] = "binary";
833 static const size_t len = sizeof(text);
834 set_test_type(marray[starttest].mp, marray[i].mp);
835 if ((ms->flags & MAGIC_DEBUG) == 0)
837 (void)fprintf(stderr, "%s%s%s: %s\n",
838 marray[i].mp->mimetype,
839 marray[i].mp->mimetype[0] == '\0' ? "" : "; ",
840 marray[i].mp->desc[0] ? marray[i].mp->desc :
842 marray[i].mp->flag & BINTEST ? binary : text);
843 if (marray[i].mp->flag & BINTEST) {
844 char *p = strstr(marray[i].mp->desc, text);
845 if (p && (p == marray[i].mp->desc ||
846 isspace((unsigned char)p[-1])) &&
847 (p + len - marray[i].mp->desc ==
848 MAXstring || (p[len] == '\0' ||
849 isspace((unsigned char)p[len]))))
850 (void)fprintf(stderr, "*** Possible "
851 "binary test for text type\n");
853 } while (++i < marraycount && marray[i].mp->cont_level != 0);
856 qsort(marray, marraycount, sizeof(*marray), apprentice_sort);
859 * Make sure that any level 0 "default" line is last (if one exists).
861 for (i = 0; i < marraycount; i++) {
862 if (marray[i].mp->cont_level == 0 &&
863 marray[i].mp->type == FILE_DEFAULT) {
864 while (++i < marraycount)
865 if (marray[i].mp->cont_level == 0)
867 if (i != marraycount) {
869 ms->line = marray[i].mp->lineno;
871 "level 0 \"default\" did not sort last");
877 for (i = 0; i < marraycount; i++)
878 mentrycount += marray[i].cont_count;
880 slen = sizeof(**magicp) * mentrycount;
881 if ((*magicp = CAST(struct magic *, malloc(slen))) == NULL) {
882 file_oomem(ms, slen);
888 for (i = 0; i < marraycount; i++) {
889 (void)memcpy(*magicp + mentrycount, marray[i].mp,
890 marray[i].cont_count * sizeof(**magicp));
891 mentrycount += marray[i].cont_count;
894 for (i = 0; i < marraycount; i++)
902 *nmagicp = mentrycount;
909 * extend the sign bit if the comparison is to be signed
912 file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
914 if (!(m->flag & UNSIGNED)) {
917 * Do not remove the casts below. They are
918 * vital. When later compared with the data,
919 * the sign extension must have happened.
962 case FILE_BESTRING16:
963 case FILE_LESTRING16:
970 if (ms->flags & MAGIC_CHECK)
971 file_magwarn(ms, "cannot happen: m->type=%d\n",
980 string_modifier_check(struct magic_set *ms, struct magic *m)
982 if ((ms->flags & MAGIC_CHECK) == 0)
985 if (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0) {
987 "'/BHhLl' modifiers are only allowed for pascal strings\n");
991 case FILE_BESTRING16:
992 case FILE_LESTRING16:
993 if (m->str_flags != 0) {
995 "no modifiers allowed for 16-bit strings\n");
1001 if ((m->str_flags & REGEX_OFFSET_START) != 0) {
1003 "'/%c' only allowed on regex and search\n",
1004 CHAR_REGEX_OFFSET_START);
1009 if (m->str_range == 0) {
1011 "missing range; defaulting to %d\n",
1012 STRING_DEFAULT_RANGE);
1013 m->str_range = STRING_DEFAULT_RANGE;
1018 if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) {
1019 file_magwarn(ms, "'/%c' not allowed on regex\n",
1020 CHAR_COMPACT_WHITESPACE);
1023 if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) {
1024 file_magwarn(ms, "'/%c' not allowed on regex\n",
1025 CHAR_COMPACT_OPTIONAL_WHITESPACE);
1030 file_magwarn(ms, "coding error: m->type=%d\n",
1050 return FILE_OPMINUS;
1052 return FILE_OPMULTIPLY;
1054 return FILE_OPDIVIDE;
1056 return FILE_OPMODULO;
1062 #ifdef ENABLE_CONDITIONALS
1064 get_cond(const char *l, const char **t)
1066 static const struct cond_tbl_s {
1071 { "if", 2, COND_IF },
1072 { "elif", 4, COND_ELIF },
1073 { "else", 4, COND_ELSE },
1074 { "", 0, COND_NONE },
1076 const struct cond_tbl_s *p;
1078 for (p = cond_tbl; p->len; p++) {
1079 if (strncmp(l, p->name, p->len) == 0 &&
1080 isspace((unsigned char)l[p->len])) {
1090 check_cond(struct magic_set *ms, int cond, uint32_t cont_level)
1093 last_cond = ms->c.li[cont_level].last_cond;
1097 if (last_cond != COND_NONE && last_cond != COND_ELIF) {
1098 if (ms->flags & MAGIC_CHECK)
1099 file_magwarn(ms, "syntax error: `if'");
1102 last_cond = COND_IF;
1106 if (last_cond != COND_IF && last_cond != COND_ELIF) {
1107 if (ms->flags & MAGIC_CHECK)
1108 file_magwarn(ms, "syntax error: `elif'");
1111 last_cond = COND_ELIF;
1115 if (last_cond != COND_IF && last_cond != COND_ELIF) {
1116 if (ms->flags & MAGIC_CHECK)
1117 file_magwarn(ms, "syntax error: `else'");
1120 last_cond = COND_NONE;
1124 last_cond = COND_NONE;
1128 ms->c.li[cont_level].last_cond = last_cond;
1131 #endif /* ENABLE_CONDITIONALS */
1134 * parse one line from magic file, put into magic[index++] if valid
1137 parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
1138 const char *line, size_t lineno, int action)
1140 #ifdef ENABLE_CONDITIONALS
1141 static uint32_t last_cont_level = 0;
1144 struct magic_entry *me;
1146 const char *l = line;
1149 uint32_t cont_level;
1154 ++l; /* step over */
1157 #ifdef ENABLE_CONDITIONALS
1158 if (cont_level == 0 || cont_level > last_cont_level)
1159 if (file_check_mem(ms, cont_level) == -1)
1161 last_cont_level = cont_level;
1164 #define ALLOC_CHUNK (size_t)10
1165 #define ALLOC_INCR (size_t)200
1167 if (cont_level != 0) {
1168 if (*nmentryp == 0) {
1169 file_error(ms, 0, "No current entry for continuation");
1172 me = &(*mentryp)[*nmentryp - 1];
1173 if (me->cont_count == me->max_count) {
1175 size_t cnt = me->max_count + ALLOC_CHUNK;
1176 if ((nm = CAST(struct magic *, realloc(me->mp,
1177 sizeof(*nm) * cnt))) == NULL) {
1178 file_oomem(ms, sizeof(*nm) * cnt);
1182 me->max_count = CAST(uint32_t, cnt);
1184 m = &me->mp[me->cont_count++];
1185 (void)memset(m, 0, sizeof(*m));
1186 m->cont_level = cont_level;
1188 if (*nmentryp == maxmagic) {
1189 struct magic_entry *mp;
1191 maxmagic += ALLOC_INCR;
1192 if ((mp = CAST(struct magic_entry *,
1193 realloc(*mentryp, sizeof(*mp) * maxmagic))) ==
1195 file_oomem(ms, sizeof(*mp) * maxmagic);
1198 (void)memset(&mp[*nmentryp], 0, sizeof(*mp) *
1202 me = &(*mentryp)[*nmentryp];
1203 if (me->mp == NULL) {
1204 size_t len = sizeof(*m) * ALLOC_CHUNK;
1205 if ((m = CAST(struct magic *, malloc(len))) == NULL) {
1206 file_oomem(ms, len);
1210 me->max_count = ALLOC_CHUNK;
1213 (void)memset(m, 0, sizeof(*m));
1214 m->factor_op = FILE_FACTOR_OP_NONE;
1218 m->lineno = CAST(uint32_t, lineno);
1220 if (*l == '&') { /* m->cont_level == 0 checked below. */
1221 ++l; /* step over */
1225 ++l; /* step over */
1227 if (m->flag & OFFADD)
1228 m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
1230 if (*l == '&') { /* m->cont_level == 0 checked below */
1231 ++l; /* step over */
1235 /* Indirect offsets are not valid at level 0. */
1236 if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD)))
1237 if (ms->flags & MAGIC_CHECK)
1238 file_magwarn(ms, "relative offset at level 0");
1240 /* get offset, then skip over it */
1241 m->offset = (uint32_t)strtoul(l, &t, 0);
1243 if (ms->flags & MAGIC_CHECK)
1244 file_magwarn(ms, "offset `%s' invalid", l);
1247 if (m->flag & INDIR) {
1248 m->in_type = FILE_LONG;
1251 * read [.lbs][+-]nnnnn)
1257 m->in_type = FILE_LELONG;
1260 m->in_type = FILE_BELONG;
1263 m->in_type = FILE_MELONG;
1267 m->in_type = FILE_LESHORT;
1271 m->in_type = FILE_BESHORT;
1277 m->in_type = FILE_BYTE;
1282 m->in_type = FILE_LEDOUBLE;
1287 m->in_type = FILE_BEDOUBLE;
1290 m->in_type = FILE_LEID3;
1293 m->in_type = FILE_BEID3;
1296 if (ms->flags & MAGIC_CHECK)
1298 "indirect offset type `%c' invalid",
1307 m->in_op |= FILE_OPINVERSE;
1310 if ((op = get_op(*l)) != -1) {
1315 m->in_op |= FILE_OPINDIRECT;
1318 if (isdigit((unsigned char)*l) || *l == '-') {
1319 m->in_offset = (int32_t)strtol(l, &t, 0);
1321 if (ms->flags & MAGIC_CHECK)
1323 "in_offset `%s' invalid", l);
1327 ((m->in_op & FILE_OPINDIRECT) && *l++ != ')'))
1328 if (ms->flags & MAGIC_CHECK)
1330 "missing ')' in indirect offset");
1334 #ifdef ENABLE_CONDITIONALS
1335 m->cond = get_cond(l, &l);
1336 if (check_cond(ms, m->cond, cont_level) == -1)
1344 m->flag |= UNSIGNED;
1347 m->type = get_type(l, &l);
1348 if (m->type == FILE_INVALID) {
1349 if (ms->flags & MAGIC_CHECK)
1350 file_magwarn(ms, "type `%s' invalid", l);
1354 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
1355 /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
1359 if (!IS_STRING(m->type))
1360 m->mask_op |= FILE_OPINVERSE;
1361 else if (ms->flags & MAGIC_CHECK)
1362 file_magwarn(ms, "'~' invalid for string types");
1366 m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
1367 if ((op = get_op(*l)) != -1) {
1368 if (!IS_STRING(m->type)) {
1372 val = (uint64_t)strtoull(l, &t, 0);
1374 m->num_mask = file_signextend(ms, m, val);
1377 else if (op == FILE_OPDIVIDE) {
1379 while (!isspace((unsigned char)*++l)) {
1381 case '0': case '1': case '2':
1382 case '3': case '4': case '5':
1383 case '6': case '7': case '8':
1386 (ms->flags & MAGIC_CHECK))
1390 m->str_range = CAST(uint32_t,
1392 if (m->str_range == 0)
1397 case CHAR_COMPACT_WHITESPACE:
1399 STRING_COMPACT_WHITESPACE;
1401 case CHAR_COMPACT_OPTIONAL_WHITESPACE:
1403 STRING_COMPACT_OPTIONAL_WHITESPACE;
1405 case CHAR_IGNORE_LOWERCASE:
1406 m->str_flags |= STRING_IGNORE_LOWERCASE;
1408 case CHAR_IGNORE_UPPERCASE:
1409 m->str_flags |= STRING_IGNORE_UPPERCASE;
1411 case CHAR_REGEX_OFFSET_START:
1412 m->str_flags |= REGEX_OFFSET_START;
1415 m->str_flags |= STRING_BINTEST;
1418 m->str_flags |= STRING_TEXTTEST;
1420 case CHAR_PSTRING_1_LE:
1421 if (m->type != FILE_PSTRING)
1423 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_1_LE;
1425 case CHAR_PSTRING_2_BE:
1426 if (m->type != FILE_PSTRING)
1428 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_BE;
1430 case CHAR_PSTRING_2_LE:
1431 if (m->type != FILE_PSTRING)
1433 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_LE;
1435 case CHAR_PSTRING_4_BE:
1436 if (m->type != FILE_PSTRING)
1438 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_BE;
1440 case CHAR_PSTRING_4_LE:
1441 if (m->type != FILE_PSTRING)
1443 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_LE;
1445 case CHAR_PSTRING_LENGTH_INCLUDES_ITSELF:
1446 if (m->type != FILE_PSTRING)
1448 m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF;
1452 if (ms->flags & MAGIC_CHECK)
1454 "string extension `%c' "
1458 /* allow multiple '/' for readability */
1460 !isspace((unsigned char)l[2]))
1463 if (string_modifier_check(ms, m) == -1)
1467 if (ms->flags & MAGIC_CHECK)
1468 file_magwarn(ms, "invalid string op: %c", *t);
1473 * We used to set mask to all 1's here, instead let's just not do
1474 * anything if mask = 0 (unless you have a better idea)
1484 if (ms->flags & MAGIC_CHECK) {
1485 file_magwarn(ms, "%c= not supported",
1492 /* Old-style anding: "0 byte &0x80 dynamically linked" */
1499 /* HP compat: ignore &= etc. */
1508 m->reln = '='; /* the default relation */
1509 if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
1510 isspace((unsigned char)l[1])) || !l[1])) {
1517 * Grab the value part, except for an 'x' reln.
1519 if (m->reln != 'x' && getvalue(ms, m, &l, action))
1523 * TODO finish this macro and start using it!
1524 * #define offsetcheck {if (offset > HOWMANY-1)
1525 * magwarn("offset too big"); }
1529 * Now get last part - the description
1535 } else if ((l[0] == '\\') && (l[1] == 'b')) {
1540 for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); )
1542 if (i == sizeof(m->desc)) {
1543 m->desc[sizeof(m->desc) - 1] = '\0';
1544 if (ms->flags & MAGIC_CHECK)
1545 file_magwarn(ms, "description `%s' truncated", m->desc);
1549 * We only do this check while compiling, or if any of the magic
1550 * files were not compiled.
1552 if (ms->flags & MAGIC_CHECK) {
1553 if (check_format(ms, m) == -1)
1556 #ifndef COMPILE_ONLY
1557 if (action == FILE_CHECK) {
1561 m->mimetype[0] = '\0'; /* initialise MIME type to none */
1562 if (m->cont_level == 0)
1563 ++(*nmentryp); /* make room for next */
1568 * parse a STRENGTH annotation line from magic file, put into magic[index - 1]
1572 parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line)
1574 const char *l = line;
1576 unsigned long factor;
1577 struct magic *m = &me->mp[0];
1579 if (m->factor_op != FILE_FACTOR_OP_NONE) {
1581 "Current entry already has a strength type: %c %d",
1582 m->factor_op, m->factor);
1587 case FILE_FACTOR_OP_NONE:
1588 case FILE_FACTOR_OP_PLUS:
1589 case FILE_FACTOR_OP_MINUS:
1590 case FILE_FACTOR_OP_TIMES:
1591 case FILE_FACTOR_OP_DIV:
1592 m->factor_op = *l++;
1595 file_magwarn(ms, "Unknown factor op `%c'", *l);
1599 factor = strtoul(l, &el, 0);
1601 file_magwarn(ms, "Too large factor `%lu'", factor);
1604 if (*el && !isspace((unsigned char)*el)) {
1605 file_magwarn(ms, "Bad factor `%s'", l);
1608 m->factor = (uint8_t)factor;
1609 if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) {
1610 file_magwarn(ms, "Cannot have factor op `%c' and factor %u",
1611 m->factor_op, m->factor);
1616 m->factor_op = FILE_FACTOR_OP_NONE;
1622 * Parse an Apple CREATOR/TYPE annotation from magic file and put it into
1626 parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line)
1629 const char *l = line;
1630 struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
1632 if (m->apple[0] != '\0') {
1633 file_magwarn(ms, "Current entry already has a APPLE type "
1634 "`%.8s', new type `%s'", m->mimetype, l);
1639 for (i = 0; *l && ((isascii((unsigned char)*l) &&
1640 isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
1641 i < sizeof(m->apple); m->apple[i++] = *l++)
1643 if (i == sizeof(m->apple) && *l) {
1644 /* We don't need to NUL terminate here, printing handles it */
1645 if (ms->flags & MAGIC_CHECK)
1646 file_magwarn(ms, "APPLE type `%s' truncated %"
1647 SIZE_T_FORMAT "u", line, i);
1657 * parse a MIME annotation line from magic file, put into magic[index - 1]
1661 parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line)
1664 const char *l = line;
1665 struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
1667 if (m->mimetype[0] != '\0') {
1668 file_magwarn(ms, "Current entry already has a MIME type `%s',"
1669 " new type `%s'", m->mimetype, l);
1674 for (i = 0; *l && ((isascii((unsigned char)*l) &&
1675 isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
1676 i < sizeof(m->mimetype); m->mimetype[i++] = *l++)
1678 if (i == sizeof(m->mimetype)) {
1679 m->mimetype[sizeof(m->mimetype) - 1] = '\0';
1680 if (ms->flags & MAGIC_CHECK)
1681 file_magwarn(ms, "MIME type `%s' truncated %"
1682 SIZE_T_FORMAT "u", m->mimetype, i);
1684 m->mimetype[i] = '\0';
1693 check_format_type(const char *ptr, int type)
1697 /* Missing format string; bad */
1710 while (isdigit((unsigned char)*ptr)) ptr++;
1713 while (isdigit((unsigned char)*ptr)) ptr++;
1765 case FILE_FMT_FLOAT:
1766 case FILE_FMT_DOUBLE:
1771 while (isdigit((unsigned char)*ptr)) ptr++;
1774 while (isdigit((unsigned char)*ptr)) ptr++;
1793 while (isdigit((unsigned char )*ptr))
1797 while (isdigit((unsigned char )*ptr))
1809 /* internal error */
1817 * Check that the optional printf format in description matches
1818 * the type of the magic.
1821 check_format(struct magic_set *ms, struct magic *m)
1825 for (ptr = m->desc; *ptr; ptr++)
1829 /* No format string; ok */
1833 assert(file_nformats == file_nnames);
1835 if (m->type >= file_nformats) {
1836 file_magwarn(ms, "Internal error inconsistency between "
1837 "m->type and format strings");
1840 if (file_formats[m->type] == FILE_FMT_NONE) {
1841 file_magwarn(ms, "No format string for `%s' with description "
1842 "`%s'", m->desc, file_names[m->type]);
1847 if (check_format_type(ptr, file_formats[m->type]) == -1) {
1849 * TODO: this error message is unhelpful if the format
1850 * string is not one character long
1852 file_magwarn(ms, "Printf format `%c' is not valid for type "
1853 "`%s' in description `%s'", *ptr ? *ptr : '?',
1854 file_names[m->type], m->desc);
1858 for (; *ptr; ptr++) {
1861 "Too many format strings (should have at most one) "
1862 "for `%s' with description `%s'",
1863 file_names[m->type], m->desc);
1871 * Read a numeric value from a pointer, into the value union of a magic
1872 * pointer, according to the magic type. Update the string pointer to point
1873 * just after the number read. Return 0 for success, non-zero for failure.
1876 getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
1879 case FILE_BESTRING16:
1880 case FILE_LESTRING16:
1885 *p = getstr(ms, m, *p, action == FILE_COMPILE);
1887 if (ms->flags & MAGIC_CHECK)
1888 file_magwarn(ms, "cannot get string from `%s'",
1896 if (m->reln != 'x') {
1898 #if defined(HAVE_STRTOF) && !defined(COMPILE_ONLY)
1899 m->value.f = strtof(*p, &ep);
1901 m->value.f = (float)strtod(*p, &ep);
1909 if (m->reln != 'x') {
1911 m->value.d = strtod(*p, &ep);
1916 if (m->reln != 'x') {
1918 m->value.q = file_signextend(ms, m,
1919 (uint64_t)strtoull(*p, &ep, 0));
1928 * Convert a string containing C character escapes. Stop at an unescaped
1930 * Copy the converted version to "m->value.s", and the length in m->vallen.
1931 * Return updated scan pointer as function result. Warn if set.
1933 private const char *
1934 getstr(struct magic_set *ms, struct magic *m, const char *s, int warn)
1936 const char *origs = s;
1937 char *p = m->value.s;
1938 size_t plen = sizeof(m->value.s);
1940 char *pmax = p + plen - 1;
1944 while ((c = *s++) != '\0') {
1945 if (isspace((unsigned char) c))
1948 file_error(ms, 0, "string too long: `%s'", origs);
1956 file_magwarn(ms, "incomplete escape");
1962 "escaped tab found, use \\t instead");
1963 warn = 0; /* already did */
1968 if (isprint((unsigned char)c)) {
1969 /* Allow escaping of
1971 if (strchr("<>&^=!", c) == NULL
1972 && (m->type != FILE_REGEX ||
1973 strchr("[]().*?^$|{}", c)
1975 file_magwarn(ms, "no "
1981 "unknown escape sequence: "
1986 /* space, perhaps force people to use \040? */
1990 * Other things people escape, but shouldn't need to,
1991 * so we disallow them
2004 /* and baskslash itself */
2037 /* \ and up to 3 octal digits */
2047 c = *s++; /* try for 2 */
2048 if (c >= '0' && c <= '7') {
2049 val = (val << 3) | (c - '0');
2050 c = *s++; /* try for 3 */
2051 if (c >= '0' && c <= '7')
2052 val = (val << 3) | (c-'0');
2061 /* \x and up to 2 hex digits */
2063 val = 'x'; /* Default if no digits */
2064 c = hextoint(*s++); /* Get next char */
2069 val = (val << 4) + c;
2082 m->vallen = CAST(unsigned char, (p - origp));
2083 if (m->type == FILE_PSTRING)
2084 m->vallen += file_pstring_length_size(m);
2089 /* Single hex char to int; -1 if not a hex char. */
2093 if (!isascii((unsigned char) c))
2095 if (isdigit((unsigned char) c))
2097 if ((c >= 'a') && (c <= 'f'))
2098 return c + 10 - 'a';
2099 if (( c>= 'A') && (c <= 'F'))
2100 return c + 10 - 'A';
2106 * Print a string containing C character escapes.
2109 file_showstr(FILE *fp, const char *s, size_t len)
2124 if (c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
2125 (void) fputc(c, fp);
2127 (void) fputc('\\', fp);
2130 (void) fputc('a', fp);
2134 (void) fputc('b', fp);
2138 (void) fputc('f', fp);
2142 (void) fputc('n', fp);
2146 (void) fputc('r', fp);
2150 (void) fputc('t', fp);
2154 (void) fputc('v', fp);
2158 (void) fprintf(fp, "%.3o", c & 0377);
2166 * eatsize(): Eat the size spec from a number [eg. 10UL]
2169 eatsize(const char **p)
2173 if (LOWCASE(*l) == 'u')
2176 switch (LOWCASE(*l)) {
2177 case 'l': /* long */
2178 case 's': /* short */
2179 case 'h': /* short */
2180 case 'b': /* char/byte */
2181 case 'c': /* char/byte */
2192 * handle a compiled file.
2195 apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
2203 char *dbname = NULL;
2206 dbname = mkdbname(ms, fn, 0);
2210 if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
2213 if (fstat(fd, &st) == -1) {
2214 file_error(ms, errno, "cannot stat `%s'", dbname);
2217 if (st.st_size < 8) {
2218 file_error(ms, 0, "file `%s' is too small", dbname);
2223 if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
2224 MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
2225 file_error(ms, errno, "cannot map `%s'", dbname);
2230 if ((mm = CAST(void *, malloc((size_t)st.st_size))) == NULL) {
2231 file_oomem(ms, (size_t)st.st_size);
2234 if (read(fd, mm, (size_t)st.st_size) != (ssize_t)st.st_size) {
2240 *magicp = CAST(struct magic *, mm);
2243 ptr = (uint32_t *)(void *)*magicp;
2244 if (*ptr != MAGICNO) {
2245 if (swap4(*ptr) != MAGICNO) {
2246 file_error(ms, 0, "bad magic in `%s'", dbname);
2253 version = swap4(ptr[1]);
2256 if (version != VERSIONNO) {
2257 file_error(ms, 0, "File %d.%d supports only version %d magic "
2258 "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,
2259 VERSIONNO, dbname, version);
2262 *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic));
2267 byteswap(*magicp, *nmagicp);
2276 (void)munmap((void *)mm, (size_t)st.st_size);
2289 private const uint32_t ar[] = {
2293 * handle an mmaped file.
2296 apprentice_compile(struct magic_set *ms, struct magic **magicp,
2297 uint32_t *nmagicp, const char *fn)
2303 dbname = mkdbname(ms, fn, 1);
2308 if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) {
2309 file_error(ms, errno, "cannot open `%s'", dbname);
2313 if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
2314 file_error(ms, errno, "error writing `%s'", dbname);
2318 if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
2319 != sizeof(struct magic)) {
2320 file_error(ms, errno, "error seeking `%s'", dbname);
2324 if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
2325 != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
2326 file_error(ms, errno, "error writing `%s'", dbname);
2337 private const char ext[] = ".mgc";
2342 mkdbname(struct magic_set *ms, const char *fn, int strip)
2348 if ((p = strrchr(fn, '/')) != NULL)
2352 for (q = fn; *q; q++)
2355 for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--)
2359 /* Did not find .mgc, restore q */
2365 /* Compatibility with old code that looked in .mime */
2366 if (ms->flags & MAGIC_MIME) {
2367 asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext);
2368 if (access(buf, R_OK) != -1) {
2369 ms->flags &= MAGIC_MIME_TYPE;
2374 asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext);
2376 /* Compatibility with old code that looked in .mime */
2377 if (strstr(p, ".mime") != NULL)
2378 ms->flags &= MAGIC_MIME_TYPE;
2383 * Byteswap an mmap'ed file if needed
2386 byteswap(struct magic *magic, uint32_t nmagic)
2389 for (i = 0; i < nmagic; i++)
2400 uint8_t *s = (uint8_t *)(void *)&sv;
2401 uint8_t *d = (uint8_t *)(void *)&rv;
2414 uint8_t *s = (uint8_t *)(void *)&sv;
2415 uint8_t *d = (uint8_t *)(void *)&rv;
2430 uint8_t *s = (uint8_t *)(void *)&sv;
2431 uint8_t *d = (uint8_t *)(void *)&rv;
2455 * byteswap a single magic entry
2458 bs1(struct magic *m)
2460 m->cont_level = swap2(m->cont_level);
2461 m->offset = swap4((uint32_t)m->offset);
2462 m->in_offset = swap4((uint32_t)m->in_offset);
2463 m->lineno = swap4((uint32_t)m->lineno);
2464 if (IS_STRING(m->type)) {
2465 if (m->type == FILE_PSTRING)
2466 printf("flags! %d\n", m->str_flags);
2467 m->str_range = swap4(m->str_range);
2468 m->str_flags = swap4(m->str_flags);
2471 m->value.q = swap8(m->value.q);
2472 m->num_mask = swap8(m->num_mask);
2477 file_pstring_length_size(const struct magic *m)
2479 switch (m->str_flags & PSTRING_LEN) {
2489 abort(); /* Impossible */
2494 file_pstring_get_length(const struct magic *m, const char *s)
2498 switch (m->str_flags & PSTRING_LEN) {
2503 len = (s[1] << 8) | s[0];
2506 len = (s[0] << 8) | s[1];
2509 len = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0];
2512 len = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
2515 abort(); /* Impossible */
2518 if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
2519 len -= file_pstring_length_size(m);