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
41 FILE_RCSID("@(#)$File: softmagic.c,v 1.120 2008/07/28 17:25:21 christos Exp $")
44 private int match(struct magic_set *, struct magic *, uint32_t,
45 const unsigned char *, size_t, int);
46 private int mget(struct magic_set *, const unsigned char *,
47 struct magic *, size_t, unsigned int);
48 private int magiccheck(struct magic_set *, struct magic *);
49 private int32_t mprint(struct magic_set *, struct magic *);
50 private void mdebug(uint32_t, const char *, size_t);
51 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
52 const unsigned char *, uint32_t, size_t, size_t);
53 private int mconvert(struct magic_set *, struct magic *);
54 private int print_sep(struct magic_set *, int);
55 private void cvt_8(union VALUETYPE *, const struct magic *);
56 private void cvt_16(union VALUETYPE *, const struct magic *);
57 private void cvt_32(union VALUETYPE *, const struct magic *);
58 private void cvt_64(union VALUETYPE *, const struct magic *);
61 * Macro to give description string according to whether we want plain
64 #define MAGIC_DESC ((ms->flags & MAGIC_MIME) ? m->mimetype : m->desc)
67 * softmagic - lookup one file in parsed, in-memory copy of database
68 * Passed the name and FILE * of one file to be typed.
70 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
72 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, int mode)
76 for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
77 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode)) != 0)
84 * Go through the whole list, stopping if you find a match. Process all
85 * the continuations of that match before returning.
87 * We support multi-level continuations:
89 * At any time when processing a successful top-level match, there is a
90 * current continuation level; it represents the level of the last
91 * successfully matched continuation.
93 * Continuations above that level are skipped as, if we see one, it
94 * means that the continuation that controls them - i.e, the
95 * lower-level continuation preceding them - failed to match.
97 * Continuations below that level are processed as, if we see one,
98 * it means we've finished processing or skipping higher-level
99 * continuations under the control of a successful or unsuccessful
100 * lower-level continuation, and are now seeing the next lower-level
101 * continuation and should process it. The current continuation
102 * level reverts to the level of the one we're seeing.
104 * Continuations at the current level are processed as, if we see
105 * one, there's no lower-level continuation that may have failed.
107 * If a continuation matches, we bump the current continuation level
108 * so that higher-level continuations are processed.
111 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
112 const unsigned char *s, size_t nbytes, int mode)
114 uint32_t magindex = 0;
115 unsigned int cont_level = 0;
116 int need_separator = 0;
117 int returnval = 0; /* if a match is found it is set to 1*/
118 int firstline = 1; /* a flag to print X\n X\n- X */
119 int printed_something = 0;
121 if (file_check_mem(ms, cont_level) == -1)
124 for (magindex = 0; magindex < nmagic; magindex++) {
126 struct magic *m = &magic[magindex];
128 if ((m->flag & BINTEST) != mode) {
130 while (magic[magindex + 1].cont_level != 0 &&
133 continue; /* Skip to next top-level test*/
136 ms->offset = m->offset;
137 ms->line = m->lineno;
139 /* if main entry matches, print it... */
140 flush = !mget(ms, s, m, nbytes, cont_level);
145 switch (magiccheck(ms, m)) {
157 * main entry didn't match,
158 * flush its continuations
160 while (magindex < nmagic - 1 &&
161 magic[magindex + 1].cont_level != 0)
167 * If we are going to print something, we'll need to print
168 * a blank before we print something else.
172 printed_something = 1;
173 if (print_sep(ms, firstline) == -1)
177 if ((ms->c.li[cont_level].off = mprint(ms, m)) == -1)
180 /* and any continuations that match */
181 if (file_check_mem(ms, ++cont_level) == -1)
184 while (magic[magindex+1].cont_level != 0 &&
185 ++magindex < nmagic) {
186 m = &magic[magindex];
187 ms->line = m->lineno; /* for messages */
189 if (cont_level < m->cont_level)
191 if (cont_level > m->cont_level) {
193 * We're at the end of the level
194 * "cont_level" continuations.
196 cont_level = m->cont_level;
198 ms->offset = m->offset;
199 if (m->flag & OFFADD) {
201 ms->c.li[cont_level - 1].off;
204 #ifdef ENABLE_CONDITIONALS
205 if (m->cond == COND_ELSE ||
206 m->cond == COND_ELIF) {
207 if (ms->c.li[cont_level].last_match == 1)
211 flush = !mget(ms, s, m, nbytes, cont_level);
212 if (flush && m->reln != '!')
215 switch (flush ? 1 : magiccheck(ms, m)) {
219 #ifdef ENABLE_CONDITIONALS
220 ms->c.li[cont_level].last_match = 0;
224 #ifdef ENABLE_CONDITIONALS
225 ms->c.li[cont_level].last_match = 1;
227 if (m->type != FILE_DEFAULT)
228 ms->c.li[cont_level].got_match = 1;
229 else if (ms->c.li[cont_level].got_match) {
230 ms->c.li[cont_level].got_match = 0;
234 * If we are going to print something,
235 * make sure that we have a separator first.
238 printed_something = 1;
239 if (print_sep(ms, firstline) == -1)
243 * This continuation matched. Print
244 * its message, with a blank before it
245 * if the previous item printed and
246 * this item isn't empty.
248 /* space if previous printed */
250 && ((m->flag & NOSPACE) == 0)
252 if (file_printf(ms, " ") == -1)
256 if ((ms->c.li[cont_level].off = mprint(ms, m)) == -1)
262 * If we see any continuations
266 if (file_check_mem(ms, ++cont_level) == -1)
271 if (printed_something) {
275 if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
276 return 1; /* don't keep searching */
279 return returnval; /* This is hit if -k is set or there is no match */
283 check_fmt(struct magic_set *ms, struct magic *m)
288 if (strchr(MAGIC_DESC, '%') == NULL)
291 rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
294 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
295 file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
298 rc = regexec(&rx, MAGIC_DESC, 0, 0, 0);
305 char * strndup(const char *, size_t);
308 strndup(const char *str, size_t n)
316 if (!(copy = malloc(len + 1)))
318 (void) memcpy(copy, str, len + 1);
322 #endif /* HAVE_STRNDUP */
325 mprint(struct magic_set *ms, struct magic *m)
332 union VALUETYPE *p = &ms->ms_value;
336 v = file_signextend(ms, m, (uint64_t)p->b);
337 switch (check_fmt(ms, m)) {
341 if (asprintf(&buf, "%c", (unsigned char)v) < 0)
343 if (file_printf(ms, MAGIC_DESC, buf) == -1)
347 if (file_printf(ms, MAGIC_DESC, (unsigned char) v) == -1)
351 t = ms->offset + sizeof(char);
357 v = file_signextend(ms, m, (uint64_t)p->h);
358 switch (check_fmt(ms, m)) {
362 if (asprintf(&buf, "%hu", (unsigned short)v) < 0)
364 if (file_printf(ms, MAGIC_DESC, buf) == -1)
368 if (file_printf(ms, MAGIC_DESC, (unsigned short) v) == -1)
372 t = ms->offset + sizeof(short);
379 v = file_signextend(ms, m, (uint64_t)p->l);
380 switch (check_fmt(ms, m)) {
384 if (asprintf(&buf, "%u", (uint32_t)v) < 0)
386 if (file_printf(ms, MAGIC_DESC, buf) == -1)
390 if (file_printf(ms, MAGIC_DESC, (uint32_t) v) == -1)
394 t = ms->offset + sizeof(int32_t);
400 v = file_signextend(ms, m, p->q);
401 if (file_printf(ms, MAGIC_DESC, (uint64_t) v) == -1)
403 t = ms->offset + sizeof(int64_t);
408 case FILE_BESTRING16:
409 case FILE_LESTRING16:
410 if (m->reln == '=' || m->reln == '!') {
411 if (file_printf(ms, MAGIC_DESC, m->value.s) == -1)
413 t = ms->offset + m->vallen;
416 if (*m->value.s == '\0')
417 p->s[strcspn(p->s, "\n")] = '\0';
418 if (file_printf(ms, MAGIC_DESC, p->s) == -1)
420 t = ms->offset + strlen(p->s);
421 if (m->type == FILE_PSTRING)
430 if (file_printf(ms, MAGIC_DESC, file_fmttime(p->l, 1)) == -1)
432 t = ms->offset + sizeof(time_t);
439 if (file_printf(ms, MAGIC_DESC, file_fmttime(p->l, 0)) == -1)
441 t = ms->offset + sizeof(time_t);
447 if (file_printf(ms, MAGIC_DESC, file_fmttime((uint32_t)p->q, 1))
450 t = ms->offset + sizeof(uint64_t);
456 if (file_printf(ms, MAGIC_DESC, file_fmttime((uint32_t)p->q, 0))
459 t = ms->offset + sizeof(uint64_t);
466 switch (check_fmt(ms, m)) {
470 if (asprintf(&buf, "%g", vf) < 0)
472 if (file_printf(ms, MAGIC_DESC, buf) == -1)
476 if (file_printf(ms, MAGIC_DESC, vf) == -1)
480 t = ms->offset + sizeof(float);
487 switch (check_fmt(ms, m)) {
491 if (asprintf(&buf, "%g", vd) < 0)
493 if (file_printf(ms, MAGIC_DESC, buf) == -1)
497 if (file_printf(ms, MAGIC_DESC, vd) == -1)
501 t = ms->offset + sizeof(double);
508 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
510 file_oomem(ms, ms->search.rm_len);
513 rval = file_printf(ms, MAGIC_DESC, cp);
519 if ((m->str_flags & REGEX_OFFSET_START))
520 t = ms->search.offset;
522 t = ms->search.offset + ms->search.rm_len;
527 if (file_printf(ms, MAGIC_DESC, m->value.s) == -1)
529 if ((m->str_flags & REGEX_OFFSET_START))
530 t = ms->search.offset;
532 t = ms->search.offset + m->vallen;
536 if (file_printf(ms, MAGIC_DESC, m->value.s) == -1)
542 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
549 #define DO_CVT(fld, cast) \
551 switch (m->mask_op & FILE_OPS_MASK) { \
553 p->fld &= cast m->num_mask; \
556 p->fld |= cast m->num_mask; \
559 p->fld ^= cast m->num_mask; \
562 p->fld += cast m->num_mask; \
565 p->fld -= cast m->num_mask; \
567 case FILE_OPMULTIPLY: \
568 p->fld *= cast m->num_mask; \
570 case FILE_OPDIVIDE: \
571 p->fld /= cast m->num_mask; \
573 case FILE_OPMODULO: \
574 p->fld %= cast m->num_mask; \
577 if (m->mask_op & FILE_OPINVERSE) \
581 cvt_8(union VALUETYPE *p, const struct magic *m)
583 DO_CVT(b, (uint8_t));
587 cvt_16(union VALUETYPE *p, const struct magic *m)
589 DO_CVT(h, (uint16_t));
593 cvt_32(union VALUETYPE *p, const struct magic *m)
595 DO_CVT(l, (uint32_t));
599 cvt_64(union VALUETYPE *p, const struct magic *m)
601 DO_CVT(q, (uint64_t));
604 #define DO_CVT2(fld, cast) \
606 switch (m->mask_op & FILE_OPS_MASK) { \
608 p->fld += cast m->num_mask; \
611 p->fld -= cast m->num_mask; \
613 case FILE_OPMULTIPLY: \
614 p->fld *= cast m->num_mask; \
616 case FILE_OPDIVIDE: \
617 p->fld /= cast m->num_mask; \
622 cvt_float(union VALUETYPE *p, const struct magic *m)
628 cvt_double(union VALUETYPE *p, const struct magic *m)
630 DO_CVT2(d, (double));
634 * Convert the byte order of the data we are looking at
635 * While we're here, let's apply the mask operation
636 * (unless you have a better idea)
639 mconvert(struct magic_set *ms, struct magic *m)
641 union VALUETYPE *p = &ms->ms_value;
661 case FILE_BESTRING16:
662 case FILE_LESTRING16: {
663 /* Null terminate and eat *trailing* return */
664 p->s[sizeof(p->s) - 1] = '\0';
666 /* Why? breaks magic numbers that end with \xa */
668 if (len-- && p->s[len] == '\n')
674 char *ptr1 = p->s, *ptr2 = ptr1 + 1;
676 if (len >= sizeof(p->s))
677 len = sizeof(p->s) - 1;
682 /* Why? breaks magic numbers that end with \xa */
684 if (len-- && p->s[len] == '\n')
690 p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
697 ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
704 (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
705 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
706 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
707 ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
711 p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
718 ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
725 (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
726 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
727 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
728 ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
735 ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
742 p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
743 ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
747 p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
748 ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
755 p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
756 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
757 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
758 ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
762 p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
763 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
764 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
765 ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
773 file_magerror(ms, "invalid type %d in mconvert()", m->type);
780 mdebug(uint32_t offset, const char *str, size_t len)
782 (void) fprintf(stderr, "mget @%d: ", offset);
783 file_showstr(stderr, str, len);
784 (void) fputc('\n', stderr);
785 (void) fputc('\n', stderr);
789 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
790 const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
793 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
794 * anything, but setup pointers into the source
799 ms->search.s = (const char *)s + offset;
800 ms->search.s_len = nbytes - offset;
801 ms->search.offset = offset;
807 const char *last; /* end of search region */
808 const char *buf; /* start of search region */
812 ms->search.s_len = 0;
816 buf = (const char *)s + offset;
817 last = (const char *)s + nbytes;
818 /* mget() guarantees buf <= last */
819 for (lines = linecnt, b = buf;
820 lines && ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
823 if (b[0] == '\r' && b[1] == '\n')
827 last = (const char *)s + nbytes;
830 ms->search.s_len = last - buf;
831 ms->search.offset = offset;
832 ms->search.rm_len = 0;
835 case FILE_BESTRING16:
836 case FILE_LESTRING16: {
837 const unsigned char *src = s + offset;
838 const unsigned char *esrc = s + nbytes;
840 char *edst = &p->s[sizeof(p->s) - 1];
842 if (type == FILE_BESTRING16)
845 /* check for pointer overflow */
847 file_magerror(ms, "invalid offset %zu in mcopy()",
851 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
857 if (type == FILE_BESTRING16 ?
866 case FILE_STRING: /* XXX - these two should not need */
867 case FILE_PSTRING: /* to copy anything, but do anyway. */
873 if (offset >= nbytes) {
874 (void)memset(p, '\0', sizeof(*p));
877 if (nbytes - offset < sizeof(*p))
878 nbytes = nbytes - offset;
882 (void)memcpy(p, s + offset, nbytes);
885 * the usefulness of padding with zeroes eludes me, it
886 * might even cause problems
888 if (nbytes < sizeof(*p))
889 (void)memset(((char *)(void *)p) + nbytes, '\0',
890 sizeof(*p) - nbytes);
895 mget(struct magic_set *ms, const unsigned char *s,
896 struct magic *m, size_t nbytes, unsigned int cont_level)
898 uint32_t offset = ms->offset;
899 uint32_t count = m->str_range;
900 union VALUETYPE *p = &ms->ms_value;
902 if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
905 if ((ms->flags & MAGIC_DEBUG) != 0) {
906 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
910 if (m->flag & INDIR) {
911 int off = m->in_offset;
912 if (m->in_op & FILE_OPINDIRECT) {
913 const union VALUETYPE *q = CAST(const union VALUETYPE *,
914 ((const void *)(s + offset + off)));
915 switch (m->in_type) {
923 off = (short)((q->hs[0]<<8)|(q->hs[1]));
926 off = (short)((q->hs[1]<<8)|(q->hs[0]));
932 off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
933 (q->hl[2]<<8)|(q->hl[3]));
936 off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
937 (q->hl[1]<<8)|(q->hl[0]));
940 off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
941 (q->hl[3]<<8)|(q->hl[2]));
945 switch (m->in_type) {
947 if (nbytes < (offset + 1))
950 switch (m->in_op & FILE_OPS_MASK) {
966 case FILE_OPMULTIPLY:
978 if (m->in_op & FILE_OPINVERSE)
982 if (nbytes < (offset + 2))
985 switch (m->in_op & FILE_OPS_MASK) {
987 offset = (short)((p->hs[0]<<8)|
992 offset = (short)((p->hs[0]<<8)|
997 offset = (short)((p->hs[0]<<8)|
1002 offset = (short)((p->hs[0]<<8)|
1007 offset = (short)((p->hs[0]<<8)|
1011 case FILE_OPMULTIPLY:
1012 offset = (short)((p->hs[0]<<8)|
1017 offset = (short)((p->hs[0]<<8)|
1022 offset = (short)((p->hs[0]<<8)|
1028 offset = (short)((p->hs[0]<<8)|
1030 if (m->in_op & FILE_OPINVERSE)
1034 if (nbytes < (offset + 2))
1037 switch (m->in_op & FILE_OPS_MASK) {
1039 offset = (short)((p->hs[1]<<8)|
1044 offset = (short)((p->hs[1]<<8)|
1049 offset = (short)((p->hs[1]<<8)|
1054 offset = (short)((p->hs[1]<<8)|
1059 offset = (short)((p->hs[1]<<8)|
1063 case FILE_OPMULTIPLY:
1064 offset = (short)((p->hs[1]<<8)|
1069 offset = (short)((p->hs[1]<<8)|
1074 offset = (short)((p->hs[1]<<8)|
1080 offset = (short)((p->hs[1]<<8)|
1082 if (m->in_op & FILE_OPINVERSE)
1086 if (nbytes < (offset + 2))
1089 switch (m->in_op & FILE_OPS_MASK) {
1091 offset = p->h & off;
1094 offset = p->h | off;
1097 offset = p->h ^ off;
1100 offset = p->h + off;
1103 offset = p->h - off;
1105 case FILE_OPMULTIPLY:
1106 offset = p->h * off;
1109 offset = p->h / off;
1112 offset = p->h % off;
1118 if (m->in_op & FILE_OPINVERSE)
1122 if (nbytes < (offset + 4))
1125 switch (m->in_op & FILE_OPS_MASK) {
1127 offset = (int32_t)((p->hl[0]<<24)|
1134 offset = (int32_t)((p->hl[0]<<24)|
1141 offset = (int32_t)((p->hl[0]<<24)|
1148 offset = (int32_t)((p->hl[0]<<24)|
1155 offset = (int32_t)((p->hl[0]<<24)|
1161 case FILE_OPMULTIPLY:
1162 offset = (int32_t)((p->hl[0]<<24)|
1169 offset = (int32_t)((p->hl[0]<<24)|
1176 offset = (int32_t)((p->hl[0]<<24)|
1184 offset = (int32_t)((p->hl[0]<<24)|
1188 if (m->in_op & FILE_OPINVERSE)
1192 if (nbytes < (offset + 4))
1195 switch (m->in_op & FILE_OPS_MASK) {
1197 offset = (int32_t)((p->hl[3]<<24)|
1204 offset = (int32_t)((p->hl[3]<<24)|
1211 offset = (int32_t)((p->hl[3]<<24)|
1218 offset = (int32_t)((p->hl[3]<<24)|
1225 offset = (int32_t)((p->hl[3]<<24)|
1231 case FILE_OPMULTIPLY:
1232 offset = (int32_t)((p->hl[3]<<24)|
1239 offset = (int32_t)((p->hl[3]<<24)|
1246 offset = (int32_t)((p->hl[3]<<24)|
1254 offset = (int32_t)((p->hl[3]<<24)|
1258 if (m->in_op & FILE_OPINVERSE)
1262 if (nbytes < (offset + 4))
1265 switch (m->in_op & FILE_OPS_MASK) {
1267 offset = (int32_t)((p->hl[1]<<24)|
1274 offset = (int32_t)((p->hl[1]<<24)|
1281 offset = (int32_t)((p->hl[1]<<24)|
1288 offset = (int32_t)((p->hl[1]<<24)|
1295 offset = (int32_t)((p->hl[1]<<24)|
1301 case FILE_OPMULTIPLY:
1302 offset = (int32_t)((p->hl[1]<<24)|
1309 offset = (int32_t)((p->hl[1]<<24)|
1316 offset = (int32_t)((p->hl[1]<<24)|
1324 offset = (int32_t)((p->hl[1]<<24)|
1328 if (m->in_op & FILE_OPINVERSE)
1332 if (nbytes < (offset + 4))
1335 switch (m->in_op & FILE_OPS_MASK) {
1337 offset = p->l & off;
1340 offset = p->l | off;
1343 offset = p->l ^ off;
1346 offset = p->l + off;
1349 offset = p->l - off;
1351 case FILE_OPMULTIPLY:
1352 offset = p->l * off;
1355 offset = p->l / off;
1358 offset = p->l % off;
1363 if (m->in_op & FILE_OPINVERSE)
1368 if (m->flag & INDIROFFADD)
1369 offset += ms->c.li[cont_level-1].off;
1370 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
1372 ms->offset = offset;
1374 if ((ms->flags & MAGIC_DEBUG) != 0) {
1375 mdebug(offset, (char *)(void *)p,
1376 sizeof(union VALUETYPE));
1381 /* Verify we have enough data to match magic type */
1384 if (nbytes < (offset + 1)) /* should alway be true */
1391 if (nbytes < (offset + 2))
1410 if (nbytes < (offset + 4))
1417 if (nbytes < (offset + 8))
1424 if (nbytes < (offset + m->vallen))
1429 if (nbytes < offset)
1433 case FILE_DEFAULT: /* nothing to check */
1437 if (!mconvert(ms, m))
1443 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1446 * Convert the source args to unsigned here so that (1) the
1447 * compare will be unsigned as it is in strncmp() and (2) so
1448 * the ctype functions will work correctly without extra
1451 const unsigned char *a = (const unsigned char *)s1;
1452 const unsigned char *b = (const unsigned char *)s2;
1456 * What we want here is v = strncmp(s1, s2, len),
1457 * but ignoring any nulls.
1460 if (0L == flags) { /* normal string: do it fast */
1462 if ((v = *b++ - *a++) != '\0')
1465 else { /* combine the others */
1467 if ((flags & STRING_IGNORE_LOWERCASE) &&
1469 if ((v = tolower(*b++) - *a++) != '\0')
1472 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1474 if ((v = toupper(*b++) - *a++) != '\0')
1477 else if ((flags & STRING_COMPACT_BLANK) &&
1480 if (isspace(*b++)) {
1489 else if ((flags & STRING_COMPACT_OPTIONAL_BLANK) &&
1496 if ((v = *b++ - *a++) != '\0')
1505 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1508 * XXX - The 16-bit string compare probably needs to be done
1509 * differently, especially if the flags are to be supported.
1510 * At the moment, I am unsure.
1513 return file_strncmp(a, b, len, flags);
1517 magiccheck(struct magic_set *ms, struct magic *m)
1519 uint64_t l = m->value.q;
1524 union VALUETYPE *p = &ms->ms_value;
1592 file_magerror(ms, "cannot happen with float: invalid relation `%c'", m->reln);
1625 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
1638 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1641 case FILE_BESTRING16:
1642 case FILE_LESTRING16:
1644 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1647 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1651 if (ms->search.s == NULL)
1654 slen = MIN(m->vallen, sizeof(m->value.s));
1658 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
1659 if (slen + idx > ms->search.s_len)
1662 v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
1663 if (v == 0) { /* found match */
1664 ms->search.offset += idx;
1675 if (ms->search.s == NULL)
1679 rc = regcomp(&rx, m->value.s,
1680 REG_EXTENDED|REG_NEWLINE|
1681 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
1683 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1684 file_magerror(ms, "regex error %d, (%s)",
1689 regmatch_t pmatch[1];
1690 #ifndef REG_STARTEND
1691 #define REG_STARTEND 0
1692 size_t l = ms->search.s_len - 1;
1693 char c = ms->search.s[l];
1694 ((char *)(intptr_t)ms->search.s)[l] = '\0';
1696 pmatch[0].rm_so = 0;
1697 pmatch[0].rm_eo = ms->search.s_len;
1699 rc = regexec(&rx, (const char *)ms->search.s,
1700 1, pmatch, REG_STARTEND);
1701 #if REG_STARTEND == 0
1702 ((char *)(intptr_t)ms->search.s)[l] = c;
1706 ms->search.s += (int)pmatch[0].rm_so;
1707 ms->search.offset += (size_t)pmatch[0].rm_so;
1709 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
1718 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1719 file_magerror(ms, "regexec error %d, (%s)",
1726 if (v == (uint64_t)-1)
1731 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
1735 v = file_signextend(ms, m, v);
1739 if ((ms->flags & MAGIC_DEBUG) != 0)
1740 (void) fprintf(stderr, "%llu == *any* = 1\n",
1741 (unsigned long long)v);
1747 if ((ms->flags & MAGIC_DEBUG) != 0)
1748 (void) fprintf(stderr, "%llu != %llu = %d\n",
1749 (unsigned long long)v, (unsigned long long)l,
1755 if ((ms->flags & MAGIC_DEBUG) != 0)
1756 (void) fprintf(stderr, "%llu == %llu = %d\n",
1757 (unsigned long long)v, (unsigned long long)l,
1762 if (m->flag & UNSIGNED) {
1764 if ((ms->flags & MAGIC_DEBUG) != 0)
1765 (void) fprintf(stderr, "%llu > %llu = %d\n",
1766 (unsigned long long)v,
1767 (unsigned long long)l, matched);
1770 matched = (int64_t) v > (int64_t) l;
1771 if ((ms->flags & MAGIC_DEBUG) != 0)
1772 (void) fprintf(stderr, "%lld > %lld = %d\n",
1773 (long long)v, (long long)l, matched);
1778 if (m->flag & UNSIGNED) {
1780 if ((ms->flags & MAGIC_DEBUG) != 0)
1781 (void) fprintf(stderr, "%llu < %llu = %d\n",
1782 (unsigned long long)v,
1783 (unsigned long long)l, matched);
1786 matched = (int64_t) v < (int64_t) l;
1787 if ((ms->flags & MAGIC_DEBUG) != 0)
1788 (void) fprintf(stderr, "%lld < %lld = %d\n",
1789 (long long)v, (long long)l, matched);
1794 matched = (v & l) == l;
1795 if ((ms->flags & MAGIC_DEBUG) != 0)
1796 (void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n",
1797 (unsigned long long)v, (unsigned long long)l,
1798 (unsigned long long)l, matched);
1802 matched = (v & l) != l;
1803 if ((ms->flags & MAGIC_DEBUG) != 0)
1804 (void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n",
1805 (unsigned long long)v, (unsigned long long)l,
1806 (unsigned long long)l, matched);
1811 file_magerror(ms, "cannot happen: invalid relation `%c'",
1820 print_sep(struct magic_set *ms, int firstline)
1825 * we found another match
1826 * put a newline and '-' to do some simple formatting
1828 return file_printf(ms, "\n- ");