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.73 2005/10/20 14:59:01 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 /* Do this here and now, because struct stat gets re-defined on solaris */
53 #define MAGIC "/etc/magic"
62 #define private static
69 # define HOWMANY (256 * 1024) /* how much of the file to look at */
71 #define MAXMAGIS 8192 /* max entries in /etc/magic */
72 #define MAXDESC 64 /* max leng of text description */
73 #define MAXstring 32 /* max leng of "string" types */
75 #define MAGICNO 0xF11E041C
77 #define FILE_MAGICSIZE (32 * 4)
81 #define FILE_COMPILE 2
85 uint16_t cont_level; /* level of ">" */
86 uint8_t nospflag; /* supress space character */
88 #define INDIR 1 /* if '>(...)' appears, */
89 #define UNSIGNED 2 /* comparison is unsigned */
90 #define OFFADD 4 /* if '>&' appears, */
91 #define INDIROFFADD 8 /* if '>&(' appears, */
93 uint8_t reln; /* relation (0=eq, '>'=gt, etc) */
94 uint8_t vallen; /* length of string value, if any */
95 uint8_t type; /* int, short, long or string. */
96 uint8_t in_type; /* type of indirrection */
100 #define FILE_STRING 5
102 #define FILE_BESHORT 7
103 #define FILE_BELONG 8
104 #define FILE_BEDATE 9
105 #define FILE_LESHORT 10
106 #define FILE_LELONG 11
107 #define FILE_LEDATE 12
108 #define FILE_PSTRING 13
109 #define FILE_LDATE 14
110 #define FILE_BELDATE 15
111 #define FILE_LELDATE 16
112 #define FILE_REGEX 17
113 #define FILE_BESTRING16 18
114 #define FILE_LESTRING16 19
115 #define FILE_SEARCH 20
116 #define FILE_MEDATE 21
117 #define FILE_MELDATE 22
118 #define FILE_MELONG 23
120 #define FILE_FORMAT_NAME \
121 /* 0 */ "invalid 0", \
124 /* 3 */ "invalid 3", \
131 /* 10 */ "leshort", \
134 /* 13 */ "pstring", \
136 /* 15 */ "beldate", \
137 /* 16 */ "leldate", \
139 /* 18 */ "bestring16", \
140 /* 19 */ "lestring16", \
143 /* 22 */ "meldate", \
146 #define FILE_FMT_NUM "cduxXi"
147 #define FILE_FMT_STR "s"
149 #define FILE_FORMAT_STRING \
151 /* 1 */ FILE_FMT_NUM, \
152 /* 2 */ FILE_FMT_NUM, \
154 /* 4 */ FILE_FMT_NUM, \
155 /* 5 */ FILE_FMT_STR, \
156 /* 6 */ FILE_FMT_STR, \
157 /* 7 */ FILE_FMT_NUM, \
158 /* 8 */ FILE_FMT_NUM, \
159 /* 9 */ FILE_FMT_STR, \
160 /* 10 */ FILE_FMT_NUM, \
161 /* 11 */ FILE_FMT_NUM, \
162 /* 12 */ FILE_FMT_STR, \
163 /* 13 */ FILE_FMT_STR, \
164 /* 14 */ FILE_FMT_STR, \
165 /* 15 */ FILE_FMT_STR, \
166 /* 16 */ FILE_FMT_STR, \
167 /* 17 */ FILE_FMT_STR, \
168 /* 18 */ FILE_FMT_STR, \
169 /* 19 */ FILE_FMT_STR, \
170 /* 20 */ FILE_FMT_STR, \
171 /* 21 */ FILE_FMT_STR, \
172 /* 22 */ FILE_FMT_STR, \
173 /* 23 */ FILE_FMT_NUM,
176 uint8_t in_op; /* operator for indirection */
177 uint8_t mask_op; /* operator for mask */
180 #define FILE_OPS "&|^+-*/%"
185 #define FILE_OPMINUS 4
186 #define FILE_OPMULTIPLY 5
187 #define FILE_OPDIVIDE 6
188 #define FILE_OPMODULO 7
189 #define FILE_OPINVERSE 0x40
190 #define FILE_OPINDIRECT 0x80
192 uint32_t offset; /* offset to magic number */
194 int32_t in_offset; /* offset from indirection */
196 uint32_t mask; /* mask before comparison with value */
211 uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
212 uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */
213 } value; /* either number or string */
215 char desc[MAXDESC]; /* description */
218 #define BIT(A) (1 << (A))
219 #define STRING_IGNORE_LOWERCASE BIT(0)
220 #define STRING_COMPACT_BLANK BIT(1)
221 #define STRING_COMPACT_OPTIONAL_BLANK BIT(2)
222 #define CHAR_IGNORE_LOWERCASE 'c'
223 #define CHAR_COMPACT_BLANK 'B'
224 #define CHAR_COMPACT_OPTIONAL_BLANK 'b'
227 /* list of magic entries */
229 struct magic *magic; /* array of magic entries */
230 uint32_t nmagic; /* number of entries in array */
231 int mapped; /* allocation type: 0 => apprentice_file
232 * 1 => apprentice_map + malloc
233 * 2 => apprentice_map + mmap */
234 struct mlist *next, *prev;
244 /* Accumulation buffer */
249 /* Printable buffer */
261 protected const char *file_fmttime(uint32_t, int);
262 protected int file_buffer(struct magic_set *, int, const void *, size_t);
263 protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
264 protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
265 protected int file_printf(struct magic_set *, const char *, ...);
266 protected int file_reset(struct magic_set *);
267 protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
268 protected int file_zmagic(struct magic_set *, int, const unsigned char *, size_t);
269 protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
270 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
271 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
272 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
273 protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t);
274 protected void file_delmagic(struct magic *, int type, size_t entries);
275 protected void file_badread(struct magic_set *);
276 protected void file_badseek(struct magic_set *);
277 protected void file_oomem(struct magic_set *);
278 protected void file_error(struct magic_set *, int, const char *, ...);
279 protected void file_magwarn(struct magic_set *, const char *, ...);
280 protected void file_mdump(struct magic *);
281 protected void file_showstr(FILE *, const char *, size_t);
282 protected size_t file_mbswidth(const char *);
283 protected const char *file_getbuffer(struct magic_set *);
285 #ifndef HAVE_STRERROR
287 extern char *sys_errlist[];
288 #define strerror(e) \
289 (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
293 #define strtoul(a, b, c) strtol(a, b, c)
296 #ifndef HAVE_SNPRINTF
297 int snprintf(char *, size_t, const char *, ...);
300 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
308 #define FILE_RCSID(id) \
309 static const char *rcsid(const char *p) { \
310 return rcsid(p = id); \
314 #endif /* __file_h__ */