xref: /openbsd/usr.bin/file/file.h (revision db3296cf)
1 /*	$OpenBSD: file.h,v 1.14 2003/07/02 21:04:10 deraadt Exp $	*/
2 
3 /*
4  * file.h - definitions for file(1) program
5  *
6  * Copyright (c) Ian F. Darwin 1986-1995.
7  * Software written by Ian F. Darwin and others;
8  * maintained 1995-present by Christos Zoulas and others.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice immediately at the beginning of the file, without modification,
15  *    this list of conditions, and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
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 <errno.h>
41 #include <stdio.h>
42 #ifdef HAVE_STDINT_H
43 #include <stdint.h>
44 #elif defined(HAVE_INTTYPES_H)
45 #include <inttypes.h>
46 #endif
47 
48 #ifndef HOWMANY
49 # define HOWMANY 8192		/* how much of the file to look at */
50 #endif
51 #define MAXMAGIS 5000		/* max entries in /etc/magic */
52 #define MAXDESC	50		/* max leng of text description */
53 #define MAXstring 32		/* max leng of "string" types */
54 
55 struct magic {
56 	short flag;
57 #define INDIR	1		/* if '>(...)' appears,  */
58 #define	UNSIGNED 2		/* comparison is unsigned */
59 #define ADD	4		/* if '>&' appears,  */
60 	short cont_level;	/* level of ">" */
61 	struct {
62 		int8_t type;	/* byte short long */
63 		int32_t offset;	/* offset from indirection */
64 	} in;
65 	int32_t offset;		/* offset to magic number */
66 	unsigned char reln;	/* relation (0=eq, '>'=gt, etc) */
67 	int8_t type;		/* int, short, long or string. */
68 	char vallen;		/* length of string value, if any */
69 #define 			BYTE	1
70 #define				SHORT	2
71 #define				LONG	4
72 #define				STRING	5
73 #define				DATE	6
74 #define				BESHORT	7
75 #define				BELONG	8
76 #define				BEDATE	9
77 #define				LESHORT	10
78 #define				LELONG	11
79 #define				LEDATE	12
80 	union VALUETYPE {
81 		unsigned char b;
82 		unsigned short h;
83 		uint32_t l;
84 		char s[MAXstring];
85 		unsigned char hs[2];	/* 2 bytes of a fixed-endian "short" */
86 		unsigned char hl[4];	/* 2 bytes of a fixed-endian "long" */
87 	} value;		/* either number or string */
88 	uint32_t mask;	/* mask before comparison with value */
89 	char nospflag;		/* supress space character */
90 	char desc[MAXDESC];	/* description */
91 };
92 
93 extern int   apprentice(char *, int);
94 extern int   ascmagic(unsigned char *, int);
95 extern void  ckfputs(const char *, FILE *);
96 struct stat;
97 extern int   fsmagic(const char *, struct stat *);
98 extern int   is_compress(const unsigned char *, int *);
99 extern int   is_tar(unsigned char *, int);
100 extern void  mdump(struct magic *);
101 extern void  process(const char *, int);
102 extern void  showstr(FILE *, const char *, int);
103 extern int   softmagic(unsigned char *, int);
104 extern int   tryit(unsigned char *, int, int);
105 extern int   zmagic(unsigned char *, int);
106 extern void  ckfprintf(FILE *, const char *, ...);
107 extern uint32_t signextend(struct magic *, uint32_t);
108 extern int internatmagic(unsigned char *, int);
109 extern void tryelf(int, unsigned char *, int);
110 
111 
112 extern int errno;		/* Some unixes don't define this..	*/
113 
114 extern char *progname;		/* the program name 			*/
115 extern char *magicfile;		/* name of the magic file		*/
116 extern int lineno;		/* current line number in magic file	*/
117 
118 extern struct magic *magic;	/* array of magic entries		*/
119 extern int nmagic;		/* number of valid magic[]s 		*/
120 
121 
122 extern int debug;		/* enable debugging?			*/
123 extern int zflag;		/* process compressed files?		*/
124 extern int lflag;		/* follow symbolic links?		*/
125 
126 extern int optind;		/* From getopt(3)			*/
127 extern char *optarg;
128 
129 #if defined(sun) || defined(__sun__) || defined (__sun)
130 # if defined(__svr4) || defined (__SVR4) || defined(__svr4__)
131 #  define SOLARIS
132 # else
133 #  define SUNOS
134 # endif
135 #endif
136 
137 
138 #if !defined(__STDC__) || defined(SUNOS) || defined(__convex__)
139 extern int sys_nerr;
140 extern char *sys_errlist[];
141 #define strerror(e) \
142 	(((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
143 #define strtoul(a, b, c)	strtol(a, b, c)
144 #endif
145 
146 #ifndef MAXPATHLEN
147 #define	MAXPATHLEN	512
148 #endif
149 
150 int	pipe2file(int, void *, size_t);
151 void	error(const char *, ...);
152 
153 #endif /* __file_h__ */
154