xref: /freebsd/contrib/file/src/apprentice.c (revision 9ce06829f29232e312130530c304d287b39b0059)
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*9ce06829SXin LI FILE_RCSID("@(#)$File: apprentice.c,v 1.238 2015/09/12 18:10:42 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 
89c2931133SXin LI #define MAP_TYPE_MMAP	0
90c2931133SXin LI #define MAP_TYPE_MALLOC	1
91c2931133SXin LI #define MAP_TYPE_USER	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 *);
146b6cee71dSXin LI private int check_format_type(const char *, int);
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 },
271b6cee71dSXin LI 	{ XX_NULL,		FILE_INVALID,		FILE_FMT_NONE },
272b6cee71dSXin LI };
273b6cee71dSXin LI 
274b6cee71dSXin LI /*
275b6cee71dSXin LI  * These are not types, and cannot be preceded by "u" to make them
276b6cee71dSXin LI  * unsigned.
277b6cee71dSXin LI  */
278b6cee71dSXin LI static const struct type_tbl_s special_tbl[] = {
279b6cee71dSXin LI 	{ XX("name"),		FILE_NAME,		FILE_FMT_STR },
280b6cee71dSXin LI 	{ XX("use"),		FILE_USE,		FILE_FMT_STR },
281b6cee71dSXin LI 	{ XX_NULL,		FILE_INVALID,		FILE_FMT_NONE },
282b6cee71dSXin LI };
283b6cee71dSXin LI # undef XX
284b6cee71dSXin LI # undef XX_NULL
285b6cee71dSXin LI 
286b6cee71dSXin LI private int
287b6cee71dSXin LI get_type(const struct type_tbl_s *tbl, const char *l, const char **t)
288b6cee71dSXin LI {
289b6cee71dSXin LI 	const struct type_tbl_s *p;
290b6cee71dSXin LI 
291b6cee71dSXin LI 	for (p = tbl; p->len; p++) {
292b6cee71dSXin LI 		if (strncmp(l, p->name, p->len) == 0) {
293b6cee71dSXin LI 			if (t)
294b6cee71dSXin LI 				*t = l + p->len;
295b6cee71dSXin LI 			break;
296b6cee71dSXin LI 		}
297b6cee71dSXin LI 	}
298b6cee71dSXin LI 	return p->type;
299b6cee71dSXin LI }
300b6cee71dSXin LI 
301b6cee71dSXin LI private int
302b6cee71dSXin LI get_standard_integer_type(const char *l, const char **t)
303b6cee71dSXin LI {
304b6cee71dSXin LI 	int type;
305b6cee71dSXin LI 
306b6cee71dSXin LI 	if (isalpha((unsigned char)l[1])) {
307b6cee71dSXin LI 		switch (l[1]) {
308b6cee71dSXin LI 		case 'C':
309b6cee71dSXin LI 			/* "dC" and "uC" */
310b6cee71dSXin LI 			type = FILE_BYTE;
311b6cee71dSXin LI 			break;
312b6cee71dSXin LI 		case 'S':
313b6cee71dSXin LI 			/* "dS" and "uS" */
314b6cee71dSXin LI 			type = FILE_SHORT;
315b6cee71dSXin LI 			break;
316b6cee71dSXin LI 		case 'I':
317b6cee71dSXin LI 		case 'L':
318b6cee71dSXin LI 			/*
319b6cee71dSXin LI 			 * "dI", "dL", "uI", and "uL".
320b6cee71dSXin LI 			 *
321b6cee71dSXin LI 			 * XXX - the actual Single UNIX Specification says
322b6cee71dSXin LI 			 * that "L" means "long", as in the C data type,
323b6cee71dSXin LI 			 * but we treat it as meaning "4-byte integer".
324b6cee71dSXin LI 			 * Given that the OS X version of file 5.04 did
325b6cee71dSXin LI 			 * the same, I guess that passes the actual SUS
326b6cee71dSXin LI 			 * validation suite; having "dL" be dependent on
327b6cee71dSXin LI 			 * how big a "long" is on the machine running
328b6cee71dSXin LI 			 * "file" is silly.
329b6cee71dSXin LI 			 */
330b6cee71dSXin LI 			type = FILE_LONG;
331b6cee71dSXin LI 			break;
332b6cee71dSXin LI 		case 'Q':
333b6cee71dSXin LI 			/* "dQ" and "uQ" */
334b6cee71dSXin LI 			type = FILE_QUAD;
335b6cee71dSXin LI 			break;
336b6cee71dSXin LI 		default:
337b6cee71dSXin LI 			/* "d{anything else}", "u{anything else}" */
338b6cee71dSXin LI 			return FILE_INVALID;
339b6cee71dSXin LI 		}
340b6cee71dSXin LI 		l += 2;
341b6cee71dSXin LI 	} else if (isdigit((unsigned char)l[1])) {
342b6cee71dSXin LI 		/*
343b6cee71dSXin LI 		 * "d{num}" and "u{num}"; we only support {num} values
344b6cee71dSXin LI 		 * of 1, 2, 4, and 8 - the Single UNIX Specification
345b6cee71dSXin LI 		 * doesn't say anything about whether arbitrary
346b6cee71dSXin LI 		 * values should be supported, but both the Solaris 10
347b6cee71dSXin LI 		 * and OS X Mountain Lion versions of file passed the
348b6cee71dSXin LI 		 * Single UNIX Specification validation suite, and
349b6cee71dSXin LI 		 * neither of them support values bigger than 8 or
350b6cee71dSXin LI 		 * non-power-of-2 values.
351b6cee71dSXin LI 		 */
352b6cee71dSXin LI 		if (isdigit((unsigned char)l[2])) {
353b6cee71dSXin LI 			/* Multi-digit, so > 9 */
354b6cee71dSXin LI 			return FILE_INVALID;
355b6cee71dSXin LI 		}
356b6cee71dSXin LI 		switch (l[1]) {
357b6cee71dSXin LI 		case '1':
358b6cee71dSXin LI 			type = FILE_BYTE;
359b6cee71dSXin LI 			break;
360b6cee71dSXin LI 		case '2':
361b6cee71dSXin LI 			type = FILE_SHORT;
362b6cee71dSXin LI 			break;
363b6cee71dSXin LI 		case '4':
364b6cee71dSXin LI 			type = FILE_LONG;
365b6cee71dSXin LI 			break;
366b6cee71dSXin LI 		case '8':
367b6cee71dSXin LI 			type = FILE_QUAD;
368b6cee71dSXin LI 			break;
369b6cee71dSXin LI 		default:
370b6cee71dSXin LI 			/* XXX - what about 3, 5, 6, or 7? */
371b6cee71dSXin LI 			return FILE_INVALID;
372b6cee71dSXin LI 		}
373b6cee71dSXin LI 		l += 2;
374b6cee71dSXin LI 	} else {
375b6cee71dSXin LI 		/*
376b6cee71dSXin LI 		 * "d" or "u" by itself.
377b6cee71dSXin LI 		 */
378b6cee71dSXin LI 		type = FILE_LONG;
379b6cee71dSXin LI 		++l;
380b6cee71dSXin LI 	}
381b6cee71dSXin LI 	if (t)
382b6cee71dSXin LI 		*t = l;
383b6cee71dSXin LI 	return type;
384b6cee71dSXin LI }
385b6cee71dSXin LI 
386b6cee71dSXin LI private void
387b6cee71dSXin LI init_file_tables(void)
388b6cee71dSXin LI {
389b6cee71dSXin LI 	static int done = 0;
390b6cee71dSXin LI 	const struct type_tbl_s *p;
391b6cee71dSXin LI 
392b6cee71dSXin LI 	if (done)
393b6cee71dSXin LI 		return;
394b6cee71dSXin LI 	done++;
395b6cee71dSXin LI 
396b6cee71dSXin LI 	for (p = type_tbl; p->len; p++) {
397b6cee71dSXin LI 		assert(p->type < FILE_NAMES_SIZE);
398b6cee71dSXin LI 		file_names[p->type] = p->name;
399b6cee71dSXin LI 		file_formats[p->type] = p->format;
400b6cee71dSXin LI 	}
401b6cee71dSXin LI 	assert(p - type_tbl == FILE_NAMES_SIZE);
402b6cee71dSXin LI }
403b6cee71dSXin LI 
404b6cee71dSXin LI private int
405b6cee71dSXin LI add_mlist(struct mlist *mlp, struct magic_map *map, size_t idx)
406b6cee71dSXin LI {
407b6cee71dSXin LI 	struct mlist *ml;
408b6cee71dSXin LI 
409c2931133SXin LI 	mlp->map = idx == 0 ? map : NULL;
410b6cee71dSXin LI 	if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL)
411b6cee71dSXin LI 		return -1;
412b6cee71dSXin LI 
413c2931133SXin LI 	ml->map = NULL;
414b6cee71dSXin LI 	ml->magic = map->magic[idx];
415b6cee71dSXin LI 	ml->nmagic = map->nmagic[idx];
416b6cee71dSXin LI 
417b6cee71dSXin LI 	mlp->prev->next = ml;
418b6cee71dSXin LI 	ml->prev = mlp->prev;
419b6cee71dSXin LI 	ml->next = mlp;
420b6cee71dSXin LI 	mlp->prev = ml;
421b6cee71dSXin LI 	return 0;
422b6cee71dSXin LI }
423b6cee71dSXin LI 
424b6cee71dSXin LI /*
425b6cee71dSXin LI  * Handle one file or directory.
426b6cee71dSXin LI  */
427b6cee71dSXin LI private int
428b6cee71dSXin LI apprentice_1(struct magic_set *ms, const char *fn, int action)
429b6cee71dSXin LI {
430b6cee71dSXin LI 	struct magic_map *map;
431b6cee71dSXin LI #ifndef COMPILE_ONLY
432c2931133SXin LI 	struct mlist *ml;
433b6cee71dSXin LI 	size_t i;
434c2931133SXin LI #endif
435b6cee71dSXin LI 
436b6cee71dSXin LI 	if (magicsize != FILE_MAGICSIZE) {
437b6cee71dSXin LI 		file_error(ms, 0, "magic element size %lu != %lu",
438b6cee71dSXin LI 		    (unsigned long)sizeof(*map->magic[0]),
439b6cee71dSXin LI 		    (unsigned long)FILE_MAGICSIZE);
440b6cee71dSXin LI 		return -1;
441b6cee71dSXin LI 	}
442b6cee71dSXin LI 
443b6cee71dSXin LI 	if (action == FILE_COMPILE) {
444b6cee71dSXin LI 		map = apprentice_load(ms, fn, action);
445b6cee71dSXin LI 		if (map == NULL)
446b6cee71dSXin LI 			return -1;
447b6cee71dSXin LI 		return apprentice_compile(ms, map, fn);
448b6cee71dSXin LI 	}
449b6cee71dSXin LI 
450b6cee71dSXin LI #ifndef COMPILE_ONLY
451b6cee71dSXin LI 	map = apprentice_map(ms, fn);
452b6cee71dSXin LI 	if (map == NULL) {
453b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
454b6cee71dSXin LI 			file_magwarn(ms, "using regular magic file `%s'", fn);
455b6cee71dSXin LI 		map = apprentice_load(ms, fn, action);
456b6cee71dSXin LI 		if (map == NULL)
457b6cee71dSXin LI 			return -1;
458b6cee71dSXin LI 	}
459b6cee71dSXin LI 
460b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
461b6cee71dSXin LI 		if (add_mlist(ms->mlist[i], map, i) == -1) {
462b6cee71dSXin LI 			file_oomem(ms, sizeof(*ml));
463c2931133SXin LI 			goto fail;
464b6cee71dSXin LI 		}
465b6cee71dSXin LI 	}
466b6cee71dSXin LI 
467b6cee71dSXin LI 	if (action == FILE_LIST) {
468b6cee71dSXin LI 		for (i = 0; i < MAGIC_SETS; i++) {
469c2931133SXin LI 			printf("Set %" SIZE_T_FORMAT "u:\nBinary patterns:\n",
470c2931133SXin LI 			    i);
471b6cee71dSXin LI 			apprentice_list(ms->mlist[i], BINTEST);
472b6cee71dSXin LI 			printf("Text patterns:\n");
473b6cee71dSXin LI 			apprentice_list(ms->mlist[i], TEXTTEST);
474b6cee71dSXin LI 		}
475b6cee71dSXin LI 	}
476b6cee71dSXin LI 	return 0;
477c2931133SXin LI fail:
478c2931133SXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
479c2931133SXin LI 		mlist_free(ms->mlist[i]);
480c2931133SXin LI 		ms->mlist[i] = NULL;
481c2931133SXin LI 	}
482c2931133SXin LI 	return -1;
483c2931133SXin LI #else
484c2931133SXin LI 	return 0;
485c2931133SXin LI #endif /* COMPILE_ONLY */
486b6cee71dSXin LI }
487b6cee71dSXin LI 
488b6cee71dSXin LI protected void
489b6cee71dSXin LI file_ms_free(struct magic_set *ms)
490b6cee71dSXin LI {
491b6cee71dSXin LI 	size_t i;
492b6cee71dSXin LI 	if (ms == NULL)
493b6cee71dSXin LI 		return;
494b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++)
495b6cee71dSXin LI 		mlist_free(ms->mlist[i]);
496b6cee71dSXin LI 	free(ms->o.pbuf);
497b6cee71dSXin LI 	free(ms->o.buf);
498b6cee71dSXin LI 	free(ms->c.li);
499b6cee71dSXin LI 	free(ms);
500b6cee71dSXin LI }
501b6cee71dSXin LI 
502b6cee71dSXin LI protected struct magic_set *
503b6cee71dSXin LI file_ms_alloc(int flags)
504b6cee71dSXin LI {
505b6cee71dSXin LI 	struct magic_set *ms;
506b6cee71dSXin LI 	size_t i, len;
507b6cee71dSXin LI 
508b6cee71dSXin LI 	if ((ms = CAST(struct magic_set *, calloc((size_t)1,
509b6cee71dSXin LI 	    sizeof(struct magic_set)))) == NULL)
510b6cee71dSXin LI 		return NULL;
511b6cee71dSXin LI 
512b6cee71dSXin LI 	if (magic_setflags(ms, flags) == -1) {
513b6cee71dSXin LI 		errno = EINVAL;
514b6cee71dSXin LI 		goto free;
515b6cee71dSXin LI 	}
516b6cee71dSXin LI 
517b6cee71dSXin LI 	ms->o.buf = ms->o.pbuf = NULL;
518b6cee71dSXin LI 	len = (ms->c.len = 10) * sizeof(*ms->c.li);
519b6cee71dSXin LI 
520b6cee71dSXin LI 	if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
521b6cee71dSXin LI 		goto free;
522b6cee71dSXin LI 
523b6cee71dSXin LI 	ms->event_flags = 0;
524b6cee71dSXin LI 	ms->error = -1;
525b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++)
526b6cee71dSXin LI 		ms->mlist[i] = NULL;
527b6cee71dSXin LI 	ms->file = "unknown";
528b6cee71dSXin LI 	ms->line = 0;
529c2931133SXin LI 	ms->indir_max = FILE_INDIR_MAX;
530c2931133SXin LI 	ms->name_max = FILE_NAME_MAX;
531c2931133SXin LI 	ms->elf_shnum_max = FILE_ELF_SHNUM_MAX;
532c2931133SXin LI 	ms->elf_phnum_max = FILE_ELF_PHNUM_MAX;
5334460e5b0SXin LI 	ms->elf_notes_max = FILE_ELF_NOTES_MAX;
534*9ce06829SXin LI 	ms->regex_max = FILE_REGEX_MAX;
535b6cee71dSXin LI 	return ms;
536b6cee71dSXin LI free:
537b6cee71dSXin LI 	free(ms);
538b6cee71dSXin LI 	return NULL;
539b6cee71dSXin LI }
540b6cee71dSXin LI 
541b6cee71dSXin LI private void
542b6cee71dSXin LI apprentice_unmap(struct magic_map *map)
543b6cee71dSXin LI {
544*9ce06829SXin LI 	size_t i;
545b6cee71dSXin LI 	if (map == NULL)
546b6cee71dSXin LI 		return;
547c2931133SXin LI 
548c2931133SXin LI 	switch (map->type) {
549b6cee71dSXin LI #ifdef QUICK
550c2931133SXin LI 	case MAP_TYPE_MMAP:
551c2931133SXin LI 		if (map->p)
552b6cee71dSXin LI 			(void)munmap(map->p, map->len);
553c2931133SXin LI 		break;
554b6cee71dSXin LI #endif
555c2931133SXin LI 	case MAP_TYPE_MALLOC:
556b6cee71dSXin LI 		free(map->p);
557*9ce06829SXin LI 		for (i = 0; i < MAGIC_SETS; i++)
558*9ce06829SXin LI 			free(map->magic[i]);
559c2931133SXin LI 		break;
560c2931133SXin LI 	case MAP_TYPE_USER:
561c2931133SXin LI 		break;
562c2931133SXin LI 	default:
563c2931133SXin LI 		abort();
564b6cee71dSXin LI 	}
565b6cee71dSXin LI 	free(map);
566b6cee71dSXin LI }
567b6cee71dSXin LI 
568b6cee71dSXin LI private struct mlist *
569b6cee71dSXin LI mlist_alloc(void)
570b6cee71dSXin LI {
571b6cee71dSXin LI 	struct mlist *mlist;
572b6cee71dSXin LI 	if ((mlist = CAST(struct mlist *, calloc(1, sizeof(*mlist)))) == NULL) {
573b6cee71dSXin LI 		return NULL;
574b6cee71dSXin LI 	}
575b6cee71dSXin LI 	mlist->next = mlist->prev = mlist;
576b6cee71dSXin LI 	return mlist;
577b6cee71dSXin LI }
578b6cee71dSXin LI 
579b6cee71dSXin LI private void
580b6cee71dSXin LI mlist_free(struct mlist *mlist)
581b6cee71dSXin LI {
582c2931133SXin LI 	struct mlist *ml, *next;
583b6cee71dSXin LI 
584b6cee71dSXin LI 	if (mlist == NULL)
585b6cee71dSXin LI 		return;
586b6cee71dSXin LI 
587c2931133SXin LI 	ml = mlist->next;
588c2931133SXin LI 	for (ml = mlist->next; (next = ml->next) != NULL; ml = next) {
589b6cee71dSXin LI 		if (ml->map)
590b6cee71dSXin LI 			apprentice_unmap(ml->map);
591b6cee71dSXin LI 		free(ml);
592c2931133SXin LI 		if (ml == mlist)
593c2931133SXin LI 			break;
594b6cee71dSXin LI 	}
595b6cee71dSXin LI }
596b6cee71dSXin LI 
597c2931133SXin LI #ifndef COMPILE_ONLY
598c2931133SXin LI /* void **bufs: an array of compiled magic files */
599c2931133SXin LI protected int
600c2931133SXin LI buffer_apprentice(struct magic_set *ms, struct magic **bufs,
601c2931133SXin LI     size_t *sizes, size_t nbufs)
602c2931133SXin LI {
603c2931133SXin LI 	size_t i, j;
604c2931133SXin LI 	struct mlist *ml;
605c2931133SXin LI 	struct magic_map *map;
606c2931133SXin LI 
607c2931133SXin LI 	if (nbufs == 0)
608c2931133SXin LI 		return -1;
609c2931133SXin LI 
610c2931133SXin LI 	if (ms->mlist[0] != NULL)
611c2931133SXin LI 		file_reset(ms);
612c2931133SXin LI 
613c2931133SXin LI 	init_file_tables();
614c2931133SXin LI 
615c2931133SXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
616c2931133SXin LI 		mlist_free(ms->mlist[i]);
617c2931133SXin LI 		if ((ms->mlist[i] = mlist_alloc()) == NULL) {
618c2931133SXin LI 			file_oomem(ms, sizeof(*ms->mlist[i]));
619c2931133SXin LI 			goto fail;
620c2931133SXin LI 		}
621c2931133SXin LI 	}
622c2931133SXin LI 
623c2931133SXin LI 	for (i = 0; i < nbufs; i++) {
624c2931133SXin LI 		map = apprentice_buf(ms, bufs[i], sizes[i]);
625c2931133SXin LI 		if (map == NULL)
626c2931133SXin LI 			goto fail;
627c2931133SXin LI 
628c2931133SXin LI 		for (j = 0; j < MAGIC_SETS; j++) {
629c2931133SXin LI 			if (add_mlist(ms->mlist[j], map, j) == -1) {
630c2931133SXin LI 				file_oomem(ms, sizeof(*ml));
631c2931133SXin LI 				goto fail;
632c2931133SXin LI 			}
633c2931133SXin LI 		}
634c2931133SXin LI 	}
635c2931133SXin LI 
636c2931133SXin LI 	return 0;
637c2931133SXin LI fail:
638c2931133SXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
639c2931133SXin LI 		mlist_free(ms->mlist[i]);
640c2931133SXin LI 		ms->mlist[i] = NULL;
641c2931133SXin LI 	}
642c2931133SXin LI 	return -1;
643c2931133SXin LI }
644c2931133SXin LI #endif
645c2931133SXin LI 
646b6cee71dSXin LI /* const char *fn: list of magic files and directories */
647b6cee71dSXin LI protected int
648b6cee71dSXin LI file_apprentice(struct magic_set *ms, const char *fn, int action)
649b6cee71dSXin LI {
650b6cee71dSXin LI 	char *p, *mfn;
651b6cee71dSXin LI 	int file_err, errs = -1;
652b6cee71dSXin LI 	size_t i;
653b6cee71dSXin LI 
654b6cee71dSXin LI 	if (ms->mlist[0] != NULL)
655b6cee71dSXin LI 		file_reset(ms);
656b6cee71dSXin LI 
657b6cee71dSXin LI 	if ((fn = magic_getpath(fn, action)) == NULL)
658b6cee71dSXin LI 		return -1;
659b6cee71dSXin LI 
660b6cee71dSXin LI 	init_file_tables();
661b6cee71dSXin LI 
662b6cee71dSXin LI 	if ((mfn = strdup(fn)) == NULL) {
663b6cee71dSXin LI 		file_oomem(ms, strlen(fn));
664b6cee71dSXin LI 		return -1;
665b6cee71dSXin LI 	}
666b6cee71dSXin LI 
667b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
668b6cee71dSXin LI 		mlist_free(ms->mlist[i]);
669b6cee71dSXin LI 		if ((ms->mlist[i] = mlist_alloc()) == NULL) {
670b6cee71dSXin LI 			file_oomem(ms, sizeof(*ms->mlist[i]));
671c2931133SXin LI 			while (i-- > 0) {
672b6cee71dSXin LI 				mlist_free(ms->mlist[i]);
673c2931133SXin LI 				ms->mlist[i] = NULL;
674b6cee71dSXin LI 			}
675b6cee71dSXin LI 			free(mfn);
676b6cee71dSXin LI 			return -1;
677b6cee71dSXin LI 		}
678b6cee71dSXin LI 	}
679b6cee71dSXin LI 	fn = mfn;
680b6cee71dSXin LI 
681b6cee71dSXin LI 	while (fn) {
682b6cee71dSXin LI 		p = strchr(fn, PATHSEP);
683b6cee71dSXin LI 		if (p)
684b6cee71dSXin LI 			*p++ = '\0';
685b6cee71dSXin LI 		if (*fn == '\0')
686b6cee71dSXin LI 			break;
687b6cee71dSXin LI 		file_err = apprentice_1(ms, fn, action);
688b6cee71dSXin LI 		errs = MAX(errs, file_err);
689b6cee71dSXin LI 		fn = p;
690b6cee71dSXin LI 	}
691b6cee71dSXin LI 
692b6cee71dSXin LI 	free(mfn);
693b6cee71dSXin LI 
694b6cee71dSXin LI 	if (errs == -1) {
695b6cee71dSXin LI 		for (i = 0; i < MAGIC_SETS; i++) {
696b6cee71dSXin LI 			mlist_free(ms->mlist[i]);
697b6cee71dSXin LI 			ms->mlist[i] = NULL;
698b6cee71dSXin LI 		}
699b6cee71dSXin LI 		file_error(ms, 0, "could not find any valid magic files!");
700b6cee71dSXin LI 		return -1;
701b6cee71dSXin LI 	}
702b6cee71dSXin LI 
703b6cee71dSXin LI #if 0
704b6cee71dSXin LI 	/*
705b6cee71dSXin LI 	 * Always leave the database loaded
706b6cee71dSXin LI 	 */
707b6cee71dSXin LI 	if (action == FILE_LOAD)
708b6cee71dSXin LI 		return 0;
709b6cee71dSXin LI 
710b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
711b6cee71dSXin LI 		mlist_free(ms->mlist[i]);
712b6cee71dSXin LI 		ms->mlist[i] = NULL;
713b6cee71dSXin LI 	}
714b6cee71dSXin LI #endif
715b6cee71dSXin LI 
716b6cee71dSXin LI 	switch (action) {
717b6cee71dSXin LI 	case FILE_LOAD:
718b6cee71dSXin LI 	case FILE_COMPILE:
719b6cee71dSXin LI 	case FILE_CHECK:
720b6cee71dSXin LI 	case FILE_LIST:
721b6cee71dSXin LI 		return 0;
722b6cee71dSXin LI 	default:
723b6cee71dSXin LI 		file_error(ms, 0, "Invalid action %d", action);
724b6cee71dSXin LI 		return -1;
725b6cee71dSXin LI 	}
726b6cee71dSXin LI }
727b6cee71dSXin LI 
728b6cee71dSXin LI /*
729b6cee71dSXin LI  * Compute the real length of a magic expression, for the purposes
730b6cee71dSXin LI  * of determining how "strong" a magic expression is (approximating
731b6cee71dSXin LI  * how specific its matches are):
732b6cee71dSXin LI  *	- magic characters count 0 unless escaped.
733b6cee71dSXin LI  *	- [] expressions count 1
734b6cee71dSXin LI  *	- {} expressions count 0
735b6cee71dSXin LI  *	- regular characters or escaped magic characters count 1
736b6cee71dSXin LI  *	- 0 length expressions count as one
737b6cee71dSXin LI  */
738b6cee71dSXin LI private size_t
739b6cee71dSXin LI nonmagic(const char *str)
740b6cee71dSXin LI {
741b6cee71dSXin LI 	const char *p;
742b6cee71dSXin LI 	size_t rv = 0;
743b6cee71dSXin LI 
744b6cee71dSXin LI 	for (p = str; *p; p++)
745b6cee71dSXin LI 		switch (*p) {
746b6cee71dSXin LI 		case '\\':	/* Escaped anything counts 1 */
747b6cee71dSXin LI 			if (!*++p)
748b6cee71dSXin LI 				p--;
749b6cee71dSXin LI 			rv++;
750b6cee71dSXin LI 			continue;
751b6cee71dSXin LI 		case '?':	/* Magic characters count 0 */
752b6cee71dSXin LI 		case '*':
753b6cee71dSXin LI 		case '.':
754b6cee71dSXin LI 		case '+':
755b6cee71dSXin LI 		case '^':
756b6cee71dSXin LI 		case '$':
757b6cee71dSXin LI 			continue;
758b6cee71dSXin LI 		case '[':	/* Bracketed expressions count 1 the ']' */
759b6cee71dSXin LI 			while (*p && *p != ']')
760b6cee71dSXin LI 				p++;
761b6cee71dSXin LI 			p--;
762b6cee71dSXin LI 			continue;
763b6cee71dSXin LI 		case '{':	/* Braced expressions count 0 */
764b6cee71dSXin LI 			while (*p && *p != '}')
765b6cee71dSXin LI 				p++;
766b6cee71dSXin LI 			if (!*p)
767b6cee71dSXin LI 				p--;
768b6cee71dSXin LI 			continue;
769b6cee71dSXin LI 		default:	/* Anything else counts 1 */
770b6cee71dSXin LI 			rv++;
771b6cee71dSXin LI 			continue;
772b6cee71dSXin LI 		}
773b6cee71dSXin LI 
774b6cee71dSXin LI 	return rv == 0 ? 1 : rv;	/* Return at least 1 */
775b6cee71dSXin LI }
776b6cee71dSXin LI 
777b6cee71dSXin LI /*
778b6cee71dSXin LI  * Get weight of this magic entry, for sorting purposes.
779b6cee71dSXin LI  */
780b6cee71dSXin LI private size_t
781b6cee71dSXin LI apprentice_magic_strength(const struct magic *m)
782b6cee71dSXin LI {
783b6cee71dSXin LI #define MULT 10
784b6cee71dSXin LI 	size_t v, val = 2 * MULT;	/* baseline strength */
785b6cee71dSXin LI 
786b6cee71dSXin LI 	switch (m->type) {
787b6cee71dSXin LI 	case FILE_DEFAULT:	/* make sure this sorts last */
788b6cee71dSXin LI 		if (m->factor_op != FILE_FACTOR_OP_NONE)
789b6cee71dSXin LI 			abort();
790b6cee71dSXin LI 		return 0;
791b6cee71dSXin LI 
792b6cee71dSXin LI 	case FILE_BYTE:
793b6cee71dSXin LI 		val += 1 * MULT;
794b6cee71dSXin LI 		break;
795b6cee71dSXin LI 
796b6cee71dSXin LI 	case FILE_SHORT:
797b6cee71dSXin LI 	case FILE_LESHORT:
798b6cee71dSXin LI 	case FILE_BESHORT:
799b6cee71dSXin LI 		val += 2 * MULT;
800b6cee71dSXin LI 		break;
801b6cee71dSXin LI 
802b6cee71dSXin LI 	case FILE_LONG:
803b6cee71dSXin LI 	case FILE_LELONG:
804b6cee71dSXin LI 	case FILE_BELONG:
805b6cee71dSXin LI 	case FILE_MELONG:
806b6cee71dSXin LI 		val += 4 * MULT;
807b6cee71dSXin LI 		break;
808b6cee71dSXin LI 
809b6cee71dSXin LI 	case FILE_PSTRING:
810b6cee71dSXin LI 	case FILE_STRING:
811b6cee71dSXin LI 		val += m->vallen * MULT;
812b6cee71dSXin LI 		break;
813b6cee71dSXin LI 
814b6cee71dSXin LI 	case FILE_BESTRING16:
815b6cee71dSXin LI 	case FILE_LESTRING16:
816b6cee71dSXin LI 		val += m->vallen * MULT / 2;
817b6cee71dSXin LI 		break;
818b6cee71dSXin LI 
819b6cee71dSXin LI 	case FILE_SEARCH:
820b6cee71dSXin LI 		val += m->vallen * MAX(MULT / m->vallen, 1);
821b6cee71dSXin LI 		break;
822b6cee71dSXin LI 
823b6cee71dSXin LI 	case FILE_REGEX:
824b6cee71dSXin LI 		v = nonmagic(m->value.s);
825b6cee71dSXin LI 		val += v * MAX(MULT / v, 1);
826b6cee71dSXin LI 		break;
827b6cee71dSXin LI 
828b6cee71dSXin LI 	case FILE_DATE:
829b6cee71dSXin LI 	case FILE_LEDATE:
830b6cee71dSXin LI 	case FILE_BEDATE:
831b6cee71dSXin LI 	case FILE_MEDATE:
832b6cee71dSXin LI 	case FILE_LDATE:
833b6cee71dSXin LI 	case FILE_LELDATE:
834b6cee71dSXin LI 	case FILE_BELDATE:
835b6cee71dSXin LI 	case FILE_MELDATE:
836b6cee71dSXin LI 	case FILE_FLOAT:
837b6cee71dSXin LI 	case FILE_BEFLOAT:
838b6cee71dSXin LI 	case FILE_LEFLOAT:
839b6cee71dSXin LI 		val += 4 * MULT;
840b6cee71dSXin LI 		break;
841b6cee71dSXin LI 
842b6cee71dSXin LI 	case FILE_QUAD:
843b6cee71dSXin LI 	case FILE_BEQUAD:
844b6cee71dSXin LI 	case FILE_LEQUAD:
845b6cee71dSXin LI 	case FILE_QDATE:
846b6cee71dSXin LI 	case FILE_LEQDATE:
847b6cee71dSXin LI 	case FILE_BEQDATE:
848b6cee71dSXin LI 	case FILE_QLDATE:
849b6cee71dSXin LI 	case FILE_LEQLDATE:
850b6cee71dSXin LI 	case FILE_BEQLDATE:
851b6cee71dSXin LI 	case FILE_QWDATE:
852b6cee71dSXin LI 	case FILE_LEQWDATE:
853b6cee71dSXin LI 	case FILE_BEQWDATE:
854b6cee71dSXin LI 	case FILE_DOUBLE:
855b6cee71dSXin LI 	case FILE_BEDOUBLE:
856b6cee71dSXin LI 	case FILE_LEDOUBLE:
857b6cee71dSXin LI 		val += 8 * MULT;
858b6cee71dSXin LI 		break;
859b6cee71dSXin LI 
860b6cee71dSXin LI 	case FILE_INDIRECT:
861b6cee71dSXin LI 	case FILE_NAME:
862b6cee71dSXin LI 	case FILE_USE:
863b6cee71dSXin LI 		break;
864b6cee71dSXin LI 
865b6cee71dSXin LI 	default:
866b6cee71dSXin LI 		(void)fprintf(stderr, "Bad type %d\n", m->type);
867b6cee71dSXin LI 		abort();
868b6cee71dSXin LI 	}
869b6cee71dSXin LI 
870b6cee71dSXin LI 	switch (m->reln) {
871b6cee71dSXin LI 	case 'x':	/* matches anything penalize */
872b6cee71dSXin LI 	case '!':       /* matches almost anything penalize */
873b6cee71dSXin LI 		val = 0;
874b6cee71dSXin LI 		break;
875b6cee71dSXin LI 
876b6cee71dSXin LI 	case '=':	/* Exact match, prefer */
877b6cee71dSXin LI 		val += MULT;
878b6cee71dSXin LI 		break;
879b6cee71dSXin LI 
880b6cee71dSXin LI 	case '>':
881b6cee71dSXin LI 	case '<':	/* comparison match reduce strength */
882b6cee71dSXin LI 		val -= 2 * MULT;
883b6cee71dSXin LI 		break;
884b6cee71dSXin LI 
885b6cee71dSXin LI 	case '^':
886b6cee71dSXin LI 	case '&':	/* masking bits, we could count them too */
887b6cee71dSXin LI 		val -= MULT;
888b6cee71dSXin LI 		break;
889b6cee71dSXin LI 
890b6cee71dSXin LI 	default:
891b6cee71dSXin LI 		(void)fprintf(stderr, "Bad relation %c\n", m->reln);
892b6cee71dSXin LI 		abort();
893b6cee71dSXin LI 	}
894b6cee71dSXin LI 
895b6cee71dSXin LI 	if (val == 0)	/* ensure we only return 0 for FILE_DEFAULT */
896b6cee71dSXin LI 		val = 1;
897b6cee71dSXin LI 
898b6cee71dSXin LI 	switch (m->factor_op) {
899b6cee71dSXin LI 	case FILE_FACTOR_OP_NONE:
900b6cee71dSXin LI 		break;
901b6cee71dSXin LI 	case FILE_FACTOR_OP_PLUS:
902b6cee71dSXin LI 		val += m->factor;
903b6cee71dSXin LI 		break;
904b6cee71dSXin LI 	case FILE_FACTOR_OP_MINUS:
905b6cee71dSXin LI 		val -= m->factor;
906b6cee71dSXin LI 		break;
907b6cee71dSXin LI 	case FILE_FACTOR_OP_TIMES:
908b6cee71dSXin LI 		val *= m->factor;
909b6cee71dSXin LI 		break;
910b6cee71dSXin LI 	case FILE_FACTOR_OP_DIV:
911b6cee71dSXin LI 		val /= m->factor;
912b6cee71dSXin LI 		break;
913b6cee71dSXin LI 	default:
914b6cee71dSXin LI 		abort();
915b6cee71dSXin LI 	}
916b6cee71dSXin LI 
917b6cee71dSXin LI 	/*
918b6cee71dSXin LI 	 * Magic entries with no description get a bonus because they depend
919b6cee71dSXin LI 	 * on subsequent magic entries to print something.
920b6cee71dSXin LI 	 */
921b6cee71dSXin LI 	if (m->desc[0] == '\0')
922b6cee71dSXin LI 		val++;
923b6cee71dSXin LI 	return val;
924b6cee71dSXin LI }
925b6cee71dSXin LI 
926b6cee71dSXin LI /*
927b6cee71dSXin LI  * Sort callback for sorting entries by "strength" (basically length)
928b6cee71dSXin LI  */
929b6cee71dSXin LI private int
930b6cee71dSXin LI apprentice_sort(const void *a, const void *b)
931b6cee71dSXin LI {
932b6cee71dSXin LI 	const struct magic_entry *ma = CAST(const struct magic_entry *, a);
933b6cee71dSXin LI 	const struct magic_entry *mb = CAST(const struct magic_entry *, b);
934b6cee71dSXin LI 	size_t sa = apprentice_magic_strength(ma->mp);
935b6cee71dSXin LI 	size_t sb = apprentice_magic_strength(mb->mp);
936b6cee71dSXin LI 	if (sa == sb)
937b6cee71dSXin LI 		return 0;
938b6cee71dSXin LI 	else if (sa > sb)
939b6cee71dSXin LI 		return -1;
940b6cee71dSXin LI 	else
941b6cee71dSXin LI 		return 1;
942b6cee71dSXin LI }
943b6cee71dSXin LI 
944b6cee71dSXin LI /*
945b6cee71dSXin LI  * Shows sorted patterns list in the order which is used for the matching
946b6cee71dSXin LI  */
947b6cee71dSXin LI private void
948b6cee71dSXin LI apprentice_list(struct mlist *mlist, int mode)
949b6cee71dSXin LI {
950b6cee71dSXin LI 	uint32_t magindex = 0;
951b6cee71dSXin LI 	struct mlist *ml;
952b6cee71dSXin LI 	for (ml = mlist->next; ml != mlist; ml = ml->next) {
953b6cee71dSXin LI 		for (magindex = 0; magindex < ml->nmagic; magindex++) {
954b6cee71dSXin LI 			struct magic *m = &ml->magic[magindex];
955b6cee71dSXin LI 			if ((m->flag & mode) != mode) {
956b6cee71dSXin LI 				/* Skip sub-tests */
957b6cee71dSXin LI 				while (magindex + 1 < ml->nmagic &&
958b6cee71dSXin LI 				       ml->magic[magindex + 1].cont_level != 0)
959b6cee71dSXin LI 					++magindex;
960b6cee71dSXin LI 				continue; /* Skip to next top-level test*/
961b6cee71dSXin LI 			}
962b6cee71dSXin LI 
963b6cee71dSXin LI 			/*
964b6cee71dSXin LI 			 * Try to iterate over the tree until we find item with
965b6cee71dSXin LI 			 * description/mimetype.
966b6cee71dSXin LI 			 */
967b6cee71dSXin LI 			while (magindex + 1 < ml->nmagic &&
968b6cee71dSXin LI 			       ml->magic[magindex + 1].cont_level != 0 &&
969b6cee71dSXin LI 			       *ml->magic[magindex].desc == '\0' &&
970b6cee71dSXin LI 			       *ml->magic[magindex].mimetype == '\0')
971b6cee71dSXin LI 				magindex++;
972b6cee71dSXin LI 
9735f0216bdSXin LI 			printf("Strength = %3" SIZE_T_FORMAT "u@%u: %s [%s]\n",
974b6cee71dSXin LI 			    apprentice_magic_strength(m),
9755f0216bdSXin LI 			    ml->magic[magindex].lineno,
976b6cee71dSXin LI 			    ml->magic[magindex].desc,
977b6cee71dSXin LI 			    ml->magic[magindex].mimetype);
978b6cee71dSXin LI 		}
979b6cee71dSXin LI 	}
980b6cee71dSXin LI }
981b6cee71dSXin LI 
982b6cee71dSXin LI private void
983b6cee71dSXin LI set_test_type(struct magic *mstart, struct magic *m)
984b6cee71dSXin LI {
985b6cee71dSXin LI 	switch (m->type) {
986b6cee71dSXin LI 	case FILE_BYTE:
987b6cee71dSXin LI 	case FILE_SHORT:
988b6cee71dSXin LI 	case FILE_LONG:
989b6cee71dSXin LI 	case FILE_DATE:
990b6cee71dSXin LI 	case FILE_BESHORT:
991b6cee71dSXin LI 	case FILE_BELONG:
992b6cee71dSXin LI 	case FILE_BEDATE:
993b6cee71dSXin LI 	case FILE_LESHORT:
994b6cee71dSXin LI 	case FILE_LELONG:
995b6cee71dSXin LI 	case FILE_LEDATE:
996b6cee71dSXin LI 	case FILE_LDATE:
997b6cee71dSXin LI 	case FILE_BELDATE:
998b6cee71dSXin LI 	case FILE_LELDATE:
999b6cee71dSXin LI 	case FILE_MEDATE:
1000b6cee71dSXin LI 	case FILE_MELDATE:
1001b6cee71dSXin LI 	case FILE_MELONG:
1002b6cee71dSXin LI 	case FILE_QUAD:
1003b6cee71dSXin LI 	case FILE_LEQUAD:
1004b6cee71dSXin LI 	case FILE_BEQUAD:
1005b6cee71dSXin LI 	case FILE_QDATE:
1006b6cee71dSXin LI 	case FILE_LEQDATE:
1007b6cee71dSXin LI 	case FILE_BEQDATE:
1008b6cee71dSXin LI 	case FILE_QLDATE:
1009b6cee71dSXin LI 	case FILE_LEQLDATE:
1010b6cee71dSXin LI 	case FILE_BEQLDATE:
1011b6cee71dSXin LI 	case FILE_QWDATE:
1012b6cee71dSXin LI 	case FILE_LEQWDATE:
1013b6cee71dSXin LI 	case FILE_BEQWDATE:
1014b6cee71dSXin LI 	case FILE_FLOAT:
1015b6cee71dSXin LI 	case FILE_BEFLOAT:
1016b6cee71dSXin LI 	case FILE_LEFLOAT:
1017b6cee71dSXin LI 	case FILE_DOUBLE:
1018b6cee71dSXin LI 	case FILE_BEDOUBLE:
1019b6cee71dSXin LI 	case FILE_LEDOUBLE:
1020b6cee71dSXin LI 		mstart->flag |= BINTEST;
1021b6cee71dSXin LI 		break;
1022b6cee71dSXin LI 	case FILE_STRING:
1023b6cee71dSXin LI 	case FILE_PSTRING:
1024b6cee71dSXin LI 	case FILE_BESTRING16:
1025b6cee71dSXin LI 	case FILE_LESTRING16:
1026b6cee71dSXin LI 		/* Allow text overrides */
1027b6cee71dSXin LI 		if (mstart->str_flags & STRING_TEXTTEST)
1028b6cee71dSXin LI 			mstart->flag |= TEXTTEST;
1029b6cee71dSXin LI 		else
1030b6cee71dSXin LI 			mstart->flag |= BINTEST;
1031b6cee71dSXin LI 		break;
1032b6cee71dSXin LI 	case FILE_REGEX:
1033b6cee71dSXin LI 	case FILE_SEARCH:
1034b6cee71dSXin LI 		/* Check for override */
1035b6cee71dSXin LI 		if (mstart->str_flags & STRING_BINTEST)
1036b6cee71dSXin LI 			mstart->flag |= BINTEST;
1037b6cee71dSXin LI 		if (mstart->str_flags & STRING_TEXTTEST)
1038b6cee71dSXin LI 			mstart->flag |= TEXTTEST;
1039b6cee71dSXin LI 
1040b6cee71dSXin LI 		if (mstart->flag & (TEXTTEST|BINTEST))
1041b6cee71dSXin LI 			break;
1042b6cee71dSXin LI 
1043b6cee71dSXin LI 		/* binary test if pattern is not text */
1044b6cee71dSXin LI 		if (file_looks_utf8(m->value.us, (size_t)m->vallen, NULL,
1045b6cee71dSXin LI 		    NULL) <= 0)
1046b6cee71dSXin LI 			mstart->flag |= BINTEST;
1047b6cee71dSXin LI 		else
1048b6cee71dSXin LI 			mstart->flag |= TEXTTEST;
1049b6cee71dSXin LI 		break;
1050b6cee71dSXin LI 	case FILE_DEFAULT:
1051b6cee71dSXin LI 		/* can't deduce anything; we shouldn't see this at the
1052b6cee71dSXin LI 		   top level anyway */
1053b6cee71dSXin LI 		break;
1054b6cee71dSXin LI 	case FILE_INVALID:
1055b6cee71dSXin LI 	default:
1056b6cee71dSXin LI 		/* invalid search type, but no need to complain here */
1057b6cee71dSXin LI 		break;
1058b6cee71dSXin LI 	}
1059b6cee71dSXin LI }
1060b6cee71dSXin LI 
1061b6cee71dSXin LI private int
1062b6cee71dSXin LI addentry(struct magic_set *ms, struct magic_entry *me,
1063b6cee71dSXin LI    struct magic_entry_set *mset)
1064b6cee71dSXin LI {
1065b6cee71dSXin LI 	size_t i = me->mp->type == FILE_NAME ? 1 : 0;
1066b6cee71dSXin LI 	if (mset[i].count == mset[i].max) {
1067b6cee71dSXin LI 		struct magic_entry *mp;
1068b6cee71dSXin LI 
1069b6cee71dSXin LI 		mset[i].max += ALLOC_INCR;
1070b6cee71dSXin LI 		if ((mp = CAST(struct magic_entry *,
1071b6cee71dSXin LI 		    realloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
1072b6cee71dSXin LI 		    NULL) {
1073b6cee71dSXin LI 			file_oomem(ms, sizeof(*mp) * mset[i].max);
1074b6cee71dSXin LI 			return -1;
1075b6cee71dSXin LI 		}
1076b6cee71dSXin LI 		(void)memset(&mp[mset[i].count], 0, sizeof(*mp) *
1077b6cee71dSXin LI 		    ALLOC_INCR);
1078b6cee71dSXin LI 		mset[i].me = mp;
1079b6cee71dSXin LI 	}
1080b6cee71dSXin LI 	mset[i].me[mset[i].count++] = *me;
1081b6cee71dSXin LI 	memset(me, 0, sizeof(*me));
1082b6cee71dSXin LI 	return 0;
1083b6cee71dSXin LI }
1084b6cee71dSXin LI 
1085b6cee71dSXin LI /*
1086b6cee71dSXin LI  * Load and parse one file.
1087b6cee71dSXin LI  */
1088b6cee71dSXin LI private void
1089b6cee71dSXin LI load_1(struct magic_set *ms, int action, const char *fn, int *errs,
1090b6cee71dSXin LI    struct magic_entry_set *mset)
1091b6cee71dSXin LI {
1092b6cee71dSXin LI 	size_t lineno = 0, llen = 0;
1093b6cee71dSXin LI 	char *line = NULL;
1094b6cee71dSXin LI 	ssize_t len;
1095b6cee71dSXin LI 	struct magic_entry me;
1096b6cee71dSXin LI 
1097b6cee71dSXin LI 	FILE *f = fopen(ms->file = fn, "r");
1098b6cee71dSXin LI 	if (f == NULL) {
1099b6cee71dSXin LI 		if (errno != ENOENT)
1100b6cee71dSXin LI 			file_error(ms, errno, "cannot read magic file `%s'",
1101b6cee71dSXin LI 				   fn);
1102b6cee71dSXin LI 		(*errs)++;
1103b6cee71dSXin LI 		return;
1104b6cee71dSXin LI 	}
1105b6cee71dSXin LI 
1106b6cee71dSXin LI 	memset(&me, 0, sizeof(me));
1107b6cee71dSXin LI 	/* read and parse this file */
1108b6cee71dSXin LI 	for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;
1109b6cee71dSXin LI 	    ms->line++) {
1110b6cee71dSXin LI 		if (len == 0) /* null line, garbage, etc */
1111b6cee71dSXin LI 			continue;
1112b6cee71dSXin LI 		if (line[len - 1] == '\n') {
1113b6cee71dSXin LI 			lineno++;
1114b6cee71dSXin LI 			line[len - 1] = '\0'; /* delete newline */
1115b6cee71dSXin LI 		}
1116b6cee71dSXin LI 		switch (line[0]) {
1117b6cee71dSXin LI 		case '\0':	/* empty, do not parse */
1118b6cee71dSXin LI 		case '#':	/* comment, do not parse */
1119b6cee71dSXin LI 			continue;
1120b6cee71dSXin LI 		case '!':
1121b6cee71dSXin LI 			if (line[1] == ':') {
1122b6cee71dSXin LI 				size_t i;
1123b6cee71dSXin LI 
1124b6cee71dSXin LI 				for (i = 0; bang[i].name != NULL; i++) {
1125b6cee71dSXin LI 					if ((size_t)(len - 2) > bang[i].len &&
1126b6cee71dSXin LI 					    memcmp(bang[i].name, line + 2,
1127b6cee71dSXin LI 					    bang[i].len) == 0)
1128b6cee71dSXin LI 						break;
1129b6cee71dSXin LI 				}
1130b6cee71dSXin LI 				if (bang[i].name == NULL) {
1131b6cee71dSXin LI 					file_error(ms, 0,
1132b6cee71dSXin LI 					    "Unknown !: entry `%s'", line);
1133b6cee71dSXin LI 					(*errs)++;
1134b6cee71dSXin LI 					continue;
1135b6cee71dSXin LI 				}
1136b6cee71dSXin LI 				if (me.mp == NULL) {
1137b6cee71dSXin LI 					file_error(ms, 0,
1138b6cee71dSXin LI 					    "No current entry for :!%s type",
1139b6cee71dSXin LI 						bang[i].name);
1140b6cee71dSXin LI 					(*errs)++;
1141b6cee71dSXin LI 					continue;
1142b6cee71dSXin LI 				}
1143b6cee71dSXin LI 				if ((*bang[i].fun)(ms, &me,
1144b6cee71dSXin LI 				    line + bang[i].len + 2) != 0) {
1145b6cee71dSXin LI 					(*errs)++;
1146b6cee71dSXin LI 					continue;
1147b6cee71dSXin LI 				}
1148b6cee71dSXin LI 				continue;
1149b6cee71dSXin LI 			}
1150b6cee71dSXin LI 			/*FALLTHROUGH*/
1151b6cee71dSXin LI 		default:
1152b6cee71dSXin LI 		again:
1153b6cee71dSXin LI 			switch (parse(ms, &me, line, lineno, action)) {
1154b6cee71dSXin LI 			case 0:
1155b6cee71dSXin LI 				continue;
1156b6cee71dSXin LI 			case 1:
1157b6cee71dSXin LI 				(void)addentry(ms, &me, mset);
1158b6cee71dSXin LI 				goto again;
1159b6cee71dSXin LI 			default:
1160b6cee71dSXin LI 				(*errs)++;
1161b6cee71dSXin LI 				break;
1162b6cee71dSXin LI 			}
1163b6cee71dSXin LI 		}
1164b6cee71dSXin LI 	}
1165b6cee71dSXin LI 	if (me.mp)
1166b6cee71dSXin LI 		(void)addentry(ms, &me, mset);
1167b6cee71dSXin LI 	free(line);
1168b6cee71dSXin LI 	(void)fclose(f);
1169b6cee71dSXin LI }
1170b6cee71dSXin LI 
1171b6cee71dSXin LI /*
1172b6cee71dSXin LI  * parse a file or directory of files
1173b6cee71dSXin LI  * const char *fn: name of magic file or directory
1174b6cee71dSXin LI  */
1175b6cee71dSXin LI private int
1176b6cee71dSXin LI cmpstrp(const void *p1, const void *p2)
1177b6cee71dSXin LI {
1178b6cee71dSXin LI         return strcmp(*(char *const *)p1, *(char *const *)p2);
1179b6cee71dSXin LI }
1180b6cee71dSXin LI 
1181b6cee71dSXin LI 
1182b6cee71dSXin LI private uint32_t
1183b6cee71dSXin LI set_text_binary(struct magic_set *ms, struct magic_entry *me, uint32_t nme,
1184b6cee71dSXin LI     uint32_t starttest)
1185b6cee71dSXin LI {
1186b6cee71dSXin LI 	static const char text[] = "text";
1187b6cee71dSXin LI 	static const char binary[] = "binary";
1188b6cee71dSXin LI 	static const size_t len = sizeof(text);
1189b6cee71dSXin LI 
1190b6cee71dSXin LI 	uint32_t i = starttest;
1191b6cee71dSXin LI 
1192b6cee71dSXin LI 	do {
1193b6cee71dSXin LI 		set_test_type(me[starttest].mp, me[i].mp);
1194b6cee71dSXin LI 		if ((ms->flags & MAGIC_DEBUG) == 0)
1195b6cee71dSXin LI 			continue;
1196b6cee71dSXin LI 		(void)fprintf(stderr, "%s%s%s: %s\n",
1197b6cee71dSXin LI 		    me[i].mp->mimetype,
1198b6cee71dSXin LI 		    me[i].mp->mimetype[0] == '\0' ? "" : "; ",
1199b6cee71dSXin LI 		    me[i].mp->desc[0] ? me[i].mp->desc : "(no description)",
1200b6cee71dSXin LI 		    me[i].mp->flag & BINTEST ? binary : text);
1201b6cee71dSXin LI 		if (me[i].mp->flag & BINTEST) {
1202b6cee71dSXin LI 			char *p = strstr(me[i].mp->desc, text);
1203b6cee71dSXin LI 			if (p && (p == me[i].mp->desc ||
1204b6cee71dSXin LI 			    isspace((unsigned char)p[-1])) &&
1205b6cee71dSXin LI 			    (p + len - me[i].mp->desc == MAXstring
1206b6cee71dSXin LI 			    || (p[len] == '\0' ||
1207b6cee71dSXin LI 			    isspace((unsigned char)p[len]))))
1208b6cee71dSXin LI 				(void)fprintf(stderr, "*** Possible "
1209b6cee71dSXin LI 				    "binary test for text type\n");
1210b6cee71dSXin LI 		}
1211b6cee71dSXin LI 	} while (++i < nme && me[i].mp->cont_level != 0);
1212b6cee71dSXin LI 	return i;
1213b6cee71dSXin LI }
1214b6cee71dSXin LI 
1215b6cee71dSXin LI private void
1216b6cee71dSXin LI set_last_default(struct magic_set *ms, struct magic_entry *me, uint32_t nme)
1217b6cee71dSXin LI {
1218b6cee71dSXin LI 	uint32_t i;
1219b6cee71dSXin LI 	for (i = 0; i < nme; i++) {
1220b6cee71dSXin LI 		if (me[i].mp->cont_level == 0 &&
1221b6cee71dSXin LI 		    me[i].mp->type == FILE_DEFAULT) {
1222b6cee71dSXin LI 			while (++i < nme)
1223b6cee71dSXin LI 				if (me[i].mp->cont_level == 0)
1224b6cee71dSXin LI 					break;
1225b6cee71dSXin LI 			if (i != nme) {
1226b6cee71dSXin LI 				/* XXX - Ugh! */
1227b6cee71dSXin LI 				ms->line = me[i].mp->lineno;
1228b6cee71dSXin LI 				file_magwarn(ms,
1229b6cee71dSXin LI 				    "level 0 \"default\" did not sort last");
1230b6cee71dSXin LI 			}
1231b6cee71dSXin LI 			return;
1232b6cee71dSXin LI 		}
1233b6cee71dSXin LI 	}
1234b6cee71dSXin LI }
1235b6cee71dSXin LI 
1236b6cee71dSXin LI private int
1237b6cee71dSXin LI coalesce_entries(struct magic_set *ms, struct magic_entry *me, uint32_t nme,
1238b6cee71dSXin LI     struct magic **ma, uint32_t *nma)
1239b6cee71dSXin LI {
1240b6cee71dSXin LI 	uint32_t i, mentrycount = 0;
1241b6cee71dSXin LI 	size_t slen;
1242b6cee71dSXin LI 
1243b6cee71dSXin LI 	for (i = 0; i < nme; i++)
1244b6cee71dSXin LI 		mentrycount += me[i].cont_count;
1245b6cee71dSXin LI 
1246b6cee71dSXin LI 	slen = sizeof(**ma) * mentrycount;
1247b6cee71dSXin LI 	if ((*ma = CAST(struct magic *, malloc(slen))) == NULL) {
1248b6cee71dSXin LI 		file_oomem(ms, slen);
1249b6cee71dSXin LI 		return -1;
1250b6cee71dSXin LI 	}
1251b6cee71dSXin LI 
1252b6cee71dSXin LI 	mentrycount = 0;
1253b6cee71dSXin LI 	for (i = 0; i < nme; i++) {
1254b6cee71dSXin LI 		(void)memcpy(*ma + mentrycount, me[i].mp,
1255b6cee71dSXin LI 		    me[i].cont_count * sizeof(**ma));
1256b6cee71dSXin LI 		mentrycount += me[i].cont_count;
1257b6cee71dSXin LI 	}
1258b6cee71dSXin LI 	*nma = mentrycount;
1259b6cee71dSXin LI 	return 0;
1260b6cee71dSXin LI }
1261b6cee71dSXin LI 
1262b6cee71dSXin LI private void
1263b6cee71dSXin LI magic_entry_free(struct magic_entry *me, uint32_t nme)
1264b6cee71dSXin LI {
1265b6cee71dSXin LI 	uint32_t i;
1266b6cee71dSXin LI 	if (me == NULL)
1267b6cee71dSXin LI 		return;
1268b6cee71dSXin LI 	for (i = 0; i < nme; i++)
1269b6cee71dSXin LI 		free(me[i].mp);
1270b6cee71dSXin LI 	free(me);
1271b6cee71dSXin LI }
1272b6cee71dSXin LI 
1273b6cee71dSXin LI private struct magic_map *
1274b6cee71dSXin LI apprentice_load(struct magic_set *ms, const char *fn, int action)
1275b6cee71dSXin LI {
1276b6cee71dSXin LI 	int errs = 0;
1277b6cee71dSXin LI 	uint32_t i, j;
1278b6cee71dSXin LI 	size_t files = 0, maxfiles = 0;
1279b6cee71dSXin LI 	char **filearr = NULL, *mfn;
1280b6cee71dSXin LI 	struct stat st;
1281b6cee71dSXin LI 	struct magic_map *map;
1282b6cee71dSXin LI 	struct magic_entry_set mset[MAGIC_SETS];
1283b6cee71dSXin LI 	DIR *dir;
1284b6cee71dSXin LI 	struct dirent *d;
1285b6cee71dSXin LI 
1286b6cee71dSXin LI 	memset(mset, 0, sizeof(mset));
1287b6cee71dSXin LI 	ms->flags |= MAGIC_CHECK;	/* Enable checks for parsed files */
1288b6cee71dSXin LI 
1289b6cee71dSXin LI 
1290b6cee71dSXin LI 	if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL)
1291b6cee71dSXin LI 	{
1292b6cee71dSXin LI 		file_oomem(ms, sizeof(*map));
1293b6cee71dSXin LI 		return NULL;
1294b6cee71dSXin LI 	}
1295*9ce06829SXin LI 	map->type = MAP_TYPE_MALLOC;
1296b6cee71dSXin LI 
1297b6cee71dSXin LI 	/* print silly verbose header for USG compat. */
1298b6cee71dSXin LI 	if (action == FILE_CHECK)
1299b6cee71dSXin LI 		(void)fprintf(stderr, "%s\n", usg_hdr);
1300b6cee71dSXin LI 
1301b6cee71dSXin LI 	/* load directory or file */
1302b6cee71dSXin LI 	if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
1303b6cee71dSXin LI 		dir = opendir(fn);
1304b6cee71dSXin LI 		if (!dir) {
1305b6cee71dSXin LI 			errs++;
1306b6cee71dSXin LI 			goto out;
1307b6cee71dSXin LI 		}
1308b6cee71dSXin LI 		while ((d = readdir(dir)) != NULL) {
1309b6cee71dSXin LI 			if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
1310b6cee71dSXin LI 				file_oomem(ms,
1311b6cee71dSXin LI 				    strlen(fn) + strlen(d->d_name) + 2);
1312b6cee71dSXin LI 				errs++;
1313b6cee71dSXin LI 				closedir(dir);
1314b6cee71dSXin LI 				goto out;
1315b6cee71dSXin LI 			}
1316b6cee71dSXin LI 			if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
1317b6cee71dSXin LI 				free(mfn);
1318b6cee71dSXin LI 				continue;
1319b6cee71dSXin LI 			}
1320b6cee71dSXin LI 			if (files >= maxfiles) {
1321b6cee71dSXin LI 				size_t mlen;
1322b6cee71dSXin LI 				maxfiles = (maxfiles + 1) * 2;
1323b6cee71dSXin LI 				mlen = maxfiles * sizeof(*filearr);
1324b6cee71dSXin LI 				if ((filearr = CAST(char **,
1325b6cee71dSXin LI 				    realloc(filearr, mlen))) == NULL) {
1326b6cee71dSXin LI 					file_oomem(ms, mlen);
1327b6cee71dSXin LI 					free(mfn);
1328b6cee71dSXin LI 					closedir(dir);
1329b6cee71dSXin LI 					errs++;
1330b6cee71dSXin LI 					goto out;
1331b6cee71dSXin LI 				}
1332b6cee71dSXin LI 			}
1333b6cee71dSXin LI 			filearr[files++] = mfn;
1334b6cee71dSXin LI 		}
1335b6cee71dSXin LI 		closedir(dir);
1336b6cee71dSXin LI 		qsort(filearr, files, sizeof(*filearr), cmpstrp);
1337b6cee71dSXin LI 		for (i = 0; i < files; i++) {
1338b6cee71dSXin LI 			load_1(ms, action, filearr[i], &errs, mset);
1339b6cee71dSXin LI 			free(filearr[i]);
1340b6cee71dSXin LI 		}
1341b6cee71dSXin LI 		free(filearr);
1342b6cee71dSXin LI 	} else
1343b6cee71dSXin LI 		load_1(ms, action, fn, &errs, mset);
1344b6cee71dSXin LI 	if (errs)
1345b6cee71dSXin LI 		goto out;
1346b6cee71dSXin LI 
1347b6cee71dSXin LI 	for (j = 0; j < MAGIC_SETS; j++) {
1348b6cee71dSXin LI 		/* Set types of tests */
1349b6cee71dSXin LI 		for (i = 0; i < mset[j].count; ) {
1350b6cee71dSXin LI 			if (mset[j].me[i].mp->cont_level != 0) {
1351b6cee71dSXin LI 				i++;
1352b6cee71dSXin LI 				continue;
1353b6cee71dSXin LI 			}
1354b6cee71dSXin LI 			i = set_text_binary(ms, mset[j].me, mset[j].count, i);
1355b6cee71dSXin LI 		}
1356*9ce06829SXin LI 		if (mset[j].me)
1357b6cee71dSXin LI 			qsort(mset[j].me, mset[j].count, sizeof(*mset[j].me),
1358b6cee71dSXin LI 			    apprentice_sort);
1359b6cee71dSXin LI 
1360b6cee71dSXin LI 		/*
1361b6cee71dSXin LI 		 * Make sure that any level 0 "default" line is last
1362b6cee71dSXin LI 		 * (if one exists).
1363b6cee71dSXin LI 		 */
1364b6cee71dSXin LI 		set_last_default(ms, mset[j].me, mset[j].count);
1365b6cee71dSXin LI 
1366b6cee71dSXin LI 		/* coalesce per file arrays into a single one */
1367b6cee71dSXin LI 		if (coalesce_entries(ms, mset[j].me, mset[j].count,
1368b6cee71dSXin LI 		    &map->magic[j], &map->nmagic[j]) == -1) {
1369b6cee71dSXin LI 			errs++;
1370b6cee71dSXin LI 			goto out;
1371b6cee71dSXin LI 		}
1372b6cee71dSXin LI 	}
1373b6cee71dSXin LI 
1374b6cee71dSXin LI out:
1375b6cee71dSXin LI 	for (j = 0; j < MAGIC_SETS; j++)
1376b6cee71dSXin LI 		magic_entry_free(mset[j].me, mset[j].count);
1377b6cee71dSXin LI 
1378b6cee71dSXin LI 	if (errs) {
1379b6cee71dSXin LI 		apprentice_unmap(map);
1380b6cee71dSXin LI 		return NULL;
1381b6cee71dSXin LI 	}
1382b6cee71dSXin LI 	return map;
1383b6cee71dSXin LI }
1384b6cee71dSXin LI 
1385b6cee71dSXin LI /*
1386b6cee71dSXin LI  * extend the sign bit if the comparison is to be signed
1387b6cee71dSXin LI  */
1388b6cee71dSXin LI protected uint64_t
1389b6cee71dSXin LI file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
1390b6cee71dSXin LI {
1391b6cee71dSXin LI 	if (!(m->flag & UNSIGNED)) {
1392b6cee71dSXin LI 		switch(m->type) {
1393b6cee71dSXin LI 		/*
1394b6cee71dSXin LI 		 * Do not remove the casts below.  They are
1395b6cee71dSXin LI 		 * vital.  When later compared with the data,
1396b6cee71dSXin LI 		 * the sign extension must have happened.
1397b6cee71dSXin LI 		 */
1398b6cee71dSXin LI 		case FILE_BYTE:
1399c2931133SXin LI 			v = (signed char) v;
1400b6cee71dSXin LI 			break;
1401b6cee71dSXin LI 		case FILE_SHORT:
1402b6cee71dSXin LI 		case FILE_BESHORT:
1403b6cee71dSXin LI 		case FILE_LESHORT:
1404b6cee71dSXin LI 			v = (short) v;
1405b6cee71dSXin LI 			break;
1406b6cee71dSXin LI 		case FILE_DATE:
1407b6cee71dSXin LI 		case FILE_BEDATE:
1408b6cee71dSXin LI 		case FILE_LEDATE:
1409b6cee71dSXin LI 		case FILE_MEDATE:
1410b6cee71dSXin LI 		case FILE_LDATE:
1411b6cee71dSXin LI 		case FILE_BELDATE:
1412b6cee71dSXin LI 		case FILE_LELDATE:
1413b6cee71dSXin LI 		case FILE_MELDATE:
1414b6cee71dSXin LI 		case FILE_LONG:
1415b6cee71dSXin LI 		case FILE_BELONG:
1416b6cee71dSXin LI 		case FILE_LELONG:
1417b6cee71dSXin LI 		case FILE_MELONG:
1418b6cee71dSXin LI 		case FILE_FLOAT:
1419b6cee71dSXin LI 		case FILE_BEFLOAT:
1420b6cee71dSXin LI 		case FILE_LEFLOAT:
1421b6cee71dSXin LI 			v = (int32_t) v;
1422b6cee71dSXin LI 			break;
1423b6cee71dSXin LI 		case FILE_QUAD:
1424b6cee71dSXin LI 		case FILE_BEQUAD:
1425b6cee71dSXin LI 		case FILE_LEQUAD:
1426b6cee71dSXin LI 		case FILE_QDATE:
1427b6cee71dSXin LI 		case FILE_QLDATE:
1428b6cee71dSXin LI 		case FILE_QWDATE:
1429b6cee71dSXin LI 		case FILE_BEQDATE:
1430b6cee71dSXin LI 		case FILE_BEQLDATE:
1431b6cee71dSXin LI 		case FILE_BEQWDATE:
1432b6cee71dSXin LI 		case FILE_LEQDATE:
1433b6cee71dSXin LI 		case FILE_LEQLDATE:
1434b6cee71dSXin LI 		case FILE_LEQWDATE:
1435b6cee71dSXin LI 		case FILE_DOUBLE:
1436b6cee71dSXin LI 		case FILE_BEDOUBLE:
1437b6cee71dSXin LI 		case FILE_LEDOUBLE:
1438b6cee71dSXin LI 			v = (int64_t) v;
1439b6cee71dSXin LI 			break;
1440b6cee71dSXin LI 		case FILE_STRING:
1441b6cee71dSXin LI 		case FILE_PSTRING:
1442b6cee71dSXin LI 		case FILE_BESTRING16:
1443b6cee71dSXin LI 		case FILE_LESTRING16:
1444b6cee71dSXin LI 		case FILE_REGEX:
1445b6cee71dSXin LI 		case FILE_SEARCH:
1446b6cee71dSXin LI 		case FILE_DEFAULT:
1447b6cee71dSXin LI 		case FILE_INDIRECT:
1448b6cee71dSXin LI 		case FILE_NAME:
1449b6cee71dSXin LI 		case FILE_USE:
1450b6cee71dSXin LI 		case FILE_CLEAR:
1451b6cee71dSXin LI 			break;
1452b6cee71dSXin LI 		default:
1453b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
1454b6cee71dSXin LI 			    file_magwarn(ms, "cannot happen: m->type=%d\n",
1455b6cee71dSXin LI 				    m->type);
1456b6cee71dSXin LI 			return ~0U;
1457b6cee71dSXin LI 		}
1458b6cee71dSXin LI 	}
1459b6cee71dSXin LI 	return v;
1460b6cee71dSXin LI }
1461b6cee71dSXin LI 
1462b6cee71dSXin LI private int
1463b6cee71dSXin LI string_modifier_check(struct magic_set *ms, struct magic *m)
1464b6cee71dSXin LI {
1465b6cee71dSXin LI 	if ((ms->flags & MAGIC_CHECK) == 0)
1466b6cee71dSXin LI 		return 0;
1467b6cee71dSXin LI 
1468b6cee71dSXin LI 	if ((m->type != FILE_REGEX || (m->str_flags & REGEX_LINE_COUNT) == 0) &&
1469b6cee71dSXin LI 	    (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0)) {
1470b6cee71dSXin LI 		file_magwarn(ms,
1471b6cee71dSXin LI 		    "'/BHhLl' modifiers are only allowed for pascal strings\n");
1472b6cee71dSXin LI 		return -1;
1473b6cee71dSXin LI 	}
1474b6cee71dSXin LI 	switch (m->type) {
1475b6cee71dSXin LI 	case FILE_BESTRING16:
1476b6cee71dSXin LI 	case FILE_LESTRING16:
1477b6cee71dSXin LI 		if (m->str_flags != 0) {
1478b6cee71dSXin LI 			file_magwarn(ms,
1479b6cee71dSXin LI 			    "no modifiers allowed for 16-bit strings\n");
1480b6cee71dSXin LI 			return -1;
1481b6cee71dSXin LI 		}
1482b6cee71dSXin LI 		break;
1483b6cee71dSXin LI 	case FILE_STRING:
1484b6cee71dSXin LI 	case FILE_PSTRING:
1485b6cee71dSXin LI 		if ((m->str_flags & REGEX_OFFSET_START) != 0) {
1486b6cee71dSXin LI 			file_magwarn(ms,
1487b6cee71dSXin LI 			    "'/%c' only allowed on regex and search\n",
1488b6cee71dSXin LI 			    CHAR_REGEX_OFFSET_START);
1489b6cee71dSXin LI 			return -1;
1490b6cee71dSXin LI 		}
1491b6cee71dSXin LI 		break;
1492b6cee71dSXin LI 	case FILE_SEARCH:
1493b6cee71dSXin LI 		if (m->str_range == 0) {
1494b6cee71dSXin LI 			file_magwarn(ms,
1495b6cee71dSXin LI 			    "missing range; defaulting to %d\n",
1496b6cee71dSXin LI                             STRING_DEFAULT_RANGE);
1497b6cee71dSXin LI 			m->str_range = STRING_DEFAULT_RANGE;
1498b6cee71dSXin LI 			return -1;
1499b6cee71dSXin LI 		}
1500b6cee71dSXin LI 		break;
1501b6cee71dSXin LI 	case FILE_REGEX:
1502b6cee71dSXin LI 		if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) {
1503b6cee71dSXin LI 			file_magwarn(ms, "'/%c' not allowed on regex\n",
1504b6cee71dSXin LI 			    CHAR_COMPACT_WHITESPACE);
1505b6cee71dSXin LI 			return -1;
1506b6cee71dSXin LI 		}
1507b6cee71dSXin LI 		if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) {
1508b6cee71dSXin LI 			file_magwarn(ms, "'/%c' not allowed on regex\n",
1509b6cee71dSXin LI 			    CHAR_COMPACT_OPTIONAL_WHITESPACE);
1510b6cee71dSXin LI 			return -1;
1511b6cee71dSXin LI 		}
1512b6cee71dSXin LI 		break;
1513b6cee71dSXin LI 	default:
1514b6cee71dSXin LI 		file_magwarn(ms, "coding error: m->type=%d\n",
1515b6cee71dSXin LI 		    m->type);
1516b6cee71dSXin LI 		return -1;
1517b6cee71dSXin LI 	}
1518b6cee71dSXin LI 	return 0;
1519b6cee71dSXin LI }
1520b6cee71dSXin LI 
1521b6cee71dSXin LI private int
1522b6cee71dSXin LI get_op(char c)
1523b6cee71dSXin LI {
1524b6cee71dSXin LI 	switch (c) {
1525b6cee71dSXin LI 	case '&':
1526b6cee71dSXin LI 		return FILE_OPAND;
1527b6cee71dSXin LI 	case '|':
1528b6cee71dSXin LI 		return FILE_OPOR;
1529b6cee71dSXin LI 	case '^':
1530b6cee71dSXin LI 		return FILE_OPXOR;
1531b6cee71dSXin LI 	case '+':
1532b6cee71dSXin LI 		return FILE_OPADD;
1533b6cee71dSXin LI 	case '-':
1534b6cee71dSXin LI 		return FILE_OPMINUS;
1535b6cee71dSXin LI 	case '*':
1536b6cee71dSXin LI 		return FILE_OPMULTIPLY;
1537b6cee71dSXin LI 	case '/':
1538b6cee71dSXin LI 		return FILE_OPDIVIDE;
1539b6cee71dSXin LI 	case '%':
1540b6cee71dSXin LI 		return FILE_OPMODULO;
1541b6cee71dSXin LI 	default:
1542b6cee71dSXin LI 		return -1;
1543b6cee71dSXin LI 	}
1544b6cee71dSXin LI }
1545b6cee71dSXin LI 
1546b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS
1547b6cee71dSXin LI private int
1548b6cee71dSXin LI get_cond(const char *l, const char **t)
1549b6cee71dSXin LI {
1550b6cee71dSXin LI 	static const struct cond_tbl_s {
1551b6cee71dSXin LI 		char name[8];
1552b6cee71dSXin LI 		size_t len;
1553b6cee71dSXin LI 		int cond;
1554b6cee71dSXin LI 	} cond_tbl[] = {
1555b6cee71dSXin LI 		{ "if",		2,	COND_IF },
1556b6cee71dSXin LI 		{ "elif",	4,	COND_ELIF },
1557b6cee71dSXin LI 		{ "else",	4,	COND_ELSE },
1558b6cee71dSXin LI 		{ "",		0,	COND_NONE },
1559b6cee71dSXin LI 	};
1560b6cee71dSXin LI 	const struct cond_tbl_s *p;
1561b6cee71dSXin LI 
1562b6cee71dSXin LI 	for (p = cond_tbl; p->len; p++) {
1563b6cee71dSXin LI 		if (strncmp(l, p->name, p->len) == 0 &&
1564b6cee71dSXin LI 		    isspace((unsigned char)l[p->len])) {
1565b6cee71dSXin LI 			if (t)
1566b6cee71dSXin LI 				*t = l + p->len;
1567b6cee71dSXin LI 			break;
1568b6cee71dSXin LI 		}
1569b6cee71dSXin LI 	}
1570b6cee71dSXin LI 	return p->cond;
1571b6cee71dSXin LI }
1572b6cee71dSXin LI 
1573b6cee71dSXin LI private int
1574b6cee71dSXin LI check_cond(struct magic_set *ms, int cond, uint32_t cont_level)
1575b6cee71dSXin LI {
1576b6cee71dSXin LI 	int last_cond;
1577b6cee71dSXin LI 	last_cond = ms->c.li[cont_level].last_cond;
1578b6cee71dSXin LI 
1579b6cee71dSXin LI 	switch (cond) {
1580b6cee71dSXin LI 	case COND_IF:
1581b6cee71dSXin LI 		if (last_cond != COND_NONE && last_cond != COND_ELIF) {
1582b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
1583b6cee71dSXin LI 				file_magwarn(ms, "syntax error: `if'");
1584b6cee71dSXin LI 			return -1;
1585b6cee71dSXin LI 		}
1586b6cee71dSXin LI 		last_cond = COND_IF;
1587b6cee71dSXin LI 		break;
1588b6cee71dSXin LI 
1589b6cee71dSXin LI 	case COND_ELIF:
1590b6cee71dSXin LI 		if (last_cond != COND_IF && last_cond != COND_ELIF) {
1591b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
1592b6cee71dSXin LI 				file_magwarn(ms, "syntax error: `elif'");
1593b6cee71dSXin LI 			return -1;
1594b6cee71dSXin LI 		}
1595b6cee71dSXin LI 		last_cond = COND_ELIF;
1596b6cee71dSXin LI 		break;
1597b6cee71dSXin LI 
1598b6cee71dSXin LI 	case COND_ELSE:
1599b6cee71dSXin LI 		if (last_cond != COND_IF && last_cond != COND_ELIF) {
1600b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
1601b6cee71dSXin LI 				file_magwarn(ms, "syntax error: `else'");
1602b6cee71dSXin LI 			return -1;
1603b6cee71dSXin LI 		}
1604b6cee71dSXin LI 		last_cond = COND_NONE;
1605b6cee71dSXin LI 		break;
1606b6cee71dSXin LI 
1607b6cee71dSXin LI 	case COND_NONE:
1608b6cee71dSXin LI 		last_cond = COND_NONE;
1609b6cee71dSXin LI 		break;
1610b6cee71dSXin LI 	}
1611b6cee71dSXin LI 
1612b6cee71dSXin LI 	ms->c.li[cont_level].last_cond = last_cond;
1613b6cee71dSXin LI 	return 0;
1614b6cee71dSXin LI }
1615b6cee71dSXin LI #endif /* ENABLE_CONDITIONALS */
1616b6cee71dSXin LI 
16174460e5b0SXin LI private int
16184460e5b0SXin LI parse_indirect_modifier(struct magic_set *ms, struct magic *m, const char **lp)
16194460e5b0SXin LI {
16204460e5b0SXin LI 	const char *l = *lp;
16214460e5b0SXin LI 
16224460e5b0SXin LI 	while (!isspace((unsigned char)*++l))
16234460e5b0SXin LI 		switch (*l) {
16244460e5b0SXin LI 		case CHAR_INDIRECT_RELATIVE:
16254460e5b0SXin LI 			m->str_flags |= INDIRECT_RELATIVE;
16264460e5b0SXin LI 			break;
16274460e5b0SXin LI 		default:
16284460e5b0SXin LI 			if (ms->flags & MAGIC_CHECK)
16294460e5b0SXin LI 				file_magwarn(ms, "indirect modifier `%c' "
16304460e5b0SXin LI 					"invalid", *l);
16314460e5b0SXin LI 			*lp = l;
16324460e5b0SXin LI 			return -1;
16334460e5b0SXin LI 		}
16344460e5b0SXin LI 	*lp = l;
16354460e5b0SXin LI 	return 0;
16364460e5b0SXin LI }
16374460e5b0SXin LI 
16384460e5b0SXin LI private void
16394460e5b0SXin LI parse_op_modifier(struct magic_set *ms, struct magic *m, const char **lp,
16404460e5b0SXin LI     int op)
16414460e5b0SXin LI {
16424460e5b0SXin LI 	const char *l = *lp;
16434460e5b0SXin LI 	char *t;
16444460e5b0SXin LI 	uint64_t val;
16454460e5b0SXin LI 
16464460e5b0SXin LI 	++l;
16474460e5b0SXin LI 	m->mask_op |= op;
16484460e5b0SXin LI 	val = (uint64_t)strtoull(l, &t, 0);
16494460e5b0SXin LI 	l = t;
16504460e5b0SXin LI 	m->num_mask = file_signextend(ms, m, val);
16514460e5b0SXin LI 	eatsize(&l);
16524460e5b0SXin LI 	*lp = l;
16534460e5b0SXin LI }
16544460e5b0SXin LI 
16554460e5b0SXin LI private int
16564460e5b0SXin LI parse_string_modifier(struct magic_set *ms, struct magic *m, const char **lp)
16574460e5b0SXin LI {
16584460e5b0SXin LI 	const char *l = *lp;
16594460e5b0SXin LI 	char *t;
16604460e5b0SXin LI 	int have_range = 0;
16614460e5b0SXin LI 
16624460e5b0SXin LI 	while (!isspace((unsigned char)*++l)) {
16634460e5b0SXin LI 		switch (*l) {
16644460e5b0SXin LI 		case '0':  case '1':  case '2':
16654460e5b0SXin LI 		case '3':  case '4':  case '5':
16664460e5b0SXin LI 		case '6':  case '7':  case '8':
16674460e5b0SXin LI 		case '9':
16684460e5b0SXin LI 			if (have_range && (ms->flags & MAGIC_CHECK))
16694460e5b0SXin LI 				file_magwarn(ms, "multiple ranges");
16704460e5b0SXin LI 			have_range = 1;
16714460e5b0SXin LI 			m->str_range = CAST(uint32_t, strtoul(l, &t, 0));
16724460e5b0SXin LI 			if (m->str_range == 0)
16734460e5b0SXin LI 				file_magwarn(ms, "zero range");
16744460e5b0SXin LI 			l = t - 1;
16754460e5b0SXin LI 			break;
16764460e5b0SXin LI 		case CHAR_COMPACT_WHITESPACE:
16774460e5b0SXin LI 			m->str_flags |= STRING_COMPACT_WHITESPACE;
16784460e5b0SXin LI 			break;
16794460e5b0SXin LI 		case CHAR_COMPACT_OPTIONAL_WHITESPACE:
16804460e5b0SXin LI 			m->str_flags |= STRING_COMPACT_OPTIONAL_WHITESPACE;
16814460e5b0SXin LI 			break;
16824460e5b0SXin LI 		case CHAR_IGNORE_LOWERCASE:
16834460e5b0SXin LI 			m->str_flags |= STRING_IGNORE_LOWERCASE;
16844460e5b0SXin LI 			break;
16854460e5b0SXin LI 		case CHAR_IGNORE_UPPERCASE:
16864460e5b0SXin LI 			m->str_flags |= STRING_IGNORE_UPPERCASE;
16874460e5b0SXin LI 			break;
16884460e5b0SXin LI 		case CHAR_REGEX_OFFSET_START:
16894460e5b0SXin LI 			m->str_flags |= REGEX_OFFSET_START;
16904460e5b0SXin LI 			break;
16914460e5b0SXin LI 		case CHAR_BINTEST:
16924460e5b0SXin LI 			m->str_flags |= STRING_BINTEST;
16934460e5b0SXin LI 			break;
16944460e5b0SXin LI 		case CHAR_TEXTTEST:
16954460e5b0SXin LI 			m->str_flags |= STRING_TEXTTEST;
16964460e5b0SXin LI 			break;
16974460e5b0SXin LI 		case CHAR_TRIM:
16984460e5b0SXin LI 			m->str_flags |= STRING_TRIM;
16994460e5b0SXin LI 			break;
17004460e5b0SXin LI 		case CHAR_PSTRING_1_LE:
17014460e5b0SXin LI #define SET_LENGTH(a) m->str_flags = (m->str_flags & ~PSTRING_LEN) | (a)
17024460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17034460e5b0SXin LI 				goto bad;
17044460e5b0SXin LI 			SET_LENGTH(PSTRING_1_LE);
17054460e5b0SXin LI 			break;
17064460e5b0SXin LI 		case CHAR_PSTRING_2_BE:
17074460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17084460e5b0SXin LI 				goto bad;
17094460e5b0SXin LI 			SET_LENGTH(PSTRING_2_BE);
17104460e5b0SXin LI 			break;
17114460e5b0SXin LI 		case CHAR_PSTRING_2_LE:
17124460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17134460e5b0SXin LI 				goto bad;
17144460e5b0SXin LI 			SET_LENGTH(PSTRING_2_LE);
17154460e5b0SXin LI 			break;
17164460e5b0SXin LI 		case CHAR_PSTRING_4_BE:
17174460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17184460e5b0SXin LI 				goto bad;
17194460e5b0SXin LI 			SET_LENGTH(PSTRING_4_BE);
17204460e5b0SXin LI 			break;
17214460e5b0SXin LI 		case CHAR_PSTRING_4_LE:
17224460e5b0SXin LI 			switch (m->type) {
17234460e5b0SXin LI 			case FILE_PSTRING:
17244460e5b0SXin LI 			case FILE_REGEX:
17254460e5b0SXin LI 				break;
17264460e5b0SXin LI 			default:
17274460e5b0SXin LI 				goto bad;
17284460e5b0SXin LI 			}
17294460e5b0SXin LI 			SET_LENGTH(PSTRING_4_LE);
17304460e5b0SXin LI 			break;
17314460e5b0SXin LI 		case CHAR_PSTRING_LENGTH_INCLUDES_ITSELF:
17324460e5b0SXin LI 			if (m->type != FILE_PSTRING)
17334460e5b0SXin LI 				goto bad;
17344460e5b0SXin LI 			m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF;
17354460e5b0SXin LI 			break;
17364460e5b0SXin LI 		default:
17374460e5b0SXin LI 		bad:
17384460e5b0SXin LI 			if (ms->flags & MAGIC_CHECK)
17394460e5b0SXin LI 				file_magwarn(ms, "string modifier `%c' "
17404460e5b0SXin LI 					"invalid", *l);
17414460e5b0SXin LI 			goto out;
17424460e5b0SXin LI 		}
17434460e5b0SXin LI 		/* allow multiple '/' for readability */
17444460e5b0SXin LI 		if (l[1] == '/' && !isspace((unsigned char)l[2]))
17454460e5b0SXin LI 			l++;
17464460e5b0SXin LI 	}
17474460e5b0SXin LI 	if (string_modifier_check(ms, m) == -1)
17484460e5b0SXin LI 		goto out;
17494460e5b0SXin LI 	*lp = l;
17504460e5b0SXin LI 	return 0;
17514460e5b0SXin LI out:
17524460e5b0SXin LI 	*lp = l;
17534460e5b0SXin LI 	return -1;
17544460e5b0SXin LI }
17554460e5b0SXin LI 
1756b6cee71dSXin LI /*
1757b6cee71dSXin LI  * parse one line from magic file, put into magic[index++] if valid
1758b6cee71dSXin LI  */
1759b6cee71dSXin LI private int
1760b6cee71dSXin LI parse(struct magic_set *ms, struct magic_entry *me, const char *line,
1761b6cee71dSXin LI     size_t lineno, int action)
1762b6cee71dSXin LI {
1763b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS
1764b6cee71dSXin LI 	static uint32_t last_cont_level = 0;
1765b6cee71dSXin LI #endif
1766b6cee71dSXin LI 	size_t i;
1767b6cee71dSXin LI 	struct magic *m;
1768b6cee71dSXin LI 	const char *l = line;
1769b6cee71dSXin LI 	char *t;
1770b6cee71dSXin LI 	int op;
1771b6cee71dSXin LI 	uint32_t cont_level;
1772b6cee71dSXin LI 	int32_t diff;
1773b6cee71dSXin LI 
1774b6cee71dSXin LI 	cont_level = 0;
1775b6cee71dSXin LI 
1776b6cee71dSXin LI 	/*
1777b6cee71dSXin LI 	 * Parse the offset.
1778b6cee71dSXin LI 	 */
1779b6cee71dSXin LI 	while (*l == '>') {
1780b6cee71dSXin LI 		++l;		/* step over */
1781b6cee71dSXin LI 		cont_level++;
1782b6cee71dSXin LI 	}
1783b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS
1784b6cee71dSXin LI 	if (cont_level == 0 || cont_level > last_cont_level)
1785b6cee71dSXin LI 		if (file_check_mem(ms, cont_level) == -1)
1786b6cee71dSXin LI 			return -1;
1787b6cee71dSXin LI 	last_cont_level = cont_level;
1788b6cee71dSXin LI #endif
1789b6cee71dSXin LI 	if (cont_level != 0) {
1790b6cee71dSXin LI 		if (me->mp == NULL) {
1791b6cee71dSXin LI 			file_magerror(ms, "No current entry for continuation");
1792b6cee71dSXin LI 			return -1;
1793b6cee71dSXin LI 		}
1794b6cee71dSXin LI 		if (me->cont_count == 0) {
1795b6cee71dSXin LI 			file_magerror(ms, "Continuations present with 0 count");
1796b6cee71dSXin LI 			return -1;
1797b6cee71dSXin LI 		}
1798b6cee71dSXin LI 		m = &me->mp[me->cont_count - 1];
1799b6cee71dSXin LI 		diff = (int32_t)cont_level - (int32_t)m->cont_level;
1800b6cee71dSXin LI 		if (diff > 1)
1801b6cee71dSXin LI 			file_magwarn(ms, "New continuation level %u is more "
1802b6cee71dSXin LI 			    "than one larger than current level %u", cont_level,
1803b6cee71dSXin LI 			    m->cont_level);
1804b6cee71dSXin LI 		if (me->cont_count == me->max_count) {
1805b6cee71dSXin LI 			struct magic *nm;
1806b6cee71dSXin LI 			size_t cnt = me->max_count + ALLOC_CHUNK;
1807b6cee71dSXin LI 			if ((nm = CAST(struct magic *, realloc(me->mp,
1808b6cee71dSXin LI 			    sizeof(*nm) * cnt))) == NULL) {
1809b6cee71dSXin LI 				file_oomem(ms, sizeof(*nm) * cnt);
1810b6cee71dSXin LI 				return -1;
1811b6cee71dSXin LI 			}
1812b6cee71dSXin LI 			me->mp = m = nm;
1813b6cee71dSXin LI 			me->max_count = CAST(uint32_t, cnt);
1814b6cee71dSXin LI 		}
1815b6cee71dSXin LI 		m = &me->mp[me->cont_count++];
1816b6cee71dSXin LI 		(void)memset(m, 0, sizeof(*m));
1817b6cee71dSXin LI 		m->cont_level = cont_level;
1818b6cee71dSXin LI 	} else {
1819b6cee71dSXin LI 		static const size_t len = sizeof(*m) * ALLOC_CHUNK;
1820b6cee71dSXin LI 		if (me->mp != NULL)
1821b6cee71dSXin LI 			return 1;
1822b6cee71dSXin LI 		if ((m = CAST(struct magic *, malloc(len))) == NULL) {
1823b6cee71dSXin LI 			file_oomem(ms, len);
1824b6cee71dSXin LI 			return -1;
1825b6cee71dSXin LI 		}
1826b6cee71dSXin LI 		me->mp = m;
1827b6cee71dSXin LI 		me->max_count = ALLOC_CHUNK;
1828b6cee71dSXin LI 		(void)memset(m, 0, sizeof(*m));
1829b6cee71dSXin LI 		m->factor_op = FILE_FACTOR_OP_NONE;
1830b6cee71dSXin LI 		m->cont_level = 0;
1831b6cee71dSXin LI 		me->cont_count = 1;
1832b6cee71dSXin LI 	}
1833b6cee71dSXin LI 	m->lineno = CAST(uint32_t, lineno);
1834b6cee71dSXin LI 
1835b6cee71dSXin LI 	if (*l == '&') {  /* m->cont_level == 0 checked below. */
1836b6cee71dSXin LI                 ++l;            /* step over */
1837b6cee71dSXin LI                 m->flag |= OFFADD;
1838b6cee71dSXin LI         }
1839b6cee71dSXin LI 	if (*l == '(') {
1840b6cee71dSXin LI 		++l;		/* step over */
1841b6cee71dSXin LI 		m->flag |= INDIR;
1842b6cee71dSXin LI 		if (m->flag & OFFADD)
1843b6cee71dSXin LI 			m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
1844b6cee71dSXin LI 
1845b6cee71dSXin LI 		if (*l == '&') {  /* m->cont_level == 0 checked below */
1846b6cee71dSXin LI 			++l;            /* step over */
1847b6cee71dSXin LI 			m->flag |= OFFADD;
1848b6cee71dSXin LI 		}
1849b6cee71dSXin LI 	}
1850b6cee71dSXin LI 	/* Indirect offsets are not valid at level 0. */
18515f0216bdSXin LI 	if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD))) {
1852b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
1853b6cee71dSXin LI 			file_magwarn(ms, "relative offset at level 0");
18545f0216bdSXin LI 		return -1;
18555f0216bdSXin LI 	}
1856b6cee71dSXin LI 
1857b6cee71dSXin LI 	/* get offset, then skip over it */
1858b6cee71dSXin LI 	m->offset = (uint32_t)strtoul(l, &t, 0);
18595f0216bdSXin LI         if (l == t) {
1860b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
1861b6cee71dSXin LI 			file_magwarn(ms, "offset `%s' invalid", l);
18625f0216bdSXin LI 		return -1;
18635f0216bdSXin LI 	}
1864b6cee71dSXin LI         l = t;
1865b6cee71dSXin LI 
1866b6cee71dSXin LI 	if (m->flag & INDIR) {
1867b6cee71dSXin LI 		m->in_type = FILE_LONG;
1868b6cee71dSXin LI 		m->in_offset = 0;
1869b6cee71dSXin LI 		/*
1870b6cee71dSXin LI 		 * read [.lbs][+-]nnnnn)
1871b6cee71dSXin LI 		 */
1872b6cee71dSXin LI 		if (*l == '.') {
1873b6cee71dSXin LI 			l++;
1874b6cee71dSXin LI 			switch (*l) {
1875b6cee71dSXin LI 			case 'l':
1876b6cee71dSXin LI 				m->in_type = FILE_LELONG;
1877b6cee71dSXin LI 				break;
1878b6cee71dSXin LI 			case 'L':
1879b6cee71dSXin LI 				m->in_type = FILE_BELONG;
1880b6cee71dSXin LI 				break;
1881b6cee71dSXin LI 			case 'm':
1882b6cee71dSXin LI 				m->in_type = FILE_MELONG;
1883b6cee71dSXin LI 				break;
1884b6cee71dSXin LI 			case 'h':
1885b6cee71dSXin LI 			case 's':
1886b6cee71dSXin LI 				m->in_type = FILE_LESHORT;
1887b6cee71dSXin LI 				break;
1888b6cee71dSXin LI 			case 'H':
1889b6cee71dSXin LI 			case 'S':
1890b6cee71dSXin LI 				m->in_type = FILE_BESHORT;
1891b6cee71dSXin LI 				break;
1892b6cee71dSXin LI 			case 'c':
1893b6cee71dSXin LI 			case 'b':
1894b6cee71dSXin LI 			case 'C':
1895b6cee71dSXin LI 			case 'B':
1896b6cee71dSXin LI 				m->in_type = FILE_BYTE;
1897b6cee71dSXin LI 				break;
1898b6cee71dSXin LI 			case 'e':
1899b6cee71dSXin LI 			case 'f':
1900b6cee71dSXin LI 			case 'g':
1901b6cee71dSXin LI 				m->in_type = FILE_LEDOUBLE;
1902b6cee71dSXin LI 				break;
1903b6cee71dSXin LI 			case 'E':
1904b6cee71dSXin LI 			case 'F':
1905b6cee71dSXin LI 			case 'G':
1906b6cee71dSXin LI 				m->in_type = FILE_BEDOUBLE;
1907b6cee71dSXin LI 				break;
1908b6cee71dSXin LI 			case 'i':
1909b6cee71dSXin LI 				m->in_type = FILE_LEID3;
1910b6cee71dSXin LI 				break;
1911b6cee71dSXin LI 			case 'I':
1912b6cee71dSXin LI 				m->in_type = FILE_BEID3;
1913b6cee71dSXin LI 				break;
1914b6cee71dSXin LI 			default:
1915b6cee71dSXin LI 				if (ms->flags & MAGIC_CHECK)
1916b6cee71dSXin LI 					file_magwarn(ms,
1917b6cee71dSXin LI 					    "indirect offset type `%c' invalid",
1918b6cee71dSXin LI 					    *l);
19195f0216bdSXin LI 				return -1;
1920b6cee71dSXin LI 			}
1921b6cee71dSXin LI 			l++;
1922b6cee71dSXin LI 		}
1923b6cee71dSXin LI 
1924b6cee71dSXin LI 		m->in_op = 0;
1925b6cee71dSXin LI 		if (*l == '~') {
1926b6cee71dSXin LI 			m->in_op |= FILE_OPINVERSE;
1927b6cee71dSXin LI 			l++;
1928b6cee71dSXin LI 		}
1929b6cee71dSXin LI 		if ((op = get_op(*l)) != -1) {
1930b6cee71dSXin LI 			m->in_op |= op;
1931b6cee71dSXin LI 			l++;
1932b6cee71dSXin LI 		}
1933b6cee71dSXin LI 		if (*l == '(') {
1934b6cee71dSXin LI 			m->in_op |= FILE_OPINDIRECT;
1935b6cee71dSXin LI 			l++;
1936b6cee71dSXin LI 		}
1937b6cee71dSXin LI 		if (isdigit((unsigned char)*l) || *l == '-') {
1938b6cee71dSXin LI 			m->in_offset = (int32_t)strtol(l, &t, 0);
19395f0216bdSXin LI 			if (l == t) {
1940b6cee71dSXin LI 				if (ms->flags & MAGIC_CHECK)
1941b6cee71dSXin LI 					file_magwarn(ms,
1942b6cee71dSXin LI 					    "in_offset `%s' invalid", l);
19435f0216bdSXin LI 				return -1;
19445f0216bdSXin LI 			}
1945b6cee71dSXin LI 			l = t;
1946b6cee71dSXin LI 		}
1947b6cee71dSXin LI 		if (*l++ != ')' ||
19485f0216bdSXin LI 		    ((m->in_op & FILE_OPINDIRECT) && *l++ != ')')) {
1949b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
1950b6cee71dSXin LI 				file_magwarn(ms,
1951b6cee71dSXin LI 				    "missing ')' in indirect offset");
19525f0216bdSXin LI 			return -1;
19535f0216bdSXin LI 		}
1954b6cee71dSXin LI 	}
1955b6cee71dSXin LI 	EATAB;
1956b6cee71dSXin LI 
1957b6cee71dSXin LI #ifdef ENABLE_CONDITIONALS
1958b6cee71dSXin LI 	m->cond = get_cond(l, &l);
1959b6cee71dSXin LI 	if (check_cond(ms, m->cond, cont_level) == -1)
1960b6cee71dSXin LI 		return -1;
1961b6cee71dSXin LI 
1962b6cee71dSXin LI 	EATAB;
1963b6cee71dSXin LI #endif
1964b6cee71dSXin LI 
1965b6cee71dSXin LI 	/*
1966b6cee71dSXin LI 	 * Parse the type.
1967b6cee71dSXin LI 	 */
1968b6cee71dSXin LI 	if (*l == 'u') {
1969b6cee71dSXin LI 		/*
1970b6cee71dSXin LI 		 * Try it as a keyword type prefixed by "u"; match what
1971b6cee71dSXin LI 		 * follows the "u".  If that fails, try it as an SUS
1972b6cee71dSXin LI 		 * integer type.
1973b6cee71dSXin LI 		 */
1974b6cee71dSXin LI 		m->type = get_type(type_tbl, l + 1, &l);
1975b6cee71dSXin LI 		if (m->type == FILE_INVALID) {
1976b6cee71dSXin LI 			/*
1977b6cee71dSXin LI 			 * Not a keyword type; parse it as an SUS type,
1978b6cee71dSXin LI 			 * 'u' possibly followed by a number or C/S/L.
1979b6cee71dSXin LI 			 */
1980b6cee71dSXin LI 			m->type = get_standard_integer_type(l, &l);
1981b6cee71dSXin LI 		}
1982b6cee71dSXin LI 		/* It's unsigned. */
1983b6cee71dSXin LI 		if (m->type != FILE_INVALID)
1984b6cee71dSXin LI 			m->flag |= UNSIGNED;
1985b6cee71dSXin LI 	} else {
1986b6cee71dSXin LI 		/*
1987b6cee71dSXin LI 		 * Try it as a keyword type.  If that fails, try it as
1988b6cee71dSXin LI 		 * an SUS integer type if it begins with "d" or as an
1989b6cee71dSXin LI 		 * SUS string type if it begins with "s".  In any case,
1990b6cee71dSXin LI 		 * it's not unsigned.
1991b6cee71dSXin LI 		 */
1992b6cee71dSXin LI 		m->type = get_type(type_tbl, l, &l);
1993b6cee71dSXin LI 		if (m->type == FILE_INVALID) {
1994b6cee71dSXin LI 			/*
1995b6cee71dSXin LI 			 * Not a keyword type; parse it as an SUS type,
1996b6cee71dSXin LI 			 * either 'd' possibly followed by a number or
1997b6cee71dSXin LI 			 * C/S/L, or just 's'.
1998b6cee71dSXin LI 			 */
1999b6cee71dSXin LI 			if (*l == 'd')
2000b6cee71dSXin LI 				m->type = get_standard_integer_type(l, &l);
2001b6cee71dSXin LI 			else if (*l == 's' && !isalpha((unsigned char)l[1])) {
2002b6cee71dSXin LI 				m->type = FILE_STRING;
2003b6cee71dSXin LI 				++l;
2004b6cee71dSXin LI 			}
2005b6cee71dSXin LI 		}
2006b6cee71dSXin LI 	}
2007b6cee71dSXin LI 
2008b6cee71dSXin LI 	if (m->type == FILE_INVALID) {
2009b6cee71dSXin LI 		/* Not found - try it as a special keyword. */
2010b6cee71dSXin LI 		m->type = get_type(special_tbl, l, &l);
2011b6cee71dSXin LI 	}
2012b6cee71dSXin LI 
2013b6cee71dSXin LI 	if (m->type == FILE_INVALID) {
2014b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
2015b6cee71dSXin LI 			file_magwarn(ms, "type `%s' invalid", l);
2016b6cee71dSXin LI 		return -1;
2017b6cee71dSXin LI 	}
2018b6cee71dSXin LI 
2019b6cee71dSXin LI 	/* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
2020b6cee71dSXin LI 	/* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
2021b6cee71dSXin LI 
2022b6cee71dSXin LI 	m->mask_op = 0;
2023b6cee71dSXin LI 	if (*l == '~') {
2024b6cee71dSXin LI 		if (!IS_STRING(m->type))
2025b6cee71dSXin LI 			m->mask_op |= FILE_OPINVERSE;
2026b6cee71dSXin LI 		else if (ms->flags & MAGIC_CHECK)
2027b6cee71dSXin LI 			file_magwarn(ms, "'~' invalid for string types");
2028b6cee71dSXin LI 		++l;
2029b6cee71dSXin LI 	}
2030b6cee71dSXin LI 	m->str_range = 0;
2031b6cee71dSXin LI 	m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
2032b6cee71dSXin LI 	if ((op = get_op(*l)) != -1) {
20334460e5b0SXin LI 		if (IS_STRING(m->type)) {
20344460e5b0SXin LI 			int r;
20354460e5b0SXin LI 
20364460e5b0SXin LI 			if (op != FILE_OPDIVIDE) {
2037b6cee71dSXin LI 				if (ms->flags & MAGIC_CHECK)
2038b6cee71dSXin LI 					file_magwarn(ms,
20394460e5b0SXin LI 					    "invalid string/indirect op: "
20404460e5b0SXin LI 					    "`%c'", *t);
2041b6cee71dSXin LI 				return -1;
2042b6cee71dSXin LI 			}
20434460e5b0SXin LI 
20444460e5b0SXin LI 			if (m->type == FILE_INDIRECT)
20454460e5b0SXin LI 				r = parse_indirect_modifier(ms, m, &l);
20464460e5b0SXin LI 			else
20474460e5b0SXin LI 				r = parse_string_modifier(ms, m, &l);
20484460e5b0SXin LI 			if (r == -1)
2049b6cee71dSXin LI 				return -1;
20504460e5b0SXin LI 		} else
20514460e5b0SXin LI 			parse_op_modifier(ms, m, &l, op);
2052b6cee71dSXin LI 	}
20534460e5b0SXin LI 
2054b6cee71dSXin LI 	/*
2055b6cee71dSXin LI 	 * We used to set mask to all 1's here, instead let's just not do
2056b6cee71dSXin LI 	 * anything if mask = 0 (unless you have a better idea)
2057b6cee71dSXin LI 	 */
2058b6cee71dSXin LI 	EATAB;
2059b6cee71dSXin LI 
2060b6cee71dSXin LI 	switch (*l) {
2061b6cee71dSXin LI 	case '>':
2062b6cee71dSXin LI 	case '<':
2063b6cee71dSXin LI   		m->reln = *l;
2064b6cee71dSXin LI   		++l;
2065b6cee71dSXin LI 		if (*l == '=') {
2066b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK) {
2067b6cee71dSXin LI 				file_magwarn(ms, "%c= not supported",
2068b6cee71dSXin LI 				    m->reln);
2069b6cee71dSXin LI 				return -1;
2070b6cee71dSXin LI 			}
2071b6cee71dSXin LI 		   ++l;
2072b6cee71dSXin LI 		}
2073b6cee71dSXin LI 		break;
2074b6cee71dSXin LI 	/* Old-style anding: "0 byte &0x80 dynamically linked" */
2075b6cee71dSXin LI 	case '&':
2076b6cee71dSXin LI 	case '^':
2077b6cee71dSXin LI 	case '=':
2078b6cee71dSXin LI   		m->reln = *l;
2079b6cee71dSXin LI   		++l;
2080b6cee71dSXin LI 		if (*l == '=') {
2081b6cee71dSXin LI 		   /* HP compat: ignore &= etc. */
2082b6cee71dSXin LI 		   ++l;
2083b6cee71dSXin LI 		}
2084b6cee71dSXin LI 		break;
2085b6cee71dSXin LI 	case '!':
2086b6cee71dSXin LI 		m->reln = *l;
2087b6cee71dSXin LI 		++l;
2088b6cee71dSXin LI 		break;
2089b6cee71dSXin LI 	default:
2090b6cee71dSXin LI   		m->reln = '=';	/* the default relation */
2091b6cee71dSXin LI 		if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
2092b6cee71dSXin LI 		    isspace((unsigned char)l[1])) || !l[1])) {
2093b6cee71dSXin LI 			m->reln = *l;
2094b6cee71dSXin LI 			++l;
2095b6cee71dSXin LI 		}
2096b6cee71dSXin LI 		break;
2097b6cee71dSXin LI 	}
2098b6cee71dSXin LI 	/*
2099b6cee71dSXin LI 	 * Grab the value part, except for an 'x' reln.
2100b6cee71dSXin LI 	 */
2101b6cee71dSXin LI 	if (m->reln != 'x' && getvalue(ms, m, &l, action))
2102b6cee71dSXin LI 		return -1;
2103b6cee71dSXin LI 
2104b6cee71dSXin LI 	/*
2105b6cee71dSXin LI 	 * TODO finish this macro and start using it!
2106b6cee71dSXin LI 	 * #define offsetcheck {if (offset > HOWMANY-1)
2107b6cee71dSXin LI 	 *	magwarn("offset too big"); }
2108b6cee71dSXin LI 	 */
2109b6cee71dSXin LI 
2110b6cee71dSXin LI 	/*
2111b6cee71dSXin LI 	 * Now get last part - the description
2112b6cee71dSXin LI 	 */
2113b6cee71dSXin LI 	EATAB;
2114b6cee71dSXin LI 	if (l[0] == '\b') {
2115b6cee71dSXin LI 		++l;
2116b6cee71dSXin LI 		m->flag |= NOSPACE;
2117b6cee71dSXin LI 	} else if ((l[0] == '\\') && (l[1] == 'b')) {
2118b6cee71dSXin LI 		++l;
2119b6cee71dSXin LI 		++l;
2120b6cee71dSXin LI 		m->flag |= NOSPACE;
2121b6cee71dSXin LI 	}
2122b6cee71dSXin LI 	for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); )
2123b6cee71dSXin LI 		continue;
2124b6cee71dSXin LI 	if (i == sizeof(m->desc)) {
2125b6cee71dSXin LI 		m->desc[sizeof(m->desc) - 1] = '\0';
2126b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
2127b6cee71dSXin LI 			file_magwarn(ms, "description `%s' truncated", m->desc);
2128b6cee71dSXin LI 	}
2129b6cee71dSXin LI 
2130b6cee71dSXin LI         /*
2131b6cee71dSXin LI 	 * We only do this check while compiling, or if any of the magic
2132b6cee71dSXin LI 	 * files were not compiled.
2133b6cee71dSXin LI          */
2134b6cee71dSXin LI         if (ms->flags & MAGIC_CHECK) {
2135b6cee71dSXin LI 		if (check_format(ms, m) == -1)
2136b6cee71dSXin LI 			return -1;
2137b6cee71dSXin LI 	}
2138b6cee71dSXin LI #ifndef COMPILE_ONLY
2139b6cee71dSXin LI 	if (action == FILE_CHECK) {
2140b6cee71dSXin LI 		file_mdump(m);
2141b6cee71dSXin LI 	}
2142b6cee71dSXin LI #endif
2143b6cee71dSXin LI 	m->mimetype[0] = '\0';		/* initialise MIME type to none */
2144b6cee71dSXin LI 	return 0;
2145b6cee71dSXin LI }
2146b6cee71dSXin LI 
2147b6cee71dSXin LI /*
2148b6cee71dSXin LI  * parse a STRENGTH annotation line from magic file, put into magic[index - 1]
2149b6cee71dSXin LI  * if valid
2150b6cee71dSXin LI  */
2151b6cee71dSXin LI private int
2152b6cee71dSXin LI parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line)
2153b6cee71dSXin LI {
2154b6cee71dSXin LI 	const char *l = line;
2155b6cee71dSXin LI 	char *el;
2156b6cee71dSXin LI 	unsigned long factor;
2157b6cee71dSXin LI 	struct magic *m = &me->mp[0];
2158b6cee71dSXin LI 
2159b6cee71dSXin LI 	if (m->factor_op != FILE_FACTOR_OP_NONE) {
2160b6cee71dSXin LI 		file_magwarn(ms,
2161b6cee71dSXin LI 		    "Current entry already has a strength type: %c %d",
2162b6cee71dSXin LI 		    m->factor_op, m->factor);
2163b6cee71dSXin LI 		return -1;
2164b6cee71dSXin LI 	}
2165b6cee71dSXin LI 	if (m->type == FILE_NAME) {
2166b6cee71dSXin LI 		file_magwarn(ms, "%s: Strength setting is not supported in "
2167b6cee71dSXin LI 		    "\"name\" magic entries", m->value.s);
2168b6cee71dSXin LI 		return -1;
2169b6cee71dSXin LI 	}
2170b6cee71dSXin LI 	EATAB;
2171b6cee71dSXin LI 	switch (*l) {
2172b6cee71dSXin LI 	case FILE_FACTOR_OP_NONE:
2173b6cee71dSXin LI 	case FILE_FACTOR_OP_PLUS:
2174b6cee71dSXin LI 	case FILE_FACTOR_OP_MINUS:
2175b6cee71dSXin LI 	case FILE_FACTOR_OP_TIMES:
2176b6cee71dSXin LI 	case FILE_FACTOR_OP_DIV:
2177b6cee71dSXin LI 		m->factor_op = *l++;
2178b6cee71dSXin LI 		break;
2179b6cee71dSXin LI 	default:
2180b6cee71dSXin LI 		file_magwarn(ms, "Unknown factor op `%c'", *l);
2181b6cee71dSXin LI 		return -1;
2182b6cee71dSXin LI 	}
2183b6cee71dSXin LI 	EATAB;
2184b6cee71dSXin LI 	factor = strtoul(l, &el, 0);
2185b6cee71dSXin LI 	if (factor > 255) {
2186b6cee71dSXin LI 		file_magwarn(ms, "Too large factor `%lu'", factor);
2187b6cee71dSXin LI 		goto out;
2188b6cee71dSXin LI 	}
2189b6cee71dSXin LI 	if (*el && !isspace((unsigned char)*el)) {
2190b6cee71dSXin LI 		file_magwarn(ms, "Bad factor `%s'", l);
2191b6cee71dSXin LI 		goto out;
2192b6cee71dSXin LI 	}
2193b6cee71dSXin LI 	m->factor = (uint8_t)factor;
2194b6cee71dSXin LI 	if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) {
2195b6cee71dSXin LI 		file_magwarn(ms, "Cannot have factor op `%c' and factor %u",
2196b6cee71dSXin LI 		    m->factor_op, m->factor);
2197b6cee71dSXin LI 		goto out;
2198b6cee71dSXin LI 	}
2199b6cee71dSXin LI 	return 0;
2200b6cee71dSXin LI out:
2201b6cee71dSXin LI 	m->factor_op = FILE_FACTOR_OP_NONE;
2202b6cee71dSXin LI 	m->factor = 0;
2203b6cee71dSXin LI 	return -1;
2204b6cee71dSXin LI }
2205b6cee71dSXin LI 
2206b6cee71dSXin LI private int
2207c2931133SXin LI goodchar(unsigned char x, const char *extra)
2208c2931133SXin LI {
2209c2931133SXin LI 	return (isascii(x) && isalnum(x)) || strchr(extra, x);
2210c2931133SXin LI }
2211c2931133SXin LI 
2212c2931133SXin LI private int
2213b6cee71dSXin LI parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line,
2214c2931133SXin LI     off_t off, size_t len, const char *name, const char *extra, int nt)
2215b6cee71dSXin LI {
2216b6cee71dSXin LI 	size_t i;
2217b6cee71dSXin LI 	const char *l = line;
2218b6cee71dSXin LI 	struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
22195f0216bdSXin LI 	char *buf = CAST(char *, CAST(void *, m)) + off;
2220b6cee71dSXin LI 
2221b6cee71dSXin LI 	if (buf[0] != '\0') {
2222b6cee71dSXin LI 		len = nt ? strlen(buf) : len;
2223b6cee71dSXin LI 		file_magwarn(ms, "Current entry already has a %s type "
2224b6cee71dSXin LI 		    "`%.*s', new type `%s'", name, (int)len, buf, l);
2225b6cee71dSXin LI 		return -1;
2226b6cee71dSXin LI 	}
2227b6cee71dSXin LI 
2228b6cee71dSXin LI 	if (*m->desc == '\0') {
2229b6cee71dSXin LI 		file_magwarn(ms, "Current entry does not yet have a "
2230b6cee71dSXin LI 		    "description for adding a %s type", name);
2231b6cee71dSXin LI 		return -1;
2232b6cee71dSXin LI 	}
2233b6cee71dSXin LI 
2234b6cee71dSXin LI 	EATAB;
2235c2931133SXin LI 	for (i = 0; *l && i < len && goodchar(*l, extra); buf[i++] = *l++)
2236b6cee71dSXin LI 		continue;
2237b6cee71dSXin LI 
2238b6cee71dSXin LI 	if (i == len && *l) {
2239b6cee71dSXin LI 		if (nt)
2240b6cee71dSXin LI 			buf[len - 1] = '\0';
2241b6cee71dSXin LI 		if (ms->flags & MAGIC_CHECK)
2242b6cee71dSXin LI 			file_magwarn(ms, "%s type `%s' truncated %"
2243b6cee71dSXin LI 			    SIZE_T_FORMAT "u", name, line, i);
2244b6cee71dSXin LI 	} else {
2245c2931133SXin LI 		if (!isspace((unsigned char)*l) && !goodchar(*l, extra))
2246c2931133SXin LI 			file_magwarn(ms, "%s type `%s' has bad char '%c'",
2247c2931133SXin LI 			    name, line, *l);
2248b6cee71dSXin LI 		if (nt)
2249b6cee71dSXin LI 			buf[i] = '\0';
2250b6cee71dSXin LI 	}
2251b6cee71dSXin LI 
2252b6cee71dSXin LI 	if (i > 0)
2253b6cee71dSXin LI 		return 0;
2254c2931133SXin LI 
2255c2931133SXin LI 	file_magerror(ms, "Bad magic entry '%s'", line);
2256b6cee71dSXin LI 	return -1;
2257b6cee71dSXin LI }
2258b6cee71dSXin LI 
2259b6cee71dSXin LI /*
2260b6cee71dSXin LI  * Parse an Apple CREATOR/TYPE annotation from magic file and put it into
2261b6cee71dSXin LI  * magic[index - 1]
2262b6cee71dSXin LI  */
2263b6cee71dSXin LI private int
2264b6cee71dSXin LI parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line)
2265b6cee71dSXin LI {
2266b6cee71dSXin LI 	struct magic *m = &me->mp[0];
2267b6cee71dSXin LI 
22685f0216bdSXin LI 	return parse_extra(ms, me, line,
22695f0216bdSXin LI 	    CAST(off_t, offsetof(struct magic, apple)),
2270c2931133SXin LI 	    sizeof(m->apple), "APPLE", "!+-./", 0);
2271b6cee71dSXin LI }
2272b6cee71dSXin LI 
2273b6cee71dSXin LI /*
22745f0216bdSXin LI  * Parse a comma-separated list of extensions
22755f0216bdSXin LI  */
22765f0216bdSXin LI private int
22775f0216bdSXin LI parse_ext(struct magic_set *ms, struct magic_entry *me, const char *line)
22785f0216bdSXin LI {
22795f0216bdSXin LI 	struct magic *m = &me->mp[0];
22805f0216bdSXin LI 
22815f0216bdSXin LI 	return parse_extra(ms, me, line,
22825f0216bdSXin LI 	    CAST(off_t, offsetof(struct magic, ext)),
22835f0216bdSXin LI 	    sizeof(m->ext), "EXTENSION", ",!+-/", 0);
22845f0216bdSXin LI }
22855f0216bdSXin LI 
22865f0216bdSXin LI /*
2287b6cee71dSXin LI  * parse a MIME annotation line from magic file, put into magic[index - 1]
2288b6cee71dSXin LI  * if valid
2289b6cee71dSXin LI  */
2290b6cee71dSXin LI private int
2291b6cee71dSXin LI parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line)
2292b6cee71dSXin LI {
2293b6cee71dSXin LI 	struct magic *m = &me->mp[0];
2294b6cee71dSXin LI 
22955f0216bdSXin LI 	return parse_extra(ms, me, line,
22965f0216bdSXin LI 	    CAST(off_t, offsetof(struct magic, mimetype)),
2297c2931133SXin LI 	    sizeof(m->mimetype), "MIME", "+-/.", 1);
2298b6cee71dSXin LI }
2299b6cee71dSXin LI 
2300b6cee71dSXin LI private int
2301b6cee71dSXin LI check_format_type(const char *ptr, int type)
2302b6cee71dSXin LI {
2303b6cee71dSXin LI 	int quad = 0, h;
2304b6cee71dSXin LI 	if (*ptr == '\0') {
2305b6cee71dSXin LI 		/* Missing format string; bad */
2306b6cee71dSXin LI 		return -1;
2307b6cee71dSXin LI 	}
2308b6cee71dSXin LI 
2309b6cee71dSXin LI 	switch (file_formats[type]) {
2310b6cee71dSXin LI 	case FILE_FMT_QUAD:
2311b6cee71dSXin LI 		quad = 1;
2312b6cee71dSXin LI 		/*FALLTHROUGH*/
2313b6cee71dSXin LI 	case FILE_FMT_NUM:
2314b6cee71dSXin LI 		if (quad == 0) {
2315b6cee71dSXin LI 			switch (type) {
2316b6cee71dSXin LI 			case FILE_BYTE:
2317b6cee71dSXin LI 				h = 2;
2318b6cee71dSXin LI 				break;
2319b6cee71dSXin LI 			case FILE_SHORT:
2320b6cee71dSXin LI 			case FILE_BESHORT:
2321b6cee71dSXin LI 			case FILE_LESHORT:
2322b6cee71dSXin LI 				h = 1;
2323b6cee71dSXin LI 				break;
2324b6cee71dSXin LI 			case FILE_LONG:
2325b6cee71dSXin LI 			case FILE_BELONG:
2326b6cee71dSXin LI 			case FILE_LELONG:
2327b6cee71dSXin LI 			case FILE_MELONG:
2328b6cee71dSXin LI 			case FILE_LEID3:
2329b6cee71dSXin LI 			case FILE_BEID3:
2330b6cee71dSXin LI 			case FILE_INDIRECT:
2331b6cee71dSXin LI 				h = 0;
2332b6cee71dSXin LI 				break;
2333b6cee71dSXin LI 			default:
2334b6cee71dSXin LI 				abort();
2335b6cee71dSXin LI 			}
2336b6cee71dSXin LI 		} else
2337b6cee71dSXin LI 			h = 0;
2338b6cee71dSXin LI 		if (*ptr == '-')
2339b6cee71dSXin LI 			ptr++;
2340b6cee71dSXin LI 		if (*ptr == '.')
2341b6cee71dSXin LI 			ptr++;
2342b6cee71dSXin LI 		while (isdigit((unsigned char)*ptr)) ptr++;
2343b6cee71dSXin LI 		if (*ptr == '.')
2344b6cee71dSXin LI 			ptr++;
2345b6cee71dSXin LI 		while (isdigit((unsigned char)*ptr)) ptr++;
2346b6cee71dSXin LI 		if (quad) {
2347b6cee71dSXin LI 			if (*ptr++ != 'l')
2348b6cee71dSXin LI 				return -1;
2349b6cee71dSXin LI 			if (*ptr++ != 'l')
2350b6cee71dSXin LI 				return -1;
2351b6cee71dSXin LI 		}
2352b6cee71dSXin LI 
2353b6cee71dSXin LI 		switch (*ptr++) {
2354b6cee71dSXin LI #ifdef STRICT_FORMAT 	/* "long" formats are int formats for us */
2355b6cee71dSXin LI 		/* so don't accept the 'l' modifier */
2356b6cee71dSXin LI 		case 'l':
2357b6cee71dSXin LI 			switch (*ptr++) {
2358b6cee71dSXin LI 			case 'i':
2359b6cee71dSXin LI 			case 'd':
2360b6cee71dSXin LI 			case 'u':
2361b6cee71dSXin LI 			case 'o':
2362b6cee71dSXin LI 			case 'x':
2363b6cee71dSXin LI 			case 'X':
2364b6cee71dSXin LI 				return h != 0 ? -1 : 0;
2365b6cee71dSXin LI 			default:
2366b6cee71dSXin LI 				return -1;
2367b6cee71dSXin LI 			}
2368b6cee71dSXin LI 
2369b6cee71dSXin LI 		/*
2370b6cee71dSXin LI 		 * Don't accept h and hh modifiers. They make writing
2371b6cee71dSXin LI 		 * magic entries more complicated, for very little benefit
2372b6cee71dSXin LI 		 */
2373b6cee71dSXin LI 		case 'h':
2374b6cee71dSXin LI 			if (h-- <= 0)
2375b6cee71dSXin LI 				return -1;
2376b6cee71dSXin LI 			switch (*ptr++) {
2377b6cee71dSXin LI 			case 'h':
2378b6cee71dSXin LI 				if (h-- <= 0)
2379b6cee71dSXin LI 					return -1;
2380b6cee71dSXin LI 				switch (*ptr++) {
2381b6cee71dSXin LI 				case 'i':
2382b6cee71dSXin LI 				case 'd':
2383b6cee71dSXin LI 				case 'u':
2384b6cee71dSXin LI 				case 'o':
2385b6cee71dSXin LI 				case 'x':
2386b6cee71dSXin LI 				case 'X':
2387b6cee71dSXin LI 					return 0;
2388b6cee71dSXin LI 				default:
2389b6cee71dSXin LI 					return -1;
2390b6cee71dSXin LI 				}
2391b6cee71dSXin LI 			case 'i':
2392b6cee71dSXin LI 			case 'd':
2393b6cee71dSXin LI 			case 'u':
2394b6cee71dSXin LI 			case 'o':
2395b6cee71dSXin LI 			case 'x':
2396b6cee71dSXin LI 			case 'X':
2397b6cee71dSXin LI 				return h != 0 ? -1 : 0;
2398b6cee71dSXin LI 			default:
2399b6cee71dSXin LI 				return -1;
2400b6cee71dSXin LI 			}
2401b6cee71dSXin LI #endif
2402b6cee71dSXin LI 		case 'c':
2403b6cee71dSXin LI 			return h != 2 ? -1 : 0;
2404b6cee71dSXin LI 		case 'i':
2405b6cee71dSXin LI 		case 'd':
2406b6cee71dSXin LI 		case 'u':
2407b6cee71dSXin LI 		case 'o':
2408b6cee71dSXin LI 		case 'x':
2409b6cee71dSXin LI 		case 'X':
2410b6cee71dSXin LI #ifdef STRICT_FORMAT
2411b6cee71dSXin LI 			return h != 0 ? -1 : 0;
2412b6cee71dSXin LI #else
2413b6cee71dSXin LI 			return 0;
2414b6cee71dSXin LI #endif
2415b6cee71dSXin LI 		default:
2416b6cee71dSXin LI 			return -1;
2417b6cee71dSXin LI 		}
2418b6cee71dSXin LI 
2419b6cee71dSXin LI 	case FILE_FMT_FLOAT:
2420b6cee71dSXin LI 	case FILE_FMT_DOUBLE:
2421b6cee71dSXin LI 		if (*ptr == '-')
2422b6cee71dSXin LI 			ptr++;
2423b6cee71dSXin LI 		if (*ptr == '.')
2424b6cee71dSXin LI 			ptr++;
2425b6cee71dSXin LI 		while (isdigit((unsigned char)*ptr)) ptr++;
2426b6cee71dSXin LI 		if (*ptr == '.')
2427b6cee71dSXin LI 			ptr++;
2428b6cee71dSXin LI 		while (isdigit((unsigned char)*ptr)) ptr++;
2429b6cee71dSXin LI 
2430b6cee71dSXin LI 		switch (*ptr++) {
2431b6cee71dSXin LI 		case 'e':
2432b6cee71dSXin LI 		case 'E':
2433b6cee71dSXin LI 		case 'f':
2434b6cee71dSXin LI 		case 'F':
2435b6cee71dSXin LI 		case 'g':
2436b6cee71dSXin LI 		case 'G':
2437b6cee71dSXin LI 			return 0;
2438b6cee71dSXin LI 
2439b6cee71dSXin LI 		default:
2440b6cee71dSXin LI 			return -1;
2441b6cee71dSXin LI 		}
2442b6cee71dSXin LI 
2443b6cee71dSXin LI 
2444b6cee71dSXin LI 	case FILE_FMT_STR:
2445b6cee71dSXin LI 		if (*ptr == '-')
2446b6cee71dSXin LI 			ptr++;
2447b6cee71dSXin LI 		while (isdigit((unsigned char )*ptr))
2448b6cee71dSXin LI 			ptr++;
2449b6cee71dSXin LI 		if (*ptr == '.') {
2450b6cee71dSXin LI 			ptr++;
2451b6cee71dSXin LI 			while (isdigit((unsigned char )*ptr))
2452b6cee71dSXin LI 				ptr++;
2453b6cee71dSXin LI 		}
2454b6cee71dSXin LI 
2455b6cee71dSXin LI 		switch (*ptr++) {
2456b6cee71dSXin LI 		case 's':
2457b6cee71dSXin LI 			return 0;
2458b6cee71dSXin LI 		default:
2459b6cee71dSXin LI 			return -1;
2460b6cee71dSXin LI 		}
2461b6cee71dSXin LI 
2462b6cee71dSXin LI 	default:
2463b6cee71dSXin LI 		/* internal error */
2464b6cee71dSXin LI 		abort();
2465b6cee71dSXin LI 	}
2466b6cee71dSXin LI 	/*NOTREACHED*/
2467b6cee71dSXin LI 	return -1;
2468b6cee71dSXin LI }
2469b6cee71dSXin LI 
2470b6cee71dSXin LI /*
2471b6cee71dSXin LI  * Check that the optional printf format in description matches
2472b6cee71dSXin LI  * the type of the magic.
2473b6cee71dSXin LI  */
2474b6cee71dSXin LI private int
2475b6cee71dSXin LI check_format(struct magic_set *ms, struct magic *m)
2476b6cee71dSXin LI {
2477b6cee71dSXin LI 	char *ptr;
2478b6cee71dSXin LI 
2479b6cee71dSXin LI 	for (ptr = m->desc; *ptr; ptr++)
2480b6cee71dSXin LI 		if (*ptr == '%')
2481b6cee71dSXin LI 			break;
2482b6cee71dSXin LI 	if (*ptr == '\0') {
2483b6cee71dSXin LI 		/* No format string; ok */
2484b6cee71dSXin LI 		return 1;
2485b6cee71dSXin LI 	}
2486b6cee71dSXin LI 
2487b6cee71dSXin LI 	assert(file_nformats == file_nnames);
2488b6cee71dSXin LI 
2489b6cee71dSXin LI 	if (m->type >= file_nformats) {
2490b6cee71dSXin LI 		file_magwarn(ms, "Internal error inconsistency between "
2491b6cee71dSXin LI 		    "m->type and format strings");
2492b6cee71dSXin LI 		return -1;
2493b6cee71dSXin LI 	}
2494b6cee71dSXin LI 	if (file_formats[m->type] == FILE_FMT_NONE) {
2495b6cee71dSXin LI 		file_magwarn(ms, "No format string for `%s' with description "
2496b6cee71dSXin LI 		    "`%s'", m->desc, file_names[m->type]);
2497b6cee71dSXin LI 		return -1;
2498b6cee71dSXin LI 	}
2499b6cee71dSXin LI 
2500b6cee71dSXin LI 	ptr++;
2501b6cee71dSXin LI 	if (check_format_type(ptr, m->type) == -1) {
2502b6cee71dSXin LI 		/*
2503b6cee71dSXin LI 		 * TODO: this error message is unhelpful if the format
2504b6cee71dSXin LI 		 * string is not one character long
2505b6cee71dSXin LI 		 */
2506b6cee71dSXin LI 		file_magwarn(ms, "Printf format `%c' is not valid for type "
2507b6cee71dSXin LI 		    "`%s' in description `%s'", *ptr ? *ptr : '?',
2508b6cee71dSXin LI 		    file_names[m->type], m->desc);
2509b6cee71dSXin LI 		return -1;
2510b6cee71dSXin LI 	}
2511b6cee71dSXin LI 
2512b6cee71dSXin LI 	for (; *ptr; ptr++) {
2513b6cee71dSXin LI 		if (*ptr == '%') {
2514b6cee71dSXin LI 			file_magwarn(ms,
2515b6cee71dSXin LI 			    "Too many format strings (should have at most one) "
2516b6cee71dSXin LI 			    "for `%s' with description `%s'",
2517b6cee71dSXin LI 			    file_names[m->type], m->desc);
2518b6cee71dSXin LI 			return -1;
2519b6cee71dSXin LI 		}
2520b6cee71dSXin LI 	}
2521b6cee71dSXin LI 	return 0;
2522b6cee71dSXin LI }
2523b6cee71dSXin LI 
2524b6cee71dSXin LI /*
2525b6cee71dSXin LI  * Read a numeric value from a pointer, into the value union of a magic
2526b6cee71dSXin LI  * pointer, according to the magic type.  Update the string pointer to point
2527b6cee71dSXin LI  * just after the number read.  Return 0 for success, non-zero for failure.
2528b6cee71dSXin LI  */
2529b6cee71dSXin LI private int
2530b6cee71dSXin LI getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
2531b6cee71dSXin LI {
2532b6cee71dSXin LI 	switch (m->type) {
2533b6cee71dSXin LI 	case FILE_BESTRING16:
2534b6cee71dSXin LI 	case FILE_LESTRING16:
2535b6cee71dSXin LI 	case FILE_STRING:
2536b6cee71dSXin LI 	case FILE_PSTRING:
2537b6cee71dSXin LI 	case FILE_REGEX:
2538b6cee71dSXin LI 	case FILE_SEARCH:
2539b6cee71dSXin LI 	case FILE_NAME:
2540b6cee71dSXin LI 	case FILE_USE:
2541b6cee71dSXin LI 		*p = getstr(ms, m, *p, action == FILE_COMPILE);
2542b6cee71dSXin LI 		if (*p == NULL) {
2543b6cee71dSXin LI 			if (ms->flags & MAGIC_CHECK)
2544b6cee71dSXin LI 				file_magwarn(ms, "cannot get string from `%s'",
2545b6cee71dSXin LI 				    m->value.s);
2546b6cee71dSXin LI 			return -1;
2547b6cee71dSXin LI 		}
2548b6cee71dSXin LI 		if (m->type == FILE_REGEX) {
2549b6cee71dSXin LI 			file_regex_t rx;
2550b6cee71dSXin LI 			int rc = file_regcomp(&rx, m->value.s, REG_EXTENDED);
2551b6cee71dSXin LI 			if (rc) {
2552b6cee71dSXin LI 				if (ms->flags & MAGIC_CHECK)
2553b6cee71dSXin LI 					file_regerror(&rx, rc, ms);
2554b6cee71dSXin LI 			}
2555b6cee71dSXin LI 			file_regfree(&rx);
2556b6cee71dSXin LI 			return rc ? -1 : 0;
2557b6cee71dSXin LI 		}
2558b6cee71dSXin LI 		return 0;
2559b6cee71dSXin LI 	case FILE_FLOAT:
2560b6cee71dSXin LI 	case FILE_BEFLOAT:
2561b6cee71dSXin LI 	case FILE_LEFLOAT:
2562b6cee71dSXin LI 		if (m->reln != 'x') {
2563b6cee71dSXin LI 			char *ep;
2564*9ce06829SXin LI 			errno = 0;
2565b6cee71dSXin LI #ifdef HAVE_STRTOF
2566b6cee71dSXin LI 			m->value.f = strtof(*p, &ep);
2567b6cee71dSXin LI #else
2568b6cee71dSXin LI 			m->value.f = (float)strtod(*p, &ep);
2569b6cee71dSXin LI #endif
2570*9ce06829SXin LI 			if (errno == 0)
2571b6cee71dSXin LI 				*p = ep;
2572b6cee71dSXin LI 		}
2573b6cee71dSXin LI 		return 0;
2574b6cee71dSXin LI 	case FILE_DOUBLE:
2575b6cee71dSXin LI 	case FILE_BEDOUBLE:
2576b6cee71dSXin LI 	case FILE_LEDOUBLE:
2577b6cee71dSXin LI 		if (m->reln != 'x') {
2578b6cee71dSXin LI 			char *ep;
2579*9ce06829SXin LI 			errno = 0;
2580b6cee71dSXin LI 			m->value.d = strtod(*p, &ep);
2581*9ce06829SXin LI 			if (errno == 0)
2582b6cee71dSXin LI 				*p = ep;
2583b6cee71dSXin LI 		}
2584b6cee71dSXin LI 		return 0;
2585b6cee71dSXin LI 	default:
2586b6cee71dSXin LI 		if (m->reln != 'x') {
2587b6cee71dSXin LI 			char *ep;
2588*9ce06829SXin LI 			errno = 0;
2589b6cee71dSXin LI 			m->value.q = file_signextend(ms, m,
2590b6cee71dSXin LI 			    (uint64_t)strtoull(*p, &ep, 0));
2591*9ce06829SXin LI 			if (errno == 0) {
2592b6cee71dSXin LI 				*p = ep;
2593b6cee71dSXin LI 				eatsize(p);
2594b6cee71dSXin LI 			}
2595*9ce06829SXin LI 		}
2596b6cee71dSXin LI 		return 0;
2597b6cee71dSXin LI 	}
2598b6cee71dSXin LI }
2599b6cee71dSXin LI 
2600b6cee71dSXin LI /*
2601b6cee71dSXin LI  * Convert a string containing C character escapes.  Stop at an unescaped
2602b6cee71dSXin LI  * space or tab.
2603b6cee71dSXin LI  * Copy the converted version to "m->value.s", and the length in m->vallen.
2604b6cee71dSXin LI  * Return updated scan pointer as function result. Warn if set.
2605b6cee71dSXin LI  */
2606b6cee71dSXin LI private const char *
2607b6cee71dSXin LI getstr(struct magic_set *ms, struct magic *m, const char *s, int warn)
2608b6cee71dSXin LI {
2609b6cee71dSXin LI 	const char *origs = s;
2610b6cee71dSXin LI 	char	*p = m->value.s;
2611b6cee71dSXin LI 	size_t  plen = sizeof(m->value.s);
2612b6cee71dSXin LI 	char 	*origp = p;
2613b6cee71dSXin LI 	char	*pmax = p + plen - 1;
2614b6cee71dSXin LI 	int	c;
2615b6cee71dSXin LI 	int	val;
2616b6cee71dSXin LI 
2617b6cee71dSXin LI 	while ((c = *s++) != '\0') {
2618b6cee71dSXin LI 		if (isspace((unsigned char) c))
2619b6cee71dSXin LI 			break;
2620b6cee71dSXin LI 		if (p >= pmax) {
2621b6cee71dSXin LI 			file_error(ms, 0, "string too long: `%s'", origs);
2622b6cee71dSXin LI 			return NULL;
2623b6cee71dSXin LI 		}
2624b6cee71dSXin LI 		if (c == '\\') {
2625b6cee71dSXin LI 			switch(c = *s++) {
2626b6cee71dSXin LI 
2627b6cee71dSXin LI 			case '\0':
2628b6cee71dSXin LI 				if (warn)
2629b6cee71dSXin LI 					file_magwarn(ms, "incomplete escape");
2630*9ce06829SXin LI 				s--;
2631b6cee71dSXin LI 				goto out;
2632b6cee71dSXin LI 
2633b6cee71dSXin LI 			case '\t':
2634b6cee71dSXin LI 				if (warn) {
2635b6cee71dSXin LI 					file_magwarn(ms,
2636b6cee71dSXin LI 					    "escaped tab found, use \\t instead");
2637b6cee71dSXin LI 					warn = 0;	/* already did */
2638b6cee71dSXin LI 				}
2639b6cee71dSXin LI 				/*FALLTHROUGH*/
2640b6cee71dSXin LI 			default:
2641b6cee71dSXin LI 				if (warn) {
2642b6cee71dSXin LI 					if (isprint((unsigned char)c)) {
2643b6cee71dSXin LI 						/* Allow escaping of
2644b6cee71dSXin LI 						 * ``relations'' */
2645b6cee71dSXin LI 						if (strchr("<>&^=!", c) == NULL
2646b6cee71dSXin LI 						    && (m->type != FILE_REGEX ||
2647b6cee71dSXin LI 						    strchr("[]().*?^$|{}", c)
2648b6cee71dSXin LI 						    == NULL)) {
2649b6cee71dSXin LI 							file_magwarn(ms, "no "
2650b6cee71dSXin LI 							    "need to escape "
2651b6cee71dSXin LI 							    "`%c'", c);
2652b6cee71dSXin LI 						}
2653b6cee71dSXin LI 					} else {
2654b6cee71dSXin LI 						file_magwarn(ms,
2655b6cee71dSXin LI 						    "unknown escape sequence: "
2656b6cee71dSXin LI 						    "\\%03o", c);
2657b6cee71dSXin LI 					}
2658b6cee71dSXin LI 				}
2659b6cee71dSXin LI 				/*FALLTHROUGH*/
2660b6cee71dSXin LI 			/* space, perhaps force people to use \040? */
2661b6cee71dSXin LI 			case ' ':
2662b6cee71dSXin LI #if 0
2663b6cee71dSXin LI 			/*
2664b6cee71dSXin LI 			 * Other things people escape, but shouldn't need to,
2665b6cee71dSXin LI 			 * so we disallow them
2666b6cee71dSXin LI 			 */
2667b6cee71dSXin LI 			case '\'':
2668b6cee71dSXin LI 			case '"':
2669b6cee71dSXin LI 			case '?':
2670b6cee71dSXin LI #endif
2671b6cee71dSXin LI 			/* Relations */
2672b6cee71dSXin LI 			case '>':
2673b6cee71dSXin LI 			case '<':
2674b6cee71dSXin LI 			case '&':
2675b6cee71dSXin LI 			case '^':
2676b6cee71dSXin LI 			case '=':
2677b6cee71dSXin LI 			case '!':
2678b6cee71dSXin LI 			/* and baskslash itself */
2679b6cee71dSXin LI 			case '\\':
2680b6cee71dSXin LI 				*p++ = (char) c;
2681b6cee71dSXin LI 				break;
2682b6cee71dSXin LI 
2683b6cee71dSXin LI 			case 'a':
2684b6cee71dSXin LI 				*p++ = '\a';
2685b6cee71dSXin LI 				break;
2686b6cee71dSXin LI 
2687b6cee71dSXin LI 			case 'b':
2688b6cee71dSXin LI 				*p++ = '\b';
2689b6cee71dSXin LI 				break;
2690b6cee71dSXin LI 
2691b6cee71dSXin LI 			case 'f':
2692b6cee71dSXin LI 				*p++ = '\f';
2693b6cee71dSXin LI 				break;
2694b6cee71dSXin LI 
2695b6cee71dSXin LI 			case 'n':
2696b6cee71dSXin LI 				*p++ = '\n';
2697b6cee71dSXin LI 				break;
2698b6cee71dSXin LI 
2699b6cee71dSXin LI 			case 'r':
2700b6cee71dSXin LI 				*p++ = '\r';
2701b6cee71dSXin LI 				break;
2702b6cee71dSXin LI 
2703b6cee71dSXin LI 			case 't':
2704b6cee71dSXin LI 				*p++ = '\t';
2705b6cee71dSXin LI 				break;
2706b6cee71dSXin LI 
2707b6cee71dSXin LI 			case 'v':
2708b6cee71dSXin LI 				*p++ = '\v';
2709b6cee71dSXin LI 				break;
2710b6cee71dSXin LI 
2711b6cee71dSXin LI 			/* \ and up to 3 octal digits */
2712b6cee71dSXin LI 			case '0':
2713b6cee71dSXin LI 			case '1':
2714b6cee71dSXin LI 			case '2':
2715b6cee71dSXin LI 			case '3':
2716b6cee71dSXin LI 			case '4':
2717b6cee71dSXin LI 			case '5':
2718b6cee71dSXin LI 			case '6':
2719b6cee71dSXin LI 			case '7':
2720b6cee71dSXin LI 				val = c - '0';
2721b6cee71dSXin LI 				c = *s++;  /* try for 2 */
2722b6cee71dSXin LI 				if (c >= '0' && c <= '7') {
2723b6cee71dSXin LI 					val = (val << 3) | (c - '0');
2724b6cee71dSXin LI 					c = *s++;  /* try for 3 */
2725b6cee71dSXin LI 					if (c >= '0' && c <= '7')
2726b6cee71dSXin LI 						val = (val << 3) | (c-'0');
2727b6cee71dSXin LI 					else
2728b6cee71dSXin LI 						--s;
2729b6cee71dSXin LI 				}
2730b6cee71dSXin LI 				else
2731b6cee71dSXin LI 					--s;
2732b6cee71dSXin LI 				*p++ = (char)val;
2733b6cee71dSXin LI 				break;
2734b6cee71dSXin LI 
2735b6cee71dSXin LI 			/* \x and up to 2 hex digits */
2736b6cee71dSXin LI 			case 'x':
2737b6cee71dSXin LI 				val = 'x';	/* Default if no digits */
2738b6cee71dSXin LI 				c = hextoint(*s++);	/* Get next char */
2739b6cee71dSXin LI 				if (c >= 0) {
2740b6cee71dSXin LI 					val = c;
2741b6cee71dSXin LI 					c = hextoint(*s++);
2742b6cee71dSXin LI 					if (c >= 0)
2743b6cee71dSXin LI 						val = (val << 4) + c;
2744b6cee71dSXin LI 					else
2745b6cee71dSXin LI 						--s;
2746b6cee71dSXin LI 				} else
2747b6cee71dSXin LI 					--s;
2748b6cee71dSXin LI 				*p++ = (char)val;
2749b6cee71dSXin LI 				break;
2750b6cee71dSXin LI 			}
2751b6cee71dSXin LI 		} else
2752b6cee71dSXin LI 			*p++ = (char)c;
2753b6cee71dSXin LI 	}
2754*9ce06829SXin LI 	--s;
2755b6cee71dSXin LI out:
2756b6cee71dSXin LI 	*p = '\0';
2757b6cee71dSXin LI 	m->vallen = CAST(unsigned char, (p - origp));
2758b6cee71dSXin LI 	if (m->type == FILE_PSTRING)
2759b6cee71dSXin LI 		m->vallen += (unsigned char)file_pstring_length_size(m);
2760b6cee71dSXin LI 	return s;
2761b6cee71dSXin LI }
2762b6cee71dSXin LI 
2763b6cee71dSXin LI 
2764b6cee71dSXin LI /* Single hex char to int; -1 if not a hex char. */
2765b6cee71dSXin LI private int
2766b6cee71dSXin LI hextoint(int c)
2767b6cee71dSXin LI {
2768b6cee71dSXin LI 	if (!isascii((unsigned char) c))
2769b6cee71dSXin LI 		return -1;
2770b6cee71dSXin LI 	if (isdigit((unsigned char) c))
2771b6cee71dSXin LI 		return c - '0';
2772b6cee71dSXin LI 	if ((c >= 'a') && (c <= 'f'))
2773b6cee71dSXin LI 		return c + 10 - 'a';
2774b6cee71dSXin LI 	if (( c>= 'A') && (c <= 'F'))
2775b6cee71dSXin LI 		return c + 10 - 'A';
2776b6cee71dSXin LI 	return -1;
2777b6cee71dSXin LI }
2778b6cee71dSXin LI 
2779b6cee71dSXin LI 
2780b6cee71dSXin LI /*
2781b6cee71dSXin LI  * Print a string containing C character escapes.
2782b6cee71dSXin LI  */
2783b6cee71dSXin LI protected void
2784b6cee71dSXin LI file_showstr(FILE *fp, const char *s, size_t len)
2785b6cee71dSXin LI {
2786b6cee71dSXin LI 	char	c;
2787b6cee71dSXin LI 
2788b6cee71dSXin LI 	for (;;) {
2789b6cee71dSXin LI 		if (len == ~0U) {
2790b6cee71dSXin LI 			c = *s++;
2791b6cee71dSXin LI 			if (c == '\0')
2792b6cee71dSXin LI 				break;
2793b6cee71dSXin LI 		}
2794b6cee71dSXin LI 		else  {
2795b6cee71dSXin LI 			if (len-- == 0)
2796b6cee71dSXin LI 				break;
2797b6cee71dSXin LI 			c = *s++;
2798b6cee71dSXin LI 		}
2799b6cee71dSXin LI 		if (c >= 040 && c <= 0176)	/* TODO isprint && !iscntrl */
2800b6cee71dSXin LI 			(void) fputc(c, fp);
2801b6cee71dSXin LI 		else {
2802b6cee71dSXin LI 			(void) fputc('\\', fp);
2803b6cee71dSXin LI 			switch (c) {
2804b6cee71dSXin LI 			case '\a':
2805b6cee71dSXin LI 				(void) fputc('a', fp);
2806b6cee71dSXin LI 				break;
2807b6cee71dSXin LI 
2808b6cee71dSXin LI 			case '\b':
2809b6cee71dSXin LI 				(void) fputc('b', fp);
2810b6cee71dSXin LI 				break;
2811b6cee71dSXin LI 
2812b6cee71dSXin LI 			case '\f':
2813b6cee71dSXin LI 				(void) fputc('f', fp);
2814b6cee71dSXin LI 				break;
2815b6cee71dSXin LI 
2816b6cee71dSXin LI 			case '\n':
2817b6cee71dSXin LI 				(void) fputc('n', fp);
2818b6cee71dSXin LI 				break;
2819b6cee71dSXin LI 
2820b6cee71dSXin LI 			case '\r':
2821b6cee71dSXin LI 				(void) fputc('r', fp);
2822b6cee71dSXin LI 				break;
2823b6cee71dSXin LI 
2824b6cee71dSXin LI 			case '\t':
2825b6cee71dSXin LI 				(void) fputc('t', fp);
2826b6cee71dSXin LI 				break;
2827b6cee71dSXin LI 
2828b6cee71dSXin LI 			case '\v':
2829b6cee71dSXin LI 				(void) fputc('v', fp);
2830b6cee71dSXin LI 				break;
2831b6cee71dSXin LI 
2832b6cee71dSXin LI 			default:
2833b6cee71dSXin LI 				(void) fprintf(fp, "%.3o", c & 0377);
2834b6cee71dSXin LI 				break;
2835b6cee71dSXin LI 			}
2836b6cee71dSXin LI 		}
2837b6cee71dSXin LI 	}
2838b6cee71dSXin LI }
2839b6cee71dSXin LI 
2840b6cee71dSXin LI /*
2841b6cee71dSXin LI  * eatsize(): Eat the size spec from a number [eg. 10UL]
2842b6cee71dSXin LI  */
2843b6cee71dSXin LI private void
2844b6cee71dSXin LI eatsize(const char **p)
2845b6cee71dSXin LI {
2846b6cee71dSXin LI 	const char *l = *p;
2847b6cee71dSXin LI 
2848b6cee71dSXin LI 	if (LOWCASE(*l) == 'u')
2849b6cee71dSXin LI 		l++;
2850b6cee71dSXin LI 
2851b6cee71dSXin LI 	switch (LOWCASE(*l)) {
2852b6cee71dSXin LI 	case 'l':    /* long */
2853b6cee71dSXin LI 	case 's':    /* short */
2854b6cee71dSXin LI 	case 'h':    /* short */
2855b6cee71dSXin LI 	case 'b':    /* char/byte */
2856b6cee71dSXin LI 	case 'c':    /* char/byte */
2857b6cee71dSXin LI 		l++;
2858b6cee71dSXin LI 		/*FALLTHROUGH*/
2859b6cee71dSXin LI 	default:
2860b6cee71dSXin LI 		break;
2861b6cee71dSXin LI 	}
2862b6cee71dSXin LI 
2863b6cee71dSXin LI 	*p = l;
2864b6cee71dSXin LI }
2865b6cee71dSXin LI 
2866b6cee71dSXin LI /*
2867c2931133SXin LI  * handle a buffer containing a compiled file.
2868c2931133SXin LI  */
2869c2931133SXin LI private struct magic_map *
2870c2931133SXin LI apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len)
2871c2931133SXin LI {
2872c2931133SXin LI 	struct magic_map *map;
2873c2931133SXin LI 
2874c2931133SXin LI 	if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
2875c2931133SXin LI 		file_oomem(ms, sizeof(*map));
2876c2931133SXin LI 		return NULL;
2877c2931133SXin LI 	}
2878c2931133SXin LI 	map->len = len;
2879c2931133SXin LI 	map->p = buf;
2880c2931133SXin LI 	map->type = MAP_TYPE_USER;
2881c2931133SXin LI 	if (check_buffer(ms, map, "buffer") != 0) {
2882c2931133SXin LI 		apprentice_unmap(map);
2883c2931133SXin LI 		return NULL;
2884c2931133SXin LI 	}
2885c2931133SXin LI 	return map;
2886c2931133SXin LI }
2887c2931133SXin LI 
2888c2931133SXin LI /*
2889b6cee71dSXin LI  * handle a compiled file.
2890b6cee71dSXin LI  */
2891b6cee71dSXin LI 
2892b6cee71dSXin LI private struct magic_map *
2893b6cee71dSXin LI apprentice_map(struct magic_set *ms, const char *fn)
2894b6cee71dSXin LI {
2895b6cee71dSXin LI 	int fd;
2896b6cee71dSXin LI 	struct stat st;
2897b6cee71dSXin LI 	char *dbname = NULL;
2898b6cee71dSXin LI 	struct magic_map *map;
2899b6cee71dSXin LI 
2900b6cee71dSXin LI 	fd = -1;
2901b6cee71dSXin LI 	if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
2902b6cee71dSXin LI 		file_oomem(ms, sizeof(*map));
2903b6cee71dSXin LI 		goto error;
2904b6cee71dSXin LI 	}
2905b6cee71dSXin LI 
2906b6cee71dSXin LI 	dbname = mkdbname(ms, fn, 0);
2907b6cee71dSXin LI 	if (dbname == NULL)
2908b6cee71dSXin LI 		goto error;
2909b6cee71dSXin LI 
2910b6cee71dSXin LI 	if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
2911b6cee71dSXin LI 		goto error;
2912b6cee71dSXin LI 
2913b6cee71dSXin LI 	if (fstat(fd, &st) == -1) {
2914b6cee71dSXin LI 		file_error(ms, errno, "cannot stat `%s'", dbname);
2915b6cee71dSXin LI 		goto error;
2916b6cee71dSXin LI 	}
2917b6cee71dSXin LI 	if (st.st_size < 8 || st.st_size > MAXMAGIC_SIZE) {
2918b6cee71dSXin LI 		file_error(ms, 0, "file `%s' is too %s", dbname,
2919b6cee71dSXin LI 		    st.st_size < 8 ? "small" : "large");
2920b6cee71dSXin LI 		goto error;
2921b6cee71dSXin LI 	}
2922b6cee71dSXin LI 
2923b6cee71dSXin LI 	map->len = (size_t)st.st_size;
2924b6cee71dSXin LI #ifdef QUICK
2925b6cee71dSXin LI 	if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
2926b6cee71dSXin LI 	    MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
2927b6cee71dSXin LI 		file_error(ms, errno, "cannot map `%s'", dbname);
2928b6cee71dSXin LI 		goto error;
2929b6cee71dSXin LI 	}
2930c2931133SXin LI 	map->type = MAP_TYPE_MMAP;
2931b6cee71dSXin LI #else
2932b6cee71dSXin LI 	if ((map->p = CAST(void *, malloc(map->len))) == NULL) {
2933b6cee71dSXin LI 		file_oomem(ms, map->len);
2934b6cee71dSXin LI 		goto error;
2935b6cee71dSXin LI 	}
2936b6cee71dSXin LI 	if (read(fd, map->p, map->len) != (ssize_t)map->len) {
2937b6cee71dSXin LI 		file_badread(ms);
2938b6cee71dSXin LI 		goto error;
2939b6cee71dSXin LI 	}
2940c2931133SXin LI 	map->type = MAP_TYPE_MALLOC;
2941b6cee71dSXin LI #define RET	1
2942b6cee71dSXin LI #endif
2943b6cee71dSXin LI 	(void)close(fd);
2944b6cee71dSXin LI 	fd = -1;
2945c2931133SXin LI 
2946c2931133SXin LI 	if (check_buffer(ms, map, dbname) != 0)
2947c2931133SXin LI 		goto error;
2948c2931133SXin LI 
2949c2931133SXin LI 	free(dbname);
2950c2931133SXin LI 	return map;
2951c2931133SXin LI 
2952c2931133SXin LI error:
2953c2931133SXin LI 	if (fd != -1)
2954c2931133SXin LI 		(void)close(fd);
2955c2931133SXin LI 	apprentice_unmap(map);
2956c2931133SXin LI 	free(dbname);
2957c2931133SXin LI 	return NULL;
2958c2931133SXin LI }
2959c2931133SXin LI 
2960c2931133SXin LI private int
2961c2931133SXin LI check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname)
2962c2931133SXin LI {
2963c2931133SXin LI 	uint32_t *ptr;
2964c2931133SXin LI 	uint32_t entries, nentries;
2965c2931133SXin LI 	uint32_t version;
2966c2931133SXin LI 	int i, needsbyteswap;
2967c2931133SXin LI 
2968b6cee71dSXin LI 	ptr = CAST(uint32_t *, map->p);
2969b6cee71dSXin LI 	if (*ptr != MAGICNO) {
2970b6cee71dSXin LI 		if (swap4(*ptr) != MAGICNO) {
2971b6cee71dSXin LI 			file_error(ms, 0, "bad magic in `%s'", dbname);
2972c2931133SXin LI 			return -1;
2973b6cee71dSXin LI 		}
2974b6cee71dSXin LI 		needsbyteswap = 1;
2975b6cee71dSXin LI 	} else
2976b6cee71dSXin LI 		needsbyteswap = 0;
2977b6cee71dSXin LI 	if (needsbyteswap)
2978b6cee71dSXin LI 		version = swap4(ptr[1]);
2979b6cee71dSXin LI 	else
2980b6cee71dSXin LI 		version = ptr[1];
2981b6cee71dSXin LI 	if (version != VERSIONNO) {
2982b6cee71dSXin LI 		file_error(ms, 0, "File %s supports only version %d magic "
2983b6cee71dSXin LI 		    "files. `%s' is version %d", VERSION,
2984b6cee71dSXin LI 		    VERSIONNO, dbname, version);
2985c2931133SXin LI 		return -1;
2986b6cee71dSXin LI 	}
2987c2931133SXin LI 	entries = (uint32_t)(map->len / sizeof(struct magic));
2988c2931133SXin LI 	if ((entries * sizeof(struct magic)) != map->len) {
2989c2931133SXin LI 		file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not "
2990b6cee71dSXin LI 		    "a multiple of %" SIZE_T_FORMAT "u",
2991c2931133SXin LI 		    dbname, map->len, sizeof(struct magic));
2992c2931133SXin LI 		return -1;
2993b6cee71dSXin LI 	}
2994b6cee71dSXin LI 	map->magic[0] = CAST(struct magic *, map->p) + 1;
2995b6cee71dSXin LI 	nentries = 0;
2996b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
2997b6cee71dSXin LI 		if (needsbyteswap)
2998b6cee71dSXin LI 			map->nmagic[i] = swap4(ptr[i + 2]);
2999b6cee71dSXin LI 		else
3000b6cee71dSXin LI 			map->nmagic[i] = ptr[i + 2];
3001b6cee71dSXin LI 		if (i != MAGIC_SETS - 1)
3002b6cee71dSXin LI 			map->magic[i + 1] = map->magic[i] + map->nmagic[i];
3003b6cee71dSXin LI 		nentries += map->nmagic[i];
3004b6cee71dSXin LI 	}
3005b6cee71dSXin LI 	if (entries != nentries + 1) {
3006b6cee71dSXin LI 		file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
3007b6cee71dSXin LI 		    dbname, entries, nentries + 1);
3008c2931133SXin LI 		return -1;
3009b6cee71dSXin LI 	}
3010b6cee71dSXin LI 	if (needsbyteswap)
3011b6cee71dSXin LI 		for (i = 0; i < MAGIC_SETS; i++)
3012b6cee71dSXin LI 			byteswap(map->magic[i], map->nmagic[i]);
3013c2931133SXin LI 	return 0;
3014b6cee71dSXin LI }
3015b6cee71dSXin LI 
3016b6cee71dSXin LI /*
3017b6cee71dSXin LI  * handle an mmaped file.
3018b6cee71dSXin LI  */
3019b6cee71dSXin LI private int
3020b6cee71dSXin LI apprentice_compile(struct magic_set *ms, struct magic_map *map, const char *fn)
3021b6cee71dSXin LI {
3022b6cee71dSXin LI 	static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS;
3023b6cee71dSXin LI 	static const size_t m = sizeof(**map->magic);
3024b6cee71dSXin LI 	int fd = -1;
3025b6cee71dSXin LI 	size_t len;
3026b6cee71dSXin LI 	char *dbname;
3027b6cee71dSXin LI 	int rv = -1;
3028b6cee71dSXin LI 	uint32_t i;
3029b6cee71dSXin LI 	union {
3030b6cee71dSXin LI 		struct magic m;
3031b6cee71dSXin LI 		uint32_t h[2 + MAGIC_SETS];
3032b6cee71dSXin LI 	} hdr;
3033b6cee71dSXin LI 
3034b6cee71dSXin LI 	dbname = mkdbname(ms, fn, 1);
3035b6cee71dSXin LI 
3036b6cee71dSXin LI 	if (dbname == NULL)
3037b6cee71dSXin LI 		goto out;
3038b6cee71dSXin LI 
3039b6cee71dSXin LI 	if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1)
3040b6cee71dSXin LI 	{
3041b6cee71dSXin LI 		file_error(ms, errno, "cannot open `%s'", dbname);
3042b6cee71dSXin LI 		goto out;
3043b6cee71dSXin LI 	}
3044b6cee71dSXin LI 	memset(&hdr, 0, sizeof(hdr));
3045b6cee71dSXin LI 	hdr.h[0] = MAGICNO;
3046b6cee71dSXin LI 	hdr.h[1] = VERSIONNO;
3047b6cee71dSXin LI 	memcpy(hdr.h + 2, map->nmagic, nm);
3048b6cee71dSXin LI 
3049b6cee71dSXin LI 	if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
3050b6cee71dSXin LI 		file_error(ms, errno, "error writing `%s'", dbname);
3051b6cee71dSXin LI 		goto out;
3052b6cee71dSXin LI 	}
3053b6cee71dSXin LI 
3054b6cee71dSXin LI 	for (i = 0; i < MAGIC_SETS; i++) {
3055b6cee71dSXin LI 		len = m * map->nmagic[i];
3056b6cee71dSXin LI 		if (write(fd, map->magic[i], len) != (ssize_t)len) {
3057b6cee71dSXin LI 			file_error(ms, errno, "error writing `%s'", dbname);
3058b6cee71dSXin LI 			goto out;
3059b6cee71dSXin LI 		}
3060b6cee71dSXin LI 	}
3061b6cee71dSXin LI 
3062b6cee71dSXin LI 	if (fd != -1)
3063b6cee71dSXin LI 		(void)close(fd);
3064b6cee71dSXin LI 	rv = 0;
3065b6cee71dSXin LI out:
3066b6cee71dSXin LI 	free(dbname);
3067b6cee71dSXin LI 	return rv;
3068b6cee71dSXin LI }
3069b6cee71dSXin LI 
3070b6cee71dSXin LI private const char ext[] = ".mgc";
3071b6cee71dSXin LI /*
3072b6cee71dSXin LI  * make a dbname
3073b6cee71dSXin LI  */
3074b6cee71dSXin LI private char *
3075b6cee71dSXin LI mkdbname(struct magic_set *ms, const char *fn, int strip)
3076b6cee71dSXin LI {
3077b6cee71dSXin LI 	const char *p, *q;
3078b6cee71dSXin LI 	char *buf;
3079b6cee71dSXin LI 
3080b6cee71dSXin LI 	if (strip) {
3081b6cee71dSXin LI 		if ((p = strrchr(fn, '/')) != NULL)
3082b6cee71dSXin LI 			fn = ++p;
3083b6cee71dSXin LI 	}
3084b6cee71dSXin LI 
3085b6cee71dSXin LI 	for (q = fn; *q; q++)
3086b6cee71dSXin LI 		continue;
3087b6cee71dSXin LI 	/* Look for .mgc */
3088b6cee71dSXin LI 	for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--)
3089b6cee71dSXin LI 		if (*p != *q)
3090b6cee71dSXin LI 			break;
3091b6cee71dSXin LI 
3092b6cee71dSXin LI 	/* Did not find .mgc, restore q */
3093b6cee71dSXin LI 	if (p >= ext)
3094b6cee71dSXin LI 		while (*q)
3095b6cee71dSXin LI 			q++;
3096b6cee71dSXin LI 
3097b6cee71dSXin LI 	q++;
3098b6cee71dSXin LI 	/* Compatibility with old code that looked in .mime */
3099b6cee71dSXin LI 	if (ms->flags & MAGIC_MIME) {
3100b6cee71dSXin LI 		if (asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext) < 0)
3101b6cee71dSXin LI 			return NULL;
3102b6cee71dSXin LI 		if (access(buf, R_OK) != -1) {
3103b6cee71dSXin LI 			ms->flags &= MAGIC_MIME_TYPE;
3104b6cee71dSXin LI 			return buf;
3105b6cee71dSXin LI 		}
3106b6cee71dSXin LI 		free(buf);
3107b6cee71dSXin LI 	}
3108b6cee71dSXin LI 	if (asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext) < 0)
3109b6cee71dSXin LI 		return NULL;
3110b6cee71dSXin LI 
3111b6cee71dSXin LI 	/* Compatibility with old code that looked in .mime */
3112b6cee71dSXin LI 	if (strstr(p, ".mime") != NULL)
3113b6cee71dSXin LI 		ms->flags &= MAGIC_MIME_TYPE;
3114b6cee71dSXin LI 	return buf;
3115b6cee71dSXin LI }
3116b6cee71dSXin LI 
3117b6cee71dSXin LI /*
3118b6cee71dSXin LI  * Byteswap an mmap'ed file if needed
3119b6cee71dSXin LI  */
3120b6cee71dSXin LI private void
3121b6cee71dSXin LI byteswap(struct magic *magic, uint32_t nmagic)
3122b6cee71dSXin LI {
3123b6cee71dSXin LI 	uint32_t i;
3124b6cee71dSXin LI 	for (i = 0; i < nmagic; i++)
3125b6cee71dSXin LI 		bs1(&magic[i]);
3126b6cee71dSXin LI }
3127b6cee71dSXin LI 
3128b6cee71dSXin LI /*
3129b6cee71dSXin LI  * swap a short
3130b6cee71dSXin LI  */
3131b6cee71dSXin LI private uint16_t
3132b6cee71dSXin LI swap2(uint16_t sv)
3133b6cee71dSXin LI {
3134b6cee71dSXin LI 	uint16_t rv;
3135b6cee71dSXin LI 	uint8_t *s = (uint8_t *)(void *)&sv;
3136b6cee71dSXin LI 	uint8_t *d = (uint8_t *)(void *)&rv;
3137b6cee71dSXin LI 	d[0] = s[1];
3138b6cee71dSXin LI 	d[1] = s[0];
3139b6cee71dSXin LI 	return rv;
3140b6cee71dSXin LI }
3141b6cee71dSXin LI 
3142b6cee71dSXin LI /*
3143b6cee71dSXin LI  * swap an int
3144b6cee71dSXin LI  */
3145b6cee71dSXin LI private uint32_t
3146b6cee71dSXin LI swap4(uint32_t sv)
3147b6cee71dSXin LI {
3148b6cee71dSXin LI 	uint32_t rv;
3149b6cee71dSXin LI 	uint8_t *s = (uint8_t *)(void *)&sv;
3150b6cee71dSXin LI 	uint8_t *d = (uint8_t *)(void *)&rv;
3151b6cee71dSXin LI 	d[0] = s[3];
3152b6cee71dSXin LI 	d[1] = s[2];
3153b6cee71dSXin LI 	d[2] = s[1];
3154b6cee71dSXin LI 	d[3] = s[0];
3155b6cee71dSXin LI 	return rv;
3156b6cee71dSXin LI }
3157b6cee71dSXin LI 
3158b6cee71dSXin LI /*
3159b6cee71dSXin LI  * swap a quad
3160b6cee71dSXin LI  */
3161b6cee71dSXin LI private uint64_t
3162b6cee71dSXin LI swap8(uint64_t sv)
3163b6cee71dSXin LI {
3164b6cee71dSXin LI 	uint64_t rv;
3165b6cee71dSXin LI 	uint8_t *s = (uint8_t *)(void *)&sv;
3166b6cee71dSXin LI 	uint8_t *d = (uint8_t *)(void *)&rv;
3167b6cee71dSXin LI #if 0
3168b6cee71dSXin LI 	d[0] = s[3];
3169b6cee71dSXin LI 	d[1] = s[2];
3170b6cee71dSXin LI 	d[2] = s[1];
3171b6cee71dSXin LI 	d[3] = s[0];
3172b6cee71dSXin LI 	d[4] = s[7];
3173b6cee71dSXin LI 	d[5] = s[6];
3174b6cee71dSXin LI 	d[6] = s[5];
3175b6cee71dSXin LI 	d[7] = s[4];
3176b6cee71dSXin LI #else
3177b6cee71dSXin LI 	d[0] = s[7];
3178b6cee71dSXin LI 	d[1] = s[6];
3179b6cee71dSXin LI 	d[2] = s[5];
3180b6cee71dSXin LI 	d[3] = s[4];
3181b6cee71dSXin LI 	d[4] = s[3];
3182b6cee71dSXin LI 	d[5] = s[2];
3183b6cee71dSXin LI 	d[6] = s[1];
3184b6cee71dSXin LI 	d[7] = s[0];
3185b6cee71dSXin LI #endif
3186b6cee71dSXin LI 	return rv;
3187b6cee71dSXin LI }
3188b6cee71dSXin LI 
3189b6cee71dSXin LI /*
3190b6cee71dSXin LI  * byteswap a single magic entry
3191b6cee71dSXin LI  */
3192b6cee71dSXin LI private void
3193b6cee71dSXin LI bs1(struct magic *m)
3194b6cee71dSXin LI {
3195b6cee71dSXin LI 	m->cont_level = swap2(m->cont_level);
3196b6cee71dSXin LI 	m->offset = swap4((uint32_t)m->offset);
3197b6cee71dSXin LI 	m->in_offset = swap4((uint32_t)m->in_offset);
3198b6cee71dSXin LI 	m->lineno = swap4((uint32_t)m->lineno);
3199b6cee71dSXin LI 	if (IS_STRING(m->type)) {
3200b6cee71dSXin LI 		m->str_range = swap4(m->str_range);
3201b6cee71dSXin LI 		m->str_flags = swap4(m->str_flags);
3202b6cee71dSXin LI 	}
3203b6cee71dSXin LI 	else {
3204b6cee71dSXin LI 		m->value.q = swap8(m->value.q);
3205b6cee71dSXin LI 		m->num_mask = swap8(m->num_mask);
3206b6cee71dSXin LI 	}
3207b6cee71dSXin LI }
3208b6cee71dSXin LI 
3209b6cee71dSXin LI protected size_t
3210b6cee71dSXin LI file_pstring_length_size(const struct magic *m)
3211b6cee71dSXin LI {
3212b6cee71dSXin LI 	switch (m->str_flags & PSTRING_LEN) {
3213b6cee71dSXin LI 	case PSTRING_1_LE:
3214b6cee71dSXin LI 		return 1;
3215b6cee71dSXin LI 	case PSTRING_2_LE:
3216b6cee71dSXin LI 	case PSTRING_2_BE:
3217b6cee71dSXin LI 		return 2;
3218b6cee71dSXin LI 	case PSTRING_4_LE:
3219b6cee71dSXin LI 	case PSTRING_4_BE:
3220b6cee71dSXin LI 		return 4;
3221b6cee71dSXin LI 	default:
3222b6cee71dSXin LI 		abort();	/* Impossible */
3223b6cee71dSXin LI 		return 1;
3224b6cee71dSXin LI 	}
3225b6cee71dSXin LI }
3226b6cee71dSXin LI protected size_t
3227*9ce06829SXin LI file_pstring_get_length(const struct magic *m, const char *ss)
3228b6cee71dSXin LI {
3229b6cee71dSXin LI 	size_t len = 0;
3230*9ce06829SXin LI 	const unsigned char *s = (const unsigned char *)ss;
3231b6cee71dSXin LI 
3232b6cee71dSXin LI 	switch (m->str_flags & PSTRING_LEN) {
3233b6cee71dSXin LI 	case PSTRING_1_LE:
3234b6cee71dSXin LI 		len = *s;
3235b6cee71dSXin LI 		break;
3236b6cee71dSXin LI 	case PSTRING_2_LE:
3237b6cee71dSXin LI 		len = (s[1] << 8) | s[0];
3238b6cee71dSXin LI 		break;
3239b6cee71dSXin LI 	case PSTRING_2_BE:
3240b6cee71dSXin LI 		len = (s[0] << 8) | s[1];
3241b6cee71dSXin LI 		break;
3242b6cee71dSXin LI 	case PSTRING_4_LE:
3243b6cee71dSXin LI 		len = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0];
3244b6cee71dSXin LI 		break;
3245b6cee71dSXin LI 	case PSTRING_4_BE:
3246b6cee71dSXin LI 		len = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
3247b6cee71dSXin LI 		break;
3248b6cee71dSXin LI 	default:
3249b6cee71dSXin LI 		abort();	/* Impossible */
3250b6cee71dSXin LI 	}
3251b6cee71dSXin LI 
3252b6cee71dSXin LI 	if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
3253b6cee71dSXin LI 		len -= file_pstring_length_size(m);
3254b6cee71dSXin LI 
3255b6cee71dSXin LI 	return len;
3256b6cee71dSXin LI }
3257b6cee71dSXin LI 
3258b6cee71dSXin LI protected int
3259b6cee71dSXin LI file_magicfind(struct magic_set *ms, const char *name, struct mlist *v)
3260b6cee71dSXin LI {
3261b6cee71dSXin LI 	uint32_t i, j;
3262b6cee71dSXin LI 	struct mlist *mlist, *ml;
3263b6cee71dSXin LI 
3264b6cee71dSXin LI 	mlist = ms->mlist[1];
3265b6cee71dSXin LI 
3266b6cee71dSXin LI 	for (ml = mlist->next; ml != mlist; ml = ml->next) {
3267b6cee71dSXin LI 		struct magic *ma = ml->magic;
3268b6cee71dSXin LI 		uint32_t nma = ml->nmagic;
3269b6cee71dSXin LI 		for (i = 0; i < nma; i++) {
3270b6cee71dSXin LI 			if (ma[i].type != FILE_NAME)
3271b6cee71dSXin LI 				continue;
3272b6cee71dSXin LI 			if (strcmp(ma[i].value.s, name) == 0) {
3273b6cee71dSXin LI 				v->magic = &ma[i];
3274b6cee71dSXin LI 				for (j = i + 1; j < nma; j++)
3275b6cee71dSXin LI 				    if (ma[j].cont_level == 0)
3276b6cee71dSXin LI 					    break;
3277b6cee71dSXin LI 				v->nmagic = j - i;
3278b6cee71dSXin LI 				return 0;
3279b6cee71dSXin LI 			}
3280b6cee71dSXin LI 		}
3281b6cee71dSXin LI 	}
3282b6cee71dSXin LI 	return -1;
3283b6cee71dSXin LI }
3284