Back to index

php5  5.3.10
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.119 2009/02/04 18:24:32 christos Exp $
00031  */
00032 
00033 #ifndef __file_h__
00034 #define __file_h__
00035 
00036 #include "config.h"
00037 
00038 #include <stdio.h>   /* Include that here, to make sure __P gets defined */
00039 #include <errno.h>
00040 #include <fcntl.h>   /* For open and flags */
00041 #ifdef HAVE_STDINT_H
00042 #include <stdint.h>
00043 #endif
00044 #ifdef HAVE_INTTYPES_H
00045 #include <inttypes.h>
00046 #endif
00047 #ifdef PHP_WIN32
00048 #include "win32/php_stdint.h"
00049 #endif
00050 
00051 #include "php.h"
00052 #include "ext/standard/php_string.h"
00053 #include "ext/pcre/php_pcre.h"
00054 
00055 #include <sys/types.h>
00056 #ifdef PHP_WIN32
00057 #include "win32/param.h"
00058 #else
00059 #include <sys/param.h>
00060 #endif
00061 /* Do this here and now, because struct stat gets re-defined on solaris */
00062 #include <sys/stat.h>
00063 #include <stdarg.h>
00064 
00065 #define ENABLE_CONDITIONALS
00066 
00067 #ifndef MAGIC
00068 #define MAGIC "/etc/magic"
00069 #endif
00070 
00071 #if defined(__EMX__) || defined(PHP_WIN32)
00072 #define PATHSEP      ';'
00073 #else
00074 #define PATHSEP      ':'
00075 #endif
00076 
00077 #define private static
00078 #ifndef protected
00079 #define protected
00080 #endif
00081 #define public
00082 
00083 #ifndef __GNUC_PREREQ__
00084 #ifdef __GNUC__
00085 #define       __GNUC_PREREQ__(x, y)                                          \
00086        ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||                 \
00087         (__GNUC__ > (x)))
00088 #else
00089 #define       __GNUC_PREREQ__(x, y)       0
00090 #endif
00091 #endif
00092 
00093 #ifndef MIN
00094 #define       MIN(a,b)      (((a) < (b)) ? (a) : (b))
00095 #endif
00096 
00097 #ifndef MAX
00098 #define       MAX(a,b)      (((a) > (b)) ? (a) : (b))
00099 #endif
00100 
00101 #ifndef HOWMANY
00102 # define HOWMANY (256 * 1024)      /* how much of the file to look at */
00103 #endif
00104 #define MAXMAGIS 8192              /* max entries in any one magic file
00105                                or directory */
00106 #define MAXDESC      64            /* max leng of text description/MIME type */
00107 #define MAXstring 32        /* max leng of "string" types */
00108 
00109 #define MAGICNO             0xF11E041C
00110 #define VERSIONNO    7
00111 #define FILE_MAGICSIZE      200
00112 
00113 #define       FILE_LOAD     0
00114 #define FILE_CHECK   1
00115 #define FILE_COMPILE 2
00116 
00117 union VALUETYPE {
00118        uint8_t b;
00119        uint16_t h;
00120        uint32_t l;
00121        uint64_t q;
00122        uint8_t hs[2];       /* 2 bytes of a fixed-endian "short" */
00123        uint8_t hl[4];       /* 4 bytes of a fixed-endian "long" */
00124        uint8_t hq[8];       /* 8 bytes of a fixed-endian "quad" */
00125        char s[MAXstring];   /* the search string or regex pattern */
00126        unsigned char us[MAXstring];
00127        float f;
00128        double d;
00129 };
00130 
00131 struct magic {
00132        /* Word 1 */
00133        uint16_t cont_level; /* level of ">" */
00134        uint8_t flag;
00135 #define INDIR        0x01   /* if '(...)' appears */
00136 #define OFFADD              0x02   /* if '>&' or '>...(&' appears */
00137 #define INDIROFFADD  0x04   /* if '>&(' appears */
00138 #define UNSIGNED     0x08   /* comparison is unsigned */
00139 #define NOSPACE             0x10   /* suppress space character before output */
00140 #define BINTEST             0x20   /* test is for a binary type (set only
00141                                for top-level tests) */
00142 #define TEXTTEST     0      /* for passing to file_softmagic */
00143 
00144        uint8_t factor;
00145 
00146        /* Word 2 */
00147        uint8_t reln;        /* relation (0=eq, '>'=gt, etc) */
00148        uint8_t vallen;             /* length of string value, if any */
00149        uint8_t type;        /* comparison type (FILE_*) */
00150        uint8_t in_type;     /* type of indirection */
00151 #define                     FILE_INVALID  0
00152 #define                     FILE_BYTE     1
00153 #define                            FILE_SHORT    2
00154 #define                            FILE_DEFAULT  3
00155 #define                            FILE_LONG     4
00156 #define                            FILE_STRING   5
00157 #define                            FILE_DATE     6
00158 #define                            FILE_BESHORT  7
00159 #define                            FILE_BELONG   8
00160 #define                            FILE_BEDATE   9
00161 #define                            FILE_LESHORT  10
00162 #define                            FILE_LELONG   11
00163 #define                            FILE_LEDATE   12
00164 #define                            FILE_PSTRING  13
00165 #define                            FILE_LDATE    14
00166 #define                            FILE_BELDATE  15
00167 #define                            FILE_LELDATE  16
00168 #define                            FILE_REGEX    17
00169 #define                            FILE_BESTRING16      18
00170 #define                            FILE_LESTRING16      19
00171 #define                            FILE_SEARCH   20
00172 #define                            FILE_MEDATE   21
00173 #define                            FILE_MELDATE  22
00174 #define                            FILE_MELONG   23
00175 #define                            FILE_QUAD     24
00176 #define                            FILE_LEQUAD   25
00177 #define                            FILE_BEQUAD   26
00178 #define                            FILE_QDATE    27
00179 #define                            FILE_LEQDATE  28
00180 #define                            FILE_BEQDATE  29
00181 #define                            FILE_QLDATE   30
00182 #define                            FILE_LEQLDATE 31
00183 #define                            FILE_BEQLDATE 32
00184 #define                            FILE_FLOAT    33
00185 #define                            FILE_BEFLOAT  34
00186 #define                            FILE_LEFLOAT  35
00187 #define                            FILE_DOUBLE   36
00188 #define                            FILE_BEDOUBLE 37
00189 #define                            FILE_LEDOUBLE 38
00190 #define                            FILE_BEID3    39
00191 #define                            FILE_LEID3    40
00192 #define                            FILE_INDIRECT 41
00193 #define                            FILE_NAMES_SIZE      42/* size of array to contain all names */
00194 
00195 #define IS_LIBMAGIC_STRING(t) \
00196        ((t) == FILE_STRING || \
00197         (t) == FILE_PSTRING || \
00198         (t) == FILE_BESTRING16 || \
00199         (t) == FILE_LESTRING16 || \
00200         (t) == FILE_REGEX || \
00201         (t) == FILE_SEARCH || \
00202         (t) == FILE_DEFAULT)
00203 
00204 #define FILE_FMT_NONE 0
00205 #define FILE_FMT_NUM  1 /* "cduxXi" */
00206 #define FILE_FMT_STR  2 /* "s" */
00207 #define FILE_FMT_QUAD 3 /* "ll" */
00208 #define FILE_FMT_FLOAT 4 /* "eEfFgG" */
00209 #define FILE_FMT_DOUBLE 5 /* "eEfFgG" */
00210 
00211        /* Word 3 */
00212        uint8_t in_op;              /* operator for indirection */
00213        uint8_t mask_op;     /* operator for mask */
00214 #ifdef ENABLE_CONDITIONALS
00215        uint8_t cond;        /* conditional type */
00216 #else
00217        uint8_t dummy;       
00218 #endif
00219        uint8_t factor_op;
00220 #define              FILE_FACTOR_OP_PLUS  '+'
00221 #define              FILE_FACTOR_OP_MINUS '-'
00222 #define              FILE_FACTOR_OP_TIMES '*'
00223 #define              FILE_FACTOR_OP_DIV   '/'
00224 #define              FILE_FACTOR_OP_NONE  '\0'
00225 
00226 #define                            FILE_OPS      "&|^+-*/%"
00227 #define                            FILE_OPAND    0
00228 #define                            FILE_OPOR     1
00229 #define                            FILE_OPXOR    2
00230 #define                            FILE_OPADD    3
00231 #define                            FILE_OPMINUS  4
00232 #define                            FILE_OPMULTIPLY      5
00233 #define                            FILE_OPDIVIDE 6
00234 #define                            FILE_OPMODULO 7
00235 #define                            FILE_OPS_MASK 0x07 /* mask for above ops */
00236 #define                            FILE_UNUSED_1 0x08
00237 #define                            FILE_UNUSED_2 0x10
00238 #define                            FILE_UNUSED_3 0x20
00239 #define                            FILE_OPINVERSE       0x40
00240 #define                            FILE_OPINDIRECT      0x80
00241 
00242 #ifdef ENABLE_CONDITIONALS
00243 #define                            COND_NONE     0
00244 #define                            COND_IF              1
00245 #define                            COND_ELIF     2
00246 #define                            COND_ELSE     3
00247 #endif /* ENABLE_CONDITIONALS */
00248 
00249        /* Word 4 */
00250        uint32_t offset;     /* offset to magic number */
00251        /* Word 5 */
00252        int32_t in_offset;   /* offset from indirection */
00253        /* Word 6 */
00254        uint32_t lineno;     /* line number in magic file */
00255        /* Word 7,8 */
00256        union {
00257               uint64_t _mask;      /* for use with numeric and date types */
00258               struct {
00259                      uint32_t _count;     /* repeat/line count */
00260                      uint32_t _flags;     /* modifier flags */
00261               } _s;         /* for use with string types */
00262        } _u;
00263 #define num_mask _u._mask
00264 #define str_range _u._s._count
00265 #define str_flags _u._s._flags
00266        /* Words 9-16 */
00267        union VALUETYPE value;      /* either number or string */
00268        /* Words 17-24 */
00269        char desc[MAXDESC];  /* description */
00270        /* Words 25-32 */
00271        char mimetype[MAXDESC]; /* MIME type */
00272        /* Words 33-34 */
00273        char apple[8];
00274 };
00275 
00276 #define BIT(A)   (1 << (A))
00277 #define STRING_COMPACT_BLANK              BIT(0)
00278 #define STRING_COMPACT_OPTIONAL_BLANK     BIT(1)
00279 #define STRING_IGNORE_LOWERCASE           BIT(2)
00280 #define STRING_IGNORE_UPPERCASE           BIT(3)
00281 #define REGEX_OFFSET_START         BIT(4)
00282 #define CHAR_COMPACT_BLANK         'B'
00283 #define CHAR_COMPACT_OPTIONAL_BLANK       'b'
00284 #define CHAR_IGNORE_LOWERCASE             'c'
00285 #define CHAR_IGNORE_UPPERCASE             'C'
00286 #define CHAR_REGEX_OFFSET_START           's'
00287 #define STRING_IGNORE_CASE         (STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
00288 #define STRING_DEFAULT_RANGE              100
00289 
00290 
00291 /* list of magic entries */
00292 struct mlist {
00293        struct magic *magic;        /* array of magic entries */
00294        uint32_t nmagic;                   /* number of entries in array */
00295        int mapped;  /* allocation type: 0 => apprentice_file
00296                     *                  1 => apprentice_map + malloc
00297                     *                  2 => apprentice_map + mmap */
00298        struct mlist *next, *prev;
00299 };
00300 
00301 #ifdef __cplusplus
00302 #define CAST(T, b)   static_cast<T>(b)
00303 #else
00304 #define CAST(T, b)   (b)
00305 #endif
00306 
00307 struct level_info {
00308        int32_t off;
00309        int got_match;
00310 #ifdef ENABLE_CONDITIONALS
00311        int last_match;
00312        int last_cond;       /* used for error checking by parse() */
00313 #endif
00314 };
00315 struct magic_set {
00316        struct mlist *mlist;
00317        struct cont {
00318               size_t len;
00319               struct level_info *li;
00320        } c;
00321        struct out {
00322               char *buf;           /* Accumulation buffer */
00323               char *pbuf;          /* Printable buffer */
00324        } o;
00325        uint32_t offset;
00326        int error;
00327        int flags;                  /* Control magic tests. */
00328        int event_flags;            /* Note things that happened. */
00329 #define              EVENT_HAD_ERR        0x01
00330        const char *file;
00331        size_t line;                /* current magic line number */
00332 
00333        /* data for searches */
00334        struct {
00335               const char *s;              /* start of search in original source */
00336               size_t s_len;        /* length of search region */
00337               size_t offset;              /* starting offset in source: XXX - should this be off_t? */
00338               size_t rm_len;              /* match length */
00339        } search;
00340 
00341        /* FIXME: Make the string dynamically allocated so that e.g.
00342           strings matched in files can be longer than MAXstring */
00343        union VALUETYPE ms_value;   /* either number or string */
00344 };
00345 
00346 /* Type for Unicode characters */
00347 typedef unsigned long unichar;
00348 
00349 struct stat;
00350 protected const char *file_fmttime(uint32_t, int);
00351 protected int file_buffer(struct magic_set *, php_stream *, const char *, const void *,
00352     size_t);
00353 protected int file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb, php_stream *stream);
00354 protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
00355 protected int file_printf(struct magic_set *, const char *, ...);
00356 protected int file_reset(struct magic_set *);
00357 protected int file_tryelf(struct magic_set *, int, const unsigned char *,
00358     size_t);
00359 protected int file_trycdf(struct magic_set *, int, const unsigned char *,
00360     size_t);
00361 #ifdef PHP_FILEINFO_UNCOMPRESS 
00362 protected int file_zmagic(struct magic_set *, int, const char *,
00363     const unsigned char *, size_t);
00364 #endif
00365 protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
00366 protected int file_ascmagic_with_encoding(struct magic_set *,
00367     const unsigned char *, size_t, unichar *, size_t, const char *,
00368     const char *);
00369 protected int file_encoding(struct magic_set *, const unsigned char *, size_t,
00370     unichar **, size_t *, const char **, const char **, const char **);
00371 protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
00372 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
00373     int);
00374 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
00375 protected uint64_t file_signextend(struct magic_set *, struct magic *,
00376     uint64_t);
00377 protected void file_delmagic(struct magic *, int type, size_t entries);
00378 protected void file_badread(struct magic_set *);
00379 protected void file_badseek(struct magic_set *);
00380 protected void file_oomem(struct magic_set *, size_t);
00381 protected void file_error(struct magic_set *, int, const char *, ...);
00382 protected void file_magerror(struct magic_set *, const char *, ...);
00383 protected void file_magwarn(struct magic_set *, const char *, ...);
00384 protected void file_showstr(FILE *, const char *, size_t);
00385 protected size_t file_mbswidth(const char *);
00386 protected const char *file_getbuffer(struct magic_set *);
00387 protected ssize_t sread(int, void *, size_t, int);
00388 protected int file_check_mem(struct magic_set *, unsigned int);
00389 protected int file_looks_utf8(const unsigned char *, size_t, unichar *,
00390     size_t *);
00391 #ifdef __EMX__
00392 protected int file_os2_apptype(struct magic_set *, const char *, const void *,
00393     size_t);
00394 #endif /* __EMX__ */
00395 
00396 extern const char *file_names[];
00397 extern const size_t file_nnames;
00398 
00399 #ifndef HAVE_STRERROR
00400 extern int sys_nerr;
00401 extern char *sys_errlist[];
00402 #define strerror(e) \
00403        (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
00404 #endif
00405 
00406 #ifndef HAVE_STRTOUL
00407 #define strtoul(a, b, c)    strtol(a, b, c)
00408 #endif
00409 
00410 #ifndef strlcpy
00411 size_t strlcpy(char *dst, const char *src, size_t siz);
00412 #endif
00413 #ifndef strlcat
00414 size_t strlcat(char *dst, const char *src, size_t siz);
00415 #endif
00416 
00417 
00418 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
00419 #define QUICK
00420 #endif
00421 
00422 #ifndef O_BINARY
00423 #define O_BINARY     0
00424 #endif
00425 
00426 #ifndef __cplusplus
00427 #ifdef __GNUC__
00428 #define FILE_RCSID(id) \
00429 static const char rcsid[] __attribute__((__used__)) = id;
00430 #else
00431 #define FILE_RCSID(id) \
00432 static const char *rcsid(const char *p) { \
00433        return rcsid(p = id); \
00434 }
00435 #endif
00436 #else
00437 #define FILE_RCSID(id)
00438 #endif
00439 
00440 #endif /* __file_h__ */