file.h revision 139368
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.64 2004/11/20 23:50:12 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#ifdef HAVE_STDINT_H
43#include <stdint.h>
44#endif
45#ifdef HAVE_INTTYPES_H
46#include <inttypes.h>
47#endif
48/* Do this here and now, because struct stat gets re-defined on solaris */
49#include <sys/stat.h>
50
51#ifndef MAGIC
52#define MAGIC "/etc/magic"
53#endif
54
55#ifdef __EMX__
56#define PATHSEP	';'
57#else
58#define PATHSEP	':'
59#endif
60
61#define private static
62#ifndef protected
63#define protected
64#endif
65#define public
66
67#ifndef HOWMANY
68# define HOWMANY 65536		/* how much of the file to look at */
69#endif
70#define MAXMAGIS 4096		/* max entries in /etc/magic */
71#define MAXDESC	64		/* max leng of text description */
72#define MAXstring 32		/* max leng of "string" types */
73
74#define MAGICNO		0xF11E041C
75#define VERSIONNO	2
76#define FILE_MAGICSIZE	(32 * 4)
77
78#define	FILE_LOAD	0
79#define FILE_CHECK	1
80#define FILE_COMPILE	2
81
82struct magic {
83	/* Word 1 */
84	uint16_t cont_level;	/* level of ">" */
85	uint8_t nospflag;	/* supress space character */
86	uint8_t flag;
87#define INDIR	1		/* if '>(...)' appears,  */
88#define	UNSIGNED 2		/* comparison is unsigned */
89#define OFFADD	4		/* if '>&' appears,  */
90	/* Word 2 */
91	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
92	uint8_t vallen;		/* length of string value, if any */
93	uint8_t type;		/* int, short, long or string. */
94	uint8_t in_type;	/* type of indirrection */
95#define 			FILE_BYTE	1
96#define				FILE_SHORT	2
97#define				FILE_LONG	4
98#define				FILE_STRING	5
99#define				FILE_DATE	6
100#define				FILE_BESHORT	7
101#define				FILE_BELONG	8
102#define				FILE_BEDATE	9
103#define				FILE_LESHORT	10
104#define				FILE_LELONG	11
105#define				FILE_LEDATE	12
106#define				FILE_PSTRING	13
107#define				FILE_LDATE	14
108#define				FILE_BELDATE	15
109#define				FILE_LELDATE	16
110#define				FILE_REGEX	17
111#define				FILE_BESTRING16	18
112#define				FILE_LESTRING16	19
113
114#define				FILE_FORMAT_NAME	\
115/* 0 */ 			"invalid 0",		\
116/* 1 */				"byte",			\
117/* 2 */ 			"short",		\
118/* 3 */ 			"invalid 3",		\
119/* 4 */ 			"long",			\
120/* 5 */ 			"string",		\
121/* 6 */ 			"date",			\
122/* 7 */ 			"beshort",		\
123/* 8 */ 			"belong",		\
124/* 9 */ 			"bedate"		\
125/* 10 */ 			"leshort",		\
126/* 11 */ 			"lelong",		\
127/* 12 */ 			"ledate",		\
128/* 13 */ 			"pstring",		\
129/* 14 */ 			"ldate",		\
130/* 15 */ 			"beldate",		\
131/* 16 */ 			"leldate",		\
132/* 17 */ 			"regex",		\
133/* 18 */			"bestring16",		\
134/* 19 */			"lestring16",
135
136#define	FILE_FMT_NUM	"cduxXi"
137#define FILE_FMT_STR	"s"
138
139#define				FILE_FORMAT_STRING	\
140/* 0 */ 			NULL,			\
141/* 1 */				FILE_FMT_NUM,		\
142/* 2 */ 			FILE_FMT_NUM,		\
143/* 3 */ 			NULL,			\
144/* 4 */ 			FILE_FMT_NUM,		\
145/* 5 */ 			FILE_FMT_STR,		\
146/* 6 */ 			FILE_FMT_STR,		\
147/* 7 */ 			FILE_FMT_NUM,		\
148/* 8 */ 			FILE_FMT_NUM,		\
149/* 9 */ 			FILE_FMT_STR,		\
150/* 10 */ 			FILE_FMT_NUM,		\
151/* 11 */ 			FILE_FMT_NUM,		\
152/* 12 */ 			FILE_FMT_STR,		\
153/* 13 */ 			FILE_FMT_STR,		\
154/* 14 */ 			FILE_FMT_STR,		\
155/* 15 */ 			FILE_FMT_STR,		\
156/* 16 */ 			FILE_FMT_STR,		\
157/* 17 */ 			FILE_FMT_STR,		\
158/* 18 */			FILE_FMT_STR,		\
159/* 19 */			FILE_FMT_STR,
160
161	/* Word 3 */
162	uint8_t in_op;		/* operator for indirection */
163	uint8_t mask_op;	/* operator for mask */
164	uint8_t dummy1;
165	uint8_t dummy2;
166#define				FILE_OPS	"&|^+-*/%"
167#define				FILE_OPAND	0
168#define				FILE_OPOR	1
169#define				FILE_OPXOR	2
170#define				FILE_OPADD	3
171#define				FILE_OPMINUS	4
172#define				FILE_OPMULTIPLY	5
173#define				FILE_OPDIVIDE	6
174#define				FILE_OPMODULO	7
175#define				FILE_OPINVERSE	0x80
176	/* Word 4 */
177	uint32_t offset;	/* offset to magic number */
178	/* Word 5 */
179	uint32_t in_offset;	/* offset from indirection */
180	/* Word 6 */
181	uint32_t mask;	/* mask before comparison with value */
182	/* Word 7 */
183	uint32_t dummy3;
184	/* Word 8 */
185	uint32_t dummp4;
186	/* Words 9-16 */
187	union VALUETYPE {
188		uint8_t b;
189		uint16_t h;
190		uint32_t l;
191		char s[MAXstring];
192		char *buf;
193		uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
194		uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
195	} value;		/* either number or string */
196	/* Words 17..31 */
197	char desc[MAXDESC];	/* description */
198};
199
200#define BIT(A)   (1 << (A))
201#define STRING_IGNORE_LOWERCASE		BIT(0)
202#define STRING_COMPACT_BLANK		BIT(1)
203#define STRING_COMPACT_OPTIONAL_BLANK	BIT(2)
204#define CHAR_IGNORE_LOWERCASE		'c'
205#define CHAR_COMPACT_BLANK		'B'
206#define CHAR_COMPACT_OPTIONAL_BLANK	'b'
207
208
209/* list of magic entries */
210struct mlist {
211	struct magic *magic;		/* array of magic entries */
212	uint32_t nmagic;			/* number of entries in array */
213	int mapped;  /* allocation type: 0 => apprentice_file
214		      *                  1 => apprentice_map + malloc
215		      *                  2 => apprentice_map + mmap */
216	struct mlist *next, *prev;
217};
218
219struct magic_set {
220    struct mlist *mlist;
221    struct cont {
222	size_t len;
223	int32_t *off;
224    } c;
225    struct out {
226	/* Accumulation buffer */
227	char *buf;
228	char *ptr;
229	size_t len;
230	size_t size;
231	/* Printable buffer */
232	char *pbuf;
233	size_t psize;
234    } o;
235    int error;
236    int flags;
237    int haderr;
238    const char *file;
239    size_t line;
240};
241
242struct stat;
243protected char *file_fmttime(uint32_t, int);
244protected int file_buffer(struct magic_set *, const void *, size_t);
245protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
246protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
247protected int file_printf(struct magic_set *, const char *, ...);
248protected int file_reset(struct magic_set *);
249protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
250protected int file_zmagic(struct magic_set *, const unsigned char *, size_t);
251protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
252protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
253protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
254protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
255protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t);
256protected void file_delmagic(struct magic *, int type, size_t entries);
257protected void file_badread(struct magic_set *);
258protected void file_badseek(struct magic_set *);
259protected void file_oomem(struct magic_set *);
260protected void file_error(struct magic_set *, int, const char *, ...);
261protected void file_magwarn(struct magic_set *, const char *, ...);
262protected void file_mdump(struct magic *);
263protected void file_showstr(FILE *, const char *, size_t);
264protected size_t file_mbswidth(const char *);
265protected const char *file_getbuffer(struct magic_set *);
266
267#ifndef HAVE_STRERROR
268extern int sys_nerr;
269extern char *sys_errlist[];
270#define strerror(e) \
271	(((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
272#endif
273
274#ifndef HAVE_STRTOUL
275#define strtoul(a, b, c)	strtol(a, b, c)
276#endif
277
278#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
279#define QUICK
280#endif
281
282#define FILE_RCSID(id) \
283static const char *rcsid(const char *p) { \
284	return rcsid(p = id); \
285}
286#else
287
288#endif /* __file_h__ */
289