version 1.18, 2007/11/26 09:28:34 |
version 1.19, 2008/05/08 01:40:56 |
|
|
|
|
#include <stdio.h> /* Include that here, to make sure __P gets defined */ |
#include <stdio.h> /* Include that here, to make sure __P gets defined */ |
#include <errno.h> |
#include <errno.h> |
|
#include <fcntl.h> /* For open and flags */ |
#ifdef HAVE_STDINT_H |
#ifdef HAVE_STDINT_H |
#include <stdint.h> |
#include <stdint.h> |
#endif |
#endif |
#ifdef HAVE_INTTYPES_H |
#ifdef HAVE_INTTYPES_H |
#include <inttypes.h> |
#include <inttypes.h> |
#endif |
#endif |
|
#include <regex.h> |
|
#include <sys/types.h> |
/* Do this here and now, because struct stat gets re-defined on solaris */ |
/* Do this here and now, because struct stat gets re-defined on solaris */ |
#include <sys/stat.h> |
#include <sys/stat.h> |
|
|
|
#define ENABLE_CONDITIONALS |
|
|
#ifndef MAGIC |
#ifndef MAGIC |
#define MAGIC "/etc/magic" |
#define MAGIC "/etc/magic" |
#endif |
#endif |
|
|
#endif |
#endif |
#define public |
#define public |
|
|
|
#ifndef __GNUC_PREREQ__ |
|
#ifdef __GNUC__ |
|
#define __GNUC_PREREQ__(x, y) \ |
|
((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) || \ |
|
(__GNUC__ > (x))) |
|
#else |
|
#define __GNUC_PREREQ__(x, y) 0 |
|
#endif |
|
#endif |
|
|
|
#ifndef MIN |
|
#define MIN(a,b) (((a) < (b)) ? (a) : (b)) |
|
#endif |
|
|
#ifndef HOWMANY |
#ifndef HOWMANY |
# define HOWMANY 65536 /* how much of the file to look at */ |
# define HOWMANY (256 * 1024) /* how much of the file to look at */ |
#endif |
#endif |
#define MAXMAGIS 4096 /* max entries in /etc/magic */ |
#define MAXMAGIS 8192 /* max entries in /etc/magic */ |
#define MAXDESC 64 /* max leng of text description */ |
#define MAXDESC 64 /* max leng of text description */ |
#define MAXstring 32 /* max leng of "string" types */ |
#define MAXstring 32 /* max leng of "string" types */ |
|
|
#define MAGICNO 0xF11E041C |
#define MAGICNO 0xF11E041C |
#define VERSIONNO 2 |
#define VERSIONNO 4 |
#define FILE_MAGICSIZE (32 * 4) |
#define FILE_MAGICSIZE (32 * 4) |
|
|
#define FILE_LOAD 0 |
#define FILE_LOAD 0 |
|
|
uint16_t cont_level; /* level of ">" */ |
uint16_t cont_level; /* level of ">" */ |
uint8_t nospflag; /* suppress space character */ |
uint8_t nospflag; /* suppress space character */ |
uint8_t flag; |
uint8_t flag; |
#define INDIR 1 /* if '>(...)' appears, */ |
#define INDIR 1 /* if '(...)' appears */ |
#define UNSIGNED 2 /* comparison is unsigned */ |
#define OFFADD 2 /* if '>&' or '>...(&' appears */ |
#define OFFADD 4 /* if '>&' appears, */ |
#define INDIROFFADD 4 /* if '>&(' appears */ |
|
#define UNSIGNED 8 /* comparison is unsigned */ |
|
|
/* Word 2 */ |
/* Word 2 */ |
uint8_t reln; /* relation (0=eq, '>'=gt, etc) */ |
uint8_t reln; /* relation (0=eq, '>'=gt, etc) */ |
uint8_t vallen; /* length of string value, if any */ |
uint8_t vallen; /* length of string value, if any */ |
uint8_t type; /* int, short, long or string. */ |
uint8_t type; /* int, short, long or string. */ |
uint8_t in_type; /* type of indirrection */ |
uint8_t in_type; /* type of indirrection */ |
|
#define FILE_INVALID 0 |
#define FILE_BYTE 1 |
#define FILE_BYTE 1 |
#define FILE_SHORT 2 |
#define FILE_SHORT 2 |
|
#define FILE_DEFAULT 3 |
#define FILE_LONG 4 |
#define FILE_LONG 4 |
#define FILE_STRING 5 |
#define FILE_STRING 5 |
#define FILE_DATE 6 |
#define FILE_DATE 6 |
|
|
#define FILE_BELDATE 15 |
#define FILE_BELDATE 15 |
#define FILE_LELDATE 16 |
#define FILE_LELDATE 16 |
#define FILE_REGEX 17 |
#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_QUAD 24 |
|
#define FILE_LEQUAD 25 |
|
#define FILE_BEQUAD 26 |
|
#define FILE_QDATE 27 |
|
#define FILE_LEQDATE 28 |
|
#define FILE_BEQDATE 29 |
|
#define FILE_QLDATE 30 |
|
#define FILE_LEQLDATE 31 |
|
#define FILE_BEQLDATE 32 |
|
#define FILE_NAMES_SIZE 33/* size of array to contain all names */ |
|
|
|
#define IS_STRING(t) \ |
|
((t) == FILE_STRING || \ |
|
(t) == FILE_PSTRING || \ |
|
(t) == FILE_BESTRING16 || \ |
|
(t) == FILE_LESTRING16 || \ |
|
(t) == FILE_REGEX || \ |
|
(t) == FILE_SEARCH || \ |
|
(t) == FILE_DEFAULT) |
|
|
|
#define FILE_FMT_NONE 0 |
|
#define FILE_FMT_NUM 1 /* "cduxXi" */ |
|
#define FILE_FMT_STR 2 /* "s" */ |
|
#define FILE_FMT_QUAD 3 /* "ll" */ |
|
|
/* Word 3 */ |
/* Word 3 */ |
uint8_t in_op; /* operator for indirection */ |
uint8_t in_op; /* operator for indirection */ |
uint8_t mask_op; /* operator for mask */ |
uint8_t mask_op; /* operator for mask */ |
|
#ifdef ENABLE_CONDITIONALS |
|
uint8_t cond; /* conditional type */ |
uint8_t dummy1; |
uint8_t dummy1; |
|
#else |
|
uint8_t dummy1; |
uint8_t dummy2; |
uint8_t dummy2; |
|
#endif |
|
|
#define FILE_OPS "&|^+-*/%" |
#define FILE_OPS "&|^+-*/%" |
#define FILE_OPAND 0 |
#define FILE_OPAND 0 |
#define FILE_OPOR 1 |
#define FILE_OPOR 1 |
|
|
#define FILE_OPMULTIPLY 5 |
#define FILE_OPMULTIPLY 5 |
#define FILE_OPDIVIDE 6 |
#define FILE_OPDIVIDE 6 |
#define FILE_OPMODULO 7 |
#define FILE_OPMODULO 7 |
#define FILE_OPINVERSE 0x80 |
#define FILE_OPS_MASK 0x07 /* mask for above ops */ |
|
#define FILE_UNUSED_1 0x08 |
|
#define FILE_UNUSED_2 0x10 |
|
#define FILE_UNUSED_3 0x20 |
|
#define FILE_OPINVERSE 0x40 |
|
#define FILE_OPINDIRECT 0x80 |
|
|
|
#ifdef ENABLE_CONDITIONALS |
|
#define COND_NONE 0 |
|
#define COND_IF 1 |
|
#define COND_ELIF 2 |
|
#define COND_ELSE 3 |
|
#endif /* ENABLE_CONDITIONALS */ |
|
|
/* Word 4 */ |
/* Word 4 */ |
uint32_t offset; /* offset to magic number */ |
uint32_t offset; /* offset to magic number */ |
/* Word 5 */ |
/* Word 5 */ |
uint32_t in_offset; /* offset from indirection */ |
int32_t in_offset; /* offset from indirection */ |
/* Word 6 */ |
/* Word 6 */ |
uint32_t mask; /* mask before comparison with value */ |
uint32_t lineno; /* line number in magic file */ |
/* Word 7 */ |
/* Word 7,8 */ |
uint32_t dummy3; |
union { |
/* Word 8 */ |
uint64_t _mask; /* for use with numeric and date types */ |
uint32_t dummp4; |
struct { |
|
uint32_t _count; /* repeat/line count */ |
|
uint32_t _flags; /* modifier flags */ |
|
} _s; /* for use with string types */ |
|
} _u; |
|
#define num_mask _u._mask |
|
#define str_count _u._s._count |
|
#define str_flags _u._s._flags |
|
|
/* Words 9-16 */ |
/* Words 9-16 */ |
union VALUETYPE { |
union VALUETYPE { |
uint8_t b; |
uint8_t b; |
uint16_t h; |
uint16_t h; |
uint32_t l; |
uint32_t l; |
char s[MAXstring]; |
uint64_t q; |
char *buf; |
|
uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */ |
uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */ |
uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */ |
uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */ |
|
uint8_t hq[8]; /* 8 bytes of a fixed-endian "quad" */ |
|
char s[MAXstring]; /* the search string or regex pattern */ |
} value; /* either number or string */ |
} value; /* either number or string */ |
/* Words 17..31 */ |
/* Words 17..31 */ |
char desc[MAXDESC]; /* description */ |
char desc[MAXDESC]; /* description */ |
}; |
}; |
|
|
#define BIT(A) (1 << (A)) |
#define BIT(A) (1 << (A)) |
#define STRING_IGNORE_LOWERCASE BIT(0) |
#define STRING_COMPACT_BLANK BIT(0) |
#define STRING_COMPACT_BLANK BIT(1) |
#define STRING_COMPACT_OPTIONAL_BLANK BIT(1) |
#define STRING_COMPACT_OPTIONAL_BLANK BIT(2) |
#define STRING_IGNORE_LOWERCASE BIT(2) |
#define CHAR_IGNORE_LOWERCASE 'c' |
#define STRING_IGNORE_UPPERCASE BIT(3) |
|
#define REGEX_OFFSET_START BIT(4) |
#define CHAR_COMPACT_BLANK 'B' |
#define CHAR_COMPACT_BLANK 'B' |
#define CHAR_COMPACT_OPTIONAL_BLANK 'b' |
#define CHAR_COMPACT_OPTIONAL_BLANK 'b' |
|
#define CHAR_IGNORE_LOWERCASE 'c' |
|
#define CHAR_IGNORE_UPPERCASE 'C' |
|
#define CHAR_REGEX_OFFSET_START 's' |
|
#define STRING_IGNORE_CASE (STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE) |
|
|
|
|
/* list of magic entries */ |
/* list of magic entries */ |
|
|
}; |
}; |
|
|
struct magic_set { |
struct magic_set { |
struct mlist *mlist; |
struct mlist *mlist; |
struct cont { |
struct cont { |
size_t len; |
size_t len; |
int32_t *off; |
struct level_info { |
} c; |
int32_t off; |
struct out { |
int got_match; |
/* Accumulation buffer */ |
#ifdef ENABLE_CONDITIONALS |
char *buf; |
int last_match; |
char *ptr; |
int last_cond; /* used for error checking by parse() */ |
size_t left; |
#endif |
size_t size; |
} *li; |
/* Printable buffer */ |
} c; |
char *pbuf; |
struct out { |
size_t psize; |
/* Accumulation buffer */ |
} o; |
char *buf; |
int error; |
char *ptr; |
int flags; |
size_t left; |
int haderr; |
size_t size; |
|
/* Printable buffer */ |
|
char *pbuf; |
|
size_t psize; |
|
} o; |
|
uint32_t offset; |
|
int error; |
|
int flags; |
|
int haderr; |
|
const char *file; |
|
size_t line; /* current magic line number */ |
|
|
|
/* data for searches */ |
|
struct { |
|
const char *s; /* start of search in original source */ |
|
size_t s_len; /* length of search region */ |
|
size_t offset; /* starting offset in source: XXX - should this be off_t? */ |
|
size_t rm_len; /* match length */ |
|
} search; |
|
|
|
union VALUETYPE ms_value; /* either number or string */ |
}; |
}; |
|
|
struct stat; |
struct stat; |
protected char *file_fmttime(uint32_t, int); |
protected const char *file_fmttime(uint32_t, int); |
protected int file_buffer(struct magic_set *, const void *, size_t); |
protected int file_buffer(struct magic_set *, int, const char *, const void *, |
|
size_t); |
protected int file_fsmagic(struct magic_set *, const char *, struct stat *); |
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_pipe2file(struct magic_set *, int, const void *, size_t); |
protected int file_printf(struct magic_set *, const char *, ...); |
protected int file_printf(struct magic_set *, const char *, ...); |
protected int file_reset(struct magic_set *); |
protected int file_reset(struct magic_set *); |
protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t); |
protected int file_tryelf(struct magic_set *, int, const unsigned char *, |
protected int file_zmagic(struct magic_set *, const unsigned char *, size_t); |
size_t); |
|
protected int file_zmagic(struct magic_set *, int, const char *, |
|
const unsigned char *, size_t); |
protected int file_ascmagic(struct magic_set *, 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_is_tar(struct magic_set *, const unsigned char *, size_t); |
protected int file_softmagic(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 struct mlist *file_apprentice(struct magic_set *, const char *, int); |
protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t); |
protected uint64_t file_signextend(struct magic_set *, struct magic *, |
|
uint64_t); |
protected void file_delmagic(struct magic *, int type, size_t entries); |
protected void file_delmagic(struct magic *, int type, size_t entries); |
protected void file_badread(struct magic_set *); |
protected void file_badread(struct magic_set *); |
protected void file_badseek(struct magic_set *); |
protected void file_badseek(struct magic_set *); |
protected void file_oomem(struct magic_set *); |
protected void file_oomem(struct magic_set *, size_t); |
protected void file_error(struct magic_set *, int, const char *, ...); |
protected void file_error(struct magic_set *, int, const char *, ...); |
protected void file_magwarn(const char *, ...); |
protected void file_magerror(struct magic_set *, const char *, ...); |
|
protected void file_magwarn(struct magic_set *, const char *, ...); |
protected void file_mdump(struct magic *); |
protected void file_mdump(struct magic *); |
protected void file_showstr(FILE *, const char *, size_t); |
protected void file_showstr(FILE *, const char *, size_t); |
protected size_t file_mbswidth(const char *); |
protected size_t file_mbswidth(const char *); |
protected const char *file_getbuffer(struct magic_set *); |
protected const char *file_getbuffer(struct magic_set *); |
|
protected ssize_t sread(int, void *, size_t, int); |
|
protected int file_check_mem(struct magic_set *, unsigned int); |
|
|
|
#ifndef COMPILE_ONLY |
|
extern const char *file_names[]; |
|
extern const size_t file_nnames; |
|
#endif |
|
|
|
#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) |
#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK) |
#define QUICK |
#define QUICK |
#endif |
#endif |
|
|
|
#ifndef O_BINARY |
|
#define O_BINARY 0 |
|
#endif |
|
|
#define FILE_RCSID(id) \ |
#define FILE_RCSID(id) \ |
static const char *rcsid(const char *p) { \ |
static const char *rcsid(const char *p) { \ |
return rcsid(p = id); \ |
return rcsid(p = id); \ |
} |
} |
#else |
|
|
|
#endif /* __file_h__ */ |
#endif /* __file_h__ */ |