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.135 2009/03/27 22:42:49 christos Exp $")
45 private int match(struct magic_set *, struct magic *, uint32_t,
46 const unsigned char *, size_t, int, int);
47 private int mget(struct magic_set *, const unsigned char *,
48 struct magic *, size_t, unsigned 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)
78 for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
79 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode, level)) != 0)
86 * Go through the whole list, stopping if you find a match. Process all
87 * the continuations of that match before returning.
89 * We support multi-level continuations:
91 * At any time when processing a successful top-level match, there is a
92 * current continuation level; it represents the level of the last
93 * successfully matched continuation.
95 * Continuations above that level are skipped as, if we see one, it
96 * means that the continuation that controls them - i.e, the
97 * lower-level continuation preceding them - failed to match.
99 * Continuations below that level are processed as, if we see one,
100 * it means we've finished processing or skipping higher-level
101 * continuations under the control of a successful or unsuccessful
102 * lower-level continuation, and are now seeing the next lower-level
103 * continuation and should process it. The current continuation
104 * level reverts to the level of the one we're seeing.
106 * Continuations at the current level are processed as, if we see
107 * one, there's no lower-level continuation that may have failed.
109 * If a continuation matches, we bump the current continuation level
110 * so that higher-level continuations are processed.
113 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
114 const unsigned char *s, size_t nbytes, int mode, int recursion_level)
116 uint32_t magindex = 0;
117 unsigned int cont_level = 0;
118 int need_separator = 0;
119 int returnval = 0, e; /* if a match is found it is set to 1*/
120 int firstline = 1; /* a flag to print X\n X\n- X */
121 int printed_something = 0;
122 int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
124 if (file_check_mem(ms, cont_level) == -1)
127 for (magindex = 0; magindex < nmagic; magindex++) {
129 struct magic *m = &magic[magindex];
131 if ((m->flag & BINTEST) != mode) {
133 while (magic[magindex + 1].cont_level != 0 &&
136 continue; /* Skip to next top-level test*/
139 ms->offset = m->offset;
140 ms->line = m->lineno;
142 /* if main entry matches, print it... */
143 switch (mget(ms, s, m, nbytes, cont_level, recursion_level + 1)) {
147 flush = m->reln != '!';
150 if (m->type == FILE_INDIRECT)
153 switch (magiccheck(ms, m)) {
167 * main entry didn't match,
168 * flush its continuations
170 while (magindex < nmagic - 1 &&
171 magic[magindex + 1].cont_level != 0)
177 * If we are going to print something, we'll need to print
178 * a blank before we print something else.
182 printed_something = 1;
183 if ((e = handle_annotation(ms, m)) != 0)
185 if (print_sep(ms, firstline) == -1)
190 if (print && mprint(ms, m) == -1)
193 ms->c.li[cont_level].off = moffset(ms, m);
195 /* and any continuations that match */
196 if (file_check_mem(ms, ++cont_level) == -1)
199 while (magic[magindex+1].cont_level != 0 &&
200 ++magindex < nmagic) {
201 m = &magic[magindex];
202 ms->line = m->lineno; /* for messages */
204 if (cont_level < m->cont_level)
206 if (cont_level > m->cont_level) {
208 * We're at the end of the level
209 * "cont_level" continuations.
211 cont_level = m->cont_level;
213 ms->offset = m->offset;
214 if (m->flag & OFFADD) {
216 ms->c.li[cont_level - 1].off;
219 #ifdef ENABLE_CONDITIONALS
220 if (m->cond == COND_ELSE ||
221 m->cond == COND_ELIF) {
222 if (ms->c.li[cont_level].last_match == 1)
226 switch (mget(ms, s, m, nbytes, cont_level, recursion_level + 1)) {
235 if (m->type == FILE_INDIRECT)
241 switch (flush ? 1 : magiccheck(ms, m)) {
245 #ifdef ENABLE_CONDITIONALS
246 ms->c.li[cont_level].last_match = 0;
250 #ifdef ENABLE_CONDITIONALS
251 ms->c.li[cont_level].last_match = 1;
253 if (m->type != FILE_DEFAULT)
254 ms->c.li[cont_level].got_match = 1;
255 else if (ms->c.li[cont_level].got_match) {
256 ms->c.li[cont_level].got_match = 0;
260 * If we are going to print something,
261 * make sure that we have a separator first.
264 if ((e = handle_annotation(ms, m)) != 0)
266 if (!printed_something) {
267 printed_something = 1;
268 if (print_sep(ms, firstline)
274 * This continuation matched. Print
275 * its message, with a blank before it
276 * if the previous item printed and
277 * this item isn't empty.
279 /* space if previous printed */
281 && ((m->flag & NOSPACE) == 0)
284 file_printf(ms, " ") == -1)
288 if (print && mprint(ms, m) == -1)
291 ms->c.li[cont_level].off = moffset(ms, m);
297 * If we see any continuations
301 if (file_check_mem(ms, ++cont_level) == -1)
306 if (printed_something) {
311 if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
312 return returnval; /* don't keep searching */
315 return returnval; /* This is hit if -k is set or there is no match */
319 check_fmt(struct magic_set *ms, struct magic *m)
324 if (strchr(m->desc, '%') == NULL)
327 rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
330 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
331 file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
334 rc = regexec(&rx, m->desc, 0, 0, 0);
341 char * strndup(const char *, size_t);
344 strndup(const char *str, size_t n)
349 for (len = 0; len < n && str[len]; len++)
351 if ((copy = malloc(len + 1)) == NULL)
353 (void)memcpy(copy, str, len);
357 #endif /* HAVE_STRNDUP */
360 mprint(struct magic_set *ms, struct magic *m)
367 union VALUETYPE *p = &ms->ms_value;
371 v = file_signextend(ms, m, (uint64_t)p->b);
372 switch (check_fmt(ms, m)) {
376 (void)snprintf(buf, sizeof(buf), "%c",
378 if (file_printf(ms, m->desc, buf) == -1)
382 if (file_printf(ms, m->desc, (unsigned char) v) == -1)
386 t = ms->offset + sizeof(char);
392 v = file_signextend(ms, m, (uint64_t)p->h);
393 switch (check_fmt(ms, m)) {
397 (void)snprintf(buf, sizeof(buf), "%hu",
399 if (file_printf(ms, m->desc, buf) == -1)
404 file_printf(ms, m->desc, (unsigned short) v) == -1)
408 t = ms->offset + sizeof(short);
415 v = file_signextend(ms, m, (uint64_t)p->l);
416 switch (check_fmt(ms, m)) {
420 (void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v);
421 if (file_printf(ms, m->desc, buf) == -1)
425 if (file_printf(ms, m->desc, (uint32_t) v) == -1)
429 t = ms->offset + sizeof(int32_t);
435 v = file_signextend(ms, m, p->q);
436 if (file_printf(ms, m->desc, (uint64_t) v) == -1)
438 t = ms->offset + sizeof(int64_t);
443 case FILE_BESTRING16:
444 case FILE_LESTRING16:
445 if (m->reln == '=' || m->reln == '!') {
446 if (file_printf(ms, m->desc, m->value.s) == -1)
448 t = ms->offset + m->vallen;
451 if (*m->value.s == '\0')
452 p->s[strcspn(p->s, "\n")] = '\0';
453 if (file_printf(ms, m->desc, p->s) == -1)
455 t = ms->offset + strlen(p->s);
456 if (m->type == FILE_PSTRING)
465 if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
467 t = ms->offset + sizeof(time_t);
474 if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
476 t = ms->offset + sizeof(time_t);
482 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
485 t = ms->offset + sizeof(uint64_t);
491 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
494 t = ms->offset + sizeof(uint64_t);
501 switch (check_fmt(ms, m)) {
505 (void)snprintf(buf, sizeof(buf), "%g", vf);
506 if (file_printf(ms, m->desc, buf) == -1)
510 if (file_printf(ms, m->desc, vf) == -1)
514 t = ms->offset + sizeof(float);
521 switch (check_fmt(ms, m)) {
525 (void)snprintf(buf, sizeof(buf), "%g", vd);
526 if (file_printf(ms, m->desc, buf) == -1)
530 if (file_printf(ms, m->desc, vd) == -1)
534 t = ms->offset + sizeof(double);
541 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
543 file_oomem(ms, ms->search.rm_len);
546 rval = file_printf(ms, m->desc, cp);
552 if ((m->str_flags & REGEX_OFFSET_START))
553 t = ms->search.offset;
555 t = ms->search.offset + ms->search.rm_len;
560 if (file_printf(ms, m->desc, m->value.s) == -1)
562 if ((m->str_flags & REGEX_OFFSET_START))
563 t = ms->search.offset;
565 t = ms->search.offset + m->vallen;
569 if (file_printf(ms, m->desc, m->value.s) == -1)
579 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
586 moffset(struct magic_set *ms, struct magic *m)
590 return ms->offset + sizeof(char);
595 return ms->offset + sizeof(short);
601 return ms->offset + sizeof(int32_t);
606 return ms->offset + sizeof(int64_t);
610 case FILE_BESTRING16:
611 case FILE_LESTRING16:
612 if (m->reln == '=' || m->reln == '!')
613 return ms->offset + m->vallen;
615 union VALUETYPE *p = &ms->ms_value;
618 if (*m->value.s == '\0')
619 p->s[strcspn(p->s, "\n")] = '\0';
620 t = ms->offset + strlen(p->s);
621 if (m->type == FILE_PSTRING)
630 return ms->offset + sizeof(time_t);
636 return ms->offset + sizeof(time_t);
641 return ms->offset + sizeof(uint64_t);
646 return ms->offset + sizeof(uint64_t);
651 return ms->offset + sizeof(float);
656 return ms->offset + sizeof(double);
660 if ((m->str_flags & REGEX_OFFSET_START) != 0)
661 return ms->search.offset;
663 return ms->search.offset + ms->search.rm_len;
666 if ((m->str_flags & REGEX_OFFSET_START) != 0)
667 return ms->search.offset;
669 return ms->search.offset + m->vallen;
682 #define DO_CVT(fld, cast) \
684 switch (m->mask_op & FILE_OPS_MASK) { \
686 p->fld &= cast m->num_mask; \
689 p->fld |= cast m->num_mask; \
692 p->fld ^= cast m->num_mask; \
695 p->fld += cast m->num_mask; \
698 p->fld -= cast m->num_mask; \
700 case FILE_OPMULTIPLY: \
701 p->fld *= cast m->num_mask; \
703 case FILE_OPDIVIDE: \
704 p->fld /= cast m->num_mask; \
706 case FILE_OPMODULO: \
707 p->fld %= cast m->num_mask; \
710 if (m->mask_op & FILE_OPINVERSE) \
714 cvt_8(union VALUETYPE *p, const struct magic *m)
716 DO_CVT(b, (uint8_t));
720 cvt_16(union VALUETYPE *p, const struct magic *m)
722 DO_CVT(h, (uint16_t));
726 cvt_32(union VALUETYPE *p, const struct magic *m)
728 DO_CVT(l, (uint32_t));
732 cvt_64(union VALUETYPE *p, const struct magic *m)
734 DO_CVT(q, (uint64_t));
737 #define DO_CVT2(fld, cast) \
739 switch (m->mask_op & FILE_OPS_MASK) { \
741 p->fld += cast m->num_mask; \
744 p->fld -= cast m->num_mask; \
746 case FILE_OPMULTIPLY: \
747 p->fld *= cast m->num_mask; \
749 case FILE_OPDIVIDE: \
750 p->fld /= cast m->num_mask; \
755 cvt_float(union VALUETYPE *p, const struct magic *m)
761 cvt_double(union VALUETYPE *p, const struct magic *m)
763 DO_CVT2(d, (double));
767 * Convert the byte order of the data we are looking at
768 * While we're here, let's apply the mask operation
769 * (unless you have a better idea)
772 mconvert(struct magic_set *ms, struct magic *m)
774 union VALUETYPE *p = &ms->ms_value;
794 case FILE_BESTRING16:
795 case FILE_LESTRING16: {
796 /* Null terminate and eat *trailing* return */
797 p->s[sizeof(p->s) - 1] = '\0';
799 /* Why? breaks magic numbers that end with \xa */
801 if (len-- && p->s[len] == '\n')
807 char *ptr1 = p->s, *ptr2 = ptr1 + 1;
809 if (len >= sizeof(p->s))
810 len = sizeof(p->s) - 1;
815 /* Why? breaks magic numbers that end with \xa */
817 if (len-- && p->s[len] == '\n')
823 p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
830 ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
837 (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
838 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
839 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
840 ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
844 p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
851 ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
858 (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
859 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
860 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
861 ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
868 ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
875 p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
876 ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
880 p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
881 ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
888 p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
889 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
890 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
891 ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
895 p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
896 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
897 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
898 ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
906 file_magerror(ms, "invalid type %d in mconvert()", m->type);
913 mdebug(uint32_t offset, const char *str, size_t len)
915 (void) fprintf(stderr, "mget @%d: ", offset);
916 file_showstr(stderr, str, len);
917 (void) fputc('\n', stderr);
918 (void) fputc('\n', stderr);
922 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
923 const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
926 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
927 * anything, but setup pointers into the source
932 ms->search.s = (const char *)s + offset;
933 ms->search.s_len = nbytes - offset;
934 ms->search.offset = offset;
940 const char *last; /* end of search region */
941 const char *buf; /* start of search region */
946 ms->search.s_len = 0;
950 buf = (const char *)s + offset;
951 end = last = (const char *)s + nbytes;
952 /* mget() guarantees buf <= last */
953 for (lines = linecnt, b = buf;
954 lines && ((b = memchr(c = b, '\n', end - b)) || (b = memchr(c, '\r', end - c)));
957 if (b[0] == '\r' && b[1] == '\n')
961 last = (const char *)s + nbytes;
964 ms->search.s_len = last - buf;
965 ms->search.offset = offset;
966 ms->search.rm_len = 0;
969 case FILE_BESTRING16:
970 case FILE_LESTRING16: {
971 const unsigned char *src = s + offset;
972 const unsigned char *esrc = s + nbytes;
974 char *edst = &p->s[sizeof(p->s) - 1];
976 if (type == FILE_BESTRING16)
979 /* check for pointer overflow */
981 file_magerror(ms, "invalid offset %u in mcopy()",
985 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
991 if (type == FILE_BESTRING16 ?
1000 case FILE_STRING: /* XXX - these two should not need */
1001 case FILE_PSTRING: /* to copy anything, but do anyway. */
1007 if (offset >= nbytes) {
1008 (void)memset(p, '\0', sizeof(*p));
1011 if (nbytes - offset < sizeof(*p))
1012 nbytes = nbytes - offset;
1014 nbytes = sizeof(*p);
1016 (void)memcpy(p, s + offset, nbytes);
1019 * the usefulness of padding with zeroes eludes me, it
1020 * might even cause problems
1022 if (nbytes < sizeof(*p))
1023 (void)memset(((char *)(void *)p) + nbytes, '\0',
1024 sizeof(*p) - nbytes);
1029 mget(struct magic_set *ms, const unsigned char *s,
1030 struct magic *m, size_t nbytes, unsigned int cont_level, int recursion_level)
1032 uint32_t offset = ms->offset;
1033 uint32_t count = m->str_range;
1034 union VALUETYPE *p = &ms->ms_value;
1036 if (recursion_level >= MAX_RECURSION_LEVEL) {
1037 file_error(ms, 0, "recursion nesting exceeded");
1041 if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
1044 if ((ms->flags & MAGIC_DEBUG) != 0) {
1045 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1046 #ifndef COMPILE_ONLY
1051 if (m->flag & INDIR) {
1052 int off = m->in_offset;
1053 if (m->in_op & FILE_OPINDIRECT) {
1054 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1055 ((const void *)(s + offset + off)));
1056 switch (m->in_type) {
1064 off = (short)((q->hs[0]<<8)|(q->hs[1]));
1067 off = (short)((q->hs[1]<<8)|(q->hs[0]));
1074 off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
1075 (q->hl[2]<<8)|(q->hl[3]));
1079 off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
1080 (q->hl[1]<<8)|(q->hl[0]));
1083 off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
1084 (q->hl[3]<<8)|(q->hl[2]));
1088 switch (m->in_type) {
1090 if (OFFSET_OOB(nbytes, offset, 1))
1093 switch (m->in_op & FILE_OPS_MASK) {
1095 offset = p->b & off;
1098 offset = p->b | off;
1101 offset = p->b ^ off;
1104 offset = p->b + off;
1107 offset = p->b - off;
1109 case FILE_OPMULTIPLY:
1110 offset = p->b * off;
1113 offset = p->b / off;
1116 offset = p->b % off;
1121 if (m->in_op & FILE_OPINVERSE)
1125 if (OFFSET_OOB(nbytes, offset, 2))
1128 switch (m->in_op & FILE_OPS_MASK) {
1130 offset = (short)((p->hs[0]<<8)|
1135 offset = (short)((p->hs[0]<<8)|
1140 offset = (short)((p->hs[0]<<8)|
1145 offset = (short)((p->hs[0]<<8)|
1150 offset = (short)((p->hs[0]<<8)|
1154 case FILE_OPMULTIPLY:
1155 offset = (short)((p->hs[0]<<8)|
1160 offset = (short)((p->hs[0]<<8)|
1165 offset = (short)((p->hs[0]<<8)|
1171 offset = (short)((p->hs[0]<<8)|
1173 if (m->in_op & FILE_OPINVERSE)
1177 if (OFFSET_OOB(nbytes, offset, 2))
1180 switch (m->in_op & FILE_OPS_MASK) {
1182 offset = (short)((p->hs[1]<<8)|
1187 offset = (short)((p->hs[1]<<8)|
1192 offset = (short)((p->hs[1]<<8)|
1197 offset = (short)((p->hs[1]<<8)|
1202 offset = (short)((p->hs[1]<<8)|
1206 case FILE_OPMULTIPLY:
1207 offset = (short)((p->hs[1]<<8)|
1212 offset = (short)((p->hs[1]<<8)|
1217 offset = (short)((p->hs[1]<<8)|
1223 offset = (short)((p->hs[1]<<8)|
1225 if (m->in_op & FILE_OPINVERSE)
1229 if (OFFSET_OOB(nbytes, offset, 2))
1232 switch (m->in_op & FILE_OPS_MASK) {
1234 offset = p->h & off;
1237 offset = p->h | off;
1240 offset = p->h ^ off;
1243 offset = p->h + off;
1246 offset = p->h - off;
1248 case FILE_OPMULTIPLY:
1249 offset = p->h * off;
1252 offset = p->h / off;
1255 offset = p->h % off;
1261 if (m->in_op & FILE_OPINVERSE)
1266 if (OFFSET_OOB(nbytes, offset, 4))
1269 switch (m->in_op & FILE_OPS_MASK) {
1271 offset = (int32_t)((p->hl[0]<<24)|
1278 offset = (int32_t)((p->hl[0]<<24)|
1285 offset = (int32_t)((p->hl[0]<<24)|
1292 offset = (int32_t)((p->hl[0]<<24)|
1299 offset = (int32_t)((p->hl[0]<<24)|
1305 case FILE_OPMULTIPLY:
1306 offset = (int32_t)((p->hl[0]<<24)|
1313 offset = (int32_t)((p->hl[0]<<24)|
1320 offset = (int32_t)((p->hl[0]<<24)|
1328 offset = (int32_t)((p->hl[0]<<24)|
1332 if (m->in_op & FILE_OPINVERSE)
1337 if (OFFSET_OOB(nbytes, offset, 4))
1340 switch (m->in_op & FILE_OPS_MASK) {
1342 offset = (int32_t)((p->hl[3]<<24)|
1349 offset = (int32_t)((p->hl[3]<<24)|
1356 offset = (int32_t)((p->hl[3]<<24)|
1363 offset = (int32_t)((p->hl[3]<<24)|
1370 offset = (int32_t)((p->hl[3]<<24)|
1376 case FILE_OPMULTIPLY:
1377 offset = (int32_t)((p->hl[3]<<24)|
1384 offset = (int32_t)((p->hl[3]<<24)|
1391 offset = (int32_t)((p->hl[3]<<24)|
1399 offset = (int32_t)((p->hl[3]<<24)|
1403 if (m->in_op & FILE_OPINVERSE)
1407 if (OFFSET_OOB(nbytes, offset, 4))
1410 switch (m->in_op & FILE_OPS_MASK) {
1412 offset = (int32_t)((p->hl[1]<<24)|
1419 offset = (int32_t)((p->hl[1]<<24)|
1426 offset = (int32_t)((p->hl[1]<<24)|
1433 offset = (int32_t)((p->hl[1]<<24)|
1440 offset = (int32_t)((p->hl[1]<<24)|
1446 case FILE_OPMULTIPLY:
1447 offset = (int32_t)((p->hl[1]<<24)|
1454 offset = (int32_t)((p->hl[1]<<24)|
1461 offset = (int32_t)((p->hl[1]<<24)|
1469 offset = (int32_t)((p->hl[1]<<24)|
1473 if (m->in_op & FILE_OPINVERSE)
1477 if (OFFSET_OOB(nbytes, offset, 4))
1480 switch (m->in_op & FILE_OPS_MASK) {
1482 offset = p->l & off;
1485 offset = p->l | off;
1488 offset = p->l ^ off;
1491 offset = p->l + off;
1494 offset = p->l - off;
1496 case FILE_OPMULTIPLY:
1497 offset = p->l * off;
1500 offset = p->l / off;
1503 offset = p->l % off;
1508 if (m->in_op & FILE_OPINVERSE)
1513 switch (m->in_type) {
1516 offset = ((((offset >> 0) & 0x7f) << 0) |
1517 (((offset >> 8) & 0x7f) << 7) |
1518 (((offset >> 16) & 0x7f) << 14) |
1519 (((offset >> 24) & 0x7f) << 21)) + 10;
1525 if (m->flag & INDIROFFADD) {
1526 offset += ms->c.li[cont_level-1].off;
1528 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
1530 ms->offset = offset;
1532 if ((ms->flags & MAGIC_DEBUG) != 0) {
1533 mdebug(offset, (char *)(void *)p,
1534 sizeof(union VALUETYPE));
1535 #ifndef COMPILE_ONLY
1541 /* Verify we have enough data to match magic type */
1544 if (OFFSET_OOB(nbytes, offset, 1))
1551 if (OFFSET_OOB(nbytes, offset, 2))
1570 if (OFFSET_OOB(nbytes, offset, 4))
1577 if (OFFSET_OOB(nbytes, offset, 8))
1584 if (OFFSET_OOB(nbytes, offset, m->vallen))
1589 if (OFFSET_OOB(nbytes, offset, 0))
1596 if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
1597 file_printf(ms, m->desc) == -1)
1599 if (OFFSET_OOB(nbytes, offset, 0))
1601 return file_softmagic(ms, s + offset, nbytes - offset,
1602 recursion_level, BINTEST);
1604 case FILE_DEFAULT: /* nothing to check */
1608 if (!mconvert(ms, m))
1614 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1617 * Convert the source args to unsigned here so that (1) the
1618 * compare will be unsigned as it is in strncmp() and (2) so
1619 * the ctype functions will work correctly without extra
1622 const unsigned char *a = (const unsigned char *)s1;
1623 const unsigned char *b = (const unsigned char *)s2;
1627 * What we want here is v = strncmp(s1, s2, len),
1628 * but ignoring any nulls.
1631 if (0L == flags) { /* normal string: do it fast */
1633 if ((v = *b++ - *a++) != '\0')
1636 else { /* combine the others */
1638 if ((flags & STRING_IGNORE_LOWERCASE) &&
1640 if ((v = tolower(*b++) - *a++) != '\0')
1643 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1645 if ((v = toupper(*b++) - *a++) != '\0')
1648 else if ((flags & STRING_COMPACT_BLANK) &&
1651 if (isspace(*b++)) {
1660 else if ((flags & STRING_COMPACT_OPTIONAL_BLANK) &&
1667 if ((v = *b++ - *a++) != '\0')
1676 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1679 * XXX - The 16-bit string compare probably needs to be done
1680 * differently, especially if the flags are to be supported.
1681 * At the moment, I am unsure.
1684 return file_strncmp(a, b, len, flags);
1688 magiccheck(struct magic_set *ms, struct magic *m)
1690 uint64_t l = m->value.q;
1695 union VALUETYPE *p = &ms->ms_value;
1763 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1797 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
1810 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1813 case FILE_BESTRING16:
1814 case FILE_LESTRING16:
1816 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1819 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1823 if (ms->search.s == NULL)
1826 slen = MIN(m->vallen, sizeof(m->value.s));
1830 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
1831 if (slen + idx > ms->search.s_len)
1834 v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
1835 if (v == 0) { /* found match */
1836 ms->search.offset += idx;
1847 if (ms->search.s == NULL)
1851 rc = regcomp(&rx, m->value.s,
1852 REG_EXTENDED|REG_NEWLINE|
1853 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
1855 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1856 file_magerror(ms, "regex error %d, (%s)",
1861 regmatch_t pmatch[1];
1862 #ifndef REG_STARTEND
1863 #define REG_STARTEND 0
1864 size_t l = ms->search.s_len - 1;
1865 char c = ms->search.s[l];
1866 ((char *)(intptr_t)ms->search.s)[l] = '\0';
1868 pmatch[0].rm_so = 0;
1869 pmatch[0].rm_eo = ms->search.s_len;
1871 rc = regexec(&rx, (const char *)ms->search.s,
1872 1, pmatch, REG_STARTEND);
1873 #if REG_STARTEND == 0
1874 ((char *)(intptr_t)ms->search.s)[l] = c;
1878 ms->search.s += (int)pmatch[0].rm_so;
1879 ms->search.offset += (size_t)pmatch[0].rm_so;
1881 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
1890 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1891 file_magerror(ms, "regexec error %d, (%s)",
1898 if (v == (uint64_t)-1)
1905 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
1909 v = file_signextend(ms, m, v);
1913 if ((ms->flags & MAGIC_DEBUG) != 0)
1914 (void) fprintf(stderr, "%llu == *any* = 1\n",
1915 (unsigned long long)v);
1921 if ((ms->flags & MAGIC_DEBUG) != 0)
1922 (void) fprintf(stderr, "%llu != %llu = %d\n",
1923 (unsigned long long)v, (unsigned long long)l,
1929 if ((ms->flags & MAGIC_DEBUG) != 0)
1930 (void) fprintf(stderr, "%llu == %llu = %d\n",
1931 (unsigned long long)v, (unsigned long long)l,
1936 if (m->flag & UNSIGNED) {
1938 if ((ms->flags & MAGIC_DEBUG) != 0)
1939 (void) fprintf(stderr, "%llu > %llu = %d\n",
1940 (unsigned long long)v,
1941 (unsigned long long)l, matched);
1944 matched = (int64_t) v > (int64_t) l;
1945 if ((ms->flags & MAGIC_DEBUG) != 0)
1946 (void) fprintf(stderr, "%lld > %lld = %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, "%llu < %llu = %d\n",
1956 (unsigned long long)v,
1957 (unsigned long long)l, matched);
1960 matched = (int64_t) v < (int64_t) l;
1961 if ((ms->flags & MAGIC_DEBUG) != 0)
1962 (void) fprintf(stderr, "%lld < %lld = %d\n",
1963 (long long)v, (long long)l, matched);
1968 matched = (v & l) == l;
1969 if ((ms->flags & MAGIC_DEBUG) != 0)
1970 (void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n",
1971 (unsigned long long)v, (unsigned long long)l,
1972 (unsigned long long)l, matched);
1976 matched = (v & l) != l;
1977 if ((ms->flags & MAGIC_DEBUG) != 0)
1978 (void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n",
1979 (unsigned long long)v, (unsigned long long)l,
1980 (unsigned long long)l, matched);
1985 file_magerror(ms, "cannot happen: invalid relation `%c'",
1994 handle_annotation(struct magic_set *ms, struct magic *m)
1996 if (ms->flags & MAGIC_APPLE) {
1997 if (file_printf(ms, "%.8s", m->apple) == -1)
2001 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2002 if (file_printf(ms, "%s", m->mimetype) == -1)
2010 print_sep(struct magic_set *ms, int firstline)
2012 if (ms->flags & MAGIC_MIME)
2017 * we found another match
2018 * put a newline and '-' to do some simple formatting
2020 return file_printf(ms, "\n- ");