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