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.169 2011/05/10 17:08:13 christos Exp $")
52 #define EATAB {while (isascii((unsigned char) *l) && \
53 isspace((unsigned char) *l)) ++l;}
54 #define LOWCASE(l) (isupper((unsigned char) (l)) ? \
55 tolower((unsigned char) (l)) : (l))
57 * Work around a bug in headers on Digital Unix.
58 * At least confirmed for: OSF1 V4.0 878
60 #if defined(__osf__) && defined(__DECC)
67 #define MAP_FAILED (void *) -1
80 int file_formats[FILE_NAMES_SIZE];
81 const size_t file_nformats = FILE_NAMES_SIZE;
82 const char *file_names[FILE_NAMES_SIZE];
83 const size_t file_nnames = FILE_NAMES_SIZE;
85 private int getvalue(struct magic_set *ms, struct magic *, const char **, int);
86 private int hextoint(int);
87 private const char *getstr(struct magic_set *, struct magic *, const char *,
89 private int parse(struct magic_set *, struct magic_entry **, uint32_t *,
90 const char *, size_t, int);
91 private void eatsize(const char **);
92 private int apprentice_1(struct magic_set *, const char *, int, struct mlist *);
93 private size_t apprentice_magic_strength(const struct magic *);
94 private int apprentice_sort(const void *, const void *);
95 private void apprentice_list(struct mlist *, int );
96 private int apprentice_load(struct magic_set *, struct magic **, uint32_t *,
98 private void byteswap(struct magic *, uint32_t);
99 private void bs1(struct magic *);
100 private uint16_t swap2(uint16_t);
101 private uint32_t swap4(uint32_t);
102 private uint64_t swap8(uint64_t);
103 private char *mkdbname(struct magic_set *, const char *, int);
104 private int apprentice_map(struct magic_set *, struct magic **, uint32_t *,
106 private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
108 private int check_format_type(const char *, int);
109 private int check_format(struct magic_set *, struct magic *);
110 private int get_op(char);
111 private int parse_mime(struct magic_set *, struct magic_entry *, const char *);
112 private int parse_strength(struct magic_set *, struct magic_entry *, const char *);
113 private int parse_apple(struct magic_set *, struct magic_entry *, const char *);
116 private size_t maxmagic = 0;
117 private size_t magicsize = sizeof(struct magic);
119 private const char usg_hdr[] = "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
124 int (*fun)(struct magic_set *, struct magic_entry *, const char *);
126 #define DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name }
128 DECLARE_FIELD(apple),
129 DECLARE_FIELD(strength),
136 int main(int, char *[]);
139 main(int argc, char *argv[])
142 struct magic_set *ms;
145 if ((progname = strrchr(argv[0], '/')) != NULL)
151 (void)fprintf(stderr, "Usage: %s file\n", progname);
155 if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
156 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
159 ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
161 (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
165 #endif /* COMPILE_ONLY */
167 static const struct type_tbl_s {
173 # define XX(s) s, (sizeof(s) - 1)
174 # define XX_NULL "", 0
175 { XX("byte"), FILE_BYTE, FILE_FMT_NUM },
176 { XX("short"), FILE_SHORT, FILE_FMT_NUM },
177 { XX("default"), FILE_DEFAULT, FILE_FMT_STR },
178 { XX("long"), FILE_LONG, FILE_FMT_NUM },
179 { XX("string"), FILE_STRING, FILE_FMT_STR },
180 { XX("date"), FILE_DATE, FILE_FMT_STR },
181 { XX("beshort"), FILE_BESHORT, FILE_FMT_NUM },
182 { XX("belong"), FILE_BELONG, FILE_FMT_NUM },
183 { XX("bedate"), FILE_BEDATE, FILE_FMT_STR },
184 { XX("leshort"), FILE_LESHORT, FILE_FMT_NUM },
185 { XX("lelong"), FILE_LELONG, FILE_FMT_NUM },
186 { XX("ledate"), FILE_LEDATE, FILE_FMT_STR },
187 { XX("pstring"), FILE_PSTRING, FILE_FMT_STR },
188 { XX("ldate"), FILE_LDATE, FILE_FMT_STR },
189 { XX("beldate"), FILE_BELDATE, FILE_FMT_STR },
190 { XX("leldate"), FILE_LELDATE, FILE_FMT_STR },
191 { XX("regex"), FILE_REGEX, FILE_FMT_STR },
192 { XX("bestring16"), FILE_BESTRING16, FILE_FMT_STR },
193 { XX("lestring16"), FILE_LESTRING16, FILE_FMT_STR },
194 { XX("search"), FILE_SEARCH, FILE_FMT_STR },
195 { XX("medate"), FILE_MEDATE, FILE_FMT_STR },
196 { XX("meldate"), FILE_MELDATE, FILE_FMT_STR },
197 { XX("melong"), FILE_MELONG, FILE_FMT_NUM },
198 { XX("quad"), FILE_QUAD, FILE_FMT_QUAD },
199 { XX("lequad"), FILE_LEQUAD, FILE_FMT_QUAD },
200 { XX("bequad"), FILE_BEQUAD, FILE_FMT_QUAD },
201 { XX("qdate"), FILE_QDATE, FILE_FMT_STR },
202 { XX("leqdate"), FILE_LEQDATE, FILE_FMT_STR },
203 { XX("beqdate"), FILE_BEQDATE, FILE_FMT_STR },
204 { XX("qldate"), FILE_QLDATE, FILE_FMT_STR },
205 { XX("leqldate"), FILE_LEQLDATE, FILE_FMT_STR },
206 { XX("beqldate"), FILE_BEQLDATE, FILE_FMT_STR },
207 { XX("float"), FILE_FLOAT, FILE_FMT_FLOAT },
208 { XX("befloat"), FILE_BEFLOAT, FILE_FMT_FLOAT },
209 { XX("lefloat"), FILE_LEFLOAT, FILE_FMT_FLOAT },
210 { XX("double"), FILE_DOUBLE, FILE_FMT_DOUBLE },
211 { XX("bedouble"), FILE_BEDOUBLE, FILE_FMT_DOUBLE },
212 { XX("ledouble"), FILE_LEDOUBLE, FILE_FMT_DOUBLE },
213 { XX("leid3"), FILE_LEID3, FILE_FMT_NUM },
214 { XX("beid3"), FILE_BEID3, FILE_FMT_NUM },
215 { XX("indirect"), FILE_INDIRECT, FILE_FMT_NONE },
216 { XX_NULL, FILE_INVALID, FILE_FMT_NONE },
222 get_type(const char *l, const char **t)
224 const struct type_tbl_s *p;
226 for (p = type_tbl; p->len; p++) {
227 if (strncmp(l, p->name, p->len) == 0) {
237 init_file_tables(void)
240 const struct type_tbl_s *p;
246 for (p = type_tbl; p->len; p++) {
247 assert(p->type < FILE_NAMES_SIZE);
248 file_names[p->type] = p->name;
249 file_formats[p->type] = p->format;
254 * Handle one file or directory.
257 apprentice_1(struct magic_set *ms, const char *fn, int action,
260 struct magic *magic = NULL;
266 if (magicsize != FILE_MAGICSIZE) {
267 file_error(ms, 0, "magic element size %lu != %lu",
268 (unsigned long)sizeof(*magic),
269 (unsigned long)FILE_MAGICSIZE);
273 if (action == FILE_COMPILE) {
274 rv = apprentice_load(ms, &magic, &nmagic, fn, action);
277 rv = apprentice_compile(ms, &magic, &nmagic, fn);
283 if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
284 if (ms->flags & MAGIC_CHECK)
285 file_magwarn(ms, "using regular magic file `%s'", fn);
286 rv = apprentice_load(ms, &magic, &nmagic, fn, action);
294 file_delmagic(magic, mapped, nmagic);
298 if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL) {
299 file_delmagic(magic, mapped, nmagic);
300 file_oomem(ms, sizeof(*ml));
308 mlist->prev->next = ml;
309 ml->prev = mlist->prev;
313 if (action == FILE_LIST) {
314 printf("Binary patterns:\n");
315 apprentice_list(mlist, BINTEST);
316 printf("Text patterns:\n");
317 apprentice_list(mlist, TEXTTEST);
321 #endif /* COMPILE_ONLY */
325 file_delmagic(struct magic *p, int type, size_t entries)
333 (void)munmap((void *)p, sizeof(*p) * (entries + 1));
351 /* const char *fn: list of magic files and directories */
352 protected struct mlist *
353 file_apprentice(struct magic_set *ms, const char *fn, int action)
356 int file_err, errs = -1;
359 if ((fn = magic_getpath(fn, action)) == NULL)
364 if ((mfn = strdup(fn)) == NULL) {
365 file_oomem(ms, strlen(fn));
370 if ((mlist = CAST(struct mlist *, malloc(sizeof(*mlist)))) == NULL) {
372 file_oomem(ms, sizeof(*mlist));
375 mlist->next = mlist->prev = mlist;
378 p = strchr(fn, PATHSEP);
383 file_err = apprentice_1(ms, fn, action, mlist);
384 errs = MAX(errs, file_err);
391 file_error(ms, 0, "could not find any magic files!");
399 * Get weight of this magic entry, for sorting purposes.
402 apprentice_magic_strength(const struct magic *m)
405 size_t val = 2 * MULT; /* baseline strength */
408 case FILE_DEFAULT: /* make sure this sorts last */
409 if (m->factor_op != FILE_FACTOR_OP_NONE)
432 val += m->vallen * MULT;
435 case FILE_BESTRING16:
436 case FILE_LESTRING16:
437 val += m->vallen * MULT / 2;
442 val += m->vallen * MAX(MULT / m->vallen, 1);
476 (void)fprintf(stderr, "Bad type %d\n", m->type);
481 case 'x': /* matches anything penalize */
482 case '!': /* matches almost anything penalize */
486 case '=': /* Exact match, prefer */
491 case '<': /* comparison match reduce strength */
496 case '&': /* masking bits, we could count them too */
501 (void)fprintf(stderr, "Bad relation %c\n", m->reln);
505 if (val == 0) /* ensure we only return 0 for FILE_DEFAULT */
508 switch (m->factor_op) {
509 case FILE_FACTOR_OP_NONE:
511 case FILE_FACTOR_OP_PLUS:
514 case FILE_FACTOR_OP_MINUS:
517 case FILE_FACTOR_OP_TIMES:
520 case FILE_FACTOR_OP_DIV:
528 * Magic entries with no description get a bonus because they depend
529 * on subsequent magic entries to print something.
531 if (m->desc[0] == '\0')
537 * Sort callback for sorting entries by "strength" (basically length)
540 apprentice_sort(const void *a, const void *b)
542 const struct magic_entry *ma = CAST(const struct magic_entry *, a);
543 const struct magic_entry *mb = CAST(const struct magic_entry *, b);
544 size_t sa = apprentice_magic_strength(ma->mp);
545 size_t sb = apprentice_magic_strength(mb->mp);
555 * Shows sorted patterns list in the order which is used for the matching
558 apprentice_list(struct mlist *mlist, int mode)
560 uint32_t magindex = 0;
562 for (ml = mlist->next; ml != mlist; ml = ml->next) {
563 for (magindex = 0; magindex < ml->nmagic; magindex++) {
564 struct magic *m = &ml->magic[magindex];
565 if ((m->flag & mode) != mode) {
567 while (magindex + 1 < ml->nmagic &&
568 ml->magic[magindex + 1].cont_level != 0)
570 continue; /* Skip to next top-level test*/
574 * Try to iterate over the tree until we find item with
575 * description/mimetype.
577 while (magindex + 1 < ml->nmagic &&
578 ml->magic[magindex + 1].cont_level != 0 &&
579 *ml->magic[magindex].desc == '\0' &&
580 *ml->magic[magindex].mimetype == '\0')
583 printf("Strength = %3" SIZE_T_FORMAT "u : %s [%s]\n",
584 apprentice_magic_strength(m),
585 ml->magic[magindex].desc,
586 ml->magic[magindex].mimetype);
592 set_test_type(struct magic *mstart, struct magic *m)
626 mstart->flag |= BINTEST;
630 case FILE_BESTRING16:
631 case FILE_LESTRING16:
632 /* Allow text overrides */
633 if (mstart->str_flags & STRING_TEXTTEST)
634 mstart->flag |= TEXTTEST;
636 mstart->flag |= BINTEST;
641 if (mstart->str_flags & STRING_BINTEST)
642 mstart->flag |= BINTEST;
643 if (mstart->str_flags & STRING_TEXTTEST)
644 mstart->flag |= TEXTTEST;
646 if (mstart->flag & (TEXTTEST|BINTEST))
649 /* binary test if pattern is not text */
650 if (file_looks_utf8(m->value.us, (size_t)m->vallen, NULL,
652 mstart->flag |= BINTEST;
654 mstart->flag |= TEXTTEST;
658 /* can't deduce anything; we shouldn't see this at the
663 /* invalid search type, but no need to complain here */
669 * Load and parse one file.
672 load_1(struct magic_set *ms, int action, const char *fn, int *errs,
673 struct magic_entry **marray, uint32_t *marraycount)
675 size_t lineno = 0, llen = 0;
679 FILE *f = fopen(ms->file = fn, "r");
682 file_error(ms, errno, "cannot read magic file `%s'",
688 /* read and parse this file */
689 for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;
691 if (len == 0) /* null line, garbage, etc */
693 if (line[len - 1] == '\n') {
695 line[len - 1] = '\0'; /* delete newline */
698 case '\0': /* empty, do not parse */
699 case '#': /* comment, do not parse */
702 if (line[1] == ':') {
705 for (i = 0; bang[i].name != NULL; i++) {
706 if ((size_t)(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) {
734 if (parse(ms, marray, marraycount, line, lineno,
746 * parse a file or directory of files
747 * const char *fn: name of magic file or directory
750 cmpstrp(const void *p1, const void *p2)
752 return strcmp(*(char *const *)p1, *(char *const *)p2);
756 apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
757 const char *fn, int action)
760 struct magic_entry *marray;
761 uint32_t marraycount, i, mentrycount = 0, starttest;
762 size_t slen, files = 0, maxfiles = 0;
763 char **filearr = NULL, *mfn;
768 ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
771 if ((marray = CAST(struct magic_entry *, calloc(maxmagic,
772 sizeof(*marray)))) == NULL) {
773 file_oomem(ms, maxmagic * sizeof(*marray));
778 /* print silly verbose header for USG compat. */
779 if (action == FILE_CHECK)
780 (void)fprintf(stderr, "%s\n", usg_hdr);
782 /* load directory or file */
783 if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
789 while ((d = readdir(dir)) != NULL) {
790 if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
792 strlen(fn) + strlen(d->d_name) + 2);
796 if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
800 if (files >= maxfiles) {
802 maxfiles = (maxfiles + 1) * 2;
803 mlen = maxfiles * sizeof(*filearr);
804 if ((filearr = CAST(char **,
805 realloc(filearr, mlen))) == NULL) {
806 file_oomem(ms, mlen);
812 filearr[files++] = mfn;
815 qsort(filearr, files, sizeof(*filearr), cmpstrp);
816 for (i = 0; i < files; i++) {
817 load_1(ms, action, filearr[i], &errs, &marray,
823 load_1(ms, action, fn, &errs, &marray, &marraycount);
827 /* Set types of tests */
828 for (i = 0; i < marraycount; ) {
829 if (marray[i].mp->cont_level != 0) {
836 static const char text[] = "text";
837 static const char binary[] = "binary";
838 static const size_t len = sizeof(text);
839 set_test_type(marray[starttest].mp, marray[i].mp);
840 if ((ms->flags & MAGIC_DEBUG) == 0)
842 (void)fprintf(stderr, "%s%s%s: %s\n",
843 marray[i].mp->mimetype,
844 marray[i].mp->mimetype[0] == '\0' ? "" : "; ",
845 marray[i].mp->desc[0] ? marray[i].mp->desc :
847 marray[i].mp->flag & BINTEST ? binary : text);
848 if (marray[i].mp->flag & BINTEST) {
849 char *p = strstr(marray[i].mp->desc, text);
850 if (p && (p == marray[i].mp->desc ||
851 isspace((unsigned char)p[-1])) &&
852 (p + len - marray[i].mp->desc ==
853 MAXstring || (p[len] == '\0' ||
854 isspace((unsigned char)p[len]))))
855 (void)fprintf(stderr, "*** Possible "
856 "binary test for text type\n");
858 } while (++i < marraycount && marray[i].mp->cont_level != 0);
861 qsort(marray, marraycount, sizeof(*marray), apprentice_sort);
864 * Make sure that any level 0 "default" line is last (if one exists).
866 for (i = 0; i < marraycount; i++) {
867 if (marray[i].mp->cont_level == 0 &&
868 marray[i].mp->type == FILE_DEFAULT) {
869 while (++i < marraycount)
870 if (marray[i].mp->cont_level == 0)
872 if (i != marraycount) {
874 ms->line = marray[i].mp->lineno;
876 "level 0 \"default\" did not sort last");
882 for (i = 0; i < marraycount; i++)
883 mentrycount += marray[i].cont_count;
885 slen = sizeof(**magicp) * mentrycount;
886 if ((*magicp = CAST(struct magic *, malloc(slen))) == NULL) {
887 file_oomem(ms, slen);
893 for (i = 0; i < marraycount; i++) {
894 (void)memcpy(*magicp + mentrycount, marray[i].mp,
895 marray[i].cont_count * sizeof(**magicp));
896 mentrycount += marray[i].cont_count;
899 for (i = 0; i < marraycount; i++)
907 *nmagicp = mentrycount;
914 * extend the sign bit if the comparison is to be signed
917 file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
919 if (!(m->flag & UNSIGNED)) {
922 * Do not remove the casts below. They are
923 * vital. When later compared with the data,
924 * the sign extension must have happened.
967 case FILE_BESTRING16:
968 case FILE_LESTRING16:
975 if (ms->flags & MAGIC_CHECK)
976 file_magwarn(ms, "cannot happen: m->type=%d\n",
985 string_modifier_check(struct magic_set *ms, struct magic *m)
987 if ((ms->flags & MAGIC_CHECK) == 0)
990 if (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0) {
992 "'/BHhLl' modifiers are only allowed for pascal strings\n");
996 case FILE_BESTRING16:
997 case FILE_LESTRING16:
998 if (m->str_flags != 0) {
1000 "no modifiers allowed for 16-bit strings\n");
1006 if ((m->str_flags & REGEX_OFFSET_START) != 0) {
1008 "'/%c' only allowed on regex and search\n",
1009 CHAR_REGEX_OFFSET_START);
1014 if (m->str_range == 0) {
1016 "missing range; defaulting to %d\n",
1017 STRING_DEFAULT_RANGE);
1018 m->str_range = STRING_DEFAULT_RANGE;
1023 if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) {
1024 file_magwarn(ms, "'/%c' not allowed on regex\n",
1025 CHAR_COMPACT_WHITESPACE);
1028 if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) {
1029 file_magwarn(ms, "'/%c' not allowed on regex\n",
1030 CHAR_COMPACT_OPTIONAL_WHITESPACE);
1035 file_magwarn(ms, "coding error: m->type=%d\n",
1055 return FILE_OPMINUS;
1057 return FILE_OPMULTIPLY;
1059 return FILE_OPDIVIDE;
1061 return FILE_OPMODULO;
1067 #ifdef ENABLE_CONDITIONALS
1069 get_cond(const char *l, const char **t)
1071 static const struct cond_tbl_s {
1076 { "if", 2, COND_IF },
1077 { "elif", 4, COND_ELIF },
1078 { "else", 4, COND_ELSE },
1079 { "", 0, COND_NONE },
1081 const struct cond_tbl_s *p;
1083 for (p = cond_tbl; p->len; p++) {
1084 if (strncmp(l, p->name, p->len) == 0 &&
1085 isspace((unsigned char)l[p->len])) {
1095 check_cond(struct magic_set *ms, int cond, uint32_t cont_level)
1098 last_cond = ms->c.li[cont_level].last_cond;
1102 if (last_cond != COND_NONE && last_cond != COND_ELIF) {
1103 if (ms->flags & MAGIC_CHECK)
1104 file_magwarn(ms, "syntax error: `if'");
1107 last_cond = COND_IF;
1111 if (last_cond != COND_IF && last_cond != COND_ELIF) {
1112 if (ms->flags & MAGIC_CHECK)
1113 file_magwarn(ms, "syntax error: `elif'");
1116 last_cond = COND_ELIF;
1120 if (last_cond != COND_IF && last_cond != COND_ELIF) {
1121 if (ms->flags & MAGIC_CHECK)
1122 file_magwarn(ms, "syntax error: `else'");
1125 last_cond = COND_NONE;
1129 last_cond = COND_NONE;
1133 ms->c.li[cont_level].last_cond = last_cond;
1136 #endif /* ENABLE_CONDITIONALS */
1139 * parse one line from magic file, put into magic[index++] if valid
1142 parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
1143 const char *line, size_t lineno, int action)
1145 #ifdef ENABLE_CONDITIONALS
1146 static uint32_t last_cont_level = 0;
1149 struct magic_entry *me;
1151 const char *l = line;
1154 uint32_t cont_level;
1159 ++l; /* step over */
1162 #ifdef ENABLE_CONDITIONALS
1163 if (cont_level == 0 || cont_level > last_cont_level)
1164 if (file_check_mem(ms, cont_level) == -1)
1166 last_cont_level = cont_level;
1169 #define ALLOC_CHUNK (size_t)10
1170 #define ALLOC_INCR (size_t)200
1172 if (cont_level != 0) {
1173 if (*nmentryp == 0) {
1174 file_error(ms, 0, "No current entry for continuation");
1177 me = &(*mentryp)[*nmentryp - 1];
1178 if (me->cont_count == me->max_count) {
1180 size_t cnt = me->max_count + ALLOC_CHUNK;
1181 if ((nm = CAST(struct magic *, realloc(me->mp,
1182 sizeof(*nm) * cnt))) == NULL) {
1183 file_oomem(ms, sizeof(*nm) * cnt);
1187 me->max_count = CAST(uint32_t, cnt);
1189 m = &me->mp[me->cont_count++];
1190 (void)memset(m, 0, sizeof(*m));
1191 m->cont_level = cont_level;
1193 if (*nmentryp == maxmagic) {
1194 struct magic_entry *mp;
1196 maxmagic += ALLOC_INCR;
1197 if ((mp = CAST(struct magic_entry *,
1198 realloc(*mentryp, sizeof(*mp) * maxmagic))) ==
1200 file_oomem(ms, sizeof(*mp) * maxmagic);
1203 (void)memset(&mp[*nmentryp], 0, sizeof(*mp) *
1207 me = &(*mentryp)[*nmentryp];
1208 if (me->mp == NULL) {
1209 size_t len = sizeof(*m) * ALLOC_CHUNK;
1210 if ((m = CAST(struct magic *, malloc(len))) == NULL) {
1211 file_oomem(ms, len);
1215 me->max_count = ALLOC_CHUNK;
1218 (void)memset(m, 0, sizeof(*m));
1219 m->factor_op = FILE_FACTOR_OP_NONE;
1223 m->lineno = CAST(uint32_t, lineno);
1225 if (*l == '&') { /* m->cont_level == 0 checked below. */
1226 ++l; /* step over */
1230 ++l; /* step over */
1232 if (m->flag & OFFADD)
1233 m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
1235 if (*l == '&') { /* m->cont_level == 0 checked below */
1236 ++l; /* step over */
1240 /* Indirect offsets are not valid at level 0. */
1241 if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD)))
1242 if (ms->flags & MAGIC_CHECK)
1243 file_magwarn(ms, "relative offset at level 0");
1245 /* get offset, then skip over it */
1246 m->offset = (uint32_t)strtoul(l, &t, 0);
1248 if (ms->flags & MAGIC_CHECK)
1249 file_magwarn(ms, "offset `%s' invalid", l);
1252 if (m->flag & INDIR) {
1253 m->in_type = FILE_LONG;
1256 * read [.lbs][+-]nnnnn)
1262 m->in_type = FILE_LELONG;
1265 m->in_type = FILE_BELONG;
1268 m->in_type = FILE_MELONG;
1272 m->in_type = FILE_LESHORT;
1276 m->in_type = FILE_BESHORT;
1282 m->in_type = FILE_BYTE;
1287 m->in_type = FILE_LEDOUBLE;
1292 m->in_type = FILE_BEDOUBLE;
1295 m->in_type = FILE_LEID3;
1298 m->in_type = FILE_BEID3;
1301 if (ms->flags & MAGIC_CHECK)
1303 "indirect offset type `%c' invalid",
1312 m->in_op |= FILE_OPINVERSE;
1315 if ((op = get_op(*l)) != -1) {
1320 m->in_op |= FILE_OPINDIRECT;
1323 if (isdigit((unsigned char)*l) || *l == '-') {
1324 m->in_offset = (int32_t)strtol(l, &t, 0);
1326 if (ms->flags & MAGIC_CHECK)
1328 "in_offset `%s' invalid", l);
1332 ((m->in_op & FILE_OPINDIRECT) && *l++ != ')'))
1333 if (ms->flags & MAGIC_CHECK)
1335 "missing ')' in indirect offset");
1339 #ifdef ENABLE_CONDITIONALS
1340 m->cond = get_cond(l, &l);
1341 if (check_cond(ms, m->cond, cont_level) == -1)
1349 m->flag |= UNSIGNED;
1352 m->type = get_type(l, &l);
1353 if (m->type == FILE_INVALID) {
1354 if (ms->flags & MAGIC_CHECK)
1355 file_magwarn(ms, "type `%s' invalid", l);
1359 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
1360 /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
1364 if (!IS_STRING(m->type))
1365 m->mask_op |= FILE_OPINVERSE;
1366 else if (ms->flags & MAGIC_CHECK)
1367 file_magwarn(ms, "'~' invalid for string types");
1371 m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
1372 if ((op = get_op(*l)) != -1) {
1373 if (!IS_STRING(m->type)) {
1377 val = (uint64_t)strtoull(l, &t, 0);
1379 m->num_mask = file_signextend(ms, m, val);
1382 else if (op == FILE_OPDIVIDE) {
1384 while (!isspace((unsigned char)*++l)) {
1386 case '0': case '1': case '2':
1387 case '3': case '4': case '5':
1388 case '6': case '7': case '8':
1391 (ms->flags & MAGIC_CHECK))
1395 m->str_range = CAST(uint32_t,
1397 if (m->str_range == 0)
1402 case CHAR_COMPACT_WHITESPACE:
1404 STRING_COMPACT_WHITESPACE;
1406 case CHAR_COMPACT_OPTIONAL_WHITESPACE:
1408 STRING_COMPACT_OPTIONAL_WHITESPACE;
1410 case CHAR_IGNORE_LOWERCASE:
1411 m->str_flags |= STRING_IGNORE_LOWERCASE;
1413 case CHAR_IGNORE_UPPERCASE:
1414 m->str_flags |= STRING_IGNORE_UPPERCASE;
1416 case CHAR_REGEX_OFFSET_START:
1417 m->str_flags |= REGEX_OFFSET_START;
1420 m->str_flags |= STRING_BINTEST;
1423 m->str_flags |= STRING_TEXTTEST;
1425 case CHAR_PSTRING_1_LE:
1426 if (m->type != FILE_PSTRING)
1428 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_1_LE;
1430 case CHAR_PSTRING_2_BE:
1431 if (m->type != FILE_PSTRING)
1433 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_BE;
1435 case CHAR_PSTRING_2_LE:
1436 if (m->type != FILE_PSTRING)
1438 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_LE;
1440 case CHAR_PSTRING_4_BE:
1441 if (m->type != FILE_PSTRING)
1443 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_BE;
1445 case CHAR_PSTRING_4_LE:
1446 if (m->type != FILE_PSTRING)
1448 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_LE;
1450 case CHAR_PSTRING_LENGTH_INCLUDES_ITSELF:
1451 if (m->type != FILE_PSTRING)
1453 m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF;
1457 if (ms->flags & MAGIC_CHECK)
1459 "string extension `%c' "
1463 /* allow multiple '/' for readability */
1465 !isspace((unsigned char)l[2]))
1468 if (string_modifier_check(ms, m) == -1)
1472 if (ms->flags & MAGIC_CHECK)
1473 file_magwarn(ms, "invalid string op: %c", *t);
1478 * We used to set mask to all 1's here, instead let's just not do
1479 * anything if mask = 0 (unless you have a better idea)
1489 if (ms->flags & MAGIC_CHECK) {
1490 file_magwarn(ms, "%c= not supported",
1497 /* Old-style anding: "0 byte &0x80 dynamically linked" */
1504 /* HP compat: ignore &= etc. */
1513 m->reln = '='; /* the default relation */
1514 if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
1515 isspace((unsigned char)l[1])) || !l[1])) {
1522 * Grab the value part, except for an 'x' reln.
1524 if (m->reln != 'x' && getvalue(ms, m, &l, action))
1528 * TODO finish this macro and start using it!
1529 * #define offsetcheck {if (offset > HOWMANY-1)
1530 * magwarn("offset too big"); }
1534 * Now get last part - the description
1540 } else if ((l[0] == '\\') && (l[1] == 'b')) {
1545 for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); )
1547 if (i == sizeof(m->desc)) {
1548 m->desc[sizeof(m->desc) - 1] = '\0';
1549 if (ms->flags & MAGIC_CHECK)
1550 file_magwarn(ms, "description `%s' truncated", m->desc);
1554 * We only do this check while compiling, or if any of the magic
1555 * files were not compiled.
1557 if (ms->flags & MAGIC_CHECK) {
1558 if (check_format(ms, m) == -1)
1561 #ifndef COMPILE_ONLY
1562 if (action == FILE_CHECK) {
1566 m->mimetype[0] = '\0'; /* initialise MIME type to none */
1567 if (m->cont_level == 0)
1568 ++(*nmentryp); /* make room for next */
1573 * parse a STRENGTH annotation line from magic file, put into magic[index - 1]
1577 parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line)
1579 const char *l = line;
1581 unsigned long factor;
1582 struct magic *m = &me->mp[0];
1584 if (m->factor_op != FILE_FACTOR_OP_NONE) {
1586 "Current entry already has a strength type: %c %d",
1587 m->factor_op, m->factor);
1592 case FILE_FACTOR_OP_NONE:
1593 case FILE_FACTOR_OP_PLUS:
1594 case FILE_FACTOR_OP_MINUS:
1595 case FILE_FACTOR_OP_TIMES:
1596 case FILE_FACTOR_OP_DIV:
1597 m->factor_op = *l++;
1600 file_magwarn(ms, "Unknown factor op `%c'", *l);
1604 factor = strtoul(l, &el, 0);
1606 file_magwarn(ms, "Too large factor `%lu'", factor);
1609 if (*el && !isspace((unsigned char)*el)) {
1610 file_magwarn(ms, "Bad factor `%s'", l);
1613 m->factor = (uint8_t)factor;
1614 if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) {
1615 file_magwarn(ms, "Cannot have factor op `%c' and factor %u",
1616 m->factor_op, m->factor);
1621 m->factor_op = FILE_FACTOR_OP_NONE;
1627 * Parse an Apple CREATOR/TYPE annotation from magic file and put it into
1631 parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line)
1634 const char *l = line;
1635 struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
1637 if (m->apple[0] != '\0') {
1638 file_magwarn(ms, "Current entry already has a APPLE type "
1639 "`%.8s', new type `%s'", m->mimetype, l);
1644 for (i = 0; *l && ((isascii((unsigned char)*l) &&
1645 isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
1646 i < sizeof(m->apple); m->apple[i++] = *l++)
1648 if (i == sizeof(m->apple) && *l) {
1649 /* We don't need to NUL terminate here, printing handles it */
1650 if (ms->flags & MAGIC_CHECK)
1651 file_magwarn(ms, "APPLE type `%s' truncated %"
1652 SIZE_T_FORMAT "u", line, i);
1662 * parse a MIME annotation line from magic file, put into magic[index - 1]
1666 parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line)
1669 const char *l = line;
1670 struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
1672 if (m->mimetype[0] != '\0') {
1673 file_magwarn(ms, "Current entry already has a MIME type `%s',"
1674 " new type `%s'", m->mimetype, l);
1679 for (i = 0; *l && ((isascii((unsigned char)*l) &&
1680 isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
1681 i < sizeof(m->mimetype); m->mimetype[i++] = *l++)
1683 if (i == sizeof(m->mimetype)) {
1684 m->mimetype[sizeof(m->mimetype) - 1] = '\0';
1685 if (ms->flags & MAGIC_CHECK)
1686 file_magwarn(ms, "MIME type `%s' truncated %"
1687 SIZE_T_FORMAT "u", m->mimetype, i);
1689 m->mimetype[i] = '\0';
1698 check_format_type(const char *ptr, int type)
1702 /* Missing format string; bad */
1715 while (isdigit((unsigned char)*ptr)) ptr++;
1718 while (isdigit((unsigned char)*ptr)) ptr++;
1770 case FILE_FMT_FLOAT:
1771 case FILE_FMT_DOUBLE:
1776 while (isdigit((unsigned char)*ptr)) ptr++;
1779 while (isdigit((unsigned char)*ptr)) ptr++;
1798 while (isdigit((unsigned char )*ptr))
1802 while (isdigit((unsigned char )*ptr))
1814 /* internal error */
1822 * Check that the optional printf format in description matches
1823 * the type of the magic.
1826 check_format(struct magic_set *ms, struct magic *m)
1830 for (ptr = m->desc; *ptr; ptr++)
1834 /* No format string; ok */
1838 assert(file_nformats == file_nnames);
1840 if (m->type >= file_nformats) {
1841 file_magwarn(ms, "Internal error inconsistency between "
1842 "m->type and format strings");
1845 if (file_formats[m->type] == FILE_FMT_NONE) {
1846 file_magwarn(ms, "No format string for `%s' with description "
1847 "`%s'", m->desc, file_names[m->type]);
1852 if (check_format_type(ptr, file_formats[m->type]) == -1) {
1854 * TODO: this error message is unhelpful if the format
1855 * string is not one character long
1857 file_magwarn(ms, "Printf format `%c' is not valid for type "
1858 "`%s' in description `%s'", *ptr ? *ptr : '?',
1859 file_names[m->type], m->desc);
1863 for (; *ptr; ptr++) {
1866 "Too many format strings (should have at most one) "
1867 "for `%s' with description `%s'",
1868 file_names[m->type], m->desc);
1876 * Read a numeric value from a pointer, into the value union of a magic
1877 * pointer, according to the magic type. Update the string pointer to point
1878 * just after the number read. Return 0 for success, non-zero for failure.
1881 getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
1884 case FILE_BESTRING16:
1885 case FILE_LESTRING16:
1890 *p = getstr(ms, m, *p, action == FILE_COMPILE);
1892 if (ms->flags & MAGIC_CHECK)
1893 file_magwarn(ms, "cannot get string from `%s'",
1901 if (m->reln != 'x') {
1903 #if defined(HAVE_STRTOF) && !defined(COMPILE_ONLY)
1904 m->value.f = strtof(*p, &ep);
1906 m->value.f = (float)strtod(*p, &ep);
1914 if (m->reln != 'x') {
1916 m->value.d = strtod(*p, &ep);
1921 if (m->reln != 'x') {
1923 m->value.q = file_signextend(ms, m,
1924 (uint64_t)strtoull(*p, &ep, 0));
1933 * Convert a string containing C character escapes. Stop at an unescaped
1935 * Copy the converted version to "m->value.s", and the length in m->vallen.
1936 * Return updated scan pointer as function result. Warn if set.
1938 private const char *
1939 getstr(struct magic_set *ms, struct magic *m, const char *s, int warn)
1941 const char *origs = s;
1942 char *p = m->value.s;
1943 size_t plen = sizeof(m->value.s);
1945 char *pmax = p + plen - 1;
1949 while ((c = *s++) != '\0') {
1950 if (isspace((unsigned char) c))
1953 file_error(ms, 0, "string too long: `%s'", origs);
1961 file_magwarn(ms, "incomplete escape");
1967 "escaped tab found, use \\t instead");
1968 warn = 0; /* already did */
1973 if (isprint((unsigned char)c)) {
1974 /* Allow escaping of
1976 if (strchr("<>&^=!", c) == NULL
1977 && (m->type != FILE_REGEX ||
1978 strchr("[]().*?^$|{}", c)
1980 file_magwarn(ms, "no "
1986 "unknown escape sequence: "
1991 /* space, perhaps force people to use \040? */
1995 * Other things people escape, but shouldn't need to,
1996 * so we disallow them
2009 /* and baskslash itself */
2042 /* \ and up to 3 octal digits */
2052 c = *s++; /* try for 2 */
2053 if (c >= '0' && c <= '7') {
2054 val = (val << 3) | (c - '0');
2055 c = *s++; /* try for 3 */
2056 if (c >= '0' && c <= '7')
2057 val = (val << 3) | (c-'0');
2066 /* \x and up to 2 hex digits */
2068 val = 'x'; /* Default if no digits */
2069 c = hextoint(*s++); /* Get next char */
2074 val = (val << 4) + c;
2087 m->vallen = CAST(unsigned char, (p - origp));
2088 if (m->type == FILE_PSTRING)
2089 m->vallen += file_pstring_length_size(m);
2094 /* Single hex char to int; -1 if not a hex char. */
2098 if (!isascii((unsigned char) c))
2100 if (isdigit((unsigned char) c))
2102 if ((c >= 'a') && (c <= 'f'))
2103 return c + 10 - 'a';
2104 if (( c>= 'A') && (c <= 'F'))
2105 return c + 10 - 'A';
2111 * Print a string containing C character escapes.
2114 file_showstr(FILE *fp, const char *s, size_t len)
2129 if (c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
2130 (void) fputc(c, fp);
2132 (void) fputc('\\', fp);
2135 (void) fputc('a', fp);
2139 (void) fputc('b', fp);
2143 (void) fputc('f', fp);
2147 (void) fputc('n', fp);
2151 (void) fputc('r', fp);
2155 (void) fputc('t', fp);
2159 (void) fputc('v', fp);
2163 (void) fprintf(fp, "%.3o", c & 0377);
2171 * eatsize(): Eat the size spec from a number [eg. 10UL]
2174 eatsize(const char **p)
2178 if (LOWCASE(*l) == 'u')
2181 switch (LOWCASE(*l)) {
2182 case 'l': /* long */
2183 case 's': /* short */
2184 case 'h': /* short */
2185 case 'b': /* char/byte */
2186 case 'c': /* char/byte */
2197 * handle a compiled file.
2200 apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
2208 char *dbname = NULL;
2211 dbname = mkdbname(ms, fn, 0);
2215 if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
2218 if (fstat(fd, &st) == -1) {
2219 file_error(ms, errno, "cannot stat `%s'", dbname);
2222 if (st.st_size < 8) {
2223 file_error(ms, 0, "file `%s' is too small", dbname);
2228 if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
2229 MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
2230 file_error(ms, errno, "cannot map `%s'", dbname);
2235 if ((mm = CAST(void *, malloc((size_t)st.st_size))) == NULL) {
2236 file_oomem(ms, (size_t)st.st_size);
2239 if (read(fd, mm, (size_t)st.st_size) != (ssize_t)st.st_size) {
2245 *magicp = CAST(struct magic *, mm);
2248 ptr = (uint32_t *)(void *)*magicp;
2249 if (*ptr != MAGICNO) {
2250 if (swap4(*ptr) != MAGICNO) {
2251 file_error(ms, 0, "bad magic in `%s'", dbname);
2258 version = swap4(ptr[1]);
2261 if (version != VERSIONNO) {
2262 file_error(ms, 0, "File %s supports only version %d magic "
2263 "files. `%s' is version %d", VERSION,
2264 VERSIONNO, dbname, version);
2267 *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic));
2272 byteswap(*magicp, *nmagicp);
2281 (void)munmap((void *)mm, (size_t)st.st_size);
2294 private const uint32_t ar[] = {
2298 * handle an mmaped file.
2301 apprentice_compile(struct magic_set *ms, struct magic **magicp,
2302 uint32_t *nmagicp, const char *fn)
2308 dbname = mkdbname(ms, fn, 1);
2313 if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) {
2314 file_error(ms, errno, "cannot open `%s'", dbname);
2318 if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
2319 file_error(ms, errno, "error writing `%s'", dbname);
2323 if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
2324 != sizeof(struct magic)) {
2325 file_error(ms, errno, "error seeking `%s'", dbname);
2329 if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
2330 != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
2331 file_error(ms, errno, "error writing `%s'", dbname);
2342 private const char ext[] = ".mgc";
2347 mkdbname(struct magic_set *ms, const char *fn, int strip)
2353 if ((p = strrchr(fn, '/')) != NULL)
2357 for (q = fn; *q; q++)
2360 for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--)
2364 /* Did not find .mgc, restore q */
2370 /* Compatibility with old code that looked in .mime */
2371 if (ms->flags & MAGIC_MIME) {
2372 asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext);
2373 if (access(buf, R_OK) != -1) {
2374 ms->flags &= MAGIC_MIME_TYPE;
2379 asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext);
2381 /* Compatibility with old code that looked in .mime */
2382 if (strstr(p, ".mime") != NULL)
2383 ms->flags &= MAGIC_MIME_TYPE;
2388 * Byteswap an mmap'ed file if needed
2391 byteswap(struct magic *magic, uint32_t nmagic)
2394 for (i = 0; i < nmagic; i++)
2405 uint8_t *s = (uint8_t *)(void *)&sv;
2406 uint8_t *d = (uint8_t *)(void *)&rv;
2419 uint8_t *s = (uint8_t *)(void *)&sv;
2420 uint8_t *d = (uint8_t *)(void *)&rv;
2435 uint8_t *s = (uint8_t *)(void *)&sv;
2436 uint8_t *d = (uint8_t *)(void *)&rv;
2460 * byteswap a single magic entry
2463 bs1(struct magic *m)
2465 m->cont_level = swap2(m->cont_level);
2466 m->offset = swap4((uint32_t)m->offset);
2467 m->in_offset = swap4((uint32_t)m->in_offset);
2468 m->lineno = swap4((uint32_t)m->lineno);
2469 if (IS_STRING(m->type)) {
2470 if (m->type == FILE_PSTRING)
2471 printf("flags! %d\n", m->str_flags);
2472 m->str_range = swap4(m->str_range);
2473 m->str_flags = swap4(m->str_flags);
2476 m->value.q = swap8(m->value.q);
2477 m->num_mask = swap8(m->num_mask);
2482 file_pstring_length_size(const struct magic *m)
2484 switch (m->str_flags & PSTRING_LEN) {
2494 abort(); /* Impossible */
2499 file_pstring_get_length(const struct magic *m, const char *s)
2503 switch (m->str_flags & PSTRING_LEN) {
2508 len = (s[1] << 8) | s[0];
2511 len = (s[0] << 8) | s[1];
2514 len = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0];
2517 len = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
2520 abort(); /* Impossible */
2523 if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
2524 len -= file_pstring_length_size(m);