1 /**	$MirOS: src/usr.bin/file/file.h,v 1.3 2013/10/31 20:07:02 tg Exp $ */
2 /*	$OpenBSD: file.h,v 1.17 2007/07/09 16:39:48 dim Exp $ */
3 /*
4  * Copyright © 2013
5  *	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
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  * file.h - definitions for file(1) program
34  * @(#)$Id: file.h,v 1.17 2007/07/09 16:39:48 dim Exp $
35  */
36 
37 #ifndef __file_h__
38 #define __file_h__
39 
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43 
44 #include <stdio.h>	/* Include that here, to make sure __P gets defined */
45 #include <errno.h>
46 #ifdef HAVE_STDINT_H
47 #include <stdint.h>
48 #endif
49 #ifdef HAVE_INTTYPES_H
50 #include <inttypes.h>
51 #endif
52 /* Do this here and now, because struct stat gets re-defined on solaris */
53 #include <sys/stat.h>
54 
55 #ifndef MAGIC
56 #define MAGIC "/etc/magic"
57 #endif
58 
59 #ifdef __EMX__
60 #define PATHSEP	';'
61 #else
62 #define PATHSEP	':'
63 #endif
64 
65 #define private static
66 #ifndef protected
67 #define protected
68 #endif
69 #define public
70 
71 #ifndef HOWMANY
72 # define HOWMANY 65536		/* how much of the file to look at */
73 #endif
74 #define MAXMAGIS 4096		/* max entries in /etc/magic */
75 #define MAXDESC	64		/* max leng of text description */
76 #define MAXstring 32		/* max leng of "string" types */
77 
78 #define MAGICNO		0xF11E041C
79 #define VERSIONNO	2
80 #define FILE_MAGICSIZE	(32 * 4)
81 
82 #define	FILE_LOAD	0
83 #define FILE_CHECK	1
84 #define FILE_COMPILE	2
85 
86 struct magic {
87 	/* Word 1 */
88 	uint16_t cont_level;	/* level of ">" */
89 	uint8_t nospflag;	/* supress space character */
90 	uint8_t flag;
91 #define INDIR	1		/* if '>(...)' appears,  */
92 #define	UNSIGNED 2		/* comparison is unsigned */
93 #define OFFADD	4		/* if '>&' appears,  */
94 	/* Word 2 */
95 	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
96 	uint8_t vallen;		/* length of string value, if any */
97 	uint8_t type;		/* int, short, long or string. */
98 	uint8_t in_type;	/* type of indirrection */
99 #define 			FILE_BYTE	1
100 #define				FILE_SHORT	2
101 #define				FILE_LONG	4
102 #define				FILE_STRING	5
103 #define				FILE_DATE	6
104 #define				FILE_BESHORT	7
105 #define				FILE_BELONG	8
106 #define				FILE_BEDATE	9
107 #define				FILE_LESHORT	10
108 #define				FILE_LELONG	11
109 #define				FILE_LEDATE	12
110 #define				FILE_PSTRING	13
111 #define				FILE_LDATE	14
112 #define				FILE_BELDATE	15
113 #define				FILE_LELDATE	16
114 #define				FILE_REGEX	17
115 	/* Word 3 */
116 	uint8_t in_op;		/* operator for indirection */
117 	uint8_t mask_op;	/* operator for mask */
118 	uint8_t dummy1;
119 	uint8_t dummy2;
120 #define				FILE_OPS	"&|^+-*/%"
121 #define				FILE_OPAND	0
122 #define				FILE_OPOR	1
123 #define				FILE_OPXOR	2
124 #define				FILE_OPADD	3
125 #define				FILE_OPMINUS	4
126 #define				FILE_OPMULTIPLY	5
127 #define				FILE_OPDIVIDE	6
128 #define				FILE_OPMODULO	7
129 #define				FILE_OPINVERSE	0x80
130 	/* Word 4 */
131 	uint32_t offset;	/* offset to magic number */
132 	/* Word 5 */
133 	uint32_t in_offset;	/* offset from indirection */
134 	/* Word 6 */
135 	uint32_t mask;	/* mask before comparison with value */
136 	/* Word 7 */
137 	uint32_t dummy3;
138 	/* Word 8 */
139 	uint32_t dummp4;
140 	/* Words 9-16 */
141 	union VALUETYPE {
142 		uint8_t b;
143 		uint16_t h;
144 		uint32_t l;
145 		char s[MAXstring];
146 		char *buf;
147 		uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
148 		uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
149 	} value;		/* either number or string */
150 	/* Words 17..31 */
151 	char desc[MAXDESC];	/* description */
152 };
153 
154 #define BIT(A)   (1 << (A))
155 #define STRING_IGNORE_LOWERCASE		BIT(0)
156 #define STRING_COMPACT_BLANK		BIT(1)
157 #define STRING_COMPACT_OPTIONAL_BLANK	BIT(2)
158 #define CHAR_IGNORE_LOWERCASE		'c'
159 #define CHAR_COMPACT_BLANK		'B'
160 #define CHAR_COMPACT_OPTIONAL_BLANK	'b'
161 
162 
163 /* list of magic entries */
164 struct mlist {
165 	struct magic *magic;		/* array of magic entries */
166 	uint32_t nmagic;			/* number of entries in array */
167 	int mapped;  /* allocation type: 0 => apprentice_file
168 		      *                  1 => apprentice_map + malloc
169 		      *                  2 => apprentice_map + mmap */
170 	struct mlist *next, *prev;
171 };
172 
173 struct magic_set {
174     struct mlist *mlist;
175     struct cont {
176 	size_t len;
177 	int32_t *off;
178     } c;
179     struct out {
180 	/* Accumulation buffer */
181 	char *buf;
182 	char *ptr;
183 	size_t left;
184 	size_t size;
185 	/* Printable buffer */
186 	char *pbuf;
187 	size_t psize;
188     } o;
189     int error;
190     int flags;
191     int haderr;
192 };
193 
194 struct stat;
195 protected char *file_fmttime(uint32_t, int);
196 protected int file_buffer(struct magic_set *, const void *, size_t);
197 protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
198 protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
199 protected int file_printf(struct magic_set *, const char *, ...)
200     __attribute__((__format__(__printf__, 2, 3)));
201 protected int file_reset(struct magic_set *);
202 protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
203 protected int file_zmagic(struct magic_set *, const unsigned char *, size_t);
204 protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
205 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
206 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
207 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
208 protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t);
209 protected void file_delmagic(struct magic *, int type, size_t entries);
210 protected void file_badread(struct magic_set *);
211 protected void file_badseek(struct magic_set *);
212 protected void file_oomem(struct magic_set *);
213 protected void file_error(struct magic_set *, int, const char *, ...)
214     __attribute__((__format__(__printf__, 3, 4)));
215 protected void file_magwarn(const char *, ...)
216     __attribute__((__format__(__printf__, 1, 2)));
217 protected void file_mdump(struct magic *);
218 protected void file_showstr(FILE *, const char *, size_t);
219 protected size_t file_mbswidth(const char *);
220 protected const char *file_getbuffer(struct magic_set *);
221 
222 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
223 #define QUICK
224 #endif
225 
226 #define FILE_RCSID(id) \
227 static const char *rcsid(const char *p) { \
228 	return rcsid(p = id); \
229 }
230 #else
231 
232 #endif /* __file_h__ */
233