file.h revision 133359
160484Sobrien/*
260484Sobrien * Copyright (c) Ian F. Darwin 1986-1995.
360484Sobrien * Software written by Ian F. Darwin and others;
460484Sobrien * maintained 1995-present by Christos Zoulas and others.
560484Sobrien *
660484Sobrien * Redistribution and use in source and binary forms, with or without
760484Sobrien * modification, are permitted provided that the following conditions
860484Sobrien * are met:
960484Sobrien * 1. Redistributions of source code must retain the above copyright
1060484Sobrien *    notice immediately at the beginning of the file, without modification,
1160484Sobrien *    this list of conditions, and the following disclaimer.
1260484Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1360484Sobrien *    notice, this list of conditions and the following disclaimer in the
1460484Sobrien *    documentation and/or other materials provided with the distribution.
1560484Sobrien * 3. All advertising materials mentioning features or use of this software
1660484Sobrien *    must display the following acknowledgement:
1760484Sobrien *    This product includes software developed by Ian F. Darwin and others.
1860484Sobrien * 4. The name of the author may not be used to endorse or promote products
1960484Sobrien *    derived from this software without specific prior written permission.
2060484Sobrien *
2160484Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2260484Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2360484Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2460484Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2560484Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2660484Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2760484Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2860484Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2960484Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3060484Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3160484Sobrien * SUCH DAMAGE.
3260484Sobrien */
3360484Sobrien/*
3460484Sobrien * file.h - definitions for file(1) program
3560484Sobrien * @(#)$Id: file.h,v 1.61 2004/05/12 14:53:01 christos Exp $
3660484Sobrien */
3760484Sobrien
3860484Sobrien#ifndef __file_h__
3960484Sobrien#define __file_h__
4060484Sobrien
4160484Sobrien#ifdef HAVE_CONFIG_H
4260484Sobrien#include <config.h>
4360484Sobrien#endif
4460484Sobrien
4560484Sobrien#include <stdio.h>	/* Include that here, to make sure __P gets defined */
4660484Sobrien#include <errno.h>
4760484Sobrien#ifdef HAVE_STDINT_H
4860484Sobrien#include <stdint.h>
4960484Sobrien#endif
5060484Sobrien#ifdef HAVE_INTTYPES_H
5160484Sobrien#include <inttypes.h>
5260484Sobrien#endif
5360484Sobrien/* Do this here and now, because struct stat gets re-defined on solaris */
5460484Sobrien#include <sys/stat.h>
5560484Sobrien
5660484Sobrien#ifndef MAGIC
5760484Sobrien#define MAGIC "/etc/magic"
5860484Sobrien#endif
5960484Sobrien
6060484Sobrien#ifdef __EMX__
6160484Sobrien#define PATHSEP	';'
6260484Sobrien#else
6360484Sobrien#define PATHSEP	':'
6460484Sobrien#endif
6560484Sobrien
6660484Sobrien#define private static
6760484Sobrien#ifndef protected
6860484Sobrien#define protected
6960484Sobrien#endif
7060484Sobrien#define public
7160484Sobrien
7260484Sobrien#ifndef HOWMANY
7360484Sobrien# define HOWMANY 65536		/* how much of the file to look at */
7460484Sobrien#endif
7560484Sobrien#define MAXMAGIS 4096		/* max entries in /etc/magic */
7660484Sobrien#define MAXDESC	64		/* max leng of text description */
7760484Sobrien#define MAXstring 32		/* max leng of "string" types */
7860484Sobrien
7960484Sobrien#define MAGICNO		0xF11E041C
8060484Sobrien#define VERSIONNO	2
8160484Sobrien#define FILE_MAGICSIZE	(32 * 4)
8260484Sobrien
8360484Sobrien#define	FILE_LOAD	0
8460484Sobrien#define FILE_CHECK	1
8560484Sobrien#define FILE_COMPILE	2
8660484Sobrien
8760484Sobrienstruct magic {
8860484Sobrien	/* Word 1 */
8960484Sobrien	uint16_t cont_level;	/* level of ">" */
9060484Sobrien	uint8_t nospflag;	/* supress space character */
9160484Sobrien	uint8_t flag;
9260484Sobrien#define INDIR	1		/* if '>(...)' appears,  */
9360484Sobrien#define	UNSIGNED 2		/* comparison is unsigned */
9460484Sobrien#define OFFADD	4		/* if '>&' appears,  */
9560484Sobrien	/* Word 2 */
9660484Sobrien	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
9760484Sobrien	uint8_t vallen;		/* length of string value, if any */
9860484Sobrien	uint8_t type;		/* int, short, long or string. */
9960484Sobrien	uint8_t in_type;	/* type of indirrection */
10060484Sobrien#define 			FILE_BYTE	1
10160484Sobrien#define				FILE_SHORT	2
10260484Sobrien#define				FILE_LONG	4
10360484Sobrien#define				FILE_STRING	5
10460484Sobrien#define				FILE_DATE	6
10560484Sobrien#define				FILE_BESHORT	7
10660484Sobrien#define				FILE_BELONG	8
10760484Sobrien#define				FILE_BEDATE	9
10860484Sobrien#define				FILE_LESHORT	10
10960484Sobrien#define				FILE_LELONG	11
11060484Sobrien#define				FILE_LEDATE	12
11160484Sobrien#define				FILE_PSTRING	13
11260484Sobrien#define				FILE_LDATE	14
11360484Sobrien#define				FILE_BELDATE	15
11460484Sobrien#define				FILE_LELDATE	16
11560484Sobrien#define				FILE_REGEX	17
11660484Sobrien
11760484Sobrien#define				FILE_FORMAT_NAME	\
118/* 0 */ 			"invalid 0",		\
119/* 1 */				"byte",			\
120/* 2 */ 			"short",		\
121/* 3 */ 			"invalid 3",		\
122/* 4 */ 			"long",			\
123/* 5 */ 			"string",		\
124/* 6 */ 			"date",			\
125/* 7 */ 			"beshort",		\
126/* 8 */ 			"belong",		\
127/* 9 */ 			"bedate"		\
128/* 10 */ 			"leshort",		\
129/* 11 */ 			"lelong",		\
130/* 12 */ 			"ledate",		\
131/* 13 */ 			"pstring",		\
132/* 14 */ 			"ldate",		\
133/* 15 */ 			"beldate",		\
134/* 16 */ 			"leldate",		\
135/* 17 */ 			"regex",
136
137#define	FILE_FMT_NUM	"cduxXi"
138#define FILE_FMT_STR	"s"
139
140#define				FILE_FORMAT_STRING	\
141/* 0 */ 			NULL,			\
142/* 1 */				FILE_FMT_NUM,		\
143/* 2 */ 			FILE_FMT_NUM,		\
144/* 3 */ 			NULL,			\
145/* 4 */ 			FILE_FMT_NUM,		\
146/* 5 */ 			FILE_FMT_STR,		\
147/* 6 */ 			FILE_FMT_STR,		\
148/* 7 */ 			FILE_FMT_NUM,		\
149/* 8 */ 			FILE_FMT_NUM,		\
150/* 9 */ 			FILE_FMT_STR,		\
151/* 10 */ 			FILE_FMT_NUM,		\
152/* 11 */ 			FILE_FMT_NUM,		\
153/* 12 */ 			FILE_FMT_STR,		\
154/* 13 */ 			FILE_FMT_STR,		\
155/* 14 */ 			FILE_FMT_STR,		\
156/* 15 */ 			FILE_FMT_STR,		\
157/* 16 */ 			FILE_FMT_STR,		\
158/* 17 */ 			FILE_FMT_STR,
159
160	/* Word 3 */
161	uint8_t in_op;		/* operator for indirection */
162	uint8_t mask_op;	/* operator for mask */
163	uint8_t dummy1;
164	uint8_t dummy2;
165#define				FILE_OPS	"&|^+-*/%"
166#define				FILE_OPAND	0
167#define				FILE_OPOR	1
168#define				FILE_OPXOR	2
169#define				FILE_OPADD	3
170#define				FILE_OPMINUS	4
171#define				FILE_OPMULTIPLY	5
172#define				FILE_OPDIVIDE	6
173#define				FILE_OPMODULO	7
174#define				FILE_OPINVERSE	0x80
175	/* Word 4 */
176	uint32_t offset;	/* offset to magic number */
177	/* Word 5 */
178	uint32_t in_offset;	/* offset from indirection */
179	/* Word 6 */
180	uint32_t mask;	/* mask before comparison with value */
181	/* Word 7 */
182	uint32_t dummy3;
183	/* Word 8 */
184	uint32_t dummp4;
185	/* Words 9-16 */
186	union VALUETYPE {
187		uint8_t b;
188		uint16_t h;
189		uint32_t l;
190		char s[MAXstring];
191		char *buf;
192		uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
193		uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
194	} value;		/* either number or string */
195	/* Words 17..31 */
196	char desc[MAXDESC];	/* description */
197};
198
199#define BIT(A)   (1 << (A))
200#define STRING_IGNORE_LOWERCASE		BIT(0)
201#define STRING_COMPACT_BLANK		BIT(1)
202#define STRING_COMPACT_OPTIONAL_BLANK	BIT(2)
203#define CHAR_IGNORE_LOWERCASE		'c'
204#define CHAR_COMPACT_BLANK		'B'
205#define CHAR_COMPACT_OPTIONAL_BLANK	'b'
206
207
208/* list of magic entries */
209struct mlist {
210	struct magic *magic;		/* array of magic entries */
211	uint32_t nmagic;			/* number of entries in array */
212	int mapped;  /* allocation type: 0 => apprentice_file
213		      *                  1 => apprentice_map + malloc
214		      *                  2 => apprentice_map + mmap */
215	struct mlist *next, *prev;
216};
217
218struct magic_set {
219    struct mlist *mlist;
220    struct cont {
221	size_t len;
222	int32_t *off;
223    } c;
224    struct out {
225	/* Accumulation buffer */
226	char *buf;
227	char *ptr;
228	size_t len;
229	size_t size;
230	/* Printable buffer */
231	char *pbuf;
232	size_t psize;
233    } o;
234    int error;
235    int flags;
236    int haderr;
237};
238
239struct stat;
240protected char *file_fmttime(uint32_t, int);
241protected int file_buffer(struct magic_set *, const void *, size_t);
242protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
243protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
244protected int file_printf(struct magic_set *, const char *, ...);
245protected int file_reset(struct magic_set *);
246protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
247protected int file_zmagic(struct magic_set *, const unsigned char *, size_t);
248protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
249protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
250protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
251protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
252protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t);
253protected void file_delmagic(struct magic *, int type, size_t entries);
254protected void file_badread(struct magic_set *);
255protected void file_badseek(struct magic_set *);
256protected void file_oomem(struct magic_set *);
257protected void file_error(struct magic_set *, int, const char *, ...);
258protected void file_magwarn(const char *, ...);
259protected void file_mdump(struct magic *);
260protected void file_showstr(FILE *, const char *, size_t);
261protected size_t file_mbswidth(const char *);
262protected const char *file_getbuffer(struct magic_set *);
263
264#ifndef HAVE_STRERROR
265extern int sys_nerr;
266extern char *sys_errlist[];
267#define strerror(e) \
268	(((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
269#endif
270
271#ifndef HAVE_STRTOUL
272#define strtoul(a, b, c)	strtol(a, b, c)
273#endif
274
275#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
276#define QUICK
277#endif
278
279#define FILE_RCSID(id) \
280static const char *rcsid(const char *p) { \
281	return rcsid(p = id); \
282}
283#else
284
285#endif /* __file_h__ */
286