]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/file/file.h
This commit was generated by cvs2svn to compensate for changes in r168993,
[FreeBSD/FreeBSD.git] / contrib / file / 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  * @(#)$Id: file.h,v 1.73 2005/10/20 14:59:01 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 /* Do this here and now, because struct stat gets re-defined on solaris */
50 #include <sys/stat.h>
51
52 #ifndef MAGIC
53 #define MAGIC "/etc/magic"
54 #endif
55
56 #ifdef __EMX__
57 #define PATHSEP ';'
58 #else
59 #define PATHSEP ':'
60 #endif
61
62 #define private static
63 #ifndef protected
64 #define protected
65 #endif
66 #define public
67
68 #ifndef HOWMANY
69 # define HOWMANY (256 * 1024)   /* how much of the file to look at */
70 #endif
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 */
74
75 #define MAGICNO         0xF11E041C
76 #define VERSIONNO       2
77 #define FILE_MAGICSIZE  (32 * 4)
78
79 #define FILE_LOAD       0
80 #define FILE_CHECK      1
81 #define FILE_COMPILE    2
82
83 struct magic {
84         /* Word 1 */
85         uint16_t cont_level;    /* level of ">" */
86         uint8_t nospflag;       /* supress space character */
87         uint8_t flag;
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,  */
92         /* Word 2 */
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 */
97 #define                         FILE_BYTE       1
98 #define                         FILE_SHORT      2
99 #define                         FILE_LONG       4
100 #define                         FILE_STRING     5
101 #define                         FILE_DATE       6
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
119
120 #define                         FILE_FORMAT_NAME        \
121 /* 0 */                         "invalid 0",            \
122 /* 1 */                         "byte",                 \
123 /* 2 */                         "short",                \
124 /* 3 */                         "invalid 3",            \
125 /* 4 */                         "long",                 \
126 /* 5 */                         "string",               \
127 /* 6 */                         "date",                 \
128 /* 7 */                         "beshort",              \
129 /* 8 */                         "belong",               \
130 /* 9 */                         "bedate",               \
131 /* 10 */                        "leshort",              \
132 /* 11 */                        "lelong",               \
133 /* 12 */                        "ledate",               \
134 /* 13 */                        "pstring",              \
135 /* 14 */                        "ldate",                \
136 /* 15 */                        "beldate",              \
137 /* 16 */                        "leldate",              \
138 /* 17 */                        "regex",                \
139 /* 18 */                        "bestring16",           \
140 /* 19 */                        "lestring16",           \
141 /* 20 */                        "search",               \
142 /* 21 */                        "medate",               \
143 /* 22 */                        "meldate",              \
144 /* 23 */                        "melong",
145
146 #define FILE_FMT_NUM    "cduxXi"
147 #define FILE_FMT_STR    "s"     
148
149 #define                         FILE_FORMAT_STRING      \
150 /* 0 */                         NULL,                   \
151 /* 1 */                         FILE_FMT_NUM,           \
152 /* 2 */                         FILE_FMT_NUM,           \
153 /* 3 */                         NULL,                   \
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,
174
175         /* Word 3 */
176         uint8_t in_op;          /* operator for indirection */
177         uint8_t mask_op;        /* operator for mask */
178         uint8_t dummy1; 
179         uint8_t dummy2; 
180 #define                         FILE_OPS        "&|^+-*/%"
181 #define                         FILE_OPAND      0
182 #define                         FILE_OPOR       1
183 #define                         FILE_OPXOR      2
184 #define                         FILE_OPADD      3
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
191         /* Word 4 */
192         uint32_t offset;        /* offset to magic number */
193         /* Word 5 */
194         int32_t in_offset;      /* offset from indirection */
195         /* Word 6 */
196         uint32_t mask;  /* mask before comparison with value */
197         /* Word 7 */
198         uint32_t dummy3;
199         /* Word 8 */
200         uint32_t dummp4;
201         /* Words 9-16 */
202         union VALUETYPE {
203                 uint8_t b;
204                 uint16_t h;
205                 uint32_t l;
206                 char s[MAXstring];
207                 struct {
208                         char *buf;
209                         size_t buflen;
210                 } search;
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 */
214         /* Words 17..31 */
215         char desc[MAXDESC];     /* description */
216 };
217
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'
225
226
227 /* list of magic entries */
228 struct mlist {
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;
235 };
236
237 struct magic_set {
238     struct mlist *mlist;
239     struct cont {
240         size_t len;
241         int32_t *off;
242     } c;
243     struct out {
244         /* Accumulation buffer */
245         char *buf;
246         char *ptr;
247         size_t len;
248         size_t size;
249         /* Printable buffer */
250         char *pbuf;
251         size_t psize;
252     } o;
253     int error;
254     int flags;
255     int haderr;
256     const char *file;
257     size_t line;
258 };
259
260 struct stat;
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 *);
284
285 #ifndef HAVE_STRERROR
286 extern int sys_nerr;
287 extern char *sys_errlist[];
288 #define strerror(e) \
289         (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
290 #endif
291
292 #ifndef HAVE_STRTOUL
293 #define strtoul(a, b, c)        strtol(a, b, c)
294 #endif
295
296 #ifndef HAVE_SNPRINTF
297 int snprintf(char *, size_t, const char *, ...);
298 #endif
299
300 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
301 #define QUICK
302 #endif
303
304 #ifndef O_BINARY
305 #define O_BINARY        0
306 #endif
307
308 #define FILE_RCSID(id) \
309 static const char *rcsid(const char *p) { \
310         return rcsid(p = id); \
311 }
312 #else
313
314 #endif /* __file_h__ */