xref: /freebsd/contrib/file/src/apprentice.c (revision 58a0f0d00c0cc4a90ce584a61470290751bfcac7)
1b6cee71dSXin LI /*
2b6cee71dSXin LI  * Copyright (c) Ian F. Darwin 1986-1995.
3b6cee71dSXin LI  * Software written by Ian F. Darwin and others;
4b6cee71dSXin LI  * maintained 1995-present by Christos Zoulas and others.
5b6cee71dSXin LI  *
6b6cee71dSXin LI  * Redistribution and use in source and binary forms, with or without
7b6cee71dSXin LI  * modification, are permitted provided that the following conditions
8b6cee71dSXin LI  * are met:
9b6cee71dSXin LI  * 1. Redistributions of source code must retain the above copyright
10b6cee71dSXin LI  *    notice immediately at the beginning of the file, without modification,
11b6cee71dSXin LI  *    this list of conditions, and the following disclaimer.
12b6cee71dSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
13b6cee71dSXin LI  *    notice, this list of conditions and the following disclaimer in the
14b6cee71dSXin LI  *    documentation and/or other materials provided with the distribution.
15b6cee71dSXin LI  *
16b6cee71dSXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b6cee71dSXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b6cee71dSXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b6cee71dSXin LI  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20b6cee71dSXin LI  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b6cee71dSXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b6cee71dSXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b6cee71dSXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b6cee71dSXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b6cee71dSXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b6cee71dSXin LI  * SUCH DAMAGE.
27b6cee71dSXin LI  */
28b6cee71dSXin LI /*
29b6cee71dSXin LI  * apprentice - make one pass through /etc/magic, learning its secrets.
30b6cee71dSXin LI  */
31b6cee71dSXin LI 
32b6cee71dSXin LI #include "file.h"
33b6cee71dSXin LI 
34b6cee71dSXin LI #ifndef	lint
35*58a0f0d0SEitan Adler FILE_RCSID("@(#)$File: apprentice.c,v 1.270 2018/02/21 21:26:48 christos Exp $")
36b6cee71dSXin LI #endif	/* lint */
37b6cee71dSXin LI 
38b6cee71dSXin LI #include "magic.h"
39b6cee71dSXin LI #include <stdlib.h>
40b6cee71dSXin LI #ifdef HAVE_UNISTD_H
41b6cee71dSXin LI #include <unistd.h>
42b6cee71dSXin LI #endif
43b6cee71dSXin LI #ifdef HAVE_STDDEF_H
44b6cee71dSXin LI #include <stddef.h>
45b6cee71dSXin LI #endif
46b6cee71dSXin LI #include <string.h>
47b6cee71dSXin LI #include <assert.h>
48b6cee71dSXin LI #include <ctype.h>
49b6cee71dSXin LI #include <fcntl.h>
50b6cee71dSXin LI #ifdef QUICK
51b6cee71dSXin LI #include <sys/mman.h>
52b6cee71dSXin LI #endif
53b6cee71dSXin LI #include <dirent.h>
54b6cee71dSXin LI #if defined(HAVE_LIMITS_H)
55b6cee71dSXin LI #include <limits.h>
56b6cee71dSXin LI #endif
57b6cee71dSXin LI 
58b6cee71dSXin LI #ifndef SSIZE_MAX
59b6cee71dSXin LI #define MAXMAGIC_SIZE        ((ssize_t)0x7fffffff)
60b6cee71dSXin LI #else
61b6cee71dSXin LI #define MAXMAGIC_SIZE        SSIZE_MAX
62b6cee71dSXin LI #endif
63b6cee71dSXin LI 
64b6cee71dSXin LI #define	EATAB {while (isascii((unsigned char) *l) && \
65b6cee71dSXin LI 		      isspace((unsigned char) *l))  ++l;}
66b6cee71dSXin LI #define LOWCASE(l) (isupper((unsigned char) (l)) ? \
67b6cee71dSXin LI 			tolower((unsigned char) (l)) : (l))
68b6cee71dSXin LI /*
69b6cee71dSXin LI  * Work around a bug in headers on Digital Unix.
70b6cee71dSXin LI  * At least confirmed for: OSF1 V4.0 878
71b6cee71dSXin LI  */
72b6cee71dSXin LI #if defined(__osf__) && defined(__DECC)
73b6cee71dSXin LI #ifdef MAP_FAILED
74b6cee71dSXin LI #undef MAP_FAILED
75b6cee71dSXin LI #endif
76b6cee71dSXin LI #endif
77b6cee71dSXin LI 
78b6cee71dSXin LI #ifndef MAP_FAILED
79b6cee71dSXin LI #define MAP_FAILED (void *) -1
80b6cee71dSXin LI #endif
81b6cee71dSXin LI 
82b6cee71dSXin LI #ifndef MAP_FILE
83b6cee71dSXin LI #define MAP_FILE 0
84b6cee71dSXin LI #endif
85b6cee71dSXin LI 
86b6cee71dSXin LI #define ALLOC_CHUNK	(size_t)10
87b6cee71dSXin LI #define ALLOC_INCR	(size_t)200
88b6cee71dSXin LI 
893e41d09dSXin LI #define MAP_TYPE_USER	0
90c2931133SXin LI #define MAP_TYPE_MALLOC	1
913e41d09dSXin LI #define MAP_TYPE_MMAP	2
92c2931133SXin LI 
93b6cee71dSXin LI struct magic_entry {
94b6cee71dSXin LI 	struct magic *mp;
95b6cee71dSXin LI 	uint32_t cont_count;
96b6cee71dSXin LI 	uint32_t max_count;
97b6cee71dSXin LI };
98b6cee71dSXin LI 
99b6cee71dSXin LI struct magic_entry_set {
100b6cee71dSXin LI 	struct magic_entry *me;
101b6cee71dSXin LI 	uint32_t count;
102b6cee71dSXin LI 	uint32_t max;
103b6cee71dSXin LI };
104b6cee71dSXin LI 
105b6cee71dSXin LI struct magic_map {
106b6cee71dSXin LI 	void *p;
107b6cee71dSXin LI 	size_t len;
108c2931133SXin LI 	int type;
109b6cee71dSXin LI 	struct magic *magic[MAGIC_SETS];
110b6cee71dSXin LI 	uint32_t nmagic[MAGIC_SETS];
111b6cee71dSXin LI };
112b6cee71dSXin LI 
113b6cee71dSXin LI int file_formats[FILE_NAMES_SIZE];
114b6cee71dSXin LI const size_t file_nformats = FILE_NAMES_SIZE;
115b6cee71dSXin LI const char *file_names[FILE_NAMES_SIZE];
116b6cee71dSXin LI const size_t file_nnames = FILE_NAMES_SIZE;
117b6cee71dSXin LI 
118b6cee71dSXin LI private int getvalue(struct magic_set *ms, struct magic *, const char **, int);
119b6cee71dSXin LI private int hextoint(int);
120b6cee71dSXin LI private const char *getstr(struct magic_set *, struct magic *, const char *,
121b6cee71dSXin LI     int);
122b6cee71dSXin LI private int parse(struct magic_set *, struct magic_entry *, const char *,
123b6cee71dSXin LI     size_t, int);
124b6cee71dSXin LI private void eatsize(const char **);
125b6cee71dSXin LI private int apprentice_1(struct magic_set *, const char *, int);
126b6cee71dSXin LI private size_t apprentice_magic_strength(const struct magic *);
127b6cee71dSXin LI private int apprentice_sort(const void *, const void *);
128b6cee71dSXin LI private void apprentice_list(struct mlist *, int );
129b6cee71dSXin LI private struct magic_map *apprentice_load(struct magic_set *,
130b6cee71dSXin LI     const char *, int);
131b6cee71dSXin LI private struct mlist *mlist_alloc(void);
132b6cee71dSXin LI private void mlist_free(struct mlist *);
133b6cee71dSXin LI private void byteswap(struct magic *, uint32_t);
134b6cee71dSXin LI private void bs1(struct magic *);
135b6cee71dSXin LI private uint16_t swap2(uint16_t);
136b6cee71dSXin LI private uint32_t swap4(uint32_t);
137b6cee71dSXin LI private uint64_t swap8(uint64_t);
138b6cee71dSXin LI private char *mkdbname(struct magic_set *, const char *, int);
139c2931133SXin LI private struct magic_map *apprentice_buf(struct magic_set *, struct magic *,
140c2931133SXin LI     size_t);
141b6cee71dSXin LI private struct magic_map *apprentice_map(struct magic_set *, const char *);
142c2931133SXin LI private int check_buffer(struct magic_set *, struct magic_map *, const char *);
143b6cee71dSXin LI private void apprentice_unmap(struct magic_map *);
144b6cee71dSXin LI private int apprentice_compile(struct magic_set *, struct magic_map *,
145b6cee71dSXin LI     const char *);
1463e41d09dSXin LI private int check_format_type(const char *, int, const char **);
147b6cee71dSXin LI private int check_format(struct magic_set *, struct magic *);
148b6cee71dSXin LI private int get_op(char);
149b6cee71dSXin LI private int parse_mime(struct magic_set *, struct magic_entry *, const char *);
150b6cee71dSXin LI private int parse_strength(struct magic_set *, struct magic_entry *, const char *);
151b6cee71dSXin LI private int parse_apple(struct magic_set *, struct magic_entry *, const char *);
1525f0216bdSXin LI private int parse_ext(struct magic_set *, struct magic_entry *, const char *);
153b6cee71dSXin LI 
154b6cee71dSXin LI 
155b6cee71dSXin LI private size_t magicsize = sizeof(struct magic);
156b6cee71dSXin LI 
157b6cee71dSXin LI private const char usg_hdr[] = "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
158b6cee71dSXin LI 
159b6cee71dSXin LI private struct {
160b6cee71dSXin LI 	const char *name;
161b6cee71dSXin LI 	size_t len;
162b6cee71dSXin LI 	int (*fun)(struct magic_set *, struct magic_entry *, const char *);
163b6cee71dSXin LI } bang[] = {
164b6cee71dSXin LI #define	DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name }
165b6cee71dSXin LI 	DECLARE_FIELD(mime),
166b6cee71dSXin LI 	DECLARE_FIELD(apple),
1675f0216bdSXin LI 	DECLARE_FIELD(ext),
168b6cee71dSXin LI 	DECLARE_FIELD(strength),
169b6cee71dSXin LI #undef	DECLARE_FIELD
170b6cee71dSXin LI 	{ NULL, 0, NULL }
171b6cee71dSXin LI };
172b6cee71dSXin LI 
173b6cee71dSXin LI #ifdef COMPILE_ONLY
174b6cee71dSXin LI 
175b6cee71dSXin LI int main(int, char *[]);
176b6cee71dSXin LI 
177b6cee71dSXin LI int
178b6cee71dSXin LI main(int argc, char *argv[])
179b6cee71dSXin LI {
180b6cee71dSXin LI 	int ret;
181b6cee71dSXin LI 	struct magic_set *ms;
182b6cee71dSXin LI 	char *progname;
183b6cee71dSXin LI 
184b6cee71dSXin LI 	if ((progname = strrchr(argv[0], '/')) != NULL)
185b6cee71dSXin LI 		progname++;
186b6cee71dSXin LI 	else
187b6cee71dSXin LI 		progname = argv[0];
188b6cee71dSXin LI 
189b6cee71dSXin LI 	if (argc != 2) {
190b6cee71dSXin LI 		(void)fprintf(stderr, "Usage: %s file\n", progname);
191b6cee71dSXin LI 		return 1;
192b6cee71dSXin LI 	}
193b6cee71dSXin LI 
194b6cee71dSXin LI 	if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
195b6cee71dSXin LI 		(void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
196b6cee71dSXin LI 		return 1;
197b6cee71dSXin LI 	}
198b6cee71dSXin LI 	ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
199b6cee71dSXin LI 	if (ret == 1)
200b6cee71dSXin LI 		(void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
201b6cee71dSXin LI 	magic_close(ms);
202b6cee71dSXin LI 	return ret;
203b6cee71dSXin LI }
204b6cee71dSXin LI #endif /* COMPILE_ONLY */
205b6cee71dSXin LI 
206b6cee71dSXin LI struct type_tbl_s {
207b6cee71dSXin LI 	const char name[16];
208b6cee71dSXin LI 	const size_t len;
209b6cee71dSXin LI 	const int type;
210b6cee71dSXin LI 	const int format;
211b6cee71dSXin LI };
212b6cee71dSXin LI 
213b6cee71dSXin LI /*
214b6cee71dSXin LI  * XXX - the actual Single UNIX Specification says that "long" means "long",
215b6cee71dSXin LI  * as in the C data type, but we treat it as meaning "4-byte integer".
216b6cee71dSXin LI  * Given that the OS X version of file 5.04 did the same, I guess that passes
217b6cee71dSXin LI  * the actual test; having "long" be dependent on how big a "long" is on
218b6cee71dSXin LI  * the machine running "file" is silly.
219b6cee71dSXin LI  */
220b6cee71dSXin LI static const struct type_tbl_s type_tbl[] = {
221b6cee71dSXin LI # define XX(s)		s, (sizeof(s) - 1)
222b6cee71dSXin LI # define XX_NULL	"", 0
223b6cee71dSXin LI 	{ XX("invalid"),	FILE_INVALID,		FILE_FMT_NONE },
224b6cee71dSXin LI 	{ XX("byte"),		FILE_BYTE,		FILE_FMT_NUM },
225b6cee71dSXin LI 	{ XX("short"),		FILE_SHORT,		FILE_FMT_NUM },
226b6cee71dSXin LI 	{ XX("default"),	FILE_DEFAULT,		FILE_FMT_NONE },
227b6cee71dSXin LI 	{ XX("long"),		FILE_LONG,		FILE_FMT_NUM },
228b6cee71dSXin LI 	{ XX("string"),		FILE_STRING,		FILE_FMT_STR },
229b6cee71dSXin LI 	{ XX("date"),		FILE_DATE,		FILE_FMT_STR },
230b6cee71dSXin LI 	{ XX("beshort"),	FILE_BESHORT,		FILE_FMT_NUM },
231b6cee71dSXin LI 	{ XX("belong"),		FILE_BELONG,		FILE_FMT_NUM },
232b6cee71dSXin LI 	{ XX("bedate"),		FILE_BEDATE,		FILE_FMT_STR },
233b6cee71dSXin LI 	{ XX("leshort"),	FILE_LESHORT,		FILE_FMT_NUM },
234b6cee71dSXin LI 	{ XX("lelong"),		FILE_LELONG,		FILE_FMT_NUM },
235b6cee71dSXin LI 	{ XX("ledate"),		FILE_LEDATE,		FILE_FMT_STR },
236b6cee71dSXin LI 	{ XX("pstring"),	FILE_PSTRING,		FILE_FMT_STR },
237b6cee71dSXin LI 	{ XX("ldate"),		FILE_LDATE,		FILE_FMT_STR },
238b6cee71dSXin LI 	{ XX("beldate"),	FILE_BELDATE,		FILE_FMT_STR },
239b6cee71dSXin LI 	{ XX("leldate"),	FILE_LELDATE,		FILE_FMT_STR },
240b6cee71dSXin LI 	{ XX("regex"),		FILE_REGEX,		FILE_FMT_STR },
241b6cee71dSXin LI 	{ XX("bestring16"),	FILE_BESTRING16,	FILE_FMT_STR },
242b6cee71dSXin LI 	{ XX("lestring16"),	FILE_LESTRING16,	FILE_FMT_STR },
243b6cee71dSXin LI 	{ XX("search"),		FILE_SEARCH,		FILE_FMT_STR },
244b6cee71dSXin LI 	{ XX("medate"),		FILE_MEDATE,		FILE_FMT_STR },
245b6cee71dSXin LI 	{ XX("meldate"),	FILE_MELDATE,		FILE_FMT_STR },
246b6cee71dSXin LI 	{ XX("melong"),		FILE_MELONG,		FILE_FMT_NUM },
247b6cee71dSXin LI 	{ XX("quad"),		FILE_QUAD,		FILE_FMT_QUAD },
248b6cee71dSXin LI 	{ XX("lequad"),		FILE_LEQUAD,		FILE_FMT_QUAD },
249b6cee71dSXin LI 	{ XX("bequad"),		FILE_BEQUAD,		FILE_FMT_QUAD },
250b6cee71dSXin LI 	{ XX("qdate"),		FILE_QDATE,		FILE_FMT_STR },
251b6cee71dSXin LI 	{ XX("leqdate"),	FILE_LEQDATE,		FILE_FMT_STR },
252b6cee71dSXin LI 	{ XX("beqdate"),	FILE_BEQDATE,		FILE_FMT_STR },
253b6cee71dSXin LI 	{ XX("qldate"),		FILE_QLDATE,		FILE_FMT_STR },
254b6cee71dSXin LI 	{ XX("leqldate"),	FILE_LEQLDATE,		FILE_FMT_STR },
255b6cee71dSXin LI 	{ XX("beqldate"),	FILE_BEQLDATE,		FILE_FMT_STR },
256b6cee71dSXin LI 	{ XX("float"),		FILE_FLOAT,		FILE_FMT_FLOAT },
257b6cee71dSXin LI 	{ XX("befloat"),	FILE_BEFLOAT,		FILE_FMT_FLOAT },
258b6cee71dSXin LI 	{ XX("lefloat"),	FILE_LEFLOAT,		FILE_FMT_FLOAT },
259b6cee71dSXin LI 	{ XX("double"),		FILE_DOUBLE,		FILE_FMT_DOUBLE },
260b6cee71dSXin LI 	{ XX("bedouble"),	FILE_BEDOUBLE,		FILE_FMT_DOUBLE },
261b6cee71dSXin LI 	{ XX("ledouble"),	FILE_LEDOUBLE,		FILE_FMT_DOUBLE },
262b6cee71dSXin LI 	{ XX("leid3"),		FILE_LEID3,		FILE_FMT_NUM },
263b6cee71dSXin LI 	{ XX("beid3"),		FILE_BEID3,		FILE_FMT_NUM },
264b6cee71dSXin LI 	{ XX("indirect"),	FILE_INDIRECT,		FILE_FMT_NUM },
265b6cee71dSXin LI 	{ XX("qwdate"),		FILE_QWDATE,		FILE_FMT_STR },
266b6cee71dSXin LI 	{ XX("leqwdate"),	FILE_LEQWDATE,		FILE_FMT_STR },
267b6cee71dSXin LI 	{ XX("beqwdate"),	FILE_BEQWDATE,		FILE_FMT_STR },
268b6cee71dSXin LI 	{ XX("name"),		FILE_NAME,		FILE_FMT_NONE },
269b6cee71dSXin LI 	{ XX("use"),		FILE_USE,		FILE_FMT_NONE },
270b6cee71dSXin LI 	{ XX("clear"),		FILE_CLEAR,		FILE_FMT_NONE },
2713e41d09dSXin LI 	{ XX("der"),		FILE_DER,		FILE_FMT_STR },
272b6cee71dSXin LI 	{ XX_NULL,		FILE_INVALID,		FILE_FMT_NONE },
273b6cee71dSXin LI };
274b6cee71dSXin LI 
275b6cee71dSXin LI /*
276b6cee71dSXin LI  * These are not types, and cannot be preceded by "u" to make them
277b6cee71dSXin LI  * unsigned.
278b6cee71dSXin LI  */
279b6cee71dSXin LI static const struct type_tbl_s special_tbl[] = {
2803e41d09dSXin LI 	{ XX("der"),		FILE_DER,		FILE_FMT_STR },
281b6cee71dSXin LI 	{ XX("name"),		FILE_NAME,		FILE_FMT_STR },
282b6cee71dSXin LI 	{ XX("use"),		FILE_USE,		FILE_FMT_STR },
283b6cee71dSXin LI 	{ XX_NULL,		FILE_INVALID,		FILE_FMT_NONE },
284b6cee71dSXin LI };
285b6cee71dSXin LI # undef XX
286b6cee71dSXin LI # undef XX_NULL
287b6cee71dSXin LI 
288b6cee71dSXin LI private int
289b6cee71dSXin LI get_type(const struct type_tbl_s *tbl, const char *l, const char **t)
290b6cee71dSXin LI {
291b6cee71dSXin LI 	const struct type_tbl_s *p;
292b6cee71dSXin LI 
293b6cee71dSXin LI 	for (p = tbl; p->len; p++) {
294b6cee71dSXin LI 		if (strncmp(l, p->name, p->len) == 0) {
295b6cee71dSXin LI 			if (t)
296b6cee71dSXin LI 				*t = l + p->len;
297b6cee71dSXin LI 			break;
298b6cee71dSXin LI 		}
299b6cee71dSXin LI 	}
300b6cee71dSXin LI 	return p->type;
301b6cee71dSXin LI }
302b6cee71dSXin LI 
303b6cee71dSXin LI private int
304b6cee71dSXin LI get_standard_integer_type(const char *l, const char **t)
305b6cee71dSXin LI {
306b6cee71dSXin LI 	int type;
307b6cee71dSXin LI 
308b6cee71dSXin LI 	if (isalpha((unsigned char)l[1])) {
309b6cee71dSXin LI 		switch (l[1]) {
310b6cee71dSXin LI 		case 'C':
311b6cee71dSXin LI 			/* "dC" and "uC" */
312b6cee71dSXin LI 			type = FILE_BYTE;
313b6cee71dSXin LI 			break;
314b6cee71dSXin LI 		case 'S':
315b6cee71dSXin LI 			/* "dS" and "uS" */
316b6cee71dSXin LI 			type = FILE_SHORT;
317b6cee71dSXin LI 			break;
318b6cee71dSXin LI 		case 'I':
319b6cee71dSXin LI 		case 'L':
320b6cee71dSXin LI 			/*
321b6cee71dSXin LI 			 * "dI", "dL", "uI", and "uL".
322b6cee71dSXin LI 			 *
323b6cee71dSXin LI 			 * XXX - the actual Single UNIX Specification says
324b6cee71dSXin LI 			 * that "L" means "long", as in the C data type,
325b6cee71dSXin LI 			 * but we treat it as meaning "4-byte integer".
326b6cee71dSXin LI 			 * Given that the OS X version of file 5.04 did
327b6cee71dSXin LI 			 * the same, I guess that passes the actual SUS
328b6cee71dSXin LI 			 * validation suite; having "dL" be dependent on
329b6cee71dSXin LI 			 * how big a "long" is on the machine running
330b6cee71dSXin LI 			 * "file" is silly.
331b6cee71dSXin LI 			 */
332b6cee71dSXin LI 			type = FILE_LONG;
333b6cee71dSXin LI 			break;
334b6cee71dSXin LI 		case 'Q':
335b6cee71dSXin LI 			/* "dQ" and "uQ" */
336b6cee71dSXin LI 			type = FILE_QUAD;
337b6cee71dSXin LI 			break;
338b6cee71dSXin LI 		default:
339b6cee71dSXin LI 			/* "d{anything else}", "u{anything else}" */
340b6cee71dSXin LI 			return FILE_INVALID;
341b6cee71dSXin LI 		}
342b6cee71dSXin LI 		l += 2;
343b6cee71dSXin LI 	} else if (isdigit((unsigned char)l[1])) {
344b6cee71dSXin LI 		/*
345b6cee71dSXin LI 		 * "d{num}" and "u{num}"; we only support {num} values
346b6cee71dSXin LI 		 * of 1, 2, 4, and 8 - the Single UNIX Specification
347b6cee71dSXin LI 		 * doesn't say anything about whether arbitrary
348b6cee71dSXin LI 		 * values should be supported, but both the Solaris 10
349b6cee71dSXin LI 		 * and OS X Mountain Lion versions of file passed the
350b6cee71dSXin LI 		 * Single UNIX Specification validation suite, and
351b6cee71dSXin LI 		 * neither of them support values bigger than 8 or
352b6cee71dSXin LI 		 * non-power-of-2 values.
353b6cee71dSXin LI 		 */
354b6cee71dSXin LI 		if (isdigit((unsigned char)l[2])) {
355b6cee71dSXin LI 			/* Multi-digit, so > 9 */
356b6cee71dSXin LI 			return FILE_INVALID;
357b6cee71dSXin LI 		}
358b6cee71dSXin LI 		switch (l[1]) {
359b6cee71dSXin LI 		case '1':
360b6cee71dSXin LI 			type = FILE_BYTE;
361b6cee71dSXin LI 			break;
362b6cee71dSXin LI 		case '2':
363b6cee71dSXin LI 			type = FILE_SHORT;
364b6cee71dSXin LI 			break;
365b6cee71dSXin LI 		case '4':
366b6cee71dSXin LI 			type = FILE_LONG;
367b6cee71dSXin LI 			break;
368b6cee71dSXin LI 		case '8':
369b6cee71dSXin LI 			type = FILE_QUAD;
370b6cee71dSXin LI 			break;
371b6cee71dSXin LI 		default:
372b6cee71dSXin LI 			/* XXX - what about 3, 5, 6, or 7? */
373b6cee71dSXin LI 			return FILE_INVALID;
374b6cee71dSXin LI 		}
375b6cee71dSXin LI 		l += 2;
376b6cee71dSXin LI 	} else {
377b6cee71dSXin LI 		/*
378b6cee71dSXin LI 		 * "d" or "u" by itself.
379b6cee71dSXin LI 		 */
380b6cee71dSXin LI 		type = FILE_LONG;
381b6cee71dSXin LI 		++l;
382b6cee71dSXin LI 	}
383b6cee71dSXin LI 	if (t)
384b6cee71dSXin LI 		*t = l;
385b6cee71dSXin LI 	return type;
386b6cee71dSXin LI }
387b6cee71dSXin LI 
388b6cee71dSXin LI private void
389b6cee71dSXin LI init_file_tables(void)
390b6cee71dSXin LI {
391b6cee71dSXin LI 	static int done = 0;
392b6cee71dSXin LI 	const struct type_tbl_s *p;
393b6cee71dSXin LI 
394b6cee71dSXin LI 	if (done)
395b6cee71dSXin LI 		return;
396b6cee71dSXin LI 	done++;
397b6cee71dSXin LI 
398b6cee71dSXin LI 	for (p = type_tbl; p->len; p++) {
399b6cee71dSXin LI 		assert(p->type < FILE_NAMES_SIZE);
400b6cee71dSXin LI 		file_names[p->type] = p->name;
401b6cee71dSXin LI 		file_formats[p->type] = p->format;
402b6cee71dSXin LI 	}
403b6cee71dSXin LI 	assert(p - type_tbl == FILE_NAMES_SIZE);
404b6cee71dSXin LI }
405b6cee71dSXin LI 
406b6cee71dSXin LI private int
407b6cee71dSXin LI add_mlist(struct mlist *mlp, struct magic_map *map, size_t idx)
408b6cee71dSXin LI {
409b6cee71dSXin LI 	struct mlist *ml;
410b6cee71dSXin LI 
411a5d223e6SXin LI 	mlp->map = NULL;
412b6cee71dSXin LI 	if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL)
413b6cee71dSXin LI 		return -1;
414b6cee71dSXin LI 
415a5d223e6SXin LI 	ml->map = idx == 0 ? map : NULL;
416b6cee71dSXin LI 	ml->magic = map->magic[idx];
417b6cee71dSXin LI 	ml->nmagic = map->nmagic[idx];
418b6cee71dSXin LI 
419b6cee71dSXin LI 	mlp->prev->next = ml;
420b6cee71dSXin LI 	ml->prev = mlp->prev;
421b6cee71dSXin LI 	ml->next = mlp;
422b6cee71dSXin LI 	mlp->prev = ml;
423b6cee71dSXin LI 	return 0;
424b6cee71dSXin LI }
425b6cee71dSXin LI 
426b6cee71dSXin LI /*
427b6cee71dSXin LI  * Handle one file or directory.
428b6cee71dSXin LI  */
429b6cee71dSXin LI private int
430b6cee71dSXin LI apprentice_1(struct magic_set *ms, const char *fn, int action)
431b6cee71dSXin LI {
432b6cee71dSXin LI 	struct magic_map *map;
433b6cee71dSXin LI #ifndef COMPILE_ONLY
434c2931133SXin LI 	struct mlist *ml;
435b6cee71dSXin LI 	size_t i;
436c2931133SXin LI #endif
437b6cee71dSXin LI 
438b6cee71dSXin LI 	if (magicsize != FILE_MAGICSIZE) {
439b6cee71dSXin LI 		file_error(ms, 0, "magic element size %lu != %lu",
440b6cee71dSXin LI 		    (unsigned long)sizeof(*map->magic[0]),
441b6cee71dSXin LI 		    (unsigned long)FILE_MAGICSIZE);
442b6cee71dSXin LI 		return -1;
443b6cee71dSXin LI 	}
444b6cee71dSXin LI 
445b6cee71dSXin LI 	if (action == FILE_COMPILE) {
446b6cee71dSXin LI 		map = apprentice_load(ms, fn, action);
447b6cee71dSXin LI 		if (map == NULL)
448b6cee71dSXin LI 			return -1;
449b6cee71dSXin LI 		return apprentice_compile(ms, map, fn);
450b6cee71dSXin LI 	}
451b6cee71dSXin LI 
452b6cee71dSXin LI #ifndef COMPILE_ONLY
453b6cee71dSXin LI 	map = apprentice_map(ms, fn);
454a5d223e6SXin LI 	if (map == (struct magic_map *)-1)
455a5d223e6SXin LI 		return -1;
456b6cee71dSXin LI 	if (map == NULL) {
457b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
458b6cee71dSXin LI 			file_magwarn(ms, "using regular magic file `%s'", fn);
459b6cee71dSXin LI 		map = apprentice_load(ms, fn, action);
460b6cee71dSXin LI 		if (map == NULL)
461b6cee71dSXin LI 			return -1;
462b6cee71dSXin LI 	}
463b6cee71dSXin LI 
464b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
465b6cee71dSXin LI 		if (add_mlist(ms->mlist[i], map, i) == -1) {
466b6cee71dSXin LI 			file_oomem(ms, sizeof(*ml));
467a5d223e6SXin LI 			return -1;
468b6cee71dSXin LI 		}
469b6cee71dSXin LI 	}
470b6cee71dSXin LI 
471b6cee71dSXin LI 	if (action == FILE_LIST) {
472b6cee71dSXin LI 		for (i = 0; i < MAGIC_SETS; i++) {
473c2931133SXin LI 			printf("Set %" SIZE_T_FORMAT "u:\nBinary patterns:\n",
474c2931133SXin LI 			    i);
475b6cee71dSXin LI 			apprentice_list(ms->mlist[i], BINTEST);
476b6cee71dSXin LI 			printf("Text patterns:\n");
477b6cee71dSXin LI 			apprentice_list(ms->mlist[i], TEXTTEST);
478b6cee71dSXin LI 		}
479b6cee71dSXin LI 	}
480b6cee71dSXin LI 	return 0;
481c2931133SXin LI #else
482c2931133SXin LI 	return 0;
483c2931133SXin LI #endif /* COMPILE_ONLY */
484b6cee71dSXin LI }
485b6cee71dSXin LI 
486b6cee71dSXin LI protected void
487b6cee71dSXin LI file_ms_free(struct magic_set *ms)
488b6cee71dSXin LI {
489b6cee71dSXin LI 	size_t i;
490b6cee71dSXin LI 	if (ms == NULL)
491b6cee71dSXin LI 		return;
492b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++)
493b6cee71dSXin LI 		mlist_free(ms->mlist[i]);
494b6cee71dSXin LI 	free(ms->o.pbuf);
495b6cee71dSXin LI 	free(ms->o.buf);
496b6cee71dSXin LI 	free(ms->c.li);
497b6cee71dSXin LI 	free(ms);
498b6cee71dSXin LI }
499b6cee71dSXin LI 
500b6cee71dSXin LI protected struct magic_set *
501b6cee71dSXin LI file_ms_alloc(int flags)
502b6cee71dSXin LI {
503b6cee71dSXin LI 	struct magic_set *ms;
504b6cee71dSXin LI 	size_t i, len;
505b6cee71dSXin LI 
506b6cee71dSXin LI 	if ((ms = CAST(struct magic_set *, calloc((size_t)1,
507b6cee71dSXin LI 	    sizeof(struct magic_set)))) == NULL)
508b6cee71dSXin LI 		return NULL;
509b6cee71dSXin LI 
510b6cee71dSXin LI 	if (magic_setflags(ms, flags) == -1) {
511b6cee71dSXin LI 		errno = EINVAL;
512b6cee71dSXin LI 		goto free;
513b6cee71dSXin LI 	}
514b6cee71dSXin LI 
515b6cee71dSXin LI 	ms->o.buf = ms->o.pbuf = NULL;
516b6cee71dSXin LI 	len = (ms->c.len = 10) * sizeof(*ms->c.li);
517b6cee71dSXin LI 
518b6cee71dSXin LI 	if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
519b6cee71dSXin LI 		goto free;
520b6cee71dSXin LI 
521b6cee71dSXin LI 	ms->event_flags = 0;
522b6cee71dSXin LI 	ms->error = -1;
523b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++)
524b6cee71dSXin LI 		ms->mlist[i] = NULL;
525b6cee71dSXin LI 	ms->file = "unknown";
526b6cee71dSXin LI 	ms->line = 0;
527c2931133SXin LI 	ms->indir_max = FILE_INDIR_MAX;
528c2931133SXin LI 	ms->name_max = FILE_NAME_MAX;
529c2931133SXin LI 	ms->elf_shnum_max = FILE_ELF_SHNUM_MAX;
530c2931133SXin LI 	ms->elf_phnum_max = FILE_ELF_PHNUM_MAX;
5314460e5b0SXin LI 	ms->elf_notes_max = FILE_ELF_NOTES_MAX;
5329ce06829SXin LI 	ms->regex_max = FILE_REGEX_MAX;
5333e41d09dSXin LI 	ms->bytes_max = FILE_BYTES_MAX;
534b6cee71dSXin LI 	return ms;
535b6cee71dSXin LI free:
536b6cee71dSXin LI 	free(ms);
537b6cee71dSXin LI 	return NULL;
538b6cee71dSXin LI }
539b6cee71dSXin LI 
540b6cee71dSXin LI private void
541b6cee71dSXin LI apprentice_unmap(struct magic_map *map)
542b6cee71dSXin LI {
5439ce06829SXin LI 	size_t i;
544b6cee71dSXin LI 	if (map == NULL)
545b6cee71dSXin LI 		return;
546c2931133SXin LI 
547c2931133SXin LI 	switch (map->type) {
5483e41d09dSXin LI 	case MAP_TYPE_USER:
5493e41d09dSXin LI 		break;
5503e41d09dSXin LI 	case MAP_TYPE_MALLOC:
5513e41d09dSXin LI 		for (i = 0; i < MAGIC_SETS; i++) {
55240427ccaSGordon Tetlow 			void *b = map->magic[i];
55340427ccaSGordon Tetlow 			void *p = map->p;
55440427ccaSGordon Tetlow 			if (CAST(char *, b) >= CAST(char *, p) &&
55540427ccaSGordon Tetlow 			    CAST(char *, b) <= CAST(char *, p) + map->len)
5563e41d09dSXin LI 				continue;
5573e41d09dSXin LI 			free(map->magic[i]);
5583e41d09dSXin LI 		}
5593e41d09dSXin LI 		free(map->p);
5603e41d09dSXin LI 		break;
561b6cee71dSXin LI #ifdef QUICK
562c2931133SXin LI 	case MAP_TYPE_MMAP:
5633e41d09dSXin LI 		if (map->p && map->p != MAP_FAILED)
564b6cee71dSXin LI 			(void)munmap(map->p, map->len);
565c2931133SXin LI 		break;
566b6cee71dSXin LI #endif
567c2931133SXin LI 	default:
568c2931133SXin LI 		abort();
569b6cee71dSXin LI 	}
570b6cee71dSXin LI 	free(map);
571b6cee71dSXin LI }
572b6cee71dSXin LI 
573b6cee71dSXin LI private struct mlist *
574b6cee71dSXin LI mlist_alloc(void)
575b6cee71dSXin LI {
576b6cee71dSXin LI 	struct mlist *mlist;
577b6cee71dSXin LI 	if ((mlist = CAST(struct mlist *, calloc(1, sizeof(*mlist)))) == NULL) {
578b6cee71dSXin LI 		return NULL;
579b6cee71dSXin LI 	}
580b6cee71dSXin LI 	mlist->next = mlist->prev = mlist;
581b6cee71dSXin LI 	return mlist;
582b6cee71dSXin LI }
583b6cee71dSXin LI 
584b6cee71dSXin LI private void
585b6cee71dSXin LI mlist_free(struct mlist *mlist)
586b6cee71dSXin LI {
587c2931133SXin LI 	struct mlist *ml, *next;
588b6cee71dSXin LI 
589b6cee71dSXin LI 	if (mlist == NULL)
590b6cee71dSXin LI 		return;
591b6cee71dSXin LI 
592c2931133SXin LI 	ml = mlist->next;
593c2931133SXin LI 	for (ml = mlist->next; (next = ml->next) != NULL; ml = next) {
594b6cee71dSXin LI 		if (ml->map)
595a5d223e6SXin LI 			apprentice_unmap(CAST(struct magic_map *, ml->map));
596b6cee71dSXin LI 		free(ml);
597c2931133SXin LI 		if (ml == mlist)
598c2931133SXin LI 			break;
599b6cee71dSXin LI 	}
600b6cee71dSXin LI }
601b6cee71dSXin LI 
602c2931133SXin LI #ifndef COMPILE_ONLY
603c2931133SXin LI /* void **bufs: an array of compiled magic files */
604c2931133SXin LI protected int
605c2931133SXin LI buffer_apprentice(struct magic_set *ms, struct magic **bufs,
606c2931133SXin LI     size_t *sizes, size_t nbufs)
607c2931133SXin LI {
608c2931133SXin LI 	size_t i, j;
609c2931133SXin LI 	struct mlist *ml;
610c2931133SXin LI 	struct magic_map *map;
611c2931133SXin LI 
612c2931133SXin LI 	if (nbufs == 0)
613c2931133SXin LI 		return -1;
614c2931133SXin LI 
61540427ccaSGordon Tetlow 	(void)file_reset(ms, 0);
616c2931133SXin LI 
617c2931133SXin LI 	init_file_tables();
618c2931133SXin LI 
619c2931133SXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
620c2931133SXin LI 		mlist_free(ms->mlist[i]);
621c2931133SXin LI 		if ((ms->mlist[i] = mlist_alloc()) == NULL) {
622c2931133SXin LI 			file_oomem(ms, sizeof(*ms->mlist[i]));
623c2931133SXin LI 			goto fail;
624c2931133SXin LI 		}
625c2931133SXin LI 	}
626c2931133SXin LI 
627c2931133SXin LI 	for (i = 0; i < nbufs; i++) {
628c2931133SXin LI 		map = apprentice_buf(ms, bufs[i], sizes[i]);
629c2931133SXin LI 		if (map == NULL)
630c2931133SXin LI 			goto fail;
631c2931133SXin LI 
632c2931133SXin LI 		for (j = 0; j < MAGIC_SETS; j++) {
633c2931133SXin LI 			if (add_mlist(ms->mlist[j], map, j) == -1) {
634c2931133SXin LI 				file_oomem(ms, sizeof(*ml));
635c2931133SXin LI 				goto fail;
636c2931133SXin LI 			}
637c2931133SXin LI 		}
638c2931133SXin LI 	}
639c2931133SXin LI 
640c2931133SXin LI 	return 0;
641c2931133SXin LI fail:
642c2931133SXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
643c2931133SXin LI 		mlist_free(ms->mlist[i]);
644c2931133SXin LI 		ms->mlist[i] = NULL;
645c2931133SXin LI 	}
646c2931133SXin LI 	return -1;
647c2931133SXin LI }
648c2931133SXin LI #endif
649c2931133SXin LI 
650b6cee71dSXin LI /* const char *fn: list of magic files and directories */
651b6cee71dSXin LI protected int
652b6cee71dSXin LI file_apprentice(struct magic_set *ms, const char *fn, int action)
653b6cee71dSXin LI {
654b6cee71dSXin LI 	char *p, *mfn;
655*58a0f0d0SEitan Adler 	int fileerr, errs = -1;
656b6cee71dSXin LI 	size_t i;
657b6cee71dSXin LI 
65840427ccaSGordon Tetlow 	(void)file_reset(ms, 0);
659b6cee71dSXin LI 
660b6cee71dSXin LI 	if ((fn = magic_getpath(fn, action)) == NULL)
661b6cee71dSXin LI 		return -1;
662b6cee71dSXin LI 
663b6cee71dSXin LI 	init_file_tables();
664b6cee71dSXin LI 
665b6cee71dSXin LI 	if ((mfn = strdup(fn)) == NULL) {
666b6cee71dSXin LI 		file_oomem(ms, strlen(fn));
667b6cee71dSXin LI 		return -1;
668b6cee71dSXin LI 	}
669b6cee71dSXin LI 
670b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
671b6cee71dSXin LI 		mlist_free(ms->mlist[i]);
672b6cee71dSXin LI 		if ((ms->mlist[i] = mlist_alloc()) == NULL) {
673b6cee71dSXin LI 			file_oomem(ms, sizeof(*ms->mlist[i]));
674c2931133SXin LI 			while (i-- > 0) {
675b6cee71dSXin LI 				mlist_free(ms->mlist[i]);
676c2931133SXin LI 				ms->mlist[i] = NULL;
677b6cee71dSXin LI 			}
678b6cee71dSXin LI 			free(mfn);
679b6cee71dSXin LI 			return -1;
680b6cee71dSXin LI 		}
681b6cee71dSXin LI 	}
682b6cee71dSXin LI 	fn = mfn;
683b6cee71dSXin LI 
684b6cee71dSXin LI 	while (fn) {
685b6cee71dSXin LI 		p = strchr(fn, PATHSEP);
686b6cee71dSXin LI 		if (p)
687b6cee71dSXin LI 			*p++ = '\0';
688b6cee71dSXin LI 		if (*fn == '\0')
689b6cee71dSXin LI 			break;
690*58a0f0d0SEitan Adler 		fileerr = apprentice_1(ms, fn, action);
691*58a0f0d0SEitan Adler 		errs = MAX(errs, fileerr);
692b6cee71dSXin LI 		fn = p;
693b6cee71dSXin LI 	}
694b6cee71dSXin LI 
695b6cee71dSXin LI 	free(mfn);
696b6cee71dSXin LI 
697b6cee71dSXin LI 	if (errs == -1) {
698b6cee71dSXin LI 		for (i = 0; i < MAGIC_SETS; i++) {
699b6cee71dSXin LI 			mlist_free(ms->mlist[i]);
700b6cee71dSXin LI 			ms->mlist[i] = NULL;
701b6cee71dSXin LI 		}
702b6cee71dSXin LI 		file_error(ms, 0, "could not find any valid magic files!");
703b6cee71dSXin LI 		return -1;
704b6cee71dSXin LI 	}
705b6cee71dSXin LI 
706b6cee71dSXin LI #if 0
707b6cee71dSXin LI 	/*
708b6cee71dSXin LI 	 * Always leave the database loaded
709b6cee71dSXin LI 	 */
710b6cee71dSXin LI 	if (action == FILE_LOAD)
711b6cee71dSXin LI 		return 0;
712b6cee71dSXin LI 
713b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
714b6cee71dSXin LI 		mlist_free(ms->mlist[i]);
715b6cee71dSXin LI 		ms->mlist[i] = NULL;
716b6cee71dSXin LI 	}
717b6cee71dSXin LI #endif
718b6cee71dSXin LI 
719b6cee71dSXin LI 	switch (action) {
720b6cee71dSXin LI 	case FILE_LOAD:
721b6cee71dSXin LI 	case FILE_COMPILE:
722b6cee71dSXin LI 	case FILE_CHECK:
723b6cee71dSXin LI 	case FILE_LIST:
724b6cee71dSXin LI 		return 0;
725b6cee71dSXin LI 	default:
726b6cee71dSXin LI 		file_error(ms, 0, "Invalid action %d", action);
727b6cee71dSXin LI 		return -1;
728b6cee71dSXin LI 	}
729b6cee71dSXin LI }
730b6cee71dSXin LI 
731b6cee71dSXin LI /*
732b6cee71dSXin LI  * Compute the real length of a magic expression, for the purposes
733b6cee71dSXin LI  * of determining how "strong" a magic expression is (approximating
734b6cee71dSXin LI  * how specific its matches are):
735b6cee71dSXin LI  *	- magic characters count 0 unless escaped.
736b6cee71dSXin LI  *	- [] expressions count 1
737b6cee71dSXin LI  *	- {} expressions count 0
738b6cee71dSXin LI  *	- regular characters or escaped magic characters count 1
739b6cee71dSXin LI  *	- 0 length expressions count as one
740b6cee71dSXin LI  */
741b6cee71dSXin LI private size_t
742b6cee71dSXin LI nonmagic(const char *str)
743b6cee71dSXin LI {
744b6cee71dSXin LI 	const char *p;
745b6cee71dSXin LI 	size_t rv = 0;
746b6cee71dSXin LI 
747b6cee71dSXin LI 	for (p = str; *p; p++)
748b6cee71dSXin LI 		switch (*p) {
749b6cee71dSXin LI 		case '\\':	/* Escaped anything counts 1 */
750b6cee71dSXin LI 			if (!*++p)
751b6cee71dSXin LI 				p--;
752b6cee71dSXin LI 			rv++;
753b6cee71dSXin LI 			continue;
754b6cee71dSXin LI 		case '?':	/* Magic characters count 0 */
755b6cee71dSXin LI 		case '*':
756b6cee71dSXin LI 		case '.':
757b6cee71dSXin LI 		case '+':
758b6cee71dSXin LI 		case '^':
759b6cee71dSXin LI 		case '$':
760b6cee71dSXin LI 			continue;
761b6cee71dSXin LI 		case '[':	/* Bracketed expressions count 1 the ']' */
762b6cee71dSXin LI 			while (*p && *p != ']')
763b6cee71dSXin LI 				p++;
764b6cee71dSXin LI 			p--;
765b6cee71dSXin LI 			continue;
766b6cee71dSXin LI 		case '{':	/* Braced expressions count 0 */
767b6cee71dSXin LI 			while (*p && *p != '}')
768b6cee71dSXin LI 				p++;
769b6cee71dSXin LI 			if (!*p)
770b6cee71dSXin LI 				p--;
771b6cee71dSXin LI 			continue;
772b6cee71dSXin LI 		default:	/* Anything else counts 1 */
773b6cee71dSXin LI 			rv++;
774b6cee71dSXin LI 			continue;
775b6cee71dSXin LI 		}
776b6cee71dSXin LI 
777b6cee71dSXin LI 	return rv == 0 ? 1 : rv;	/* Return at least 1 */
778b6cee71dSXin LI }
779b6cee71dSXin LI 
78040427ccaSGordon Tetlow 
78140427ccaSGordon Tetlow private size_t
78240427ccaSGordon Tetlow typesize(int type)
78340427ccaSGordon Tetlow {
78440427ccaSGordon Tetlow 	switch (type) {
78540427ccaSGordon Tetlow 	case FILE_BYTE:
78640427ccaSGordon Tetlow 		return 1;
78740427ccaSGordon Tetlow 
78840427ccaSGordon Tetlow 	case FILE_SHORT:
78940427ccaSGordon Tetlow 	case FILE_LESHORT:
79040427ccaSGordon Tetlow 	case FILE_BESHORT:
79140427ccaSGordon Tetlow 		return 2;
79240427ccaSGordon Tetlow 
79340427ccaSGordon Tetlow 	case FILE_LONG:
79440427ccaSGordon Tetlow 	case FILE_LELONG:
79540427ccaSGordon Tetlow 	case FILE_BELONG:
79640427ccaSGordon Tetlow 	case FILE_MELONG:
79740427ccaSGordon Tetlow 		return 4;
79840427ccaSGordon Tetlow 
79940427ccaSGordon Tetlow 	case FILE_DATE:
80040427ccaSGordon Tetlow 	case FILE_LEDATE:
80140427ccaSGordon Tetlow 	case FILE_BEDATE:
80240427ccaSGordon Tetlow 	case FILE_MEDATE:
80340427ccaSGordon Tetlow 	case FILE_LDATE:
80440427ccaSGordon Tetlow 	case FILE_LELDATE:
80540427ccaSGordon Tetlow 	case FILE_BELDATE:
80640427ccaSGordon Tetlow 	case FILE_MELDATE:
80740427ccaSGordon Tetlow 	case FILE_FLOAT:
80840427ccaSGordon Tetlow 	case FILE_BEFLOAT:
80940427ccaSGordon Tetlow 	case FILE_LEFLOAT:
81040427ccaSGordon Tetlow 		return 4;
81140427ccaSGordon Tetlow 
81240427ccaSGordon Tetlow 	case FILE_QUAD:
81340427ccaSGordon Tetlow 	case FILE_BEQUAD:
81440427ccaSGordon Tetlow 	case FILE_LEQUAD:
81540427ccaSGordon Tetlow 	case FILE_QDATE:
81640427ccaSGordon Tetlow 	case FILE_LEQDATE:
81740427ccaSGordon Tetlow 	case FILE_BEQDATE:
81840427ccaSGordon Tetlow 	case FILE_QLDATE:
81940427ccaSGordon Tetlow 	case FILE_LEQLDATE:
82040427ccaSGordon Tetlow 	case FILE_BEQLDATE:
82140427ccaSGordon Tetlow 	case FILE_QWDATE:
82240427ccaSGordon Tetlow 	case FILE_LEQWDATE:
82340427ccaSGordon Tetlow 	case FILE_BEQWDATE:
82440427ccaSGordon Tetlow 	case FILE_DOUBLE:
82540427ccaSGordon Tetlow 	case FILE_BEDOUBLE:
82640427ccaSGordon Tetlow 	case FILE_LEDOUBLE:
82740427ccaSGordon Tetlow 		return 8;
82840427ccaSGordon Tetlow 	default:
82940427ccaSGordon Tetlow 		return (size_t)~0;
83040427ccaSGordon Tetlow 	}
83140427ccaSGordon Tetlow }
83240427ccaSGordon Tetlow 
833b6cee71dSXin LI /*
834b6cee71dSXin LI  * Get weight of this magic entry, for sorting purposes.
835b6cee71dSXin LI  */
836b6cee71dSXin LI private size_t
837b6cee71dSXin LI apprentice_magic_strength(const struct magic *m)
838b6cee71dSXin LI {
839b6cee71dSXin LI #define MULT 10
84040427ccaSGordon Tetlow 	size_t ts, v, val = 2 * MULT;	/* baseline strength */
841b6cee71dSXin LI 
842b6cee71dSXin LI 	switch (m->type) {
843b6cee71dSXin LI 	case FILE_DEFAULT:	/* make sure this sorts last */
844b6cee71dSXin LI 		if (m->factor_op != FILE_FACTOR_OP_NONE)
845b6cee71dSXin LI 			abort();
846b6cee71dSXin LI 		return 0;
847b6cee71dSXin LI 
848b6cee71dSXin LI 	case FILE_BYTE:
849b6cee71dSXin LI 	case FILE_SHORT:
850b6cee71dSXin LI 	case FILE_LESHORT:
851b6cee71dSXin LI 	case FILE_BESHORT:
852b6cee71dSXin LI 	case FILE_LONG:
853b6cee71dSXin LI 	case FILE_LELONG:
854b6cee71dSXin LI 	case FILE_BELONG:
855b6cee71dSXin LI 	case FILE_MELONG:
85640427ccaSGordon Tetlow 	case FILE_DATE:
85740427ccaSGordon Tetlow 	case FILE_LEDATE:
85840427ccaSGordon Tetlow 	case FILE_BEDATE:
85940427ccaSGordon Tetlow 	case FILE_MEDATE:
86040427ccaSGordon Tetlow 	case FILE_LDATE:
86140427ccaSGordon Tetlow 	case FILE_LELDATE:
86240427ccaSGordon Tetlow 	case FILE_BELDATE:
86340427ccaSGordon Tetlow 	case FILE_MELDATE:
86440427ccaSGordon Tetlow 	case FILE_FLOAT:
86540427ccaSGordon Tetlow 	case FILE_BEFLOAT:
86640427ccaSGordon Tetlow 	case FILE_LEFLOAT:
86740427ccaSGordon Tetlow 	case FILE_QUAD:
86840427ccaSGordon Tetlow 	case FILE_BEQUAD:
86940427ccaSGordon Tetlow 	case FILE_LEQUAD:
87040427ccaSGordon Tetlow 	case FILE_QDATE:
87140427ccaSGordon Tetlow 	case FILE_LEQDATE:
87240427ccaSGordon Tetlow 	case FILE_BEQDATE:
87340427ccaSGordon Tetlow 	case FILE_QLDATE:
87440427ccaSGordon Tetlow 	case FILE_LEQLDATE:
87540427ccaSGordon Tetlow 	case FILE_BEQLDATE:
87640427ccaSGordon Tetlow 	case FILE_QWDATE:
87740427ccaSGordon Tetlow 	case FILE_LEQWDATE:
87840427ccaSGordon Tetlow 	case FILE_BEQWDATE:
87940427ccaSGordon Tetlow 	case FILE_DOUBLE:
88040427ccaSGordon Tetlow 	case FILE_BEDOUBLE:
88140427ccaSGordon Tetlow 	case FILE_LEDOUBLE:
88240427ccaSGordon Tetlow 		ts = typesize(m->type);
88340427ccaSGordon Tetlow 		if (ts == (size_t)~0)
88440427ccaSGordon Tetlow 			abort();
88540427ccaSGordon Tetlow 		val += ts * MULT;
886b6cee71dSXin LI 		break;
887b6cee71dSXin LI 
888b6cee71dSXin LI 	case FILE_PSTRING:
889b6cee71dSXin LI 	case FILE_STRING:
890b6cee71dSXin LI 		val += m->vallen * MULT;
891b6cee71dSXin LI 		break;
892b6cee71dSXin LI 
893b6cee71dSXin LI 	case FILE_BESTRING16:
894b6cee71dSXin LI 	case FILE_LESTRING16:
895b6cee71dSXin LI 		val += m->vallen * MULT / 2;
896b6cee71dSXin LI 		break;
897b6cee71dSXin LI 
898b6cee71dSXin LI 	case FILE_SEARCH:
899b6cee71dSXin LI 		val += m->vallen * MAX(MULT / m->vallen, 1);
900b6cee71dSXin LI 		break;
901b6cee71dSXin LI 
902b6cee71dSXin LI 	case FILE_REGEX:
903b6cee71dSXin LI 		v = nonmagic(m->value.s);
904b6cee71dSXin LI 		val += v * MAX(MULT / v, 1);
905b6cee71dSXin LI 		break;
906b6cee71dSXin LI 
907b6cee71dSXin LI 	case FILE_INDIRECT:
908b6cee71dSXin LI 	case FILE_NAME:
909b6cee71dSXin LI 	case FILE_USE:
910b6cee71dSXin LI 		break;
911b6cee71dSXin LI 
9123e41d09dSXin LI 	case FILE_DER:
9133e41d09dSXin LI 		val += MULT;
9143e41d09dSXin LI 		break;
9153e41d09dSXin LI 
916b6cee71dSXin LI 	default:
917b6cee71dSXin LI 		(void)fprintf(stderr, "Bad type %d\n", m->type);
918b6cee71dSXin LI 		abort();
919b6cee71dSXin LI 	}
920b6cee71dSXin LI 
921b6cee71dSXin LI 	switch (m->reln) {
922b6cee71dSXin LI 	case 'x':	/* matches anything penalize */
923b6cee71dSXin LI 	case '!':       /* matches almost anything penalize */
924b6cee71dSXin LI 		val = 0;
925b6cee71dSXin LI 		break;
926b6cee71dSXin LI 
927b6cee71dSXin LI 	case '=':	/* Exact match, prefer */
928b6cee71dSXin LI 		val += MULT;
929b6cee71dSXin LI 		break;
930b6cee71dSXin LI 
931b6cee71dSXin LI 	case '>':
932b6cee71dSXin LI 	case '<':	/* comparison match reduce strength */
933b6cee71dSXin LI 		val -= 2 * MULT;
934b6cee71dSXin LI 		break;
935b6cee71dSXin LI 
936b6cee71dSXin LI 	case '^':
937b6cee71dSXin LI 	case '&':	/* masking bits, we could count them too */
938b6cee71dSXin LI 		val -= MULT;
939b6cee71dSXin LI 		break;
940b6cee71dSXin LI 
941b6cee71dSXin LI 	default:
942b6cee71dSXin LI 		(void)fprintf(stderr, "Bad relation %c\n", m->reln);
943b6cee71dSXin LI 		abort();
944b6cee71dSXin LI 	}
945b6cee71dSXin LI 
946b6cee71dSXin LI 	if (val == 0)	/* ensure we only return 0 for FILE_DEFAULT */
947b6cee71dSXin LI 		val = 1;
948b6cee71dSXin LI 
949b6cee71dSXin LI 	switch (m->factor_op) {
950b6cee71dSXin LI 	case FILE_FACTOR_OP_NONE:
951b6cee71dSXin LI 		break;
952b6cee71dSXin LI 	case FILE_FACTOR_OP_PLUS:
953b6cee71dSXin LI 		val += m->factor;
954b6cee71dSXin LI 		break;
955b6cee71dSXin LI 	case FILE_FACTOR_OP_MINUS:
956b6cee71dSXin LI 		val -= m->factor;
957b6cee71dSXin LI 		break;
958b6cee71dSXin LI 	case FILE_FACTOR_OP_TIMES:
959b6cee71dSXin LI 		val *= m->factor;
960b6cee71dSXin LI 		break;
961b6cee71dSXin LI 	case FILE_FACTOR_OP_DIV:
962b6cee71dSXin LI 		val /= m->factor;
963b6cee71dSXin LI 		break;
964b6cee71dSXin LI 	default:
965b6cee71dSXin LI 		abort();
966b6cee71dSXin LI 	}
967b6cee71dSXin LI 
968b6cee71dSXin LI 	/*
969b6cee71dSXin LI 	 * Magic entries with no description get a bonus because they depend
970b6cee71dSXin LI 	 * on subsequent magic entries to print something.
971b6cee71dSXin LI 	 */
972b6cee71dSXin LI 	if (m->desc[0] == '\0')
973b6cee71dSXin LI 		val++;
974b6cee71dSXin LI 	return val;
975b6cee71dSXin LI }
976b6cee71dSXin LI 
977b6cee71dSXin LI /*
978b6cee71dSXin LI  * Sort callback for sorting entries by "strength" (basically length)
979b6cee71dSXin LI  */
980b6cee71dSXin LI private int
981b6cee71dSXin LI apprentice_sort(const void *a, const void *b)
982b6cee71dSXin LI {
983b6cee71dSXin LI 	const struct magic_entry *ma = CAST(const struct magic_entry *, a);
984b6cee71dSXin LI 	const struct magic_entry *mb = CAST(const struct magic_entry *, b);
985b6cee71dSXin LI 	size_t sa = apprentice_magic_strength(ma->mp);
986b6cee71dSXin LI 	size_t sb = apprentice_magic_strength(mb->mp);
987b6cee71dSXin LI 	if (sa == sb)
988b6cee71dSXin LI 		return 0;
989b6cee71dSXin LI 	else if (sa > sb)
990b6cee71dSXin LI 		return -1;
991b6cee71dSXin LI 	else
992b6cee71dSXin LI 		return 1;
993b6cee71dSXin LI }
994b6cee71dSXin LI 
995b6cee71dSXin LI /*
996b6cee71dSXin LI  * Shows sorted patterns list in the order which is used for the matching
997b6cee71dSXin LI  */
998b6cee71dSXin LI private void
999b6cee71dSXin LI apprentice_list(struct mlist *mlist, int mode)
1000b6cee71dSXin LI {
1001b6cee71dSXin LI 	uint32_t magindex = 0;
1002b6cee71dSXin LI 	struct mlist *ml;
1003b6cee71dSXin LI 	for (ml = mlist->next; ml != mlist; ml = ml->next) {
1004b6cee71dSXin LI 		for (magindex = 0; magindex < ml->nmagic; magindex++) {
1005b6cee71dSXin LI 			struct magic *m = &ml->magic[magindex];
1006b6cee71dSXin LI 			if ((m->flag & mode) != mode) {
1007b6cee71dSXin LI 				/* Skip sub-tests */
1008b6cee71dSXin LI 				while (magindex + 1 < ml->nmagic &&
1009b6cee71dSXin LI 				       ml->magic[magindex + 1].cont_level != 0)
1010b6cee71dSXin LI 					++magindex;
1011b6cee71dSXin LI 				continue; /* Skip to next top-level test*/
1012b6cee71dSXin LI 			}
1013b6cee71dSXin LI 
1014b6cee71dSXin LI 			/*
1015b6cee71dSXin LI 			 * Try to iterate over the tree until we find item with
1016b6cee71dSXin LI 			 * description/mimetype.
1017b6cee71dSXin LI 			 */
1018b6cee71dSXin LI 			while (magindex + 1 < ml->nmagic &&
1019b6cee71dSXin LI 			       ml->magic[magindex + 1].cont_level != 0 &&
1020b6cee71dSXin LI 			       *ml->magic[magindex].desc == '\0' &&
1021b6cee71dSXin LI 			       *ml->magic[magindex].mimetype == '\0')
1022b6cee71dSXin LI 				magindex++;
1023b6cee71dSXin LI 
10245f0216bdSXin LI 			printf("Strength = %3" SIZE_T_FORMAT "u@%u: %s [%s]\n",
1025b6cee71dSXin LI 			    apprentice_magic_strength(m),
10265f0216bdSXin LI 			    ml->magic[magindex].lineno,
1027b6cee71dSXin LI 			    ml->magic[magindex].desc,
1028b6cee71dSXin LI 			    ml->magic[magindex].mimetype);
1029b6cee71dSXin LI 		}
1030b6cee71dSXin LI 	}
1031b6cee71dSXin LI }
1032b6cee71dSXin LI 
1033b6cee71dSXin LI private void
1034b6cee71dSXin LI set_test_type(struct magic *mstart, struct magic *m)
1035b6cee71dSXin LI {
1036b6cee71dSXin LI 	switch (m->type) {
1037b6cee71dSXin LI 	case FILE_BYTE:
1038b6cee71dSXin LI 	case FILE_SHORT:
1039b6cee71dSXin LI 	case FILE_LONG:
1040b6cee71dSXin LI 	case FILE_DATE:
1041b6cee71dSXin LI 	case FILE_BESHORT:
1042b6cee71dSXin LI 	case FILE_BELONG:
1043b6cee71dSXin LI 	case FILE_BEDATE:
1044b6cee71dSXin LI 	case FILE_LESHORT:
1045b6cee71dSXin LI 	case FILE_LELONG:
1046b6cee71dSXin LI 	case FILE_LEDATE:
1047b6cee71dSXin LI 	case FILE_LDATE:
1048b6cee71dSXin LI 	case FILE_BELDATE:
1049b6cee71dSXin LI 	case FILE_LELDATE:
1050b6cee71dSXin LI 	case FILE_MEDATE:
1051b6cee71dSXin LI 	case FILE_MELDATE:
1052b6cee71dSXin LI 	case FILE_MELONG:
1053b6cee71dSXin LI 	case FILE_QUAD:
1054b6cee71dSXin LI 	case FILE_LEQUAD:
1055b6cee71dSXin LI 	case FILE_BEQUAD:
1056b6cee71dSXin LI 	case FILE_QDATE:
1057b6cee71dSXin LI 	case FILE_LEQDATE:
1058b6cee71dSXin LI 	case FILE_BEQDATE:
1059b6cee71dSXin LI 	case FILE_QLDATE:
1060b6cee71dSXin LI 	case FILE_LEQLDATE:
1061b6cee71dSXin LI 	case FILE_BEQLDATE:
1062b6cee71dSXin LI 	case FILE_QWDATE:
1063b6cee71dSXin LI 	case FILE_LEQWDATE:
1064b6cee71dSXin LI 	case FILE_BEQWDATE:
1065b6cee71dSXin LI 	case FILE_FLOAT:
1066b6cee71dSXin LI 	case FILE_BEFLOAT:
1067b6cee71dSXin LI 	case FILE_LEFLOAT:
1068b6cee71dSXin LI 	case FILE_DOUBLE:
1069b6cee71dSXin LI 	case FILE_BEDOUBLE:
1070b6cee71dSXin LI 	case FILE_LEDOUBLE:
10713e41d09dSXin LI 	case FILE_DER:
1072b6cee71dSXin LI 		mstart->flag |= BINTEST;
1073b6cee71dSXin LI 		break;
1074b6cee71dSXin LI 	case FILE_STRING:
1075b6cee71dSXin LI 	case FILE_PSTRING:
1076b6cee71dSXin LI 	case FILE_BESTRING16:
1077b6cee71dSXin LI 	case FILE_LESTRING16:
1078b6cee71dSXin LI 		/* Allow text overrides */
1079b6cee71dSXin LI 		if (mstart->str_flags & STRING_TEXTTEST)
1080b6cee71dSXin LI 			mstart->flag |= TEXTTEST;
1081b6cee71dSXin LI 		else
1082b6cee71dSXin LI 			mstart->flag |= BINTEST;
1083b6cee71dSXin LI 		break;
1084b6cee71dSXin LI 	case FILE_REGEX:
1085b6cee71dSXin LI 	case FILE_SEARCH:
1086b6cee71dSXin LI 		/* Check for override */
1087b6cee71dSXin LI 		if (mstart->str_flags & STRING_BINTEST)
1088b6cee71dSXin LI 			mstart->flag |= BINTEST;
1089b6cee71dSXin LI 		if (mstart->str_flags & STRING_TEXTTEST)
1090b6cee71dSXin LI 			mstart->flag |= TEXTTEST;
1091b6cee71dSXin LI 
1092b6cee71dSXin LI 		if (mstart->flag & (TEXTTEST|BINTEST))
1093b6cee71dSXin LI 			break;
1094b6cee71dSXin LI 
1095b6cee71dSXin LI 		/* binary test if pattern is not text */
1096b6cee71dSXin LI 		if (file_looks_utf8(m->value.us, (size_t)m->vallen, NULL,
1097b6cee71dSXin LI 		    NULL) <= 0)
1098b6cee71dSXin LI 			mstart->flag |= BINTEST;
1099b6cee71dSXin LI 		else
1100b6cee71dSXin LI 			mstart->flag |= TEXTTEST;
1101b6cee71dSXin LI 		break;
1102b6cee71dSXin LI 	case FILE_DEFAULT:
1103b6cee71dSXin LI 		/* can't deduce anything; we shouldn't see this at the
1104b6cee71dSXin LI 		   top level anyway */
1105b6cee71dSXin LI 		break;
1106b6cee71dSXin LI 	case FILE_INVALID:
1107b6cee71dSXin LI 	default:
1108b6cee71dSXin LI 		/* invalid search type, but no need to complain here */
1109b6cee71dSXin LI 		break;
1110b6cee71dSXin LI 	}
1111b6cee71dSXin LI }
1112b6cee71dSXin LI 
1113b6cee71dSXin LI private int
1114b6cee71dSXin LI addentry(struct magic_set *ms, struct magic_entry *me,
1115b6cee71dSXin LI    struct magic_entry_set *mset)
1116b6cee71dSXin LI {
1117b6cee71dSXin LI 	size_t i = me->mp->type == FILE_NAME ? 1 : 0;
1118b6cee71dSXin LI 	if (mset[i].count == mset[i].max) {
1119b6cee71dSXin LI 		struct magic_entry *mp;
1120b6cee71dSXin LI 
1121b6cee71dSXin LI 		mset[i].max += ALLOC_INCR;
1122b6cee71dSXin LI 		if ((mp = CAST(struct magic_entry *,
1123b6cee71dSXin LI 		    realloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
1124b6cee71dSXin LI 		    NULL) {
1125b6cee71dSXin LI 			file_oomem(ms, sizeof(*mp) * mset[i].max);
1126b6cee71dSXin LI 			return -1;
1127b6cee71dSXin LI 		}
1128b6cee71dSXin LI 		(void)memset(&mp[mset[i].count], 0, sizeof(*mp) *
1129b6cee71dSXin LI 		    ALLOC_INCR);
1130b6cee71dSXin LI 		mset[i].me = mp;
1131b6cee71dSXin LI 	}
1132b6cee71dSXin LI 	mset[i].me[mset[i].count++] = *me;
1133b6cee71dSXin LI 	memset(me, 0, sizeof(*me));
1134b6cee71dSXin LI 	return 0;
1135b6cee71dSXin LI }
1136b6cee71dSXin LI 
1137b6cee71dSXin LI /*
1138b6cee71dSXin LI  * Load and parse one file.
1139b6cee71dSXin LI  */
1140b6cee71dSXin LI private void
1141b6cee71dSXin LI load_1(struct magic_set *ms, int action, const char *fn, int *errs,
1142b6cee71dSXin LI    struct magic_entry_set *mset)
1143b6cee71dSXin LI {
1144b6cee71dSXin LI 	size_t lineno = 0, llen = 0;
1145b6cee71dSXin LI 	char *line = NULL;
1146b6cee71dSXin LI 	ssize_t len;
1147b6cee71dSXin LI 	struct magic_entry me;
1148b6cee71dSXin LI 
1149b6cee71dSXin LI 	FILE *f = fopen(ms->file = fn, "r");
1150b6cee71dSXin LI 	if (f == NULL) {
1151b6cee71dSXin LI 		if (errno != ENOENT)
1152b6cee71dSXin LI 			file_error(ms, errno, "cannot read magic file `%s'",
1153b6cee71dSXin LI 				   fn);
1154b6cee71dSXin LI 		(*errs)++;
1155b6cee71dSXin LI 		return;
1156b6cee71dSXin LI 	}
1157b6cee71dSXin LI 
1158b6cee71dSXin LI 	memset(&me, 0, sizeof(me));
1159b6cee71dSXin LI 	/* read and parse this file */
1160b6cee71dSXin LI 	for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;
1161b6cee71dSXin LI 	    ms->line++) {
1162b6cee71dSXin LI 		if (len == 0) /* null line, garbage, etc */
1163b6cee71dSXin LI 			continue;
1164b6cee71dSXin LI 		if (line[len - 1] == '\n') {
1165b6cee71dSXin LI 			lineno++;
1166b6cee71dSXin LI 			line[len - 1] = '\0'; /* delete newline */
1167b6cee71dSXin LI 		}
1168b6cee71dSXin LI 		switch (line[0]) {
1169b6cee71dSXin LI 		case '\0':	/* empty, do not parse */
1170b6cee71dSXin LI 		case '#':	/* comment, do not parse */
1171b6cee71dSXin LI 			continue;
1172b6cee71dSXin LI 		case '!':
1173b6cee71dSXin LI 			if (line[1] == ':') {
1174b6cee71dSXin LI 				size_t i;
1175b6cee71dSXin LI 
1176b6cee71dSXin LI 				for (i = 0; bang[i].name != NULL; i++) {
1177b6cee71dSXin LI 					if ((size_t)(len - 2) > bang[i].len &&
1178b6cee71dSXin LI 					    memcmp(bang[i].name, line + 2,
1179b6cee71dSXin LI 					    bang[i].len) == 0)
1180b6cee71dSXin LI 						break;
1181b6cee71dSXin LI 				}
1182b6cee71dSXin LI 				if (bang[i].name == NULL) {
1183b6cee71dSXin LI 					file_error(ms, 0,
1184b6cee71dSXin LI 					    "Unknown !: entry `%s'", line);
1185b6cee71dSXin LI 					(*errs)++;
1186b6cee71dSXin LI 					continue;
1187b6cee71dSXin LI 				}
1188b6cee71dSXin LI 				if (me.mp == NULL) {
1189b6cee71dSXin LI 					file_error(ms, 0,
1190b6cee71dSXin LI 					    "No current entry for :!%s type",
1191b6cee71dSXin LI 						bang[i].name);
1192b6cee71dSXin LI 					(*errs)++;
1193b6cee71dSXin LI 					continue;
1194b6cee71dSXin LI 				}
1195b6cee71dSXin LI 				if ((*bang[i].fun)(ms, &me,
1196b6cee71dSXin LI 				    line + bang[i].len + 2) != 0) {
1197b6cee71dSXin LI 					(*errs)++;
1198b6cee71dSXin LI 					continue;
1199b6cee71dSXin LI 				}
1200b6cee71dSXin LI 				continue;
1201b6cee71dSXin LI 			}
1202b6cee71dSXin LI 			/*FALLTHROUGH*/
1203b6cee71dSXin LI 		default:
1204b6cee71dSXin LI 		again:
1205b6cee71dSXin LI 			switch (parse(ms, &me, line, lineno, action)) {
1206b6cee71dSXin LI 			case 0:
1207b6cee71dSXin LI 				continue;
1208b6cee71dSXin LI 			case 1:
1209b6cee71dSXin LI 				(void)addentry(ms, &me, mset);
1210b6cee71dSXin LI 				goto again;
1211b6cee71dSXin LI 			default:
1212b6cee71dSXin LI 				(*errs)++;
1213b6cee71dSXin LI 				break;
1214b6cee71dSXin LI 			}
1215b6cee71dSXin LI 		}
1216b6cee71dSXin LI 	}
1217b6cee71dSXin LI 	if (me.mp)
1218b6cee71dSXin LI 		(void)addentry(ms, &me, mset);
1219b6cee71dSXin LI 	free(line);
1220b6cee71dSXin LI 	(void)fclose(f);
1221b6cee71dSXin LI }
1222b6cee71dSXin LI 
1223b6cee71dSXin LI /*
1224b6cee71dSXin LI  * parse a file or directory of files
1225b6cee71dSXin LI  * const char *fn: name of magic file or directory
1226b6cee71dSXin LI  */
1227b6cee71dSXin LI private int
1228b6cee71dSXin LI cmpstrp(const void *p1, const void *p2)
1229b6cee71dSXin LI {
1230b6cee71dSXin LI         return strcmp(*(char *const *)p1, *(char *const *)p2);
1231b6cee71dSXin LI }
1232b6cee71dSXin LI 
1233b6cee71dSXin LI 
1234b6cee71dSXin LI private uint32_t
1235b6cee71dSXin LI set_text_binary(struct magic_set *ms, struct magic_entry *me, uint32_t nme,
1236b6cee71dSXin LI     uint32_t starttest)
1237b6cee71dSXin LI {
1238b6cee71dSXin LI 	static const char text[] = "text";
1239b6cee71dSXin LI 	static const char binary[] = "binary";
1240b6cee71dSXin LI 	static const size_t len = sizeof(text);
1241b6cee71dSXin LI 
1242b6cee71dSXin LI 	uint32_t i = starttest;
1243b6cee71dSXin LI 
1244b6cee71dSXin LI 	do {
1245b6cee71dSXin LI 		set_test_type(me[starttest].mp, me[i].mp);
1246b6cee71dSXin LI 		if ((ms->flags & MAGIC_DEBUG) == 0)
1247b6cee71dSXin LI 			continue;
1248b6cee71dSXin LI 		(void)fprintf(stderr, "%s%s%s: %s\n",
1249b6cee71dSXin LI 		    me[i].mp->mimetype,
1250b6cee71dSXin LI 		    me[i].mp->mimetype[0] == '\0' ? "" : "; ",
1251b6cee71dSXin LI 		    me[i].mp->desc[0] ? me[i].mp->desc : "(no description)",
1252b6cee71dSXin LI 		    me[i].mp->flag & BINTEST ? binary : text);
1253b6cee71dSXin LI 		if (me[i].mp->flag & BINTEST) {
1254b6cee71dSXin LI 			char *p = strstr(me[i].mp->desc, text);
1255b6cee71dSXin LI 			if (p && (p == me[i].mp->desc ||
1256b6cee71dSXin LI 			    isspace((unsigned char)p[-1])) &&
1257b6cee71dSXin LI 			    (p + len - me[i].mp->desc == MAXstring
1258b6cee71dSXin LI 			    || (p[len] == '\0' ||
1259b6cee71dSXin LI 			    isspace((unsigned char)p[len]))))
1260b6cee71dSXin LI 				(void)fprintf(stderr, "*** Possible "
1261b6cee71dSXin LI 				    "binary test for text type\n");
1262b6cee71dSXin LI 		}
1263b6cee71dSXin LI 	} while (++i < nme && me[i].mp->cont_level != 0);
1264b6cee71dSXin LI 	return i;
1265b6cee71dSXin LI }
1266b6cee71dSXin LI 
1267b6cee71dSXin LI private void
1268b6cee71dSXin LI set_last_default(struct magic_set *ms, struct magic_entry *me, uint32_t nme)
1269b6cee71dSXin LI {
1270b6cee71dSXin LI 	uint32_t i;
1271b6cee71dSXin LI 	for (i = 0; i < nme; i++) {
1272b6cee71dSXin LI 		if (me[i].mp->cont_level == 0 &&
1273b6cee71dSXin LI 		    me[i].mp->type == FILE_DEFAULT) {
1274b6cee71dSXin LI 			while (++i < nme)
1275b6cee71dSXin LI 				if (me[i].mp->cont_level == 0)
1276b6cee71dSXin LI 					break;
1277b6cee71dSXin LI 			if (i != nme) {
1278b6cee71dSXin LI 				/* XXX - Ugh! */
1279b6cee71dSXin LI 				ms->line = me[i].mp->lineno;
1280b6cee71dSXin LI 				file_magwarn(ms,
1281b6cee71dSXin LI 				    "level 0 \"default\" did not sort last");
1282b6cee71dSXin LI 			}
1283b6cee71dSXin LI 			return;
1284b6cee71dSXin LI 		}
1285b6cee71dSXin LI 	}
1286b6cee71dSXin LI }
1287b6cee71dSXin LI 
1288b6cee71dSXin LI private int
1289b6cee71dSXin LI coalesce_entries(struct magic_set *ms, struct magic_entry *me, uint32_t nme,
1290b6cee71dSXin LI     struct magic **ma, uint32_t *nma)
1291b6cee71dSXin LI {
1292b6cee71dSXin LI 	uint32_t i, mentrycount = 0;
1293b6cee71dSXin LI 	size_t slen;
1294b6cee71dSXin LI 
1295b6cee71dSXin LI 	for (i = 0; i < nme; i++)
1296b6cee71dSXin LI 		mentrycount += me[i].cont_count;
1297b6cee71dSXin LI 
1298b6cee71dSXin LI 	slen = sizeof(**ma) * mentrycount;
1299b6cee71dSXin LI 	if ((*ma = CAST(struct magic *, malloc(slen))) == NULL) {
1300b6cee71dSXin LI 		file_oomem(ms, slen);
1301b6cee71dSXin LI 		return -1;
1302b6cee71dSXin LI 	}
1303b6cee71dSXin LI 
1304b6cee71dSXin LI 	mentrycount = 0;
1305b6cee71dSXin LI 	for (i = 0; i < nme; i++) {
1306b6cee71dSXin LI 		(void)memcpy(*ma + mentrycount, me[i].mp,
1307b6cee71dSXin LI 		    me[i].cont_count * sizeof(**ma));
1308b6cee71dSXin LI 		mentrycount += me[i].cont_count;
1309b6cee71dSXin LI 	}
1310b6cee71dSXin LI 	*nma = mentrycount;
1311b6cee71dSXin LI 	return 0;
1312b6cee71dSXin LI }
1313b6cee71dSXin LI 
1314b6cee71dSXin LI private void
1315b6cee71dSXin LI magic_entry_free(struct magic_entry *me, uint32_t nme)
1316b6cee71dSXin LI {
1317b6cee71dSXin LI 	uint32_t i;
1318b6cee71dSXin LI 	if (me == NULL)
1319b6cee71dSXin LI 		return;
1320b6cee71dSXin LI 	for (i = 0; i < nme; i++)
1321b6cee71dSXin LI 		free(me[i].mp);
1322b6cee71dSXin LI 	free(me);
1323b6cee71dSXin LI }
1324b6cee71dSXin LI 
1325b6cee71dSXin LI private struct magic_map *
1326b6cee71dSXin LI apprentice_load(struct magic_set *ms, const char *fn, int action)
1327b6cee71dSXin LI {
1328b6cee71dSXin LI 	int errs = 0;
1329b6cee71dSXin LI 	uint32_t i, j;
1330b6cee71dSXin LI 	size_t files = 0, maxfiles = 0;
1331b6cee71dSXin LI 	char **filearr = NULL, *mfn;
1332b6cee71dSXin LI 	struct stat st;
1333b6cee71dSXin LI 	struct magic_map *map;
1334b6cee71dSXin LI 	struct magic_entry_set mset[MAGIC_SETS];
1335b6cee71dSXin LI 	DIR *dir;
1336b6cee71dSXin LI 	struct dirent *d;
1337b6cee71dSXin LI 
1338b6cee71dSXin LI 	memset(mset, 0, sizeof(mset));
1339b6cee71dSXin LI 	ms->flags |= MAGIC_CHECK;	/* Enable checks for parsed files */
1340b6cee71dSXin LI 
1341b6cee71dSXin LI 
1342b6cee71dSXin LI 	if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL)
1343b6cee71dSXin LI 	{
1344b6cee71dSXin LI 		file_oomem(ms, sizeof(*map));
1345b6cee71dSXin LI 		return NULL;
1346b6cee71dSXin LI 	}
13479ce06829SXin LI 	map->type = MAP_TYPE_MALLOC;
1348b6cee71dSXin LI 
1349b6cee71dSXin LI 	/* print silly verbose header for USG compat. */
1350b6cee71dSXin LI 	if (action == FILE_CHECK)
1351b6cee71dSXin LI 		(void)fprintf(stderr, "%s\n", usg_hdr);
1352b6cee71dSXin LI 
1353b6cee71dSXin LI 	/* load directory or file */
1354b6cee71dSXin LI 	if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
1355b6cee71dSXin LI 		dir = opendir(fn);
1356b6cee71dSXin LI 		if (!dir) {
1357b6cee71dSXin LI 			errs++;
1358b6cee71dSXin LI 			goto out;
1359b6cee71dSXin LI 		}
1360b6cee71dSXin LI 		while ((d = readdir(dir)) != NULL) {
136140427ccaSGordon Tetlow 			if (d->d_name[0] == '.')
136240427ccaSGordon Tetlow 				continue;
1363b6cee71dSXin LI 			if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
1364b6cee71dSXin LI 				file_oomem(ms,
1365b6cee71dSXin LI 				    strlen(fn) + strlen(d->d_name) + 2);
1366b6cee71dSXin LI 				errs++;
1367b6cee71dSXin LI 				closedir(dir);
1368b6cee71dSXin LI 				goto out;
1369b6cee71dSXin LI 			}
1370b6cee71dSXin LI 			if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
1371b6cee71dSXin LI 				free(mfn);
1372b6cee71dSXin LI 				continue;
1373b6cee71dSXin LI 			}
1374b6cee71dSXin LI 			if (files >= maxfiles) {
1375b6cee71dSXin LI 				size_t mlen;
1376b6cee71dSXin LI 				maxfiles = (maxfiles + 1) * 2;
1377b6cee71dSXin LI 				mlen = maxfiles * sizeof(*filearr);
1378b6cee71dSXin LI 				if ((filearr = CAST(char **,
1379b6cee71dSXin LI 				    realloc(filearr, mlen))) == NULL) {
1380b6cee71dSXin LI 					file_oomem(ms, mlen);
1381b6cee71dSXin LI 					free(mfn);
1382b6cee71dSXin LI 					closedir(dir);
1383b6cee71dSXin LI 					errs++;
1384b6cee71dSXin LI 					goto out;
1385b6cee71dSXin LI 				}
1386b6cee71dSXin LI 			}
1387b6cee71dSXin LI 			filearr[files++] = mfn;
1388b6cee71dSXin LI 		}
1389b6cee71dSXin LI 		closedir(dir);
1390b6cee71dSXin LI 		qsort(filearr, files, sizeof(*filearr), cmpstrp);
1391b6cee71dSXin LI 		for (i = 0; i < files; i++) {
1392b6cee71dSXin LI 			load_1(ms, action, filearr[i], &errs, mset);
1393b6cee71dSXin LI 			free(filearr[i]);
1394b6cee71dSXin LI 		}
1395b6cee71dSXin LI 		free(filearr);
1396b6cee71dSXin LI 	} else
1397b6cee71dSXin LI 		load_1(ms, action, fn, &errs, mset);
1398b6cee71dSXin LI 	if (errs)
1399b6cee71dSXin LI 		goto out;
1400b6cee71dSXin LI 
1401b6cee71dSXin LI 	for (j = 0; j < MAGIC_SETS; j++) {
1402b6cee71dSXin LI 		/* Set types of tests */
1403b6cee71dSXin LI 		for (i = 0; i < mset[j].count; ) {
1404b6cee71dSXin LI 			if (mset[j].me[i].mp->cont_level != 0) {
1405b6cee71dSXin LI 				i++;
1406b6cee71dSXin LI 				continue;
1407b6cee71dSXin LI 			}
1408b6cee71dSXin LI 			i = set_text_binary(ms, mset[j].me, mset[j].count, i);
1409b6cee71dSXin LI 		}
14109ce06829SXin LI 		if (mset[j].me)
1411b6cee71dSXin LI 			qsort(mset[j].me, mset[j].count, sizeof(*mset[j].me),
1412b6cee71dSXin LI 			    apprentice_sort);
1413b6cee71dSXin LI 
1414b6cee71dSXin LI 		/*
1415b6cee71dSXin LI 		 * Make sure that any level 0 "default" line is last
1416b6cee71dSXin LI 		 * (if one exists).
1417b6cee71dSXin LI 		 */
1418b6cee71dSXin LI 		set_last_default(ms, mset[j].me, mset[j].count);
1419b6cee71dSXin LI 
1420b6cee71dSXin LI 		/* coalesce per file arrays into a single one */
1421b6cee71dSXin LI 		if (coalesce_entries(ms, mset[j].me, mset[j].count,
1422b6cee71dSXin LI 		    &map->magic[j], &map->nmagic[j]) == -1) {
1423b6cee71dSXin LI 			errs++;
1424b6cee71dSXin LI 			goto out;
1425b6cee71dSXin LI 		}
1426b6cee71dSXin LI 	}
1427b6cee71dSXin LI 
1428b6cee71dSXin LI out:
1429b6cee71dSXin LI 	for (j = 0; j < MAGIC_SETS; j++)
1430b6cee71dSXin LI 		magic_entry_free(mset[j].me, mset[j].count);
1431b6cee71dSXin LI 
1432b6cee71dSXin LI 	if (errs) {
1433b6cee71dSXin LI 		apprentice_unmap(map);
1434b6cee71dSXin LI 		return NULL;
1435b6cee71dSXin LI 	}
1436b6cee71dSXin LI 	return map;
1437b6cee71dSXin LI }
1438b6cee71dSXin LI 
1439b6cee71dSXin LI /*
1440b6cee71dSXin LI  * extend the sign bit if the comparison is to be signed
1441b6cee71dSXin LI  */
1442b6cee71dSXin LI protected uint64_t
1443b6cee71dSXin LI file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
1444b6cee71dSXin LI {
1445b6cee71dSXin LI 	if (!(m->flag & UNSIGNED)) {
1446b6cee71dSXin LI 		switch(m->type) {
1447b6cee71dSXin LI 		/*
1448b6cee71dSXin LI 		 * Do not remove the casts below.  They are
1449b6cee71dSXin LI 		 * vital.  When later compared with the data,
1450b6cee71dSXin LI 		 * the sign extension must have happened.
1451b6cee71dSXin LI 		 */
1452b6cee71dSXin LI 		case FILE_BYTE:
1453c2931133SXin LI 			v = (signed char) v;
1454b6cee71dSXin LI 			break;
1455b6cee71dSXin LI 		case FILE_SHORT:
1456b6cee71dSXin LI 		case FILE_BESHORT:
1457b6cee71dSXin LI 		case FILE_LESHORT:
1458b6cee71dSXin LI 			v = (short) v;
1459b6cee71dSXin LI 			break;
1460b6cee71dSXin LI 		case FILE_DATE:
1461b6cee71dSXin LI 		case FILE_BEDATE:
1462b6cee71dSXin LI 		case FILE_LEDATE:
1463b6cee71dSXin LI 		case FILE_MEDATE:
1464b6cee71dSXin LI 		case FILE_LDATE:
1465b6cee71dSXin LI 		case FILE_BELDATE:
1466b6cee71dSXin LI 		case FILE_LELDATE:
1467b6cee71dSXin LI 		case FILE_MELDATE:
1468b6cee71dSXin LI 		case FILE_LONG:
1469b6cee71dSXin LI 		case FILE_BELONG:
1470b6cee71dSXin LI 		case FILE_LELONG:
1471b6cee71dSXin LI 		case FILE_MELONG:
1472b6cee71dSXin LI 		case FILE_FLOAT:
1473b6cee71dSXin LI 		case FILE_BEFLOAT:
1474b6cee71dSXin LI 		case FILE_LEFLOAT:
1475b6cee71dSXin LI 			v = (int32_t) v;
1476b6cee71dSXin LI 			break;
1477b6cee71dSXin LI 		case FILE_QUAD:
1478b6cee71dSXin LI 		case FILE_BEQUAD:
1479b6cee71dSXin LI 		case FILE_LEQUAD:
1480b6cee71dSXin LI 		case FILE_QDATE:
1481b6cee71dSXin LI 		case FILE_QLDATE:
1482b6cee71dSXin LI 		case FILE_QWDATE:
1483b6cee71dSXin LI 		case FILE_BEQDATE:
1484b6cee71dSXin LI 		case FILE_BEQLDATE:
1485b6cee71dSXin LI 		case FILE_BEQWDATE:
1486b6cee71dSXin LI 		case FILE_LEQDATE:
1487b6cee71dSXin LI 		case FILE_LEQLDATE:
1488b6cee71dSXin LI 		case FILE_LEQWDATE:
1489b6cee71dSXin LI 		case FILE_DOUBLE:
1490b6cee71dSXin LI 		case FILE_BEDOUBLE:
1491b6cee71dSXin LI 		case FILE_LEDOUBLE:
1492b6cee71dSXin LI 			v = (int64_t) v;
1493b6cee71dSXin LI 			break;
1494b6cee71dSXin LI 		case FILE_STRING:
1495b6cee71dSXin LI 		case FILE_PSTRING:
1496b6cee71dSXin LI 		case FILE_BESTRING16:
1497b6cee71dSXin LI 		case FILE_LESTRING16:
1498b6cee71dSXin LI 		case FILE_REGEX:
1499b6cee71dSXin LI 		case FILE_SEARCH:
1500b6cee71dSXin LI 		case FILE_DEFAULT:
1501b6cee71dSXin LI 		case FILE_INDIRECT:
1502b6cee71dSXin LI 		case FILE_NAME:
1503b6cee71dSXin LI 		case FILE_USE:
1504b6cee71dSXin LI 		case FILE_CLEAR:
15053e41d09dSXin LI 		case FILE_DER:
1506b6cee71dSXin LI 			break;
1507b6cee71dSXin LI 		default:
1508b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
1509b6cee71dSXin LI 			    file_magwarn(ms, "cannot happen: m->type=%d\n",
1510b6cee71dSXin LI 				    m->type);
1511b6cee71dSXin LI 			return ~0U;
1512b6cee71dSXin LI 		}
1513b6cee71dSXin LI 	}
1514b6cee71dSXin LI 	return v;
1515b6cee71dSXin LI }
1516b6cee71dSXin LI 
1517b6cee71dSXin LI private int
1518b6cee71dSXin LI string_modifier_check(struct magic_set *ms, struct magic *m)
1519b6cee71dSXin LI {
1520b6cee71dSXin LI 	if ((ms->flags & MAGIC_CHECK) == 0)
1521b6cee71dSXin LI 		return 0;
1522b6cee71dSXin LI 
1523b6cee71dSXin LI 	if ((m->type != FILE_REGEX || (m->str_flags & REGEX_LINE_COUNT) == 0) &&
1524b6cee71dSXin LI 	    (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0)) {
1525b6cee71dSXin LI 		file_magwarn(ms,
1526b6cee71dSXin LI 		    "'/BHhLl' modifiers are only allowed for pascal strings\n");
1527b6cee71dSXin LI 		return -1;
1528b6cee71dSXin LI 	}
1529b6cee71dSXin LI 	switch (m->type) {
1530b6cee71dSXin LI 	case FILE_BESTRING16:
1531b6cee71dSXin LI 	case FILE_LESTRING16:
1532b6cee71dSXin LI 		if (m->str_flags != 0) {
1533b6cee71dSXin LI 			file_magwarn(ms,
1534b6cee71dSXin LI 			    "no modifiers allowed for 16-bit strings\n");
1535b6cee71dSXin LI 			return -1;
1536b6cee71dSXin LI 		}
1537b6cee71dSXin LI 		break;
1538b6cee71dSXin LI 	case FILE_STRING:
1539b6cee71dSXin LI 	case FILE_PSTRING:
1540b6cee71dSXin LI 		if ((m->str_flags & REGEX_OFFSET_START) != 0) {
1541b6cee71dSXin LI 			file_magwarn(ms,
1542b6cee71dSXin LI 			    "'/%c' only allowed on regex and search\n",
1543b6cee71dSXin LI 			    CHAR_REGEX_OFFSET_START);
1544b6cee71dSXin LI 			return -1;
1545b6cee71dSXin LI 		}
1546b6cee71dSXin LI 		break;
1547b6cee71dSXin LI 	case FILE_SEARCH:
1548b6cee71dSXin LI 		if (m->str_range == 0) {
1549b6cee71dSXin LI 			file_magwarn(ms,
1550b6cee71dSXin LI 			    "missing range; defaulting to %d\n",
1551b6cee71dSXin LI                             STRING_DEFAULT_RANGE);
1552b6cee71dSXin LI 			m->str_range = STRING_DEFAULT_RANGE;
1553b6cee71dSXin LI 			return -1;
1554b6cee71dSXin LI 		}
1555b6cee71dSXin LI 		break;
1556b6cee71dSXin LI 	case FILE_REGEX:
1557b6cee71dSXin LI 		if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) {
1558b6cee71dSXin LI 			file_magwarn(ms, "'/%c' not allowed on regex\n",
1559b6cee71dSXin LI 			    CHAR_COMPACT_WHITESPACE);
1560b6cee71dSXin LI 			return -1;
1561b6cee71dSXin LI 		}
1562b6cee71dSXin LI 		if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) {
1563b6cee71dSXin LI 			file_magwarn(ms, "'/%c' not allowed on regex\n",
1564b6cee71dSXin LI 			    CHAR_COMPACT_OPTIONAL_WHITESPACE);
1565b6cee71dSXin LI 			return -1;
1566b6cee71dSXin LI 		}
1567b6cee71dSXin LI 		break;
1568b6cee71dSXin LI 	default:
1569b6cee71dSXin LI 		file_magwarn(ms, "coding error: m->type=%d\n",
1570b6cee71dSXin LI 		    m->type);
1571b6cee71dSXin LI 		return -1;
1572b6cee71dSXin LI 	}
1573b6cee71dSXin LI 	return 0;
1574b6cee71dSXin LI }
1575b6cee71dSXin LI 
1576b6cee71dSXin LI private int
1577b6cee71dSXin LI get_op(char c)
1578b6cee71dSXin LI {
1579b6cee71dSXin LI 	switch (c) {
1580b6cee71dSXin LI 	case '&':
1581b6cee71dSXin LI 		return FILE_OPAND;
1582b6cee71dSXin LI 	case '|':
1583b6cee71dSXin LI 		return FILE_OPOR;
1584b6cee71dSXin LI 	case '^':
1585b6cee71dSXin LI 		return FILE_OPXOR;
1586b6cee71dSXin LI 	case '+':
1587b6cee71dSXin LI 		return FILE_OPADD;
1588b6cee71dSXin LI 	case '-':
1589b6cee71dSXin LI 		return FILE_OPMINUS;
1590b6cee71dSXin LI 	case '*':
1591b6cee71dSXin LI 		return FILE_OPMULTIPLY;
1592b6cee71dSXin LI 	case '/':
1593b6cee71dSXin LI 		return FILE_OPDIVIDE;
1594b6cee71dSXin LI 	case '%':
1595b6cee71dSXin LI 		return FILE_OPMODULO;
1596b6cee71dSXin LI 	default:
1597b6cee71dSXin LI 		return -1;
1598b6cee71dSXin LI 	}
1599b6cee71dSXin LI }
1600b6cee71dSXin LI 
1601b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS
1602b6cee71dSXin LI private int
1603b6cee71dSXin LI get_cond(const char *l, const char **t)
1604b6cee71dSXin LI {
1605b6cee71dSXin LI 	static const struct cond_tbl_s {
1606b6cee71dSXin LI 		char name[8];
1607b6cee71dSXin LI 		size_t len;
1608b6cee71dSXin LI 		int cond;
1609b6cee71dSXin LI 	} cond_tbl[] = {
1610b6cee71dSXin LI 		{ "if",		2,	COND_IF },
1611b6cee71dSXin LI 		{ "elif",	4,	COND_ELIF },
1612b6cee71dSXin LI 		{ "else",	4,	COND_ELSE },
1613b6cee71dSXin LI 		{ "",		0,	COND_NONE },
1614b6cee71dSXin LI 	};
1615b6cee71dSXin LI 	const struct cond_tbl_s *p;
1616b6cee71dSXin LI 
1617b6cee71dSXin LI 	for (p = cond_tbl; p->len; p++) {
1618b6cee71dSXin LI 		if (strncmp(l, p->name, p->len) == 0 &&
1619b6cee71dSXin LI 		    isspace((unsigned char)l[p->len])) {
1620b6cee71dSXin LI 			if (t)
1621b6cee71dSXin LI 				*t = l + p->len;
1622b6cee71dSXin LI 			break;
1623b6cee71dSXin LI 		}
1624b6cee71dSXin LI 	}
1625b6cee71dSXin LI 	return p->cond;
1626b6cee71dSXin LI }
1627b6cee71dSXin LI 
1628b6cee71dSXin LI private int
1629b6cee71dSXin LI check_cond(struct magic_set *ms, int cond, uint32_t cont_level)
1630b6cee71dSXin LI {
1631b6cee71dSXin LI 	int last_cond;
1632b6cee71dSXin LI 	last_cond = ms->c.li[cont_level].last_cond;
1633b6cee71dSXin LI 
1634b6cee71dSXin LI 	switch (cond) {
1635b6cee71dSXin LI 	case COND_IF:
1636b6cee71dSXin LI 		if (last_cond != COND_NONE && last_cond != COND_ELIF) {
1637b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
1638b6cee71dSXin LI 				file_magwarn(ms, "syntax error: `if'");
1639b6cee71dSXin LI 			return -1;
1640b6cee71dSXin LI 		}
1641b6cee71dSXin LI 		last_cond = COND_IF;
1642b6cee71dSXin LI 		break;
1643b6cee71dSXin LI 
1644b6cee71dSXin LI 	case COND_ELIF:
1645b6cee71dSXin LI 		if (last_cond != COND_IF && last_cond != COND_ELIF) {
1646b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
1647b6cee71dSXin LI 				file_magwarn(ms, "syntax error: `elif'");
1648b6cee71dSXin LI 			return -1;
1649b6cee71dSXin LI 		}
1650b6cee71dSXin LI 		last_cond = COND_ELIF;
1651b6cee71dSXin LI 		break;
1652b6cee71dSXin LI 
1653b6cee71dSXin LI 	case COND_ELSE:
1654b6cee71dSXin LI 		if (last_cond != COND_IF && last_cond != COND_ELIF) {
1655b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
1656b6cee71dSXin LI 				file_magwarn(ms, "syntax error: `else'");
1657b6cee71dSXin LI 			return -1;
1658b6cee71dSXin LI 		}
1659b6cee71dSXin LI 		last_cond = COND_NONE;
1660b6cee71dSXin LI 		break;
1661b6cee71dSXin LI 
1662b6cee71dSXin LI 	case COND_NONE:
1663b6cee71dSXin LI 		last_cond = COND_NONE;
1664b6cee71dSXin LI 		break;
1665b6cee71dSXin LI 	}
1666b6cee71dSXin LI 
1667b6cee71dSXin LI 	ms->c.li[cont_level].last_cond = last_cond;
1668b6cee71dSXin LI 	return 0;
1669b6cee71dSXin LI }
1670b6cee71dSXin LI #endif /* ENABLE_CONDITIONALS */
1671b6cee71dSXin LI 
16724460e5b0SXin LI private int
16734460e5b0SXin LI parse_indirect_modifier(struct magic_set *ms, struct magic *m, const char **lp)
16744460e5b0SXin LI {
16754460e5b0SXin LI 	const char *l = *lp;
16764460e5b0SXin LI 
16774460e5b0SXin LI 	while (!isspace((unsigned char)*++l))
16784460e5b0SXin LI 		switch (*l) {
16794460e5b0SXin LI 		case CHAR_INDIRECT_RELATIVE:
16804460e5b0SXin LI 			m->str_flags |= INDIRECT_RELATIVE;
16814460e5b0SXin LI 			break;
16824460e5b0SXin LI 		default:
16834460e5b0SXin LI 			if (ms->flags & MAGIC_CHECK)
16844460e5b0SXin LI 				file_magwarn(ms, "indirect modifier `%c' "
16854460e5b0SXin LI 					"invalid", *l);
16864460e5b0SXin LI 			*lp = l;
16874460e5b0SXin LI 			return -1;
16884460e5b0SXin LI 		}
16894460e5b0SXin LI 	*lp = l;
16904460e5b0SXin LI 	return 0;
16914460e5b0SXin LI }
16924460e5b0SXin LI 
16934460e5b0SXin LI private void
16944460e5b0SXin LI parse_op_modifier(struct magic_set *ms, struct magic *m, const char **lp,
16954460e5b0SXin LI     int op)
16964460e5b0SXin LI {
16974460e5b0SXin LI 	const char *l = *lp;
16984460e5b0SXin LI 	char *t;
16994460e5b0SXin LI 	uint64_t val;
17004460e5b0SXin LI 
17014460e5b0SXin LI 	++l;
17024460e5b0SXin LI 	m->mask_op |= op;
17034460e5b0SXin LI 	val = (uint64_t)strtoull(l, &t, 0);
17044460e5b0SXin LI 	l = t;
17054460e5b0SXin LI 	m->num_mask = file_signextend(ms, m, val);
17064460e5b0SXin LI 	eatsize(&l);
17074460e5b0SXin LI 	*lp = l;
17084460e5b0SXin LI }
17094460e5b0SXin LI 
17104460e5b0SXin LI private int
17114460e5b0SXin LI parse_string_modifier(struct magic_set *ms, struct magic *m, const char **lp)
17124460e5b0SXin LI {
17134460e5b0SXin LI 	const char *l = *lp;
17144460e5b0SXin LI 	char *t;
17154460e5b0SXin LI 	int have_range = 0;
17164460e5b0SXin LI 
17174460e5b0SXin LI 	while (!isspace((unsigned char)*++l)) {
17184460e5b0SXin LI 		switch (*l) {
17194460e5b0SXin LI 		case '0':  case '1':  case '2':
17204460e5b0SXin LI 		case '3':  case '4':  case '5':
17214460e5b0SXin LI 		case '6':  case '7':  case '8':
17224460e5b0SXin LI 		case '9':
17234460e5b0SXin LI 			if (have_range && (ms->flags & MAGIC_CHECK))
17244460e5b0SXin LI 				file_magwarn(ms, "multiple ranges");
17254460e5b0SXin LI 			have_range = 1;
17264460e5b0SXin LI 			m->str_range = CAST(uint32_t, strtoul(l, &t, 0));
17274460e5b0SXin LI 			if (m->str_range == 0)
17284460e5b0SXin LI 				file_magwarn(ms, "zero range");
17294460e5b0SXin LI 			l = t - 1;
17304460e5b0SXin LI 			break;
17314460e5b0SXin LI 		case CHAR_COMPACT_WHITESPACE:
17324460e5b0SXin LI 			m->str_flags |= STRING_COMPACT_WHITESPACE;
17334460e5b0SXin LI 			break;
17344460e5b0SXin LI 		case CHAR_COMPACT_OPTIONAL_WHITESPACE:
17354460e5b0SXin LI 			m->str_flags |= STRING_COMPACT_OPTIONAL_WHITESPACE;
17364460e5b0SXin LI 			break;
17374460e5b0SXin LI 		case CHAR_IGNORE_LOWERCASE:
17384460e5b0SXin LI 			m->str_flags |= STRING_IGNORE_LOWERCASE;
17394460e5b0SXin LI 			break;
17404460e5b0SXin LI 		case CHAR_IGNORE_UPPERCASE:
17414460e5b0SXin LI 			m->str_flags |= STRING_IGNORE_UPPERCASE;
17424460e5b0SXin LI 			break;
17434460e5b0SXin LI 		case CHAR_REGEX_OFFSET_START:
17444460e5b0SXin LI 			m->str_flags |= REGEX_OFFSET_START;
17454460e5b0SXin LI 			break;
17464460e5b0SXin LI 		case CHAR_BINTEST:
17474460e5b0SXin LI 			m->str_flags |= STRING_BINTEST;
17484460e5b0SXin LI 			break;
17494460e5b0SXin LI 		case CHAR_TEXTTEST:
17504460e5b0SXin LI 			m->str_flags |= STRING_TEXTTEST;
17514460e5b0SXin LI 			break;
17524460e5b0SXin LI 		case CHAR_TRIM:
17534460e5b0SXin LI 			m->str_flags |= STRING_TRIM;
17544460e5b0SXin LI 			break;
17554460e5b0SXin LI 		case CHAR_PSTRING_1_LE:
17564460e5b0SXin LI #define SET_LENGTH(a) m->str_flags = (m->str_flags & ~PSTRING_LEN) | (a)
17574460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17584460e5b0SXin LI 				goto bad;
17594460e5b0SXin LI 			SET_LENGTH(PSTRING_1_LE);
17604460e5b0SXin LI 			break;
17614460e5b0SXin LI 		case CHAR_PSTRING_2_BE:
17624460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17634460e5b0SXin LI 				goto bad;
17644460e5b0SXin LI 			SET_LENGTH(PSTRING_2_BE);
17654460e5b0SXin LI 			break;
17664460e5b0SXin LI 		case CHAR_PSTRING_2_LE:
17674460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17684460e5b0SXin LI 				goto bad;
17694460e5b0SXin LI 			SET_LENGTH(PSTRING_2_LE);
17704460e5b0SXin LI 			break;
17714460e5b0SXin LI 		case CHAR_PSTRING_4_BE:
17724460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17734460e5b0SXin LI 				goto bad;
17744460e5b0SXin LI 			SET_LENGTH(PSTRING_4_BE);
17754460e5b0SXin LI 			break;
17764460e5b0SXin LI 		case CHAR_PSTRING_4_LE:
17774460e5b0SXin LI 			switch (m->type) {
17784460e5b0SXin LI 			case FILE_PSTRING:
17794460e5b0SXin LI 			case FILE_REGEX:
17804460e5b0SXin LI 				break;
17814460e5b0SXin LI 			default:
17824460e5b0SXin LI 				goto bad;
17834460e5b0SXin LI 			}
17844460e5b0SXin LI 			SET_LENGTH(PSTRING_4_LE);
17854460e5b0SXin LI 			break;
17864460e5b0SXin LI 		case CHAR_PSTRING_LENGTH_INCLUDES_ITSELF:
17874460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17884460e5b0SXin LI 				goto bad;
17894460e5b0SXin LI 			m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF;
17904460e5b0SXin LI 			break;
17914460e5b0SXin LI 		default:
17924460e5b0SXin LI 		bad:
17934460e5b0SXin LI 			if (ms->flags & MAGIC_CHECK)
17944460e5b0SXin LI 				file_magwarn(ms, "string modifier `%c' "
17954460e5b0SXin LI 					"invalid", *l);
17964460e5b0SXin LI 			goto out;
17974460e5b0SXin LI 		}
17984460e5b0SXin LI 		/* allow multiple '/' for readability */
17994460e5b0SXin LI 		if (l[1] == '/' && !isspace((unsigned char)l[2]))
18004460e5b0SXin LI 			l++;
18014460e5b0SXin LI 	}
18024460e5b0SXin LI 	if (string_modifier_check(ms, m) == -1)
18034460e5b0SXin LI 		goto out;
18044460e5b0SXin LI 	*lp = l;
18054460e5b0SXin LI 	return 0;
18064460e5b0SXin LI out:
18074460e5b0SXin LI 	*lp = l;
18084460e5b0SXin LI 	return -1;
18094460e5b0SXin LI }
18104460e5b0SXin LI 
1811b6cee71dSXin LI /*
1812b6cee71dSXin LI  * parse one line from magic file, put into magic[index++] if valid
1813b6cee71dSXin LI  */
1814b6cee71dSXin LI private int
1815b6cee71dSXin LI parse(struct magic_set *ms, struct magic_entry *me, const char *line,
1816b6cee71dSXin LI     size_t lineno, int action)
1817b6cee71dSXin LI {
1818b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS
1819b6cee71dSXin LI 	static uint32_t last_cont_level = 0;
1820b6cee71dSXin LI #endif
1821b6cee71dSXin LI 	size_t i;
1822b6cee71dSXin LI 	struct magic *m;
1823b6cee71dSXin LI 	const char *l = line;
1824b6cee71dSXin LI 	char *t;
1825b6cee71dSXin LI 	int op;
1826b6cee71dSXin LI 	uint32_t cont_level;
1827b6cee71dSXin LI 	int32_t diff;
1828b6cee71dSXin LI 
1829b6cee71dSXin LI 	cont_level = 0;
1830b6cee71dSXin LI 
1831b6cee71dSXin LI 	/*
1832b6cee71dSXin LI 	 * Parse the offset.
1833b6cee71dSXin LI 	 */
1834b6cee71dSXin LI 	while (*l == '>') {
1835b6cee71dSXin LI 		++l;		/* step over */
1836b6cee71dSXin LI 		cont_level++;
1837b6cee71dSXin LI 	}
1838b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS
1839b6cee71dSXin LI 	if (cont_level == 0 || cont_level > last_cont_level)
1840b6cee71dSXin LI 		if (file_check_mem(ms, cont_level) == -1)
1841b6cee71dSXin LI 			return -1;
1842b6cee71dSXin LI 	last_cont_level = cont_level;
1843b6cee71dSXin LI #endif
1844b6cee71dSXin LI 	if (cont_level != 0) {
1845b6cee71dSXin LI 		if (me->mp == NULL) {
1846b6cee71dSXin LI 			file_magerror(ms, "No current entry for continuation");
1847b6cee71dSXin LI 			return -1;
1848b6cee71dSXin LI 		}
1849b6cee71dSXin LI 		if (me->cont_count == 0) {
1850b6cee71dSXin LI 			file_magerror(ms, "Continuations present with 0 count");
1851b6cee71dSXin LI 			return -1;
1852b6cee71dSXin LI 		}
1853b6cee71dSXin LI 		m = &me->mp[me->cont_count - 1];
1854b6cee71dSXin LI 		diff = (int32_t)cont_level - (int32_t)m->cont_level;
1855b6cee71dSXin LI 		if (diff > 1)
1856b6cee71dSXin LI 			file_magwarn(ms, "New continuation level %u is more "
1857b6cee71dSXin LI 			    "than one larger than current level %u", cont_level,
1858b6cee71dSXin LI 			    m->cont_level);
1859b6cee71dSXin LI 		if (me->cont_count == me->max_count) {
1860b6cee71dSXin LI 			struct magic *nm;
1861b6cee71dSXin LI 			size_t cnt = me->max_count + ALLOC_CHUNK;
1862b6cee71dSXin LI 			if ((nm = CAST(struct magic *, realloc(me->mp,
1863b6cee71dSXin LI 			    sizeof(*nm) * cnt))) == NULL) {
1864b6cee71dSXin LI 				file_oomem(ms, sizeof(*nm) * cnt);
1865b6cee71dSXin LI 				return -1;
1866b6cee71dSXin LI 			}
1867b6cee71dSXin LI 			me->mp = m = nm;
1868b6cee71dSXin LI 			me->max_count = CAST(uint32_t, cnt);
1869b6cee71dSXin LI 		}
1870b6cee71dSXin LI 		m = &me->mp[me->cont_count++];
1871b6cee71dSXin LI 		(void)memset(m, 0, sizeof(*m));
1872b6cee71dSXin LI 		m->cont_level = cont_level;
1873b6cee71dSXin LI 	} else {
1874b6cee71dSXin LI 		static const size_t len = sizeof(*m) * ALLOC_CHUNK;
1875b6cee71dSXin LI 		if (me->mp != NULL)
1876b6cee71dSXin LI 			return 1;
1877b6cee71dSXin LI 		if ((m = CAST(struct magic *, malloc(len))) == NULL) {
1878b6cee71dSXin LI 			file_oomem(ms, len);
1879b6cee71dSXin LI 			return -1;
1880b6cee71dSXin LI 		}
1881b6cee71dSXin LI 		me->mp = m;
1882b6cee71dSXin LI 		me->max_count = ALLOC_CHUNK;
1883b6cee71dSXin LI 		(void)memset(m, 0, sizeof(*m));
1884b6cee71dSXin LI 		m->factor_op = FILE_FACTOR_OP_NONE;
1885b6cee71dSXin LI 		m->cont_level = 0;
1886b6cee71dSXin LI 		me->cont_count = 1;
1887b6cee71dSXin LI 	}
1888b6cee71dSXin LI 	m->lineno = CAST(uint32_t, lineno);
1889b6cee71dSXin LI 
1890b6cee71dSXin LI 	if (*l == '&') {  /* m->cont_level == 0 checked below. */
1891b6cee71dSXin LI                 ++l;            /* step over */
1892b6cee71dSXin LI                 m->flag |= OFFADD;
1893b6cee71dSXin LI         }
1894b6cee71dSXin LI 	if (*l == '(') {
1895b6cee71dSXin LI 		++l;		/* step over */
1896b6cee71dSXin LI 		m->flag |= INDIR;
1897b6cee71dSXin LI 		if (m->flag & OFFADD)
1898b6cee71dSXin LI 			m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
1899b6cee71dSXin LI 
1900b6cee71dSXin LI 		if (*l == '&') {  /* m->cont_level == 0 checked below */
1901b6cee71dSXin LI 			++l;            /* step over */
1902b6cee71dSXin LI 			m->flag |= OFFADD;
1903b6cee71dSXin LI 		}
1904b6cee71dSXin LI 	}
1905b6cee71dSXin LI 	/* Indirect offsets are not valid at level 0. */
19065f0216bdSXin LI 	if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD))) {
1907b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
1908b6cee71dSXin LI 			file_magwarn(ms, "relative offset at level 0");
19095f0216bdSXin LI 		return -1;
19105f0216bdSXin LI 	}
1911b6cee71dSXin LI 
1912b6cee71dSXin LI 	/* get offset, then skip over it */
1913*58a0f0d0SEitan Adler 	m->offset = (int32_t)strtol(l, &t, 0);
19145f0216bdSXin LI         if (l == t) {
1915b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
1916b6cee71dSXin LI 			file_magwarn(ms, "offset `%s' invalid", l);
19175f0216bdSXin LI 		return -1;
19185f0216bdSXin LI 	}
1919*58a0f0d0SEitan Adler #if 0
1920*58a0f0d0SEitan Adler         if (m->offset < 0 && cont_level != 0 &&
1921*58a0f0d0SEitan Adler 	    (m->flag & (OFFADD | INDIROFFADD)) == 0) {
1922*58a0f0d0SEitan Adler 		if (ms->flags & MAGIC_CHECK) {
1923*58a0f0d0SEitan Adler 			file_magwarn(ms,
1924*58a0f0d0SEitan Adler 			    "negative direct offset `%s' at level %u",
1925*58a0f0d0SEitan Adler 			    l, cont_level);
1926*58a0f0d0SEitan Adler 		}
1927*58a0f0d0SEitan Adler 		return -1;
1928*58a0f0d0SEitan Adler 	}
1929*58a0f0d0SEitan Adler #endif
1930b6cee71dSXin LI         l = t;
1931b6cee71dSXin LI 
1932b6cee71dSXin LI 	if (m->flag & INDIR) {
1933b6cee71dSXin LI 		m->in_type = FILE_LONG;
1934b6cee71dSXin LI 		m->in_offset = 0;
1935a5d223e6SXin LI 		m->in_op = 0;
1936b6cee71dSXin LI 		/*
1937a5d223e6SXin LI 		 * read [.,lbs][+-]nnnnn)
1938b6cee71dSXin LI 		 */
1939a5d223e6SXin LI 		if (*l == '.' || *l == ',') {
1940a5d223e6SXin LI 			if (*l == ',')
1941a5d223e6SXin LI 				m->in_op |= FILE_OPSIGNED;
1942b6cee71dSXin LI 			l++;
1943b6cee71dSXin LI 			switch (*l) {
1944b6cee71dSXin LI 			case 'l':
1945b6cee71dSXin LI 				m->in_type = FILE_LELONG;
1946b6cee71dSXin LI 				break;
1947b6cee71dSXin LI 			case 'L':
1948b6cee71dSXin LI 				m->in_type = FILE_BELONG;
1949b6cee71dSXin LI 				break;
1950b6cee71dSXin LI 			case 'm':
1951b6cee71dSXin LI 				m->in_type = FILE_MELONG;
1952b6cee71dSXin LI 				break;
1953b6cee71dSXin LI 			case 'h':
1954b6cee71dSXin LI 			case 's':
1955b6cee71dSXin LI 				m->in_type = FILE_LESHORT;
1956b6cee71dSXin LI 				break;
1957b6cee71dSXin LI 			case 'H':
1958b6cee71dSXin LI 			case 'S':
1959b6cee71dSXin LI 				m->in_type = FILE_BESHORT;
1960b6cee71dSXin LI 				break;
1961b6cee71dSXin LI 			case 'c':
1962b6cee71dSXin LI 			case 'b':
1963b6cee71dSXin LI 			case 'C':
1964b6cee71dSXin LI 			case 'B':
1965b6cee71dSXin LI 				m->in_type = FILE_BYTE;
1966b6cee71dSXin LI 				break;
1967b6cee71dSXin LI 			case 'e':
1968b6cee71dSXin LI 			case 'f':
1969b6cee71dSXin LI 			case 'g':
1970b6cee71dSXin LI 				m->in_type = FILE_LEDOUBLE;
1971b6cee71dSXin LI 				break;
1972b6cee71dSXin LI 			case 'E':
1973b6cee71dSXin LI 			case 'F':
1974b6cee71dSXin LI 			case 'G':
1975b6cee71dSXin LI 				m->in_type = FILE_BEDOUBLE;
1976b6cee71dSXin LI 				break;
1977b6cee71dSXin LI 			case 'i':
1978b6cee71dSXin LI 				m->in_type = FILE_LEID3;
1979b6cee71dSXin LI 				break;
1980b6cee71dSXin LI 			case 'I':
1981b6cee71dSXin LI 				m->in_type = FILE_BEID3;
1982b6cee71dSXin LI 				break;
1983b6cee71dSXin LI 			default:
1984b6cee71dSXin LI 				if (ms->flags & MAGIC_CHECK)
1985b6cee71dSXin LI 					file_magwarn(ms,
1986b6cee71dSXin LI 					    "indirect offset type `%c' invalid",
1987b6cee71dSXin LI 					    *l);
19885f0216bdSXin LI 				return -1;
1989b6cee71dSXin LI 			}
1990b6cee71dSXin LI 			l++;
1991b6cee71dSXin LI 		}
1992b6cee71dSXin LI 
1993b6cee71dSXin LI 		if (*l == '~') {
1994b6cee71dSXin LI 			m->in_op |= FILE_OPINVERSE;
1995b6cee71dSXin LI 			l++;
1996b6cee71dSXin LI 		}
1997b6cee71dSXin LI 		if ((op = get_op(*l)) != -1) {
1998b6cee71dSXin LI 			m->in_op |= op;
1999b6cee71dSXin LI 			l++;
2000b6cee71dSXin LI 		}
2001b6cee71dSXin LI 		if (*l == '(') {
2002b6cee71dSXin LI 			m->in_op |= FILE_OPINDIRECT;
2003b6cee71dSXin LI 			l++;
2004b6cee71dSXin LI 		}
2005b6cee71dSXin LI 		if (isdigit((unsigned char)*l) || *l == '-') {
2006b6cee71dSXin LI 			m->in_offset = (int32_t)strtol(l, &t, 0);
20075f0216bdSXin LI 			if (l == t) {
2008b6cee71dSXin LI 				if (ms->flags & MAGIC_CHECK)
2009b6cee71dSXin LI 					file_magwarn(ms,
2010b6cee71dSXin LI 					    "in_offset `%s' invalid", l);
20115f0216bdSXin LI 				return -1;
20125f0216bdSXin LI 			}
2013b6cee71dSXin LI 			l = t;
2014b6cee71dSXin LI 		}
2015b6cee71dSXin LI 		if (*l++ != ')' ||
20165f0216bdSXin LI 		    ((m->in_op & FILE_OPINDIRECT) && *l++ != ')')) {
2017b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
2018b6cee71dSXin LI 				file_magwarn(ms,
2019b6cee71dSXin LI 				    "missing ')' in indirect offset");
20205f0216bdSXin LI 			return -1;
20215f0216bdSXin LI 		}
2022b6cee71dSXin LI 	}
2023b6cee71dSXin LI 	EATAB;
2024b6cee71dSXin LI 
2025b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS
2026b6cee71dSXin LI 	m->cond = get_cond(l, &l);
2027b6cee71dSXin LI 	if (check_cond(ms, m->cond, cont_level) == -1)
2028b6cee71dSXin LI 		return -1;
2029b6cee71dSXin LI 
2030b6cee71dSXin LI 	EATAB;
2031b6cee71dSXin LI #endif
2032b6cee71dSXin LI 
2033b6cee71dSXin LI 	/*
2034b6cee71dSXin LI 	 * Parse the type.
2035b6cee71dSXin LI 	 */
2036b6cee71dSXin LI 	if (*l == 'u') {
2037b6cee71dSXin LI 		/*
2038b6cee71dSXin LI 		 * Try it as a keyword type prefixed by "u"; match what
2039b6cee71dSXin LI 		 * follows the "u".  If that fails, try it as an SUS
2040b6cee71dSXin LI 		 * integer type.
2041b6cee71dSXin LI 		 */
2042b6cee71dSXin LI 		m->type = get_type(type_tbl, l + 1, &l);
2043b6cee71dSXin LI 		if (m->type == FILE_INVALID) {
2044b6cee71dSXin LI 			/*
2045b6cee71dSXin LI 			 * Not a keyword type; parse it as an SUS type,
2046b6cee71dSXin LI 			 * 'u' possibly followed by a number or C/S/L.
2047b6cee71dSXin LI 			 */
2048b6cee71dSXin LI 			m->type = get_standard_integer_type(l, &l);
2049b6cee71dSXin LI 		}
2050b6cee71dSXin LI 		/* It's unsigned. */
2051b6cee71dSXin LI 		if (m->type != FILE_INVALID)
2052b6cee71dSXin LI 			m->flag |= UNSIGNED;
2053b6cee71dSXin LI 	} else {
2054b6cee71dSXin LI 		/*
2055b6cee71dSXin LI 		 * Try it as a keyword type.  If that fails, try it as
2056b6cee71dSXin LI 		 * an SUS integer type if it begins with "d" or as an
2057b6cee71dSXin LI 		 * SUS string type if it begins with "s".  In any case,
2058b6cee71dSXin LI 		 * it's not unsigned.
2059b6cee71dSXin LI 		 */
2060b6cee71dSXin LI 		m->type = get_type(type_tbl, l, &l);
2061b6cee71dSXin LI 		if (m->type == FILE_INVALID) {
2062b6cee71dSXin LI 			/*
2063b6cee71dSXin LI 			 * Not a keyword type; parse it as an SUS type,
2064b6cee71dSXin LI 			 * either 'd' possibly followed by a number or
2065b6cee71dSXin LI 			 * C/S/L, or just 's'.
2066b6cee71dSXin LI 			 */
2067b6cee71dSXin LI 			if (*l == 'd')
2068b6cee71dSXin LI 				m->type = get_standard_integer_type(l, &l);
2069b6cee71dSXin LI 			else if (*l == 's' && !isalpha((unsigned char)l[1])) {
2070b6cee71dSXin LI 				m->type = FILE_STRING;
2071b6cee71dSXin LI 				++l;
2072b6cee71dSXin LI 			}
2073b6cee71dSXin LI 		}
2074b6cee71dSXin LI 	}
2075b6cee71dSXin LI 
2076b6cee71dSXin LI 	if (m->type == FILE_INVALID) {
2077b6cee71dSXin LI 		/* Not found - try it as a special keyword. */
2078b6cee71dSXin LI 		m->type = get_type(special_tbl, l, &l);
2079b6cee71dSXin LI 	}
2080b6cee71dSXin LI 
2081b6cee71dSXin LI 	if (m->type == FILE_INVALID) {
2082b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
2083b6cee71dSXin LI 			file_magwarn(ms, "type `%s' invalid", l);
2084b6cee71dSXin LI 		return -1;
2085b6cee71dSXin LI 	}
2086b6cee71dSXin LI 
2087b6cee71dSXin LI 	/* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
2088b6cee71dSXin LI 	/* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
2089b6cee71dSXin LI 
2090b6cee71dSXin LI 	m->mask_op = 0;
2091b6cee71dSXin LI 	if (*l == '~') {
2092b6cee71dSXin LI 		if (!IS_STRING(m->type))
2093b6cee71dSXin LI 			m->mask_op |= FILE_OPINVERSE;
2094b6cee71dSXin LI 		else if (ms->flags & MAGIC_CHECK)
2095b6cee71dSXin LI 			file_magwarn(ms, "'~' invalid for string types");
2096b6cee71dSXin LI 		++l;
2097b6cee71dSXin LI 	}
2098b6cee71dSXin LI 	m->str_range = 0;
2099b6cee71dSXin LI 	m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
2100b6cee71dSXin LI 	if ((op = get_op(*l)) != -1) {
21014460e5b0SXin LI 		if (IS_STRING(m->type)) {
21024460e5b0SXin LI 			int r;
21034460e5b0SXin LI 
21044460e5b0SXin LI 			if (op != FILE_OPDIVIDE) {
2105b6cee71dSXin LI 				if (ms->flags & MAGIC_CHECK)
2106b6cee71dSXin LI 					file_magwarn(ms,
21074460e5b0SXin LI 					    "invalid string/indirect op: "
21084460e5b0SXin LI 					    "`%c'", *t);
2109b6cee71dSXin LI 				return -1;
2110b6cee71dSXin LI 			}
21114460e5b0SXin LI 
21124460e5b0SXin LI 			if (m->type == FILE_INDIRECT)
21134460e5b0SXin LI 				r = parse_indirect_modifier(ms, m, &l);
21144460e5b0SXin LI 			else
21154460e5b0SXin LI 				r = parse_string_modifier(ms, m, &l);
21164460e5b0SXin LI 			if (r == -1)
2117b6cee71dSXin LI 				return -1;
21184460e5b0SXin LI 		} else
21194460e5b0SXin LI 			parse_op_modifier(ms, m, &l, op);
2120b6cee71dSXin LI 	}
21214460e5b0SXin LI 
2122b6cee71dSXin LI 	/*
2123b6cee71dSXin LI 	 * We used to set mask to all 1's here, instead let's just not do
2124b6cee71dSXin LI 	 * anything if mask = 0 (unless you have a better idea)
2125b6cee71dSXin LI 	 */
2126b6cee71dSXin LI 	EATAB;
2127b6cee71dSXin LI 
2128b6cee71dSXin LI 	switch (*l) {
2129b6cee71dSXin LI 	case '>':
2130b6cee71dSXin LI 	case '<':
2131b6cee71dSXin LI   		m->reln = *l;
2132b6cee71dSXin LI   		++l;
2133b6cee71dSXin LI 		if (*l == '=') {
2134b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK) {
2135b6cee71dSXin LI 				file_magwarn(ms, "%c= not supported",
2136b6cee71dSXin LI 				    m->reln);
2137b6cee71dSXin LI 				return -1;
2138b6cee71dSXin LI 			}
2139b6cee71dSXin LI 		   ++l;
2140b6cee71dSXin LI 		}
2141b6cee71dSXin LI 		break;
2142b6cee71dSXin LI 	/* Old-style anding: "0 byte &0x80 dynamically linked" */
2143b6cee71dSXin LI 	case '&':
2144b6cee71dSXin LI 	case '^':
2145b6cee71dSXin LI 	case '=':
2146b6cee71dSXin LI   		m->reln = *l;
2147b6cee71dSXin LI   		++l;
2148b6cee71dSXin LI 		if (*l == '=') {
2149b6cee71dSXin LI 		   /* HP compat: ignore &= etc. */
2150b6cee71dSXin LI 		   ++l;
2151b6cee71dSXin LI 		}
2152b6cee71dSXin LI 		break;
2153b6cee71dSXin LI 	case '!':
2154b6cee71dSXin LI 		m->reln = *l;
2155b6cee71dSXin LI 		++l;
2156b6cee71dSXin LI 		break;
2157b6cee71dSXin LI 	default:
2158b6cee71dSXin LI   		m->reln = '=';	/* the default relation */
2159b6cee71dSXin LI 		if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
2160b6cee71dSXin LI 		    isspace((unsigned char)l[1])) || !l[1])) {
2161b6cee71dSXin LI 			m->reln = *l;
2162b6cee71dSXin LI 			++l;
2163b6cee71dSXin LI 		}
2164b6cee71dSXin LI 		break;
2165b6cee71dSXin LI 	}
2166b6cee71dSXin LI 	/*
2167b6cee71dSXin LI 	 * Grab the value part, except for an 'x' reln.
2168b6cee71dSXin LI 	 */
2169b6cee71dSXin LI 	if (m->reln != 'x' && getvalue(ms, m, &l, action))
2170b6cee71dSXin LI 		return -1;
2171b6cee71dSXin LI 
2172b6cee71dSXin LI 	/*
2173b6cee71dSXin LI 	 * TODO finish this macro and start using it!
21743e41d09dSXin LI 	 * #define offsetcheck {if (offset > ms->bytes_max -1)
2175b6cee71dSXin LI 	 *	magwarn("offset too big"); }
2176b6cee71dSXin LI 	 */
2177b6cee71dSXin LI 
2178b6cee71dSXin LI 	/*
2179b6cee71dSXin LI 	 * Now get last part - the description
2180b6cee71dSXin LI 	 */
2181b6cee71dSXin LI 	EATAB;
2182b6cee71dSXin LI 	if (l[0] == '\b') {
2183b6cee71dSXin LI 		++l;
2184b6cee71dSXin LI 		m->flag |= NOSPACE;
2185b6cee71dSXin LI 	} else if ((l[0] == '\\') && (l[1] == 'b')) {
2186b6cee71dSXin LI 		++l;
2187b6cee71dSXin LI 		++l;
2188b6cee71dSXin LI 		m->flag |= NOSPACE;
2189b6cee71dSXin LI 	}
2190b6cee71dSXin LI 	for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); )
2191b6cee71dSXin LI 		continue;
2192b6cee71dSXin LI 	if (i == sizeof(m->desc)) {
2193b6cee71dSXin LI 		m->desc[sizeof(m->desc) - 1] = '\0';
2194b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
2195b6cee71dSXin LI 			file_magwarn(ms, "description `%s' truncated", m->desc);
2196b6cee71dSXin LI 	}
2197b6cee71dSXin LI 
2198b6cee71dSXin LI         /*
2199b6cee71dSXin LI 	 * We only do this check while compiling, or if any of the magic
2200b6cee71dSXin LI 	 * files were not compiled.
2201b6cee71dSXin LI          */
2202b6cee71dSXin LI         if (ms->flags & MAGIC_CHECK) {
2203b6cee71dSXin LI 		if (check_format(ms, m) == -1)
2204b6cee71dSXin LI 			return -1;
2205b6cee71dSXin LI 	}
2206b6cee71dSXin LI #ifndef COMPILE_ONLY
2207b6cee71dSXin LI 	if (action == FILE_CHECK) {
2208b6cee71dSXin LI 		file_mdump(m);
2209b6cee71dSXin LI 	}
2210b6cee71dSXin LI #endif
2211b6cee71dSXin LI 	m->mimetype[0] = '\0';		/* initialise MIME type to none */
2212b6cee71dSXin LI 	return 0;
2213b6cee71dSXin LI }
2214b6cee71dSXin LI 
2215b6cee71dSXin LI /*
2216b6cee71dSXin LI  * parse a STRENGTH annotation line from magic file, put into magic[index - 1]
2217b6cee71dSXin LI  * if valid
2218b6cee71dSXin LI  */
2219b6cee71dSXin LI private int
2220b6cee71dSXin LI parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line)
2221b6cee71dSXin LI {
2222b6cee71dSXin LI 	const char *l = line;
2223b6cee71dSXin LI 	char *el;
2224b6cee71dSXin LI 	unsigned long factor;
2225b6cee71dSXin LI 	struct magic *m = &me->mp[0];
2226b6cee71dSXin LI 
2227b6cee71dSXin LI 	if (m->factor_op != FILE_FACTOR_OP_NONE) {
2228b6cee71dSXin LI 		file_magwarn(ms,
2229b6cee71dSXin LI 		    "Current entry already has a strength type: %c %d",
2230b6cee71dSXin LI 		    m->factor_op, m->factor);
2231b6cee71dSXin LI 		return -1;
2232b6cee71dSXin LI 	}
2233b6cee71dSXin LI 	if (m->type == FILE_NAME) {
2234b6cee71dSXin LI 		file_magwarn(ms, "%s: Strength setting is not supported in "
2235b6cee71dSXin LI 		    "\"name\" magic entries", m->value.s);
2236b6cee71dSXin LI 		return -1;
2237b6cee71dSXin LI 	}
2238b6cee71dSXin LI 	EATAB;
2239b6cee71dSXin LI 	switch (*l) {
2240b6cee71dSXin LI 	case FILE_FACTOR_OP_NONE:
2241b6cee71dSXin LI 	case FILE_FACTOR_OP_PLUS:
2242b6cee71dSXin LI 	case FILE_FACTOR_OP_MINUS:
2243b6cee71dSXin LI 	case FILE_FACTOR_OP_TIMES:
2244b6cee71dSXin LI 	case FILE_FACTOR_OP_DIV:
2245b6cee71dSXin LI 		m->factor_op = *l++;
2246b6cee71dSXin LI 		break;
2247b6cee71dSXin LI 	default:
2248b6cee71dSXin LI 		file_magwarn(ms, "Unknown factor op `%c'", *l);
2249b6cee71dSXin LI 		return -1;
2250b6cee71dSXin LI 	}
2251b6cee71dSXin LI 	EATAB;
2252b6cee71dSXin LI 	factor = strtoul(l, &el, 0);
2253b6cee71dSXin LI 	if (factor > 255) {
2254b6cee71dSXin LI 		file_magwarn(ms, "Too large factor `%lu'", factor);
2255b6cee71dSXin LI 		goto out;
2256b6cee71dSXin LI 	}
2257b6cee71dSXin LI 	if (*el && !isspace((unsigned char)*el)) {
2258b6cee71dSXin LI 		file_magwarn(ms, "Bad factor `%s'", l);
2259b6cee71dSXin LI 		goto out;
2260b6cee71dSXin LI 	}
2261b6cee71dSXin LI 	m->factor = (uint8_t)factor;
2262b6cee71dSXin LI 	if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) {
2263b6cee71dSXin LI 		file_magwarn(ms, "Cannot have factor op `%c' and factor %u",
2264b6cee71dSXin LI 		    m->factor_op, m->factor);
2265b6cee71dSXin LI 		goto out;
2266b6cee71dSXin LI 	}
2267b6cee71dSXin LI 	return 0;
2268b6cee71dSXin LI out:
2269b6cee71dSXin LI 	m->factor_op = FILE_FACTOR_OP_NONE;
2270b6cee71dSXin LI 	m->factor = 0;
2271b6cee71dSXin LI 	return -1;
2272b6cee71dSXin LI }
2273b6cee71dSXin LI 
2274b6cee71dSXin LI private int
2275c2931133SXin LI goodchar(unsigned char x, const char *extra)
2276c2931133SXin LI {
2277c2931133SXin LI 	return (isascii(x) && isalnum(x)) || strchr(extra, x);
2278c2931133SXin LI }
2279c2931133SXin LI 
2280c2931133SXin LI private int
2281b6cee71dSXin LI parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line,
2282c2931133SXin LI     off_t off, size_t len, const char *name, const char *extra, int nt)
2283b6cee71dSXin LI {
2284b6cee71dSXin LI 	size_t i;
2285b6cee71dSXin LI 	const char *l = line;
2286b6cee71dSXin LI 	struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
22875f0216bdSXin LI 	char *buf = CAST(char *, CAST(void *, m)) + off;
2288b6cee71dSXin LI 
2289b6cee71dSXin LI 	if (buf[0] != '\0') {
2290b6cee71dSXin LI 		len = nt ? strlen(buf) : len;
2291b6cee71dSXin LI 		file_magwarn(ms, "Current entry already has a %s type "
2292b6cee71dSXin LI 		    "`%.*s', new type `%s'", name, (int)len, buf, l);
2293b6cee71dSXin LI 		return -1;
2294b6cee71dSXin LI 	}
2295b6cee71dSXin LI 
2296b6cee71dSXin LI 	if (*m->desc == '\0') {
2297b6cee71dSXin LI 		file_magwarn(ms, "Current entry does not yet have a "
2298b6cee71dSXin LI 		    "description for adding a %s type", name);
2299b6cee71dSXin LI 		return -1;
2300b6cee71dSXin LI 	}
2301b6cee71dSXin LI 
2302b6cee71dSXin LI 	EATAB;
2303c2931133SXin LI 	for (i = 0; *l && i < len && goodchar(*l, extra); buf[i++] = *l++)
2304b6cee71dSXin LI 		continue;
2305b6cee71dSXin LI 
2306b6cee71dSXin LI 	if (i == len && *l) {
2307b6cee71dSXin LI 		if (nt)
2308b6cee71dSXin LI 			buf[len - 1] = '\0';
2309b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
2310b6cee71dSXin LI 			file_magwarn(ms, "%s type `%s' truncated %"
2311b6cee71dSXin LI 			    SIZE_T_FORMAT "u", name, line, i);
2312b6cee71dSXin LI 	} else {
2313c2931133SXin LI 		if (!isspace((unsigned char)*l) && !goodchar(*l, extra))
2314c2931133SXin LI 			file_magwarn(ms, "%s type `%s' has bad char '%c'",
2315c2931133SXin LI 			    name, line, *l);
2316b6cee71dSXin LI 		if (nt)
2317b6cee71dSXin LI 			buf[i] = '\0';
2318b6cee71dSXin LI 	}
2319b6cee71dSXin LI 
2320b6cee71dSXin LI 	if (i > 0)
2321b6cee71dSXin LI 		return 0;
2322c2931133SXin LI 
2323c2931133SXin LI 	file_magerror(ms, "Bad magic entry '%s'", line);
2324b6cee71dSXin LI 	return -1;
2325b6cee71dSXin LI }
2326b6cee71dSXin LI 
2327b6cee71dSXin LI /*
2328b6cee71dSXin LI  * Parse an Apple CREATOR/TYPE annotation from magic file and put it into
2329b6cee71dSXin LI  * magic[index - 1]
2330b6cee71dSXin LI  */
2331b6cee71dSXin LI private int
2332b6cee71dSXin LI parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line)
2333b6cee71dSXin LI {
2334b6cee71dSXin LI 	struct magic *m = &me->mp[0];
2335b6cee71dSXin LI 
23365f0216bdSXin LI 	return parse_extra(ms, me, line,
23375f0216bdSXin LI 	    CAST(off_t, offsetof(struct magic, apple)),
23383e41d09dSXin LI 	    sizeof(m->apple), "APPLE", "!+-./?", 0);
2339b6cee71dSXin LI }
2340b6cee71dSXin LI 
2341b6cee71dSXin LI /*
23425f0216bdSXin LI  * Parse a comma-separated list of extensions
23435f0216bdSXin LI  */
23445f0216bdSXin LI private int
23455f0216bdSXin LI parse_ext(struct magic_set *ms, struct magic_entry *me, const char *line)
23465f0216bdSXin LI {
23475f0216bdSXin LI 	struct magic *m = &me->mp[0];
23485f0216bdSXin LI 
23495f0216bdSXin LI 	return parse_extra(ms, me, line,
23505f0216bdSXin LI 	    CAST(off_t, offsetof(struct magic, ext)),
2351*58a0f0d0SEitan Adler 	    sizeof(m->ext), "EXTENSION", ",!+-/@?_$", 0);
23525f0216bdSXin LI }
23535f0216bdSXin LI 
23545f0216bdSXin LI /*
2355b6cee71dSXin LI  * parse a MIME annotation line from magic file, put into magic[index - 1]
2356b6cee71dSXin LI  * if valid
2357b6cee71dSXin LI  */
2358b6cee71dSXin LI private int
2359b6cee71dSXin LI parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line)
2360b6cee71dSXin LI {
2361b6cee71dSXin LI 	struct magic *m = &me->mp[0];
2362b6cee71dSXin LI 
23635f0216bdSXin LI 	return parse_extra(ms, me, line,
23645f0216bdSXin LI 	    CAST(off_t, offsetof(struct magic, mimetype)),
2365*58a0f0d0SEitan Adler 	    sizeof(m->mimetype), "MIME", "+-/.$?:{}", 1);
2366b6cee71dSXin LI }
2367b6cee71dSXin LI 
2368b6cee71dSXin LI private int
23693e41d09dSXin LI check_format_type(const char *ptr, int type, const char **estr)
2370b6cee71dSXin LI {
2371b6cee71dSXin LI 	int quad = 0, h;
23723e41d09dSXin LI 	size_t len, cnt;
2373b6cee71dSXin LI 	if (*ptr == '\0') {
2374b6cee71dSXin LI 		/* Missing format string; bad */
23753e41d09dSXin LI 		*estr = "missing format spec";
2376b6cee71dSXin LI 		return -1;
2377b6cee71dSXin LI 	}
2378b6cee71dSXin LI 
2379b6cee71dSXin LI 	switch (file_formats[type]) {
2380b6cee71dSXin LI 	case FILE_FMT_QUAD:
2381b6cee71dSXin LI 		quad = 1;
2382b6cee71dSXin LI 		/*FALLTHROUGH*/
2383b6cee71dSXin LI 	case FILE_FMT_NUM:
2384b6cee71dSXin LI 		if (quad == 0) {
2385b6cee71dSXin LI 			switch (type) {
2386b6cee71dSXin LI 			case FILE_BYTE:
2387b6cee71dSXin LI 				h = 2;
2388b6cee71dSXin LI 				break;
2389b6cee71dSXin LI 			case FILE_SHORT:
2390b6cee71dSXin LI 			case FILE_BESHORT:
2391b6cee71dSXin LI 			case FILE_LESHORT:
2392b6cee71dSXin LI 				h = 1;
2393b6cee71dSXin LI 				break;
2394b6cee71dSXin LI 			case FILE_LONG:
2395b6cee71dSXin LI 			case FILE_BELONG:
2396b6cee71dSXin LI 			case FILE_LELONG:
2397b6cee71dSXin LI 			case FILE_MELONG:
2398b6cee71dSXin LI 			case FILE_LEID3:
2399b6cee71dSXin LI 			case FILE_BEID3:
2400b6cee71dSXin LI 			case FILE_INDIRECT:
2401b6cee71dSXin LI 				h = 0;
2402b6cee71dSXin LI 				break;
2403b6cee71dSXin LI 			default:
2404b6cee71dSXin LI 				abort();
2405b6cee71dSXin LI 			}
2406b6cee71dSXin LI 		} else
2407b6cee71dSXin LI 			h = 0;
2408b6cee71dSXin LI 		if (*ptr == '-')
2409b6cee71dSXin LI 			ptr++;
2410b6cee71dSXin LI 		if (*ptr == '.')
2411b6cee71dSXin LI 			ptr++;
241240427ccaSGordon Tetlow 		if (*ptr == '#')
241340427ccaSGordon Tetlow 			ptr++;
24143e41d09dSXin LI #define CHECKLEN() do { \
24153e41d09dSXin LI 	for (len = cnt = 0; isdigit((unsigned char)*ptr); ptr++, cnt++) \
24163e41d09dSXin LI 		len = len * 10 + (*ptr - '0'); \
24173e41d09dSXin LI 	if (cnt > 5 || len > 1024) \
24183e41d09dSXin LI 		goto toolong; \
24193e41d09dSXin LI } while (/*CONSTCOND*/0)
24203e41d09dSXin LI 
24213e41d09dSXin LI 		CHECKLEN();
2422b6cee71dSXin LI 		if (*ptr == '.')
2423b6cee71dSXin LI 			ptr++;
24243e41d09dSXin LI 		CHECKLEN();
2425b6cee71dSXin LI 		if (quad) {
2426b6cee71dSXin LI 			if (*ptr++ != 'l')
24273e41d09dSXin LI 				goto invalid;
2428b6cee71dSXin LI 			if (*ptr++ != 'l')
24293e41d09dSXin LI 				goto invalid;
2430b6cee71dSXin LI 		}
2431b6cee71dSXin LI 
2432b6cee71dSXin LI 		switch (*ptr++) {
2433b6cee71dSXin LI #ifdef STRICT_FORMAT 	/* "long" formats are int formats for us */
2434b6cee71dSXin LI 		/* so don't accept the 'l' modifier */
2435b6cee71dSXin LI 		case 'l':
2436b6cee71dSXin LI 			switch (*ptr++) {
2437b6cee71dSXin LI 			case 'i':
2438b6cee71dSXin LI 			case 'd':
2439b6cee71dSXin LI 			case 'u':
2440b6cee71dSXin LI 			case 'o':
2441b6cee71dSXin LI 			case 'x':
2442b6cee71dSXin LI 			case 'X':
24433e41d09dSXin LI 				if (h == 0)
24443e41d09dSXin LI 					return 0;
24453e41d09dSXin LI 				/*FALLTHROUGH*/
2446b6cee71dSXin LI 			default:
24473e41d09dSXin LI 				goto invalid;
2448b6cee71dSXin LI 			}
2449b6cee71dSXin LI 
2450b6cee71dSXin LI 		/*
2451b6cee71dSXin LI 		 * Don't accept h and hh modifiers. They make writing
2452b6cee71dSXin LI 		 * magic entries more complicated, for very little benefit
2453b6cee71dSXin LI 		 */
2454b6cee71dSXin LI 		case 'h':
2455b6cee71dSXin LI 			if (h-- <= 0)
24563e41d09dSXin LI 				goto invalid;
2457b6cee71dSXin LI 			switch (*ptr++) {
2458b6cee71dSXin LI 			case 'h':
2459b6cee71dSXin LI 				if (h-- <= 0)
24603e41d09dSXin LI 					goto invalid;
2461b6cee71dSXin LI 				switch (*ptr++) {
2462b6cee71dSXin LI 				case 'i':
2463b6cee71dSXin LI 				case 'd':
2464b6cee71dSXin LI 				case 'u':
2465b6cee71dSXin LI 				case 'o':
2466b6cee71dSXin LI 				case 'x':
2467b6cee71dSXin LI 				case 'X':
2468b6cee71dSXin LI 					return 0;
2469b6cee71dSXin LI 				default:
24703e41d09dSXin LI 					goto invalid;
2471b6cee71dSXin LI 				}
2472b6cee71dSXin LI 			case 'i':
2473b6cee71dSXin LI 			case 'd':
2474b6cee71dSXin LI 			case 'u':
2475b6cee71dSXin LI 			case 'o':
2476b6cee71dSXin LI 			case 'x':
2477b6cee71dSXin LI 			case 'X':
24783e41d09dSXin LI 				if (h == 0)
24793e41d09dSXin LI 					return 0;
24803e41d09dSXin LI 				/*FALLTHROUGH*/
2481b6cee71dSXin LI 			default:
24823e41d09dSXin LI 				goto invalid;
2483b6cee71dSXin LI 			}
2484b6cee71dSXin LI #endif
2485b6cee71dSXin LI 		case 'c':
24863e41d09dSXin LI 			if (h == 2)
24873e41d09dSXin LI 				return 0;
24883e41d09dSXin LI 			goto invalid;
2489b6cee71dSXin LI 		case 'i':
2490b6cee71dSXin LI 		case 'd':
2491b6cee71dSXin LI 		case 'u':
2492b6cee71dSXin LI 		case 'o':
2493b6cee71dSXin LI 		case 'x':
2494b6cee71dSXin LI 		case 'X':
2495b6cee71dSXin LI #ifdef STRICT_FORMAT
24963e41d09dSXin LI 			if (h == 0)
24973e41d09dSXin LI 				return 0;
24983e41d09dSXin LI 			/*FALLTHROUGH*/
2499b6cee71dSXin LI #else
2500b6cee71dSXin LI 			return 0;
2501b6cee71dSXin LI #endif
2502b6cee71dSXin LI 		default:
25033e41d09dSXin LI 			goto invalid;
2504b6cee71dSXin LI 		}
2505b6cee71dSXin LI 
2506b6cee71dSXin LI 	case FILE_FMT_FLOAT:
2507b6cee71dSXin LI 	case FILE_FMT_DOUBLE:
2508b6cee71dSXin LI 		if (*ptr == '-')
2509b6cee71dSXin LI 			ptr++;
2510b6cee71dSXin LI 		if (*ptr == '.')
2511b6cee71dSXin LI 			ptr++;
25123e41d09dSXin LI 		CHECKLEN();
2513b6cee71dSXin LI 		if (*ptr == '.')
2514b6cee71dSXin LI 			ptr++;
25153e41d09dSXin LI 		CHECKLEN();
2516b6cee71dSXin LI 		switch (*ptr++) {
2517b6cee71dSXin LI 		case 'e':
2518b6cee71dSXin LI 		case 'E':
2519b6cee71dSXin LI 		case 'f':
2520b6cee71dSXin LI 		case 'F':
2521b6cee71dSXin LI 		case 'g':
2522b6cee71dSXin LI 		case 'G':
2523b6cee71dSXin LI 			return 0;
2524b6cee71dSXin LI 
2525b6cee71dSXin LI 		default:
25263e41d09dSXin LI 			goto invalid;
2527b6cee71dSXin LI 		}
2528b6cee71dSXin LI 
2529b6cee71dSXin LI 
2530b6cee71dSXin LI 	case FILE_FMT_STR:
2531b6cee71dSXin LI 		if (*ptr == '-')
2532b6cee71dSXin LI 			ptr++;
2533b6cee71dSXin LI 		while (isdigit((unsigned char )*ptr))
2534b6cee71dSXin LI 			ptr++;
2535b6cee71dSXin LI 		if (*ptr == '.') {
2536b6cee71dSXin LI 			ptr++;
2537b6cee71dSXin LI 			while (isdigit((unsigned char )*ptr))
2538b6cee71dSXin LI 				ptr++;
2539b6cee71dSXin LI 		}
2540b6cee71dSXin LI 
2541b6cee71dSXin LI 		switch (*ptr++) {
2542b6cee71dSXin LI 		case 's':
2543b6cee71dSXin LI 			return 0;
2544b6cee71dSXin LI 		default:
25453e41d09dSXin LI 			goto invalid;
2546b6cee71dSXin LI 		}
2547b6cee71dSXin LI 
2548b6cee71dSXin LI 	default:
2549b6cee71dSXin LI 		/* internal error */
2550b6cee71dSXin LI 		abort();
2551b6cee71dSXin LI 	}
25523e41d09dSXin LI invalid:
25533e41d09dSXin LI 	*estr = "not valid";
25543e41d09dSXin LI toolong:
25553e41d09dSXin LI 	*estr = "too long";
2556b6cee71dSXin LI 	return -1;
2557b6cee71dSXin LI }
2558b6cee71dSXin LI 
2559b6cee71dSXin LI /*
2560b6cee71dSXin LI  * Check that the optional printf format in description matches
2561b6cee71dSXin LI  * the type of the magic.
2562b6cee71dSXin LI  */
2563b6cee71dSXin LI private int
2564b6cee71dSXin LI check_format(struct magic_set *ms, struct magic *m)
2565b6cee71dSXin LI {
2566b6cee71dSXin LI 	char *ptr;
25673e41d09dSXin LI 	const char *estr;
2568b6cee71dSXin LI 
2569b6cee71dSXin LI 	for (ptr = m->desc; *ptr; ptr++)
2570b6cee71dSXin LI 		if (*ptr == '%')
2571b6cee71dSXin LI 			break;
2572b6cee71dSXin LI 	if (*ptr == '\0') {
2573b6cee71dSXin LI 		/* No format string; ok */
2574b6cee71dSXin LI 		return 1;
2575b6cee71dSXin LI 	}
2576b6cee71dSXin LI 
2577b6cee71dSXin LI 	assert(file_nformats == file_nnames);
2578b6cee71dSXin LI 
2579b6cee71dSXin LI 	if (m->type >= file_nformats) {
2580b6cee71dSXin LI 		file_magwarn(ms, "Internal error inconsistency between "
2581b6cee71dSXin LI 		    "m->type and format strings");
2582b6cee71dSXin LI 		return -1;
2583b6cee71dSXin LI 	}
2584b6cee71dSXin LI 	if (file_formats[m->type] == FILE_FMT_NONE) {
2585b6cee71dSXin LI 		file_magwarn(ms, "No format string for `%s' with description "
2586b6cee71dSXin LI 		    "`%s'", m->desc, file_names[m->type]);
2587b6cee71dSXin LI 		return -1;
2588b6cee71dSXin LI 	}
2589b6cee71dSXin LI 
2590b6cee71dSXin LI 	ptr++;
25913e41d09dSXin LI 	if (check_format_type(ptr, m->type, &estr) == -1) {
2592b6cee71dSXin LI 		/*
2593b6cee71dSXin LI 		 * TODO: this error message is unhelpful if the format
2594b6cee71dSXin LI 		 * string is not one character long
2595b6cee71dSXin LI 		 */
25963e41d09dSXin LI 		file_magwarn(ms, "Printf format is %s for type "
25973e41d09dSXin LI 		    "`%s' in description `%s'", estr,
2598b6cee71dSXin LI 		    file_names[m->type], m->desc);
2599b6cee71dSXin LI 		return -1;
2600b6cee71dSXin LI 	}
2601b6cee71dSXin LI 
2602b6cee71dSXin LI 	for (; *ptr; ptr++) {
2603b6cee71dSXin LI 		if (*ptr == '%') {
2604b6cee71dSXin LI 			file_magwarn(ms,
2605b6cee71dSXin LI 			    "Too many format strings (should have at most one) "
2606b6cee71dSXin LI 			    "for `%s' with description `%s'",
2607b6cee71dSXin LI 			    file_names[m->type], m->desc);
2608b6cee71dSXin LI 			return -1;
2609b6cee71dSXin LI 		}
2610b6cee71dSXin LI 	}
2611b6cee71dSXin LI 	return 0;
2612b6cee71dSXin LI }
2613b6cee71dSXin LI 
2614b6cee71dSXin LI /*
2615b6cee71dSXin LI  * Read a numeric value from a pointer, into the value union of a magic
2616b6cee71dSXin LI  * pointer, according to the magic type.  Update the string pointer to point
2617b6cee71dSXin LI  * just after the number read.  Return 0 for success, non-zero for failure.
2618b6cee71dSXin LI  */
2619b6cee71dSXin LI private int
2620b6cee71dSXin LI getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
2621b6cee71dSXin LI {
2622*58a0f0d0SEitan Adler 	char *ep;
2623*58a0f0d0SEitan Adler 	uint64_t ull;
2624*58a0f0d0SEitan Adler 
2625b6cee71dSXin LI 	switch (m->type) {
2626b6cee71dSXin LI 	case FILE_BESTRING16:
2627b6cee71dSXin LI 	case FILE_LESTRING16:
2628b6cee71dSXin LI 	case FILE_STRING:
2629b6cee71dSXin LI 	case FILE_PSTRING:
2630b6cee71dSXin LI 	case FILE_REGEX:
2631b6cee71dSXin LI 	case FILE_SEARCH:
2632b6cee71dSXin LI 	case FILE_NAME:
2633b6cee71dSXin LI 	case FILE_USE:
26343e41d09dSXin LI 	case FILE_DER:
2635b6cee71dSXin LI 		*p = getstr(ms, m, *p, action == FILE_COMPILE);
2636b6cee71dSXin LI 		if (*p == NULL) {
2637b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
2638b6cee71dSXin LI 				file_magwarn(ms, "cannot get string from `%s'",
2639b6cee71dSXin LI 				    m->value.s);
2640b6cee71dSXin LI 			return -1;
2641b6cee71dSXin LI 		}
2642b6cee71dSXin LI 		if (m->type == FILE_REGEX) {
2643b6cee71dSXin LI 			file_regex_t rx;
2644b6cee71dSXin LI 			int rc = file_regcomp(&rx, m->value.s, REG_EXTENDED);
2645b6cee71dSXin LI 			if (rc) {
2646b6cee71dSXin LI 				if (ms->flags & MAGIC_CHECK)
2647b6cee71dSXin LI 					file_regerror(&rx, rc, ms);
2648b6cee71dSXin LI 			}
2649b6cee71dSXin LI 			file_regfree(&rx);
2650b6cee71dSXin LI 			return rc ? -1 : 0;
2651b6cee71dSXin LI 		}
2652b6cee71dSXin LI 		return 0;
2653*58a0f0d0SEitan Adler 	default:
2654*58a0f0d0SEitan Adler 		if (m->reln == 'x')
2655*58a0f0d0SEitan Adler 			return 0;
2656*58a0f0d0SEitan Adler 		break;
2657*58a0f0d0SEitan Adler 	}
2658*58a0f0d0SEitan Adler 
2659*58a0f0d0SEitan Adler 	switch (m->type) {
2660b6cee71dSXin LI 	case FILE_FLOAT:
2661b6cee71dSXin LI 	case FILE_BEFLOAT:
2662b6cee71dSXin LI 	case FILE_LEFLOAT:
26639ce06829SXin LI 		errno = 0;
2664b6cee71dSXin LI #ifdef HAVE_STRTOF
2665b6cee71dSXin LI 		m->value.f = strtof(*p, &ep);
2666b6cee71dSXin LI #else
2667b6cee71dSXin LI 		m->value.f = (float)strtod(*p, &ep);
2668b6cee71dSXin LI #endif
26699ce06829SXin LI 		if (errno == 0)
2670b6cee71dSXin LI 			*p = ep;
2671b6cee71dSXin LI 		return 0;
2672b6cee71dSXin LI 	case FILE_DOUBLE:
2673b6cee71dSXin LI 	case FILE_BEDOUBLE:
2674b6cee71dSXin LI 	case FILE_LEDOUBLE:
26759ce06829SXin LI 		errno = 0;
2676b6cee71dSXin LI 		m->value.d = strtod(*p, &ep);
26779ce06829SXin LI 		if (errno == 0)
2678b6cee71dSXin LI 			*p = ep;
2679b6cee71dSXin LI 		return 0;
2680b6cee71dSXin LI 	default:
26819ce06829SXin LI 		errno = 0;
268240427ccaSGordon Tetlow 		ull = (uint64_t)strtoull(*p, &ep, 0);
268340427ccaSGordon Tetlow 		m->value.q = file_signextend(ms, m, ull);
268440427ccaSGordon Tetlow 		if (*p == ep) {
268540427ccaSGordon Tetlow 			file_magwarn(ms, "Unparseable number `%s'", *p);
268640427ccaSGordon Tetlow 		} else {
268740427ccaSGordon Tetlow 			size_t ts = typesize(m->type);
268840427ccaSGordon Tetlow 			uint64_t x;
268940427ccaSGordon Tetlow 			const char *q;
269040427ccaSGordon Tetlow 
269140427ccaSGordon Tetlow 			if (ts == (size_t)~0) {
2692*58a0f0d0SEitan Adler 				file_magwarn(ms,
2693*58a0f0d0SEitan Adler 				    "Expected numeric type got `%s'",
269440427ccaSGordon Tetlow 				    type_tbl[m->type].name);
269540427ccaSGordon Tetlow 			}
269640427ccaSGordon Tetlow 			for (q = *p; isspace((unsigned char)*q); q++)
269740427ccaSGordon Tetlow 				continue;
269840427ccaSGordon Tetlow 			if (*q == '-')
269940427ccaSGordon Tetlow 				ull = -(int64_t)ull;
270040427ccaSGordon Tetlow 			switch (ts) {
270140427ccaSGordon Tetlow 			case 1:
2702*58a0f0d0SEitan Adler 				x = (uint64_t)(ull & ~0xffULL);
270340427ccaSGordon Tetlow 				break;
270440427ccaSGordon Tetlow 			case 2:
2705*58a0f0d0SEitan Adler 				x = (uint64_t)(ull & ~0xffffULL);
270640427ccaSGordon Tetlow 				break;
270740427ccaSGordon Tetlow 			case 4:
2708*58a0f0d0SEitan Adler 				x = (uint64_t)(ull & ~0xffffffffULL);
270940427ccaSGordon Tetlow 				break;
271040427ccaSGordon Tetlow 			case 8:
271140427ccaSGordon Tetlow 				x = 0;
271240427ccaSGordon Tetlow 				break;
271340427ccaSGordon Tetlow 			default:
271440427ccaSGordon Tetlow 				abort();
271540427ccaSGordon Tetlow 			}
271640427ccaSGordon Tetlow 			if (x) {
2717*58a0f0d0SEitan Adler 				file_magwarn(ms, "Overflow for numeric"
2718*58a0f0d0SEitan Adler 				    " type `%s' value %#" PRIx64,
271940427ccaSGordon Tetlow 				    type_tbl[m->type].name, ull);
272040427ccaSGordon Tetlow 			}
272140427ccaSGordon Tetlow 		}
27229ce06829SXin LI 		if (errno == 0) {
2723b6cee71dSXin LI 			*p = ep;
2724b6cee71dSXin LI 			eatsize(p);
2725b6cee71dSXin LI 		}
2726b6cee71dSXin LI 		return 0;
2727b6cee71dSXin LI 	}
2728b6cee71dSXin LI }
2729b6cee71dSXin LI 
2730b6cee71dSXin LI /*
2731b6cee71dSXin LI  * Convert a string containing C character escapes.  Stop at an unescaped
2732b6cee71dSXin LI  * space or tab.
2733b6cee71dSXin LI  * Copy the converted version to "m->value.s", and the length in m->vallen.
2734b6cee71dSXin LI  * Return updated scan pointer as function result. Warn if set.
2735b6cee71dSXin LI  */
2736b6cee71dSXin LI private const char *
2737b6cee71dSXin LI getstr(struct magic_set *ms, struct magic *m, const char *s, int warn)
2738b6cee71dSXin LI {
2739b6cee71dSXin LI 	const char *origs = s;
2740b6cee71dSXin LI 	char	*p = m->value.s;
2741b6cee71dSXin LI 	size_t  plen = sizeof(m->value.s);
2742b6cee71dSXin LI 	char 	*origp = p;
2743b6cee71dSXin LI 	char	*pmax = p + plen - 1;
2744b6cee71dSXin LI 	int	c;
2745b6cee71dSXin LI 	int	val;
2746b6cee71dSXin LI 
2747b6cee71dSXin LI 	while ((c = *s++) != '\0') {
2748b6cee71dSXin LI 		if (isspace((unsigned char) c))
2749b6cee71dSXin LI 			break;
2750b6cee71dSXin LI 		if (p >= pmax) {
2751b6cee71dSXin LI 			file_error(ms, 0, "string too long: `%s'", origs);
2752b6cee71dSXin LI 			return NULL;
2753b6cee71dSXin LI 		}
2754b6cee71dSXin LI 		if (c == '\\') {
2755b6cee71dSXin LI 			switch(c = *s++) {
2756b6cee71dSXin LI 
2757b6cee71dSXin LI 			case '\0':
2758b6cee71dSXin LI 				if (warn)
2759b6cee71dSXin LI 					file_magwarn(ms, "incomplete escape");
27609ce06829SXin LI 				s--;
2761b6cee71dSXin LI 				goto out;
2762b6cee71dSXin LI 
2763b6cee71dSXin LI 			case '\t':
2764b6cee71dSXin LI 				if (warn) {
2765b6cee71dSXin LI 					file_magwarn(ms,
2766b6cee71dSXin LI 					    "escaped tab found, use \\t instead");
2767b6cee71dSXin LI 					warn = 0;	/* already did */
2768b6cee71dSXin LI 				}
2769b6cee71dSXin LI 				/*FALLTHROUGH*/
2770b6cee71dSXin LI 			default:
2771b6cee71dSXin LI 				if (warn) {
2772b6cee71dSXin LI 					if (isprint((unsigned char)c)) {
2773b6cee71dSXin LI 						/* Allow escaping of
2774b6cee71dSXin LI 						 * ``relations'' */
2775b6cee71dSXin LI 						if (strchr("<>&^=!", c) == NULL
2776b6cee71dSXin LI 						    && (m->type != FILE_REGEX ||
2777b6cee71dSXin LI 						    strchr("[]().*?^$|{}", c)
2778b6cee71dSXin LI 						    == NULL)) {
2779b6cee71dSXin LI 							file_magwarn(ms, "no "
2780b6cee71dSXin LI 							    "need to escape "
2781b6cee71dSXin LI 							    "`%c'", c);
2782b6cee71dSXin LI 						}
2783b6cee71dSXin LI 					} else {
2784b6cee71dSXin LI 						file_magwarn(ms,
2785b6cee71dSXin LI 						    "unknown escape sequence: "
2786b6cee71dSXin LI 						    "\\%03o", c);
2787b6cee71dSXin LI 					}
2788b6cee71dSXin LI 				}
2789b6cee71dSXin LI 				/*FALLTHROUGH*/
2790b6cee71dSXin LI 			/* space, perhaps force people to use \040? */
2791b6cee71dSXin LI 			case ' ':
2792b6cee71dSXin LI #if 0
2793b6cee71dSXin LI 			/*
2794b6cee71dSXin LI 			 * Other things people escape, but shouldn't need to,
2795b6cee71dSXin LI 			 * so we disallow them
2796b6cee71dSXin LI 			 */
2797b6cee71dSXin LI 			case '\'':
2798b6cee71dSXin LI 			case '"':
2799b6cee71dSXin LI 			case '?':
2800b6cee71dSXin LI #endif
2801b6cee71dSXin LI 			/* Relations */
2802b6cee71dSXin LI 			case '>':
2803b6cee71dSXin LI 			case '<':
2804b6cee71dSXin LI 			case '&':
2805b6cee71dSXin LI 			case '^':
2806b6cee71dSXin LI 			case '=':
2807b6cee71dSXin LI 			case '!':
2808b6cee71dSXin LI 			/* and baskslash itself */
2809b6cee71dSXin LI 			case '\\':
2810b6cee71dSXin LI 				*p++ = (char) c;
2811b6cee71dSXin LI 				break;
2812b6cee71dSXin LI 
2813b6cee71dSXin LI 			case 'a':
2814b6cee71dSXin LI 				*p++ = '\a';
2815b6cee71dSXin LI 				break;
2816b6cee71dSXin LI 
2817b6cee71dSXin LI 			case 'b':
2818b6cee71dSXin LI 				*p++ = '\b';
2819b6cee71dSXin LI 				break;
2820b6cee71dSXin LI 
2821b6cee71dSXin LI 			case 'f':
2822b6cee71dSXin LI 				*p++ = '\f';
2823b6cee71dSXin LI 				break;
2824b6cee71dSXin LI 
2825b6cee71dSXin LI 			case 'n':
2826b6cee71dSXin LI 				*p++ = '\n';
2827b6cee71dSXin LI 				break;
2828b6cee71dSXin LI 
2829b6cee71dSXin LI 			case 'r':
2830b6cee71dSXin LI 				*p++ = '\r';
2831b6cee71dSXin LI 				break;
2832b6cee71dSXin LI 
2833b6cee71dSXin LI 			case 't':
2834b6cee71dSXin LI 				*p++ = '\t';
2835b6cee71dSXin LI 				break;
2836b6cee71dSXin LI 
2837b6cee71dSXin LI 			case 'v':
2838b6cee71dSXin LI 				*p++ = '\v';
2839b6cee71dSXin LI 				break;
2840b6cee71dSXin LI 
2841b6cee71dSXin LI 			/* \ and up to 3 octal digits */
2842b6cee71dSXin LI 			case '0':
2843b6cee71dSXin LI 			case '1':
2844b6cee71dSXin LI 			case '2':
2845b6cee71dSXin LI 			case '3':
2846b6cee71dSXin LI 			case '4':
2847b6cee71dSXin LI 			case '5':
2848b6cee71dSXin LI 			case '6':
2849b6cee71dSXin LI 			case '7':
2850b6cee71dSXin LI 				val = c - '0';
2851b6cee71dSXin LI 				c = *s++;  /* try for 2 */
2852b6cee71dSXin LI 				if (c >= '0' && c <= '7') {
2853b6cee71dSXin LI 					val = (val << 3) | (c - '0');
2854b6cee71dSXin LI 					c = *s++;  /* try for 3 */
2855b6cee71dSXin LI 					if (c >= '0' && c <= '7')
2856b6cee71dSXin LI 						val = (val << 3) | (c-'0');
2857b6cee71dSXin LI 					else
2858b6cee71dSXin LI 						--s;
2859b6cee71dSXin LI 				}
2860b6cee71dSXin LI 				else
2861b6cee71dSXin LI 					--s;
2862b6cee71dSXin LI 				*p++ = (char)val;
2863b6cee71dSXin LI 				break;
2864b6cee71dSXin LI 
2865b6cee71dSXin LI 			/* \x and up to 2 hex digits */
2866b6cee71dSXin LI 			case 'x':
2867b6cee71dSXin LI 				val = 'x';	/* Default if no digits */
2868b6cee71dSXin LI 				c = hextoint(*s++);	/* Get next char */
2869b6cee71dSXin LI 				if (c >= 0) {
2870b6cee71dSXin LI 					val = c;
2871b6cee71dSXin LI 					c = hextoint(*s++);
2872b6cee71dSXin LI 					if (c >= 0)
2873b6cee71dSXin LI 						val = (val << 4) + c;
2874b6cee71dSXin LI 					else
2875b6cee71dSXin LI 						--s;
2876b6cee71dSXin LI 				} else
2877b6cee71dSXin LI 					--s;
2878b6cee71dSXin LI 				*p++ = (char)val;
2879b6cee71dSXin LI 				break;
2880b6cee71dSXin LI 			}
2881b6cee71dSXin LI 		} else
2882b6cee71dSXin LI 			*p++ = (char)c;
2883b6cee71dSXin LI 	}
28849ce06829SXin LI 	--s;
2885b6cee71dSXin LI out:
2886b6cee71dSXin LI 	*p = '\0';
2887b6cee71dSXin LI 	m->vallen = CAST(unsigned char, (p - origp));
2888b6cee71dSXin LI 	if (m->type == FILE_PSTRING)
2889b6cee71dSXin LI 		m->vallen += (unsigned char)file_pstring_length_size(m);
2890b6cee71dSXin LI 	return s;
2891b6cee71dSXin LI }
2892b6cee71dSXin LI 
2893b6cee71dSXin LI 
2894b6cee71dSXin LI /* Single hex char to int; -1 if not a hex char. */
2895b6cee71dSXin LI private int
2896b6cee71dSXin LI hextoint(int c)
2897b6cee71dSXin LI {
2898b6cee71dSXin LI 	if (!isascii((unsigned char) c))
2899b6cee71dSXin LI 		return -1;
2900b6cee71dSXin LI 	if (isdigit((unsigned char) c))
2901b6cee71dSXin LI 		return c - '0';
2902b6cee71dSXin LI 	if ((c >= 'a') && (c <= 'f'))
2903b6cee71dSXin LI 		return c + 10 - 'a';
2904b6cee71dSXin LI 	if (( c>= 'A') && (c <= 'F'))
2905b6cee71dSXin LI 		return c + 10 - 'A';
2906b6cee71dSXin LI 	return -1;
2907b6cee71dSXin LI }
2908b6cee71dSXin LI 
2909b6cee71dSXin LI 
2910b6cee71dSXin LI /*
2911b6cee71dSXin LI  * Print a string containing C character escapes.
2912b6cee71dSXin LI  */
2913b6cee71dSXin LI protected void
2914b6cee71dSXin LI file_showstr(FILE *fp, const char *s, size_t len)
2915b6cee71dSXin LI {
2916b6cee71dSXin LI 	char	c;
2917b6cee71dSXin LI 
2918b6cee71dSXin LI 	for (;;) {
2919b6cee71dSXin LI 		if (len == ~0U) {
2920b6cee71dSXin LI 			c = *s++;
2921b6cee71dSXin LI 			if (c == '\0')
2922b6cee71dSXin LI 				break;
2923b6cee71dSXin LI 		}
2924b6cee71dSXin LI 		else  {
2925b6cee71dSXin LI 			if (len-- == 0)
2926b6cee71dSXin LI 				break;
2927b6cee71dSXin LI 			c = *s++;
2928b6cee71dSXin LI 		}
2929b6cee71dSXin LI 		if (c >= 040 && c <= 0176)	/* TODO isprint && !iscntrl */
2930b6cee71dSXin LI 			(void) fputc(c, fp);
2931b6cee71dSXin LI 		else {
2932b6cee71dSXin LI 			(void) fputc('\\', fp);
2933b6cee71dSXin LI 			switch (c) {
2934b6cee71dSXin LI 			case '\a':
2935b6cee71dSXin LI 				(void) fputc('a', fp);
2936b6cee71dSXin LI 				break;
2937b6cee71dSXin LI 
2938b6cee71dSXin LI 			case '\b':
2939b6cee71dSXin LI 				(void) fputc('b', fp);
2940b6cee71dSXin LI 				break;
2941b6cee71dSXin LI 
2942b6cee71dSXin LI 			case '\f':
2943b6cee71dSXin LI 				(void) fputc('f', fp);
2944b6cee71dSXin LI 				break;
2945b6cee71dSXin LI 
2946b6cee71dSXin LI 			case '\n':
2947b6cee71dSXin LI 				(void) fputc('n', fp);
2948b6cee71dSXin LI 				break;
2949b6cee71dSXin LI 
2950b6cee71dSXin LI 			case '\r':
2951b6cee71dSXin LI 				(void) fputc('r', fp);
2952b6cee71dSXin LI 				break;
2953b6cee71dSXin LI 
2954b6cee71dSXin LI 			case '\t':
2955b6cee71dSXin LI 				(void) fputc('t', fp);
2956b6cee71dSXin LI 				break;
2957b6cee71dSXin LI 
2958b6cee71dSXin LI 			case '\v':
2959b6cee71dSXin LI 				(void) fputc('v', fp);
2960b6cee71dSXin LI 				break;
2961b6cee71dSXin LI 
2962b6cee71dSXin LI 			default:
2963b6cee71dSXin LI 				(void) fprintf(fp, "%.3o", c & 0377);
2964b6cee71dSXin LI 				break;
2965b6cee71dSXin LI 			}
2966b6cee71dSXin LI 		}
2967b6cee71dSXin LI 	}
2968b6cee71dSXin LI }
2969b6cee71dSXin LI 
2970b6cee71dSXin LI /*
2971b6cee71dSXin LI  * eatsize(): Eat the size spec from a number [eg. 10UL]
2972b6cee71dSXin LI  */
2973b6cee71dSXin LI private void
2974b6cee71dSXin LI eatsize(const char **p)
2975b6cee71dSXin LI {
2976b6cee71dSXin LI 	const char *l = *p;
2977b6cee71dSXin LI 
2978b6cee71dSXin LI 	if (LOWCASE(*l) == 'u')
2979b6cee71dSXin LI 		l++;
2980b6cee71dSXin LI 
2981b6cee71dSXin LI 	switch (LOWCASE(*l)) {
2982b6cee71dSXin LI 	case 'l':    /* long */
2983b6cee71dSXin LI 	case 's':    /* short */
2984b6cee71dSXin LI 	case 'h':    /* short */
2985b6cee71dSXin LI 	case 'b':    /* char/byte */
2986b6cee71dSXin LI 	case 'c':    /* char/byte */
2987b6cee71dSXin LI 		l++;
2988b6cee71dSXin LI 		/*FALLTHROUGH*/
2989b6cee71dSXin LI 	default:
2990b6cee71dSXin LI 		break;
2991b6cee71dSXin LI 	}
2992b6cee71dSXin LI 
2993b6cee71dSXin LI 	*p = l;
2994b6cee71dSXin LI }
2995b6cee71dSXin LI 
2996b6cee71dSXin LI /*
2997c2931133SXin LI  * handle a buffer containing a compiled file.
2998c2931133SXin LI  */
2999c2931133SXin LI private struct magic_map *
3000c2931133SXin LI apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len)
3001c2931133SXin LI {
3002c2931133SXin LI 	struct magic_map *map;
3003c2931133SXin LI 
3004c2931133SXin LI 	if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
3005c2931133SXin LI 		file_oomem(ms, sizeof(*map));
3006c2931133SXin LI 		return NULL;
3007c2931133SXin LI 	}
3008c2931133SXin LI 	map->len = len;
3009c2931133SXin LI 	map->p = buf;
3010c2931133SXin LI 	map->type = MAP_TYPE_USER;
3011c2931133SXin LI 	if (check_buffer(ms, map, "buffer") != 0) {
3012c2931133SXin LI 		apprentice_unmap(map);
3013c2931133SXin LI 		return NULL;
3014c2931133SXin LI 	}
3015c2931133SXin LI 	return map;
3016c2931133SXin LI }
3017c2931133SXin LI 
3018c2931133SXin LI /*
3019b6cee71dSXin LI  * handle a compiled file.
3020b6cee71dSXin LI  */
3021b6cee71dSXin LI 
3022b6cee71dSXin LI private struct magic_map *
3023b6cee71dSXin LI apprentice_map(struct magic_set *ms, const char *fn)
3024b6cee71dSXin LI {
3025b6cee71dSXin LI 	int fd;
3026b6cee71dSXin LI 	struct stat st;
3027b6cee71dSXin LI 	char *dbname = NULL;
3028b6cee71dSXin LI 	struct magic_map *map;
3029a5d223e6SXin LI 	struct magic_map *rv = NULL;
3030b6cee71dSXin LI 
3031b6cee71dSXin LI 	fd = -1;
3032b6cee71dSXin LI 	if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
3033b6cee71dSXin LI 		file_oomem(ms, sizeof(*map));
3034b6cee71dSXin LI 		goto error;
3035b6cee71dSXin LI 	}
30363e41d09dSXin LI 	map->type = MAP_TYPE_USER;	/* unspecified */
3037b6cee71dSXin LI 
3038b6cee71dSXin LI 	dbname = mkdbname(ms, fn, 0);
3039b6cee71dSXin LI 	if (dbname == NULL)
3040b6cee71dSXin LI 		goto error;
3041b6cee71dSXin LI 
3042b6cee71dSXin LI 	if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
3043b6cee71dSXin LI 		goto error;
3044b6cee71dSXin LI 
3045b6cee71dSXin LI 	if (fstat(fd, &st) == -1) {
3046b6cee71dSXin LI 		file_error(ms, errno, "cannot stat `%s'", dbname);
3047b6cee71dSXin LI 		goto error;
3048b6cee71dSXin LI 	}
3049b6cee71dSXin LI 	if (st.st_size < 8 || st.st_size > MAXMAGIC_SIZE) {
3050b6cee71dSXin LI 		file_error(ms, 0, "file `%s' is too %s", dbname,
3051b6cee71dSXin LI 		    st.st_size < 8 ? "small" : "large");
3052b6cee71dSXin LI 		goto error;
3053b6cee71dSXin LI 	}
3054b6cee71dSXin LI 
3055b6cee71dSXin LI 	map->len = (size_t)st.st_size;
3056b6cee71dSXin LI #ifdef QUICK
30573e41d09dSXin LI 	map->type = MAP_TYPE_MMAP;
3058b6cee71dSXin LI 	if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
3059b6cee71dSXin LI 	    MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
3060b6cee71dSXin LI 		file_error(ms, errno, "cannot map `%s'", dbname);
3061b6cee71dSXin LI 		goto error;
3062b6cee71dSXin LI 	}
3063b6cee71dSXin LI #else
30643e41d09dSXin LI 	map->type = MAP_TYPE_MALLOC;
3065b6cee71dSXin LI 	if ((map->p = CAST(void *, malloc(map->len))) == NULL) {
3066b6cee71dSXin LI 		file_oomem(ms, map->len);
3067b6cee71dSXin LI 		goto error;
3068b6cee71dSXin LI 	}
3069b6cee71dSXin LI 	if (read(fd, map->p, map->len) != (ssize_t)map->len) {
3070b6cee71dSXin LI 		file_badread(ms);
3071b6cee71dSXin LI 		goto error;
3072b6cee71dSXin LI 	}
3073b6cee71dSXin LI #define RET	1
3074b6cee71dSXin LI #endif
3075b6cee71dSXin LI 	(void)close(fd);
3076b6cee71dSXin LI 	fd = -1;
3077c2931133SXin LI 
3078a5d223e6SXin LI 	if (check_buffer(ms, map, dbname) != 0) {
3079a5d223e6SXin LI 		rv = (struct magic_map *)-1;
3080c2931133SXin LI 		goto error;
3081a5d223e6SXin LI 	}
30823e41d09dSXin LI #ifdef QUICK
30833e41d09dSXin LI 	if (mprotect(map->p, (size_t)st.st_size, PROT_READ) == -1) {
30843e41d09dSXin LI 		file_error(ms, errno, "cannot mprotect `%s'", dbname);
30853e41d09dSXin LI 		goto error;
30863e41d09dSXin LI 	}
30873e41d09dSXin LI #endif
3088c2931133SXin LI 
3089c2931133SXin LI 	free(dbname);
3090c2931133SXin LI 	return map;
3091c2931133SXin LI 
3092c2931133SXin LI error:
3093c2931133SXin LI 	if (fd != -1)
3094c2931133SXin LI 		(void)close(fd);
3095c2931133SXin LI 	apprentice_unmap(map);
3096c2931133SXin LI 	free(dbname);
3097a5d223e6SXin LI 	return rv;
3098c2931133SXin LI }
3099c2931133SXin LI 
3100c2931133SXin LI private int
3101c2931133SXin LI check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname)
3102c2931133SXin LI {
3103c2931133SXin LI 	uint32_t *ptr;
3104c2931133SXin LI 	uint32_t entries, nentries;
3105c2931133SXin LI 	uint32_t version;
3106c2931133SXin LI 	int i, needsbyteswap;
3107c2931133SXin LI 
3108b6cee71dSXin LI 	ptr = CAST(uint32_t *, map->p);
3109b6cee71dSXin LI 	if (*ptr != MAGICNO) {
3110b6cee71dSXin LI 		if (swap4(*ptr) != MAGICNO) {
3111b6cee71dSXin LI 			file_error(ms, 0, "bad magic in `%s'", dbname);
3112c2931133SXin LI 			return -1;
3113b6cee71dSXin LI 		}
3114b6cee71dSXin LI 		needsbyteswap = 1;
3115b6cee71dSXin LI 	} else
3116b6cee71dSXin LI 		needsbyteswap = 0;
3117b6cee71dSXin LI 	if (needsbyteswap)
3118b6cee71dSXin LI 		version = swap4(ptr[1]);
3119b6cee71dSXin LI 	else
3120b6cee71dSXin LI 		version = ptr[1];
3121b6cee71dSXin LI 	if (version != VERSIONNO) {
3122b6cee71dSXin LI 		file_error(ms, 0, "File %s supports only version %d magic "
3123b6cee71dSXin LI 		    "files. `%s' is version %d", VERSION,
3124b6cee71dSXin LI 		    VERSIONNO, dbname, version);
3125c2931133SXin LI 		return -1;
3126b6cee71dSXin LI 	}
3127c2931133SXin LI 	entries = (uint32_t)(map->len / sizeof(struct magic));
3128c2931133SXin LI 	if ((entries * sizeof(struct magic)) != map->len) {
3129c2931133SXin LI 		file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not "
3130b6cee71dSXin LI 		    "a multiple of %" SIZE_T_FORMAT "u",
3131c2931133SXin LI 		    dbname, map->len, sizeof(struct magic));
3132c2931133SXin LI 		return -1;
3133b6cee71dSXin LI 	}
3134b6cee71dSXin LI 	map->magic[0] = CAST(struct magic *, map->p) + 1;
3135b6cee71dSXin LI 	nentries = 0;
3136b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
3137b6cee71dSXin LI 		if (needsbyteswap)
3138b6cee71dSXin LI 			map->nmagic[i] = swap4(ptr[i + 2]);
3139b6cee71dSXin LI 		else
3140b6cee71dSXin LI 			map->nmagic[i] = ptr[i + 2];
3141b6cee71dSXin LI 		if (i != MAGIC_SETS - 1)
3142b6cee71dSXin LI 			map->magic[i + 1] = map->magic[i] + map->nmagic[i];
3143b6cee71dSXin LI 		nentries += map->nmagic[i];
3144b6cee71dSXin LI 	}
3145b6cee71dSXin LI 	if (entries != nentries + 1) {
3146b6cee71dSXin LI 		file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
3147b6cee71dSXin LI 		    dbname, entries, nentries + 1);
3148c2931133SXin LI 		return -1;
3149b6cee71dSXin LI 	}
3150b6cee71dSXin LI 	if (needsbyteswap)
3151b6cee71dSXin LI 		for (i = 0; i < MAGIC_SETS; i++)
3152b6cee71dSXin LI 			byteswap(map->magic[i], map->nmagic[i]);
3153c2931133SXin LI 	return 0;
3154b6cee71dSXin LI }
3155b6cee71dSXin LI 
3156b6cee71dSXin LI /*
3157b6cee71dSXin LI  * handle an mmaped file.
3158b6cee71dSXin LI  */
3159b6cee71dSXin LI private int
3160b6cee71dSXin LI apprentice_compile(struct magic_set *ms, struct magic_map *map, const char *fn)
3161b6cee71dSXin LI {
3162b6cee71dSXin LI 	static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS;
3163b6cee71dSXin LI 	static const size_t m = sizeof(**map->magic);
3164b6cee71dSXin LI 	int fd = -1;
3165b6cee71dSXin LI 	size_t len;
3166b6cee71dSXin LI 	char *dbname;
3167b6cee71dSXin LI 	int rv = -1;
3168b6cee71dSXin LI 	uint32_t i;
3169b6cee71dSXin LI 	union {
3170b6cee71dSXin LI 		struct magic m;
3171b6cee71dSXin LI 		uint32_t h[2 + MAGIC_SETS];
3172b6cee71dSXin LI 	} hdr;
3173b6cee71dSXin LI 
3174b6cee71dSXin LI 	dbname = mkdbname(ms, fn, 1);
3175b6cee71dSXin LI 
3176b6cee71dSXin LI 	if (dbname == NULL)
3177b6cee71dSXin LI 		goto out;
3178b6cee71dSXin LI 
3179b6cee71dSXin LI 	if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1)
3180b6cee71dSXin LI 	{
3181b6cee71dSXin LI 		file_error(ms, errno, "cannot open `%s'", dbname);
3182b6cee71dSXin LI 		goto out;
3183b6cee71dSXin LI 	}
3184b6cee71dSXin LI 	memset(&hdr, 0, sizeof(hdr));
3185b6cee71dSXin LI 	hdr.h[0] = MAGICNO;
3186b6cee71dSXin LI 	hdr.h[1] = VERSIONNO;
3187b6cee71dSXin LI 	memcpy(hdr.h + 2, map->nmagic, nm);
3188b6cee71dSXin LI 
3189b6cee71dSXin LI 	if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
3190b6cee71dSXin LI 		file_error(ms, errno, "error writing `%s'", dbname);
3191*58a0f0d0SEitan Adler 		goto out2;
3192b6cee71dSXin LI 	}
3193b6cee71dSXin LI 
3194b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
3195b6cee71dSXin LI 		len = m * map->nmagic[i];
3196b6cee71dSXin LI 		if (write(fd, map->magic[i], len) != (ssize_t)len) {
3197b6cee71dSXin LI 			file_error(ms, errno, "error writing `%s'", dbname);
3198*58a0f0d0SEitan Adler 			goto out2;
3199b6cee71dSXin LI 		}
3200b6cee71dSXin LI 	}
3201b6cee71dSXin LI 
3202*58a0f0d0SEitan Adler 	rv = 0;
3203*58a0f0d0SEitan Adler out2:
3204b6cee71dSXin LI 	if (fd != -1)
3205b6cee71dSXin LI 		(void)close(fd);
3206b6cee71dSXin LI out:
3207282e23f0SXin LI 	apprentice_unmap(map);
3208b6cee71dSXin LI 	free(dbname);
3209b6cee71dSXin LI 	return rv;
3210b6cee71dSXin LI }
3211b6cee71dSXin LI 
3212b6cee71dSXin LI private const char ext[] = ".mgc";
3213b6cee71dSXin LI /*
3214b6cee71dSXin LI  * make a dbname
3215b6cee71dSXin LI  */
3216b6cee71dSXin LI private char *
3217b6cee71dSXin LI mkdbname(struct magic_set *ms, const char *fn, int strip)
3218b6cee71dSXin LI {
3219b6cee71dSXin LI 	const char *p, *q;
3220b6cee71dSXin LI 	char *buf;
3221b6cee71dSXin LI 
3222b6cee71dSXin LI 	if (strip) {
3223b6cee71dSXin LI 		if ((p = strrchr(fn, '/')) != NULL)
3224b6cee71dSXin LI 			fn = ++p;
3225b6cee71dSXin LI 	}
3226b6cee71dSXin LI 
3227b6cee71dSXin LI 	for (q = fn; *q; q++)
3228b6cee71dSXin LI 		continue;
3229b6cee71dSXin LI 	/* Look for .mgc */
3230b6cee71dSXin LI 	for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--)
3231b6cee71dSXin LI 		if (*p != *q)
3232b6cee71dSXin LI 			break;
3233b6cee71dSXin LI 
3234b6cee71dSXin LI 	/* Did not find .mgc, restore q */
3235b6cee71dSXin LI 	if (p >= ext)
3236b6cee71dSXin LI 		while (*q)
3237b6cee71dSXin LI 			q++;
3238b6cee71dSXin LI 
3239b6cee71dSXin LI 	q++;
3240b6cee71dSXin LI 	/* Compatibility with old code that looked in .mime */
3241b6cee71dSXin LI 	if (ms->flags & MAGIC_MIME) {
3242b6cee71dSXin LI 		if (asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext) < 0)
3243b6cee71dSXin LI 			return NULL;
3244b6cee71dSXin LI 		if (access(buf, R_OK) != -1) {
3245b6cee71dSXin LI 			ms->flags &= MAGIC_MIME_TYPE;
3246b6cee71dSXin LI 			return buf;
3247b6cee71dSXin LI 		}
3248b6cee71dSXin LI 		free(buf);
3249b6cee71dSXin LI 	}
3250b6cee71dSXin LI 	if (asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext) < 0)
3251b6cee71dSXin LI 		return NULL;
3252b6cee71dSXin LI 
3253b6cee71dSXin LI 	/* Compatibility with old code that looked in .mime */
3254a5d223e6SXin LI 	if (strstr(fn, ".mime") != NULL)
3255b6cee71dSXin LI 		ms->flags &= MAGIC_MIME_TYPE;
3256b6cee71dSXin LI 	return buf;
3257b6cee71dSXin LI }
3258b6cee71dSXin LI 
3259b6cee71dSXin LI /*
3260b6cee71dSXin LI  * Byteswap an mmap'ed file if needed
3261b6cee71dSXin LI  */
3262b6cee71dSXin LI private void
3263b6cee71dSXin LI byteswap(struct magic *magic, uint32_t nmagic)
3264b6cee71dSXin LI {
3265b6cee71dSXin LI 	uint32_t i;
3266b6cee71dSXin LI 	for (i = 0; i < nmagic; i++)
3267b6cee71dSXin LI 		bs1(&magic[i]);
3268b6cee71dSXin LI }
3269b6cee71dSXin LI 
3270b6cee71dSXin LI /*
3271b6cee71dSXin LI  * swap a short
3272b6cee71dSXin LI  */
3273b6cee71dSXin LI private uint16_t
3274b6cee71dSXin LI swap2(uint16_t sv)
3275b6cee71dSXin LI {
3276b6cee71dSXin LI 	uint16_t rv;
3277b6cee71dSXin LI 	uint8_t *s = (uint8_t *)(void *)&sv;
3278b6cee71dSXin LI 	uint8_t *d = (uint8_t *)(void *)&rv;
3279b6cee71dSXin LI 	d[0] = s[1];
3280b6cee71dSXin LI 	d[1] = s[0];
3281b6cee71dSXin LI 	return rv;
3282b6cee71dSXin LI }
3283b6cee71dSXin LI 
3284b6cee71dSXin LI /*
3285b6cee71dSXin LI  * swap an int
3286b6cee71dSXin LI  */
3287b6cee71dSXin LI private uint32_t
3288b6cee71dSXin LI swap4(uint32_t sv)
3289b6cee71dSXin LI {
3290b6cee71dSXin LI 	uint32_t rv;
3291b6cee71dSXin LI 	uint8_t *s = (uint8_t *)(void *)&sv;
3292b6cee71dSXin LI 	uint8_t *d = (uint8_t *)(void *)&rv;
3293b6cee71dSXin LI 	d[0] = s[3];
3294b6cee71dSXin LI 	d[1] = s[2];
3295b6cee71dSXin LI 	d[2] = s[1];
3296b6cee71dSXin LI 	d[3] = s[0];
3297b6cee71dSXin LI 	return rv;
3298b6cee71dSXin LI }
3299b6cee71dSXin LI 
3300b6cee71dSXin LI /*
3301b6cee71dSXin LI  * swap a quad
3302b6cee71dSXin LI  */
3303b6cee71dSXin LI private uint64_t
3304b6cee71dSXin LI swap8(uint64_t sv)
3305b6cee71dSXin LI {
3306b6cee71dSXin LI 	uint64_t rv;
3307b6cee71dSXin LI 	uint8_t *s = (uint8_t *)(void *)&sv;
3308b6cee71dSXin LI 	uint8_t *d = (uint8_t *)(void *)&rv;
3309b6cee71dSXin LI #if 0
3310b6cee71dSXin LI 	d[0] = s[3];
3311b6cee71dSXin LI 	d[1] = s[2];
3312b6cee71dSXin LI 	d[2] = s[1];
3313b6cee71dSXin LI 	d[3] = s[0];
3314b6cee71dSXin LI 	d[4] = s[7];
3315b6cee71dSXin LI 	d[5] = s[6];
3316b6cee71dSXin LI 	d[6] = s[5];
3317b6cee71dSXin LI 	d[7] = s[4];
3318b6cee71dSXin LI #else
3319b6cee71dSXin LI 	d[0] = s[7];
3320b6cee71dSXin LI 	d[1] = s[6];
3321b6cee71dSXin LI 	d[2] = s[5];
3322b6cee71dSXin LI 	d[3] = s[4];
3323b6cee71dSXin LI 	d[4] = s[3];
3324b6cee71dSXin LI 	d[5] = s[2];
3325b6cee71dSXin LI 	d[6] = s[1];
3326b6cee71dSXin LI 	d[7] = s[0];
3327b6cee71dSXin LI #endif
3328b6cee71dSXin LI 	return rv;
3329b6cee71dSXin LI }
3330b6cee71dSXin LI 
3331b6cee71dSXin LI /*
3332b6cee71dSXin LI  * byteswap a single magic entry
3333b6cee71dSXin LI  */
3334b6cee71dSXin LI private void
3335b6cee71dSXin LI bs1(struct magic *m)
3336b6cee71dSXin LI {
3337b6cee71dSXin LI 	m->cont_level = swap2(m->cont_level);
3338*58a0f0d0SEitan Adler 	m->offset = swap4((int32_t)m->offset);
3339b6cee71dSXin LI 	m->in_offset = swap4((uint32_t)m->in_offset);
3340b6cee71dSXin LI 	m->lineno = swap4((uint32_t)m->lineno);
3341b6cee71dSXin LI 	if (IS_STRING(m->type)) {
3342b6cee71dSXin LI 		m->str_range = swap4(m->str_range);
3343b6cee71dSXin LI 		m->str_flags = swap4(m->str_flags);
3344b6cee71dSXin LI 	}
3345b6cee71dSXin LI 	else {
3346b6cee71dSXin LI 		m->value.q = swap8(m->value.q);
3347b6cee71dSXin LI 		m->num_mask = swap8(m->num_mask);
3348b6cee71dSXin LI 	}
3349b6cee71dSXin LI }
3350b6cee71dSXin LI 
3351b6cee71dSXin LI protected size_t
3352b6cee71dSXin LI file_pstring_length_size(const struct magic *m)
3353b6cee71dSXin LI {
3354b6cee71dSXin LI 	switch (m->str_flags & PSTRING_LEN) {
3355b6cee71dSXin LI 	case PSTRING_1_LE:
3356b6cee71dSXin LI 		return 1;
3357b6cee71dSXin LI 	case PSTRING_2_LE:
3358b6cee71dSXin LI 	case PSTRING_2_BE:
3359b6cee71dSXin LI 		return 2;
3360b6cee71dSXin LI 	case PSTRING_4_LE:
3361b6cee71dSXin LI 	case PSTRING_4_BE:
3362b6cee71dSXin LI 		return 4;
3363b6cee71dSXin LI 	default:
3364b6cee71dSXin LI 		abort();	/* Impossible */
3365b6cee71dSXin LI 		return 1;
3366b6cee71dSXin LI 	}
3367b6cee71dSXin LI }
3368b6cee71dSXin LI protected size_t
33699ce06829SXin LI file_pstring_get_length(const struct magic *m, const char *ss)
3370b6cee71dSXin LI {
3371b6cee71dSXin LI 	size_t len = 0;
33729ce06829SXin LI 	const unsigned char *s = (const unsigned char *)ss;
337340427ccaSGordon Tetlow 	unsigned int s3, s2, s1, s0;
3374b6cee71dSXin LI 
3375b6cee71dSXin LI 	switch (m->str_flags & PSTRING_LEN) {
3376b6cee71dSXin LI 	case PSTRING_1_LE:
3377b6cee71dSXin LI 		len = *s;
3378b6cee71dSXin LI 		break;
3379b6cee71dSXin LI 	case PSTRING_2_LE:
338040427ccaSGordon Tetlow 		s0 = s[0];
338140427ccaSGordon Tetlow 		s1 = s[1];
338240427ccaSGordon Tetlow 		len = (s1 << 8) | s0;
3383b6cee71dSXin LI 		break;
3384b6cee71dSXin LI 	case PSTRING_2_BE:
338540427ccaSGordon Tetlow 		s0 = s[0];
338640427ccaSGordon Tetlow 		s1 = s[1];
338740427ccaSGordon Tetlow 		len = (s0 << 8) | s1;
3388b6cee71dSXin LI 		break;
3389b6cee71dSXin LI 	case PSTRING_4_LE:
339040427ccaSGordon Tetlow 		s0 = s[0];
339140427ccaSGordon Tetlow 		s1 = s[1];
339240427ccaSGordon Tetlow 		s2 = s[2];
339340427ccaSGordon Tetlow 		s3 = s[3];
339440427ccaSGordon Tetlow 		len = (s3 << 24) | (s2 << 16) | (s1 << 8) | s0;
3395b6cee71dSXin LI 		break;
3396b6cee71dSXin LI 	case PSTRING_4_BE:
339740427ccaSGordon Tetlow 		s0 = s[0];
339840427ccaSGordon Tetlow 		s1 = s[1];
339940427ccaSGordon Tetlow 		s2 = s[2];
340040427ccaSGordon Tetlow 		s3 = s[3];
340140427ccaSGordon Tetlow 		len = (s0 << 24) | (s1 << 16) | (s2 << 8) | s3;
3402b6cee71dSXin LI 		break;
3403b6cee71dSXin LI 	default:
3404b6cee71dSXin LI 		abort();	/* Impossible */
3405b6cee71dSXin LI 	}
3406b6cee71dSXin LI 
3407b6cee71dSXin LI 	if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
3408b6cee71dSXin LI 		len -= file_pstring_length_size(m);
3409b6cee71dSXin LI 
3410b6cee71dSXin LI 	return len;
3411b6cee71dSXin LI }
3412b6cee71dSXin LI 
3413b6cee71dSXin LI protected int
3414b6cee71dSXin LI file_magicfind(struct magic_set *ms, const char *name, struct mlist *v)
3415b6cee71dSXin LI {
3416b6cee71dSXin LI 	uint32_t i, j;
3417b6cee71dSXin LI 	struct mlist *mlist, *ml;
3418b6cee71dSXin LI 
3419b6cee71dSXin LI 	mlist = ms->mlist[1];
3420b6cee71dSXin LI 
3421b6cee71dSXin LI 	for (ml = mlist->next; ml != mlist; ml = ml->next) {
3422b6cee71dSXin LI 		struct magic *ma = ml->magic;
3423b6cee71dSXin LI 		uint32_t nma = ml->nmagic;
3424b6cee71dSXin LI 		for (i = 0; i < nma; i++) {
3425b6cee71dSXin LI 			if (ma[i].type != FILE_NAME)
3426b6cee71dSXin LI 				continue;
3427b6cee71dSXin LI 			if (strcmp(ma[i].value.s, name) == 0) {
3428b6cee71dSXin LI 				v->magic = &ma[i];
3429b6cee71dSXin LI 				for (j = i + 1; j < nma; j++)
3430b6cee71dSXin LI 				    if (ma[j].cont_level == 0)
3431b6cee71dSXin LI 					    break;
3432b6cee71dSXin LI 				v->nmagic = j - i;
3433b6cee71dSXin LI 				return 0;
3434b6cee71dSXin LI 			}
3435b6cee71dSXin LI 		}
3436b6cee71dSXin LI 	}
3437b6cee71dSXin LI 	return -1;
3438b6cee71dSXin LI }
3439