Use the vendor-supplied man pages for file.1 and magic.5 instead of the
[dragonfly.git] / contrib / file-4 / src / file.h
CommitLineData
ab0b56cc
JS
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
9b22a626 30 * @(#)$Id: file.h,v 1.80 2006/10/31 20:57:45 christos Exp $
ab0b56cc
JS
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>
9b22a626 42#include <fcntl.h> /* For open and flags */
ab0b56cc
JS
43#ifdef HAVE_STDINT_H
44#include <stdint.h>
45#endif
46#ifdef HAVE_INTTYPES_H
47#include <inttypes.h>
48#endif
9b22a626 49#include <sys/types.h>
ab0b56cc
JS
50/* Do this here and now, because struct stat gets re-defined on solaris */
51#include <sys/stat.h>
52
53#ifndef MAGIC
54#define MAGIC "/etc/magic"
55#endif
56
57#ifdef __EMX__
58#define PATHSEP ';'
59#else
60#define PATHSEP ':'
61#endif
62
63#define private static
64#ifndef protected
65#define protected
66#endif
67#define public
68
69#ifndef HOWMANY
2be182fc 70# define HOWMANY (256 * 1024) /* how much of the file to look at */
ab0b56cc 71#endif
2be182fc 72#define MAXMAGIS 8192 /* max entries in /etc/magic */
ab0b56cc
JS
73#define MAXDESC 64 /* max leng of text description */
74#define MAXstring 32 /* max leng of "string" types */
75
76#define MAGICNO 0xF11E041C
9b22a626 77#define VERSIONNO 3
ab0b56cc
JS
78#define FILE_MAGICSIZE (32 * 4)
79
80#define FILE_LOAD 0
81#define FILE_CHECK 1
82#define FILE_COMPILE 2
83
84struct magic {
85 /* Word 1 */
86 uint16_t cont_level; /* level of ">" */
87 uint8_t nospflag; /* supress space character */
88 uint8_t flag;
89#define INDIR 1 /* if '>(...)' appears, */
90#define UNSIGNED 2 /* comparison is unsigned */
91#define OFFADD 4 /* if '>&' appears, */
2be182fc 92#define INDIROFFADD 8 /* if '>&(' appears, */
ab0b56cc
JS
93 /* Word 2 */
94 uint8_t reln; /* relation (0=eq, '>'=gt, etc) */
95 uint8_t vallen; /* length of string value, if any */
96 uint8_t type; /* int, short, long or string. */
97 uint8_t in_type; /* type of indirrection */
98#define FILE_BYTE 1
99#define FILE_SHORT 2
100#define FILE_LONG 4
101#define FILE_STRING 5
102#define FILE_DATE 6
103#define FILE_BESHORT 7
104#define FILE_BELONG 8
105#define FILE_BEDATE 9
106#define FILE_LESHORT 10
107#define FILE_LELONG 11
108#define FILE_LEDATE 12
109#define FILE_PSTRING 13
110#define FILE_LDATE 14
111#define FILE_BELDATE 15
112#define FILE_LELDATE 16
113#define FILE_REGEX 17
114#define FILE_BESTRING16 18
115#define FILE_LESTRING16 19
2be182fc 116#define FILE_SEARCH 20
9b22a626
PA
117#define FILE_MEDATE 21
118#define FILE_MELDATE 22
119#define FILE_MELONG 23
120#define FILE_QUAD 24
121#define FILE_LEQUAD 25
122#define FILE_BEQUAD 26
123#define FILE_QDATE 27
124#define FILE_LEQDATE 28
125#define FILE_BEQDATE 29
126#define FILE_QLDATE 30
127#define FILE_LEQLDATE 31
128#define FILE_BEQLDATE 32
ab0b56cc
JS
129
130#define FILE_FORMAT_NAME \
131/* 0 */ "invalid 0", \
132/* 1 */ "byte", \
133/* 2 */ "short", \
134/* 3 */ "invalid 3", \
135/* 4 */ "long", \
136/* 5 */ "string", \
137/* 6 */ "date", \
138/* 7 */ "beshort", \
139/* 8 */ "belong", \
2be182fc 140/* 9 */ "bedate", \
ab0b56cc
JS
141/* 10 */ "leshort", \
142/* 11 */ "lelong", \
143/* 12 */ "ledate", \
144/* 13 */ "pstring", \
145/* 14 */ "ldate", \
146/* 15 */ "beldate", \
147/* 16 */ "leldate", \
148/* 17 */ "regex", \
149/* 18 */ "bestring16", \
2be182fc 150/* 19 */ "lestring16", \
9b22a626
PA
151/* 20 */ "search", \
152/* 21 */ "medate", \
153/* 22 */ "meldate", \
154/* 23 */ "melong", \
155/* 24 */ "quad", \
156/* 25 */ "lequad", \
157/* 26 */ "bequad", \
158/* 27 */ "qdate", \
159/* 28 */ "leqdate", \
160/* 29 */ "beqdate", \
161/* 30 */ "qldate", \
162/* 31 */ "leqldate", \
163/* 32 */ "beqldate",
ab0b56cc 164
9b22a626
PA
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" */
ab0b56cc
JS
170
171#define FILE_FORMAT_STRING \
9b22a626 172/* 0 */ FILE_FMT_NONE, \
ab0b56cc
JS
173/* 1 */ FILE_FMT_NUM, \
174/* 2 */ FILE_FMT_NUM, \
9b22a626 175/* 3 */ FILE_FMT_NONE, \
ab0b56cc
JS
176/* 4 */ FILE_FMT_NUM, \
177/* 5 */ FILE_FMT_STR, \
178/* 6 */ FILE_FMT_STR, \
179/* 7 */ FILE_FMT_NUM, \
180/* 8 */ FILE_FMT_NUM, \
181/* 9 */ FILE_FMT_STR, \
182/* 10 */ FILE_FMT_NUM, \
183/* 11 */ FILE_FMT_NUM, \
184/* 12 */ FILE_FMT_STR, \
185/* 13 */ FILE_FMT_STR, \
186/* 14 */ FILE_FMT_STR, \
187/* 15 */ FILE_FMT_STR, \
188/* 16 */ FILE_FMT_STR, \
189/* 17 */ FILE_FMT_STR, \
190/* 18 */ FILE_FMT_STR, \
2be182fc 191/* 19 */ FILE_FMT_STR, \
9b22a626
PA
192/* 20 */ FILE_FMT_STR, \
193/* 21 */ FILE_FMT_STR, \
194/* 22 */ FILE_FMT_STR, \
195/* 23 */ FILE_FMT_NUM, \
196/* 24 */ FILE_FMT_QUAD, \
197/* 25 */ FILE_FMT_QUAD, \
198/* 26 */ FILE_FMT_QUAD, \
199/* 27 */ FILE_FMT_STR, \
200/* 28 */ FILE_FMT_STR, \
201/* 29 */ FILE_FMT_STR, \
202/* 30 */ FILE_FMT_STR, \
203/* 31 */ FILE_FMT_STR, \
204/* 32 */ FILE_FMT_STR,
205
ab0b56cc
JS
206
207 /* Word 3 */
208 uint8_t in_op; /* operator for indirection */
209 uint8_t mask_op; /* operator for mask */
210 uint8_t dummy1;
211 uint8_t dummy2;
212#define FILE_OPS "&|^+-*/%"
213#define FILE_OPAND 0
214#define FILE_OPOR 1
215#define FILE_OPXOR 2
216#define FILE_OPADD 3
217#define FILE_OPMINUS 4
218#define FILE_OPMULTIPLY 5
219#define FILE_OPDIVIDE 6
220#define FILE_OPMODULO 7
2be182fc
JS
221#define FILE_OPINVERSE 0x40
222#define FILE_OPINDIRECT 0x80
ab0b56cc
JS
223 /* Word 4 */
224 uint32_t offset; /* offset to magic number */
225 /* Word 5 */
2be182fc 226 int32_t in_offset; /* offset from indirection */
ab0b56cc 227 /* Word 6 */
9b22a626
PA
228 uint32_t dummy4;
229 /* Word 7,8 */
230 uint64_t mask; /* mask before comparison with value */
ab0b56cc
JS
231 /* Words 9-16 */
232 union VALUETYPE {
233 uint8_t b;
234 uint16_t h;
235 uint32_t l;
9b22a626 236 uint64_t q;
ab0b56cc 237 char s[MAXstring];
9b22a626
PA
238 struct {
239 char *buf;
240 size_t buflen;
241 } search;
ab0b56cc
JS
242 uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
243 uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */
9b22a626 244 uint8_t hq[8]; /* 8 bytes of a fixed-endian "quad" */
ab0b56cc
JS
245 } value; /* either number or string */
246 /* Words 17..31 */
247 char desc[MAXDESC]; /* description */
248};
249
250#define BIT(A) (1 << (A))
251#define STRING_IGNORE_LOWERCASE BIT(0)
252#define STRING_COMPACT_BLANK BIT(1)
253#define STRING_COMPACT_OPTIONAL_BLANK BIT(2)
254#define CHAR_IGNORE_LOWERCASE 'c'
255#define CHAR_COMPACT_BLANK 'B'
256#define CHAR_COMPACT_OPTIONAL_BLANK 'b'
257
258
259/* list of magic entries */
260struct mlist {
261 struct magic *magic; /* array of magic entries */
262 uint32_t nmagic; /* number of entries in array */
263 int mapped; /* allocation type: 0 => apprentice_file
264 * 1 => apprentice_map + malloc
265 * 2 => apprentice_map + mmap */
266 struct mlist *next, *prev;
267};
268
269struct magic_set {
270 struct mlist *mlist;
271 struct cont {
272 size_t len;
273 int32_t *off;
274 } c;
275 struct out {
276 /* Accumulation buffer */
277 char *buf;
278 char *ptr;
279 size_t len;
280 size_t size;
281 /* Printable buffer */
282 char *pbuf;
283 size_t psize;
284 } o;
285 int error;
286 int flags;
287 int haderr;
288 const char *file;
289 size_t line;
290};
291
292struct stat;
2be182fc 293protected const char *file_fmttime(uint32_t, int);
ab0b56cc
JS
294protected int file_buffer(struct magic_set *, int, const void *, size_t);
295protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
296protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
297protected int file_printf(struct magic_set *, const char *, ...);
298protected int file_reset(struct magic_set *);
299protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
300protected int file_zmagic(struct magic_set *, int, const unsigned char *, size_t);
301protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
302protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
303protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
304protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
9b22a626 305protected uint64_t file_signextend(struct magic_set *, struct magic *, uint64_t);
ab0b56cc
JS
306protected void file_delmagic(struct magic *, int type, size_t entries);
307protected void file_badread(struct magic_set *);
308protected void file_badseek(struct magic_set *);
309protected void file_oomem(struct magic_set *);
310protected void file_error(struct magic_set *, int, const char *, ...);
311protected void file_magwarn(struct magic_set *, const char *, ...);
312protected void file_mdump(struct magic *);
313protected void file_showstr(FILE *, const char *, size_t);
314protected size_t file_mbswidth(const char *);
315protected const char *file_getbuffer(struct magic_set *);
9b22a626
PA
316protected ssize_t sread(int, void *, size_t);
317
318#ifndef COMPILE_ONLY
319extern const char *file_names[];
320extern const size_t file_nnames;
321#endif
ab0b56cc
JS
322
323#ifndef HAVE_STRERROR
324extern int sys_nerr;
325extern char *sys_errlist[];
326#define strerror(e) \
327 (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
328#endif
329
330#ifndef HAVE_STRTOUL
331#define strtoul(a, b, c) strtol(a, b, c)
332#endif
333
9b22a626
PA
334#ifndef HAVE_SNPRINTF
335int snprintf(char *, size_t, const char *, ...);
336#endif
337
ab0b56cc
JS
338#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
339#define QUICK
340#endif
341
9b22a626
PA
342#ifndef O_BINARY
343#define O_BINARY 0
344#endif
345
ab0b56cc
JS
346#define FILE_RCSID(id) \
347static const char *rcsid(const char *p) { \
348 return rcsid(p = id); \
349}
ab0b56cc
JS
350
351#endif /* __file_h__ */