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.173 2011/12/08 12:38:24 rrt 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,
745 * parse a file or directory of files
746 * const char *fn: name of magic file or directory
749 cmpstrp(const void *p1, const void *p2)
751 return strcmp(*(char *const *)p1, *(char *const *)p2);
755 apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
756 const char *fn, int action)
759 struct magic_entry *marray;
760 uint32_t marraycount, i, mentrycount = 0, starttest;
761 size_t slen, files = 0, maxfiles = 0;
762 char **filearr = NULL, *mfn;
767 ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
770 if ((marray = CAST(struct magic_entry *, calloc(maxmagic,
771 sizeof(*marray)))) == NULL) {
772 file_oomem(ms, maxmagic * sizeof(*marray));
777 /* print silly verbose header for USG compat. */
778 if (action == FILE_CHECK)
779 (void)fprintf(stderr, "%s\n", usg_hdr);
781 /* load directory or file */
782 if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
788 while ((d = readdir(dir)) != NULL) {
789 if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
791 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);
813 filearr[files++] = mfn;
816 qsort(filearr, files, sizeof(*filearr), cmpstrp);
817 for (i = 0; i < files; i++) {
818 load_1(ms, action, filearr[i], &errs, &marray,
824 load_1(ms, action, fn, &errs, &marray, &marraycount);
828 /* Set types of tests */
829 for (i = 0; i < marraycount; ) {
830 if (marray[i].mp->cont_level != 0) {
837 static const char text[] = "text";
838 static const char binary[] = "binary";
839 static const size_t len = sizeof(text);
840 set_test_type(marray[starttest].mp, marray[i].mp);
841 if ((ms->flags & MAGIC_DEBUG) == 0)
843 (void)fprintf(stderr, "%s%s%s: %s\n",
844 marray[i].mp->mimetype,
845 marray[i].mp->mimetype[0] == '\0' ? "" : "; ",
846 marray[i].mp->desc[0] ? marray[i].mp->desc :
848 marray[i].mp->flag & BINTEST ? binary : text);
849 if (marray[i].mp->flag & BINTEST) {
850 char *p = strstr(marray[i].mp->desc, text);
851 if (p && (p == marray[i].mp->desc ||
852 isspace((unsigned char)p[-1])) &&
853 (p + len - marray[i].mp->desc ==
854 MAXstring || (p[len] == '\0' ||
855 isspace((unsigned char)p[len]))))
856 (void)fprintf(stderr, "*** Possible "
857 "binary test for text type\n");
859 } while (++i < marraycount && marray[i].mp->cont_level != 0);
862 qsort(marray, marraycount, sizeof(*marray), apprentice_sort);
865 * Make sure that any level 0 "default" line is last (if one exists).
867 for (i = 0; i < marraycount; i++) {
868 if (marray[i].mp->cont_level == 0 &&
869 marray[i].mp->type == FILE_DEFAULT) {
870 while (++i < marraycount)
871 if (marray[i].mp->cont_level == 0)
873 if (i != marraycount) {
875 ms->line = marray[i].mp->lineno;
877 "level 0 \"default\" did not sort last");
883 for (i = 0; i < marraycount; i++)
884 mentrycount += marray[i].cont_count;
886 slen = sizeof(**magicp) * mentrycount;
887 if ((*magicp = CAST(struct magic *, malloc(slen))) == NULL) {
888 file_oomem(ms, slen);
894 for (i = 0; i < marraycount; i++) {
895 (void)memcpy(*magicp + mentrycount, marray[i].mp,
896 marray[i].cont_count * sizeof(**magicp));
897 mentrycount += marray[i].cont_count;
900 for (i = 0; i < marraycount; i++)
908 *nmagicp = mentrycount;
915 * extend the sign bit if the comparison is to be signed
918 file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
920 if (!(m->flag & UNSIGNED)) {
923 * Do not remove the casts below. They are
924 * vital. When later compared with the data,
925 * the sign extension must have happened.
968 case FILE_BESTRING16:
969 case FILE_LESTRING16:
976 if (ms->flags & MAGIC_CHECK)
977 file_magwarn(ms, "cannot happen: m->type=%d\n",
986 string_modifier_check(struct magic_set *ms, struct magic *m)
988 if ((ms->flags & MAGIC_CHECK) == 0)
991 if (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0) {
993 "'/BHhLl' modifiers are only allowed for pascal strings\n");
997 case FILE_BESTRING16:
998 case FILE_LESTRING16:
999 if (m->str_flags != 0) {
1001 "no modifiers allowed for 16-bit strings\n");
1007 if ((m->str_flags & REGEX_OFFSET_START) != 0) {
1009 "'/%c' only allowed on regex and search\n",
1010 CHAR_REGEX_OFFSET_START);
1015 if (m->str_range == 0) {
1017 "missing range; defaulting to %d\n",
1018 STRING_DEFAULT_RANGE);
1019 m->str_range = STRING_DEFAULT_RANGE;
1024 if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) {
1025 file_magwarn(ms, "'/%c' not allowed on regex\n",
1026 CHAR_COMPACT_WHITESPACE);
1029 if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) {
1030 file_magwarn(ms, "'/%c' not allowed on regex\n",
1031 CHAR_COMPACT_OPTIONAL_WHITESPACE);
1036 file_magwarn(ms, "coding error: m->type=%d\n",
1056 return FILE_OPMINUS;
1058 return FILE_OPMULTIPLY;
1060 return FILE_OPDIVIDE;
1062 return FILE_OPMODULO;
1068 #ifdef ENABLE_CONDITIONALS
1070 get_cond(const char *l, const char **t)
1072 static const struct cond_tbl_s {
1077 { "if", 2, COND_IF },
1078 { "elif", 4, COND_ELIF },
1079 { "else", 4, COND_ELSE },
1080 { "", 0, COND_NONE },
1082 const struct cond_tbl_s *p;
1084 for (p = cond_tbl; p->len; p++) {
1085 if (strncmp(l, p->name, p->len) == 0 &&
1086 isspace((unsigned char)l[p->len])) {
1096 check_cond(struct magic_set *ms, int cond, uint32_t cont_level)
1099 last_cond = ms->c.li[cont_level].last_cond;
1103 if (last_cond != COND_NONE && last_cond != COND_ELIF) {
1104 if (ms->flags & MAGIC_CHECK)
1105 file_magwarn(ms, "syntax error: `if'");
1108 last_cond = COND_IF;
1112 if (last_cond != COND_IF && last_cond != COND_ELIF) {
1113 if (ms->flags & MAGIC_CHECK)
1114 file_magwarn(ms, "syntax error: `elif'");
1117 last_cond = COND_ELIF;
1121 if (last_cond != COND_IF && last_cond != COND_ELIF) {
1122 if (ms->flags & MAGIC_CHECK)
1123 file_magwarn(ms, "syntax error: `else'");
1126 last_cond = COND_NONE;
1130 last_cond = COND_NONE;
1134 ms->c.li[cont_level].last_cond = last_cond;
1137 #endif /* ENABLE_CONDITIONALS */
1140 * parse one line from magic file, put into magic[index++] if valid
1143 parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
1144 const char *line, size_t lineno, int action)
1146 #ifdef ENABLE_CONDITIONALS
1147 static uint32_t last_cont_level = 0;
1150 struct magic_entry *me;
1152 const char *l = line;
1155 uint32_t cont_level;
1160 ++l; /* step over */
1163 #ifdef ENABLE_CONDITIONALS
1164 if (cont_level == 0 || cont_level > last_cont_level)
1165 if (file_check_mem(ms, cont_level) == -1)
1167 last_cont_level = cont_level;
1170 #define ALLOC_CHUNK (size_t)10
1171 #define ALLOC_INCR (size_t)200
1173 if (cont_level != 0) {
1174 if (*nmentryp == 0) {
1175 file_error(ms, 0, "No current entry for continuation");
1178 me = &(*mentryp)[*nmentryp - 1];
1179 if (me->cont_count == me->max_count) {
1181 size_t cnt = me->max_count + ALLOC_CHUNK;
1182 if ((nm = CAST(struct magic *, realloc(me->mp,
1183 sizeof(*nm) * cnt))) == NULL) {
1184 file_oomem(ms, sizeof(*nm) * cnt);
1188 me->max_count = CAST(uint32_t, cnt);
1190 m = &me->mp[me->cont_count++];
1191 (void)memset(m, 0, sizeof(*m));
1192 m->cont_level = cont_level;
1194 if (*nmentryp == maxmagic) {
1195 struct magic_entry *mp;
1197 maxmagic += ALLOC_INCR;
1198 if ((mp = CAST(struct magic_entry *,
1199 realloc(*mentryp, sizeof(*mp) * maxmagic))) ==
1201 file_oomem(ms, sizeof(*mp) * maxmagic);
1204 (void)memset(&mp[*nmentryp], 0, sizeof(*mp) *
1208 me = &(*mentryp)[*nmentryp];
1209 if (me->mp == NULL) {
1210 size_t len = sizeof(*m) * ALLOC_CHUNK;
1211 if ((m = CAST(struct magic *, malloc(len))) == NULL) {
1212 file_oomem(ms, len);
1216 me->max_count = ALLOC_CHUNK;
1219 (void)memset(m, 0, sizeof(*m));
1220 m->factor_op = FILE_FACTOR_OP_NONE;
1224 m->lineno = CAST(uint32_t, lineno);
1226 if (*l == '&') { /* m->cont_level == 0 checked below. */
1227 ++l; /* step over */
1231 ++l; /* step over */
1233 if (m->flag & OFFADD)
1234 m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
1236 if (*l == '&') { /* m->cont_level == 0 checked below */
1237 ++l; /* step over */
1241 /* Indirect offsets are not valid at level 0. */
1242 if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD)))
1243 if (ms->flags & MAGIC_CHECK)
1244 file_magwarn(ms, "relative offset at level 0");
1246 /* get offset, then skip over it */
1247 m->offset = (uint32_t)strtoul(l, &t, 0);
1249 if (ms->flags & MAGIC_CHECK)
1250 file_magwarn(ms, "offset `%s' invalid", l);
1253 if (m->flag & INDIR) {
1254 m->in_type = FILE_LONG;
1257 * read [.lbs][+-]nnnnn)
1263 m->in_type = FILE_LELONG;
1266 m->in_type = FILE_BELONG;
1269 m->in_type = FILE_MELONG;
1273 m->in_type = FILE_LESHORT;
1277 m->in_type = FILE_BESHORT;
1283 m->in_type = FILE_BYTE;
1288 m->in_type = FILE_LEDOUBLE;
1293 m->in_type = FILE_BEDOUBLE;
1296 m->in_type = FILE_LEID3;
1299 m->in_type = FILE_BEID3;
1302 if (ms->flags & MAGIC_CHECK)
1304 "indirect offset type `%c' invalid",
1313 m->in_op |= FILE_OPINVERSE;
1316 if ((op = get_op(*l)) != -1) {
1321 m->in_op |= FILE_OPINDIRECT;
1324 if (isdigit((unsigned char)*l) || *l == '-') {
1325 m->in_offset = (int32_t)strtol(l, &t, 0);
1327 if (ms->flags & MAGIC_CHECK)
1329 "in_offset `%s' invalid", l);
1333 ((m->in_op & FILE_OPINDIRECT) && *l++ != ')'))
1334 if (ms->flags & MAGIC_CHECK)
1336 "missing ')' in indirect offset");
1340 #ifdef ENABLE_CONDITIONALS
1341 m->cond = get_cond(l, &l);
1342 if (check_cond(ms, m->cond, cont_level) == -1)
1350 m->flag |= UNSIGNED;
1353 m->type = get_type(l, &l);
1354 if (m->type == FILE_INVALID) {
1355 if (ms->flags & MAGIC_CHECK)
1356 file_magwarn(ms, "type `%s' invalid", l);
1360 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
1361 /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
1365 if (!IS_STRING(m->type))
1366 m->mask_op |= FILE_OPINVERSE;
1367 else if (ms->flags & MAGIC_CHECK)
1368 file_magwarn(ms, "'~' invalid for string types");
1372 m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
1373 if ((op = get_op(*l)) != -1) {
1374 if (!IS_STRING(m->type)) {
1378 val = (uint64_t)strtoull(l, &t, 0);
1380 m->num_mask = file_signextend(ms, m, val);
1383 else if (op == FILE_OPDIVIDE) {
1385 while (!isspace((unsigned char)*++l)) {
1387 case '0': case '1': case '2':
1388 case '3': case '4': case '5':
1389 case '6': case '7': case '8':
1392 (ms->flags & MAGIC_CHECK))
1396 m->str_range = CAST(uint32_t,
1398 if (m->str_range == 0)
1403 case CHAR_COMPACT_WHITESPACE:
1405 STRING_COMPACT_WHITESPACE;
1407 case CHAR_COMPACT_OPTIONAL_WHITESPACE:
1409 STRING_COMPACT_OPTIONAL_WHITESPACE;
1411 case CHAR_IGNORE_LOWERCASE:
1412 m->str_flags |= STRING_IGNORE_LOWERCASE;
1414 case CHAR_IGNORE_UPPERCASE:
1415 m->str_flags |= STRING_IGNORE_UPPERCASE;
1417 case CHAR_REGEX_OFFSET_START:
1418 m->str_flags |= REGEX_OFFSET_START;
1421 m->str_flags |= STRING_BINTEST;
1424 m->str_flags |= STRING_TEXTTEST;
1426 case CHAR_PSTRING_1_LE:
1427 if (m->type != FILE_PSTRING)
1429 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_1_LE;
1431 case CHAR_PSTRING_2_BE:
1432 if (m->type != FILE_PSTRING)
1434 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_BE;
1436 case CHAR_PSTRING_2_LE:
1437 if (m->type != FILE_PSTRING)
1439 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_LE;
1441 case CHAR_PSTRING_4_BE:
1442 if (m->type != FILE_PSTRING)
1444 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_BE;
1446 case CHAR_PSTRING_4_LE:
1447 if (m->type != FILE_PSTRING)
1449 m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_LE;
1451 case CHAR_PSTRING_LENGTH_INCLUDES_ITSELF:
1452 if (m->type != FILE_PSTRING)
1454 m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF;
1458 if (ms->flags & MAGIC_CHECK)
1460 "string extension `%c' "
1464 /* allow multiple '/' for readability */
1466 !isspace((unsigned char)l[2]))
1469 if (string_modifier_check(ms, m) == -1)
1473 if (ms->flags & MAGIC_CHECK)
1474 file_magwarn(ms, "invalid string op: %c", *t);
1479 * We used to set mask to all 1's here, instead let's just not do
1480 * anything if mask = 0 (unless you have a better idea)
1490 if (ms->flags & MAGIC_CHECK) {
1491 file_magwarn(ms, "%c= not supported",
1498 /* Old-style anding: "0 byte &0x80 dynamically linked" */
1505 /* HP compat: ignore &= etc. */
1514 m->reln = '='; /* the default relation */
1515 if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
1516 isspace((unsigned char)l[1])) || !l[1])) {
1523 * Grab the value part, except for an 'x' reln.
1525 if (m->reln != 'x' && getvalue(ms, m, &l, action))
1529 * TODO finish this macro and start using it!
1530 * #define offsetcheck {if (offset > HOWMANY-1)
1531 * magwarn("offset too big"); }
1535 * Now get last part - the description
1541 } else if ((l[0] == '\\') && (l[1] == 'b')) {
1546 for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); )
1548 if (i == sizeof(m->desc)) {
1549 m->desc[sizeof(m->desc) - 1] = '\0';
1550 if (ms->flags & MAGIC_CHECK)
1551 file_magwarn(ms, "description `%s' truncated", m->desc);
1555 * We only do this check while compiling, or if any of the magic
1556 * files were not compiled.
1558 if (ms->flags & MAGIC_CHECK) {
1559 if (check_format(ms, m) == -1)
1562 #ifndef COMPILE_ONLY
1563 if (action == FILE_CHECK) {
1567 m->mimetype[0] = '\0'; /* initialise MIME type to none */
1568 if (m->cont_level == 0)
1569 ++(*nmentryp); /* make room for next */
1574 * parse a STRENGTH annotation line from magic file, put into magic[index - 1]
1578 parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line)
1580 const char *l = line;
1582 unsigned long factor;
1583 struct magic *m = &me->mp[0];
1585 if (m->factor_op != FILE_FACTOR_OP_NONE) {
1587 "Current entry already has a strength type: %c %d",
1588 m->factor_op, m->factor);
1593 case FILE_FACTOR_OP_NONE:
1594 case FILE_FACTOR_OP_PLUS:
1595 case FILE_FACTOR_OP_MINUS:
1596 case FILE_FACTOR_OP_TIMES:
1597 case FILE_FACTOR_OP_DIV:
1598 m->factor_op = *l++;
1601 file_magwarn(ms, "Unknown factor op `%c'", *l);
1605 factor = strtoul(l, &el, 0);
1607 file_magwarn(ms, "Too large factor `%lu'", factor);
1610 if (*el && !isspace((unsigned char)*el)) {
1611 file_magwarn(ms, "Bad factor `%s'", l);
1614 m->factor = (uint8_t)factor;
1615 if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) {
1616 file_magwarn(ms, "Cannot have factor op `%c' and factor %u",
1617 m->factor_op, m->factor);
1622 m->factor_op = FILE_FACTOR_OP_NONE;
1628 * Parse an Apple CREATOR/TYPE annotation from magic file and put it into
1632 parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line)
1635 const char *l = line;
1636 struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
1638 if (m->apple[0] != '\0') {
1639 file_magwarn(ms, "Current entry already has a APPLE type "
1640 "`%.8s', new type `%s'", m->mimetype, l);
1645 for (i = 0; *l && ((isascii((unsigned char)*l) &&
1646 isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
1647 i < sizeof(m->apple); m->apple[i++] = *l++)
1649 if (i == sizeof(m->apple) && *l) {
1650 /* We don't need to NUL terminate here, printing handles it */
1651 if (ms->flags & MAGIC_CHECK)
1652 file_magwarn(ms, "APPLE type `%s' truncated %"
1653 SIZE_T_FORMAT "u", line, i);
1663 * parse a MIME annotation line from magic file, put into magic[index - 1]
1667 parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line)
1670 const char *l = line;
1671 struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
1673 if (m->mimetype[0] != '\0') {
1674 file_magwarn(ms, "Current entry already has a MIME type `%s',"
1675 " new type `%s'", m->mimetype, l);
1680 for (i = 0; *l && ((isascii((unsigned char)*l) &&
1681 isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
1682 i < sizeof(m->mimetype); m->mimetype[i++] = *l++)
1684 if (i == sizeof(m->mimetype)) {
1685 m->mimetype[sizeof(m->mimetype) - 1] = '\0';
1686 if (ms->flags & MAGIC_CHECK)
1687 file_magwarn(ms, "MIME type `%s' truncated %"
1688 SIZE_T_FORMAT "u", m->mimetype, i);
1690 m->mimetype[i] = '\0';
1699 check_format_type(const char *ptr, int type)
1703 /* Missing format string; bad */
1716 while (isdigit((unsigned char)*ptr)) ptr++;
1719 while (isdigit((unsigned char)*ptr)) ptr++;
1771 case FILE_FMT_FLOAT:
1772 case FILE_FMT_DOUBLE:
1777 while (isdigit((unsigned char)*ptr)) ptr++;
1780 while (isdigit((unsigned char)*ptr)) ptr++;
1799 while (isdigit((unsigned char )*ptr))
1803 while (isdigit((unsigned char )*ptr))
1815 /* internal error */
1823 * Check that the optional printf format in description matches
1824 * the type of the magic.
1827 check_format(struct magic_set *ms, struct magic *m)
1831 for (ptr = m->desc; *ptr; ptr++)
1835 /* No format string; ok */
1839 assert(file_nformats == file_nnames);
1841 if (m->type >= file_nformats) {
1842 file_magwarn(ms, "Internal error inconsistency between "
1843 "m->type and format strings");
1846 if (file_formats[m->type] == FILE_FMT_NONE) {
1847 file_magwarn(ms, "No format string for `%s' with description "
1848 "`%s'", m->desc, file_names[m->type]);
1853 if (check_format_type(ptr, file_formats[m->type]) == -1) {
1855 * TODO: this error message is unhelpful if the format
1856 * string is not one character long
1858 file_magwarn(ms, "Printf format `%c' is not valid for type "
1859 "`%s' in description `%s'", *ptr ? *ptr : '?',
1860 file_names[m->type], m->desc);
1864 for (; *ptr; ptr++) {
1867 "Too many format strings (should have at most one) "
1868 "for `%s' with description `%s'",
1869 file_names[m->type], m->desc);
1877 * Read a numeric value from a pointer, into the value union of a magic
1878 * pointer, according to the magic type. Update the string pointer to point
1879 * just after the number read. Return 0 for success, non-zero for failure.
1882 getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
1885 case FILE_BESTRING16:
1886 case FILE_LESTRING16:
1891 *p = getstr(ms, m, *p, action == FILE_COMPILE);
1893 if (ms->flags & MAGIC_CHECK)
1894 file_magwarn(ms, "cannot get string from `%s'",
1902 if (m->reln != 'x') {
1904 #if defined(HAVE_STRTOF) && !defined(COMPILE_ONLY)
1905 m->value.f = strtof(*p, &ep);
1907 m->value.f = (float)strtod(*p, &ep);
1915 if (m->reln != 'x') {
1917 m->value.d = strtod(*p, &ep);
1922 if (m->reln != 'x') {
1924 m->value.q = file_signextend(ms, m,
1925 (uint64_t)strtoull(*p, &ep, 0));
1934 * Convert a string containing C character escapes. Stop at an unescaped
1936 * Copy the converted version to "m->value.s", and the length in m->vallen.
1937 * Return updated scan pointer as function result. Warn if set.
1939 private const char *
1940 getstr(struct magic_set *ms, struct magic *m, const char *s, int warn)
1942 const char *origs = s;
1943 char *p = m->value.s;
1944 size_t plen = sizeof(m->value.s);
1946 char *pmax = p + plen - 1;
1950 while ((c = *s++) != '\0') {
1951 if (isspace((unsigned char) c))
1954 file_error(ms, 0, "string too long: `%s'", origs);
1962 file_magwarn(ms, "incomplete escape");
1968 "escaped tab found, use \\t instead");
1969 warn = 0; /* already did */
1974 if (isprint((unsigned char)c)) {
1975 /* Allow escaping of
1977 if (strchr("<>&^=!", c) == NULL
1978 && (m->type != FILE_REGEX ||
1979 strchr("[]().*?^$|{}", c)
1981 file_magwarn(ms, "no "
1987 "unknown escape sequence: "
1992 /* space, perhaps force people to use \040? */
1996 * Other things people escape, but shouldn't need to,
1997 * so we disallow them
2010 /* and baskslash itself */
2043 /* \ and up to 3 octal digits */
2053 c = *s++; /* try for 2 */
2054 if (c >= '0' && c <= '7') {
2055 val = (val << 3) | (c - '0');
2056 c = *s++; /* try for 3 */
2057 if (c >= '0' && c <= '7')
2058 val = (val << 3) | (c-'0');
2067 /* \x and up to 2 hex digits */
2069 val = 'x'; /* Default if no digits */
2070 c = hextoint(*s++); /* Get next char */
2075 val = (val << 4) + c;
2088 m->vallen = CAST(unsigned char, (p - origp));
2089 if (m->type == FILE_PSTRING)
2090 m->vallen += (unsigned char)file_pstring_length_size(m);
2095 /* Single hex char to int; -1 if not a hex char. */
2099 if (!isascii((unsigned char) c))
2101 if (isdigit((unsigned char) c))
2103 if ((c >= 'a') && (c <= 'f'))
2104 return c + 10 - 'a';
2105 if (( c>= 'A') && (c <= 'F'))
2106 return c + 10 - 'A';
2112 * Print a string containing C character escapes.
2115 file_showstr(FILE *fp, const char *s, size_t len)
2130 if (c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
2131 (void) fputc(c, fp);
2133 (void) fputc('\\', fp);
2136 (void) fputc('a', fp);
2140 (void) fputc('b', fp);
2144 (void) fputc('f', fp);
2148 (void) fputc('n', fp);
2152 (void) fputc('r', fp);
2156 (void) fputc('t', fp);
2160 (void) fputc('v', fp);
2164 (void) fprintf(fp, "%.3o", c & 0377);
2172 * eatsize(): Eat the size spec from a number [eg. 10UL]
2175 eatsize(const char **p)
2179 if (LOWCASE(*l) == 'u')
2182 switch (LOWCASE(*l)) {
2183 case 'l': /* long */
2184 case 's': /* short */
2185 case 'h': /* short */
2186 case 'b': /* char/byte */
2187 case 'c': /* char/byte */
2198 * handle a compiled file.
2201 apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
2209 char *dbname = NULL;
2212 dbname = mkdbname(ms, fn, 0);
2216 if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
2219 if (fstat(fd, &st) == -1) {
2220 file_error(ms, errno, "cannot stat `%s'", dbname);
2223 if (st.st_size < 8) {
2224 file_error(ms, 0, "file `%s' is too small", dbname);
2229 if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
2230 MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
2231 file_error(ms, errno, "cannot map `%s'", dbname);
2236 if ((mm = CAST(void *, malloc((size_t)st.st_size))) == NULL) {
2237 file_oomem(ms, (size_t)st.st_size);
2240 if (read(fd, mm, (size_t)st.st_size) != (ssize_t)st.st_size) {
2246 *magicp = CAST(struct magic *, mm);
2249 ptr = (uint32_t *)(void *)*magicp;
2250 if (*ptr != MAGICNO) {
2251 if (swap4(*ptr) != MAGICNO) {
2252 file_error(ms, 0, "bad magic in `%s'", dbname);
2259 version = swap4(ptr[1]);
2262 if (version != VERSIONNO) {
2263 file_error(ms, 0, "File %s supports only version %d magic "
2264 "files. `%s' is version %d", VERSION,
2265 VERSIONNO, dbname, version);
2268 *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic));
2273 byteswap(*magicp, *nmagicp);
2282 (void)munmap((void *)mm, (size_t)st.st_size);
2295 private const uint32_t ar[] = {
2299 * handle an mmaped file.
2302 apprentice_compile(struct magic_set *ms, struct magic **magicp,
2303 uint32_t *nmagicp, const char *fn)
2309 dbname = mkdbname(ms, fn, 1);
2314 if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) {
2315 file_error(ms, errno, "cannot open `%s'", dbname);
2319 if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
2320 file_error(ms, errno, "error writing `%s'", dbname);
2324 if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
2325 != sizeof(struct magic)) {
2326 file_error(ms, errno, "error seeking `%s'", dbname);
2330 if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
2331 != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
2332 file_error(ms, errno, "error writing `%s'", dbname);
2344 private const char ext[] = ".mgc";
2349 mkdbname(struct magic_set *ms, const char *fn, int strip)
2355 if ((p = strrchr(fn, '/')) != NULL)
2359 for (q = fn; *q; q++)
2362 for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--)
2366 /* Did not find .mgc, restore q */
2372 /* Compatibility with old code that looked in .mime */
2373 if (ms->flags & MAGIC_MIME) {
2374 asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext);
2375 if (access(buf, R_OK) != -1) {
2376 ms->flags &= MAGIC_MIME_TYPE;
2381 asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext);
2383 /* Compatibility with old code that looked in .mime */
2384 if (strstr(p, ".mime") != NULL)
2385 ms->flags &= MAGIC_MIME_TYPE;
2390 * Byteswap an mmap'ed file if needed
2393 byteswap(struct magic *magic, uint32_t nmagic)
2396 for (i = 0; i < nmagic; i++)
2407 uint8_t *s = (uint8_t *)(void *)&sv;
2408 uint8_t *d = (uint8_t *)(void *)&rv;
2421 uint8_t *s = (uint8_t *)(void *)&sv;
2422 uint8_t *d = (uint8_t *)(void *)&rv;
2437 uint8_t *s = (uint8_t *)(void *)&sv;
2438 uint8_t *d = (uint8_t *)(void *)&rv;
2462 * byteswap a single magic entry
2465 bs1(struct magic *m)
2467 m->cont_level = swap2(m->cont_level);
2468 m->offset = swap4((uint32_t)m->offset);
2469 m->in_offset = swap4((uint32_t)m->in_offset);
2470 m->lineno = swap4((uint32_t)m->lineno);
2471 if (IS_STRING(m->type)) {
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);