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