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.299 2020/06/07 21:58:01 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
283 if ((e = handle_annotation(ms, m, firstline)) != 0)
286 *printed_something = 1;
292 * If we are going to print something, we'll need to print
293 * a blank before we print something else.
295 if (print && *m->desc) {
297 *printed_something = 1;
299 if (print_sep(ms, firstline) == -1)
301 if (mprint(ms, m) == -1)
305 switch (moffset(ms, m, &bb, &ms->c.li[cont_level].off)) {
313 /* and any continuations that match */
314 if (file_check_mem(ms, ++cont_level) == -1)
317 while (magindex + 1 < nmagic &&
318 magic[magindex + 1].cont_level != 0) {
319 m = &magic[++magindex];
320 ms->line = m->lineno; /* for messages */
322 if (cont_level < m->cont_level)
324 if (cont_level > m->cont_level) {
326 * We're at the end of the level
327 * "cont_level" continuations.
329 cont_level = m->cont_level;
331 if (msetoffset(ms, m, &bb, b, offset, cont_level) == -1)
333 if (m->flag & OFFADD) {
334 if (cont_level == 0) {
335 if ((ms->flags & MAGIC_DEBUG) != 0)
342 ms->c.li[cont_level - 1].off;
345 #ifdef ENABLE_CONDITIONALS
346 if (m->cond == COND_ELSE ||
347 m->cond == COND_ELIF) {
348 if (ms->c.li[cont_level].last_match == 1)
352 switch (mget(ms, m, b, CAST(const unsigned char *,
353 bb.fbuf), bb.flen, offset,
354 cont_level, mode, text, flip, indir_count,
355 name_count, printed_something, need_separator,
356 returnval, found_match)) {
365 if (m->type == FILE_INDIRECT) {
373 switch (flush ? 1 : magiccheck(ms, m)) {
377 #ifdef ENABLE_CONDITIONALS
378 ms->c.li[cont_level].last_match = 0;
382 #ifdef ENABLE_CONDITIONALS
383 ms->c.li[cont_level].last_match = 1;
385 if (m->type == FILE_CLEAR)
386 ms->c.li[cont_level].got_match = 0;
387 else if (ms->c.li[cont_level].got_match) {
388 if (m->type == FILE_DEFAULT)
391 ms->c.li[cont_level].got_match = 1;
396 if ((e = handle_annotation(ms, m, firstline))
399 *printed_something = 1;
403 if (print && *m->desc) {
405 * This continuation matched. Print
406 * its message, with a blank before it
407 * if the previous item printed and
408 * this item isn't empty.
411 * If we are going to print something,
412 * make sure that we have a separator
415 if (!*printed_something) {
416 *printed_something = 1;
417 if (print_sep(ms, firstline)
421 /* space if previous printed */
423 && (m->flag & NOSPACE) == 0) {
424 if (file_printf(ms, " ") == -1)
429 if (mprint(ms, m) == -1)
434 switch (moffset(ms, m, &bb,
435 &ms->c.li[cont_level].off)) {
446 * If we see any continuations
450 if (file_check_mem(ms, ++cont_level) == -1)
455 if (*printed_something) {
459 if ((ms->flags & MAGIC_CONTINUE) == 0)
460 return *returnval; /* don't keep searching */
461 // So that we print a separator
462 *printed_something = 0;
467 return *returnval; /* This is hit if -k is set or there is no match */
471 check_fmt(struct magic_set *ms, const char *fmt)
476 if (strchr(fmt, '%') == NULL)
479 rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
481 file_regerror(&rx, rc, ms);
483 rc = file_regexec(&rx, fmt, 0, 0, 0);
490 #if !defined(HAVE_STRNDUP) || defined(__aiws__)
492 # define strndup aix_strndup /* aix is broken */
494 char *strndup(const char *, size_t);
497 strndup(const char *str, size_t n)
502 for (len = 0; len < n && str[len]; len++)
504 if ((copy = malloc(len + 1)) == NULL)
506 (void)memcpy(copy, str, len);
510 #endif /* HAVE_STRNDUP */
513 varexpand(struct magic_set *ms, char *buf, size_t len, const char *str)
515 const char *ptr, *sptr, *e, *t, *ee, *et;
518 for (sptr = str; (ptr = strstr(sptr, "${")) != NULL;) {
519 l = CAST(size_t, ptr - sptr);
522 memcpy(buf, sptr, l);
526 if (!*ptr || ptr[1] != '?')
528 for (et = t = ptr + 2; *et && *et != ':'; et++)
532 for (ee = e = et + 1; *ee && *ee != '}'; ee++)
538 if (ms->mode & 0111) {
561 memcpy(buf, sptr, l);
568 mprint(struct magic_set *ms, struct magic *m)
574 char buf[128], tbuf[26], sbuf[512], ebuf[512];
576 union VALUETYPE *p = &ms->ms_value;
578 if (varexpand(ms, ebuf, sizeof(ebuf), m->desc) == -1)
585 v = file_signextend(ms, m, CAST(uint64_t, p->b));
586 switch (check_fmt(ms, desc)) {
590 (void)snprintf(buf, sizeof(buf), "%d",
591 CAST(unsigned char, v));
592 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
596 if (file_printf(ms, F(ms, desc, "%d"),
597 CAST(unsigned char, v)) == -1)
601 t = ms->offset + sizeof(char);
607 v = file_signextend(ms, m, CAST(uint64_t, p->h));
608 switch (check_fmt(ms, desc)) {
612 (void)snprintf(buf, sizeof(buf), "%u",
613 CAST(unsigned short, v));
614 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
618 if (file_printf(ms, F(ms, desc, "%u"),
619 CAST(unsigned short, v)) == -1)
623 t = ms->offset + sizeof(short);
630 v = file_signextend(ms, m, CAST(uint64_t, p->l));
631 switch (check_fmt(ms, desc)) {
635 (void)snprintf(buf, sizeof(buf), "%u",
637 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
641 if (file_printf(ms, F(ms, desc, "%u"),
642 CAST(uint32_t, v)) == -1)
646 t = ms->offset + sizeof(int32_t);
653 v = file_signextend(ms, m, p->q);
654 switch (check_fmt(ms, desc)) {
658 (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
659 CAST(unsigned long long, v));
660 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
664 if (file_printf(ms, F(ms, desc, "%" INT64_T_FORMAT "u"),
665 CAST(unsigned long long, v)) == -1)
669 t = ms->offset + sizeof(int64_t);
674 case FILE_BESTRING16:
675 case FILE_LESTRING16:
676 if (m->reln == '=' || m->reln == '!') {
677 if (file_printf(ms, F(ms, desc, "%s"),
678 file_printable(sbuf, sizeof(sbuf), m->value.s,
679 sizeof(m->value.s))) == -1)
681 t = ms->offset + m->vallen;
686 /* compute t before we mangle the string? */
687 t = ms->offset + strlen(str);
689 if (*m->value.s == '\0')
690 str[strcspn(str, "\r\n")] = '\0';
692 if (m->str_flags & STRING_TRIM) {
694 while (isspace(CAST(unsigned char, *str)))
700 while (isspace(CAST(unsigned char, *last)))
705 if (file_printf(ms, F(ms, desc, "%s"),
706 file_printable(sbuf, sizeof(sbuf), str,
707 sizeof(p->s) - (str - p->s))) == -1)
710 if (m->type == FILE_PSTRING) {
711 size_t l = file_pstring_length_size(ms, m);
712 if (l == FILE_BADSIZE)
723 if (file_printf(ms, F(ms, desc, "%s"),
724 file_fmttime(tbuf, sizeof(tbuf), p->l, 0)) == -1)
726 t = ms->offset + sizeof(uint32_t);
733 if (file_printf(ms, F(ms, desc, "%s"),
734 file_fmttime(tbuf, sizeof(tbuf), p->l, FILE_T_LOCAL)) == -1)
736 t = ms->offset + sizeof(uint32_t);
742 if (file_printf(ms, F(ms, desc, "%s"),
743 file_fmttime(tbuf, sizeof(tbuf), p->q, 0)) == -1)
745 t = ms->offset + sizeof(uint64_t);
751 if (file_printf(ms, F(ms, desc, "%s"),
752 file_fmttime(tbuf, sizeof(tbuf), p->q, FILE_T_LOCAL)) == -1)
754 t = ms->offset + sizeof(uint64_t);
760 if (file_printf(ms, F(ms, desc, "%s"),
761 file_fmttime(tbuf, sizeof(tbuf), p->q, FILE_T_WINDOWS))
764 t = ms->offset + sizeof(uint64_t);
771 switch (check_fmt(ms, desc)) {
775 (void)snprintf(buf, sizeof(buf), "%g", vf);
776 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
780 if (file_printf(ms, F(ms, desc, "%g"), vf) == -1)
784 t = ms->offset + sizeof(float);
791 switch (check_fmt(ms, desc)) {
795 (void)snprintf(buf, sizeof(buf), "%g", vd);
796 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
800 if (file_printf(ms, F(ms, desc, "%g"), vd) == -1)
804 t = ms->offset + sizeof(double);
812 cp = strndup(RCAST(const char *, ms->search.s),
815 file_oomem(ms, ms->search.rm_len);
818 rval = file_printf(ms, F(ms, desc, "%s"),
819 file_printable(sbuf, sizeof(sbuf), cp, ms->search.rm_len));
825 if ((m->str_flags & REGEX_OFFSET_START))
826 t = ms->search.offset;
828 t = ms->search.offset + ms->search.rm_len;
834 if (file_printf(ms, "%s", m->desc) == -1)
845 if (file_printf(ms, F(ms, desc, "%s"),
846 file_printable(sbuf, sizeof(sbuf), ms->ms_value.s,
847 sizeof(ms->ms_value.s))) == -1)
852 (void) file_print_guid(buf, sizeof(buf), ms->ms_value.guid);
853 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
858 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
861 return CAST(int32_t, t);
865 moffset(struct magic_set *ms, struct magic *m, const struct buffer *b,
868 size_t nbytes = b->flen;
873 o = CAST(int32_t, (ms->offset + sizeof(char)));
879 o = CAST(int32_t, (ms->offset + sizeof(short)));
886 o = CAST(int32_t, (ms->offset + sizeof(int32_t)));
892 o = CAST(int32_t, (ms->offset + sizeof(int64_t)));
897 case FILE_BESTRING16:
898 case FILE_LESTRING16:
899 if (m->reln == '=' || m->reln == '!') {
900 o = ms->offset + m->vallen;
902 union VALUETYPE *p = &ms->ms_value;
904 if (*m->value.s == '\0')
905 p->s[strcspn(p->s, "\r\n")] = '\0';
906 o = CAST(uint32_t, (ms->offset + strlen(p->s)));
907 if (m->type == FILE_PSTRING) {
908 size_t l = file_pstring_length_size(ms, m);
909 if (l == FILE_BADSIZE)
911 o += CAST(uint32_t, l);
920 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
927 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
933 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
939 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
945 o = CAST(int32_t, (ms->offset + sizeof(float)));
951 o = CAST(int32_t, (ms->offset + sizeof(double)));
955 if ((m->str_flags & REGEX_OFFSET_START) != 0)
956 o = CAST(int32_t, ms->search.offset);
959 (ms->search.offset + ms->search.rm_len));
963 if ((m->str_flags & REGEX_OFFSET_START) != 0)
964 o = CAST(int32_t, ms->search.offset);
966 o = CAST(int32_t, (ms->search.offset + m->vallen));
977 o = der_offs(ms, m, nbytes);
978 if (o == -1 || CAST(size_t, o) > nbytes) {
979 if ((ms->flags & MAGIC_DEBUG) != 0) {
980 (void)fprintf(stderr,
981 "Bad DER offset %d nbytes=%"
982 SIZE_T_FORMAT "u", o, nbytes);
990 o = CAST(int32_t, (ms->offset + 2 * sizeof(uint64_t)));
998 if (CAST(size_t, o) > nbytes) {
1000 file_error(ms, 0, "Offset out of range %" SIZE_T_FORMAT
1001 "u > %" SIZE_T_FORMAT "u", (size_t)o, nbytes);
1010 cvt_id3(struct magic_set *ms, uint32_t v)
1012 v = ((((v >> 0) & 0x7f) << 0) |
1013 (((v >> 8) & 0x7f) << 7) |
1014 (((v >> 16) & 0x7f) << 14) |
1015 (((v >> 24) & 0x7f) << 21));
1016 if ((ms->flags & MAGIC_DEBUG) != 0)
1017 fprintf(stderr, "id3 offs=%u\n", v);
1022 cvt_flip(int type, int flip)
1028 return FILE_LESHORT;
1034 return FILE_LELDATE;
1038 return FILE_LEQDATE;
1040 return FILE_LEQLDATE;
1042 return FILE_LEQWDATE;
1044 return FILE_BESHORT;
1050 return FILE_BELDATE;
1054 return FILE_BEQDATE;
1056 return FILE_BEQLDATE;
1058 return FILE_BEQWDATE;
1060 return FILE_LEFLOAT;
1062 return FILE_BEFLOAT;
1064 return FILE_LEDOUBLE;
1066 return FILE_BEDOUBLE;
1071 #define DO_CVT(fld, type) \
1073 switch (m->mask_op & FILE_OPS_MASK) { \
1075 p->fld &= CAST(type, m->num_mask); \
1078 p->fld |= CAST(type, m->num_mask); \
1081 p->fld ^= CAST(type, m->num_mask); \
1084 p->fld += CAST(type, m->num_mask); \
1086 case FILE_OPMINUS: \
1087 p->fld -= CAST(type, m->num_mask); \
1089 case FILE_OPMULTIPLY: \
1090 p->fld *= CAST(type, m->num_mask); \
1092 case FILE_OPDIVIDE: \
1093 if (CAST(type, m->num_mask) == 0) \
1095 p->fld /= CAST(type, m->num_mask); \
1097 case FILE_OPMODULO: \
1098 if (CAST(type, m->num_mask) == 0) \
1100 p->fld %= CAST(type, m->num_mask); \
1103 if (m->mask_op & FILE_OPINVERSE) \
1107 cvt_8(union VALUETYPE *p, const struct magic *m)
1114 cvt_16(union VALUETYPE *p, const struct magic *m)
1116 DO_CVT(h, uint16_t);
1121 cvt_32(union VALUETYPE *p, const struct magic *m)
1123 DO_CVT(l, uint32_t);
1128 cvt_64(union VALUETYPE *p, const struct magic *m)
1130 DO_CVT(q, uint64_t);
1134 #define DO_CVT2(fld, type) \
1136 switch (m->mask_op & FILE_OPS_MASK) { \
1138 p->fld += CAST(type, m->num_mask); \
1140 case FILE_OPMINUS: \
1141 p->fld -= CAST(type, m->num_mask); \
1143 case FILE_OPMULTIPLY: \
1144 p->fld *= CAST(type, m->num_mask); \
1146 case FILE_OPDIVIDE: \
1147 if (CAST(type, m->num_mask) == 0) \
1149 p->fld /= CAST(type, m->num_mask); \
1154 cvt_float(union VALUETYPE *p, const struct magic *m)
1161 cvt_double(union VALUETYPE *p, const struct magic *m)
1168 * Convert the byte order of the data we are looking at
1169 * While we're here, let's apply the mask operation
1170 * (unless you have a better idea)
1173 mconvert(struct magic_set *ms, struct magic *m, int flip)
1175 union VALUETYPE *p = &ms->ms_value;
1177 switch (cvt_flip(m->type, flip)) {
1179 if (cvt_8(p, m) == -1)
1183 if (cvt_16(p, m) == -1)
1189 if (cvt_32(p, m) == -1)
1197 if (cvt_64(p, m) == -1)
1201 case FILE_BESTRING16:
1202 case FILE_LESTRING16: {
1203 /* Null terminate and eat *trailing* return */
1204 p->s[sizeof(p->s) - 1] = '\0';
1207 case FILE_PSTRING: {
1209 size_t len, sz = file_pstring_length_size(ms, m);
1210 if (sz == FILE_BADSIZE)
1214 len = file_pstring_get_length(ms, m, ptr1);
1215 if (len == FILE_BADSIZE)
1217 sz = sizeof(p->s) - sz; /* maximum length of string */
1220 * The size of the pascal string length (sz)
1221 * is 1, 2, or 4. We need at least 1 byte for NUL
1222 * termination, but we've already truncated the
1223 * string by p->s, so we need to deduct sz.
1224 * Because we can use one of the bytes of the length
1225 * after we shifted as NUL termination.
1235 p->h = CAST(short, BE16(p));
1236 if (cvt_16(p, m) == -1)
1242 p->l = CAST(int32_t, BE32(p));
1243 if (cvt_32(p, m) == -1)
1250 p->q = CAST(uint64_t, BE64(p));
1251 if (cvt_64(p, m) == -1)
1255 p->h = CAST(short, LE16(p));
1256 if (cvt_16(p, m) == -1)
1262 p->l = CAST(int32_t, LE32(p));
1263 if (cvt_32(p, m) == -1)
1270 p->q = CAST(uint64_t, LE64(p));
1271 if (cvt_64(p, m) == -1)
1277 p->l = CAST(int32_t, ME32(p));
1278 if (cvt_32(p, m) == -1)
1282 if (cvt_float(p, m) == -1)
1287 if (cvt_float(p, m) == -1)
1292 if (cvt_float(p, m) == -1)
1296 if (cvt_double(p, m) == -1)
1301 if (cvt_double(p, m) == -1)
1306 if (cvt_double(p, m) == -1)
1319 file_magerror(ms, "invalid type %d in mconvert()", m->type);
1323 file_magerror(ms, "zerodivide in mconvert()");
1329 mdebug(uint32_t offset, const char *str, size_t len)
1331 (void) fprintf(stderr, "mget/%" SIZE_T_FORMAT "u @%d: ", len, offset);
1332 file_showstr(stderr, str, len);
1333 (void) fputc('\n', stderr);
1334 (void) fputc('\n', stderr);
1338 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
1339 const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
1342 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1343 * anything, but setup pointers into the source
1349 if (offset > nbytes)
1350 offset = CAST(uint32_t, nbytes);
1351 ms->search.s = RCAST(const char *, s) + offset;
1352 ms->search.s_len = nbytes - offset;
1353 ms->search.offset = offset;
1359 const char *last; /* end of search region */
1360 const char *buf; /* start of search region */
1362 size_t lines, linecnt, bytecnt;
1364 if (s == NULL || nbytes < offset) {
1365 ms->search.s_len = 0;
1366 ms->search.s = NULL;
1370 if (m->str_flags & REGEX_LINE_COUNT) {
1371 linecnt = m->str_range;
1372 bytecnt = linecnt * 80;
1375 bytecnt = m->str_range;
1378 if (bytecnt == 0 || bytecnt > nbytes - offset)
1379 bytecnt = nbytes - offset;
1380 if (bytecnt > ms->regex_max)
1381 bytecnt = ms->regex_max;
1383 buf = RCAST(const char *, s) + offset;
1384 end = last = RCAST(const char *, s) + bytecnt + offset;
1385 /* mget() guarantees buf <= last */
1386 for (lines = linecnt, b = buf; lines && b < end &&
1387 ((b = CAST(const char *,
1388 memchr(c = b, '\n', CAST(size_t, (end - b)))))
1389 || (b = CAST(const char *,
1390 memchr(c, '\r', CAST(size_t, (end - c))))));
1392 if (b < end - 1 && b[0] == '\r' && b[1] == '\n')
1394 if (b < end - 1 && b[0] == '\n')
1402 ms->search.s_len = last - buf;
1403 ms->search.offset = offset;
1404 ms->search.rm_len = 0;
1407 case FILE_BESTRING16:
1408 case FILE_LESTRING16: {
1409 const unsigned char *src = s + offset;
1410 const unsigned char *esrc = s + nbytes;
1412 char *edst = &p->s[sizeof(p->s) - 1];
1414 if (type == FILE_BESTRING16)
1417 /* check that offset is within range */
1418 if (offset >= nbytes)
1420 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
1426 if (type == FILE_BESTRING16 ?
1427 *(src - 1) != '\0' :
1428 ((src + 1 < esrc) &&
1429 *(src + 1) != '\0'))
1436 case FILE_STRING: /* XXX - these two should not need */
1437 case FILE_PSTRING: /* to copy anything, but do anyway. */
1443 if (type == FILE_OFFSET) {
1444 (void)memset(p, '\0', sizeof(*p));
1449 if (offset >= nbytes) {
1450 (void)memset(p, '\0', sizeof(*p));
1453 if (nbytes - offset < sizeof(*p))
1454 nbytes = nbytes - offset;
1456 nbytes = sizeof(*p);
1458 (void)memcpy(p, s + offset, nbytes);
1461 * the usefulness of padding with zeroes eludes me, it
1462 * might even cause problems
1464 if (nbytes < sizeof(*p))
1465 (void)memset(RCAST(char *, RCAST(void *, p)) + nbytes, '\0',
1466 sizeof(*p) - nbytes);
1471 do_ops(struct magic *m, intmax_t lhs, intmax_t off)
1475 switch (m->in_op & FILE_OPS_MASK) {
1491 case FILE_OPMULTIPLY:
1503 if (m->in_op & FILE_OPINVERSE)
1506 return CAST(uint32_t, offset);
1510 msetoffset(struct magic_set *ms, struct magic *m, struct buffer *bb,
1511 const struct buffer *b, size_t o, unsigned int cont_level)
1514 if (m->flag & OFFNEGATIVE) {
1515 offset = -m->offset;
1516 if (cont_level > 0) {
1517 if (m->flag & (OFFADD|INDIROFFADD))
1520 file_error(ms, 0, "negative offset %d at continuation"
1521 "level %u", m->offset, cont_level);
1525 if (buffer_fill(b) == -1)
1529 file_magerror(ms, "non zero offset %" SIZE_T_FORMAT
1530 "u at level %u", o, cont_level);
1533 if (CAST(size_t, m->offset) > b->elen)
1535 buffer_init(bb, -1, NULL, b->ebuf, b->elen);
1536 ms->eoffset = ms->offset = CAST(int32_t, b->elen - m->offset);
1539 if (cont_level == 0) {
1541 // XXX: Pass real fd, then who frees bb?
1542 buffer_init(bb, -1, NULL, b->fbuf, b->flen);
1543 ms->offset = offset;
1546 ms->offset = ms->eoffset + offset;
1549 if ((ms->flags & MAGIC_DEBUG) != 0) {
1550 fprintf(stderr, "bb=[%p,%" SIZE_T_FORMAT "u,%"
1551 SIZE_T_FORMAT "u], %d [b=%p,%"
1552 SIZE_T_FORMAT "u,%" SIZE_T_FORMAT "u], [o=%#x, c=%d]\n",
1553 bb->fbuf, bb->flen, bb->elen, ms->offset, b->fbuf,
1554 b->flen, b->elen, offset, cont_level);
1560 mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
1561 const unsigned char *s, size_t nbytes, size_t o, unsigned int cont_level,
1562 int mode, int text, int flip, uint16_t *indir_count, uint16_t *name_count,
1563 int *printed_something, int *need_separator, int *returnval,
1566 uint32_t offset = ms->offset;
1570 int rv, oneed_separator, in_type;
1572 union VALUETYPE *p = &ms->ms_value;
1575 if (*indir_count >= ms->indir_max) {
1576 file_error(ms, 0, "indirect count (%hu) exceeded",
1581 if (*name_count >= ms->name_max) {
1582 file_error(ms, 0, "name use count (%hu) exceeded",
1589 if (mcopy(ms, p, m->type, m->flag & INDIR, s,
1590 CAST(uint32_t, offset + o), CAST(uint32_t, nbytes), m) == -1)
1593 if ((ms->flags & MAGIC_DEBUG) != 0) {
1594 fprintf(stderr, "mget(type=%d, flag=%#x, offset=%u, o=%"
1595 SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
1596 "u, il=%hu, nc=%hu)\n",
1597 m->type, m->flag, offset, o, nbytes,
1598 *indir_count, *name_count);
1599 mdebug(offset, RCAST(char *, RCAST(void *, p)),
1600 sizeof(union VALUETYPE));
1601 #ifndef COMPILE_ONLY
1606 if (m->flag & INDIR) {
1607 intmax_t off = m->in_offset;
1608 const int sgn = m->in_op & FILE_OPSIGNED;
1609 if (m->in_op & FILE_OPINDIRECT) {
1610 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1611 RCAST(const void *, s + offset + off));
1613 switch (op = cvt_flip(m->in_type, flip)) {
1615 if (OFFSET_OOB(nbytes, offset + off, 1))
1617 off = SEXT(sgn,8,q->b);
1620 if (OFFSET_OOB(nbytes, offset + off, 2))
1622 off = SEXT(sgn,16,q->h);
1625 if (OFFSET_OOB(nbytes, offset + off, 2))
1627 off = SEXT(sgn,16,BE16(q));
1630 if (OFFSET_OOB(nbytes, offset + off, 2))
1632 off = SEXT(sgn,16,LE16(q));
1635 if (OFFSET_OOB(nbytes, offset + off, 4))
1637 off = SEXT(sgn,32,q->l);
1641 if (OFFSET_OOB(nbytes, offset + off, 4))
1643 off = SEXT(sgn,32,BE32(q));
1647 if (OFFSET_OOB(nbytes, offset + off, 4))
1649 off = SEXT(sgn,32,LE32(q));
1652 if (OFFSET_OOB(nbytes, offset + off, 4))
1654 off = SEXT(sgn,32,ME32(q));
1657 if (OFFSET_OOB(nbytes, offset + off, 8))
1659 off = SEXT(sgn,64,BE64(q));
1662 if (OFFSET_OOB(nbytes, offset + off, 8))
1664 off = SEXT(sgn,64,LE64(q));
1667 if ((ms->flags & MAGIC_DEBUG) != 0)
1668 fprintf(stderr, "bad op=%d\n", op);
1671 if ((ms->flags & MAGIC_DEBUG) != 0)
1672 fprintf(stderr, "indirect offs=%jd\n", off);
1674 switch (in_type = cvt_flip(m->in_type, flip)) {
1676 if (OFFSET_OOB(nbytes, offset, 1))
1678 offset = do_ops(m, SEXT(sgn,8,p->b), off);
1681 if (OFFSET_OOB(nbytes, offset, 2))
1683 offset = do_ops(m, SEXT(sgn,16,BE16(p)), off);
1686 if (OFFSET_OOB(nbytes, offset, 2))
1688 offset = do_ops(m, SEXT(sgn,16,LE16(p)), off);
1691 if (OFFSET_OOB(nbytes, offset, 2))
1693 offset = do_ops(m, SEXT(sgn,16,p->h), off);
1697 if (OFFSET_OOB(nbytes, offset, 4))
1700 if (in_type == FILE_BEID3)
1701 lhs = cvt_id3(ms, CAST(uint32_t, lhs));
1702 offset = do_ops(m, SEXT(sgn,32,lhs), off);
1706 if (OFFSET_OOB(nbytes, offset, 4))
1709 if (in_type == FILE_LEID3)
1710 lhs = cvt_id3(ms, CAST(uint32_t, lhs));
1711 offset = do_ops(m, SEXT(sgn,32,lhs), off);
1714 if (OFFSET_OOB(nbytes, offset, 4))
1716 offset = do_ops(m, SEXT(sgn,32,ME32(p)), off);
1719 if (OFFSET_OOB(nbytes, offset, 4))
1721 offset = do_ops(m, SEXT(sgn,32,p->l), off);
1724 if (OFFSET_OOB(nbytes, offset, 8))
1726 offset = do_ops(m, SEXT(sgn,64,LE64(p)), off);
1729 if (OFFSET_OOB(nbytes, offset, 8))
1731 offset = do_ops(m, SEXT(sgn,64,BE64(p)), off);
1734 if ((ms->flags & MAGIC_DEBUG) != 0)
1735 fprintf(stderr, "bad in_type=%d\n", in_type);
1739 if (m->flag & INDIROFFADD) {
1740 if (cont_level == 0) {
1741 if ((ms->flags & MAGIC_DEBUG) != 0)
1743 "indirect *zero* cont_level\n");
1746 offset += ms->c.li[cont_level - 1].off;
1748 if ((ms->flags & MAGIC_DEBUG) != 0)
1750 "indirect *zero* offset\n");
1753 if ((ms->flags & MAGIC_DEBUG) != 0)
1754 fprintf(stderr, "indirect +offs=%u\n", offset);
1756 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
1758 ms->offset = offset;
1760 if ((ms->flags & MAGIC_DEBUG) != 0) {
1761 mdebug(offset, RCAST(char *, RCAST(void *, p)),
1762 sizeof(union VALUETYPE));
1763 #ifndef COMPILE_ONLY
1769 /* Verify we have enough data to match magic type */
1772 if (OFFSET_OOB(nbytes, offset, 1))
1779 if (OFFSET_OOB(nbytes, offset, 2))
1798 if (OFFSET_OOB(nbytes, offset, 4))
1805 if (OFFSET_OOB(nbytes, offset, 8))
1810 if (OFFSET_OOB(nbytes, offset, 16))
1817 if (OFFSET_OOB(nbytes, offset, m->vallen))
1822 if (nbytes < offset)
1827 if (m->str_flags & INDIRECT_RELATIVE)
1828 offset += CAST(uint32_t, o);
1832 if (nbytes < offset)
1835 if ((pb = file_push_buffer(ms)) == NULL)
1840 bb.fbuf = s + offset;
1841 bb.flen = nbytes - offset;
1842 rv = file_softmagic(ms, &bb,
1843 indir_count, name_count, BINTEST, text);
1845 if ((ms->flags & MAGIC_DEBUG) != 0)
1846 fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1848 rbuf = file_pop_buffer(ms, pb);
1849 if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
1853 if ((ms->flags & MAGIC_NODESC) == 0 &&
1854 file_printf(ms, F(ms, m->desc, "%u"), offset) == -1)
1859 if (file_printf(ms, "%s", rbuf) == -1) {
1868 if (nbytes < offset)
1875 if (file_magicfind(ms, rbuf, &ml) == -1) {
1876 file_error(ms, 0, "cannot find entry `%s'", rbuf);
1880 oneed_separator = *need_separator;
1881 if (m->flag & NOSPACE)
1882 *need_separator = 0;
1883 rv = match(ms, ml.magic, ml.nmagic, b, offset + o,
1884 mode, text, flip, indir_count, name_count,
1885 printed_something, need_separator, returnval, found_match);
1888 *need_separator = oneed_separator;
1892 if (ms->flags & MAGIC_NODESC)
1894 if (file_printf(ms, "%s", m->desc) == -1)
1898 case FILE_DEFAULT: /* nothing to check */
1903 if (!mconvert(ms, m, flip))
1909 file_strncmp(const char *s1, const char *s2, size_t len, size_t maxlen,
1913 * Convert the source args to unsigned here so that (1) the
1914 * compare will be unsigned as it is in strncmp() and (2) so
1915 * the ctype functions will work correctly without extra
1918 const unsigned char *a = RCAST(const unsigned char *, s1);
1919 const unsigned char *b = RCAST(const unsigned char *, s2);
1920 uint32_t ws = flags & (STRING_COMPACT_WHITESPACE |
1921 STRING_COMPACT_OPTIONAL_WHITESPACE);
1922 const unsigned char *eb = b + (ws ? maxlen : len);
1926 * What we want here is v = strncmp(s1, s2, len),
1927 * but ignoring any nulls.
1930 if (0L == flags) { /* normal string: do it fast */
1932 if ((v = *b++ - *a++) != '\0')
1935 else { /* combine the others */
1941 if ((flags & STRING_IGNORE_LOWERCASE) &&
1943 if ((v = tolower(*b++) - *a++) != '\0')
1946 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1948 if ((v = toupper(*b++) - *a++) != '\0')
1951 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1954 if (isspace(*b++)) {
1956 while (b < eb && isspace(*b))
1964 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1967 while (b < eb && isspace(*b))
1971 if ((v = *b++ - *a++) != '\0')
1980 file_strncmp16(const char *a, const char *b, size_t len, size_t maxlen,
1984 * XXX - The 16-bit string compare probably needs to be done
1985 * differently, especially if the flags are to be supported.
1986 * At the moment, I am unsure.
1989 return file_strncmp(a, b, len, maxlen, flags);
1993 magiccheck(struct magic_set *ms, struct magic *m)
1995 uint64_t l = m->value.q;
2000 union VALUETYPE *p = &ms->ms_value;
2071 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
2104 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
2118 v = file_strncmp(m->value.s, p->s, CAST(size_t, m->vallen),
2119 sizeof(p->s), m->str_flags);
2122 case FILE_BESTRING16:
2123 case FILE_LESTRING16:
2125 v = file_strncmp16(m->value.s, p->s, CAST(size_t, m->vallen),
2126 sizeof(p->s), m->str_flags);
2129 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
2133 if (ms->search.s == NULL)
2136 slen = MIN(m->vallen, sizeof(m->value.s));
2140 if (slen > 0 && m->str_flags == 0) {
2142 idx = m->str_range + slen;
2143 if (m->str_range == 0 || ms->search.s_len < idx)
2144 idx = ms->search.s_len;
2145 found = CAST(const char *, memmem(ms->search.s, idx,
2149 idx = found - ms->search.s;
2150 ms->search.offset += idx;
2151 ms->search.rm_len = ms->search.s_len - idx;
2156 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
2157 if (slen + idx > ms->search.s_len)
2160 v = file_strncmp(m->value.s, ms->search.s + idx, slen,
2161 ms->search.s_len - idx, m->str_flags);
2162 if (v == 0) { /* found match */
2163 ms->search.offset += idx;
2164 ms->search.rm_len = ms->search.s_len - idx;
2175 if (ms->search.s == NULL)
2179 rc = file_regcomp(&rx, m->value.s,
2180 REG_EXTENDED|REG_NEWLINE|
2181 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
2183 file_regerror(&rx, rc, ms);
2184 v = CAST(uint64_t, -1);
2187 size_t slen = ms->search.s_len;
2190 copy = CAST(char *, malloc(slen));
2193 file_error(ms, errno,
2194 "can't allocate %" SIZE_T_FORMAT "u bytes",
2198 memcpy(copy, ms->search.s, slen);
2199 copy[--slen] = '\0';
2202 search = CCAST(char *, "");
2205 rc = file_regexec(&rx, RCAST(const char *, search),
2210 ms->search.s += CAST(int, pmatch.rm_so);
2211 ms->search.offset += CAST(size_t, pmatch.rm_so);
2212 ms->search.rm_len = CAST(size_t,
2213 pmatch.rm_eo - pmatch.rm_so);
2222 file_regerror(&rx, rc, ms);
2223 v = CAST(uint64_t, -1);
2228 if (v == CAST(uint64_t, -1))
2237 matched = der_cmp(ms, m);
2238 if (matched == -1) {
2239 if ((ms->flags & MAGIC_DEBUG) != 0) {
2240 (void) fprintf(stderr,
2241 "EOF comparing DER entries");
2248 v = memcmp(m->value.guid, p->guid, sizeof(p->guid));
2251 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2255 v = file_signextend(ms, m, v);
2259 if ((ms->flags & MAGIC_DEBUG) != 0)
2260 (void) fprintf(stderr, "%" INT64_T_FORMAT
2261 "u == *any* = 1\n", CAST(unsigned long long, v));
2267 if ((ms->flags & MAGIC_DEBUG) != 0)
2268 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
2269 INT64_T_FORMAT "u = %d\n",
2270 CAST(unsigned long long, v),
2271 CAST(unsigned long long, l), matched);
2276 if ((ms->flags & MAGIC_DEBUG) != 0)
2277 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2278 INT64_T_FORMAT "u = %d\n",
2279 CAST(unsigned long long, v),
2280 CAST(unsigned long long, l), matched);
2284 if (m->flag & UNSIGNED) {
2286 if ((ms->flags & MAGIC_DEBUG) != 0)
2287 (void) fprintf(stderr, "%" INT64_T_FORMAT
2288 "u > %" INT64_T_FORMAT "u = %d\n",
2289 CAST(unsigned long long, v),
2290 CAST(unsigned long long, l), matched);
2293 matched = CAST(int64_t, v) > CAST(int64_t, l);
2294 if ((ms->flags & MAGIC_DEBUG) != 0)
2295 (void) fprintf(stderr, "%" INT64_T_FORMAT
2296 "d > %" INT64_T_FORMAT "d = %d\n",
2298 CAST(long long, l), matched);
2303 if (m->flag & UNSIGNED) {
2305 if ((ms->flags & MAGIC_DEBUG) != 0)
2306 (void) fprintf(stderr, "%" INT64_T_FORMAT
2307 "u < %" INT64_T_FORMAT "u = %d\n",
2308 CAST(unsigned long long, v),
2309 CAST(unsigned long long, l), matched);
2312 matched = CAST(int64_t, v) < CAST(int64_t, l);
2313 if ((ms->flags & MAGIC_DEBUG) != 0)
2314 (void) fprintf(stderr, "%" INT64_T_FORMAT
2315 "d < %" INT64_T_FORMAT "d = %d\n",
2317 CAST(long long, l), matched);
2322 matched = (v & l) == l;
2323 if ((ms->flags & MAGIC_DEBUG) != 0)
2324 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2325 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2326 "x) = %d\n", CAST(unsigned long long, v),
2327 CAST(unsigned long long, l),
2328 CAST(unsigned long long, l),
2333 matched = (v & l) != l;
2334 if ((ms->flags & MAGIC_DEBUG) != 0)
2335 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2336 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2337 "x) = %d\n", CAST(unsigned long long, v),
2338 CAST(unsigned long long, l),
2339 CAST(unsigned long long, l), matched);
2343 file_magerror(ms, "cannot happen: invalid relation `%c'",
2352 handle_annotation(struct magic_set *ms, struct magic *m, int firstline)
2354 if ((ms->flags & MAGIC_APPLE) && m->apple[0]) {
2355 if (print_sep(ms, firstline) == -1)
2357 if (file_printf(ms, "%.8s", m->apple) == -1)
2361 if ((ms->flags & MAGIC_EXTENSION) && m->ext[0]) {
2362 if (print_sep(ms, firstline) == -1)
2364 if (file_printf(ms, "%s", m->ext) == -1)
2368 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2371 if (print_sep(ms, firstline) == -1)
2373 if (varexpand(ms, buf, sizeof(buf), m->mimetype) == -1)
2377 if (file_printf(ms, "%s", p) == -1)
2385 print_sep(struct magic_set *ms, int firstline)
2390 * we found another match
2391 * put a newline and '-' to do some simple formatting
2393 return file_separator(ms);