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.259 2018/03/11 01:23:52 christos Exp $")
46 private int match(struct magic_set *, struct magic *, uint32_t,
47 const struct buffer *, size_t, int, int, int, uint16_t *,
48 uint16_t *, int *, int *, int *);
49 private int mget(struct magic_set *, struct magic *, const struct buffer *,
50 const unsigned char *, size_t,
51 size_t, unsigned int, int, int, int, uint16_t *,
52 uint16_t *, int *, int *, int *);
53 private int msetoffset(struct magic_set *, struct magic *, struct buffer *,
54 const struct buffer *, size_t, unsigned int);
55 private int magiccheck(struct magic_set *, struct magic *);
56 private int32_t mprint(struct magic_set *, struct magic *,
57 const struct buffer *);
58 private int moffset(struct magic_set *, struct magic *, const struct buffer *,
60 private void mdebug(uint32_t, const char *, size_t);
61 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
62 const unsigned char *, uint32_t, size_t, struct magic *);
63 private int mconvert(struct magic_set *, struct magic *, int);
64 private int print_sep(struct magic_set *, int);
65 private int handle_annotation(struct magic_set *, struct magic *,
66 const struct buffer *, int);
67 private int cvt_8(union VALUETYPE *, const struct magic *);
68 private int cvt_16(union VALUETYPE *, const struct magic *);
69 private int cvt_32(union VALUETYPE *, const struct magic *);
70 private int cvt_64(union VALUETYPE *, const struct magic *);
72 #define OFFSET_OOB(n, o, i) ((n) < (uint32_t)(o) || (i) > ((n) - (o)))
73 #define BE64(p) (((uint64_t)(p)->hq[0]<<56)|((uint64_t)(p)->hq[1]<<48)| \
74 ((uint64_t)(p)->hq[2]<<40)|((uint64_t)(p)->hq[3]<<32)| \
75 ((uint64_t)(p)->hq[4]<<24)|((uint64_t)(p)->hq[5]<<16)| \
76 ((uint64_t)(p)->hq[6]<<8)|((uint64_t)(p)->hq[7]))
77 #define LE64(p) (((uint64_t)(p)->hq[7]<<56)|((uint64_t)(p)->hq[6]<<48)| \
78 ((uint64_t)(p)->hq[5]<<40)|((uint64_t)(p)->hq[4]<<32)| \
79 ((uint64_t)(p)->hq[3]<<24)|((uint64_t)(p)->hq[2]<<16)| \
80 ((uint64_t)(p)->hq[1]<<8)|((uint64_t)(p)->hq[0]))
81 #define LE32(p) (((uint32_t)(p)->hl[3]<<24)|((uint32_t)(p)->hl[2]<<16)| \
82 ((uint32_t)(p)->hl[1]<<8)|((uint32_t)(p)->hl[0]))
83 #define BE32(p) (((uint32_t)(p)->hl[0]<<24)|((uint32_t)(p)->hl[1]<<16)| \
84 ((uint32_t)(p)->hl[2]<<8)|((uint32_t)(p)->hl[3]))
85 #define ME32(p) (((uint32_t)(p)->hl[1]<<24)|((uint32_t)(p)->hl[0]<<16)| \
86 ((uint32_t)(p)->hl[3]<<8)|((uint32_t)(p)->hl[2]))
87 #define BE16(p) (((uint16_t)(p)->hs[0]<<8)|((uint16_t)(p)->hs[1]))
88 #define LE16(p) (((uint16_t)(p)->hs[1]<<8)|((uint16_t)(p)->hs[0]))
89 #define SEXT(s,v,p) ((s)?(intmax_t)(int##v##_t)(p):(intmax_t)(uint##v##_t)(p))
92 * softmagic - lookup one file in parsed, in-memory copy of database
93 * Passed the name and FILE * of one file to be typed.
95 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
97 file_softmagic(struct magic_set *ms, const struct buffer *b,
98 uint16_t *indir_count, uint16_t *name_count, int mode, int text)
101 int rv, printed_something = 0, need_separator = 0;
104 if (name_count == NULL) {
108 if (indir_count == NULL) {
113 for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
114 if ((rv = match(ms, ml->magic, ml->nmagic, b, 0, mode,
115 text, 0, indir_count, name_count,
116 &printed_something, &need_separator, NULL)) != 0)
122 #define FILE_FMTDEBUG
124 #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
126 private const char * __attribute__((__format_arg__(3)))
127 file_fmtcheck(struct magic_set *ms, const char *desc, const char *def,
128 const char *file, size_t line)
130 const char *ptr = fmtcheck(desc, def);
133 "%s, %" SIZE_T_FORMAT "u: format `%s' does not match"
134 " with `%s'", file, line, desc, def);
138 #define F(a, b, c) fmtcheck((b), (c))
142 * Go through the whole list, stopping if you find a match. Process all
143 * the continuations of that match before returning.
145 * We support multi-level continuations:
147 * At any time when processing a successful top-level match, there is a
148 * current continuation level; it represents the level of the last
149 * successfully matched continuation.
151 * Continuations above that level are skipped as, if we see one, it
152 * means that the continuation that controls them - i.e, the
153 * lower-level continuation preceding them - failed to match.
155 * Continuations below that level are processed as, if we see one,
156 * it means we've finished processing or skipping higher-level
157 * continuations under the control of a successful or unsuccessful
158 * lower-level continuation, and are now seeing the next lower-level
159 * continuation and should process it. The current continuation
160 * level reverts to the level of the one we're seeing.
162 * Continuations at the current level are processed as, if we see
163 * one, there's no lower-level continuation that may have failed.
165 * If a continuation matches, we bump the current continuation level
166 * so that higher-level continuations are processed.
169 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
170 const struct buffer *b, size_t offset, int mode, int text,
171 int flip, uint16_t *indir_count, uint16_t *name_count,
172 int *printed_something, int *need_separator, int *returnval)
174 uint32_t magindex = 0;
175 unsigned int cont_level = 0;
176 int returnvalv = 0, e; /* if a match is found it is set to 1*/
177 int firstline = 1; /* a flag to print X\n X\n- X */
179 int print = (ms->flags & MAGIC_NODESC) == 0;
181 if (returnval == NULL)
182 returnval = &returnvalv;
184 if (file_check_mem(ms, cont_level) == -1)
187 for (magindex = 0; magindex < nmagic; magindex++) {
189 struct magic *m = &magic[magindex];
191 if (m->type != FILE_NAME)
192 if ((IS_STRING(m->type) &&
193 #define FLT (STRING_BINTEST | STRING_TEXTTEST)
194 ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
195 (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
196 (m->flag & mode) != mode) {
199 while (magindex < nmagic - 1 &&
200 magic[magindex + 1].cont_level != 0)
203 continue; /* Skip to next top-level test*/
206 if (msetoffset(ms, m, &bb, b, offset, cont_level) == -1)
208 ms->line = m->lineno;
210 /* if main entry matches, print it... */
211 switch (mget(ms, m, b, bb.fbuf, bb.flen, offset, cont_level,
212 mode, text, flip, indir_count, name_count,
213 printed_something, need_separator, returnval)) {
217 flush = m->reln != '!';
220 if (m->type == FILE_INDIRECT)
223 switch (magiccheck(ms, m)) {
237 * main entry didn't match,
238 * flush its continuations
243 if ((e = handle_annotation(ms, m, b, firstline)) != 0) {
245 *printed_something = 1;
251 * If we are going to print something, we'll need to print
252 * a blank before we print something else.
256 *printed_something = 1;
257 if (print_sep(ms, firstline) == -1)
261 if (print && mprint(ms, m, b) == -1)
264 switch (moffset(ms, m, &bb, &ms->c.li[cont_level].off)) {
272 /* and any continuations that match */
273 if (file_check_mem(ms, ++cont_level) == -1)
276 while (magindex + 1 < nmagic &&
277 magic[magindex + 1].cont_level != 0) {
278 m = &magic[++magindex];
279 ms->line = m->lineno; /* for messages */
281 if (cont_level < m->cont_level)
283 if (cont_level > m->cont_level) {
285 * We're at the end of the level
286 * "cont_level" continuations.
288 cont_level = m->cont_level;
290 if (msetoffset(ms, m, &bb, b, offset, cont_level) == -1)
292 if (m->flag & OFFADD) {
294 ms->c.li[cont_level - 1].off;
297 #ifdef ENABLE_CONDITIONALS
298 if (m->cond == COND_ELSE ||
299 m->cond == COND_ELIF) {
300 if (ms->c.li[cont_level].last_match == 1)
304 switch (mget(ms, m, b, bb.fbuf, bb.flen, offset,
305 cont_level, mode, text, flip, indir_count,
306 name_count, printed_something, need_separator,
316 if (m->type == FILE_INDIRECT)
322 switch (flush ? 1 : magiccheck(ms, m)) {
326 #ifdef ENABLE_CONDITIONALS
327 ms->c.li[cont_level].last_match = 0;
331 #ifdef ENABLE_CONDITIONALS
332 ms->c.li[cont_level].last_match = 1;
334 if (m->type == FILE_CLEAR)
335 ms->c.li[cont_level].got_match = 0;
336 else if (ms->c.li[cont_level].got_match) {
337 if (m->type == FILE_DEFAULT)
340 ms->c.li[cont_level].got_match = 1;
342 if ((e = handle_annotation(ms, m, b, firstline))
345 *printed_something = 1;
350 * If we are going to print something,
351 * make sure that we have a separator first.
354 if (!*printed_something) {
355 *printed_something = 1;
356 if (print_sep(ms, firstline)
362 * This continuation matched. Print
363 * its message, with a blank before it
364 * if the previous item printed and
365 * this item isn't empty.
367 /* space if previous printed */
369 && ((m->flag & NOSPACE) == 0)
372 file_printf(ms, " ") == -1)
376 if (print && mprint(ms, m, b) == -1)
379 switch (moffset(ms, m, &bb,
380 &ms->c.li[cont_level].off)) {
394 * If we see any continuations
398 if (file_check_mem(ms, ++cont_level) == -1)
403 if (*printed_something) {
408 if ((ms->flags & MAGIC_CONTINUE) == 0 && *printed_something) {
409 return *returnval; /* don't keep searching */
413 return *returnval; /* This is hit if -k is set or there is no match */
417 check_fmt(struct magic_set *ms, const char *fmt)
422 if (strchr(fmt, '%') == NULL)
425 rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
427 file_regerror(&rx, rc, ms);
429 rc = file_regexec(&rx, fmt, 0, 0, 0);
437 char * strndup(const char *, size_t);
440 strndup(const char *str, size_t n)
445 for (len = 0; len < n && str[len]; len++)
447 if ((copy = malloc(len + 1)) == NULL)
449 (void)memcpy(copy, str, len);
453 #endif /* HAVE_STRNDUP */
456 varexpand(char *buf, size_t len, const struct buffer *b, const char *str)
458 const char *ptr, *sptr, *e, *t, *ee, *et;
461 for (sptr = str; (ptr = strstr(sptr, "${")) != NULL;) {
462 l = (size_t)(ptr - sptr);
465 memcpy(buf, sptr, l);
469 if (!*ptr || ptr[1] != '?')
471 for (et = t = ptr + 2; *et && *et != ':'; et++)
475 for (ee = e = et + 1; *ee && *ee != '}'; ee++)
481 if (b->st.st_mode & 0111) {
504 memcpy(buf, sptr, l);
511 mprint(struct magic_set *ms, struct magic *m, const struct buffer *b)
517 char buf[128], tbuf[26], sbuf[512], ebuf[512];
519 union VALUETYPE *p = &ms->ms_value;
521 if (varexpand(ebuf, sizeof(ebuf), b, m->desc) == -1)
528 v = file_signextend(ms, m, (uint64_t)p->b);
529 switch (check_fmt(ms, desc)) {
533 (void)snprintf(buf, sizeof(buf), "%d",
535 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
539 if (file_printf(ms, F(ms, desc, "%d"),
540 (unsigned char) v) == -1)
544 t = ms->offset + sizeof(char);
550 v = file_signextend(ms, m, (uint64_t)p->h);
551 switch (check_fmt(ms, desc)) {
555 (void)snprintf(buf, sizeof(buf), "%u",
557 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
561 if (file_printf(ms, F(ms, desc, "%u"),
562 (unsigned short) v) == -1)
566 t = ms->offset + sizeof(short);
573 v = file_signextend(ms, m, (uint64_t)p->l);
574 switch (check_fmt(ms, desc)) {
578 (void)snprintf(buf, sizeof(buf), "%u", (uint32_t) v);
579 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
583 if (file_printf(ms, F(ms, desc, "%u"), (uint32_t) v) == -1)
587 t = ms->offset + sizeof(int32_t);
593 v = file_signextend(ms, m, p->q);
594 switch (check_fmt(ms, desc)) {
598 (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
599 (unsigned long long)v);
600 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
604 if (file_printf(ms, F(ms, desc, "%" INT64_T_FORMAT "u"),
605 (unsigned long long) v) == -1)
609 t = ms->offset + sizeof(int64_t);
614 case FILE_BESTRING16:
615 case FILE_LESTRING16:
616 if (m->reln == '=' || m->reln == '!') {
617 if (file_printf(ms, F(ms, desc, "%s"),
618 file_printable(sbuf, sizeof(sbuf), m->value.s))
621 t = ms->offset + m->vallen;
626 /* compute t before we mangle the string? */
627 t = ms->offset + strlen(str);
629 if (*m->value.s == '\0')
630 str[strcspn(str, "\r\n")] = '\0';
632 if (m->str_flags & STRING_TRIM) {
634 while (isspace((unsigned char)*str))
640 while (isspace((unsigned char)*last))
645 if (file_printf(ms, F(ms, desc, "%s"),
646 file_printable(sbuf, sizeof(sbuf), str)) == -1)
649 if (m->type == FILE_PSTRING)
650 t += file_pstring_length_size(m);
658 if (file_printf(ms, F(ms, desc, "%s"),
659 file_fmttime(p->l, 0, tbuf)) == -1)
661 t = ms->offset + sizeof(uint32_t);
668 if (file_printf(ms, F(ms, desc, "%s"),
669 file_fmttime(p->l, FILE_T_LOCAL, tbuf)) == -1)
671 t = ms->offset + sizeof(uint32_t);
677 if (file_printf(ms, F(ms, desc, "%s"),
678 file_fmttime(p->q, 0, tbuf)) == -1)
680 t = ms->offset + sizeof(uint64_t);
686 if (file_printf(ms, F(ms, desc, "%s"),
687 file_fmttime(p->q, FILE_T_LOCAL, tbuf)) == -1)
689 t = ms->offset + sizeof(uint64_t);
695 if (file_printf(ms, F(ms, desc, "%s"),
696 file_fmttime(p->q, FILE_T_WINDOWS, tbuf)) == -1)
698 t = ms->offset + sizeof(uint64_t);
705 switch (check_fmt(ms, desc)) {
709 (void)snprintf(buf, sizeof(buf), "%g", vf);
710 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
714 if (file_printf(ms, F(ms, desc, "%g"), vf) == -1)
718 t = ms->offset + sizeof(float);
725 switch (check_fmt(ms, desc)) {
729 (void)snprintf(buf, sizeof(buf), "%g", vd);
730 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
734 if (file_printf(ms, F(ms, desc, "%g"), vd) == -1)
738 t = ms->offset + sizeof(double);
746 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
748 file_oomem(ms, ms->search.rm_len);
751 rval = file_printf(ms, F(ms, desc, "%s"),
752 file_printable(sbuf, sizeof(sbuf), cp));
758 if ((m->str_flags & REGEX_OFFSET_START))
759 t = ms->search.offset;
761 t = ms->search.offset + ms->search.rm_len;
767 if (file_printf(ms, "%s", m->desc) == -1)
778 if (file_printf(ms, F(ms, desc, "%s"),
779 file_printable(sbuf, sizeof(sbuf), ms->ms_value.s)) == -1)
784 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
791 moffset(struct magic_set *ms, struct magic *m, const struct buffer *b,
794 size_t nbytes = b->flen;
799 o = CAST(int32_t, (ms->offset + sizeof(char)));
805 o = CAST(int32_t, (ms->offset + sizeof(short)));
812 o = CAST(int32_t, (ms->offset + sizeof(int32_t)));
818 o = CAST(int32_t, (ms->offset + sizeof(int64_t)));
823 case FILE_BESTRING16:
824 case FILE_LESTRING16:
825 if (m->reln == '=' || m->reln == '!') {
826 o = ms->offset + m->vallen;
828 union VALUETYPE *p = &ms->ms_value;
830 if (*m->value.s == '\0')
831 p->s[strcspn(p->s, "\r\n")] = '\0';
832 o = CAST(uint32_t, (ms->offset + strlen(p->s)));
833 if (m->type == FILE_PSTRING)
834 o += (uint32_t)file_pstring_length_size(m);
842 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
849 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
855 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
861 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
867 o = CAST(int32_t, (ms->offset + sizeof(float)));
873 o = CAST(int32_t, (ms->offset + sizeof(double)));
877 if ((m->str_flags & REGEX_OFFSET_START) != 0)
878 o = CAST(int32_t, ms->search.offset);
881 (ms->search.offset + ms->search.rm_len));
885 if ((m->str_flags & REGEX_OFFSET_START) != 0)
886 o = CAST(int32_t, ms->search.offset);
888 o = CAST(int32_t, (ms->search.offset + m->vallen));
899 o = der_offs(ms, m, nbytes);
900 if (o == -1 || (size_t)o > nbytes) {
901 if ((ms->flags & MAGIC_DEBUG) != 0) {
902 (void)fprintf(stderr,
903 "Bad DER offset %d nbytes=%zu",
917 if ((size_t)o > nbytes) {
919 file_error(ms, 0, "Offset out of range %zu > %zu",
929 cvt_id3(struct magic_set *ms, uint32_t v)
931 v = ((((v >> 0) & 0x7f) << 0) |
932 (((v >> 8) & 0x7f) << 7) |
933 (((v >> 16) & 0x7f) << 14) |
934 (((v >> 24) & 0x7f) << 21));
935 if ((ms->flags & MAGIC_DEBUG) != 0)
936 fprintf(stderr, "id3 offs=%u\n", v);
941 cvt_flip(int type, int flip)
959 return FILE_LEQLDATE;
961 return FILE_LEQWDATE;
975 return FILE_BEQLDATE;
977 return FILE_BEQWDATE;
983 return FILE_LEDOUBLE;
985 return FILE_BEDOUBLE;
990 #define DO_CVT(fld, cast) \
992 switch (m->mask_op & FILE_OPS_MASK) { \
994 p->fld &= cast m->num_mask; \
997 p->fld |= cast m->num_mask; \
1000 p->fld ^= cast m->num_mask; \
1003 p->fld += cast m->num_mask; \
1005 case FILE_OPMINUS: \
1006 p->fld -= cast m->num_mask; \
1008 case FILE_OPMULTIPLY: \
1009 p->fld *= cast m->num_mask; \
1011 case FILE_OPDIVIDE: \
1012 if (cast m->num_mask == 0) \
1014 p->fld /= cast m->num_mask; \
1016 case FILE_OPMODULO: \
1017 if (cast m->num_mask == 0) \
1019 p->fld %= cast m->num_mask; \
1022 if (m->mask_op & FILE_OPINVERSE) \
1026 cvt_8(union VALUETYPE *p, const struct magic *m)
1028 DO_CVT(b, (uint8_t));
1033 cvt_16(union VALUETYPE *p, const struct magic *m)
1035 DO_CVT(h, (uint16_t));
1040 cvt_32(union VALUETYPE *p, const struct magic *m)
1042 DO_CVT(l, (uint32_t));
1047 cvt_64(union VALUETYPE *p, const struct magic *m)
1049 DO_CVT(q, (uint64_t));
1053 #define DO_CVT2(fld, cast) \
1055 switch (m->mask_op & FILE_OPS_MASK) { \
1057 p->fld += cast m->num_mask; \
1059 case FILE_OPMINUS: \
1060 p->fld -= cast m->num_mask; \
1062 case FILE_OPMULTIPLY: \
1063 p->fld *= cast m->num_mask; \
1065 case FILE_OPDIVIDE: \
1066 if (cast m->num_mask == 0) \
1068 p->fld /= cast m->num_mask; \
1073 cvt_float(union VALUETYPE *p, const struct magic *m)
1075 DO_CVT2(f, (float));
1080 cvt_double(union VALUETYPE *p, const struct magic *m)
1082 DO_CVT2(d, (double));
1087 * Convert the byte order of the data we are looking at
1088 * While we're here, let's apply the mask operation
1089 * (unless you have a better idea)
1092 mconvert(struct magic_set *ms, struct magic *m, int flip)
1094 union VALUETYPE *p = &ms->ms_value;
1096 switch (cvt_flip(m->type, flip)) {
1098 if (cvt_8(p, m) == -1)
1102 if (cvt_16(p, m) == -1)
1108 if (cvt_32(p, m) == -1)
1115 if (cvt_64(p, m) == -1)
1119 case FILE_BESTRING16:
1120 case FILE_LESTRING16: {
1121 /* Null terminate and eat *trailing* return */
1122 p->s[sizeof(p->s) - 1] = '\0';
1125 case FILE_PSTRING: {
1126 size_t sz = file_pstring_length_size(m);
1127 char *ptr1 = p->s, *ptr2 = ptr1 + sz;
1128 size_t len = file_pstring_get_length(m, ptr1);
1129 sz = sizeof(p->s) - sz; /* maximum length of string */
1132 * The size of the pascal string length (sz)
1133 * is 1, 2, or 4. We need at least 1 byte for NUL
1134 * termination, but we've already truncated the
1135 * string by p->s, so we need to deduct sz.
1136 * Because we can use one of the bytes of the length
1137 * after we shifted as NUL termination.
1147 p->h = (short)BE16(p);
1148 if (cvt_16(p, m) == -1)
1154 p->l = (int32_t)BE32(p);
1155 if (cvt_32(p, m) == -1)
1162 p->q = (uint64_t)BE64(p);
1163 if (cvt_64(p, m) == -1)
1167 p->h = (short)LE16(p);
1168 if (cvt_16(p, m) == -1)
1174 p->l = (int32_t)LE32(p);
1175 if (cvt_32(p, m) == -1)
1182 p->q = (uint64_t)LE64(p);
1183 if (cvt_64(p, m) == -1)
1189 p->l = (int32_t)ME32(p);
1190 if (cvt_32(p, m) == -1)
1194 if (cvt_float(p, m) == -1)
1199 if (cvt_float(p, m) == -1)
1204 if (cvt_float(p, m) == -1)
1208 if (cvt_double(p, m) == -1)
1213 if (cvt_double(p, m) == -1)
1218 if (cvt_double(p, m) == -1)
1230 file_magerror(ms, "invalid type %d in mconvert()", m->type);
1234 file_magerror(ms, "zerodivide in mconvert()");
1240 mdebug(uint32_t offset, const char *str, size_t len)
1242 (void) fprintf(stderr, "mget/%" SIZE_T_FORMAT "u @%d: ", len, offset);
1243 file_showstr(stderr, str, len);
1244 (void) fputc('\n', stderr);
1245 (void) fputc('\n', stderr);
1249 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
1250 const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
1253 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1254 * anything, but setup pointers into the source
1260 if (offset > nbytes)
1261 offset = CAST(uint32_t, nbytes);
1262 ms->search.s = RCAST(const char *, s) + offset;
1263 ms->search.s_len = nbytes - offset;
1264 ms->search.offset = offset;
1270 const char *last; /* end of search region */
1271 const char *buf; /* start of search region */
1273 size_t lines, linecnt, bytecnt;
1275 if (s == NULL || nbytes < offset) {
1276 ms->search.s_len = 0;
1277 ms->search.s = NULL;
1281 if (m->str_flags & REGEX_LINE_COUNT) {
1282 linecnt = m->str_range;
1283 bytecnt = linecnt * 80;
1286 bytecnt = m->str_range;
1289 if (bytecnt == 0 || bytecnt > nbytes - offset)
1290 bytecnt = nbytes - offset;
1291 if (bytecnt > ms->regex_max)
1292 bytecnt = ms->regex_max;
1294 buf = RCAST(const char *, s) + offset;
1295 end = last = RCAST(const char *, s) + bytecnt + offset;
1296 /* mget() guarantees buf <= last */
1297 for (lines = linecnt, b = buf; lines && b < end &&
1298 ((b = CAST(const char *,
1299 memchr(c = b, '\n', CAST(size_t, (end - b)))))
1300 || (b = CAST(const char *,
1301 memchr(c, '\r', CAST(size_t, (end - c))))));
1304 if (b < end - 1 && b[0] == '\r' && b[1] == '\n')
1311 ms->search.s_len = last - buf;
1312 ms->search.offset = offset;
1313 ms->search.rm_len = 0;
1316 case FILE_BESTRING16:
1317 case FILE_LESTRING16: {
1318 const unsigned char *src = s + offset;
1319 const unsigned char *esrc = s + nbytes;
1321 char *edst = &p->s[sizeof(p->s) - 1];
1323 if (type == FILE_BESTRING16)
1326 /* check that offset is within range */
1327 if (offset >= nbytes)
1329 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
1335 if (type == FILE_BESTRING16 ?
1336 *(src - 1) != '\0' :
1337 ((src + 1 < esrc) &&
1338 *(src + 1) != '\0'))
1345 case FILE_STRING: /* XXX - these two should not need */
1346 case FILE_PSTRING: /* to copy anything, but do anyway. */
1352 if (offset >= nbytes) {
1353 (void)memset(p, '\0', sizeof(*p));
1356 if (nbytes - offset < sizeof(*p))
1357 nbytes = nbytes - offset;
1359 nbytes = sizeof(*p);
1361 (void)memcpy(p, s + offset, nbytes);
1364 * the usefulness of padding with zeroes eludes me, it
1365 * might even cause problems
1367 if (nbytes < sizeof(*p))
1368 (void)memset(((char *)(void *)p) + nbytes, '\0',
1369 sizeof(*p) - nbytes);
1374 do_ops(struct magic *m, intmax_t lhs, intmax_t off)
1378 switch (m->in_op & FILE_OPS_MASK) {
1394 case FILE_OPMULTIPLY:
1406 if (m->in_op & FILE_OPINVERSE)
1409 return (uint32_t)offset;
1413 msetoffset(struct magic_set *ms, struct magic *m, struct buffer *bb,
1414 const struct buffer *b, size_t o, unsigned int cont_level)
1416 if (m->offset < 0) {
1417 if (cont_level > 0) {
1418 if (m->flag & (OFFADD|INDIROFFADD))
1421 file_error(ms, 0, "negative offset %d at continuation"
1422 "level %u", m->offset, cont_level);
1426 if (buffer_fill(b) == -1)
1430 file_magerror(ms, "non zero offset %zu at"
1431 " level %u", o, cont_level);
1434 if ((size_t)-m->offset > b->elen)
1436 buffer_init(bb, -1, b->ebuf, b->elen);
1437 ms->eoffset = ms->offset = b->elen + m->offset;
1439 if (cont_level == 0) {
1441 // XXX: Pass real fd, then who frees bb?
1442 buffer_init(bb, -1, b->fbuf, b->flen);
1443 ms->offset = m->offset;
1446 ms->offset = ms->eoffset + m->offset;
1449 if ((ms->flags & MAGIC_DEBUG) != 0) {
1450 fprintf(stderr, "bb=[%p,%zu], %d [b=%p,%zu], [o=%#x, c=%d]\n",
1451 bb->fbuf, bb->flen, ms->offset, b->fbuf, b->flen,
1452 m->offset, cont_level);
1458 mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
1459 const unsigned char *s, size_t nbytes, size_t o, unsigned int cont_level,
1460 int mode, int text, int flip, uint16_t *indir_count, uint16_t *name_count,
1461 int *printed_something, int *need_separator, int *returnval)
1463 uint32_t offset = ms->offset;
1467 int rv, oneed_separator, in_type;
1469 union VALUETYPE *p = &ms->ms_value;
1472 if (*indir_count >= ms->indir_max) {
1473 file_error(ms, 0, "indirect count (%hu) exceeded",
1478 if (*name_count >= ms->name_max) {
1479 file_error(ms, 0, "name use count (%hu) exceeded",
1486 if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
1487 (uint32_t)nbytes, m) == -1)
1490 if ((ms->flags & MAGIC_DEBUG) != 0) {
1491 fprintf(stderr, "mget(type=%d, flag=%#x, offset=%u, o=%"
1492 SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
1493 "u, il=%hu, nc=%hu)\n",
1494 m->type, m->flag, offset, o, nbytes,
1495 *indir_count, *name_count);
1496 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1497 #ifndef COMPILE_ONLY
1502 if (m->flag & INDIR) {
1503 intmax_t off = m->in_offset;
1504 const int sgn = m->in_op & FILE_OPSIGNED;
1505 if (m->in_op & FILE_OPINDIRECT) {
1506 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1507 ((const void *)(s + offset + off)));
1508 if (OFFSET_OOB(nbytes, offset + off, sizeof(*q)))
1510 switch (cvt_flip(m->in_type, flip)) {
1512 off = SEXT(sgn,8,q->b);
1515 off = SEXT(sgn,16,q->h);
1518 off = SEXT(sgn,16,BE16(q));
1521 off = SEXT(sgn,16,LE16(q));
1524 off = SEXT(sgn,32,q->l);
1528 off = SEXT(sgn,32,BE32(q));
1532 off = SEXT(sgn,32,LE32(q));
1535 off = SEXT(sgn,32,ME32(q));
1538 if ((ms->flags & MAGIC_DEBUG) != 0)
1539 fprintf(stderr, "indirect offs=%jd\n", off);
1541 switch (in_type = cvt_flip(m->in_type, flip)) {
1543 if (OFFSET_OOB(nbytes, offset, 1))
1545 offset = do_ops(m, SEXT(sgn,8,p->b), off);
1548 if (OFFSET_OOB(nbytes, offset, 2))
1550 offset = do_ops(m, SEXT(sgn,16,BE16(p)), off);
1553 if (OFFSET_OOB(nbytes, offset, 2))
1555 offset = do_ops(m, SEXT(sgn,16,LE16(p)), off);
1558 if (OFFSET_OOB(nbytes, offset, 2))
1560 offset = do_ops(m, SEXT(sgn,16,p->h), off);
1564 if (OFFSET_OOB(nbytes, offset, 4))
1567 if (in_type == FILE_BEID3)
1568 lhs = cvt_id3(ms, (uint32_t)lhs);
1569 offset = do_ops(m, SEXT(sgn,32,lhs), off);
1573 if (OFFSET_OOB(nbytes, offset, 4))
1576 if (in_type == FILE_LEID3)
1577 lhs = cvt_id3(ms, (uint32_t)lhs);
1578 offset = do_ops(m, SEXT(sgn,32,lhs), off);
1581 if (OFFSET_OOB(nbytes, offset, 4))
1583 offset = do_ops(m, SEXT(sgn,32,ME32(p)), off);
1586 if (OFFSET_OOB(nbytes, offset, 4))
1588 offset = do_ops(m, SEXT(sgn,32,p->l), off);
1594 if (m->flag & INDIROFFADD) {
1595 offset += ms->c.li[cont_level-1].off;
1597 if ((ms->flags & MAGIC_DEBUG) != 0)
1599 "indirect *zero* offset\n");
1602 if ((ms->flags & MAGIC_DEBUG) != 0)
1603 fprintf(stderr, "indirect +offs=%u\n", offset);
1605 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
1607 ms->offset = offset;
1609 if ((ms->flags & MAGIC_DEBUG) != 0) {
1610 mdebug(offset, (char *)(void *)p,
1611 sizeof(union VALUETYPE));
1612 #ifndef COMPILE_ONLY
1618 /* Verify we have enough data to match magic type */
1621 if (OFFSET_OOB(nbytes, offset, 1))
1628 if (OFFSET_OOB(nbytes, offset, 2))
1647 if (OFFSET_OOB(nbytes, offset, 4))
1654 if (OFFSET_OOB(nbytes, offset, 8))
1661 if (OFFSET_OOB(nbytes, offset, m->vallen))
1666 if (nbytes < offset)
1671 if (m->str_flags & INDIRECT_RELATIVE)
1672 offset += CAST(uint32_t, o);
1676 if (nbytes < offset)
1679 if ((pb = file_push_buffer(ms)) == NULL)
1684 bb.fbuf = s + offset;
1685 bb.flen = nbytes - offset;
1686 rv = file_softmagic(ms, &bb,
1687 indir_count, name_count, BINTEST, text);
1689 if ((ms->flags & MAGIC_DEBUG) != 0)
1690 fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1692 rbuf = file_pop_buffer(ms, pb);
1693 if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
1697 if ((ms->flags & MAGIC_NODESC) == 0 &&
1698 file_printf(ms, F(ms, m->desc, "%u"), offset) == -1) {
1702 if (file_printf(ms, "%s", rbuf) == -1) {
1711 if (nbytes < offset)
1718 if (file_magicfind(ms, rbuf, &ml) == -1) {
1719 file_error(ms, 0, "cannot find entry `%s'", rbuf);
1723 oneed_separator = *need_separator;
1724 if (m->flag & NOSPACE)
1725 *need_separator = 0;
1726 rv = match(ms, ml.magic, ml.nmagic, b, offset + o,
1727 mode, text, flip, indir_count, name_count,
1728 printed_something, need_separator, returnval);
1730 *need_separator = oneed_separator;
1734 if (ms->flags & MAGIC_NODESC)
1736 if (file_printf(ms, "%s", m->desc) == -1)
1740 case FILE_DEFAULT: /* nothing to check */
1745 if (!mconvert(ms, m, flip))
1751 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1754 * Convert the source args to unsigned here so that (1) the
1755 * compare will be unsigned as it is in strncmp() and (2) so
1756 * the ctype functions will work correctly without extra
1759 const unsigned char *a = (const unsigned char *)s1;
1760 const unsigned char *b = (const unsigned char *)s2;
1761 const unsigned char *eb = b + len;
1765 * What we want here is v = strncmp(s1, s2, len),
1766 * but ignoring any nulls.
1769 if (0L == flags) { /* normal string: do it fast */
1771 if ((v = *b++ - *a++) != '\0')
1774 else { /* combine the others */
1780 if ((flags & STRING_IGNORE_LOWERCASE) &&
1782 if ((v = tolower(*b++) - *a++) != '\0')
1785 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1787 if ((v = toupper(*b++) - *a++) != '\0')
1790 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1793 if (isspace(*b++)) {
1795 while (b < eb && isspace(*b))
1803 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1806 while (b < eb && isspace(*b))
1810 if ((v = *b++ - *a++) != '\0')
1819 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1822 * XXX - The 16-bit string compare probably needs to be done
1823 * differently, especially if the flags are to be supported.
1824 * At the moment, I am unsure.
1827 return file_strncmp(a, b, len, flags);
1831 magiccheck(struct magic_set *ms, struct magic *m)
1833 uint64_t l = m->value.q;
1838 union VALUETYPE *p = &ms->ms_value;
1908 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1941 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
1955 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1958 case FILE_BESTRING16:
1959 case FILE_LESTRING16:
1961 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1964 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1968 if (ms->search.s == NULL)
1971 slen = MIN(m->vallen, sizeof(m->value.s));
1975 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
1976 if (slen + idx > ms->search.s_len)
1979 v = file_strncmp(m->value.s, ms->search.s + idx, slen,
1981 if (v == 0) { /* found match */
1982 ms->search.offset += idx;
1983 ms->search.rm_len = ms->search.s_len - idx;
1994 if (ms->search.s == NULL)
1998 rc = file_regcomp(&rx, m->value.s,
1999 REG_EXTENDED|REG_NEWLINE|
2000 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
2002 file_regerror(&rx, rc, ms);
2006 size_t slen = ms->search.s_len;
2009 copy = CAST(char *, malloc(slen));
2012 file_error(ms, errno,
2013 "can't allocate %" SIZE_T_FORMAT "u bytes",
2017 memcpy(copy, ms->search.s, slen);
2018 copy[--slen] = '\0';
2021 search = CCAST(char *, "");
2024 rc = file_regexec(&rx, (const char *)search,
2029 ms->search.s += (int)pmatch.rm_so;
2030 ms->search.offset += (size_t)pmatch.rm_so;
2032 (size_t)(pmatch.rm_eo - pmatch.rm_so);
2041 file_regerror(&rx, rc, ms);
2047 if (v == (uint64_t)-1)
2056 matched = der_cmp(ms, m);
2057 if (matched == -1) {
2058 if ((ms->flags & MAGIC_DEBUG) != 0) {
2059 (void) fprintf(stderr,
2060 "EOF comparing DER entries");
2066 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2070 v = file_signextend(ms, m, v);
2074 if ((ms->flags & MAGIC_DEBUG) != 0)
2075 (void) fprintf(stderr, "%" INT64_T_FORMAT
2076 "u == *any* = 1\n", (unsigned long long)v);
2082 if ((ms->flags & MAGIC_DEBUG) != 0)
2083 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
2084 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2085 (unsigned long long)l, matched);
2090 if ((ms->flags & MAGIC_DEBUG) != 0)
2091 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2092 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2093 (unsigned long long)l, matched);
2097 if (m->flag & UNSIGNED) {
2099 if ((ms->flags & MAGIC_DEBUG) != 0)
2100 (void) fprintf(stderr, "%" INT64_T_FORMAT
2101 "u > %" INT64_T_FORMAT "u = %d\n",
2102 (unsigned long long)v,
2103 (unsigned long long)l, matched);
2106 matched = (int64_t) v > (int64_t) l;
2107 if ((ms->flags & MAGIC_DEBUG) != 0)
2108 (void) fprintf(stderr, "%" INT64_T_FORMAT
2109 "d > %" INT64_T_FORMAT "d = %d\n",
2110 (long long)v, (long long)l, matched);
2115 if (m->flag & UNSIGNED) {
2117 if ((ms->flags & MAGIC_DEBUG) != 0)
2118 (void) fprintf(stderr, "%" INT64_T_FORMAT
2119 "u < %" INT64_T_FORMAT "u = %d\n",
2120 (unsigned long long)v,
2121 (unsigned long long)l, matched);
2124 matched = (int64_t) v < (int64_t) l;
2125 if ((ms->flags & MAGIC_DEBUG) != 0)
2126 (void) fprintf(stderr, "%" INT64_T_FORMAT
2127 "d < %" INT64_T_FORMAT "d = %d\n",
2128 (long long)v, (long long)l, matched);
2133 matched = (v & l) == l;
2134 if ((ms->flags & MAGIC_DEBUG) != 0)
2135 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2136 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2137 "x) = %d\n", (unsigned long long)v,
2138 (unsigned long long)l, (unsigned long long)l,
2143 matched = (v & l) != l;
2144 if ((ms->flags & MAGIC_DEBUG) != 0)
2145 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2146 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2147 "x) = %d\n", (unsigned long long)v,
2148 (unsigned long long)l, (unsigned long long)l,
2153 file_magerror(ms, "cannot happen: invalid relation `%c'",
2162 handle_annotation(struct magic_set *ms, struct magic *m, const struct buffer *b,
2165 if ((ms->flags & MAGIC_APPLE) && m->apple[0]) {
2166 if (!firstline && file_printf(ms, "\n- ") == -1)
2168 if (file_printf(ms, "%.8s", m->apple) == -1)
2172 if ((ms->flags & MAGIC_EXTENSION) && m->ext[0]) {
2173 if (!firstline && file_printf(ms, "\n- ") == -1)
2175 if (file_printf(ms, "%s", m->ext) == -1)
2179 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2182 if (!firstline && file_printf(ms, "\n- ") == -1)
2184 if (varexpand(buf, sizeof(buf), b, m->mimetype) == -1)
2188 if (file_printf(ms, "%s", p) == -1)
2196 print_sep(struct magic_set *ms, int firstline)
2198 // if (ms->flags & MAGIC_NODESC)
2203 * we found another match
2204 * put a newline and '-' to do some simple formatting
2206 return file_printf(ms, "\n- ");