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 * @(#)$Id: file.h,v 1.83 2006/12/11 21:48:49 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
49 #include <sys/types.h>
50 /* Do this here and now, because struct stat gets re-defined on solaris */
54 #define MAGIC "/etc/magic"
63 #define private static
70 # define HOWMANY (256 * 1024) /* how much of the file to look at */
72 #define MAXMAGIS 8192 /* max entries in /etc/magic */
73 #define MAXDESC 64 /* max leng of text description */
74 #define MAXstring 32 /* max leng of "string" types */
76 #define MAGICNO 0xF11E041C
78 #define FILE_MAGICSIZE (32 * 4)
82 #define FILE_COMPILE 2
86 uint16_t cont_level; /* level of ">" */
87 uint8_t nospflag; /* supress space character */
89 #define INDIR 1 /* if '>(...)' appears, */
90 #define UNSIGNED 2 /* comparison is unsigned */
91 #define OFFADD 4 /* if '>&' appears, */
92 #define INDIROFFADD 8 /* if '>&(' appears, */
94 uint8_t reln; /* relation (0=eq, '>'=gt, etc) */
95 uint8_t vallen; /* length of string value, if any */
96 uint8_t type; /* int, short, long or string. */
97 uint8_t in_type; /* type of indirrection */
101 #define FILE_STRING 5
103 #define FILE_BESHORT 7
104 #define FILE_BELONG 8
105 #define FILE_BEDATE 9
106 #define FILE_LESHORT 10
107 #define FILE_LELONG 11
108 #define FILE_LEDATE 12
109 #define FILE_PSTRING 13
110 #define FILE_LDATE 14
111 #define FILE_BELDATE 15
112 #define FILE_LELDATE 16
113 #define FILE_REGEX 17
114 #define FILE_BESTRING16 18
115 #define FILE_LESTRING16 19
116 #define FILE_SEARCH 20
117 #define FILE_MEDATE 21
118 #define FILE_MELDATE 22
119 #define FILE_MELONG 23
121 #define FILE_LEQUAD 25
122 #define FILE_BEQUAD 26
123 #define FILE_QDATE 27
124 #define FILE_LEQDATE 28
125 #define FILE_BEQDATE 29
126 #define FILE_QLDATE 30
127 #define FILE_LEQLDATE 31
128 #define FILE_BEQLDATE 32
130 #define FILE_FORMAT_NAME \
131 /* 0 */ "invalid 0", \
134 /* 3 */ "invalid 3", \
141 /* 10 */ "leshort", \
144 /* 13 */ "pstring", \
146 /* 15 */ "beldate", \
147 /* 16 */ "leldate", \
149 /* 18 */ "bestring16", \
150 /* 19 */ "lestring16", \
153 /* 22 */ "meldate", \
159 /* 28 */ "leqdate", \
160 /* 29 */ "beqdate", \
162 /* 31 */ "leqldate", \
166 #define FILE_FMT_NONE 0
167 #define FILE_FMT_NUM 1 /* "cduxXi" */
168 #define FILE_FMT_STR 2 /* "s" */
169 #define FILE_FMT_QUAD 3 /* "ll" */
171 #define FILE_FORMAT_STRING \
172 /* 0 */ FILE_FMT_NONE, \
173 /* 1 */ FILE_FMT_NUM, \
174 /* 2 */ FILE_FMT_NUM, \
175 /* 3 */ FILE_FMT_NONE, \
176 /* 4 */ FILE_FMT_NUM, \
177 /* 5 */ FILE_FMT_STR, \
178 /* 6 */ FILE_FMT_STR, \
179 /* 7 */ FILE_FMT_NUM, \
180 /* 8 */ FILE_FMT_NUM, \
181 /* 9 */ FILE_FMT_STR, \
182 /* 10 */ FILE_FMT_NUM, \
183 /* 11 */ FILE_FMT_NUM, \
184 /* 12 */ FILE_FMT_STR, \
185 /* 13 */ FILE_FMT_STR, \
186 /* 14 */ FILE_FMT_STR, \
187 /* 15 */ FILE_FMT_STR, \
188 /* 16 */ FILE_FMT_STR, \
189 /* 17 */ FILE_FMT_STR, \
190 /* 18 */ FILE_FMT_STR, \
191 /* 19 */ FILE_FMT_STR, \
192 /* 20 */ FILE_FMT_STR, \
193 /* 21 */ FILE_FMT_STR, \
194 /* 22 */ FILE_FMT_STR, \
195 /* 23 */ FILE_FMT_NUM, \
196 /* 24 */ FILE_FMT_QUAD, \
197 /* 25 */ FILE_FMT_QUAD, \
198 /* 26 */ FILE_FMT_QUAD, \
199 /* 27 */ FILE_FMT_STR, \
200 /* 28 */ FILE_FMT_STR, \
201 /* 29 */ FILE_FMT_STR, \
202 /* 30 */ FILE_FMT_STR, \
203 /* 31 */ FILE_FMT_STR, \
204 /* 32 */ FILE_FMT_STR,
208 uint8_t in_op; /* operator for indirection */
209 uint8_t mask_op; /* operator for mask */
212 #define FILE_OPS "&|^+-*/%"
217 #define FILE_OPMINUS 4
218 #define FILE_OPMULTIPLY 5
219 #define FILE_OPDIVIDE 6
220 #define FILE_OPMODULO 7
221 #define FILE_OPINVERSE 0x40
222 #define FILE_OPINDIRECT 0x80
224 uint32_t offset; /* offset to magic number */
226 int32_t in_offset; /* offset from indirection */
228 uint32_t lineno; /* line number in magic file */
230 uint64_t mask; /* mask before comparison with value */
242 uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
243 uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */
244 uint8_t hq[8]; /* 8 bytes of a fixed-endian "quad" */
245 } value; /* either number or string */
247 char desc[MAXDESC]; /* description */
250 #define BIT(A) (1 << (A))
251 #define STRING_IGNORE_LOWERCASE BIT(0)
252 #define STRING_COMPACT_BLANK BIT(1)
253 #define STRING_COMPACT_OPTIONAL_BLANK BIT(2)
254 #define CHAR_IGNORE_LOWERCASE 'c'
255 #define CHAR_COMPACT_BLANK 'B'
256 #define CHAR_COMPACT_OPTIONAL_BLANK 'b'
259 /* list of magic entries */
261 struct magic *magic; /* array of magic entries */
262 uint32_t nmagic; /* number of entries in array */
263 int mapped; /* allocation type: 0 => apprentice_file
264 * 1 => apprentice_map + malloc
265 * 2 => apprentice_map + mmap */
266 struct mlist *next, *prev;
276 /* Accumulation buffer */
281 /* Printable buffer */
294 protected const char *file_fmttime(uint32_t, int);
295 protected int file_buffer(struct magic_set *, int, const void *, size_t);
296 protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
297 protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
298 protected int file_printf(struct magic_set *, const char *, ...);
299 protected int file_reset(struct magic_set *);
300 protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
301 protected int file_zmagic(struct magic_set *, int, const unsigned char *, size_t);
302 protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
303 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
304 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
305 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
306 protected uint64_t file_signextend(struct magic_set *, struct magic *, uint64_t);
307 protected void file_delmagic(struct magic *, int type, size_t entries);
308 protected void file_badread(struct magic_set *);
309 protected void file_badseek(struct magic_set *);
310 protected void file_oomem(struct magic_set *, size_t);
311 protected void file_error(struct magic_set *, int, const char *, ...);
312 protected void file_magwarn(struct magic_set *, const char *, ...);
313 protected void file_mdump(struct magic *);
314 protected void file_showstr(FILE *, const char *, size_t);
315 protected size_t file_mbswidth(const char *);
316 protected const char *file_getbuffer(struct magic_set *);
317 protected ssize_t sread(int, void *, size_t);
320 extern const char *file_names[];
321 extern const size_t file_nnames;
324 #ifndef HAVE_STRERROR
326 extern char *sys_errlist[];
327 #define strerror(e) \
328 (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
332 #define strtoul(a, b, c) strtol(a, b, c)
335 #ifndef HAVE_SNPRINTF
336 int snprintf(char *, size_t, const char *, ...);
339 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
347 #define FILE_RCSID(id) \
348 static const char *rcsid(const char *p) { \
349 return rcsid(p = id); \
352 #endif /* __file_h__ */