file/src/file.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Ian F. Darwin 1986-1995.
00003  * Software written by Ian F. Darwin and others;
00004  * maintained 1995-present by Christos Zoulas and others.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice immediately at the beginning of the file, without modification,
00011  *    this list of conditions, and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  *  
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
00020  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00022  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00024  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00025  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00026  * SUCH DAMAGE.
00027  */
00028 /*
00029  * file.h - definitions for file(1) program
00030  * @(#)$File: file.h,v 1.91 2007/03/25 03:13:47 christos Exp $
00031  */
00032 
00033 #ifndef __file_h__
00034 #define __file_h__
00035 
00036 #ifdef HAVE_CONFIG_H
00037 #include <config.h>
00038 #endif
00039 
00040 #include <stdio.h>      /* Include that here, to make sure __P gets defined */
00041 #include <errno.h>
00042 #include <fcntl.h>      /* For open and flags */
00043 #ifdef HAVE_STDINT_H
00044 #include <stdint.h>
00045 #endif
00046 #ifdef HAVE_INTTYPES_H
00047 #include <inttypes.h>
00048 #endif
00049 #include <regex.h>
00050 #include <sys/types.h>
00051 /* Do this here and now, because struct stat gets re-defined on solaris */
00052 #include <sys/stat.h>
00053 
00054 #define ENABLE_CONDITIONALS
00055 
00056 #ifndef MAGIC
00057 #define MAGIC "/etc/magic"
00058 #endif
00059 
00060 #ifdef __EMX__
00061 #define PATHSEP ';'
00062 #else
00063 #define PATHSEP ':'
00064 #endif
00065 
00066 #define private static
00067 #ifndef protected
00068 #define protected
00069 #endif
00070 #define public
00071 
00072 #ifndef __GNUC_PREREQ__
00073 #ifdef __GNUC__
00074 #define __GNUC_PREREQ__(x, y)                                           \
00075         ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||                  \
00076          (__GNUC__ > (x)))
00077 #else
00078 #define __GNUC_PREREQ__(x, y)   0
00079 #endif
00080 #endif
00081 
00082 #ifndef f__unused
00083 #if __GNUC_PREREQ__(2, 7)
00084 #define f__unused       __attribute__((__unused__))
00085 #else
00086 #define f__unused       /* delete */
00087 #endif
00088 #endif
00089 
00090 #ifndef MIN
00091 #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
00092 #endif
00093 
00094 #ifndef HOWMANY
00095 # define HOWMANY (256 * 1024)   /* how much of the file to look at */
00096 #endif
00097 #define MAXMAGIS 8192           /* max entries in /etc/magic */
00098 #define MAXDESC 64              /* max leng of text description */
00099 #define MAXstring 32            /* max leng of "string" types */
00100 
00101 #define MAGICNO         0xF11E041C
00102 #define VERSIONNO       4
00103 #define FILE_MAGICSIZE  (32 * 4)
00104 
00105 #define FILE_LOAD       0
00106 #define FILE_CHECK      1
00107 #define FILE_COMPILE    2
00108 
00109 struct magic {
00110         /* Word 1 */
00111         uint16_t cont_level;    /* level of ">" */
00112         uint8_t nospflag;       /* supress space character */
00113         uint8_t flag;
00114 #define INDIR           1       /* if '(...)' appears */
00115 #define OFFADD          2       /* if '>&' or '>...(&' appears */
00116 #define INDIROFFADD     4       /* if '>&(' appears */
00117 #define UNSIGNED        8       /* comparison is unsigned */
00118 
00119         /* Word 2 */
00120         uint8_t reln;           /* relation (0=eq, '>'=gt, etc) */
00121         uint8_t vallen;         /* length of string value, if any */
00122         uint8_t type;           /* int, short, long or string. */
00123         uint8_t in_type;        /* type of indirrection */
00124 #define                         FILE_INVALID    0
00125 #define                         FILE_BYTE       1
00126 #define                         FILE_SHORT      2
00127 #define                         FILE_DEFAULT    3
00128 #define                         FILE_LONG       4
00129 #define                         FILE_STRING     5
00130 #define                         FILE_DATE       6
00131 #define                         FILE_BESHORT    7
00132 #define                         FILE_BELONG     8
00133 #define                         FILE_BEDATE     9
00134 #define                         FILE_LESHORT    10
00135 #define                         FILE_LELONG     11
00136 #define                         FILE_LEDATE     12
00137 #define                         FILE_PSTRING    13
00138 #define                         FILE_LDATE      14
00139 #define                         FILE_BELDATE    15
00140 #define                         FILE_LELDATE    16
00141 #define                         FILE_REGEX      17
00142 #define                         FILE_BESTRING16 18
00143 #define                         FILE_LESTRING16 19
00144 #define                         FILE_SEARCH     20
00145 #define                         FILE_MEDATE     21
00146 #define                         FILE_MELDATE    22
00147 #define                         FILE_MELONG     23
00148 #define                         FILE_QUAD       24
00149 #define                         FILE_LEQUAD     25
00150 #define                         FILE_BEQUAD     26
00151 #define                         FILE_QDATE      27
00152 #define                         FILE_LEQDATE    28
00153 #define                         FILE_BEQDATE    29
00154 #define                         FILE_QLDATE     30
00155 #define                         FILE_LEQLDATE   31
00156 #define                         FILE_BEQLDATE   32
00157 #define                         FILE_NAMES_SIZE 33/* size of array to contain all names */
00158 
00159 #define IS_STRING(t) \
00160         ((t) == FILE_STRING || \
00161          (t) == FILE_PSTRING || \
00162          (t) == FILE_BESTRING16 || \
00163          (t) == FILE_LESTRING16 || \
00164          (t) == FILE_REGEX || \
00165          (t) == FILE_SEARCH || \
00166          (t) == FILE_DEFAULT)
00167 
00168 #define FILE_FMT_NONE 0
00169 #define FILE_FMT_NUM  1 /* "cduxXi" */
00170 #define FILE_FMT_STR  2 /* "s" */
00171 #define FILE_FMT_QUAD 3 /* "ll" */
00172 
00173         /* Word 3 */
00174         uint8_t in_op;          /* operator for indirection */
00175         uint8_t mask_op;        /* operator for mask */
00176 #ifdef ENABLE_CONDITIONALS
00177         uint8_t cond;           /* conditional type */
00178         uint8_t dummy1; 
00179 #else
00180         uint8_t dummy1; 
00181         uint8_t dummy2; 
00182 #endif
00183 
00184 #define                         FILE_OPS        "&|^+-*/%"
00185 #define                         FILE_OPAND      0
00186 #define                         FILE_OPOR       1
00187 #define                         FILE_OPXOR      2
00188 #define                         FILE_OPADD      3
00189 #define                         FILE_OPMINUS    4
00190 #define                         FILE_OPMULTIPLY 5
00191 #define                         FILE_OPDIVIDE   6
00192 #define                         FILE_OPMODULO   7
00193 #define                         FILE_OPS_MASK   0x07 /* mask for above ops */
00194 #define                         FILE_UNUSED_1   0x08
00195 #define                         FILE_UNUSED_2   0x10
00196 #define                         FILE_UNUSED_3   0x20
00197 #define                         FILE_OPINVERSE  0x40
00198 #define                         FILE_OPINDIRECT 0x80
00199 
00200 #ifdef ENABLE_CONDITIONALS
00201 #define                         COND_NONE       0
00202 #define                         COND_IF         1
00203 #define                         COND_ELIF       2
00204 #define                         COND_ELSE       3
00205 #endif /* ENABLE_CONDITIONALS */
00206 
00207         /* Word 4 */
00208         uint32_t offset;        /* offset to magic number */
00209         /* Word 5 */
00210         int32_t in_offset;      /* offset from indirection */
00211         /* Word 6 */
00212         uint32_t lineno;        /* line number in magic file */
00213         /* Word 7,8 */
00214         union {
00215                 uint64_t _mask; /* for use with numeric and date types */
00216                 struct {
00217                         uint32_t _count;        /* repeat/line count */
00218                         uint32_t _flags;        /* modifier flags */
00219                 } _s;           /* for use with string types */
00220         } _u;
00221 #define num_mask _u._mask
00222 #define str_count _u._s._count
00223 #define str_flags _u._s._flags
00224 
00225         /* Words 9-16 */
00226         union VALUETYPE {
00227                 uint8_t b;
00228                 uint16_t h;
00229                 uint32_t l;
00230                 uint64_t q;
00231                 uint8_t hs[2];  /* 2 bytes of a fixed-endian "short" */
00232                 uint8_t hl[4];  /* 4 bytes of a fixed-endian "long" */
00233                 uint8_t hq[8];  /* 8 bytes of a fixed-endian "quad" */
00234                 char s[MAXstring];      /* the search string or regex pattern */
00235         } value;                /* either number or string */
00236         /* Words 17..31 */
00237         char desc[MAXDESC];     /* description */
00238 };
00239 
00240 #define BIT(A)   (1 << (A))
00241 #define STRING_COMPACT_BLANK            BIT(0)
00242 #define STRING_COMPACT_OPTIONAL_BLANK   BIT(1)
00243 #define STRING_IGNORE_LOWERCASE         BIT(2)
00244 #define STRING_IGNORE_UPPERCASE         BIT(3)
00245 #define REGEX_OFFSET_START              BIT(4)
00246 #define CHAR_COMPACT_BLANK              'B'
00247 #define CHAR_COMPACT_OPTIONAL_BLANK     'b'
00248 #define CHAR_IGNORE_LOWERCASE           'c'
00249 #define CHAR_IGNORE_UPPERCASE           'C'
00250 #define CHAR_REGEX_OFFSET_START         's'
00251 #define STRING_IGNORE_CASE              (STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
00252 
00253 
00254 /* list of magic entries */
00255 struct mlist {
00256         struct magic *magic;            /* array of magic entries */
00257         uint32_t nmagic;                        /* number of entries in array */
00258         int mapped;  /* allocation type: 0 => apprentice_file
00259                       *                  1 => apprentice_map + malloc
00260                       *                  2 => apprentice_map + mmap */
00261         struct mlist *next, *prev;
00262 };
00263 
00264 struct magic_set {
00265 /*@null@*/
00266     struct mlist *mlist;
00267     struct cont {
00268         size_t len;
00269                 struct level_info {
00270                         int32_t off;
00271                         int got_match;
00272 #ifdef ENABLE_CONDITIONALS
00273                         int last_match;
00274                         int last_cond;  /* used for error checking by parse() */
00275 #endif
00276                 } *li;
00277     } c;
00278     struct out {
00279         /* Accumulation buffer */
00280         char *buf;
00281         char *ptr;
00282                 size_t left;
00283         size_t size;
00284         /* Printable buffer */
00285         char *pbuf;
00286         size_t psize;
00287     } o;
00288         uint32_t offset;
00289     int error;
00290     int flags;
00291     int haderr;
00292     const char *file;
00293         size_t line;                    /* current magic line number */
00294 
00295         /* data for searches */
00296         struct {
00297                 const char *s;          /* start of search in original source */
00298                 size_t s_len;           /* length of search region */
00299                 size_t offset;          /* starting offset in source: XXX - should this be off_t? */
00300                 size_t rm_len;          /* match length */
00301         } search;
00302 
00303         union VALUETYPE ms_value;       /* either number or string */
00304 };
00305 
00306 struct stat;
00307 /*@observer@*/
00308 protected const char *file_fmttime(uint32_t, int)
00309         /*@*/;
00310 protected int file_buffer(struct magic_set *ms, int fd, /*@null@*/ const char *inname, const void *buf,
00311     size_t nb)
00312         /*@globals fileSystem, internalState @*/
00313         /*@modifies ms, fileSystem, internalState @*/;
00314 protected int file_fsmagic(struct magic_set *ms, const char *, struct stat *sb)
00315         /*@modifies ms, sb @*/;
00316 protected int file_pipe2file(struct magic_set *ms, int, const void *, size_t)
00317         /*@globals errno, fileSystem, internalState @*/
00318         /*@modifies ms, errno, fileSystem, internalState @*/;
00319 protected int file_printf(struct magic_set *ms, const char *, ...)
00320         /*@modifies ms @*/;
00321 protected int file_reset(struct magic_set *ms)
00322         /*@modifies ms @*/;
00323 protected int file_tryelf(struct magic_set *ms, int, const unsigned char *,
00324     size_t)
00325         /*@globals fileSystem, internalState @*/
00326         /*@modifies ms, fileSystem, internalState @*/;
00327 protected int file_zmagic(struct magic_set *ms, int fd, /*@null@*/ const char *name,
00328     const unsigned char *buf, size_t nbytes)
00329         /*@globals fileSystem, internalState @*/
00330         /*@modifies ms, fileSystem, internalState @*/;
00331 protected int file_ascmagic(struct magic_set *ms, const unsigned char *, size_t)
00332         /*@modifies ms @*/;
00333 protected int file_is_tar(struct magic_set *ms, const unsigned char *, size_t)
00334         /*@modifies ms @*/;
00335 protected int file_softmagic(struct magic_set *ms, const unsigned char *, size_t)
00336         /*@globals fileSystem @*/
00337         /*@modifies ms, fileSystem @*/;
00338 /*@null@*/
00339 protected struct mlist *file_apprentice(struct magic_set *ms, const char *, int)
00340         /*@globals fileSystem, internalState @*/
00341         /*@modifies ms, fileSystem, internalState @*/;
00342 protected uint64_t file_signextend(struct magic_set *ms, struct magic *,
00343     uint64_t)
00344         /*@globals fileSystem @*/
00345         /*@modifies ms, fileSystem @*/;
00346 protected void file_delmagic(/*@only@*/ struct magic *p, int type, size_t entries)
00347         /*@globals fileSystem @*/
00348         /*@modifies p, fileSystem @*/;
00349 protected void file_badread(struct magic_set *ms)
00350         /*@modifies ms @*/;
00351 protected void file_badseek(struct magic_set *ms)
00352         /*@modifies ms @*/;
00353 protected void file_oomem(struct magic_set *ms, size_t)
00354         /*@modifies ms @*/;
00355 protected void file_error(struct magic_set *ms, int error, const char *f, ...)
00356         /*@modifies ms @*/;
00357 protected void file_magerror(struct magic_set *ms, const char *, ...)
00358         /*@modifies ms @*/;
00359 protected void file_magwarn(struct magic_set *ms, const char *, ...)
00360         /*@globals stdout, stderr, fileSystem @*/
00361         /*@modifies ms, stdout, stderr, fileSystem @*/;
00362 protected void file_mdump(struct magic *)
00363         /*@globals fileSystem @*/
00364         /*@modifies fileSystem @*/;
00365 protected void file_showstr(FILE *fp, const char *, size_t)
00366         /*@globals fileSystem @*/
00367         /*@modifies fp, fileSystem @*/;
00368 protected size_t file_mbswidth(const char *)
00369         /*@*/;
00370 /*@observer@*/
00371 protected const char *file_getbuffer(struct magic_set *ms)
00372         /*@modifies ms @*/;
00373 protected ssize_t sread(int fd, void *buf, size_t n, int canbepipe)
00374         /*@modifies buf @*/;
00375 protected int file_check_mem(struct magic_set *ms, unsigned int)
00376         /*@modifies ms @*/;
00377 
00378 #ifndef COMPILE_ONLY
00379 extern const char *file_names[];
00380 extern const size_t file_nnames;
00381 #endif
00382 
00383 #ifndef HAVE_STRERROR
00384 extern int sys_nerr;
00385 extern char *sys_errlist[];
00386 #define strerror(e) \
00387         (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
00388 #endif
00389 
00390 #ifndef HAVE_STRTOUL
00391 #define strtoul(a, b, c)        strtol(a, b, c)
00392 #endif
00393 
00394 #ifndef HAVE_SNPRINTF
00395 int snprintf(char *, size_t, const char *, ...)
00396         /*@*/;
00397 #endif
00398 
00399 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
00400 #define QUICK
00401 #endif
00402 
00403 #ifndef O_BINARY
00404 #define O_BINARY        0
00405 #endif
00406 
00407 #define FILE_RCSID(id) \
00408 static const char *rcsid(const char *p) /*@*/ { \
00409         return rcsid(p = id); \
00410 }
00411 
00412 #endif /* __file_h__ */

Generated on Fri Aug 31 10:38:31 2007 for rpm by  doxygen 1.5.1