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.147 2011/11/05 15:44:22 rrt Exp $")
45 private int match(struct magic_set *, struct magic *, uint32_t,
46 const unsigned char *, size_t, int, int, int);
47 private int mget(struct magic_set *, const unsigned char *,
48 struct magic *, size_t, unsigned int, int, int);
49 private int magiccheck(struct magic_set *, struct magic *);
50 private int32_t mprint(struct magic_set *, struct magic *);
51 private int32_t moffset(struct magic_set *, struct magic *);
52 private void mdebug(uint32_t, const char *, size_t);
53 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
54 const unsigned char *, uint32_t, size_t, size_t);
55 private int mconvert(struct magic_set *, struct magic *);
56 private int print_sep(struct magic_set *, int);
57 private int handle_annotation(struct magic_set *, struct magic *);
58 private void cvt_8(union VALUETYPE *, const struct magic *);
59 private void cvt_16(union VALUETYPE *, const struct magic *);
60 private void cvt_32(union VALUETYPE *, const struct magic *);
61 private void cvt_64(union VALUETYPE *, const struct magic *);
63 #define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
65 #define MAX_RECURSION_LEVEL 10
68 * softmagic - lookup one file in parsed, in-memory copy of database
69 * Passed the name and FILE * of one file to be typed.
71 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
73 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
74 size_t level, int mode, int text)
78 for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
79 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode,
87 * Go through the whole list, stopping if you find a match. Process all
88 * the continuations of that match before returning.
90 * We support multi-level continuations:
92 * At any time when processing a successful top-level match, there is a
93 * current continuation level; it represents the level of the last
94 * successfully matched continuation.
96 * Continuations above that level are skipped as, if we see one, it
97 * means that the continuation that controls them - i.e, the
98 * lower-level continuation preceding them - failed to match.
100 * Continuations below that level are processed as, if we see one,
101 * it means we've finished processing or skipping higher-level
102 * continuations under the control of a successful or unsuccessful
103 * lower-level continuation, and are now seeing the next lower-level
104 * continuation and should process it. The current continuation
105 * level reverts to the level of the one we're seeing.
107 * Continuations at the current level are processed as, if we see
108 * one, there's no lower-level continuation that may have failed.
110 * If a continuation matches, we bump the current continuation level
111 * so that higher-level continuations are processed.
114 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
115 const unsigned char *s, size_t nbytes, int mode, int text,
118 uint32_t magindex = 0;
119 unsigned int cont_level = 0;
120 int need_separator = 0;
121 int returnval = 0, e; /* if a match is found it is set to 1*/
122 int firstline = 1; /* a flag to print X\n X\n- X */
123 int printed_something = 0;
124 int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
126 if (file_check_mem(ms, cont_level) == -1)
129 for (magindex = 0; magindex < nmagic; magindex++) {
131 struct magic *m = &magic[magindex];
133 if ((IS_STRING(m->type) &&
134 ((text && (m->str_flags & (STRING_BINTEST | STRING_TEXTTEST)) == STRING_BINTEST) ||
135 (!text && (m->str_flags & (STRING_TEXTTEST | STRING_BINTEST)) == STRING_TEXTTEST))) ||
136 (m->flag & mode) != mode) {
138 while (magic[magindex + 1].cont_level != 0 &&
141 continue; /* Skip to next top-level test*/
144 ms->offset = m->offset;
145 ms->line = m->lineno;
147 /* if main entry matches, print it... */
148 switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
152 flush = m->reln != '!';
155 if (m->type == FILE_INDIRECT)
158 switch (magiccheck(ms, m)) {
172 * main entry didn't match,
173 * flush its continuations
175 while (magindex < nmagic - 1 &&
176 magic[magindex + 1].cont_level != 0)
181 if ((e = handle_annotation(ms, m)) != 0)
184 * If we are going to print something, we'll need to print
185 * a blank before we print something else.
189 printed_something = 1;
190 if (print_sep(ms, firstline) == -1)
195 if (print && mprint(ms, m) == -1)
198 ms->c.li[cont_level].off = moffset(ms, m);
200 /* and any continuations that match */
201 if (file_check_mem(ms, ++cont_level) == -1)
204 while (magic[magindex+1].cont_level != 0 &&
205 ++magindex < nmagic) {
206 m = &magic[magindex];
207 ms->line = m->lineno; /* for messages */
209 if (cont_level < m->cont_level)
211 if (cont_level > m->cont_level) {
213 * We're at the end of the level
214 * "cont_level" continuations.
216 cont_level = m->cont_level;
218 ms->offset = m->offset;
219 if (m->flag & OFFADD) {
221 ms->c.li[cont_level - 1].off;
224 #ifdef ENABLE_CONDITIONALS
225 if (m->cond == COND_ELSE ||
226 m->cond == COND_ELIF) {
227 if (ms->c.li[cont_level].last_match == 1)
231 switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
240 if (m->type == FILE_INDIRECT)
246 switch (flush ? 1 : magiccheck(ms, m)) {
250 #ifdef ENABLE_CONDITIONALS
251 ms->c.li[cont_level].last_match = 0;
255 #ifdef ENABLE_CONDITIONALS
256 ms->c.li[cont_level].last_match = 1;
258 if (m->type != FILE_DEFAULT)
259 ms->c.li[cont_level].got_match = 1;
260 else if (ms->c.li[cont_level].got_match) {
261 ms->c.li[cont_level].got_match = 0;
264 if ((e = handle_annotation(ms, m)) != 0)
267 * If we are going to print something,
268 * make sure that we have a separator first.
271 if (!printed_something) {
272 printed_something = 1;
273 if (print_sep(ms, firstline)
279 * This continuation matched. Print
280 * its message, with a blank before it
281 * if the previous item printed and
282 * this item isn't empty.
284 /* space if previous printed */
286 && ((m->flag & NOSPACE) == 0)
289 file_printf(ms, " ") == -1)
293 if (print && mprint(ms, m) == -1)
296 ms->c.li[cont_level].off = moffset(ms, m);
302 * If we see any continuations
306 if (file_check_mem(ms, ++cont_level) == -1)
311 if (printed_something) {
316 if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
317 return returnval; /* don't keep searching */
320 return returnval; /* This is hit if -k is set or there is no match */
324 check_fmt(struct magic_set *ms, struct magic *m)
329 if (strchr(m->desc, '%') == NULL)
332 rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
335 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
336 file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
339 rc = regexec(&rx, m->desc, 0, 0, 0);
346 char * strndup(const char *, size_t);
349 strndup(const char *str, size_t n)
354 for (len = 0; len < n && str[len]; len++)
356 if ((copy = malloc(len + 1)) == NULL)
358 (void)memcpy(copy, str, len);
362 #endif /* HAVE_STRNDUP */
365 mprint(struct magic_set *ms, struct magic *m)
372 union VALUETYPE *p = &ms->ms_value;
376 v = file_signextend(ms, m, (uint64_t)p->b);
377 switch (check_fmt(ms, m)) {
381 (void)snprintf(buf, sizeof(buf), "%c",
383 if (file_printf(ms, m->desc, buf) == -1)
387 if (file_printf(ms, m->desc, (unsigned char) v) == -1)
391 t = ms->offset + sizeof(char);
397 v = file_signextend(ms, m, (uint64_t)p->h);
398 switch (check_fmt(ms, m)) {
402 (void)snprintf(buf, sizeof(buf), "%hu",
404 if (file_printf(ms, m->desc, buf) == -1)
409 file_printf(ms, m->desc, (unsigned short) v) == -1)
413 t = ms->offset + sizeof(short);
420 v = file_signextend(ms, m, (uint64_t)p->l);
421 switch (check_fmt(ms, m)) {
425 (void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v);
426 if (file_printf(ms, m->desc, buf) == -1)
430 if (file_printf(ms, m->desc, (uint32_t) v) == -1)
434 t = ms->offset + sizeof(int32_t);
440 v = file_signextend(ms, m, p->q);
441 if (file_printf(ms, m->desc, (uint64_t) v) == -1)
443 t = ms->offset + sizeof(int64_t);
448 case FILE_BESTRING16:
449 case FILE_LESTRING16:
450 if (m->reln == '=' || m->reln == '!') {
451 if (file_printf(ms, m->desc, m->value.s) == -1)
453 t = ms->offset + m->vallen;
456 if (*m->value.s == '\0')
457 p->s[strcspn(p->s, "\n")] = '\0';
458 if (file_printf(ms, m->desc, p->s) == -1)
460 t = ms->offset + strlen(p->s);
461 if (m->type == FILE_PSTRING)
462 t += file_pstring_length_size(m);
470 if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
472 t = ms->offset + sizeof(time_t);
479 if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
481 t = ms->offset + sizeof(time_t);
487 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
490 t = ms->offset + sizeof(uint64_t);
496 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
499 t = ms->offset + sizeof(uint64_t);
506 switch (check_fmt(ms, m)) {
510 (void)snprintf(buf, sizeof(buf), "%g", vf);
511 if (file_printf(ms, m->desc, buf) == -1)
515 if (file_printf(ms, m->desc, vf) == -1)
519 t = ms->offset + sizeof(float);
526 switch (check_fmt(ms, m)) {
530 (void)snprintf(buf, sizeof(buf), "%g", vd);
531 if (file_printf(ms, m->desc, buf) == -1)
535 if (file_printf(ms, m->desc, vd) == -1)
539 t = ms->offset + sizeof(double);
546 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
548 file_oomem(ms, ms->search.rm_len);
551 rval = file_printf(ms, m->desc, cp);
557 if ((m->str_flags & REGEX_OFFSET_START))
558 t = ms->search.offset;
560 t = ms->search.offset + ms->search.rm_len;
565 if (file_printf(ms, m->desc, m->value.s) == -1)
567 if ((m->str_flags & REGEX_OFFSET_START))
568 t = ms->search.offset;
570 t = ms->search.offset + m->vallen;
574 if (file_printf(ms, m->desc, m->value.s) == -1)
584 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
591 moffset(struct magic_set *ms, struct magic *m)
595 return CAST(int32_t, (ms->offset + sizeof(char)));
600 return CAST(int32_t, (ms->offset + sizeof(short)));
606 return CAST(int32_t, (ms->offset + sizeof(int32_t)));
611 return CAST(int32_t, (ms->offset + sizeof(int64_t)));
615 case FILE_BESTRING16:
616 case FILE_LESTRING16:
617 if (m->reln == '=' || m->reln == '!')
618 return ms->offset + m->vallen;
620 union VALUETYPE *p = &ms->ms_value;
623 if (*m->value.s == '\0')
624 p->s[strcspn(p->s, "\n")] = '\0';
625 t = CAST(uint32_t, (ms->offset + strlen(p->s)));
626 if (m->type == FILE_PSTRING)
627 t += file_pstring_length_size(m);
635 return CAST(int32_t, (ms->offset + sizeof(time_t)));
641 return CAST(int32_t, (ms->offset + sizeof(time_t)));
646 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
651 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
656 return CAST(int32_t, (ms->offset + sizeof(float)));
661 return CAST(int32_t, (ms->offset + sizeof(double)));
664 if ((m->str_flags & REGEX_OFFSET_START) != 0)
665 return CAST(int32_t, ms->search.offset);
667 return CAST(int32_t, (ms->search.offset +
671 if ((m->str_flags & REGEX_OFFSET_START) != 0)
672 return CAST(int32_t, ms->search.offset);
674 return CAST(int32_t, (ms->search.offset + m->vallen));
687 #define DO_CVT(fld, cast) \
689 switch (m->mask_op & FILE_OPS_MASK) { \
691 p->fld &= cast m->num_mask; \
694 p->fld |= cast m->num_mask; \
697 p->fld ^= cast m->num_mask; \
700 p->fld += cast m->num_mask; \
703 p->fld -= cast m->num_mask; \
705 case FILE_OPMULTIPLY: \
706 p->fld *= cast m->num_mask; \
708 case FILE_OPDIVIDE: \
709 p->fld /= cast m->num_mask; \
711 case FILE_OPMODULO: \
712 p->fld %= cast m->num_mask; \
715 if (m->mask_op & FILE_OPINVERSE) \
719 cvt_8(union VALUETYPE *p, const struct magic *m)
721 DO_CVT(b, (uint8_t));
725 cvt_16(union VALUETYPE *p, const struct magic *m)
727 DO_CVT(h, (uint16_t));
731 cvt_32(union VALUETYPE *p, const struct magic *m)
733 DO_CVT(l, (uint32_t));
737 cvt_64(union VALUETYPE *p, const struct magic *m)
739 DO_CVT(q, (uint64_t));
742 #define DO_CVT2(fld, cast) \
744 switch (m->mask_op & FILE_OPS_MASK) { \
746 p->fld += cast m->num_mask; \
749 p->fld -= cast m->num_mask; \
751 case FILE_OPMULTIPLY: \
752 p->fld *= cast m->num_mask; \
754 case FILE_OPDIVIDE: \
755 p->fld /= cast m->num_mask; \
760 cvt_float(union VALUETYPE *p, const struct magic *m)
766 cvt_double(union VALUETYPE *p, const struct magic *m)
768 DO_CVT2(d, (double));
772 * Convert the byte order of the data we are looking at
773 * While we're here, let's apply the mask operation
774 * (unless you have a better idea)
777 mconvert(struct magic_set *ms, struct magic *m)
779 union VALUETYPE *p = &ms->ms_value;
799 case FILE_BESTRING16:
800 case FILE_LESTRING16: {
801 /* Null terminate and eat *trailing* return */
802 p->s[sizeof(p->s) - 1] = '\0';
806 char *ptr1 = p->s, *ptr2 = ptr1 + file_pstring_length_size(m);
807 size_t len = file_pstring_get_length(m, ptr1);
808 if (len >= sizeof(p->s))
809 len = sizeof(p->s) - 1;
816 p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
823 ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
830 (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
831 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
832 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
833 ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
837 p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
844 ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
851 (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
852 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
853 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
854 ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
861 ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
868 p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
869 ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
873 p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
874 ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
881 p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
882 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
883 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
884 ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
888 p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
889 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
890 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
891 ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
899 file_magerror(ms, "invalid type %d in mconvert()", m->type);
906 mdebug(uint32_t offset, const char *str, size_t len)
908 (void) fprintf(stderr, "mget @%d: ", offset);
909 file_showstr(stderr, str, len);
910 (void) fputc('\n', stderr);
911 (void) fputc('\n', stderr);
915 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
916 const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
919 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
920 * anything, but setup pointers into the source
925 ms->search.s = RCAST(const char *, s) + offset;
926 ms->search.s_len = nbytes - offset;
927 ms->search.offset = offset;
933 const char *last; /* end of search region */
934 const char *buf; /* start of search region */
939 ms->search.s_len = 0;
943 buf = RCAST(const char *, s) + offset;
944 end = last = RCAST(const char *, s) + nbytes;
945 /* mget() guarantees buf <= last */
946 for (lines = linecnt, b = buf; lines && b < end &&
947 ((b = CAST(const char *,
948 memchr(c = b, '\n', CAST(size_t, (end - b)))))
949 || (b = CAST(const char *,
950 memchr(c, '\r', CAST(size_t, (end - c))))));
953 if (b[0] == '\r' && b[1] == '\n')
957 last = RCAST(const char *, s) + nbytes;
960 ms->search.s_len = last - buf;
961 ms->search.offset = offset;
962 ms->search.rm_len = 0;
965 case FILE_BESTRING16:
966 case FILE_LESTRING16: {
967 const unsigned char *src = s + offset;
968 const unsigned char *esrc = s + nbytes;
970 char *edst = &p->s[sizeof(p->s) - 1];
972 if (type == FILE_BESTRING16)
975 /* check for pointer overflow */
977 file_magerror(ms, "invalid offset %u in mcopy()",
981 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
987 if (type == FILE_BESTRING16 ?
996 case FILE_STRING: /* XXX - these two should not need */
997 case FILE_PSTRING: /* to copy anything, but do anyway. */
1003 if (offset >= nbytes) {
1004 (void)memset(p, '\0', sizeof(*p));
1007 if (nbytes - offset < sizeof(*p))
1008 nbytes = nbytes - offset;
1010 nbytes = sizeof(*p);
1012 (void)memcpy(p, s + offset, nbytes);
1015 * the usefulness of padding with zeroes eludes me, it
1016 * might even cause problems
1018 if (nbytes < sizeof(*p))
1019 (void)memset(((char *)(void *)p) + nbytes, '\0',
1020 sizeof(*p) - nbytes);
1025 mget(struct magic_set *ms, const unsigned char *s,
1026 struct magic *m, size_t nbytes, unsigned int cont_level, int text,
1027 int recursion_level)
1029 uint32_t offset = ms->offset;
1030 uint32_t count = m->str_range;
1031 union VALUETYPE *p = &ms->ms_value;
1033 if (recursion_level >= MAX_RECURSION_LEVEL) {
1034 file_error(ms, 0, "recursion nesting exceeded");
1038 if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
1041 if ((ms->flags & MAGIC_DEBUG) != 0) {
1042 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1043 #ifndef COMPILE_ONLY
1048 if (m->flag & INDIR) {
1049 int off = m->in_offset;
1050 if (m->in_op & FILE_OPINDIRECT) {
1051 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1052 ((const void *)(s + offset + off)));
1053 switch (m->in_type) {
1061 off = (short)((q->hs[0]<<8)|(q->hs[1]));
1064 off = (short)((q->hs[1]<<8)|(q->hs[0]));
1071 off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
1072 (q->hl[2]<<8)|(q->hl[3]));
1076 off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
1077 (q->hl[1]<<8)|(q->hl[0]));
1080 off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
1081 (q->hl[3]<<8)|(q->hl[2]));
1085 switch (m->in_type) {
1087 if (OFFSET_OOB(nbytes, offset, 1))
1090 switch (m->in_op & FILE_OPS_MASK) {
1092 offset = p->b & off;
1095 offset = p->b | off;
1098 offset = p->b ^ off;
1101 offset = p->b + off;
1104 offset = p->b - off;
1106 case FILE_OPMULTIPLY:
1107 offset = p->b * off;
1110 offset = p->b / off;
1113 offset = p->b % off;
1118 if (m->in_op & FILE_OPINVERSE)
1122 if (OFFSET_OOB(nbytes, offset, 2))
1125 switch (m->in_op & FILE_OPS_MASK) {
1127 offset = (short)((p->hs[0]<<8)|
1132 offset = (short)((p->hs[0]<<8)|
1137 offset = (short)((p->hs[0]<<8)|
1142 offset = (short)((p->hs[0]<<8)|
1147 offset = (short)((p->hs[0]<<8)|
1151 case FILE_OPMULTIPLY:
1152 offset = (short)((p->hs[0]<<8)|
1157 offset = (short)((p->hs[0]<<8)|
1162 offset = (short)((p->hs[0]<<8)|
1168 offset = (short)((p->hs[0]<<8)|
1170 if (m->in_op & FILE_OPINVERSE)
1174 if (OFFSET_OOB(nbytes, offset, 2))
1177 switch (m->in_op & FILE_OPS_MASK) {
1179 offset = (short)((p->hs[1]<<8)|
1184 offset = (short)((p->hs[1]<<8)|
1189 offset = (short)((p->hs[1]<<8)|
1194 offset = (short)((p->hs[1]<<8)|
1199 offset = (short)((p->hs[1]<<8)|
1203 case FILE_OPMULTIPLY:
1204 offset = (short)((p->hs[1]<<8)|
1209 offset = (short)((p->hs[1]<<8)|
1214 offset = (short)((p->hs[1]<<8)|
1220 offset = (short)((p->hs[1]<<8)|
1222 if (m->in_op & FILE_OPINVERSE)
1226 if (OFFSET_OOB(nbytes, offset, 2))
1229 switch (m->in_op & FILE_OPS_MASK) {
1231 offset = p->h & off;
1234 offset = p->h | off;
1237 offset = p->h ^ off;
1240 offset = p->h + off;
1243 offset = p->h - off;
1245 case FILE_OPMULTIPLY:
1246 offset = p->h * off;
1249 offset = p->h / off;
1252 offset = p->h % off;
1258 if (m->in_op & FILE_OPINVERSE)
1263 if (OFFSET_OOB(nbytes, offset, 4))
1266 switch (m->in_op & FILE_OPS_MASK) {
1268 offset = (int32_t)((p->hl[0]<<24)|
1275 offset = (int32_t)((p->hl[0]<<24)|
1282 offset = (int32_t)((p->hl[0]<<24)|
1289 offset = (int32_t)((p->hl[0]<<24)|
1296 offset = (int32_t)((p->hl[0]<<24)|
1302 case FILE_OPMULTIPLY:
1303 offset = (int32_t)((p->hl[0]<<24)|
1310 offset = (int32_t)((p->hl[0]<<24)|
1317 offset = (int32_t)((p->hl[0]<<24)|
1325 offset = (int32_t)((p->hl[0]<<24)|
1329 if (m->in_op & FILE_OPINVERSE)
1334 if (OFFSET_OOB(nbytes, offset, 4))
1337 switch (m->in_op & FILE_OPS_MASK) {
1339 offset = (int32_t)((p->hl[3]<<24)|
1346 offset = (int32_t)((p->hl[3]<<24)|
1353 offset = (int32_t)((p->hl[3]<<24)|
1360 offset = (int32_t)((p->hl[3]<<24)|
1367 offset = (int32_t)((p->hl[3]<<24)|
1373 case FILE_OPMULTIPLY:
1374 offset = (int32_t)((p->hl[3]<<24)|
1381 offset = (int32_t)((p->hl[3]<<24)|
1388 offset = (int32_t)((p->hl[3]<<24)|
1396 offset = (int32_t)((p->hl[3]<<24)|
1400 if (m->in_op & FILE_OPINVERSE)
1404 if (OFFSET_OOB(nbytes, offset, 4))
1407 switch (m->in_op & FILE_OPS_MASK) {
1409 offset = (int32_t)((p->hl[1]<<24)|
1416 offset = (int32_t)((p->hl[1]<<24)|
1423 offset = (int32_t)((p->hl[1]<<24)|
1430 offset = (int32_t)((p->hl[1]<<24)|
1437 offset = (int32_t)((p->hl[1]<<24)|
1443 case FILE_OPMULTIPLY:
1444 offset = (int32_t)((p->hl[1]<<24)|
1451 offset = (int32_t)((p->hl[1]<<24)|
1458 offset = (int32_t)((p->hl[1]<<24)|
1466 offset = (int32_t)((p->hl[1]<<24)|
1470 if (m->in_op & FILE_OPINVERSE)
1474 if (OFFSET_OOB(nbytes, offset, 4))
1477 switch (m->in_op & FILE_OPS_MASK) {
1479 offset = p->l & off;
1482 offset = p->l | off;
1485 offset = p->l ^ off;
1488 offset = p->l + off;
1491 offset = p->l - off;
1493 case FILE_OPMULTIPLY:
1494 offset = p->l * off;
1497 offset = p->l / off;
1500 offset = p->l % off;
1505 if (m->in_op & FILE_OPINVERSE)
1510 switch (m->in_type) {
1513 offset = ((((offset >> 0) & 0x7f) << 0) |
1514 (((offset >> 8) & 0x7f) << 7) |
1515 (((offset >> 16) & 0x7f) << 14) |
1516 (((offset >> 24) & 0x7f) << 21)) + 10;
1522 if (m->flag & INDIROFFADD) {
1523 offset += ms->c.li[cont_level-1].off;
1525 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
1527 ms->offset = offset;
1529 if ((ms->flags & MAGIC_DEBUG) != 0) {
1530 mdebug(offset, (char *)(void *)p,
1531 sizeof(union VALUETYPE));
1532 #ifndef COMPILE_ONLY
1538 /* Verify we have enough data to match magic type */
1541 if (OFFSET_OOB(nbytes, offset, 1))
1548 if (OFFSET_OOB(nbytes, offset, 2))
1567 if (OFFSET_OOB(nbytes, offset, 4))
1574 if (OFFSET_OOB(nbytes, offset, 8))
1581 if (OFFSET_OOB(nbytes, offset, m->vallen))
1586 if (nbytes < offset)
1593 if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
1594 file_printf(ms, "%s", m->desc) == -1)
1596 if (nbytes < offset)
1598 return file_softmagic(ms, s + offset, nbytes - offset,
1599 recursion_level, BINTEST, text);
1601 case FILE_DEFAULT: /* nothing to check */
1605 if (!mconvert(ms, m))
1611 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1614 * Convert the source args to unsigned here so that (1) the
1615 * compare will be unsigned as it is in strncmp() and (2) so
1616 * the ctype functions will work correctly without extra
1619 const unsigned char *a = (const unsigned char *)s1;
1620 const unsigned char *b = (const unsigned char *)s2;
1624 * What we want here is v = strncmp(s1, s2, len),
1625 * but ignoring any nulls.
1628 if (0L == flags) { /* normal string: do it fast */
1630 if ((v = *b++ - *a++) != '\0')
1633 else { /* combine the others */
1635 if ((flags & STRING_IGNORE_LOWERCASE) &&
1637 if ((v = tolower(*b++) - *a++) != '\0')
1640 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1642 if ((v = toupper(*b++) - *a++) != '\0')
1645 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1648 if (isspace(*b++)) {
1658 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1665 if ((v = *b++ - *a++) != '\0')
1674 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1677 * XXX - The 16-bit string compare probably needs to be done
1678 * differently, especially if the flags are to be supported.
1679 * At the moment, I am unsure.
1682 return file_strncmp(a, b, len, flags);
1686 magiccheck(struct magic_set *ms, struct magic *m)
1688 uint64_t l = m->value.q;
1693 union VALUETYPE *p = &ms->ms_value;
1761 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1795 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
1808 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1811 case FILE_BESTRING16:
1812 case FILE_LESTRING16:
1814 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1817 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1821 if (ms->search.s == NULL)
1824 slen = MIN(m->vallen, sizeof(m->value.s));
1828 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
1829 if (slen + idx > ms->search.s_len)
1832 v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
1833 if (v == 0) { /* found match */
1834 ms->search.offset += idx;
1845 if (ms->search.s == NULL)
1849 rc = regcomp(&rx, m->value.s,
1850 REG_EXTENDED|REG_NEWLINE|
1851 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
1853 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1854 file_magerror(ms, "regex error %d, (%s)",
1859 regmatch_t pmatch[1];
1860 #ifndef REG_STARTEND
1861 #define REG_STARTEND 0
1862 size_t l = ms->search.s_len - 1;
1863 char c = ms->search.s[l];
1864 ((char *)(intptr_t)ms->search.s)[l] = '\0';
1866 pmatch[0].rm_so = 0;
1867 pmatch[0].rm_eo = ms->search.s_len;
1869 rc = regexec(&rx, (const char *)ms->search.s,
1870 1, pmatch, REG_STARTEND);
1871 #if REG_STARTEND == 0
1872 ((char *)(intptr_t)ms->search.s)[l] = c;
1876 ms->search.s += (int)pmatch[0].rm_so;
1877 ms->search.offset += (size_t)pmatch[0].rm_so;
1879 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
1888 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1889 file_magerror(ms, "regexec error %d, (%s)",
1896 if (v == (uint64_t)-1)
1903 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
1907 v = file_signextend(ms, m, v);
1911 if ((ms->flags & MAGIC_DEBUG) != 0)
1912 (void) fprintf(stderr, "%" INT64_T_FORMAT
1913 "u == *any* = 1\n", (unsigned long long)v);
1919 if ((ms->flags & MAGIC_DEBUG) != 0)
1920 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
1921 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
1922 (unsigned long long)l, matched);
1927 if ((ms->flags & MAGIC_DEBUG) != 0)
1928 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
1929 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
1930 (unsigned long long)l, matched);
1934 if (m->flag & UNSIGNED) {
1936 if ((ms->flags & MAGIC_DEBUG) != 0)
1937 (void) fprintf(stderr, "%" INT64_T_FORMAT
1938 "u > %" INT64_T_FORMAT "u = %d\n",
1939 (unsigned long long)v,
1940 (unsigned long long)l, matched);
1943 matched = (int64_t) v > (int64_t) l;
1944 if ((ms->flags & MAGIC_DEBUG) != 0)
1945 (void) fprintf(stderr, "%" INT64_T_FORMAT
1946 "d > %" INT64_T_FORMAT "d = %d\n",
1947 (long long)v, (long long)l, matched);
1952 if (m->flag & UNSIGNED) {
1954 if ((ms->flags & MAGIC_DEBUG) != 0)
1955 (void) fprintf(stderr, "%" INT64_T_FORMAT
1956 "u < %" INT64_T_FORMAT "u = %d\n",
1957 (unsigned long long)v,
1958 (unsigned long long)l, matched);
1961 matched = (int64_t) v < (int64_t) l;
1962 if ((ms->flags & MAGIC_DEBUG) != 0)
1963 (void) fprintf(stderr, "%" INT64_T_FORMAT
1964 "d < %" INT64_T_FORMAT "d = %d\n",
1965 (long long)v, (long long)l, matched);
1970 matched = (v & l) == l;
1971 if ((ms->flags & MAGIC_DEBUG) != 0)
1972 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
1973 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
1974 "x) = %d\n", (unsigned long long)v,
1975 (unsigned long long)l, (unsigned long long)l,
1980 matched = (v & l) != l;
1981 if ((ms->flags & MAGIC_DEBUG) != 0)
1982 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
1983 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
1984 "x) = %d\n", (unsigned long long)v,
1985 (unsigned long long)l, (unsigned long long)l,
1991 file_magerror(ms, "cannot happen: invalid relation `%c'",
2000 handle_annotation(struct magic_set *ms, struct magic *m)
2002 if (ms->flags & MAGIC_APPLE) {
2003 if (file_printf(ms, "%.8s", m->apple) == -1)
2007 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2008 if (file_printf(ms, "%s", m->mimetype) == -1)
2016 print_sep(struct magic_set *ms, int firstline)
2018 if (ms->flags & MAGIC_MIME)
2023 * we found another match
2024 * put a newline and '-' to do some simple formatting
2026 return file_printf(ms, "\n- ");