Start sentences on a new line, fix typo and use .An.
[dragonfly.git] / contrib / file-4 / src / file.h
1 /*
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.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
15  *  
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
26  * SUCH DAMAGE.
27  */
28 /*
29  * file.h - definitions for file(1) program
30  * @(#)$File: file.h,v 1.91 2007/03/25 03:13:47 christos Exp $
31  */
32
33 #ifndef __file_h__
34 #define __file_h__
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <stdio.h>      /* Include that here, to make sure __P gets defined */
41 #include <errno.h>
42 #include <fcntl.h>      /* For open and flags */
43 #ifdef HAVE_STDINT_H
44 #include <stdint.h>
45 #endif
46 #ifdef HAVE_INTTYPES_H
47 #include <inttypes.h>
48 #endif
49 #include <regex.h>
50 #include <sys/types.h>
51 /* Do this here and now, because struct stat gets re-defined on solaris */
52 #include <sys/stat.h>
53
54 #define ENABLE_CONDITIONALS
55
56 #ifndef MAGIC
57 #define MAGIC "/etc/magic"
58 #endif
59
60 #ifdef __EMX__
61 #define PATHSEP ';'
62 #else
63 #define PATHSEP ':'
64 #endif
65
66 #define private static
67 #ifndef protected
68 #define protected
69 #endif
70 #define public
71
72 #ifndef __GNUC_PREREQ__
73 #ifdef __GNUC__
74 #define __GNUC_PREREQ__(x, y)                                           \
75         ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||                  \
76          (__GNUC__ > (x)))
77 #else
78 #define __GNUC_PREREQ__(x, y)   0
79 #endif
80 #endif
81
82 #ifndef MIN
83 #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
84 #endif
85
86 #ifndef HOWMANY
87 # define HOWMANY (256 * 1024)   /* how much of the file to look at */
88 #endif
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 */
92
93 #define MAGICNO         0xF11E041C
94 #define VERSIONNO       4
95 #define FILE_MAGICSIZE  (32 * 4)
96
97 #define FILE_LOAD       0
98 #define FILE_CHECK      1
99 #define FILE_COMPILE    2
100
101 struct magic {
102         /* Word 1 */
103         uint16_t cont_level;    /* level of ">" */
104         uint8_t nospflag;       /* supress space character */
105         uint8_t flag;
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 */
110
111         /* Word 2 */
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
117 #define                         FILE_BYTE       1
118 #define                         FILE_SHORT      2
119 #define                         FILE_DEFAULT    3
120 #define                         FILE_LONG       4
121 #define                         FILE_STRING     5
122 #define                         FILE_DATE       6
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
140 #define                         FILE_QUAD       24
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 */
150
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 || \
158          (t) == FILE_DEFAULT)
159
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" */
164
165         /* Word 3 */
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 */
170         uint8_t dummy1; 
171 #else
172         uint8_t dummy1; 
173         uint8_t dummy2; 
174 #endif
175
176 #define                         FILE_OPS        "&|^+-*/%"
177 #define                         FILE_OPAND      0
178 #define                         FILE_OPOR       1
179 #define                         FILE_OPXOR      2
180 #define                         FILE_OPADD      3
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
191
192 #ifdef ENABLE_CONDITIONALS
193 #define                         COND_NONE       0
194 #define                         COND_IF         1
195 #define                         COND_ELIF       2
196 #define                         COND_ELSE       3
197 #endif /* ENABLE_CONDITIONALS */
198
199         /* Word 4 */
200         uint32_t offset;        /* offset to magic number */
201         /* Word 5 */
202         int32_t in_offset;      /* offset from indirection */
203         /* Word 6 */
204         uint32_t lineno;        /* line number in magic file */
205         /* Word 7,8 */
206         union {
207                 uint64_t _mask; /* for use with numeric and date types */
208                 struct {
209                         uint32_t _count;        /* repeat/line count */
210                         uint32_t _flags;        /* modifier flags */
211                 } _s;           /* for use with string types */
212         } _u;
213 #define num_mask _u._mask
214 #define str_count _u._s._count
215 #define str_flags _u._s._flags
216
217         /* Words 9-16 */
218         union VALUETYPE {
219                 uint8_t b;
220                 uint16_t h;
221                 uint32_t l;
222                 uint64_t q;
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 */
228         /* Words 17..31 */
229         char desc[MAXDESC];     /* description */
230 };
231
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)
244
245
246 /* list of magic entries */
247 struct mlist {
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;
254 };
255
256 struct magic_set {
257         struct mlist *mlist;
258         struct cont {
259                 size_t len;
260                 struct level_info {
261                         int32_t off;
262                         int got_match;
263 #ifdef ENABLE_CONDITIONALS
264                         int last_match;
265                         int last_cond;  /* used for error checking by parse() */
266 #endif
267                 } *li;
268         } c;
269         struct out {
270                 /* Accumulation buffer */
271                 char *buf;
272                 char *ptr;
273                 size_t left;
274                 size_t size;
275                 /* Printable buffer */
276                 char *pbuf;
277                 size_t psize;
278         } o;
279         uint32_t offset;
280         int error;
281         int flags;
282         int haderr;
283         const char *file;
284         size_t line;                    /* current magic line number */
285
286         /* data for searches */
287         struct {
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 */
292         } search;
293
294         union VALUETYPE ms_value;       /* either number or string */
295 };
296
297 struct stat;
298 protected const char *file_fmttime(uint32_t, int);
299 protected int file_buffer(struct magic_set *, int, const char *, const void *,
300     size_t);
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 *,
306     size_t);
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 *,
314     uint64_t);
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);
328
329 #ifndef COMPILE_ONLY
330 extern const char *file_names[];
331 extern const size_t file_nnames;
332 #endif
333
334 #ifndef HAVE_STRERROR
335 extern int sys_nerr;
336 extern char *sys_errlist[];
337 #define strerror(e) \
338         (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
339 #endif
340
341 #ifndef HAVE_STRTOUL
342 #define strtoul(a, b, c)        strtol(a, b, c)
343 #endif
344
345 #ifndef HAVE_SNPRINTF
346 int snprintf(char *, size_t, const char *, ...);
347 #endif
348
349 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
350 #define QUICK
351 #endif
352
353 #ifndef O_BINARY
354 #define O_BINARY        0
355 #endif
356
357 #define FILE_RCSID(id) \
358 static const char *rcsid(const char *p) { \
359         return rcsid(p = id); \
360 }
361
362 #endif /* __file_h__ */