Annotation of src/usr.bin/file/file.h, Revision 1.16.10.1
1.16.10.1! ckuethe 1: /* $OpenBSD: file.h,v 1.17 2007/07/09 16:39:48 dim Exp $ */
1.1 deraadt 2: /*
1.12 ian 3: * Copyright (c) Ian F. Darwin 1986-1995.
4: * Software written by Ian F. Darwin and others;
5: * maintained 1995-present by Christos Zoulas and others.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice immediately at the beginning of the file, without modification,
12: * this list of conditions, and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
1.1 deraadt 28: */
1.16 tedu 29: /*
30: * file.h - definitions for file(1) program
1.16.10.1! ckuethe 31: * @(#)$Id: file.h,v 1.17 2007/07/09 16:39:48 dim Exp $
1.16 tedu 32: */
1.1 deraadt 33:
1.3 millert 34: #ifndef __file_h__
35: #define __file_h__
36:
1.11 ian 37: #ifdef HAVE_CONFIG_H
38: #include "config.h"
39: #endif
40:
1.16 tedu 41: #include <stdio.h> /* Include that here, to make sure __P gets defined */
1.11 ian 42: #include <errno.h>
43: #ifdef HAVE_STDINT_H
44: #include <stdint.h>
1.16 tedu 45: #endif
46: #ifdef HAVE_INTTYPES_H
1.11 ian 47: #include <inttypes.h>
48: #endif
1.16 tedu 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
1.11 ian 67:
1.1 deraadt 68: #ifndef HOWMANY
1.16 tedu 69: # define HOWMANY 65536 /* how much of the file to look at */
1.1 deraadt 70: #endif
1.16 tedu 71: #define MAXMAGIS 4096 /* max entries in /etc/magic */
72: #define MAXDESC 64 /* max leng of text description */
1.1 deraadt 73: #define MAXstring 32 /* max leng of "string" types */
74:
1.16 tedu 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:
1.1 deraadt 83: struct magic {
1.16 tedu 84: /* Word 1 */
85: uint16_t cont_level; /* level of ">" */
86: uint8_t nospflag; /* supress space character */
87: uint8_t flag;
1.1 deraadt 88: #define INDIR 1 /* if '>(...)' appears, */
89: #define UNSIGNED 2 /* comparison is unsigned */
1.16 tedu 90: #define OFFADD 4 /* if '>&' appears, */
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: /* Word 3 */
113: uint8_t in_op; /* operator for indirection */
114: uint8_t mask_op; /* operator for mask */
115: uint8_t dummy1;
116: uint8_t dummy2;
117: #define FILE_OPS "&|^+-*/%"
118: #define FILE_OPAND 0
119: #define FILE_OPOR 1
120: #define FILE_OPXOR 2
121: #define FILE_OPADD 3
122: #define FILE_OPMINUS 4
123: #define FILE_OPMULTIPLY 5
124: #define FILE_OPDIVIDE 6
125: #define FILE_OPMODULO 7
126: #define FILE_OPINVERSE 0x80
127: /* Word 4 */
128: uint32_t offset; /* offset to magic number */
129: /* Word 5 */
130: uint32_t in_offset; /* offset from indirection */
131: /* Word 6 */
132: uint32_t mask; /* mask before comparison with value */
133: /* Word 7 */
134: uint32_t dummy3;
135: /* Word 8 */
136: uint32_t dummp4;
137: /* Words 9-16 */
1.1 deraadt 138: union VALUETYPE {
1.16 tedu 139: uint8_t b;
140: uint16_t h;
1.10 itojun 141: uint32_t l;
1.1 deraadt 142: char s[MAXstring];
1.16 tedu 143: char *buf;
144: uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
145: uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */
1.1 deraadt 146: } value; /* either number or string */
1.16 tedu 147: /* Words 17..31 */
1.1 deraadt 148: char desc[MAXDESC]; /* description */
149: };
150:
1.16 tedu 151: #define BIT(A) (1 << (A))
152: #define STRING_IGNORE_LOWERCASE BIT(0)
153: #define STRING_COMPACT_BLANK BIT(1)
154: #define STRING_COMPACT_OPTIONAL_BLANK BIT(2)
155: #define CHAR_IGNORE_LOWERCASE 'c'
156: #define CHAR_COMPACT_BLANK 'B'
157: #define CHAR_COMPACT_OPTIONAL_BLANK 'b'
158:
159:
160: /* list of magic entries */
161: struct mlist {
162: struct magic *magic; /* array of magic entries */
163: uint32_t nmagic; /* number of entries in array */
164: int mapped; /* allocation type: 0 => apprentice_file
165: * 1 => apprentice_map + malloc
166: * 2 => apprentice_map + mmap */
167: struct mlist *next, *prev;
168: };
1.1 deraadt 169:
1.16 tedu 170: struct magic_set {
171: struct mlist *mlist;
172: struct cont {
173: size_t len;
174: int32_t *off;
175: } c;
176: struct out {
177: /* Accumulation buffer */
178: char *buf;
179: char *ptr;
1.16.10.1! ckuethe 180: size_t left;
1.16 tedu 181: size_t size;
182: /* Printable buffer */
183: char *pbuf;
184: size_t psize;
185: } o;
186: int error;
187: int flags;
188: int haderr;
189: };
1.1 deraadt 190:
1.16 tedu 191: struct stat;
192: protected char *file_fmttime(uint32_t, int);
193: protected int file_buffer(struct magic_set *, const void *, size_t);
194: protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
195: protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
196: protected int file_printf(struct magic_set *, const char *, ...);
197: protected int file_reset(struct magic_set *);
198: protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
199: protected int file_zmagic(struct magic_set *, const unsigned char *, size_t);
200: protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
201: protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
202: protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
203: protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
204: protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t);
205: protected void file_delmagic(struct magic *, int type, size_t entries);
206: protected void file_badread(struct magic_set *);
207: protected void file_badseek(struct magic_set *);
208: protected void file_oomem(struct magic_set *);
209: protected void file_error(struct magic_set *, int, const char *, ...);
210: protected void file_magwarn(const char *, ...);
211: protected void file_mdump(struct magic *);
212: protected void file_showstr(FILE *, const char *, size_t);
213: protected size_t file_mbswidth(const char *);
214: protected const char *file_getbuffer(struct magic_set *);
215:
216: #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
217: #define QUICK
218: #endif
219:
220: #define FILE_RCSID(id) \
221: static const char *rcsid(const char *p) { \
222: return rcsid(p = id); \
223: }
224: #else
1.3 millert 225:
226: #endif /* __file_h__ */