Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

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  * 3. All advertising materials mentioning features or use of this software
00016  *    must display the following acknowledgement:
00017  *    This product includes software developed by Ian F. Darwin and others.
00018  * 4. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *  
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00022  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
00025  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  */
00033 /*
00034  * file.h - definitions for file(1) program
00035  * @(#) Id: file.h,v 1.53 2003/03/28 21:02:03 christos Exp 
00036  */
00037 
00038 #ifndef __file_h__
00039 #define __file_h__
00040 
00041 /*@-redef@*/
00042 
00043 #ifndef HOWMANY
00044 # define HOWMANY 65536          /* how much of the file to look at */
00045 #endif
00046 #define MAXMAGIS 4096           /* max entries in /etc/magic */
00047 #define MAXDESC 64              /* max leng of text description */
00048 #define MAXstring 32            /* max leng of "string" types */
00049 
00050 #define MAGICNO         0xF11E041C
00051 #define VERSIONNO       2
00052 #define FILE_MAGICSIZE  (32 * 4)
00053 
00054 #define FILE_CHECK      1
00055 #define FILE_COMPILE    2
00056 
00057 struct magic {
00058         /* Word 1 */
00059         uint16_t cont_level;    /* level of ">" */
00060         uint8_t nospflag;       /* supress space character */
00061         uint8_t flag;
00062 #define INDIR   1               /* if '>(...)' appears,  */
00063 #define UNSIGNED 2              /* comparison is unsigned */
00064 #define OFFADD  4               /* if '>&' appears,  */
00065         /* Word 2 */
00066         uint8_t reln;           /* relation (0=eq, '>'=gt, etc) */
00067         uint8_t vallen;         /* length of string value, if any */
00068         uint8_t type;           /* int, short, long or string. */
00069         uint8_t in_type;        /* type of indirrection */
00070 #define                         FILE_BYTE       1
00071 #define                         FILE_SHORT      2
00072 #define                         FILE_LONG       4
00073 #define                         FILE_STRING     5
00074 #define                         FILE_DATE       6
00075 #define                         FILE_BESHORT    7
00076 #define                         FILE_BELONG     8
00077 #define                         FILE_BEDATE     9
00078 #define                         FILE_LESHORT    10
00079 #define                         FILE_LELONG     11
00080 #define                         FILE_LEDATE     12
00081 #define                         FILE_PSTRING    13
00082 #define                         FILE_LDATE      14
00083 #define                         FILE_BELDATE    15
00084 #define                         FILE_LELDATE    16
00085 #define                         FILE_REGEX      17
00086         /* Word 3 */
00087         uint8_t in_op;          /* operator for indirection */
00088         uint8_t mask_op;        /* operator for mask */
00089         uint8_t dummy1;
00090         uint8_t dummy2;
00091 #define                         FILE_OPS        "&|^+-*%/"
00092 #define                         FILE_OPAND      0
00093 #define                         FILE_OPOR       1
00094 #define                         FILE_OPXOR      2
00095 #define                         FILE_OPADD      3
00096 #define                         FILE_OPMINUS    4
00097 #define                         FILE_OPMULTIPLY 5
00098 #define                         FILE_OPDIVIDE   6
00099 #define                         FILE_OPMODULO   7
00100 #define                         FILE_OPINVERSE  0x80
00101         /* Word 4 */
00102         int32_t offset;         /* offset to magic number */
00103         /* Word 5 */
00104         int32_t in_offset;      /* offset from indirection */
00105         /* Word 6 */
00106         uint32_t mask;  /* mask before comparison with value */
00107         /* Word 7 */
00108         uint32_t dummy3;
00109         /* Word 8 */
00110         uint32_t dummp4;
00111         /* Words 9-16 */
00112         union VALUETYPE {
00113                 uint8_t b;
00114                 uint16_t h;
00115                 uint32_t l;
00116                 char s[MAXstring];
00117                 const char * buf;
00118                 uint8_t hs[2];  /* 2 bytes of a fixed-endian "short" */
00119                 uint8_t hl[4];  /* 4 bytes of a fixed-endian "long" */
00120         } value;                /* either number or string */
00121         /* Words 17..31 */
00122         char desc[MAXDESC];     /* description */
00123 };
00124 
00125 #define BIT(A)   (1 << (A))
00126 #define STRING_IGNORE_LOWERCASE         BIT(0)
00127 #define STRING_COMPACT_BLANK            BIT(1)
00128 #define STRING_COMPACT_OPTIONAL_BLANK   BIT(2)
00129 #define CHAR_IGNORE_LOWERCASE           'c'
00130 #define CHAR_COMPACT_BLANK              'B'
00131 #define CHAR_COMPACT_OPTIONAL_BLANK     'b'
00132 
00133 
00134 /* list of magic entries */
00135 struct mlist {
00136         struct magic *magic;            /* array of magic entries */
00137         uint32_t nmagic;                /* number of entries in array */
00138         int mapped;  /* allocation type: 0 => apprentice_file
00139                       *                  1 => apprentice_map + malloc
00140                       *                  2 => apprentice_map + mmap */
00141 /*@relnull@*/
00142         struct mlist *next;
00143 /*@relnull@*/
00144         struct mlist *prev;
00145 };
00146 
00147 struct magic_set {
00148     struct mlist *mlist;
00149     struct cont {
00150         size_t len;
00151         int32_t *off;
00152     } c;
00153     struct out {
00154         char *buf;
00155         char *ptr;
00156         size_t len;
00157         size_t size;
00158     } o;
00159     int flags;
00160     int haderr;
00161 };
00162 
00163 enum fmagicFlags_e {
00164 /*@-enummemuse@*/
00165     FMAGIC_FLAGS_NONE           = 0,
00166 /*@=enummemuse@*/
00167     FMAGIC_FLAGS_DEBUG          = (1 << 0),
00168     FMAGIC_FLAGS_BRIEF          = (1 << 1),     
00169     FMAGIC_FLAGS_MIME           = (1 << 2),     
00170     FMAGIC_FLAGS_CONTINUE       = (1 << 3),     
00171     FMAGIC_FLAGS_FOLLOW         = (1 << 4),     
00172     FMAGIC_FLAGS_SPECIAL        = (1 << 5),     
00173     FMAGIC_FLAGS_UNCOMPRESS     = (1 << 6),     
00174     FMAGIC_FLAGS_NOPAD          = (1 << 7)      
00175 };
00176 
00177 struct fmagic_s {
00178     int flags;                  
00179 /*@dependent@*/ /*@observer@*/ /*@relnull@*/
00180     const char *magicfile;      
00181 /*@dependent@*/ /*@observer@*/
00182     const char *separator;      
00183     int lineno;                 
00184 /*@relnull@*/
00185     struct mlist * mlist;       
00186 /*@relnull@*/
00187     struct mlist * ml;          
00188 /*@observer@*/
00189     const char * fn;            
00190     int fd;                     
00191     struct stat sb;             
00192 /*@relnull@*/
00193     unsigned char * buf;        
00194     int nb;                     
00195     union VALUETYPE val;        
00196     int cls;                    
00197     int swap;                   
00198 /*@dependent@*/
00199     char * obp;                 
00200     size_t nob;                 
00201     char obuf[512];             
00202 };
00203 
00204 typedef /*@abstract@*/ struct fmagic_s * fmagic;
00205 
00206 /*@unchecked@*/
00207 extern fmagic global_fmagic;
00208 
00209 /*@unchecked@*//*@observer@*/
00210 extern const char * default_magicfile;
00211 
00212 #ifdef __cplusplus
00213 extern "C" {
00214 #endif
00215 
00216 /*@mayexit@*/
00217 extern int fmagicSetup(fmagic fm, const char *fn, int action)
00218         /*@globals fileSystem, internalState @*/
00219         /*@modifies fm, fileSystem, internalState @*/;
00220 extern int fmagicProcess(fmagic fm, const char *fn, int wid)
00221         /*@globals fileSystem, internalState @*/
00222         /*@modifies fm, fileSystem, internalState @*/;
00223 
00224 extern int fmagicA(fmagic fm)
00225         /*@modifies fm @*/;
00226 extern int fmagicD(fmagic fm)
00227         /*@globals fileSystem, internalState @*/
00228         /*@modifies fm, fileSystem, internalState @*/;
00229 extern void fmagicE(fmagic fm)
00230         /*@globals fileSystem, internalState @*/
00231         /*@modifies fm, fileSystem, internalState @*/;
00232 extern int fmagicF(fmagic fm, int zfl)
00233         /*@globals fileSystem, internalState @*/
00234         /*@modifies fm, fileSystem, internalState @*/;
00235 extern int fmagicS(fmagic fm)
00236         /*@globals fileSystem @*/
00237         /*@modifies fm, fileSystem @*/;
00238 extern int fmagicZ(fmagic fm)
00239         /*@globals fileSystem, internalState @*/
00240         /*@modifies fm, fileSystem, internalState @*/;
00241 
00242 extern void file_printf(const fmagic fm, const char *f, ...)
00243         /*@modifies fm @*/;
00244 
00245 /*@observer@*/
00246 extern const char *file_fmttime(uint32_t v, int local)
00247         /*@*/;
00248 
00249 extern void file_magwarn(const char *f, ...)
00250         /*@globals fileSystem @*/
00251         /*@modifies fileSystem @*/;
00252 extern void file_mdump(struct magic *m)
00253         /*@globals fileSystem @*/
00254         /*@modifies fileSystem @*/;
00255 extern void file_showstr(FILE *fp, const char *s, size_t len)
00256         /*@globals fileSystem @*/
00257         /*@modifies fp, fileSystem @*/;
00258 
00259 extern uint32_t file_signextend(struct magic *m, uint32_t v)
00260         /*@globals fileSystem @*/
00261         /*@modifies fileSystem @*/;
00262 extern int file_pipe2file(int fd, const void *startbuf, size_t nbytes)
00263         /*@globals errno, fileSystem, internalState @*/
00264         /*@modifies errno, fileSystem, internalState @*/;
00265 
00266 #ifdef __cplusplus
00267 }
00268 #endif
00269 
00270 /*@=redef@*/
00271 #endif /* __file_h__ */

Generated on Fri May 4 07:55:13 2007 for rpm by  doxygen 1.3.9.1