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.315 2021/09/03 13:17: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 *, 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 *, 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 private int moffset(struct magic_set *, struct magic *, const struct buffer *,
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, struct magic *);
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 *, int);
65 private int cvt_8(union VALUETYPE *, const struct magic *);
66 private int cvt_16(union VALUETYPE *, const struct magic *);
67 private int cvt_32(union VALUETYPE *, const struct magic *);
68 private int cvt_64(union VALUETYPE *, const struct magic *);
70 #define OFFSET_OOB(n, o, i) ((n) < CAST(uint32_t, (o)) || (i) > ((n) - (o)))
72 (CAST(uint64_t, (p)->hq[0])<<56)| \
73 (CAST(uint64_t, (p)->hq[1])<<48)| \
74 (CAST(uint64_t, (p)->hq[2])<<40)| \
75 (CAST(uint64_t, (p)->hq[3])<<32)| \
76 (CAST(uint64_t, (p)->hq[4])<<24)| \
77 (CAST(uint64_t, (p)->hq[5])<<16)| \
78 (CAST(uint64_t, (p)->hq[6])<<8)| \
79 (CAST(uint64_t, (p)->hq[7])))
81 (CAST(uint64_t, (p)->hq[7])<<56)| \
82 (CAST(uint64_t, (p)->hq[6])<<48)| \
83 (CAST(uint64_t, (p)->hq[5])<<40)| \
84 (CAST(uint64_t, (p)->hq[4])<<32)| \
85 (CAST(uint64_t, (p)->hq[3])<<24)| \
86 (CAST(uint64_t, (p)->hq[2])<<16)| \
87 (CAST(uint64_t, (p)->hq[1])<<8)| \
88 (CAST(uint64_t, (p)->hq[0])))
90 (CAST(uint32_t, (p)->hl[3])<<24)| \
91 (CAST(uint32_t, (p)->hl[2])<<16)| \
92 (CAST(uint32_t, (p)->hl[1])<<8)| \
93 (CAST(uint32_t, (p)->hl[0])))
95 (CAST(uint32_t, (p)->hl[0])<<24)| \
96 (CAST(uint32_t, (p)->hl[1])<<16)| \
97 (CAST(uint32_t, (p)->hl[2])<<8)| \
98 (CAST(uint32_t, (p)->hl[3])))
100 (CAST(uint32_t, (p)->hl[1])<<24)| \
101 (CAST(uint32_t, (p)->hl[0])<<16)| \
102 (CAST(uint32_t, (p)->hl[3])<<8)| \
103 (CAST(uint32_t, (p)->hl[2])))
105 #define BE16(p) ((CAST(uint16_t, (p)->hs[0])<<8)|(CAST(uint16_t, (p)->hs[1])))
106 #define LE16(p) ((CAST(uint16_t, (p)->hs[1])<<8)|(CAST(uint16_t, (p)->hs[0])))
107 #define SEXT(s,v,p) ((s) ? \
108 CAST(intmax_t, CAST(int##v##_t, p)) : \
109 CAST(intmax_t, CAST(uint##v##_t, p)))
112 * softmagic - lookup one file in parsed, in-memory copy of database
113 * Passed the name and FILE * of one file to be typed.
115 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
117 file_softmagic(struct magic_set *ms, const struct buffer *b,
118 uint16_t *indir_count, uint16_t *name_count, int mode, int text)
121 int rv, printed_something = 0, need_separator = 0;
124 if (name_count == NULL) {
128 if (indir_count == NULL) {
133 for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
134 if ((rv = match(ms, ml->magic, ml->nmagic, b, 0, mode,
135 text, 0, indir_count, name_count,
136 &printed_something, &need_separator, NULL, NULL)) != 0)
142 #define FILE_FMTDEBUG
144 #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
146 private const char * __attribute__((__format_arg__(3)))
147 file_fmtcheck(struct magic_set *ms, const char *desc, const char *def,
148 const char *file, size_t line)
152 if (strchr(desc, '%') == NULL)
155 ptr = fmtcheck(desc, def);
158 "%s, %" SIZE_T_FORMAT "u: format `%s' does not match"
159 " with `%s'", file, line, desc, def);
163 #define F(a, b, c) fmtcheck((b), (c))
167 * Go through the whole list, stopping if you find a match. Process all
168 * the continuations of that match before returning.
170 * We support multi-level continuations:
172 * At any time when processing a successful top-level match, there is a
173 * current continuation level; it represents the level of the last
174 * successfully matched continuation.
176 * Continuations above that level are skipped as, if we see one, it
177 * means that the continuation that controls them - i.e, the
178 * lower-level continuation preceding them - failed to match.
180 * Continuations below that level are processed as, if we see one,
181 * it means we've finished processing or skipping higher-level
182 * continuations under the control of a successful or unsuccessful
183 * lower-level continuation, and are now seeing the next lower-level
184 * continuation and should process it. The current continuation
185 * level reverts to the level of the one we're seeing.
187 * Continuations at the current level are processed as, if we see
188 * one, there's no lower-level continuation that may have failed.
190 * If a continuation matches, we bump the current continuation level
191 * so that higher-level continuations are processed.
194 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
195 const struct buffer *b, size_t offset, int mode, int text,
196 int flip, uint16_t *indir_count, uint16_t *name_count,
197 int *printed_something, int *need_separator, int *returnval,
200 uint32_t magindex = 0;
201 unsigned int cont_level = 0;
202 int found_matchv = 0; /* if a match is found it is set to 1*/
203 int returnvalv = 0, e;
204 int firstline = 1; /* a flag to print X\n X\n- X */
206 int print = (ms->flags & MAGIC_NODESC) == 0;
209 * returnval can be 0 if a match is found, but there was no
210 * annotation to be printed.
212 if (returnval == NULL)
213 returnval = &returnvalv;
214 if (found_match == NULL)
215 found_match = &found_matchv;
217 if (file_check_mem(ms, cont_level) == -1)
220 for (magindex = 0; magindex < nmagic; magindex++) {
222 struct magic *m = &magic[magindex];
224 if (m->type != FILE_NAME)
225 if ((IS_STRING(m->type) &&
226 #define FLT (STRING_BINTEST | STRING_TEXTTEST)
227 ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
228 (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
229 (m->flag & mode) != mode) {
232 while (magindex < nmagic - 1 &&
233 magic[magindex + 1].cont_level != 0)
236 continue; /* Skip to next top-level test*/
239 if (msetoffset(ms, m, &bb, b, offset, cont_level) == -1)
241 ms->line = m->lineno;
243 /* if main entry matches, print it... */
244 switch (mget(ms, m, b, CAST(const unsigned char *, bb.fbuf),
245 bb.flen, offset, cont_level,
246 mode, text, flip, indir_count, name_count,
247 printed_something, need_separator, returnval, found_match))
252 flush = m->reln != '!';
255 if (m->type == FILE_INDIRECT) {
260 switch (magiccheck(ms, m)) {
274 * main entry didn't match,
275 * flush its continuations
280 if ((e = handle_annotation(ms, m, firstline)) != 0)
284 *printed_something = 1;
290 * If we are going to print something, we'll need to print
291 * a blank before we print something else.
298 *printed_something = 1;
299 if (print_sep(ms, firstline) == -1)
301 if (mprint(ms, m) == -1)
306 switch (moffset(ms, m, &bb, &ms->c.li[cont_level].off)) {
314 /* and any continuations that match */
315 if (file_check_mem(ms, ++cont_level) == -1)
318 while (magindex + 1 < nmagic &&
319 magic[magindex + 1].cont_level != 0) {
320 m = &magic[++magindex];
321 ms->line = m->lineno; /* for messages */
323 if (cont_level < m->cont_level)
325 if (cont_level > m->cont_level) {
327 * We're at the end of the level
328 * "cont_level" continuations.
330 cont_level = m->cont_level;
332 if (msetoffset(ms, m, &bb, b, offset, cont_level) == -1)
334 if (m->flag & OFFADD) {
335 if (cont_level == 0) {
336 if ((ms->flags & MAGIC_DEBUG) != 0)
343 ms->c.li[cont_level - 1].off;
346 #ifdef ENABLE_CONDITIONALS
347 if (m->cond == COND_ELSE ||
348 m->cond == COND_ELIF) {
349 if (ms->c.li[cont_level].last_match == 1)
353 switch (mget(ms, m, b, CAST(const unsigned char *,
354 bb.fbuf), bb.flen, offset,
355 cont_level, mode, text, flip, indir_count,
356 name_count, printed_something, need_separator,
357 returnval, found_match)) {
366 if (m->type == FILE_INDIRECT) {
374 switch (flush ? 1 : magiccheck(ms, m)) {
378 #ifdef ENABLE_CONDITIONALS
379 ms->c.li[cont_level].last_match = 0;
383 #ifdef ENABLE_CONDITIONALS
384 ms->c.li[cont_level].last_match = 1;
386 if (m->type == FILE_CLEAR)
387 ms->c.li[cont_level].got_match = 0;
388 else if (ms->c.li[cont_level].got_match) {
389 if (m->type == FILE_DEFAULT)
392 ms->c.li[cont_level].got_match = 1;
394 if ((e = handle_annotation(ms, m, firstline))
398 *printed_something = 1;
405 if (print && *m->desc) {
408 * This continuation matched. Print
409 * its message, with a blank before it
410 * if the previous item printed and
411 * this item isn't empty.
414 * If we are going to print something,
415 * make sure that we have a separator
418 if (!*printed_something) {
419 *printed_something = 1;
420 if (print_sep(ms, firstline)
424 /* space if previous printed */
426 && (m->flag & NOSPACE) == 0) {
427 if (file_printf(ms, " ") == -1)
430 if (mprint(ms, m) == -1)
435 switch (moffset(ms, m, &bb,
436 &ms->c.li[cont_level].off)) {
447 * If we see any continuations
451 if (file_check_mem(ms, ++cont_level) == -1)
456 if (*printed_something) {
460 if ((ms->flags & MAGIC_CONTINUE) == 0)
462 // So that we print a separator
463 *printed_something = 0;
472 check_fmt(struct magic_set *ms, const char *fmt)
477 if (strchr(fmt, '%') == NULL)
480 rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
482 file_regerror(&rx, rc, ms);
484 rc = file_regexec(&rx, fmt, 0, 0, 0);
491 #if !defined(HAVE_STRNDUP) || defined(__aiws__) || defined(_AIX)
492 # if defined(__aiws__) || defined(_AIX)
493 # define strndup aix_strndup /* aix is broken */
495 char *strndup(const char *, size_t);
498 strndup(const char *str, size_t n)
503 for (len = 0; len < n && str[len]; len++)
505 if ((copy = malloc(len + 1)) == NULL)
507 (void)memcpy(copy, str, len);
511 #endif /* HAVE_STRNDUP */
514 varexpand(struct magic_set *ms, char *buf, size_t len, const char *str)
516 const char *ptr, *sptr, *e, *t, *ee, *et;
519 for (sptr = str; (ptr = strstr(sptr, "${")) != NULL;) {
520 l = CAST(size_t, ptr - sptr);
523 memcpy(buf, sptr, l);
527 if (!*ptr || ptr[1] != '?')
529 for (et = t = ptr + 2; *et && *et != ':'; et++)
533 for (ee = e = et + 1; *ee && *ee != '}'; ee++)
539 if (ms->mode & 0111) {
562 memcpy(buf, sptr, l);
569 mprint(struct magic_set *ms, struct magic *m)
575 char buf[128], tbuf[26], sbuf[512], ebuf[512];
577 union VALUETYPE *p = &ms->ms_value;
579 if (varexpand(ms, ebuf, sizeof(ebuf), m->desc) == -1)
584 #define PRINTER(value, format, stype, utype) \
585 v = file_signextend(ms, m, CAST(uint64_t, value)); \
586 switch (check_fmt(ms, desc)) { \
590 if (m->flag & UNSIGNED) { \
591 (void)snprintf(buf, sizeof(buf), "%" format "u", \
594 (void)snprintf(buf, sizeof(buf), "%" format "d", \
597 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1) \
601 if (m->flag & UNSIGNED) { \
602 if (file_printf(ms, F(ms, desc, "%" format "u"), \
603 CAST(utype, v)) == -1) \
606 if (file_printf(ms, F(ms, desc, "%" format "d"), \
607 CAST(stype, v)) == -1) \
612 t = ms->offset + sizeof(stype); \
617 PRINTER(p->b, "", int8_t, uint8_t);
622 PRINTER(p->h, "", int16_t, uint16_t);
628 PRINTER(p->l, "", int32_t, uint32_t);
634 PRINTER(p->q, INT64_T_FORMAT, long long, unsigned long long);
638 case FILE_BESTRING16:
639 case FILE_LESTRING16:
640 if (m->reln == '=' || m->reln == '!') {
641 if (file_printf(ms, F(ms, desc, "%s"),
642 file_printable(ms, sbuf, sizeof(sbuf), m->value.s,
643 sizeof(m->value.s))) == -1)
645 t = ms->offset + m->vallen;
650 /* compute t before we mangle the string? */
651 t = ms->offset + strlen(str);
653 if (*m->value.s == '\0')
654 str[strcspn(str, "\r\n")] = '\0';
656 if (m->str_flags & STRING_TRIM)
657 str = file_strtrim(str);
659 if (file_printf(ms, F(ms, desc, "%s"),
660 file_printable(ms, sbuf, sizeof(sbuf), str,
661 sizeof(p->s) - (str - p->s))) == -1)
664 if (m->type == FILE_PSTRING) {
665 size_t l = file_pstring_length_size(ms, m);
666 if (l == FILE_BADSIZE)
677 if (file_printf(ms, F(ms, desc, "%s"),
678 file_fmttime(tbuf, sizeof(tbuf), p->l, 0)) == -1)
680 t = ms->offset + sizeof(uint32_t);
687 if (file_printf(ms, F(ms, desc, "%s"),
688 file_fmttime(tbuf, sizeof(tbuf), p->l, FILE_T_LOCAL)) == -1)
690 t = ms->offset + sizeof(uint32_t);
696 if (file_printf(ms, F(ms, desc, "%s"),
697 file_fmttime(tbuf, sizeof(tbuf), p->q, 0)) == -1)
699 t = ms->offset + sizeof(uint64_t);
705 if (file_printf(ms, F(ms, desc, "%s"),
706 file_fmttime(tbuf, sizeof(tbuf), p->q, FILE_T_LOCAL)) == -1)
708 t = ms->offset + sizeof(uint64_t);
714 if (file_printf(ms, F(ms, desc, "%s"),
715 file_fmttime(tbuf, sizeof(tbuf), p->q, FILE_T_WINDOWS))
718 t = ms->offset + sizeof(uint64_t);
725 switch (check_fmt(ms, desc)) {
729 (void)snprintf(buf, sizeof(buf), "%g", vf);
730 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
734 if (file_printf(ms, F(ms, desc, "%g"), vf) == -1)
738 t = ms->offset + sizeof(float);
745 switch (check_fmt(ms, desc)) {
749 (void)snprintf(buf, sizeof(buf), "%g", vd);
750 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
754 if (file_printf(ms, F(ms, desc, "%g"), vd) == -1)
758 t = ms->offset + sizeof(double);
766 cp = strndup(RCAST(const char *, ms->search.s),
769 file_oomem(ms, ms->search.rm_len);
772 scp = (m->str_flags & STRING_TRIM) ? file_strtrim(cp) : cp;
774 rval = file_printf(ms, F(ms, desc, "%s"), file_printable(ms,
775 sbuf, sizeof(sbuf), scp, ms->search.rm_len));
781 if ((m->str_flags & REGEX_OFFSET_START))
782 t = ms->search.offset;
784 t = ms->search.offset + ms->search.rm_len;
790 if (file_printf(ms, "%s", m->desc) == -1)
801 if (file_printf(ms, F(ms, desc, "%s"),
802 file_printable(ms, sbuf, sizeof(sbuf), ms->ms_value.s,
803 sizeof(ms->ms_value.s))) == -1)
808 (void) file_print_guid(buf, sizeof(buf), ms->ms_value.guid);
809 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
814 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
817 return CAST(int32_t, t);
821 moffset(struct magic_set *ms, struct magic *m, const struct buffer *b,
824 size_t nbytes = b->flen;
829 o = CAST(int32_t, (ms->offset + sizeof(char)));
835 o = CAST(int32_t, (ms->offset + sizeof(short)));
842 o = CAST(int32_t, (ms->offset + sizeof(int32_t)));
848 o = CAST(int32_t, (ms->offset + sizeof(int64_t)));
853 case FILE_BESTRING16:
854 case FILE_LESTRING16:
855 if (m->reln == '=' || m->reln == '!') {
856 o = ms->offset + m->vallen;
858 union VALUETYPE *p = &ms->ms_value;
860 if (*m->value.s == '\0')
861 p->s[strcspn(p->s, "\r\n")] = '\0';
862 o = CAST(uint32_t, (ms->offset + strlen(p->s)));
863 if (m->type == FILE_PSTRING) {
864 size_t l = file_pstring_length_size(ms, m);
865 if (l == FILE_BADSIZE)
867 o += CAST(uint32_t, l);
876 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
883 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
889 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
895 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
901 o = CAST(int32_t, (ms->offset + sizeof(float)));
907 o = CAST(int32_t, (ms->offset + sizeof(double)));
911 if ((m->str_flags & REGEX_OFFSET_START) != 0)
912 o = CAST(int32_t, ms->search.offset);
915 (ms->search.offset + ms->search.rm_len));
919 if ((m->str_flags & REGEX_OFFSET_START) != 0)
920 o = CAST(int32_t, ms->search.offset);
922 o = CAST(int32_t, (ms->search.offset + m->vallen));
934 o = der_offs(ms, m, nbytes);
935 if (o == -1 || CAST(size_t, o) > nbytes) {
936 if ((ms->flags & MAGIC_DEBUG) != 0) {
937 (void)fprintf(stderr,
938 "Bad DER offset %d nbytes=%"
939 SIZE_T_FORMAT "u", o, nbytes);
947 o = CAST(int32_t, (ms->offset + 2 * sizeof(uint64_t)));
955 if (CAST(size_t, o) > nbytes) {
957 file_error(ms, 0, "Offset out of range %" SIZE_T_FORMAT
958 "u > %" SIZE_T_FORMAT "u", (size_t)o, nbytes);
967 cvt_id3(struct magic_set *ms, uint32_t v)
969 v = ((((v >> 0) & 0x7f) << 0) |
970 (((v >> 8) & 0x7f) << 7) |
971 (((v >> 16) & 0x7f) << 14) |
972 (((v >> 24) & 0x7f) << 21));
973 if ((ms->flags & MAGIC_DEBUG) != 0)
974 fprintf(stderr, "id3 offs=%u\n", v);
979 cvt_flip(int type, int flip)
997 return FILE_LEQLDATE;
999 return FILE_LEQWDATE;
1001 return FILE_BESHORT;
1007 return FILE_BELDATE;
1011 return FILE_BEQDATE;
1013 return FILE_BEQLDATE;
1015 return FILE_BEQWDATE;
1017 return FILE_LEFLOAT;
1019 return FILE_BEFLOAT;
1021 return FILE_LEDOUBLE;
1023 return FILE_BEDOUBLE;
1028 #define DO_CVT(fld, type) \
1030 switch (m->mask_op & FILE_OPS_MASK) { \
1032 p->fld &= CAST(type, m->num_mask); \
1035 p->fld |= CAST(type, m->num_mask); \
1038 p->fld ^= CAST(type, m->num_mask); \
1041 p->fld += CAST(type, m->num_mask); \
1043 case FILE_OPMINUS: \
1044 p->fld -= CAST(type, m->num_mask); \
1046 case FILE_OPMULTIPLY: \
1047 p->fld *= CAST(type, m->num_mask); \
1049 case FILE_OPDIVIDE: \
1050 if (CAST(type, m->num_mask) == 0) \
1052 p->fld /= CAST(type, m->num_mask); \
1054 case FILE_OPMODULO: \
1055 if (CAST(type, m->num_mask) == 0) \
1057 p->fld %= CAST(type, m->num_mask); \
1060 if (m->mask_op & FILE_OPINVERSE) \
1064 cvt_8(union VALUETYPE *p, const struct magic *m)
1071 cvt_16(union VALUETYPE *p, const struct magic *m)
1073 DO_CVT(h, uint16_t);
1078 cvt_32(union VALUETYPE *p, const struct magic *m)
1080 DO_CVT(l, uint32_t);
1085 cvt_64(union VALUETYPE *p, const struct magic *m)
1087 DO_CVT(q, uint64_t);
1091 #define DO_CVT2(fld, type) \
1093 switch (m->mask_op & FILE_OPS_MASK) { \
1095 p->fld += CAST(type, m->num_mask); \
1097 case FILE_OPMINUS: \
1098 p->fld -= CAST(type, m->num_mask); \
1100 case FILE_OPMULTIPLY: \
1101 p->fld *= CAST(type, m->num_mask); \
1103 case FILE_OPDIVIDE: \
1104 if (CAST(type, m->num_mask) == 0) \
1106 p->fld /= CAST(type, m->num_mask); \
1111 cvt_float(union VALUETYPE *p, const struct magic *m)
1118 cvt_double(union VALUETYPE *p, const struct magic *m)
1125 * Convert the byte order of the data we are looking at
1126 * While we're here, let's apply the mask operation
1127 * (unless you have a better idea)
1130 mconvert(struct magic_set *ms, struct magic *m, int flip)
1132 union VALUETYPE *p = &ms->ms_value;
1134 switch (cvt_flip(m->type, flip)) {
1136 if (cvt_8(p, m) == -1)
1140 if (cvt_16(p, m) == -1)
1146 if (cvt_32(p, m) == -1)
1154 if (cvt_64(p, m) == -1)
1158 case FILE_BESTRING16:
1159 case FILE_LESTRING16: {
1160 /* Null terminate and eat *trailing* return */
1161 p->s[sizeof(p->s) - 1] = '\0';
1164 case FILE_PSTRING: {
1166 size_t len, sz = file_pstring_length_size(ms, m);
1167 if (sz == FILE_BADSIZE)
1171 len = file_pstring_get_length(ms, m, ptr1);
1172 if (len == FILE_BADSIZE)
1174 sz = sizeof(p->s) - sz; /* maximum length of string */
1177 * The size of the pascal string length (sz)
1178 * is 1, 2, or 4. We need at least 1 byte for NUL
1179 * termination, but we've already truncated the
1180 * string by p->s, so we need to deduct sz.
1181 * Because we can use one of the bytes of the length
1182 * after we shifted as NUL termination.
1192 p->h = CAST(short, BE16(p));
1193 if (cvt_16(p, m) == -1)
1199 p->l = CAST(int32_t, BE32(p));
1200 if (cvt_32(p, m) == -1)
1207 p->q = CAST(uint64_t, BE64(p));
1208 if (cvt_64(p, m) == -1)
1212 p->h = CAST(short, LE16(p));
1213 if (cvt_16(p, m) == -1)
1219 p->l = CAST(int32_t, LE32(p));
1220 if (cvt_32(p, m) == -1)
1227 p->q = CAST(uint64_t, LE64(p));
1228 if (cvt_64(p, m) == -1)
1234 p->l = CAST(int32_t, ME32(p));
1235 if (cvt_32(p, m) == -1)
1239 if (cvt_float(p, m) == -1)
1244 if (cvt_float(p, m) == -1)
1249 if (cvt_float(p, m) == -1)
1253 if (cvt_double(p, m) == -1)
1258 if (cvt_double(p, m) == -1)
1263 if (cvt_double(p, m) == -1)
1276 file_magerror(ms, "invalid type %d in mconvert()", m->type);
1280 file_magerror(ms, "zerodivide in mconvert()");
1286 mdebug(uint32_t offset, const char *str, size_t len)
1288 (void) fprintf(stderr, "mget/%" SIZE_T_FORMAT "u @%d: ", len, offset);
1289 file_showstr(stderr, str, len);
1290 (void) fputc('\n', stderr);
1291 (void) fputc('\n', stderr);
1295 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
1296 const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
1299 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1300 * anything, but setup pointers into the source
1306 if (offset > nbytes)
1307 offset = CAST(uint32_t, nbytes);
1308 ms->search.s = RCAST(const char *, s) + offset;
1309 ms->search.s_len = nbytes - offset;
1310 ms->search.offset = offset;
1316 const char *last; /* end of search region */
1317 const char *buf; /* start of search region */
1319 size_t lines, linecnt, bytecnt;
1321 if (s == NULL || nbytes < offset) {
1322 ms->search.s_len = 0;
1323 ms->search.s = NULL;
1327 if (m->str_flags & REGEX_LINE_COUNT) {
1328 linecnt = m->str_range;
1329 bytecnt = linecnt * 80;
1332 bytecnt = m->str_range;
1335 if (bytecnt == 0 || bytecnt > nbytes - offset)
1336 bytecnt = nbytes - offset;
1337 if (bytecnt > ms->regex_max)
1338 bytecnt = ms->regex_max;
1340 buf = RCAST(const char *, s) + offset;
1341 end = last = RCAST(const char *, s) + bytecnt + offset;
1342 /* mget() guarantees buf <= last */
1343 for (lines = linecnt, b = buf; lines && b < end &&
1344 ((b = CAST(const char *,
1345 memchr(c = b, '\n', CAST(size_t, (end - b)))))
1346 || (b = CAST(const char *,
1347 memchr(c, '\r', CAST(size_t, (end - c))))));
1349 if (b < end - 1 && b[0] == '\r' && b[1] == '\n')
1351 if (b < end - 1 && b[0] == '\n')
1359 ms->search.s_len = last - buf;
1360 ms->search.offset = offset;
1361 ms->search.rm_len = 0;
1364 case FILE_BESTRING16:
1365 case FILE_LESTRING16: {
1366 const unsigned char *src = s + offset;
1367 const unsigned char *esrc = s + nbytes;
1369 char *edst = &p->s[sizeof(p->s) - 1];
1371 if (type == FILE_BESTRING16)
1374 /* check that offset is within range */
1375 if (offset >= nbytes)
1377 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
1383 if (type == FILE_BESTRING16 ?
1384 *(src - 1) != '\0' :
1385 ((src + 1 < esrc) &&
1386 *(src + 1) != '\0'))
1393 case FILE_STRING: /* XXX - these two should not need */
1394 case FILE_PSTRING: /* to copy anything, but do anyway. */
1400 if (type == FILE_OFFSET) {
1401 (void)memset(p, '\0', sizeof(*p));
1406 if (offset >= nbytes) {
1407 (void)memset(p, '\0', sizeof(*p));
1410 if (nbytes - offset < sizeof(*p))
1411 nbytes = nbytes - offset;
1413 nbytes = sizeof(*p);
1415 (void)memcpy(p, s + offset, nbytes);
1418 * the usefulness of padding with zeroes eludes me, it
1419 * might even cause problems
1421 if (nbytes < sizeof(*p))
1422 (void)memset(RCAST(char *, RCAST(void *, p)) + nbytes, '\0',
1423 sizeof(*p) - nbytes);
1428 do_ops(struct magic *m, intmax_t lhs, intmax_t off)
1432 switch (m->in_op & FILE_OPS_MASK) {
1448 case FILE_OPMULTIPLY:
1460 if (m->in_op & FILE_OPINVERSE)
1463 return CAST(uint32_t, offset);
1467 msetoffset(struct magic_set *ms, struct magic *m, struct buffer *bb,
1468 const struct buffer *b, size_t o, unsigned int cont_level)
1471 if (m->flag & OFFNEGATIVE) {
1472 offset = -m->offset;
1473 if (cont_level > 0) {
1474 if (m->flag & (OFFADD|INDIROFFADD))
1477 file_error(ms, 0, "negative offset %d at continuation"
1478 "level %u", m->offset, cont_level);
1482 if (buffer_fill(b) == -1)
1486 file_magerror(ms, "non zero offset %" SIZE_T_FORMAT
1487 "u at level %u", o, cont_level);
1490 if (CAST(size_t, m->offset) > b->elen)
1492 buffer_init(bb, -1, NULL, b->ebuf, b->elen);
1493 ms->eoffset = ms->offset = CAST(int32_t, b->elen - m->offset);
1496 if (cont_level == 0) {
1498 // XXX: Pass real fd, then who frees bb?
1499 buffer_init(bb, -1, NULL, b->fbuf, b->flen);
1500 ms->offset = offset;
1503 ms->offset = ms->eoffset + offset;
1506 if ((ms->flags & MAGIC_DEBUG) != 0) {
1507 fprintf(stderr, "bb=[%p,%" SIZE_T_FORMAT "u,%"
1508 SIZE_T_FORMAT "u], %d [b=%p,%"
1509 SIZE_T_FORMAT "u,%" SIZE_T_FORMAT "u], [o=%#x, c=%d]\n",
1510 bb->fbuf, bb->flen, bb->elen, ms->offset, b->fbuf,
1511 b->flen, b->elen, offset, cont_level);
1517 save_cont(struct magic_set *ms, struct cont *c)
1521 len = c->len * sizeof(*c->li);
1522 ms->c.li = CAST(struct level_info *, malloc(len));
1523 if (ms->c.li == NULL) {
1527 memcpy(ms->c.li, c->li, len);
1532 restore_cont(struct magic_set *ms, struct cont *c)
1539 mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
1540 const unsigned char *s, size_t nbytes, size_t o, unsigned int cont_level,
1541 int mode, int text, int flip, uint16_t *indir_count, uint16_t *name_count,
1542 int *printed_something, int *need_separator, int *returnval,
1545 uint32_t eoffset, offset = ms->offset;
1549 int rv, oneed_separator, in_type, nfound_match;
1551 union VALUETYPE *p = &ms->ms_value;
1552 struct mlist ml, *mlp;
1555 if (*indir_count >= ms->indir_max) {
1556 file_error(ms, 0, "indirect count (%hu) exceeded",
1561 if (*name_count >= ms->name_max) {
1562 file_error(ms, 0, "name use count (%hu) exceeded",
1569 if (mcopy(ms, p, m->type, m->flag & INDIR, s,
1570 CAST(uint32_t, offset + o), CAST(uint32_t, nbytes), m) == -1)
1573 if ((ms->flags & MAGIC_DEBUG) != 0) {
1574 fprintf(stderr, "mget(type=%d, flag=%#x, offset=%u, o=%"
1575 SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
1576 "u, il=%hu, nc=%hu)\n",
1577 m->type, m->flag, offset, o, nbytes,
1578 *indir_count, *name_count);
1579 mdebug(offset, RCAST(char *, RCAST(void *, p)),
1580 sizeof(union VALUETYPE));
1581 #ifndef COMPILE_ONLY
1586 if (m->flag & INDIR) {
1587 intmax_t off = m->in_offset;
1588 const int sgn = m->in_op & FILE_OPSIGNED;
1589 if (m->in_op & FILE_OPINDIRECT) {
1590 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1591 RCAST(const void *, s + offset + off));
1593 switch (op = cvt_flip(m->in_type, flip)) {
1595 if (OFFSET_OOB(nbytes, offset + off, 1))
1597 off = SEXT(sgn,8,q->b);
1600 if (OFFSET_OOB(nbytes, offset + off, 2))
1602 off = SEXT(sgn,16,q->h);
1605 if (OFFSET_OOB(nbytes, offset + off, 2))
1607 off = SEXT(sgn,16,BE16(q));
1610 if (OFFSET_OOB(nbytes, offset + off, 2))
1612 off = SEXT(sgn,16,LE16(q));
1615 if (OFFSET_OOB(nbytes, offset + off, 4))
1617 off = SEXT(sgn,32,q->l);
1621 if (OFFSET_OOB(nbytes, offset + off, 4))
1623 off = SEXT(sgn,32,BE32(q));
1627 if (OFFSET_OOB(nbytes, offset + off, 4))
1629 off = SEXT(sgn,32,LE32(q));
1632 if (OFFSET_OOB(nbytes, offset + off, 4))
1634 off = SEXT(sgn,32,ME32(q));
1637 if (OFFSET_OOB(nbytes, offset + off, 8))
1639 off = SEXT(sgn,64,BE64(q));
1642 if (OFFSET_OOB(nbytes, offset + off, 8))
1644 off = SEXT(sgn,64,LE64(q));
1647 if ((ms->flags & MAGIC_DEBUG) != 0)
1648 fprintf(stderr, "bad op=%d\n", op);
1651 if ((ms->flags & MAGIC_DEBUG) != 0)
1652 fprintf(stderr, "indirect offs=%jd\n", off);
1654 switch (in_type = cvt_flip(m->in_type, flip)) {
1656 if (OFFSET_OOB(nbytes, offset, 1))
1658 offset = do_ops(m, SEXT(sgn,8,p->b), off);
1661 if (OFFSET_OOB(nbytes, offset, 2))
1663 offset = do_ops(m, SEXT(sgn,16,BE16(p)), off);
1666 if (OFFSET_OOB(nbytes, offset, 2))
1668 offset = do_ops(m, SEXT(sgn,16,LE16(p)), off);
1671 if (OFFSET_OOB(nbytes, offset, 2))
1673 offset = do_ops(m, SEXT(sgn,16,p->h), off);
1677 if (OFFSET_OOB(nbytes, offset, 4))
1680 if (in_type == FILE_BEID3)
1681 lhs = cvt_id3(ms, CAST(uint32_t, lhs));
1682 offset = do_ops(m, SEXT(sgn,32,lhs), off);
1686 if (OFFSET_OOB(nbytes, offset, 4))
1689 if (in_type == FILE_LEID3)
1690 lhs = cvt_id3(ms, CAST(uint32_t, lhs));
1691 offset = do_ops(m, SEXT(sgn,32,lhs), off);
1694 if (OFFSET_OOB(nbytes, offset, 4))
1696 offset = do_ops(m, SEXT(sgn,32,ME32(p)), off);
1699 if (OFFSET_OOB(nbytes, offset, 4))
1701 offset = do_ops(m, SEXT(sgn,32,p->l), off);
1704 if (OFFSET_OOB(nbytes, offset, 8))
1706 offset = do_ops(m, SEXT(sgn,64,LE64(p)), off);
1709 if (OFFSET_OOB(nbytes, offset, 8))
1711 offset = do_ops(m, SEXT(sgn,64,BE64(p)), off);
1714 if ((ms->flags & MAGIC_DEBUG) != 0)
1715 fprintf(stderr, "bad in_type=%d\n", in_type);
1719 if (m->flag & INDIROFFADD) {
1720 if (cont_level == 0) {
1721 if ((ms->flags & MAGIC_DEBUG) != 0)
1723 "indirect *zero* cont_level\n");
1726 offset += ms->c.li[cont_level - 1].off;
1728 if ((ms->flags & MAGIC_DEBUG) != 0)
1730 "indirect *zero* offset\n");
1733 if ((ms->flags & MAGIC_DEBUG) != 0)
1734 fprintf(stderr, "indirect +offs=%u\n", offset);
1736 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
1738 ms->offset = offset;
1740 if ((ms->flags & MAGIC_DEBUG) != 0) {
1741 mdebug(offset, RCAST(char *, RCAST(void *, p)),
1742 sizeof(union VALUETYPE));
1743 #ifndef COMPILE_ONLY
1749 /* Verify we have enough data to match magic type */
1752 if (OFFSET_OOB(nbytes, offset, 1))
1759 if (OFFSET_OOB(nbytes, offset, 2))
1778 if (OFFSET_OOB(nbytes, offset, 4))
1785 if (OFFSET_OOB(nbytes, offset, 8))
1790 if (OFFSET_OOB(nbytes, offset, 16))
1797 if (OFFSET_OOB(nbytes, offset, m->vallen))
1802 if (nbytes < offset)
1807 if (m->str_flags & INDIRECT_RELATIVE)
1808 offset += CAST(uint32_t, o);
1812 if (nbytes < offset)
1815 if ((pb = file_push_buffer(ms)) == NULL)
1820 bb.fbuf = s + offset;
1821 bb.flen = nbytes - offset;
1822 for (mlp = ms->mlist[0]->next; mlp != ms->mlist[0];
1825 if ((rv = match(ms, mlp->magic, mlp->nmagic, &bb, 0,
1826 BINTEST, text, 0, indir_count, name_count,
1827 printed_something, need_separator, NULL,
1832 if ((ms->flags & MAGIC_DEBUG) != 0)
1833 fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1835 rbuf = file_pop_buffer(ms, pb);
1836 if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
1840 if ((ms->flags & MAGIC_NODESC) == 0 &&
1841 file_printf(ms, F(ms, m->desc, "%u"), offset) == -1)
1846 if (file_printf(ms, "%s", rbuf) == -1) {
1855 if (nbytes < offset)
1862 if (file_magicfind(ms, rbuf, &ml) == -1) {
1863 file_error(ms, 0, "cannot find entry `%s'", rbuf);
1866 if (save_cont(ms, &c) == -1) {
1867 file_error(ms, errno, "can't allocate continuation");
1871 oneed_separator = *need_separator;
1872 if (m->flag & NOSPACE)
1873 *need_separator = 0;
1877 eoffset = ms->eoffset;
1878 rv = match(ms, ml.magic, ml.nmagic, b, offset + o,
1879 mode, text, flip, indir_count, name_count,
1880 printed_something, need_separator, returnval,
1882 ms->ms_value.q = nfound_match;
1884 *found_match |= nfound_match;
1886 restore_cont(ms, &c);
1889 *need_separator = oneed_separator;
1890 ms->offset = offset;
1891 ms->eoffset = eoffset;
1895 if (ms->flags & MAGIC_NODESC)
1897 if (file_printf(ms, "%s", m->desc) == -1)
1901 case FILE_DEFAULT: /* nothing to check */
1906 if (!mconvert(ms, m, flip))
1912 file_strncmp(const char *s1, const char *s2, size_t len, size_t maxlen,
1916 * Convert the source args to unsigned here so that (1) the
1917 * compare will be unsigned as it is in strncmp() and (2) so
1918 * the ctype functions will work correctly without extra
1921 const unsigned char *a = RCAST(const unsigned char *, s1);
1922 const unsigned char *b = RCAST(const unsigned char *, s2);
1923 uint32_t ws = flags & (STRING_COMPACT_WHITESPACE |
1924 STRING_COMPACT_OPTIONAL_WHITESPACE);
1925 const unsigned char *eb = b + (ws ? maxlen : len);
1929 * What we want here is v = strncmp(s1, s2, len),
1930 * but ignoring any nulls.
1934 if (0L == flags) { /* normal string: do it fast */
1936 if ((v = *b++ - *a++) != '\0')
1939 else { /* combine the others */
1945 if ((flags & STRING_IGNORE_LOWERCASE) &&
1947 if ((v = tolower(*b++) - *a++) != '\0')
1950 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1952 if ((v = toupper(*b++) - *a++) != '\0')
1955 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1961 while (b < eb && isspace(*b))
1969 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1972 while (b < eb && isspace(*b))
1976 if ((v = *b++ - *a++) != '\0')
1980 if (len == 0 && v == 0 && (flags & STRING_FULL_WORD)) {
1981 if (*b && !isspace(*b))
1989 file_strncmp16(const char *a, const char *b, size_t len, size_t maxlen,
1993 * XXX - The 16-bit string compare probably needs to be done
1994 * differently, especially if the flags are to be supported.
1995 * At the moment, I am unsure.
1998 return file_strncmp(a, b, len, maxlen, flags);
2002 magiccheck(struct magic_set *ms, struct magic *m)
2004 uint64_t l = m->value.q;
2009 union VALUETYPE *p = &ms->ms_value;
2080 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
2113 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
2127 v = file_strncmp(m->value.s, p->s, CAST(size_t, m->vallen),
2128 sizeof(p->s), m->str_flags);
2131 case FILE_BESTRING16:
2132 case FILE_LESTRING16:
2134 v = file_strncmp16(m->value.s, p->s, CAST(size_t, m->vallen),
2135 sizeof(p->s), m->str_flags);
2138 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
2142 if (ms->search.s == NULL)
2145 slen = MIN(m->vallen, sizeof(m->value.s));
2149 if (slen > 0 && m->str_flags == 0) {
2151 idx = m->str_range + slen;
2152 if (m->str_range == 0 || ms->search.s_len < idx)
2153 idx = ms->search.s_len;
2154 found = CAST(const char *, memmem(ms->search.s, idx,
2160 idx = found - ms->search.s;
2161 ms->search.offset += idx;
2162 ms->search.rm_len = ms->search.s_len - idx;
2167 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
2168 if (slen + idx > ms->search.s_len) {
2173 v = file_strncmp(m->value.s, ms->search.s + idx, slen,
2174 ms->search.s_len - idx, m->str_flags);
2175 if (v == 0) { /* found match */
2176 ms->search.offset += idx;
2177 ms->search.rm_len = ms->search.s_len - idx;
2188 if (ms->search.s == NULL)
2192 rc = file_regcomp(&rx, m->value.s,
2193 REG_EXTENDED|REG_NEWLINE|
2194 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
2196 file_regerror(&rx, rc, ms);
2197 v = CAST(uint64_t, -1);
2200 size_t slen = ms->search.s_len;
2203 copy = CAST(char *, malloc(slen));
2206 file_error(ms, errno,
2207 "can't allocate %" SIZE_T_FORMAT "u bytes",
2211 memcpy(copy, ms->search.s, slen);
2212 copy[--slen] = '\0';
2215 search = CCAST(char *, "");
2218 rc = file_regexec(&rx, RCAST(const char *, search),
2223 ms->search.s += CAST(int, pmatch.rm_so);
2224 ms->search.offset += CAST(size_t, pmatch.rm_so);
2225 ms->search.rm_len = CAST(size_t,
2226 pmatch.rm_eo - pmatch.rm_so);
2235 file_regerror(&rx, rc, ms);
2236 v = CAST(uint64_t, -1);
2241 if (v == CAST(uint64_t, -1))
2246 return ms->ms_value.q != 0;
2251 matched = der_cmp(ms, m);
2252 if (matched == -1) {
2253 if ((ms->flags & MAGIC_DEBUG) != 0) {
2254 (void) fprintf(stderr,
2255 "EOF comparing DER entries");
2262 v = memcmp(m->value.guid, p->guid, sizeof(p->guid));
2265 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2269 v = file_signextend(ms, m, v);
2273 if ((ms->flags & MAGIC_DEBUG) != 0)
2274 (void) fprintf(stderr, "%" INT64_T_FORMAT
2275 "u == *any* = 1\n", CAST(unsigned long long, v));
2281 if ((ms->flags & MAGIC_DEBUG) != 0)
2282 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
2283 INT64_T_FORMAT "u = %d\n",
2284 CAST(unsigned long long, v),
2285 CAST(unsigned long long, l), matched);
2290 if ((ms->flags & MAGIC_DEBUG) != 0)
2291 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2292 INT64_T_FORMAT "u = %d\n",
2293 CAST(unsigned long long, v),
2294 CAST(unsigned long long, l), matched);
2298 if (m->flag & UNSIGNED) {
2300 if ((ms->flags & MAGIC_DEBUG) != 0)
2301 (void) fprintf(stderr, "%" INT64_T_FORMAT
2302 "u > %" INT64_T_FORMAT "u = %d\n",
2303 CAST(unsigned long long, v),
2304 CAST(unsigned long long, l), matched);
2307 matched = CAST(int64_t, v) > CAST(int64_t, l);
2308 if ((ms->flags & MAGIC_DEBUG) != 0)
2309 (void) fprintf(stderr, "%" INT64_T_FORMAT
2310 "d > %" INT64_T_FORMAT "d = %d\n",
2312 CAST(long long, l), matched);
2317 if (m->flag & UNSIGNED) {
2319 if ((ms->flags & MAGIC_DEBUG) != 0)
2320 (void) fprintf(stderr, "%" INT64_T_FORMAT
2321 "u < %" INT64_T_FORMAT "u = %d\n",
2322 CAST(unsigned long long, v),
2323 CAST(unsigned long long, l), matched);
2326 matched = CAST(int64_t, v) < CAST(int64_t, l);
2327 if ((ms->flags & MAGIC_DEBUG) != 0)
2328 (void) fprintf(stderr, "%" INT64_T_FORMAT
2329 "d < %" INT64_T_FORMAT "d = %d\n",
2331 CAST(long long, l), matched);
2336 matched = (v & l) == l;
2337 if ((ms->flags & MAGIC_DEBUG) != 0)
2338 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2339 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2340 "x) = %d\n", CAST(unsigned long long, v),
2341 CAST(unsigned long long, l),
2342 CAST(unsigned long long, l),
2347 matched = (v & l) != l;
2348 if ((ms->flags & MAGIC_DEBUG) != 0)
2349 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2350 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2351 "x) = %d\n", CAST(unsigned long long, v),
2352 CAST(unsigned long long, l),
2353 CAST(unsigned long long, l), matched);
2357 file_magerror(ms, "cannot happen: invalid relation `%c'",
2366 handle_annotation(struct magic_set *ms, struct magic *m, int firstline)
2368 if ((ms->flags & MAGIC_APPLE) && m->apple[0]) {
2369 if (print_sep(ms, firstline) == -1)
2371 if (file_printf(ms, "%.8s", m->apple) == -1)
2375 if ((ms->flags & MAGIC_EXTENSION) && m->ext[0]) {
2376 if (print_sep(ms, firstline) == -1)
2378 if (file_printf(ms, "%s", m->ext) == -1)
2382 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2385 if (print_sep(ms, firstline) == -1)
2387 if (varexpand(ms, buf, sizeof(buf), m->mimetype) == -1)
2391 if (file_printf(ms, "%s", p) == -1)
2399 print_sep(struct magic_set *ms, int firstline)
2404 * we found another match
2405 * put a newline and '-' to do some simple formatting
2407 return file_separator(ms);