file.h revision 169962
1317017Sdim/*
2317017Sdim * Copyright (c) Ian F. Darwin 1986-1995.
3353358Sdim * Software written by Ian F. Darwin and others;
4353358Sdim * maintained 1995-present by Christos Zoulas and others.
5353358Sdim *
6317017Sdim * Redistribution and use in source and binary forms, with or without
7317017Sdim * modification, are permitted provided that the following conditions
8317017Sdim * are met:
9317017Sdim * 1. Redistributions of source code must retain the above copyright
10317017Sdim *    notice immediately at the beginning of the file, without modification,
11317017Sdim *    this list of conditions, and the following disclaimer.
12317017Sdim * 2. Redistributions in binary form must reproduce the above copyright
13317017Sdim *    notice, this list of conditions and the following disclaimer in the
14317017Sdim *    documentation and/or other materials provided with the distribution.
15317017Sdim *
16317017Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17317017Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18327952Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19317017Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20317017Sdim * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21317017Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22317017Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23317017Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24317017Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25317017Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26341825Sdim * SUCH DAMAGE.
27317017Sdim */
28317017Sdim/*
29317017Sdim * file.h - definitions for file(1) program
30317017Sdim * @(#)$File: file.h,v 1.91 2007/03/25 03:13:47 christos Exp $
31317017Sdim */
32317017Sdim
33317017Sdim#ifndef __file_h__
34317017Sdim#define __file_h__
35327952Sdim
36317017Sdim#ifdef HAVE_CONFIG_H
37317017Sdim#include <config.h>
38317017Sdim#endif
39317017Sdim
40317017Sdim#include <stdio.h>	/* Include that here, to make sure __P gets defined */
41317017Sdim#include <errno.h>
42317017Sdim#include <fcntl.h>	/* For open and flags */
43317017Sdim#ifdef HAVE_STDINT_H
44317017Sdim#include <stdint.h>
45317017Sdim#endif
46317017Sdim#ifdef HAVE_INTTYPES_H
47317017Sdim#include <inttypes.h>
48317017Sdim#endif
49317017Sdim#include <regex.h>
50317017Sdim#include <sys/types.h>
51317017Sdim/* Do this here and now, because struct stat gets re-defined on solaris */
52317017Sdim#include <sys/stat.h>
53317017Sdim
54317017Sdim#define ENABLE_CONDITIONALS
55317017Sdim
56317017Sdim#ifndef MAGIC
57317017Sdim#define MAGIC "/etc/magic"
58317017Sdim#endif
59317017Sdim
60317017Sdim#ifdef __EMX__
61317017Sdim#define PATHSEP	';'
62317017Sdim#else
63317017Sdim#define PATHSEP	':'
64317017Sdim#endif
65317017Sdim
66317017Sdim#define private static
67317017Sdim#ifndef protected
68317017Sdim#define protected
69317017Sdim#endif
70317017Sdim#define public
71317017Sdim
72317017Sdim#ifndef __GNUC_PREREQ__
73317017Sdim#ifdef __GNUC__
74317017Sdim#define	__GNUC_PREREQ__(x, y)						\
75327952Sdim	((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||			\
76327952Sdim	 (__GNUC__ > (x)))
77327952Sdim#else
78317017Sdim#define	__GNUC_PREREQ__(x, y)	0
79317017Sdim#endif
80317017Sdim#endif
81317017Sdim
82317017Sdim#ifndef MIN
83317017Sdim#define	MIN(a,b)	(((a) < (b)) ? (a) : (b))
84317017Sdim#endif
85317017Sdim
86317017Sdim#ifndef HOWMANY
87317017Sdim# define HOWMANY (256 * 1024)	/* how much of the file to look at */
88317017Sdim#endif
89317017Sdim#define MAXMAGIS 8192		/* max entries in /etc/magic */
90317017Sdim#define MAXDESC	64		/* max leng of text description */
91317017Sdim#define MAXstring 32		/* max leng of "string" types */
92317017Sdim
93317017Sdim#define MAGICNO		0xF11E041C
94317017Sdim#define VERSIONNO	4
95317017Sdim#define FILE_MAGICSIZE	(32 * 4)
96317017Sdim
97317017Sdim#define	FILE_LOAD	0
98317017Sdim#define FILE_CHECK	1
99317017Sdim#define FILE_COMPILE	2
100317017Sdim
101327952Sdimstruct magic {
102327952Sdim	/* Word 1 */
103327952Sdim	uint16_t cont_level;	/* level of ">" */
104317017Sdim	uint8_t nospflag;	/* supress space character */
105317017Sdim	uint8_t flag;
106317017Sdim#define INDIR		1	/* if '(...)' appears */
107317017Sdim#define OFFADD		2	/* if '>&' or '>...(&' appears */
108317017Sdim#define INDIROFFADD	4	/* if '>&(' appears */
109317017Sdim#define	UNSIGNED	8	/* comparison is unsigned */
110317017Sdim
111317017Sdim	/* Word 2 */
112317017Sdim	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
113317017Sdim	uint8_t vallen;		/* length of string value, if any */
114317017Sdim	uint8_t type;		/* int, short, long or string. */
115317017Sdim	uint8_t in_type;	/* type of indirrection */
116317017Sdim#define 			FILE_INVALID	0
117317017Sdim#define 			FILE_BYTE	1
118317017Sdim#define				FILE_SHORT	2
119317017Sdim#define				FILE_DEFAULT	3
120317017Sdim#define				FILE_LONG	4
121317017Sdim#define				FILE_STRING	5
122317017Sdim#define				FILE_DATE	6
123317017Sdim#define				FILE_BESHORT	7
124317017Sdim#define				FILE_BELONG	8
125317017Sdim#define				FILE_BEDATE	9
126317017Sdim#define				FILE_LESHORT	10
127327952Sdim#define				FILE_LELONG	11
128327952Sdim#define				FILE_LEDATE	12
129327952Sdim#define				FILE_PSTRING	13
130317017Sdim#define				FILE_LDATE	14
131317017Sdim#define				FILE_BELDATE	15
132317017Sdim#define				FILE_LELDATE	16
133317017Sdim#define				FILE_REGEX	17
134317017Sdim#define				FILE_BESTRING16	18
135317017Sdim#define				FILE_LESTRING16	19
136317017Sdim#define				FILE_SEARCH	20
137317017Sdim#define				FILE_MEDATE	21
138317017Sdim#define				FILE_MELDATE	22
139317017Sdim#define				FILE_MELONG	23
140317017Sdim#define				FILE_QUAD	24
141317017Sdim#define				FILE_LEQUAD	25
142320572Sdim#define				FILE_BEQUAD	26
143317017Sdim#define				FILE_QDATE	27
144317017Sdim#define				FILE_LEQDATE	28
145317017Sdim#define				FILE_BEQDATE	29
146317017Sdim#define				FILE_QLDATE	30
147317017Sdim#define				FILE_LEQLDATE	31
148317017Sdim#define				FILE_BEQLDATE	32
149317017Sdim#define				FILE_NAMES_SIZE	33/* size of array to contain all names */
150317017Sdim
151317017Sdim#define IS_STRING(t) \
152317017Sdim	((t) == FILE_STRING || \
153341825Sdim	 (t) == FILE_PSTRING || \
154317017Sdim	 (t) == FILE_BESTRING16 || \
155317017Sdim	 (t) == FILE_LESTRING16 || \
156317017Sdim	 (t) == FILE_REGEX || \
157317017Sdim	 (t) == FILE_SEARCH || \
158317017Sdim	 (t) == FILE_DEFAULT)
159317017Sdim
160327952Sdim#define FILE_FMT_NONE 0
161353358Sdim#define FILE_FMT_NUM  1 /* "cduxXi" */
162353358Sdim#define FILE_FMT_STR  2 /* "s" */
163353358Sdim#define FILE_FMT_QUAD 3 /* "ll" */
164353358Sdim
165317017Sdim	/* Word 3 */
166317017Sdim	uint8_t in_op;		/* operator for indirection */
167341825Sdim	uint8_t mask_op;	/* operator for mask */
168327952Sdim#ifdef ENABLE_CONDITIONALS
169327952Sdim	uint8_t cond;		/* conditional type */
170327952Sdim	uint8_t dummy1;
171327952Sdim#else
172327952Sdim	uint8_t dummy1;
173327952Sdim	uint8_t dummy2;
174327952Sdim#endif
175353358Sdim
176353358Sdim#define				FILE_OPS	"&|^+-*/%"
177353358Sdim#define				FILE_OPAND	0
178353358Sdim#define				FILE_OPOR	1
179353358Sdim#define				FILE_OPXOR	2
180327952Sdim#define				FILE_OPADD	3
181327952Sdim#define				FILE_OPMINUS	4
182327952Sdim#define				FILE_OPMULTIPLY	5
183327952Sdim#define				FILE_OPDIVIDE	6
184327952Sdim#define				FILE_OPMODULO	7
185360784Sdim#define				FILE_OPS_MASK	0x07 /* mask for above ops */
186360784Sdim#define				FILE_UNUSED_1	0x08
187360784Sdim#define				FILE_UNUSED_2	0x10
188360784Sdim#define				FILE_UNUSED_3	0x20
189317017Sdim#define				FILE_OPINVERSE	0x40
190317017Sdim#define				FILE_OPINDIRECT	0x80
191317017Sdim
192317017Sdim#ifdef ENABLE_CONDITIONALS
193317017Sdim#define				COND_NONE	0
194317017Sdim#define				COND_IF		1
195317017Sdim#define				COND_ELIF	2
196317017Sdim#define				COND_ELSE	3
197317017Sdim#endif /* ENABLE_CONDITIONALS */
198317017Sdim
199317017Sdim	/* Word 4 */
200317017Sdim	uint32_t offset;	/* offset to magic number */
201317017Sdim	/* Word 5 */
202341825Sdim	int32_t in_offset;	/* offset from indirection */
203317017Sdim	/* Word 6 */
204317017Sdim	uint32_t lineno;	/* line number in magic file */
205317017Sdim	/* Word 7,8 */
206317017Sdim	union {
207317017Sdim		uint64_t _mask;	/* for use with numeric and date types */
208317017Sdim		struct {
209317017Sdim			uint32_t _count;	/* repeat/line count */
210317017Sdim			uint32_t _flags;	/* modifier flags */
211317017Sdim		} _s;		/* for use with string types */
212317017Sdim	} _u;
213317017Sdim#define num_mask _u._mask
214317017Sdim#define str_count _u._s._count
215317017Sdim#define str_flags _u._s._flags
216317017Sdim
217317017Sdim	/* Words 9-16 */
218317017Sdim	union VALUETYPE {
219317017Sdim		uint8_t b;
220317017Sdim		uint16_t h;
221317017Sdim		uint32_t l;
222317017Sdim		uint64_t q;
223317017Sdim		uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
224317017Sdim		uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
225317017Sdim		uint8_t hq[8];	/* 8 bytes of a fixed-endian "quad" */
226317017Sdim		char s[MAXstring];	/* the search string or regex pattern */
227317017Sdim	} value;		/* either number or string */
228317017Sdim	/* Words 17..31 */
229317017Sdim	char desc[MAXDESC];	/* description */
230317017Sdim};
231317017Sdim
232#define BIT(A)   (1 << (A))
233#define STRING_COMPACT_BLANK		BIT(0)
234#define STRING_COMPACT_OPTIONAL_BLANK	BIT(1)
235#define STRING_IGNORE_LOWERCASE		BIT(2)
236#define STRING_IGNORE_UPPERCASE		BIT(3)
237#define REGEX_OFFSET_START		BIT(4)
238#define CHAR_COMPACT_BLANK		'B'
239#define CHAR_COMPACT_OPTIONAL_BLANK	'b'
240#define CHAR_IGNORE_LOWERCASE		'c'
241#define CHAR_IGNORE_UPPERCASE		'C'
242#define CHAR_REGEX_OFFSET_START		's'
243#define STRING_IGNORE_CASE		(STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
244
245
246/* list of magic entries */
247struct mlist {
248	struct magic *magic;		/* array of magic entries */
249	uint32_t nmagic;			/* number of entries in array */
250	int mapped;  /* allocation type: 0 => apprentice_file
251		      *                  1 => apprentice_map + malloc
252		      *                  2 => apprentice_map + mmap */
253	struct mlist *next, *prev;
254};
255
256struct magic_set {
257	struct mlist *mlist;
258	struct cont {
259		size_t len;
260		struct level_info {
261			int32_t off;
262			int got_match;
263#ifdef ENABLE_CONDITIONALS
264			int last_match;
265			int last_cond;	/* used for error checking by parse() */
266#endif
267		} *li;
268	} c;
269	struct out {
270		/* Accumulation buffer */
271		char *buf;
272		char *ptr;
273		size_t left;
274		size_t size;
275		/* Printable buffer */
276		char *pbuf;
277		size_t psize;
278	} o;
279	uint32_t offset;
280	int error;
281	int flags;
282	int haderr;
283	const char *file;
284	size_t line;			/* current magic line number */
285
286	/* data for searches */
287	struct {
288		const char *s;		/* start of search in original source */
289		size_t s_len;		/* length of search region */
290		size_t offset;		/* starting offset in source: XXX - should this be off_t? */
291		size_t rm_len;		/* match length */
292	} search;
293
294	union VALUETYPE ms_value;	/* either number or string */
295};
296
297struct stat;
298protected const char *file_fmttime(uint32_t, int);
299protected int file_buffer(struct magic_set *, int, const char *, const void *,
300    size_t);
301protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
302protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
303protected int file_printf(struct magic_set *, const char *, ...);
304protected int file_reset(struct magic_set *);
305protected int file_tryelf(struct magic_set *, int, const unsigned char *,
306    size_t);
307protected int file_zmagic(struct magic_set *, int, const char *,
308    const unsigned char *, size_t);
309protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
310protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
311protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
312protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
313protected uint64_t file_signextend(struct magic_set *, struct magic *,
314    uint64_t);
315protected void file_delmagic(struct magic *, int type, size_t entries);
316protected void file_badread(struct magic_set *);
317protected void file_badseek(struct magic_set *);
318protected void file_oomem(struct magic_set *, size_t);
319protected void file_error(struct magic_set *, int, const char *, ...);
320protected void file_magerror(struct magic_set *, const char *, ...);
321protected void file_magwarn(struct magic_set *, const char *, ...);
322protected void file_mdump(struct magic *);
323protected void file_showstr(FILE *, const char *, size_t);
324protected size_t file_mbswidth(const char *);
325protected const char *file_getbuffer(struct magic_set *);
326protected ssize_t sread(int, void *, size_t, int);
327protected int file_check_mem(struct magic_set *, unsigned int);
328
329#ifndef COMPILE_ONLY
330extern const char *file_names[];
331extern const size_t file_nnames;
332#endif
333
334#ifndef HAVE_STRERROR
335extern int sys_nerr;
336extern char *sys_errlist[];
337#define strerror(e) \
338	(((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
339#endif
340
341#ifndef HAVE_STRTOUL
342#define strtoul(a, b, c)	strtol(a, b, c)
343#endif
344
345#ifndef HAVE_SNPRINTF
346int snprintf(char *, size_t, const char *, ...);
347#endif
348
349#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
350#define QUICK
351#endif
352
353#ifndef O_BINARY
354#define O_BINARY	0
355#endif
356
357#define FILE_RCSID(id) \
358static const char *rcsid(const char *p) { \
359	return rcsid(p = id); \
360}
361
362#endif /* __file_h__ */
363