file.h revision 110949
1/*
2 * file.h - definitions for file(1) program
3 * @(#)$Id: file.h,v 1.45 2003/02/08 18:33:53 christos Exp $
4 *
5 * Copyright (c) Ian F. Darwin, 1987.
6 * Written by Ian F. Darwin.
7 *
8 * This software is not subject to any license of the American Telephone
9 * and Telegraph Company or of the Regents of the University of California.
10 *
11 * Permission is granted to anyone to use this software for any purpose on
12 * any computer system, and to alter it and redistribute it freely, subject
13 * to the following restrictions:
14 *
15 * 1. The author is not responsible for the consequences of use of this
16 *    software, no matter how awful, even if they arise from flaws in it.
17 *
18 * 2. The origin of this software must not be misrepresented, either by
19 *    explicit claim or by omission.  Since few users ever read sources,
20 *    credits must appear in the documentation.
21 *
22 * 3. Altered versions must be plainly marked as such, and must not be
23 *    misrepresented as being the original software.  Since few users
24 *    ever read sources, credits must appear in the documentation.
25 *
26 * 4. This notice may not be removed or altered.
27 */
28
29#ifndef __file_h__
30#define __file_h__
31
32#ifndef __linux__
33#define _LARGEFILE_SOURCE
34#define _LARGEFILE64_SOURCE
35#define _FILE_OFFSET_BITS 64
36#endif
37
38#ifdef HAVE_CONFIG_H
39#include <config.h>
40#endif
41
42#include <errno.h>
43#include <stdio.h>
44#ifdef HAVE_STDINT_H
45#include <stdint.h>
46#elif defined(HAVE_INTTYPES_H)
47#include <inttypes.h>
48#endif
49/* Do this here and now, because struct stat gets re-defined on solaris */
50#include <sys/stat.h>
51
52#ifndef HOWMANY
53# define HOWMANY 65536		/* how much of the file to look at */
54#endif
55#define MAXMAGIS 4096		/* max entries in /etc/magic */
56#define MAXDESC	50		/* max leng of text description */
57#define MAXstring 32		/* max leng of "string" types */
58
59#define MAGICNO		0xF11E041C
60#define VERSIONNO	1
61
62#define CHECK	1
63#define COMPILE	2
64
65#ifndef __GNUC__
66#define __attribute__(a)
67#endif
68
69struct magic {
70	uint16_t cont_level;	/* level of ">" */
71	uint8_t nospflag;	/* supress space character */
72	uint8_t flag;
73#define INDIR	1		/* if '>(...)' appears,  */
74#define	UNSIGNED 2		/* comparison is unsigned */
75#define OFFADD	4		/* if '>&' appears,  */
76	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
77	uint8_t vallen;		/* length of string value, if any */
78	uint8_t type;		/* int, short, long or string. */
79	uint8_t in_type;	/* type of indirrection */
80#define 			BYTE	1
81#define				SHORT	2
82#define				LONG	4
83#define				STRING	5
84#define				DATE	6
85#define				BESHORT	7
86#define				BELONG	8
87#define				BEDATE	9
88#define				LESHORT	10
89#define				LELONG	11
90#define				LEDATE	12
91#define				PSTRING	13
92#define				LDATE	14
93#define				BELDATE	15
94#define				LELDATE	16
95#define				REGEX	17
96	uint8_t in_op;		/* operator for indirection */
97	uint8_t mask_op;	/* operator for mask */
98#define				OPAND	1
99#define				OPOR	2
100#define				OPXOR	3
101#define				OPADD	4
102#define				OPMINUS	5
103#define				OPMULTIPLY	6
104#define				OPDIVIDE	7
105#define				OPMODULO	8
106#define				OPINVERSE	0x80
107	int32_t offset;		/* offset to magic number */
108	int32_t in_offset;	/* offset from indirection */
109	union VALUETYPE {
110		uint8_t b;
111		uint16_t h;
112		uint32_t l;
113		char s[MAXstring];
114		char *buf;
115		uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
116		uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
117	} value;		/* either number or string */
118	uint32_t mask;	/* mask before comparison with value */
119	char desc[MAXDESC];	/* description */
120} __attribute__((__packed__));
121
122#define BIT(A)   (1 << (A))
123#define STRING_IGNORE_LOWERCASE		BIT(0)
124#define STRING_COMPACT_BLANK		BIT(1)
125#define STRING_COMPACT_OPTIONAL_BLANK	BIT(2)
126#define CHAR_IGNORE_LOWERCASE		'c'
127#define CHAR_COMPACT_BLANK		'B'
128#define CHAR_COMPACT_OPTIONAL_BLANK	'b'
129
130
131/* list of magic entries */
132struct mlist {
133	struct magic *magic;		/* array of magic entries */
134	uint32_t nmagic;		/* number of entries in array */
135	struct mlist *next, *prev;
136};
137
138extern int   apprentice(const char *, int);
139extern int   ascmagic(unsigned char *, int);
140extern void  error(const char *, ...);
141extern void  ckfputs(const char *, FILE *);
142struct stat;
143extern int   fsmagic(const char *, struct stat *);
144extern char *fmttime(long, int);
145extern int   is_compress(const unsigned char *, int *);
146extern int   is_tar(unsigned char *, int);
147extern void  magwarn(const char *, ...);
148extern void  mdump(struct magic *);
149extern void  process(const char *, int);
150extern void  showstr(FILE *, const char *, int);
151extern int   softmagic(unsigned char *, int);
152extern int   tryit(const char *, unsigned char *, int, int);
153extern int   zmagic(const char *, unsigned char *, int);
154extern void  ckfprintf(FILE *, const char *, ...);
155extern uint32_t signextend(struct magic *, unsigned int32);
156extern void tryelf(int, unsigned char *, int);
157extern int pipe2file(int, void *, size_t);
158
159
160extern char *progname;		/* the program name 			*/
161extern const char *magicfile;	/* name of the magic file		*/
162extern int lineno;		/* current line number in magic file	*/
163
164extern struct mlist mlist;	/* list of arrays of magic entries	*/
165
166extern int debug;		/* enable debugging?			*/
167extern int zflag;		/* process compressed files?		*/
168extern int lflag;		/* follow symbolic links?		*/
169extern int sflag;		/* read/analyze block special files?	*/
170extern int iflag;		/* Output types as mime-types		*/
171
172#ifdef NEED_GETOPT
173extern int optind;		/* From getopt(3)			*/
174extern char *optarg;
175#endif
176
177#ifndef HAVE_STRERROR
178extern int sys_nerr;
179extern char *sys_errlist[];
180#define strerror(e) \
181	(((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
182#endif
183
184#ifndef HAVE_STRTOUL
185#define strtoul(a, b, c)	strtol(a, b, c)
186#endif
187
188#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
189#define QUICK
190#endif
191
192#define FILE_RCSID(id) \
193static const char *rcsid(const char *p) { \
194	return rcsid(p = id); \
195}
196
197#endif /* __file_h__ */
198