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.229 2016/03/21 23:04:40 christos Exp $")
46 private int match(struct magic_set *, struct magic *, uint32_t,
47 const unsigned char *, size_t, size_t, int, int, int, uint16_t *,
48 uint16_t *, int *, int *, int *);
49 private int mget(struct magic_set *, const unsigned char *,
50 struct magic *, size_t, size_t, unsigned int, int, int, int, uint16_t *,
51 uint16_t *, int *, int *, int *);
52 private int magiccheck(struct magic_set *, struct magic *);
53 private int32_t mprint(struct magic_set *, struct magic *);
54 private int moffset(struct magic_set *, struct magic *, size_t, int32_t *);
55 private void mdebug(uint32_t, const char *, size_t);
56 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
57 const unsigned char *, uint32_t, size_t, struct magic *);
58 private int mconvert(struct magic_set *, struct magic *, int);
59 private int print_sep(struct magic_set *, int);
60 private int handle_annotation(struct magic_set *, struct magic *);
61 private int cvt_8(union VALUETYPE *, const struct magic *);
62 private int cvt_16(union VALUETYPE *, const struct magic *);
63 private int cvt_32(union VALUETYPE *, const struct magic *);
64 private int cvt_64(union VALUETYPE *, const struct magic *);
66 #define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
67 #define BE64(p) (((uint64_t)(p)->hq[0]<<56)|((uint64_t)(p)->hq[1]<<48)| \
68 ((uint64_t)(p)->hq[2]<<40)|((uint64_t)(p)->hq[3]<<32)| \
69 ((uint64_t)(p)->hq[4]<<24)|((uint64_t)(p)->hq[5]<<16)| \
70 ((uint64_t)(p)->hq[6]<<8)|((uint64_t)(p)->hq[7]))
71 #define LE64(p) (((uint64_t)(p)->hq[7]<<56)|((uint64_t)(p)->hq[6]<<48)| \
72 ((uint64_t)(p)->hq[5]<<40)|((uint64_t)(p)->hq[4]<<32)| \
73 ((uint64_t)(p)->hq[3]<<24)|((uint64_t)(p)->hq[2]<<16)| \
74 ((uint64_t)(p)->hq[1]<<8)|((uint64_t)(p)->hq[0]))
75 #define LE32(p) (((uint32_t)(p)->hl[3]<<24)|((uint32_t)(p)->hl[2]<<16)| \
76 ((uint32_t)(p)->hl[1]<<8)|((uint32_t)(p)->hl[0]))
77 #define BE32(p) (((uint32_t)(p)->hl[0]<<24)|((uint32_t)(p)->hl[1]<<16)| \
78 ((uint32_t)(p)->hl[2]<<8)|((uint32_t)(p)->hl[3]))
79 #define ME32(p) (((uint32_t)(p)->hl[1]<<24)|((uint32_t)(p)->hl[0]<<16)| \
80 ((uint32_t)(p)->hl[3]<<8)|((uint32_t)(p)->hl[2]))
81 #define BE16(p) (((uint16_t)(p)->hs[0]<<8)|((uint16_t)(p)->hs[1]))
82 #define LE16(p) (((uint16_t)(p)->hs[1]<<8)|((uint16_t)(p)->hs[0]))
85 * softmagic - lookup one file in parsed, in-memory copy of database
86 * Passed the name and FILE * of one file to be typed.
88 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
90 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
91 uint16_t *indir_count, uint16_t *name_count, int mode, int text)
94 int rv, printed_something = 0, need_separator = 0;
97 if (name_count == NULL) {
101 if (indir_count == NULL) {
106 for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
107 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode,
108 text, 0, indir_count, name_count,
109 &printed_something, &need_separator, NULL)) != 0)
115 #define FILE_FMTDEBUG
117 #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
119 private const char * __attribute__((__format_arg__(3)))
120 file_fmtcheck(struct magic_set *ms, const struct magic *m, const char *def,
121 const char *file, size_t line)
123 const char *ptr = fmtcheck(m->desc, def);
126 "%s, %" SIZE_T_FORMAT "u: format `%s' does not match"
127 " with `%s'", file, line, m->desc, def);
131 #define F(a, b, c) fmtcheck((b)->desc, (c))
135 * Go through the whole list, stopping if you find a match. Process all
136 * the continuations of that match before returning.
138 * We support multi-level continuations:
140 * At any time when processing a successful top-level match, there is a
141 * current continuation level; it represents the level of the last
142 * successfully matched continuation.
144 * Continuations above that level are skipped as, if we see one, it
145 * means that the continuation that controls them - i.e, the
146 * lower-level continuation preceding them - failed to match.
148 * Continuations below that level are processed as, if we see one,
149 * it means we've finished processing or skipping higher-level
150 * continuations under the control of a successful or unsuccessful
151 * lower-level continuation, and are now seeing the next lower-level
152 * continuation and should process it. The current continuation
153 * level reverts to the level of the one we're seeing.
155 * Continuations at the current level are processed as, if we see
156 * one, there's no lower-level continuation that may have failed.
158 * If a continuation matches, we bump the current continuation level
159 * so that higher-level continuations are processed.
162 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
163 const unsigned char *s, size_t nbytes, size_t offset, int mode, int text,
164 int flip, uint16_t *indir_count, uint16_t *name_count,
165 int *printed_something, int *need_separator, int *returnval)
167 uint32_t magindex = 0;
168 unsigned int cont_level = 0;
169 int returnvalv = 0, e; /* if a match is found it is set to 1*/
170 int firstline = 1; /* a flag to print X\n X\n- X */
171 int print = (ms->flags & MAGIC_NODESC) == 0;
173 if (returnval == NULL)
174 returnval = &returnvalv;
176 if (file_check_mem(ms, cont_level) == -1)
179 for (magindex = 0; magindex < nmagic; magindex++) {
181 struct magic *m = &magic[magindex];
183 if (m->type != FILE_NAME)
184 if ((IS_STRING(m->type) &&
185 #define FLT (STRING_BINTEST | STRING_TEXTTEST)
186 ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
187 (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
188 (m->flag & mode) != mode) {
190 while (magindex + 1 < nmagic &&
191 magic[magindex + 1].cont_level != 0 &&
194 continue; /* Skip to next top-level test*/
197 ms->offset = m->offset;
198 ms->line = m->lineno;
200 /* if main entry matches, print it... */
201 switch (mget(ms, s, m, nbytes, offset, cont_level, mode, text,
202 flip, indir_count, name_count,
203 printed_something, need_separator, returnval)) {
207 flush = m->reln != '!';
210 if (m->type == FILE_INDIRECT)
213 switch (magiccheck(ms, m)) {
227 * main entry didn't match,
228 * flush its continuations
230 while (magindex < nmagic - 1 &&
231 magic[magindex + 1].cont_level != 0)
236 if ((e = handle_annotation(ms, m)) != 0) {
238 *printed_something = 1;
244 * If we are going to print something, we'll need to print
245 * a blank before we print something else.
249 *printed_something = 1;
250 if (print_sep(ms, firstline) == -1)
255 if (print && mprint(ms, m) == -1)
258 if (moffset(ms, m, nbytes, &ms->c.li[cont_level].off) == -1)
261 /* and any continuations that match */
262 if (file_check_mem(ms, ++cont_level) == -1)
265 while (magindex + 1 < nmagic &&
266 magic[magindex + 1].cont_level != 0) {
267 m = &magic[++magindex];
268 ms->line = m->lineno; /* for messages */
270 if (cont_level < m->cont_level)
272 if (cont_level > m->cont_level) {
274 * We're at the end of the level
275 * "cont_level" continuations.
277 cont_level = m->cont_level;
279 ms->offset = m->offset;
280 if (m->flag & OFFADD) {
282 ms->c.li[cont_level - 1].off;
285 #ifdef ENABLE_CONDITIONALS
286 if (m->cond == COND_ELSE ||
287 m->cond == COND_ELIF) {
288 if (ms->c.li[cont_level].last_match == 1)
292 switch (mget(ms, s, m, nbytes, offset, cont_level, mode,
293 text, flip, indir_count, name_count,
294 printed_something, need_separator, returnval)) {
303 if (m->type == FILE_INDIRECT)
309 switch (flush ? 1 : magiccheck(ms, m)) {
313 #ifdef ENABLE_CONDITIONALS
314 ms->c.li[cont_level].last_match = 0;
318 #ifdef ENABLE_CONDITIONALS
319 ms->c.li[cont_level].last_match = 1;
321 if (m->type == FILE_CLEAR)
322 ms->c.li[cont_level].got_match = 0;
323 else if (ms->c.li[cont_level].got_match) {
324 if (m->type == FILE_DEFAULT)
327 ms->c.li[cont_level].got_match = 1;
329 if ((e = handle_annotation(ms, m)) != 0) {
331 *printed_something = 1;
336 * If we are going to print something,
337 * make sure that we have a separator first.
340 if (!*printed_something) {
341 *printed_something = 1;
342 if (print_sep(ms, firstline)
348 * This continuation matched. Print
349 * its message, with a blank before it
350 * if the previous item printed and
351 * this item isn't empty.
353 /* space if previous printed */
355 && ((m->flag & NOSPACE) == 0)
358 file_printf(ms, " ") == -1)
362 if (print && mprint(ms, m) == -1)
365 if (moffset(ms, m, nbytes,
366 &ms->c.li[cont_level].off) == -1)
373 * If we see any continuations
377 if (file_check_mem(ms, ++cont_level) == -1)
382 if (*printed_something) {
387 if ((ms->flags & MAGIC_CONTINUE) == 0 && *printed_something) {
388 return *returnval; /* don't keep searching */
392 return *returnval; /* This is hit if -k is set or there is no match */
396 check_fmt(struct magic_set *ms, struct magic *m)
401 if (strchr(m->desc, '%') == NULL)
404 rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
406 file_regerror(&rx, rc, ms);
408 rc = file_regexec(&rx, m->desc, 0, 0, 0);
416 char * strndup(const char *, size_t);
419 strndup(const char *str, size_t n)
424 for (len = 0; len < n && str[len]; len++)
426 if ((copy = malloc(len + 1)) == NULL)
428 (void)memcpy(copy, str, len);
432 #endif /* HAVE_STRNDUP */
435 mprint(struct magic_set *ms, struct magic *m)
441 char buf[128], tbuf[26], sbuf[512];
442 union VALUETYPE *p = &ms->ms_value;
446 v = file_signextend(ms, m, (uint64_t)p->b);
447 switch (check_fmt(ms, m)) {
451 (void)snprintf(buf, sizeof(buf), "%d",
453 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
457 if (file_printf(ms, F(ms, m, "%d"),
458 (unsigned char) v) == -1)
462 t = ms->offset + sizeof(char);
468 v = file_signextend(ms, m, (uint64_t)p->h);
469 switch (check_fmt(ms, m)) {
473 (void)snprintf(buf, sizeof(buf), "%u",
475 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
479 if (file_printf(ms, F(ms, m, "%u"),
480 (unsigned short) v) == -1)
484 t = ms->offset + sizeof(short);
491 v = file_signextend(ms, m, (uint64_t)p->l);
492 switch (check_fmt(ms, m)) {
496 (void)snprintf(buf, sizeof(buf), "%u", (uint32_t) v);
497 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
501 if (file_printf(ms, F(ms, m, "%u"), (uint32_t) v) == -1)
505 t = ms->offset + sizeof(int32_t);
511 v = file_signextend(ms, m, p->q);
512 switch (check_fmt(ms, m)) {
516 (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
517 (unsigned long long)v);
518 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
522 if (file_printf(ms, F(ms, m, "%" INT64_T_FORMAT "u"),
523 (unsigned long long) v) == -1)
527 t = ms->offset + sizeof(int64_t);
532 case FILE_BESTRING16:
533 case FILE_LESTRING16:
534 if (m->reln == '=' || m->reln == '!') {
535 if (file_printf(ms, F(ms, m, "%s"),
536 file_printable(sbuf, sizeof(sbuf), m->value.s))
539 t = ms->offset + m->vallen;
544 /* compute t before we mangle the string? */
545 t = ms->offset + strlen(str);
547 if (*m->value.s == '\0')
548 str[strcspn(str, "\r\n")] = '\0';
550 if (m->str_flags & STRING_TRIM) {
552 while (isspace((unsigned char)*str))
558 while (isspace((unsigned char)*last))
563 if (file_printf(ms, F(ms, m, "%s"),
564 file_printable(sbuf, sizeof(sbuf), str)) == -1)
567 if (m->type == FILE_PSTRING)
568 t += file_pstring_length_size(m);
576 if (file_printf(ms, F(ms, m, "%s"),
577 file_fmttime(p->l, 0, tbuf)) == -1)
579 t = ms->offset + sizeof(uint32_t);
586 if (file_printf(ms, F(ms, m, "%s"),
587 file_fmttime(p->l, FILE_T_LOCAL, tbuf)) == -1)
589 t = ms->offset + sizeof(uint32_t);
595 if (file_printf(ms, F(ms, m, "%s"),
596 file_fmttime(p->q, 0, tbuf)) == -1)
598 t = ms->offset + sizeof(uint64_t);
604 if (file_printf(ms, F(ms, m, "%s"),
605 file_fmttime(p->q, FILE_T_LOCAL, tbuf)) == -1)
607 t = ms->offset + sizeof(uint64_t);
613 if (file_printf(ms, F(ms, m, "%s"),
614 file_fmttime(p->q, FILE_T_WINDOWS, tbuf)) == -1)
616 t = ms->offset + sizeof(uint64_t);
623 switch (check_fmt(ms, m)) {
627 (void)snprintf(buf, sizeof(buf), "%g", vf);
628 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
632 if (file_printf(ms, F(ms, m, "%g"), vf) == -1)
636 t = ms->offset + sizeof(float);
643 switch (check_fmt(ms, m)) {
647 (void)snprintf(buf, sizeof(buf), "%g", vd);
648 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
652 if (file_printf(ms, F(ms, m, "%g"), vd) == -1)
656 t = ms->offset + sizeof(double);
664 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
666 file_oomem(ms, ms->search.rm_len);
669 rval = file_printf(ms, F(ms, m, "%s"),
670 file_printable(sbuf, sizeof(sbuf), cp));
676 if ((m->str_flags & REGEX_OFFSET_START))
677 t = ms->search.offset;
679 t = ms->search.offset + ms->search.rm_len;
685 if (file_printf(ms, "%s", m->desc) == -1)
696 if (file_printf(ms, F(ms, m, "%s"),
697 file_printable(sbuf, sizeof(sbuf), ms->ms_value.s)) == -1)
702 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
709 moffset(struct magic_set *ms, struct magic *m, size_t nbytes, int32_t *op)
715 o = CAST(int32_t, (ms->offset + sizeof(char)));
721 o = CAST(int32_t, (ms->offset + sizeof(short)));
728 o = CAST(int32_t, (ms->offset + sizeof(int32_t)));
734 o = CAST(int32_t, (ms->offset + sizeof(int64_t)));
739 case FILE_BESTRING16:
740 case FILE_LESTRING16:
741 if (m->reln == '=' || m->reln == '!') {
742 o = ms->offset + m->vallen;
744 union VALUETYPE *p = &ms->ms_value;
746 if (*m->value.s == '\0')
747 p->s[strcspn(p->s, "\r\n")] = '\0';
748 o = CAST(uint32_t, (ms->offset + strlen(p->s)));
749 if (m->type == FILE_PSTRING)
750 o += (uint32_t)file_pstring_length_size(m);
758 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
765 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
771 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
777 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
783 o = CAST(int32_t, (ms->offset + sizeof(float)));
789 o = CAST(int32_t, (ms->offset + sizeof(double)));
793 if ((m->str_flags & REGEX_OFFSET_START) != 0)
794 o = CAST(int32_t, ms->search.offset);
797 (ms->search.offset + ms->search.rm_len));
801 if ((m->str_flags & REGEX_OFFSET_START) != 0)
802 o = CAST(int32_t, ms->search.offset);
804 o = CAST(int32_t, (ms->search.offset + m->vallen));
815 o = der_offs(ms, m, nbytes);
817 file_error(ms, 0, "EOF computing DER offset");
828 if ((size_t)o >= nbytes) {
829 file_error(ms, 0, "Offset out of range");
837 cvt_id3(struct magic_set *ms, uint32_t v)
839 v = ((((v >> 0) & 0x7f) << 0) |
840 (((v >> 8) & 0x7f) << 7) |
841 (((v >> 16) & 0x7f) << 14) |
842 (((v >> 24) & 0x7f) << 21));
843 if ((ms->flags & MAGIC_DEBUG) != 0)
844 fprintf(stderr, "id3 offs=%u\n", v);
849 cvt_flip(int type, int flip)
867 return FILE_LEQLDATE;
869 return FILE_LEQWDATE;
883 return FILE_BEQLDATE;
885 return FILE_BEQWDATE;
891 return FILE_LEDOUBLE;
893 return FILE_BEDOUBLE;
898 #define DO_CVT(fld, cast) \
900 switch (m->mask_op & FILE_OPS_MASK) { \
902 p->fld &= cast m->num_mask; \
905 p->fld |= cast m->num_mask; \
908 p->fld ^= cast m->num_mask; \
911 p->fld += cast m->num_mask; \
914 p->fld -= cast m->num_mask; \
916 case FILE_OPMULTIPLY: \
917 p->fld *= cast m->num_mask; \
919 case FILE_OPDIVIDE: \
920 if (cast m->num_mask == 0) \
922 p->fld /= cast m->num_mask; \
924 case FILE_OPMODULO: \
925 if (cast m->num_mask == 0) \
927 p->fld %= cast m->num_mask; \
930 if (m->mask_op & FILE_OPINVERSE) \
934 cvt_8(union VALUETYPE *p, const struct magic *m)
936 DO_CVT(b, (uint8_t));
941 cvt_16(union VALUETYPE *p, const struct magic *m)
943 DO_CVT(h, (uint16_t));
948 cvt_32(union VALUETYPE *p, const struct magic *m)
950 DO_CVT(l, (uint32_t));
955 cvt_64(union VALUETYPE *p, const struct magic *m)
957 DO_CVT(q, (uint64_t));
961 #define DO_CVT2(fld, cast) \
963 switch (m->mask_op & FILE_OPS_MASK) { \
965 p->fld += cast m->num_mask; \
968 p->fld -= cast m->num_mask; \
970 case FILE_OPMULTIPLY: \
971 p->fld *= cast m->num_mask; \
973 case FILE_OPDIVIDE: \
974 if (cast m->num_mask == 0) \
976 p->fld /= cast m->num_mask; \
981 cvt_float(union VALUETYPE *p, const struct magic *m)
988 cvt_double(union VALUETYPE *p, const struct magic *m)
990 DO_CVT2(d, (double));
995 * Convert the byte order of the data we are looking at
996 * While we're here, let's apply the mask operation
997 * (unless you have a better idea)
1000 mconvert(struct magic_set *ms, struct magic *m, int flip)
1002 union VALUETYPE *p = &ms->ms_value;
1005 switch (type = cvt_flip(m->type, flip)) {
1007 if (cvt_8(p, m) == -1)
1011 if (cvt_16(p, m) == -1)
1017 if (cvt_32(p, m) == -1)
1024 if (cvt_64(p, m) == -1)
1028 case FILE_BESTRING16:
1029 case FILE_LESTRING16: {
1030 /* Null terminate and eat *trailing* return */
1031 p->s[sizeof(p->s) - 1] = '\0';
1034 case FILE_PSTRING: {
1035 size_t sz = file_pstring_length_size(m);
1036 char *ptr1 = p->s, *ptr2 = ptr1 + sz;
1037 size_t len = file_pstring_get_length(m, ptr1);
1038 sz = sizeof(p->s) - sz; /* maximum length of string */
1041 * The size of the pascal string length (sz)
1042 * is 1, 2, or 4. We need at least 1 byte for NUL
1043 * termination, but we've already truncated the
1044 * string by p->s, so we need to deduct sz.
1045 * Because we can use one of the bytes of the length
1046 * after we shifted as NUL termination.
1056 p->h = (short)BE16(p);
1057 if (cvt_16(p, m) == -1)
1063 p->l = (int32_t)BE32(p);
1064 if (cvt_32(p, m) == -1)
1071 p->q = (uint64_t)BE64(p);
1072 if (cvt_64(p, m) == -1)
1076 p->h = (short)LE16(p);
1077 if (cvt_16(p, m) == -1)
1083 p->l = (int32_t)LE32(p);
1084 if (cvt_32(p, m) == -1)
1091 p->q = (uint64_t)LE64(p);
1092 if (cvt_64(p, m) == -1)
1098 p->l = (int32_t)ME32(p);
1099 if (cvt_32(p, m) == -1)
1103 if (cvt_float(p, m) == -1)
1108 if (cvt_float(p, m) == -1)
1113 if (cvt_float(p, m) == -1)
1117 if (cvt_double(p, m) == -1)
1122 if (cvt_double(p, m) == -1)
1127 if (cvt_double(p, m) == -1)
1139 file_magerror(ms, "invalid type %d in mconvert()", m->type);
1143 file_magerror(ms, "zerodivide in mconvert()");
1149 mdebug(uint32_t offset, const char *str, size_t len)
1151 (void) fprintf(stderr, "mget/%" SIZE_T_FORMAT "u @%d: ", len, offset);
1152 file_showstr(stderr, str, len);
1153 (void) fputc('\n', stderr);
1154 (void) fputc('\n', stderr);
1158 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
1159 const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
1162 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1163 * anything, but setup pointers into the source
1169 ms->search.s = RCAST(const char *, s) + offset;
1170 ms->search.s_len = nbytes - offset;
1171 ms->search.offset = offset;
1177 const char *last; /* end of search region */
1178 const char *buf; /* start of search region */
1180 size_t lines, linecnt, bytecnt;
1183 ms->search.s_len = 0;
1184 ms->search.s = NULL;
1188 if (m->str_flags & REGEX_LINE_COUNT) {
1189 linecnt = m->str_range;
1190 bytecnt = linecnt * 80;
1193 bytecnt = m->str_range;
1196 if (bytecnt == 0 || bytecnt > nbytes - offset)
1197 bytecnt = nbytes - offset;
1198 if (bytecnt > ms->regex_max)
1199 bytecnt = ms->regex_max;
1201 buf = RCAST(const char *, s) + offset;
1202 end = last = RCAST(const char *, s) + bytecnt + offset;
1203 /* mget() guarantees buf <= last */
1204 for (lines = linecnt, b = buf; lines && b < end &&
1205 ((b = CAST(const char *,
1206 memchr(c = b, '\n', CAST(size_t, (end - b)))))
1207 || (b = CAST(const char *,
1208 memchr(c, '\r', CAST(size_t, (end - c))))));
1211 if (b[0] == '\r' && b[1] == '\n')
1215 last = RCAST(const char *, s) + bytecnt;
1218 ms->search.s_len = last - buf;
1219 ms->search.offset = offset;
1220 ms->search.rm_len = 0;
1223 case FILE_BESTRING16:
1224 case FILE_LESTRING16: {
1225 const unsigned char *src = s + offset;
1226 const unsigned char *esrc = s + nbytes;
1228 char *edst = &p->s[sizeof(p->s) - 1];
1230 if (type == FILE_BESTRING16)
1233 /* check that offset is within range */
1234 if (offset >= nbytes)
1236 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
1242 if (type == FILE_BESTRING16 ?
1243 *(src - 1) != '\0' :
1251 case FILE_STRING: /* XXX - these two should not need */
1252 case FILE_PSTRING: /* to copy anything, but do anyway. */
1258 if (offset >= nbytes) {
1259 (void)memset(p, '\0', sizeof(*p));
1262 if (nbytes - offset < sizeof(*p))
1263 nbytes = nbytes - offset;
1265 nbytes = sizeof(*p);
1267 (void)memcpy(p, s + offset, nbytes);
1270 * the usefulness of padding with zeroes eludes me, it
1271 * might even cause problems
1273 if (nbytes < sizeof(*p))
1274 (void)memset(((char *)(void *)p) + nbytes, '\0',
1275 sizeof(*p) - nbytes);
1280 mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
1281 size_t nbytes, size_t o, unsigned int cont_level, int mode, int text,
1282 int flip, uint16_t *indir_count, uint16_t *name_count,
1283 int *printed_something, int *need_separator, int *returnval)
1285 uint32_t offset = ms->offset;
1288 int rv, oneed_separator, in_type;
1290 union VALUETYPE *p = &ms->ms_value;
1293 if (*indir_count >= ms->indir_max) {
1294 file_error(ms, 0, "indirect count (%hu) exceeded",
1299 if (*name_count >= ms->name_max) {
1300 file_error(ms, 0, "name use count (%hu) exceeded",
1305 if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
1306 (uint32_t)nbytes, m) == -1)
1309 if ((ms->flags & MAGIC_DEBUG) != 0) {
1310 fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%"
1311 SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
1312 "u, il=%hu, nc=%hu)\n",
1313 m->type, m->flag, offset, o, nbytes,
1314 *indir_count, *name_count);
1315 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1316 #ifndef COMPILE_ONLY
1321 if (m->flag & INDIR) {
1322 int off = m->in_offset;
1323 if (m->in_op & FILE_OPINDIRECT) {
1324 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1325 ((const void *)(s + offset + off)));
1326 if (OFFSET_OOB(nbytes, offset + off, sizeof(*q)))
1328 switch (cvt_flip(m->in_type, flip)) {
1336 off = (short)BE16(q);
1339 off = (short)LE16(q);
1346 off = (int32_t)BE32(q);
1350 off = (int32_t)LE32(q);
1353 off = (int32_t)ME32(q);
1356 if ((ms->flags & MAGIC_DEBUG) != 0)
1357 fprintf(stderr, "indirect offs=%u\n", off);
1359 switch (in_type = cvt_flip(m->in_type, flip)) {
1361 if (OFFSET_OOB(nbytes, offset, 1))
1364 switch (m->in_op & FILE_OPS_MASK) {
1366 offset = p->b & off;
1369 offset = p->b | off;
1372 offset = p->b ^ off;
1375 offset = p->b + off;
1378 offset = p->b - off;
1380 case FILE_OPMULTIPLY:
1381 offset = p->b * off;
1384 offset = p->b / off;
1387 offset = p->b % off;
1392 if (m->in_op & FILE_OPINVERSE)
1396 if (OFFSET_OOB(nbytes, offset, 2))
1398 lhs = (p->hs[0] << 8) | p->hs[1];
1400 switch (m->in_op & FILE_OPS_MASK) {
1416 case FILE_OPMULTIPLY:
1428 if (m->in_op & FILE_OPINVERSE)
1432 if (OFFSET_OOB(nbytes, offset, 2))
1434 lhs = (p->hs[1] << 8) | p->hs[0];
1436 switch (m->in_op & FILE_OPS_MASK) {
1452 case FILE_OPMULTIPLY:
1464 if (m->in_op & FILE_OPINVERSE)
1468 if (OFFSET_OOB(nbytes, offset, 2))
1471 switch (m->in_op & FILE_OPS_MASK) {
1473 offset = p->h & off;
1476 offset = p->h | off;
1479 offset = p->h ^ off;
1482 offset = p->h + off;
1485 offset = p->h - off;
1487 case FILE_OPMULTIPLY:
1488 offset = p->h * off;
1491 offset = p->h / off;
1494 offset = p->h % off;
1500 if (m->in_op & FILE_OPINVERSE)
1505 if (OFFSET_OOB(nbytes, offset, 4))
1508 if (in_type == FILE_BEID3)
1509 lhs = cvt_id3(ms, lhs);
1511 switch (m->in_op & FILE_OPS_MASK) {
1527 case FILE_OPMULTIPLY:
1539 if (m->in_op & FILE_OPINVERSE)
1544 if (OFFSET_OOB(nbytes, offset, 4))
1547 if (in_type == FILE_LEID3)
1548 lhs = cvt_id3(ms, lhs);
1550 switch (m->in_op & FILE_OPS_MASK) {
1566 case FILE_OPMULTIPLY:
1578 if (m->in_op & FILE_OPINVERSE)
1582 if (OFFSET_OOB(nbytes, offset, 4))
1586 switch (m->in_op & FILE_OPS_MASK) {
1602 case FILE_OPMULTIPLY:
1614 if (m->in_op & FILE_OPINVERSE)
1618 if (OFFSET_OOB(nbytes, offset, 4))
1621 switch (m->in_op & FILE_OPS_MASK) {
1623 offset = p->l & off;
1626 offset = p->l | off;
1629 offset = p->l ^ off;
1632 offset = p->l + off;
1635 offset = p->l - off;
1637 case FILE_OPMULTIPLY:
1638 offset = p->l * off;
1641 offset = p->l / off;
1644 offset = p->l % off;
1649 if (m->in_op & FILE_OPINVERSE)
1656 if (m->flag & INDIROFFADD) {
1657 offset += ms->c.li[cont_level-1].off;
1659 if ((ms->flags & MAGIC_DEBUG) != 0)
1661 "indirect *zero* offset\n");
1664 if ((ms->flags & MAGIC_DEBUG) != 0)
1665 fprintf(stderr, "indirect +offs=%u\n", offset);
1667 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
1669 ms->offset = offset;
1671 if ((ms->flags & MAGIC_DEBUG) != 0) {
1672 mdebug(offset, (char *)(void *)p,
1673 sizeof(union VALUETYPE));
1674 #ifndef COMPILE_ONLY
1680 /* Verify we have enough data to match magic type */
1683 if (OFFSET_OOB(nbytes, offset, 1))
1690 if (OFFSET_OOB(nbytes, offset, 2))
1709 if (OFFSET_OOB(nbytes, offset, 4))
1716 if (OFFSET_OOB(nbytes, offset, 8))
1723 if (OFFSET_OOB(nbytes, offset, m->vallen))
1728 if (nbytes < offset)
1733 if (m->str_flags & INDIRECT_RELATIVE)
1734 offset += CAST(uint32_t, o);
1738 if (nbytes < offset)
1741 if ((pb = file_push_buffer(ms)) == NULL)
1745 rv = file_softmagic(ms, s + offset, nbytes - offset,
1746 indir_count, name_count, BINTEST, text);
1748 if ((ms->flags & MAGIC_DEBUG) != 0)
1749 fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1751 rbuf = file_pop_buffer(ms, pb);
1752 if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
1756 if ((ms->flags & MAGIC_NODESC) == 0 &&
1757 file_printf(ms, F(ms, m, "%u"), offset) == -1) {
1761 if (file_printf(ms, "%s", rbuf) == -1) {
1770 if (nbytes < offset)
1777 if (file_magicfind(ms, rbuf, &ml) == -1) {
1778 file_error(ms, 0, "cannot find entry `%s'", rbuf);
1782 oneed_separator = *need_separator;
1783 if (m->flag & NOSPACE)
1784 *need_separator = 0;
1785 rv = match(ms, ml.magic, ml.nmagic, s, nbytes, offset + o,
1786 mode, text, flip, indir_count, name_count,
1787 printed_something, need_separator, returnval);
1789 *need_separator = oneed_separator;
1793 if (ms->flags & MAGIC_NODESC)
1795 if (file_printf(ms, "%s", m->desc) == -1)
1799 case FILE_DEFAULT: /* nothing to check */
1804 if (!mconvert(ms, m, flip))
1810 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1813 * Convert the source args to unsigned here so that (1) the
1814 * compare will be unsigned as it is in strncmp() and (2) so
1815 * the ctype functions will work correctly without extra
1818 const unsigned char *a = (const unsigned char *)s1;
1819 const unsigned char *b = (const unsigned char *)s2;
1823 * What we want here is v = strncmp(s1, s2, len),
1824 * but ignoring any nulls.
1827 if (0L == flags) { /* normal string: do it fast */
1829 if ((v = *b++ - *a++) != '\0')
1832 else { /* combine the others */
1834 if ((flags & STRING_IGNORE_LOWERCASE) &&
1836 if ((v = tolower(*b++) - *a++) != '\0')
1839 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1841 if ((v = toupper(*b++) - *a++) != '\0')
1844 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1847 if (isspace(*b++)) {
1857 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1864 if ((v = *b++ - *a++) != '\0')
1873 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1876 * XXX - The 16-bit string compare probably needs to be done
1877 * differently, especially if the flags are to be supported.
1878 * At the moment, I am unsure.
1881 return file_strncmp(a, b, len, flags);
1885 magiccheck(struct magic_set *ms, struct magic *m)
1887 uint64_t l = m->value.q;
1892 union VALUETYPE *p = &ms->ms_value;
1962 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1995 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
2009 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
2012 case FILE_BESTRING16:
2013 case FILE_LESTRING16:
2015 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
2018 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
2022 if (ms->search.s == NULL)
2025 slen = MIN(m->vallen, sizeof(m->value.s));
2029 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
2030 if (slen + idx > ms->search.s_len)
2033 v = file_strncmp(m->value.s, ms->search.s + idx, slen,
2035 if (v == 0) { /* found match */
2036 ms->search.offset += idx;
2037 ms->search.rm_len = m->str_range - idx;
2048 if (ms->search.s == NULL)
2052 rc = file_regcomp(&rx, m->value.s,
2053 REG_EXTENDED|REG_NEWLINE|
2054 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
2056 file_regerror(&rx, rc, ms);
2060 size_t slen = ms->search.s_len;
2063 copy = malloc(slen);
2065 file_error(ms, errno,
2066 "can't allocate %" SIZE_T_FORMAT "u bytes",
2070 memcpy(copy, ms->search.s, slen);
2071 copy[--slen] = '\0';
2074 search = ms->search.s;
2077 rc = file_regexec(&rx, (const char *)search,
2082 ms->search.s += (int)pmatch.rm_so;
2083 ms->search.offset += (size_t)pmatch.rm_so;
2085 (size_t)(pmatch.rm_eo - pmatch.rm_so);
2094 file_regerror(&rx, rc, ms);
2100 if (v == (uint64_t)-1)
2109 matched = der_cmp(ms, m);
2111 file_error(ms, 0, "EOF comparing DER entries");
2114 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2118 v = file_signextend(ms, m, v);
2122 if ((ms->flags & MAGIC_DEBUG) != 0)
2123 (void) fprintf(stderr, "%" INT64_T_FORMAT
2124 "u == *any* = 1\n", (unsigned long long)v);
2130 if ((ms->flags & MAGIC_DEBUG) != 0)
2131 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
2132 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2133 (unsigned long long)l, matched);
2138 if ((ms->flags & MAGIC_DEBUG) != 0)
2139 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2140 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2141 (unsigned long long)l, matched);
2145 if (m->flag & UNSIGNED) {
2147 if ((ms->flags & MAGIC_DEBUG) != 0)
2148 (void) fprintf(stderr, "%" INT64_T_FORMAT
2149 "u > %" INT64_T_FORMAT "u = %d\n",
2150 (unsigned long long)v,
2151 (unsigned long long)l, matched);
2154 matched = (int64_t) v > (int64_t) l;
2155 if ((ms->flags & MAGIC_DEBUG) != 0)
2156 (void) fprintf(stderr, "%" INT64_T_FORMAT
2157 "d > %" INT64_T_FORMAT "d = %d\n",
2158 (long long)v, (long long)l, matched);
2163 if (m->flag & UNSIGNED) {
2165 if ((ms->flags & MAGIC_DEBUG) != 0)
2166 (void) fprintf(stderr, "%" INT64_T_FORMAT
2167 "u < %" INT64_T_FORMAT "u = %d\n",
2168 (unsigned long long)v,
2169 (unsigned long long)l, matched);
2172 matched = (int64_t) v < (int64_t) l;
2173 if ((ms->flags & MAGIC_DEBUG) != 0)
2174 (void) fprintf(stderr, "%" INT64_T_FORMAT
2175 "d < %" INT64_T_FORMAT "d = %d\n",
2176 (long long)v, (long long)l, matched);
2181 matched = (v & l) == l;
2182 if ((ms->flags & MAGIC_DEBUG) != 0)
2183 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2184 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2185 "x) = %d\n", (unsigned long long)v,
2186 (unsigned long long)l, (unsigned long long)l,
2191 matched = (v & l) != l;
2192 if ((ms->flags & MAGIC_DEBUG) != 0)
2193 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2194 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2195 "x) = %d\n", (unsigned long long)v,
2196 (unsigned long long)l, (unsigned long long)l,
2201 file_magerror(ms, "cannot happen: invalid relation `%c'",
2210 handle_annotation(struct magic_set *ms, struct magic *m)
2212 if ((ms->flags & MAGIC_APPLE) && m->apple[0]) {
2213 if (file_printf(ms, "%.8s", m->apple) == -1)
2217 if ((ms->flags & MAGIC_EXTENSION) && m->ext[0]) {
2218 if (file_printf(ms, "%s", m->ext) == -1)
2222 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2223 if (file_printf(ms, "%s", m->mimetype) == -1)
2231 print_sep(struct magic_set *ms, int firstline)
2233 if (ms->flags & MAGIC_NODESC)
2238 * we found another match
2239 * put a newline and '-' to do some simple formatting
2241 return file_printf(ms, "\n- ");