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.309 2021/02/05 22:29:07 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;
406 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)
431 if (mprint(ms, m) == -1)
436 switch (moffset(ms, m, &bb,
437 &ms->c.li[cont_level].off)) {
448 * If we see any continuations
452 if (file_check_mem(ms, ++cont_level) == -1)
457 if (*printed_something) {
461 if ((ms->flags & MAGIC_CONTINUE) == 0)
463 // So that we print a separator
464 *printed_something = 0;
471 * If we are not printing (we are doing mime etc.)
472 * and we did not find a mime entry, and we are at 0 level
473 * we want to return 0 so that the default mime printer
474 * takes over and prints "application/octet-stream"
476 if (! print && ! *printed_something && ! *name_count)
478 return *returnval; /* This is hit if -k is set or there is no match */
482 check_fmt(struct magic_set *ms, const char *fmt)
487 if (strchr(fmt, '%') == NULL)
490 rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
492 file_regerror(&rx, rc, ms);
494 rc = file_regexec(&rx, fmt, 0, 0, 0);
501 #if !defined(HAVE_STRNDUP) || defined(__aiws__) || defined(_AIX)
502 # if defined(__aiws__) || defined(_AIX)
503 # define strndup aix_strndup /* aix is broken */
505 char *strndup(const char *, size_t);
508 strndup(const char *str, size_t n)
513 for (len = 0; len < n && str[len]; len++)
515 if ((copy = malloc(len + 1)) == NULL)
517 (void)memcpy(copy, str, len);
521 #endif /* HAVE_STRNDUP */
524 varexpand(struct magic_set *ms, char *buf, size_t len, const char *str)
526 const char *ptr, *sptr, *e, *t, *ee, *et;
529 for (sptr = str; (ptr = strstr(sptr, "${")) != NULL;) {
530 l = CAST(size_t, ptr - sptr);
533 memcpy(buf, sptr, l);
537 if (!*ptr || ptr[1] != '?')
539 for (et = t = ptr + 2; *et && *et != ':'; et++)
543 for (ee = e = et + 1; *ee && *ee != '}'; ee++)
549 if (ms->mode & 0111) {
572 memcpy(buf, sptr, l);
579 mprint(struct magic_set *ms, struct magic *m)
585 char buf[128], tbuf[26], sbuf[512], ebuf[512];
587 union VALUETYPE *p = &ms->ms_value;
589 if (varexpand(ms, ebuf, sizeof(ebuf), m->desc) == -1)
594 #define PRINTER(value, format, stype, utype) \
595 v = file_signextend(ms, m, CAST(uint64_t, value)); \
596 switch (check_fmt(ms, desc)) { \
600 if (m->flag & UNSIGNED) { \
601 (void)snprintf(buf, sizeof(buf), "%" format "u", \
604 (void)snprintf(buf, sizeof(buf), "%" format "d", \
607 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1) \
611 if (m->flag & UNSIGNED) { \
612 if (file_printf(ms, F(ms, desc, "%" format "u"), \
613 CAST(utype, v)) == -1) \
616 if (file_printf(ms, F(ms, desc, "%" format "d"), \
617 CAST(stype, v)) == -1) \
622 t = ms->offset + sizeof(stype); \
627 PRINTER(p->b, "", int8_t, uint8_t);
632 PRINTER(p->h, "", int16_t, uint16_t);
638 PRINTER(p->l, "", int32_t, uint32_t);
645 PRINTER(p->q, INT64_T_FORMAT, long long, unsigned long long);
650 case FILE_BESTRING16:
651 case FILE_LESTRING16:
652 if (m->reln == '=' || m->reln == '!') {
653 if (file_printf(ms, F(ms, desc, "%s"),
654 file_printable(sbuf, sizeof(sbuf), m->value.s,
655 sizeof(m->value.s))) == -1)
657 t = ms->offset + m->vallen;
662 /* compute t before we mangle the string? */
663 t = ms->offset + strlen(str);
665 if (*m->value.s == '\0')
666 str[strcspn(str, "\r\n")] = '\0';
668 if (m->str_flags & STRING_TRIM)
669 str = file_strtrim(str);
671 if (file_printf(ms, F(ms, desc, "%s"),
672 file_printable(sbuf, sizeof(sbuf), str,
673 sizeof(p->s) - (str - p->s))) == -1)
676 if (m->type == FILE_PSTRING) {
677 size_t l = file_pstring_length_size(ms, m);
678 if (l == FILE_BADSIZE)
689 if (file_printf(ms, F(ms, desc, "%s"),
690 file_fmttime(tbuf, sizeof(tbuf), p->l, 0)) == -1)
692 t = ms->offset + sizeof(uint32_t);
699 if (file_printf(ms, F(ms, desc, "%s"),
700 file_fmttime(tbuf, sizeof(tbuf), p->l, FILE_T_LOCAL)) == -1)
702 t = ms->offset + sizeof(uint32_t);
708 if (file_printf(ms, F(ms, desc, "%s"),
709 file_fmttime(tbuf, sizeof(tbuf), p->q, 0)) == -1)
711 t = ms->offset + sizeof(uint64_t);
717 if (file_printf(ms, F(ms, desc, "%s"),
718 file_fmttime(tbuf, sizeof(tbuf), p->q, FILE_T_LOCAL)) == -1)
720 t = ms->offset + sizeof(uint64_t);
726 if (file_printf(ms, F(ms, desc, "%s"),
727 file_fmttime(tbuf, sizeof(tbuf), p->q, FILE_T_WINDOWS))
730 t = ms->offset + sizeof(uint64_t);
737 switch (check_fmt(ms, desc)) {
741 (void)snprintf(buf, sizeof(buf), "%g", vf);
742 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
746 if (file_printf(ms, F(ms, desc, "%g"), vf) == -1)
750 t = ms->offset + sizeof(float);
757 switch (check_fmt(ms, desc)) {
761 (void)snprintf(buf, sizeof(buf), "%g", vd);
762 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
766 if (file_printf(ms, F(ms, desc, "%g"), vd) == -1)
770 t = ms->offset + sizeof(double);
778 cp = strndup(RCAST(const char *, ms->search.s),
781 file_oomem(ms, ms->search.rm_len);
784 scp = (m->str_flags & STRING_TRIM) ? file_strtrim(cp) : cp;
786 rval = file_printf(ms, F(ms, desc, "%s"),
787 file_printable(sbuf, sizeof(sbuf), scp, ms->search.rm_len));
793 if ((m->str_flags & REGEX_OFFSET_START))
794 t = ms->search.offset;
796 t = ms->search.offset + ms->search.rm_len;
802 if (file_printf(ms, "%s", m->desc) == -1)
813 if (file_printf(ms, F(ms, desc, "%s"),
814 file_printable(sbuf, sizeof(sbuf), ms->ms_value.s,
815 sizeof(ms->ms_value.s))) == -1)
820 (void) file_print_guid(buf, sizeof(buf), ms->ms_value.guid);
821 if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
826 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
829 return CAST(int32_t, t);
833 moffset(struct magic_set *ms, struct magic *m, const struct buffer *b,
836 size_t nbytes = b->flen;
841 o = CAST(int32_t, (ms->offset + sizeof(char)));
847 o = CAST(int32_t, (ms->offset + sizeof(short)));
854 o = CAST(int32_t, (ms->offset + sizeof(int32_t)));
860 o = CAST(int32_t, (ms->offset + sizeof(int64_t)));
865 case FILE_BESTRING16:
866 case FILE_LESTRING16:
867 if (m->reln == '=' || m->reln == '!') {
868 o = ms->offset + m->vallen;
870 union VALUETYPE *p = &ms->ms_value;
872 if (*m->value.s == '\0')
873 p->s[strcspn(p->s, "\r\n")] = '\0';
874 o = CAST(uint32_t, (ms->offset + strlen(p->s)));
875 if (m->type == FILE_PSTRING) {
876 size_t l = file_pstring_length_size(ms, m);
877 if (l == FILE_BADSIZE)
879 o += CAST(uint32_t, l);
888 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
895 o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
901 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
907 o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
913 o = CAST(int32_t, (ms->offset + sizeof(float)));
919 o = CAST(int32_t, (ms->offset + sizeof(double)));
923 if ((m->str_flags & REGEX_OFFSET_START) != 0)
924 o = CAST(int32_t, ms->search.offset);
927 (ms->search.offset + ms->search.rm_len));
931 if ((m->str_flags & REGEX_OFFSET_START) != 0)
932 o = CAST(int32_t, ms->search.offset);
934 o = CAST(int32_t, (ms->search.offset + m->vallen));
946 o = der_offs(ms, m, nbytes);
947 if (o == -1 || CAST(size_t, o) > nbytes) {
948 if ((ms->flags & MAGIC_DEBUG) != 0) {
949 (void)fprintf(stderr,
950 "Bad DER offset %d nbytes=%"
951 SIZE_T_FORMAT "u", o, nbytes);
959 o = CAST(int32_t, (ms->offset + 2 * sizeof(uint64_t)));
967 if (CAST(size_t, o) > nbytes) {
969 file_error(ms, 0, "Offset out of range %" SIZE_T_FORMAT
970 "u > %" SIZE_T_FORMAT "u", (size_t)o, nbytes);
979 cvt_id3(struct magic_set *ms, uint32_t v)
981 v = ((((v >> 0) & 0x7f) << 0) |
982 (((v >> 8) & 0x7f) << 7) |
983 (((v >> 16) & 0x7f) << 14) |
984 (((v >> 24) & 0x7f) << 21));
985 if ((ms->flags & MAGIC_DEBUG) != 0)
986 fprintf(stderr, "id3 offs=%u\n", v);
991 cvt_flip(int type, int flip)
1003 return FILE_LELDATE;
1007 return FILE_LEQDATE;
1009 return FILE_LEQLDATE;
1011 return FILE_LEQWDATE;
1013 return FILE_BESHORT;
1019 return FILE_BELDATE;
1023 return FILE_BEQDATE;
1025 return FILE_BEQLDATE;
1027 return FILE_BEQWDATE;
1029 return FILE_LEFLOAT;
1031 return FILE_BEFLOAT;
1033 return FILE_LEDOUBLE;
1035 return FILE_BEDOUBLE;
1040 #define DO_CVT(fld, type) \
1042 switch (m->mask_op & FILE_OPS_MASK) { \
1044 p->fld &= CAST(type, m->num_mask); \
1047 p->fld |= CAST(type, m->num_mask); \
1050 p->fld ^= CAST(type, m->num_mask); \
1053 p->fld += CAST(type, m->num_mask); \
1055 case FILE_OPMINUS: \
1056 p->fld -= CAST(type, m->num_mask); \
1058 case FILE_OPMULTIPLY: \
1059 p->fld *= CAST(type, m->num_mask); \
1061 case FILE_OPDIVIDE: \
1062 if (CAST(type, m->num_mask) == 0) \
1064 p->fld /= CAST(type, m->num_mask); \
1066 case FILE_OPMODULO: \
1067 if (CAST(type, m->num_mask) == 0) \
1069 p->fld %= CAST(type, m->num_mask); \
1072 if (m->mask_op & FILE_OPINVERSE) \
1076 cvt_8(union VALUETYPE *p, const struct magic *m)
1083 cvt_16(union VALUETYPE *p, const struct magic *m)
1085 DO_CVT(h, uint16_t);
1090 cvt_32(union VALUETYPE *p, const struct magic *m)
1092 DO_CVT(l, uint32_t);
1097 cvt_64(union VALUETYPE *p, const struct magic *m)
1099 DO_CVT(q, uint64_t);
1103 #define DO_CVT2(fld, type) \
1105 switch (m->mask_op & FILE_OPS_MASK) { \
1107 p->fld += CAST(type, m->num_mask); \
1109 case FILE_OPMINUS: \
1110 p->fld -= CAST(type, m->num_mask); \
1112 case FILE_OPMULTIPLY: \
1113 p->fld *= CAST(type, m->num_mask); \
1115 case FILE_OPDIVIDE: \
1116 if (CAST(type, m->num_mask) == 0) \
1118 p->fld /= CAST(type, m->num_mask); \
1123 cvt_float(union VALUETYPE *p, const struct magic *m)
1130 cvt_double(union VALUETYPE *p, const struct magic *m)
1137 * Convert the byte order of the data we are looking at
1138 * While we're here, let's apply the mask operation
1139 * (unless you have a better idea)
1142 mconvert(struct magic_set *ms, struct magic *m, int flip)
1144 union VALUETYPE *p = &ms->ms_value;
1146 switch (cvt_flip(m->type, flip)) {
1148 if (cvt_8(p, m) == -1)
1152 if (cvt_16(p, m) == -1)
1158 if (cvt_32(p, m) == -1)
1166 if (cvt_64(p, m) == -1)
1170 case FILE_BESTRING16:
1171 case FILE_LESTRING16: {
1172 /* Null terminate and eat *trailing* return */
1173 p->s[sizeof(p->s) - 1] = '\0';
1176 case FILE_PSTRING: {
1178 size_t len, sz = file_pstring_length_size(ms, m);
1179 if (sz == FILE_BADSIZE)
1183 len = file_pstring_get_length(ms, m, ptr1);
1184 if (len == FILE_BADSIZE)
1186 sz = sizeof(p->s) - sz; /* maximum length of string */
1189 * The size of the pascal string length (sz)
1190 * is 1, 2, or 4. We need at least 1 byte for NUL
1191 * termination, but we've already truncated the
1192 * string by p->s, so we need to deduct sz.
1193 * Because we can use one of the bytes of the length
1194 * after we shifted as NUL termination.
1204 p->h = CAST(short, BE16(p));
1205 if (cvt_16(p, m) == -1)
1211 p->l = CAST(int32_t, BE32(p));
1212 if (cvt_32(p, m) == -1)
1219 p->q = CAST(uint64_t, BE64(p));
1220 if (cvt_64(p, m) == -1)
1224 p->h = CAST(short, LE16(p));
1225 if (cvt_16(p, m) == -1)
1231 p->l = CAST(int32_t, LE32(p));
1232 if (cvt_32(p, m) == -1)
1239 p->q = CAST(uint64_t, LE64(p));
1240 if (cvt_64(p, m) == -1)
1246 p->l = CAST(int32_t, ME32(p));
1247 if (cvt_32(p, m) == -1)
1251 if (cvt_float(p, m) == -1)
1256 if (cvt_float(p, m) == -1)
1261 if (cvt_float(p, m) == -1)
1265 if (cvt_double(p, m) == -1)
1270 if (cvt_double(p, m) == -1)
1275 if (cvt_double(p, m) == -1)
1288 file_magerror(ms, "invalid type %d in mconvert()", m->type);
1292 file_magerror(ms, "zerodivide in mconvert()");
1298 mdebug(uint32_t offset, const char *str, size_t len)
1300 (void) fprintf(stderr, "mget/%" SIZE_T_FORMAT "u @%d: ", len, offset);
1301 file_showstr(stderr, str, len);
1302 (void) fputc('\n', stderr);
1303 (void) fputc('\n', stderr);
1307 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
1308 const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
1311 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1312 * anything, but setup pointers into the source
1318 if (offset > nbytes)
1319 offset = CAST(uint32_t, nbytes);
1320 ms->search.s = RCAST(const char *, s) + offset;
1321 ms->search.s_len = nbytes - offset;
1322 ms->search.offset = offset;
1328 const char *last; /* end of search region */
1329 const char *buf; /* start of search region */
1331 size_t lines, linecnt, bytecnt;
1333 if (s == NULL || nbytes < offset) {
1334 ms->search.s_len = 0;
1335 ms->search.s = NULL;
1339 if (m->str_flags & REGEX_LINE_COUNT) {
1340 linecnt = m->str_range;
1341 bytecnt = linecnt * 80;
1344 bytecnt = m->str_range;
1347 if (bytecnt == 0 || bytecnt > nbytes - offset)
1348 bytecnt = nbytes - offset;
1349 if (bytecnt > ms->regex_max)
1350 bytecnt = ms->regex_max;
1352 buf = RCAST(const char *, s) + offset;
1353 end = last = RCAST(const char *, s) + bytecnt + offset;
1354 /* mget() guarantees buf <= last */
1355 for (lines = linecnt, b = buf; lines && b < end &&
1356 ((b = CAST(const char *,
1357 memchr(c = b, '\n', CAST(size_t, (end - b)))))
1358 || (b = CAST(const char *,
1359 memchr(c, '\r', CAST(size_t, (end - c))))));
1361 if (b < end - 1 && b[0] == '\r' && b[1] == '\n')
1363 if (b < end - 1 && b[0] == '\n')
1371 ms->search.s_len = last - buf;
1372 ms->search.offset = offset;
1373 ms->search.rm_len = 0;
1376 case FILE_BESTRING16:
1377 case FILE_LESTRING16: {
1378 const unsigned char *src = s + offset;
1379 const unsigned char *esrc = s + nbytes;
1381 char *edst = &p->s[sizeof(p->s) - 1];
1383 if (type == FILE_BESTRING16)
1386 /* check that offset is within range */
1387 if (offset >= nbytes)
1389 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
1395 if (type == FILE_BESTRING16 ?
1396 *(src - 1) != '\0' :
1397 ((src + 1 < esrc) &&
1398 *(src + 1) != '\0'))
1405 case FILE_STRING: /* XXX - these two should not need */
1406 case FILE_PSTRING: /* to copy anything, but do anyway. */
1412 if (type == FILE_OFFSET) {
1413 (void)memset(p, '\0', sizeof(*p));
1418 if (offset >= nbytes) {
1419 (void)memset(p, '\0', sizeof(*p));
1422 if (nbytes - offset < sizeof(*p))
1423 nbytes = nbytes - offset;
1425 nbytes = sizeof(*p);
1427 (void)memcpy(p, s + offset, nbytes);
1430 * the usefulness of padding with zeroes eludes me, it
1431 * might even cause problems
1433 if (nbytes < sizeof(*p))
1434 (void)memset(RCAST(char *, RCAST(void *, p)) + nbytes, '\0',
1435 sizeof(*p) - nbytes);
1440 do_ops(struct magic *m, intmax_t lhs, intmax_t off)
1442 intmax_t offset = 0;
1444 switch (m->in_op & FILE_OPS_MASK) {
1460 case FILE_OPMULTIPLY:
1472 if (m->in_op & FILE_OPINVERSE)
1475 return CAST(uint32_t, offset);
1479 msetoffset(struct magic_set *ms, struct magic *m, struct buffer *bb,
1480 const struct buffer *b, size_t o, unsigned int cont_level)
1483 if (m->flag & OFFNEGATIVE) {
1484 offset = -m->offset;
1485 if (cont_level > 0) {
1486 if (m->flag & (OFFADD|INDIROFFADD))
1489 file_error(ms, 0, "negative offset %d at continuation"
1490 "level %u", m->offset, cont_level);
1494 if (buffer_fill(b) == -1)
1498 file_magerror(ms, "non zero offset %" SIZE_T_FORMAT
1499 "u at level %u", o, cont_level);
1502 if (CAST(size_t, m->offset) > b->elen)
1504 buffer_init(bb, -1, NULL, b->ebuf, b->elen);
1505 ms->eoffset = ms->offset = CAST(int32_t, b->elen - m->offset);
1508 if (cont_level == 0) {
1510 // XXX: Pass real fd, then who frees bb?
1511 buffer_init(bb, -1, NULL, b->fbuf, b->flen);
1512 ms->offset = offset;
1515 ms->offset = ms->eoffset + offset;
1518 if ((ms->flags & MAGIC_DEBUG) != 0) {
1519 fprintf(stderr, "bb=[%p,%" SIZE_T_FORMAT "u,%"
1520 SIZE_T_FORMAT "u], %d [b=%p,%"
1521 SIZE_T_FORMAT "u,%" SIZE_T_FORMAT "u], [o=%#x, c=%d]\n",
1522 bb->fbuf, bb->flen, bb->elen, ms->offset, b->fbuf,
1523 b->flen, b->elen, offset, cont_level);
1529 save_cont(struct magic_set *ms, struct cont *c)
1533 len = c->len * sizeof(*c->li);
1534 ms->c.li = CAST(struct level_info *, malloc(len));
1535 if (ms->c.li == NULL) {
1539 memcpy(ms->c.li, c->li, len);
1544 restore_cont(struct magic_set *ms, struct cont *c)
1551 mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
1552 const unsigned char *s, size_t nbytes, size_t o, unsigned int cont_level,
1553 int mode, int text, int flip, uint16_t *indir_count, uint16_t *name_count,
1554 int *printed_something, int *need_separator, int *returnval,
1557 uint32_t eoffset, offset = ms->offset;
1561 int rv, oneed_separator, in_type, nfound_match;
1563 union VALUETYPE *p = &ms->ms_value;
1567 if (*indir_count >= ms->indir_max) {
1568 file_error(ms, 0, "indirect count (%hu) exceeded",
1573 if (*name_count >= ms->name_max) {
1574 file_error(ms, 0, "name use count (%hu) exceeded",
1581 if (mcopy(ms, p, m->type, m->flag & INDIR, s,
1582 CAST(uint32_t, offset + o), CAST(uint32_t, nbytes), m) == -1)
1585 if ((ms->flags & MAGIC_DEBUG) != 0) {
1586 fprintf(stderr, "mget(type=%d, flag=%#x, offset=%u, o=%"
1587 SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
1588 "u, il=%hu, nc=%hu)\n",
1589 m->type, m->flag, offset, o, nbytes,
1590 *indir_count, *name_count);
1591 mdebug(offset, RCAST(char *, RCAST(void *, p)),
1592 sizeof(union VALUETYPE));
1593 #ifndef COMPILE_ONLY
1598 if (m->flag & INDIR) {
1599 intmax_t off = m->in_offset;
1600 const int sgn = m->in_op & FILE_OPSIGNED;
1601 if (m->in_op & FILE_OPINDIRECT) {
1602 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1603 RCAST(const void *, s + offset + off));
1605 switch (op = cvt_flip(m->in_type, flip)) {
1607 if (OFFSET_OOB(nbytes, offset + off, 1))
1609 off = SEXT(sgn,8,q->b);
1612 if (OFFSET_OOB(nbytes, offset + off, 2))
1614 off = SEXT(sgn,16,q->h);
1617 if (OFFSET_OOB(nbytes, offset + off, 2))
1619 off = SEXT(sgn,16,BE16(q));
1622 if (OFFSET_OOB(nbytes, offset + off, 2))
1624 off = SEXT(sgn,16,LE16(q));
1627 if (OFFSET_OOB(nbytes, offset + off, 4))
1629 off = SEXT(sgn,32,q->l);
1633 if (OFFSET_OOB(nbytes, offset + off, 4))
1635 off = SEXT(sgn,32,BE32(q));
1639 if (OFFSET_OOB(nbytes, offset + off, 4))
1641 off = SEXT(sgn,32,LE32(q));
1644 if (OFFSET_OOB(nbytes, offset + off, 4))
1646 off = SEXT(sgn,32,ME32(q));
1649 if (OFFSET_OOB(nbytes, offset + off, 8))
1651 off = SEXT(sgn,64,BE64(q));
1654 if (OFFSET_OOB(nbytes, offset + off, 8))
1656 off = SEXT(sgn,64,LE64(q));
1659 if ((ms->flags & MAGIC_DEBUG) != 0)
1660 fprintf(stderr, "bad op=%d\n", op);
1663 if ((ms->flags & MAGIC_DEBUG) != 0)
1664 fprintf(stderr, "indirect offs=%jd\n", off);
1666 switch (in_type = cvt_flip(m->in_type, flip)) {
1668 if (OFFSET_OOB(nbytes, offset, 1))
1670 offset = do_ops(m, SEXT(sgn,8,p->b), off);
1673 if (OFFSET_OOB(nbytes, offset, 2))
1675 offset = do_ops(m, SEXT(sgn,16,BE16(p)), off);
1678 if (OFFSET_OOB(nbytes, offset, 2))
1680 offset = do_ops(m, SEXT(sgn,16,LE16(p)), off);
1683 if (OFFSET_OOB(nbytes, offset, 2))
1685 offset = do_ops(m, SEXT(sgn,16,p->h), off);
1689 if (OFFSET_OOB(nbytes, offset, 4))
1692 if (in_type == FILE_BEID3)
1693 lhs = cvt_id3(ms, CAST(uint32_t, lhs));
1694 offset = do_ops(m, SEXT(sgn,32,lhs), off);
1698 if (OFFSET_OOB(nbytes, offset, 4))
1701 if (in_type == FILE_LEID3)
1702 lhs = cvt_id3(ms, CAST(uint32_t, lhs));
1703 offset = do_ops(m, SEXT(sgn,32,lhs), off);
1706 if (OFFSET_OOB(nbytes, offset, 4))
1708 offset = do_ops(m, SEXT(sgn,32,ME32(p)), off);
1711 if (OFFSET_OOB(nbytes, offset, 4))
1713 offset = do_ops(m, SEXT(sgn,32,p->l), off);
1716 if (OFFSET_OOB(nbytes, offset, 8))
1718 offset = do_ops(m, SEXT(sgn,64,LE64(p)), off);
1721 if (OFFSET_OOB(nbytes, offset, 8))
1723 offset = do_ops(m, SEXT(sgn,64,BE64(p)), off);
1726 if ((ms->flags & MAGIC_DEBUG) != 0)
1727 fprintf(stderr, "bad in_type=%d\n", in_type);
1731 if (m->flag & INDIROFFADD) {
1732 if (cont_level == 0) {
1733 if ((ms->flags & MAGIC_DEBUG) != 0)
1735 "indirect *zero* cont_level\n");
1738 offset += ms->c.li[cont_level - 1].off;
1740 if ((ms->flags & MAGIC_DEBUG) != 0)
1742 "indirect *zero* offset\n");
1745 if ((ms->flags & MAGIC_DEBUG) != 0)
1746 fprintf(stderr, "indirect +offs=%u\n", offset);
1748 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
1750 ms->offset = offset;
1752 if ((ms->flags & MAGIC_DEBUG) != 0) {
1753 mdebug(offset, RCAST(char *, RCAST(void *, p)),
1754 sizeof(union VALUETYPE));
1755 #ifndef COMPILE_ONLY
1761 /* Verify we have enough data to match magic type */
1764 if (OFFSET_OOB(nbytes, offset, 1))
1771 if (OFFSET_OOB(nbytes, offset, 2))
1790 if (OFFSET_OOB(nbytes, offset, 4))
1797 if (OFFSET_OOB(nbytes, offset, 8))
1802 if (OFFSET_OOB(nbytes, offset, 16))
1809 if (OFFSET_OOB(nbytes, offset, m->vallen))
1814 if (nbytes < offset)
1819 if (m->str_flags & INDIRECT_RELATIVE)
1820 offset += CAST(uint32_t, o);
1824 if (nbytes < offset)
1827 if ((pb = file_push_buffer(ms)) == NULL)
1832 bb.fbuf = s + offset;
1833 bb.flen = nbytes - offset;
1834 rv = file_softmagic(ms, &bb,
1835 indir_count, name_count, BINTEST, text);
1837 if ((ms->flags & MAGIC_DEBUG) != 0)
1838 fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1840 rbuf = file_pop_buffer(ms, pb);
1841 if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
1845 if ((ms->flags & MAGIC_NODESC) == 0 &&
1846 file_printf(ms, F(ms, m->desc, "%u"), offset) == -1)
1851 if (file_printf(ms, "%s", rbuf) == -1) {
1860 if (nbytes < offset)
1867 if (file_magicfind(ms, rbuf, &ml) == -1) {
1868 file_error(ms, 0, "cannot find entry `%s'", rbuf);
1871 if (save_cont(ms, &c) == -1) {
1872 file_error(ms, errno, "can't allocate continuation");
1876 oneed_separator = *need_separator;
1877 if (m->flag & NOSPACE)
1878 *need_separator = 0;
1882 eoffset = ms->eoffset;
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,
1887 ms->ms_value.q = nfound_match;
1889 *found_match |= nfound_match;
1891 restore_cont(ms, &c);
1894 *need_separator = oneed_separator;
1895 ms->offset = offset;
1896 ms->eoffset = eoffset;
1900 if (ms->flags & MAGIC_NODESC)
1902 if (file_printf(ms, "%s", m->desc) == -1)
1906 case FILE_DEFAULT: /* nothing to check */
1911 if (!mconvert(ms, m, flip))
1917 file_strncmp(const char *s1, const char *s2, size_t len, size_t maxlen,
1921 * Convert the source args to unsigned here so that (1) the
1922 * compare will be unsigned as it is in strncmp() and (2) so
1923 * the ctype functions will work correctly without extra
1926 const unsigned char *a = RCAST(const unsigned char *, s1);
1927 const unsigned char *b = RCAST(const unsigned char *, s2);
1928 uint32_t ws = flags & (STRING_COMPACT_WHITESPACE |
1929 STRING_COMPACT_OPTIONAL_WHITESPACE);
1930 const unsigned char *eb = b + (ws ? maxlen : len);
1934 * What we want here is v = strncmp(s1, s2, len),
1935 * but ignoring any nulls.
1938 if (0L == flags) { /* normal string: do it fast */
1940 if ((v = *b++ - *a++) != '\0')
1943 else { /* combine the others */
1949 if ((flags & STRING_IGNORE_LOWERCASE) &&
1951 if ((v = tolower(*b++) - *a++) != '\0')
1954 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1956 if ((v = toupper(*b++) - *a++) != '\0')
1959 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1962 if (isspace(*b++)) {
1964 while (b < eb && isspace(*b))
1972 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1975 while (b < eb && isspace(*b))
1979 if ((v = *b++ - *a++) != '\0')
1988 file_strncmp16(const char *a, const char *b, size_t len, size_t maxlen,
1992 * XXX - The 16-bit string compare probably needs to be done
1993 * differently, especially if the flags are to be supported.
1994 * At the moment, I am unsure.
1997 return file_strncmp(a, b, len, maxlen, flags);
2001 magiccheck(struct magic_set *ms, struct magic *m)
2003 uint64_t l = m->value.q;
2008 union VALUETYPE *p = &ms->ms_value;
2079 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
2112 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
2126 v = file_strncmp(m->value.s, p->s, CAST(size_t, m->vallen),
2127 sizeof(p->s), m->str_flags);
2130 case FILE_BESTRING16:
2131 case FILE_LESTRING16:
2133 v = file_strncmp16(m->value.s, p->s, CAST(size_t, m->vallen),
2134 sizeof(p->s), m->str_flags);
2137 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
2141 if (ms->search.s == NULL)
2144 slen = MIN(m->vallen, sizeof(m->value.s));
2148 if (slen > 0 && m->str_flags == 0) {
2150 idx = m->str_range + slen;
2151 if (m->str_range == 0 || ms->search.s_len < idx)
2152 idx = ms->search.s_len;
2153 found = CAST(const char *, memmem(ms->search.s, idx,
2157 idx = found - ms->search.s;
2158 ms->search.offset += idx;
2159 ms->search.rm_len = ms->search.s_len - idx;
2164 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
2165 if (slen + idx > ms->search.s_len)
2168 v = file_strncmp(m->value.s, ms->search.s + idx, slen,
2169 ms->search.s_len - idx, m->str_flags);
2170 if (v == 0) { /* found match */
2171 ms->search.offset += idx;
2172 ms->search.rm_len = ms->search.s_len - idx;
2183 if (ms->search.s == NULL)
2187 rc = file_regcomp(&rx, m->value.s,
2188 REG_EXTENDED|REG_NEWLINE|
2189 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
2191 file_regerror(&rx, rc, ms);
2192 v = CAST(uint64_t, -1);
2195 size_t slen = ms->search.s_len;
2198 copy = CAST(char *, malloc(slen));
2201 file_error(ms, errno,
2202 "can't allocate %" SIZE_T_FORMAT "u bytes",
2206 memcpy(copy, ms->search.s, slen);
2207 copy[--slen] = '\0';
2210 search = CCAST(char *, "");
2213 rc = file_regexec(&rx, RCAST(const char *, search),
2218 ms->search.s += CAST(int, pmatch.rm_so);
2219 ms->search.offset += CAST(size_t, pmatch.rm_so);
2220 ms->search.rm_len = CAST(size_t,
2221 pmatch.rm_eo - pmatch.rm_so);
2230 file_regerror(&rx, rc, ms);
2231 v = CAST(uint64_t, -1);
2236 if (v == CAST(uint64_t, -1))
2241 return ms->ms_value.q != 0;
2246 matched = der_cmp(ms, m);
2247 if (matched == -1) {
2248 if ((ms->flags & MAGIC_DEBUG) != 0) {
2249 (void) fprintf(stderr,
2250 "EOF comparing DER entries");
2257 v = memcmp(m->value.guid, p->guid, sizeof(p->guid));
2260 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2264 v = file_signextend(ms, m, v);
2268 if ((ms->flags & MAGIC_DEBUG) != 0)
2269 (void) fprintf(stderr, "%" INT64_T_FORMAT
2270 "u == *any* = 1\n", CAST(unsigned long long, v));
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);
2285 if ((ms->flags & MAGIC_DEBUG) != 0)
2286 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2287 INT64_T_FORMAT "u = %d\n",
2288 CAST(unsigned long long, v),
2289 CAST(unsigned long long, l), matched);
2293 if (m->flag & UNSIGNED) {
2295 if ((ms->flags & MAGIC_DEBUG) != 0)
2296 (void) fprintf(stderr, "%" INT64_T_FORMAT
2297 "u > %" INT64_T_FORMAT "u = %d\n",
2298 CAST(unsigned long long, v),
2299 CAST(unsigned long long, l), matched);
2302 matched = CAST(int64_t, v) > CAST(int64_t, l);
2303 if ((ms->flags & MAGIC_DEBUG) != 0)
2304 (void) fprintf(stderr, "%" INT64_T_FORMAT
2305 "d > %" INT64_T_FORMAT "d = %d\n",
2307 CAST(long long, l), matched);
2312 if (m->flag & UNSIGNED) {
2314 if ((ms->flags & MAGIC_DEBUG) != 0)
2315 (void) fprintf(stderr, "%" INT64_T_FORMAT
2316 "u < %" INT64_T_FORMAT "u = %d\n",
2317 CAST(unsigned long long, v),
2318 CAST(unsigned long long, l), matched);
2321 matched = CAST(int64_t, v) < CAST(int64_t, l);
2322 if ((ms->flags & MAGIC_DEBUG) != 0)
2323 (void) fprintf(stderr, "%" INT64_T_FORMAT
2324 "d < %" INT64_T_FORMAT "d = %d\n",
2326 CAST(long long, l), matched);
2331 matched = (v & l) == l;
2332 if ((ms->flags & MAGIC_DEBUG) != 0)
2333 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2334 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2335 "x) = %d\n", CAST(unsigned long long, v),
2336 CAST(unsigned long long, l),
2337 CAST(unsigned long long, l),
2342 matched = (v & l) != l;
2343 if ((ms->flags & MAGIC_DEBUG) != 0)
2344 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2345 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2346 "x) = %d\n", CAST(unsigned long long, v),
2347 CAST(unsigned long long, l),
2348 CAST(unsigned long long, l), matched);
2352 file_magerror(ms, "cannot happen: invalid relation `%c'",
2361 handle_annotation(struct magic_set *ms, struct magic *m, int firstline)
2363 if ((ms->flags & MAGIC_APPLE) && m->apple[0]) {
2364 if (print_sep(ms, firstline) == -1)
2366 if (file_printf(ms, "%.8s", m->apple) == -1)
2370 if ((ms->flags & MAGIC_EXTENSION) && m->ext[0]) {
2371 if (print_sep(ms, firstline) == -1)
2373 if (file_printf(ms, "%s", m->ext) == -1)
2377 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2380 if (print_sep(ms, firstline) == -1)
2382 if (varexpand(ms, buf, sizeof(buf), m->mimetype) == -1)
2386 if (file_printf(ms, "%s", p) == -1)
2394 print_sep(struct magic_set *ms, int firstline)
2399 * we found another match
2400 * put a newline and '-' to do some simple formatting
2402 return file_separator(ms);