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  * @(#)$Id: file.h,v 1.72 2005/10/17 15:36:22 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 /* Do this here and now, because struct stat gets re-defined on solaris */
00050 #include <sys/stat.h>
00051 
00052 #ifndef MAGIC
00053 #define MAGIC "/etc/magic"
00054 #endif
00055 
00056 #ifdef __EMX__
00057 #define PATHSEP ';'
00058 #else
00059 #define PATHSEP ':'
00060 #endif
00061 
00062 #define private static
00063 #ifndef protected
00064 #define protected
00065 #endif
00066 #define public
00067 
00068 #ifndef HOWMANY
00069 # define HOWMANY (256 * 1024)   /* how much of the file to look at */
00070 #endif
00071 #define MAXMAGIS 8192           /* max entries in /etc/magic */
00072 #define MAXDESC 64              /* max leng of text description */
00073 #define MAXstring 32            /* max leng of "string" types */
00074 
00075 #define MAGICNO         0xF11E041C
00076 #define VERSIONNO       2
00077 #define FILE_MAGICSIZE  (32 * 4)
00078 
00079 #define FILE_LOAD       0
00080 #define FILE_CHECK      1
00081 #define FILE_COMPILE    2
00082 
00083 struct magic {
00084         /* Word 1 */
00085         uint16_t cont_level;    /* level of ">" */
00086         uint8_t nospflag;       /* supress space character */
00087         uint8_t flag;
00088 #define INDIR   1               /* if '>(...)' appears,  */
00089 #define UNSIGNED 2              /* comparison is unsigned */
00090 #define OFFADD  4               /* if '>&' appears,  */
00091 #define INDIROFFADD     8       /* if '>&(' appears,  */
00092         /* Word 2 */
00093         uint8_t reln;           /* relation (0=eq, '>'=gt, etc) */
00094         uint8_t vallen;         /* length of string value, if any */
00095         uint8_t type;           /* int, short, long or string. */
00096         uint8_t in_type;        /* type of indirrection */
00097 #define                         FILE_BYTE       1
00098 #define                         FILE_SHORT      2
00099 #define                         FILE_LONG       4
00100 #define                         FILE_STRING     5
00101 #define                         FILE_DATE       6
00102 #define                         FILE_BESHORT    7
00103 #define                         FILE_BELONG     8
00104 #define                         FILE_BEDATE     9
00105 #define                         FILE_LESHORT    10
00106 #define                         FILE_LELONG     11
00107 #define                         FILE_LEDATE     12
00108 #define                         FILE_PSTRING    13
00109 #define                         FILE_LDATE      14
00110 #define                         FILE_BELDATE    15
00111 #define                         FILE_LELDATE    16
00112 #define                         FILE_REGEX      17
00113 #define                         FILE_BESTRING16 18
00114 #define                         FILE_LESTRING16 19
00115 #define                         FILE_SEARCH     20
00116 
00117 #define                         FILE_FORMAT_NAME        \
00118 /* 0 */                         "invalid 0",            \
00119 /* 1 */                         "byte",                 \
00120 /* 2 */                         "short",                \
00121 /* 3 */                         "invalid 3",            \
00122 /* 4 */                         "long",                 \
00123 /* 5 */                         "string",               \
00124 /* 6 */                         "date",                 \
00125 /* 7 */                         "beshort",              \
00126 /* 8 */                         "belong",               \
00127 /* 9 */                         "bedate",               \
00128 /* 10 */                        "leshort",              \
00129 /* 11 */                        "lelong",               \
00130 /* 12 */                        "ledate",               \
00131 /* 13 */                        "pstring",              \
00132 /* 14 */                        "ldate",                \
00133 /* 15 */                        "beldate",              \
00134 /* 16 */                        "leldate",              \
00135 /* 17 */                        "regex",                \
00136 /* 18 */                        "bestring16",           \
00137 /* 19 */                        "lestring16",           \
00138 /* 20 */                        "search",
00139 
00140 #define FILE_FMT_NUM    "cduxXi"
00141 #define FILE_FMT_STR    "s"     
00142 
00143 #define                         FILE_FORMAT_STRING      \
00144 /* 0 */                         NULL,                   \
00145 /* 1 */                         FILE_FMT_NUM,           \
00146 /* 2 */                         FILE_FMT_NUM,           \
00147 /* 3 */                         NULL,                   \
00148 /* 4 */                         FILE_FMT_NUM,           \
00149 /* 5 */                         FILE_FMT_STR,           \
00150 /* 6 */                         FILE_FMT_STR,           \
00151 /* 7 */                         FILE_FMT_NUM,           \
00152 /* 8 */                         FILE_FMT_NUM,           \
00153 /* 9 */                         FILE_FMT_STR,           \
00154 /* 10 */                        FILE_FMT_NUM,           \
00155 /* 11 */                        FILE_FMT_NUM,           \
00156 /* 12 */                        FILE_FMT_STR,           \
00157 /* 13 */                        FILE_FMT_STR,           \
00158 /* 14 */                        FILE_FMT_STR,           \
00159 /* 15 */                        FILE_FMT_STR,           \
00160 /* 16 */                        FILE_FMT_STR,           \
00161 /* 17 */                        FILE_FMT_STR,           \
00162 /* 18 */                        FILE_FMT_STR,           \
00163 /* 19 */                        FILE_FMT_STR,           \
00164 /* 20 */                        FILE_FMT_STR,
00165 
00166         /* Word 3 */
00167         uint8_t in_op;          /* operator for indirection */
00168         uint8_t mask_op;        /* operator for mask */
00169         uint8_t dummy1; 
00170         uint8_t dummy2; 
00171 #define                         FILE_OPS        "&|^+-*/%"
00172 #define                         FILE_OPAND      0
00173 #define                         FILE_OPOR       1
00174 #define                         FILE_OPXOR      2
00175 #define                         FILE_OPADD      3
00176 #define                         FILE_OPMINUS    4
00177 #define                         FILE_OPMULTIPLY 5
00178 #define                         FILE_OPDIVIDE   6
00179 #define                         FILE_OPMODULO   7
00180 #define                         FILE_OPINVERSE  0x40
00181 #define                         FILE_OPINDIRECT 0x80
00182         /* Word 4 */
00183         uint32_t offset;        /* offset to magic number */
00184         /* Word 5 */
00185         int32_t in_offset;      /* offset from indirection */
00186         /* Word 6 */
00187         uint32_t mask;  /* mask before comparison with value */
00188         /* Word 7 */
00189         uint32_t dummy3;
00190         /* Word 8 */
00191         uint32_t dummp4;
00192         /* Words 9-16 */
00193         union VALUETYPE {
00194                 uint8_t b;
00195                 uint16_t h;
00196                 uint32_t l;
00197                 char s[MAXstring];
00198                 struct {
00199 /*@relnull@*/
00200                         char *buf;
00201                         size_t buflen;
00202                 } search;
00203                 uint8_t hs[2];  /* 2 bytes of a fixed-endian "short" */
00204                 uint8_t hl[4];  /* 4 bytes of a fixed-endian "long" */
00205         } value;                /* either number or string */
00206         /* Words 17..31 */
00207         char desc[MAXDESC];     /* description */
00208 };
00209 
00210 #define BIT(A)   (1 << (A))
00211 #define STRING_IGNORE_LOWERCASE         BIT(0)
00212 #define STRING_COMPACT_BLANK            BIT(1)
00213 #define STRING_COMPACT_OPTIONAL_BLANK   BIT(2)
00214 #define CHAR_IGNORE_LOWERCASE           'c'
00215 #define CHAR_COMPACT_BLANK              'B'
00216 #define CHAR_COMPACT_OPTIONAL_BLANK     'b'
00217 
00218 
00219 /* list of magic entries */
00220 struct mlist {
00221         struct magic *magic;            /* array of magic entries */
00222         uint32_t nmagic;                        /* number of entries in array */
00223         int mapped;  /* allocation type: 0 => apprentice_file
00224                       *                  1 => apprentice_map + malloc
00225                       *                  2 => apprentice_map + mmap */
00226 /*@dependent@*/
00227         struct mlist *next, *prev;
00228 };
00229 
00230 struct magic_set {
00231 /*@relnull@*/
00232     struct mlist *mlist;
00233     struct cont {
00234         size_t len;
00235 /*@relnull@*/
00236         int32_t *off;
00237     } c;
00238     struct out {
00239         /* Accumulation buffer */
00240 /*@owned@*/ /*@relnull@*/
00241         char *buf;
00242 /*@dependent@*/ /*@relnull@*/
00243         char *ptr;
00244         size_t left;
00245         size_t size;
00246         /* Printable buffer */
00247 /*@only@*/
00248         char *pbuf;
00249         size_t psize;
00250     } o;
00251     int error;
00252     int flags;
00253     int haderr;
00254     const char *file;
00255     size_t line;
00256 };
00257 
00258 struct stat;
00259 /*@observer@*/
00260 protected const char *file_fmttime(uint32_t, int)
00261         /*@*/;
00262 protected int file_buffer(struct magic_set *ms, int, const void *, size_t)
00263         /*@globals fileSystem, internalState @*/
00264         /*@modifies ms, fileSystem, internalState @*/;
00265 protected int file_fsmagic(struct magic_set *ms, /*@null@*/ const char *fn, struct stat *sb)
00266         /*@modifies ms, sb @*/;
00267 protected int file_pipe2file(struct magic_set *ms, int fd, const void *startbuf, size_t nbytes)
00268         /*@globals errno, fileSystem, internalState @*/
00269         /*@modifies ms, errno, fileSystem, internalState @*/;
00270 protected int file_printf(struct magic_set *ms, const char *, ...)
00271         /*@modifies ms @*/;
00272 protected int file_reset(struct magic_set *ms)
00273         /*@modifies ms @*/;
00274 protected int file_tryelf(struct magic_set *ms, int, const unsigned char *, size_t)
00275         /*@globals fileSystem, internalState @*/
00276         /*@modifies ms, fileSystem, internalState @*/;
00277 protected int file_zmagic(struct magic_set *ms, int, const unsigned char *, size_t)
00278         /*@globals fileSystem, internalState @*/
00279         /*@modifies ms, fileSystem, internalState @*/;
00280 protected int file_ascmagic(struct magic_set *ms, const unsigned char *, size_t)
00281         /*@modifies ms @*/;
00282 protected int file_is_tar(struct magic_set *ms, const unsigned char *, size_t)
00283         /*@modifies ms @*/;
00284 protected int file_softmagic(struct magic_set *ms, const unsigned char *, size_t)
00285         /*@globals fileSystem @*/
00286         /*@modifies ms, fileSystem @*/;
00287 /*@null@*/
00288 protected struct mlist *file_apprentice(struct magic_set *ms, const char *, int)
00289         /*@globals fileSystem, internalState @*/
00290         /*@modifies ms, fileSystem, internalState @*/;
00291 protected uint32_t file_signextend(struct magic_set *ms, struct magic *, uint32_t)
00292         /*@globals fileSystem @*/
00293         /*@modifies ms, fileSystem @*/;
00294 protected void file_delmagic(struct magic *p, int type, size_t entries)
00295         /*@globals fileSystem @*/
00296         /*@modifies p, fileSystem @*/;
00297 protected void file_badread(struct magic_set *ms)
00298         /*@modifies ms @*/;
00299 protected void file_badseek(struct magic_set *ms)
00300         /*@modifies ms @*/;
00301 protected void file_oomem(struct magic_set *ms)
00302         /*@modifies ms @*/;
00303 protected void file_error(struct magic_set *ms, int, /*@null@*/ const char *, ...)
00304         /*@modifies ms @*/;
00305 protected void file_magwarn(struct magic_set *ms, /*@null@*/ const char *, ...)
00306         /*@globals fileSystem @*/
00307         /*@modifies fileSystem @*/;
00308 protected void file_mdump(struct magic *m)
00309         /*@globals fileSystem @*/
00310         /*@modifies m, fileSystem @*/;
00311 protected void file_showstr(FILE *fp, const char *, size_t)
00312         /*@globals fileSystem @*/
00313         /*@modifies fp, fileSystem @*/;
00314 protected size_t file_mbswidth(const char *)
00315         /*@*/;
00316 /*@observer@*/
00317 protected const char *file_getbuffer(struct magic_set *ms)
00318         /*@modifies ms @*/;
00319 
00320 #ifndef HAVE_STRERROR
00321 extern int sys_nerr;
00322 extern char *sys_errlist[];
00323 #define strerror(e) \
00324         (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
00325 #endif
00326 
00327 #ifndef HAVE_STRTOUL
00328 #define strtoul(a, b, c)        strtol(a, b, c)
00329 #endif
00330 
00331 #if 0 /* HACK -- we have snprintf and I don't want to plumb the autofoo */
00332 int snprintf(char *, size_t, const char *, ...);
00333 #endif
00334 
00335 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
00336 #define QUICK
00337 #endif
00338 
00339 #ifndef O_BINARY
00340 #define O_BINARY        0
00341 #endif
00342 
00343 #define FILE_RCSID(id) \
00344 static const char *rcsid(const char *p) { \
00345         return rcsid(p = id); \
00346 }
00347 #else
00348 
00349 #endif /* __file_h__ */

Generated on Fri Oct 12 08:44:53 2007 for rpm by  doxygen 1.5.2