Merge from vendor branch FILE:
[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.92 2007/11/08 00:31:37 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_FLOAT      33
150 #define                         FILE_BEFLOAT    34
151 #define                         FILE_LEFLOAT    35
152 #define                         FILE_DOUBLE     36
153 #define                         FILE_BEDOUBLE   37
154 #define                         FILE_LEDOUBLE   38
155 #define                         FILE_NAMES_SIZE 39/* size of array to contain all names */
156
157 #define IS_STRING(t) \
158         ((t) == FILE_STRING || \
159          (t) == FILE_PSTRING || \
160          (t) == FILE_BESTRING16 || \
161          (t) == FILE_LESTRING16 || \
162          (t) == FILE_REGEX || \
163          (t) == FILE_SEARCH || \
164          (t) == FILE_DEFAULT)
165
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" */
170 #define FILE_FMT_FLOAT 4 /* "eEfFgG" */
171 #define FILE_FMT_DOUBLE 5 /* "eEfFgG" */
172
173         /* Word 3 */
174         uint8_t in_op;          /* operator for indirection */
175         uint8_t mask_op;        /* operator for mask */
176 #ifdef ENABLE_CONDITIONALS
177         uint8_t cond;           /* conditional type */
178         uint8_t dummy1; 
179 #else
180         uint8_t dummy1; 
181         uint8_t dummy2; 
182 #endif
183
184 #define                         FILE_OPS        "&|^+-*/%"
185 #define                         FILE_OPAND      0
186 #define                         FILE_OPOR       1
187 #define                         FILE_OPXOR      2
188 #define                         FILE_OPADD      3
189 #define                         FILE_OPMINUS    4
190 #define                         FILE_OPMULTIPLY 5
191 #define                         FILE_OPDIVIDE   6
192 #define                         FILE_OPMODULO   7
193 #define                         FILE_OPS_MASK   0x07 /* mask for above ops */
194 #define                         FILE_UNUSED_1   0x08
195 #define                         FILE_UNUSED_2   0x10
196 #define                         FILE_UNUSED_3   0x20
197 #define                         FILE_OPINVERSE  0x40
198 #define                         FILE_OPINDIRECT 0x80
199
200 #ifdef ENABLE_CONDITIONALS
201 #define                         COND_NONE       0
202 #define                         COND_IF         1
203 #define                         COND_ELIF       2
204 #define                         COND_ELSE       3
205 #endif /* ENABLE_CONDITIONALS */
206
207         /* Word 4 */
208         uint32_t offset;        /* offset to magic number */
209         /* Word 5 */
210         int32_t in_offset;      /* offset from indirection */
211         /* Word 6 */
212         uint32_t lineno;        /* line number in magic file */
213         /* Word 7,8 */
214         union {
215                 uint64_t _mask; /* for use with numeric and date types */
216                 struct {
217                         uint32_t _count;        /* repeat/line count */
218                         uint32_t _flags;        /* modifier flags */
219                 } _s;           /* for use with string types */
220         } _u;
221 #define num_mask _u._mask
222 #define str_count _u._s._count
223 #define str_flags _u._s._flags
224
225         /* Words 9-16 */
226         union VALUETYPE {
227                 uint8_t b;
228                 uint16_t h;
229                 uint32_t l;
230                 uint64_t q;
231                 uint8_t hs[2];  /* 2 bytes of a fixed-endian "short" */
232                 uint8_t hl[4];  /* 4 bytes of a fixed-endian "long" */
233                 uint8_t hq[8];  /* 8 bytes of a fixed-endian "quad" */
234                 char s[MAXstring];      /* the search string or regex pattern */
235                 float f;
236                 double d;
237         } value;                /* either number or string */
238         /* Words 17..31 */
239         char desc[MAXDESC];     /* description */
240 };
241
242 #define BIT(A)   (1 << (A))
243 #define STRING_COMPACT_BLANK            BIT(0)
244 #define STRING_COMPACT_OPTIONAL_BLANK   BIT(1)
245 #define STRING_IGNORE_LOWERCASE         BIT(2)
246 #define STRING_IGNORE_UPPERCASE         BIT(3)
247 #define REGEX_OFFSET_START              BIT(4)
248 #define CHAR_COMPACT_BLANK              'B'
249 #define CHAR_COMPACT_OPTIONAL_BLANK     'b'
250 #define CHAR_IGNORE_LOWERCASE           'c'
251 #define CHAR_IGNORE_UPPERCASE           'C'
252 #define CHAR_REGEX_OFFSET_START         's'
253 #define STRING_IGNORE_CASE              (STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
254
255
256 /* list of magic entries */
257 struct mlist {
258         struct magic *magic;            /* array of magic entries */
259         uint32_t nmagic;                        /* number of entries in array */
260         int mapped;  /* allocation type: 0 => apprentice_file
261                       *                  1 => apprentice_map + malloc
262                       *                  2 => apprentice_map + mmap */
263         struct mlist *next, *prev;
264 };
265
266 struct magic_set {
267         struct mlist *mlist;
268         struct cont {
269                 size_t len;
270                 struct level_info {
271                         int32_t off;
272                         int got_match;
273 #ifdef ENABLE_CONDITIONALS
274                         int last_match;
275                         int last_cond;  /* used for error checking by parse() */
276 #endif
277                 } *li;
278         } c;
279         struct out {
280                 /* Accumulation buffer */
281                 char *buf;
282                 char *ptr;
283                 size_t left;
284                 size_t size;
285                 /* Printable buffer */
286                 char *pbuf;
287                 size_t psize;
288         } o;
289         uint32_t offset;
290         int error;
291         int flags;
292         int haderr;
293         const char *file;
294         size_t line;                    /* current magic line number */
295
296         /* data for searches */
297         struct {
298                 const char *s;          /* start of search in original source */
299                 size_t s_len;           /* length of search region */
300                 size_t offset;          /* starting offset in source: XXX - should this be off_t? */
301                 size_t rm_len;          /* match length */
302         } search;
303
304         union VALUETYPE ms_value;       /* either number or string */
305 };
306
307 struct stat;
308 protected const char *file_fmttime(uint32_t, int);
309 protected int file_buffer(struct magic_set *, int, const char *, const void *,
310     size_t);
311 protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
312 protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
313 protected int file_printf(struct magic_set *, const char *, ...);
314 protected int file_reset(struct magic_set *);
315 protected int file_tryelf(struct magic_set *, int, const unsigned char *,
316     size_t);
317 protected int file_zmagic(struct magic_set *, int, const char *,
318     const unsigned char *, size_t);
319 protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
320 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
321 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
322 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
323 protected uint64_t file_signextend(struct magic_set *, struct magic *,
324     uint64_t);
325 protected void file_delmagic(struct magic *, int type, size_t entries);
326 protected void file_badread(struct magic_set *);
327 protected void file_badseek(struct magic_set *);
328 protected void file_oomem(struct magic_set *, size_t);
329 protected void file_error(struct magic_set *, int, const char *, ...);
330 protected void file_magerror(struct magic_set *, const char *, ...);
331 protected void file_magwarn(struct magic_set *, const char *, ...);
332 protected void file_mdump(struct magic *);
333 protected void file_showstr(FILE *, const char *, size_t);
334 protected size_t file_mbswidth(const char *);
335 protected const char *file_getbuffer(struct magic_set *);
336 protected ssize_t sread(int, void *, size_t, int);
337 protected int file_check_mem(struct magic_set *, unsigned int);
338
339 #ifndef COMPILE_ONLY
340 extern const char *file_names[];
341 extern const size_t file_nnames;
342 #endif
343
344 #ifndef HAVE_STRERROR
345 extern int sys_nerr;
346 extern char *sys_errlist[];
347 #define strerror(e) \
348         (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
349 #endif
350
351 #ifndef HAVE_STRTOUL
352 #define strtoul(a, b, c)        strtol(a, b, c)
353 #endif
354
355 #ifndef HAVE_SNPRINTF
356 int snprintf(char *, size_t, const char *, ...);
357 #endif
358
359 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
360 #define QUICK
361 #endif
362
363 #ifndef O_BINARY
364 #define O_BINARY        0
365 #endif
366
367 #define FILE_RCSID(id) \
368 static const char *rcsid(const char *p) { \
369         return rcsid(p = id); \
370 }
371
372 #endif /* __file_h__ */