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.144 2011/01/07 23:22:28 rrt Exp $")
45 private int match(struct magic_set *, struct magic *, uint32_t,
46 const unsigned char *, size_t, int);
47 private int mget(struct magic_set *, const unsigned char *,
48 struct magic *, size_t, unsigned 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 *);
64 * softmagic - lookup one file in parsed, in-memory copy of database
65 * Passed the name and FILE * of one file to be typed.
67 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
69 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, int mode)
73 for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
74 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode)) != 0)
81 * Go through the whole list, stopping if you find a match. Process all
82 * the continuations of that match before returning.
84 * We support multi-level continuations:
86 * At any time when processing a successful top-level match, there is a
87 * current continuation level; it represents the level of the last
88 * successfully matched continuation.
90 * Continuations above that level are skipped as, if we see one, it
91 * means that the continuation that controls them - i.e, the
92 * lower-level continuation preceding them - failed to match.
94 * Continuations below that level are processed as, if we see one,
95 * it means we've finished processing or skipping higher-level
96 * continuations under the control of a successful or unsuccessful
97 * lower-level continuation, and are now seeing the next lower-level
98 * continuation and should process it. The current continuation
99 * level reverts to the level of the one we're seeing.
101 * Continuations at the current level are processed as, if we see
102 * one, there's no lower-level continuation that may have failed.
104 * If a continuation matches, we bump the current continuation level
105 * so that higher-level continuations are processed.
108 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
109 const unsigned char *s, size_t nbytes, int mode)
111 uint32_t magindex = 0;
112 unsigned int cont_level = 0;
113 int need_separator = 0;
114 int returnval = 0, e; /* if a match is found it is set to 1*/
115 int firstline = 1; /* a flag to print X\n X\n- X */
116 int printed_something = 0;
117 int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
119 if (file_check_mem(ms, cont_level) == -1)
122 for (magindex = 0; magindex < nmagic; magindex++) {
124 struct magic *m = &magic[magindex];
126 if ((m->flag & mode) != mode) {
128 while (magic[magindex + 1].cont_level != 0 &&
131 continue; /* Skip to next top-level test*/
134 ms->offset = m->offset;
135 ms->line = m->lineno;
137 /* if main entry matches, print it... */
138 switch (mget(ms, s, m, nbytes, cont_level)) {
142 flush = m->reln != '!';
145 if (m->type == FILE_INDIRECT)
148 switch (magiccheck(ms, m)) {
162 * main entry didn't match,
163 * flush its continuations
165 while (magindex < nmagic - 1 &&
166 magic[magindex + 1].cont_level != 0)
171 if ((e = handle_annotation(ms, m)) != 0)
174 * If we are going to print something, we'll need to print
175 * a blank before we print something else.
179 printed_something = 1;
180 if (print_sep(ms, firstline) == -1)
185 if (print && mprint(ms, m) == -1)
188 ms->c.li[cont_level].off = moffset(ms, m);
190 /* and any continuations that match */
191 if (file_check_mem(ms, ++cont_level) == -1)
194 while (magic[magindex+1].cont_level != 0 &&
195 ++magindex < nmagic) {
196 m = &magic[magindex];
197 ms->line = m->lineno; /* for messages */
199 if (cont_level < m->cont_level)
201 if (cont_level > m->cont_level) {
203 * We're at the end of the level
204 * "cont_level" continuations.
206 cont_level = m->cont_level;
208 ms->offset = m->offset;
209 if (m->flag & OFFADD) {
211 ms->c.li[cont_level - 1].off;
214 #ifdef ENABLE_CONDITIONALS
215 if (m->cond == COND_ELSE ||
216 m->cond == COND_ELIF) {
217 if (ms->c.li[cont_level].last_match == 1)
221 switch (mget(ms, s, m, nbytes, cont_level)) {
230 if (m->type == FILE_INDIRECT)
236 switch (flush ? 1 : magiccheck(ms, m)) {
240 #ifdef ENABLE_CONDITIONALS
241 ms->c.li[cont_level].last_match = 0;
245 #ifdef ENABLE_CONDITIONALS
246 ms->c.li[cont_level].last_match = 1;
248 if (m->type != FILE_DEFAULT)
249 ms->c.li[cont_level].got_match = 1;
250 else if (ms->c.li[cont_level].got_match) {
251 ms->c.li[cont_level].got_match = 0;
254 if ((e = handle_annotation(ms, m)) != 0)
257 * If we are going to print something,
258 * make sure that we have a separator first.
261 if (!printed_something) {
262 printed_something = 1;
263 if (print_sep(ms, firstline)
269 * This continuation matched. Print
270 * its message, with a blank before it
271 * if the previous item printed and
272 * this item isn't empty.
274 /* space if previous printed */
276 && ((m->flag & NOSPACE) == 0)
279 file_printf(ms, " ") == -1)
283 if (print && mprint(ms, m) == -1)
286 ms->c.li[cont_level].off = moffset(ms, m);
292 * If we see any continuations
296 if (file_check_mem(ms, ++cont_level) == -1)
301 if (printed_something) {
306 if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
307 return returnval; /* don't keep searching */
310 return returnval; /* This is hit if -k is set or there is no match */
314 check_fmt(struct magic_set *ms, struct magic *m)
319 if (strchr(m->desc, '%') == NULL)
322 rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
325 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
326 file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
329 rc = regexec(&rx, m->desc, 0, 0, 0);
336 char * strndup(const char *, size_t);
339 strndup(const char *str, size_t n)
344 for (len = 0; len < n && str[len]; len++)
346 if ((copy = malloc(len + 1)) == NULL)
348 (void)memcpy(copy, str, len);
352 #endif /* HAVE_STRNDUP */
355 mprint(struct magic_set *ms, struct magic *m)
362 union VALUETYPE *p = &ms->ms_value;
366 v = file_signextend(ms, m, (uint64_t)p->b);
367 switch (check_fmt(ms, m)) {
371 (void)snprintf(buf, sizeof(buf), "%c",
373 if (file_printf(ms, m->desc, buf) == -1)
377 if (file_printf(ms, m->desc, (unsigned char) v) == -1)
381 t = ms->offset + sizeof(char);
387 v = file_signextend(ms, m, (uint64_t)p->h);
388 switch (check_fmt(ms, m)) {
392 (void)snprintf(buf, sizeof(buf), "%hu",
394 if (file_printf(ms, m->desc, buf) == -1)
399 file_printf(ms, m->desc, (unsigned short) v) == -1)
403 t = ms->offset + sizeof(short);
410 v = file_signextend(ms, m, (uint64_t)p->l);
411 switch (check_fmt(ms, m)) {
415 (void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v);
416 if (file_printf(ms, m->desc, buf) == -1)
420 if (file_printf(ms, m->desc, (uint32_t) v) == -1)
424 t = ms->offset + sizeof(int32_t);
430 v = file_signextend(ms, m, p->q);
431 if (file_printf(ms, m->desc, (uint64_t) v) == -1)
433 t = ms->offset + sizeof(int64_t);
438 case FILE_BESTRING16:
439 case FILE_LESTRING16:
440 if (m->reln == '=' || m->reln == '!') {
441 if (file_printf(ms, m->desc, m->value.s) == -1)
443 t = ms->offset + m->vallen;
446 if (*m->value.s == '\0')
447 p->s[strcspn(p->s, "\n")] = '\0';
448 if (file_printf(ms, m->desc, p->s) == -1)
450 t = ms->offset + strlen(p->s);
451 if (m->type == FILE_PSTRING)
452 t += file_pstring_length_size(m);
460 if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
462 t = ms->offset + sizeof(time_t);
469 if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
471 t = ms->offset + sizeof(time_t);
477 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
480 t = ms->offset + sizeof(uint64_t);
486 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
489 t = ms->offset + sizeof(uint64_t);
496 switch (check_fmt(ms, m)) {
500 (void)snprintf(buf, sizeof(buf), "%g", vf);
501 if (file_printf(ms, m->desc, buf) == -1)
505 if (file_printf(ms, m->desc, vf) == -1)
509 t = ms->offset + sizeof(float);
516 switch (check_fmt(ms, m)) {
520 (void)snprintf(buf, sizeof(buf), "%g", vd);
521 if (file_printf(ms, m->desc, buf) == -1)
525 if (file_printf(ms, m->desc, vd) == -1)
529 t = ms->offset + sizeof(double);
536 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
538 file_oomem(ms, ms->search.rm_len);
541 rval = file_printf(ms, m->desc, cp);
547 if ((m->str_flags & REGEX_OFFSET_START))
548 t = ms->search.offset;
550 t = ms->search.offset + ms->search.rm_len;
555 if (file_printf(ms, m->desc, m->value.s) == -1)
557 if ((m->str_flags & REGEX_OFFSET_START))
558 t = ms->search.offset;
560 t = ms->search.offset + m->vallen;
564 if (file_printf(ms, m->desc, m->value.s) == -1)
574 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
581 moffset(struct magic_set *ms, struct magic *m)
585 return CAST(int32_t, (ms->offset + sizeof(char)));
590 return CAST(int32_t, (ms->offset + sizeof(short)));
596 return CAST(int32_t, (ms->offset + sizeof(int32_t)));
601 return CAST(int32_t, (ms->offset + sizeof(int64_t)));
605 case FILE_BESTRING16:
606 case FILE_LESTRING16:
607 if (m->reln == '=' || m->reln == '!')
608 return ms->offset + m->vallen;
610 union VALUETYPE *p = &ms->ms_value;
613 if (*m->value.s == '\0')
614 p->s[strcspn(p->s, "\n")] = '\0';
615 t = CAST(uint32_t, (ms->offset + strlen(p->s)));
616 if (m->type == FILE_PSTRING)
617 t += file_pstring_length_size(m);
625 return CAST(int32_t, (ms->offset + sizeof(time_t)));
631 return CAST(int32_t, (ms->offset + sizeof(time_t)));
636 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
641 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
646 return CAST(int32_t, (ms->offset + sizeof(float)));
651 return CAST(int32_t, (ms->offset + sizeof(double)));
654 if ((m->str_flags & REGEX_OFFSET_START) != 0)
655 return CAST(int32_t, ms->search.offset);
657 return CAST(int32_t, (ms->search.offset +
661 if ((m->str_flags & REGEX_OFFSET_START) != 0)
662 return CAST(int32_t, ms->search.offset);
664 return CAST(int32_t, (ms->search.offset + m->vallen));
677 #define DO_CVT(fld, cast) \
679 switch (m->mask_op & FILE_OPS_MASK) { \
681 p->fld &= cast m->num_mask; \
684 p->fld |= cast m->num_mask; \
687 p->fld ^= cast m->num_mask; \
690 p->fld += cast m->num_mask; \
693 p->fld -= cast m->num_mask; \
695 case FILE_OPMULTIPLY: \
696 p->fld *= cast m->num_mask; \
698 case FILE_OPDIVIDE: \
699 p->fld /= cast m->num_mask; \
701 case FILE_OPMODULO: \
702 p->fld %= cast m->num_mask; \
705 if (m->mask_op & FILE_OPINVERSE) \
709 cvt_8(union VALUETYPE *p, const struct magic *m)
711 DO_CVT(b, (uint8_t));
715 cvt_16(union VALUETYPE *p, const struct magic *m)
717 DO_CVT(h, (uint16_t));
721 cvt_32(union VALUETYPE *p, const struct magic *m)
723 DO_CVT(l, (uint32_t));
727 cvt_64(union VALUETYPE *p, const struct magic *m)
729 DO_CVT(q, (uint64_t));
732 #define DO_CVT2(fld, cast) \
734 switch (m->mask_op & FILE_OPS_MASK) { \
736 p->fld += cast m->num_mask; \
739 p->fld -= cast m->num_mask; \
741 case FILE_OPMULTIPLY: \
742 p->fld *= cast m->num_mask; \
744 case FILE_OPDIVIDE: \
745 p->fld /= cast m->num_mask; \
750 cvt_float(union VALUETYPE *p, const struct magic *m)
756 cvt_double(union VALUETYPE *p, const struct magic *m)
758 DO_CVT2(d, (double));
762 * Convert the byte order of the data we are looking at
763 * While we're here, let's apply the mask operation
764 * (unless you have a better idea)
767 mconvert(struct magic_set *ms, struct magic *m)
769 union VALUETYPE *p = &ms->ms_value;
789 case FILE_BESTRING16:
790 case FILE_LESTRING16: {
791 /* Null terminate and eat *trailing* return */
792 p->s[sizeof(p->s) - 1] = '\0';
796 char *ptr1 = p->s, *ptr2 = ptr1 + file_pstring_length_size(m);
797 size_t len = file_pstring_get_length(m, ptr1);
798 if (len >= sizeof(p->s))
799 len = sizeof(p->s) - 1;
806 p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
813 ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
820 (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
821 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
822 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
823 ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
827 p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
834 ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
841 (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
842 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
843 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
844 ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
851 ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
858 p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
859 ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
863 p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
864 ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
871 p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
872 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
873 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
874 ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
878 p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
879 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
880 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
881 ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
889 file_magerror(ms, "invalid type %d in mconvert()", m->type);
896 mdebug(uint32_t offset, const char *str, size_t len)
898 (void) fprintf(stderr, "mget @%d: ", offset);
899 file_showstr(stderr, str, len);
900 (void) fputc('\n', stderr);
901 (void) fputc('\n', stderr);
905 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
906 const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
909 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
910 * anything, but setup pointers into the source
915 ms->search.s = (const char *)s + offset;
916 ms->search.s_len = nbytes - offset;
917 ms->search.offset = offset;
923 const char *last; /* end of search region */
924 const char *buf; /* start of search region */
929 ms->search.s_len = 0;
933 buf = (const char *)s + offset;
934 end = last = (const char *)s + nbytes;
935 /* mget() guarantees buf <= last */
936 for (lines = linecnt, b = buf; lines && b < end &&
937 ((b = CAST(const char *,
938 memchr(c = b, '\n', CAST(size_t, (end - b)))))
939 || (b = CAST(const char *,
940 memchr(c, '\r', CAST(size_t, (end - c))))));
943 if (b[0] == '\r' && b[1] == '\n')
947 last = (const char *)s + nbytes;
950 ms->search.s_len = last - buf;
951 ms->search.offset = offset;
952 ms->search.rm_len = 0;
955 case FILE_BESTRING16:
956 case FILE_LESTRING16: {
957 const unsigned char *src = s + offset;
958 const unsigned char *esrc = s + nbytes;
960 char *edst = &p->s[sizeof(p->s) - 1];
962 if (type == FILE_BESTRING16)
965 /* check for pointer overflow */
967 file_magerror(ms, "invalid offset %u in mcopy()",
971 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
977 if (type == FILE_BESTRING16 ?
986 case FILE_STRING: /* XXX - these two should not need */
987 case FILE_PSTRING: /* to copy anything, but do anyway. */
993 if (offset >= nbytes) {
994 (void)memset(p, '\0', sizeof(*p));
997 if (nbytes - offset < sizeof(*p))
998 nbytes = nbytes - offset;
1000 nbytes = sizeof(*p);
1002 (void)memcpy(p, s + offset, nbytes);
1005 * the usefulness of padding with zeroes eludes me, it
1006 * might even cause problems
1008 if (nbytes < sizeof(*p))
1009 (void)memset(((char *)(void *)p) + nbytes, '\0',
1010 sizeof(*p) - nbytes);
1015 mget(struct magic_set *ms, const unsigned char *s,
1016 struct magic *m, size_t nbytes, unsigned int cont_level)
1018 uint32_t offset = ms->offset;
1019 uint32_t count = m->str_range;
1020 union VALUETYPE *p = &ms->ms_value;
1022 if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
1025 if ((ms->flags & MAGIC_DEBUG) != 0) {
1026 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1027 #ifndef COMPILE_ONLY
1032 if (m->flag & INDIR) {
1033 int off = m->in_offset;
1034 if (m->in_op & FILE_OPINDIRECT) {
1035 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1036 ((const void *)(s + offset + off)));
1037 switch (m->in_type) {
1045 off = (short)((q->hs[0]<<8)|(q->hs[1]));
1048 off = (short)((q->hs[1]<<8)|(q->hs[0]));
1055 off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
1056 (q->hl[2]<<8)|(q->hl[3]));
1060 off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
1061 (q->hl[1]<<8)|(q->hl[0]));
1064 off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
1065 (q->hl[3]<<8)|(q->hl[2]));
1069 switch (m->in_type) {
1071 if (nbytes < (offset + 1))
1074 switch (m->in_op & FILE_OPS_MASK) {
1076 offset = p->b & off;
1079 offset = p->b | off;
1082 offset = p->b ^ off;
1085 offset = p->b + off;
1088 offset = p->b - off;
1090 case FILE_OPMULTIPLY:
1091 offset = p->b * off;
1094 offset = p->b / off;
1097 offset = p->b % off;
1102 if (m->in_op & FILE_OPINVERSE)
1106 if (nbytes < (offset + 2))
1109 switch (m->in_op & FILE_OPS_MASK) {
1111 offset = (short)((p->hs[0]<<8)|
1116 offset = (short)((p->hs[0]<<8)|
1121 offset = (short)((p->hs[0]<<8)|
1126 offset = (short)((p->hs[0]<<8)|
1131 offset = (short)((p->hs[0]<<8)|
1135 case FILE_OPMULTIPLY:
1136 offset = (short)((p->hs[0]<<8)|
1141 offset = (short)((p->hs[0]<<8)|
1146 offset = (short)((p->hs[0]<<8)|
1152 offset = (short)((p->hs[0]<<8)|
1154 if (m->in_op & FILE_OPINVERSE)
1158 if (nbytes < (offset + 2))
1161 switch (m->in_op & FILE_OPS_MASK) {
1163 offset = (short)((p->hs[1]<<8)|
1168 offset = (short)((p->hs[1]<<8)|
1173 offset = (short)((p->hs[1]<<8)|
1178 offset = (short)((p->hs[1]<<8)|
1183 offset = (short)((p->hs[1]<<8)|
1187 case FILE_OPMULTIPLY:
1188 offset = (short)((p->hs[1]<<8)|
1193 offset = (short)((p->hs[1]<<8)|
1198 offset = (short)((p->hs[1]<<8)|
1204 offset = (short)((p->hs[1]<<8)|
1206 if (m->in_op & FILE_OPINVERSE)
1210 if (nbytes < (offset + 2))
1213 switch (m->in_op & FILE_OPS_MASK) {
1215 offset = p->h & off;
1218 offset = p->h | off;
1221 offset = p->h ^ off;
1224 offset = p->h + off;
1227 offset = p->h - off;
1229 case FILE_OPMULTIPLY:
1230 offset = p->h * off;
1233 offset = p->h / off;
1236 offset = p->h % off;
1242 if (m->in_op & FILE_OPINVERSE)
1247 if (nbytes < (offset + 4))
1250 switch (m->in_op & FILE_OPS_MASK) {
1252 offset = (int32_t)((p->hl[0]<<24)|
1259 offset = (int32_t)((p->hl[0]<<24)|
1266 offset = (int32_t)((p->hl[0]<<24)|
1273 offset = (int32_t)((p->hl[0]<<24)|
1280 offset = (int32_t)((p->hl[0]<<24)|
1286 case FILE_OPMULTIPLY:
1287 offset = (int32_t)((p->hl[0]<<24)|
1294 offset = (int32_t)((p->hl[0]<<24)|
1301 offset = (int32_t)((p->hl[0]<<24)|
1309 offset = (int32_t)((p->hl[0]<<24)|
1313 if (m->in_op & FILE_OPINVERSE)
1318 if (nbytes < (offset + 4))
1321 switch (m->in_op & FILE_OPS_MASK) {
1323 offset = (int32_t)((p->hl[3]<<24)|
1330 offset = (int32_t)((p->hl[3]<<24)|
1337 offset = (int32_t)((p->hl[3]<<24)|
1344 offset = (int32_t)((p->hl[3]<<24)|
1351 offset = (int32_t)((p->hl[3]<<24)|
1357 case FILE_OPMULTIPLY:
1358 offset = (int32_t)((p->hl[3]<<24)|
1365 offset = (int32_t)((p->hl[3]<<24)|
1372 offset = (int32_t)((p->hl[3]<<24)|
1380 offset = (int32_t)((p->hl[3]<<24)|
1384 if (m->in_op & FILE_OPINVERSE)
1388 if (nbytes < (offset + 4))
1391 switch (m->in_op & FILE_OPS_MASK) {
1393 offset = (int32_t)((p->hl[1]<<24)|
1400 offset = (int32_t)((p->hl[1]<<24)|
1407 offset = (int32_t)((p->hl[1]<<24)|
1414 offset = (int32_t)((p->hl[1]<<24)|
1421 offset = (int32_t)((p->hl[1]<<24)|
1427 case FILE_OPMULTIPLY:
1428 offset = (int32_t)((p->hl[1]<<24)|
1435 offset = (int32_t)((p->hl[1]<<24)|
1442 offset = (int32_t)((p->hl[1]<<24)|
1450 offset = (int32_t)((p->hl[1]<<24)|
1454 if (m->in_op & FILE_OPINVERSE)
1458 if (nbytes < (offset + 4))
1461 switch (m->in_op & FILE_OPS_MASK) {
1463 offset = p->l & off;
1466 offset = p->l | off;
1469 offset = p->l ^ off;
1472 offset = p->l + off;
1475 offset = p->l - off;
1477 case FILE_OPMULTIPLY:
1478 offset = p->l * off;
1481 offset = p->l / off;
1484 offset = p->l % off;
1489 if (m->in_op & FILE_OPINVERSE)
1494 switch (m->in_type) {
1497 offset = ((((offset >> 0) & 0x7f) << 0) |
1498 (((offset >> 8) & 0x7f) << 7) |
1499 (((offset >> 16) & 0x7f) << 14) |
1500 (((offset >> 24) & 0x7f) << 21)) + 10;
1506 if (m->flag & INDIROFFADD) {
1507 offset += ms->c.li[cont_level-1].off;
1509 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
1511 ms->offset = offset;
1513 if ((ms->flags & MAGIC_DEBUG) != 0) {
1514 mdebug(offset, (char *)(void *)p,
1515 sizeof(union VALUETYPE));
1516 #ifndef COMPILE_ONLY
1522 /* Verify we have enough data to match magic type */
1525 if (nbytes < (offset + 1)) /* should alway be true */
1532 if (nbytes < (offset + 2))
1551 if (nbytes < (offset + 4))
1558 if (nbytes < (offset + 8))
1565 if (nbytes < (offset + m->vallen))
1570 if (nbytes < offset)
1575 if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
1576 file_printf(ms, "%s", m->desc) == -1)
1578 if (nbytes < offset)
1580 return file_softmagic(ms, s + offset, nbytes - offset,
1583 case FILE_DEFAULT: /* nothing to check */
1587 if (!mconvert(ms, m))
1593 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1596 * Convert the source args to unsigned here so that (1) the
1597 * compare will be unsigned as it is in strncmp() and (2) so
1598 * the ctype functions will work correctly without extra
1601 const unsigned char *a = (const unsigned char *)s1;
1602 const unsigned char *b = (const unsigned char *)s2;
1606 * What we want here is v = strncmp(s1, s2, len),
1607 * but ignoring any nulls.
1610 if (0L == flags) { /* normal string: do it fast */
1612 if ((v = *b++ - *a++) != '\0')
1615 else { /* combine the others */
1617 if ((flags & STRING_IGNORE_LOWERCASE) &&
1619 if ((v = tolower(*b++) - *a++) != '\0')
1622 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1624 if ((v = toupper(*b++) - *a++) != '\0')
1627 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1630 if (isspace(*b++)) {
1640 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1647 if ((v = *b++ - *a++) != '\0')
1656 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1659 * XXX - The 16-bit string compare probably needs to be done
1660 * differently, especially if the flags are to be supported.
1661 * At the moment, I am unsure.
1664 return file_strncmp(a, b, len, flags);
1668 magiccheck(struct magic_set *ms, struct magic *m)
1670 uint64_t l = m->value.q;
1675 union VALUETYPE *p = &ms->ms_value;
1743 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1777 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
1790 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1793 case FILE_BESTRING16:
1794 case FILE_LESTRING16:
1796 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1799 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1803 if (ms->search.s == NULL)
1806 slen = MIN(m->vallen, sizeof(m->value.s));
1810 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
1811 if (slen + idx > ms->search.s_len)
1814 v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
1815 if (v == 0) { /* found match */
1816 ms->search.offset += idx;
1827 if (ms->search.s == NULL)
1831 rc = regcomp(&rx, m->value.s,
1832 REG_EXTENDED|REG_NEWLINE|
1833 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
1835 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1836 file_magerror(ms, "regex error %d, (%s)",
1841 regmatch_t pmatch[1];
1842 #ifndef REG_STARTEND
1843 #define REG_STARTEND 0
1844 size_t l = ms->search.s_len - 1;
1845 char c = ms->search.s[l];
1846 ((char *)(intptr_t)ms->search.s)[l] = '\0';
1848 pmatch[0].rm_so = 0;
1849 pmatch[0].rm_eo = ms->search.s_len;
1851 rc = regexec(&rx, (const char *)ms->search.s,
1852 1, pmatch, REG_STARTEND);
1853 #if REG_STARTEND == 0
1854 ((char *)(intptr_t)ms->search.s)[l] = c;
1858 ms->search.s += (int)pmatch[0].rm_so;
1859 ms->search.offset += (size_t)pmatch[0].rm_so;
1861 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
1870 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1871 file_magerror(ms, "regexec error %d, (%s)",
1878 if (v == (uint64_t)-1)
1885 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
1889 v = file_signextend(ms, m, v);
1893 if ((ms->flags & MAGIC_DEBUG) != 0)
1894 (void) fprintf(stderr, "%" INT64_T_FORMAT
1895 "u == *any* = 1\n", (unsigned long long)v);
1901 if ((ms->flags & MAGIC_DEBUG) != 0)
1902 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
1903 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
1904 (unsigned long long)l, matched);
1909 if ((ms->flags & MAGIC_DEBUG) != 0)
1910 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
1911 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
1912 (unsigned long long)l, matched);
1916 if (m->flag & UNSIGNED) {
1918 if ((ms->flags & MAGIC_DEBUG) != 0)
1919 (void) fprintf(stderr, "%" INT64_T_FORMAT
1920 "u > %" INT64_T_FORMAT "u = %d\n",
1921 (unsigned long long)v,
1922 (unsigned long long)l, matched);
1925 matched = (int64_t) v > (int64_t) l;
1926 if ((ms->flags & MAGIC_DEBUG) != 0)
1927 (void) fprintf(stderr, "%" INT64_T_FORMAT
1928 "d > %" INT64_T_FORMAT "d = %d\n",
1929 (long long)v, (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 matched = (v & l) == l;
1953 if ((ms->flags & MAGIC_DEBUG) != 0)
1954 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
1955 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
1956 "x) = %d\n", (unsigned long long)v,
1957 (unsigned long long)l, (unsigned long long)l,
1962 matched = (v & l) != l;
1963 if ((ms->flags & MAGIC_DEBUG) != 0)
1964 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
1965 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
1966 "x) = %d\n", (unsigned long long)v,
1967 (unsigned long long)l, (unsigned long long)l,
1973 file_magerror(ms, "cannot happen: invalid relation `%c'",
1982 handle_annotation(struct magic_set *ms, struct magic *m)
1984 if (ms->flags & MAGIC_APPLE) {
1985 if (file_printf(ms, "%.8s", m->apple) == -1)
1989 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
1990 if (file_printf(ms, "%s", m->mimetype) == -1)
1998 print_sep(struct magic_set *ms, int firstline)
2000 if (ms->flags & MAGIC_MIME)
2005 * we found another match
2006 * put a newline and '-' to do some simple formatting
2008 return file_printf(ms, "\n- ");