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
41 FILE_RCSID("@(#)$File: softmagic.c,v 1.99 2007/05/08 14:44:18 christos Exp $")
44 private int match(struct magic_set *, struct magic *, uint32_t,
45 const unsigned char *, size_t);
46 private int mget(struct magic_set *, const unsigned char *,
47 struct magic *, size_t, unsigned int);
48 private int magiccheck(struct magic_set *, struct magic *);
49 private int32_t mprint(struct magic_set *, struct magic *);
50 private void mdebug(uint32_t, const char *, size_t);
51 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
52 const unsigned char *, uint32_t, size_t, size_t);
53 private int mconvert(struct magic_set *, struct magic *);
54 private int print_sep(struct magic_set *, int);
55 private void cvt_8(union VALUETYPE *, const struct magic *);
56 private void cvt_16(union VALUETYPE *, const struct magic *);
57 private void cvt_32(union VALUETYPE *, const struct magic *);
58 private void cvt_64(union VALUETYPE *, const struct magic *);
61 * softmagic - lookup one file in parsed, in-memory copy of database
62 * Passed the name and FILE * of one file to be typed.
64 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
66 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
70 for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
71 if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes)) != 0)
78 * Go through the whole list, stopping if you find a match. Process all
79 * the continuations of that match before returning.
81 * We support multi-level continuations:
83 * At any time when processing a successful top-level match, there is a
84 * current continuation level; it represents the level of the last
85 * successfully matched continuation.
87 * Continuations above that level are skipped as, if we see one, it
88 * means that the continuation that controls them - i.e, the
89 * lower-level continuation preceding them - failed to match.
91 * Continuations below that level are processed as, if we see one,
92 * it means we've finished processing or skipping higher-level
93 * continuations under the control of a successful or unsuccessful
94 * lower-level continuation, and are now seeing the next lower-level
95 * continuation and should process it. The current continuation
96 * level reverts to the level of the one we're seeing.
98 * Continuations at the current level are processed as, if we see
99 * one, there's no lower-level continuation that may have failed.
101 * If a continuation matches, we bump the current continuation level
102 * so that higher-level continuations are processed.
105 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
106 const unsigned char *s, size_t nbytes)
108 uint32_t magindex = 0;
109 unsigned int cont_level = 0;
110 int need_separator = 0;
111 int returnval = 0; /* if a match is found it is set to 1*/
112 int firstline = 1; /* a flag to print X\n X\n- X */
113 int printed_something = 0;
115 if (file_check_mem(ms, cont_level) == -1)
118 for (magindex = 0; magindex < nmagic; magindex++) {
121 ms->offset = magic[magindex].offset;
122 ms->line = magic[magindex].lineno;
124 /* if main entry matches, print it... */
125 flush = !mget(ms, s, &magic[magindex], nbytes, cont_level);
127 if (magic[magindex].reln == '!')
130 switch (magiccheck(ms, &magic[magindex])) {
142 * main entry didn't match,
143 * flush its continuations
145 while (magindex < nmagic - 1 &&
146 magic[magindex + 1].cont_level != 0)
152 * If we are going to print something, we'll need to print
153 * a blank before we print something else.
155 if (magic[magindex].desc[0]) {
157 printed_something = 1;
158 if (print_sep(ms, firstline) == -1)
162 if ((ms->c.li[cont_level].off = mprint(ms, &magic[magindex]))
166 /* and any continuations that match */
167 if (file_check_mem(ms, ++cont_level) == -1)
170 while (magic[magindex+1].cont_level != 0 &&
171 ++magindex < nmagic) {
172 ms->line = magic[magindex].lineno; /* for messages */
174 if (cont_level < magic[magindex].cont_level)
176 if (cont_level > magic[magindex].cont_level) {
178 * We're at the end of the level
179 * "cont_level" continuations.
181 cont_level = magic[magindex].cont_level;
183 ms->offset = magic[magindex].offset;
184 if (magic[magindex].flag & OFFADD) {
186 ms->c.li[cont_level - 1].off;
189 #ifdef ENABLE_CONDITIONALS
190 if (magic[magindex].cond == COND_ELSE ||
191 magic[magindex].cond == COND_ELIF) {
192 if (ms->c.li[cont_level].last_match == 1)
196 flush = !mget(ms, s, &magic[magindex], nbytes,
198 if (flush && magic[magindex].reln != '!')
201 switch (flush ? 1 : magiccheck(ms, &magic[magindex])) {
205 #ifdef ENABLE_CONDITIONALS
206 ms->c.li[cont_level].last_match = 0;
210 #ifdef ENABLE_CONDITIONALS
211 ms->c.li[cont_level].last_match = 1;
213 if (magic[magindex].type != FILE_DEFAULT)
214 ms->c.li[cont_level].got_match = 1;
215 else if (ms->c.li[cont_level].got_match) {
216 ms->c.li[cont_level].got_match = 0;
220 * If we are going to print something,
221 * make sure that we have a separator first.
223 if (magic[magindex].desc[0]) {
224 printed_something = 1;
225 if (print_sep(ms, firstline) == -1)
229 * This continuation matched. Print
230 * its message, with a blank before it
231 * if the previous item printed and
232 * this item isn't empty.
234 /* space if previous printed */
236 && (magic[magindex].nospflag == 0)
237 && (magic[magindex].desc[0] != '\0')) {
238 if (file_printf(ms, " ") == -1)
242 if ((ms->c.li[cont_level].off = mprint(ms, &magic[magindex])) == -1)
244 if (magic[magindex].desc[0])
248 * If we see any continuations
252 if (file_check_mem(ms, ++cont_level) == -1)
258 if (printed_something)
260 if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
261 return 1; /* don't keep searching */
264 return returnval; /* This is hit if -k is set or there is no match */
268 check_fmt(struct magic_set *ms, struct magic *m)
273 if (strchr(m->desc, '%') == NULL)
276 rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
279 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
280 file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
283 rc = regexec(&rx, m->desc, 0, 0, 0);
290 char * strndup(const char *, size_t);
293 strndup(const char *str, size_t n)
301 if (!(copy = malloc(len + 1)))
303 (void) memcpy(copy, str, len + 1);
307 #endif /* HAVE_STRNDUP */
310 mprint(struct magic_set *ms, struct magic *m)
315 union VALUETYPE *p = &ms->ms_value;
319 v = file_signextend(ms, m, (uint64_t)p->b);
320 switch (check_fmt(ms, m)) {
324 if (snprintf(buf, sizeof(buf), "%c",
325 (unsigned char)v) < 0)
327 if (file_printf(ms, m->desc, buf) == -1)
331 if (file_printf(ms, m->desc, (unsigned char) v) == -1)
335 t = ms->offset + sizeof(char);
341 v = file_signextend(ms, m, (uint64_t)p->h);
342 switch (check_fmt(ms, m)) {
346 if (snprintf(buf, sizeof(buf), "%hu",
347 (unsigned short)v) < 0)
349 if (file_printf(ms, m->desc, buf) == -1)
353 if (file_printf(ms, m->desc, (unsigned short) v) == -1)
357 t = ms->offset + sizeof(short);
364 v = file_signextend(ms, m, (uint64_t)p->l);
365 switch (check_fmt(ms, m)) {
369 if (snprintf(buf, sizeof(buf), "%u", (uint32_t)v) < 0)
371 if (file_printf(ms, m->desc, buf) == -1)
375 if (file_printf(ms, m->desc, (uint32_t) v) == -1)
379 t = ms->offset + sizeof(int32_t);
385 v = file_signextend(ms, m, p->q);
386 if (file_printf(ms, m->desc, (uint64_t) v) == -1)
388 t = ms->offset + sizeof(int64_t);
393 case FILE_BESTRING16:
394 case FILE_LESTRING16:
395 if (m->reln == '=' || m->reln == '!') {
396 if (file_printf(ms, m->desc, m->value.s) == -1)
398 t = ms->offset + m->vallen;
401 if (*m->value.s == '\0') {
402 char *cp = strchr(p->s,'\n');
406 if (file_printf(ms, m->desc, p->s) == -1)
408 t = ms->offset + strlen(p->s);
416 if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
418 t = ms->offset + sizeof(time_t);
425 if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
427 t = ms->offset + sizeof(time_t);
433 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q, 1))
436 t = ms->offset + sizeof(uint64_t);
442 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q, 0))
445 t = ms->offset + sizeof(uint64_t);
452 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
454 file_oomem(ms, ms->search.rm_len);
457 rval = file_printf(ms, m->desc, cp);
463 if ((m->str_flags & REGEX_OFFSET_START))
464 t = ms->search.offset;
466 t = ms->search.offset + ms->search.rm_len;
471 if (file_printf(ms, m->desc, m->value.s) == -1)
473 if ((m->str_flags & REGEX_OFFSET_START))
474 t = ms->search.offset;
476 t = ms->search.offset + m->vallen;
480 if (file_printf(ms, m->desc, m->value.s) == -1)
486 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
493 #define DO_CVT(fld, cast) \
495 switch (m->mask_op & FILE_OPS_MASK) { \
497 p->fld &= cast m->num_mask; \
500 p->fld |= cast m->num_mask; \
503 p->fld ^= cast m->num_mask; \
506 p->fld += cast m->num_mask; \
509 p->fld -= cast m->num_mask; \
511 case FILE_OPMULTIPLY: \
512 p->fld *= cast m->num_mask; \
514 case FILE_OPDIVIDE: \
515 p->fld /= cast m->num_mask; \
517 case FILE_OPMODULO: \
518 p->fld %= cast m->num_mask; \
521 if (m->mask_op & FILE_OPINVERSE) \
525 cvt_8(union VALUETYPE *p, const struct magic *m)
527 DO_CVT(b, (uint8_t));
531 cvt_16(union VALUETYPE *p, const struct magic *m)
533 DO_CVT(h, (uint16_t));
537 cvt_32(union VALUETYPE *p, const struct magic *m)
539 DO_CVT(l, (uint32_t));
543 cvt_64(union VALUETYPE *p, const struct magic *m)
545 DO_CVT(q, (uint64_t));
549 * Convert the byte order of the data we are looking at
550 * While we're here, let's apply the mask operation
551 * (unless you have a better idea)
554 mconvert(struct magic_set *ms, struct magic *m)
556 union VALUETYPE *p = &ms->ms_value;
576 case FILE_BESTRING16:
577 case FILE_LESTRING16: {
580 /* Null terminate and eat *trailing* return */
581 p->s[sizeof(p->s) - 1] = '\0';
583 if (len-- && p->s[len] == '\n')
588 char *ptr1 = p->s, *ptr2 = ptr1 + 1;
590 if (len >= sizeof(p->s))
591 len = sizeof(p->s) - 1;
596 if (len-- && p->s[len] == '\n')
601 p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
608 ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
615 (((int64_t)p->hq[0]<<56)|((int64_t)p->hq[1]<<48)|
616 ((int64_t)p->hq[2]<<40)|((int64_t)p->hq[3]<<32)|
617 (p->hq[4]<<24)|(p->hq[5]<<16)|(p->hq[6]<<8)|(p->hq[7]));
621 p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
628 ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
635 (((int64_t)p->hq[7]<<56)|((int64_t)p->hq[6]<<48)|
636 ((int64_t)p->hq[5]<<40)|((int64_t)p->hq[4]<<32)|
637 (p->hq[3]<<24)|(p->hq[2]<<16)|(p->hq[1]<<8)|(p->hq[0]));
644 ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
652 file_magerror(ms, "invalid type %d in mconvert()", m->type);
659 mdebug(uint32_t offset, const char *str, size_t len)
661 (void) fprintf(stderr, "mget @%d: ", offset);
662 file_showstr(stderr, str, len);
663 (void) fputc('\n', stderr);
664 (void) fputc('\n', stderr);
668 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
669 const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
672 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
673 * anything, but setup pointers into the source
678 ms->search.s = (const char *)s + offset;
679 ms->search.s_len = nbytes - offset;
684 * offset is interpreted as last line to search,
685 * (starting at 1), not as bytes-from start-of-file
689 const char *last; /* end of search region */
690 const char *buf; /* start of search region */
694 ms->search.s_len = 0;
698 buf = (const char *)s + offset;
699 last = (const char *)s + nbytes;
700 /* mget() guarantees buf <= last */
701 for (lines = linecnt, b = buf;
702 lines && ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
705 if (b[0] == '\r' && b[1] == '\n')
709 last = (const char *)s + nbytes;
712 ms->search.s_len = last - buf;
713 ms->search.offset = offset;
714 ms->search.rm_len = 0;
717 case FILE_BESTRING16:
718 case FILE_LESTRING16: {
719 const unsigned char *src = s + offset;
720 const unsigned char *esrc = s + nbytes;
722 char *edst = &p->s[sizeof(p->s) - 1];
724 if (type == FILE_BESTRING16)
727 /* check for pointer overflow */
729 file_magerror(ms, "invalid offset %zu in mcopy()",
733 for (/*EMPTY*/; src < esrc; src++, dst++) {
744 case FILE_STRING: /* XXX - these two should not need */
745 case FILE_PSTRING: /* to copy anything, but do anyway. */
751 if (offset >= nbytes) {
752 (void)memset(p, '\0', sizeof(*p));
755 if (nbytes - offset < sizeof(*p))
756 nbytes = nbytes - offset;
760 (void)memcpy(p, s + offset, nbytes);
763 * the usefulness of padding with zeroes eludes me, it
764 * might even cause problems
766 if (nbytes < sizeof(*p))
767 (void)memset(((char *)(void *)p) + nbytes, '\0',
768 sizeof(*p) - nbytes);
773 mget(struct magic_set *ms, const unsigned char *s,
774 struct magic *m, size_t nbytes, unsigned int cont_level)
776 uint32_t offset = ms->offset;
777 uint32_t count = m->str_count;
778 union VALUETYPE *p = &ms->ms_value;
780 if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
783 if ((ms->flags & MAGIC_DEBUG) != 0) {
784 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
788 if (m->flag & INDIR) {
789 int off = m->in_offset;
790 if (m->in_op & FILE_OPINDIRECT) {
791 const union VALUETYPE *q =
792 ((const void *)(s + offset + off));
793 switch (m->in_type) {
801 off = (short)((q->hs[0]<<8)|(q->hs[1]));
804 off = (short)((q->hs[1]<<8)|(q->hs[0]));
810 off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
811 (q->hl[2]<<8)|(q->hl[3]));
814 off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
815 (q->hl[1]<<8)|(q->hl[0]));
818 off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
819 (q->hl[3]<<8)|(q->hl[2]));
823 switch (m->in_type) {
825 if (nbytes < (offset + 1))
828 switch (m->in_op & FILE_OPS_MASK) {
844 case FILE_OPMULTIPLY:
856 if (m->in_op & FILE_OPINVERSE)
860 if (nbytes < (offset + 2))
863 switch (m->in_op & FILE_OPS_MASK) {
865 offset = (short)((p->hs[0]<<8)|
870 offset = (short)((p->hs[0]<<8)|
875 offset = (short)((p->hs[0]<<8)|
880 offset = (short)((p->hs[0]<<8)|
885 offset = (short)((p->hs[0]<<8)|
889 case FILE_OPMULTIPLY:
890 offset = (short)((p->hs[0]<<8)|
895 offset = (short)((p->hs[0]<<8)|
900 offset = (short)((p->hs[0]<<8)|
906 offset = (short)((p->hs[0]<<8)|
908 if (m->in_op & FILE_OPINVERSE)
912 if (nbytes < (offset + 2))
915 switch (m->in_op & FILE_OPS_MASK) {
917 offset = (short)((p->hs[1]<<8)|
922 offset = (short)((p->hs[1]<<8)|
927 offset = (short)((p->hs[1]<<8)|
932 offset = (short)((p->hs[1]<<8)|
937 offset = (short)((p->hs[1]<<8)|
941 case FILE_OPMULTIPLY:
942 offset = (short)((p->hs[1]<<8)|
947 offset = (short)((p->hs[1]<<8)|
952 offset = (short)((p->hs[1]<<8)|
958 offset = (short)((p->hs[1]<<8)|
960 if (m->in_op & FILE_OPINVERSE)
964 if (nbytes < (offset + 2))
967 switch (m->in_op & FILE_OPS_MASK) {
983 case FILE_OPMULTIPLY:
996 if (m->in_op & FILE_OPINVERSE)
1000 if (nbytes < (offset + 4))
1003 switch (m->in_op & FILE_OPS_MASK) {
1005 offset = (int32_t)((p->hl[0]<<24)|
1012 offset = (int32_t)((p->hl[0]<<24)|
1019 offset = (int32_t)((p->hl[0]<<24)|
1026 offset = (int32_t)((p->hl[0]<<24)|
1033 offset = (int32_t)((p->hl[0]<<24)|
1039 case FILE_OPMULTIPLY:
1040 offset = (int32_t)((p->hl[0]<<24)|
1047 offset = (int32_t)((p->hl[0]<<24)|
1054 offset = (int32_t)((p->hl[0]<<24)|
1062 offset = (int32_t)((p->hl[0]<<24)|
1066 if (m->in_op & FILE_OPINVERSE)
1070 if (nbytes < (offset + 4))
1073 switch (m->in_op & FILE_OPS_MASK) {
1075 offset = (int32_t)((p->hl[3]<<24)|
1082 offset = (int32_t)((p->hl[3]<<24)|
1089 offset = (int32_t)((p->hl[3]<<24)|
1096 offset = (int32_t)((p->hl[3]<<24)|
1103 offset = (int32_t)((p->hl[3]<<24)|
1109 case FILE_OPMULTIPLY:
1110 offset = (int32_t)((p->hl[3]<<24)|
1117 offset = (int32_t)((p->hl[3]<<24)|
1124 offset = (int32_t)((p->hl[3]<<24)|
1132 offset = (int32_t)((p->hl[3]<<24)|
1136 if (m->in_op & FILE_OPINVERSE)
1140 if (nbytes < (offset + 4))
1143 switch (m->in_op & FILE_OPS_MASK) {
1145 offset = (int32_t)((p->hl[1]<<24)|
1152 offset = (int32_t)((p->hl[1]<<24)|
1159 offset = (int32_t)((p->hl[1]<<24)|
1166 offset = (int32_t)((p->hl[1]<<24)|
1173 offset = (int32_t)((p->hl[1]<<24)|
1179 case FILE_OPMULTIPLY:
1180 offset = (int32_t)((p->hl[1]<<24)|
1187 offset = (int32_t)((p->hl[1]<<24)|
1194 offset = (int32_t)((p->hl[1]<<24)|
1202 offset = (int32_t)((p->hl[1]<<24)|
1206 if (m->in_op & FILE_OPINVERSE)
1210 if (nbytes < (offset + 4))
1213 switch (m->in_op & FILE_OPS_MASK) {
1215 offset = p->l & off;
1218 offset = p->l | off;
1221 offset = p->l ^ off;
1224 offset = p->l + off;
1227 offset = p->l - off;
1229 case FILE_OPMULTIPLY:
1230 offset = p->l * off;
1233 offset = p->l / off;
1236 offset = p->l % off;
1238 /* case TOOMANYSWITCHBLOCKS:
1239 * ugh = p->eye % m->strain;
1242 * off = p->tab & m->in_gest;
1248 if (m->in_op & FILE_OPINVERSE)
1253 if (m->flag & INDIROFFADD)
1254 offset += ms->c.li[cont_level-1].off;
1255 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
1257 ms->offset = offset;
1259 if ((ms->flags & MAGIC_DEBUG) != 0) {
1260 mdebug(offset, (char *)(void *)p,
1261 sizeof(union VALUETYPE));
1266 /* Verify we have enough data to match magic type */
1269 if (nbytes < (offset + 1)) /* should alway be true */
1276 if (nbytes < (offset + 2))
1292 if (nbytes < (offset + 4))
1299 if (nbytes < (offset + m->vallen))
1304 if (nbytes < offset)
1308 case FILE_DEFAULT: /* nothing to check */
1312 if (!mconvert(ms, m))
1318 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1321 * Convert the source args to unsigned here so that (1) the
1322 * compare will be unsigned as it is in strncmp() and (2) so
1323 * the ctype functions will work correctly without extra
1326 const unsigned char *a = (const unsigned char *)s1;
1327 const unsigned char *b = (const unsigned char *)s2;
1331 * What we want here is:
1332 * v = strncmp(m->value.s, p->s, m->vallen);
1333 * but ignoring any nulls. bcmp doesn't give -/+/0
1334 * and isn't universally available anyway.
1337 if (0L == flags) { /* normal string: do it fast */
1339 if ((v = *b++ - *a++) != '\0')
1342 else { /* combine the others */
1344 if ((flags & STRING_IGNORE_LOWERCASE) &&
1346 if ((v = tolower(*b++) - *a++) != '\0')
1349 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1351 if ((v = toupper(*b++) - *a++) != '\0')
1354 else if ((flags & STRING_COMPACT_BLANK) &&
1357 if (isspace(*b++)) {
1366 else if ((flags & STRING_COMPACT_OPTIONAL_BLANK) &&
1373 if ((v = *b++ - *a++) != '\0')
1382 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1385 * XXX - The 16-bit string compare probably needs to be done
1386 * differently, especially if the flags are to be supported.
1387 * At the moment, I am unsure.
1390 return file_strncmp(a, b, len, flags);
1394 magiccheck(struct magic_set *ms, struct magic *m)
1396 uint64_t l = m->value.q;
1399 union VALUETYPE *p = &ms->ms_value;
1447 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1450 case FILE_BESTRING16:
1451 case FILE_LESTRING16:
1453 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1456 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1460 if (ms->search.s == NULL)
1463 slen = MIN(m->vallen, sizeof(m->value.s));
1466 ms->search.offset = m->offset;
1468 for (idx = 0; m->str_count == 0 || idx < m->str_count; idx++) {
1469 if (slen + idx > ms->search.s_len)
1472 v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
1473 if (v == 0) { /* found match */
1474 ms->search.offset = m->offset + idx;
1485 if (ms->search.s == NULL)
1489 rc = regcomp(&rx, m->value.s,
1490 REG_EXTENDED|REG_NEWLINE|
1491 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
1493 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1494 file_magerror(ms, "regex error %d, (%s)",
1499 regmatch_t pmatch[1];
1500 #ifndef REG_STARTEND
1501 #define REG_STARTEND 0
1502 size_t l = ms->search.s_len - 1;
1503 char c = ms->search.s[l];
1504 ((char *)(intptr_t)ms->search.s)[l] = '\0';
1506 pmatch[0].rm_so = 0;
1507 pmatch[0].rm_eo = ms->search.s_len;
1509 rc = regexec(&rx, (const char *)ms->search.s,
1510 1, pmatch, REG_STARTEND);
1511 #if REG_STARTEND == 0
1512 ((char *)(intptr_t)ms->search.s)[l] = c;
1516 ms->search.s += (int)pmatch[0].rm_so;
1517 ms->search.offset += (size_t)pmatch[0].rm_so;
1519 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
1528 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1529 file_magerror(ms, "regexec error %d, (%s)",
1536 if (v == (uint64_t)-1)
1541 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
1545 v = file_signextend(ms, m, v);
1549 if ((ms->flags & MAGIC_DEBUG) != 0)
1550 (void) fprintf(stderr, "%llu == *any* = 1\n",
1551 (unsigned long long)v);
1557 if ((ms->flags & MAGIC_DEBUG) != 0)
1558 (void) fprintf(stderr, "%llu != %llu = %d\n",
1559 (unsigned long long)v, (unsigned long long)l,
1565 if ((ms->flags & MAGIC_DEBUG) != 0)
1566 (void) fprintf(stderr, "%llu == %llu = %d\n",
1567 (unsigned long long)v, (unsigned long long)l,
1572 if (m->flag & UNSIGNED) {
1574 if ((ms->flags & MAGIC_DEBUG) != 0)
1575 (void) fprintf(stderr, "%llu > %llu = %d\n",
1576 (unsigned long long)v,
1577 (unsigned long long)l, matched);
1580 matched = (int64_t) v > (int64_t) l;
1581 if ((ms->flags & MAGIC_DEBUG) != 0)
1582 (void) fprintf(stderr, "%lld > %lld = %d\n",
1583 (long long)v, (long long)l, matched);
1588 if (m->flag & UNSIGNED) {
1590 if ((ms->flags & MAGIC_DEBUG) != 0)
1591 (void) fprintf(stderr, "%llu < %llu = %d\n",
1592 (unsigned long long)v,
1593 (unsigned long long)l, matched);
1596 matched = (int64_t) v < (int64_t) l;
1597 if ((ms->flags & MAGIC_DEBUG) != 0)
1598 (void) fprintf(stderr, "%lld < %lld = %d\n",
1599 (long long)v, (long long)l, matched);
1604 matched = (v & l) == l;
1605 if ((ms->flags & MAGIC_DEBUG) != 0)
1606 (void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n",
1607 (unsigned long long)v, (unsigned long long)l,
1608 (unsigned long long)l, matched);
1612 matched = (v & l) != l;
1613 if ((ms->flags & MAGIC_DEBUG) != 0)
1614 (void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n",
1615 (unsigned long long)v, (unsigned long long)l,
1616 (unsigned long long)l, matched);
1621 file_magerror(ms, "cannot happen: invalid relation `%c'",
1630 print_sep(struct magic_set *ms, int firstline)
1635 * we found another match
1636 * put a newline and '-' to do some simple formatting
1638 return file_printf(ms, "\n- ");