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 * softmagic - interpret variable magic from MAGIC
35 FILE_RCSID("@(#)$File: softmagic.c,v 1.218 2015/09/11 17:24:09 christos Exp $")
45 private int match(struct magic_set *, struct magic *, uint32_t,
46 const unsigned char *, size_t, size_t, int, int, int, uint16_t,
47 uint16_t *, int *, int *, int *);
48 private int mget(struct magic_set *, const unsigned char *,
49 struct magic *, size_t, size_t, unsigned int, int, int, int, uint16_t,
50 uint16_t *, int *, int *, int *);
51 private int magiccheck(struct magic_set *, struct magic *);
52 private int32_t mprint(struct magic_set *, struct magic *);
53 private int32_t moffset(struct magic_set *, struct magic *);
54 private void mdebug(uint32_t, const char *, size_t);
55 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
56 const unsigned char *, uint32_t, size_t, struct magic *);
57 private int mconvert(struct magic_set *, struct magic *, int);
58 private int print_sep(struct magic_set *, int);
59 private int handle_annotation(struct magic_set *, struct magic *);
60 private void cvt_8(union VALUETYPE *, const struct magic *);
61 private void cvt_16(union VALUETYPE *, const struct magic *);
62 private void cvt_32(union VALUETYPE *, const struct magic *);
63 private void cvt_64(union VALUETYPE *, const struct magic *);
65 #define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
66 #define BE64(p) (((uint64_t)(p)->hq[0]<<56)|((uint64_t)(p)->hq[1]<<48)| \
67 ((uint64_t)(p)->hq[2]<<40)|((uint64_t)(p)->hq[3]<<32)| \
68 ((uint64_t)(p)->hq[4]<<24)|((uint64_t)(p)->hq[5]<<16)| \
69 ((uint64_t)(p)->hq[6]<<8)|((uint64_t)(p)->hq[7]))
70 #define LE64(p) (((uint64_t)(p)->hq[7]<<56)|((uint64_t)(p)->hq[6]<<48)| \
71 ((uint64_t)(p)->hq[5]<<40)|((uint64_t)(p)->hq[4]<<32)| \
72 ((uint64_t)(p)->hq[3]<<24)|((uint64_t)(p)->hq[2]<<16)| \
73 ((uint64_t)(p)->hq[1]<<8)|((uint64_t)(p)->hq[0]))
74 #define LE32(p) (((uint32_t)(p)->hl[3]<<24)|((uint32_t)(p)->hl[2]<<16)| \
75 ((uint32_t)(p)->hl[1]<<8)|((uint32_t)(p)->hl[0]))
76 #define BE32(p) (((uint32_t)(p)->hl[0]<<24)|((uint32_t)(p)->hl[1]<<16)| \
77 ((uint32_t)(p)->hl[2]<<8)|((uint32_t)(p)->hl[3]))
78 #define ME32(p) (((uint32_t)(p)->hl[1]<<24)|((uint32_t)(p)->hl[0]<<16)| \
79 ((uint32_t)(p)->hl[3]<<8)|((uint32_t)(p)->hl[2]))
80 #define BE16(p) (((uint16_t)(p)->hs[0]<<8)|((uint16_t)(p)->hs[1]))
81 #define LE16(p) (((uint16_t)(p)->hs[1]<<8)|((uint16_t)(p)->hs[0]))
84 * softmagic - lookup one file in parsed, in-memory copy of database
85 * Passed the name and FILE * of one file to be typed.
87 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
89 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
90 uint16_t indir_level, uint16_t *name_count, int mode, int text)
93 int rv, printed_something = 0, need_separator = 0;
96 if (name_count == NULL) {
101 for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
102 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode,
103 text, 0, indir_level, name_count,
104 &printed_something, &need_separator, NULL)) != 0)
110 #define FILE_FMTDEBUG
112 #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
114 private const char * __attribute__((__format_arg__(3)))
115 file_fmtcheck(struct magic_set *ms, const struct magic *m, const char *def,
116 const char *file, size_t line)
118 const char *ptr = fmtcheck(m->desc, def);
121 "%s, %" SIZE_T_FORMAT "u: format `%s' does not match"
122 " with `%s'", file, line, m->desc, def);
126 #define F(a, b, c) fmtcheck((b)->desc, (c))
130 * Go through the whole list, stopping if you find a match. Process all
131 * the continuations of that match before returning.
133 * We support multi-level continuations:
135 * At any time when processing a successful top-level match, there is a
136 * current continuation level; it represents the level of the last
137 * successfully matched continuation.
139 * Continuations above that level are skipped as, if we see one, it
140 * means that the continuation that controls them - i.e, the
141 * lower-level continuation preceding them - failed to match.
143 * Continuations below that level are processed as, if we see one,
144 * it means we've finished processing or skipping higher-level
145 * continuations under the control of a successful or unsuccessful
146 * lower-level continuation, and are now seeing the next lower-level
147 * continuation and should process it. The current continuation
148 * level reverts to the level of the one we're seeing.
150 * Continuations at the current level are processed as, if we see
151 * one, there's no lower-level continuation that may have failed.
153 * If a continuation matches, we bump the current continuation level
154 * so that higher-level continuations are processed.
157 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
158 const unsigned char *s, size_t nbytes, size_t offset, int mode, int text,
159 int flip, uint16_t indir_level, uint16_t *name_count,
160 int *printed_something, int *need_separator, int *returnval)
162 uint32_t magindex = 0;
163 unsigned int cont_level = 0;
164 int returnvalv = 0, e; /* if a match is found it is set to 1*/
165 int firstline = 1; /* a flag to print X\n X\n- X */
166 int print = (ms->flags & MAGIC_NODESC) == 0;
168 if (returnval == NULL)
169 returnval = &returnvalv;
171 if (file_check_mem(ms, cont_level) == -1)
174 for (magindex = 0; magindex < nmagic; magindex++) {
176 struct magic *m = &magic[magindex];
178 if (m->type != FILE_NAME)
179 if ((IS_STRING(m->type) &&
180 #define FLT (STRING_BINTEST | STRING_TEXTTEST)
181 ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
182 (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
183 (m->flag & mode) != mode) {
185 while (magindex + 1 < nmagic &&
186 magic[magindex + 1].cont_level != 0 &&
189 continue; /* Skip to next top-level test*/
192 ms->offset = m->offset;
193 ms->line = m->lineno;
195 /* if main entry matches, print it... */
196 switch (mget(ms, s, m, nbytes, offset, cont_level, mode, text,
197 flip, indir_level, name_count,
198 printed_something, need_separator, returnval)) {
202 flush = m->reln != '!';
205 if (m->type == FILE_INDIRECT)
208 switch (magiccheck(ms, m)) {
222 * main entry didn't match,
223 * flush its continuations
225 while (magindex < nmagic - 1 &&
226 magic[magindex + 1].cont_level != 0)
231 if ((e = handle_annotation(ms, m)) != 0) {
233 *printed_something = 1;
238 * If we are going to print something, we'll need to print
239 * a blank before we print something else.
243 *printed_something = 1;
244 if (print_sep(ms, firstline) == -1)
249 if (print && mprint(ms, m) == -1)
252 ms->c.li[cont_level].off = moffset(ms, m);
254 /* and any continuations that match */
255 if (file_check_mem(ms, ++cont_level) == -1)
258 while (magindex + 1 < nmagic &&
259 magic[magindex + 1].cont_level != 0) {
260 m = &magic[++magindex];
261 ms->line = m->lineno; /* for messages */
263 if (cont_level < m->cont_level)
265 if (cont_level > m->cont_level) {
267 * We're at the end of the level
268 * "cont_level" continuations.
270 cont_level = m->cont_level;
272 ms->offset = m->offset;
273 if (m->flag & OFFADD) {
275 ms->c.li[cont_level - 1].off;
278 #ifdef ENABLE_CONDITIONALS
279 if (m->cond == COND_ELSE ||
280 m->cond == COND_ELIF) {
281 if (ms->c.li[cont_level].last_match == 1)
285 switch (mget(ms, s, m, nbytes, offset, cont_level, mode,
286 text, flip, indir_level, name_count,
287 printed_something, need_separator, returnval)) {
296 if (m->type == FILE_INDIRECT)
302 switch (flush ? 1 : magiccheck(ms, m)) {
306 #ifdef ENABLE_CONDITIONALS
307 ms->c.li[cont_level].last_match = 0;
311 #ifdef ENABLE_CONDITIONALS
312 ms->c.li[cont_level].last_match = 1;
314 if (m->type == FILE_CLEAR)
315 ms->c.li[cont_level].got_match = 0;
316 else if (ms->c.li[cont_level].got_match) {
317 if (m->type == FILE_DEFAULT)
320 ms->c.li[cont_level].got_match = 1;
321 if ((e = handle_annotation(ms, m)) != 0) {
323 *printed_something = 1;
328 * If we are going to print something,
329 * make sure that we have a separator first.
332 if (!*printed_something) {
333 *printed_something = 1;
334 if (print_sep(ms, firstline)
340 * This continuation matched. Print
341 * its message, with a blank before it
342 * if the previous item printed and
343 * this item isn't empty.
345 /* space if previous printed */
347 && ((m->flag & NOSPACE) == 0)
350 file_printf(ms, " ") == -1)
354 if (print && mprint(ms, m) == -1)
357 ms->c.li[cont_level].off = moffset(ms, m);
363 * If we see any continuations
367 if (file_check_mem(ms, ++cont_level) == -1)
372 if (*printed_something) {
377 if ((ms->flags & MAGIC_CONTINUE) == 0 && *printed_something) {
378 return *returnval; /* don't keep searching */
382 return *returnval; /* This is hit if -k is set or there is no match */
386 check_fmt(struct magic_set *ms, struct magic *m)
391 if (strchr(m->desc, '%') == NULL)
394 rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
396 file_regerror(&rx, rc, ms);
398 rc = file_regexec(&rx, m->desc, 0, 0, 0);
406 char * strndup(const char *, size_t);
409 strndup(const char *str, size_t n)
414 for (len = 0; len < n && str[len]; len++)
416 if ((copy = malloc(len + 1)) == NULL)
418 (void)memcpy(copy, str, len);
422 #endif /* HAVE_STRNDUP */
425 mprint(struct magic_set *ms, struct magic *m)
431 char buf[128], tbuf[26], sbuf[512];
432 union VALUETYPE *p = &ms->ms_value;
436 v = file_signextend(ms, m, (uint64_t)p->b);
437 switch (check_fmt(ms, m)) {
441 (void)snprintf(buf, sizeof(buf), "%d",
443 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
447 if (file_printf(ms, F(ms, m, "%d"),
448 (unsigned char) v) == -1)
452 t = ms->offset + sizeof(char);
458 v = file_signextend(ms, m, (uint64_t)p->h);
459 switch (check_fmt(ms, m)) {
463 (void)snprintf(buf, sizeof(buf), "%u",
465 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
469 if (file_printf(ms, F(ms, m, "%u"),
470 (unsigned short) v) == -1)
474 t = ms->offset + sizeof(short);
481 v = file_signextend(ms, m, (uint64_t)p->l);
482 switch (check_fmt(ms, m)) {
486 (void)snprintf(buf, sizeof(buf), "%u", (uint32_t) v);
487 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
491 if (file_printf(ms, F(ms, m, "%u"), (uint32_t) v) == -1)
495 t = ms->offset + sizeof(int32_t);
501 v = file_signextend(ms, m, p->q);
502 switch (check_fmt(ms, m)) {
506 (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
507 (unsigned long long)v);
508 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
512 if (file_printf(ms, F(ms, m, "%" INT64_T_FORMAT "u"),
513 (unsigned long long) v) == -1)
517 t = ms->offset + sizeof(int64_t);
522 case FILE_BESTRING16:
523 case FILE_LESTRING16:
524 if (m->reln == '=' || m->reln == '!') {
525 if (file_printf(ms, F(ms, m, "%s"),
526 file_printable(sbuf, sizeof(sbuf), m->value.s))
529 t = ms->offset + m->vallen;
534 /* compute t before we mangle the string? */
535 t = ms->offset + strlen(str);
537 if (*m->value.s == '\0')
538 str[strcspn(str, "\r\n")] = '\0';
540 if (m->str_flags & STRING_TRIM) {
542 while (isspace((unsigned char)*str))
548 while (isspace((unsigned char)*last))
553 if (file_printf(ms, F(ms, m, "%s"),
554 file_printable(sbuf, sizeof(sbuf), str)) == -1)
557 if (m->type == FILE_PSTRING)
558 t += file_pstring_length_size(m);
566 if (file_printf(ms, F(ms, m, "%s"),
567 file_fmttime(p->l, 0, tbuf)) == -1)
569 t = ms->offset + sizeof(uint32_t);
576 if (file_printf(ms, F(ms, m, "%s"),
577 file_fmttime(p->l, FILE_T_LOCAL, tbuf)) == -1)
579 t = ms->offset + sizeof(uint32_t);
585 if (file_printf(ms, F(ms, m, "%s"),
586 file_fmttime(p->q, 0, tbuf)) == -1)
588 t = ms->offset + sizeof(uint64_t);
594 if (file_printf(ms, F(ms, m, "%s"),
595 file_fmttime(p->q, FILE_T_LOCAL, tbuf)) == -1)
597 t = ms->offset + sizeof(uint64_t);
603 if (file_printf(ms, F(ms, m, "%s"),
604 file_fmttime(p->q, FILE_T_WINDOWS, tbuf)) == -1)
606 t = ms->offset + sizeof(uint64_t);
613 switch (check_fmt(ms, m)) {
617 (void)snprintf(buf, sizeof(buf), "%g", vf);
618 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
622 if (file_printf(ms, F(ms, m, "%g"), vf) == -1)
626 t = ms->offset + sizeof(float);
633 switch (check_fmt(ms, m)) {
637 (void)snprintf(buf, sizeof(buf), "%g", vd);
638 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
642 if (file_printf(ms, F(ms, m, "%g"), vd) == -1)
646 t = ms->offset + sizeof(double);
654 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
656 file_oomem(ms, ms->search.rm_len);
659 rval = file_printf(ms, F(ms, m, "%s"),
660 file_printable(sbuf, sizeof(sbuf), cp));
666 if ((m->str_flags & REGEX_OFFSET_START))
667 t = ms->search.offset;
669 t = ms->search.offset + ms->search.rm_len;
675 if (file_printf(ms, "%s", m->desc) == -1)
687 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
694 moffset(struct magic_set *ms, struct magic *m)
698 return CAST(int32_t, (ms->offset + sizeof(char)));
703 return CAST(int32_t, (ms->offset + sizeof(short)));
709 return CAST(int32_t, (ms->offset + sizeof(int32_t)));
714 return CAST(int32_t, (ms->offset + sizeof(int64_t)));
718 case FILE_BESTRING16:
719 case FILE_LESTRING16:
720 if (m->reln == '=' || m->reln == '!')
721 return ms->offset + m->vallen;
723 union VALUETYPE *p = &ms->ms_value;
726 if (*m->value.s == '\0')
727 p->s[strcspn(p->s, "\r\n")] = '\0';
728 t = CAST(uint32_t, (ms->offset + strlen(p->s)));
729 if (m->type == FILE_PSTRING)
730 t += (uint32_t)file_pstring_length_size(m);
738 return CAST(int32_t, (ms->offset + sizeof(uint32_t)));
744 return CAST(int32_t, (ms->offset + sizeof(uint32_t)));
749 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
754 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
759 return CAST(int32_t, (ms->offset + sizeof(float)));
764 return CAST(int32_t, (ms->offset + sizeof(double)));
767 if ((m->str_flags & REGEX_OFFSET_START) != 0)
768 return CAST(int32_t, ms->search.offset);
770 return CAST(int32_t, (ms->search.offset +
774 if ((m->str_flags & REGEX_OFFSET_START) != 0)
775 return CAST(int32_t, ms->search.offset);
777 return CAST(int32_t, (ms->search.offset + m->vallen));
790 cvt_flip(int type, int flip)
808 return FILE_LEQLDATE;
810 return FILE_LEQWDATE;
824 return FILE_BEQLDATE;
826 return FILE_BEQWDATE;
832 return FILE_LEDOUBLE;
834 return FILE_BEDOUBLE;
839 #define DO_CVT(fld, cast) \
841 switch (m->mask_op & FILE_OPS_MASK) { \
843 p->fld &= cast m->num_mask; \
846 p->fld |= cast m->num_mask; \
849 p->fld ^= cast m->num_mask; \
852 p->fld += cast m->num_mask; \
855 p->fld -= cast m->num_mask; \
857 case FILE_OPMULTIPLY: \
858 p->fld *= cast m->num_mask; \
860 case FILE_OPDIVIDE: \
861 p->fld /= cast m->num_mask; \
863 case FILE_OPMODULO: \
864 p->fld %= cast m->num_mask; \
867 if (m->mask_op & FILE_OPINVERSE) \
871 cvt_8(union VALUETYPE *p, const struct magic *m)
873 DO_CVT(b, (uint8_t));
877 cvt_16(union VALUETYPE *p, const struct magic *m)
879 DO_CVT(h, (uint16_t));
883 cvt_32(union VALUETYPE *p, const struct magic *m)
885 DO_CVT(l, (uint32_t));
889 cvt_64(union VALUETYPE *p, const struct magic *m)
891 DO_CVT(q, (uint64_t));
894 #define DO_CVT2(fld, cast) \
896 switch (m->mask_op & FILE_OPS_MASK) { \
898 p->fld += cast m->num_mask; \
901 p->fld -= cast m->num_mask; \
903 case FILE_OPMULTIPLY: \
904 p->fld *= cast m->num_mask; \
906 case FILE_OPDIVIDE: \
907 p->fld /= cast m->num_mask; \
912 cvt_float(union VALUETYPE *p, const struct magic *m)
918 cvt_double(union VALUETYPE *p, const struct magic *m)
920 DO_CVT2(d, (double));
924 * Convert the byte order of the data we are looking at
925 * While we're here, let's apply the mask operation
926 * (unless you have a better idea)
929 mconvert(struct magic_set *ms, struct magic *m, int flip)
931 union VALUETYPE *p = &ms->ms_value;
934 switch (type = cvt_flip(m->type, flip)) {
953 case FILE_BESTRING16:
954 case FILE_LESTRING16: {
955 /* Null terminate and eat *trailing* return */
956 p->s[sizeof(p->s) - 1] = '\0';
960 size_t sz = file_pstring_length_size(m);
961 char *ptr1 = p->s, *ptr2 = ptr1 + sz;
962 size_t len = file_pstring_get_length(m, ptr1);
963 sz = sizeof(p->s) - sz; /* maximum length of string */
966 * The size of the pascal string length (sz)
967 * is 1, 2, or 4. We need at least 1 byte for NUL
968 * termination, but we've already truncated the
969 * string by p->s, so we need to deduct sz.
970 * Because we can use one of the bytes of the length
971 * after we shifted as NUL termination.
981 p->h = (short)BE16(p);
987 p->l = (int32_t)BE32(p);
994 p->q = (uint64_t)BE64(p);
998 p->h = (short)LE16(p);
1004 p->l = (int32_t)LE32(p);
1011 p->q = (uint64_t)LE64(p);
1017 p->l = (int32_t)ME32(p);
1050 file_magerror(ms, "invalid type %d in mconvert()", m->type);
1057 mdebug(uint32_t offset, const char *str, size_t len)
1059 (void) fprintf(stderr, "mget/%" SIZE_T_FORMAT "u @%d: ", len, offset);
1060 file_showstr(stderr, str, len);
1061 (void) fputc('\n', stderr);
1062 (void) fputc('\n', stderr);
1066 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
1067 const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
1070 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1071 * anything, but setup pointers into the source
1076 ms->search.s = RCAST(const char *, s) + offset;
1077 ms->search.s_len = nbytes - offset;
1078 ms->search.offset = offset;
1084 const char *last; /* end of search region */
1085 const char *buf; /* start of search region */
1087 size_t lines, linecnt, bytecnt;
1090 ms->search.s_len = 0;
1091 ms->search.s = NULL;
1095 if (m->str_flags & REGEX_LINE_COUNT) {
1096 linecnt = m->str_range;
1097 bytecnt = linecnt * 80;
1100 bytecnt = m->str_range;
1103 if (bytecnt == 0 || bytecnt > nbytes - offset)
1104 bytecnt = nbytes - offset;
1105 if (bytecnt > ms->regex_max)
1106 bytecnt = ms->regex_max;
1108 buf = RCAST(const char *, s) + offset;
1109 end = last = RCAST(const char *, s) + bytecnt + offset;
1110 /* mget() guarantees buf <= last */
1111 for (lines = linecnt, b = buf; lines && b < end &&
1112 ((b = CAST(const char *,
1113 memchr(c = b, '\n', CAST(size_t, (end - b)))))
1114 || (b = CAST(const char *,
1115 memchr(c, '\r', CAST(size_t, (end - c))))));
1118 if (b[0] == '\r' && b[1] == '\n')
1122 last = RCAST(const char *, s) + bytecnt;
1125 ms->search.s_len = last - buf;
1126 ms->search.offset = offset;
1127 ms->search.rm_len = 0;
1130 case FILE_BESTRING16:
1131 case FILE_LESTRING16: {
1132 const unsigned char *src = s + offset;
1133 const unsigned char *esrc = s + nbytes;
1135 char *edst = &p->s[sizeof(p->s) - 1];
1137 if (type == FILE_BESTRING16)
1140 /* check that offset is within range */
1141 if (offset >= nbytes)
1143 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
1149 if (type == FILE_BESTRING16 ?
1150 *(src - 1) != '\0' :
1158 case FILE_STRING: /* XXX - these two should not need */
1159 case FILE_PSTRING: /* to copy anything, but do anyway. */
1165 if (offset >= nbytes) {
1166 (void)memset(p, '\0', sizeof(*p));
1169 if (nbytes - offset < sizeof(*p))
1170 nbytes = nbytes - offset;
1172 nbytes = sizeof(*p);
1174 (void)memcpy(p, s + offset, nbytes);
1177 * the usefulness of padding with zeroes eludes me, it
1178 * might even cause problems
1180 if (nbytes < sizeof(*p))
1181 (void)memset(((char *)(void *)p) + nbytes, '\0',
1182 sizeof(*p) - nbytes);
1187 mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
1188 size_t nbytes, size_t o, unsigned int cont_level, int mode, int text,
1189 int flip, uint16_t indir_level, uint16_t *name_count,
1190 int *printed_something, int *need_separator, int *returnval)
1192 uint32_t offset = ms->offset;
1195 int rv, oneed_separator, in_type;
1197 union VALUETYPE *p = &ms->ms_value;
1200 if (indir_level >= ms->indir_max) {
1201 file_error(ms, 0, "indirect recursion nesting (%hu) exceeded",
1206 if (*name_count >= ms->name_max) {
1207 file_error(ms, 0, "name use count (%hu) exceeded",
1212 if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
1213 (uint32_t)nbytes, m) == -1)
1216 if ((ms->flags & MAGIC_DEBUG) != 0) {
1217 fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%"
1218 SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
1219 "u, il=%hu, nc=%hu)\n",
1220 m->type, m->flag, offset, o, nbytes,
1221 indir_level, *name_count);
1222 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1223 #ifndef COMPILE_ONLY
1228 if (m->flag & INDIR) {
1229 int off = m->in_offset;
1230 if (m->in_op & FILE_OPINDIRECT) {
1231 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1232 ((const void *)(s + offset + off)));
1233 switch (cvt_flip(m->in_type, flip)) {
1241 off = (short)BE16(q);
1244 off = (short)LE16(q);
1251 off = (int32_t)BE32(q);
1255 off = (int32_t)LE32(q);
1258 off = (int32_t)ME32(q);
1261 if ((ms->flags & MAGIC_DEBUG) != 0)
1262 fprintf(stderr, "indirect offs=%u\n", off);
1264 switch (in_type = cvt_flip(m->in_type, flip)) {
1266 if (OFFSET_OOB(nbytes, offset, 1))
1269 switch (m->in_op & FILE_OPS_MASK) {
1271 offset = p->b & off;
1274 offset = p->b | off;
1277 offset = p->b ^ off;
1280 offset = p->b + off;
1283 offset = p->b - off;
1285 case FILE_OPMULTIPLY:
1286 offset = p->b * off;
1289 offset = p->b / off;
1292 offset = p->b % off;
1297 if (m->in_op & FILE_OPINVERSE)
1301 if (OFFSET_OOB(nbytes, offset, 2))
1303 lhs = (p->hs[0] << 8) | p->hs[1];
1305 switch (m->in_op & FILE_OPS_MASK) {
1321 case FILE_OPMULTIPLY:
1333 if (m->in_op & FILE_OPINVERSE)
1337 if (OFFSET_OOB(nbytes, offset, 2))
1339 lhs = (p->hs[1] << 8) | p->hs[0];
1341 switch (m->in_op & FILE_OPS_MASK) {
1357 case FILE_OPMULTIPLY:
1369 if (m->in_op & FILE_OPINVERSE)
1373 if (OFFSET_OOB(nbytes, offset, 2))
1376 switch (m->in_op & FILE_OPS_MASK) {
1378 offset = p->h & off;
1381 offset = p->h | off;
1384 offset = p->h ^ off;
1387 offset = p->h + off;
1390 offset = p->h - off;
1392 case FILE_OPMULTIPLY:
1393 offset = p->h * off;
1396 offset = p->h / off;
1399 offset = p->h % off;
1405 if (m->in_op & FILE_OPINVERSE)
1410 if (OFFSET_OOB(nbytes, offset, 4))
1414 switch (m->in_op & FILE_OPS_MASK) {
1430 case FILE_OPMULTIPLY:
1442 if (m->in_op & FILE_OPINVERSE)
1447 if (OFFSET_OOB(nbytes, offset, 4))
1451 switch (m->in_op & FILE_OPS_MASK) {
1467 case FILE_OPMULTIPLY:
1479 if (m->in_op & FILE_OPINVERSE)
1483 if (OFFSET_OOB(nbytes, offset, 4))
1487 switch (m->in_op & FILE_OPS_MASK) {
1503 case FILE_OPMULTIPLY:
1515 if (m->in_op & FILE_OPINVERSE)
1519 if (OFFSET_OOB(nbytes, offset, 4))
1522 switch (m->in_op & FILE_OPS_MASK) {
1524 offset = p->l & off;
1527 offset = p->l | off;
1530 offset = p->l ^ off;
1533 offset = p->l + off;
1536 offset = p->l - off;
1538 case FILE_OPMULTIPLY:
1539 offset = p->l * off;
1542 offset = p->l / off;
1545 offset = p->l % off;
1550 if (m->in_op & FILE_OPINVERSE)
1560 offset = ((((offset >> 0) & 0x7f) << 0) |
1561 (((offset >> 8) & 0x7f) << 7) |
1562 (((offset >> 16) & 0x7f) << 14) |
1563 (((offset >> 24) & 0x7f) << 21));
1564 if ((ms->flags & MAGIC_DEBUG) != 0)
1565 fprintf(stderr, "id3 offs=%u\n", offset);
1571 if (m->flag & INDIROFFADD) {
1572 offset += ms->c.li[cont_level-1].off;
1574 if ((ms->flags & MAGIC_DEBUG) != 0)
1576 "indirect *zero* offset\n");
1579 if ((ms->flags & MAGIC_DEBUG) != 0)
1580 fprintf(stderr, "indirect +offs=%u\n", offset);
1582 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
1584 ms->offset = offset;
1586 if ((ms->flags & MAGIC_DEBUG) != 0) {
1587 mdebug(offset, (char *)(void *)p,
1588 sizeof(union VALUETYPE));
1589 #ifndef COMPILE_ONLY
1595 /* Verify we have enough data to match magic type */
1598 if (OFFSET_OOB(nbytes, offset, 1))
1605 if (OFFSET_OOB(nbytes, offset, 2))
1624 if (OFFSET_OOB(nbytes, offset, 4))
1631 if (OFFSET_OOB(nbytes, offset, 8))
1638 if (OFFSET_OOB(nbytes, offset, m->vallen))
1643 if (nbytes < offset)
1648 if (m->str_flags & INDIRECT_RELATIVE)
1649 offset += CAST(uint32_t, o);
1653 if (nbytes < offset)
1656 if ((pb = file_push_buffer(ms)) == NULL)
1659 rv = file_softmagic(ms, s + offset, nbytes - offset,
1660 indir_level + 1, name_count, BINTEST, text);
1662 if ((ms->flags & MAGIC_DEBUG) != 0)
1663 fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1665 rbuf = file_pop_buffer(ms, pb);
1666 if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
1670 if ((ms->flags & MAGIC_NODESC) == 0 &&
1671 file_printf(ms, F(ms, m, "%u"), offset) == -1) {
1675 if (file_printf(ms, "%s", rbuf) == -1) {
1684 if (nbytes < offset)
1691 if (file_magicfind(ms, rbuf, &ml) == -1) {
1692 file_error(ms, 0, "cannot find entry `%s'", rbuf);
1696 oneed_separator = *need_separator;
1697 if (m->flag & NOSPACE)
1698 *need_separator = 0;
1699 rv = match(ms, ml.magic, ml.nmagic, s, nbytes, offset + o,
1700 mode, text, flip, indir_level, name_count,
1701 printed_something, need_separator, returnval);
1703 *need_separator = oneed_separator;
1707 if (ms->flags & MAGIC_NODESC)
1709 if (file_printf(ms, "%s", m->desc) == -1)
1712 case FILE_DEFAULT: /* nothing to check */
1717 if (!mconvert(ms, m, flip))
1723 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1726 * Convert the source args to unsigned here so that (1) the
1727 * compare will be unsigned as it is in strncmp() and (2) so
1728 * the ctype functions will work correctly without extra
1731 const unsigned char *a = (const unsigned char *)s1;
1732 const unsigned char *b = (const unsigned char *)s2;
1736 * What we want here is v = strncmp(s1, s2, len),
1737 * but ignoring any nulls.
1740 if (0L == flags) { /* normal string: do it fast */
1742 if ((v = *b++ - *a++) != '\0')
1745 else { /* combine the others */
1747 if ((flags & STRING_IGNORE_LOWERCASE) &&
1749 if ((v = tolower(*b++) - *a++) != '\0')
1752 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1754 if ((v = toupper(*b++) - *a++) != '\0')
1757 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1760 if (isspace(*b++)) {
1770 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1777 if ((v = *b++ - *a++) != '\0')
1786 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1789 * XXX - The 16-bit string compare probably needs to be done
1790 * differently, especially if the flags are to be supported.
1791 * At the moment, I am unsure.
1794 return file_strncmp(a, b, len, flags);
1798 magiccheck(struct magic_set *ms, struct magic *m)
1800 uint64_t l = m->value.q;
1805 union VALUETYPE *p = &ms->ms_value;
1875 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1908 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
1922 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1925 case FILE_BESTRING16:
1926 case FILE_LESTRING16:
1928 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1931 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1935 if (ms->search.s == NULL)
1938 slen = MIN(m->vallen, sizeof(m->value.s));
1942 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
1943 if (slen + idx > ms->search.s_len)
1946 v = file_strncmp(m->value.s, ms->search.s + idx, slen,
1948 if (v == 0) { /* found match */
1949 ms->search.offset += idx;
1950 ms->search.rm_len = m->str_range - idx;
1961 if (ms->search.s == NULL)
1965 rc = file_regcomp(&rx, m->value.s,
1966 REG_EXTENDED|REG_NEWLINE|
1967 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
1969 file_regerror(&rx, rc, ms);
1972 regmatch_t pmatch[1];
1973 size_t slen = ms->search.s_len;
1974 #ifndef REG_STARTEND
1975 #define REG_STARTEND 0
1978 copy = malloc(slen);
1980 file_error(ms, errno,
1981 "can't allocate %" SIZE_T_FORMAT "u bytes",
1985 memcpy(copy, ms->search.s, slen);
1986 copy[--slen] = '\0';
1989 search = ms->search.s;
1993 search = ms->search.s;
1994 pmatch[0].rm_so = 0;
1995 pmatch[0].rm_eo = slen;
1997 rc = file_regexec(&rx, (const char *)search,
1998 1, pmatch, REG_STARTEND);
1999 #if REG_STARTEND == 0
2004 ms->search.s += (int)pmatch[0].rm_so;
2005 ms->search.offset += (size_t)pmatch[0].rm_so;
2007 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
2016 file_regerror(&rx, rc, ms);
2022 if (v == (uint64_t)-1)
2031 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2035 v = file_signextend(ms, m, v);
2039 if ((ms->flags & MAGIC_DEBUG) != 0)
2040 (void) fprintf(stderr, "%" INT64_T_FORMAT
2041 "u == *any* = 1\n", (unsigned long long)v);
2047 if ((ms->flags & MAGIC_DEBUG) != 0)
2048 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
2049 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2050 (unsigned long long)l, matched);
2055 if ((ms->flags & MAGIC_DEBUG) != 0)
2056 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2057 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2058 (unsigned long long)l, matched);
2062 if (m->flag & UNSIGNED) {
2064 if ((ms->flags & MAGIC_DEBUG) != 0)
2065 (void) fprintf(stderr, "%" INT64_T_FORMAT
2066 "u > %" INT64_T_FORMAT "u = %d\n",
2067 (unsigned long long)v,
2068 (unsigned long long)l, matched);
2071 matched = (int64_t) v > (int64_t) l;
2072 if ((ms->flags & MAGIC_DEBUG) != 0)
2073 (void) fprintf(stderr, "%" INT64_T_FORMAT
2074 "d > %" INT64_T_FORMAT "d = %d\n",
2075 (long long)v, (long long)l, matched);
2080 if (m->flag & UNSIGNED) {
2082 if ((ms->flags & MAGIC_DEBUG) != 0)
2083 (void) fprintf(stderr, "%" INT64_T_FORMAT
2084 "u < %" INT64_T_FORMAT "u = %d\n",
2085 (unsigned long long)v,
2086 (unsigned long long)l, matched);
2089 matched = (int64_t) v < (int64_t) l;
2090 if ((ms->flags & MAGIC_DEBUG) != 0)
2091 (void) fprintf(stderr, "%" INT64_T_FORMAT
2092 "d < %" INT64_T_FORMAT "d = %d\n",
2093 (long long)v, (long long)l, matched);
2098 matched = (v & l) == l;
2099 if ((ms->flags & MAGIC_DEBUG) != 0)
2100 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2101 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2102 "x) = %d\n", (unsigned long long)v,
2103 (unsigned long long)l, (unsigned long long)l,
2108 matched = (v & l) != l;
2109 if ((ms->flags & MAGIC_DEBUG) != 0)
2110 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2111 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2112 "x) = %d\n", (unsigned long long)v,
2113 (unsigned long long)l, (unsigned long long)l,
2118 file_magerror(ms, "cannot happen: invalid relation `%c'",
2127 handle_annotation(struct magic_set *ms, struct magic *m)
2129 if (ms->flags & MAGIC_APPLE) {
2130 if (file_printf(ms, "%.8s", m->apple) == -1)
2134 if (ms->flags & MAGIC_EXTENSION) {
2135 if (file_printf(ms, "%s", m->ext) == -1)
2139 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2140 if (file_printf(ms, "%s", m->mimetype) == -1)
2148 print_sep(struct magic_set *ms, int firstline)
2150 if (ms->flags & MAGIC_NODESC)
2155 * we found another match
2156 * put a newline and '-' to do some simple formatting
2158 return file_printf(ms, "\n- ");