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.
|
2009-05-02 06:25:51 +00:00
|
|
|
*
|
2004-08-09 08:45:41 +00:00
|
|
|
* 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.
|
2009-05-02 06:25:51 +00:00
|
|
|
*
|
2004-08-09 08:45:41 +00:00
|
|
|
* 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
|
|
|
/*
|
2001-04-25 07:41:21 +00:00
|
|
|
* softmagic - interpret variable magic from MAGIC
|
1994-09-03 19:16:28 +00:00
|
|
|
*/
|
|
|
|
|
2002-09-16 05:54:26 +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: softmagic.c,v 1.135 2009/03/27 22:42:49 christos Exp $")
|
2009-05-02 06:25:51 +00:00
|
|
|
#endif /* lint */
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
#include "magic.h"
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <string.h>
|
2000-11-05 08:33:55 +00:00
|
|
|
#include <ctype.h>
|
1997-03-18 18:00:03 +00:00
|
|
|
#include <stdlib.h>
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private int match(struct magic_set *, struct magic *, uint32_t,
|
2009-01-02 02:57:16 +00:00
|
|
|
const unsigned char *, size_t, int);
|
2007-05-24 21:59:38 +00:00
|
|
|
private int mget(struct magic_set *, const unsigned char *,
|
2006-06-19 07:52:15 +00:00
|
|
|
struct magic *, size_t, unsigned int);
|
2007-05-24 21:59:38 +00:00
|
|
|
private int magiccheck(struct magic_set *, struct magic *);
|
|
|
|
private int32_t mprint(struct magic_set *, struct magic *);
|
2009-05-02 06:25:51 +00:00
|
|
|
private int32_t moffset(struct magic_set *, struct magic *);
|
2004-08-09 08:45:41 +00:00
|
|
|
private void mdebug(uint32_t, const char *, size_t);
|
2004-12-28 04:31:47 +00:00
|
|
|
private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
|
2007-05-24 21:59:38 +00:00
|
|
|
const unsigned char *, uint32_t, size_t, size_t);
|
|
|
|
private int mconvert(struct magic_set *, struct magic *);
|
2007-05-24 16:12:23 +00:00
|
|
|
private int print_sep(struct magic_set *, int);
|
2009-05-02 06:25:51 +00:00
|
|
|
private int handle_annotation(struct magic_set *, struct magic *);
|
2007-05-24 16:12:23 +00:00
|
|
|
private void cvt_8(union VALUETYPE *, const struct magic *);
|
|
|
|
private void cvt_16(union VALUETYPE *, const struct magic *);
|
|
|
|
private void cvt_32(union VALUETYPE *, const struct magic *);
|
|
|
|
private void cvt_64(union VALUETYPE *, const struct magic *);
|
2001-04-25 07:41:21 +00:00
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
/*
|
2007-05-24 16:12:23 +00:00
|
|
|
* softmagic - lookup one file in parsed, in-memory copy of database
|
1994-09-03 19:16:28 +00:00
|
|
|
* Passed the name and FILE * of one file to be typed.
|
|
|
|
*/
|
|
|
|
/*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
|
2004-08-09 08:45:41 +00:00
|
|
|
protected int
|
2009-01-02 02:57:16 +00:00
|
|
|
file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, int mode)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2001-03-25 18:37:04 +00:00
|
|
|
struct mlist *ml;
|
2007-05-24 16:12:23 +00:00
|
|
|
int rv;
|
2004-08-09 08:45:41 +00:00
|
|
|
for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
|
2009-01-02 02:57:16 +00:00
|
|
|
if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode)) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
return rv;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go through the whole list, stopping if you find a match. Process all
|
|
|
|
* the continuations of that match before returning.
|
|
|
|
*
|
|
|
|
* We support multi-level continuations:
|
|
|
|
*
|
|
|
|
* At any time when processing a successful top-level match, there is a
|
|
|
|
* current continuation level; it represents the level of the last
|
|
|
|
* successfully matched continuation.
|
|
|
|
*
|
|
|
|
* Continuations above that level are skipped as, if we see one, it
|
|
|
|
* means that the continuation that controls them - i.e, the
|
|
|
|
* lower-level continuation preceding them - failed to match.
|
|
|
|
*
|
|
|
|
* Continuations below that level are processed as, if we see one,
|
|
|
|
* it means we've finished processing or skipping higher-level
|
|
|
|
* continuations under the control of a successful or unsuccessful
|
|
|
|
* lower-level continuation, and are now seeing the next lower-level
|
|
|
|
* continuation and should process it. The current continuation
|
|
|
|
* level reverts to the level of the one we're seeing.
|
|
|
|
*
|
|
|
|
* Continuations at the current level are processed as, if we see
|
|
|
|
* one, there's no lower-level continuation that may have failed.
|
|
|
|
*
|
|
|
|
* If a continuation matches, we bump the current continuation level
|
|
|
|
* so that higher-level continuations are processed.
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
|
|
|
match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
|
2009-01-02 02:57:16 +00:00
|
|
|
const unsigned char *s, size_t nbytes, int mode)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2004-08-09 08:45:41 +00:00
|
|
|
uint32_t magindex = 0;
|
|
|
|
unsigned int cont_level = 0;
|
1994-09-03 19:16:28 +00:00
|
|
|
int need_separator = 0;
|
2009-05-02 06:25:51 +00:00
|
|
|
int returnval = 0, e; /* if a match is found it is set to 1*/
|
2000-11-05 08:33:55 +00:00
|
|
|
int firstline = 1; /* a flag to print X\n X\n- X */
|
2007-05-24 16:12:23 +00:00
|
|
|
int printed_something = 0;
|
2009-05-02 06:25:51 +00:00
|
|
|
int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
|
1997-03-18 18:00:03 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
if (file_check_mem(ms, cont_level) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
for (magindex = 0; magindex < nmagic; magindex++) {
|
2009-05-02 06:25:51 +00:00
|
|
|
int flush = 0;
|
2009-01-02 02:57:16 +00:00
|
|
|
struct magic *m = &magic[magindex];
|
2007-05-24 21:59:38 +00:00
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
if ((m->flag & BINTEST) != mode) {
|
|
|
|
/* Skip sub-tests */
|
|
|
|
while (magic[magindex + 1].cont_level != 0 &&
|
|
|
|
++magindex < nmagic)
|
|
|
|
continue;
|
|
|
|
continue; /* Skip to next top-level test*/
|
|
|
|
}
|
|
|
|
|
|
|
|
ms->offset = m->offset;
|
|
|
|
ms->line = m->lineno;
|
2007-05-24 21:59:38 +00:00
|
|
|
|
|
|
|
/* if main entry matches, print it... */
|
2009-05-02 06:25:51 +00:00
|
|
|
switch (mget(ms, s, m, nbytes, cont_level)) {
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 0:
|
|
|
|
flush = m->reln != '!';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (m->type == FILE_INDIRECT)
|
|
|
|
returnval = 1;
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
switch (magiccheck(ms, m)) {
|
2006-06-19 07:52:15 +00:00
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 0:
|
|
|
|
flush++;
|
|
|
|
break;
|
|
|
|
default:
|
2009-05-02 06:25:51 +00:00
|
|
|
flush = 0;
|
2006-06-19 07:52:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-05-02 06:25:51 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
|
|
|
if (flush) {
|
2009-05-02 06:25:51 +00:00
|
|
|
/*
|
2004-08-09 08:45:41 +00:00
|
|
|
* main entry didn't match,
|
|
|
|
* flush its continuations
|
|
|
|
*/
|
|
|
|
while (magindex < nmagic - 1 &&
|
2007-05-24 21:59:38 +00:00
|
|
|
magic[magindex + 1].cont_level != 0)
|
|
|
|
magindex++;
|
2004-08-09 08:45:41 +00:00
|
|
|
continue;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
/*
|
|
|
|
* If we are going to print something, we'll need to print
|
|
|
|
* a blank before we print something else.
|
|
|
|
*/
|
2009-05-02 06:25:51 +00:00
|
|
|
if (*m->desc) {
|
2007-05-24 16:12:23 +00:00
|
|
|
need_separator = 1;
|
|
|
|
printed_something = 1;
|
2009-05-02 06:25:51 +00:00
|
|
|
if ((e = handle_annotation(ms, m)) != 0)
|
|
|
|
return e;
|
2007-05-24 16:12:23 +00:00
|
|
|
if (print_sep(ms, firstline) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2000-11-05 08:33:55 +00:00
|
|
|
}
|
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
|
|
|
|
if (print && mprint(ms, m) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 16:12:23 +00:00
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
ms->c.li[cont_level].off = moffset(ms, m);
|
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
/* and any continuations that match */
|
2007-05-24 21:59:38 +00:00
|
|
|
if (file_check_mem(ms, ++cont_level) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
while (magic[magindex+1].cont_level != 0 &&
|
|
|
|
++magindex < nmagic) {
|
2009-01-02 02:57:16 +00:00
|
|
|
m = &magic[magindex];
|
|
|
|
ms->line = m->lineno; /* for messages */
|
2007-05-24 21:59:38 +00:00
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
if (cont_level < m->cont_level)
|
2004-08-09 08:45:41 +00:00
|
|
|
continue;
|
2009-01-02 02:57:16 +00:00
|
|
|
if (cont_level > m->cont_level) {
|
2004-08-09 08:45:41 +00:00
|
|
|
/*
|
|
|
|
* We're at the end of the level
|
|
|
|
* "cont_level" continuations.
|
|
|
|
*/
|
2009-01-02 02:57:16 +00:00
|
|
|
cont_level = m->cont_level;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
2009-01-02 02:57:16 +00:00
|
|
|
ms->offset = m->offset;
|
|
|
|
if (m->flag & OFFADD) {
|
2007-05-24 16:12:23 +00:00
|
|
|
ms->offset +=
|
2007-05-24 21:59:38 +00:00
|
|
|
ms->c.li[cont_level - 1].off;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
#ifdef ENABLE_CONDITIONALS
|
2009-01-02 02:57:16 +00:00
|
|
|
if (m->cond == COND_ELSE ||
|
|
|
|
m->cond == COND_ELIF) {
|
2007-05-24 21:59:38 +00:00
|
|
|
if (ms->c.li[cont_level].last_match == 1)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2009-05-02 06:25:51 +00:00
|
|
|
switch (mget(ms, s, m, nbytes, cont_level)) {
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 0:
|
|
|
|
if (m->reln != '!')
|
|
|
|
continue;
|
|
|
|
flush = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (m->type == FILE_INDIRECT)
|
|
|
|
returnval = 1;
|
|
|
|
flush = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
switch (flush ? 1 : magiccheck(ms, m)) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 0:
|
2007-05-24 21:59:38 +00:00
|
|
|
#ifdef ENABLE_CONDITIONALS
|
|
|
|
ms->c.li[cont_level].last_match = 0;
|
|
|
|
#endif
|
2004-08-09 08:45:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
2007-05-24 21:59:38 +00:00
|
|
|
#ifdef ENABLE_CONDITIONALS
|
|
|
|
ms->c.li[cont_level].last_match = 1;
|
|
|
|
#endif
|
2009-01-02 02:57:16 +00:00
|
|
|
if (m->type != FILE_DEFAULT)
|
2007-05-24 21:59:38 +00:00
|
|
|
ms->c.li[cont_level].got_match = 1;
|
|
|
|
else if (ms->c.li[cont_level].got_match) {
|
|
|
|
ms->c.li[cont_level].got_match = 0;
|
|
|
|
break;
|
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
/*
|
|
|
|
* If we are going to print something,
|
|
|
|
* make sure that we have a separator first.
|
|
|
|
*/
|
2009-05-02 06:25:51 +00:00
|
|
|
if (*m->desc) {
|
|
|
|
if ((e = handle_annotation(ms, m)) != 0)
|
|
|
|
return e;
|
2009-05-18 22:27:42 +00:00
|
|
|
if (!printed_something) {
|
|
|
|
printed_something = 1;
|
|
|
|
if (print_sep(ms, firstline)
|
|
|
|
== -1)
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
/*
|
2007-05-24 21:59:38 +00:00
|
|
|
* This continuation matched. Print
|
|
|
|
* its message, with a blank before it
|
|
|
|
* if the previous item printed and
|
|
|
|
* this item isn't empty.
|
2004-08-09 08:45:41 +00:00
|
|
|
*/
|
|
|
|
/* space if previous printed */
|
|
|
|
if (need_separator
|
2009-01-02 02:57:16 +00:00
|
|
|
&& ((m->flag & NOSPACE) == 0)
|
2009-05-02 06:25:51 +00:00
|
|
|
&& *m->desc) {
|
|
|
|
if (print &&
|
|
|
|
file_printf(ms, " ") == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
|
|
|
need_separator = 0;
|
1997-03-18 18:00:03 +00:00
|
|
|
}
|
2009-05-02 06:25:51 +00:00
|
|
|
if (print && mprint(ms, m) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
|
|
|
ms->c.li[cont_level].off = moffset(ms, m);
|
|
|
|
|
|
|
|
if (*m->desc)
|
2004-08-09 08:45:41 +00:00
|
|
|
need_separator = 1;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
/*
|
|
|
|
* If we see any continuations
|
|
|
|
* at a higher level,
|
|
|
|
* process them.
|
|
|
|
*/
|
2007-05-24 21:59:38 +00:00
|
|
|
if (file_check_mem(ms, ++cont_level) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 21:59:38 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2008-01-13 20:35:16 +00:00
|
|
|
if (printed_something) {
|
|
|
|
firstline = 0;
|
2009-05-02 06:25:51 +00:00
|
|
|
if (print)
|
|
|
|
returnval = 1;
|
2008-01-13 20:35:16 +00:00
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
|
2009-05-02 06:25:51 +00:00
|
|
|
return returnval; /* don't keep searching */
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2000-11-05 08:33:55 +00:00
|
|
|
return returnval; /* This is hit if -k is set or there is no match */
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
private int
|
|
|
|
check_fmt(struct magic_set *ms, struct magic *m)
|
|
|
|
{
|
|
|
|
regex_t rx;
|
|
|
|
int rc;
|
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
if (strchr(m->desc, '%') == NULL)
|
2007-05-24 16:12:23 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
|
|
|
|
if (rc) {
|
|
|
|
char errmsg[512];
|
2007-05-24 21:59:38 +00:00
|
|
|
(void)regerror(rc, &rx, errmsg, sizeof(errmsg));
|
|
|
|
file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
2009-05-02 06:25:51 +00:00
|
|
|
rc = regexec(&rx, m->desc, 0, 0, 0);
|
2007-05-24 16:12:23 +00:00
|
|
|
regfree(&rx);
|
|
|
|
return !rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
#ifndef HAVE_STRNDUP
|
|
|
|
char * strndup(const char *, size_t);
|
|
|
|
|
|
|
|
char *
|
|
|
|
strndup(const char *str, size_t n)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *copy;
|
|
|
|
|
2009-05-18 22:27:42 +00:00
|
|
|
for (len = 0; len < n && str[len]; len++)
|
|
|
|
continue;
|
|
|
|
if ((copy = malloc(len + 1)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
(void)memcpy(copy, str, len);
|
2007-05-24 21:59:38 +00:00
|
|
|
copy[len] = '\0';
|
2009-05-18 22:27:42 +00:00
|
|
|
return copy;
|
2007-05-24 21:59:38 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_STRNDUP */
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private int32_t
|
2007-05-24 21:59:38 +00:00
|
|
|
mprint(struct magic_set *ms, struct magic *m)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2007-05-24 16:12:23 +00:00
|
|
|
uint64_t v;
|
2008-01-13 20:35:16 +00:00
|
|
|
float vf;
|
|
|
|
double vd;
|
2007-05-24 21:59:38 +00:00
|
|
|
int64_t t = 0;
|
2009-05-02 06:25:51 +00:00
|
|
|
char buf[128];
|
2007-05-24 21:59:38 +00:00
|
|
|
union VALUETYPE *p = &ms->ms_value;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
switch (m->type) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_BYTE:
|
2007-05-24 16:12:23 +00:00
|
|
|
v = file_signextend(ms, m, (uint64_t)p->b);
|
|
|
|
switch (check_fmt(ms, m)) {
|
|
|
|
case -1:
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 16:12:23 +00:00
|
|
|
case 1:
|
2009-05-02 06:25:51 +00:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%c",
|
|
|
|
(unsigned char)v);
|
|
|
|
if (file_printf(ms, m->desc, buf) == -1)
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
default:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, (unsigned char) v) == -1)
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t = ms->offset + sizeof(char);
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_SHORT:
|
|
|
|
case FILE_BESHORT:
|
|
|
|
case FILE_LESHORT:
|
2007-05-24 16:12:23 +00:00
|
|
|
v = file_signextend(ms, m, (uint64_t)p->h);
|
|
|
|
switch (check_fmt(ms, m)) {
|
|
|
|
case -1:
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 16:12:23 +00:00
|
|
|
case 1:
|
2009-05-02 06:25:51 +00:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%hu",
|
|
|
|
(unsigned short)v);
|
|
|
|
if (file_printf(ms, m->desc, buf) == -1)
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
default:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (
|
|
|
|
file_printf(ms, m->desc, (unsigned short) v) == -1)
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t = ms->offset + sizeof(short);
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
|
|
|
|
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:
|
2007-05-24 16:12:23 +00:00
|
|
|
v = file_signextend(ms, m, (uint64_t)p->l);
|
|
|
|
switch (check_fmt(ms, m)) {
|
|
|
|
case -1:
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 16:12:23 +00:00
|
|
|
case 1:
|
2009-05-02 06:25:51 +00:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v);
|
|
|
|
if (file_printf(ms, m->desc, buf) == -1)
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
default:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, (uint32_t) v) == -1)
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t = ms->offset + sizeof(int32_t);
|
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:
|
|
|
|
v = file_signextend(ms, m, p->q);
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, (uint64_t) v) == -1)
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
t = ms->offset + sizeof(int64_t);
|
|
|
|
break;
|
2007-05-24 21:59:38 +00:00
|
|
|
|
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:
|
2006-06-19 07:52:15 +00:00
|
|
|
if (m->reln == '=' || m->reln == '!') {
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, m->value.s) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 16:12:23 +00:00
|
|
|
t = ms->offset + m->vallen;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-01-13 20:35:16 +00:00
|
|
|
if (*m->value.s == '\0')
|
|
|
|
p->s[strcspn(p->s, "\n")] = '\0';
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, p->s) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 16:12:23 +00:00
|
|
|
t = ms->offset + strlen(p->s);
|
2009-01-02 02:57:16 +00:00
|
|
|
if (m->type == FILE_PSTRING)
|
|
|
|
t++;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
1997-03-18 18:00:03 +00:00
|
|
|
break;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
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:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 16:12:23 +00:00
|
|
|
t = ms->offset + sizeof(time_t);
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
|
|
|
|
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:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 16:12:23 +00:00
|
|
|
t = ms->offset + sizeof(time_t);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_QDATE:
|
|
|
|
case FILE_BEQDATE:
|
|
|
|
case FILE_LEQDATE:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
|
|
|
|
1)) == -1)
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
t = ms->offset + sizeof(uint64_t);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_QLDATE:
|
|
|
|
case FILE_BEQLDATE:
|
|
|
|
case FILE_LEQLDATE:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
|
|
|
|
0)) == -1)
|
2007-05-24 16:12:23 +00:00
|
|
|
return -1;
|
|
|
|
t = ms->offset + sizeof(uint64_t);
|
1997-03-18 18:00:03 +00:00
|
|
|
break;
|
2007-05-24 16:12:23 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_FLOAT:
|
|
|
|
case FILE_BEFLOAT:
|
|
|
|
case FILE_LEFLOAT:
|
|
|
|
vf = p->f;
|
|
|
|
switch (check_fmt(ms, m)) {
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 1:
|
2009-05-02 06:25:51 +00:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%g", vf);
|
|
|
|
if (file_printf(ms, m->desc, buf) == -1)
|
2008-01-13 20:35:16 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
default:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, vf) == -1)
|
2008-01-13 20:35:16 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t = ms->offset + sizeof(float);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_DOUBLE:
|
|
|
|
case FILE_BEDOUBLE:
|
|
|
|
case FILE_LEDOUBLE:
|
|
|
|
vd = p->d;
|
|
|
|
switch (check_fmt(ms, m)) {
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 1:
|
2009-05-02 06:25:51 +00:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%g", vd);
|
|
|
|
if (file_printf(ms, m->desc, buf) == -1)
|
2008-01-13 20:35:16 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
default:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, vd) == -1)
|
2008-01-13 20:35:16 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t = ms->offset + sizeof(double);
|
|
|
|
break;
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_REGEX: {
|
|
|
|
char *cp;
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
cp = strndup((const char *)ms->search.s, ms->search.rm_len);
|
|
|
|
if (cp == NULL) {
|
|
|
|
file_oomem(ms, ms->search.rm_len);
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
2007-05-24 21:59:38 +00:00
|
|
|
}
|
2009-05-02 06:25:51 +00:00
|
|
|
rval = file_printf(ms, m->desc, cp);
|
2007-05-24 21:59:38 +00:00
|
|
|
free(cp);
|
|
|
|
|
|
|
|
if (rval == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((m->str_flags & REGEX_OFFSET_START))
|
|
|
|
t = ms->search.offset;
|
|
|
|
else
|
|
|
|
t = ms->search.offset + ms->search.rm_len;
|
2002-09-16 05:54:26 +00:00
|
|
|
break;
|
2007-05-24 21:59:38 +00:00
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_SEARCH:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, m->value.s) == -1)
|
2006-06-19 07:52:15 +00:00
|
|
|
return -1;
|
2007-05-24 21:59:38 +00:00
|
|
|
if ((m->str_flags & REGEX_OFFSET_START))
|
|
|
|
t = ms->search.offset;
|
|
|
|
else
|
|
|
|
t = ms->search.offset + m->vallen;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_DEFAULT:
|
2009-05-02 06:25:51 +00:00
|
|
|
if (file_printf(ms, m->desc, m->value.s) == -1)
|
2007-05-24 21:59:38 +00:00
|
|
|
return -1;
|
|
|
|
t = ms->offset;
|
2006-06-19 07:52:15 +00:00
|
|
|
break;
|
1997-03-18 18:00:03 +00:00
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
case FILE_INDIRECT:
|
|
|
|
t = ms->offset;
|
|
|
|
break;
|
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
default:
|
2007-05-24 21:59:38 +00:00
|
|
|
file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2009-05-02 06:25:51 +00:00
|
|
|
return (int32_t)t;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
private int32_t
|
|
|
|
moffset(struct magic_set *ms, struct magic *m)
|
|
|
|
{
|
|
|
|
switch (m->type) {
|
|
|
|
case FILE_BYTE:
|
|
|
|
return ms->offset + sizeof(char);
|
|
|
|
|
|
|
|
case FILE_SHORT:
|
|
|
|
case FILE_BESHORT:
|
|
|
|
case FILE_LESHORT:
|
|
|
|
return ms->offset + sizeof(short);
|
|
|
|
|
|
|
|
case FILE_LONG:
|
|
|
|
case FILE_BELONG:
|
|
|
|
case FILE_LELONG:
|
|
|
|
case FILE_MELONG:
|
|
|
|
return ms->offset + sizeof(int32_t);
|
|
|
|
|
|
|
|
case FILE_QUAD:
|
|
|
|
case FILE_BEQUAD:
|
|
|
|
case FILE_LEQUAD:
|
|
|
|
return ms->offset + sizeof(int64_t);
|
|
|
|
|
|
|
|
case FILE_STRING:
|
|
|
|
case FILE_PSTRING:
|
|
|
|
case FILE_BESTRING16:
|
|
|
|
case FILE_LESTRING16:
|
|
|
|
if (m->reln == '=' || m->reln == '!')
|
|
|
|
return ms->offset + m->vallen;
|
|
|
|
else {
|
|
|
|
union VALUETYPE *p = &ms->ms_value;
|
|
|
|
uint32_t t;
|
|
|
|
|
|
|
|
if (*m->value.s == '\0')
|
|
|
|
p->s[strcspn(p->s, "\n")] = '\0';
|
|
|
|
t = ms->offset + strlen(p->s);
|
|
|
|
if (m->type == FILE_PSTRING)
|
|
|
|
t++;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FILE_DATE:
|
|
|
|
case FILE_BEDATE:
|
|
|
|
case FILE_LEDATE:
|
|
|
|
case FILE_MEDATE:
|
|
|
|
return ms->offset + sizeof(time_t);
|
|
|
|
|
|
|
|
case FILE_LDATE:
|
|
|
|
case FILE_BELDATE:
|
|
|
|
case FILE_LELDATE:
|
|
|
|
case FILE_MELDATE:
|
|
|
|
return ms->offset + sizeof(time_t);
|
|
|
|
|
|
|
|
case FILE_QDATE:
|
|
|
|
case FILE_BEQDATE:
|
|
|
|
case FILE_LEQDATE:
|
|
|
|
return ms->offset + sizeof(uint64_t);
|
|
|
|
|
|
|
|
case FILE_QLDATE:
|
|
|
|
case FILE_BEQLDATE:
|
|
|
|
case FILE_LEQLDATE:
|
|
|
|
return ms->offset + sizeof(uint64_t);
|
|
|
|
|
|
|
|
case FILE_FLOAT:
|
|
|
|
case FILE_BEFLOAT:
|
|
|
|
case FILE_LEFLOAT:
|
|
|
|
return ms->offset + sizeof(float);
|
|
|
|
|
|
|
|
case FILE_DOUBLE:
|
|
|
|
case FILE_BEDOUBLE:
|
|
|
|
case FILE_LEDOUBLE:
|
|
|
|
return ms->offset + sizeof(double);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_REGEX:
|
|
|
|
if ((m->str_flags & REGEX_OFFSET_START) != 0)
|
|
|
|
return ms->search.offset;
|
|
|
|
else
|
|
|
|
return ms->search.offset + ms->search.rm_len;
|
|
|
|
|
|
|
|
case FILE_SEARCH:
|
|
|
|
if ((m->str_flags & REGEX_OFFSET_START) != 0)
|
|
|
|
return ms->search.offset;
|
|
|
|
else
|
|
|
|
return ms->search.offset + m->vallen;
|
|
|
|
|
|
|
|
case FILE_DEFAULT:
|
|
|
|
return ms->offset;
|
|
|
|
|
|
|
|
case FILE_INDIRECT:
|
|
|
|
return ms->offset;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
|
|
|
|
#define DO_CVT(fld, cast) \
|
2007-05-24 21:59:38 +00:00
|
|
|
if (m->num_mask) \
|
|
|
|
switch (m->mask_op & FILE_OPS_MASK) { \
|
2007-05-24 16:12:23 +00:00
|
|
|
case FILE_OPAND: \
|
2007-05-24 21:59:38 +00:00
|
|
|
p->fld &= cast m->num_mask; \
|
2007-05-24 16:12:23 +00:00
|
|
|
break; \
|
|
|
|
case FILE_OPOR: \
|
2007-05-24 21:59:38 +00:00
|
|
|
p->fld |= cast m->num_mask; \
|
2007-05-24 16:12:23 +00:00
|
|
|
break; \
|
|
|
|
case FILE_OPXOR: \
|
2007-05-24 21:59:38 +00:00
|
|
|
p->fld ^= cast m->num_mask; \
|
2007-05-24 16:12:23 +00:00
|
|
|
break; \
|
|
|
|
case FILE_OPADD: \
|
2007-05-24 21:59:38 +00:00
|
|
|
p->fld += cast m->num_mask; \
|
2007-05-24 16:12:23 +00:00
|
|
|
break; \
|
|
|
|
case FILE_OPMINUS: \
|
2007-05-24 21:59:38 +00:00
|
|
|
p->fld -= cast m->num_mask; \
|
2007-05-24 16:12:23 +00:00
|
|
|
break; \
|
|
|
|
case FILE_OPMULTIPLY: \
|
2007-05-24 21:59:38 +00:00
|
|
|
p->fld *= cast m->num_mask; \
|
2007-05-24 16:12:23 +00:00
|
|
|
break; \
|
|
|
|
case FILE_OPDIVIDE: \
|
2007-05-24 21:59:38 +00:00
|
|
|
p->fld /= cast m->num_mask; \
|
2007-05-24 16:12:23 +00:00
|
|
|
break; \
|
|
|
|
case FILE_OPMODULO: \
|
2007-05-24 21:59:38 +00:00
|
|
|
p->fld %= cast m->num_mask; \
|
2007-05-24 16:12:23 +00:00
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
if (m->mask_op & FILE_OPINVERSE) \
|
|
|
|
p->fld = ~p->fld \
|
|
|
|
|
|
|
|
private void
|
|
|
|
cvt_8(union VALUETYPE *p, const struct magic *m)
|
|
|
|
{
|
|
|
|
DO_CVT(b, (uint8_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void
|
|
|
|
cvt_16(union VALUETYPE *p, const struct magic *m)
|
|
|
|
{
|
|
|
|
DO_CVT(h, (uint16_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void
|
|
|
|
cvt_32(union VALUETYPE *p, const struct magic *m)
|
|
|
|
{
|
|
|
|
DO_CVT(l, (uint32_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void
|
|
|
|
cvt_64(union VALUETYPE *p, const struct magic *m)
|
|
|
|
{
|
|
|
|
DO_CVT(q, (uint64_t));
|
|
|
|
}
|
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
#define DO_CVT2(fld, cast) \
|
|
|
|
if (m->num_mask) \
|
|
|
|
switch (m->mask_op & FILE_OPS_MASK) { \
|
|
|
|
case FILE_OPADD: \
|
|
|
|
p->fld += cast m->num_mask; \
|
|
|
|
break; \
|
|
|
|
case FILE_OPMINUS: \
|
|
|
|
p->fld -= cast m->num_mask; \
|
|
|
|
break; \
|
|
|
|
case FILE_OPMULTIPLY: \
|
|
|
|
p->fld *= cast m->num_mask; \
|
|
|
|
break; \
|
|
|
|
case FILE_OPDIVIDE: \
|
|
|
|
p->fld /= cast m->num_mask; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
|
|
|
|
private void
|
|
|
|
cvt_float(union VALUETYPE *p, const struct magic *m)
|
|
|
|
{
|
|
|
|
DO_CVT2(f, (float));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void
|
|
|
|
cvt_double(union VALUETYPE *p, const struct magic *m)
|
|
|
|
{
|
|
|
|
DO_CVT2(d, (double));
|
|
|
|
}
|
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
/*
|
|
|
|
* Convert the byte order of the data we are looking at
|
2001-07-30 03:09:46 +00:00
|
|
|
* While we're here, let's apply the mask operation
|
|
|
|
* (unless you have a better idea)
|
1994-09-03 19:16:28 +00:00
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
2007-05-24 21:59:38 +00:00
|
|
|
mconvert(struct magic_set *ms, struct magic *m)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2007-05-24 21:59:38 +00:00
|
|
|
union VALUETYPE *p = &ms->ms_value;
|
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
switch (m->type) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_BYTE:
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_8(p, m);
|
2001-07-30 03:09:46 +00:00
|
|
|
return 1;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_SHORT:
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_16(p, m);
|
2001-07-30 03:09:46 +00:00
|
|
|
return 1;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_LONG:
|
|
|
|
case FILE_DATE:
|
|
|
|
case FILE_LDATE:
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_32(p, m);
|
|
|
|
return 1;
|
|
|
|
case FILE_QUAD:
|
|
|
|
case FILE_QDATE:
|
|
|
|
case FILE_QLDATE:
|
|
|
|
cvt_64(p, m);
|
1994-09-03 19:16:28 +00:00
|
|
|
return 1;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_STRING:
|
2004-12-28 04:31:47 +00:00
|
|
|
case FILE_BESTRING16:
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_LESTRING16: {
|
|
|
|
/* Null terminate and eat *trailing* return */
|
|
|
|
p->s[sizeof(p->s) - 1] = '\0';
|
2009-01-02 02:57:16 +00:00
|
|
|
#if 0
|
|
|
|
/* Why? breaks magic numbers that end with \xa */
|
2007-05-24 21:59:38 +00:00
|
|
|
len = strlen(p->s);
|
|
|
|
if (len-- && p->s[len] == '\n')
|
|
|
|
p->s[len] = '\0';
|
2009-01-02 02:57:16 +00:00
|
|
|
#endif
|
2007-05-24 21:59:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case FILE_PSTRING: {
|
|
|
|
char *ptr1 = p->s, *ptr2 = ptr1 + 1;
|
|
|
|
size_t len = *p->s;
|
|
|
|
if (len >= sizeof(p->s))
|
|
|
|
len = sizeof(p->s) - 1;
|
|
|
|
while (len--)
|
|
|
|
*ptr1++ = *ptr2++;
|
|
|
|
*ptr1 = '\0';
|
2009-01-02 02:57:16 +00:00
|
|
|
#if 0
|
|
|
|
/* Why? breaks magic numbers that end with \xa */
|
2007-05-24 21:59:38 +00:00
|
|
|
len = strlen(p->s);
|
|
|
|
if (len-- && p->s[len] == '\n')
|
|
|
|
p->s[len] = '\0';
|
2009-01-02 02:57:16 +00:00
|
|
|
#endif
|
2007-05-24 21:59:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_BESHORT:
|
1994-09-03 19:16:28 +00:00
|
|
|
p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_16(p, m);
|
1994-09-03 19:16:28 +00:00
|
|
|
return 1;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_BELONG:
|
|
|
|
case FILE_BEDATE:
|
|
|
|
case FILE_BELDATE:
|
2002-09-16 05:54:26 +00:00
|
|
|
p->l = (int32_t)
|
1994-09-03 19:16:28 +00:00
|
|
|
((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_32(p, m);
|
|
|
|
return 1;
|
|
|
|
case FILE_BEQUAD:
|
|
|
|
case FILE_BEQDATE:
|
|
|
|
case FILE_BEQLDATE:
|
2009-01-02 02:57:16 +00:00
|
|
|
p->q = (uint64_t)
|
|
|
|
(((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
|
|
|
|
((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
|
|
|
|
((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
|
|
|
|
((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_64(p, m);
|
1994-09-03 19:16:28 +00:00
|
|
|
return 1;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_LESHORT:
|
1994-09-03 19:16:28 +00:00
|
|
|
p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_16(p, m);
|
1994-09-03 19:16:28 +00:00
|
|
|
return 1;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_LELONG:
|
|
|
|
case FILE_LEDATE:
|
|
|
|
case FILE_LELDATE:
|
2002-09-16 05:54:26 +00:00
|
|
|
p->l = (int32_t)
|
1994-09-03 19:16:28 +00:00
|
|
|
((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_32(p, m);
|
|
|
|
return 1;
|
|
|
|
case FILE_LEQUAD:
|
|
|
|
case FILE_LEQDATE:
|
|
|
|
case FILE_LEQLDATE:
|
2009-01-02 02:57:16 +00:00
|
|
|
p->q = (uint64_t)
|
|
|
|
(((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
|
|
|
|
((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
|
|
|
|
((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
|
|
|
|
((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_64(p, m);
|
1994-09-03 19:16:28 +00:00
|
|
|
return 1;
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_MELONG:
|
|
|
|
case FILE_MEDATE:
|
|
|
|
case FILE_MELDATE:
|
|
|
|
p->l = (int32_t)
|
|
|
|
((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
|
2007-05-24 16:12:23 +00:00
|
|
|
cvt_32(p, m);
|
2006-06-19 07:52:15 +00:00
|
|
|
return 1;
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_FLOAT:
|
|
|
|
cvt_float(p, m);
|
|
|
|
return 1;
|
|
|
|
case FILE_BEFLOAT:
|
|
|
|
p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
|
|
|
|
((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
|
|
|
|
cvt_float(p, m);
|
|
|
|
return 1;
|
|
|
|
case FILE_LEFLOAT:
|
|
|
|
p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
|
|
|
|
((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
|
|
|
|
cvt_float(p, m);
|
|
|
|
return 1;
|
|
|
|
case FILE_DOUBLE:
|
|
|
|
cvt_double(p, m);
|
|
|
|
return 1;
|
|
|
|
case FILE_BEDOUBLE:
|
|
|
|
p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
|
|
|
|
((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
|
|
|
|
((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
|
|
|
|
((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
|
|
|
|
cvt_double(p, m);
|
|
|
|
return 1;
|
|
|
|
case FILE_LEDOUBLE:
|
|
|
|
p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
|
|
|
|
((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
|
|
|
|
((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
|
|
|
|
((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
|
|
|
|
cvt_double(p, m);
|
|
|
|
return 1;
|
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:
|
2002-09-16 05:54:26 +00:00
|
|
|
return 1;
|
1994-09-03 19:16:28 +00:00
|
|
|
default:
|
2007-05-24 21:59:38 +00:00
|
|
|
file_magerror(ms, "invalid type %d in mconvert()", m->type);
|
1994-09-03 19:16:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private void
|
|
|
|
mdebug(uint32_t offset, const char *str, size_t len)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
1997-03-18 18:00:03 +00:00
|
|
|
(void) fprintf(stderr, "mget @%d: ", offset);
|
2004-08-09 08:45:41 +00:00
|
|
|
file_showstr(stderr, str, len);
|
1994-09-03 19:16:28 +00:00
|
|
|
(void) fputc('\n', stderr);
|
|
|
|
(void) fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
2004-12-28 04:31:47 +00:00
|
|
|
mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
|
2007-05-24 21:59:38 +00:00
|
|
|
const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2007-05-24 21:59:38 +00:00
|
|
|
/*
|
|
|
|
* Note: FILE_SEARCH and FILE_REGEX do not actually copy
|
|
|
|
* anything, but setup pointers into the source
|
|
|
|
*/
|
|
|
|
if (indir == 0) {
|
|
|
|
switch (type) {
|
|
|
|
case FILE_SEARCH:
|
|
|
|
ms->search.s = (const char *)s + offset;
|
|
|
|
ms->search.s_len = nbytes - offset;
|
2009-01-02 02:57:16 +00:00
|
|
|
ms->search.offset = offset;
|
2006-06-19 07:52:15 +00:00
|
|
|
return 0;
|
2004-12-28 04:31:47 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_REGEX: {
|
|
|
|
const char *b;
|
|
|
|
const char *c;
|
|
|
|
const char *last; /* end of search region */
|
|
|
|
const char *buf; /* start of search region */
|
2009-05-02 06:25:51 +00:00
|
|
|
const char *end;
|
2007-05-24 21:59:38 +00:00
|
|
|
size_t lines;
|
|
|
|
|
|
|
|
if (s == NULL) {
|
|
|
|
ms->search.s_len = 0;
|
|
|
|
ms->search.s = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buf = (const char *)s + offset;
|
2009-05-02 06:25:51 +00:00
|
|
|
end = last = (const char *)s + nbytes;
|
2007-05-24 21:59:38 +00:00
|
|
|
/* mget() guarantees buf <= last */
|
|
|
|
for (lines = linecnt, b = buf;
|
2009-05-02 06:25:51 +00:00
|
|
|
lines && ((b = memchr(c = b, '\n', end - b)) || (b = memchr(c, '\r', end - c)));
|
2007-05-24 21:59:38 +00:00
|
|
|
lines--, b++) {
|
|
|
|
last = b;
|
|
|
|
if (b[0] == '\r' && b[1] == '\n')
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
if (lines)
|
|
|
|
last = (const char *)s + nbytes;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
ms->search.s = buf;
|
|
|
|
ms->search.s_len = last - buf;
|
|
|
|
ms->search.offset = offset;
|
|
|
|
ms->search.rm_len = 0;
|
|
|
|
return 0;
|
2007-05-24 16:12:23 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_BESTRING16:
|
|
|
|
case FILE_LESTRING16: {
|
|
|
|
const unsigned char *src = s + offset;
|
|
|
|
const unsigned char *esrc = s + nbytes;
|
|
|
|
char *dst = p->s;
|
|
|
|
char *edst = &p->s[sizeof(p->s) - 1];
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
if (type == FILE_BESTRING16)
|
|
|
|
src++;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
/* check for pointer overflow */
|
|
|
|
if (src < s) {
|
2009-05-02 06:25:51 +00:00
|
|
|
file_magerror(ms, "invalid offset %u in mcopy()",
|
2007-05-24 21:59:38 +00:00
|
|
|
offset);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-01-13 20:35:16 +00:00
|
|
|
for (/*EMPTY*/; src < esrc; src += 2, dst++) {
|
2007-05-24 21:59:38 +00:00
|
|
|
if (dst < edst)
|
2008-01-13 20:35:16 +00:00
|
|
|
*dst = *src;
|
2007-05-24 21:59:38 +00:00
|
|
|
else
|
|
|
|
break;
|
2008-01-13 20:35:16 +00:00
|
|
|
if (*dst == '\0') {
|
|
|
|
if (type == FILE_BESTRING16 ?
|
|
|
|
*(src - 1) != '\0' :
|
|
|
|
*(src + 1) != '\0')
|
|
|
|
*dst = ' ';
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
}
|
|
|
|
*edst = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case FILE_STRING: /* XXX - these two should not need */
|
|
|
|
case FILE_PSTRING: /* to copy anything, but do anyway. */
|
|
|
|
default:
|
|
|
|
break;
|
2004-12-28 04:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset >= nbytes) {
|
|
|
|
(void)memset(p, '\0', sizeof(*p));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (nbytes - offset < sizeof(*p))
|
|
|
|
nbytes = nbytes - offset;
|
|
|
|
else
|
|
|
|
nbytes = sizeof(*p);
|
|
|
|
|
|
|
|
(void)memcpy(p, s + offset, nbytes);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the usefulness of padding with zeroes eludes me, it
|
|
|
|
* might even cause problems
|
|
|
|
*/
|
|
|
|
if (nbytes < sizeof(*p))
|
2006-06-19 07:52:15 +00:00
|
|
|
(void)memset(((char *)(void *)p) + nbytes, '\0',
|
|
|
|
sizeof(*p) - nbytes);
|
2004-12-28 04:31:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int
|
2007-05-24 21:59:38 +00:00
|
|
|
mget(struct magic_set *ms, const unsigned char *s,
|
2006-06-19 07:52:15 +00:00
|
|
|
struct magic *m, size_t nbytes, unsigned int cont_level)
|
2004-12-28 04:31:47 +00:00
|
|
|
{
|
2007-05-24 16:12:23 +00:00
|
|
|
uint32_t offset = ms->offset;
|
2009-01-02 02:57:16 +00:00
|
|
|
uint32_t count = m->str_range;
|
2007-05-24 21:59:38 +00:00
|
|
|
union VALUETYPE *p = &ms->ms_value;
|
2004-12-28 04:31:47 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
|
2004-12-28 04:31:47 +00:00
|
|
|
return -1;
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0) {
|
|
|
|
mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
|
2009-05-02 06:25:51 +00:00
|
|
|
#ifndef COMPILE_ONLY
|
2004-08-09 08:45:41 +00:00
|
|
|
file_mdump(m);
|
2009-05-02 06:25:51 +00:00
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m->flag & INDIR) {
|
2006-06-19 07:52:15 +00:00
|
|
|
int off = m->in_offset;
|
|
|
|
if (m->in_op & FILE_OPINDIRECT) {
|
2009-01-02 02:57:16 +00:00
|
|
|
const union VALUETYPE *q = CAST(const union VALUETYPE *,
|
|
|
|
((const void *)(s + offset + off)));
|
2006-06-19 07:52:15 +00:00
|
|
|
switch (m->in_type) {
|
|
|
|
case FILE_BYTE:
|
|
|
|
off = q->b;
|
|
|
|
break;
|
|
|
|
case FILE_SHORT:
|
|
|
|
off = q->h;
|
|
|
|
break;
|
|
|
|
case FILE_BESHORT:
|
|
|
|
off = (short)((q->hs[0]<<8)|(q->hs[1]));
|
|
|
|
break;
|
|
|
|
case FILE_LESHORT:
|
|
|
|
off = (short)((q->hs[1]<<8)|(q->hs[0]));
|
|
|
|
break;
|
|
|
|
case FILE_LONG:
|
|
|
|
off = q->l;
|
|
|
|
break;
|
|
|
|
case FILE_BELONG:
|
2009-05-02 06:25:51 +00:00
|
|
|
case FILE_BEID3:
|
2006-06-19 07:52:15 +00:00
|
|
|
off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
|
|
|
|
(q->hl[2]<<8)|(q->hl[3]));
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
case FILE_LEID3:
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_LELONG:
|
|
|
|
off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
|
|
|
|
(q->hl[1]<<8)|(q->hl[0]));
|
|
|
|
break;
|
|
|
|
case FILE_MELONG:
|
|
|
|
off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
|
|
|
|
(q->hl[3]<<8)|(q->hl[2]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-03-25 18:37:04 +00:00
|
|
|
switch (m->in_type) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_BYTE:
|
2007-05-24 21:59:38 +00:00
|
|
|
if (nbytes < (offset + 1))
|
|
|
|
return 0;
|
2006-06-19 07:52:15 +00:00
|
|
|
if (off) {
|
2007-05-24 21:59:38 +00:00
|
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPAND:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->b & off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPOR:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->b | off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPXOR:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->b ^ off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPADD:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->b + off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMINUS:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->b - off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMULTIPLY:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->b * off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPDIVIDE:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->b / off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMODULO:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->b % off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
} else
|
|
|
|
offset = p->b;
|
|
|
|
if (m->in_op & FILE_OPINVERSE)
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = ~offset;
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_BESHORT:
|
2006-06-19 07:52:15 +00:00
|
|
|
if (nbytes < (offset + 2))
|
|
|
|
return 0;
|
|
|
|
if (off) {
|
2007-05-24 21:59:38 +00:00
|
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPAND:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[0]<<8)|
|
|
|
|
(p->hs[1])) &
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPOR:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[0]<<8)|
|
|
|
|
(p->hs[1])) |
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPXOR:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[0]<<8)|
|
|
|
|
(p->hs[1])) ^
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPADD:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[0]<<8)|
|
|
|
|
(p->hs[1])) +
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMINUS:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[0]<<8)|
|
|
|
|
(p->hs[1])) -
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMULTIPLY:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[0]<<8)|
|
|
|
|
(p->hs[1])) *
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPDIVIDE:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[0]<<8)|
|
|
|
|
(p->hs[1])) /
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMODULO:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[0]<<8)|
|
|
|
|
(p->hs[1])) %
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
} else
|
|
|
|
offset = (short)((p->hs[0]<<8)|
|
|
|
|
(p->hs[1]));
|
|
|
|
if (m->in_op & FILE_OPINVERSE)
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = ~offset;
|
2000-11-05 08:33:55 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_LESHORT:
|
2006-06-19 07:52:15 +00:00
|
|
|
if (nbytes < (offset + 2))
|
|
|
|
return 0;
|
|
|
|
if (off) {
|
2007-05-24 21:59:38 +00:00
|
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPAND:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[1]<<8)|
|
|
|
|
(p->hs[0])) &
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPOR:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[1]<<8)|
|
|
|
|
(p->hs[0])) |
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPXOR:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[1]<<8)|
|
|
|
|
(p->hs[0])) ^
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPADD:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[1]<<8)|
|
|
|
|
(p->hs[0])) +
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMINUS:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[1]<<8)|
|
|
|
|
(p->hs[0])) -
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMULTIPLY:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[1]<<8)|
|
|
|
|
(p->hs[0])) *
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPDIVIDE:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[1]<<8)|
|
|
|
|
(p->hs[0])) /
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMODULO:
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = (short)((p->hs[1]<<8)|
|
|
|
|
(p->hs[0])) %
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
} else
|
|
|
|
offset = (short)((p->hs[1]<<8)|
|
|
|
|
(p->hs[0]));
|
|
|
|
if (m->in_op & FILE_OPINVERSE)
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = ~offset;
|
2000-11-05 08:33:55 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_SHORT:
|
2006-06-19 07:52:15 +00:00
|
|
|
if (nbytes < (offset + 2))
|
|
|
|
return 0;
|
|
|
|
if (off) {
|
2007-05-24 21:59:38 +00:00
|
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPAND:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->h & off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPOR:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->h | off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPXOR:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->h ^ off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPADD:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->h + off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMINUS:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->h - off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMULTIPLY:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->h * off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPDIVIDE:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->h / off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMODULO:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->h % off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
offset = p->h;
|
|
|
|
if (m->in_op & FILE_OPINVERSE)
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = ~offset;
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_BELONG:
|
2009-05-02 06:25:51 +00:00
|
|
|
case FILE_BEID3:
|
2006-06-19 07:52:15 +00:00
|
|
|
if (nbytes < (offset + 4))
|
|
|
|
return 0;
|
|
|
|
if (off) {
|
2007-05-24 21:59:38 +00:00
|
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPAND:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[0]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[1]<<16)|
|
|
|
|
(p->hl[2]<<8)|
|
|
|
|
(p->hl[3])) &
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPOR:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[0]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[1]<<16)|
|
|
|
|
(p->hl[2]<<8)|
|
|
|
|
(p->hl[3])) |
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPXOR:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[0]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[1]<<16)|
|
|
|
|
(p->hl[2]<<8)|
|
|
|
|
(p->hl[3])) ^
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPADD:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[0]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[1]<<16)|
|
|
|
|
(p->hl[2]<<8)|
|
|
|
|
(p->hl[3])) +
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMINUS:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[0]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[1]<<16)|
|
|
|
|
(p->hl[2]<<8)|
|
|
|
|
(p->hl[3])) -
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMULTIPLY:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[0]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[1]<<16)|
|
|
|
|
(p->hl[2]<<8)|
|
|
|
|
(p->hl[3])) *
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPDIVIDE:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[0]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[1]<<16)|
|
|
|
|
(p->hl[2]<<8)|
|
|
|
|
(p->hl[3])) /
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMODULO:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[0]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[1]<<16)|
|
|
|
|
(p->hl[2]<<8)|
|
|
|
|
(p->hl[3])) %
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
} else
|
|
|
|
offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
(p->hl[1]<<16)|
|
|
|
|
(p->hl[2]<<8)|
|
|
|
|
(p->hl[3]));
|
|
|
|
if (m->in_op & FILE_OPINVERSE)
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = ~offset;
|
2000-11-05 08:33:55 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_LELONG:
|
2009-05-02 06:25:51 +00:00
|
|
|
case FILE_LEID3:
|
2006-06-19 07:52:15 +00:00
|
|
|
if (nbytes < (offset + 4))
|
|
|
|
return 0;
|
|
|
|
if (off) {
|
2007-05-24 21:59:38 +00:00
|
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPAND:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[3]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[2]<<16)|
|
|
|
|
(p->hl[1]<<8)|
|
|
|
|
(p->hl[0])) &
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPOR:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[3]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[2]<<16)|
|
|
|
|
(p->hl[1]<<8)|
|
|
|
|
(p->hl[0])) |
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPXOR:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[3]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[2]<<16)|
|
|
|
|
(p->hl[1]<<8)|
|
|
|
|
(p->hl[0])) ^
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPADD:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[3]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[2]<<16)|
|
|
|
|
(p->hl[1]<<8)|
|
|
|
|
(p->hl[0])) +
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMINUS:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[3]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[2]<<16)|
|
|
|
|
(p->hl[1]<<8)|
|
|
|
|
(p->hl[0])) -
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMULTIPLY:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[3]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[2]<<16)|
|
|
|
|
(p->hl[1]<<8)|
|
|
|
|
(p->hl[0])) *
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPDIVIDE:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[3]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[2]<<16)|
|
|
|
|
(p->hl[1]<<8)|
|
|
|
|
(p->hl[0])) /
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMODULO:
|
2002-09-16 05:54:26 +00:00
|
|
|
offset = (int32_t)((p->hl[3]<<24)|
|
2001-07-30 03:09:46 +00:00
|
|
|
(p->hl[2]<<16)|
|
|
|
|
(p->hl[1]<<8)|
|
|
|
|
(p->hl[0])) %
|
2006-06-19 07:52:15 +00:00
|
|
|
off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
} else
|
|
|
|
offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
(p->hl[2]<<16)|
|
|
|
|
(p->hl[1]<<8)|
|
|
|
|
(p->hl[0]));
|
|
|
|
if (m->in_op & FILE_OPINVERSE)
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = ~offset;
|
2000-11-05 08:33:55 +00:00
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_MELONG:
|
|
|
|
if (nbytes < (offset + 4))
|
|
|
|
return 0;
|
|
|
|
if (off) {
|
2007-05-24 21:59:38 +00:00
|
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
2006-06-19 07:52:15 +00:00
|
|
|
case FILE_OPAND:
|
|
|
|
offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
(p->hl[0]<<16)|
|
|
|
|
(p->hl[3]<<8)|
|
|
|
|
(p->hl[2])) &
|
|
|
|
off;
|
|
|
|
break;
|
|
|
|
case FILE_OPOR:
|
|
|
|
offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
(p->hl[0]<<16)|
|
|
|
|
(p->hl[3]<<8)|
|
|
|
|
(p->hl[2])) |
|
|
|
|
off;
|
|
|
|
break;
|
|
|
|
case FILE_OPXOR:
|
|
|
|
offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
(p->hl[0]<<16)|
|
|
|
|
(p->hl[3]<<8)|
|
|
|
|
(p->hl[2])) ^
|
|
|
|
off;
|
|
|
|
break;
|
|
|
|
case FILE_OPADD:
|
|
|
|
offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
(p->hl[0]<<16)|
|
|
|
|
(p->hl[3]<<8)|
|
|
|
|
(p->hl[2])) +
|
|
|
|
off;
|
|
|
|
break;
|
|
|
|
case FILE_OPMINUS:
|
|
|
|
offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
(p->hl[0]<<16)|
|
|
|
|
(p->hl[3]<<8)|
|
|
|
|
(p->hl[2])) -
|
|
|
|
off;
|
|
|
|
break;
|
|
|
|
case FILE_OPMULTIPLY:
|
|
|
|
offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
(p->hl[0]<<16)|
|
|
|
|
(p->hl[3]<<8)|
|
|
|
|
(p->hl[2])) *
|
|
|
|
off;
|
|
|
|
break;
|
|
|
|
case FILE_OPDIVIDE:
|
|
|
|
offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
(p->hl[0]<<16)|
|
|
|
|
(p->hl[3]<<8)|
|
|
|
|
(p->hl[2])) /
|
|
|
|
off;
|
|
|
|
break;
|
|
|
|
case FILE_OPMODULO:
|
|
|
|
offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
(p->hl[0]<<16)|
|
|
|
|
(p->hl[3]<<8)|
|
|
|
|
(p->hl[2])) %
|
|
|
|
off;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
(p->hl[0]<<16)|
|
|
|
|
(p->hl[3]<<8)|
|
|
|
|
(p->hl[2]));
|
|
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
|
|
offset = ~offset;
|
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_LONG:
|
2006-06-19 07:52:15 +00:00
|
|
|
if (nbytes < (offset + 4))
|
|
|
|
return 0;
|
|
|
|
if (off) {
|
2007-05-24 21:59:38 +00:00
|
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPAND:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->l & off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPOR:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->l | off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPXOR:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->l ^ off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPADD:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->l + off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMINUS:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->l - off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMULTIPLY:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->l * off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPDIVIDE:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->l / off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_OPMODULO:
|
2006-06-19 07:52:15 +00:00
|
|
|
offset = p->l % off;
|
2001-07-30 03:09:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
} else
|
|
|
|
offset = p->l;
|
|
|
|
if (m->in_op & FILE_OPINVERSE)
|
2001-07-30 03:09:46 +00:00
|
|
|
offset = ~offset;
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
switch (m->in_type) {
|
|
|
|
case FILE_LEID3:
|
|
|
|
case FILE_BEID3:
|
|
|
|
offset = ((((offset >> 0) & 0x7f) << 0) |
|
|
|
|
(((offset >> 8) & 0x7f) << 7) |
|
|
|
|
(((offset >> 16) & 0x7f) << 14) |
|
|
|
|
(((offset >> 24) & 0x7f) << 21)) + 10;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->flag & INDIROFFADD) {
|
2007-05-24 21:59:38 +00:00
|
|
|
offset += ms->c.li[cont_level-1].off;
|
2009-05-02 06:25:51 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
|
2004-12-28 04:31:47 +00:00
|
|
|
return -1;
|
2007-05-24 16:12:23 +00:00
|
|
|
ms->offset = offset;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0) {
|
|
|
|
mdebug(offset, (char *)(void *)p,
|
|
|
|
sizeof(union VALUETYPE));
|
2009-05-02 06:25:51 +00:00
|
|
|
#ifndef COMPILE_ONLY
|
2004-08-09 08:45:41 +00:00
|
|
|
file_mdump(m);
|
2009-05-02 06:25:51 +00:00
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
|
|
|
|
/* Verify we have enough data to match magic type */
|
|
|
|
switch (m->type) {
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_BYTE:
|
|
|
|
if (nbytes < (offset + 1)) /* should alway be true */
|
|
|
|
return 0;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_SHORT:
|
|
|
|
case FILE_BESHORT:
|
|
|
|
case FILE_LESHORT:
|
|
|
|
if (nbytes < (offset + 2))
|
|
|
|
return 0;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_LONG:
|
|
|
|
case FILE_BELONG:
|
|
|
|
case FILE_LELONG:
|
|
|
|
case FILE_MELONG:
|
|
|
|
case FILE_DATE:
|
|
|
|
case FILE_BEDATE:
|
|
|
|
case FILE_LEDATE:
|
|
|
|
case FILE_MEDATE:
|
|
|
|
case FILE_LDATE:
|
|
|
|
case FILE_BELDATE:
|
|
|
|
case FILE_LELDATE:
|
|
|
|
case FILE_MELDATE:
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_FLOAT:
|
|
|
|
case FILE_BEFLOAT:
|
|
|
|
case FILE_LEFLOAT:
|
2007-05-24 21:59:38 +00:00
|
|
|
if (nbytes < (offset + 4))
|
|
|
|
return 0;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_DOUBLE:
|
|
|
|
case FILE_BEDOUBLE:
|
|
|
|
case FILE_LEDOUBLE:
|
|
|
|
if (nbytes < (offset + 8))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_STRING:
|
|
|
|
case FILE_PSTRING:
|
|
|
|
case FILE_SEARCH:
|
|
|
|
if (nbytes < (offset + m->vallen))
|
|
|
|
return 0;
|
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_REGEX:
|
|
|
|
if (nbytes < offset)
|
|
|
|
return 0;
|
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
case FILE_INDIRECT:
|
|
|
|
if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
|
|
|
|
file_printf(ms, m->desc) == -1)
|
|
|
|
return -1;
|
|
|
|
if (nbytes < offset)
|
|
|
|
return 0;
|
|
|
|
return file_softmagic(ms, s + offset, nbytes - offset,
|
|
|
|
BINTEST);
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_DEFAULT: /* nothing to check */
|
|
|
|
default:
|
|
|
|
break;
|
2006-06-19 07:52:15 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
if (!mconvert(ms, m))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
private uint64_t
|
|
|
|
file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Convert the source args to unsigned here so that (1) the
|
|
|
|
* compare will be unsigned as it is in strncmp() and (2) so
|
|
|
|
* the ctype functions will work correctly without extra
|
|
|
|
* casting.
|
|
|
|
*/
|
|
|
|
const unsigned char *a = (const unsigned char *)s1;
|
|
|
|
const unsigned char *b = (const unsigned char *)s2;
|
|
|
|
uint64_t v;
|
|
|
|
|
|
|
|
/*
|
2009-01-02 02:57:16 +00:00
|
|
|
* What we want here is v = strncmp(s1, s2, len),
|
|
|
|
* but ignoring any nulls.
|
2007-05-24 21:59:38 +00:00
|
|
|
*/
|
|
|
|
v = 0;
|
|
|
|
if (0L == flags) { /* normal string: do it fast */
|
|
|
|
while (len-- > 0)
|
|
|
|
if ((v = *b++ - *a++) != '\0')
|
2009-05-02 06:25:51 +00:00
|
|
|
break;
|
2007-05-24 21:59:38 +00:00
|
|
|
}
|
|
|
|
else { /* combine the others */
|
|
|
|
while (len-- > 0) {
|
|
|
|
if ((flags & STRING_IGNORE_LOWERCASE) &&
|
|
|
|
islower(*a)) {
|
|
|
|
if ((v = tolower(*b++) - *a++) != '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if ((flags & STRING_IGNORE_UPPERCASE) &&
|
|
|
|
isupper(*a)) {
|
|
|
|
if ((v = toupper(*b++) - *a++) != '\0')
|
|
|
|
break;
|
|
|
|
}
|
2009-05-02 06:25:51 +00:00
|
|
|
else if ((flags & STRING_COMPACT_BLANK) &&
|
|
|
|
isspace(*a)) {
|
2007-05-24 21:59:38 +00:00
|
|
|
a++;
|
|
|
|
if (isspace(*b++)) {
|
|
|
|
while (isspace(*b))
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
v = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((flags & STRING_COMPACT_OPTIONAL_BLANK) &&
|
|
|
|
isspace(*a)) {
|
|
|
|
a++;
|
|
|
|
while (isspace(*b))
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((v = *b++ - *a++) != '\0')
|
|
|
|
break;
|
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
private uint64_t
|
|
|
|
file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* XXX - The 16-bit string compare probably needs to be done
|
|
|
|
* differently, especially if the flags are to be supported.
|
|
|
|
* At the moment, I am unsure.
|
|
|
|
*/
|
|
|
|
flags = 0;
|
|
|
|
return file_strncmp(a, b, len, flags);
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private int
|
2007-05-24 21:59:38 +00:00
|
|
|
magiccheck(struct magic_set *ms, struct magic *m)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2007-05-24 16:12:23 +00:00
|
|
|
uint64_t l = m->value.q;
|
|
|
|
uint64_t v;
|
2008-01-13 20:35:16 +00:00
|
|
|
float fl, fv;
|
|
|
|
double dl, dv;
|
1994-09-03 19:16:28 +00:00
|
|
|
int matched;
|
2007-05-24 21:59:38 +00:00
|
|
|
union VALUETYPE *p = &ms->ms_value;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
switch (m->type) {
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_BYTE:
|
1994-09-03 19:16:28 +00:00
|
|
|
v = p->b;
|
|
|
|
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 = p->h;
|
|
|
|
break;
|
|
|
|
|
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:
|
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:
|
1994-09-03 19:16:28 +00:00
|
|
|
v = p->l;
|
|
|
|
break;
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
case FILE_QUAD:
|
|
|
|
case FILE_LEQUAD:
|
|
|
|
case FILE_BEQUAD:
|
|
|
|
case FILE_QDATE:
|
|
|
|
case FILE_BEQDATE:
|
|
|
|
case FILE_LEQDATE:
|
|
|
|
case FILE_QLDATE:
|
|
|
|
case FILE_BEQLDATE:
|
|
|
|
case FILE_LEQLDATE:
|
|
|
|
v = p->q;
|
|
|
|
break;
|
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case FILE_FLOAT:
|
|
|
|
case FILE_BEFLOAT:
|
|
|
|
case FILE_LEFLOAT:
|
|
|
|
fl = m->value.f;
|
|
|
|
fv = p->f;
|
|
|
|
switch (m->reln) {
|
|
|
|
case 'x':
|
|
|
|
matched = 1;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case '!':
|
|
|
|
matched = fv != fl;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case '=':
|
|
|
|
matched = fv == fl;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case '>':
|
|
|
|
matched = fv > fl;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case '<':
|
|
|
|
matched = fv < fl;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
default:
|
|
|
|
matched = 0;
|
2009-05-02 06:25:51 +00:00
|
|
|
file_magerror(ms, "cannot happen with float: invalid relation `%c'",
|
|
|
|
m->reln);
|
2008-01-13 20:35:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return matched;
|
|
|
|
|
|
|
|
case FILE_DOUBLE:
|
|
|
|
case FILE_BEDOUBLE:
|
|
|
|
case FILE_LEDOUBLE:
|
|
|
|
dl = m->value.d;
|
|
|
|
dv = p->d;
|
|
|
|
switch (m->reln) {
|
|
|
|
case 'x':
|
|
|
|
matched = 1;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case '!':
|
|
|
|
matched = dv != dl;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case '=':
|
|
|
|
matched = dv == dl;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case '>':
|
|
|
|
matched = dv > dl;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
case '<':
|
|
|
|
matched = dv < dl;
|
|
|
|
break;
|
2009-05-02 06:25:51 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
default:
|
|
|
|
matched = 0;
|
|
|
|
file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return matched;
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_DEFAULT:
|
|
|
|
l = 0;
|
|
|
|
v = 0;
|
|
|
|
break;
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
case FILE_STRING:
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_PSTRING:
|
|
|
|
l = 0;
|
|
|
|
v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
|
|
|
|
break;
|
|
|
|
|
2004-12-28 04:31:47 +00:00
|
|
|
case FILE_BESTRING16:
|
|
|
|
case FILE_LESTRING16:
|
2007-05-24 21:59:38 +00:00
|
|
|
l = 0;
|
|
|
|
v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
|
|
|
|
size_t slen;
|
|
|
|
size_t idx;
|
|
|
|
|
|
|
|
if (ms->search.s == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
slen = MIN(m->vallen, sizeof(m->value.s));
|
2000-11-05 08:33:55 +00:00
|
|
|
l = 0;
|
1994-09-03 19:16:28 +00:00
|
|
|
v = 0;
|
2007-05-24 21:59:38 +00:00
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
|
2007-05-24 21:59:38 +00:00
|
|
|
if (slen + idx > ms->search.s_len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
|
|
|
|
if (v == 0) { /* found match */
|
2009-01-02 02:57:16 +00:00
|
|
|
ms->search.offset += idx;
|
2007-05-24 21:59:38 +00:00
|
|
|
break;
|
2000-11-05 08:33:55 +00:00
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-11-05 08:33:55 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
case FILE_REGEX: {
|
2002-09-16 05:54:26 +00:00
|
|
|
int rc;
|
|
|
|
regex_t rx;
|
|
|
|
char errmsg[512];
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
if (ms->search.s == NULL)
|
2006-06-19 07:52:15 +00:00
|
|
|
return 0;
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
l = 0;
|
2006-06-19 07:52:15 +00:00
|
|
|
rc = regcomp(&rx, m->value.s,
|
2007-05-24 21:59:38 +00:00
|
|
|
REG_EXTENDED|REG_NEWLINE|
|
|
|
|
((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
|
2002-09-16 05:54:26 +00:00
|
|
|
if (rc) {
|
2007-05-24 21:59:38 +00:00
|
|
|
(void)regerror(rc, &rx, errmsg, sizeof(errmsg));
|
|
|
|
file_magerror(ms, "regex error %d, (%s)",
|
|
|
|
rc, errmsg);
|
|
|
|
v = (uint64_t)-1;
|
2002-09-16 05:54:26 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
else {
|
|
|
|
regmatch_t pmatch[1];
|
|
|
|
#ifndef REG_STARTEND
|
|
|
|
#define REG_STARTEND 0
|
|
|
|
size_t l = ms->search.s_len - 1;
|
|
|
|
char c = ms->search.s[l];
|
|
|
|
((char *)(intptr_t)ms->search.s)[l] = '\0';
|
|
|
|
#else
|
|
|
|
pmatch[0].rm_so = 0;
|
|
|
|
pmatch[0].rm_eo = ms->search.s_len;
|
|
|
|
#endif
|
|
|
|
rc = regexec(&rx, (const char *)ms->search.s,
|
|
|
|
1, pmatch, REG_STARTEND);
|
|
|
|
#if REG_STARTEND == 0
|
|
|
|
((char *)(intptr_t)ms->search.s)[l] = c;
|
|
|
|
#endif
|
|
|
|
switch (rc) {
|
|
|
|
case 0:
|
|
|
|
ms->search.s += (int)pmatch[0].rm_so;
|
|
|
|
ms->search.offset += (size_t)pmatch[0].rm_so;
|
|
|
|
ms->search.rm_len =
|
|
|
|
(size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
|
|
|
|
v = 0;
|
2006-06-19 07:52:15 +00:00
|
|
|
break;
|
2007-05-24 21:59:38 +00:00
|
|
|
|
|
|
|
case REG_NOMATCH:
|
2007-05-24 16:12:23 +00:00
|
|
|
v = 1;
|
2006-06-19 07:52:15 +00:00
|
|
|
break;
|
2007-05-24 21:59:38 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
(void)regerror(rc, &rx, errmsg, sizeof(errmsg));
|
|
|
|
file_magerror(ms, "regexec error %d, (%s)",
|
|
|
|
rc, errmsg);
|
|
|
|
v = (uint64_t)-1;
|
|
|
|
break;
|
2007-05-24 16:12:23 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
regfree(&rx);
|
2006-06-19 07:52:15 +00:00
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
if (v == (uint64_t)-1)
|
|
|
|
return -1;
|
2006-06-19 07:52:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-05-02 06:25:51 +00:00
|
|
|
case FILE_INDIRECT:
|
|
|
|
return 1;
|
1994-09-03 19:16:28 +00:00
|
|
|
default:
|
2007-05-24 21:59:38 +00:00
|
|
|
file_magerror(ms, "invalid type %d in magiccheck()", m->type);
|
2004-08-09 08:45:41 +00:00
|
|
|
return -1;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
v = file_signextend(ms, m, v);
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
switch (m->reln) {
|
|
|
|
case 'x':
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
(void) fprintf(stderr, "%llu == *any* = 1\n",
|
|
|
|
(unsigned long long)v);
|
1994-09-03 19:16:28 +00:00
|
|
|
matched = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '!':
|
|
|
|
matched = v != l;
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
(void) fprintf(stderr, "%llu != %llu = %d\n",
|
|
|
|
(unsigned long long)v, (unsigned long long)l,
|
|
|
|
matched);
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '=':
|
|
|
|
matched = v == l;
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
(void) fprintf(stderr, "%llu == %llu = %d\n",
|
|
|
|
(unsigned long long)v, (unsigned long long)l,
|
|
|
|
matched);
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '>':
|
|
|
|
if (m->flag & UNSIGNED) {
|
|
|
|
matched = v > l;
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
(void) fprintf(stderr, "%llu > %llu = %d\n",
|
|
|
|
(unsigned long long)v,
|
|
|
|
(unsigned long long)l, matched);
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-05-24 21:59:38 +00:00
|
|
|
matched = (int64_t) v > (int64_t) l;
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
(void) fprintf(stderr, "%lld > %lld = %d\n",
|
|
|
|
(long long)v, (long long)l, matched);
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '<':
|
|
|
|
if (m->flag & UNSIGNED) {
|
|
|
|
matched = v < l;
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
(void) fprintf(stderr, "%llu < %llu = %d\n",
|
|
|
|
(unsigned long long)v,
|
|
|
|
(unsigned long long)l, matched);
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-05-24 21:59:38 +00:00
|
|
|
matched = (int64_t) v < (int64_t) l;
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
(void) fprintf(stderr, "%lld < %lld = %d\n",
|
|
|
|
(long long)v, (long long)l, matched);
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&':
|
|
|
|
matched = (v & l) == l;
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
(void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n",
|
|
|
|
(unsigned long long)v, (unsigned long long)l,
|
|
|
|
(unsigned long long)l, matched);
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '^':
|
|
|
|
matched = (v & l) != l;
|
2004-08-09 08:45:41 +00:00
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
2007-05-24 16:12:23 +00:00
|
|
|
(void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n",
|
|
|
|
(unsigned long long)v, (unsigned long long)l,
|
|
|
|
(unsigned long long)l, matched);
|
1994-09-03 19:16:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
matched = 0;
|
2007-05-24 21:59:38 +00:00
|
|
|
file_magerror(ms, "cannot happen: invalid relation `%c'",
|
2004-08-09 08:45:41 +00:00
|
|
|
m->reln);
|
|
|
|
return -1;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return matched;
|
|
|
|
}
|
2007-05-24 16:12:23 +00:00
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
private int
|
|
|
|
handle_annotation(struct magic_set *ms, struct magic *m)
|
|
|
|
{
|
|
|
|
if (ms->flags & MAGIC_APPLE) {
|
|
|
|
if (file_printf(ms, "%.8s", m->apple) == -1)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
|
|
|
|
if (file_printf(ms, "%s", m->mimetype) == -1)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
private int
|
|
|
|
print_sep(struct magic_set *ms, int firstline)
|
|
|
|
{
|
2009-05-02 06:25:51 +00:00
|
|
|
if (ms->flags & MAGIC_MIME)
|
|
|
|
return 0;
|
2007-05-24 16:12:23 +00:00
|
|
|
if (firstline)
|
|
|
|
return 0;
|
|
|
|
/*
|
2009-05-02 06:25:51 +00:00
|
|
|
* we found another match
|
2007-05-24 16:12:23 +00:00
|
|
|
* put a newline and '-' to do some simple formatting
|
|
|
|
*/
|
|
|
|
return file_printf(ms, "\n- ");
|
|
|
|
}
|