1 /* $NetBSD: softmagic.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $ */
4 * Copyright (c) Ian F. Darwin 1986-1995.
5 * Software written by Ian F. Darwin and others;
6 * maintained 1995-present by Christos Zoulas and others.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * softmagic - interpret variable magic from MAGIC
38 FILE_RCSID("@(#)$File: softmagic.c,v 1.135 2009/03/27 22:42:49 christos Exp $")
40 __RCSID("$NetBSD: softmagic.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $");
51 private int match(struct magic_set
*, struct magic
*, uint32_t,
52 const unsigned char *, size_t, int);
53 private int mget(struct magic_set
*, const unsigned char *,
54 struct magic
*, 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 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, size_t);
61 private int mconvert(struct magic_set
*, struct magic
*);
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
*);
70 * softmagic - lookup one file in parsed, in-memory copy of database
71 * Passed the name and FILE * of one file to be typed.
73 /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
75 file_softmagic(struct magic_set
*ms
, const unsigned char *buf
, size_t nbytes
, int mode
)
79 for (ml
= ms
->mlist
->next
; ml
!= ms
->mlist
; ml
= ml
->next
)
80 if ((rv
= match(ms
, ml
->magic
, ml
->nmagic
, buf
, nbytes
, mode
)) != 0)
87 * Go through the whole list, stopping if you find a match. Process all
88 * the continuations of that match before returning.
90 * We support multi-level continuations:
92 * At any time when processing a successful top-level match, there is a
93 * current continuation level; it represents the level of the last
94 * successfully matched continuation.
96 * Continuations above that level are skipped as, if we see one, it
97 * means that the continuation that controls them - i.e, the
98 * lower-level continuation preceding them - failed to match.
100 * Continuations below that level are processed as, if we see one,
101 * it means we've finished processing or skipping higher-level
102 * continuations under the control of a successful or unsuccessful
103 * lower-level continuation, and are now seeing the next lower-level
104 * continuation and should process it. The current continuation
105 * level reverts to the level of the one we're seeing.
107 * Continuations at the current level are processed as, if we see
108 * one, there's no lower-level continuation that may have failed.
110 * If a continuation matches, we bump the current continuation level
111 * so that higher-level continuations are processed.
114 match(struct magic_set
*ms
, struct magic
*magic
, uint32_t nmagic
,
115 const unsigned char *s
, size_t nbytes
, int mode
)
117 uint32_t magindex
= 0;
118 unsigned int cont_level
= 0;
119 int need_separator
= 0;
120 int returnval
= 0, e
; /* if a match is found it is set to 1*/
121 int firstline
= 1; /* a flag to print X\n X\n- X */
122 int printed_something
= 0;
123 int print
= (ms
->flags
& (MAGIC_MIME
|MAGIC_APPLE
)) == 0;
125 if (file_check_mem(ms
, cont_level
) == -1)
128 for (magindex
= 0; magindex
< nmagic
; magindex
++) {
130 struct magic
*m
= &magic
[magindex
];
132 if ((m
->flag
& BINTEST
) != mode
) {
134 while (magic
[magindex
+ 1].cont_level
!= 0 &&
137 continue; /* Skip to next top-level test*/
140 ms
->offset
= m
->offset
;
141 ms
->line
= m
->lineno
;
143 /* if main entry matches, print it... */
144 switch (mget(ms
, s
, m
, nbytes
, cont_level
)) {
148 flush
= m
->reln
!= '!';
151 if (m
->type
== FILE_INDIRECT
)
154 switch (magiccheck(ms
, m
)) {
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.
183 printed_something
= 1;
184 if ((e
= handle_annotation(ms
, m
)) != 0)
186 if (print_sep(ms
, firstline
) == -1)
191 if (print
&& mprint(ms
, m
) == -1)
194 ms
->c
.li
[cont_level
].off
= moffset(ms
, m
);
196 /* and any continuations that match */
197 if (file_check_mem(ms
, ++cont_level
) == -1)
200 while (magic
[magindex
+1].cont_level
!= 0 &&
201 ++magindex
< nmagic
) {
202 m
= &magic
[magindex
];
203 ms
->line
= m
->lineno
; /* for messages */
205 if (cont_level
< m
->cont_level
)
207 if (cont_level
> m
->cont_level
) {
209 * We're at the end of the level
210 * "cont_level" continuations.
212 cont_level
= m
->cont_level
;
214 ms
->offset
= m
->offset
;
215 if (m
->flag
& OFFADD
) {
217 ms
->c
.li
[cont_level
- 1].off
;
220 #ifdef ENABLE_CONDITIONALS
221 if (m
->cond
== COND_ELSE
||
222 m
->cond
== COND_ELIF
) {
223 if (ms
->c
.li
[cont_level
].last_match
== 1)
227 switch (mget(ms
, s
, m
, nbytes
, cont_level
)) {
236 if (m
->type
== FILE_INDIRECT
)
242 switch (flush
? 1 : magiccheck(ms
, m
)) {
246 #ifdef ENABLE_CONDITIONALS
247 ms
->c
.li
[cont_level
].last_match
= 0;
251 #ifdef ENABLE_CONDITIONALS
252 ms
->c
.li
[cont_level
].last_match
= 1;
254 if (m
->type
!= FILE_DEFAULT
)
255 ms
->c
.li
[cont_level
].got_match
= 1;
256 else if (ms
->c
.li
[cont_level
].got_match
) {
257 ms
->c
.li
[cont_level
].got_match
= 0;
261 * If we are going to print something,
262 * make sure that we have a separator first.
265 if ((e
= handle_annotation(ms
, m
)) != 0)
267 if (!printed_something
) {
268 printed_something
= 1;
269 if (print_sep(ms
, firstline
)
275 * This continuation matched. Print
276 * its message, with a blank before it
277 * if the previous item printed and
278 * this item isn't empty.
280 /* space if previous printed */
282 && ((m
->flag
& NOSPACE
) == 0)
285 file_printf(ms
, " ") == -1)
289 if (print
&& mprint(ms
, m
) == -1)
292 ms
->c
.li
[cont_level
].off
= moffset(ms
, m
);
298 * If we see any continuations
302 if (file_check_mem(ms
, ++cont_level
) == -1)
307 if (printed_something
) {
312 if ((ms
->flags
& MAGIC_CONTINUE
) == 0 && printed_something
) {
313 return returnval
; /* don't keep searching */
316 return returnval
; /* This is hit if -k is set or there is no match */
320 check_fmt(struct magic_set
*ms
, struct magic
*m
)
325 if (strchr(m
->desc
, '%') == NULL
)
328 rc
= regcomp(&rx
, "%[-0-9\\.]*s", REG_EXTENDED
|REG_NOSUB
);
331 (void)regerror(rc
, &rx
, errmsg
, sizeof(errmsg
));
332 file_magerror(ms
, "regex error %d, (%s)", rc
, errmsg
);
335 rc
= regexec(&rx
, m
->desc
, 0, 0, 0);
342 char * strndup(const char *, size_t);
345 strndup(const char *str
, size_t n
)
350 for (len
= 0; len
< n
&& str
[len
]; len
++)
352 if ((copy
= malloc(len
+ 1)) == NULL
)
354 (void)memcpy(copy
, str
, len
);
358 #endif /* HAVE_STRNDUP */
361 mprint(struct magic_set
*ms
, struct magic
*m
)
368 union VALUETYPE
*p
= &ms
->ms_value
;
372 v
= file_signextend(ms
, m
, (uint64_t)p
->b
);
373 switch (check_fmt(ms
, m
)) {
377 (void)snprintf(buf
, sizeof(buf
), "%c",
379 if (file_printf(ms
, m
->desc
, buf
) == -1)
383 if (file_printf(ms
, m
->desc
, (unsigned char) v
) == -1)
387 t
= ms
->offset
+ sizeof(char);
393 v
= file_signextend(ms
, m
, (uint64_t)p
->h
);
394 switch (check_fmt(ms
, m
)) {
398 (void)snprintf(buf
, sizeof(buf
), "%hu",
400 if (file_printf(ms
, m
->desc
, buf
) == -1)
405 file_printf(ms
, m
->desc
, (unsigned short) v
) == -1)
409 t
= ms
->offset
+ sizeof(short);
416 v
= file_signextend(ms
, m
, (uint64_t)p
->l
);
417 switch (check_fmt(ms
, m
)) {
421 (void)snprintf(buf
, sizeof(buf
), "%u", (uint32_t)v
);
422 if (file_printf(ms
, m
->desc
, buf
) == -1)
426 if (file_printf(ms
, m
->desc
, (uint32_t) v
) == -1)
430 t
= ms
->offset
+ sizeof(int32_t);
436 v
= file_signextend(ms
, m
, p
->q
);
437 if (file_printf(ms
, m
->desc
, (uint64_t) v
) == -1)
439 t
= ms
->offset
+ sizeof(int64_t);
444 case FILE_BESTRING16
:
445 case FILE_LESTRING16
:
446 if (m
->reln
== '=' || m
->reln
== '!') {
447 if (file_printf(ms
, m
->desc
, m
->value
.s
) == -1)
449 t
= ms
->offset
+ m
->vallen
;
452 if (*m
->value
.s
== '\0')
453 p
->s
[strcspn(p
->s
, "\n")] = '\0';
454 if (file_printf(ms
, m
->desc
, p
->s
) == -1)
456 t
= ms
->offset
+ strlen(p
->s
);
457 if (m
->type
== FILE_PSTRING
)
466 if (file_printf(ms
, m
->desc
, file_fmttime(p
->l
, 1)) == -1)
468 t
= ms
->offset
+ sizeof(time_t);
475 if (file_printf(ms
, m
->desc
, file_fmttime(p
->l
, 0)) == -1)
477 t
= ms
->offset
+ sizeof(time_t);
483 if (file_printf(ms
, m
->desc
, file_fmttime((uint32_t)p
->q
,
486 t
= ms
->offset
+ sizeof(uint64_t);
492 if (file_printf(ms
, m
->desc
, file_fmttime((uint32_t)p
->q
,
495 t
= ms
->offset
+ sizeof(uint64_t);
502 switch (check_fmt(ms
, m
)) {
506 (void)snprintf(buf
, sizeof(buf
), "%g", vf
);
507 if (file_printf(ms
, m
->desc
, buf
) == -1)
511 if (file_printf(ms
, m
->desc
, vf
) == -1)
515 t
= ms
->offset
+ sizeof(float);
522 switch (check_fmt(ms
, m
)) {
526 (void)snprintf(buf
, sizeof(buf
), "%g", vd
);
527 if (file_printf(ms
, m
->desc
, buf
) == -1)
531 if (file_printf(ms
, m
->desc
, vd
) == -1)
535 t
= ms
->offset
+ sizeof(double);
542 cp
= strndup((const char *)ms
->search
.s
, ms
->search
.rm_len
);
544 file_oomem(ms
, ms
->search
.rm_len
);
547 rval
= file_printf(ms
, m
->desc
, cp
);
553 if ((m
->str_flags
& REGEX_OFFSET_START
))
554 t
= ms
->search
.offset
;
556 t
= ms
->search
.offset
+ ms
->search
.rm_len
;
561 if (file_printf(ms
, m
->desc
, m
->value
.s
) == -1)
563 if ((m
->str_flags
& REGEX_OFFSET_START
))
564 t
= ms
->search
.offset
;
566 t
= ms
->search
.offset
+ m
->vallen
;
570 if (file_printf(ms
, m
->desc
, m
->value
.s
) == -1)
580 file_magerror(ms
, "invalid m->type (%d) in mprint()", m
->type
);
587 moffset(struct magic_set
*ms
, struct magic
*m
)
591 return CAST(int32_t, (ms
->offset
+ sizeof(char)));
596 return CAST(int32_t, (ms
->offset
+ sizeof(short)));
602 return CAST(int32_t, (ms
->offset
+ sizeof(int32_t)));
607 return CAST(int32_t, (ms
->offset
+ sizeof(int64_t)));
611 case FILE_BESTRING16
:
612 case FILE_LESTRING16
:
613 if (m
->reln
== '=' || m
->reln
== '!')
614 return ms
->offset
+ m
->vallen
;
616 union VALUETYPE
*p
= &ms
->ms_value
;
619 if (*m
->value
.s
== '\0')
620 p
->s
[strcspn(p
->s
, "\n")] = '\0';
621 t
= CAST(uint32_t, (ms
->offset
+ strlen(p
->s
)));
622 if (m
->type
== FILE_PSTRING
)
631 return CAST(int32_t, (ms
->offset
+ sizeof(time_t)));
637 return CAST(int32_t, (ms
->offset
+ sizeof(time_t)));
642 return CAST(int32_t, (ms
->offset
+ sizeof(uint64_t)));
647 return CAST(int32_t, (ms
->offset
+ sizeof(uint64_t)));
652 return CAST(int32_t, (ms
->offset
+ sizeof(float)));
657 return CAST(int32_t, (ms
->offset
+ sizeof(double)));
660 if ((m
->str_flags
& REGEX_OFFSET_START
) != 0)
661 return CAST(int32_t, ms
->search
.offset
);
663 return CAST(int32_t, (ms
->search
.offset
+
667 if ((m
->str_flags
& REGEX_OFFSET_START
) != 0)
668 return CAST(int32_t, ms
->search
.offset
);
670 return CAST(int32_t, (ms
->search
.offset
+ m
->vallen
));
683 #define DO_CVT(fld, cast) \
685 switch (m->mask_op & FILE_OPS_MASK) { \
687 p->fld &= cast m->num_mask; \
690 p->fld |= cast m->num_mask; \
693 p->fld ^= cast m->num_mask; \
696 p->fld += cast m->num_mask; \
699 p->fld -= cast m->num_mask; \
701 case FILE_OPMULTIPLY: \
702 p->fld *= cast m->num_mask; \
704 case FILE_OPDIVIDE: \
705 p->fld /= cast m->num_mask; \
707 case FILE_OPMODULO: \
708 p->fld %= cast m->num_mask; \
711 if (m->mask_op & FILE_OPINVERSE) \
715 cvt_8(union VALUETYPE
*p
, const struct magic
*m
)
717 DO_CVT(b
, (uint8_t));
721 cvt_16(union VALUETYPE
*p
, const struct magic
*m
)
723 DO_CVT(h
, (uint16_t));
727 cvt_32(union VALUETYPE
*p
, const struct magic
*m
)
729 DO_CVT(l
, (uint32_t));
733 cvt_64(union VALUETYPE
*p
, const struct magic
*m
)
735 DO_CVT(q
, (uint64_t));
738 #define DO_CVT2(fld, cast) \
740 switch (m->mask_op & FILE_OPS_MASK) { \
742 p->fld += cast m->num_mask; \
745 p->fld -= cast m->num_mask; \
747 case FILE_OPMULTIPLY: \
748 p->fld *= cast m->num_mask; \
750 case FILE_OPDIVIDE: \
751 p->fld /= cast m->num_mask; \
756 cvt_float(union VALUETYPE
*p
, const struct magic
*m
)
762 cvt_double(union VALUETYPE
*p
, const struct magic
*m
)
764 DO_CVT2(d
, (double));
768 * Convert the byte order of the data we are looking at
769 * While we're here, let's apply the mask operation
770 * (unless you have a better idea)
773 mconvert(struct magic_set
*ms
, struct magic
*m
)
775 union VALUETYPE
*p
= &ms
->ms_value
;
795 case FILE_BESTRING16
:
796 case FILE_LESTRING16
: {
797 /* Null terminate and eat *trailing* return */
798 p
->s
[sizeof(p
->s
) - 1] = '\0';
800 /* Why? breaks magic numbers that end with \xa */
802 if (len
-- && p
->s
[len
] == '\n')
808 char *ptr1
= p
->s
, *ptr2
= ptr1
+ 1;
810 if (len
>= sizeof(p
->s
))
811 len
= sizeof(p
->s
) - 1;
816 /* Why? breaks magic numbers that end with \xa */
818 if (len
-- && p
->s
[len
] == '\n')
824 p
->h
= (short)((p
->hs
[0]<<8)|(p
->hs
[1]));
831 ((p
->hl
[0]<<24)|(p
->hl
[1]<<16)|(p
->hl
[2]<<8)|(p
->hl
[3]));
838 (((uint64_t)p
->hq
[0]<<56)|((uint64_t)p
->hq
[1]<<48)|
839 ((uint64_t)p
->hq
[2]<<40)|((uint64_t)p
->hq
[3]<<32)|
840 ((uint64_t)p
->hq
[4]<<24)|((uint64_t)p
->hq
[5]<<16)|
841 ((uint64_t)p
->hq
[6]<<8)|((uint64_t)p
->hq
[7]));
845 p
->h
= (short)((p
->hs
[1]<<8)|(p
->hs
[0]));
852 ((p
->hl
[3]<<24)|(p
->hl
[2]<<16)|(p
->hl
[1]<<8)|(p
->hl
[0]));
859 (((uint64_t)p
->hq
[7]<<56)|((uint64_t)p
->hq
[6]<<48)|
860 ((uint64_t)p
->hq
[5]<<40)|((uint64_t)p
->hq
[4]<<32)|
861 ((uint64_t)p
->hq
[3]<<24)|((uint64_t)p
->hq
[2]<<16)|
862 ((uint64_t)p
->hq
[1]<<8)|((uint64_t)p
->hq
[0]));
869 ((p
->hl
[1]<<24)|(p
->hl
[0]<<16)|(p
->hl
[3]<<8)|(p
->hl
[2]));
876 p
->l
= ((uint32_t)p
->hl
[0]<<24)|((uint32_t)p
->hl
[1]<<16)|
877 ((uint32_t)p
->hl
[2]<<8) |((uint32_t)p
->hl
[3]);
881 p
->l
= ((uint32_t)p
->hl
[3]<<24)|((uint32_t)p
->hl
[2]<<16)|
882 ((uint32_t)p
->hl
[1]<<8) |((uint32_t)p
->hl
[0]);
889 p
->q
= ((uint64_t)p
->hq
[0]<<56)|((uint64_t)p
->hq
[1]<<48)|
890 ((uint64_t)p
->hq
[2]<<40)|((uint64_t)p
->hq
[3]<<32)|
891 ((uint64_t)p
->hq
[4]<<24)|((uint64_t)p
->hq
[5]<<16)|
892 ((uint64_t)p
->hq
[6]<<8) |((uint64_t)p
->hq
[7]);
896 p
->q
= ((uint64_t)p
->hq
[7]<<56)|((uint64_t)p
->hq
[6]<<48)|
897 ((uint64_t)p
->hq
[5]<<40)|((uint64_t)p
->hq
[4]<<32)|
898 ((uint64_t)p
->hq
[3]<<24)|((uint64_t)p
->hq
[2]<<16)|
899 ((uint64_t)p
->hq
[1]<<8) |((uint64_t)p
->hq
[0]);
907 file_magerror(ms
, "invalid type %d in mconvert()", m
->type
);
914 mdebug(uint32_t offset
, const char *str
, size_t len
)
916 (void) fprintf(stderr
, "mget @%d: ", offset
);
917 file_showstr(stderr
, str
, len
);
918 (void) fputc('\n', stderr
);
919 (void) fputc('\n', stderr
);
923 mcopy(struct magic_set
*ms
, union VALUETYPE
*p
, int type
, int indir
,
924 const unsigned char *s
, uint32_t offset
, size_t nbytes
, size_t linecnt
)
927 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
928 * anything, but setup pointers into the source
933 ms
->search
.s
= CAST(const char *, s
) + offset
;
934 ms
->search
.s_len
= nbytes
- offset
;
935 ms
->search
.offset
= offset
;
941 const char *last
; /* end of search region */
942 const char *buf
; /* start of search region */
947 ms
->search
.s_len
= 0;
951 buf
= CAST(const char *, s
) + offset
;
952 end
= last
= CAST(const char *, s
) + nbytes
;
953 /* mget() guarantees buf <= last */
954 for (lines
= linecnt
, b
= buf
; lines
&&
955 ((b
= memchr(c
= b
, '\n', CAST(size_t, (end
- b
))))
956 || (b
= memchr(c
, '\r', CAST(size_t, (end
- c
)))));
959 if (b
[0] == '\r' && b
[1] == '\n')
963 last
= CAST(const char *, s
) + nbytes
;
966 ms
->search
.s_len
= last
- buf
;
967 ms
->search
.offset
= offset
;
968 ms
->search
.rm_len
= 0;
971 case FILE_BESTRING16
:
972 case FILE_LESTRING16
: {
973 const unsigned char *src
= s
+ offset
;
974 const unsigned char *esrc
= s
+ nbytes
;
976 char *edst
= &p
->s
[sizeof(p
->s
) - 1];
978 if (type
== FILE_BESTRING16
)
981 /* check for pointer overflow */
983 file_magerror(ms
, "invalid offset %u in mcopy()",
987 for (/*EMPTY*/; src
< esrc
; src
+= 2, dst
++) {
993 if (type
== FILE_BESTRING16
?
1002 case FILE_STRING
: /* XXX - these two should not need */
1003 case FILE_PSTRING
: /* to copy anything, but do anyway. */
1009 if (offset
>= nbytes
) {
1010 (void)memset(p
, '\0', sizeof(*p
));
1013 if (nbytes
- offset
< sizeof(*p
))
1014 nbytes
= nbytes
- offset
;
1016 nbytes
= sizeof(*p
);
1018 (void)memcpy(p
, s
+ offset
, nbytes
);
1021 * the usefulness of padding with zeroes eludes me, it
1022 * might even cause problems
1024 if (nbytes
< sizeof(*p
))
1025 (void)memset(((char *)(void *)p
) + nbytes
, '\0',
1026 sizeof(*p
) - nbytes
);
1031 mget(struct magic_set
*ms
, const unsigned char *s
,
1032 struct magic
*m
, size_t nbytes
, unsigned int cont_level
)
1034 uint32_t offset
= ms
->offset
;
1035 uint32_t count
= m
->str_range
;
1036 union VALUETYPE
*p
= &ms
->ms_value
;
1038 if (mcopy(ms
, p
, m
->type
, m
->flag
& INDIR
, s
, offset
, nbytes
, count
) == -1)
1041 if ((ms
->flags
& MAGIC_DEBUG
) != 0) {
1042 mdebug(offset
, (char *)(void *)p
, sizeof(union VALUETYPE
));
1043 #ifndef COMPILE_ONLY
1048 if (m
->flag
& INDIR
) {
1049 int off
= m
->in_offset
;
1050 if (m
->in_op
& FILE_OPINDIRECT
) {
1051 const union VALUETYPE
*q
= CAST(const union VALUETYPE
*,
1052 ((const void *)(s
+ offset
+ off
)));
1053 switch (m
->in_type
) {
1061 off
= (short)((q
->hs
[0]<<8)|(q
->hs
[1]));
1064 off
= (short)((q
->hs
[1]<<8)|(q
->hs
[0]));
1071 off
= (int32_t)((q
->hl
[0]<<24)|(q
->hl
[1]<<16)|
1072 (q
->hl
[2]<<8)|(q
->hl
[3]));
1076 off
= (int32_t)((q
->hl
[3]<<24)|(q
->hl
[2]<<16)|
1077 (q
->hl
[1]<<8)|(q
->hl
[0]));
1080 off
= (int32_t)((q
->hl
[1]<<24)|(q
->hl
[0]<<16)|
1081 (q
->hl
[3]<<8)|(q
->hl
[2]));
1085 switch (m
->in_type
) {
1087 if (nbytes
< (offset
+ 1))
1090 switch (m
->in_op
& FILE_OPS_MASK
) {
1092 offset
= p
->b
& off
;
1095 offset
= p
->b
| off
;
1098 offset
= p
->b
^ off
;
1101 offset
= p
->b
+ off
;
1104 offset
= p
->b
- off
;
1106 case FILE_OPMULTIPLY
:
1107 offset
= p
->b
* off
;
1110 offset
= p
->b
/ off
;
1113 offset
= p
->b
% off
;
1118 if (m
->in_op
& FILE_OPINVERSE
)
1122 if (nbytes
< (offset
+ 2))
1125 switch (m
->in_op
& FILE_OPS_MASK
) {
1127 offset
= (short)((p
->hs
[0]<<8)|
1132 offset
= (short)((p
->hs
[0]<<8)|
1137 offset
= (short)((p
->hs
[0]<<8)|
1142 offset
= (short)((p
->hs
[0]<<8)|
1147 offset
= (short)((p
->hs
[0]<<8)|
1151 case FILE_OPMULTIPLY
:
1152 offset
= (short)((p
->hs
[0]<<8)|
1157 offset
= (short)((p
->hs
[0]<<8)|
1162 offset
= (short)((p
->hs
[0]<<8)|
1168 offset
= (short)((p
->hs
[0]<<8)|
1170 if (m
->in_op
& FILE_OPINVERSE
)
1174 if (nbytes
< (offset
+ 2))
1177 switch (m
->in_op
& FILE_OPS_MASK
) {
1179 offset
= (short)((p
->hs
[1]<<8)|
1184 offset
= (short)((p
->hs
[1]<<8)|
1189 offset
= (short)((p
->hs
[1]<<8)|
1194 offset
= (short)((p
->hs
[1]<<8)|
1199 offset
= (short)((p
->hs
[1]<<8)|
1203 case FILE_OPMULTIPLY
:
1204 offset
= (short)((p
->hs
[1]<<8)|
1209 offset
= (short)((p
->hs
[1]<<8)|
1214 offset
= (short)((p
->hs
[1]<<8)|
1220 offset
= (short)((p
->hs
[1]<<8)|
1222 if (m
->in_op
& FILE_OPINVERSE
)
1226 if (nbytes
< (offset
+ 2))
1229 switch (m
->in_op
& FILE_OPS_MASK
) {
1231 offset
= p
->h
& off
;
1234 offset
= p
->h
| off
;
1237 offset
= p
->h
^ off
;
1240 offset
= p
->h
+ off
;
1243 offset
= p
->h
- off
;
1245 case FILE_OPMULTIPLY
:
1246 offset
= p
->h
* off
;
1249 offset
= p
->h
/ off
;
1252 offset
= p
->h
% off
;
1258 if (m
->in_op
& FILE_OPINVERSE
)
1263 if (nbytes
< (offset
+ 4))
1266 switch (m
->in_op
& FILE_OPS_MASK
) {
1268 offset
= (int32_t)((p
->hl
[0]<<24)|
1275 offset
= (int32_t)((p
->hl
[0]<<24)|
1282 offset
= (int32_t)((p
->hl
[0]<<24)|
1289 offset
= (int32_t)((p
->hl
[0]<<24)|
1296 offset
= (int32_t)((p
->hl
[0]<<24)|
1302 case FILE_OPMULTIPLY
:
1303 offset
= (int32_t)((p
->hl
[0]<<24)|
1310 offset
= (int32_t)((p
->hl
[0]<<24)|
1317 offset
= (int32_t)((p
->hl
[0]<<24)|
1325 offset
= (int32_t)((p
->hl
[0]<<24)|
1329 if (m
->in_op
& FILE_OPINVERSE
)
1334 if (nbytes
< (offset
+ 4))
1337 switch (m
->in_op
& FILE_OPS_MASK
) {
1339 offset
= (int32_t)((p
->hl
[3]<<24)|
1346 offset
= (int32_t)((p
->hl
[3]<<24)|
1353 offset
= (int32_t)((p
->hl
[3]<<24)|
1360 offset
= (int32_t)((p
->hl
[3]<<24)|
1367 offset
= (int32_t)((p
->hl
[3]<<24)|
1373 case FILE_OPMULTIPLY
:
1374 offset
= (int32_t)((p
->hl
[3]<<24)|
1381 offset
= (int32_t)((p
->hl
[3]<<24)|
1388 offset
= (int32_t)((p
->hl
[3]<<24)|
1396 offset
= (int32_t)((p
->hl
[3]<<24)|
1400 if (m
->in_op
& FILE_OPINVERSE
)
1404 if (nbytes
< (offset
+ 4))
1407 switch (m
->in_op
& FILE_OPS_MASK
) {
1409 offset
= (int32_t)((p
->hl
[1]<<24)|
1416 offset
= (int32_t)((p
->hl
[1]<<24)|
1423 offset
= (int32_t)((p
->hl
[1]<<24)|
1430 offset
= (int32_t)((p
->hl
[1]<<24)|
1437 offset
= (int32_t)((p
->hl
[1]<<24)|
1443 case FILE_OPMULTIPLY
:
1444 offset
= (int32_t)((p
->hl
[1]<<24)|
1451 offset
= (int32_t)((p
->hl
[1]<<24)|
1458 offset
= (int32_t)((p
->hl
[1]<<24)|
1466 offset
= (int32_t)((p
->hl
[1]<<24)|
1470 if (m
->in_op
& FILE_OPINVERSE
)
1474 if (nbytes
< (offset
+ 4))
1477 switch (m
->in_op
& FILE_OPS_MASK
) {
1479 offset
= p
->l
& off
;
1482 offset
= p
->l
| off
;
1485 offset
= p
->l
^ off
;
1488 offset
= p
->l
+ off
;
1491 offset
= p
->l
- off
;
1493 case FILE_OPMULTIPLY
:
1494 offset
= p
->l
* off
;
1497 offset
= p
->l
/ off
;
1500 offset
= p
->l
% off
;
1505 if (m
->in_op
& FILE_OPINVERSE
)
1510 switch (m
->in_type
) {
1513 offset
= ((((offset
>> 0) & 0x7f) << 0) |
1514 (((offset
>> 8) & 0x7f) << 7) |
1515 (((offset
>> 16) & 0x7f) << 14) |
1516 (((offset
>> 24) & 0x7f) << 21)) + 10;
1522 if (m
->flag
& INDIROFFADD
) {
1523 offset
+= ms
->c
.li
[cont_level
-1].off
;
1525 if (mcopy(ms
, p
, m
->type
, 0, s
, offset
, nbytes
, count
) == -1)
1527 ms
->offset
= offset
;
1529 if ((ms
->flags
& MAGIC_DEBUG
) != 0) {
1530 mdebug(offset
, (char *)(void *)p
,
1531 sizeof(union VALUETYPE
));
1532 #ifndef COMPILE_ONLY
1538 /* Verify we have enough data to match magic type */
1541 if (nbytes
< (offset
+ 1)) /* should alway be true */
1548 if (nbytes
< (offset
+ 2))
1567 if (nbytes
< (offset
+ 4))
1574 if (nbytes
< (offset
+ 8))
1581 if (nbytes
< (offset
+ m
->vallen
))
1586 if (nbytes
< offset
)
1591 if ((ms
->flags
& (MAGIC_MIME
|MAGIC_APPLE
)) == 0 &&
1592 file_printf(ms
, m
->desc
) == -1)
1594 if (nbytes
< offset
)
1596 return file_softmagic(ms
, s
+ offset
, nbytes
- offset
,
1599 case FILE_DEFAULT
: /* nothing to check */
1603 if (!mconvert(ms
, m
))
1609 file_strncmp(const char *s1
, const char *s2
, size_t len
, uint32_t flags
)
1612 * Convert the source args to unsigned here so that (1) the
1613 * compare will be unsigned as it is in strncmp() and (2) so
1614 * the ctype functions will work correctly without extra
1617 const unsigned char *a
= (const unsigned char *)s1
;
1618 const unsigned char *b
= (const unsigned char *)s2
;
1622 * What we want here is v = strncmp(s1, s2, len),
1623 * but ignoring any nulls.
1626 if (0L == flags
) { /* normal string: do it fast */
1628 if ((v
= *b
++ - *a
++) != '\0')
1631 else { /* combine the others */
1633 if ((flags
& STRING_IGNORE_LOWERCASE
) &&
1635 if ((v
= tolower(*b
++) - *a
++) != '\0')
1638 else if ((flags
& STRING_IGNORE_UPPERCASE
) &&
1640 if ((v
= toupper(*b
++) - *a
++) != '\0')
1643 else if ((flags
& STRING_COMPACT_BLANK
) &&
1646 if (isspace(*b
++)) {
1655 else if ((flags
& STRING_COMPACT_OPTIONAL_BLANK
) &&
1662 if ((v
= *b
++ - *a
++) != '\0')
1671 file_strncmp16(const char *a
, const char *b
, size_t len
, uint32_t flags
)
1674 * XXX - The 16-bit string compare probably needs to be done
1675 * differently, especially if the flags are to be supported.
1676 * At the moment, I am unsure.
1679 return file_strncmp(a
, b
, len
, flags
);
1683 magiccheck(struct magic_set
*ms
, struct magic
*m
)
1685 uint64_t l
= m
->value
.q
;
1690 union VALUETYPE
*p
= &ms
->ms_value
;
1758 file_magerror(ms
, "cannot happen with float: invalid relation `%c'",
1792 file_magerror(ms
, "cannot happen with double: invalid relation `%c'", m
->reln
);
1805 v
= file_strncmp(m
->value
.s
, p
->s
, (size_t)m
->vallen
, m
->str_flags
);
1808 case FILE_BESTRING16
:
1809 case FILE_LESTRING16
:
1811 v
= file_strncmp16(m
->value
.s
, p
->s
, (size_t)m
->vallen
, m
->str_flags
);
1814 case FILE_SEARCH
: { /* search ms->search.s for the string m->value.s */
1818 if (ms
->search
.s
== NULL
)
1821 slen
= MIN(m
->vallen
, sizeof(m
->value
.s
));
1825 for (idx
= 0; m
->str_range
== 0 || idx
< m
->str_range
; idx
++) {
1826 if (slen
+ idx
> ms
->search
.s_len
)
1829 v
= file_strncmp(m
->value
.s
, ms
->search
.s
+ idx
, slen
, m
->str_flags
);
1830 if (v
== 0) { /* found match */
1831 ms
->search
.offset
+= idx
;
1842 if (ms
->search
.s
== NULL
)
1846 rc
= regcomp(&rx
, m
->value
.s
,
1847 REG_EXTENDED
|REG_NEWLINE
|
1848 ((m
->str_flags
& STRING_IGNORE_CASE
) ? REG_ICASE
: 0));
1850 (void)regerror(rc
, &rx
, errmsg
, sizeof(errmsg
));
1851 file_magerror(ms
, "regex error %d, (%s)",
1856 regmatch_t pmatch
[1];
1857 #ifndef REG_STARTEND
1858 #define REG_STARTEND 0
1859 size_t l
= ms
->search
.s_len
- 1;
1860 char c
= ms
->search
.s
[l
];
1861 ((char *)(intptr_t)ms
->search
.s
)[l
] = '\0';
1863 pmatch
[0].rm_so
= 0;
1864 pmatch
[0].rm_eo
= ms
->search
.s_len
;
1866 rc
= regexec(&rx
, (const char *)ms
->search
.s
,
1867 1, pmatch
, REG_STARTEND
);
1868 #if REG_STARTEND == 0
1869 ((char *)(intptr_t)ms
->search
.s
)[l
] = c
;
1873 ms
->search
.s
+= (int)pmatch
[0].rm_so
;
1874 ms
->search
.offset
+= (size_t)pmatch
[0].rm_so
;
1876 (size_t)(pmatch
[0].rm_eo
- pmatch
[0].rm_so
);
1885 (void)regerror(rc
, &rx
, errmsg
, sizeof(errmsg
));
1886 file_magerror(ms
, "regexec error %d, (%s)",
1893 if (v
== (uint64_t)-1)
1900 file_magerror(ms
, "invalid type %d in magiccheck()", m
->type
);
1904 v
= file_signextend(ms
, m
, v
);
1908 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1909 (void) fprintf(stderr
, "%llu == *any* = 1\n",
1910 (unsigned long long)v
);
1916 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1917 (void) fprintf(stderr
, "%llu != %llu = %d\n",
1918 (unsigned long long)v
, (unsigned long long)l
,
1924 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1925 (void) fprintf(stderr
, "%llu == %llu = %d\n",
1926 (unsigned long long)v
, (unsigned long long)l
,
1931 if (m
->flag
& UNSIGNED
) {
1933 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1934 (void) fprintf(stderr
, "%llu > %llu = %d\n",
1935 (unsigned long long)v
,
1936 (unsigned long long)l
, matched
);
1939 matched
= (int64_t) v
> (int64_t) l
;
1940 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1941 (void) fprintf(stderr
, "%lld > %lld = %d\n",
1942 (long long)v
, (long long)l
, matched
);
1947 if (m
->flag
& UNSIGNED
) {
1949 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1950 (void) fprintf(stderr
, "%llu < %llu = %d\n",
1951 (unsigned long long)v
,
1952 (unsigned long long)l
, matched
);
1955 matched
= (int64_t) v
< (int64_t) l
;
1956 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1957 (void) fprintf(stderr
, "%lld < %lld = %d\n",
1958 (long long)v
, (long long)l
, matched
);
1963 matched
= (v
& l
) == l
;
1964 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1965 (void) fprintf(stderr
, "((%llx & %llx) == %llx) = %d\n",
1966 (unsigned long long)v
, (unsigned long long)l
,
1967 (unsigned long long)l
, matched
);
1971 matched
= (v
& l
) != l
;
1972 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1973 (void) fprintf(stderr
, "((%llx & %llx) != %llx) = %d\n",
1974 (unsigned long long)v
, (unsigned long long)l
,
1975 (unsigned long long)l
, matched
);
1980 file_magerror(ms
, "cannot happen: invalid relation `%c'",
1989 handle_annotation(struct magic_set
*ms
, struct magic
*m
)
1991 if (ms
->flags
& MAGIC_APPLE
) {
1992 if (file_printf(ms
, "%.8s", m
->apple
) == -1)
1996 if ((ms
->flags
& MAGIC_MIME_TYPE
) && m
->mimetype
[0]) {
1997 if (file_printf(ms
, "%s", m
->mimetype
) == -1)
2005 print_sep(struct magic_set
*ms
, int firstline
)
2007 if (ms
->flags
& MAGIC_MIME
)
2012 * we found another match
2013 * put a newline and '-' to do some simple formatting
2015 return file_printf(ms
, "\n- ");