00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef __file_h__
00039 #define __file_h__
00040
00041
00042
00043 #ifndef HOWMANY
00044 # define HOWMANY 65536
00045 #endif
00046 #define MAXMAGIS 4096
00047 #define MAXDESC 64
00048 #define MAXstring 32
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
00059 uint16_t cont_level;
00060 uint8_t nospflag;
00061 uint8_t flag;
00062 #define INDIR 1
00063 #define UNSIGNED 2
00064 #define OFFADD 4
00065
00066 uint8_t reln;
00067 uint8_t vallen;
00068 uint8_t type;
00069 uint8_t in_type;
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
00087 uint8_t in_op;
00088 uint8_t mask_op;
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
00102 int32_t offset;
00103
00104 int32_t in_offset;
00105
00106 uint32_t mask;
00107
00108 uint32_t dummy3;
00109
00110 uint32_t dummp4;
00111
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];
00119 uint8_t hl[4];
00120 } value;
00121
00122 char desc[MAXDESC];
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
00135 struct mlist {
00136 struct magic *magic;
00137 uint32_t nmagic;
00138 int mapped;
00139
00140
00141
00142 struct mlist *next;
00143
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
00165 FMAGIC_FLAGS_NONE = 0,
00166
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
00180 const char *magicfile;
00181
00182 const char *separator;
00183 int lineno;
00184
00185 struct mlist * mlist;
00186
00187 struct mlist * ml;
00188
00189 const char * fn;
00190 int fd;
00191 struct stat sb;
00192
00193 unsigned char * buf;
00194 int nb;
00195 union VALUETYPE val;
00196 int cls;
00197 int swap;
00198
00199 char * obp;
00200 size_t nob;
00201 char obuf[512];
00202 };
00203
00204 typedef struct fmagic_s * fmagic;
00205
00206
00207 extern fmagic global_fmagic;
00208
00209
00210 extern const char * default_magicfile;
00211
00212 #ifdef __cplusplus
00213 extern "C" {
00214 #endif
00215
00216
00217 extern int fmagicSetup(fmagic fm, const char *fn, int action)
00218
00219 ;
00220 extern int fmagicProcess(fmagic fm, const char *fn, int wid)
00221
00222 ;
00223
00224 extern int fmagicA(fmagic fm)
00225 ;
00226 extern int fmagicD(fmagic fm)
00227
00228 ;
00229 extern void fmagicE(fmagic fm)
00230
00231 ;
00232 extern int fmagicF(fmagic fm, int zfl)
00233
00234 ;
00235 extern int fmagicS(fmagic fm)
00236
00237 ;
00238 extern int fmagicZ(fmagic fm)
00239
00240 ;
00241
00242 extern void file_printf(const fmagic fm, const char *f, ...)
00243 ;
00244
00245
00246 extern const char *file_fmttime(uint32_t v, int local)
00247 ;
00248
00249 extern void file_magwarn(const char *f, ...)
00250
00251 ;
00252 extern void file_mdump(struct magic *m)
00253
00254 ;
00255 extern void file_showstr(FILE *fp, const char *s, size_t len)
00256
00257 ;
00258
00259 extern uint32_t file_signextend(struct magic *m, uint32_t v)
00260
00261 ;
00262 extern int file_pipe2file(int fd, const void *startbuf, size_t nbytes)
00263
00264 ;
00265
00266 #ifdef __cplusplus
00267 }
00268 #endif
00269
00270
00271 #endif