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