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