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