xref: /freebsd/contrib/file/src/file.h (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*
2  * Copyright (c) Ian F. Darwin 1986-1995.
3  * Software written by Ian F. Darwin and others;
4  * maintained 1995-present by Christos Zoulas and others.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /*
29  * file.h - definitions for file(1) program
30  * @(#)$File: file.h,v 1.247 2023/07/27 19:40:22 christos Exp $
31  */
32 
33 #ifndef __file_h__
34 #define __file_h__
35 
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39 
40 #ifdef HAVE_STDINT_H
41 #include <stdint.h>
42 #endif
43 
44 #ifdef HAVE_INTTYPES_H
45 #include <inttypes.h>
46 #endif
47 
48 #ifndef __STDC_LIMIT_MACROS
49 #define __STDC_LIMIT_MACROS
50 #endif
51 #ifndef __STDC_FORMAT_MACROS
52 #define __STDC_FORMAT_MACROS
53 #endif
54 
55 #ifdef _WIN32
56 # ifdef PRIu32
57 #  ifdef _WIN64
58 #   define SIZE_T_FORMAT PRIu64
59 #  else
60 #   define SIZE_T_FORMAT PRIu32
61 #  endif
62 #  define INT64_T_FORMAT PRIi64
63 #  define INTMAX_T_FORMAT PRIiMAX
64 # else
65 #  ifdef _WIN64
66 #   define SIZE_T_FORMAT "I64"
67 #  else
68 #   define SIZE_T_FORMAT ""
69 #  endif
70 #  define INT64_T_FORMAT "I64"
71 #  define INTMAX_T_FORMAT "I64"
72 # endif
73 #else
74 # define SIZE_T_FORMAT "z"
75 # define INT64_T_FORMAT "ll"
76 # define INTMAX_T_FORMAT "j"
77 #endif
78 
79 #include <stdio.h>	/* Include that here, to make sure __P gets defined */
80 #include <errno.h>
81 #include <fcntl.h>	/* For open and flags */
82 #include <regex.h>
83 #include <time.h>
84 #include <sys/types.h>
85 #ifndef WIN32
86 #include <sys/param.h>
87 #endif
88 /* Do this here and now, because struct stat gets re-defined on solaris */
89 #include <sys/stat.h>
90 #include <stdarg.h>
91 #include <locale.h>
92 #if defined(HAVE_XLOCALE_H)
93 #include <xlocale.h>
94 #endif
95 
96 #define ENABLE_CONDITIONALS
97 
98 #ifndef MAGIC
99 #define MAGIC "/etc/magic"
100 #endif
101 
102 #if defined(__EMX__) || defined (WIN32)
103 #define PATHSEP	';'
104 #else
105 #define PATHSEP	':'
106 #endif
107 
108 #define file_private static
109 
110 #if HAVE_VISIBILITY && !defined(WIN32)
111 #define file_public  __attribute__ ((__visibility__("default")))
112 #ifndef file_protected
113 #define file_protected __attribute__ ((__visibility__("hidden")))
114 #endif
115 #else
116 #define file_public
117 #ifndef file_protected
118 #define file_protected
119 #endif
120 #endif
121 
122 #ifndef __arraycount
123 #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
124 #endif
125 
126 #ifndef __GNUC_PREREQ__
127 #ifdef __GNUC__
128 #define	__GNUC_PREREQ__(x, y)						\
129 	((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||			\
130 	 (__GNUC__ > (x)))
131 #else
132 #define	__GNUC_PREREQ__(x, y)	0
133 #endif
134 #endif
135 
136 #ifndef __GNUC__
137 #ifndef __attribute__
138 #define __attribute__(a)
139 #endif
140 #endif
141 
142 #ifndef MIN
143 #define	MIN(a,b)	(((a) < (b)) ? (a) : (b))
144 #endif
145 
146 #ifndef MAX
147 #define	MAX(a,b)	(((a) > (b)) ? (a) : (b))
148 #endif
149 
150 #ifndef O_CLOEXEC
151 # define O_CLOEXEC 0
152 #endif
153 
154 #ifndef FD_CLOEXEC
155 # define FD_CLOEXEC 1
156 #endif
157 
158 
159 /*
160  * Dec 31, 23:59:59 9999
161  * we need to make sure that we don't exceed 9999 because some libc
162  * implementations like muslc crash otherwise
163  */
164 #define	MAX_CTIME	CAST(time_t, 0x3afff487cfULL)
165 
166 #define FILE_BADSIZE CAST(size_t, ~0ul)
167 #define MAXDESC	64		/* max len of text description/MIME type */
168 #define MAXMIME	80		/* max len of text MIME type */
169 #define MAXstring 128		/* max len of "string" types */
170 
171 #define MAGICNO		0xF11E041C
172 #define VERSIONNO	18
173 #define FILE_MAGICSIZE	376
174 
175 #define FILE_GUID_SIZE	sizeof("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
176 
177 #define	FILE_LOAD	0
178 #define FILE_CHECK	1
179 #define FILE_COMPILE	2
180 #define FILE_LIST	3
181 
182 typedef regex_t file_regex_t;
183 
184 struct buffer {
185 	int fd;
186 	struct stat st;
187 	const void *fbuf;
188 	size_t flen;
189 	off_t eoff;
190 	void *ebuf;
191 	size_t elen;
192 };
193 
194 union VALUETYPE {
195 	uint8_t b;
196 	uint16_t h;
197 	uint32_t l;
198 	uint64_t q;
199 	uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
200 	uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
201 	uint8_t hq[8];	/* 8 bytes of a fixed-endian "quad" */
202 	char s[MAXstring];	/* the search string or regex pattern */
203 	unsigned char us[MAXstring];
204 	uint64_t guid[2];
205 	float f;
206 	double d;
207 };
208 
209 struct magic {
210 	/* Word 1 */
211 	uint16_t cont_level;	/* level of ">" */
212 	uint8_t flag;
213 #define INDIR		0x01	/* if '(...)' appears */
214 #define OFFADD		0x02	/* if '>&' or '>...(&' appears */
215 #define INDIROFFADD	0x04	/* if '>&(' appears */
216 #define UNSIGNED	0x08	/* comparison is unsigned */
217 #define NOSPACE		0x10	/* suppress space character before output */
218 #define BINTEST		0x20	/* test is for a binary type (set only
219 				   for top-level tests) */
220 #define TEXTTEST	0x40	/* for passing to file_softmagic */
221 #define OFFNEGATIVE	0x80	/* relative to the end of file */
222 
223 	uint8_t factor;
224 
225 	/* Word 2 */
226 	uint8_t reln;		/* relation (0=eq, '>'=gt, etc) */
227 	uint8_t vallen;		/* length of string value, if any */
228 	uint8_t type;		/* comparison type (FILE_*) */
229 	uint8_t in_type;	/* type of indirection */
230 #define 			FILE_INVALID		0
231 #define 			FILE_BYTE		1
232 #define				FILE_SHORT		2
233 #define				FILE_DEFAULT		3
234 #define				FILE_LONG		4
235 #define				FILE_STRING		5
236 #define				FILE_DATE		6
237 #define				FILE_BESHORT		7
238 #define				FILE_BELONG		8
239 #define				FILE_BEDATE		9
240 #define				FILE_LESHORT		10
241 #define				FILE_LELONG		11
242 #define				FILE_LEDATE		12
243 #define				FILE_PSTRING		13
244 #define				FILE_LDATE		14
245 #define				FILE_BELDATE		15
246 #define				FILE_LELDATE		16
247 #define				FILE_REGEX		17
248 #define				FILE_BESTRING16		18
249 #define				FILE_LESTRING16		19
250 #define				FILE_SEARCH		20
251 #define				FILE_MEDATE		21
252 #define				FILE_MELDATE		22
253 #define				FILE_MELONG		23
254 #define				FILE_QUAD		24
255 #define				FILE_LEQUAD		25
256 #define				FILE_BEQUAD		26
257 #define				FILE_QDATE		27
258 #define				FILE_LEQDATE		28
259 #define				FILE_BEQDATE		29
260 #define				FILE_QLDATE		30
261 #define				FILE_LEQLDATE		31
262 #define				FILE_BEQLDATE		32
263 #define				FILE_FLOAT		33
264 #define				FILE_BEFLOAT		34
265 #define				FILE_LEFLOAT		35
266 #define				FILE_DOUBLE		36
267 #define				FILE_BEDOUBLE		37
268 #define				FILE_LEDOUBLE		38
269 #define				FILE_BEID3		39
270 #define				FILE_LEID3		40
271 #define				FILE_INDIRECT		41
272 #define				FILE_QWDATE		42
273 #define				FILE_LEQWDATE		43
274 #define				FILE_BEQWDATE		44
275 #define				FILE_NAME		45
276 #define				FILE_USE		46
277 #define				FILE_CLEAR		47
278 #define				FILE_DER		48
279 #define				FILE_GUID		49
280 #define				FILE_OFFSET		50
281 #define				FILE_BEVARINT		51
282 #define				FILE_LEVARINT		52
283 #define				FILE_MSDOSDATE		53
284 #define				FILE_LEMSDOSDATE	54
285 #define				FILE_BEMSDOSDATE	55
286 #define				FILE_MSDOSTIME		56
287 #define				FILE_LEMSDOSTIME	57
288 #define				FILE_BEMSDOSTIME	58
289 #define				FILE_OCTAL		59
290 #define				FILE_NAMES_SIZE		60 /* size of array to contain all names */
291 
292 #define IS_STRING(t) \
293 	((t) == FILE_STRING || \
294 	 (t) == FILE_PSTRING || \
295 	 (t) == FILE_BESTRING16 || \
296 	 (t) == FILE_LESTRING16 || \
297 	 (t) == FILE_REGEX || \
298 	 (t) == FILE_SEARCH || \
299 	 (t) == FILE_INDIRECT || \
300 	 (t) == FILE_NAME || \
301 	 (t) == FILE_USE || \
302 	 (t) == FILE_OCTAL)
303 
304 #define FILE_FMT_NONE 0
305 #define FILE_FMT_NUM  1 /* "cduxXi" */
306 #define FILE_FMT_STR  2 /* "s" */
307 #define FILE_FMT_QUAD 3 /* "ll" */
308 #define FILE_FMT_FLOAT 4 /* "eEfFgG" */
309 #define FILE_FMT_DOUBLE 5 /* "eEfFgG" */
310 
311 	/* Word 3 */
312 	uint8_t in_op;		/* operator for indirection */
313 	uint8_t mask_op;	/* operator for mask */
314 #ifdef ENABLE_CONDITIONALS
315 	uint8_t cond;		/* conditional type */
316 #else
317 	uint8_t dummy;
318 #endif
319 	uint8_t factor_op;
320 #define		FILE_FACTOR_OP_PLUS	'+'
321 #define		FILE_FACTOR_OP_MINUS	'-'
322 #define		FILE_FACTOR_OP_TIMES	'*'
323 #define		FILE_FACTOR_OP_DIV	'/'
324 #define		FILE_FACTOR_OP_NONE	'\0'
325 
326 #define				FILE_OPS	"&|^+-*/%"
327 #define				FILE_OPAND	0
328 #define				FILE_OPOR	1
329 #define				FILE_OPXOR	2
330 #define				FILE_OPADD	3
331 #define				FILE_OPMINUS	4
332 #define				FILE_OPMULTIPLY	5
333 #define				FILE_OPDIVIDE	6
334 #define				FILE_OPMODULO	7
335 #define				FILE_OPS_MASK	0x07 /* mask for above ops */
336 #define				FILE_UNUSED_1	0x08
337 #define				FILE_UNUSED_2	0x10
338 #define				FILE_OPSIGNED	0x20
339 #define				FILE_OPINVERSE	0x40
340 #define				FILE_OPINDIRECT	0x80
341 
342 #ifdef ENABLE_CONDITIONALS
343 #define				COND_NONE	0
344 #define				COND_IF		1
345 #define				COND_ELIF	2
346 #define				COND_ELSE	3
347 #endif /* ENABLE_CONDITIONALS */
348 
349 	/* Word 4 */
350 	int32_t offset;		/* offset to magic number */
351 	/* Word 5 */
352 	int32_t in_offset;	/* offset from indirection */
353 	/* Word 6 */
354 	uint32_t lineno;	/* line number in magic file */
355 	/* Word 7,8 */
356 	union {
357 		uint64_t _mask;	/* for use with numeric and date types */
358 		struct {
359 			uint32_t _count;	/* repeat/line count */
360 			uint32_t _flags;	/* modifier flags */
361 		} _s;		/* for use with string types */
362 	} _u;
363 #define num_mask _u._mask
364 #define str_range _u._s._count
365 #define str_flags _u._s._flags
366 	/* Words 9-24 */
367 	union VALUETYPE value;	/* either number or string */
368 	/* Words 25-40 */
369 	char desc[MAXDESC];	/* description */
370 	/* Words 41-60 */
371 	char mimetype[MAXMIME]; /* MIME type */
372 	/* Words 61-62 */
373 	char apple[8];		/* APPLE CREATOR/TYPE */
374 	/* Words 63-78 */
375 	char ext[64];		/* Popular extensions */
376 };
377 
378 #define BIT(A)   (1 << (A))
379 #define STRING_COMPACT_WHITESPACE		BIT(0)
380 #define STRING_COMPACT_OPTIONAL_WHITESPACE	BIT(1)
381 #define STRING_IGNORE_LOWERCASE			BIT(2)
382 #define STRING_IGNORE_UPPERCASE			BIT(3)
383 #define REGEX_OFFSET_START			BIT(4)
384 #define STRING_TEXTTEST				BIT(5)
385 #define STRING_BINTEST				BIT(6)
386 #define PSTRING_1_BE				BIT(7)
387 #define PSTRING_1_LE				BIT(7)
388 #define PSTRING_2_BE				BIT(8)
389 #define PSTRING_2_LE				BIT(9)
390 #define PSTRING_4_BE				BIT(10)
391 #define PSTRING_4_LE				BIT(11)
392 #define REGEX_LINE_COUNT			BIT(11)
393 #define PSTRING_LEN	\
394     (PSTRING_1_BE|PSTRING_2_LE|PSTRING_2_BE|PSTRING_4_LE|PSTRING_4_BE)
395 #define PSTRING_LENGTH_INCLUDES_ITSELF		BIT(12)
396 #define	STRING_TRIM				BIT(13)
397 #define	STRING_FULL_WORD			BIT(14)
398 #define CHAR_COMPACT_WHITESPACE			'W'
399 #define CHAR_COMPACT_OPTIONAL_WHITESPACE	'w'
400 #define CHAR_IGNORE_LOWERCASE			'c'
401 #define CHAR_IGNORE_UPPERCASE			'C'
402 #define CHAR_REGEX_OFFSET_START			's'
403 #define CHAR_TEXTTEST				't'
404 #define	CHAR_TRIM				'T'
405 #define	CHAR_FULL_WORD				'f'
406 #define CHAR_BINTEST				'b'
407 #define CHAR_PSTRING_1_BE			'B'
408 #define CHAR_PSTRING_1_LE			'B'
409 #define CHAR_PSTRING_2_BE			'H'
410 #define CHAR_PSTRING_2_LE			'h'
411 #define CHAR_PSTRING_4_BE			'L'
412 #define CHAR_PSTRING_4_LE			'l'
413 #define CHAR_PSTRING_LENGTH_INCLUDES_ITSELF     'J'
414 #define STRING_IGNORE_CASE		(STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
415 #define STRING_DEFAULT_RANGE		100
416 
417 #define	INDIRECT_RELATIVE			BIT(0)
418 #define	CHAR_INDIRECT_RELATIVE			'r'
419 
420 /* list of magic entries */
421 struct mlist {
422 	struct magic *magic;		/* array of magic entries */
423 	file_regex_t **magic_rxcomp;	/* array of compiled regexps */
424 	size_t nmagic;			/* number of entries in array */
425 	void *map;			/* internal resources used by entry */
426 	struct mlist *next, *prev;
427 };
428 
429 #ifdef __cplusplus
430 #define CAST(T, b)	static_cast<T>(b)
431 #define RCAST(T, b)	reinterpret_cast<T>(b)
432 #define CCAST(T, b)	const_cast<T>(b)
433 #else
434 #define CAST(T, b)	((T)(b))
435 #define RCAST(T, b)	((T)(uintptr_t)(b))
436 #define CCAST(T, b)	((T)(uintptr_t)(b))
437 #endif
438 
439 struct level_info {
440 	int32_t off;
441 	int got_match;
442 #ifdef ENABLE_CONDITIONALS
443 	int last_match;
444 	int last_cond;	/* used for error checking by parse() */
445 #endif
446 };
447 
448 struct cont {
449 	size_t len;
450 	struct level_info *li;
451 };
452 
453 #define MAGIC_SETS	2
454 
455 struct magic_set {
456 	struct mlist *mlist[MAGIC_SETS];	/* list of regular entries */
457 	struct cont c;
458 	struct out {
459 		char *buf;		/* Accumulation buffer */
460 		size_t blen;		/* Length of buffer */
461 		char *pbuf;		/* Printable buffer */
462 	} o;
463 	uint32_t offset;			/* a copy of m->offset while we */
464 					/* are working on the magic entry */
465 	uint32_t eoffset;		/* offset from end of file */
466 	int error;
467 	int flags;			/* Control magic tests. */
468 	int event_flags;		/* Note things that happened. */
469 #define 		EVENT_HAD_ERR		0x01
470 	const char *file;
471 	size_t line;			/* current magic line number */
472 	mode_t mode;			/* copy of current stat mode */
473 
474 	/* data for searches */
475 	struct {
476 		const char *s;		/* start of search in original source */
477 		size_t s_len;		/* length of search region */
478 		size_t offset;		/* starting offset in source: XXX - should this be off_t? */
479 		size_t rm_len;		/* match length */
480 	} search;
481 
482 	/* FIXME: Make the string dynamically allocated so that e.g.
483 	   strings matched in files can be longer than MAXstring */
484 	union VALUETYPE ms_value;	/* either number or string */
485 	uint16_t indir_max;
486 	uint16_t name_max;
487 	uint16_t elf_shnum_max;
488 	uint16_t elf_phnum_max;
489 	uint16_t elf_notes_max;
490 	uint16_t regex_max;
491 	size_t bytes_max;		/* number of bytes to read from file */
492 	size_t encoding_max;		/* bytes to look for encoding */
493 	size_t	elf_shsize_max;
494 #ifndef FILE_BYTES_MAX
495 # define FILE_BYTES_MAX (7 * 1024 * 1024)/* how much of the file to look at */
496 #endif /* above 0x6ab0f4 map offset for HelveticaNeue.dfont */
497 #define	FILE_ELF_NOTES_MAX		256
498 #define	FILE_ELF_PHNUM_MAX		2048
499 #define	FILE_ELF_SHNUM_MAX		32768
500 #define	FILE_ELF_SHSIZE_MAX		(128 * 1024 * 1024)
501 #define	FILE_INDIR_MAX			50
502 #define	FILE_NAME_MAX			50
503 #define	FILE_REGEX_MAX			8192
504 #define	FILE_ENCODING_MAX		(64 * 1024)
505 #if defined(HAVE_NEWLOCALE) && defined(HAVE_USELOCALE) && defined(HAVE_FREELOCALE)
506 #define USE_C_LOCALE
507 	locale_t c_lc_ctype;
508 #define file_locale_used
509 #else
510 #define file_locale_used __attribute__((__unused__))
511 #endif
512 };
513 
514 /* Type for Unicode characters */
515 typedef unsigned long file_unichar_t;
516 
517 struct stat;
518 #define FILE_T_LOCAL	1
519 #define FILE_T_WINDOWS	2
520 file_protected const char *file_fmtdatetime(char *, size_t, uint64_t, int);
521 file_protected const char *file_fmtdate(char *, size_t, uint16_t);
522 file_protected const char *file_fmttime(char *, size_t, uint16_t);
523 file_protected const char *file_fmtvarint(char *, size_t, const unsigned char *,
524     int);
525 file_protected const char *file_fmtnum(char *, size_t, const char *, int);
526 file_protected struct magic_set *file_ms_alloc(int);
527 file_protected void file_ms_free(struct magic_set *);
528 file_protected int file_default(struct magic_set *, size_t);
529 file_protected int file_buffer(struct magic_set *, int, struct stat *,
530     const char *, const void *, size_t);
531 file_protected int file_fsmagic(struct magic_set *, const char *,
532     struct stat *);
533 file_protected int file_pipe2file(struct magic_set *, int, const void *,
534     size_t);
535 file_protected int file_vprintf(struct magic_set *, const char *, va_list)
536     __attribute__((__format__(__printf__, 2, 0)));
537 file_protected int file_separator(struct magic_set *);
538 file_protected char *file_copystr(char *, size_t, size_t, const char *);
539 file_protected int file_checkfmt(char *, size_t, const char *);
540 file_protected size_t file_printedlen(const struct magic_set *);
541 file_protected int file_print_guid(char *, size_t, const uint64_t *);
542 file_protected int file_parse_guid(const char *, uint64_t *);
543 file_protected int file_replace(struct magic_set *, const char *, const char *);
544 file_protected int file_printf(struct magic_set *, const char *, ...)
545     __attribute__((__format__(__printf__, 2, 3)));
546 file_protected int file_reset(struct magic_set *, int);
547 file_protected int file_tryelf(struct magic_set *, const struct buffer *);
548 file_protected int file_trycdf(struct magic_set *, const struct buffer *);
549 #if HAVE_FORK
550 file_protected int file_zmagic(struct magic_set *, const struct buffer *,
551     const char *);
552 #endif
553 file_protected int file_ascmagic(struct magic_set *, const struct buffer *,
554     int);
555 file_protected int file_ascmagic_with_encoding(struct magic_set *,
556     const struct buffer *, file_unichar_t *, size_t, const char *, const char *, int);
557 file_protected int file_encoding(struct magic_set *, const struct buffer *,
558     file_unichar_t **, size_t *, const char **, const char **, const char **);
559 file_protected int file_is_json(struct magic_set *, const struct buffer *);
560 file_protected int file_is_csv(struct magic_set *, const struct buffer *, int,
561     const char *);
562 file_protected int file_is_simh(struct magic_set *, const struct buffer *);
563 file_protected int file_is_tar(struct magic_set *, const struct buffer *);
564 file_protected int file_softmagic(struct magic_set *, const struct buffer *,
565     uint16_t *, uint16_t *, int, int);
566 file_protected int file_apprentice(struct magic_set *, const char *, int);
567 file_protected size_t file_magic_strength(const struct magic *, size_t);
568 file_protected int buffer_apprentice(struct magic_set *, struct magic **,
569     size_t *, size_t);
570 file_protected int file_magicfind(struct magic_set *, const char *,
571     struct mlist *);
572 file_protected uint64_t file_signextend(struct magic_set *, struct magic *,
573     uint64_t);
574 file_protected uintmax_t file_varint2uintmax_t(const unsigned char *, int,
575     size_t *);
576 
577 file_protected void file_badread(struct magic_set *);
578 file_protected void file_badseek(struct magic_set *);
579 file_protected void file_oomem(struct magic_set *, size_t);
580 file_protected void file_error(struct magic_set *, int, const char *, ...)
581     __attribute__((__format__(__printf__, 3, 4)));
582 file_protected void file_magerror(struct magic_set *, const char *, ...)
583     __attribute__((__format__(__printf__, 2, 3)));
584 file_protected void file_magwarn(struct magic_set *, const char *, ...)
585     __attribute__((__format__(__printf__, 2, 3)));
586 file_protected void file_mdump(struct magic *);
587 file_protected void file_showstr(FILE *, const char *, size_t);
588 file_protected size_t file_mbswidth(struct magic_set *, const char *);
589 file_protected const char *file_getbuffer(struct magic_set *);
590 file_protected ssize_t sread(int, void *, size_t, int);
591 file_protected int file_check_mem(struct magic_set *, unsigned int);
592 file_protected int file_looks_utf8(const unsigned char *, size_t,
593     file_unichar_t *, size_t *);
594 file_protected size_t file_pstring_length_size(struct magic_set *,
595     const struct magic *);
596 file_protected size_t file_pstring_get_length(struct magic_set *,
597     const struct magic *, const char *);
598 file_protected char * file_printable(struct magic_set *, char *, size_t,
599     const char *, size_t);
600 #ifdef __EMX__
601 file_protected int file_os2_apptype(struct magic_set *, const char *,
602     const void *, size_t);
603 #endif /* __EMX__ */
604 file_protected int file_pipe_closexec(int *);
605 file_protected int file_clear_closexec(int);
606 file_protected char *file_strtrim(char *);
607 
608 file_protected void buffer_init(struct buffer *, int, const struct stat *,
609     const void *, size_t);
610 file_protected void buffer_fini(struct buffer *);
611 file_protected int buffer_fill(const struct buffer *);
612 
613 
614 
615 file_protected int file_regcomp(struct magic_set *, file_regex_t *,
616     const char *, int);
617 file_protected int file_regexec(struct magic_set *, file_regex_t *,
618     const char *, size_t, regmatch_t *, int);
619 file_protected void file_regfree(file_regex_t *);
620 
621 typedef struct {
622 	char *buf;
623 	size_t blen;
624 	uint32_t offset;
625 } file_pushbuf_t;
626 
627 file_protected file_pushbuf_t *file_push_buffer(struct magic_set *);
628 file_protected char  *file_pop_buffer(struct magic_set *, file_pushbuf_t *);
629 
630 #ifndef COMPILE_ONLY
631 extern const char *file_names[];
632 extern const size_t file_nnames;
633 #endif
634 
635 #ifndef HAVE_PREAD
636 ssize_t pread(int, void *, size_t, off_t);
637 #endif
638 #ifndef HAVE_VASPRINTF
639 int vasprintf(char **, const char *, va_list);
640 #endif
641 #ifndef HAVE_ASPRINTF
642 int asprintf(char **, const char *, ...);
643 #endif
644 #ifndef HAVE_DPRINTF
645 int dprintf(int, const char *, ...);
646 #endif
647 
648 #ifndef HAVE_STRLCPY
649 size_t strlcpy(char *, const char *, size_t);
650 #endif
651 #ifndef HAVE_STRLCAT
652 size_t strlcat(char *, const char *, size_t);
653 #endif
654 #ifndef HAVE_STRCASESTR
655 char *strcasestr(const char *, const char *);
656 #endif
657 #ifndef HAVE_GETLINE
658 ssize_t getline(char **, size_t *, FILE *);
659 ssize_t getdelim(char **, size_t *, int, FILE *);
660 #endif
661 #ifndef HAVE_CTIME_R
662 char   *ctime_r(const time_t *, char *);
663 #endif
664 #ifndef HAVE_ASCTIME_R
665 char   *asctime_r(const struct tm *, char *);
666 #endif
667 #ifndef HAVE_GMTIME_R
668 struct tm *gmtime_r(const time_t *, struct tm *);
669 #endif
670 #ifndef HAVE_LOCALTIME_R
671 struct tm *localtime_r(const time_t *, struct tm *);
672 #endif
673 #ifndef HAVE_FMTCHECK
674 const char *fmtcheck(const char *, const char *)
675      __attribute__((__format_arg__(2)));
676 #endif
677 
678 #ifdef HAVE_LIBSECCOMP
679 // basic filter
680 // this mode should not interfere with normal operations
681 // only some dangerous syscalls are blacklisted
682 int enable_sandbox_basic(void);
683 
684 // enhanced filter
685 // this mode allows only the necessary syscalls used during normal operation
686 // extensive testing required !!!
687 int enable_sandbox_full(void);
688 #endif
689 
690 file_protected const char *file_getprogname(void);
691 file_protected void file_setprogname(const char *);
692 file_protected void file_err(int, const char *, ...)
693     __attribute__((__format__(__printf__, 2, 3), __noreturn__));
694 file_protected void file_errx(int, const char *, ...)
695     __attribute__((__format__(__printf__, 2, 3), __noreturn__));
696 file_protected void file_warn(const char *, ...)
697     __attribute__((__format__(__printf__, 1, 2)));
698 file_protected void file_warnx(const char *, ...)
699     __attribute__((__format__(__printf__, 1, 2)));
700 
701 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
702 #define QUICK
703 #endif
704 
705 #ifndef O_BINARY
706 #define O_BINARY	0
707 #endif
708 #ifndef O_NONBLOCK
709 #define O_NONBLOCK	0
710 #endif
711 
712 #ifndef __cplusplus
713 #if defined(__GNUC__) && (__GNUC__ >= 3)
714 #define FILE_RCSID(id) \
715 static const char rcsid[] __attribute__((__used__)) = id;
716 #else
717 #define FILE_RCSID(id) \
718 static const char *rcsid(const char *p) { \
719 	return rcsid(p = id); \
720 }
721 #endif
722 #else
723 #define FILE_RCSID(id)
724 #endif
725 #ifndef __RCSID
726 #define __RCSID(a)
727 #endif
728 
729 #endif /* __file_h__ */
730