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.203 2014/12/04 15:22:05 christos Exp $")
45 private int match(struct magic_set *, struct magic *, uint32_t,
46 const unsigned char *, size_t, size_t, int, int, int, uint16_t,
47 uint16_t *, int *, int *, int *);
48 private int mget(struct magic_set *, const unsigned char *,
49 struct magic *, size_t, size_t, unsigned int, int, int, int, uint16_t,
50 uint16_t *, int *, int *, int *);
51 private int magiccheck(struct magic_set *, struct magic *);
52 private int32_t mprint(struct magic_set *, struct magic *);
53 private int32_t moffset(struct magic_set *, struct magic *);
54 private void mdebug(uint32_t, const char *, size_t);
55 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
56 const unsigned char *, uint32_t, size_t, struct magic *);
57 private int mconvert(struct magic_set *, struct magic *, int);
58 private int print_sep(struct magic_set *, int);
59 private int handle_annotation(struct magic_set *, struct magic *);
60 private void cvt_8(union VALUETYPE *, const struct magic *);
61 private void cvt_16(union VALUETYPE *, const struct magic *);
62 private void cvt_32(union VALUETYPE *, const struct magic *);
63 private void cvt_64(union VALUETYPE *, const struct magic *);
65 #define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
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 uint16_t indir_level, uint16_t *name_count, int mode, int text)
77 int rv, printed_something = 0, need_separator = 0;
80 if (name_count == NULL) {
85 for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
86 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode,
87 text, 0, indir_level, name_count,
88 &printed_something, &need_separator, NULL)) != 0)
96 #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
98 private const char * __attribute__((__format_arg__(3)))
99 file_fmtcheck(struct magic_set *ms, const struct magic *m, const char *def,
100 const char *file, size_t line)
102 const char *ptr = fmtcheck(m->desc, def);
105 "%s, %" SIZE_T_FORMAT "u: format `%s' does not match"
106 " with `%s'", file, line, m->desc, def);
110 #define F(a, b, c) fmtcheck((b)->desc, (c))
114 * Go through the whole list, stopping if you find a match. Process all
115 * the continuations of that match before returning.
117 * We support multi-level continuations:
119 * At any time when processing a successful top-level match, there is a
120 * current continuation level; it represents the level of the last
121 * successfully matched continuation.
123 * Continuations above that level are skipped as, if we see one, it
124 * means that the continuation that controls them - i.e, the
125 * lower-level continuation preceding them - failed to match.
127 * Continuations below that level are processed as, if we see one,
128 * it means we've finished processing or skipping higher-level
129 * continuations under the control of a successful or unsuccessful
130 * lower-level continuation, and are now seeing the next lower-level
131 * continuation and should process it. The current continuation
132 * level reverts to the level of the one we're seeing.
134 * Continuations at the current level are processed as, if we see
135 * one, there's no lower-level continuation that may have failed.
137 * If a continuation matches, we bump the current continuation level
138 * so that higher-level continuations are processed.
141 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
142 const unsigned char *s, size_t nbytes, size_t offset, int mode, int text,
143 int flip, uint16_t indir_level, uint16_t *name_count,
144 int *printed_something, int *need_separator, int *returnval)
146 uint32_t magindex = 0;
147 unsigned int cont_level = 0;
148 int returnvalv = 0, e; /* if a match is found it is set to 1*/
149 int firstline = 1; /* a flag to print X\n X\n- X */
150 int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
152 if (returnval == NULL)
153 returnval = &returnvalv;
155 if (file_check_mem(ms, cont_level) == -1)
158 for (magindex = 0; magindex < nmagic; magindex++) {
160 struct magic *m = &magic[magindex];
162 if (m->type != FILE_NAME)
163 if ((IS_STRING(m->type) &&
164 #define FLT (STRING_BINTEST | STRING_TEXTTEST)
165 ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
166 (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
167 (m->flag & mode) != mode) {
169 while (magindex + 1 < nmagic &&
170 magic[magindex + 1].cont_level != 0 &&
173 continue; /* Skip to next top-level test*/
176 ms->offset = m->offset;
177 ms->line = m->lineno;
179 /* if main entry matches, print it... */
180 switch (mget(ms, s, m, nbytes, offset, cont_level, mode, text,
181 flip, indir_level, name_count,
182 printed_something, need_separator, returnval)) {
186 flush = m->reln != '!';
189 if (m->type == FILE_INDIRECT)
192 switch (magiccheck(ms, m)) {
206 * main entry didn't match,
207 * flush its continuations
209 while (magindex < nmagic - 1 &&
210 magic[magindex + 1].cont_level != 0)
215 if ((e = handle_annotation(ms, m)) != 0) {
217 *printed_something = 1;
222 * If we are going to print something, we'll need to print
223 * a blank before we print something else.
227 *printed_something = 1;
228 if (print_sep(ms, firstline) == -1)
233 if (print && mprint(ms, m) == -1)
236 ms->c.li[cont_level].off = moffset(ms, m);
238 /* and any continuations that match */
239 if (file_check_mem(ms, ++cont_level) == -1)
242 while (magindex + 1 < nmagic &&
243 magic[magindex + 1].cont_level != 0) {
244 m = &magic[++magindex];
245 ms->line = m->lineno; /* for messages */
247 if (cont_level < m->cont_level)
249 if (cont_level > m->cont_level) {
251 * We're at the end of the level
252 * "cont_level" continuations.
254 cont_level = m->cont_level;
256 ms->offset = m->offset;
257 if (m->flag & OFFADD) {
259 ms->c.li[cont_level - 1].off;
262 #ifdef ENABLE_CONDITIONALS
263 if (m->cond == COND_ELSE ||
264 m->cond == COND_ELIF) {
265 if (ms->c.li[cont_level].last_match == 1)
269 switch (mget(ms, s, m, nbytes, offset, cont_level, mode,
270 text, flip, indir_level, name_count,
271 printed_something, need_separator, returnval)) {
280 if (m->type == FILE_INDIRECT)
286 switch (flush ? 1 : magiccheck(ms, m)) {
290 #ifdef ENABLE_CONDITIONALS
291 ms->c.li[cont_level].last_match = 0;
295 #ifdef ENABLE_CONDITIONALS
296 ms->c.li[cont_level].last_match = 1;
298 if (m->type == FILE_CLEAR)
299 ms->c.li[cont_level].got_match = 0;
300 else if (ms->c.li[cont_level].got_match) {
301 if (m->type == FILE_DEFAULT)
304 ms->c.li[cont_level].got_match = 1;
305 if ((e = handle_annotation(ms, m)) != 0) {
307 *printed_something = 1;
312 * If we are going to print something,
313 * make sure that we have a separator first.
316 if (!*printed_something) {
317 *printed_something = 1;
318 if (print_sep(ms, firstline)
324 * This continuation matched. Print
325 * its message, with a blank before it
326 * if the previous item printed and
327 * this item isn't empty.
329 /* space if previous printed */
331 && ((m->flag & NOSPACE) == 0)
334 file_printf(ms, " ") == -1)
338 if (print && mprint(ms, m) == -1)
341 ms->c.li[cont_level].off = moffset(ms, m);
347 * If we see any continuations
351 if (file_check_mem(ms, ++cont_level) == -1)
356 if (*printed_something) {
361 if ((ms->flags & MAGIC_CONTINUE) == 0 && *printed_something) {
362 return *returnval; /* don't keep searching */
365 return *returnval; /* This is hit if -k is set or there is no match */
369 check_fmt(struct magic_set *ms, struct magic *m)
374 if (strchr(m->desc, '%') == NULL)
377 rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
379 file_regerror(&rx, rc, ms);
381 rc = file_regexec(&rx, m->desc, 0, 0, 0);
389 char * strndup(const char *, size_t);
392 strndup(const char *str, size_t n)
397 for (len = 0; len < n && str[len]; len++)
399 if ((copy = malloc(len + 1)) == NULL)
401 (void)memcpy(copy, str, len);
405 #endif /* HAVE_STRNDUP */
408 printable(char *buf, size_t bufsiz, const char *str)
411 const unsigned char *s = (const unsigned char *)str;
413 for (ptr = buf, eptr = ptr + bufsiz - 1; ptr < eptr && *s; s++) {
421 *ptr++ = ((*s >> 6) & 7) + '0';
422 *ptr++ = ((*s >> 3) & 7) + '0';
423 *ptr++ = ((*s >> 0) & 7) + '0';
430 mprint(struct magic_set *ms, struct magic *m)
436 char buf[128], tbuf[26];
437 union VALUETYPE *p = &ms->ms_value;
441 v = file_signextend(ms, m, (uint64_t)p->b);
442 switch (check_fmt(ms, m)) {
446 (void)snprintf(buf, sizeof(buf), "%d",
448 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
452 if (file_printf(ms, F(ms, m, "%d"),
453 (unsigned char) v) == -1)
457 t = ms->offset + sizeof(char);
463 v = file_signextend(ms, m, (uint64_t)p->h);
464 switch (check_fmt(ms, m)) {
468 (void)snprintf(buf, sizeof(buf), "%u",
470 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
474 if (file_printf(ms, F(ms, m, "%u"),
475 (unsigned short) v) == -1)
479 t = ms->offset + sizeof(short);
486 v = file_signextend(ms, m, (uint64_t)p->l);
487 switch (check_fmt(ms, m)) {
491 (void)snprintf(buf, sizeof(buf), "%u", (uint32_t) v);
492 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
496 if (file_printf(ms, F(ms, m, "%u"), (uint32_t) v) == -1)
500 t = ms->offset + sizeof(int32_t);
506 v = file_signextend(ms, m, p->q);
507 switch (check_fmt(ms, m)) {
511 (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
512 (unsigned long long)v);
513 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
517 if (file_printf(ms, F(ms, m, "%" INT64_T_FORMAT "u"),
518 (unsigned long long) v) == -1)
522 t = ms->offset + sizeof(int64_t);
527 case FILE_BESTRING16:
528 case FILE_LESTRING16:
529 if (m->reln == '=' || m->reln == '!') {
530 if (file_printf(ms, F(ms, m, "%s"), m->value.s) == -1)
532 t = ms->offset + m->vallen;
538 /* compute t before we mangle the string? */
539 t = ms->offset + strlen(str);
541 if (*m->value.s == '\0')
542 str[strcspn(str, "\n")] = '\0';
544 if (m->str_flags & STRING_TRIM) {
546 while (isspace((unsigned char)*str))
552 while (isspace((unsigned char)*last))
557 if (file_printf(ms, F(ms, m, "%s"),
558 printable(sbuf, sizeof(sbuf), str)) == -1)
561 if (m->type == FILE_PSTRING)
562 t += file_pstring_length_size(m);
570 if (file_printf(ms, F(ms, m, "%s"),
571 file_fmttime(p->l + m->num_mask, FILE_T_LOCAL, tbuf)) == -1)
573 t = ms->offset + sizeof(uint32_t);
580 if (file_printf(ms, F(ms, m, "%s"),
581 file_fmttime(p->l + m->num_mask, 0, tbuf)) == -1)
583 t = ms->offset + sizeof(uint32_t);
589 if (file_printf(ms, F(ms, m, "%s"),
590 file_fmttime(p->q + m->num_mask, FILE_T_LOCAL, tbuf)) == -1)
592 t = ms->offset + sizeof(uint64_t);
598 if (file_printf(ms, F(ms, m, "%s"),
599 file_fmttime(p->q + m->num_mask, 0, tbuf)) == -1)
601 t = ms->offset + sizeof(uint64_t);
607 if (file_printf(ms, F(ms, m, "%s"),
608 file_fmttime(p->q + m->num_mask, FILE_T_WINDOWS, tbuf)) == -1)
610 t = ms->offset + sizeof(uint64_t);
617 switch (check_fmt(ms, m)) {
621 (void)snprintf(buf, sizeof(buf), "%g", vf);
622 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
626 if (file_printf(ms, F(ms, m, "%g"), vf) == -1)
630 t = ms->offset + sizeof(float);
637 switch (check_fmt(ms, m)) {
641 (void)snprintf(buf, sizeof(buf), "%g", vd);
642 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
646 if (file_printf(ms, F(ms, m, "%g"), vd) == -1)
650 t = ms->offset + sizeof(double);
657 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
659 file_oomem(ms, ms->search.rm_len);
662 rval = file_printf(ms, F(ms, m, "%s"), cp);
668 if ((m->str_flags & REGEX_OFFSET_START))
669 t = ms->search.offset;
671 t = ms->search.offset + ms->search.rm_len;
676 if (file_printf(ms, F(ms, m, "%s"), m->value.s) == -1)
678 if ((m->str_flags & REGEX_OFFSET_START))
679 t = ms->search.offset;
681 t = ms->search.offset + m->vallen;
686 if (file_printf(ms, "%s", m->desc) == -1)
698 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
705 moffset(struct magic_set *ms, struct magic *m)
709 return CAST(int32_t, (ms->offset + sizeof(char)));
714 return CAST(int32_t, (ms->offset + sizeof(short)));
720 return CAST(int32_t, (ms->offset + sizeof(int32_t)));
725 return CAST(int32_t, (ms->offset + sizeof(int64_t)));
729 case FILE_BESTRING16:
730 case FILE_LESTRING16:
731 if (m->reln == '=' || m->reln == '!')
732 return ms->offset + m->vallen;
734 union VALUETYPE *p = &ms->ms_value;
737 if (*m->value.s == '\0')
738 p->s[strcspn(p->s, "\n")] = '\0';
739 t = CAST(uint32_t, (ms->offset + strlen(p->s)));
740 if (m->type == FILE_PSTRING)
741 t += (uint32_t)file_pstring_length_size(m);
749 return CAST(int32_t, (ms->offset + sizeof(uint32_t)));
755 return CAST(int32_t, (ms->offset + sizeof(uint32_t)));
760 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
765 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
770 return CAST(int32_t, (ms->offset + sizeof(float)));
775 return CAST(int32_t, (ms->offset + sizeof(double)));
778 if ((m->str_flags & REGEX_OFFSET_START) != 0)
779 return CAST(int32_t, ms->search.offset);
781 return CAST(int32_t, (ms->search.offset +
785 if ((m->str_flags & REGEX_OFFSET_START) != 0)
786 return CAST(int32_t, ms->search.offset);
788 return CAST(int32_t, (ms->search.offset + m->vallen));
801 cvt_flip(int type, int flip)
819 return FILE_LEQLDATE;
821 return FILE_LEQWDATE;
835 return FILE_BEQLDATE;
837 return FILE_BEQWDATE;
843 return FILE_LEDOUBLE;
845 return FILE_BEDOUBLE;
850 #define DO_CVT(fld, cast) \
852 switch (m->mask_op & FILE_OPS_MASK) { \
854 p->fld &= cast m->num_mask; \
857 p->fld |= cast m->num_mask; \
860 p->fld ^= cast m->num_mask; \
863 p->fld += cast m->num_mask; \
866 p->fld -= cast m->num_mask; \
868 case FILE_OPMULTIPLY: \
869 p->fld *= cast m->num_mask; \
871 case FILE_OPDIVIDE: \
872 p->fld /= cast m->num_mask; \
874 case FILE_OPMODULO: \
875 p->fld %= cast m->num_mask; \
878 if (m->mask_op & FILE_OPINVERSE) \
882 cvt_8(union VALUETYPE *p, const struct magic *m)
884 DO_CVT(b, (uint8_t));
888 cvt_16(union VALUETYPE *p, const struct magic *m)
890 DO_CVT(h, (uint16_t));
894 cvt_32(union VALUETYPE *p, const struct magic *m)
896 DO_CVT(l, (uint32_t));
900 cvt_64(union VALUETYPE *p, const struct magic *m)
902 DO_CVT(q, (uint64_t));
905 #define DO_CVT2(fld, cast) \
907 switch (m->mask_op & FILE_OPS_MASK) { \
909 p->fld += cast m->num_mask; \
912 p->fld -= cast m->num_mask; \
914 case FILE_OPMULTIPLY: \
915 p->fld *= cast m->num_mask; \
917 case FILE_OPDIVIDE: \
918 p->fld /= cast m->num_mask; \
923 cvt_float(union VALUETYPE *p, const struct magic *m)
929 cvt_double(union VALUETYPE *p, const struct magic *m)
931 DO_CVT2(d, (double));
935 * Convert the byte order of the data we are looking at
936 * While we're here, let's apply the mask operation
937 * (unless you have a better idea)
940 mconvert(struct magic_set *ms, struct magic *m, int flip)
942 union VALUETYPE *p = &ms->ms_value;
945 switch (type = cvt_flip(m->type, flip)) {
964 case FILE_BESTRING16:
965 case FILE_LESTRING16: {
966 /* Null terminate and eat *trailing* return */
967 p->s[sizeof(p->s) - 1] = '\0';
971 size_t sz = file_pstring_length_size(m);
972 char *ptr1 = p->s, *ptr2 = ptr1 + sz;
973 size_t len = file_pstring_get_length(m, ptr1);
974 sz = sizeof(p->s) - sz; /* maximum length of string */
977 * The size of the pascal string length (sz)
978 * is 1, 2, or 4. We need at least 1 byte for NUL
979 * termination, but we've already truncated the
980 * string by p->s, so we need to deduct sz.
981 * Because we can use one of the bytes of the length
982 * after we shifted as NUL termination.
992 p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
999 ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
1000 if (type == FILE_BELONG)
1008 (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
1009 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
1010 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
1011 ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
1012 if (type == FILE_BEQUAD)
1016 p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
1023 ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
1024 if (type == FILE_LELONG)
1032 (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
1033 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
1034 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
1035 ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
1036 if (type == FILE_LEQUAD)
1043 ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
1044 if (type == FILE_MELONG)
1051 p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
1052 ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
1056 p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
1057 ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
1064 p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
1065 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
1066 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
1067 ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
1071 p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
1072 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
1073 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
1074 ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
1085 file_magerror(ms, "invalid type %d in mconvert()", m->type);
1092 mdebug(uint32_t offset, const char *str, size_t len)
1094 (void) fprintf(stderr, "mget/%" SIZE_T_FORMAT "u @%d: ", len, offset);
1095 file_showstr(stderr, str, len);
1096 (void) fputc('\n', stderr);
1097 (void) fputc('\n', stderr);
1101 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
1102 const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
1105 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1106 * anything, but setup pointers into the source
1111 ms->search.s = RCAST(const char *, s) + offset;
1112 ms->search.s_len = nbytes - offset;
1113 ms->search.offset = offset;
1119 const char *last; /* end of search region */
1120 const char *buf; /* start of search region */
1122 size_t lines, linecnt, bytecnt;
1125 ms->search.s_len = 0;
1126 ms->search.s = NULL;
1130 if (m->str_flags & REGEX_LINE_COUNT) {
1131 linecnt = m->str_range;
1132 bytecnt = linecnt * 80;
1135 bytecnt = m->str_range;
1140 if (bytecnt > nbytes)
1143 buf = RCAST(const char *, s) + offset;
1144 end = last = RCAST(const char *, s) + bytecnt;
1145 /* mget() guarantees buf <= last */
1146 for (lines = linecnt, b = buf; lines && b < end &&
1147 ((b = CAST(const char *,
1148 memchr(c = b, '\n', CAST(size_t, (end - b)))))
1149 || (b = CAST(const char *,
1150 memchr(c, '\r', CAST(size_t, (end - c))))));
1153 if (b[0] == '\r' && b[1] == '\n')
1157 last = RCAST(const char *, s) + bytecnt;
1160 ms->search.s_len = last - buf;
1161 ms->search.offset = offset;
1162 ms->search.rm_len = 0;
1165 case FILE_BESTRING16:
1166 case FILE_LESTRING16: {
1167 const unsigned char *src = s + offset;
1168 const unsigned char *esrc = s + nbytes;
1170 char *edst = &p->s[sizeof(p->s) - 1];
1172 if (type == FILE_BESTRING16)
1175 /* check that offset is within range */
1176 if (offset >= nbytes)
1178 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
1184 if (type == FILE_BESTRING16 ?
1185 *(src - 1) != '\0' :
1193 case FILE_STRING: /* XXX - these two should not need */
1194 case FILE_PSTRING: /* to copy anything, but do anyway. */
1200 if (offset >= nbytes) {
1201 (void)memset(p, '\0', sizeof(*p));
1204 if (nbytes - offset < sizeof(*p))
1205 nbytes = nbytes - offset;
1207 nbytes = sizeof(*p);
1209 (void)memcpy(p, s + offset, nbytes);
1212 * the usefulness of padding with zeroes eludes me, it
1213 * might even cause problems
1215 if (nbytes < sizeof(*p))
1216 (void)memset(((char *)(void *)p) + nbytes, '\0',
1217 sizeof(*p) - nbytes);
1222 mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
1223 size_t nbytes, size_t o, unsigned int cont_level, int mode, int text,
1224 int flip, uint16_t indir_level, uint16_t *name_count,
1225 int *printed_something, int *need_separator, int *returnval)
1227 uint32_t offset = ms->offset;
1230 int rv, oneed_separator, in_type;
1232 union VALUETYPE *p = &ms->ms_value;
1235 if (indir_level >= ms->indir_max) {
1236 file_error(ms, 0, "indirect recursion nesting (%hu) exceeded",
1241 if (*name_count >= ms->name_max) {
1242 file_error(ms, 0, "name use count (%hu) exceeded",
1247 if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
1248 (uint32_t)nbytes, m) == -1)
1251 if ((ms->flags & MAGIC_DEBUG) != 0) {
1252 fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%"
1253 SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
1254 "u, il=%hu, nc=%hu)\n",
1255 m->type, m->flag, offset, o, nbytes,
1256 indir_level, *name_count);
1257 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1258 #ifndef COMPILE_ONLY
1263 if (m->flag & INDIR) {
1264 int off = m->in_offset;
1265 if (m->in_op & FILE_OPINDIRECT) {
1266 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1267 ((const void *)(s + offset + off)));
1268 switch (cvt_flip(m->in_type, flip)) {
1276 off = (short)((q->hs[0]<<8)|(q->hs[1]));
1279 off = (short)((q->hs[1]<<8)|(q->hs[0]));
1286 off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
1287 (q->hl[2]<<8)|(q->hl[3]));
1291 off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
1292 (q->hl[1]<<8)|(q->hl[0]));
1295 off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
1296 (q->hl[3]<<8)|(q->hl[2]));
1299 if ((ms->flags & MAGIC_DEBUG) != 0)
1300 fprintf(stderr, "indirect offs=%u\n", off);
1302 switch (in_type = cvt_flip(m->in_type, flip)) {
1304 if (OFFSET_OOB(nbytes, offset, 1))
1307 switch (m->in_op & FILE_OPS_MASK) {
1309 offset = p->b & off;
1312 offset = p->b | off;
1315 offset = p->b ^ off;
1318 offset = p->b + off;
1321 offset = p->b - off;
1323 case FILE_OPMULTIPLY:
1324 offset = p->b * off;
1327 offset = p->b / off;
1330 offset = p->b % off;
1335 if (m->in_op & FILE_OPINVERSE)
1339 if (OFFSET_OOB(nbytes, offset, 2))
1341 lhs = (p->hs[0] << 8) | p->hs[1];
1343 switch (m->in_op & FILE_OPS_MASK) {
1359 case FILE_OPMULTIPLY:
1371 if (m->in_op & FILE_OPINVERSE)
1375 if (OFFSET_OOB(nbytes, offset, 2))
1377 lhs = (p->hs[1] << 8) | p->hs[0];
1379 switch (m->in_op & FILE_OPS_MASK) {
1395 case FILE_OPMULTIPLY:
1407 if (m->in_op & FILE_OPINVERSE)
1411 if (OFFSET_OOB(nbytes, offset, 2))
1414 switch (m->in_op & FILE_OPS_MASK) {
1416 offset = p->h & off;
1419 offset = p->h | off;
1422 offset = p->h ^ off;
1425 offset = p->h + off;
1428 offset = p->h - off;
1430 case FILE_OPMULTIPLY:
1431 offset = p->h * off;
1434 offset = p->h / off;
1437 offset = p->h % off;
1443 if (m->in_op & FILE_OPINVERSE)
1448 if (OFFSET_OOB(nbytes, offset, 4))
1450 lhs = (p->hl[0] << 24) | (p->hl[1] << 16) |
1451 (p->hl[2] << 8) | p->hl[3];
1453 switch (m->in_op & FILE_OPS_MASK) {
1469 case FILE_OPMULTIPLY:
1481 if (m->in_op & FILE_OPINVERSE)
1486 if (OFFSET_OOB(nbytes, offset, 4))
1488 lhs = (p->hl[3] << 24) | (p->hl[2] << 16) |
1489 (p->hl[1] << 8) | p->hl[0];
1491 switch (m->in_op & FILE_OPS_MASK) {
1507 case FILE_OPMULTIPLY:
1519 if (m->in_op & FILE_OPINVERSE)
1523 if (OFFSET_OOB(nbytes, offset, 4))
1525 lhs = (p->hl[1] << 24) | (p->hl[0] << 16) |
1526 (p->hl[3] << 8) | p->hl[2];
1528 switch (m->in_op & FILE_OPS_MASK) {
1544 case FILE_OPMULTIPLY:
1556 if (m->in_op & FILE_OPINVERSE)
1560 if (OFFSET_OOB(nbytes, offset, 4))
1563 switch (m->in_op & FILE_OPS_MASK) {
1565 offset = p->l & off;
1568 offset = p->l | off;
1571 offset = p->l ^ off;
1574 offset = p->l + off;
1577 offset = p->l - off;
1579 case FILE_OPMULTIPLY:
1580 offset = p->l * off;
1583 offset = p->l / off;
1586 offset = p->l % off;
1591 if (m->in_op & FILE_OPINVERSE)
1601 offset = ((((offset >> 0) & 0x7f) << 0) |
1602 (((offset >> 8) & 0x7f) << 7) |
1603 (((offset >> 16) & 0x7f) << 14) |
1604 (((offset >> 24) & 0x7f) << 21)) + 10;
1610 if (m->flag & INDIROFFADD) {
1611 offset += ms->c.li[cont_level-1].off;
1613 if ((ms->flags & MAGIC_DEBUG) != 0)
1615 "indirect *zero* offset\n");
1618 if ((ms->flags & MAGIC_DEBUG) != 0)
1619 fprintf(stderr, "indirect +offs=%u\n", offset);
1621 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
1623 ms->offset = offset;
1625 if ((ms->flags & MAGIC_DEBUG) != 0) {
1626 mdebug(offset, (char *)(void *)p,
1627 sizeof(union VALUETYPE));
1628 #ifndef COMPILE_ONLY
1634 /* Verify we have enough data to match magic type */
1637 if (OFFSET_OOB(nbytes, offset, 1))
1644 if (OFFSET_OOB(nbytes, offset, 2))
1663 if (OFFSET_OOB(nbytes, offset, 4))
1670 if (OFFSET_OOB(nbytes, offset, 8))
1677 if (OFFSET_OOB(nbytes, offset, m->vallen))
1682 if (nbytes < offset)
1690 if (nbytes < offset)
1693 if ((pb = file_push_buffer(ms)) == NULL)
1696 rv = file_softmagic(ms, s + offset, nbytes - offset,
1697 indir_level + 1, name_count, BINTEST, text);
1699 if ((ms->flags & MAGIC_DEBUG) != 0)
1700 fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1702 rbuf = file_pop_buffer(ms, pb);
1703 if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
1707 if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
1708 file_printf(ms, F(ms, m, "%u"), offset) == -1) {
1712 if (file_printf(ms, "%s", rbuf) == -1) {
1721 if (nbytes < offset)
1728 if (file_magicfind(ms, rbuf, &ml) == -1) {
1729 file_error(ms, 0, "cannot find entry `%s'", rbuf);
1733 oneed_separator = *need_separator;
1734 if (m->flag & NOSPACE)
1735 *need_separator = 0;
1736 rv = match(ms, ml.magic, ml.nmagic, s, nbytes, offset + o,
1737 mode, text, flip, indir_level, name_count,
1738 printed_something, need_separator, returnval);
1740 *need_separator = oneed_separator;
1744 if (file_printf(ms, "%s", m->desc) == -1)
1747 case FILE_DEFAULT: /* nothing to check */
1752 if (!mconvert(ms, m, flip))
1758 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1761 * Convert the source args to unsigned here so that (1) the
1762 * compare will be unsigned as it is in strncmp() and (2) so
1763 * the ctype functions will work correctly without extra
1766 const unsigned char *a = (const unsigned char *)s1;
1767 const unsigned char *b = (const unsigned char *)s2;
1771 * What we want here is v = strncmp(s1, s2, len),
1772 * but ignoring any nulls.
1775 if (0L == flags) { /* normal string: do it fast */
1777 if ((v = *b++ - *a++) != '\0')
1780 else { /* combine the others */
1782 if ((flags & STRING_IGNORE_LOWERCASE) &&
1784 if ((v = tolower(*b++) - *a++) != '\0')
1787 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1789 if ((v = toupper(*b++) - *a++) != '\0')
1792 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1795 if (isspace(*b++)) {
1805 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1812 if ((v = *b++ - *a++) != '\0')
1821 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1824 * XXX - The 16-bit string compare probably needs to be done
1825 * differently, especially if the flags are to be supported.
1826 * At the moment, I am unsure.
1829 return file_strncmp(a, b, len, flags);
1833 magiccheck(struct magic_set *ms, struct magic *m)
1835 uint64_t l = m->value.q;
1840 union VALUETYPE *p = &ms->ms_value;
1910 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1943 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
1957 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1960 case FILE_BESTRING16:
1961 case FILE_LESTRING16:
1963 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1966 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1970 if (ms->search.s == NULL)
1973 slen = MIN(m->vallen, sizeof(m->value.s));
1977 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
1978 if (slen + idx > ms->search.s_len)
1981 v = file_strncmp(m->value.s, ms->search.s + idx, slen,
1983 if (v == 0) { /* found match */
1984 ms->search.offset += idx;
1995 if (ms->search.s == NULL)
1999 rc = file_regcomp(&rx, m->value.s,
2000 REG_EXTENDED|REG_NEWLINE|
2001 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
2003 file_regerror(&rx, rc, ms);
2006 regmatch_t pmatch[1];
2007 size_t slen = ms->search.s_len;
2008 #ifndef REG_STARTEND
2009 #define REG_STARTEND 0
2012 copy = malloc(slen);
2014 file_error(ms, errno,
2015 "can't allocate %" SIZE_T_FORMAT "u bytes",
2019 memcpy(copy, ms->search.s, slen);
2020 copy[--slen] = '\0';
2023 search = ms->search.s;
2027 search = ms->search.s;
2028 pmatch[0].rm_so = 0;
2029 pmatch[0].rm_eo = slen;
2031 rc = file_regexec(&rx, (const char *)search,
2032 1, pmatch, REG_STARTEND);
2033 #if REG_STARTEND == 0
2038 ms->search.s += (int)pmatch[0].rm_so;
2039 ms->search.offset += (size_t)pmatch[0].rm_so;
2041 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
2050 file_regerror(&rx, rc, ms);
2056 if (v == (uint64_t)-1)
2065 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2069 v = file_signextend(ms, m, v);
2073 if ((ms->flags & MAGIC_DEBUG) != 0)
2074 (void) fprintf(stderr, "%" INT64_T_FORMAT
2075 "u == *any* = 1\n", (unsigned long long)v);
2081 if ((ms->flags & MAGIC_DEBUG) != 0)
2082 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
2083 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2084 (unsigned long long)l, matched);
2089 if ((ms->flags & MAGIC_DEBUG) != 0)
2090 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2091 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2092 (unsigned long long)l, matched);
2096 if (m->flag & UNSIGNED) {
2098 if ((ms->flags & MAGIC_DEBUG) != 0)
2099 (void) fprintf(stderr, "%" INT64_T_FORMAT
2100 "u > %" INT64_T_FORMAT "u = %d\n",
2101 (unsigned long long)v,
2102 (unsigned long long)l, matched);
2105 matched = (int64_t) v > (int64_t) l;
2106 if ((ms->flags & MAGIC_DEBUG) != 0)
2107 (void) fprintf(stderr, "%" INT64_T_FORMAT
2108 "d > %" INT64_T_FORMAT "d = %d\n",
2109 (long long)v, (long long)l, matched);
2114 if (m->flag & UNSIGNED) {
2116 if ((ms->flags & MAGIC_DEBUG) != 0)
2117 (void) fprintf(stderr, "%" INT64_T_FORMAT
2118 "u < %" INT64_T_FORMAT "u = %d\n",
2119 (unsigned long long)v,
2120 (unsigned long long)l, matched);
2123 matched = (int64_t) v < (int64_t) l;
2124 if ((ms->flags & MAGIC_DEBUG) != 0)
2125 (void) fprintf(stderr, "%" INT64_T_FORMAT
2126 "d < %" INT64_T_FORMAT "d = %d\n",
2127 (long long)v, (long long)l, matched);
2132 matched = (v & l) == l;
2133 if ((ms->flags & MAGIC_DEBUG) != 0)
2134 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2135 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2136 "x) = %d\n", (unsigned long long)v,
2137 (unsigned long long)l, (unsigned long long)l,
2142 matched = (v & l) != l;
2143 if ((ms->flags & MAGIC_DEBUG) != 0)
2144 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2145 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2146 "x) = %d\n", (unsigned long long)v,
2147 (unsigned long long)l, (unsigned long long)l,
2152 file_magerror(ms, "cannot happen: invalid relation `%c'",
2161 handle_annotation(struct magic_set *ms, struct magic *m)
2163 if (ms->flags & MAGIC_APPLE) {
2164 if (file_printf(ms, "%.8s", m->apple) == -1)
2168 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2169 if (file_printf(ms, "%s", m->mimetype) == -1)
2177 print_sep(struct magic_set *ms, int firstline)
2179 if (ms->flags & MAGIC_MIME)
2184 * we found another match
2185 * put a newline and '-' to do some simple formatting
2187 return file_printf(ms, "\n- ");