1 /* $NetBSD: softmagic.c,v 1.13 2015/01/02 21:15:32 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.206 2015/01/01 17:07:34 christos Exp $")
40 __RCSID("$NetBSD: softmagic.c,v 1.13 2015/01/02 21:15:32 christos Exp $");
51 private int match(struct magic_set
*, struct magic
*, uint32_t,
52 const unsigned char *, size_t, size_t, int, int, int, uint16_t,
53 uint16_t *, int *, int *, int *);
54 private int mget(struct magic_set
*, const unsigned char *,
55 struct magic
*, size_t, size_t, unsigned int, int, int, int, uint16_t,
56 uint16_t *, int *, int *, int *);
57 private int magiccheck(struct magic_set
*, struct magic
*);
58 private int32_t mprint(struct magic_set
*, struct magic
*);
59 private int32_t moffset(struct magic_set
*, struct magic
*);
60 private void mdebug(uint32_t, const char *, size_t);
61 private int mcopy(struct magic_set
*, union VALUETYPE
*, int, int,
62 const unsigned char *, uint32_t, size_t, struct magic
*);
63 private int mconvert(struct magic_set
*, struct magic
*, int);
64 private int print_sep(struct magic_set
*, int);
65 private int handle_annotation(struct magic_set
*, struct magic
*);
66 private void cvt_8(union VALUETYPE
*, const struct magic
*);
67 private void cvt_16(union VALUETYPE
*, const struct magic
*);
68 private void cvt_32(union VALUETYPE
*, const struct magic
*);
69 private void cvt_64(union VALUETYPE
*, const struct magic
*);
71 #define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
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 uint16_t indir_level
, uint16_t *name_count
, int mode
, int text
)
83 int rv
, printed_something
= 0, need_separator
= 0;
86 if (name_count
== NULL
) {
91 for (ml
= ms
->mlist
[0]->next
; ml
!= ms
->mlist
[0]; ml
= ml
->next
)
92 if ((rv
= match(ms
, ml
->magic
, ml
->nmagic
, buf
, nbytes
, 0, mode
,
93 text
, 0, indir_level
, name_count
,
94 &printed_something
, &need_separator
, NULL
)) != 0)
100 #define FILE_FMTDEBUG
102 #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
104 private const char * __attribute__((__format_arg__(3)))
105 file_fmtcheck(struct magic_set
*ms
, const struct magic
*m
, const char *def
,
106 const char *file
, size_t line
)
108 const char *ptr
= fmtcheck(m
->desc
, def
);
111 "%s, %" SIZE_T_FORMAT
"u: format `%s' does not match"
112 " with `%s'", file
, line
, m
->desc
, def
);
116 #define F(a, b, c) fmtcheck((b)->desc, (c))
120 * Go through the whole list, stopping if you find a match. Process all
121 * the continuations of that match before returning.
123 * We support multi-level continuations:
125 * At any time when processing a successful top-level match, there is a
126 * current continuation level; it represents the level of the last
127 * successfully matched continuation.
129 * Continuations above that level are skipped as, if we see one, it
130 * means that the continuation that controls them - i.e, the
131 * lower-level continuation preceding them - failed to match.
133 * Continuations below that level are processed as, if we see one,
134 * it means we've finished processing or skipping higher-level
135 * continuations under the control of a successful or unsuccessful
136 * lower-level continuation, and are now seeing the next lower-level
137 * continuation and should process it. The current continuation
138 * level reverts to the level of the one we're seeing.
140 * Continuations at the current level are processed as, if we see
141 * one, there's no lower-level continuation that may have failed.
143 * If a continuation matches, we bump the current continuation level
144 * so that higher-level continuations are processed.
147 match(struct magic_set
*ms
, struct magic
*magic
, uint32_t nmagic
,
148 const unsigned char *s
, size_t nbytes
, size_t offset
, int mode
, int text
,
149 int flip
, uint16_t indir_level
, uint16_t *name_count
,
150 int *printed_something
, int *need_separator
, int *returnval
)
152 uint32_t magindex
= 0;
153 unsigned int cont_level
= 0;
154 int returnvalv
= 0, e
; /* if a match is found it is set to 1*/
155 int firstline
= 1; /* a flag to print X\n X\n- X */
156 int print
= (ms
->flags
& (MAGIC_MIME
|MAGIC_APPLE
)) == 0;
158 if (returnval
== NULL
)
159 returnval
= &returnvalv
;
161 if (file_check_mem(ms
, cont_level
) == -1)
164 for (magindex
= 0; magindex
< nmagic
; magindex
++) {
166 struct magic
*m
= &magic
[magindex
];
168 if (m
->type
!= FILE_NAME
)
169 if ((IS_STRING(m
->type
) &&
170 #define FLT (STRING_BINTEST | STRING_TEXTTEST)
171 ((text
&& (m
->str_flags
& FLT
) == STRING_BINTEST
) ||
172 (!text
&& (m
->str_flags
& FLT
) == STRING_TEXTTEST
))) ||
173 (m
->flag
& mode
) != mode
) {
175 while (magindex
+ 1 < nmagic
&&
176 magic
[magindex
+ 1].cont_level
!= 0 &&
179 continue; /* Skip to next top-level test*/
182 ms
->offset
= m
->offset
;
183 ms
->line
= m
->lineno
;
185 /* if main entry matches, print it... */
186 switch (mget(ms
, s
, m
, nbytes
, offset
, cont_level
, mode
, text
,
187 flip
, indir_level
, name_count
,
188 printed_something
, need_separator
, returnval
)) {
192 flush
= m
->reln
!= '!';
195 if (m
->type
== FILE_INDIRECT
)
198 switch (magiccheck(ms
, m
)) {
212 * main entry didn't match,
213 * flush its continuations
215 while (magindex
< nmagic
- 1 &&
216 magic
[magindex
+ 1].cont_level
!= 0)
221 if ((e
= handle_annotation(ms
, m
)) != 0) {
223 *printed_something
= 1;
228 * If we are going to print something, we'll need to print
229 * a blank before we print something else.
233 *printed_something
= 1;
234 if (print_sep(ms
, firstline
) == -1)
239 if (print
&& mprint(ms
, m
) == -1)
242 ms
->c
.li
[cont_level
].off
= moffset(ms
, m
);
244 /* and any continuations that match */
245 if (file_check_mem(ms
, ++cont_level
) == -1)
248 while (magindex
+ 1 < nmagic
&&
249 magic
[magindex
+ 1].cont_level
!= 0) {
250 m
= &magic
[++magindex
];
251 ms
->line
= m
->lineno
; /* for messages */
253 if (cont_level
< m
->cont_level
)
255 if (cont_level
> m
->cont_level
) {
257 * We're at the end of the level
258 * "cont_level" continuations.
260 cont_level
= m
->cont_level
;
262 ms
->offset
= m
->offset
;
263 if (m
->flag
& OFFADD
) {
265 ms
->c
.li
[cont_level
- 1].off
;
268 #ifdef ENABLE_CONDITIONALS
269 if (m
->cond
== COND_ELSE
||
270 m
->cond
== COND_ELIF
) {
271 if (ms
->c
.li
[cont_level
].last_match
== 1)
275 switch (mget(ms
, s
, m
, nbytes
, offset
, cont_level
, mode
,
276 text
, flip
, indir_level
, name_count
,
277 printed_something
, need_separator
, returnval
)) {
286 if (m
->type
== FILE_INDIRECT
)
292 switch (flush
? 1 : magiccheck(ms
, m
)) {
296 #ifdef ENABLE_CONDITIONALS
297 ms
->c
.li
[cont_level
].last_match
= 0;
301 #ifdef ENABLE_CONDITIONALS
302 ms
->c
.li
[cont_level
].last_match
= 1;
304 if (m
->type
== FILE_CLEAR
)
305 ms
->c
.li
[cont_level
].got_match
= 0;
306 else if (ms
->c
.li
[cont_level
].got_match
) {
307 if (m
->type
== FILE_DEFAULT
)
310 ms
->c
.li
[cont_level
].got_match
= 1;
311 if ((e
= handle_annotation(ms
, m
)) != 0) {
313 *printed_something
= 1;
318 * If we are going to print something,
319 * make sure that we have a separator first.
322 if (!*printed_something
) {
323 *printed_something
= 1;
324 if (print_sep(ms
, firstline
)
330 * This continuation matched. Print
331 * its message, with a blank before it
332 * if the previous item printed and
333 * this item isn't empty.
335 /* space if previous printed */
337 && ((m
->flag
& NOSPACE
) == 0)
340 file_printf(ms
, " ") == -1)
344 if (print
&& mprint(ms
, m
) == -1)
347 ms
->c
.li
[cont_level
].off
= moffset(ms
, m
);
353 * If we see any continuations
357 if (file_check_mem(ms
, ++cont_level
) == -1)
362 if (*printed_something
) {
367 if ((ms
->flags
& MAGIC_CONTINUE
) == 0 && *printed_something
) {
368 return *returnval
; /* don't keep searching */
371 return *returnval
; /* This is hit if -k is set or there is no match */
375 check_fmt(struct magic_set
*ms
, struct magic
*m
)
380 if (strchr(m
->desc
, '%') == NULL
)
383 rc
= file_regcomp(&rx
, "%[-0-9\\.]*s", REG_EXTENDED
|REG_NOSUB
);
385 file_regerror(&rx
, rc
, ms
);
387 rc
= file_regexec(&rx
, m
->desc
, 0, 0, 0);
395 char * strndup(const char *, size_t);
398 strndup(const char *str
, size_t n
)
403 for (len
= 0; len
< n
&& str
[len
]; len
++)
405 if ((copy
= malloc(len
+ 1)) == NULL
)
407 (void)memcpy(copy
, str
, len
);
411 #endif /* HAVE_STRNDUP */
414 mprint(struct magic_set
*ms
, struct magic
*m
)
420 char buf
[128], tbuf
[26], sbuf
[512];
421 union VALUETYPE
*p
= &ms
->ms_value
;
425 v
= file_signextend(ms
, m
, (uint64_t)p
->b
);
426 switch (check_fmt(ms
, m
)) {
430 (void)snprintf(buf
, sizeof(buf
), "%d",
432 if (file_printf(ms
, F(ms
, m
, "%s"), buf
) == -1)
436 if (file_printf(ms
, F(ms
, m
, "%d"),
437 (unsigned char) v
) == -1)
441 t
= ms
->offset
+ sizeof(char);
447 v
= file_signextend(ms
, m
, (uint64_t)p
->h
);
448 switch (check_fmt(ms
, m
)) {
452 (void)snprintf(buf
, sizeof(buf
), "%u",
454 if (file_printf(ms
, F(ms
, m
, "%s"), buf
) == -1)
458 if (file_printf(ms
, F(ms
, m
, "%u"),
459 (unsigned short) v
) == -1)
463 t
= ms
->offset
+ sizeof(short);
470 v
= file_signextend(ms
, m
, (uint64_t)p
->l
);
471 switch (check_fmt(ms
, m
)) {
475 (void)snprintf(buf
, sizeof(buf
), "%u", (uint32_t) v
);
476 if (file_printf(ms
, F(ms
, m
, "%s"), buf
) == -1)
480 if (file_printf(ms
, F(ms
, m
, "%u"), (uint32_t) v
) == -1)
484 t
= ms
->offset
+ sizeof(int32_t);
490 v
= file_signextend(ms
, m
, p
->q
);
491 switch (check_fmt(ms
, m
)) {
495 (void)snprintf(buf
, sizeof(buf
), "%" INT64_T_FORMAT
"u",
496 (unsigned long long)v
);
497 if (file_printf(ms
, F(ms
, m
, "%s"), buf
) == -1)
501 if (file_printf(ms
, F(ms
, m
, "%" INT64_T_FORMAT
"u"),
502 (unsigned long long) v
) == -1)
506 t
= ms
->offset
+ sizeof(int64_t);
511 case FILE_BESTRING16
:
512 case FILE_LESTRING16
:
513 if (m
->reln
== '=' || m
->reln
== '!') {
514 if (file_printf(ms
, F(ms
, m
, "%s"),
515 file_printable(sbuf
, sizeof(sbuf
), m
->value
.s
))
518 t
= ms
->offset
+ m
->vallen
;
523 /* compute t before we mangle the string? */
524 t
= ms
->offset
+ strlen(str
);
526 if (*m
->value
.s
== '\0')
527 str
[strcspn(str
, "\n")] = '\0';
529 if (m
->str_flags
& STRING_TRIM
) {
531 while (isspace((unsigned char)*str
))
537 while (isspace((unsigned char)*last
))
542 if (file_printf(ms
, F(ms
, m
, "%s"),
543 file_printable(sbuf
, sizeof(sbuf
), str
)) == -1)
546 if (m
->type
== FILE_PSTRING
)
547 t
+= file_pstring_length_size(m
);
555 if (file_printf(ms
, F(ms
, m
, "%s"),
556 file_fmttime(p
->l
+ m
->num_mask
, FILE_T_LOCAL
, tbuf
)) == -1)
558 t
= ms
->offset
+ sizeof(uint32_t);
565 if (file_printf(ms
, F(ms
, m
, "%s"),
566 file_fmttime(p
->l
+ m
->num_mask
, 0, tbuf
)) == -1)
568 t
= ms
->offset
+ sizeof(uint32_t);
574 if (file_printf(ms
, F(ms
, m
, "%s"),
575 file_fmttime(p
->q
+ m
->num_mask
, FILE_T_LOCAL
, tbuf
)) == -1)
577 t
= ms
->offset
+ sizeof(uint64_t);
583 if (file_printf(ms
, F(ms
, m
, "%s"),
584 file_fmttime(p
->q
+ m
->num_mask
, 0, tbuf
)) == -1)
586 t
= ms
->offset
+ sizeof(uint64_t);
592 if (file_printf(ms
, F(ms
, m
, "%s"),
593 file_fmttime(p
->q
+ m
->num_mask
, FILE_T_WINDOWS
, tbuf
)) == -1)
595 t
= ms
->offset
+ sizeof(uint64_t);
602 switch (check_fmt(ms
, m
)) {
606 (void)snprintf(buf
, sizeof(buf
), "%g", vf
);
607 if (file_printf(ms
, F(ms
, m
, "%s"), buf
) == -1)
611 if (file_printf(ms
, F(ms
, m
, "%g"), vf
) == -1)
615 t
= ms
->offset
+ sizeof(float);
622 switch (check_fmt(ms
, m
)) {
626 (void)snprintf(buf
, sizeof(buf
), "%g", vd
);
627 if (file_printf(ms
, F(ms
, m
, "%s"), buf
) == -1)
631 if (file_printf(ms
, F(ms
, m
, "%g"), vd
) == -1)
635 t
= ms
->offset
+ sizeof(double);
642 cp
= strndup((const char *)ms
->search
.s
, ms
->search
.rm_len
);
644 file_oomem(ms
, ms
->search
.rm_len
);
647 rval
= file_printf(ms
, F(ms
, m
, "%s"),
648 file_printable(sbuf
, sizeof(sbuf
), cp
));
654 if ((m
->str_flags
& REGEX_OFFSET_START
))
655 t
= ms
->search
.offset
;
657 t
= ms
->search
.offset
+ ms
->search
.rm_len
;
662 if (file_printf(ms
, F(ms
, m
, "%s"),
663 file_printable(sbuf
, sizeof(sbuf
), m
->value
.s
)) == -1)
665 if ((m
->str_flags
& REGEX_OFFSET_START
))
666 t
= ms
->search
.offset
;
668 t
= ms
->search
.offset
+ m
->vallen
;
673 if (file_printf(ms
, "%s", m
->desc
) == -1)
685 file_magerror(ms
, "invalid m->type (%d) in mprint()", m
->type
);
692 moffset(struct magic_set
*ms
, struct magic
*m
)
696 return CAST(int32_t, (ms
->offset
+ sizeof(char)));
701 return CAST(int32_t, (ms
->offset
+ sizeof(short)));
707 return CAST(int32_t, (ms
->offset
+ sizeof(int32_t)));
712 return CAST(int32_t, (ms
->offset
+ sizeof(int64_t)));
716 case FILE_BESTRING16
:
717 case FILE_LESTRING16
:
718 if (m
->reln
== '=' || m
->reln
== '!')
719 return ms
->offset
+ m
->vallen
;
721 union VALUETYPE
*p
= &ms
->ms_value
;
724 if (*m
->value
.s
== '\0')
725 p
->s
[strcspn(p
->s
, "\n")] = '\0';
726 t
= CAST(uint32_t, (ms
->offset
+ strlen(p
->s
)));
727 if (m
->type
== FILE_PSTRING
)
728 t
+= (uint32_t)file_pstring_length_size(m
);
736 return CAST(int32_t, (ms
->offset
+ sizeof(uint32_t)));
742 return CAST(int32_t, (ms
->offset
+ sizeof(uint32_t)));
747 return CAST(int32_t, (ms
->offset
+ sizeof(uint64_t)));
752 return CAST(int32_t, (ms
->offset
+ sizeof(uint64_t)));
757 return CAST(int32_t, (ms
->offset
+ sizeof(float)));
762 return CAST(int32_t, (ms
->offset
+ sizeof(double)));
765 if ((m
->str_flags
& REGEX_OFFSET_START
) != 0)
766 return CAST(int32_t, ms
->search
.offset
);
768 return CAST(int32_t, (ms
->search
.offset
+
772 if ((m
->str_flags
& REGEX_OFFSET_START
) != 0)
773 return CAST(int32_t, ms
->search
.offset
);
775 return CAST(int32_t, (ms
->search
.offset
+ m
->vallen
));
788 cvt_flip(int type
, int flip
)
806 return FILE_LEQLDATE
;
808 return FILE_LEQWDATE
;
822 return FILE_BEQLDATE
;
824 return FILE_BEQWDATE
;
830 return FILE_LEDOUBLE
;
832 return FILE_BEDOUBLE
;
837 #define DO_CVT(fld, cast) \
839 switch (m->mask_op & FILE_OPS_MASK) { \
841 p->fld &= cast m->num_mask; \
844 p->fld |= cast m->num_mask; \
847 p->fld ^= cast m->num_mask; \
850 p->fld += cast m->num_mask; \
853 p->fld -= cast m->num_mask; \
855 case FILE_OPMULTIPLY: \
856 p->fld *= cast m->num_mask; \
858 case FILE_OPDIVIDE: \
859 p->fld /= cast m->num_mask; \
861 case FILE_OPMODULO: \
862 p->fld %= cast m->num_mask; \
865 if (m->mask_op & FILE_OPINVERSE) \
869 cvt_8(union VALUETYPE
*p
, const struct magic
*m
)
871 DO_CVT(b
, (uint8_t));
875 cvt_16(union VALUETYPE
*p
, const struct magic
*m
)
877 DO_CVT(h
, (uint16_t));
881 cvt_32(union VALUETYPE
*p
, const struct magic
*m
)
883 DO_CVT(l
, (uint32_t));
887 cvt_64(union VALUETYPE
*p
, const struct magic
*m
)
889 DO_CVT(q
, (uint64_t));
892 #define DO_CVT2(fld, cast) \
894 switch (m->mask_op & FILE_OPS_MASK) { \
896 p->fld += cast m->num_mask; \
899 p->fld -= cast m->num_mask; \
901 case FILE_OPMULTIPLY: \
902 p->fld *= cast m->num_mask; \
904 case FILE_OPDIVIDE: \
905 p->fld /= cast m->num_mask; \
910 cvt_float(union VALUETYPE
*p
, const struct magic
*m
)
916 cvt_double(union VALUETYPE
*p
, const struct magic
*m
)
918 DO_CVT2(d
, (double));
922 * Convert the byte order of the data we are looking at
923 * While we're here, let's apply the mask operation
924 * (unless you have a better idea)
927 mconvert(struct magic_set
*ms
, struct magic
*m
, int flip
)
929 union VALUETYPE
*p
= &ms
->ms_value
;
932 switch (type
= cvt_flip(m
->type
, flip
)) {
951 case FILE_BESTRING16
:
952 case FILE_LESTRING16
: {
953 /* Null terminate and eat *trailing* return */
954 p
->s
[sizeof(p
->s
) - 1] = '\0';
958 size_t sz
= file_pstring_length_size(m
);
959 char *ptr1
= p
->s
, *ptr2
= ptr1
+ sz
;
960 size_t len
= file_pstring_get_length(m
, ptr1
);
961 sz
= sizeof(p
->s
) - sz
; /* maximum length of string */
964 * The size of the pascal string length (sz)
965 * is 1, 2, or 4. We need at least 1 byte for NUL
966 * termination, but we've already truncated the
967 * string by p->s, so we need to deduct sz.
968 * Because we can use one of the bytes of the length
969 * after we shifted as NUL termination.
979 p
->h
= (short)((p
->hs
[0]<<8)|(p
->hs
[1]));
986 ((p
->hl
[0]<<24)|(p
->hl
[1]<<16)|(p
->hl
[2]<<8)|(p
->hl
[3]));
987 if (type
== FILE_BELONG
)
995 (((uint64_t)p
->hq
[0]<<56)|((uint64_t)p
->hq
[1]<<48)|
996 ((uint64_t)p
->hq
[2]<<40)|((uint64_t)p
->hq
[3]<<32)|
997 ((uint64_t)p
->hq
[4]<<24)|((uint64_t)p
->hq
[5]<<16)|
998 ((uint64_t)p
->hq
[6]<<8)|((uint64_t)p
->hq
[7]));
999 if (type
== FILE_BEQUAD
)
1003 p
->h
= (short)((p
->hs
[1]<<8)|(p
->hs
[0]));
1010 ((p
->hl
[3]<<24)|(p
->hl
[2]<<16)|(p
->hl
[1]<<8)|(p
->hl
[0]));
1011 if (type
== FILE_LELONG
)
1019 (((uint64_t)p
->hq
[7]<<56)|((uint64_t)p
->hq
[6]<<48)|
1020 ((uint64_t)p
->hq
[5]<<40)|((uint64_t)p
->hq
[4]<<32)|
1021 ((uint64_t)p
->hq
[3]<<24)|((uint64_t)p
->hq
[2]<<16)|
1022 ((uint64_t)p
->hq
[1]<<8)|((uint64_t)p
->hq
[0]));
1023 if (type
== FILE_LEQUAD
)
1030 ((p
->hl
[1]<<24)|(p
->hl
[0]<<16)|(p
->hl
[3]<<8)|(p
->hl
[2]));
1031 if (type
== FILE_MELONG
)
1038 p
->l
= ((uint32_t)p
->hl
[0]<<24)|((uint32_t)p
->hl
[1]<<16)|
1039 ((uint32_t)p
->hl
[2]<<8) |((uint32_t)p
->hl
[3]);
1043 p
->l
= ((uint32_t)p
->hl
[3]<<24)|((uint32_t)p
->hl
[2]<<16)|
1044 ((uint32_t)p
->hl
[1]<<8) |((uint32_t)p
->hl
[0]);
1051 p
->q
= ((uint64_t)p
->hq
[0]<<56)|((uint64_t)p
->hq
[1]<<48)|
1052 ((uint64_t)p
->hq
[2]<<40)|((uint64_t)p
->hq
[3]<<32)|
1053 ((uint64_t)p
->hq
[4]<<24)|((uint64_t)p
->hq
[5]<<16)|
1054 ((uint64_t)p
->hq
[6]<<8) |((uint64_t)p
->hq
[7]);
1058 p
->q
= ((uint64_t)p
->hq
[7]<<56)|((uint64_t)p
->hq
[6]<<48)|
1059 ((uint64_t)p
->hq
[5]<<40)|((uint64_t)p
->hq
[4]<<32)|
1060 ((uint64_t)p
->hq
[3]<<24)|((uint64_t)p
->hq
[2]<<16)|
1061 ((uint64_t)p
->hq
[1]<<8) |((uint64_t)p
->hq
[0]);
1072 file_magerror(ms
, "invalid type %d in mconvert()", m
->type
);
1079 mdebug(uint32_t offset
, const char *str
, size_t len
)
1081 (void) fprintf(stderr
, "mget/%" SIZE_T_FORMAT
"u @%d: ", len
, offset
);
1082 file_showstr(stderr
, str
, len
);
1083 (void) fputc('\n', stderr
);
1084 (void) fputc('\n', stderr
);
1088 mcopy(struct magic_set
*ms
, union VALUETYPE
*p
, int type
, int indir
,
1089 const unsigned char *s
, uint32_t offset
, size_t nbytes
, struct magic
*m
)
1092 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
1093 * anything, but setup pointers into the source
1098 ms
->search
.s
= RCAST(const char *, s
) + offset
;
1099 ms
->search
.s_len
= nbytes
- offset
;
1100 ms
->search
.offset
= offset
;
1106 const char *last
; /* end of search region */
1107 const char *buf
; /* start of search region */
1109 size_t lines
, linecnt
, bytecnt
;
1112 ms
->search
.s_len
= 0;
1113 ms
->search
.s
= NULL
;
1117 if (m
->str_flags
& REGEX_LINE_COUNT
) {
1118 linecnt
= m
->str_range
;
1119 bytecnt
= linecnt
* 80;
1122 bytecnt
= m
->str_range
;
1127 if (bytecnt
> nbytes
)
1130 buf
= RCAST(const char *, s
) + offset
;
1131 end
= last
= RCAST(const char *, s
) + bytecnt
;
1132 /* mget() guarantees buf <= last */
1133 for (lines
= linecnt
, b
= buf
; lines
&& b
< end
&&
1134 ((b
= CAST(const char *,
1135 memchr(c
= b
, '\n', CAST(size_t, (end
- b
)))))
1136 || (b
= CAST(const char *,
1137 memchr(c
, '\r', CAST(size_t, (end
- c
))))));
1140 if (b
[0] == '\r' && b
[1] == '\n')
1144 last
= RCAST(const char *, s
) + bytecnt
;
1147 ms
->search
.s_len
= last
- buf
;
1148 ms
->search
.offset
= offset
;
1149 ms
->search
.rm_len
= 0;
1152 case FILE_BESTRING16
:
1153 case FILE_LESTRING16
: {
1154 const unsigned char *src
= s
+ offset
;
1155 const unsigned char *esrc
= s
+ nbytes
;
1157 char *edst
= &p
->s
[sizeof(p
->s
) - 1];
1159 if (type
== FILE_BESTRING16
)
1162 /* check that offset is within range */
1163 if (offset
>= nbytes
)
1165 for (/*EMPTY*/; src
< esrc
; src
+= 2, dst
++) {
1171 if (type
== FILE_BESTRING16
?
1172 *(src
- 1) != '\0' :
1180 case FILE_STRING
: /* XXX - these two should not need */
1181 case FILE_PSTRING
: /* to copy anything, but do anyway. */
1187 if (offset
>= nbytes
) {
1188 (void)memset(p
, '\0', sizeof(*p
));
1191 if (nbytes
- offset
< sizeof(*p
))
1192 nbytes
= nbytes
- offset
;
1194 nbytes
= sizeof(*p
);
1196 (void)memcpy(p
, s
+ offset
, nbytes
);
1199 * the usefulness of padding with zeroes eludes me, it
1200 * might even cause problems
1202 if (nbytes
< sizeof(*p
))
1203 (void)memset(((char *)(void *)p
) + nbytes
, '\0',
1204 sizeof(*p
) - nbytes
);
1209 mget(struct magic_set
*ms
, const unsigned char *s
, struct magic
*m
,
1210 size_t nbytes
, size_t o
, unsigned int cont_level
, int mode
, int text
,
1211 int flip
, uint16_t indir_level
, uint16_t *name_count
,
1212 int *printed_something
, int *need_separator
, int *returnval
)
1214 uint32_t offset
= ms
->offset
;
1217 int rv
, oneed_separator
, in_type
;
1219 union VALUETYPE
*p
= &ms
->ms_value
;
1222 if (indir_level
>= ms
->indir_max
) {
1223 file_error(ms
, 0, "indirect recursion nesting (%hu) exceeded",
1228 if (*name_count
>= ms
->name_max
) {
1229 file_error(ms
, 0, "name use count (%hu) exceeded",
1234 if (mcopy(ms
, p
, m
->type
, m
->flag
& INDIR
, s
, (uint32_t)(offset
+ o
),
1235 (uint32_t)nbytes
, m
) == -1)
1238 if ((ms
->flags
& MAGIC_DEBUG
) != 0) {
1239 fprintf(stderr
, "mget(type=%d, flag=%x, offset=%u, o=%"
1240 SIZE_T_FORMAT
"u, " "nbytes=%" SIZE_T_FORMAT
1241 "u, il=%hu, nc=%hu)\n",
1242 m
->type
, m
->flag
, offset
, o
, nbytes
,
1243 indir_level
, *name_count
);
1244 mdebug(offset
, (char *)(void *)p
, sizeof(union VALUETYPE
));
1245 #ifndef COMPILE_ONLY
1250 if (m
->flag
& INDIR
) {
1251 int off
= m
->in_offset
;
1252 if (m
->in_op
& FILE_OPINDIRECT
) {
1253 const union VALUETYPE
*q
= CAST(const union VALUETYPE
*,
1254 ((const void *)(s
+ offset
+ off
)));
1255 switch (cvt_flip(m
->in_type
, flip
)) {
1263 off
= (short)((q
->hs
[0]<<8)|(q
->hs
[1]));
1266 off
= (short)((q
->hs
[1]<<8)|(q
->hs
[0]));
1273 off
= (int32_t)((q
->hl
[0]<<24)|(q
->hl
[1]<<16)|
1274 (q
->hl
[2]<<8)|(q
->hl
[3]));
1278 off
= (int32_t)((q
->hl
[3]<<24)|(q
->hl
[2]<<16)|
1279 (q
->hl
[1]<<8)|(q
->hl
[0]));
1282 off
= (int32_t)((q
->hl
[1]<<24)|(q
->hl
[0]<<16)|
1283 (q
->hl
[3]<<8)|(q
->hl
[2]));
1286 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1287 fprintf(stderr
, "indirect offs=%u\n", off
);
1289 switch (in_type
= cvt_flip(m
->in_type
, flip
)) {
1291 if (OFFSET_OOB(nbytes
, offset
, 1))
1294 switch (m
->in_op
& FILE_OPS_MASK
) {
1296 offset
= p
->b
& off
;
1299 offset
= p
->b
| off
;
1302 offset
= p
->b
^ off
;
1305 offset
= p
->b
+ off
;
1308 offset
= p
->b
- off
;
1310 case FILE_OPMULTIPLY
:
1311 offset
= p
->b
* off
;
1314 offset
= p
->b
/ off
;
1317 offset
= p
->b
% off
;
1322 if (m
->in_op
& FILE_OPINVERSE
)
1326 if (OFFSET_OOB(nbytes
, offset
, 2))
1328 lhs
= (p
->hs
[0] << 8) | p
->hs
[1];
1330 switch (m
->in_op
& FILE_OPS_MASK
) {
1346 case FILE_OPMULTIPLY
:
1358 if (m
->in_op
& FILE_OPINVERSE
)
1362 if (OFFSET_OOB(nbytes
, offset
, 2))
1364 lhs
= (p
->hs
[1] << 8) | p
->hs
[0];
1366 switch (m
->in_op
& FILE_OPS_MASK
) {
1382 case FILE_OPMULTIPLY
:
1394 if (m
->in_op
& FILE_OPINVERSE
)
1398 if (OFFSET_OOB(nbytes
, offset
, 2))
1401 switch (m
->in_op
& FILE_OPS_MASK
) {
1403 offset
= p
->h
& off
;
1406 offset
= p
->h
| off
;
1409 offset
= p
->h
^ off
;
1412 offset
= p
->h
+ off
;
1415 offset
= p
->h
- off
;
1417 case FILE_OPMULTIPLY
:
1418 offset
= p
->h
* off
;
1421 offset
= p
->h
/ off
;
1424 offset
= p
->h
% off
;
1430 if (m
->in_op
& FILE_OPINVERSE
)
1435 if (OFFSET_OOB(nbytes
, offset
, 4))
1437 lhs
= (p
->hl
[0] << 24) | (p
->hl
[1] << 16) |
1438 (p
->hl
[2] << 8) | p
->hl
[3];
1440 switch (m
->in_op
& FILE_OPS_MASK
) {
1456 case FILE_OPMULTIPLY
:
1468 if (m
->in_op
& FILE_OPINVERSE
)
1473 if (OFFSET_OOB(nbytes
, offset
, 4))
1475 lhs
= (p
->hl
[3] << 24) | (p
->hl
[2] << 16) |
1476 (p
->hl
[1] << 8) | p
->hl
[0];
1478 switch (m
->in_op
& FILE_OPS_MASK
) {
1494 case FILE_OPMULTIPLY
:
1506 if (m
->in_op
& FILE_OPINVERSE
)
1510 if (OFFSET_OOB(nbytes
, offset
, 4))
1512 lhs
= (p
->hl
[1] << 24) | (p
->hl
[0] << 16) |
1513 (p
->hl
[3] << 8) | p
->hl
[2];
1515 switch (m
->in_op
& FILE_OPS_MASK
) {
1531 case FILE_OPMULTIPLY
:
1543 if (m
->in_op
& FILE_OPINVERSE
)
1547 if (OFFSET_OOB(nbytes
, offset
, 4))
1550 switch (m
->in_op
& FILE_OPS_MASK
) {
1552 offset
= p
->l
& off
;
1555 offset
= p
->l
| off
;
1558 offset
= p
->l
^ off
;
1561 offset
= p
->l
+ off
;
1564 offset
= p
->l
- off
;
1566 case FILE_OPMULTIPLY
:
1567 offset
= p
->l
* off
;
1570 offset
= p
->l
/ off
;
1573 offset
= p
->l
% off
;
1578 if (m
->in_op
& FILE_OPINVERSE
)
1588 offset
= ((((offset
>> 0) & 0x7f) << 0) |
1589 (((offset
>> 8) & 0x7f) << 7) |
1590 (((offset
>> 16) & 0x7f) << 14) |
1591 (((offset
>> 24) & 0x7f) << 21)) + 10;
1597 if (m
->flag
& INDIROFFADD
) {
1598 offset
+= ms
->c
.li
[cont_level
-1].off
;
1600 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1602 "indirect *zero* offset\n");
1605 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1606 fprintf(stderr
, "indirect +offs=%u\n", offset
);
1608 if (mcopy(ms
, p
, m
->type
, 0, s
, offset
, nbytes
, m
) == -1)
1610 ms
->offset
= offset
;
1612 if ((ms
->flags
& MAGIC_DEBUG
) != 0) {
1613 mdebug(offset
, (char *)(void *)p
,
1614 sizeof(union VALUETYPE
));
1615 #ifndef COMPILE_ONLY
1621 /* Verify we have enough data to match magic type */
1624 if (OFFSET_OOB(nbytes
, offset
, 1))
1631 if (OFFSET_OOB(nbytes
, offset
, 2))
1650 if (OFFSET_OOB(nbytes
, offset
, 4))
1657 if (OFFSET_OOB(nbytes
, offset
, 8))
1664 if (OFFSET_OOB(nbytes
, offset
, m
->vallen
))
1669 if (nbytes
< offset
)
1674 if (m
->str_flags
& INDIRECT_RELATIVE
)
1675 offset
+= CAST(uint32_t, o
);
1679 if (nbytes
< offset
)
1682 if ((pb
= file_push_buffer(ms
)) == NULL
)
1685 rv
= file_softmagic(ms
, s
+ offset
, nbytes
- offset
,
1686 indir_level
+ 1, name_count
, BINTEST
, text
);
1688 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
1689 fprintf(stderr
, "indirect @offs=%u[%d]\n", offset
, rv
);
1691 rbuf
= file_pop_buffer(ms
, pb
);
1692 if (rbuf
== NULL
&& ms
->event_flags
& EVENT_HAD_ERR
)
1696 if ((ms
->flags
& (MAGIC_MIME
|MAGIC_APPLE
)) == 0 &&
1697 file_printf(ms
, F(ms
, m
, "%u"), offset
) == -1) {
1701 if (file_printf(ms
, "%s", rbuf
) == -1) {
1710 if (nbytes
< offset
)
1717 if (file_magicfind(ms
, rbuf
, &ml
) == -1) {
1718 file_error(ms
, 0, "cannot find entry `%s'", rbuf
);
1722 oneed_separator
= *need_separator
;
1723 if (m
->flag
& NOSPACE
)
1724 *need_separator
= 0;
1725 rv
= match(ms
, ml
.magic
, ml
.nmagic
, s
, nbytes
, offset
+ o
,
1726 mode
, text
, flip
, indir_level
, name_count
,
1727 printed_something
, need_separator
, returnval
);
1729 *need_separator
= oneed_separator
;
1733 if (file_printf(ms
, "%s", m
->desc
) == -1)
1736 case FILE_DEFAULT
: /* nothing to check */
1741 if (!mconvert(ms
, m
, flip
))
1747 file_strncmp(const char *s1
, const char *s2
, size_t len
, uint32_t flags
)
1750 * Convert the source args to unsigned here so that (1) the
1751 * compare will be unsigned as it is in strncmp() and (2) so
1752 * the ctype functions will work correctly without extra
1755 const unsigned char *a
= (const unsigned char *)s1
;
1756 const unsigned char *b
= (const unsigned char *)s2
;
1760 * What we want here is v = strncmp(s1, s2, len),
1761 * but ignoring any nulls.
1764 if (0L == flags
) { /* normal string: do it fast */
1766 if ((v
= *b
++ - *a
++) != '\0')
1769 else { /* combine the others */
1771 if ((flags
& STRING_IGNORE_LOWERCASE
) &&
1773 if ((v
= tolower(*b
++) - *a
++) != '\0')
1776 else if ((flags
& STRING_IGNORE_UPPERCASE
) &&
1778 if ((v
= toupper(*b
++) - *a
++) != '\0')
1781 else if ((flags
& STRING_COMPACT_WHITESPACE
) &&
1784 if (isspace(*b
++)) {
1794 else if ((flags
& STRING_COMPACT_OPTIONAL_WHITESPACE
) &&
1801 if ((v
= *b
++ - *a
++) != '\0')
1810 file_strncmp16(const char *a
, const char *b
, size_t len
, uint32_t flags
)
1813 * XXX - The 16-bit string compare probably needs to be done
1814 * differently, especially if the flags are to be supported.
1815 * At the moment, I am unsure.
1818 return file_strncmp(a
, b
, len
, flags
);
1822 magiccheck(struct magic_set
*ms
, struct magic
*m
)
1824 uint64_t l
= m
->value
.q
;
1829 union VALUETYPE
*p
= &ms
->ms_value
;
1899 file_magerror(ms
, "cannot happen with float: invalid relation `%c'",
1932 file_magerror(ms
, "cannot happen with double: invalid relation `%c'", m
->reln
);
1946 v
= file_strncmp(m
->value
.s
, p
->s
, (size_t)m
->vallen
, m
->str_flags
);
1949 case FILE_BESTRING16
:
1950 case FILE_LESTRING16
:
1952 v
= file_strncmp16(m
->value
.s
, p
->s
, (size_t)m
->vallen
, m
->str_flags
);
1955 case FILE_SEARCH
: { /* search ms->search.s for the string m->value.s */
1959 if (ms
->search
.s
== NULL
)
1962 slen
= MIN(m
->vallen
, sizeof(m
->value
.s
));
1966 for (idx
= 0; m
->str_range
== 0 || idx
< m
->str_range
; idx
++) {
1967 if (slen
+ idx
> ms
->search
.s_len
)
1970 v
= file_strncmp(m
->value
.s
, ms
->search
.s
+ idx
, slen
,
1972 if (v
== 0) { /* found match */
1973 ms
->search
.offset
+= idx
;
1984 if (ms
->search
.s
== NULL
)
1988 rc
= file_regcomp(&rx
, m
->value
.s
,
1989 REG_EXTENDED
|REG_NEWLINE
|
1990 ((m
->str_flags
& STRING_IGNORE_CASE
) ? REG_ICASE
: 0));
1992 file_regerror(&rx
, rc
, ms
);
1995 regmatch_t pmatch
[1];
1996 size_t slen
= ms
->search
.s_len
;
1997 #ifndef REG_STARTEND
1998 #define REG_STARTEND 0
2001 copy
= malloc(slen
);
2003 file_error(ms
, errno
,
2004 "can't allocate %" SIZE_T_FORMAT
"u bytes",
2008 memcpy(copy
, ms
->search
.s
, slen
);
2009 copy
[--slen
] = '\0';
2012 search
= ms
->search
.s
;
2016 search
= ms
->search
.s
;
2017 pmatch
[0].rm_so
= 0;
2018 pmatch
[0].rm_eo
= slen
;
2020 rc
= file_regexec(&rx
, (const char *)search
,
2021 1, pmatch
, REG_STARTEND
);
2022 #if REG_STARTEND == 0
2027 ms
->search
.s
+= (int)pmatch
[0].rm_so
;
2028 ms
->search
.offset
+= (size_t)pmatch
[0].rm_so
;
2030 (size_t)(pmatch
[0].rm_eo
- pmatch
[0].rm_so
);
2039 file_regerror(&rx
, rc
, ms
);
2045 if (v
== (uint64_t)-1)
2054 file_magerror(ms
, "invalid type %d in magiccheck()", m
->type
);
2058 v
= file_signextend(ms
, m
, v
);
2062 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
2063 (void) fprintf(stderr
, "%" INT64_T_FORMAT
2064 "u == *any* = 1\n", (unsigned long long)v
);
2070 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
2071 (void) fprintf(stderr
, "%" INT64_T_FORMAT
"u != %"
2072 INT64_T_FORMAT
"u = %d\n", (unsigned long long)v
,
2073 (unsigned long long)l
, matched
);
2078 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
2079 (void) fprintf(stderr
, "%" INT64_T_FORMAT
"u == %"
2080 INT64_T_FORMAT
"u = %d\n", (unsigned long long)v
,
2081 (unsigned long long)l
, matched
);
2085 if (m
->flag
& UNSIGNED
) {
2087 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
2088 (void) fprintf(stderr
, "%" INT64_T_FORMAT
2089 "u > %" INT64_T_FORMAT
"u = %d\n",
2090 (unsigned long long)v
,
2091 (unsigned long long)l
, matched
);
2094 matched
= (int64_t) v
> (int64_t) l
;
2095 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
2096 (void) fprintf(stderr
, "%" INT64_T_FORMAT
2097 "d > %" INT64_T_FORMAT
"d = %d\n",
2098 (long long)v
, (long long)l
, matched
);
2103 if (m
->flag
& UNSIGNED
) {
2105 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
2106 (void) fprintf(stderr
, "%" INT64_T_FORMAT
2107 "u < %" INT64_T_FORMAT
"u = %d\n",
2108 (unsigned long long)v
,
2109 (unsigned long long)l
, matched
);
2112 matched
= (int64_t) v
< (int64_t) l
;
2113 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
2114 (void) fprintf(stderr
, "%" INT64_T_FORMAT
2115 "d < %" INT64_T_FORMAT
"d = %d\n",
2116 (long long)v
, (long long)l
, matched
);
2121 matched
= (v
& l
) == l
;
2122 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
2123 (void) fprintf(stderr
, "((%" INT64_T_FORMAT
"x & %"
2124 INT64_T_FORMAT
"x) == %" INT64_T_FORMAT
2125 "x) = %d\n", (unsigned long long)v
,
2126 (unsigned long long)l
, (unsigned long long)l
,
2131 matched
= (v
& l
) != l
;
2132 if ((ms
->flags
& MAGIC_DEBUG
) != 0)
2133 (void) fprintf(stderr
, "((%" INT64_T_FORMAT
"x & %"
2134 INT64_T_FORMAT
"x) != %" INT64_T_FORMAT
2135 "x) = %d\n", (unsigned long long)v
,
2136 (unsigned long long)l
, (unsigned long long)l
,
2141 file_magerror(ms
, "cannot happen: invalid relation `%c'",
2150 handle_annotation(struct magic_set
*ms
, struct magic
*m
)
2152 if (ms
->flags
& MAGIC_APPLE
) {
2153 if (file_printf(ms
, "%.8s", m
->apple
) == -1)
2157 if ((ms
->flags
& MAGIC_MIME_TYPE
) && m
->mimetype
[0]) {
2158 if (file_printf(ms
, "%s", m
->mimetype
) == -1)
2166 print_sep(struct magic_set
*ms
, int firstline
)
2168 if (ms
->flags
& MAGIC_MIME
)
2173 * we found another match
2174 * put a newline and '-' to do some simple formatting
2176 return file_printf(ms
, "\n- ");