#include <dirent.h>
#ifndef lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.136 2008/05/18 23:20:40 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.137 2008/07/02 15:22:47 christos Exp $")
#endif /* lint */
#define EATAB {while (isascii((unsigned char) *l) && \
return -1;
}
- if ((ml = malloc(sizeof(*ml))) == NULL) {
+ if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL) {
file_delmagic(magic, mapped, nmagic);
file_oomem(ms, sizeof(*ml));
return -1;
}
fn = mfn;
- if ((mlist = malloc(sizeof(*mlist))) == NULL) {
+ if ((mlist = CAST(struct mlist *, malloc(sizeof(*mlist)))) == NULL) {
free(mfn);
file_oomem(ms, sizeof(*mlist));
return NULL;
private int
apprentice_sort(const void *a, const void *b)
{
- const struct magic_entry *ma = a;
- const struct magic_entry *mb = b;
+ const struct magic_entry *ma = CAST(const struct magic_entry *, a);
+ const struct magic_entry *mb = CAST(const struct magic_entry *, b);
size_t sa = apprentice_magic_strength(ma->mp);
size_t sb = apprentice_magic_strength(mb->mp);
if (sa == sb)
int errs = 0;
struct magic_entry *marray;
uint32_t marraycount, i, mentrycount = 0, starttest;
+ size_t slen;
char subfn[MAXPATHLEN];
struct stat st;
DIR *dir;
ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
maxmagic = MAXMAGIS;
- if ((marray = calloc(maxmagic, sizeof(*marray))) == NULL) {
+ if ((marray = CAST(struct magic_entry *, calloc(maxmagic,
+ sizeof(*marray)))) == NULL) {
file_oomem(ms, maxmagic * sizeof(*marray));
return -1;
}
for (i = 0; i < marraycount; i++)
mentrycount += marray[i].cont_count;
- if ((*magicp = malloc(sizeof(**magicp) * mentrycount)) == NULL) {
- file_oomem(ms, sizeof(**magicp) * mentrycount);
+ slen = sizeof(**magicp) * mentrycount;
+ if ((*magicp = CAST(struct magic *, malloc(slen))) == NULL) {
+ file_oomem(ms, slen);
errs++;
goto out;
}
if (me->cont_count == me->max_count) {
struct magic *nm;
size_t cnt = me->max_count + ALLOC_CHUNK;
- if ((nm = realloc(me->mp, sizeof(*nm) * cnt)) == NULL) {
+ if ((nm = CAST(struct magic *, realloc(me->mp,
+ sizeof(*nm) * cnt))) == NULL) {
file_oomem(ms, sizeof(*nm) * cnt);
return -1;
}
struct magic_entry *mp;
maxmagic += ALLOC_INCR;
- if ((mp = realloc(*mentryp, sizeof(*mp) * maxmagic)) ==
+ if ((mp = CAST(struct magic_entry *,
+ realloc(*mentryp, sizeof(*mp) * maxmagic))) ==
NULL) {
file_oomem(ms, sizeof(*mp) * maxmagic);
return -1;
}
me = &(*mentryp)[*nmentryp];
if (me->mp == NULL) {
- if ((m = malloc(sizeof(*m) * ALLOC_CHUNK)) == NULL) {
- file_oomem(ms, sizeof(*m) * ALLOC_CHUNK);
+ size_t len = sizeof(*m) * ALLOC_CHUNK;
+ if ((m = CAST(struct magic *, malloc(len))) == NULL) {
+ file_oomem(ms, len);
return -1;
}
me->mp = m;
}
#define RET 2
#else
- if ((mm = malloc((size_t)st.st_size)) == NULL) {
+ if ((mm = CAST(void *, malloc((size_t)st.st_size))) == NULL) {
file_oomem(ms, (size_t)st.st_size);
goto error1;
}
}
#define RET 1
#endif
- *magicp = mm;
+ *magicp = CAST(struct magic *, mm);
(void)close(fd);
fd = -1;
ptr = (uint32_t *)(void *)*magicp;
#include "names.h"
#ifndef lint
-FILE_RCSID("@(#)$File: ascmagic.c,v 1.62 2008/03/01 22:21:48 rrt Exp $")
+FILE_RCSID("@(#)$File: ascmagic.c,v 1.63 2008/05/09 14:20:28 christos Exp $")
#endif /* lint */
#define MAXLINELEN 300 /* longest sane line length */
while (nbytes > 1 && buf[nbytes - 1] == '\0')
nbytes--;
- if ((nbuf = calloc(1, (nbytes + 1) * sizeof(nbuf[0]))) == NULL)
+ if ((nbuf = CAST(unsigned char *, calloc((size_t)1,
+ (nbytes + 1) * sizeof(nbuf[0])))) == NULL)
goto done;
- if ((ubuf = calloc(1, (nbytes + 1) * sizeof(ubuf[0]))) == NULL)
+ if ((ubuf = CAST(unichar *, calloc((size_t)1,
+ (nbytes + 1) * sizeof(ubuf[0])))) == NULL)
goto done;
/*
re-converting improved, or at least realloced after
re-converting conversion. */
mlen = ulen * 6;
- if ((utf8_buf = malloc(mlen)) == NULL) {
+ if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
file_oomem(ms, mlen);
goto done;
}
#ifndef lint
-FILE_RCSID("@(#)$File: compress.c,v 1.55 2008/01/26 18:45:16 christos Exp $")
+FILE_RCSID("@(#)$File: compress.c,v 1.56 2008/02/07 00:58:52 christos Exp $")
#endif
private const struct {
if (data_start >= n)
return 0;
- if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
+ if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) {
return 0;
}
switch (type) {
#ifdef ELFCORE
case ET_CORE:
- if (dophn_core(ms, class, swap, fd,
+ if (dophn_core(ms, clazz, swap, fd,
(off_t)elf_getu(swap, elfhdr.e_phoff),
elf_getu16(swap, elfhdr.e_phnum),
(size_t)elf_getu16(swap, elfhdr.e_phentsize),
#endif
case ET_EXEC:
case ET_DYN:
- if (dophn_exec(ms, class, swap, fd,
+ if (dophn_exec(ms, clazz, swap, fd,
(off_t)elf_getu(swap, elfhdr.e_phoff),
elf_getu16(swap, elfhdr.e_phnum),
(size_t)elf_getu16(swap, elfhdr.e_phentsize),
return -1;
/*FALLTHROUGH*/
case ET_REL:
- if (doshn(ms, class, swap, fd,
+ if (doshn(ms, clazz, swap, fd,
(off_t)elf_getu(swap, elfhdr.e_shoff),
elf_getu16(swap, elfhdr.e_shnum),
(size_t)elf_getu16(swap, elfhdr.e_shentsize),
*/
/*
* file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.106 2008/07/02 15:22:47 christos Exp $
+ * @(#)$File: file.h,v 1.107 2008/07/15 17:45:13 christos Exp $
*/
#ifndef __file_h__
#define FILE_CHECK 1
#define FILE_COMPILE 2
+union VALUETYPE {
+ uint8_t b;
+ uint16_t h;
+ uint32_t l;
+ uint64_t q;
+ uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
+ 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 */
+ unsigned char us[MAXstring];
+ float f;
+ double d;
+};
+
struct magic {
/* Word 1 */
uint16_t cont_level; /* level of ">" */
#define num_mask _u._mask
#define str_range _u._s._count
#define str_flags _u._s._flags
-
/* Words 9-16 */
- union VALUETYPE {
- uint8_t b;
- uint16_t h;
- uint32_t l;
- uint64_t q;
- uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */
- 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 */
- unsigned char us[MAXstring];
- float f;
- double d;
- } value; /* either number or string */
+ union VALUETYPE value; /* either number or string */
/* Words 17..31 */
char desc[MAXDESC]; /* description */
/* Words 32..47 */
struct mlist *next, *prev;
};
+#ifdef __cplusplus
+#define CAST(T, b) static_cast<T>(b)
+#else
+#define CAST(T, b) (b)
+#endif
+
+struct level_info {
+ int32_t off;
+ int got_match;
+#ifdef ENABLE_CONDITIONALS
+ int last_match;
+ int last_cond; /* used for error checking by parse() */
+#endif
+} *li;
struct magic_set {
struct mlist *mlist;
struct cont {
size_t len;
- struct level_info {
- int32_t off;
- int got_match;
-#ifdef ENABLE_CONDITIONALS
- int last_match;
- int last_cond; /* used for error checking by parse() */
-#endif
- } *li;
+ struct level_info *li;
} c;
struct out {
char *buf; /* Accumulation buffer */
#define O_BINARY 0
#endif
+#ifndef __cplusplus
#ifdef __GNUC__
static const char *rcsid(const char *) __attribute__((__used__));
#endif
static const char *rcsid(const char *p) { \
return rcsid(p = id); \
}
+#else
+#define FILE_RCSID(id)
+#endif
#endif /* __file_h__ */
#endif
#ifndef lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.42 2008/07/03 15:48:18 christos Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.43 2008/07/03 15:53:10 christos Exp $")
#endif /* lint */
#ifndef SIZE_MAX
{
int m;
int mime = ms->flags & MAGIC_MIME;
+ const unsigned char *ubuf = CAST(const unsigned char *, buf);
if (nb == 0) {
if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
/* try compression stuff */
if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) != 0 ||
- (m = file_zmagic(ms, fd, inname, buf, nb)) == 0) {
+ (m = file_zmagic(ms, fd, inname, ubuf, nb)) == 0) {
/* Check if we have a tar file */
if ((ms->flags & MAGIC_NO_CHECK_TAR) != 0 ||
- (m = file_is_tar(ms, buf, nb)) == 0) {
+ (m = file_is_tar(ms, ubuf, nb)) == 0) {
/* try tests in /etc/magic (or surrogate magic file) */
if ((ms->flags & MAGIC_NO_CHECK_SOFT) != 0 ||
- (m = file_softmagic(ms, buf, nb, BINTEST)) == 0) {
+ (m = file_softmagic(ms, ubuf, nb, BINTEST)) == 0) {
/* try known keywords, check whether it is ASCII */
if ((ms->flags & MAGIC_NO_CHECK_ASCII) != 0 ||
- (m = file_ascmagic(ms, buf, nb)) == 0) {
+ (m = file_ascmagic(ms, ubuf, nb)) == 0) {
/* abandon hope, all ye who remain here */
if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
file_printf(ms, mime ? "application/octet-stream" :
* information from the ELF headers that cannot easily
* be extracted with rules in the magic file.
*/
- (void)file_tryelf(ms, fd, buf, nb);
+ (void)file_tryelf(ms, fd, ubuf, nb);
}
#endif
return m;
return NULL;
}
psize = len * 4 + 1;
- if ((pbuf = realloc(ms->o.pbuf, psize)) == NULL) {
+ if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
file_oomem(ms, psize);
return NULL;
}
if (level >= ms->c.len) {
len = (ms->c.len += 20) * sizeof(*ms->c.li);
- ms->c.li = (ms->c.li == NULL) ? malloc(len) :
- realloc(ms->c.li, len);
+ ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
+ malloc(len) :
+ realloc(ms->c.li, len));
if (ms->c.li == NULL) {
file_oomem(ms, len);
return -1;
#include "patchlevel.h"
#ifndef lint
-FILE_RCSID("@(#)$File: magic.c,v 1.51 2008/05/16 14:25:01 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.52 2008/07/03 15:53:10 christos Exp $")
#endif /* lint */
#ifndef PIPE_BUF
magic_open(int flags)
{
struct magic_set *ms;
+ size_t len;
- if ((ms = calloc((size_t)1, sizeof(struct magic_set))) == NULL)
+ if ((ms = CAST(magic_set *, calloc((size_t)1,
+ sizeof(struct magic_set)))) == NULL)
return NULL;
if (magic_setflags(ms, flags) == -1) {
}
ms->o.buf = ms->o.pbuf = NULL;
+ len = (ms->c.len = 10) * sizeof(*ms->c.li);
- ms->c.li = malloc((ms->c.len = 10) * sizeof(*ms->c.li));
- if (ms->c.li == NULL)
+ if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
goto free;
ms->haderr = 0;
* some overlapping space for matches near EOF
*/
#define SLOP (1 + sizeof(union VALUETYPE))
- if ((buf = malloc(HOWMANY + SLOP)) == NULL)
+ if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
return NULL;
if (file_reset(ms) == -1)
#include "magic.h"
#ifndef lint
-FILE_RCSID("@(#)$File: readelf.c,v 1.74 2008/05/28 21:02:29 christos Exp $")
+FILE_RCSID("@(#)$File: readelf.c,v 1.75 2008/06/05 12:59:15 christos Exp $")
#endif
#ifdef ELFCORE
off_t, int *, int);
private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, int *,
int);
-private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
+private size_t donote(struct magic_set *, void *, size_t, size_t, int,
int, size_t, int *);
#define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
# define elf_getu64(swap, value) getu64(swap, value)
#endif
-#define xsh_addr (class == ELFCLASS32 \
+#define xsh_addr (clazz == ELFCLASS32 \
? (void *) &sh32 \
: (void *) &sh64)
-#define xsh_sizeof (class == ELFCLASS32 \
+#define xsh_sizeof (clazz == ELFCLASS32 \
? sizeof sh32 \
: sizeof sh64)
-#define xsh_size (class == ELFCLASS32 \
+#define xsh_size (clazz == ELFCLASS32 \
? elf_getu32(swap, sh32.sh_size) \
: elf_getu64(swap, sh64.sh_size))
-#define xsh_offset (class == ELFCLASS32 \
+#define xsh_offset (clazz == ELFCLASS32 \
? elf_getu32(swap, sh32.sh_offset) \
: elf_getu64(swap, sh64.sh_offset))
-#define xsh_type (class == ELFCLASS32 \
+#define xsh_type (clazz == ELFCLASS32 \
? elf_getu32(swap, sh32.sh_type) \
: elf_getu32(swap, sh64.sh_type))
-#define xph_addr (class == ELFCLASS32 \
+#define xph_addr (clazz == ELFCLASS32 \
? (void *) &ph32 \
: (void *) &ph64)
-#define xph_sizeof (class == ELFCLASS32 \
+#define xph_sizeof (clazz == ELFCLASS32 \
? sizeof ph32 \
: sizeof ph64)
-#define xph_type (class == ELFCLASS32 \
+#define xph_type (clazz == ELFCLASS32 \
? elf_getu32(swap, ph32.p_type) \
: elf_getu32(swap, ph64.p_type))
-#define xph_offset (off_t)(class == ELFCLASS32 \
+#define xph_offset (off_t)(clazz == ELFCLASS32 \
? elf_getu32(swap, ph32.p_offset) \
: elf_getu64(swap, ph64.p_offset))
-#define xph_align (size_t)((class == ELFCLASS32 \
+#define xph_align (size_t)((clazz == ELFCLASS32 \
? (off_t) (ph32.p_align ? \
elf_getu32(swap, ph32.p_align) : 4) \
: (off_t) (ph64.p_align ? \
elf_getu64(swap, ph64.p_align) : 4)))
-#define xph_filesz (size_t)((class == ELFCLASS32 \
+#define xph_filesz (size_t)((clazz == ELFCLASS32 \
? elf_getu32(swap, ph32.p_filesz) \
: elf_getu64(swap, ph64.p_filesz)))
-#define xnh_addr (class == ELFCLASS32 \
+#define xnh_addr (clazz == ELFCLASS32 \
? (void *) &nh32 \
: (void *) &nh64)
-#define xph_memsz (size_t)((class == ELFCLASS32 \
+#define xph_memsz (size_t)((clazz == ELFCLASS32 \
? elf_getu32(swap, ph32.p_memsz) \
: elf_getu64(swap, ph64.p_memsz)))
-#define xnh_sizeof (class == ELFCLASS32 \
+#define xnh_sizeof (clazz == ELFCLASS32 \
? sizeof nh32 \
: sizeof nh64)
-#define xnh_type (class == ELFCLASS32 \
+#define xnh_type (clazz == ELFCLASS32 \
? elf_getu32(swap, nh32.n_type) \
: elf_getu32(swap, nh64.n_type))
-#define xnh_namesz (class == ELFCLASS32 \
+#define xnh_namesz (clazz == ELFCLASS32 \
? elf_getu32(swap, nh32.n_namesz) \
: elf_getu32(swap, nh64.n_namesz))
-#define xnh_descsz (class == ELFCLASS32 \
+#define xnh_descsz (clazz == ELFCLASS32 \
? elf_getu32(swap, nh32.n_descsz) \
: elf_getu32(swap, nh64.n_descsz))
-#define prpsoffsets(i) (class == ELFCLASS32 \
+#define prpsoffsets(i) (clazz == ELFCLASS32 \
? prpsoffsets32[i] \
: prpsoffsets64[i])
-#define xcap_addr (class == ELFCLASS32 \
+#define xcap_addr (clazz == ELFCLASS32 \
? (void *) &cap32 \
: (void *) &cap64)
-#define xcap_sizeof (class == ELFCLASS32 \
+#define xcap_sizeof (clazz == ELFCLASS32 \
? sizeof cap32 \
: sizeof cap64)
-#define xcap_tag (class == ELFCLASS32 \
+#define xcap_tag (clazz == ELFCLASS32 \
? elf_getu32(swap, cap32.c_tag) \
: elf_getu64(swap, cap64.c_tag))
-#define xcap_val (class == ELFCLASS32 \
+#define xcap_val (clazz == ELFCLASS32 \
? elf_getu32(swap, cap32.c_un.c_val) \
: elf_getu64(swap, cap64.c_un.c_val))
#define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
#define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
-#define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
+#define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
/*
* Look through the program headers of an executable image, searching
#define FLAGS_DID_CORE_STYLE 4
private int
-dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
+dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
int num, size_t size, off_t fsize, int *flags)
{
Elf32_Phdr ph32;
if (offset >= (size_t)bufsize)
break;
offset = donote(ms, nbuf, offset, (size_t)bufsize,
- class, swap, 4, flags);
+ clazz, swap, 4, flags);
if (offset == 0)
break;
#endif
private size_t
-donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
- int class, int swap, size_t align, int *flags)
+donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
+ int clazz, int swap, size_t align, int *flags)
{
Elf32_Nhdr nh32;
Elf64_Nhdr nh64;
int os_style = -1;
#endif
uint32_t namesz, descsz;
+ unsigned char *nbuf = CAST(unsigned char *, vbuf);
(void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
offset += xnh_sizeof;
};
private int
-doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
+doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
size_t size, int *flags, int mach)
{
Elf32_Shdr sh32;
if (noff >= (size_t)xsh_size)
break;
noff = donote(ms, nbuf, (size_t)noff,
- (size_t)xsh_size, class, swap, 4,
+ (size_t)xsh_size, clazz, swap, 4,
flags);
if (noff == 0)
break;
* otherwise it's statically linked.
*/
private int
-dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
+dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
int num, size_t size, off_t fsize, int *flags, int sh_num)
{
Elf32_Phdr ph32;
if (offset >= (size_t)bufsize)
break;
offset = donote(ms, nbuf, offset,
- (size_t)bufsize, class, swap, align,
+ (size_t)bufsize, clazz, swap, align,
flags);
if (offset == 0)
break;
int32_t l;
char c[sizeof (int32_t)];
} u;
- int class;
+ int clazz;
int swap;
struct stat st;
off_t fsize;
}
fsize = st.st_size;
- class = buf[EI_CLASS];
+ clazz = buf[EI_CLASS];
- switch (class) {
+ switch (clazz) {
case ELFCLASS32:
#undef elf_getu
#define elf_getu(a, b) elf_getu32(a, b)
#define elfhdr elf64hdr
#include "elfclass.h"
default:
- if (file_printf(ms, ", unknown class %d", class) == -1)
+ if (file_printf(ms, ", unknown class %d", clazz) == -1)
return -1;
break;
}
#ifndef lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.116 2008/02/25 02:54:08 rrt Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.117 2008/03/01 22:21:49 rrt Exp $")
#endif /* lint */
private int match(struct magic_set *, struct magic *, uint32_t,
if (m->flag & INDIR) {
int off = m->in_offset;
if (m->in_op & FILE_OPINDIRECT) {
- const union VALUETYPE *q =
- ((const void *)(s + offset + off));
+ const union VALUETYPE *q = CAST(const union VALUETYPE *,
+ ((const void *)(s + offset + off)));
switch (m->in_type) {
case FILE_BYTE:
off = q->b;