file.h revision 110949
168349Sobrien/*
268349Sobrien * file.h - definitions for file(1) program
3110949Sobrien * @(#)$Id: file.h,v 1.45 2003/02/08 18:33:53 christos Exp $
468349Sobrien *
568349Sobrien * Copyright (c) Ian F. Darwin, 1987.
668349Sobrien * Written by Ian F. Darwin.
768349Sobrien *
868349Sobrien * This software is not subject to any license of the American Telephone
968349Sobrien * and Telegraph Company or of the Regents of the University of California.
1068349Sobrien *
1168349Sobrien * Permission is granted to anyone to use this software for any purpose on
1268349Sobrien * any computer system, and to alter it and redistribute it freely, subject
1368349Sobrien * to the following restrictions:
1468349Sobrien *
1568349Sobrien * 1. The author is not responsible for the consequences of use of this
1668349Sobrien *    software, no matter how awful, even if they arise from flaws in it.
1768349Sobrien *
1868349Sobrien * 2. The origin of this software must not be misrepresented, either by
1968349Sobrien *    explicit claim or by omission.  Since few users ever read sources,
2068349Sobrien *    credits must appear in the documentation.
2168349Sobrien *
2268349Sobrien * 3. Altered versions must be plainly marked as such, and must not be
2368349Sobrien *    misrepresented as being the original software.  Since few users
2468349Sobrien *    ever read sources, credits must appear in the documentation.
2568349Sobrien *
2668349Sobrien * 4. This notice may not be removed or altered.
2768349Sobrien */
2868349Sobrien
2968349Sobrien#ifndef __file_h__
3068349Sobrien#define __file_h__
3168349Sobrien
32103373Sobrien#ifndef __linux__
33103373Sobrien#define _LARGEFILE_SOURCE
34103373Sobrien#define _LARGEFILE64_SOURCE
35103373Sobrien#define _FILE_OFFSET_BITS 64
36103373Sobrien#endif
37103373Sobrien
3868349Sobrien#ifdef HAVE_CONFIG_H
3968349Sobrien#include <config.h>
4068349Sobrien#endif
4168349Sobrien
42103373Sobrien#include <errno.h>
43103373Sobrien#include <stdio.h>
44103373Sobrien#ifdef HAVE_STDINT_H
45103373Sobrien#include <stdint.h>
46110949Sobrien#elif defined(HAVE_INTTYPES_H)
47110949Sobrien#include <inttypes.h>
48103373Sobrien#endif
49103373Sobrien/* Do this here and now, because struct stat gets re-defined on solaris */
50103373Sobrien#include <sys/stat.h>
5168349Sobrien
5268349Sobrien#ifndef HOWMANY
53103373Sobrien# define HOWMANY 65536		/* how much of the file to look at */
5468349Sobrien#endif
55103373Sobrien#define MAXMAGIS 4096		/* max entries in /etc/magic */
5668349Sobrien#define MAXDESC	50		/* max leng of text description */
5768349Sobrien#define MAXstring 32		/* max leng of "string" types */
5868349Sobrien
5974784Sobrien#define MAGICNO		0xF11E041C
6074784Sobrien#define VERSIONNO	1
6174784Sobrien
6274784Sobrien#define CHECK	1
6374784Sobrien#define COMPILE	2
6474784Sobrien
65110949Sobrien#ifndef __GNUC__
66110949Sobrien#define __attribute__(a)
67110949Sobrien#endif
68110949Sobrien
6968349Sobrienstruct magic {
70103373Sobrien	uint16_t cont_level;	/* level of ">" */
71103373Sobrien	uint8_t nospflag;	/* supress space character */
72103373Sobrien	uint8_t flag;
7368349Sobrien#define INDIR	1		/* if '>(...)' appears,  */
7468349Sobrien#define	UNSIGNED 2		/* comparison is unsigned */
7580588Sobrien#define OFFADD	4		/* if '>&' appears,  */
76103373Sobrien	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
77103373Sobrien	uint8_t vallen;		/* length of string value, if any */
78103373Sobrien	uint8_t type;		/* int, short, long or string. */
79103373Sobrien	uint8_t in_type;	/* type of indirrection */
8068349Sobrien#define 			BYTE	1
8168349Sobrien#define				SHORT	2
8268349Sobrien#define				LONG	4
8368349Sobrien#define				STRING	5
8468349Sobrien#define				DATE	6
8568349Sobrien#define				BESHORT	7
8668349Sobrien#define				BELONG	8
8768349Sobrien#define				BEDATE	9
8868349Sobrien#define				LESHORT	10
8968349Sobrien#define				LELONG	11
9068349Sobrien#define				LEDATE	12
9180588Sobrien#define				PSTRING	13
9280588Sobrien#define				LDATE	14
9380588Sobrien#define				BELDATE	15
9480588Sobrien#define				LELDATE	16
95103373Sobrien#define				REGEX	17
96103373Sobrien	uint8_t in_op;		/* operator for indirection */
97103373Sobrien	uint8_t mask_op;	/* operator for mask */
9880588Sobrien#define				OPAND	1
9980588Sobrien#define				OPOR	2
10080588Sobrien#define				OPXOR	3
10180588Sobrien#define				OPADD	4
10280588Sobrien#define				OPMINUS	5
10380588Sobrien#define				OPMULTIPLY	6
10480588Sobrien#define				OPDIVIDE	7
10580588Sobrien#define				OPMODULO	8
10680588Sobrien#define				OPINVERSE	0x80
107103373Sobrien	int32_t offset;		/* offset to magic number */
108103373Sobrien	int32_t in_offset;	/* offset from indirection */
10968349Sobrien	union VALUETYPE {
110103373Sobrien		uint8_t b;
111103373Sobrien		uint16_t h;
112103373Sobrien		uint32_t l;
11368349Sobrien		char s[MAXstring];
114103373Sobrien		char *buf;
115103373Sobrien		uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
116103373Sobrien		uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
11768349Sobrien	} value;		/* either number or string */
118103373Sobrien	uint32_t mask;	/* mask before comparison with value */
11968349Sobrien	char desc[MAXDESC];	/* description */
120103373Sobrien} __attribute__((__packed__));
12168349Sobrien
12268349Sobrien#define BIT(A)   (1 << (A))
12368349Sobrien#define STRING_IGNORE_LOWERCASE		BIT(0)
12468349Sobrien#define STRING_COMPACT_BLANK		BIT(1)
12568349Sobrien#define STRING_COMPACT_OPTIONAL_BLANK	BIT(2)
12668349Sobrien#define CHAR_IGNORE_LOWERCASE		'c'
12768349Sobrien#define CHAR_COMPACT_BLANK		'B'
12868349Sobrien#define CHAR_COMPACT_OPTIONAL_BLANK	'b'
12968349Sobrien
13068349Sobrien
13174784Sobrien/* list of magic entries */
13274784Sobrienstruct mlist {
13374784Sobrien	struct magic *magic;		/* array of magic entries */
134103373Sobrien	uint32_t nmagic;		/* number of entries in array */
13574784Sobrien	struct mlist *next, *prev;
13674784Sobrien};
13774784Sobrien
138103373Sobrienextern int   apprentice(const char *, int);
139103373Sobrienextern int   ascmagic(unsigned char *, int);
140103373Sobrienextern void  error(const char *, ...);
141103373Sobrienextern void  ckfputs(const char *, FILE *);
142103373Sobrienstruct stat;
143103373Sobrienextern int   fsmagic(const char *, struct stat *);
144103373Sobrienextern char *fmttime(long, int);
145103373Sobrienextern int   is_compress(const unsigned char *, int *);
146103373Sobrienextern int   is_tar(unsigned char *, int);
147103373Sobrienextern void  magwarn(const char *, ...);
148103373Sobrienextern void  mdump(struct magic *);
149103373Sobrienextern void  process(const char *, int);
150103373Sobrienextern void  showstr(FILE *, const char *, int);
151103373Sobrienextern int   softmagic(unsigned char *, int);
152103373Sobrienextern int   tryit(const char *, unsigned char *, int, int);
153103373Sobrienextern int   zmagic(const char *, unsigned char *, int);
154103373Sobrienextern void  ckfprintf(FILE *, const char *, ...);
155103373Sobrienextern uint32_t signextend(struct magic *, unsigned int32);
156103373Sobrienextern void tryelf(int, unsigned char *, int);
157103373Sobrienextern int pipe2file(int, void *, size_t);
15868349Sobrien
15968349Sobrien
16068349Sobrienextern char *progname;		/* the program name 			*/
16168349Sobrienextern const char *magicfile;	/* name of the magic file		*/
16268349Sobrienextern int lineno;		/* current line number in magic file	*/
16368349Sobrien
16474784Sobrienextern struct mlist mlist;	/* list of arrays of magic entries	*/
16568349Sobrien
16668349Sobrienextern int debug;		/* enable debugging?			*/
16768349Sobrienextern int zflag;		/* process compressed files?		*/
16868349Sobrienextern int lflag;		/* follow symbolic links?		*/
16968349Sobrienextern int sflag;		/* read/analyze block special files?	*/
17068349Sobrienextern int iflag;		/* Output types as mime-types		*/
17168349Sobrien
172103373Sobrien#ifdef NEED_GETOPT
17368349Sobrienextern int optind;		/* From getopt(3)			*/
17468349Sobrienextern char *optarg;
175103373Sobrien#endif
17668349Sobrien
17768349Sobrien#ifndef HAVE_STRERROR
17868349Sobrienextern int sys_nerr;
17968349Sobrienextern char *sys_errlist[];
18068349Sobrien#define strerror(e) \
18168349Sobrien	(((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
18268349Sobrien#endif
18368349Sobrien
18468349Sobrien#ifndef HAVE_STRTOUL
18568349Sobrien#define strtoul(a, b, c)	strtol(a, b, c)
18668349Sobrien#endif
18768349Sobrien
18880588Sobrien#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
18980588Sobrien#define QUICK
19080588Sobrien#endif
19180588Sobrien
19268349Sobrien#define FILE_RCSID(id) \
19368349Sobrienstatic const char *rcsid(const char *p) { \
19468349Sobrien	return rcsid(p = id); \
19568349Sobrien}
19668349Sobrien
19768349Sobrien#endif /* __file_h__ */
198