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