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.191 2014/06/04 17:36:34 christos Exp $")
44 #if defined(HAVE_LOCALE_H)
49 private int match(struct magic_set *, struct magic *, uint32_t,
50 const unsigned char *, size_t, size_t, int, int, int, int, int *, int *,
52 private int mget(struct magic_set *, const unsigned char *,
53 struct magic *, size_t, size_t, unsigned int, int, int, int, int, int *,
55 private int magiccheck(struct magic_set *, struct magic *);
56 private int32_t mprint(struct magic_set *, struct magic *);
57 private int32_t moffset(struct magic_set *, struct magic *);
58 private void mdebug(uint32_t, const char *, size_t);
59 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
60 const unsigned char *, uint32_t, size_t, struct magic *);
61 private int mconvert(struct magic_set *, struct magic *, int);
62 private int print_sep(struct magic_set *, int);
63 private int handle_annotation(struct magic_set *, struct magic *);
64 private void cvt_8(union VALUETYPE *, const struct magic *);
65 private void cvt_16(union VALUETYPE *, const struct magic *);
66 private void cvt_32(union VALUETYPE *, const struct magic *);
67 private void cvt_64(union VALUETYPE *, const struct magic *);
69 #define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
71 #define MAX_RECURSION_LEVEL 10
74 * softmagic - lookup one file in parsed, in-memory copy of database
75 * Passed the name and FILE * of one file to be typed.
77 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
79 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
80 size_t level, int mode, int text)
83 int rv, printed_something = 0, need_separator = 0;
84 for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
85 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode,
86 text, 0, level, &printed_something, &need_separator,
95 #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
97 private const char * __attribute__((__format_arg__(3)))
98 file_fmtcheck(struct magic_set *ms, const struct magic *m, const char *def,
99 const char *file, size_t line)
101 const char *ptr = fmtcheck(m->desc, def);
104 "%s, %zu: format `%s' does not match with `%s'",
105 file, line, m->desc, def);
109 #define F(a, b, c) fmtcheck((b)->desc, (c))
113 * Go through the whole list, stopping if you find a match. Process all
114 * the continuations of that match before returning.
116 * We support multi-level continuations:
118 * At any time when processing a successful top-level match, there is a
119 * current continuation level; it represents the level of the last
120 * successfully matched continuation.
122 * Continuations above that level are skipped as, if we see one, it
123 * means that the continuation that controls them - i.e, the
124 * lower-level continuation preceding them - failed to match.
126 * Continuations below that level are processed as, if we see one,
127 * it means we've finished processing or skipping higher-level
128 * continuations under the control of a successful or unsuccessful
129 * lower-level continuation, and are now seeing the next lower-level
130 * continuation and should process it. The current continuation
131 * level reverts to the level of the one we're seeing.
133 * Continuations at the current level are processed as, if we see
134 * one, there's no lower-level continuation that may have failed.
136 * If a continuation matches, we bump the current continuation level
137 * so that higher-level continuations are processed.
140 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
141 const unsigned char *s, size_t nbytes, size_t offset, int mode, int text,
142 int flip, int recursion_level, int *printed_something, int *need_separator,
145 uint32_t magindex = 0;
146 unsigned int cont_level = 0;
147 int returnvalv = 0, e; /* if a match is found it is set to 1*/
148 int firstline = 1; /* a flag to print X\n X\n- X */
149 int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
151 if (returnval == NULL)
152 returnval = &returnvalv;
154 if (file_check_mem(ms, cont_level) == -1)
157 for (magindex = 0; magindex < nmagic; magindex++) {
159 struct magic *m = &magic[magindex];
161 if (m->type != FILE_NAME)
162 if ((IS_STRING(m->type) &&
163 #define FLT (STRING_BINTEST | STRING_TEXTTEST)
164 ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
165 (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
166 (m->flag & mode) != mode) {
168 while (magindex + 1 < nmagic &&
169 magic[magindex + 1].cont_level != 0 &&
172 continue; /* Skip to next top-level test*/
175 ms->offset = m->offset;
176 ms->line = m->lineno;
178 /* if main entry matches, print it... */
179 switch (mget(ms, s, m, nbytes, offset, cont_level, mode, text,
180 flip, recursion_level + 1, printed_something,
181 need_separator, returnval)) {
185 flush = m->reln != '!';
188 if (m->type == FILE_INDIRECT)
191 switch (magiccheck(ms, m)) {
205 * main entry didn't match,
206 * flush its continuations
208 while (magindex < nmagic - 1 &&
209 magic[magindex + 1].cont_level != 0)
214 if ((e = handle_annotation(ms, m)) != 0) {
216 *printed_something = 1;
221 * If we are going to print something, we'll need to print
222 * a blank before we print something else.
226 *printed_something = 1;
227 if (print_sep(ms, firstline) == -1)
232 if (print && mprint(ms, m) == -1)
235 ms->c.li[cont_level].off = moffset(ms, m);
237 /* and any continuations that match */
238 if (file_check_mem(ms, ++cont_level) == -1)
241 while (++magindex < nmagic &&
242 magic[magindex].cont_level != 0) {
243 m = &magic[magindex];
244 ms->line = m->lineno; /* for messages */
246 if (cont_level < m->cont_level)
248 if (cont_level > m->cont_level) {
250 * We're at the end of the level
251 * "cont_level" continuations.
253 cont_level = m->cont_level;
255 ms->offset = m->offset;
256 if (m->flag & OFFADD) {
258 ms->c.li[cont_level - 1].off;
261 #ifdef ENABLE_CONDITIONALS
262 if (m->cond == COND_ELSE ||
263 m->cond == COND_ELIF) {
264 if (ms->c.li[cont_level].last_match == 1)
268 switch (mget(ms, s, m, nbytes, offset, cont_level, mode,
269 text, flip, recursion_level + 1, printed_something,
270 need_separator, returnval)) {
279 if (m->type == FILE_INDIRECT)
285 switch (flush ? 1 : magiccheck(ms, m)) {
289 #ifdef ENABLE_CONDITIONALS
290 ms->c.li[cont_level].last_match = 0;
294 #ifdef ENABLE_CONDITIONALS
295 ms->c.li[cont_level].last_match = 1;
297 if (m->type == FILE_CLEAR)
298 ms->c.li[cont_level].got_match = 0;
299 else if (ms->c.li[cont_level].got_match) {
300 if (m->type == FILE_DEFAULT)
303 ms->c.li[cont_level].got_match = 1;
304 if ((e = handle_annotation(ms, m)) != 0) {
306 *printed_something = 1;
311 * If we are going to print something,
312 * make sure that we have a separator first.
315 if (!*printed_something) {
316 *printed_something = 1;
317 if (print_sep(ms, firstline)
323 * This continuation matched. Print
324 * its message, with a blank before it
325 * if the previous item printed and
326 * this item isn't empty.
328 /* space if previous printed */
330 && ((m->flag & NOSPACE) == 0)
333 file_printf(ms, " ") == -1)
337 if (print && mprint(ms, m) == -1)
340 ms->c.li[cont_level].off = moffset(ms, m);
346 * If we see any continuations
350 if (file_check_mem(ms, ++cont_level) == -1)
355 if (*printed_something) {
360 if ((ms->flags & MAGIC_CONTINUE) == 0 && *printed_something) {
361 return *returnval; /* don't keep searching */
364 return *returnval; /* This is hit if -k is set or there is no match */
368 check_fmt(struct magic_set *ms, struct magic *m)
373 if (strchr(m->desc, '%') == NULL)
376 rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
378 file_regerror(&rx, rc, ms);
380 rc = file_regexec(&rx, m->desc, 0, 0, 0);
388 char * strndup(const char *, size_t);
391 strndup(const char *str, size_t n)
396 for (len = 0; len < n && str[len]; len++)
398 if ((copy = malloc(len + 1)) == NULL)
400 (void)memcpy(copy, str, len);
404 #endif /* HAVE_STRNDUP */
407 mprint(struct magic_set *ms, struct magic *m)
413 char buf[128], tbuf[26];
414 union VALUETYPE *p = &ms->ms_value;
418 v = file_signextend(ms, m, (uint64_t)p->b);
419 switch (check_fmt(ms, m)) {
423 (void)snprintf(buf, sizeof(buf), "%d",
425 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
429 if (file_printf(ms, F(ms, m, "%d"),
430 (unsigned char) v) == -1)
434 t = ms->offset + sizeof(char);
440 v = file_signextend(ms, m, (uint64_t)p->h);
441 switch (check_fmt(ms, m)) {
445 (void)snprintf(buf, sizeof(buf), "%u",
447 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
451 if (file_printf(ms, F(ms, m, "%u"),
452 (unsigned short) v) == -1)
456 t = ms->offset + sizeof(short);
463 v = file_signextend(ms, m, (uint64_t)p->l);
464 switch (check_fmt(ms, m)) {
468 (void)snprintf(buf, sizeof(buf), "%u", (uint32_t) v);
469 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
473 if (file_printf(ms, F(ms, m, "%u"), (uint32_t) v) == -1)
477 t = ms->offset + sizeof(int32_t);
483 v = file_signextend(ms, m, p->q);
484 switch (check_fmt(ms, m)) {
488 (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
489 (unsigned long long)v);
490 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
494 if (file_printf(ms, F(ms, m, "%" INT64_T_FORMAT "u"),
495 (unsigned long long) v) == -1)
499 t = ms->offset + sizeof(int64_t);
504 case FILE_BESTRING16:
505 case FILE_LESTRING16:
506 if (m->reln == '=' || m->reln == '!') {
507 if (file_printf(ms, F(ms, m, "%s"), m->value.s) == -1)
509 t = ms->offset + m->vallen;
514 /* compute t before we mangle the string? */
515 t = ms->offset + strlen(str);
517 if (*m->value.s == '\0')
518 str[strcspn(str, "\n")] = '\0';
520 if (m->str_flags & STRING_TRIM) {
522 while (isspace((unsigned char)*str))
528 while (isspace((unsigned char)*last))
533 if (file_printf(ms, F(ms, m, "%s"), str) == -1)
536 if (m->type == FILE_PSTRING)
537 t += file_pstring_length_size(m);
545 if (file_printf(ms, F(ms, m, "%s"),
546 file_fmttime(p->l + m->num_mask, FILE_T_LOCAL, tbuf)) == -1)
548 t = ms->offset + sizeof(uint32_t);
555 if (file_printf(ms, F(ms, m, "%s"),
556 file_fmttime(p->l + m->num_mask, 0, tbuf)) == -1)
558 t = ms->offset + sizeof(uint32_t);
564 if (file_printf(ms, F(ms, m, "%s"),
565 file_fmttime(p->q + m->num_mask, FILE_T_LOCAL, tbuf)) == -1)
567 t = ms->offset + sizeof(uint64_t);
573 if (file_printf(ms, F(ms, m, "%s"),
574 file_fmttime(p->q + m->num_mask, 0, tbuf)) == -1)
576 t = ms->offset + sizeof(uint64_t);
582 if (file_printf(ms, F(ms, m, "%s"),
583 file_fmttime(p->q + m->num_mask, FILE_T_WINDOWS, tbuf)) == -1)
585 t = ms->offset + sizeof(uint64_t);
592 switch (check_fmt(ms, m)) {
596 (void)snprintf(buf, sizeof(buf), "%g", vf);
597 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
601 if (file_printf(ms, F(ms, m, "%g"), vf) == -1)
605 t = ms->offset + sizeof(float);
612 switch (check_fmt(ms, m)) {
616 (void)snprintf(buf, sizeof(buf), "%g", vd);
617 if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
621 if (file_printf(ms, F(ms, m, "%g"), vd) == -1)
625 t = ms->offset + sizeof(double);
632 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
634 file_oomem(ms, ms->search.rm_len);
637 rval = file_printf(ms, F(ms, m, "%s"), cp);
643 if ((m->str_flags & REGEX_OFFSET_START))
644 t = ms->search.offset;
646 t = ms->search.offset + ms->search.rm_len;
651 if (file_printf(ms, F(ms, m, "%s"), m->value.s) == -1)
653 if ((m->str_flags & REGEX_OFFSET_START))
654 t = ms->search.offset;
656 t = ms->search.offset + m->vallen;
661 if (file_printf(ms, "%s", m->desc) == -1)
673 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
680 moffset(struct magic_set *ms, struct magic *m)
684 return CAST(int32_t, (ms->offset + sizeof(char)));
689 return CAST(int32_t, (ms->offset + sizeof(short)));
695 return CAST(int32_t, (ms->offset + sizeof(int32_t)));
700 return CAST(int32_t, (ms->offset + sizeof(int64_t)));
704 case FILE_BESTRING16:
705 case FILE_LESTRING16:
706 if (m->reln == '=' || m->reln == '!')
707 return ms->offset + m->vallen;
709 union VALUETYPE *p = &ms->ms_value;
712 if (*m->value.s == '\0')
713 p->s[strcspn(p->s, "\n")] = '\0';
714 t = CAST(uint32_t, (ms->offset + strlen(p->s)));
715 if (m->type == FILE_PSTRING)
716 t += (uint32_t)file_pstring_length_size(m);
724 return CAST(int32_t, (ms->offset + sizeof(uint32_t)));
730 return CAST(int32_t, (ms->offset + sizeof(uint32_t)));
735 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
740 return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
745 return CAST(int32_t, (ms->offset + sizeof(float)));
750 return CAST(int32_t, (ms->offset + sizeof(double)));
753 if ((m->str_flags & REGEX_OFFSET_START) != 0)
754 return CAST(int32_t, ms->search.offset);
756 return CAST(int32_t, (ms->search.offset +
760 if ((m->str_flags & REGEX_OFFSET_START) != 0)
761 return CAST(int32_t, ms->search.offset);
763 return CAST(int32_t, (ms->search.offset + m->vallen));
776 cvt_flip(int type, int flip)
794 return FILE_LEQLDATE;
796 return FILE_LEQWDATE;
810 return FILE_BEQLDATE;
812 return FILE_BEQWDATE;
818 return FILE_LEDOUBLE;
820 return FILE_BEDOUBLE;
825 #define DO_CVT(fld, cast) \
827 switch (m->mask_op & FILE_OPS_MASK) { \
829 p->fld &= cast m->num_mask; \
832 p->fld |= cast m->num_mask; \
835 p->fld ^= cast m->num_mask; \
838 p->fld += cast m->num_mask; \
841 p->fld -= cast m->num_mask; \
843 case FILE_OPMULTIPLY: \
844 p->fld *= cast m->num_mask; \
846 case FILE_OPDIVIDE: \
847 p->fld /= cast m->num_mask; \
849 case FILE_OPMODULO: \
850 p->fld %= cast m->num_mask; \
853 if (m->mask_op & FILE_OPINVERSE) \
857 cvt_8(union VALUETYPE *p, const struct magic *m)
859 DO_CVT(b, (uint8_t));
863 cvt_16(union VALUETYPE *p, const struct magic *m)
865 DO_CVT(h, (uint16_t));
869 cvt_32(union VALUETYPE *p, const struct magic *m)
871 DO_CVT(l, (uint32_t));
875 cvt_64(union VALUETYPE *p, const struct magic *m)
877 DO_CVT(q, (uint64_t));
880 #define DO_CVT2(fld, cast) \
882 switch (m->mask_op & FILE_OPS_MASK) { \
884 p->fld += cast m->num_mask; \
887 p->fld -= cast m->num_mask; \
889 case FILE_OPMULTIPLY: \
890 p->fld *= cast m->num_mask; \
892 case FILE_OPDIVIDE: \
893 p->fld /= cast m->num_mask; \
898 cvt_float(union VALUETYPE *p, const struct magic *m)
904 cvt_double(union VALUETYPE *p, const struct magic *m)
906 DO_CVT2(d, (double));
910 * Convert the byte order of the data we are looking at
911 * While we're here, let's apply the mask operation
912 * (unless you have a better idea)
915 mconvert(struct magic_set *ms, struct magic *m, int flip)
917 union VALUETYPE *p = &ms->ms_value;
920 switch (type = cvt_flip(m->type, flip)) {
939 case FILE_BESTRING16:
940 case FILE_LESTRING16: {
941 /* Null terminate and eat *trailing* return */
942 p->s[sizeof(p->s) - 1] = '\0';
946 size_t sz = file_pstring_length_size(m);
947 char *ptr1 = p->s, *ptr2 = ptr1 + sz;
948 size_t len = file_pstring_get_length(m, ptr1);
949 if (len >= sizeof(p->s)) {
951 * The size of the pascal string length (sz)
952 * is 1, 2, or 4. We need at least 1 byte for NUL
953 * termination, but we've already truncated the
954 * string by p->s, so we need to deduct sz.
956 len = sizeof(p->s) - sz;
964 p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
971 ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
972 if (type == FILE_BELONG)
980 (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
981 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
982 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
983 ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
984 if (type == FILE_BEQUAD)
988 p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
995 ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
996 if (type == FILE_LELONG)
1004 (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
1005 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
1006 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
1007 ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
1008 if (type == FILE_LEQUAD)
1015 ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
1016 if (type == FILE_MELONG)
1023 p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
1024 ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
1028 p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
1029 ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
1036 p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
1037 ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
1038 ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
1039 ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
1043 p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
1044 ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
1045 ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
1046 ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
1057 file_magerror(ms, "invalid type %d in mconvert()", m->type);
1064 mdebug(uint32_t offset, const char *str, size_t len)
1066 (void) fprintf(stderr, "mget/%zu @%d: ", len, offset);
1067 file_showstr(stderr, str, len);
1068 (void) fputc('\n', stderr);
1069 (void) fputc('\n', stderr);
1073 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
1074 const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
1077 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1078 * anything, but setup pointers into the source
1083 ms->search.s = RCAST(const char *, s) + offset;
1084 ms->search.s_len = nbytes - offset;
1085 ms->search.offset = offset;
1091 const char *last; /* end of search region */
1092 const char *buf; /* start of search region */
1094 size_t lines, linecnt, bytecnt;
1097 ms->search.s_len = 0;
1098 ms->search.s = NULL;
1102 if (m->str_flags & REGEX_LINE_COUNT) {
1103 linecnt = m->str_range;
1104 bytecnt = linecnt * 80;
1107 bytecnt = m->str_range;
1112 if (bytecnt > nbytes)
1115 buf = RCAST(const char *, s) + offset;
1116 end = last = RCAST(const char *, s) + bytecnt;
1117 /* mget() guarantees buf <= last */
1118 for (lines = linecnt, b = buf; lines && b < end &&
1119 ((b = CAST(const char *,
1120 memchr(c = b, '\n', CAST(size_t, (end - b)))))
1121 || (b = CAST(const char *,
1122 memchr(c, '\r', CAST(size_t, (end - c))))));
1125 if (b[0] == '\r' && b[1] == '\n')
1129 last = RCAST(const char *, s) + bytecnt;
1132 ms->search.s_len = last - buf;
1133 ms->search.offset = offset;
1134 ms->search.rm_len = 0;
1137 case FILE_BESTRING16:
1138 case FILE_LESTRING16: {
1139 const unsigned char *src = s + offset;
1140 const unsigned char *esrc = s + nbytes;
1142 char *edst = &p->s[sizeof(p->s) - 1];
1144 if (type == FILE_BESTRING16)
1147 /* check that offset is within range */
1148 if (offset >= nbytes)
1150 for (/*EMPTY*/; src < esrc; src += 2, dst++) {
1156 if (type == FILE_BESTRING16 ?
1157 *(src - 1) != '\0' :
1165 case FILE_STRING: /* XXX - these two should not need */
1166 case FILE_PSTRING: /* to copy anything, but do anyway. */
1172 if (offset >= nbytes) {
1173 (void)memset(p, '\0', sizeof(*p));
1176 if (nbytes - offset < sizeof(*p))
1177 nbytes = nbytes - offset;
1179 nbytes = sizeof(*p);
1181 (void)memcpy(p, s + offset, nbytes);
1184 * the usefulness of padding with zeroes eludes me, it
1185 * might even cause problems
1187 if (nbytes < sizeof(*p))
1188 (void)memset(((char *)(void *)p) + nbytes, '\0',
1189 sizeof(*p) - nbytes);
1194 mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
1195 size_t nbytes, size_t o, unsigned int cont_level, int mode, int text,
1196 int flip, int recursion_level, int *printed_something,
1197 int *need_separator, int *returnval)
1199 uint32_t offset = ms->offset;
1202 int rv, oneed_separator, in_type;
1204 union VALUETYPE *p = &ms->ms_value;
1207 if (recursion_level >= MAX_RECURSION_LEVEL) {
1208 file_error(ms, 0, "recursion nesting exceeded");
1212 if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
1213 (uint32_t)nbytes, m) == -1)
1216 if ((ms->flags & MAGIC_DEBUG) != 0) {
1217 fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%zu, "
1218 "nbytes=%zu)\n", m->type, m->flag, offset, o, nbytes);
1219 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1220 #ifndef COMPILE_ONLY
1225 if (m->flag & INDIR) {
1226 int off = m->in_offset;
1227 if (m->in_op & FILE_OPINDIRECT) {
1228 const union VALUETYPE *q = CAST(const union VALUETYPE *,
1229 ((const void *)(s + offset + off)));
1230 switch (cvt_flip(m->in_type, flip)) {
1238 off = (short)((q->hs[0]<<8)|(q->hs[1]));
1241 off = (short)((q->hs[1]<<8)|(q->hs[0]));
1248 off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
1249 (q->hl[2]<<8)|(q->hl[3]));
1253 off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
1254 (q->hl[1]<<8)|(q->hl[0]));
1257 off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
1258 (q->hl[3]<<8)|(q->hl[2]));
1261 if ((ms->flags & MAGIC_DEBUG) != 0)
1262 fprintf(stderr, "indirect offs=%u\n", off);
1264 switch (in_type = cvt_flip(m->in_type, flip)) {
1266 if (OFFSET_OOB(nbytes, offset, 1))
1269 switch (m->in_op & FILE_OPS_MASK) {
1271 offset = p->b & off;
1274 offset = p->b | off;
1277 offset = p->b ^ off;
1280 offset = p->b + off;
1283 offset = p->b - off;
1285 case FILE_OPMULTIPLY:
1286 offset = p->b * off;
1289 offset = p->b / off;
1292 offset = p->b % off;
1297 if (m->in_op & FILE_OPINVERSE)
1301 if (OFFSET_OOB(nbytes, offset, 2))
1303 lhs = (p->hs[0] << 8) | p->hs[1];
1305 switch (m->in_op & FILE_OPS_MASK) {
1321 case FILE_OPMULTIPLY:
1333 if (m->in_op & FILE_OPINVERSE)
1337 if (OFFSET_OOB(nbytes, offset, 2))
1339 lhs = (p->hs[1] << 8) | p->hs[0];
1341 switch (m->in_op & FILE_OPS_MASK) {
1357 case FILE_OPMULTIPLY:
1369 if (m->in_op & FILE_OPINVERSE)
1373 if (OFFSET_OOB(nbytes, offset, 2))
1376 switch (m->in_op & FILE_OPS_MASK) {
1378 offset = p->h & off;
1381 offset = p->h | off;
1384 offset = p->h ^ off;
1387 offset = p->h + off;
1390 offset = p->h - off;
1392 case FILE_OPMULTIPLY:
1393 offset = p->h * off;
1396 offset = p->h / off;
1399 offset = p->h % off;
1405 if (m->in_op & FILE_OPINVERSE)
1410 if (OFFSET_OOB(nbytes, offset, 4))
1412 lhs = (p->hl[0] << 24) | (p->hl[1] << 16) |
1413 (p->hl[2] << 8) | p->hl[3];
1415 switch (m->in_op & FILE_OPS_MASK) {
1431 case FILE_OPMULTIPLY:
1443 if (m->in_op & FILE_OPINVERSE)
1448 if (OFFSET_OOB(nbytes, offset, 4))
1450 lhs = (p->hl[3] << 24) | (p->hl[2] << 16) |
1451 (p->hl[1] << 8) | p->hl[0];
1453 switch (m->in_op & FILE_OPS_MASK) {
1469 case FILE_OPMULTIPLY:
1481 if (m->in_op & FILE_OPINVERSE)
1485 if (OFFSET_OOB(nbytes, offset, 4))
1487 lhs = (p->hl[1] << 24) | (p->hl[0] << 16) |
1488 (p->hl[3] << 8) | p->hl[2];
1490 switch (m->in_op & FILE_OPS_MASK) {
1506 case FILE_OPMULTIPLY:
1518 if (m->in_op & FILE_OPINVERSE)
1522 if (OFFSET_OOB(nbytes, offset, 4))
1525 switch (m->in_op & FILE_OPS_MASK) {
1527 offset = p->l & off;
1530 offset = p->l | off;
1533 offset = p->l ^ off;
1536 offset = p->l + off;
1539 offset = p->l - off;
1541 case FILE_OPMULTIPLY:
1542 offset = p->l * off;
1545 offset = p->l / off;
1548 offset = p->l % off;
1553 if (m->in_op & FILE_OPINVERSE)
1563 offset = ((((offset >> 0) & 0x7f) << 0) |
1564 (((offset >> 8) & 0x7f) << 7) |
1565 (((offset >> 16) & 0x7f) << 14) |
1566 (((offset >> 24) & 0x7f) << 21)) + 10;
1572 if (m->flag & INDIROFFADD) {
1573 offset += ms->c.li[cont_level-1].off;
1575 if ((ms->flags & MAGIC_DEBUG) != 0)
1577 "indirect *zero* offset\n");
1580 if ((ms->flags & MAGIC_DEBUG) != 0)
1581 fprintf(stderr, "indirect +offs=%u\n", offset);
1583 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
1585 ms->offset = offset;
1587 if ((ms->flags & MAGIC_DEBUG) != 0) {
1588 mdebug(offset, (char *)(void *)p,
1589 sizeof(union VALUETYPE));
1590 #ifndef COMPILE_ONLY
1596 /* Verify we have enough data to match magic type */
1599 if (OFFSET_OOB(nbytes, offset, 1))
1606 if (OFFSET_OOB(nbytes, offset, 2))
1625 if (OFFSET_OOB(nbytes, offset, 4))
1632 if (OFFSET_OOB(nbytes, offset, 8))
1639 if (OFFSET_OOB(nbytes, offset, m->vallen))
1644 if (nbytes < offset)
1652 if (nbytes < offset)
1655 if ((pb = file_push_buffer(ms)) == NULL)
1658 rv = file_softmagic(ms, s + offset, nbytes - offset,
1659 recursion_level, BINTEST, text);
1661 if ((ms->flags & MAGIC_DEBUG) != 0)
1662 fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1664 rbuf = file_pop_buffer(ms, pb);
1665 if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
1669 if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
1670 file_printf(ms, F(ms, m, "%u"), offset) == -1) {
1674 if (file_printf(ms, "%s", rbuf) == -1) {
1683 if (nbytes < offset)
1690 if (file_magicfind(ms, rbuf, &ml) == -1) {
1691 file_error(ms, 0, "cannot find entry `%s'", rbuf);
1695 oneed_separator = *need_separator;
1696 if (m->flag & NOSPACE)
1697 *need_separator = 0;
1698 rv = match(ms, ml.magic, ml.nmagic, s, nbytes, offset + o,
1699 mode, text, flip, recursion_level, printed_something,
1700 need_separator, returnval);
1702 *need_separator = oneed_separator;
1706 if (file_printf(ms, "%s", m->desc) == -1)
1709 case FILE_DEFAULT: /* nothing to check */
1714 if (!mconvert(ms, m, flip))
1720 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1723 * Convert the source args to unsigned here so that (1) the
1724 * compare will be unsigned as it is in strncmp() and (2) so
1725 * the ctype functions will work correctly without extra
1728 const unsigned char *a = (const unsigned char *)s1;
1729 const unsigned char *b = (const unsigned char *)s2;
1733 * What we want here is v = strncmp(s1, s2, len),
1734 * but ignoring any nulls.
1737 if (0L == flags) { /* normal string: do it fast */
1739 if ((v = *b++ - *a++) != '\0')
1742 else { /* combine the others */
1744 if ((flags & STRING_IGNORE_LOWERCASE) &&
1746 if ((v = tolower(*b++) - *a++) != '\0')
1749 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1751 if ((v = toupper(*b++) - *a++) != '\0')
1754 else if ((flags & STRING_COMPACT_WHITESPACE) &&
1757 if (isspace(*b++)) {
1767 else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1774 if ((v = *b++ - *a++) != '\0')
1783 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1786 * XXX - The 16-bit string compare probably needs to be done
1787 * differently, especially if the flags are to be supported.
1788 * At the moment, I am unsure.
1791 return file_strncmp(a, b, len, flags);
1795 magiccheck(struct magic_set *ms, struct magic *m)
1797 uint64_t l = m->value.q;
1802 union VALUETYPE *p = &ms->ms_value;
1872 file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1905 file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
1919 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1922 case FILE_BESTRING16:
1923 case FILE_LESTRING16:
1925 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1928 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1932 if (ms->search.s == NULL)
1935 slen = MIN(m->vallen, sizeof(m->value.s));
1939 for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
1940 if (slen + idx > ms->search.s_len)
1943 v = file_strncmp(m->value.s, ms->search.s + idx, slen,
1945 if (v == 0) { /* found match */
1946 ms->search.offset += idx;
1956 if (ms->search.s == NULL)
1960 rc = file_regcomp(&rx, m->value.s,
1961 REG_EXTENDED|REG_NEWLINE|
1962 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
1964 file_regerror(&rx, rc, ms);
1967 regmatch_t pmatch[1];
1968 size_t slen = ms->search.s_len;
1969 #ifndef REG_STARTEND
1970 #define REG_STARTEND 0
1974 c = ms->search.s[slen];
1975 ((char *)(intptr_t)ms->search.s)[slen] = '\0';
1977 pmatch[0].rm_so = 0;
1978 pmatch[0].rm_eo = slen;
1980 rc = file_regexec(&rx, (const char *)ms->search.s,
1981 1, pmatch, REG_STARTEND);
1982 #if REG_STARTEND == 0
1983 ((char *)(intptr_t)ms->search.s)[l] = c;
1987 ms->search.s += (int)pmatch[0].rm_so;
1988 ms->search.offset += (size_t)pmatch[0].rm_so;
1990 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
1999 file_regerror(&rx, rc, ms);
2005 if (v == (uint64_t)-1)
2014 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2018 v = file_signextend(ms, m, v);
2022 if ((ms->flags & MAGIC_DEBUG) != 0)
2023 (void) fprintf(stderr, "%" INT64_T_FORMAT
2024 "u == *any* = 1\n", (unsigned long long)v);
2030 if ((ms->flags & MAGIC_DEBUG) != 0)
2031 (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
2032 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2033 (unsigned long long)l, matched);
2038 if ((ms->flags & MAGIC_DEBUG) != 0)
2039 (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2040 INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2041 (unsigned long long)l, matched);
2045 if (m->flag & UNSIGNED) {
2047 if ((ms->flags & MAGIC_DEBUG) != 0)
2048 (void) fprintf(stderr, "%" INT64_T_FORMAT
2049 "u > %" INT64_T_FORMAT "u = %d\n",
2050 (unsigned long long)v,
2051 (unsigned long long)l, matched);
2054 matched = (int64_t) v > (int64_t) l;
2055 if ((ms->flags & MAGIC_DEBUG) != 0)
2056 (void) fprintf(stderr, "%" INT64_T_FORMAT
2057 "d > %" INT64_T_FORMAT "d = %d\n",
2058 (long long)v, (long long)l, matched);
2063 if (m->flag & UNSIGNED) {
2065 if ((ms->flags & MAGIC_DEBUG) != 0)
2066 (void) fprintf(stderr, "%" INT64_T_FORMAT
2067 "u < %" INT64_T_FORMAT "u = %d\n",
2068 (unsigned long long)v,
2069 (unsigned long long)l, matched);
2072 matched = (int64_t) v < (int64_t) l;
2073 if ((ms->flags & MAGIC_DEBUG) != 0)
2074 (void) fprintf(stderr, "%" INT64_T_FORMAT
2075 "d < %" INT64_T_FORMAT "d = %d\n",
2076 (long long)v, (long long)l, matched);
2081 matched = (v & l) == l;
2082 if ((ms->flags & MAGIC_DEBUG) != 0)
2083 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2084 INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2085 "x) = %d\n", (unsigned long long)v,
2086 (unsigned long long)l, (unsigned long long)l,
2091 matched = (v & l) != l;
2092 if ((ms->flags & MAGIC_DEBUG) != 0)
2093 (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2094 INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2095 "x) = %d\n", (unsigned long long)v,
2096 (unsigned long long)l, (unsigned long long)l,
2101 file_magerror(ms, "cannot happen: invalid relation `%c'",
2110 handle_annotation(struct magic_set *ms, struct magic *m)
2112 if (ms->flags & MAGIC_APPLE) {
2113 if (file_printf(ms, "%.8s", m->apple) == -1)
2117 if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2118 if (file_printf(ms, "%s", m->mimetype) == -1)
2126 print_sep(struct magic_set *ms, int firstline)
2128 if (ms->flags & MAGIC_MIME)
2133 * we found another match
2134 * put a newline and '-' to do some simple formatting
2136 return file_printf(ms, "\n- ");