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.65 2005/01/07 19:17:26 christos Exp $
40 #include <stdio.h> /* Include that here, to make sure __P gets defined */
45 #ifdef HAVE_INTTYPES_H
48 /* Do this here and now, because struct stat gets re-defined on solaris */
52 #define MAGIC "/etc/magic"
61 #define private static
68 # define HOWMANY 65536 /* how much of the file to look at */
70 #define MAXMAGIS 4096 /* max entries in /etc/magic */
71 #define MAXDESC 64 /* max leng of text description */
72 #define MAXstring 32 /* max leng of "string" types */
74 #define MAGICNO 0xF11E041C
76 #define FILE_MAGICSIZE (32 * 4)
80 #define FILE_COMPILE 2
84 uint16_t cont_level; /* level of ">" */
85 uint8_t nospflag; /* supress space character */
87 #define INDIR 1 /* if '>(...)' appears, */
88 #define UNSIGNED 2 /* comparison is unsigned */
89 #define OFFADD 4 /* if '>&' appears, */
91 uint8_t reln; /* relation (0=eq, '>'=gt, etc) */
92 uint8_t vallen; /* length of string value, if any */
93 uint8_t type; /* int, short, long or string. */
94 uint8_t in_type; /* type of indirrection */
100 #define FILE_BESHORT 7
101 #define FILE_BELONG 8
102 #define FILE_BEDATE 9
103 #define FILE_LESHORT 10
104 #define FILE_LELONG 11
105 #define FILE_LEDATE 12
106 #define FILE_PSTRING 13
107 #define FILE_LDATE 14
108 #define FILE_BELDATE 15
109 #define FILE_LELDATE 16
110 #define FILE_REGEX 17
111 #define FILE_BESTRING16 18
112 #define FILE_LESTRING16 19
114 #define FILE_FORMAT_NAME \
115 /* 0 */ "invalid 0", \
118 /* 3 */ "invalid 3", \
125 /* 10 */ "leshort", \
128 /* 13 */ "pstring", \
130 /* 15 */ "beldate", \
131 /* 16 */ "leldate", \
133 /* 18 */ "bestring16", \
134 /* 19 */ "lestring16",
136 #define FILE_FMT_NUM "cduxXi"
137 #define FILE_FMT_STR "s"
139 #define FILE_FORMAT_STRING \
141 /* 1 */ FILE_FMT_NUM, \
142 /* 2 */ FILE_FMT_NUM, \
144 /* 4 */ FILE_FMT_NUM, \
145 /* 5 */ FILE_FMT_STR, \
146 /* 6 */ FILE_FMT_STR, \
147 /* 7 */ FILE_FMT_NUM, \
148 /* 8 */ FILE_FMT_NUM, \
149 /* 9 */ FILE_FMT_STR, \
150 /* 10 */ FILE_FMT_NUM, \
151 /* 11 */ FILE_FMT_NUM, \
152 /* 12 */ FILE_FMT_STR, \
153 /* 13 */ FILE_FMT_STR, \
154 /* 14 */ FILE_FMT_STR, \
155 /* 15 */ FILE_FMT_STR, \
156 /* 16 */ FILE_FMT_STR, \
157 /* 17 */ FILE_FMT_STR, \
158 /* 18 */ FILE_FMT_STR, \
159 /* 19 */ FILE_FMT_STR,
162 uint8_t in_op; /* operator for indirection */
163 uint8_t mask_op; /* operator for mask */
166 #define FILE_OPS "&|^+-*/%"
171 #define FILE_OPMINUS 4
172 #define FILE_OPMULTIPLY 5
173 #define FILE_OPDIVIDE 6
174 #define FILE_OPMODULO 7
175 #define FILE_OPINVERSE 0x80
177 uint32_t offset; /* offset to magic number */
179 uint32_t in_offset; /* offset from indirection */
181 uint32_t mask; /* mask before comparison with value */
193 uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
194 uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */
195 } value; /* either number or string */
197 char desc[MAXDESC]; /* description */
200 #define BIT(A) (1 << (A))
201 #define STRING_IGNORE_LOWERCASE BIT(0)
202 #define STRING_COMPACT_BLANK BIT(1)
203 #define STRING_COMPACT_OPTIONAL_BLANK BIT(2)
204 #define CHAR_IGNORE_LOWERCASE 'c'
205 #define CHAR_COMPACT_BLANK 'B'
206 #define CHAR_COMPACT_OPTIONAL_BLANK 'b'
209 /* list of magic entries */
211 struct magic *magic; /* array of magic entries */
212 uint32_t nmagic; /* number of entries in array */
213 int mapped; /* allocation type: 0 => apprentice_file
214 * 1 => apprentice_map + malloc
215 * 2 => apprentice_map + mmap */
216 struct mlist *next, *prev;
226 /* Accumulation buffer */
231 /* Printable buffer */
243 protected char *file_fmttime(uint32_t, int);
244 protected int file_buffer(struct magic_set *, int, const void *, size_t);
245 protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
246 protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
247 protected int file_printf(struct magic_set *, const char *, ...);
248 protected int file_reset(struct magic_set *);
249 protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
250 protected int file_zmagic(struct magic_set *, int, const unsigned char *, size_t);
251 protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
252 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
253 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
254 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
255 protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t);
256 protected void file_delmagic(struct magic *, int type, size_t entries);
257 protected void file_badread(struct magic_set *);
258 protected void file_badseek(struct magic_set *);
259 protected void file_oomem(struct magic_set *);
260 protected void file_error(struct magic_set *, int, const char *, ...);
261 protected void file_magwarn(struct magic_set *, const char *, ...);
262 protected void file_mdump(struct magic *);
263 protected void file_showstr(FILE *, const char *, size_t);
264 protected size_t file_mbswidth(const char *);
265 protected const char *file_getbuffer(struct magic_set *);
267 #ifndef HAVE_STRERROR
269 extern char *sys_errlist[];
270 #define strerror(e) \
271 (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
275 #define strtoul(a, b, c) strtol(a, b, c)
278 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
282 #define FILE_RCSID(id) \
283 static const char *rcsid(const char *p) { \
284 return rcsid(p = id); \
288 #endif /* __file_h__ */