2004-08-09 08:45:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Ian F. Darwin 1986-1995.
|
|
|
|
* Software written by Ian F. Darwin and others;
|
|
|
|
* maintained 1995-present by Christos Zoulas and others.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice immediately at the beginning of the file, without modification,
|
|
|
|
* this list of conditions, and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
1994-09-03 19:16:28 +00:00
|
|
|
/*
|
|
|
|
* apprentice - make one pass through /etc/magic, learning its secrets.
|
|
|
|
*/
|
|
|
|
|
2001-07-30 03:09:46 +00:00
|
|
|
#include "file.h"
|
2009-05-02 06:25:51 +00:00
|
|
|
|
|
|
|
#ifndef lint
|
2009-05-18 22:27:42 +00:00
|
|
|
FILE_RCSID("@(#)$File: apprentice.c,v 1.151 2009/03/18 15:19:23 christos Exp $")
|
2009-05-02 06:25:51 +00:00
|
|
|
#endif /* lint */
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
#include "magic.h"
|
2008-01-13 20:35:16 +00:00
|
|
|
#include "patchlevel.h"
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <stdlib.h>
|
2001-10-08 22:50:54 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <string.h>
|
2007-05-24 16:12:23 +00:00
|
|
|
#include <assert.h>
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <ctype.h>
|
2001-03-25 18:37:04 +00:00
|
|
|
#include <fcntl.h>
|
2002-09-16 05:54:26 +00:00
|
|
|
#ifdef QUICK
|
2001-03-25 18:37:04 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
2009-01-02 02:57:16 +00:00
|
|
|
#include <dirent.h>
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
#define EATAB {while (isascii((unsigned char) *l) && \
|
|
|
|
isspace((unsigned char) *l)) ++l;}
|
1996-01-23 12:29:07 +00:00
|
|
|
#define LOWCASE(l) (isupper((unsigned char) (l)) ? \
|
|
|
|
tolower((unsigned char) (l)) : (l))
|
2001-04-25 07:41:21 +00:00
|
|
|
/*
|
|
|
|
* Work around a bug in headers on Digital Unix.
|
|
|
|
* At least confirmed for: OSF1 V4.0 878
|
|
|
|
*/
|
|
|
|
#if defined(__osf__) && defined(__DECC)
|
|
|
|
#ifdef MAP_FAILED
|
|
|
|
#undef MAP_FAILED
|
|
|
|
#endif
|
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2001-04-25 07:41:21 +00:00
|
|
|
#ifndef MAP_FAILED
|
|
|
|
#define MAP_FAILED (void *) -1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAP_FILE
|
|
|
|
#define MAP_FILE 0
|
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
#define MAXPATHLEN 1024
|
2000-11-05 08:33:55 +00:00
|
|
|
#endif
|
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
struct magic_entry {
|
|
|
|
struct magic *mp;
|
|
|
|
uint32_t cont_count;
|
|
|
|
uint32_t max_count;
|
|
|
|
};
|
2004-12-28 04:31:47 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
int file_formats[FILE_NAMES_SIZE];
|
|
|
|
const size_t file_nformats = FILE_NAMES_SIZE;
|
|
|
|
const char *file_names[FILE_NAMES_SIZE];
|
|
|
|
const size_t file_nnames = FILE_NAMES_SIZE;
|
2007-05-24 16:12:23 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
private int getvalue(struct magic_set *ms, struct magic *, const char **, int);
|
2004-08-09 08:45:41 +00:00
|
|
|
private int hextoint(int);
|
2009-05-18 22:27:42 +00:00
|
|
|
private const char *getstr(struct magic_set *, struct magic *, const char *,
|
|
|
|
int);
|
2006-06-19 07:52:15 +00:00
|
|
|
private int parse(struct magic_set *, struct magic_entry **, uint32_t *,
|
2007-05-24 16:12:23 +00:00
|
|
|
const char *, size_t, int);
|
2006-06-19 07:52:15 +00:00
|
|
|
private void eatsize(const char **);
|
2004-08-09 08:45:41 +00:00
|
|
|
private int apprentice_1(struct magic_set *, const char *, int, struct mlist *);
|
2006-06-19 07:52:15 +00:00
|
|
|
private size_t apprentice_magic_strength(const struct magic *);
|
|
|
|
private int apprentice_sort(const void *, const void *);
|
2009-01-02 02:57:16 +00:00
|
|
|
private int apprentice_load(struct magic_set *, struct magic **, uint32_t *,
|
2004-08-09 08:45:41 +00:00
|
|
|
const char *, int);
|
|
|
|
private void byteswap(struct magic *, uint32_t);
|
|
|
|
private void bs1(struct magic *);
|
|
|
|
private uint16_t swap2(uint16_t);
|
|
|
|
private uint32_t swap4(uint32_t);
|
2007-05-24 16:12:23 +00:00
|
|
|
private uint64_t swap8(uint64_t);
|
2009-05-02 06:25:51 +00:00
|
|
|
private char *mkdbname(struct magic_set *, const char *, int);
|
2004-08-09 08:45:41 +00:00
|
|
|
private int apprentice_map(struct magic_set *, struct magic **, uint32_t *,
|
|
|
|
const char *);
|
|
|
|
private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
|
|
|
|
const char *);
|
2007-05-24 16:12:23 +00:00
|
|
|
private int check_format_type(const char *, int);
|
2004-12-28 04:31:47 +00:00
|
|
|
private int check_format(struct magic_set *, struct magic *);
|
2008-01-13 20:35:16 +00:00
|
|
|
private int get_op(char);
|
2009-01-02 02:57:16 +00:00
|
|
|
private int parse_mime(struct magic_set *, struct magic_entry *, const char *);
|
2009-05-02 06:25:51 +00:00
|
|
|
private int parse_strength(struct magic_set *, struct magic_entry *, const char *);
|
|
|
|
private int parse_apple(struct magic_set *, struct magic_entry *, const char *);
|
2009-01-02 02:57:16 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
|
|
|
|
private size_t maxmagic = 0;
|
|
|
|
private size_t magicsize = sizeof(struct magic);
|
2001-03-25 18:37:04 +00:00
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
private const char usg_hdr[] = "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
|
|
|
|
|
|
|
|
private struct {
|
|
|
|
const char *name;
|
|
|
|
size_t len;
|
|
|
|
int (*fun)(struct magic_set *, struct magic_entry *, const char *);
|
|
|
|
} bang[] = {
|
|
|
|
#define DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name }
|
|
|
|
DECLARE_FIELD(mime),
|
2009-05-02 06:25:51 +00:00
|
|
|
DECLARE_FIELD(apple),
|
2009-01-02 02:57:16 +00:00
|
|
|
DECLARE_FIELD(strength),
|
|
|
|
#undef DECLARE_FIELD
|
|
|
|
{ NULL, 0, NULL }
|
|
|
|
};
|
2006-06-19 07:52:15 +00:00
|
|
|
|
2001-07-30 03:09:46 +00:00
|
|
|
#ifdef COMPILE_ONLY
|
|
|
|
|
2002-09-16 05:54:26 +00:00
|
|
|
int main(int, char *[]);
|
2001-07-30 03:09:46 +00:00
|
|
|
|
|
|
|
int
|
2002-09-16 05:54:26 +00:00
|
|
|
main(int argc, char *argv[])
|
2001-07-30 03:09:46 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2004-08-09 08:45:41 +00:00
|
|
|
struct magic_set *ms;
|
|
|
|
char *progname;
|
2001-07-30 03:09:46 +00:00
|
|
|
|
|
|
|
if ((progname = strrchr(argv[0], '/')) != NULL)
|
|
|
|
progname++;
|
|
|
|
else
|
|
|
|
progname = argv[0];
|
|
|
|
|
|
|
|
if (argc != 2) {
|
2004-08-09 08:45:41 +00:00
|
|
|
(void)fprintf(stderr, "Usage: %s file\n", progname);
|
|
|
|
return 1;
|
2001-07-30 03:09:46 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
|
|
|
|
(void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
|
|
|
|
if (ret == 1)
|
|
|
|
(void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
|
|
|
|
magic_close(ms);
|
|
|
|
return ret;
|
2001-07-30 03:09:46 +00:00
|
|
|
}
|
|
|
|
#endif /* COMPILE_ONLY */
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
static const struct type_tbl_s {
|
2009-01-02 02:57:16 +00:00
|
|
|
const char name[16];
|
2007-05-24 21:59:38 +00:00
|
|
|
const size_t len;
|
|
|
|
const int type;
|
|
|
|
const int format;
|
|
|
|
} type_tbl[] = {
|
|
|
|
# define XX(s) s, (sizeof(s) - 1)
|
2009-01-02 02:57:16 +00:00
|
|
|
# define XX_NULL "", 0
|
2007-05-24 21:59:38 +00:00
|
|
|
{ XX("byte"), FILE_BYTE, FILE_FMT_NUM },
|
|
|
|
{ XX("short"), FILE_SHORT, FILE_FMT_NUM },
|
|
|
|
{ XX("default"), FILE_DEFAULT, FILE_FMT_STR },
|
|
|
|
{ XX("long"), FILE_LONG, FILE_FMT_NUM },
|
|
|
|
{ XX("string"), FILE_STRING, FILE_FMT_STR },
|
|
|
|
{ XX("date"), FILE_DATE, FILE_FMT_STR },
|
|
|
|
{ XX("beshort"), FILE_BESHORT, FILE_FMT_NUM },
|
|
|
|
{ XX("belong"), FILE_BELONG, FILE_FMT_NUM },
|
|
|
|
{ XX("bedate"), FILE_BEDATE, FILE_FMT_STR },
|
|
|
|
{ XX("leshort"), FILE_LESHORT, FILE_FMT_NUM },
|
|
|
|
{ XX("lelong"), FILE_LELONG, FILE_FMT_NUM },
|
|
|
|
{ XX("ledate"), FILE_LEDATE, FILE_FMT_STR },
|
|
|
|
{ XX("pstring"), FILE_PSTRING, FILE_FMT_STR },
|
|
|
|
{ XX("ldate"), FILE_LDATE, FILE_FMT_STR },
|
|
|
|
{ XX("beldate"), FILE_BELDATE, FILE_FMT_STR },
|
|
|
|
{ XX("leldate"), FILE_LELDATE, FILE_FMT_STR },
|
|
|
|
{ XX("regex"), FILE_REGEX, FILE_FMT_STR },
|
|
|
|
{ XX("bestring16"), FILE_BESTRING16, FILE_FMT_STR },
|
|
|
|
{ XX("lestring16"), FILE_LESTRING16, FILE_FMT_STR },
|
|
|
|
{ XX("search"), FILE_SEARCH, FILE_FMT_STR },
|
|
|
|
{ XX("medate"), FILE_MEDATE, FILE_FMT_STR },
|
|
|
|
{ XX("meldate"), FILE_MELDATE, FILE_FMT_STR },
|
|
|
|
{ XX("melong"), FILE_MELONG, FILE_FMT_NUM },
|
|
|
|
{ XX("quad"), FILE_QUAD, FILE_FMT_QUAD },
|
|
|
|
{ XX("lequad"), FILE_LEQUAD, FILE_FMT_QUAD },
|
|
|
|
{ XX("bequad"), FILE_BEQUAD, FILE_FMT_QUAD },
|
|
|
|
{ XX("qdate"), FILE_QDATE, FILE_FMT_STR },
|
|
|
|
{ XX("leqdate"), FILE_LEQDATE, FILE_FMT_STR },
|
|
|
|
{ XX("beqdate"), FILE_BEQDATE, FILE_FMT_STR },
|
|
|
|
{ XX("qldate"), FILE_QLDATE, FILE_FMT_STR },
|
|
|
|
{ XX("leqldate"), FILE_LEQLDATE, FILE_FMT_STR },
|
|
|
|
{ XX("beqldate"), FILE_BEQLDATE, FILE_FMT_STR },
|
2008-01-13 20:35:16 +00:00
|
|
|
{ XX("float"), FILE_FLOAT, FILE_FMT_FLOAT },
|
|
|
|
{ XX("befloat"), FILE_BEFLOAT, FILE_FMT_FLOAT },
|
|
|
|
{ XX("lefloat"), FILE_LEFLOAT, FILE_FMT_FLOAT },
|
|
|
|
{ XX("double"), FILE_DOUBLE, FILE_FMT_DOUBLE },
|
|
|
|
{ XX("bedouble"), FILE_BEDOUBLE, FILE_FMT_DOUBLE },
|
|
|
|
{ XX("ledouble"), FILE_LEDOUBLE, FILE_FMT_DOUBLE },
|
2009-05-02 06:25:51 +00:00
|
|
|
{ XX("leid3"), FILE_LEID3, FILE_FMT_NUM },
|
|
|
|
{ XX("beid3"), FILE_BEID3, FILE_FMT_NUM },
|
|
|
|
{ XX("indirect"), FILE_INDIRECT, FILE_FMT_NONE },
|
2007-05-24 21:59:38 +00:00
|
|
|
{ XX_NULL, FILE_INVALID, FILE_FMT_NONE },
|
|
|
|
# undef XX
|
|
|
|
# undef XX_NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
private int
|
|
|
|
get_type(const char *l, const char **t)
|
|
|
|
{
|
|
|
|
const struct type_tbl_s *p;
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
for (p = type_tbl; p->len; p++) {
|
2007-05-24 21:59:38 +00:00
|
|
|
if (strncmp(l, p->name, p->len) == 0) {
|
|
|
|
if (t)
|
|
|
|
*t = l + p->len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void
|
|
|
|
init_file_tables(void)
|
|
|
|
{
|
|
|
|
static int done = 0;
|
|
|
|
const struct type_tbl_s *p;
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
return;
|
|
|
|
done++;
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
for (p = type_tbl; p->len; p++) {
|
2007-05-24 21:59:38 +00:00
|
|
|
assert(p->type < FILE_NAMES_SIZE);
|
|
|
|
file_names[p->type] = p->name;
|
|
|
|
file_formats[p->type] = p->format;
|
|
|
|
}
|
|
|
|
}
|
2001-03-25 18:37:04 +00:00
|
|
|
|
|
|
|
/*
|
2009-01-02 02:57:16 +00:00
|
|
|
* Handle one file or directory.
|
2001-03-25 18:37:04 +00:00
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
|
|
|
apprentice_1(struct magic_set *ms, const char *fn, int action,
|
|
|
|
struct mlist *mlist)
|
2001-03-25 18:37:04 +00:00
|
|
|
{
|
|
|
|
struct magic *magic = NULL;
|
2002-09-16 05:54:26 +00:00
|
|
|
uint32_t nmagic = 0;
|
2001-03-25 18:37:04 +00:00
|
|
|
struct mlist *ml;
|
|
|
|
int rv = -1;
|
2004-08-09 08:45:41 +00:00
|
|
|
int mapped;
|
2001-03-25 18:37:04 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if (magicsize != FILE_MAGICSIZE) {
|
|
|
|
file_error(ms, 0, "magic element size %lu != %lu",
|
|
|
|
(unsigned long)sizeof(*magic),
|
|
|
|
(unsigned long)FILE_MAGICSIZE);
|
|
|
|
return -1;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
|
|
|
|
if (action == FILE_COMPILE) {
|
2009-01-02 02:57:16 +00:00
|
|
|
rv = apprentice_load(ms, &magic, &nmagic, fn, action);
|
2004-08-09 08:45:41 +00:00
|
|
|
if (rv != 0)
|
|
|
|
return -1;
|
|
|
|
rv = apprentice_compile(ms, &magic, &nmagic, fn);
|
|
|
|
free(magic);
|
2001-03-25 18:37:04 +00:00
|
|
|
return rv;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
#ifndef COMPILE_ONLY
|
|
|
|
if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
2004-12-28 04:31:47 +00:00
|
|
|
file_magwarn(ms, "using regular magic file `%s'", fn);
|
2009-01-02 02:57:16 +00:00
|
|
|
rv = apprentice_load(ms, &magic, &nmagic, fn, action);
|
2004-08-09 08:45:41 +00:00
|
|
|
if (rv != 0)
|
2001-03-25 18:37:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
mapped = rv;
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
if (magic == NULL) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_delmagic(magic, mapped, nmagic);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_delmagic(magic, mapped, nmagic);
|
2007-05-24 16:12:23 +00:00
|
|
|
file_oomem(ms, sizeof(*ml));
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-03-25 18:37:04 +00:00
|
|
|
|
|
|
|
ml->magic = magic;
|
|
|
|
ml->nmagic = nmagic;
|
2004-08-09 08:45:41 +00:00
|
|
|
ml->mapped = mapped;
|
2001-03-25 18:37:04 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
mlist->prev->next = ml;
|
|
|
|
ml->prev = mlist->prev;
|
|
|
|
ml->next = mlist;
|
|
|
|
mlist->prev = ml;
|
2001-03-25 18:37:04 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
return 0;
|
2001-07-30 03:09:46 +00:00
|
|
|
#endif /* COMPILE_ONLY */
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
protected void
|
|
|
|
file_delmagic(struct magic *p, int type, size_t entries)
|
|
|
|
{
|
|
|
|
if (p == NULL)
|
|
|
|
return;
|
|
|
|
switch (type) {
|
|
|
|
case 2:
|
2009-05-18 22:27:42 +00:00
|
|
|
#ifdef QUICK
|
2004-08-09 08:45:41 +00:00
|
|
|
p--;
|
|
|
|
(void)munmap((void *)p, sizeof(*p) * (entries + 1));
|
|
|
|
break;
|
2009-05-18 22:27:42 +00:00
|
|
|
#else
|
|
|
|
(void)&entries;
|
|
|
|
abort();
|
|
|
|
/*NOTREACHED*/
|
2008-01-13 20:35:16 +00:00
|
|
|
#endif
|
2004-08-09 08:45:41 +00:00
|
|
|
case 1:
|
|
|
|
p--;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case 0:
|
|
|
|
free(p);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
/* const char *fn: list of magic files and directories */
|
2004-08-09 08:45:41 +00:00
|
|
|
protected struct mlist *
|
|
|
|
file_apprentice(struct magic_set *ms, const char *fn, int action)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2009-01-02 02:57:16 +00:00
|
|
|
char *p, *mfn;
|
1996-01-23 12:29:07 +00:00
|
|
|
int file_err, errs = -1;
|
2004-08-09 08:45:41 +00:00
|
|
|
struct mlist *mlist;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
init_file_tables();
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if (fn == NULL)
|
|
|
|
fn = getenv("MAGIC");
|
|
|
|
if (fn == NULL)
|
|
|
|
fn = MAGIC;
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
if ((mfn = strdup(fn)) == NULL) {
|
2007-05-24 16:12:23 +00:00
|
|
|
file_oomem(ms, strlen(fn));
|
2004-08-09 08:45:41 +00:00
|
|
|
return NULL;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
fn = mfn;
|
2004-08-09 08:45:41 +00:00
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
if ((mlist = CAST(struct mlist *, malloc(sizeof(*mlist)))) == NULL) {
|
2004-08-09 08:45:41 +00:00
|
|
|
free(mfn);
|
2007-05-24 16:12:23 +00:00
|
|
|
file_oomem(ms, sizeof(*mlist));
|
2004-08-09 08:45:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mlist->next = mlist->prev = mlist;
|
|
|
|
|
1996-01-23 12:29:07 +00:00
|
|
|
while (fn) {
|
2000-11-05 08:33:55 +00:00
|
|
|
p = strchr(fn, PATHSEP);
|
1996-01-23 12:29:07 +00:00
|
|
|
if (p)
|
|
|
|
*p++ = '\0';
|
2004-08-09 08:45:41 +00:00
|
|
|
if (*fn == '\0')
|
|
|
|
break;
|
|
|
|
file_err = apprentice_1(ms, fn, action, mlist);
|
2009-01-02 02:57:16 +00:00
|
|
|
errs = MAX(errs, file_err);
|
1996-01-23 12:29:07 +00:00
|
|
|
fn = p;
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
if (errs == -1) {
|
|
|
|
free(mfn);
|
|
|
|
free(mlist);
|
|
|
|
mlist = NULL;
|
|
|
|
file_error(ms, 0, "could not find any magic files!");
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-01-23 12:29:07 +00:00
|
|
|
free(mfn);
|
2004-08-09 08:45:41 +00:00
|
|
|
return mlist;
|
1996-01-23 12:29:07 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
/*
|
|
|
|
* Get weight of this magic entry, for sorting purposes.
|
|
|
|
*/
|
2006-06-19 07:52:15 +00:00
|
|
|
private size_t
|
|
|
|
apprentice_magic_strength(const struct magic *m)
|
|
|
|
{
|
2007-05-24 16:12:23 +00:00
|
|
|
#define MULT 10
|
|
|
|
size_t val = 2 * MULT; /* baseline strength */
|
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
switch (m->type) {
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_DEFAULT: /* make sure this sorts last */
|
2009-01-02 02:57:16 +00:00
|
|
|
if (m->factor_op != FILE_FACTOR_OP_NONE)
|
|
|
|
abort();
|
2007-05-24 21:59:38 +00:00
|
|
|
return 0;
|
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_BYTE:
|
2007-05-24 16:12:23 +00:00
|
|
|
val += 1 * MULT;
|
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
|
|
|
|
case FILE_SHORT:
|
|
|
|
case FILE_LESHORT:
|
|
|
|
case FILE_BESHORT:
|
2007-05-24 16:12:23 +00:00
|
|
|
val += 2 * MULT;
|
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
|
|
|
|
case FILE_LONG:
|
|
|
|
case FILE_LELONG:
|
|
|
|
case FILE_BELONG:
|
|
|
|
case FILE_MELONG:
|
2007-05-24 16:12:23 +00:00
|
|
|
val += 4 * MULT;
|
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
|
|
|
|
case FILE_PSTRING:
|
|
|
|
case FILE_STRING:
|
2007-05-24 16:12:23 +00:00
|
|
|
val += m->vallen * MULT;
|
|
|
|
break;
|
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_BESTRING16:
|
|
|
|
case FILE_LESTRING16:
|
2007-05-24 16:12:23 +00:00
|
|
|
val += m->vallen * MULT / 2;
|
|
|
|
break;
|
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_SEARCH:
|
2007-05-24 16:12:23 +00:00
|
|
|
case FILE_REGEX:
|
2009-01-02 02:57:16 +00:00
|
|
|
val += m->vallen * MAX(MULT / m->vallen, 1);
|
2007-05-24 16:12:23 +00:00
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
|
|
|
|
case FILE_DATE:
|
|
|
|
case FILE_LEDATE:
|
|
|
|
case FILE_BEDATE:
|
|
|
|
case FILE_MEDATE:
|
|
|
|
case FILE_LDATE:
|
|
|
|
case FILE_LELDATE:
|
|
|
|
case FILE_BELDATE:
|
|
|
|
case FILE_MELDATE:
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_FLOAT:
|
|
|
|
case FILE_BEFLOAT:
|
|
|
|
case FILE_LEFLOAT:
|
2007-05-24 16:12:23 +00:00
|
|
|
val += 4 * MULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_QUAD:
|
|
|
|
case FILE_BEQUAD:
|
|
|
|
case FILE_LEQUAD:
|
|
|
|
case FILE_QDATE:
|
|
|
|
case FILE_LEQDATE:
|
|
|
|
case FILE_BEQDATE:
|
|
|
|
case FILE_QLDATE:
|
|
|
|
case FILE_LEQLDATE:
|
|
|
|
case FILE_BEQLDATE:
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_DOUBLE:
|
|
|
|
case FILE_BEDOUBLE:
|
|
|
|
case FILE_LEDOUBLE:
|
2007-05-24 16:12:23 +00:00
|
|
|
val += 8 * MULT;
|
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
|
|
|
|
default:
|
2007-05-24 16:12:23 +00:00
|
|
|
val = 0;
|
|
|
|
(void)fprintf(stderr, "Bad type %d\n", m->type);
|
|
|
|
abort();
|
2006-06-19 07:52:15 +00:00
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
|
|
|
|
switch (m->reln) {
|
|
|
|
case 'x': /* matches anything penalize */
|
2009-01-02 02:57:16 +00:00
|
|
|
case '!': /* matches almost anything penalize */
|
2007-05-24 16:12:23 +00:00
|
|
|
val = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '=': /* Exact match, prefer */
|
|
|
|
val += MULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '>':
|
|
|
|
case '<': /* comparison match reduce strength */
|
|
|
|
val -= 2 * MULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '^':
|
|
|
|
case '&': /* masking bits, we could count them too */
|
|
|
|
val -= MULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
(void)fprintf(stderr, "Bad relation %c\n", m->reln);
|
|
|
|
abort();
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
|
|
|
|
if (val == 0) /* ensure we only return 0 for FILE_DEFAULT */
|
|
|
|
val = 1;
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
switch (m->factor_op) {
|
|
|
|
case FILE_FACTOR_OP_NONE:
|
|
|
|
break;
|
|
|
|
case FILE_FACTOR_OP_PLUS:
|
|
|
|
val += m->factor;
|
|
|
|
break;
|
|
|
|
case FILE_FACTOR_OP_MINUS:
|
|
|
|
val -= m->factor;
|
|
|
|
break;
|
|
|
|
case FILE_FACTOR_OP_TIMES:
|
|
|
|
val *= m->factor;
|
|
|
|
break;
|
|
|
|
case FILE_FACTOR_OP_DIV:
|
|
|
|
val /= m->factor;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Magic entries with no description get a bonus because they depend
|
|
|
|
* on subsequent magic entries to print something.
|
|
|
|
*/
|
|
|
|
if (m->desc[0] == '\0')
|
|
|
|
val++;
|
2007-05-24 16:12:23 +00:00
|
|
|
return val;
|
2006-06-19 07:52:15 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
/*
|
|
|
|
* Sort callback for sorting entries by "strength" (basically length)
|
|
|
|
*/
|
2006-06-19 07:52:15 +00:00
|
|
|
private int
|
|
|
|
apprentice_sort(const void *a, const void *b)
|
|
|
|
{
|
2009-01-02 02:57:16 +00:00
|
|
|
const struct magic_entry *ma = CAST(const struct magic_entry *, a);
|
|
|
|
const struct magic_entry *mb = CAST(const struct magic_entry *, b);
|
2006-06-19 07:52:15 +00:00
|
|
|
size_t sa = apprentice_magic_strength(ma->mp);
|
|
|
|
size_t sb = apprentice_magic_strength(mb->mp);
|
|
|
|
if (sa == sb)
|
|
|
|
return 0;
|
|
|
|
else if (sa > sb)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
private void
|
|
|
|
set_test_type(struct magic *mstart, struct magic *m)
|
|
|
|
{
|
|
|
|
switch (m->type) {
|
|
|
|
case FILE_BYTE:
|
|
|
|
case FILE_SHORT:
|
|
|
|
case FILE_LONG:
|
|
|
|
case FILE_DATE:
|
|
|
|
case FILE_BESHORT:
|
|
|
|
case FILE_BELONG:
|
|
|
|
case FILE_BEDATE:
|
|
|
|
case FILE_LESHORT:
|
|
|
|
case FILE_LELONG:
|
|
|
|
case FILE_LEDATE:
|
|
|
|
case FILE_LDATE:
|
|
|
|
case FILE_BELDATE:
|
|
|
|
case FILE_LELDATE:
|
|
|
|
case FILE_MEDATE:
|
|
|
|
case FILE_MELDATE:
|
|
|
|
case FILE_MELONG:
|
|
|
|
case FILE_QUAD:
|
|
|
|
case FILE_LEQUAD:
|
|
|
|
case FILE_BEQUAD:
|
|
|
|
case FILE_QDATE:
|
|
|
|
case FILE_LEQDATE:
|
|
|
|
case FILE_BEQDATE:
|
|
|
|
case FILE_QLDATE:
|
|
|
|
case FILE_LEQLDATE:
|
|
|
|
case FILE_BEQLDATE:
|
|
|
|
case FILE_FLOAT:
|
|
|
|
case FILE_BEFLOAT:
|
|
|
|
case FILE_LEFLOAT:
|
|
|
|
case FILE_DOUBLE:
|
|
|
|
case FILE_BEDOUBLE:
|
|
|
|
case FILE_LEDOUBLE:
|
|
|
|
case FILE_STRING:
|
|
|
|
case FILE_PSTRING:
|
|
|
|
case FILE_BESTRING16:
|
|
|
|
case FILE_LESTRING16:
|
|
|
|
/* binary test, set flag */
|
|
|
|
mstart->flag |= BINTEST;
|
|
|
|
break;
|
|
|
|
case FILE_REGEX:
|
|
|
|
case FILE_SEARCH:
|
2009-01-02 03:31:45 +00:00
|
|
|
#ifndef COMPILE_ONLY
|
2009-01-02 02:57:16 +00:00
|
|
|
/* binary test if pattern is not text */
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_looks_utf8(m->value.us, (size_t)m->vallen, NULL,
|
|
|
|
NULL) <= 0)
|
2009-01-02 02:57:16 +00:00
|
|
|
mstart->flag |= BINTEST;
|
2009-01-02 03:31:45 +00:00
|
|
|
#endif
|
2009-01-02 02:57:16 +00:00
|
|
|
break;
|
|
|
|
case FILE_DEFAULT:
|
|
|
|
/* can't deduce anything; we shouldn't see this at the
|
|
|
|
top level anyway */
|
|
|
|
break;
|
|
|
|
case FILE_INVALID:
|
|
|
|
default:
|
|
|
|
/* invalid search type, but no need to complain here */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-25 18:37:04 +00:00
|
|
|
/*
|
2009-01-02 02:57:16 +00:00
|
|
|
* Load and parse one file.
|
2001-03-25 18:37:04 +00:00
|
|
|
*/
|
2009-01-02 02:57:16 +00:00
|
|
|
private void
|
|
|
|
load_1(struct magic_set *ms, int action, const char *fn, int *errs,
|
|
|
|
struct magic_entry **marray, uint32_t *marraycount)
|
1996-01-23 12:29:07 +00:00
|
|
|
{
|
2008-01-13 20:35:16 +00:00
|
|
|
char line[BUFSIZ];
|
2007-05-24 16:12:23 +00:00
|
|
|
size_t lineno = 0;
|
2009-01-02 02:57:16 +00:00
|
|
|
FILE *f = fopen(ms->file = fn, "r");
|
2001-03-25 18:37:04 +00:00
|
|
|
if (f == NULL) {
|
1996-01-23 12:29:07 +00:00
|
|
|
if (errno != ENOENT)
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno, "cannot read magic file `%s'",
|
2009-01-02 02:57:16 +00:00
|
|
|
fn);
|
|
|
|
(*errs)++;
|
|
|
|
} else {
|
|
|
|
/* read and parse this file */
|
|
|
|
for (ms->line = 1; fgets(line, sizeof(line), f) != NULL; ms->line++) {
|
|
|
|
size_t len;
|
|
|
|
len = strlen(line);
|
|
|
|
if (len == 0) /* null line, garbage, etc */
|
|
|
|
continue;
|
|
|
|
if (line[len - 1] == '\n') {
|
|
|
|
lineno++;
|
|
|
|
line[len - 1] = '\0'; /* delete newline */
|
|
|
|
}
|
|
|
|
if (line[0] == '\0') /* empty, do not parse */
|
|
|
|
continue;
|
|
|
|
if (line[0] == '#') /* comment, do not parse */
|
|
|
|
continue;
|
|
|
|
if (line[0] == '!' && line[1] == ':') {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; bang[i].name != NULL; i++) {
|
|
|
|
if (len - 2 > bang[i].len &&
|
|
|
|
memcmp(bang[i].name, line + 2,
|
|
|
|
bang[i].len) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bang[i].name == NULL) {
|
|
|
|
file_error(ms, 0,
|
|
|
|
"Unknown !: entry `%s'", line);
|
|
|
|
(*errs)++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*marraycount == 0) {
|
|
|
|
file_error(ms, 0,
|
|
|
|
"No current entry for :!%s type",
|
|
|
|
bang[i].name);
|
|
|
|
(*errs)++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((*bang[i].fun)(ms,
|
|
|
|
&(*marray)[*marraycount - 1],
|
|
|
|
line + bang[i].len + 2) != 0) {
|
|
|
|
(*errs)++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (parse(ms, marray, marraycount, line, lineno,
|
|
|
|
action) != 0)
|
|
|
|
(*errs)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)fclose(f);
|
1996-01-23 12:29:07 +00:00
|
|
|
}
|
2009-01-02 02:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse a file or directory of files
|
|
|
|
* const char *fn: name of magic file or directory
|
|
|
|
*/
|
|
|
|
private int
|
|
|
|
apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
|
|
|
|
const char *fn, int action)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
struct dirent *d;
|
|
|
|
|
|
|
|
ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
|
1996-01-23 12:29:07 +00:00
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
maxmagic = MAXMAGIS;
|
2009-01-02 02:57:16 +00:00
|
|
|
if ((marray = CAST(struct magic_entry *, calloc(maxmagic,
|
|
|
|
sizeof(*marray)))) == NULL) {
|
2007-05-24 16:12:23 +00:00
|
|
|
file_oomem(ms, maxmagic * sizeof(*marray));
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
marraycount = 0;
|
2001-03-25 18:37:04 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
/* print silly verbose header for USG compat. */
|
|
|
|
if (action == FILE_CHECK)
|
2009-01-02 02:57:16 +00:00
|
|
|
(void)fprintf(stderr, "%s\n", usg_hdr);
|
|
|
|
|
|
|
|
/* load directory or file */
|
2009-05-02 06:25:51 +00:00
|
|
|
/* FIXME: Read file names and sort them to prevent
|
|
|
|
non-determinism. See Debian bug #488562. */
|
2009-01-02 02:57:16 +00:00
|
|
|
if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
|
|
|
|
dir = opendir(fn);
|
|
|
|
if (dir) {
|
|
|
|
while ((d = readdir(dir)) != NULL) {
|
|
|
|
snprintf(subfn, sizeof(subfn), "%s/%s",
|
|
|
|
fn, d->d_name);
|
|
|
|
if (stat(subfn, &st) == 0 &&
|
|
|
|
S_ISREG(st.st_mode)) {
|
|
|
|
load_1(ms, action, subfn, &errs,
|
|
|
|
&marray, &marraycount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
} else
|
|
|
|
errs++;
|
|
|
|
} else
|
|
|
|
load_1(ms, action, fn, &errs, &marray, &marraycount);
|
|
|
|
if (errs)
|
|
|
|
goto out;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
/* Set types of tests */
|
|
|
|
for (i = 0; i < marraycount; ) {
|
|
|
|
if (marray[i].mp->cont_level != 0) {
|
|
|
|
i++;
|
1994-09-03 19:16:28 +00:00
|
|
|
continue;
|
2007-05-24 16:12:23 +00:00
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
starttest = i;
|
|
|
|
do {
|
|
|
|
static const char text[] = "text";
|
|
|
|
static const char binary[] = "binary";
|
|
|
|
static const size_t len = sizeof(text);
|
|
|
|
set_test_type(marray[starttest].mp, marray[i].mp);
|
|
|
|
if ((ms->flags & MAGIC_DEBUG) == 0)
|
|
|
|
continue;
|
|
|
|
(void)fprintf(stderr, "%s%s%s: %s\n",
|
|
|
|
marray[i].mp->mimetype,
|
|
|
|
marray[i].mp->mimetype[0] == '\0' ? "" : "; ",
|
|
|
|
marray[i].mp->desc[0] ? marray[i].mp->desc :
|
|
|
|
"(no description)",
|
|
|
|
marray[i].mp->flag & BINTEST ? binary : text);
|
|
|
|
if (marray[i].mp->flag & BINTEST) {
|
|
|
|
char *p = strstr(marray[i].mp->desc, text);
|
|
|
|
if (p && (p == marray[i].mp->desc ||
|
|
|
|
isspace((unsigned char)p[-1])) &&
|
|
|
|
(p + len - marray[i].mp->desc ==
|
|
|
|
MAXstring || (p[len] == '\0' ||
|
|
|
|
isspace((unsigned char)p[len]))))
|
|
|
|
(void)fprintf(stderr, "*** Possible "
|
|
|
|
"binary test for text type\n");
|
|
|
|
}
|
|
|
|
} while (++i < marraycount && marray[i].mp->cont_level != 0);
|
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
|
|
|
|
qsort(marray, marraycount, sizeof(*marray), apprentice_sort);
|
2009-01-02 02:57:16 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
/*
|
|
|
|
* Make sure that any level 0 "default" line is last (if one exists).
|
|
|
|
*/
|
|
|
|
for (i = 0; i < marraycount; i++) {
|
|
|
|
if (marray[i].mp->cont_level == 0 &&
|
|
|
|
marray[i].mp->type == FILE_DEFAULT) {
|
|
|
|
while (++i < marraycount)
|
|
|
|
if (marray[i].mp->cont_level == 0)
|
|
|
|
break;
|
|
|
|
if (i != marraycount) {
|
|
|
|
ms->line = marray[i].mp->lineno; /* XXX - Ugh! */
|
|
|
|
file_magwarn(ms,
|
|
|
|
"level 0 \"default\" did not sort last");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
for (i = 0; i < marraycount; i++)
|
2006-06-19 07:52:15 +00:00
|
|
|
mentrycount += marray[i].cont_count;
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
slen = sizeof(**magicp) * mentrycount;
|
|
|
|
if ((*magicp = CAST(struct magic *, malloc(slen))) == NULL) {
|
|
|
|
file_oomem(ms, slen);
|
2006-06-19 07:52:15 +00:00
|
|
|
errs++;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
mentrycount = 0;
|
|
|
|
for (i = 0; i < marraycount; i++) {
|
|
|
|
(void)memcpy(*magicp + mentrycount, marray[i].mp,
|
|
|
|
marray[i].cont_count * sizeof(**magicp));
|
|
|
|
mentrycount += marray[i].cont_count;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
for (i = 0; i < marraycount; i++)
|
|
|
|
free(marray[i].mp);
|
|
|
|
free(marray);
|
2001-03-25 18:37:04 +00:00
|
|
|
if (errs) {
|
|
|
|
*magicp = NULL;
|
|
|
|
*nmagicp = 0;
|
2006-06-19 07:52:15 +00:00
|
|
|
return errs;
|
|
|
|
} else {
|
|
|
|
*nmagicp = mentrycount;
|
|
|
|
return 0;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extend the sign bit if the comparison is to be signed
|
|
|
|
*/
|
2007-05-24 16:12:23 +00:00
|
|
|
protected uint64_t
|
|
|
|
file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2007-05-24 21:59:38 +00:00
|
|
|
if (!(m->flag & UNSIGNED)) {
|
1994-09-03 19:16:28 +00:00
|
|
|
switch(m->type) {
|
|
|
|
/*
|
|
|
|
* Do not remove the casts below. They are
|
|
|
|
* vital. When later compared with the data,
|
|
|
|
* the sign extension must have happened.
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_BYTE:
|
1994-09-03 19:16:28 +00:00
|
|
|
v = (char) v;
|
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_SHORT:
|
|
|
|
case FILE_BESHORT:
|
|
|
|
case FILE_LESHORT:
|
1994-09-03 19:16:28 +00:00
|
|
|
v = (short) v;
|
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_DATE:
|
|
|
|
case FILE_BEDATE:
|
|
|
|
case FILE_LEDATE:
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_MEDATE:
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_LDATE:
|
|
|
|
case FILE_BELDATE:
|
|
|
|
case FILE_LELDATE:
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_MELDATE:
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_LONG:
|
|
|
|
case FILE_BELONG:
|
|
|
|
case FILE_LELONG:
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_MELONG:
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_FLOAT:
|
|
|
|
case FILE_BEFLOAT:
|
|
|
|
case FILE_LEFLOAT:
|
2002-09-16 05:54:26 +00:00
|
|
|
v = (int32_t) v;
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
2007-05-24 16:12:23 +00:00
|
|
|
case FILE_QUAD:
|
|
|
|
case FILE_BEQUAD:
|
|
|
|
case FILE_LEQUAD:
|
|
|
|
case FILE_QDATE:
|
|
|
|
case FILE_QLDATE:
|
|
|
|
case FILE_BEQDATE:
|
|
|
|
case FILE_BEQLDATE:
|
|
|
|
case FILE_LEQDATE:
|
|
|
|
case FILE_LEQLDATE:
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_DOUBLE:
|
|
|
|
case FILE_BEDOUBLE:
|
|
|
|
case FILE_LEDOUBLE:
|
2007-05-24 16:12:23 +00:00
|
|
|
v = (int64_t) v;
|
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_STRING:
|
|
|
|
case FILE_PSTRING:
|
2004-12-28 04:31:47 +00:00
|
|
|
case FILE_BESTRING16:
|
|
|
|
case FILE_LESTRING16:
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_REGEX:
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_SEARCH:
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_DEFAULT:
|
2009-05-02 06:25:51 +00:00
|
|
|
case FILE_INDIRECT:
|
2002-09-16 05:54:26 +00:00
|
|
|
break;
|
1994-09-03 19:16:28 +00:00
|
|
|
default:
|
2004-08-09 08:45:41 +00:00
|
|
|
if (ms->flags & MAGIC_CHECK)
|
2004-12-28 04:31:47 +00:00
|
|
|
file_magwarn(ms, "cannot happen: m->type=%d\n",
|
2004-08-09 08:45:41 +00:00
|
|
|
m->type);
|
|
|
|
return ~0U;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
private int
|
2009-01-02 02:57:16 +00:00
|
|
|
string_modifier_check(struct magic_set *ms, struct magic *m)
|
2007-05-24 21:59:38 +00:00
|
|
|
{
|
|
|
|
if ((ms->flags & MAGIC_CHECK) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (m->type) {
|
|
|
|
case FILE_BESTRING16:
|
|
|
|
case FILE_LESTRING16:
|
|
|
|
if (m->str_flags != 0) {
|
2009-01-02 02:57:16 +00:00
|
|
|
file_magwarn(ms,
|
|
|
|
"no modifiers allowed for 16-bit strings\n");
|
2007-05-24 21:59:38 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FILE_STRING:
|
|
|
|
case FILE_PSTRING:
|
|
|
|
if ((m->str_flags & REGEX_OFFSET_START) != 0) {
|
2009-01-02 02:57:16 +00:00
|
|
|
file_magwarn(ms,
|
|
|
|
"'/%c' only allowed on regex and search\n",
|
2007-05-24 21:59:38 +00:00
|
|
|
CHAR_REGEX_OFFSET_START);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FILE_SEARCH:
|
2009-01-02 02:57:16 +00:00
|
|
|
if (m->str_range == 0) {
|
|
|
|
file_magwarn(ms,
|
|
|
|
"missing range; defaulting to %d\n",
|
|
|
|
STRING_DEFAULT_RANGE);
|
|
|
|
m->str_range = STRING_DEFAULT_RANGE;
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
break;
|
|
|
|
case FILE_REGEX:
|
|
|
|
if ((m->str_flags & STRING_COMPACT_BLANK) != 0) {
|
|
|
|
file_magwarn(ms, "'/%c' not allowed on regex\n",
|
|
|
|
CHAR_COMPACT_BLANK);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((m->str_flags & STRING_COMPACT_OPTIONAL_BLANK) != 0) {
|
|
|
|
file_magwarn(ms, "'/%c' not allowed on regex\n",
|
|
|
|
CHAR_COMPACT_OPTIONAL_BLANK);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
file_magwarn(ms, "coding error: m->type=%d\n",
|
|
|
|
m->type);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int
|
|
|
|
get_op(char c)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '&':
|
|
|
|
return FILE_OPAND;
|
|
|
|
case '|':
|
|
|
|
return FILE_OPOR;
|
|
|
|
case '^':
|
|
|
|
return FILE_OPXOR;
|
|
|
|
case '+':
|
|
|
|
return FILE_OPADD;
|
|
|
|
case '-':
|
|
|
|
return FILE_OPMINUS;
|
|
|
|
case '*':
|
|
|
|
return FILE_OPMULTIPLY;
|
|
|
|
case '/':
|
|
|
|
return FILE_OPDIVIDE;
|
|
|
|
case '%':
|
|
|
|
return FILE_OPMODULO;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_CONDITIONALS
|
|
|
|
private int
|
|
|
|
get_cond(const char *l, const char **t)
|
|
|
|
{
|
2009-01-02 02:57:16 +00:00
|
|
|
static const struct cond_tbl_s {
|
|
|
|
char name[8];
|
|
|
|
size_t len;
|
|
|
|
int cond;
|
2007-05-24 21:59:38 +00:00
|
|
|
} cond_tbl[] = {
|
|
|
|
{ "if", 2, COND_IF },
|
|
|
|
{ "elif", 4, COND_ELIF },
|
|
|
|
{ "else", 4, COND_ELSE },
|
2009-01-02 02:57:16 +00:00
|
|
|
{ "", 0, COND_NONE },
|
2007-05-24 21:59:38 +00:00
|
|
|
};
|
2009-01-02 02:57:16 +00:00
|
|
|
const struct cond_tbl_s *p;
|
2007-05-24 21:59:38 +00:00
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
for (p = cond_tbl; p->len; p++) {
|
2007-05-24 21:59:38 +00:00
|
|
|
if (strncmp(l, p->name, p->len) == 0 &&
|
|
|
|
isspace((unsigned char)l[p->len])) {
|
|
|
|
if (t)
|
|
|
|
*t = l + p->len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p->cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int
|
|
|
|
check_cond(struct magic_set *ms, int cond, uint32_t cont_level)
|
|
|
|
{
|
|
|
|
int last_cond;
|
|
|
|
last_cond = ms->c.li[cont_level].last_cond;
|
|
|
|
|
|
|
|
switch (cond) {
|
|
|
|
case COND_IF:
|
|
|
|
if (last_cond != COND_NONE && last_cond != COND_ELIF) {
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms, "syntax error: `if'");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
last_cond = COND_IF;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COND_ELIF:
|
|
|
|
if (last_cond != COND_IF && last_cond != COND_ELIF) {
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms, "syntax error: `elif'");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
last_cond = COND_ELIF;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COND_ELSE:
|
|
|
|
if (last_cond != COND_IF && last_cond != COND_ELIF) {
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms, "syntax error: `else'");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
last_cond = COND_NONE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COND_NONE:
|
|
|
|
last_cond = COND_NONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ms->c.li[cont_level].last_cond = last_cond;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_CONDITIONALS */
|
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
/*
|
|
|
|
* parse one line from magic file, put into magic[index++] if valid
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
2006-06-19 07:52:15 +00:00
|
|
|
parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
|
2007-05-24 16:12:23 +00:00
|
|
|
const char *line, size_t lineno, int action)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2007-05-24 21:59:38 +00:00
|
|
|
#ifdef ENABLE_CONDITIONALS
|
|
|
|
static uint32_t last_cont_level = 0;
|
|
|
|
#endif
|
2007-05-24 16:12:23 +00:00
|
|
|
size_t i;
|
2006-06-19 07:52:15 +00:00
|
|
|
struct magic_entry *me;
|
1994-09-03 19:16:28 +00:00
|
|
|
struct magic *m;
|
2006-06-19 07:52:15 +00:00
|
|
|
const char *l = line;
|
2001-10-08 22:50:54 +00:00
|
|
|
char *t;
|
2007-05-24 21:59:38 +00:00
|
|
|
int op;
|
2006-06-21 09:28:00 +00:00
|
|
|
uint32_t cont_level;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
cont_level = 0;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
while (*l == '>') {
|
|
|
|
++l; /* step over */
|
2006-06-19 07:52:15 +00:00
|
|
|
cont_level++;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
#ifdef ENABLE_CONDITIONALS
|
|
|
|
if (cont_level == 0 || cont_level > last_cont_level)
|
|
|
|
if (file_check_mem(ms, cont_level) == -1)
|
|
|
|
return -1;
|
|
|
|
last_cont_level = cont_level;
|
|
|
|
#endif
|
2006-06-19 07:52:15 +00:00
|
|
|
|
|
|
|
#define ALLOC_CHUNK (size_t)10
|
|
|
|
#define ALLOC_INCR (size_t)200
|
|
|
|
|
|
|
|
if (cont_level != 0) {
|
|
|
|
if (*nmentryp == 0) {
|
|
|
|
file_error(ms, 0, "No current entry for continuation");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
me = &(*mentryp)[*nmentryp - 1];
|
|
|
|
if (me->cont_count == me->max_count) {
|
|
|
|
struct magic *nm;
|
|
|
|
size_t cnt = me->max_count + ALLOC_CHUNK;
|
2009-01-02 02:57:16 +00:00
|
|
|
if ((nm = CAST(struct magic *, realloc(me->mp,
|
|
|
|
sizeof(*nm) * cnt))) == NULL) {
|
2007-05-24 16:12:23 +00:00
|
|
|
file_oomem(ms, sizeof(*nm) * cnt);
|
2006-06-19 07:52:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
me->mp = m = nm;
|
|
|
|
me->max_count = cnt;
|
|
|
|
}
|
|
|
|
m = &me->mp[me->cont_count++];
|
2007-05-24 21:59:38 +00:00
|
|
|
(void)memset(m, 0, sizeof(*m));
|
2006-06-19 07:52:15 +00:00
|
|
|
m->cont_level = cont_level;
|
|
|
|
} else {
|
|
|
|
if (*nmentryp == maxmagic) {
|
|
|
|
struct magic_entry *mp;
|
|
|
|
|
|
|
|
maxmagic += ALLOC_INCR;
|
2009-01-02 02:57:16 +00:00
|
|
|
if ((mp = CAST(struct magic_entry *,
|
|
|
|
realloc(*mentryp, sizeof(*mp) * maxmagic))) ==
|
2006-06-19 07:52:15 +00:00
|
|
|
NULL) {
|
2007-05-24 16:12:23 +00:00
|
|
|
file_oomem(ms, sizeof(*mp) * maxmagic);
|
2006-06-19 07:52:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
(void)memset(&mp[*nmentryp], 0, sizeof(*mp) *
|
|
|
|
ALLOC_INCR);
|
|
|
|
*mentryp = mp;
|
|
|
|
}
|
|
|
|
me = &(*mentryp)[*nmentryp];
|
|
|
|
if (me->mp == NULL) {
|
2009-01-02 02:57:16 +00:00
|
|
|
size_t len = sizeof(*m) * ALLOC_CHUNK;
|
|
|
|
if ((m = CAST(struct magic *, malloc(len))) == NULL) {
|
|
|
|
file_oomem(ms, len);
|
2006-06-19 07:52:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
me->mp = m;
|
|
|
|
me->max_count = ALLOC_CHUNK;
|
|
|
|
} else
|
|
|
|
m = me->mp;
|
2007-05-24 21:59:38 +00:00
|
|
|
(void)memset(m, 0, sizeof(*m));
|
2009-01-02 02:57:16 +00:00
|
|
|
m->factor_op = FILE_FACTOR_OP_NONE;
|
2006-06-19 07:52:15 +00:00
|
|
|
m->cont_level = 0;
|
|
|
|
me->cont_count = 1;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
m->lineno = lineno;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
if (*l == '&') { /* m->cont_level == 0 checked below. */
|
2006-06-19 07:52:15 +00:00
|
|
|
++l; /* step over */
|
|
|
|
m->flag |= OFFADD;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
if (*l == '(') {
|
1994-09-03 19:16:28 +00:00
|
|
|
++l; /* step over */
|
|
|
|
m->flag |= INDIR;
|
2006-06-19 07:52:15 +00:00
|
|
|
if (m->flag & OFFADD)
|
|
|
|
m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
|
2007-05-24 21:59:38 +00:00
|
|
|
|
|
|
|
if (*l == '&') { /* m->cont_level == 0 checked below */
|
|
|
|
++l; /* step over */
|
|
|
|
m->flag |= OFFADD;
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
/* Indirect offsets are not valid at level 0. */
|
|
|
|
if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD)))
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms, "relative offset at level 0");
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
/* get offset, then skip over it */
|
2004-08-09 08:45:41 +00:00
|
|
|
m->offset = (uint32_t)strtoul(l, &t, 0);
|
1994-09-03 19:16:28 +00:00
|
|
|
if (l == t)
|
2004-08-09 08:45:41 +00:00
|
|
|
if (ms->flags & MAGIC_CHECK)
|
2004-12-28 04:31:47 +00:00
|
|
|
file_magwarn(ms, "offset `%s' invalid", l);
|
1994-09-03 19:16:28 +00:00
|
|
|
l = t;
|
|
|
|
|
|
|
|
if (m->flag & INDIR) {
|
2004-08-09 08:45:41 +00:00
|
|
|
m->in_type = FILE_LONG;
|
2001-03-25 18:37:04 +00:00
|
|
|
m->in_offset = 0;
|
1994-09-03 19:16:28 +00:00
|
|
|
/*
|
|
|
|
* read [.lbs][+-]nnnnn)
|
|
|
|
*/
|
|
|
|
if (*l == '.') {
|
1996-01-23 12:29:07 +00:00
|
|
|
l++;
|
2000-11-05 08:33:55 +00:00
|
|
|
switch (*l) {
|
1994-09-03 19:16:28 +00:00
|
|
|
case 'l':
|
2004-08-09 08:45:41 +00:00
|
|
|
m->in_type = FILE_LELONG;
|
2000-11-05 08:33:55 +00:00
|
|
|
break;
|
|
|
|
case 'L':
|
2004-08-09 08:45:41 +00:00
|
|
|
m->in_type = FILE_BELONG;
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
case 'm':
|
|
|
|
m->in_type = FILE_MELONG;
|
|
|
|
break;
|
1996-01-23 12:29:07 +00:00
|
|
|
case 'h':
|
1994-09-03 19:16:28 +00:00
|
|
|
case 's':
|
2004-08-09 08:45:41 +00:00
|
|
|
m->in_type = FILE_LESHORT;
|
2000-11-05 08:33:55 +00:00
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
case 'S':
|
2004-08-09 08:45:41 +00:00
|
|
|
m->in_type = FILE_BESHORT;
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
1996-01-23 12:29:07 +00:00
|
|
|
case 'c':
|
1994-09-03 19:16:28 +00:00
|
|
|
case 'b':
|
2000-11-05 08:33:55 +00:00
|
|
|
case 'C':
|
|
|
|
case 'B':
|
2004-08-09 08:45:41 +00:00
|
|
|
m->in_type = FILE_BYTE;
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
2008-01-13 20:35:16 +00:00
|
|
|
case 'e':
|
|
|
|
case 'f':
|
|
|
|
case 'g':
|
|
|
|
m->in_type = FILE_LEDOUBLE;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
case 'F':
|
|
|
|
case 'G':
|
|
|
|
m->in_type = FILE_BEDOUBLE;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
case 'i':
|
|
|
|
m->in_type = FILE_LEID3;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
m->in_type = FILE_BEID3;
|
|
|
|
break;
|
1994-09-03 19:16:28 +00:00
|
|
|
default:
|
2004-08-09 08:45:41 +00:00
|
|
|
if (ms->flags & MAGIC_CHECK)
|
2004-12-28 04:31:47 +00:00
|
|
|
file_magwarn(ms,
|
|
|
|
"indirect offset type `%c' invalid",
|
2004-08-09 08:45:41 +00:00
|
|
|
*l);
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
l++;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
|
|
|
|
m->in_op = 0;
|
2001-07-30 03:09:46 +00:00
|
|
|
if (*l == '~') {
|
2006-06-19 07:52:15 +00:00
|
|
|
m->in_op |= FILE_OPINVERSE;
|
2001-07-30 03:09:46 +00:00
|
|
|
l++;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
if ((op = get_op(*l)) != -1) {
|
|
|
|
m->in_op |= op;
|
2001-07-30 03:09:46 +00:00
|
|
|
l++;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
if (*l == '(') {
|
|
|
|
m->in_op |= FILE_OPINDIRECT;
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
if (isdigit((unsigned char)*l) || *l == '-') {
|
|
|
|
m->in_offset = (int32_t)strtol(l, &t, 0);
|
2007-05-24 21:59:38 +00:00
|
|
|
if (l == t)
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms,
|
|
|
|
"in_offset `%s' invalid", l);
|
2006-06-19 07:52:15 +00:00
|
|
|
l = t;
|
|
|
|
}
|
|
|
|
if (*l++ != ')' ||
|
|
|
|
((m->in_op & FILE_OPINDIRECT) && *l++ != ')'))
|
2004-08-09 08:45:41 +00:00
|
|
|
if (ms->flags & MAGIC_CHECK)
|
2004-12-28 04:31:47 +00:00
|
|
|
file_magwarn(ms,
|
|
|
|
"missing ')' in indirect offset");
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
EATAB;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
#ifdef ENABLE_CONDITIONALS
|
|
|
|
m->cond = get_cond(l, &l);
|
|
|
|
if (check_cond(ms, m->cond, cont_level) == -1)
|
|
|
|
return -1;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
EATAB;
|
2007-05-24 21:59:38 +00:00
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
if (*l == 'u') {
|
|
|
|
++l;
|
|
|
|
m->flag |= UNSIGNED;
|
|
|
|
}
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
m->type = get_type(l, &l);
|
|
|
|
if (m->type == FILE_INVALID) {
|
2004-08-09 08:45:41 +00:00
|
|
|
if (ms->flags & MAGIC_CHECK)
|
2004-12-28 04:31:47 +00:00
|
|
|
file_magwarn(ms, "type `%s' invalid", l);
|
1994-09-03 19:16:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
/* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
|
2001-07-30 03:09:46 +00:00
|
|
|
/* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
|
2007-05-24 21:59:38 +00:00
|
|
|
|
|
|
|
m->mask_op = 0;
|
2001-07-30 03:09:46 +00:00
|
|
|
if (*l == '~') {
|
2004-12-28 04:31:47 +00:00
|
|
|
if (!IS_STRING(m->type))
|
2006-06-19 07:52:15 +00:00
|
|
|
m->mask_op |= FILE_OPINVERSE;
|
2007-05-24 21:59:38 +00:00
|
|
|
else if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms, "'~' invalid for string types");
|
2001-07-30 03:09:46 +00:00
|
|
|
++l;
|
|
|
|
}
|
2009-01-02 02:57:16 +00:00
|
|
|
m->str_range = 0;
|
2007-05-24 21:59:38 +00:00
|
|
|
m->str_flags = 0;
|
|
|
|
m->num_mask = 0;
|
|
|
|
if ((op = get_op(*l)) != -1) {
|
|
|
|
if (!IS_STRING(m->type)) {
|
|
|
|
uint64_t val;
|
2001-07-30 03:09:46 +00:00
|
|
|
++l;
|
2004-08-09 08:45:41 +00:00
|
|
|
m->mask_op |= op;
|
2007-05-24 16:12:23 +00:00
|
|
|
val = (uint64_t)strtoull(l, &t, 0);
|
2006-06-19 07:52:15 +00:00
|
|
|
l = t;
|
2007-05-24 21:59:38 +00:00
|
|
|
m->num_mask = file_signextend(ms, m, val);
|
2001-07-30 03:09:46 +00:00
|
|
|
eatsize(&l);
|
2007-05-24 21:59:38 +00:00
|
|
|
}
|
|
|
|
else if (op == FILE_OPDIVIDE) {
|
2009-01-02 02:57:16 +00:00
|
|
|
int have_range = 0;
|
2004-08-09 08:45:41 +00:00
|
|
|
while (!isspace((unsigned char)*++l)) {
|
2000-11-05 08:33:55 +00:00
|
|
|
switch (*l) {
|
2007-05-24 21:59:38 +00:00
|
|
|
case '0': case '1': case '2':
|
|
|
|
case '3': case '4': case '5':
|
|
|
|
case '6': case '7': case '8':
|
2009-01-02 02:57:16 +00:00
|
|
|
case '9':
|
|
|
|
if (have_range &&
|
|
|
|
(ms->flags & MAGIC_CHECK))
|
|
|
|
file_magwarn(ms,
|
|
|
|
"multiple ranges");
|
|
|
|
have_range = 1;
|
|
|
|
m->str_range = strtoul(l, &t, 0);
|
|
|
|
if (m->str_range == 0)
|
2007-05-24 21:59:38 +00:00
|
|
|
file_magwarn(ms,
|
2009-01-02 02:57:16 +00:00
|
|
|
"zero range");
|
2007-05-24 21:59:38 +00:00
|
|
|
l = t - 1;
|
2000-11-05 08:33:55 +00:00
|
|
|
break;
|
|
|
|
case CHAR_COMPACT_BLANK:
|
2007-05-24 21:59:38 +00:00
|
|
|
m->str_flags |= STRING_COMPACT_BLANK;
|
2000-11-05 08:33:55 +00:00
|
|
|
break;
|
|
|
|
case CHAR_COMPACT_OPTIONAL_BLANK:
|
2007-05-24 21:59:38 +00:00
|
|
|
m->str_flags |=
|
2000-11-05 08:33:55 +00:00
|
|
|
STRING_COMPACT_OPTIONAL_BLANK;
|
|
|
|
break;
|
2007-05-24 21:59:38 +00:00
|
|
|
case CHAR_IGNORE_LOWERCASE:
|
|
|
|
m->str_flags |= STRING_IGNORE_LOWERCASE;
|
|
|
|
break;
|
|
|
|
case CHAR_IGNORE_UPPERCASE:
|
|
|
|
m->str_flags |= STRING_IGNORE_UPPERCASE;
|
|
|
|
break;
|
|
|
|
case CHAR_REGEX_OFFSET_START:
|
|
|
|
m->str_flags |= REGEX_OFFSET_START;
|
|
|
|
break;
|
2000-11-05 08:33:55 +00:00
|
|
|
default:
|
2004-08-09 08:45:41 +00:00
|
|
|
if (ms->flags & MAGIC_CHECK)
|
2004-12-28 04:31:47 +00:00
|
|
|
file_magwarn(ms,
|
|
|
|
"string extension `%c' invalid",
|
2004-08-09 08:45:41 +00:00
|
|
|
*l);
|
2000-11-05 08:33:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
/* allow multiple '/' for readability */
|
2009-01-02 02:57:16 +00:00
|
|
|
if (l[1] == '/' &&
|
|
|
|
!isspace((unsigned char)l[2]))
|
2007-05-24 21:59:38 +00:00
|
|
|
l++;
|
2000-11-05 08:33:55 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
if (string_modifier_check(ms, m) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms, "invalid string op: %c", *t);
|
|
|
|
return -1;
|
2000-11-05 08:33:55 +00:00
|
|
|
}
|
2001-07-30 03:09:46 +00:00
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
/*
|
|
|
|
* We used to set mask to all 1's here, instead let's just not do
|
|
|
|
* anything if mask = 0 (unless you have a better idea)
|
|
|
|
*/
|
1994-09-03 19:16:28 +00:00
|
|
|
EATAB;
|
|
|
|
|
|
|
|
switch (*l) {
|
|
|
|
case '>':
|
|
|
|
case '<':
|
2009-01-02 02:57:16 +00:00
|
|
|
m->reln = *l;
|
|
|
|
++l;
|
|
|
|
if (*l == '=') {
|
|
|
|
if (ms->flags & MAGIC_CHECK) {
|
|
|
|
file_magwarn(ms, "%c= not supported",
|
|
|
|
m->reln);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
++l;
|
|
|
|
}
|
|
|
|
break;
|
1994-09-03 19:16:28 +00:00
|
|
|
/* Old-style anding: "0 byte &0x80 dynamically linked" */
|
|
|
|
case '&':
|
|
|
|
case '^':
|
|
|
|
case '=':
|
|
|
|
m->reln = *l;
|
|
|
|
++l;
|
2000-11-05 08:33:55 +00:00
|
|
|
if (*l == '=') {
|
|
|
|
/* HP compat: ignore &= etc. */
|
|
|
|
++l;
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
|
|
|
case '!':
|
2006-06-19 07:52:15 +00:00
|
|
|
m->reln = *l;
|
|
|
|
++l;
|
|
|
|
break;
|
1994-09-03 19:16:28 +00:00
|
|
|
default:
|
2007-05-24 21:59:38 +00:00
|
|
|
m->reln = '='; /* the default relation */
|
2006-06-19 07:52:15 +00:00
|
|
|
if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
|
|
|
|
isspace((unsigned char)l[1])) || !l[1])) {
|
1994-09-03 19:16:28 +00:00
|
|
|
m->reln = *l;
|
|
|
|
++l;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
/*
|
|
|
|
* Grab the value part, except for an 'x' reln.
|
|
|
|
*/
|
|
|
|
if (m->reln != 'x' && getvalue(ms, m, &l, action))
|
1994-09-03 19:16:28 +00:00
|
|
|
return -1;
|
2007-05-24 21:59:38 +00:00
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
/*
|
|
|
|
* TODO finish this macro and start using it!
|
|
|
|
* #define offsetcheck {if (offset > HOWMANY-1)
|
|
|
|
* magwarn("offset too big"); }
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2007-05-24 21:59:38 +00:00
|
|
|
* Now get last part - the description
|
1994-09-03 19:16:28 +00:00
|
|
|
*/
|
|
|
|
EATAB;
|
|
|
|
if (l[0] == '\b') {
|
|
|
|
++l;
|
2009-01-02 02:57:16 +00:00
|
|
|
m->flag |= NOSPACE;
|
1994-09-03 19:16:28 +00:00
|
|
|
} else if ((l[0] == '\\') && (l[1] == 'b')) {
|
|
|
|
++l;
|
|
|
|
++l;
|
2009-01-02 02:57:16 +00:00
|
|
|
m->flag |= NOSPACE;
|
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); )
|
|
|
|
continue;
|
|
|
|
if (i == sizeof(m->desc)) {
|
|
|
|
m->desc[sizeof(m->desc) - 1] = '\0';
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms, "description `%s' truncated", m->desc);
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
/*
|
|
|
|
* We only do this check while compiling, or if any of the magic
|
|
|
|
* files were not compiled.
|
|
|
|
*/
|
|
|
|
if (ms->flags & MAGIC_CHECK) {
|
|
|
|
if (check_format(ms, m) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-09-16 05:54:26 +00:00
|
|
|
#ifndef COMPILE_ONLY
|
2004-08-09 08:45:41 +00:00
|
|
|
if (action == FILE_CHECK) {
|
|
|
|
file_mdump(m);
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2002-09-16 05:54:26 +00:00
|
|
|
#endif
|
2009-01-02 02:57:16 +00:00
|
|
|
m->mimetype[0] = '\0'; /* initialise MIME type to none */
|
2006-06-19 07:52:15 +00:00
|
|
|
if (m->cont_level == 0)
|
|
|
|
++(*nmentryp); /* make room for next */
|
1994-09-03 19:16:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
/*
|
|
|
|
* parse a STRENGTH annotation line from magic file, put into magic[index - 1]
|
|
|
|
* if valid
|
|
|
|
*/
|
|
|
|
private int
|
|
|
|
parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line)
|
|
|
|
{
|
|
|
|
const char *l = line;
|
|
|
|
char *el;
|
|
|
|
unsigned long factor;
|
|
|
|
struct magic *m = &me->mp[0];
|
|
|
|
|
|
|
|
if (m->factor_op != FILE_FACTOR_OP_NONE) {
|
|
|
|
file_magwarn(ms,
|
|
|
|
"Current entry already has a strength type: %c %d",
|
|
|
|
m->factor_op, m->factor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
EATAB;
|
|
|
|
switch (*l) {
|
|
|
|
case FILE_FACTOR_OP_NONE:
|
|
|
|
case FILE_FACTOR_OP_PLUS:
|
|
|
|
case FILE_FACTOR_OP_MINUS:
|
|
|
|
case FILE_FACTOR_OP_TIMES:
|
|
|
|
case FILE_FACTOR_OP_DIV:
|
|
|
|
m->factor_op = *l++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
file_magwarn(ms, "Unknown factor op `%c'", *l);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
EATAB;
|
|
|
|
factor = strtoul(l, &el, 0);
|
|
|
|
if (factor > 255) {
|
|
|
|
file_magwarn(ms, "Too large factor `%lu'", factor);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (*el && !isspace((unsigned char)*el)) {
|
|
|
|
file_magwarn(ms, "Bad factor `%s'", l);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
m->factor = (uint8_t)factor;
|
|
|
|
if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) {
|
|
|
|
file_magwarn(ms, "Cannot have factor op `%c' and factor %u",
|
|
|
|
m->factor_op, m->factor);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
out:
|
|
|
|
m->factor_op = FILE_FACTOR_OP_NONE;
|
|
|
|
m->factor = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
/*
|
|
|
|
* Parse an Apple CREATOR/TYPE annotation from magic file and put it into magic[index - 1]
|
|
|
|
*/
|
|
|
|
private int
|
|
|
|
parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const char *l = line;
|
|
|
|
struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
|
|
|
|
|
|
|
|
if (m->apple[0] != '\0') {
|
|
|
|
file_magwarn(ms, "Current entry already has a APPLE type `%.8s',"
|
|
|
|
" new type `%s'", m->mimetype, l);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EATAB;
|
|
|
|
for (i = 0; *l && ((isascii((unsigned char)*l) && isalnum((unsigned char)*l))
|
|
|
|
|| strchr("-+/.", *l)) && i < sizeof(m->apple); m->apple[i++] = *l++)
|
|
|
|
continue;
|
|
|
|
if (i == sizeof(m->apple) && *l) {
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms, "APPLE type `%s' truncated %zu",
|
|
|
|
line, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
/*
|
|
|
|
* parse a MIME annotation line from magic file, put into magic[index - 1]
|
|
|
|
* if valid
|
|
|
|
*/
|
|
|
|
private int
|
|
|
|
parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const char *l = line;
|
|
|
|
struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
|
|
|
|
|
|
|
|
if (m->mimetype[0] != '\0') {
|
|
|
|
file_magwarn(ms, "Current entry already has a MIME type `%s',"
|
|
|
|
" new type `%s'", m->mimetype, l);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EATAB;
|
2009-05-02 06:25:51 +00:00
|
|
|
for (i = 0; *l && ((isascii((unsigned char)*l) && isalnum((unsigned char)*l))
|
|
|
|
|| strchr("-+/.", *l)) && i < sizeof(m->mimetype); m->mimetype[i++] = *l++)
|
2009-01-02 02:57:16 +00:00
|
|
|
continue;
|
|
|
|
if (i == sizeof(m->mimetype)) {
|
|
|
|
m->desc[sizeof(m->mimetype) - 1] = '\0';
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
|
|
|
file_magwarn(ms, "MIME type `%s' truncated %zu",
|
|
|
|
m->mimetype, i);
|
|
|
|
} else
|
|
|
|
m->mimetype[i] = '\0';
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
private int
|
|
|
|
check_format_type(const char *ptr, int type)
|
|
|
|
{
|
|
|
|
int quad = 0;
|
|
|
|
if (*ptr == '\0') {
|
|
|
|
/* Missing format string; bad */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case FILE_FMT_QUAD:
|
|
|
|
quad = 1;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case FILE_FMT_NUM:
|
|
|
|
if (*ptr == '-')
|
|
|
|
ptr++;
|
|
|
|
if (*ptr == '.')
|
|
|
|
ptr++;
|
|
|
|
while (isdigit((unsigned char)*ptr)) ptr++;
|
|
|
|
if (*ptr == '.')
|
|
|
|
ptr++;
|
|
|
|
while (isdigit((unsigned char)*ptr)) ptr++;
|
|
|
|
if (quad) {
|
|
|
|
if (*ptr++ != 'l')
|
|
|
|
return -1;
|
|
|
|
if (*ptr++ != 'l')
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*ptr++) {
|
|
|
|
case 'l':
|
|
|
|
switch (*ptr++) {
|
|
|
|
case 'i':
|
|
|
|
case 'd':
|
|
|
|
case 'u':
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
switch (*ptr++) {
|
|
|
|
case 'h':
|
|
|
|
switch (*ptr++) {
|
|
|
|
case 'i':
|
|
|
|
case 'd':
|
|
|
|
case 'u':
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
case 'd':
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
case 'c':
|
|
|
|
case 'd':
|
|
|
|
case 'u':
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_FMT_FLOAT:
|
|
|
|
case FILE_FMT_DOUBLE:
|
|
|
|
if (*ptr == '-')
|
|
|
|
ptr++;
|
|
|
|
if (*ptr == '.')
|
|
|
|
ptr++;
|
|
|
|
while (isdigit((unsigned char)*ptr)) ptr++;
|
|
|
|
if (*ptr == '.')
|
|
|
|
ptr++;
|
|
|
|
while (isdigit((unsigned char)*ptr)) ptr++;
|
|
|
|
|
|
|
|
switch (*ptr++) {
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
case FILE_FMT_STR:
|
|
|
|
if (*ptr == '-')
|
|
|
|
ptr++;
|
|
|
|
while (isdigit((unsigned char )*ptr))
|
|
|
|
ptr++;
|
|
|
|
if (*ptr == '.') {
|
|
|
|
ptr++;
|
|
|
|
while (isdigit((unsigned char )*ptr))
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*ptr++) {
|
|
|
|
case 's':
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* internal error */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
/*
|
|
|
|
* Check that the optional printf format in description matches
|
|
|
|
* the type of the magic.
|
|
|
|
*/
|
|
|
|
private int
|
2004-12-28 04:31:47 +00:00
|
|
|
check_format(struct magic_set *ms, struct magic *m)
|
2004-08-09 08:45:41 +00:00
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
for (ptr = m->desc; *ptr; ptr++)
|
|
|
|
if (*ptr == '%')
|
|
|
|
break;
|
|
|
|
if (*ptr == '\0') {
|
|
|
|
/* No format string; ok */
|
|
|
|
return 1;
|
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
|
|
|
|
assert(file_nformats == file_nnames);
|
|
|
|
|
|
|
|
if (m->type >= file_nformats) {
|
2009-01-02 02:57:16 +00:00
|
|
|
file_magwarn(ms, "Internal error inconsistency between "
|
2007-05-24 16:12:23 +00:00
|
|
|
"m->type and format strings");
|
|
|
|
return -1;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
if (file_formats[m->type] == FILE_FMT_NONE) {
|
2009-01-02 02:57:16 +00:00
|
|
|
file_magwarn(ms, "No format string for `%s' with description "
|
2007-05-24 16:12:23 +00:00
|
|
|
"`%s'", m->desc, file_names[m->type]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
if (check_format_type(ptr, file_formats[m->type]) == -1) {
|
|
|
|
/*
|
|
|
|
* TODO: this error message is unhelpful if the format
|
|
|
|
* string is not one character long
|
|
|
|
*/
|
2009-01-02 02:57:16 +00:00
|
|
|
file_magwarn(ms, "Printf format `%c' is not valid for type "
|
2009-05-18 22:27:42 +00:00
|
|
|
"`%s' in description `%s'", *ptr ? *ptr : '?',
|
2007-05-24 16:12:23 +00:00
|
|
|
file_names[m->type], m->desc);
|
|
|
|
return -1;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
for (; *ptr; ptr++) {
|
2007-05-24 16:12:23 +00:00
|
|
|
if (*ptr == '%') {
|
2009-01-02 02:57:16 +00:00
|
|
|
file_magwarn(ms,
|
2007-05-24 16:12:23 +00:00
|
|
|
"Too many format strings (should have at most one) "
|
|
|
|
"for `%s' with description `%s'",
|
|
|
|
file_names[m->type], m->desc);
|
|
|
|
return -1;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
return 0;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
/*
|
|
|
|
* Read a numeric value from a pointer, into the value union of a magic
|
|
|
|
* pointer, according to the magic type. Update the string pointer to point
|
|
|
|
* just after the number read. Return 0 for success, non-zero for failure.
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
2007-05-24 21:59:38 +00:00
|
|
|
getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2004-08-09 08:45:41 +00:00
|
|
|
switch (m->type) {
|
2004-12-28 04:31:47 +00:00
|
|
|
case FILE_BESTRING16:
|
|
|
|
case FILE_LESTRING16:
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_STRING:
|
|
|
|
case FILE_PSTRING:
|
|
|
|
case FILE_REGEX:
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_SEARCH:
|
2009-05-18 22:27:42 +00:00
|
|
|
*p = getstr(ms, m, *p, action == FILE_COMPILE);
|
2004-08-09 08:45:41 +00:00
|
|
|
if (*p == NULL) {
|
|
|
|
if (ms->flags & MAGIC_CHECK)
|
2004-12-28 04:31:47 +00:00
|
|
|
file_magwarn(ms, "cannot get string from `%s'",
|
2004-08-09 08:45:41 +00:00
|
|
|
m->value.s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_FLOAT:
|
|
|
|
case FILE_BEFLOAT:
|
|
|
|
case FILE_LEFLOAT:
|
|
|
|
if (m->reln != 'x') {
|
|
|
|
char *ep;
|
|
|
|
#ifdef HAVE_STRTOF
|
|
|
|
m->value.f = strtof(*p, &ep);
|
|
|
|
#else
|
|
|
|
m->value.f = (float)strtod(*p, &ep);
|
|
|
|
#endif
|
|
|
|
*p = ep;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case FILE_DOUBLE:
|
|
|
|
case FILE_BEDOUBLE:
|
|
|
|
case FILE_LEDOUBLE:
|
|
|
|
if (m->reln != 'x') {
|
|
|
|
char *ep;
|
|
|
|
m->value.d = strtod(*p, &ep);
|
|
|
|
*p = ep;
|
|
|
|
}
|
|
|
|
return 0;
|
2004-08-09 08:45:41 +00:00
|
|
|
default:
|
1996-01-23 12:29:07 +00:00
|
|
|
if (m->reln != 'x') {
|
2006-06-19 07:52:15 +00:00
|
|
|
char *ep;
|
2007-05-24 16:12:23 +00:00
|
|
|
m->value.q = file_signextend(ms, m,
|
|
|
|
(uint64_t)strtoull(*p, &ep, 0));
|
2006-06-19 07:52:15 +00:00
|
|
|
*p = ep;
|
1996-01-23 12:29:07 +00:00
|
|
|
eatsize(p);
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a string containing C character escapes. Stop at an unescaped
|
|
|
|
* space or tab.
|
2009-05-18 22:27:42 +00:00
|
|
|
* Copy the converted version to "m->value.s", and the length in m->vallen.
|
|
|
|
* Return updated scan pointer as function result. Warn if set.
|
1994-09-03 19:16:28 +00:00
|
|
|
*/
|
2006-06-19 07:52:15 +00:00
|
|
|
private const char *
|
2009-05-18 22:27:42 +00:00
|
|
|
getstr(struct magic_set *ms, struct magic *m, const char *s, int warn)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2006-06-19 07:52:15 +00:00
|
|
|
const char *origs = s;
|
2009-05-18 22:27:42 +00:00
|
|
|
char *p = m->value.s;
|
|
|
|
size_t plen = sizeof(m->value.s);
|
2006-06-19 07:52:15 +00:00
|
|
|
char *origp = p;
|
1994-09-03 19:16:28 +00:00
|
|
|
char *pmax = p + plen - 1;
|
2000-11-05 08:33:55 +00:00
|
|
|
int c;
|
|
|
|
int val;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
while ((c = *s++) != '\0') {
|
|
|
|
if (isspace((unsigned char) c))
|
|
|
|
break;
|
|
|
|
if (p >= pmax) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, 0, "string too long: `%s'", origs);
|
|
|
|
return NULL;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
if (c == '\\') {
|
1994-09-03 19:16:28 +00:00
|
|
|
switch(c = *s++) {
|
|
|
|
|
|
|
|
case '\0':
|
2009-05-18 22:27:42 +00:00
|
|
|
if (warn)
|
2007-05-24 21:59:38 +00:00
|
|
|
file_magwarn(ms, "incomplete escape");
|
1994-09-03 19:16:28 +00:00
|
|
|
goto out;
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case '\t':
|
2009-05-18 22:27:42 +00:00
|
|
|
if (warn) {
|
2007-05-24 21:59:38 +00:00
|
|
|
file_magwarn(ms,
|
|
|
|
"escaped tab found, use \\t instead");
|
2009-05-18 22:27:42 +00:00
|
|
|
warn = 0; /* already did */
|
2007-05-24 21:59:38 +00:00
|
|
|
}
|
|
|
|
/*FALLTHROUGH*/
|
1994-09-03 19:16:28 +00:00
|
|
|
default:
|
2009-05-18 22:27:42 +00:00
|
|
|
if (warn) {
|
|
|
|
if (isprint((unsigned char)c)) {
|
|
|
|
/* Allow escaping of
|
|
|
|
* ``relations'' */
|
|
|
|
if (strchr("<>&^=!", c)
|
|
|
|
== NULL) {
|
|
|
|
file_magwarn(ms, "no "
|
|
|
|
"need to escape "
|
|
|
|
"`%c'", c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file_magwarn(ms,
|
|
|
|
"unknown escape sequence: "
|
|
|
|
"\\%03o", c);
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
}
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
/* space, perhaps force people to use \040? */
|
|
|
|
case ' ':
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* Other things people escape, but shouldn't need to,
|
|
|
|
* so we disallow them
|
|
|
|
*/
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
case '?':
|
|
|
|
#endif
|
|
|
|
/* Relations */
|
|
|
|
case '>':
|
|
|
|
case '<':
|
|
|
|
case '&':
|
|
|
|
case '^':
|
|
|
|
case '=':
|
|
|
|
case '!':
|
|
|
|
/* and baskslash itself */
|
|
|
|
case '\\':
|
1994-09-03 19:16:28 +00:00
|
|
|
*p++ = (char) c;
|
|
|
|
break;
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case 'a':
|
|
|
|
*p++ = '\a';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
*p++ = '\b';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
*p++ = '\f';
|
|
|
|
break;
|
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
case 'n':
|
|
|
|
*p++ = '\n';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
*p++ = '\r';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
*p++ = '\t';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
*p++ = '\v';
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* \ and up to 3 octal digits */
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
val = c - '0';
|
|
|
|
c = *s++; /* try for 2 */
|
2007-05-24 21:59:38 +00:00
|
|
|
if (c >= '0' && c <= '7') {
|
|
|
|
val = (val << 3) | (c - '0');
|
1994-09-03 19:16:28 +00:00
|
|
|
c = *s++; /* try for 3 */
|
2007-05-24 21:59:38 +00:00
|
|
|
if (c >= '0' && c <= '7')
|
|
|
|
val = (val << 3) | (c-'0');
|
1994-09-03 19:16:28 +00:00
|
|
|
else
|
|
|
|
--s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
--s;
|
|
|
|
*p++ = (char)val;
|
|
|
|
break;
|
|
|
|
|
1997-03-18 18:00:03 +00:00
|
|
|
/* \x and up to 2 hex digits */
|
1994-09-03 19:16:28 +00:00
|
|
|
case 'x':
|
|
|
|
val = 'x'; /* Default if no digits */
|
|
|
|
c = hextoint(*s++); /* Get next char */
|
|
|
|
if (c >= 0) {
|
|
|
|
val = c;
|
|
|
|
c = hextoint(*s++);
|
1997-03-18 18:00:03 +00:00
|
|
|
if (c >= 0)
|
1994-09-03 19:16:28 +00:00
|
|
|
val = (val << 4) + c;
|
1997-03-18 18:00:03 +00:00
|
|
|
else
|
1994-09-03 19:16:28 +00:00
|
|
|
--s;
|
|
|
|
} else
|
|
|
|
--s;
|
|
|
|
*p++ = (char)val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
*p++ = (char)c;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
*p = '\0';
|
2009-05-18 22:27:42 +00:00
|
|
|
m->vallen = p - origp;
|
|
|
|
if (m->type == FILE_PSTRING)
|
|
|
|
m->vallen++;
|
1994-09-03 19:16:28 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Single hex char to int; -1 if not a hex char. */
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
2002-09-16 05:54:26 +00:00
|
|
|
hextoint(int c)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2000-11-05 08:33:55 +00:00
|
|
|
if (!isascii((unsigned char) c))
|
|
|
|
return -1;
|
|
|
|
if (isdigit((unsigned char) c))
|
|
|
|
return c - '0';
|
2007-05-24 21:59:38 +00:00
|
|
|
if ((c >= 'a') && (c <= 'f'))
|
2000-11-05 08:33:55 +00:00
|
|
|
return c + 10 - 'a';
|
2007-05-24 21:59:38 +00:00
|
|
|
if (( c>= 'A') && (c <= 'F'))
|
2000-11-05 08:33:55 +00:00
|
|
|
return c + 10 - 'A';
|
|
|
|
return -1;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a string containing C character escapes.
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
protected void
|
|
|
|
file_showstr(FILE *fp, const char *s, size_t len)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2000-11-05 08:33:55 +00:00
|
|
|
char c;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
c = *s++;
|
2004-08-09 08:45:41 +00:00
|
|
|
if (len == ~0U) {
|
1994-09-03 19:16:28 +00:00
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (len-- == 0)
|
|
|
|
break;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
if (c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
|
1994-09-03 19:16:28 +00:00
|
|
|
(void) fputc(c, fp);
|
|
|
|
else {
|
|
|
|
(void) fputc('\\', fp);
|
|
|
|
switch (c) {
|
2007-05-24 21:59:38 +00:00
|
|
|
case '\a':
|
|
|
|
(void) fputc('a', fp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\b':
|
|
|
|
(void) fputc('b', fp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\f':
|
|
|
|
(void) fputc('f', fp);
|
|
|
|
break;
|
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
case '\n':
|
|
|
|
(void) fputc('n', fp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
(void) fputc('r', fp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\t':
|
|
|
|
(void) fputc('t', fp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\v':
|
|
|
|
(void) fputc('v', fp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
(void) fprintf(fp, "%.3o", c & 0377);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-01-23 12:29:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* eatsize(): Eat the size spec from a number [eg. 10UL]
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private void
|
2006-06-19 07:52:15 +00:00
|
|
|
eatsize(const char **p)
|
1996-01-23 12:29:07 +00:00
|
|
|
{
|
2006-06-19 07:52:15 +00:00
|
|
|
const char *l = *p;
|
1996-01-23 12:29:07 +00:00
|
|
|
|
|
|
|
if (LOWCASE(*l) == 'u')
|
|
|
|
l++;
|
|
|
|
|
|
|
|
switch (LOWCASE(*l)) {
|
|
|
|
case 'l': /* long */
|
|
|
|
case 's': /* short */
|
|
|
|
case 'h': /* short */
|
|
|
|
case 'b': /* char/byte */
|
|
|
|
case 'c': /* char/byte */
|
|
|
|
l++;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = l;
|
|
|
|
}
|
2001-03-25 18:37:04 +00:00
|
|
|
|
|
|
|
/*
|
2002-09-16 05:54:26 +00:00
|
|
|
* handle a compiled file.
|
2001-03-25 18:37:04 +00:00
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
|
|
|
apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
|
|
|
|
const char *fn)
|
2001-03-25 18:37:04 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
2002-09-16 05:54:26 +00:00
|
|
|
uint32_t *ptr;
|
|
|
|
uint32_t version;
|
2001-03-25 18:37:04 +00:00
|
|
|
int needsbyteswap;
|
2009-01-02 02:57:16 +00:00
|
|
|
char *dbname = NULL;
|
2004-08-09 08:45:41 +00:00
|
|
|
void *mm = NULL;
|
2001-03-25 18:37:04 +00:00
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
dbname = mkdbname(ms, fn, 0);
|
2001-07-30 03:09:46 +00:00
|
|
|
if (dbname == NULL)
|
2009-01-02 02:57:16 +00:00
|
|
|
goto error2;
|
2001-07-30 03:09:46 +00:00
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
|
2009-01-02 02:57:16 +00:00
|
|
|
goto error2;
|
2001-03-25 18:37:04 +00:00
|
|
|
|
|
|
|
if (fstat(fd, &st) == -1) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno, "cannot stat `%s'", dbname);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto error1;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
2009-01-02 02:57:16 +00:00
|
|
|
if (st.st_size < 8) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, 0, "file `%s' is too small", dbname);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto error1;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
2001-07-30 03:09:46 +00:00
|
|
|
#ifdef QUICK
|
2002-09-16 05:54:26 +00:00
|
|
|
if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
|
2001-03-25 18:37:04 +00:00
|
|
|
MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno, "cannot map `%s'", dbname);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto error1;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
#define RET 2
|
2001-07-30 03:09:46 +00:00
|
|
|
#else
|
2009-01-02 02:57:16 +00:00
|
|
|
if ((mm = CAST(void *, malloc((size_t)st.st_size))) == NULL) {
|
2007-05-24 16:12:23 +00:00
|
|
|
file_oomem(ms, (size_t)st.st_size);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto error1;
|
2001-07-30 03:09:46 +00:00
|
|
|
}
|
2009-05-18 22:27:42 +00:00
|
|
|
if (read(fd, mm, (size_t)st.st_size) != (ssize_t)st.st_size) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_badread(ms);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto error1;
|
2001-07-30 03:09:46 +00:00
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
#define RET 1
|
2001-07-30 03:09:46 +00:00
|
|
|
#endif
|
2009-01-02 02:57:16 +00:00
|
|
|
*magicp = CAST(struct magic *, mm);
|
2001-03-25 18:37:04 +00:00
|
|
|
(void)close(fd);
|
2001-04-25 07:41:21 +00:00
|
|
|
fd = -1;
|
2004-08-09 08:45:41 +00:00
|
|
|
ptr = (uint32_t *)(void *)*magicp;
|
2001-03-25 18:37:04 +00:00
|
|
|
if (*ptr != MAGICNO) {
|
|
|
|
if (swap4(*ptr) != MAGICNO) {
|
2009-01-02 02:57:16 +00:00
|
|
|
file_error(ms, 0, "bad magic in `%s'", dbname);
|
|
|
|
goto error1;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
|
|
|
needsbyteswap = 1;
|
|
|
|
} else
|
|
|
|
needsbyteswap = 0;
|
|
|
|
if (needsbyteswap)
|
|
|
|
version = swap4(ptr[1]);
|
|
|
|
else
|
|
|
|
version = ptr[1];
|
|
|
|
if (version != VERSIONNO) {
|
2009-05-18 22:27:42 +00:00
|
|
|
file_error(ms, 0, "File %d.%d supports only version %d magic "
|
2008-01-13 20:35:16 +00:00
|
|
|
"files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,
|
|
|
|
VERSIONNO, dbname, version);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto error1;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
2009-01-02 02:57:16 +00:00
|
|
|
*nmagicp = (uint32_t)(st.st_size / sizeof(struct magic));
|
|
|
|
if (*nmagicp > 0)
|
|
|
|
(*nmagicp)--;
|
2001-03-25 18:37:04 +00:00
|
|
|
(*magicp)++;
|
|
|
|
if (needsbyteswap)
|
|
|
|
byteswap(*magicp, *nmagicp);
|
2009-01-02 02:57:16 +00:00
|
|
|
free(dbname);
|
2004-08-09 08:45:41 +00:00
|
|
|
return RET;
|
2001-03-25 18:37:04 +00:00
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
error1:
|
2001-03-25 18:37:04 +00:00
|
|
|
if (fd != -1)
|
|
|
|
(void)close(fd);
|
2002-09-16 05:54:26 +00:00
|
|
|
if (mm) {
|
2001-07-30 03:09:46 +00:00
|
|
|
#ifdef QUICK
|
2004-08-09 08:45:41 +00:00
|
|
|
(void)munmap((void *)mm, (size_t)st.st_size);
|
2001-07-30 03:09:46 +00:00
|
|
|
#else
|
2002-09-16 05:54:26 +00:00
|
|
|
free(mm);
|
2001-07-30 03:09:46 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
2001-03-25 18:37:04 +00:00
|
|
|
*magicp = NULL;
|
|
|
|
*nmagicp = 0;
|
|
|
|
}
|
2009-01-02 02:57:16 +00:00
|
|
|
error2:
|
|
|
|
free(dbname);
|
2001-03-25 18:37:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private const uint32_t ar[] = {
|
|
|
|
MAGICNO, VERSIONNO
|
|
|
|
};
|
2001-03-25 18:37:04 +00:00
|
|
|
/*
|
|
|
|
* handle an mmaped file.
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
|
|
|
apprentice_compile(struct magic_set *ms, struct magic **magicp,
|
|
|
|
uint32_t *nmagicp, const char *fn)
|
2001-03-25 18:37:04 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2009-01-02 02:57:16 +00:00
|
|
|
char *dbname;
|
|
|
|
int rv = -1;
|
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
dbname = mkdbname(ms, fn, 1);
|
2001-03-25 18:37:04 +00:00
|
|
|
|
2001-07-30 03:09:46 +00:00
|
|
|
if (dbname == NULL)
|
2009-01-02 02:57:16 +00:00
|
|
|
goto out;
|
2001-07-30 03:09:46 +00:00
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno, "cannot open `%s'", dbname);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto out;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
|
|
|
|
file_error(ms, errno, "error writing `%s'", dbname);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto out;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
|
|
|
|
!= sizeof(struct magic)) {
|
|
|
|
file_error(ms, errno, "error seeking `%s'", dbname);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto out;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
|
|
|
|
!= (ssize_t)(sizeof(struct magic) * *nmagicp)) {
|
|
|
|
file_error(ms, errno, "error writing `%s'", dbname);
|
2009-01-02 02:57:16 +00:00
|
|
|
goto out;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)close(fd);
|
2009-01-02 02:57:16 +00:00
|
|
|
rv = 0;
|
|
|
|
out:
|
|
|
|
free(dbname);
|
|
|
|
return rv;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private const char ext[] = ".mgc";
|
2001-03-25 18:37:04 +00:00
|
|
|
/*
|
|
|
|
* make a dbname
|
|
|
|
*/
|
2009-05-02 06:25:51 +00:00
|
|
|
private char *
|
|
|
|
mkdbname(struct magic_set *ms, const char *fn, int strip)
|
2001-03-25 18:37:04 +00:00
|
|
|
{
|
2009-05-02 06:25:51 +00:00
|
|
|
const char *p, *q;
|
|
|
|
char *buf;
|
|
|
|
|
2004-12-28 04:31:47 +00:00
|
|
|
if (strip) {
|
|
|
|
if ((p = strrchr(fn, '/')) != NULL)
|
|
|
|
fn = ++p;
|
|
|
|
}
|
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
for (q = fn; *q; q++)
|
|
|
|
continue;
|
|
|
|
/* Look for .mgc */
|
|
|
|
for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--)
|
|
|
|
if (*p != *q)
|
|
|
|
break;
|
2009-01-02 02:57:16 +00:00
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
/* Did not find .mgc, restore q */
|
|
|
|
if (p >= ext)
|
|
|
|
while (*q)
|
|
|
|
q++;
|
|
|
|
|
|
|
|
q++;
|
|
|
|
/* Compatibility with old code that looked in .mime */
|
|
|
|
if (ms->flags & MAGIC_MIME) {
|
|
|
|
asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext);
|
|
|
|
if (access(buf, R_OK) != -1) {
|
|
|
|
ms->flags &= MAGIC_MIME_TYPE;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
free(buf);
|
2009-01-02 02:57:16 +00:00
|
|
|
}
|
2009-05-02 06:25:51 +00:00
|
|
|
asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext);
|
|
|
|
|
|
|
|
/* Compatibility with old code that looked in .mime */
|
|
|
|
if (strstr(p, ".mime") != NULL)
|
|
|
|
ms->flags &= MAGIC_MIME_TYPE;
|
|
|
|
return buf;
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Byteswap an mmap'ed file if needed
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private void
|
2002-09-16 05:54:26 +00:00
|
|
|
byteswap(struct magic *magic, uint32_t nmagic)
|
2001-03-25 18:37:04 +00:00
|
|
|
{
|
2002-09-16 05:54:26 +00:00
|
|
|
uint32_t i;
|
2001-03-25 18:37:04 +00:00
|
|
|
for (i = 0; i < nmagic; i++)
|
|
|
|
bs1(&magic[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* swap a short
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private uint16_t
|
2002-09-16 05:54:26 +00:00
|
|
|
swap2(uint16_t sv)
|
2001-03-25 18:37:04 +00:00
|
|
|
{
|
2002-09-16 05:54:26 +00:00
|
|
|
uint16_t rv;
|
2004-08-09 08:45:41 +00:00
|
|
|
uint8_t *s = (uint8_t *)(void *)&sv;
|
|
|
|
uint8_t *d = (uint8_t *)(void *)&rv;
|
2001-03-25 18:37:04 +00:00
|
|
|
d[0] = s[1];
|
|
|
|
d[1] = s[0];
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* swap an int
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private uint32_t
|
2002-09-16 05:54:26 +00:00
|
|
|
swap4(uint32_t sv)
|
2001-03-25 18:37:04 +00:00
|
|
|
{
|
2002-09-16 05:54:26 +00:00
|
|
|
uint32_t rv;
|
2004-08-09 08:45:41 +00:00
|
|
|
uint8_t *s = (uint8_t *)(void *)&sv;
|
|
|
|
uint8_t *d = (uint8_t *)(void *)&rv;
|
2001-03-25 18:37:04 +00:00
|
|
|
d[0] = s[3];
|
|
|
|
d[1] = s[2];
|
|
|
|
d[2] = s[1];
|
|
|
|
d[3] = s[0];
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
/*
|
|
|
|
* swap a quad
|
|
|
|
*/
|
|
|
|
private uint64_t
|
|
|
|
swap8(uint64_t sv)
|
|
|
|
{
|
2009-01-02 02:57:16 +00:00
|
|
|
uint64_t rv;
|
2007-05-24 16:12:23 +00:00
|
|
|
uint8_t *s = (uint8_t *)(void *)&sv;
|
|
|
|
uint8_t *d = (uint8_t *)(void *)&rv;
|
2009-01-02 02:57:16 +00:00
|
|
|
#if 0
|
2007-05-24 16:12:23 +00:00
|
|
|
d[0] = s[3];
|
|
|
|
d[1] = s[2];
|
|
|
|
d[2] = s[1];
|
|
|
|
d[3] = s[0];
|
|
|
|
d[4] = s[7];
|
|
|
|
d[5] = s[6];
|
|
|
|
d[6] = s[5];
|
|
|
|
d[7] = s[4];
|
2009-01-02 02:57:16 +00:00
|
|
|
#else
|
|
|
|
d[0] = s[7];
|
|
|
|
d[1] = s[6];
|
|
|
|
d[2] = s[5];
|
|
|
|
d[3] = s[4];
|
|
|
|
d[4] = s[3];
|
|
|
|
d[5] = s[2];
|
|
|
|
d[6] = s[1];
|
|
|
|
d[7] = s[0];
|
|
|
|
#endif
|
2007-05-24 16:12:23 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-03-25 18:37:04 +00:00
|
|
|
/*
|
|
|
|
* byteswap a single magic entry
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private void
|
|
|
|
bs1(struct magic *m)
|
2001-03-25 18:37:04 +00:00
|
|
|
{
|
|
|
|
m->cont_level = swap2(m->cont_level);
|
2004-08-09 08:45:41 +00:00
|
|
|
m->offset = swap4((uint32_t)m->offset);
|
|
|
|
m->in_offset = swap4((uint32_t)m->in_offset);
|
2007-05-24 21:59:38 +00:00
|
|
|
m->lineno = swap4((uint32_t)m->lineno);
|
|
|
|
if (IS_STRING(m->type)) {
|
2009-01-02 02:57:16 +00:00
|
|
|
m->str_range = swap4(m->str_range);
|
2007-05-24 21:59:38 +00:00
|
|
|
m->str_flags = swap4(m->str_flags);
|
|
|
|
}
|
|
|
|
else {
|
2007-05-24 16:12:23 +00:00
|
|
|
m->value.q = swap8(m->value.q);
|
2007-05-24 21:59:38 +00:00
|
|
|
m->num_mask = swap8(m->num_mask);
|
|
|
|
}
|
2001-03-25 18:37:04 +00:00
|
|
|
}
|