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