file.h revision 169942
1/*
2 * Copyright (c) Ian F. Darwin 1986-1995.
3 * Software written by Ian F. Darwin and others;
4 * maintained 1995-present by Christos Zoulas and others.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice immediately at the beginning of the file, without modification,
11 *    this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28/*
29 * file.h - definitions for file(1) program
30 * @(#)$Id: file.h,v 1.83 2006/12/11 21:48:49 christos Exp $
31 */
32
33#ifndef __file_h__
34#define __file_h__
35
36#ifdef HAVE_CONFIG_H
37#include <config.h>
38#endif
39
40#include <stdio.h>	/* Include that here, to make sure __P gets defined */
41#include <errno.h>
42#include <fcntl.h>	/* For open and flags */
43#ifdef HAVE_STDINT_H
44#include <stdint.h>
45#endif
46#ifdef HAVE_INTTYPES_H
47#include <inttypes.h>
48#endif
49#include <sys/types.h>
50/* Do this here and now, because struct stat gets re-defined on solaris */
51#include <sys/stat.h>
52
53#ifndef MAGIC
54#define MAGIC "/etc/magic"
55#endif
56
57#ifdef __EMX__
58#define PATHSEP	';'
59#else
60#define PATHSEP	':'
61#endif
62
63#define private static
64#ifndef protected
65#define protected
66#endif
67#define public
68
69#ifndef HOWMANY
70# define HOWMANY (256 * 1024)	/* how much of the file to look at */
71#endif
72#define MAXMAGIS 8192		/* max entries in /etc/magic */
73#define MAXDESC	64		/* max leng of text description */
74#define MAXstring 32		/* max leng of "string" types */
75
76#define MAGICNO		0xF11E041C
77#define VERSIONNO	3
78#define FILE_MAGICSIZE	(32 * 4)
79
80#define	FILE_LOAD	0
81#define FILE_CHECK	1
82#define FILE_COMPILE	2
83
84struct magic {
85	/* Word 1 */
86	uint16_t cont_level;	/* level of ">" */
87	uint8_t nospflag;	/* supress space character */
88	uint8_t flag;
89#define INDIR	1		/* if '>(...)' appears,  */
90#define	UNSIGNED 2		/* comparison is unsigned */
91#define OFFADD	4		/* if '>&' appears,  */
92#define INDIROFFADD	8	/* if '>&(' appears,  */
93	/* Word 2 */
94	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
95	uint8_t vallen;		/* length of string value, if any */
96	uint8_t type;		/* int, short, long or string. */
97	uint8_t in_type;	/* type of indirrection */
98#define 			FILE_BYTE	1
99#define				FILE_SHORT	2
100#define				FILE_LONG	4
101#define				FILE_STRING	5
102#define				FILE_DATE	6
103#define				FILE_BESHORT	7
104#define				FILE_BELONG	8
105#define				FILE_BEDATE	9
106#define				FILE_LESHORT	10
107#define				FILE_LELONG	11
108#define				FILE_LEDATE	12
109#define				FILE_PSTRING	13
110#define				FILE_LDATE	14
111#define				FILE_BELDATE	15
112#define				FILE_LELDATE	16
113#define				FILE_REGEX	17
114#define				FILE_BESTRING16	18
115#define				FILE_LESTRING16	19
116#define				FILE_SEARCH	20
117#define				FILE_MEDATE	21
118#define				FILE_MELDATE	22
119#define				FILE_MELONG	23
120#define				FILE_QUAD	24
121#define				FILE_LEQUAD	25
122#define				FILE_BEQUAD	26
123#define				FILE_QDATE	27
124#define				FILE_LEQDATE	28
125#define				FILE_BEQDATE	29
126#define				FILE_QLDATE	30
127#define				FILE_LEQLDATE	31
128#define				FILE_BEQLDATE	32
129
130#define				FILE_FORMAT_NAME	\
131/* 0 */ 			"invalid 0",		\
132/* 1 */				"byte",			\
133/* 2 */ 			"short",		\
134/* 3 */ 			"invalid 3",		\
135/* 4 */ 			"long",			\
136/* 5 */ 			"string",		\
137/* 6 */ 			"date",			\
138/* 7 */ 			"beshort",		\
139/* 8 */ 			"belong",		\
140/* 9 */ 			"bedate",		\
141/* 10 */ 			"leshort",		\
142/* 11 */ 			"lelong",		\
143/* 12 */ 			"ledate",		\
144/* 13 */ 			"pstring",		\
145/* 14 */ 			"ldate",		\
146/* 15 */ 			"beldate",		\
147/* 16 */ 			"leldate",		\
148/* 17 */ 			"regex",		\
149/* 18 */			"bestring16",		\
150/* 19 */			"lestring16",		\
151/* 20 */ 			"search",		\
152/* 21 */ 			"medate",		\
153/* 22 */ 			"meldate",		\
154/* 23 */ 			"melong",		\
155/* 24 */ 			"quad",			\
156/* 25 */ 			"lequad",		\
157/* 26 */ 			"bequad",		\
158/* 27 */ 			"qdate",		\
159/* 28 */ 			"leqdate",		\
160/* 29 */ 			"beqdate",		\
161/* 30 */ 			"qldate",		\
162/* 31 */ 			"leqldate",		\
163/* 32 */ 			"beqldate",
164
165
166#define FILE_FMT_NONE 0
167#define FILE_FMT_NUM  1 /* "cduxXi" */
168#define FILE_FMT_STR  2 /* "s" */
169#define FILE_FMT_QUAD 3 /* "ll" */
170
171#define				FILE_FORMAT_STRING	\
172/* 0 */ 			FILE_FMT_NONE,		\
173/* 1 */				FILE_FMT_NUM,		\
174/* 2 */ 			FILE_FMT_NUM,		\
175/* 3 */ 			FILE_FMT_NONE,		\
176/* 4 */ 			FILE_FMT_NUM,		\
177/* 5 */ 			FILE_FMT_STR,		\
178/* 6 */ 			FILE_FMT_STR,		\
179/* 7 */ 			FILE_FMT_NUM,		\
180/* 8 */ 			FILE_FMT_NUM,		\
181/* 9 */ 			FILE_FMT_STR,		\
182/* 10 */ 			FILE_FMT_NUM,		\
183/* 11 */ 			FILE_FMT_NUM,		\
184/* 12 */ 			FILE_FMT_STR,		\
185/* 13 */ 			FILE_FMT_STR,		\
186/* 14 */ 			FILE_FMT_STR,		\
187/* 15 */ 			FILE_FMT_STR,		\
188/* 16 */ 			FILE_FMT_STR,		\
189/* 17 */ 			FILE_FMT_STR,		\
190/* 18 */			FILE_FMT_STR,		\
191/* 19 */			FILE_FMT_STR,		\
192/* 20 */			FILE_FMT_STR,		\
193/* 21 */			FILE_FMT_STR,		\
194/* 22 */			FILE_FMT_STR,		\
195/* 23 */			FILE_FMT_NUM,		\
196/* 24 */			FILE_FMT_QUAD,		\
197/* 25 */			FILE_FMT_QUAD,		\
198/* 26 */			FILE_FMT_QUAD,		\
199/* 27 */			FILE_FMT_STR,		\
200/* 28 */			FILE_FMT_STR,		\
201/* 29 */			FILE_FMT_STR,		\
202/* 30 */			FILE_FMT_STR,		\
203/* 31 */			FILE_FMT_STR,		\
204/* 32 */			FILE_FMT_STR,
205
206
207	/* Word 3 */
208	uint8_t in_op;		/* operator for indirection */
209	uint8_t mask_op;	/* operator for mask */
210	uint8_t dummy1;
211	uint8_t dummy2;
212#define				FILE_OPS	"&|^+-*/%"
213#define				FILE_OPAND	0
214#define				FILE_OPOR	1
215#define				FILE_OPXOR	2
216#define				FILE_OPADD	3
217#define				FILE_OPMINUS	4
218#define				FILE_OPMULTIPLY	5
219#define				FILE_OPDIVIDE	6
220#define				FILE_OPMODULO	7
221#define				FILE_OPINVERSE	0x40
222#define				FILE_OPINDIRECT	0x80
223	/* Word 4 */
224	uint32_t offset;	/* offset to magic number */
225	/* Word 5 */
226	int32_t in_offset;	/* offset from indirection */
227	/* Word 6 */
228	uint32_t lineno;	/* line number in magic file */
229	/* Word 7,8 */
230	uint64_t mask;	/* mask before comparison with value */
231	/* Words 9-16 */
232	union VALUETYPE {
233		uint8_t b;
234		uint16_t h;
235		uint32_t l;
236		uint64_t q;
237		char s[MAXstring];
238		struct {
239			char *buf;
240			size_t buflen;
241		} search;
242		uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
243		uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
244		uint8_t hq[8];	/* 8 bytes of a fixed-endian "quad" */
245	} value;		/* either number or string */
246	/* Words 17..31 */
247	char desc[MAXDESC];	/* description */
248};
249
250#define BIT(A)   (1 << (A))
251#define STRING_IGNORE_LOWERCASE		BIT(0)
252#define STRING_COMPACT_BLANK		BIT(1)
253#define STRING_COMPACT_OPTIONAL_BLANK	BIT(2)
254#define CHAR_IGNORE_LOWERCASE		'c'
255#define CHAR_COMPACT_BLANK		'B'
256#define CHAR_COMPACT_OPTIONAL_BLANK	'b'
257
258
259/* list of magic entries */
260struct mlist {
261	struct magic *magic;		/* array of magic entries */
262	uint32_t nmagic;			/* number of entries in array */
263	int mapped;  /* allocation type: 0 => apprentice_file
264		      *                  1 => apprentice_map + malloc
265		      *                  2 => apprentice_map + mmap */
266	struct mlist *next, *prev;
267};
268
269struct magic_set {
270    struct mlist *mlist;
271    struct cont {
272	size_t len;
273	int32_t *off;
274    } c;
275    struct out {
276	/* Accumulation buffer */
277	char *buf;
278	char *ptr;
279	size_t len;
280	size_t size;
281	/* Printable buffer */
282	char *pbuf;
283	size_t psize;
284    } o;
285    uint32_t offset;
286    int error;
287    int flags;
288    int haderr;
289    const char *file;
290    size_t line;
291};
292
293struct stat;
294protected const char *file_fmttime(uint32_t, int);
295protected int file_buffer(struct magic_set *, int, const void *, size_t);
296protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
297protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
298protected int file_printf(struct magic_set *, const char *, ...);
299protected int file_reset(struct magic_set *);
300protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
301protected int file_zmagic(struct magic_set *, int, const unsigned char *, size_t);
302protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
303protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
304protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
305protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
306protected uint64_t file_signextend(struct magic_set *, struct magic *, uint64_t);
307protected void file_delmagic(struct magic *, int type, size_t entries);
308protected void file_badread(struct magic_set *);
309protected void file_badseek(struct magic_set *);
310protected void file_oomem(struct magic_set *, size_t);
311protected void file_error(struct magic_set *, int, const char *, ...);
312protected void file_magwarn(struct magic_set *, const char *, ...);
313protected void file_mdump(struct magic *);
314protected void file_showstr(FILE *, const char *, size_t);
315protected size_t file_mbswidth(const char *);
316protected const char *file_getbuffer(struct magic_set *);
317protected ssize_t sread(int, void *, size_t);
318
319#ifndef COMPILE_ONLY
320extern const char *file_names[];
321extern const size_t file_nnames;
322#endif
323
324#ifndef HAVE_STRERROR
325extern int sys_nerr;
326extern char *sys_errlist[];
327#define strerror(e) \
328	(((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
329#endif
330
331#ifndef HAVE_STRTOUL
332#define strtoul(a, b, c)	strtol(a, b, c)
333#endif
334
335#ifndef HAVE_SNPRINTF
336int snprintf(char *, size_t, const char *, ...);
337#endif
338
339#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
340#define QUICK
341#endif
342
343#ifndef O_BINARY
344#define O_BINARY	0
345#endif
346
347#define FILE_RCSID(id) \
348static const char *rcsid(const char *p) { \
349	return rcsid(p = id); \
350}
351
352#endif /* __file_h__ */
353