#endif
#ifndef lint
-FILE_RCSID("@(#)$Id: apprentice.c,v 1.55 2003/03/26 15:35:30 christos Exp $")
+FILE_RCSID("@(#)$Id: apprentice.c,v 1.56 2003/03/26 16:25:25 christos Exp $")
#endif /* lint */
#define EATAB {while (isascii((unsigned char) *l) && \
int i = 0;
struct magic *m;
char *t;
+ private const char *fops = FILE_OPS;
uint32_t val;
#define ALLOC_INCR 200
}
/* get offset, then skip over it */
- m->offset = (int) strtoul(l,&t,0);
+ m->offset = (int) strtoul(l, &t, 0);
if (l == t)
if (ms->flags & MAGIC_CHECK)
file_magwarn("offset %s invalid", l);
m->mask_op = FILE_OPINVERSE;
++l;
}
- val = (uint32_t)strtoul(l, &l, 0);
- switch (*l) {
- case '&':
- m->mask_op |= FILE_OPAND;
- ++l;
- m->mask = file_signextend(ms, m, val);
- eatsize(&l);
- break;
- case '|':
- m->mask_op |= FILE_OPOR;
- ++l;
- m->mask = file_signextend(ms, m, val);
- eatsize(&l);
- break;
- case '^':
- m->mask_op |= FILE_OPXOR;
- ++l;
- m->mask = file_signextend(ms, m, val);
- eatsize(&l);
- break;
- case '+':
- m->mask_op |= FILE_OPADD;
- ++l;
- m->mask = file_signextend(ms, m, val);
- eatsize(&l);
- break;
- case '-':
- m->mask_op |= FILE_OPMINUS;
- ++l;
- m->mask = file_signextend(ms, m, val);
- eatsize(&l);
- break;
- case '*':
- m->mask_op |= FILE_OPMULTIPLY;
- ++l;
- m->mask = file_signextend(ms, m, val);
- eatsize(&l);
- break;
- case '%':
- m->mask_op |= FILE_OPMODULO;
- ++l;
- m->mask = file_signextend(ms, m, val);
- eatsize(&l);
- break;
- case '/':
- if (FILE_STRING != m->type && FILE_PSTRING != m->type) {
- m->mask_op |= FILE_OPDIVIDE;
+ if ((t = strchr(fops, *l)) != NULL) {
+ uint32_t op = (uint32_t)(t - fops);
+ if (op != FILE_OPDIVIDE ||
+ (FILE_STRING != m->type && FILE_PSTRING != m->type)) {
++l;
+ m->mask_op |= op;
+ val = (uint32_t)strtoul(l, &l, 0);
m->mask = file_signextend(ms, m, val);
eatsize(&l);
} else {
}
}
}
- break;
}
- /* We used to set mask to all 1's here, instead let's just not do anything
- if mask = 0 (unless you have a better idea) */
+ /*
+ * We used to set mask to all 1's here, instead let's just not do
+ * anything if mask = 0 (unless you have a better idea)
+ */
EATAB;
switch (*l) {
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
+#undef HAVE_LIBZ
#ifdef HAVE_LIBZ
#include <zlib.h>
#endif
#ifndef lint
-FILE_RCSID("@(#)$Id: compress.c,v 1.30 2003/03/26 15:35:30 christos Exp $")
+FILE_RCSID("@(#)$Id: compress.c,v 1.31 2003/03/26 16:25:25 christos Exp $")
#endif
protected int
file_zmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
{
- unsigned char *newbuf;
- size_t i;
+ unsigned char *newbuf = NULL;
+ size_t i, nsz;
+ int rv = 0;
if ((ms->flags & MAGIC_COMPRESS) == 0)
return 0;
if (nbytes < compr[i].maglen)
continue;
if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
- uncompressbuf(ms, i, buf, &newbuf, nbytes) != 0) {
- free(newbuf);
- if (file_printf(ms, " (") == -1)
- return -1;
+ (nsz = uncompressbuf(ms, i, buf, &newbuf, nbytes)) != 0) {
ms->flags &= ~MAGIC_COMPRESS;
- if (file_buffer(ms, buf, nbytes) == -1) {
- ms->flags |= MAGIC_COMPRESS;
- return -1;
- }
- ms->flags |= MAGIC_COMPRESS;
+ rv = -1;
+ if (file_buffer(ms, newbuf, nsz) == -1)
+ goto error;
+ if (file_printf(ms, " (") == -1)
+ goto error;
+ if (file_buffer(ms, buf, nbytes) == -1)
+ goto error;
if (file_printf(ms, ")") == -1)
- return -1;
- return 1;
+ goto error;
+ rv = 1;
+ break;
}
}
-
- return 0;
+error:
+ if (newbuf)
+ free(newbuf);
+ ms->flags |= MAGIC_COMPRESS;
+ return rv;
}
/*
*/
/*
* file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.49 2003/03/26 15:35:30 christos Exp $
+ * @(#)$Id: file.h,v 1.50 2003/03/26 16:25:25 christos Exp $
*/
#ifndef __file_h__
#define FILE_COMPILE 2
#ifndef __GNUC__
-#ifndef __attribute__
+#ifndef __lint__
#define __attribute__(a)
#endif
#endif
#define FILE_REGEX 17
uint8_t in_op; /* operator for indirection */
uint8_t mask_op; /* operator for mask */
-#define FILE_OPAND 1
-#define FILE_OPOR 2
-#define FILE_OPXOR 3
-#define FILE_OPADD 4
-#define FILE_OPMINUS 5
-#define FILE_OPMULTIPLY 6
-#define FILE_OPDIVIDE 7
-#define FILE_OPMODULO 8
+#define FILE_OPS "&|^+-*%/"
+#define FILE_OPAND 0
+#define FILE_OPOR 1
+#define FILE_OPXOR 2
+#define FILE_OPADD 3
+#define FILE_OPMINUS 4
+#define FILE_OPMULTIPLY 5
+#define FILE_OPDIVIDE 6
+#define FILE_OPMODULO 7
#define FILE_OPINVERSE 0x80
int32_t offset; /* offset to magic number */
int32_t in_offset; /* offset from indirection */