#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.199 2014/11/27 15:40:36 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.200 2014/11/27 23:42:58 christos Exp $")
#endif /* lint */
#include "magic.h"
private int match(struct magic_set *, struct magic *, uint32_t,
const unsigned char *, size_t, size_t, int, int, int, uint16_t, uint16_t,
- int *, int *, int *);
+ uint16_t *, int *, int *, int *);
private int mget(struct magic_set *, const unsigned char *,
struct magic *, size_t, size_t, unsigned int, int, int, int, uint16_t,
- uint16_t, int *, int *, int *);
+ uint16_t, uint16_t *, int *, int *, int *);
private int magiccheck(struct magic_set *, struct magic *);
private int32_t mprint(struct magic_set *, struct magic *);
private int32_t moffset(struct magic_set *, struct magic *);
/*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
protected int
file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
- uint16_t indir_level, uint16_t name_level, int mode, int text)
+ uint16_t indir_level, uint16_t name_level, uint16_t *name_count,
+ int mode, int text)
{
struct mlist *ml;
int rv, printed_something = 0, need_separator = 0;
+ uint16_t nc;
+
+ if (name_count == NULL) {
+ nc = 0;
+ name_count = &nc;
+ }
for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode,
- text, 0, indir_level, name_level, &printed_something,
- &need_separator, NULL)) != 0)
+ text, 0, indir_level, name_level, name_count,
+ &printed_something, &need_separator, NULL)) != 0)
return rv;
return 0;
private int
match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
const unsigned char *s, size_t nbytes, size_t offset, int mode, int text,
- int flip, uint16_t indir_level, uint16_t name_level, int *printed_something,
- int *need_separator, int *returnval)
+ int flip, uint16_t indir_level, uint16_t name_level, uint16_t *name_count,
+ int *printed_something, int *need_separator, int *returnval)
{
uint32_t magindex = 0;
unsigned int cont_level = 0;
/* if main entry matches, print it... */
switch (mget(ms, s, m, nbytes, offset, cont_level, mode, text,
- flip, indir_level, name_level, printed_something,
- need_separator, returnval)) {
+ flip, indir_level, name_level, name_count,
+ printed_something, need_separator, returnval)) {
case -1:
return -1;
case 0:
}
#endif
switch (mget(ms, s, m, nbytes, offset, cont_level, mode,
- text, flip, indir_level, name_level,
+ text, flip, indir_level, name_level, name_count,
printed_something, need_separator, returnval)) {
case -1:
return -1;
private int
mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
size_t nbytes, size_t o, unsigned int cont_level, int mode, int text,
- int flip, uint16_t indir_level, uint16_t name_level, int *printed_something,
- int *need_separator, int *returnval)
+ int flip, uint16_t indir_level, uint16_t name_level, uint16_t *name_count,
+ int *printed_something, int *need_separator, int *returnval)
{
uint32_t offset = ms->offset;
uint32_t lhs;
return -1;
}
+ if (*name_count >= ms->name_max) {
+ file_error(ms, 0, "name use count (%hu) exceeded",
+ *name_count);
+ return -1;
+ }
+
if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
(uint32_t)nbytes, m) == -1)
return -1;
if ((ms->flags & MAGIC_DEBUG) != 0) {
fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%"
- SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT "u)\n",
- m->type, m->flag, offset, o, nbytes);
+ SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
+ "u, il=%hu, nl=%hu nc=%hu)\n",
+ m->type, m->flag, offset, o, nbytes,
+ indir_level, name_level, *name_count);
mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
#ifndef COMPILE_ONLY
file_mdump(m);
return -1;
rv = file_softmagic(ms, s + offset, nbytes - offset,
- indir_level + 1, name_level, BINTEST, text);
+ indir_level + 1, name_level, name_count, BINTEST, text);
if ((ms->flags & MAGIC_DEBUG) != 0)
fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
file_error(ms, 0, "cannot find entry `%s'", rbuf);
return -1;
}
-
+ (*name_count)++;
oneed_separator = *need_separator;
if (m->flag & NOSPACE)
*need_separator = 0;
rv = match(ms, ml.magic, ml.nmagic, s, nbytes, offset + o,
- mode, text, flip, indir_level, name_level + 1,
+ mode, text, flip, indir_level, name_level + 1, name_count,
printed_something, need_separator, returnval);
if (rv != 1)
*need_separator = oneed_separator;