#ifndef __file_h__
#define __file_h__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#include <sys/stat.h>
#ifndef MAGIC
#define MAGIC "/etc/magic"
#endif
#ifdef __EMX__
#define PATHSEP ';'
#else
#define PATHSEP ':'
#endif
#define private static
#ifndef protected
#define protected
#endif
#define public
#ifndef HOWMANY
# define HOWMANY (256 * 1024)
#endif
#define MAXMAGIS 8192
#define MAXDESC 64
#define MAXstring 256
#define MAGICNO 0xF11E041C
#define VERSIONNO 2
#define FILE_MAGICSIZE (32 * 11)
#define FILE_LOAD 0
#define FILE_CHECK 1
#define FILE_COMPILE 2
struct magic {
uint16_t cont_level;
uint8_t nospflag;
uint8_t flag;
#define INDIR 1
#define UNSIGNED 2
#define OFFADD 4
#define INDIROFFADD 8
uint8_t reln;
uint8_t vallen;
uint8_t type;
uint8_t in_type;
#define FILE_BYTE 1
#define FILE_SHORT 2
#define FILE_LONG 4
#define FILE_STRING 5
#define FILE_DATE 6
#define FILE_BESHORT 7
#define FILE_BELONG 8
#define FILE_BEDATE 9
#define FILE_LESHORT 10
#define FILE_LELONG 11
#define FILE_LEDATE 12
#define FILE_PSTRING 13
#define FILE_LDATE 14
#define FILE_BELDATE 15
#define FILE_LELDATE 16
#define FILE_REGEX 17
#define FILE_BESTRING16 18
#define FILE_LESTRING16 19
#define FILE_SEARCH 20
#define FILE_MEDATE 21
#define FILE_MELDATE 22
#define FILE_MELONG 23
#define FILE_FORMAT_NAME \
"invalid 0", \
"byte", \
"short", \
"invalid 3", \
"long", \
"string", \
"date", \
"beshort", \
"belong", \
"bedate", \
"leshort", \
"lelong", \
"ledate", \
"pstring", \
"ldate", \
"beldate", \
"leldate", \
"regex", \
"bestring16", \
"lestring16", \
"search", \
"medate", \
"meldate", \
"melong",
#define FILE_FMT_NUM "cduxXi"
#define FILE_FMT_STR "s"
#define FILE_FORMAT_STRING \
NULL, \
FILE_FMT_NUM, \
FILE_FMT_NUM, \
NULL, \
FILE_FMT_NUM, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_NUM, \
FILE_FMT_NUM, \
FILE_FMT_STR, \
FILE_FMT_NUM, \
FILE_FMT_NUM, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_STR, \
FILE_FMT_NUM,
uint8_t in_op;
uint8_t mask_op;
uint8_t dummy1;
uint8_t dummy2;
#define FILE_OPS "&|^+-*/%"
#define FILE_OPAND 0
#define FILE_OPOR 1
#define FILE_OPXOR 2
#define FILE_OPADD 3
#define FILE_OPMINUS 4
#define FILE_OPMULTIPLY 5
#define FILE_OPDIVIDE 6
#define FILE_OPMODULO 7
#define FILE_OPINVERSE 0x40
#define FILE_OPINDIRECT 0x80
uint32_t offset;
int32_t in_offset;
uint32_t mask;
uint32_t dummy3;
uint32_t dummp4;
union VALUETYPE {
uint8_t b;
uint16_t h;
uint32_t l;
char s[MAXstring];
struct {
char *buf;
size_t buflen;
} search;
uint8_t hs[2];
uint8_t hl[4];
} value;
char desc[MAXDESC];
};
#define BIT(A) (1 << (A))
#define STRING_IGNORE_LOWERCASE BIT(0)
#define STRING_COMPACT_BLANK BIT(1)
#define STRING_COMPACT_OPTIONAL_BLANK BIT(2)
#define CHAR_IGNORE_LOWERCASE 'c'
#define CHAR_COMPACT_BLANK 'B'
#define CHAR_COMPACT_OPTIONAL_BLANK 'b'
struct mlist {
struct magic *magic;
uint32_t nmagic;
int mapped;
struct mlist *next, *prev;
};
struct magic_set {
struct mlist *mlist;
struct cont {
size_t len;
int32_t *off;
} c;
struct out {
char *buf;
char *ptr;
size_t len;
size_t size;
char *pbuf;
size_t psize;
} o;
int error;
int flags;
int haderr;
const char *file;
size_t line;
};
struct stat;
protected const char *file_fmttime(uint32_t, int);
protected int file_buffer(struct magic_set *, int, const void *, size_t);
protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
protected int file_printf(struct magic_set *, const char *, ...);
protected int file_reset(struct magic_set *);
protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t);
protected int file_trymacho(struct magic_set *, int, const unsigned char *, size_t, const char *);
protected int file_zmagic(struct magic_set *, int, const unsigned char *, size_t);
protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t);
protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t);
protected void file_delmagic(struct magic *, int type, size_t entries);
protected void file_badread(struct magic_set *);
protected void file_badseek(struct magic_set *);
protected void file_oomem(struct magic_set *);
protected void file_error(struct magic_set *, int, const char *, ...);
protected void file_magwarn(struct magic_set *, const char *, ...);
protected void file_mdump(struct magic *);
protected void file_showstr(FILE *, const char *, size_t);
protected size_t file_mbswidth(const char *);
protected const char *file_getbuffer(struct magic_set *);
#ifndef HAVE_STRERROR
extern int sys_nerr;
extern char *sys_errlist[];
#define strerror(e) \
(((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
#endif
#ifndef HAVE_STRTOUL
#define strtoul(a, b, c) strtol(a, b, c)
#endif
#ifndef HAVE_SNPRINTF
int snprintf(char *, size_t, const char *, ...);
#endif
#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
#define QUICK
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#define FILE_RCSID(id) \
static const char *rcsid(const char *p) { \
return rcsid(p = id); \
}
#else
#endif