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 #include <fcntl.h>
00043 #ifdef HAVE_STDINT_H
00044 #include <stdint.h>
00045 #endif
00046 #ifdef HAVE_INTTYPES_H
00047 #include <inttypes.h>
00048 #endif
00049 #include <regex.h>
00050 #include <sys/types.h>
00051
00052 #include <sys/stat.h>
00053
00054 #define ENABLE_CONDITIONALS
00055
00056 #ifndef MAGIC
00057 #define MAGIC "/etc/magic"
00058 #endif
00059
00060 #ifdef __EMX__
00061 #define PATHSEP ';'
00062 #else
00063 #define PATHSEP ':'
00064 #endif
00065
00066 #define private static
00067 #ifndef protected
00068 #define protected
00069 #endif
00070 #define public
00071
00072 #ifndef __GNUC_PREREQ__
00073 #ifdef __GNUC__
00074 #define __GNUC_PREREQ__(x, y) \
00075 ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) || \
00076 (__GNUC__ > (x)))
00077 #else
00078 #define __GNUC_PREREQ__(x, y) 0
00079 #endif
00080 #endif
00081
00082 #ifndef f__unused
00083 #if __GNUC_PREREQ__(2, 7)
00084 #define f__unused __attribute__((__unused__))
00085 #else
00086 #define f__unused
00087 #endif
00088 #endif
00089
00090 #ifndef MIN
00091 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
00092 #endif
00093
00094 #ifndef HOWMANY
00095 # define HOWMANY (256 * 1024)
00096 #endif
00097 #define MAXMAGIS 8192
00098 #define MAXDESC 64
00099 #define MAXstring 32
00100
00101 #define MAGICNO 0xF11E041C
00102 #define VERSIONNO 4
00103 #define FILE_MAGICSIZE (32 * 4)
00104
00105 #define FILE_LOAD 0
00106 #define FILE_CHECK 1
00107 #define FILE_COMPILE 2
00108
00109 struct magic {
00110
00111 uint16_t cont_level;
00112 uint8_t nospflag;
00113 uint8_t flag;
00114 #define INDIR 1
00115 #define OFFADD 2
00116 #define INDIROFFADD 4
00117 #define UNSIGNED 8
00118
00119
00120 uint8_t reln;
00121 uint8_t vallen;
00122 uint8_t type;
00123 uint8_t in_type;
00124 #define FILE_INVALID 0
00125 #define FILE_BYTE 1
00126 #define FILE_SHORT 2
00127 #define FILE_DEFAULT 3
00128 #define FILE_LONG 4
00129 #define FILE_STRING 5
00130 #define FILE_DATE 6
00131 #define FILE_BESHORT 7
00132 #define FILE_BELONG 8
00133 #define FILE_BEDATE 9
00134 #define FILE_LESHORT 10
00135 #define FILE_LELONG 11
00136 #define FILE_LEDATE 12
00137 #define FILE_PSTRING 13
00138 #define FILE_LDATE 14
00139 #define FILE_BELDATE 15
00140 #define FILE_LELDATE 16
00141 #define FILE_REGEX 17
00142 #define FILE_BESTRING16 18
00143 #define FILE_LESTRING16 19
00144 #define FILE_SEARCH 20
00145 #define FILE_MEDATE 21
00146 #define FILE_MELDATE 22
00147 #define FILE_MELONG 23
00148 #define FILE_QUAD 24
00149 #define FILE_LEQUAD 25
00150 #define FILE_BEQUAD 26
00151 #define FILE_QDATE 27
00152 #define FILE_LEQDATE 28
00153 #define FILE_BEQDATE 29
00154 #define FILE_QLDATE 30
00155 #define FILE_LEQLDATE 31
00156 #define FILE_BEQLDATE 32
00157 #define FILE_NAMES_SIZE 33
00158
00159 #define IS_STRING(t) \
00160 ((t) == FILE_STRING || \
00161 (t) == FILE_PSTRING || \
00162 (t) == FILE_BESTRING16 || \
00163 (t) == FILE_LESTRING16 || \
00164 (t) == FILE_REGEX || \
00165 (t) == FILE_SEARCH || \
00166 (t) == FILE_DEFAULT)
00167
00168 #define FILE_FMT_NONE 0
00169 #define FILE_FMT_NUM 1
00170 #define FILE_FMT_STR 2
00171 #define FILE_FMT_QUAD 3
00172
00173
00174 uint8_t in_op;
00175 uint8_t mask_op;
00176 #ifdef ENABLE_CONDITIONALS
00177 uint8_t cond;
00178 uint8_t dummy1;
00179 #else
00180 uint8_t dummy1;
00181 uint8_t dummy2;
00182 #endif
00183
00184 #define FILE_OPS "&|^+-*/%"
00185 #define FILE_OPAND 0
00186 #define FILE_OPOR 1
00187 #define FILE_OPXOR 2
00188 #define FILE_OPADD 3
00189 #define FILE_OPMINUS 4
00190 #define FILE_OPMULTIPLY 5
00191 #define FILE_OPDIVIDE 6
00192 #define FILE_OPMODULO 7
00193 #define FILE_OPS_MASK 0x07
00194 #define FILE_UNUSED_1 0x08
00195 #define FILE_UNUSED_2 0x10
00196 #define FILE_UNUSED_3 0x20
00197 #define FILE_OPINVERSE 0x40
00198 #define FILE_OPINDIRECT 0x80
00199
00200 #ifdef ENABLE_CONDITIONALS
00201 #define COND_NONE 0
00202 #define COND_IF 1
00203 #define COND_ELIF 2
00204 #define COND_ELSE 3
00205 #endif
00206
00207
00208 uint32_t offset;
00209
00210 int32_t in_offset;
00211
00212 uint32_t lineno;
00213
00214 union {
00215 uint64_t _mask;
00216 struct {
00217 uint32_t _count;
00218 uint32_t _flags;
00219 } _s;
00220 } _u;
00221 #define num_mask _u._mask
00222 #define str_count _u._s._count
00223 #define str_flags _u._s._flags
00224
00225
00226 union VALUETYPE {
00227 uint8_t b;
00228 uint16_t h;
00229 uint32_t l;
00230 uint64_t q;
00231 uint8_t hs[2];
00232 uint8_t hl[4];
00233 uint8_t hq[8];
00234 char s[MAXstring];
00235 } value;
00236
00237 char desc[MAXDESC];
00238 };
00239
00240 #define BIT(A) (1 << (A))
00241 #define STRING_COMPACT_BLANK BIT(0)
00242 #define STRING_COMPACT_OPTIONAL_BLANK BIT(1)
00243 #define STRING_IGNORE_LOWERCASE BIT(2)
00244 #define STRING_IGNORE_UPPERCASE BIT(3)
00245 #define REGEX_OFFSET_START BIT(4)
00246 #define CHAR_COMPACT_BLANK 'B'
00247 #define CHAR_COMPACT_OPTIONAL_BLANK 'b'
00248 #define CHAR_IGNORE_LOWERCASE 'c'
00249 #define CHAR_IGNORE_UPPERCASE 'C'
00250 #define CHAR_REGEX_OFFSET_START 's'
00251 #define STRING_IGNORE_CASE (STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
00252
00253
00254
00255 struct mlist {
00256 struct magic *magic;
00257 uint32_t nmagic;
00258 int mapped;
00259
00260
00261 struct mlist *next, *prev;
00262 };
00263
00264 struct magic_set {
00265
00266 struct mlist *mlist;
00267 struct cont {
00268 size_t len;
00269 struct level_info {
00270 int32_t off;
00271 int got_match;
00272 #ifdef ENABLE_CONDITIONALS
00273 int last_match;
00274 int last_cond;
00275 #endif
00276 } *li;
00277 } c;
00278 struct out {
00279
00280 char *buf;
00281 char *ptr;
00282 size_t left;
00283 size_t size;
00284
00285 char *pbuf;
00286 size_t psize;
00287 } o;
00288 uint32_t offset;
00289 int error;
00290 int flags;
00291 int haderr;
00292 const char *file;
00293 size_t line;
00294
00295
00296 struct {
00297 const char *s;
00298 size_t s_len;
00299 size_t offset;
00300 size_t rm_len;
00301 } search;
00302
00303 union VALUETYPE ms_value;
00304 };
00305
00306 struct stat;
00307
00308 protected const char *file_fmttime(uint32_t, int)
00309 ;
00310 protected int file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf,
00311 size_t nb)
00312
00313 ;
00314 protected int file_fsmagic(struct magic_set *ms, const char *, struct stat *sb)
00315 ;
00316 protected int file_pipe2file(struct magic_set *ms, int, const void *, size_t)
00317
00318 ;
00319 protected int file_printf(struct magic_set *ms, const char *, ...)
00320 ;
00321 protected int file_reset(struct magic_set *ms)
00322 ;
00323 protected int file_tryelf(struct magic_set *ms, int, const unsigned char *,
00324 size_t)
00325
00326 ;
00327 protected int file_zmagic(struct magic_set *ms, int fd, const char *name,
00328 const unsigned char *buf, size_t nbytes)
00329
00330 ;
00331 protected int file_ascmagic(struct magic_set *ms, const unsigned char *, size_t)
00332 ;
00333 protected int file_is_tar(struct magic_set *ms, const unsigned char *, size_t)
00334 ;
00335 protected int file_softmagic(struct magic_set *ms, const unsigned char *, size_t)
00336
00337 ;
00338
00339 protected struct mlist *file_apprentice(struct magic_set *ms, const char *, int)
00340
00341 ;
00342 protected uint64_t file_signextend(struct magic_set *ms, struct magic *,
00343 uint64_t)
00344
00345 ;
00346 protected void file_delmagic( struct magic *p, int type, size_t entries)
00347
00348 ;
00349 protected void file_badread(struct magic_set *ms)
00350 ;
00351 protected void file_badseek(struct magic_set *ms)
00352 ;
00353 protected void file_oomem(struct magic_set *ms, size_t)
00354 ;
00355 protected void file_error(struct magic_set *ms, int error, const char *f, ...)
00356 ;
00357 protected void file_magerror(struct magic_set *ms, const char *, ...)
00358 ;
00359 protected void file_magwarn(struct magic_set *ms, const char *, ...)
00360
00361 ;
00362 protected void file_mdump(struct magic *)
00363
00364 ;
00365 protected void file_showstr(FILE *fp, const char *, size_t)
00366
00367 ;
00368 protected size_t file_mbswidth(const char *)
00369 ;
00370
00371 protected const char *file_getbuffer(struct magic_set *ms)
00372 ;
00373 protected ssize_t sread(int fd, void *buf, size_t n, int canbepipe)
00374 ;
00375 protected int file_check_mem(struct magic_set *ms, unsigned int)
00376 ;
00377
00378 #ifndef COMPILE_ONLY
00379 extern const char *file_names[];
00380 extern const size_t file_nnames;
00381 #endif
00382
00383 #ifndef HAVE_STRERROR
00384 extern int sys_nerr;
00385 extern char *sys_errlist[];
00386 #define strerror(e) \
00387 (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
00388 #endif
00389
00390 #ifndef HAVE_STRTOUL
00391 #define strtoul(a, b, c) strtol(a, b, c)
00392 #endif
00393
00394 #ifndef HAVE_SNPRINTF
00395 int snprintf(char *, size_t, const char *, ...)
00396 ;
00397 #endif
00398
00399 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
00400 #define QUICK
00401 #endif
00402
00403 #ifndef O_BINARY
00404 #define O_BINARY 0
00405 #endif
00406
00407 #define FILE_RCSID(id) \
00408 static const char *rcsid(const char *p) { \
00409 return rcsid(p = id); \
00410 }
00411
00412 #endif