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.180 2014/03/15 21:47:40 christos Exp $")
39 #define F(a, b) fmtcheck((a), (b))
45 #if defined(HAVE_LOCALE_H)
50 private int match(struct magic_set *, struct magic *, uint32_t,
51 const unsigned char *, size_t, size_t, int, int, int, int, int *, int *,
53 private int mget(struct magic_set *, const unsigned char *,
54 struct magic *, size_t, size_t, unsigned int, int, int, int, int, int *,
56 private int magiccheck(struct magic_set *, struct magic *);
57 private int32_t mprint(struct magic_set *, struct magic *);
58 private int32_t moffset(struct magic_set *, struct magic *);
59 private void mdebug(uint32_t, const char *, size_t);
60 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
61 const unsigned char *, uint32_t, size_t, size_t);
62 private int mconvert(struct magic_set *, struct magic *, int);
63 private int print_sep(struct magic_set *, int);
64 private int handle_annotation(struct magic_set *, struct magic *);
65 private void cvt_8(union VALUETYPE *, const struct magic *);
66 private void cvt_16(union VALUETYPE *, const struct magic *);
67 private void cvt_32(union VALUETYPE *, const struct magic *);
68 private void cvt_64(union VALUETYPE *, const struct magic *);
70 #define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
72 * softmagic - lookup one file in parsed, in-memory copy of database
73 * Passed the name and FILE * of one file to be typed.
75 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
77 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
78 size_t level, int mode, int text)
81 int rv, printed_something = 0, need_separator = 0;
82 for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
83 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode,
84 text, 0, level, &printed_something, &need_separator,
92 * Go through the whole list, stopping if you find a match. Process all
93 * the continuations of that match before returning.
95 * We support multi-level continuations:
97 * At any time when processing a successful top-level match, there is a
98 * current continuation level; it represents the level of the last
99 * successfully matched continuation.
101 * Continuations above that level are skipped as, if we see one, it
102 * means that the continuation that controls them - i.e, the
103 * lower-level continuation preceding them - failed to match.
105 * Continuations below that level are processed as, if we see one,
106 * it means we've finished processing or skipping higher-level
107 * continuations under the control of a successful or unsuccessful
108 * lower-level continuation, and are now seeing the next lower-level
109 * continuation and should process it. The current continuation
110 * level reverts to the level of the one we're seeing.
112 * Continuations at the current level are processed as, if we see
113 * one, there's no lower-level continuation that may have failed.
115 * If a continuation matches, we bump the current continuation level
116 * so that higher-level continuations are processed.
119 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
120 const unsigned char *s, size_t nbytes, size_t offset, int mode, int text,
121 int flip, int recursion_level, int *printed_something, int *need_separator,
124 uint32_t magindex = 0;
125 unsigned int cont_level = 0;
126 int returnvalv = 0, e; /* if a match is found it is set to 1*/
127 int firstline = 1; /* a flag to print X\n X\n- X */
128 int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
130 if (returnval == NULL)
131 returnval = &returnvalv;
133 if (file_check_mem(ms, cont_level) == -1)
136 for (magindex = 0; magindex < nmagic; magindex++) {
138 struct magic *m = &magic[magindex];
140 if (m->type != FILE_NAME)
141 if ((IS_STRING(m->type) &&
142 #define FLT (STRING_BINTEST | STRING_TEXTTEST)
143 ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
144 (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
145 (m->flag & mode) != mode) {
147 while (magindex + 1 < nmagic &&
148 magic[magindex + 1].cont_level != 0 &&
151 continue; /* Skip to next top-level test*/
154 ms->offset = m->offset;
155 ms->line = m->lineno;
157 /* if main entry matches, print it... */
158 switch (mget(ms, s, m, nbytes, offset, cont_level, mode, text,
159 flip, recursion_level + 1, printed_something,
160 need_separator, returnval)) {
164 flush = m->reln != '!';
167 if (m->type == FILE_INDIRECT)
170 switch (magiccheck(ms, m)) {
184 * main entry didn't match,
185 * flush its continuations
187 while (magindex < nmagic - 1 &&
188 magic[magindex + 1].cont_level != 0)
193 if ((e = handle_annotation(ms, m)) != 0) {
195 *printed_something = 1;
200 * If we are going to print something, we'll need to print
201 * a blank before we print something else.
205 *printed_something = 1;
206 if (print_sep(ms, firstline) == -1)
211 if (print && mprint(ms, m) == -1)
214 ms->c.li[cont_level].off = moffset(ms, m);
216 /* and any continuations that match */
217 if (file_check_mem(ms, ++cont_level) == -1)
220 while (magic[magindex+1].cont_level != 0 &&
221 ++magindex < nmagic) {
222 m = &magic[magindex];
223 ms->line = m->lineno; /* for messages */
225 if (cont_level < m->cont_level)
227 if (cont_level > m->cont_level) {
229 * We're at the end of the level
230 * "cont_level" continuations.
232 cont_level = m->cont_level;
234 ms->offset = m->offset;
235 if (m->flag & OFFADD) {
237 ms->c.li[cont_level - 1].off;
240 #ifdef ENABLE_CONDITIONALS
241 if (m->cond == COND_ELSE ||
242 m->cond == COND_ELIF) {
243 if (ms->c.li[cont_level].last_match == 1)
247 switch (mget(ms, s, m, nbytes, offset, cont_level, mode,
248 text, flip, recursion_level + 1, printed_something,
249 need_separator, returnval)) {
258 if (m->type == FILE_INDIRECT)
264 switch (flush ? 1 : magiccheck(ms, m)) {
268 #ifdef ENABLE_CONDITIONALS
269 ms->c.li[cont_level].last_match = 0;
273 #ifdef ENABLE_CONDITIONALS
274 ms->c.li[cont_level].last_match = 1;
276 if (m->type == FILE_CLEAR)
277 ms->c.li[cont_level].got_match = 0;
278 else if (ms->c.li[cont_level].got_match) {
279 if (m->type == FILE_DEFAULT)
282 ms->c.li[cont_level].got_match = 1;
283 if ((e = handle_annotation(ms, m)) != 0) {
285 *printed_something = 1;
290 * If we are going to print something,
291 * make sure that we have a separator first.
294 if (!*printed_something) {
295 *printed_something = 1;
296 if (print_sep(ms, firstline)
302 * This continuation matched. Print
303 * its message, with a blank before it
304 * if the previous item printed and
305 * this item isn't empty.
307 /* space if previous printed */
309 && ((m->flag & NOSPACE) == 0)
312 file_printf(ms, " ") == -1)
316 if (print && mprint(ms, m) == -1)
319 ms->c.li[cont_level].off = moffset(ms, m);
325 * If we see any continuations
329 if (file_check_mem(ms, ++cont_level) == -1)
334 if (*printed_something) {
339 if ((ms->flags & MAGIC_CONTINUE) == 0 && *printed_something) {
340 return *returnval; /* don't keep searching */
343 return *returnval; /* This is hit if -k is set or there is no match */
347 check_fmt(struct magic_set *ms, struct magic *m)
353 if (strchr(m->desc, '%') == NULL)
356 old_lc_ctype = setlocale(LC_CTYPE, NULL);
357 assert(old_lc_ctype != NULL);
358 old_lc_ctype = strdup(old_lc_ctype);
359 assert(old_lc_ctype != NULL);
360 (void)setlocale(LC_CTYPE, "C");
361 rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
364 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
365 file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
367 rc = regexec(&rx, m->desc, 0, 0, 0);
371 (void)setlocale(LC_CTYPE, old_lc_ctype);
377 char * strndup(const char *, size_t);
380 strndup(const char *str, size_t n)
385 for (len = 0; len < n && str[len]; len++)
387 if ((copy = malloc(len + 1)) == NULL)
389 (void)memcpy(copy, str, len);
393 #endif /* HAVE_STRNDUP */
396 mprint(struct magic_set *ms, struct magic *m)
402 char buf[128], tbuf[26];
403 union VALUETYPE *p = &ms->ms_value;
407 v = file_signextend(ms, m, (uint64_t)p->b);
408 switch (check_fmt(ms, m)) {
412 (void)snprintf(buf, sizeof(buf), "%c",
414 if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
418 if (file_printf(ms, F(m->desc, "%c"),
419 (unsigned char) v) == -1)
423 t = ms->offset + sizeof(char);
429 v = file_signextend(ms, m, (uint64_t)p->h);
430 switch (check_fmt(ms, m)) {
434 (void)snprintf(buf, sizeof(buf), "%hu",
436 if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
440 if (file_printf(ms, F(m->desc, "%hu"),
441 (unsigned short) v) == -1)
445 t = ms->offset + sizeof(short);
452 v = file_signextend(ms, m, (uint64_t)p->l);
453 switch (check_fmt(ms, m)) {
457 (void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v);
458 if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
462 if (file_printf(ms, F(m->desc, "%u"),
467 t = ms->offset + sizeof(int32_t);
473 v = file_signextend(ms, m, p->q);
474 switch (check_fmt(ms, m)) {
478 (void)snprintf(buf, sizeof(buf), "%llu",
479 (unsigned long long)v);
480 if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
484 if (file_printf(ms, F(m->desc, "%llu"),
485 (unsigned long long) v) == -1)
489 t = ms->offset + sizeof(int64_t);
494 case FILE_BESTRING16:
495 case FILE_LESTRING16:
496 if (m->reln == '=' || m->reln == '!') {
497 if (file_printf(ms, F(m->desc, "%s"), m->value.s) == -1)
499 t = ms->offset + m->vallen;
504 /* compute t before we mangle the string? */
505 t = ms->offset + strlen(str);
507 if (*m->value.s == '\0')
508 str[strcspn(str, "\n")] = '\0';
510 if (m->str_flags & STRING_TRIM) {
512 while (isspace((unsigned char)*str))
518 while (isspace((unsigned char)*last))
523 if (file_printf(ms, F(m->desc, "%s"), str) == -1)
526 if (m->type == FILE_PSTRING)
527 t += file_pstring_length_size(m);
535 if (file_printf(ms, F(m->desc, "%s"),
536 file_fmttime(p->l, FILE_T_LOCAL, tbuf)) == -1)
538 t = ms->offset + sizeof(uint32_t);
545 if (file_printf(ms, F(m->desc, "%s"),
546 file_fmttime(p->l, 0, tbuf)) == -1)
548 t = ms->offset + sizeof(uint32_t);
554 if (file_printf(ms, F(m->desc, "%s"),
555 file_fmttime(p->q, FILE_T_LOCAL, tbuf)) == -1)
557 t = ms->offset + sizeof(uint64_t);
563 if (file_printf(ms, F(m->desc, "%s"),
564 file_fmttime(p->q, 0, tbuf)) == -1)
566 t = ms->offset + sizeof(uint64_t);
572 if (file_printf(ms, F(m->desc, "%s"),
573 file_fmttime(p->q, FILE_T_WINDOWS, tbuf)) == -1)
575 t = ms->offset + sizeof(uint64_t);
582 switch (check_fmt(ms, m)) {
586 (void)snprintf(buf, sizeof(buf), "%g", vf);
587 if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
591 if (file_printf(ms, F(m->desc, "%g"), vf) == -1)
595 t = ms->offset + sizeof(float);
602 switch (check_fmt(ms, m)) {
606 (void)snprintf(buf, sizeof(buf), "%g", vd);
607 if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
611 if (file_printf(ms, F(m->desc, "%g"), vd) == -1)
615 t = ms->offset + sizeof(double);
622 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
624 file_oomem(ms, ms->search.rm_len);
627 rval = file_printf(ms, F(m->desc, "%s"), cp);
633 if ((m->str_flags & REGEX_OFFSET_START))
634 t = ms->search.offset;
636 t = ms->search.offset + ms->search.rm_len;
641 if (file_printf(ms, F(m->desc, "%s"), m->value.s) == -1)
643 if ((m->str_flags & REGEX_OFFSET_START))
644 t = ms->search.offset;
646 t = ms->search.offset + m->vallen;
651 if (file_printf(ms, "%s", m->desc) == -1)
663 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
670 moffset(struct magic_set *ms, struct magic *m)
674 return CAST(int32_t, (ms->offset + sizeof(char)));
679 return CAST(int32_t, (ms->offset + sizeof(short)));
685 return CAST(int32_t, (ms->offset + sizeof(int32_t)));
690 return CAST(int32_t, (ms->offset + sizeof(int64_t)));
694 case FILE_BESTRING16:
695 case FILE_LESTRING16:
696 if (m->reln == '=' || m->reln == '!')
697 return ms->offset + m->vallen;
699 union VALUETYPE *p = &ms->ms_value;
702 if (*m->value.s == '\0')
703 p->s[strcspn(p->s, "\n")] = '\0';
704 t = CAST(uint32_t, (ms->offset + strlen(p->s)));
705 if (m->type == FILE_PSTRING)
706 t += (uint32_t)file_pstring_length_size(m);
714 return CAST(int32_t, (ms->offset + sizeof(uint32_t)));
720 return CAST(int32_t, (ms->offset + sizeof(uint32_t)));
725 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
730 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
735 return CAST(int32_t, (ms->offset + sizeof(float)));
740 return CAST(int32_t, (ms->offset + sizeof(double)));
743 if ((m->str_flags & REGEX_OFFSET_START) != 0)
744 return CAST(int32_t, ms->search.offset);
746 return CAST(int32_t, (ms->search.offset +
750 if ((m->str_flags & REGEX_OFFSET_START) != 0)
751 return CAST(int32_t, ms->search.offset);
753 return CAST(int32_t, (ms->search.offset + m->vallen));
766 cvt_flip(int type, int flip)
784 return FILE_LEQLDATE;
786 return FILE_LEQWDATE;
800 return FILE_BEQLDATE;
802 return FILE_BEQWDATE;
808 return FILE_LEDOUBLE;
810 return FILE_BEDOUBLE;
815 #define DO_CVT(fld, cast) \
817 switch (m->mask_op & FILE_OPS_MASK) { \
819 p->fld &= cast m->num_mask; \
822 p->fld |= cast m->num_mask; \
825 p->fld ^= cast m->num_mask; \
828 p->fld += cast m->num_mask; \
831 p->fld -= cast m->num_mask; \
833 case FILE_OPMULTIPLY: \
834 p->fld *= cast m->num_mask; \
836 case FILE_OPDIVIDE: \
837 p->fld /= cast m->num_mask; \
839 case FILE_OPMODULO: \
840 p->fld %= cast m->num_mask; \
843 if (m->mask_op & FILE_OPINVERSE) \
847 cvt_8(union VALUETYPE *p, const struct magic *m)
849 DO_CVT(b, (uint8_t));
853 cvt_16(union VALUETYPE *p, const struct magic *m)
855 DO_CVT(h, (uint16_t));
859 cvt_32(union VALUETYPE *p, const struct magic *m)
861 DO_CVT(l, (uint32_t));
865 cvt_64(union VALUETYPE *p, const struct magic *m)
867 DO_CVT(q, (uint64_t));
870 #define DO_CVT2(fld, cast) \
872 switch (m->mask_op & FILE_OPS_MASK) { \
874 p->fld += cast m->num_mask; \
877 p->fld -= cast m->num_mask; \
879 case FILE_OPMULTIPLY: \
880 p->fld *= cast m->num_mask; \
882 case FILE_OPDIVIDE: \
883 p->fld /= cast m->num_mask; \
888 cvt_float(union VALUETYPE *p, const struct magic *m)
894 cvt_double(union VALUETYPE *p, const struct magic *m)
896 DO_CVT2(d, (double));
900 * Convert the byte order of the data we are looking at
901 * While we're here, let's apply the mask operation
902 * (unless you have a better idea)
905 mconvert(struct magic_set *ms, struct magic *m, int flip)
907 union VALUETYPE *p = &ms->ms_value;
909 switch (cvt_flip(m->type, flip)) {
928 case FILE_BESTRING16:
929 case FILE_LESTRING16: {
930 /* Null terminate and eat *trailing* return */
931 p->s[sizeof(p->s) - 1] = '\0';
935 char *ptr1 = p->s, *ptr2 = ptr1 + file_pstring_length_size(m);
936 size_t len = file_pstring_get_length(m, ptr1);
937 if (len >= sizeof(p->s))
938 len = sizeof(p->s) - 1;
945 p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
952 ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
960 (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
961 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
962 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
963 ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
967 p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
974 ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
982 (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
983 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
984 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
985 ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
992 ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
999 p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
1000 ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
1004 p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
1005 ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
1012 p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
1013 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
1014 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
1015 ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
1019 p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
1020 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
1021 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
1022 ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
1033 file_magerror(ms, "invalid type %d in mconvert()", m->type);
1040 mdebug(uint32_t offset, const char *str, size_t len)
1042 (void) fprintf(stderr, "mget/%zu @%d: ", len, offset);
1043 file_showstr(stderr, str, len);
1044 (void) fputc('\n', stderr);
1045 (void) fputc('\n', stderr);
1049 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
1050 const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
1053 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1054 * anything, but setup pointers into the source
1059 ms->search.s = RCAST(const char *, s) + offset;
1060 ms->search.s_len = nbytes - offset;
1061 ms->search.offset = offset;
1067 const char *last; /* end of search region */
1068 const char *buf; /* start of search region */
1073 ms->search.s_len = 0;
1074 ms->search.s = NULL;
1077 buf = RCAST(const char *, s) + offset;
1078 end = last = RCAST(const char *, s) + nbytes;
1079 /* mget() guarantees buf <= last */
1080 for (lines = linecnt, b = buf; lines && b < end &&
1081 ((b = CAST(const char *,
1082 memchr(c = b, '\n', CAST(size_t, (end - b)))))
1083 || (b = CAST(const char *,
1084 memchr(c, '\r', CAST(size_t, (end - c))))));
1087 if (b[0] == '\r' && b[1] == '\n')
1091 last = RCAST(const char *, s) + nbytes;
1094 ms->search.s_len = last - buf;
1095 ms->search.offset = offset;
1096 ms->search.rm_len = 0;
1099 case FILE_BESTRING16:
1100 case FILE_LESTRING16: {
1101 const unsigned char *src = s + offset;
1102 const unsigned char *esrc = s + nbytes;
1104 char *edst = &p->s[sizeof(p->s) - 1];
1106 if (type == FILE_BESTRING16)
1109 /* check that offset is within range */
1110 if (offset >= nbytes)
1112 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
1118 if (type == FILE_BESTRING16 ?
1119 *(src - 1) != '\0' :
1127 case FILE_STRING: /* XXX - these two should not need */
1128 case FILE_PSTRING: /* to copy anything, but do anyway. */
1134 if (offset >= nbytes) {
1135 (void)memset(p, '\0', sizeof(*p));
1138 if (nbytes - offset < sizeof(*p))
1139 nbytes = nbytes - offset;
1141 nbytes = sizeof(*p);
1143 (void)memcpy(p, s + offset, nbytes);
1146 * the usefulness of padding with zeroes eludes me, it
1147 * might even cause problems
1149 if (nbytes < sizeof(*p))
1150 (void)memset(((char *)(void *)p) + nbytes, '\0',
1151 sizeof(*p) - nbytes);
1156 mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
1157 size_t nbytes, size_t o, unsigned int cont_level, int mode, int text,
1158 int flip, int recursion_level, int *printed_something,
1159 int *need_separator, int *returnval)
1161 uint32_t soffset, offset = ms->offset;
1162 uint32_t count = m->str_range;
1163 int rv, oneed_separator, in_type;
1165 union VALUETYPE *p = &ms->ms_value;
1168 if (recursion_level >= 20) {
1169 file_error(ms, 0, "recursion nesting exceeded");
1173 if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
1174 (uint32_t)nbytes, count) == -1)
1177 if ((ms->flags & MAGIC_DEBUG) != 0) {
1178 fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%zu, "
1179 "nbytes=%zu, count=%u)\n", m->type, m->flag, offset, o,
1181 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1182 #ifndef COMPILE_ONLY
1187 if (m->flag & INDIR) {
1188 int off = m->in_offset;
1189 if (m->in_op & FILE_OPINDIRECT) {
1190 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1191 ((const void *)(s + offset + off)));
1192 switch (cvt_flip(m->in_type, flip)) {
1200 off = (short)((q->hs[0]<<8)|(q->hs[1]));
1203 off = (short)((q->hs[1]<<8)|(q->hs[0]));
1210 off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
1211 (q->hl[2]<<8)|(q->hl[3]));
1215 off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
1216 (q->hl[1]<<8)|(q->hl[0]));
1219 off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
1220 (q->hl[3]<<8)|(q->hl[2]));
1223 if ((ms->flags & MAGIC_DEBUG) != 0)
1224 fprintf(stderr, "indirect offs=%u\n", off);
1226 switch (in_type = cvt_flip(m->in_type, flip)) {
1228 if (OFFSET_OOB(nbytes, offset, 1))
1231 switch (m->in_op & FILE_OPS_MASK) {
1233 offset = p->b & off;
1236 offset = p->b | off;
1239 offset = p->b ^ off;
1242 offset = p->b + off;
1245 offset = p->b - off;
1247 case FILE_OPMULTIPLY:
1248 offset = p->b * off;
1251 offset = p->b / off;
1254 offset = p->b % off;
1259 if (m->in_op & FILE_OPINVERSE)
1263 if (OFFSET_OOB(nbytes, offset, 2))
1266 switch (m->in_op & FILE_OPS_MASK) {
1268 offset = (short)((p->hs[0]<<8)|
1273 offset = (short)((p->hs[0]<<8)|
1278 offset = (short)((p->hs[0]<<8)|
1283 offset = (short)((p->hs[0]<<8)|
1288 offset = (short)((p->hs[0]<<8)|
1292 case FILE_OPMULTIPLY:
1293 offset = (short)((p->hs[0]<<8)|
1298 offset = (short)((p->hs[0]<<8)|
1303 offset = (short)((p->hs[0]<<8)|
1309 offset = (short)((p->hs[0]<<8)|
1311 if (m->in_op & FILE_OPINVERSE)
1315 if (OFFSET_OOB(nbytes, offset, 2))
1318 switch (m->in_op & FILE_OPS_MASK) {
1320 offset = (short)((p->hs[1]<<8)|
1325 offset = (short)((p->hs[1]<<8)|
1330 offset = (short)((p->hs[1]<<8)|
1335 offset = (short)((p->hs[1]<<8)|
1340 offset = (short)((p->hs[1]<<8)|
1344 case FILE_OPMULTIPLY:
1345 offset = (short)((p->hs[1]<<8)|
1350 offset = (short)((p->hs[1]<<8)|
1355 offset = (short)((p->hs[1]<<8)|
1361 offset = (short)((p->hs[1]<<8)|
1363 if (m->in_op & FILE_OPINVERSE)
1367 if (OFFSET_OOB(nbytes, offset, 2))
1370 switch (m->in_op & FILE_OPS_MASK) {
1372 offset = p->h & off;
1375 offset = p->h | off;
1378 offset = p->h ^ off;
1381 offset = p->h + off;
1384 offset = p->h - off;
1386 case FILE_OPMULTIPLY:
1387 offset = p->h * off;
1390 offset = p->h / off;
1393 offset = p->h % off;
1399 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[0]<<24)|
1416 offset = (int32_t)((p->hl[0]<<24)|
1423 offset = (int32_t)((p->hl[0]<<24)|
1430 offset = (int32_t)((p->hl[0]<<24)|
1437 offset = (int32_t)((p->hl[0]<<24)|
1443 case FILE_OPMULTIPLY:
1444 offset = (int32_t)((p->hl[0]<<24)|
1451 offset = (int32_t)((p->hl[0]<<24)|
1458 offset = (int32_t)((p->hl[0]<<24)|
1466 offset = (int32_t)((p->hl[0]<<24)|
1470 if (m->in_op & FILE_OPINVERSE)
1475 if (OFFSET_OOB(nbytes, offset, 4))
1478 switch (m->in_op & FILE_OPS_MASK) {
1480 offset = (int32_t)((p->hl[3]<<24)|
1487 offset = (int32_t)((p->hl[3]<<24)|
1494 offset = (int32_t)((p->hl[3]<<24)|
1501 offset = (int32_t)((p->hl[3]<<24)|
1508 offset = (int32_t)((p->hl[3]<<24)|
1514 case FILE_OPMULTIPLY:
1515 offset = (int32_t)((p->hl[3]<<24)|
1522 offset = (int32_t)((p->hl[3]<<24)|
1529 offset = (int32_t)((p->hl[3]<<24)|
1537 offset = (int32_t)((p->hl[3]<<24)|
1541 if (m->in_op & FILE_OPINVERSE)
1545 if (OFFSET_OOB(nbytes, offset, 4))
1548 switch (m->in_op & FILE_OPS_MASK) {
1550 offset = (int32_t)((p->hl[1]<<24)|
1557 offset = (int32_t)((p->hl[1]<<24)|
1564 offset = (int32_t)((p->hl[1]<<24)|
1571 offset = (int32_t)((p->hl[1]<<24)|
1578 offset = (int32_t)((p->hl[1]<<24)|
1584 case FILE_OPMULTIPLY:
1585 offset = (int32_t)((p->hl[1]<<24)|
1592 offset = (int32_t)((p->hl[1]<<24)|
1599 offset = (int32_t)((p->hl[1]<<24)|
1607 offset = (int32_t)((p->hl[1]<<24)|
1611 if (m->in_op & FILE_OPINVERSE)
1615 if (OFFSET_OOB(nbytes, offset, 4))
1618 switch (m->in_op & FILE_OPS_MASK) {
1620 offset = p->l & off;
1623 offset = p->l | off;
1626 offset = p->l ^ off;
1629 offset = p->l + off;
1632 offset = p->l - off;
1634 case FILE_OPMULTIPLY:
1635 offset = p->l * off;
1638 offset = p->l / off;
1641 offset = p->l % off;
1646 if (m->in_op & FILE_OPINVERSE)
1656 offset = ((((offset >> 0) & 0x7f) << 0) |
1657 (((offset >> 8) & 0x7f) << 7) |
1658 (((offset >> 16) & 0x7f) << 14) |
1659 (((offset >> 24) & 0x7f) << 21)) + 10;
1665 if (m->flag & INDIROFFADD) {
1666 offset += ms->c.li[cont_level-1].off;
1668 if ((ms->flags & MAGIC_DEBUG) != 0)
1670 "indirect *zero* offset\n");
1673 if ((ms->flags & MAGIC_DEBUG) != 0)
1674 fprintf(stderr, "indirect +offs=%u\n", offset);
1676 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
1678 ms->offset = offset;
1680 if ((ms->flags & MAGIC_DEBUG) != 0) {
1681 mdebug(offset, (char *)(void *)p,
1682 sizeof(union VALUETYPE));
1683 #ifndef COMPILE_ONLY
1689 /* Verify we have enough data to match magic type */
1692 if (OFFSET_OOB(nbytes, offset, 1))
1699 if (OFFSET_OOB(nbytes, offset, 2))
1718 if (OFFSET_OOB(nbytes, offset, 4))
1725 if (OFFSET_OOB(nbytes, offset, 8))
1732 if (OFFSET_OOB(nbytes, offset, m->vallen))
1737 if (nbytes < offset)
1744 if (nbytes < offset)
1747 soffset = ms->offset;
1750 rv = file_softmagic(ms, s + offset, nbytes - offset,
1751 recursion_level, BINTEST, text);
1752 if ((ms->flags & MAGIC_DEBUG) != 0)
1753 fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1756 ms->offset = soffset;
1758 if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
1759 file_printf(ms, F(m->desc, "%u"), offset) == -1) {
1763 if (file_printf(ms, "%s", rbuf) == -1) {
1772 if (nbytes < offset)
1779 if (file_magicfind(ms, sbuf, &ml) == -1) {
1780 file_error(ms, 0, "cannot find entry `%s'", sbuf);
1784 oneed_separator = *need_separator;
1785 if (m->flag & NOSPACE)
1786 *need_separator = 0;
1787 rv = match(ms, ml.magic, ml.nmagic, s, nbytes, offset + o,
1788 mode, text, flip, recursion_level, printed_something,
1789 need_separator, returnval);
1791 *need_separator = oneed_separator;
1795 if (file_printf(ms, "%s", m->desc) == -1)
1798 case FILE_DEFAULT: /* nothing to check */
1803 if (!mconvert(ms, m, flip))
1809 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1812 * Convert the source args to unsigned here so that (1) the
1813 * compare will be unsigned as it is in strncmp() and (2) so
1814 * the ctype functions will work correctly without extra
1817 const unsigned char *a = (const unsigned char *)s1;
1818 const unsigned char *b = (const unsigned char *)s2;
1822 * What we want here is v = strncmp(s1, s2, len),
1823 * but ignoring any nulls.
1826 if (0L == flags) { /* normal string: do it fast */
1828 if ((v = *b++ - *a++) != '\0')
1831 else { /* combine the others */
1833 if ((flags & STRING_IGNORE_LOWERCASE) &&
1835 if ((v = tolower(*b++) - *a++) != '\0')
1838 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1840 if ((v = toupper(*b++) - *a++) != '\0')
1843 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1846 if (isspace(*b++)) {
1856 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1863 if ((v = *b++ - *a++) != '\0')
1872 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1875 * XXX - The 16-bit string compare probably needs to be done
1876 * differently, especially if the flags are to be supported.
1877 * At the moment, I am unsure.
1880 return file_strncmp(a, b, len, flags);
1884 magiccheck(struct magic_set *ms, struct magic *m)
1886 uint64_t l = m->value.q;
1891 union VALUETYPE *p = &ms->ms_value;
1963 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1997 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
2011 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
2014 case FILE_BESTRING16:
2015 case FILE_LESTRING16:
2017 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
2020 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
2024 if (ms->search.s == NULL)
2027 slen = MIN(m->vallen, sizeof(m->value.s));
2031 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
2032 if (slen + idx > ms->search.s_len)
2035 v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
2036 if (v == 0) { /* found match */
2037 ms->search.offset += idx;
2048 if (ms->search.s == NULL)
2051 old_lc_ctype = setlocale(LC_CTYPE, NULL);
2052 assert(old_lc_ctype != NULL);
2053 old_lc_ctype = strdup(old_lc_ctype);
2054 assert(old_lc_ctype != NULL);
2055 (void)setlocale(LC_CTYPE, "C");
2057 rc = regcomp(&rx, m->value.s,
2058 REG_EXTENDED|REG_NEWLINE|
2059 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
2061 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
2062 file_magerror(ms, "regex error %d, (%s)",
2067 regmatch_t pmatch[1];
2068 #ifndef REG_STARTEND
2069 #define REG_STARTEND 0
2070 size_t l = ms->search.s_len - 1;
2071 char c = ms->search.s[l];
2072 ((char *)(intptr_t)ms->search.s)[l] = '\0';
2074 pmatch[0].rm_so = 0;
2075 pmatch[0].rm_eo = ms->search.s_len;
2077 rc = regexec(&rx, (const char *)ms->search.s,
2078 1, pmatch, REG_STARTEND);
2079 #if REG_STARTEND == 0
2080 ((char *)(intptr_t)ms->search.s)[l] = c;
2084 ms->search.s += (int)pmatch[0].rm_so;
2085 ms->search.offset += (size_t)pmatch[0].rm_so;
2087 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
2096 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
2097 file_magerror(ms, "regexec error %d, (%s)",
2104 (void)setlocale(LC_CTYPE, old_lc_ctype);
2106 if (v == (uint64_t)-1)
2115 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2119 v = file_signextend(ms, m, v);
2123 if ((ms->flags & MAGIC_DEBUG) != 0)
2124 (void) fprintf(stderr, "%" INT64_T_FORMAT
2125 "u == *any* = 1\n", (unsigned long long)v);
2131 if ((ms->flags & MAGIC_DEBUG) != 0)
2132 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
2133 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2134 (unsigned long long)l, matched);
2139 if ((ms->flags & MAGIC_DEBUG) != 0)
2140 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2141 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2142 (unsigned long long)l, matched);
2146 if (m->flag & UNSIGNED) {
2148 if ((ms->flags & MAGIC_DEBUG) != 0)
2149 (void) fprintf(stderr, "%" INT64_T_FORMAT
2150 "u > %" INT64_T_FORMAT "u = %d\n",
2151 (unsigned long long)v,
2152 (unsigned long long)l, matched);
2155 matched = (int64_t) v > (int64_t) l;
2156 if ((ms->flags & MAGIC_DEBUG) != 0)
2157 (void) fprintf(stderr, "%" INT64_T_FORMAT
2158 "d > %" INT64_T_FORMAT "d = %d\n",
2159 (long long)v, (long long)l, matched);
2164 if (m->flag & UNSIGNED) {
2166 if ((ms->flags & MAGIC_DEBUG) != 0)
2167 (void) fprintf(stderr, "%" INT64_T_FORMAT
2168 "u < %" INT64_T_FORMAT "u = %d\n",
2169 (unsigned long long)v,
2170 (unsigned long long)l, matched);
2173 matched = (int64_t) v < (int64_t) l;
2174 if ((ms->flags & MAGIC_DEBUG) != 0)
2175 (void) fprintf(stderr, "%" INT64_T_FORMAT
2176 "d < %" INT64_T_FORMAT "d = %d\n",
2177 (long long)v, (long long)l, matched);
2182 matched = (v & l) == l;
2183 if ((ms->flags & MAGIC_DEBUG) != 0)
2184 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2185 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2186 "x) = %d\n", (unsigned long long)v,
2187 (unsigned long long)l, (unsigned long long)l,
2192 matched = (v & l) != l;
2193 if ((ms->flags & MAGIC_DEBUG) != 0)
2194 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2195 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2196 "x) = %d\n", (unsigned long long)v,
2197 (unsigned long long)l, (unsigned long long)l,
2203 file_magerror(ms, "cannot happen: invalid relation `%c'",
2212 handle_annotation(struct magic_set *ms, struct magic *m)
2214 if (ms->flags & MAGIC_APPLE) {
2215 if (file_printf(ms, "%.8s", m->apple) == -1)
2219 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2220 if (file_printf(ms, "%s", m->mimetype) == -1)
2228 print_sep(struct magic_set *ms, int firstline)
2230 if (ms->flags & MAGIC_MIME)
2235 * we found another match
2236 * put a newline and '-' to do some simple formatting
2238 return file_printf(ms, "\n- ");