xref: /netbsd-src/external/bsd/file/dist/src/file.h (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: file.h,v 1.6 2012/02/22 17:53:51 christos Exp $	*/
2 
3 /*
4  * Copyright (c) Ian F. Darwin 1986-1995.
5  * Software written by Ian F. Darwin and others;
6  * maintained 1995-present by Christos Zoulas and others.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 /*
31  * file.h - definitions for file(1) program
32  * @(#)$File: file.h,v 1.135 2011/09/20 15:30:14 christos Exp $
33  */
34 
35 #ifndef __file_h__
36 #define __file_h__
37 
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 
42 #ifdef WIN32
43   #ifdef _WIN64
44     #define SIZE_T_FORMAT "I64"
45   #else
46     #define SIZE_T_FORMAT ""
47   #endif
48   #define INT64_T_FORMAT "I64"
49 #else
50   #define SIZE_T_FORMAT "z"
51   #define INT64_T_FORMAT "ll"
52 #endif
53 
54 #include <stdio.h>	/* Include that here, to make sure __P gets defined */
55 #include <errno.h>
56 #include <fcntl.h>	/* For open and flags */
57 #ifdef HAVE_STDINT_H
58 #ifndef __STDC_LIMIT_MACROS
59 #define __STDC_LIMIT_MACROS
60 #endif
61 #include <stdint.h>
62 #endif
63 #ifdef HAVE_INTTYPES_H
64 #include <inttypes.h>
65 #endif
66 #include <regex.h>
67 #include <sys/types.h>
68 #include <sys/param.h>
69 /* Do this here and now, because struct stat gets re-defined on solaris */
70 #include <sys/stat.h>
71 #include <stdarg.h>
72 
73 #define ENABLE_CONDITIONALS
74 
75 #ifndef MAGIC
76 #define MAGIC "/etc/magic"
77 #endif
78 
79 #if defined(__EMX__) || defined (WIN32)
80 #define PATHSEP	';'
81 #else
82 #define PATHSEP	':'
83 #endif
84 
85 #define private static
86 #ifndef protected
87 #define protected
88 #endif
89 #define public
90 
91 #ifndef __arraycount
92 #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
93 #endif
94 
95 #ifndef __GNUC_PREREQ__
96 #ifdef __GNUC__
97 #define	__GNUC_PREREQ__(x, y)						\
98 	((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||			\
99 	 (__GNUC__ > (x)))
100 #else
101 #define	__GNUC_PREREQ__(x, y)	0
102 #endif
103 #endif
104 
105 #ifndef __GNUC__
106 #ifndef __attribute__
107 #define __attribute__(a)
108 #endif
109 #endif
110 
111 #ifndef MIN
112 #define	MIN(a,b)	(((a) < (b)) ? (a) : (b))
113 #endif
114 
115 #ifndef MAX
116 #define	MAX(a,b)	(((a) > (b)) ? (a) : (b))
117 #endif
118 
119 #ifndef HOWMANY
120 # define HOWMANY (256 * 1024)	/* how much of the file to look at */
121 #endif
122 #define MAXMAGIS 8192		/* max entries in any one magic file
123 				   or directory */
124 #define MAXDESC	64		/* max leng of text description/MIME type */
125 #define MAXstring 64		/* max leng of "string" types */
126 
127 #define MAGICNO		0xF11E041C
128 #define VERSIONNO	8
129 #define FILE_MAGICSIZE	232
130 
131 #define	FILE_LOAD	0
132 #define FILE_CHECK	1
133 #define FILE_COMPILE	2
134 #define FILE_LIST	3
135 
136 union VALUETYPE {
137 	uint8_t b;
138 	uint16_t h;
139 	uint32_t l;
140 	uint64_t q;
141 	uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
142 	uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
143 	uint8_t hq[8];	/* 8 bytes of a fixed-endian "quad" */
144 	char s[MAXstring];	/* the search string or regex pattern */
145 	unsigned char us[MAXstring];
146 	float f;
147 	double d;
148 };
149 
150 struct magic {
151 	/* Word 1 */
152 	uint16_t cont_level;	/* level of ">" */
153 	uint8_t flag;
154 #define INDIR		0x01	/* if '(...)' appears */
155 #define OFFADD		0x02	/* if '>&' or '>...(&' appears */
156 #define INDIROFFADD	0x04	/* if '>&(' appears */
157 #define UNSIGNED	0x08	/* comparison is unsigned */
158 #define NOSPACE		0x10	/* suppress space character before output */
159 #define BINTEST		0x20	/* test is for a binary type (set only
160 				   for top-level tests) */
161 #define TEXTTEST	0x40	/* for passing to file_softmagic */
162 
163 	uint8_t factor;
164 
165 	/* Word 2 */
166 	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
167 	uint8_t vallen;		/* length of string value, if any */
168 	uint8_t type;		/* comparison type (FILE_*) */
169 	uint8_t in_type;	/* type of indirection */
170 #define 			FILE_INVALID	0
171 #define 			FILE_BYTE	1
172 #define				FILE_SHORT	2
173 #define				FILE_DEFAULT	3
174 #define				FILE_LONG	4
175 #define				FILE_STRING	5
176 #define				FILE_DATE	6
177 #define				FILE_BESHORT	7
178 #define				FILE_BELONG	8
179 #define				FILE_BEDATE	9
180 #define				FILE_LESHORT	10
181 #define				FILE_LELONG	11
182 #define				FILE_LEDATE	12
183 #define				FILE_PSTRING	13
184 #define				FILE_LDATE	14
185 #define				FILE_BELDATE	15
186 #define				FILE_LELDATE	16
187 #define				FILE_REGEX	17
188 #define				FILE_BESTRING16	18
189 #define				FILE_LESTRING16	19
190 #define				FILE_SEARCH	20
191 #define				FILE_MEDATE	21
192 #define				FILE_MELDATE	22
193 #define				FILE_MELONG	23
194 #define				FILE_QUAD	24
195 #define				FILE_LEQUAD	25
196 #define				FILE_BEQUAD	26
197 #define				FILE_QDATE	27
198 #define				FILE_LEQDATE	28
199 #define				FILE_BEQDATE	29
200 #define				FILE_QLDATE	30
201 #define				FILE_LEQLDATE	31
202 #define				FILE_BEQLDATE	32
203 #define				FILE_FLOAT	33
204 #define				FILE_BEFLOAT	34
205 #define				FILE_LEFLOAT	35
206 #define				FILE_DOUBLE	36
207 #define				FILE_BEDOUBLE	37
208 #define				FILE_LEDOUBLE	38
209 #define				FILE_BEID3	39
210 #define				FILE_LEID3	40
211 #define				FILE_INDIRECT	41
212 #define				FILE_NAMES_SIZE	42/* size of array to contain all names */
213 
214 #define IS_STRING(t) \
215 	((t) == FILE_STRING || \
216 	 (t) == FILE_PSTRING || \
217 	 (t) == FILE_BESTRING16 || \
218 	 (t) == FILE_LESTRING16 || \
219 	 (t) == FILE_REGEX || \
220 	 (t) == FILE_SEARCH || \
221 	 (t) == FILE_DEFAULT)
222 
223 #define FILE_FMT_NONE 0
224 #define FILE_FMT_NUM  1 /* "cduxXi" */
225 #define FILE_FMT_STR  2 /* "s" */
226 #define FILE_FMT_QUAD 3 /* "ll" */
227 #define FILE_FMT_FLOAT 4 /* "eEfFgG" */
228 #define FILE_FMT_DOUBLE 5 /* "eEfFgG" */
229 
230 	/* Word 3 */
231 	uint8_t in_op;		/* operator for indirection */
232 	uint8_t mask_op;	/* operator for mask */
233 #ifdef ENABLE_CONDITIONALS
234 	uint8_t cond;		/* conditional type */
235 #else
236 	uint8_t dummy;
237 #endif
238 	uint8_t factor_op;
239 #define		FILE_FACTOR_OP_PLUS	'+'
240 #define		FILE_FACTOR_OP_MINUS	'-'
241 #define		FILE_FACTOR_OP_TIMES	'*'
242 #define		FILE_FACTOR_OP_DIV	'/'
243 #define		FILE_FACTOR_OP_NONE	'\0'
244 
245 #define				FILE_OPS	"&|^+-*/%"
246 #define				FILE_OPAND	0
247 #define				FILE_OPOR	1
248 #define				FILE_OPXOR	2
249 #define				FILE_OPADD	3
250 #define				FILE_OPMINUS	4
251 #define				FILE_OPMULTIPLY	5
252 #define				FILE_OPDIVIDE	6
253 #define				FILE_OPMODULO	7
254 #define				FILE_OPS_MASK	0x07 /* mask for above ops */
255 #define				FILE_UNUSED_1	0x08
256 #define				FILE_UNUSED_2	0x10
257 #define				FILE_UNUSED_3	0x20
258 #define				FILE_OPINVERSE	0x40
259 #define				FILE_OPINDIRECT	0x80
260 
261 #ifdef ENABLE_CONDITIONALS
262 #define				COND_NONE	0
263 #define				COND_IF		1
264 #define				COND_ELIF	2
265 #define				COND_ELSE	3
266 #endif /* ENABLE_CONDITIONALS */
267 
268 	/* Word 4 */
269 	uint32_t offset;	/* offset to magic number */
270 	/* Word 5 */
271 	int32_t in_offset;	/* offset from indirection */
272 	/* Word 6 */
273 	uint32_t lineno;	/* line number in magic file */
274 	/* Word 7,8 */
275 	union {
276 		uint64_t _mask;	/* for use with numeric and date types */
277 		struct {
278 			uint32_t _count;	/* repeat/line count */
279 			uint32_t _flags;	/* modifier flags */
280 		} _s;		/* for use with string types */
281 	} _u;
282 #define num_mask _u._mask
283 #define str_range _u._s._count
284 #define str_flags _u._s._flags
285 	/* Words 9-16 */
286 	union VALUETYPE value;	/* either number or string */
287 	/* Words 17-32 */
288 	char desc[MAXDESC];	/* description */
289 	/* Words 33-48 */
290 	char mimetype[MAXDESC]; /* MIME type */
291 	/* Words 49-50 */
292 	char apple[8];
293 };
294 
295 #define BIT(A)   (1 << (A))
296 #define STRING_COMPACT_WHITESPACE		BIT(0)
297 #define STRING_COMPACT_OPTIONAL_WHITESPACE	BIT(1)
298 #define STRING_IGNORE_LOWERCASE			BIT(2)
299 #define STRING_IGNORE_UPPERCASE			BIT(3)
300 #define REGEX_OFFSET_START			BIT(4)
301 #define STRING_TEXTTEST				BIT(5)
302 #define STRING_BINTEST				BIT(6)
303 #define PSTRING_1_BE				BIT(7)
304 #define PSTRING_1_LE				BIT(7)
305 #define PSTRING_2_BE				BIT(8)
306 #define PSTRING_2_LE				BIT(9)
307 #define PSTRING_4_BE				BIT(10)
308 #define PSTRING_4_LE				BIT(11)
309 #define PSTRING_LEN	\
310     (PSTRING_1_BE|PSTRING_2_LE|PSTRING_2_BE|PSTRING_4_LE|PSTRING_4_BE)
311 #define PSTRING_LENGTH_INCLUDES_ITSELF		BIT(12)
312 #define CHAR_COMPACT_WHITESPACE			'W'
313 #define CHAR_COMPACT_OPTIONAL_WHITESPACE	'w'
314 #define CHAR_IGNORE_LOWERCASE			'c'
315 #define CHAR_IGNORE_UPPERCASE			'C'
316 #define CHAR_REGEX_OFFSET_START			's'
317 #define CHAR_TEXTTEST				't'
318 #define CHAR_BINTEST				'b'
319 #define CHAR_PSTRING_1_BE			'B'
320 #define CHAR_PSTRING_1_LE			'B'
321 #define CHAR_PSTRING_2_BE			'H'
322 #define CHAR_PSTRING_2_LE			'h'
323 #define CHAR_PSTRING_4_BE			'L'
324 #define CHAR_PSTRING_4_LE			'l'
325 #define CHAR_PSTRING_LENGTH_INCLUDES_ITSELF     'J'
326 #define STRING_IGNORE_CASE		(STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
327 #define STRING_DEFAULT_RANGE		100
328 
329 
330 /* list of magic entries */
331 struct mlist {
332 	struct magic *magic;		/* array of magic entries */
333 	uint32_t nmagic;			/* number of entries in array */
334 	int mapped;  /* allocation type: 0 => apprentice_file
335 		      *                  1 => apprentice_map + malloc
336 		      *                  2 => apprentice_map + mmap */
337 	struct mlist *next, *prev;
338 };
339 
340 #ifdef __cplusplus
341 #define CAST(T, b)	static_cast<T>(b)
342 #define RCAST(T, b)	reinterpret_cast<T>(b)
343 #else
344 #define CAST(T, b)	(T)(b)
345 #define RCAST(T, b)	(T)(b)
346 #endif
347 
348 struct level_info {
349 	int32_t off;
350 	int got_match;
351 #ifdef ENABLE_CONDITIONALS
352 	int last_match;
353 	int last_cond;	/* used for error checking by parse() */
354 #endif
355 };
356 struct magic_set {
357 	struct mlist *mlist;
358 	struct cont {
359 		size_t len;
360 		struct level_info *li;
361 	} c;
362 	struct out {
363 		char *buf;		/* Accumulation buffer */
364 		char *pbuf;		/* Printable buffer */
365 	} o;
366 	uint32_t offset;
367 	int error;
368 	int flags;			/* Control magic tests. */
369 	int event_flags;		/* Note things that happened. */
370 #define 		EVENT_HAD_ERR		0x01
371 	const char *file;
372 	size_t line;			/* current magic line number */
373 
374 	/* data for searches */
375 	struct {
376 		const char *s;		/* start of search in original source */
377 		size_t s_len;		/* length of search region */
378 		size_t offset;		/* starting offset in source: XXX - should this be off_t? */
379 		size_t rm_len;		/* match length */
380 	} search;
381 
382 	/* FIXME: Make the string dynamically allocated so that e.g.
383 	   strings matched in files can be longer than MAXstring */
384 	union VALUETYPE ms_value;	/* either number or string */
385 };
386 
387 /* Type for Unicode characters */
388 typedef unsigned long unichar;
389 
390 struct stat;
391 protected const char *file_fmttime(uint32_t, int);
392 protected int file_buffer(struct magic_set *, int, const char *, const void *,
393     size_t);
394 protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
395 protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
396 protected int file_vprintf(struct magic_set *, const char *, va_list);
397 protected size_t file_printedlen(const struct magic_set *);
398 protected int file_replace(struct magic_set *, const char *, const char *);
399 protected int file_printf(struct magic_set *, const char *, ...)
400     __attribute__((__format__(__printf__, 2, 3)));
401 protected int file_reset(struct magic_set *);
402 protected int file_tryelf(struct magic_set *, int, const unsigned char *,
403     size_t);
404 protected int file_trycdf(struct magic_set *, int, const unsigned char *,
405     size_t);
406 #if HAVE_FORK
407 protected int file_zmagic(struct magic_set *, int, const char *,
408     const unsigned char *, size_t);
409 #endif
410 protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t,
411     int);
412 protected int file_ascmagic_with_encoding(struct magic_set *,
413     const unsigned char *, size_t, unichar *, size_t, const char *,
414     const char *, int);
415 protected int file_encoding(struct magic_set *, const unsigned char *, size_t,
416     unichar **, size_t *, const char **, const char **, const char **);
417 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
418 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
419     int, int);
420 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
421 protected uint64_t file_signextend(struct magic_set *, struct magic *,
422     uint64_t);
423 protected void file_delmagic(struct magic *, int type, size_t entries);
424 protected void file_badread(struct magic_set *);
425 protected void file_badseek(struct magic_set *);
426 protected void file_oomem(struct magic_set *, size_t);
427 protected void file_error(struct magic_set *, int, const char *, ...)
428     __attribute__((__format__(__printf__, 3, 4)));
429 protected void file_magerror(struct magic_set *, const char *, ...)
430     __attribute__((__format__(__printf__, 2, 3)));
431 protected void file_magwarn(struct magic_set *, const char *, ...)
432     __attribute__((__format__(__printf__, 2, 3)));
433 protected void file_mdump(struct magic *);
434 protected void file_showstr(FILE *, const char *, size_t);
435 protected size_t file_mbswidth(const char *);
436 protected const char *file_getbuffer(struct magic_set *);
437 protected ssize_t sread(int, void *, size_t, int);
438 protected int file_check_mem(struct magic_set *, unsigned int);
439 protected int file_looks_utf8(const unsigned char *, size_t, unichar *,
440     size_t *);
441 protected size_t file_pstring_length_size(const struct magic *);
442 protected size_t file_pstring_get_length(const struct magic *, const char *);
443 #ifdef __EMX__
444 protected int file_os2_apptype(struct magic_set *, const char *, const void *,
445     size_t);
446 #endif /* __EMX__ */
447 
448 
449 #ifndef COMPILE_ONLY
450 extern const char *file_names[];
451 extern const size_t file_nnames;
452 #endif
453 
454 #ifndef HAVE_STRERROR
455 extern int sys_nerr;
456 extern char *sys_errlist[];
457 #define strerror(e) \
458 	(((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
459 #endif
460 
461 #ifndef HAVE_STRTOUL
462 #define strtoul(a, b, c)	strtol(a, b, c)
463 #endif
464 
465 #ifndef HAVE_VASPRINTF
466 int vasprintf(char **, const char *, va_list);
467 #endif
468 #ifndef HAVE_ASPRINTF
469 int asprintf(char **ptr, const char *format_string, ...);
470 #endif
471 
472 #ifndef HAVE_STRLCPY
473 size_t strlcpy(char *dst, const char *src, size_t siz);
474 #endif
475 #ifndef HAVE_STRLCAT
476 size_t strlcat(char *dst, const char *src, size_t siz);
477 #endif
478 #ifndef HAVE_GETLINE
479 ssize_t getline(char **dst, size_t *len, FILE *fp);
480 ssize_t getdelim(char **dst, size_t *len, int delimiter, FILE *fp);
481 #endif
482 
483 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
484 #define QUICK
485 #endif
486 
487 #ifndef O_BINARY
488 #define O_BINARY	0
489 #endif
490 
491 #ifndef __cplusplus
492 #if defined(__GNUC__) && (__GNUC__ >= 3)
493 #define FILE_RCSID(id) \
494 static const char rcsid[] __attribute__((__used__)) = id;
495 #else
496 #define FILE_RCSID(id) \
497 static const char *rcsid(const char *p) { \
498 	return rcsid(p = id); \
499 }
500 #endif
501 #else
502 #define FILE_RCSID(id)
503 #endif
504 #ifndef __RCSID
505 #define __RCSID(a)
506 #endif
507 
508 #endif /* __file_h__ */
509