|
@@ -32,11 +32,10 @@
|
|
#include "file.h"
|
|
#include "file.h"
|
|
|
|
|
|
#ifndef lint
|
|
#ifndef lint
|
|
-FILE_RCSID("@(#)$File: softmagic.c,v 1.180 2014/03/15 21:47:40 christos Exp $")
|
|
|
|
|
|
+FILE_RCSID("@(#)$File: softmagic.c,v 1.191 2014/06/04 17:36:34 christos Exp $")
|
|
#endif /* lint */
|
|
#endif /* lint */
|
|
|
|
|
|
#include "magic.h"
|
|
#include "magic.h"
|
|
-#define F(a, b) fmtcheck((a), (b))
|
|
|
|
#include <assert.h>
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <ctype.h>
|
|
@@ -58,7 +57,7 @@ private int32_t mprint(struct magic_set *, struct magic *);
|
|
private int32_t moffset(struct magic_set *, struct magic *);
|
|
private int32_t moffset(struct magic_set *, struct magic *);
|
|
private void mdebug(uint32_t, const char *, size_t);
|
|
private void mdebug(uint32_t, const char *, size_t);
|
|
private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
|
|
private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
|
|
- const unsigned char *, uint32_t, size_t, size_t);
|
|
|
|
|
|
+ const unsigned char *, uint32_t, size_t, struct magic *);
|
|
private int mconvert(struct magic_set *, struct magic *, int);
|
|
private int mconvert(struct magic_set *, struct magic *, int);
|
|
private int print_sep(struct magic_set *, int);
|
|
private int print_sep(struct magic_set *, int);
|
|
private int handle_annotation(struct magic_set *, struct magic *);
|
|
private int handle_annotation(struct magic_set *, struct magic *);
|
|
@@ -88,6 +87,25 @@ file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#define FILE_FMTDEBUG
|
|
|
|
+#ifdef FILE_FMTDEBUG
|
|
|
|
+#define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
|
|
|
|
+
|
|
|
|
+private const char * __attribute__((__format_arg__(3)))
|
|
|
|
+file_fmtcheck(struct magic_set *ms, const struct magic *m, const char *def,
|
|
|
|
+ const char *file, size_t line)
|
|
|
|
+{
|
|
|
|
+ const char *ptr = fmtcheck(m->desc, def);
|
|
|
|
+ if (ptr == def)
|
|
|
|
+ file_magerror(ms,
|
|
|
|
+ "%s, %zu: format `%s' does not match with `%s'",
|
|
|
|
+ file, line, m->desc, def);
|
|
|
|
+ return ptr;
|
|
|
|
+}
|
|
|
|
+#else
|
|
|
|
+#define F(a, b, c) fmtcheck((b)->desc, (c))
|
|
|
|
+#endif
|
|
|
|
+
|
|
/*
|
|
/*
|
|
* Go through the whole list, stopping if you find a match. Process all
|
|
* Go through the whole list, stopping if you find a match. Process all
|
|
* the continuations of that match before returning.
|
|
* the continuations of that match before returning.
|
|
@@ -217,8 +235,8 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
|
|
if (file_check_mem(ms, ++cont_level) == -1)
|
|
if (file_check_mem(ms, ++cont_level) == -1)
|
|
return -1;
|
|
return -1;
|
|
|
|
|
|
- while (magic[magindex+1].cont_level != 0 &&
|
|
|
|
- ++magindex < nmagic) {
|
|
|
|
|
|
+ while (++magindex < nmagic &&
|
|
|
|
+ magic[magindex].cont_level != 0) {
|
|
m = &magic[magindex];
|
|
m = &magic[magindex];
|
|
ms->line = m->lineno; /* for messages */
|
|
ms->line = m->lineno; /* for messages */
|
|
|
|
|
|
@@ -346,30 +364,20 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
|
|
private int
|
|
private int
|
|
check_fmt(struct magic_set *ms, struct magic *m)
|
|
check_fmt(struct magic_set *ms, struct magic *m)
|
|
{
|
|
{
|
|
- regex_t rx;
|
|
|
|
|
|
+ file_regex_t rx;
|
|
int rc, rv = -1;
|
|
int rc, rv = -1;
|
|
- char *old_lc_ctype;
|
|
|
|
|
|
|
|
if (strchr(m->desc, '%') == NULL)
|
|
if (strchr(m->desc, '%') == NULL)
|
|
return 0;
|
|
return 0;
|
|
|
|
|
|
- old_lc_ctype = setlocale(LC_CTYPE, NULL);
|
|
|
|
- assert(old_lc_ctype != NULL);
|
|
|
|
- old_lc_ctype = strdup(old_lc_ctype);
|
|
|
|
- assert(old_lc_ctype != NULL);
|
|
|
|
- (void)setlocale(LC_CTYPE, "C");
|
|
|
|
- rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
|
|
|
|
|
|
+ rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
|
|
if (rc) {
|
|
if (rc) {
|
|
- char errmsg[512];
|
|
|
|
- (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
|
|
|
|
- file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
|
|
|
|
|
|
+ file_regerror(&rx, rc, ms);
|
|
} else {
|
|
} else {
|
|
- rc = regexec(&rx, m->desc, 0, 0, 0);
|
|
|
|
- regfree(&rx);
|
|
|
|
|
|
+ rc = file_regexec(&rx, m->desc, 0, 0, 0);
|
|
rv = !rc;
|
|
rv = !rc;
|
|
}
|
|
}
|
|
- (void)setlocale(LC_CTYPE, old_lc_ctype);
|
|
|
|
- free(old_lc_ctype);
|
|
|
|
|
|
+ file_regfree(&rx);
|
|
return rv;
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -409,13 +417,13 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case -1:
|
|
case -1:
|
|
return -1;
|
|
return -1;
|
|
case 1:
|
|
case 1:
|
|
- (void)snprintf(buf, sizeof(buf), "%c",
|
|
|
|
|
|
+ (void)snprintf(buf, sizeof(buf), "%d",
|
|
(unsigned char)v);
|
|
(unsigned char)v);
|
|
- if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
- if (file_printf(ms, F(m->desc, "%c"),
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%d"),
|
|
(unsigned char) v) == -1)
|
|
(unsigned char) v) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
@@ -431,13 +439,13 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case -1:
|
|
case -1:
|
|
return -1;
|
|
return -1;
|
|
case 1:
|
|
case 1:
|
|
- (void)snprintf(buf, sizeof(buf), "%hu",
|
|
|
|
|
|
+ (void)snprintf(buf, sizeof(buf), "%u",
|
|
(unsigned short)v);
|
|
(unsigned short)v);
|
|
- if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
- if (file_printf(ms, F(m->desc, "%hu"),
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%u"),
|
|
(unsigned short) v) == -1)
|
|
(unsigned short) v) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
@@ -454,13 +462,12 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case -1:
|
|
case -1:
|
|
return -1;
|
|
return -1;
|
|
case 1:
|
|
case 1:
|
|
- (void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v);
|
|
|
|
- if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
|
|
|
|
|
|
+ (void)snprintf(buf, sizeof(buf), "%u", (uint32_t) v);
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
- if (file_printf(ms, F(m->desc, "%u"),
|
|
|
|
- (uint32_t) v) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%u"), (uint32_t) v) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -475,13 +482,13 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case -1:
|
|
case -1:
|
|
return -1;
|
|
return -1;
|
|
case 1:
|
|
case 1:
|
|
- (void)snprintf(buf, sizeof(buf), "%llu",
|
|
|
|
|
|
+ (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
|
|
(unsigned long long)v);
|
|
(unsigned long long)v);
|
|
- if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
- if (file_printf(ms, F(m->desc, "%llu"),
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%" INT64_T_FORMAT "u"),
|
|
(unsigned long long) v) == -1)
|
|
(unsigned long long) v) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
@@ -494,7 +501,7 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case FILE_BESTRING16:
|
|
case FILE_BESTRING16:
|
|
case FILE_LESTRING16:
|
|
case FILE_LESTRING16:
|
|
if (m->reln == '=' || m->reln == '!') {
|
|
if (m->reln == '=' || m->reln == '!') {
|
|
- if (file_printf(ms, F(m->desc, "%s"), m->value.s) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"), m->value.s) == -1)
|
|
return -1;
|
|
return -1;
|
|
t = ms->offset + m->vallen;
|
|
t = ms->offset + m->vallen;
|
|
}
|
|
}
|
|
@@ -520,7 +527,7 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
*++last = '\0';
|
|
*++last = '\0';
|
|
}
|
|
}
|
|
|
|
|
|
- if (file_printf(ms, F(m->desc, "%s"), str) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"), str) == -1)
|
|
return -1;
|
|
return -1;
|
|
|
|
|
|
if (m->type == FILE_PSTRING)
|
|
if (m->type == FILE_PSTRING)
|
|
@@ -532,8 +539,8 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case FILE_BEDATE:
|
|
case FILE_BEDATE:
|
|
case FILE_LEDATE:
|
|
case FILE_LEDATE:
|
|
case FILE_MEDATE:
|
|
case FILE_MEDATE:
|
|
- if (file_printf(ms, F(m->desc, "%s"),
|
|
|
|
- file_fmttime(p->l, FILE_T_LOCAL, tbuf)) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"),
|
|
|
|
+ file_fmttime(p->l + m->num_mask, FILE_T_LOCAL, tbuf)) == -1)
|
|
return -1;
|
|
return -1;
|
|
t = ms->offset + sizeof(uint32_t);
|
|
t = ms->offset + sizeof(uint32_t);
|
|
break;
|
|
break;
|
|
@@ -542,8 +549,8 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case FILE_BELDATE:
|
|
case FILE_BELDATE:
|
|
case FILE_LELDATE:
|
|
case FILE_LELDATE:
|
|
case FILE_MELDATE:
|
|
case FILE_MELDATE:
|
|
- if (file_printf(ms, F(m->desc, "%s"),
|
|
|
|
- file_fmttime(p->l, 0, tbuf)) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"),
|
|
|
|
+ file_fmttime(p->l + m->num_mask, 0, tbuf)) == -1)
|
|
return -1;
|
|
return -1;
|
|
t = ms->offset + sizeof(uint32_t);
|
|
t = ms->offset + sizeof(uint32_t);
|
|
break;
|
|
break;
|
|
@@ -551,8 +558,8 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case FILE_QDATE:
|
|
case FILE_QDATE:
|
|
case FILE_BEQDATE:
|
|
case FILE_BEQDATE:
|
|
case FILE_LEQDATE:
|
|
case FILE_LEQDATE:
|
|
- if (file_printf(ms, F(m->desc, "%s"),
|
|
|
|
- file_fmttime(p->q, FILE_T_LOCAL, tbuf)) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"),
|
|
|
|
+ file_fmttime(p->q + m->num_mask, FILE_T_LOCAL, tbuf)) == -1)
|
|
return -1;
|
|
return -1;
|
|
t = ms->offset + sizeof(uint64_t);
|
|
t = ms->offset + sizeof(uint64_t);
|
|
break;
|
|
break;
|
|
@@ -560,8 +567,8 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case FILE_QLDATE:
|
|
case FILE_QLDATE:
|
|
case FILE_BEQLDATE:
|
|
case FILE_BEQLDATE:
|
|
case FILE_LEQLDATE:
|
|
case FILE_LEQLDATE:
|
|
- if (file_printf(ms, F(m->desc, "%s"),
|
|
|
|
- file_fmttime(p->q, 0, tbuf)) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"),
|
|
|
|
+ file_fmttime(p->q + m->num_mask, 0, tbuf)) == -1)
|
|
return -1;
|
|
return -1;
|
|
t = ms->offset + sizeof(uint64_t);
|
|
t = ms->offset + sizeof(uint64_t);
|
|
break;
|
|
break;
|
|
@@ -569,8 +576,8 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
case FILE_QWDATE:
|
|
case FILE_QWDATE:
|
|
case FILE_BEQWDATE:
|
|
case FILE_BEQWDATE:
|
|
case FILE_LEQWDATE:
|
|
case FILE_LEQWDATE:
|
|
- if (file_printf(ms, F(m->desc, "%s"),
|
|
|
|
- file_fmttime(p->q, FILE_T_WINDOWS, tbuf)) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"),
|
|
|
|
+ file_fmttime(p->q + m->num_mask, FILE_T_WINDOWS, tbuf)) == -1)
|
|
return -1;
|
|
return -1;
|
|
t = ms->offset + sizeof(uint64_t);
|
|
t = ms->offset + sizeof(uint64_t);
|
|
break;
|
|
break;
|
|
@@ -584,11 +591,11 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
return -1;
|
|
return -1;
|
|
case 1:
|
|
case 1:
|
|
(void)snprintf(buf, sizeof(buf), "%g", vf);
|
|
(void)snprintf(buf, sizeof(buf), "%g", vf);
|
|
- if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
- if (file_printf(ms, F(m->desc, "%g"), vf) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%g"), vf) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -604,11 +611,11 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
return -1;
|
|
return -1;
|
|
case 1:
|
|
case 1:
|
|
(void)snprintf(buf, sizeof(buf), "%g", vd);
|
|
(void)snprintf(buf, sizeof(buf), "%g", vd);
|
|
- if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
- if (file_printf(ms, F(m->desc, "%g"), vd) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%g"), vd) == -1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -624,7 +631,7 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
file_oomem(ms, ms->search.rm_len);
|
|
file_oomem(ms, ms->search.rm_len);
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
- rval = file_printf(ms, F(m->desc, "%s"), cp);
|
|
|
|
|
|
+ rval = file_printf(ms, F(ms, m, "%s"), cp);
|
|
free(cp);
|
|
free(cp);
|
|
|
|
|
|
if (rval == -1)
|
|
if (rval == -1)
|
|
@@ -638,7 +645,7 @@ mprint(struct magic_set *ms, struct magic *m)
|
|
}
|
|
}
|
|
|
|
|
|
case FILE_SEARCH:
|
|
case FILE_SEARCH:
|
|
- if (file_printf(ms, F(m->desc, "%s"), m->value.s) == -1)
|
|
|
|
|
|
+ if (file_printf(ms, F(ms, m, "%s"), m->value.s) == -1)
|
|
return -1;
|
|
return -1;
|
|
if ((m->str_flags & REGEX_OFFSET_START))
|
|
if ((m->str_flags & REGEX_OFFSET_START))
|
|
t = ms->search.offset;
|
|
t = ms->search.offset;
|
|
@@ -905,8 +912,9 @@ private int
|
|
mconvert(struct magic_set *ms, struct magic *m, int flip)
|
|
mconvert(struct magic_set *ms, struct magic *m, int flip)
|
|
{
|
|
{
|
|
union VALUETYPE *p = &ms->ms_value;
|
|
union VALUETYPE *p = &ms->ms_value;
|
|
|
|
+ uint8_t type;
|
|
|
|
|
|
- switch (cvt_flip(m->type, flip)) {
|
|
|
|
|
|
+ switch (type = cvt_flip(m->type, flip)) {
|
|
case FILE_BYTE:
|
|
case FILE_BYTE:
|
|
cvt_8(p, m);
|
|
cvt_8(p, m);
|
|
return 1;
|
|
return 1;
|
|
@@ -932,10 +940,18 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
case FILE_PSTRING: {
|
|
case FILE_PSTRING: {
|
|
- char *ptr1 = p->s, *ptr2 = ptr1 + file_pstring_length_size(m);
|
|
|
|
|
|
+ size_t sz = file_pstring_length_size(m);
|
|
|
|
+ char *ptr1 = p->s, *ptr2 = ptr1 + sz;
|
|
size_t len = file_pstring_get_length(m, ptr1);
|
|
size_t len = file_pstring_get_length(m, ptr1);
|
|
- if (len >= sizeof(p->s))
|
|
|
|
- len = sizeof(p->s) - 1;
|
|
|
|
|
|
+ if (len >= sizeof(p->s)) {
|
|
|
|
+ /*
|
|
|
|
+ * The size of the pascal string length (sz)
|
|
|
|
+ * is 1, 2, or 4. We need at least 1 byte for NUL
|
|
|
|
+ * termination, but we've already truncated the
|
|
|
|
+ * string by p->s, so we need to deduct sz.
|
|
|
|
+ */
|
|
|
|
+ len = sizeof(p->s) - sz;
|
|
|
|
+ }
|
|
while (len--)
|
|
while (len--)
|
|
*ptr1++ = *ptr2++;
|
|
*ptr1++ = *ptr2++;
|
|
*ptr1 = '\0';
|
|
*ptr1 = '\0';
|
|
@@ -950,7 +966,8 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
|
|
case FILE_BELDATE:
|
|
case FILE_BELDATE:
|
|
p->l = (int32_t)
|
|
p->l = (int32_t)
|
|
((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
|
|
((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
|
|
- cvt_32(p, m);
|
|
|
|
|
|
+ if (type == FILE_BELONG)
|
|
|
|
+ cvt_32(p, m);
|
|
return 1;
|
|
return 1;
|
|
case FILE_BEQUAD:
|
|
case FILE_BEQUAD:
|
|
case FILE_BEQDATE:
|
|
case FILE_BEQDATE:
|
|
@@ -961,7 +978,8 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
|
|
((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
|
|
((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[4]<<24)|((uint64_t)p->hq[5]<<16)|
|
|
((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
|
|
((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
|
|
- cvt_64(p, m);
|
|
|
|
|
|
+ if (type == FILE_BEQUAD)
|
|
|
|
+ cvt_64(p, m);
|
|
return 1;
|
|
return 1;
|
|
case FILE_LESHORT:
|
|
case FILE_LESHORT:
|
|
p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
|
|
p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
|
|
@@ -972,7 +990,8 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
|
|
case FILE_LELDATE:
|
|
case FILE_LELDATE:
|
|
p->l = (int32_t)
|
|
p->l = (int32_t)
|
|
((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
|
|
((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
|
|
- cvt_32(p, m);
|
|
|
|
|
|
+ if (type == FILE_LELONG)
|
|
|
|
+ cvt_32(p, m);
|
|
return 1;
|
|
return 1;
|
|
case FILE_LEQUAD:
|
|
case FILE_LEQUAD:
|
|
case FILE_LEQDATE:
|
|
case FILE_LEQDATE:
|
|
@@ -983,14 +1002,16 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
|
|
((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
|
|
((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[3]<<24)|((uint64_t)p->hq[2]<<16)|
|
|
((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
|
|
((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
|
|
- cvt_64(p, m);
|
|
|
|
|
|
+ if (type == FILE_LEQUAD)
|
|
|
|
+ cvt_64(p, m);
|
|
return 1;
|
|
return 1;
|
|
case FILE_MELONG:
|
|
case FILE_MELONG:
|
|
case FILE_MEDATE:
|
|
case FILE_MEDATE:
|
|
case FILE_MELDATE:
|
|
case FILE_MELDATE:
|
|
p->l = (int32_t)
|
|
p->l = (int32_t)
|
|
((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
|
|
((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
|
|
- cvt_32(p, m);
|
|
|
|
|
|
+ if (type == FILE_MELONG)
|
|
|
|
+ cvt_32(p, m);
|
|
return 1;
|
|
return 1;
|
|
case FILE_FLOAT:
|
|
case FILE_FLOAT:
|
|
cvt_float(p, m);
|
|
cvt_float(p, m);
|
|
@@ -1047,7 +1068,7 @@ mdebug(uint32_t offset, const char *str, size_t len)
|
|
|
|
|
|
private int
|
|
private int
|
|
mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
|
|
mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
|
|
- const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
|
|
|
|
|
|
+ const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
|
|
{
|
|
{
|
|
/*
|
|
/*
|
|
* Note: FILE_SEARCH and FILE_REGEX do not actually copy
|
|
* Note: FILE_SEARCH and FILE_REGEX do not actually copy
|
|
@@ -1067,15 +1088,29 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
|
|
const char *last; /* end of search region */
|
|
const char *last; /* end of search region */
|
|
const char *buf; /* start of search region */
|
|
const char *buf; /* start of search region */
|
|
const char *end;
|
|
const char *end;
|
|
- size_t lines;
|
|
|
|
|
|
+ size_t lines, linecnt, bytecnt;
|
|
|
|
|
|
if (s == NULL) {
|
|
if (s == NULL) {
|
|
ms->search.s_len = 0;
|
|
ms->search.s_len = 0;
|
|
ms->search.s = NULL;
|
|
ms->search.s = NULL;
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (m->str_flags & REGEX_LINE_COUNT) {
|
|
|
|
+ linecnt = m->str_range;
|
|
|
|
+ bytecnt = linecnt * 80;
|
|
|
|
+ } else {
|
|
|
|
+ linecnt = 0;
|
|
|
|
+ bytecnt = m->str_range;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (bytecnt == 0)
|
|
|
|
+ bytecnt = 8192;
|
|
|
|
+ if (bytecnt > nbytes)
|
|
|
|
+ bytecnt = nbytes;
|
|
|
|
+
|
|
buf = RCAST(const char *, s) + offset;
|
|
buf = RCAST(const char *, s) + offset;
|
|
- end = last = RCAST(const char *, s) + nbytes;
|
|
|
|
|
|
+ end = last = RCAST(const char *, s) + bytecnt;
|
|
/* mget() guarantees buf <= last */
|
|
/* mget() guarantees buf <= last */
|
|
for (lines = linecnt, b = buf; lines && b < end &&
|
|
for (lines = linecnt, b = buf; lines && b < end &&
|
|
((b = CAST(const char *,
|
|
((b = CAST(const char *,
|
|
@@ -1088,7 +1123,7 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
|
|
b++;
|
|
b++;
|
|
}
|
|
}
|
|
if (lines)
|
|
if (lines)
|
|
- last = RCAST(const char *, s) + nbytes;
|
|
|
|
|
|
+ last = RCAST(const char *, s) + bytecnt;
|
|
|
|
|
|
ms->search.s = buf;
|
|
ms->search.s = buf;
|
|
ms->search.s_len = last - buf;
|
|
ms->search.s_len = last - buf;
|
|
@@ -1159,7 +1194,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
int *need_separator, int *returnval)
|
|
int *need_separator, int *returnval)
|
|
{
|
|
{
|
|
uint32_t soffset, offset = ms->offset;
|
|
uint32_t soffset, offset = ms->offset;
|
|
- uint32_t count = m->str_range;
|
|
|
|
|
|
+ uint32_t lhs;
|
|
int rv, oneed_separator, in_type;
|
|
int rv, oneed_separator, in_type;
|
|
char *sbuf, *rbuf;
|
|
char *sbuf, *rbuf;
|
|
union VALUETYPE *p = &ms->ms_value;
|
|
union VALUETYPE *p = &ms->ms_value;
|
|
@@ -1171,13 +1206,12 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
}
|
|
}
|
|
|
|
|
|
if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
|
|
if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
|
|
- (uint32_t)nbytes, count) == -1)
|
|
|
|
|
|
+ (uint32_t)nbytes, m) == -1)
|
|
return -1;
|
|
return -1;
|
|
|
|
|
|
if ((ms->flags & MAGIC_DEBUG) != 0) {
|
|
if ((ms->flags & MAGIC_DEBUG) != 0) {
|
|
fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%zu, "
|
|
fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%zu, "
|
|
- "nbytes=%zu, count=%u)\n", m->type, m->flag, offset, o,
|
|
|
|
- nbytes, count);
|
|
|
|
|
|
+ "nbytes=%zu)\n", m->type, m->flag, offset, o, nbytes);
|
|
mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
|
|
mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
|
|
#ifndef COMPILE_ONLY
|
|
#ifndef COMPILE_ONLY
|
|
file_mdump(m);
|
|
file_mdump(m);
|
|
@@ -1262,104 +1296,72 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
case FILE_BESHORT:
|
|
case FILE_BESHORT:
|
|
if (OFFSET_OOB(nbytes, offset, 2))
|
|
if (OFFSET_OOB(nbytes, offset, 2))
|
|
return 0;
|
|
return 0;
|
|
|
|
+ lhs = (p->hs[0] << 8) | p->hs[1];
|
|
if (off) {
|
|
if (off) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
case FILE_OPAND:
|
|
case FILE_OPAND:
|
|
- offset = (short)((p->hs[0]<<8)|
|
|
|
|
- (p->hs[1])) &
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs & off;
|
|
break;
|
|
break;
|
|
case FILE_OPOR:
|
|
case FILE_OPOR:
|
|
- offset = (short)((p->hs[0]<<8)|
|
|
|
|
- (p->hs[1])) |
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs | off;
|
|
break;
|
|
break;
|
|
case FILE_OPXOR:
|
|
case FILE_OPXOR:
|
|
- offset = (short)((p->hs[0]<<8)|
|
|
|
|
- (p->hs[1])) ^
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs ^ off;
|
|
break;
|
|
break;
|
|
case FILE_OPADD:
|
|
case FILE_OPADD:
|
|
- offset = (short)((p->hs[0]<<8)|
|
|
|
|
- (p->hs[1])) +
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs + off;
|
|
break;
|
|
break;
|
|
case FILE_OPMINUS:
|
|
case FILE_OPMINUS:
|
|
- offset = (short)((p->hs[0]<<8)|
|
|
|
|
- (p->hs[1])) -
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs - off;
|
|
break;
|
|
break;
|
|
case FILE_OPMULTIPLY:
|
|
case FILE_OPMULTIPLY:
|
|
- offset = (short)((p->hs[0]<<8)|
|
|
|
|
- (p->hs[1])) *
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs * off;
|
|
break;
|
|
break;
|
|
case FILE_OPDIVIDE:
|
|
case FILE_OPDIVIDE:
|
|
- offset = (short)((p->hs[0]<<8)|
|
|
|
|
- (p->hs[1])) /
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs / off;
|
|
break;
|
|
break;
|
|
case FILE_OPMODULO:
|
|
case FILE_OPMODULO:
|
|
- offset = (short)((p->hs[0]<<8)|
|
|
|
|
- (p->hs[1])) %
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs % off;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
} else
|
|
} else
|
|
- offset = (short)((p->hs[0]<<8)|
|
|
|
|
- (p->hs[1]));
|
|
|
|
|
|
+ offset = lhs;
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
offset = ~offset;
|
|
offset = ~offset;
|
|
break;
|
|
break;
|
|
case FILE_LESHORT:
|
|
case FILE_LESHORT:
|
|
if (OFFSET_OOB(nbytes, offset, 2))
|
|
if (OFFSET_OOB(nbytes, offset, 2))
|
|
return 0;
|
|
return 0;
|
|
|
|
+ lhs = (p->hs[1] << 8) | p->hs[0];
|
|
if (off) {
|
|
if (off) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
case FILE_OPAND:
|
|
case FILE_OPAND:
|
|
- offset = (short)((p->hs[1]<<8)|
|
|
|
|
- (p->hs[0])) &
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs & off;
|
|
break;
|
|
break;
|
|
case FILE_OPOR:
|
|
case FILE_OPOR:
|
|
- offset = (short)((p->hs[1]<<8)|
|
|
|
|
- (p->hs[0])) |
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs | off;
|
|
break;
|
|
break;
|
|
case FILE_OPXOR:
|
|
case FILE_OPXOR:
|
|
- offset = (short)((p->hs[1]<<8)|
|
|
|
|
- (p->hs[0])) ^
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs ^ off;
|
|
break;
|
|
break;
|
|
case FILE_OPADD:
|
|
case FILE_OPADD:
|
|
- offset = (short)((p->hs[1]<<8)|
|
|
|
|
- (p->hs[0])) +
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs + off;
|
|
break;
|
|
break;
|
|
case FILE_OPMINUS:
|
|
case FILE_OPMINUS:
|
|
- offset = (short)((p->hs[1]<<8)|
|
|
|
|
- (p->hs[0])) -
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs - off;
|
|
break;
|
|
break;
|
|
case FILE_OPMULTIPLY:
|
|
case FILE_OPMULTIPLY:
|
|
- offset = (short)((p->hs[1]<<8)|
|
|
|
|
- (p->hs[0])) *
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs * off;
|
|
break;
|
|
break;
|
|
case FILE_OPDIVIDE:
|
|
case FILE_OPDIVIDE:
|
|
- offset = (short)((p->hs[1]<<8)|
|
|
|
|
- (p->hs[0])) /
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs / off;
|
|
break;
|
|
break;
|
|
case FILE_OPMODULO:
|
|
case FILE_OPMODULO:
|
|
- offset = (short)((p->hs[1]<<8)|
|
|
|
|
- (p->hs[0])) %
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs % off;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
} else
|
|
} else
|
|
- offset = (short)((p->hs[1]<<8)|
|
|
|
|
- (p->hs[0]));
|
|
|
|
|
|
+ offset = lhs;
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
offset = ~offset;
|
|
offset = ~offset;
|
|
break;
|
|
break;
|
|
@@ -1403,70 +1405,37 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
case FILE_BEID3:
|
|
case FILE_BEID3:
|
|
if (OFFSET_OOB(nbytes, offset, 4))
|
|
if (OFFSET_OOB(nbytes, offset, 4))
|
|
return 0;
|
|
return 0;
|
|
|
|
+ lhs = (p->hl[0] << 24) | (p->hl[1] << 16) |
|
|
|
|
+ (p->hl[2] << 8) | p->hl[3];
|
|
if (off) {
|
|
if (off) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
case FILE_OPAND:
|
|
case FILE_OPAND:
|
|
- offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
- (p->hl[1]<<16)|
|
|
|
|
- (p->hl[2]<<8)|
|
|
|
|
- (p->hl[3])) &
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs & off;
|
|
break;
|
|
break;
|
|
case FILE_OPOR:
|
|
case FILE_OPOR:
|
|
- offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
- (p->hl[1]<<16)|
|
|
|
|
- (p->hl[2]<<8)|
|
|
|
|
- (p->hl[3])) |
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs | off;
|
|
break;
|
|
break;
|
|
case FILE_OPXOR:
|
|
case FILE_OPXOR:
|
|
- offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
- (p->hl[1]<<16)|
|
|
|
|
- (p->hl[2]<<8)|
|
|
|
|
- (p->hl[3])) ^
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs ^ off;
|
|
break;
|
|
break;
|
|
case FILE_OPADD:
|
|
case FILE_OPADD:
|
|
- offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
- (p->hl[1]<<16)|
|
|
|
|
- (p->hl[2]<<8)|
|
|
|
|
- (p->hl[3])) +
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs + off;
|
|
break;
|
|
break;
|
|
case FILE_OPMINUS:
|
|
case FILE_OPMINUS:
|
|
- offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
- (p->hl[1]<<16)|
|
|
|
|
- (p->hl[2]<<8)|
|
|
|
|
- (p->hl[3])) -
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs - off;
|
|
break;
|
|
break;
|
|
case FILE_OPMULTIPLY:
|
|
case FILE_OPMULTIPLY:
|
|
- offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
- (p->hl[1]<<16)|
|
|
|
|
- (p->hl[2]<<8)|
|
|
|
|
- (p->hl[3])) *
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs * off;
|
|
break;
|
|
break;
|
|
case FILE_OPDIVIDE:
|
|
case FILE_OPDIVIDE:
|
|
- offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
- (p->hl[1]<<16)|
|
|
|
|
- (p->hl[2]<<8)|
|
|
|
|
- (p->hl[3])) /
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs / off;
|
|
break;
|
|
break;
|
|
case FILE_OPMODULO:
|
|
case FILE_OPMODULO:
|
|
- offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
- (p->hl[1]<<16)|
|
|
|
|
- (p->hl[2]<<8)|
|
|
|
|
- (p->hl[3])) %
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs % off;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
} else
|
|
} else
|
|
- offset = (int32_t)((p->hl[0]<<24)|
|
|
|
|
- (p->hl[1]<<16)|
|
|
|
|
- (p->hl[2]<<8)|
|
|
|
|
- (p->hl[3]));
|
|
|
|
|
|
+ offset = lhs;
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
offset = ~offset;
|
|
offset = ~offset;
|
|
break;
|
|
break;
|
|
@@ -1474,140 +1443,74 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
case FILE_LEID3:
|
|
case FILE_LEID3:
|
|
if (OFFSET_OOB(nbytes, offset, 4))
|
|
if (OFFSET_OOB(nbytes, offset, 4))
|
|
return 0;
|
|
return 0;
|
|
|
|
+ lhs = (p->hl[3] << 24) | (p->hl[2] << 16) |
|
|
|
|
+ (p->hl[1] << 8) | p->hl[0];
|
|
if (off) {
|
|
if (off) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
case FILE_OPAND:
|
|
case FILE_OPAND:
|
|
- offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
- (p->hl[2]<<16)|
|
|
|
|
- (p->hl[1]<<8)|
|
|
|
|
- (p->hl[0])) &
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs & off;
|
|
break;
|
|
break;
|
|
case FILE_OPOR:
|
|
case FILE_OPOR:
|
|
- offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
- (p->hl[2]<<16)|
|
|
|
|
- (p->hl[1]<<8)|
|
|
|
|
- (p->hl[0])) |
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs | off;
|
|
break;
|
|
break;
|
|
case FILE_OPXOR:
|
|
case FILE_OPXOR:
|
|
- offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
- (p->hl[2]<<16)|
|
|
|
|
- (p->hl[1]<<8)|
|
|
|
|
- (p->hl[0])) ^
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs ^ off;
|
|
break;
|
|
break;
|
|
case FILE_OPADD:
|
|
case FILE_OPADD:
|
|
- offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
- (p->hl[2]<<16)|
|
|
|
|
- (p->hl[1]<<8)|
|
|
|
|
- (p->hl[0])) +
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs + off;
|
|
break;
|
|
break;
|
|
case FILE_OPMINUS:
|
|
case FILE_OPMINUS:
|
|
- offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
- (p->hl[2]<<16)|
|
|
|
|
- (p->hl[1]<<8)|
|
|
|
|
- (p->hl[0])) -
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs - off;
|
|
break;
|
|
break;
|
|
case FILE_OPMULTIPLY:
|
|
case FILE_OPMULTIPLY:
|
|
- offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
- (p->hl[2]<<16)|
|
|
|
|
- (p->hl[1]<<8)|
|
|
|
|
- (p->hl[0])) *
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs * off;
|
|
break;
|
|
break;
|
|
case FILE_OPDIVIDE:
|
|
case FILE_OPDIVIDE:
|
|
- offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
- (p->hl[2]<<16)|
|
|
|
|
- (p->hl[1]<<8)|
|
|
|
|
- (p->hl[0])) /
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs / off;
|
|
break;
|
|
break;
|
|
case FILE_OPMODULO:
|
|
case FILE_OPMODULO:
|
|
- offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
- (p->hl[2]<<16)|
|
|
|
|
- (p->hl[1]<<8)|
|
|
|
|
- (p->hl[0])) %
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs % off;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
} else
|
|
} else
|
|
- offset = (int32_t)((p->hl[3]<<24)|
|
|
|
|
- (p->hl[2]<<16)|
|
|
|
|
- (p->hl[1]<<8)|
|
|
|
|
- (p->hl[0]));
|
|
|
|
|
|
+ offset = lhs;
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
offset = ~offset;
|
|
offset = ~offset;
|
|
break;
|
|
break;
|
|
case FILE_MELONG:
|
|
case FILE_MELONG:
|
|
if (OFFSET_OOB(nbytes, offset, 4))
|
|
if (OFFSET_OOB(nbytes, offset, 4))
|
|
return 0;
|
|
return 0;
|
|
|
|
+ lhs = (p->hl[1] << 24) | (p->hl[0] << 16) |
|
|
|
|
+ (p->hl[3] << 8) | p->hl[2];
|
|
if (off) {
|
|
if (off) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
switch (m->in_op & FILE_OPS_MASK) {
|
|
case FILE_OPAND:
|
|
case FILE_OPAND:
|
|
- offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
- (p->hl[0]<<16)|
|
|
|
|
- (p->hl[3]<<8)|
|
|
|
|
- (p->hl[2])) &
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs & off;
|
|
break;
|
|
break;
|
|
case FILE_OPOR:
|
|
case FILE_OPOR:
|
|
- offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
- (p->hl[0]<<16)|
|
|
|
|
- (p->hl[3]<<8)|
|
|
|
|
- (p->hl[2])) |
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs | off;
|
|
break;
|
|
break;
|
|
case FILE_OPXOR:
|
|
case FILE_OPXOR:
|
|
- offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
- (p->hl[0]<<16)|
|
|
|
|
- (p->hl[3]<<8)|
|
|
|
|
- (p->hl[2])) ^
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs ^ off;
|
|
break;
|
|
break;
|
|
case FILE_OPADD:
|
|
case FILE_OPADD:
|
|
- offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
- (p->hl[0]<<16)|
|
|
|
|
- (p->hl[3]<<8)|
|
|
|
|
- (p->hl[2])) +
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs + off;
|
|
break;
|
|
break;
|
|
case FILE_OPMINUS:
|
|
case FILE_OPMINUS:
|
|
- offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
- (p->hl[0]<<16)|
|
|
|
|
- (p->hl[3]<<8)|
|
|
|
|
- (p->hl[2])) -
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs - off;
|
|
break;
|
|
break;
|
|
case FILE_OPMULTIPLY:
|
|
case FILE_OPMULTIPLY:
|
|
- offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
- (p->hl[0]<<16)|
|
|
|
|
- (p->hl[3]<<8)|
|
|
|
|
- (p->hl[2])) *
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs * off;
|
|
break;
|
|
break;
|
|
case FILE_OPDIVIDE:
|
|
case FILE_OPDIVIDE:
|
|
- offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
- (p->hl[0]<<16)|
|
|
|
|
- (p->hl[3]<<8)|
|
|
|
|
- (p->hl[2])) /
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs / off;
|
|
break;
|
|
break;
|
|
case FILE_OPMODULO:
|
|
case FILE_OPMODULO:
|
|
- offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
- (p->hl[0]<<16)|
|
|
|
|
- (p->hl[3]<<8)|
|
|
|
|
- (p->hl[2])) %
|
|
|
|
- off;
|
|
|
|
|
|
+ offset = lhs % off;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
} else
|
|
} else
|
|
- offset = (int32_t)((p->hl[1]<<24)|
|
|
|
|
- (p->hl[0]<<16)|
|
|
|
|
- (p->hl[3]<<8)|
|
|
|
|
- (p->hl[2]));
|
|
|
|
|
|
+ offset = lhs;
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
if (m->in_op & FILE_OPINVERSE)
|
|
offset = ~offset;
|
|
offset = ~offset;
|
|
break;
|
|
break;
|
|
@@ -1673,7 +1576,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
|
if ((ms->flags & MAGIC_DEBUG) != 0)
|
|
fprintf(stderr, "indirect +offs=%u\n", offset);
|
|
fprintf(stderr, "indirect +offs=%u\n", offset);
|
|
}
|
|
}
|
|
- if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
|
|
|
|
|
|
+ if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
|
|
return -1;
|
|
return -1;
|
|
ms->offset = offset;
|
|
ms->offset = offset;
|
|
|
|
|
|
@@ -1756,7 +1659,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
ms->offset = soffset;
|
|
ms->offset = soffset;
|
|
if (rv == 1) {
|
|
if (rv == 1) {
|
|
if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
|
|
if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
|
|
- file_printf(ms, F(m->desc, "%u"), offset) == -1) {
|
|
|
|
|
|
+ file_printf(ms, F(ms, m, "%u"), offset) == -1) {
|
|
free(rbuf);
|
|
free(rbuf);
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
@@ -1889,7 +1792,6 @@ magiccheck(struct magic_set *ms, struct magic *m)
|
|
double dl, dv;
|
|
double dl, dv;
|
|
int matched;
|
|
int matched;
|
|
union VALUETYPE *p = &ms->ms_value;
|
|
union VALUETYPE *p = &ms->ms_value;
|
|
- char *old_lc_ctype;
|
|
|
|
|
|
|
|
switch (m->type) {
|
|
switch (m->type) {
|
|
case FILE_BYTE:
|
|
case FILE_BYTE:
|
|
@@ -1959,7 +1861,6 @@ magiccheck(struct magic_set *ms, struct magic *m)
|
|
break;
|
|
break;
|
|
|
|
|
|
default:
|
|
default:
|
|
- matched = 0;
|
|
|
|
file_magerror(ms, "cannot happen with float: invalid relation `%c'",
|
|
file_magerror(ms, "cannot happen with float: invalid relation `%c'",
|
|
m->reln);
|
|
m->reln);
|
|
return -1;
|
|
return -1;
|
|
@@ -1993,7 +1894,6 @@ magiccheck(struct magic_set *ms, struct magic *m)
|
|
break;
|
|
break;
|
|
|
|
|
|
default:
|
|
default:
|
|
- matched = 0;
|
|
|
|
file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
|
|
file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
@@ -2032,7 +1932,8 @@ magiccheck(struct magic_set *ms, struct magic *m)
|
|
if (slen + idx > ms->search.s_len)
|
|
if (slen + idx > ms->search.s_len)
|
|
break;
|
|
break;
|
|
|
|
|
|
- v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
|
|
|
|
|
|
+ v = file_strncmp(m->value.s, ms->search.s + idx, slen,
|
|
|
|
+ m->str_flags);
|
|
if (v == 0) { /* found match */
|
|
if (v == 0) { /* found match */
|
|
ms->search.offset += idx;
|
|
ms->search.offset += idx;
|
|
break;
|
|
break;
|
|
@@ -2042,39 +1943,33 @@ magiccheck(struct magic_set *ms, struct magic *m)
|
|
}
|
|
}
|
|
case FILE_REGEX: {
|
|
case FILE_REGEX: {
|
|
int rc;
|
|
int rc;
|
|
- regex_t rx;
|
|
|
|
- char errmsg[512];
|
|
|
|
|
|
+ file_regex_t rx;
|
|
|
|
|
|
if (ms->search.s == NULL)
|
|
if (ms->search.s == NULL)
|
|
return 0;
|
|
return 0;
|
|
|
|
|
|
- old_lc_ctype = setlocale(LC_CTYPE, NULL);
|
|
|
|
- assert(old_lc_ctype != NULL);
|
|
|
|
- old_lc_ctype = strdup(old_lc_ctype);
|
|
|
|
- assert(old_lc_ctype != NULL);
|
|
|
|
- (void)setlocale(LC_CTYPE, "C");
|
|
|
|
l = 0;
|
|
l = 0;
|
|
- rc = regcomp(&rx, m->value.s,
|
|
|
|
|
|
+ rc = file_regcomp(&rx, m->value.s,
|
|
REG_EXTENDED|REG_NEWLINE|
|
|
REG_EXTENDED|REG_NEWLINE|
|
|
((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
|
|
((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
|
|
if (rc) {
|
|
if (rc) {
|
|
- (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
|
|
|
|
- file_magerror(ms, "regex error %d, (%s)",
|
|
|
|
- rc, errmsg);
|
|
|
|
|
|
+ file_regerror(&rx, rc, ms);
|
|
v = (uint64_t)-1;
|
|
v = (uint64_t)-1;
|
|
- }
|
|
|
|
- else {
|
|
|
|
|
|
+ } else {
|
|
regmatch_t pmatch[1];
|
|
regmatch_t pmatch[1];
|
|
|
|
+ size_t slen = ms->search.s_len;
|
|
#ifndef REG_STARTEND
|
|
#ifndef REG_STARTEND
|
|
#define REG_STARTEND 0
|
|
#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';
|
|
|
|
|
|
+ char c;
|
|
|
|
+ if (slen != 0)
|
|
|
|
+ slen--;
|
|
|
|
+ c = ms->search.s[slen];
|
|
|
|
+ ((char *)(intptr_t)ms->search.s)[slen] = '\0';
|
|
#else
|
|
#else
|
|
pmatch[0].rm_so = 0;
|
|
pmatch[0].rm_so = 0;
|
|
- pmatch[0].rm_eo = ms->search.s_len;
|
|
|
|
|
|
+ pmatch[0].rm_eo = slen;
|
|
#endif
|
|
#endif
|
|
- rc = regexec(&rx, (const char *)ms->search.s,
|
|
|
|
|
|
+ rc = file_regexec(&rx, (const char *)ms->search.s,
|
|
1, pmatch, REG_STARTEND);
|
|
1, pmatch, REG_STARTEND);
|
|
#if REG_STARTEND == 0
|
|
#if REG_STARTEND == 0
|
|
((char *)(intptr_t)ms->search.s)[l] = c;
|
|
((char *)(intptr_t)ms->search.s)[l] = c;
|
|
@@ -2093,16 +1988,12 @@ magiccheck(struct magic_set *ms, struct magic *m)
|
|
break;
|
|
break;
|
|
|
|
|
|
default:
|
|
default:
|
|
- (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
|
|
|
|
- file_magerror(ms, "regexec error %d, (%s)",
|
|
|
|
- rc, errmsg);
|
|
|
|
|
|
+ file_regerror(&rx, rc, ms);
|
|
v = (uint64_t)-1;
|
|
v = (uint64_t)-1;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
- regfree(&rx);
|
|
|
|
}
|
|
}
|
|
- (void)setlocale(LC_CTYPE, old_lc_ctype);
|
|
|
|
- free(old_lc_ctype);
|
|
|
|
|
|
+ file_regfree(&rx);
|
|
if (v == (uint64_t)-1)
|
|
if (v == (uint64_t)-1)
|
|
return -1;
|
|
return -1;
|
|
break;
|
|
break;
|
|
@@ -2199,7 +2090,6 @@ magiccheck(struct magic_set *ms, struct magic *m)
|
|
break;
|
|
break;
|
|
|
|
|
|
default:
|
|
default:
|
|
- matched = 0;
|
|
|
|
file_magerror(ms, "cannot happen: invalid relation `%c'",
|
|
file_magerror(ms, "cannot happen: invalid relation `%c'",
|
|
m->reln);
|
|
m->reln);
|
|
return -1;
|
|
return -1;
|