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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * file.h - definitions for file(1) program
30 * @(#)$File: file.h,v 1.91 2007/03/25 03:13:47 christos Exp $
40 #include <stdio.h> /* Include that here, to make sure __P gets defined */
42 #include <fcntl.h> /* For open and flags */
46 #ifdef HAVE_INTTYPES_H
50 #include <sys/types.h>
51 /* Do this here and now, because struct stat gets re-defined on solaris */
54 #define ENABLE_CONDITIONALS
57 #define MAGIC "/etc/magic"
66 #define private static
72 #ifndef __GNUC_PREREQ__
74 #define __GNUC_PREREQ__(x, y) \
75 ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) || \
78 #define __GNUC_PREREQ__(x, y) 0
83 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
87 # define HOWMANY (256 * 1024) /* how much of the file to look at */
89 #define MAXMAGIS 8192 /* max entries in /etc/magic */
90 #define MAXDESC 64 /* max leng of text description */
91 #define MAXstring 32 /* max leng of "string" types */
93 #define MAGICNO 0xF11E041C
95 #define FILE_MAGICSIZE (32 * 4)
99 #define FILE_COMPILE 2
103 uint16_t cont_level; /* level of ">" */
104 uint8_t nospflag; /* supress space character */
106 #define INDIR 1 /* if '(...)' appears */
107 #define OFFADD 2 /* if '>&' or '>...(&' appears */
108 #define INDIROFFADD 4 /* if '>&(' appears */
109 #define UNSIGNED 8 /* comparison is unsigned */
112 uint8_t reln; /* relation (0=eq, '>'=gt, etc) */
113 uint8_t vallen; /* length of string value, if any */
114 uint8_t type; /* int, short, long or string. */
115 uint8_t in_type; /* type of indirrection */
116 #define FILE_INVALID 0
119 #define FILE_DEFAULT 3
121 #define FILE_STRING 5
123 #define FILE_BESHORT 7
124 #define FILE_BELONG 8
125 #define FILE_BEDATE 9
126 #define FILE_LESHORT 10
127 #define FILE_LELONG 11
128 #define FILE_LEDATE 12
129 #define FILE_PSTRING 13
130 #define FILE_LDATE 14
131 #define FILE_BELDATE 15
132 #define FILE_LELDATE 16
133 #define FILE_REGEX 17
134 #define FILE_BESTRING16 18
135 #define FILE_LESTRING16 19
136 #define FILE_SEARCH 20
137 #define FILE_MEDATE 21
138 #define FILE_MELDATE 22
139 #define FILE_MELONG 23
141 #define FILE_LEQUAD 25
142 #define FILE_BEQUAD 26
143 #define FILE_QDATE 27
144 #define FILE_LEQDATE 28
145 #define FILE_BEQDATE 29
146 #define FILE_QLDATE 30
147 #define FILE_LEQLDATE 31
148 #define FILE_BEQLDATE 32
149 #define FILE_NAMES_SIZE 33/* size of array to contain all names */
151 #define IS_STRING(t) \
152 ((t) == FILE_STRING || \
153 (t) == FILE_PSTRING || \
154 (t) == FILE_BESTRING16 || \
155 (t) == FILE_LESTRING16 || \
156 (t) == FILE_REGEX || \
157 (t) == FILE_SEARCH || \
160 #define FILE_FMT_NONE 0
161 #define FILE_FMT_NUM 1 /* "cduxXi" */
162 #define FILE_FMT_STR 2 /* "s" */
163 #define FILE_FMT_QUAD 3 /* "ll" */
166 uint8_t in_op; /* operator for indirection */
167 uint8_t mask_op; /* operator for mask */
168 #ifdef ENABLE_CONDITIONALS
169 uint8_t cond; /* conditional type */
176 #define FILE_OPS "&|^+-*/%"
181 #define FILE_OPMINUS 4
182 #define FILE_OPMULTIPLY 5
183 #define FILE_OPDIVIDE 6
184 #define FILE_OPMODULO 7
185 #define FILE_OPS_MASK 0x07 /* mask for above ops */
186 #define FILE_UNUSED_1 0x08
187 #define FILE_UNUSED_2 0x10
188 #define FILE_UNUSED_3 0x20
189 #define FILE_OPINVERSE 0x40
190 #define FILE_OPINDIRECT 0x80
192 #ifdef ENABLE_CONDITIONALS
197 #endif /* ENABLE_CONDITIONALS */
200 uint32_t offset; /* offset to magic number */
202 int32_t in_offset; /* offset from indirection */
204 uint32_t lineno; /* line number in magic file */
207 uint64_t _mask; /* for use with numeric and date types */
209 uint32_t _count; /* repeat/line count */
210 uint32_t _flags; /* modifier flags */
211 } _s; /* for use with string types */
213 #define num_mask _u._mask
214 #define str_count _u._s._count
215 #define str_flags _u._s._flags
223 uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
224 uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */
225 uint8_t hq[8]; /* 8 bytes of a fixed-endian "quad" */
226 char s[MAXstring]; /* the search string or regex pattern */
227 } value; /* either number or string */
229 char desc[MAXDESC]; /* description */
232 #define BIT(A) (1 << (A))
233 #define STRING_COMPACT_BLANK BIT(0)
234 #define STRING_COMPACT_OPTIONAL_BLANK BIT(1)
235 #define STRING_IGNORE_LOWERCASE BIT(2)
236 #define STRING_IGNORE_UPPERCASE BIT(3)
237 #define REGEX_OFFSET_START BIT(4)
238 #define CHAR_COMPACT_BLANK 'B'
239 #define CHAR_COMPACT_OPTIONAL_BLANK 'b'
240 #define CHAR_IGNORE_LOWERCASE 'c'
241 #define CHAR_IGNORE_UPPERCASE 'C'
242 #define CHAR_REGEX_OFFSET_START 's'
243 #define STRING_IGNORE_CASE (STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
246 /* list of magic entries */
248 struct magic *magic; /* array of magic entries */
249 uint32_t nmagic; /* number of entries in array */
250 int mapped; /* allocation type: 0 => apprentice_file
251 * 1 => apprentice_map + malloc
252 * 2 => apprentice_map + mmap */
253 struct mlist *next, *prev;
263 #ifdef ENABLE_CONDITIONALS
265 int last_cond; /* used for error checking by parse() */
270 /* Accumulation buffer */
275 /* Printable buffer */
284 size_t line; /* current magic line number */
286 /* data for searches */
288 const char *s; /* start of search in original source */
289 size_t s_len; /* length of search region */
290 size_t offset; /* starting offset in source: XXX - should this be off_t? */
291 size_t rm_len; /* match length */
294 union VALUETYPE ms_value; /* either number or string */
298 protected const char *file_fmttime(uint32_t, int);
299 protected int file_buffer(struct magic_set *, int, const char *, const void *,
301 protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
302 protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
303 protected int file_printf(struct magic_set *, const char *, ...);
304 protected int file_reset(struct magic_set *);
305 protected int file_tryelf(struct magic_set *, int, const unsigned char *,
307 protected int file_zmagic(struct magic_set *, int, const char *,
308 const unsigned char *, size_t);
309 protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
310 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
311 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
312 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
313 protected uint64_t file_signextend(struct magic_set *, struct magic *,
315 protected void file_delmagic(struct magic *, int type, size_t entries);
316 protected void file_badread(struct magic_set *);
317 protected void file_badseek(struct magic_set *);
318 protected void file_oomem(struct magic_set *, size_t);
319 protected void file_error(struct magic_set *, int, const char *, ...);
320 protected void file_magerror(struct magic_set *, const char *, ...);
321 protected void file_magwarn(struct magic_set *, const char *, ...);
322 protected void file_mdump(struct magic *);
323 protected void file_showstr(FILE *, const char *, size_t);
324 protected size_t file_mbswidth(const char *);
325 protected const char *file_getbuffer(struct magic_set *);
326 protected ssize_t sread(int, void *, size_t, int);
327 protected int file_check_mem(struct magic_set *, unsigned int);
330 extern const char *file_names[];
331 extern const size_t file_nnames;
334 #ifndef HAVE_STRERROR
336 extern char *sys_errlist[];
337 #define strerror(e) \
338 (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
342 #define strtoul(a, b, c) strtol(a, b, c)
345 #ifndef HAVE_SNPRINTF
346 int snprintf(char *, size_t, const char *, ...);
349 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
357 #define FILE_RCSID(id) \
358 static const char *rcsid(const char *p) { \
359 return rcsid(p = id); \
362 #endif /* __file_h__ */