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