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.91 2007/01/18 05:29:33 ljt 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)
77 #ifdef ENABLE_CONDITIONALS
82 file_check_mem(struct magic_set *ms, unsigned int level)
86 if (level >= ms->c.len) {
87 len = (ms->c.len += 20) * sizeof(*ms->c.li);
88 ms->c.li = (ms->c.li == NULL) ? malloc(len) :
89 realloc(ms->c.li, len);
90 if (ms->c.li == NULL) {
95 ms->c.li[level].got_match = 0;
96 #ifdef ENABLE_CONDITIONALS
97 ms->c.li[level].last_match = 0;
98 ms->c.li[level].last_cond = COND_NONE;
99 #endif /* ENABLE_CONDITIONALS */
104 * Go through the whole list, stopping if you find a match. Process all
105 * the continuations of that match before returning.
107 * We support multi-level continuations:
109 * At any time when processing a successful top-level match, there is a
110 * current continuation level; it represents the level of the last
111 * successfully matched continuation.
113 * Continuations above that level are skipped as, if we see one, it
114 * means that the continuation that controls them - i.e, the
115 * lower-level continuation preceding them - failed to match.
117 * Continuations below that level are processed as, if we see one,
118 * it means we've finished processing or skipping higher-level
119 * continuations under the control of a successful or unsuccessful
120 * lower-level continuation, and are now seeing the next lower-level
121 * continuation and should process it. The current continuation
122 * level reverts to the level of the one we're seeing.
124 * Continuations at the current level are processed as, if we see
125 * one, there's no lower-level continuation that may have failed.
127 * If a continuation matches, we bump the current continuation level
128 * so that higher-level continuations are processed.
131 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
132 const unsigned char *s, size_t nbytes)
134 uint32_t magindex = 0;
135 unsigned int cont_level = 0;
136 int need_separator = 0;
137 int returnval = 0; /* if a match is found it is set to 1*/
138 int firstline = 1; /* a flag to print X\n X\n- X */
139 int printed_something = 0;
141 if (file_check_mem(ms, cont_level) == -1)
144 for (magindex = 0; magindex < nmagic; magindex++) {
147 ms->offset = magic[magindex].offset;
148 ms->line = magic[magindex].lineno;
150 /* if main entry matches, print it... */
151 flush = !mget(ms, s, &magic[magindex], nbytes, cont_level);
153 if (magic[magindex].reln == '!')
156 switch (magiccheck(ms, &magic[magindex])) {
168 * main entry didn't match,
169 * flush its continuations
171 while (magindex < nmagic - 1 &&
172 magic[magindex + 1].cont_level != 0)
178 * If we are going to print something, we'll need to print
179 * a blank before we print something else.
181 if (magic[magindex].desc[0]) {
183 printed_something = 1;
184 if (print_sep(ms, firstline) == -1)
188 if ((ms->c.li[cont_level].off = mprint(ms, &magic[magindex]))
192 /* and any continuations that match */
193 if (file_check_mem(ms, ++cont_level) == -1)
196 while (magic[magindex+1].cont_level != 0 &&
197 ++magindex < nmagic) {
198 ms->line = magic[magindex].lineno; /* for messages */
200 if (cont_level < magic[magindex].cont_level)
202 if (cont_level > magic[magindex].cont_level) {
204 * We're at the end of the level
205 * "cont_level" continuations.
207 cont_level = magic[magindex].cont_level;
209 ms->offset = magic[magindex].offset;
210 if (magic[magindex].flag & OFFADD) {
212 ms->c.li[cont_level - 1].off;
215 #ifdef ENABLE_CONDITIONALS
216 if (magic[magindex].cond == COND_ELSE ||
217 magic[magindex].cond == COND_ELIF) {
218 if (ms->c.li[cont_level].last_match == 1)
222 flush = !mget(ms, s, &magic[magindex], nbytes,
224 if (flush && magic[magindex].reln != '!')
227 switch (flush ? 1 : magiccheck(ms, &magic[magindex])) {
231 #ifdef ENABLE_CONDITIONALS
232 ms->c.li[cont_level].last_match = 0;
236 #ifdef ENABLE_CONDITIONALS
237 ms->c.li[cont_level].last_match = 1;
239 if (magic[magindex].type != FILE_DEFAULT)
240 ms->c.li[cont_level].got_match = 1;
241 else if (ms->c.li[cont_level].got_match) {
242 ms->c.li[cont_level].got_match = 0;
246 * If we are going to print something,
247 * make sure that we have a separator first.
249 if (magic[magindex].desc[0]) {
250 printed_something = 1;
251 if (print_sep(ms, firstline) == -1)
255 * This continuation matched. Print
256 * its message, with a blank before it
257 * if the previous item printed and
258 * this item isn't empty.
260 /* space if previous printed */
262 && (magic[magindex].nospflag == 0)
263 && (magic[magindex].desc[0] != '\0')) {
264 if (file_printf(ms, " ") == -1)
268 if ((ms->c.li[cont_level].off = mprint(ms, &magic[magindex])) == -1)
270 if (magic[magindex].desc[0])
274 * If we see any continuations
278 if (file_check_mem(ms, ++cont_level) == -1)
284 if (printed_something)
286 if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
287 return 1; /* don't keep searching */
290 return returnval; /* This is hit if -k is set or there is no match */
294 check_fmt(struct magic_set *ms, struct magic *m)
299 if (strchr(m->desc, '%') == NULL)
302 rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
305 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
306 file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
309 rc = regexec(&rx, m->desc, 0, 0, 0);
316 char * strndup(const char *, size_t);
319 strndup(const char *str, size_t n)
327 if (!(copy = malloc(len + 1)))
329 (void) memcpy(copy, str, len + 1);
333 #endif /* HAVE_STRNDUP */
336 mprint(struct magic_set *ms, struct magic *m)
341 union VALUETYPE *p = &ms->ms_value;
345 v = file_signextend(ms, m, (uint64_t)p->b);
346 switch (check_fmt(ms, m)) {
350 if (snprintf(buf, sizeof(buf), "%c",
351 (unsigned char)v) < 0)
353 if (file_printf(ms, m->desc, buf) == -1)
357 if (file_printf(ms, m->desc, (unsigned char) v) == -1)
361 t = ms->offset + sizeof(char);
367 v = file_signextend(ms, m, (uint64_t)p->h);
368 switch (check_fmt(ms, m)) {
372 if (snprintf(buf, sizeof(buf), "%hu",
373 (unsigned short)v) < 0)
375 if (file_printf(ms, m->desc, buf) == -1)
379 if (file_printf(ms, m->desc, (unsigned short) v) == -1)
383 t = ms->offset + sizeof(short);
390 v = file_signextend(ms, m, (uint64_t)p->l);
391 switch (check_fmt(ms, m)) {
395 if (snprintf(buf, sizeof(buf), "%u", (uint32_t)v) < 0)
397 if (file_printf(ms, m->desc, buf) == -1)
401 if (file_printf(ms, m->desc, (uint32_t) v) == -1)
405 t = ms->offset + sizeof(int32_t);
411 v = file_signextend(ms, m, p->q);
412 if (file_printf(ms, m->desc, (uint64_t) v) == -1)
414 t = ms->offset + sizeof(int64_t);
419 case FILE_BESTRING16:
420 case FILE_LESTRING16:
421 if (m->reln == '=' || m->reln == '!') {
422 if (file_printf(ms, m->desc, m->value.s) == -1)
424 t = ms->offset + m->vallen;
427 if (*m->value.s == '\0') {
428 char *cp = strchr(p->s,'\n');
432 if (file_printf(ms, m->desc, p->s) == -1)
434 t = ms->offset + strlen(p->s);
442 if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
444 t = ms->offset + sizeof(time_t);
451 if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
453 t = ms->offset + sizeof(time_t);
459 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q, 1))
462 t = ms->offset + sizeof(uint64_t);
468 if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q, 0))
471 t = ms->offset + sizeof(uint64_t);
478 cp = strndup((const char *)ms->search.s, ms->search.rm_len);
480 file_oomem(ms, ms->search.rm_len);
483 rval = file_printf(ms, m->desc, cp);
489 if ((m->str_flags & REGEX_OFFSET_START))
490 t = ms->search.offset;
492 t = ms->search.offset + ms->search.rm_len;
497 if (file_printf(ms, m->desc, m->value.s) == -1)
499 if ((m->str_flags & REGEX_OFFSET_START))
500 t = ms->search.offset;
502 t = ms->search.offset + m->vallen;
506 if (file_printf(ms, m->desc, m->value.s) == -1)
512 file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
519 #define DO_CVT(fld, cast) \
521 switch (m->mask_op & FILE_OPS_MASK) { \
523 p->fld &= cast m->num_mask; \
526 p->fld |= cast m->num_mask; \
529 p->fld ^= cast m->num_mask; \
532 p->fld += cast m->num_mask; \
535 p->fld -= cast m->num_mask; \
537 case FILE_OPMULTIPLY: \
538 p->fld *= cast m->num_mask; \
540 case FILE_OPDIVIDE: \
541 p->fld /= cast m->num_mask; \
543 case FILE_OPMODULO: \
544 p->fld %= cast m->num_mask; \
547 if (m->mask_op & FILE_OPINVERSE) \
551 cvt_8(union VALUETYPE *p, const struct magic *m)
553 DO_CVT(b, (uint8_t));
557 cvt_16(union VALUETYPE *p, const struct magic *m)
559 DO_CVT(h, (uint16_t));
563 cvt_32(union VALUETYPE *p, const struct magic *m)
565 DO_CVT(l, (uint32_t));
569 cvt_64(union VALUETYPE *p, const struct magic *m)
571 DO_CVT(q, (uint64_t));
575 * Convert the byte order of the data we are looking at
576 * While we're here, let's apply the mask operation
577 * (unless you have a better idea)
580 mconvert(struct magic_set *ms, struct magic *m)
582 union VALUETYPE *p = &ms->ms_value;
602 case FILE_BESTRING16:
603 case FILE_LESTRING16: {
606 /* Null terminate and eat *trailing* return */
607 p->s[sizeof(p->s) - 1] = '\0';
609 if (len-- && p->s[len] == '\n')
614 char *ptr1 = p->s, *ptr2 = ptr1 + 1;
616 if (len >= sizeof(p->s))
617 len = sizeof(p->s) - 1;
622 if (len-- && p->s[len] == '\n')
627 p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
634 ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
641 (((int64_t)p->hq[0]<<56)|((int64_t)p->hq[1]<<48)|
642 ((int64_t)p->hq[2]<<40)|((int64_t)p->hq[3]<<32)|
643 (p->hq[4]<<24)|(p->hq[5]<<16)|(p->hq[6]<<8)|(p->hq[7]));
647 p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
654 ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
661 (((int64_t)p->hq[7]<<56)|((int64_t)p->hq[6]<<48)|
662 ((int64_t)p->hq[5]<<40)|((int64_t)p->hq[4]<<32)|
663 (p->hq[3]<<24)|(p->hq[2]<<16)|(p->hq[1]<<8)|(p->hq[0]));
670 ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
678 file_magerror(ms, "invalid type %d in mconvert()", m->type);
685 mdebug(uint32_t offset, const char *str, size_t len)
687 (void) fprintf(stderr, "mget @%d: ", offset);
688 file_showstr(stderr, str, len);
689 (void) fputc('\n', stderr);
690 (void) fputc('\n', stderr);
694 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
695 const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
698 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
699 * anything, but setup pointers into the source
704 ms->search.s = (const char *)s + offset;
705 ms->search.s_len = nbytes - offset;
710 * offset is interpreted as last line to search,
711 * (starting at 1), not as bytes-from start-of-file
715 const char *last; /* end of search region */
716 const char *buf; /* start of search region */
720 ms->search.s_len = 0;
724 buf = (const char *)s + offset;
725 last = (const char *)s + nbytes;
726 /* mget() guarantees buf <= last */
727 for (lines = linecnt, b = buf;
728 lines && ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
731 if (b[0] == '\r' && b[1] == '\n')
735 last = (const char *)s + nbytes;
738 ms->search.s_len = last - buf;
739 ms->search.offset = offset;
740 ms->search.rm_len = 0;
743 case FILE_BESTRING16:
744 case FILE_LESTRING16: {
745 const unsigned char *src = s + offset;
746 const unsigned char *esrc = s + nbytes;
748 char *edst = &p->s[sizeof(p->s) - 1];
750 if (type == FILE_BESTRING16)
753 /* check for pointer overflow */
755 file_magerror(ms, "invalid offset %zu in mcopy()",
759 for (/*EMPTY*/; src < esrc; src++, dst++) {
770 case FILE_STRING: /* XXX - these two should not need */
771 case FILE_PSTRING: /* to copy anything, but do anyway. */
777 if (offset >= nbytes) {
778 (void)memset(p, '\0', sizeof(*p));
781 if (nbytes - offset < sizeof(*p))
782 nbytes = nbytes - offset;
786 (void)memcpy(p, s + offset, nbytes);
789 * the usefulness of padding with zeroes eludes me, it
790 * might even cause problems
792 if (nbytes < sizeof(*p))
793 (void)memset(((char *)(void *)p) + nbytes, '\0',
794 sizeof(*p) - nbytes);
799 mget(struct magic_set *ms, const unsigned char *s,
800 struct magic *m, size_t nbytes, unsigned int cont_level)
802 uint32_t offset = ms->offset;
803 uint32_t count = m->str_count;
804 union VALUETYPE *p = &ms->ms_value;
806 if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
809 if ((ms->flags & MAGIC_DEBUG) != 0) {
810 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
814 if (m->flag & INDIR) {
815 int off = m->in_offset;
816 if (m->in_op & FILE_OPINDIRECT) {
817 const union VALUETYPE *q =
818 ((const void *)(s + offset + off));
819 switch (m->in_type) {
827 off = (short)((q->hs[0]<<8)|(q->hs[1]));
830 off = (short)((q->hs[1]<<8)|(q->hs[0]));
836 off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
837 (q->hl[2]<<8)|(q->hl[3]));
840 off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
841 (q->hl[1]<<8)|(q->hl[0]));
844 off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
845 (q->hl[3]<<8)|(q->hl[2]));
849 switch (m->in_type) {
851 if (nbytes < (offset + 1))
854 switch (m->in_op & FILE_OPS_MASK) {
870 case FILE_OPMULTIPLY:
882 if (m->in_op & FILE_OPINVERSE)
886 if (nbytes < (offset + 2))
889 switch (m->in_op & FILE_OPS_MASK) {
891 offset = (short)((p->hs[0]<<8)|
896 offset = (short)((p->hs[0]<<8)|
901 offset = (short)((p->hs[0]<<8)|
906 offset = (short)((p->hs[0]<<8)|
911 offset = (short)((p->hs[0]<<8)|
915 case FILE_OPMULTIPLY:
916 offset = (short)((p->hs[0]<<8)|
921 offset = (short)((p->hs[0]<<8)|
926 offset = (short)((p->hs[0]<<8)|
932 offset = (short)((p->hs[0]<<8)|
934 if (m->in_op & FILE_OPINVERSE)
938 if (nbytes < (offset + 2))
941 switch (m->in_op & FILE_OPS_MASK) {
943 offset = (short)((p->hs[1]<<8)|
948 offset = (short)((p->hs[1]<<8)|
953 offset = (short)((p->hs[1]<<8)|
958 offset = (short)((p->hs[1]<<8)|
963 offset = (short)((p->hs[1]<<8)|
967 case FILE_OPMULTIPLY:
968 offset = (short)((p->hs[1]<<8)|
973 offset = (short)((p->hs[1]<<8)|
978 offset = (short)((p->hs[1]<<8)|
984 offset = (short)((p->hs[1]<<8)|
986 if (m->in_op & FILE_OPINVERSE)
990 if (nbytes < (offset + 2))
993 switch (m->in_op & FILE_OPS_MASK) {
1001 offset = p->h ^ off;
1004 offset = p->h + off;
1007 offset = p->h - off;
1009 case FILE_OPMULTIPLY:
1010 offset = p->h * off;
1013 offset = p->h / off;
1016 offset = p->h % off;
1022 if (m->in_op & FILE_OPINVERSE)
1026 if (nbytes < (offset + 4))
1029 switch (m->in_op & FILE_OPS_MASK) {
1031 offset = (int32_t)((p->hl[0]<<24)|
1038 offset = (int32_t)((p->hl[0]<<24)|
1045 offset = (int32_t)((p->hl[0]<<24)|
1052 offset = (int32_t)((p->hl[0]<<24)|
1059 offset = (int32_t)((p->hl[0]<<24)|
1065 case FILE_OPMULTIPLY:
1066 offset = (int32_t)((p->hl[0]<<24)|
1073 offset = (int32_t)((p->hl[0]<<24)|
1080 offset = (int32_t)((p->hl[0]<<24)|
1088 offset = (int32_t)((p->hl[0]<<24)|
1092 if (m->in_op & FILE_OPINVERSE)
1096 if (nbytes < (offset + 4))
1099 switch (m->in_op & FILE_OPS_MASK) {
1101 offset = (int32_t)((p->hl[3]<<24)|
1108 offset = (int32_t)((p->hl[3]<<24)|
1115 offset = (int32_t)((p->hl[3]<<24)|
1122 offset = (int32_t)((p->hl[3]<<24)|
1129 offset = (int32_t)((p->hl[3]<<24)|
1135 case FILE_OPMULTIPLY:
1136 offset = (int32_t)((p->hl[3]<<24)|
1143 offset = (int32_t)((p->hl[3]<<24)|
1150 offset = (int32_t)((p->hl[3]<<24)|
1158 offset = (int32_t)((p->hl[3]<<24)|
1162 if (m->in_op & FILE_OPINVERSE)
1166 if (nbytes < (offset + 4))
1169 switch (m->in_op & FILE_OPS_MASK) {
1171 offset = (int32_t)((p->hl[1]<<24)|
1178 offset = (int32_t)((p->hl[1]<<24)|
1185 offset = (int32_t)((p->hl[1]<<24)|
1192 offset = (int32_t)((p->hl[1]<<24)|
1199 offset = (int32_t)((p->hl[1]<<24)|
1205 case FILE_OPMULTIPLY:
1206 offset = (int32_t)((p->hl[1]<<24)|
1213 offset = (int32_t)((p->hl[1]<<24)|
1220 offset = (int32_t)((p->hl[1]<<24)|
1228 offset = (int32_t)((p->hl[1]<<24)|
1232 if (m->in_op & FILE_OPINVERSE)
1236 if (nbytes < (offset + 4))
1239 switch (m->in_op & FILE_OPS_MASK) {
1241 offset = p->l & off;
1244 offset = p->l | off;
1247 offset = p->l ^ off;
1250 offset = p->l + off;
1253 offset = p->l - off;
1255 case FILE_OPMULTIPLY:
1256 offset = p->l * off;
1259 offset = p->l / off;
1262 offset = p->l % off;
1264 /* case TOOMANYSWITCHBLOCKS:
1265 * ugh = p->eye % m->strain;
1268 * off = p->tab & m->in_gest;
1274 if (m->in_op & FILE_OPINVERSE)
1279 if (m->flag & INDIROFFADD)
1280 offset += ms->c.li[cont_level-1].off;
1281 if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
1283 ms->offset = offset;
1285 if ((ms->flags & MAGIC_DEBUG) != 0) {
1286 mdebug(offset, (char *)(void *)p,
1287 sizeof(union VALUETYPE));
1292 /* Verify we have enough data to match magic type */
1295 if (nbytes < (offset + 1)) /* should alway be true */
1302 if (nbytes < (offset + 2))
1318 if (nbytes < (offset + 4))
1325 if (nbytes < (offset + m->vallen))
1330 if (nbytes < offset)
1334 case FILE_DEFAULT: /* nothing to check */
1338 if (!mconvert(ms, m))
1344 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1347 * Convert the source args to unsigned here so that (1) the
1348 * compare will be unsigned as it is in strncmp() and (2) so
1349 * the ctype functions will work correctly without extra
1352 const unsigned char *a = (const unsigned char *)s1;
1353 const unsigned char *b = (const unsigned char *)s2;
1357 * What we want here is:
1358 * v = strncmp(m->value.s, p->s, m->vallen);
1359 * but ignoring any nulls. bcmp doesn't give -/+/0
1360 * and isn't universally available anyway.
1363 if (0L == flags) { /* normal string: do it fast */
1365 if ((v = *b++ - *a++) != '\0')
1368 else { /* combine the others */
1370 if ((flags & STRING_IGNORE_LOWERCASE) &&
1372 if ((v = tolower(*b++) - *a++) != '\0')
1375 else if ((flags & STRING_IGNORE_UPPERCASE) &&
1377 if ((v = toupper(*b++) - *a++) != '\0')
1380 else if ((flags & STRING_COMPACT_BLANK) &&
1383 if (isspace(*b++)) {
1392 else if ((flags & STRING_COMPACT_OPTIONAL_BLANK) &&
1399 if ((v = *b++ - *a++) != '\0')
1408 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1411 * XXX - The 16-bit string compare probably needs to be done
1412 * differently, especially if the flags are to be supported.
1413 * At the moment, I am unsure.
1416 return file_strncmp(a, b, len, flags);
1420 magiccheck(struct magic_set *ms, struct magic *m)
1422 uint64_t l = m->value.q;
1425 union VALUETYPE *p = &ms->ms_value;
1473 v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1476 case FILE_BESTRING16:
1477 case FILE_LESTRING16:
1479 v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1482 case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1486 if (ms->search.s == NULL)
1489 slen = MIN(m->vallen, sizeof(m->value.s));
1492 ms->search.offset = m->offset;
1494 for (idx = 0; m->str_count == 0 || idx < m->str_count; idx++) {
1495 if (slen + idx > ms->search.s_len)
1498 v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
1499 if (v == 0) { /* found match */
1500 ms->search.offset = m->offset + idx;
1511 if (ms->search.s == NULL)
1515 rc = regcomp(&rx, m->value.s,
1516 REG_EXTENDED|REG_NEWLINE|
1517 ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
1519 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1520 file_magerror(ms, "regex error %d, (%s)",
1525 regmatch_t pmatch[1];
1526 pmatch[0].rm_so = 0;
1527 pmatch[0].rm_eo = ms->search.s_len;
1528 rc = regexec(&rx, (const char *)ms->search.s,
1529 1, pmatch, REG_STARTEND);
1532 ms->search.s += (int)pmatch[0].rm_so;
1533 ms->search.offset += (size_t)pmatch[0].rm_so;
1535 (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
1544 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
1545 file_magerror(ms, "regexec error %d, (%s)",
1552 if (v == (uint64_t)-1)
1557 file_magerror(ms, "invalid type %d in magiccheck()", m->type);
1561 v = file_signextend(ms, m, v);
1565 if ((ms->flags & MAGIC_DEBUG) != 0)
1566 (void) fprintf(stderr, "%llu == *any* = 1\n",
1567 (unsigned long long)v);
1573 if ((ms->flags & MAGIC_DEBUG) != 0)
1574 (void) fprintf(stderr, "%llu != %llu = %d\n",
1575 (unsigned long long)v, (unsigned long long)l,
1581 if ((ms->flags & MAGIC_DEBUG) != 0)
1582 (void) fprintf(stderr, "%llu == %llu = %d\n",
1583 (unsigned long long)v, (unsigned long long)l,
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 if (m->flag & UNSIGNED) {
1606 if ((ms->flags & MAGIC_DEBUG) != 0)
1607 (void) fprintf(stderr, "%llu < %llu = %d\n",
1608 (unsigned long long)v,
1609 (unsigned long long)l, matched);
1612 matched = (int64_t) v < (int64_t) l;
1613 if ((ms->flags & MAGIC_DEBUG) != 0)
1614 (void) fprintf(stderr, "%lld < %lld = %d\n",
1615 (long long)v, (long long)l, matched);
1620 matched = (v & l) == l;
1621 if ((ms->flags & MAGIC_DEBUG) != 0)
1622 (void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n",
1623 (unsigned long long)v, (unsigned long long)l,
1624 (unsigned long long)l, matched);
1628 matched = (v & l) != l;
1629 if ((ms->flags & MAGIC_DEBUG) != 0)
1630 (void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n",
1631 (unsigned long long)v, (unsigned long long)l,
1632 (unsigned long long)l, matched);
1637 file_magerror(ms, "cannot happen: invalid relation `%c'",
1646 print_sep(struct magic_set *ms, int firstline)
1651 * we found another match
1652 * put a newline and '-' to do some simple formatting
1654 return file_printf(ms, "\n- ");