#endif
#ifndef lint
-FILE_RCSID("@(#)$Id: apprentice.c,v 1.79 2004/09/11 19:15:57 christos Exp $")
+FILE_RCSID("@(#)$Id: apprentice.c,v 1.80 2004/11/13 08:11:39 christos Exp $")
#endif /* lint */
#define EATAB {while (isascii((unsigned char) *l) && \
const char *);
private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
const char *);
-private int check_format(struct magic *);
+private int check_format(struct magic_set *, struct magic *);
private size_t maxmagic = 0;
private size_t magicsize = sizeof(struct magic);
#ifndef COMPILE_ONLY
if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
if (ms->flags & MAGIC_CHECK)
- file_magwarn("using regular magic file `%s'", fn);
+ file_magwarn(ms, "using regular magic file `%s'", fn);
rv = apprentice_file(ms, &magic, &nmagic, fn, action);
if (rv != 0)
return -1;
"cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
FILE *f;
char line[BUFSIZ+1];
- int lineno;
int errs = 0;
- f = fopen(fn, "r");
+ f = fopen(ms->file = fn, "r");
if (f == NULL) {
if (errno != ENOENT)
file_error(ms, errno, "cannot read magic file `%s'",
(void)fprintf(stderr, "%s\n", hdr);
/* parse it */
- for (lineno = 1; fgets(line, BUFSIZ, f) != NULL; lineno++) {
+ for (ms->line = 1; fgets(line, BUFSIZ, f) != NULL; ms->line++) {
+ size_t len;
if (line[0]=='#') /* comment, do not parse */
continue;
- if (strlen(line) <= (unsigned)1) /* null line, garbage, etc */
+ len = strlen(line);
+ if (len < 2) /* null line, garbage, etc */
continue;
- line[strlen(line)-1] = '\0'; /* delete newline */
+ line[len - 1] = '\0'; /* delete newline */
if (parse(ms, magicp, nmagicp, line, action) != 0)
errs = 1;
}
break;
default:
if (ms->flags & MAGIC_CHECK)
- file_magwarn("cannot happen: m->type=%d\n",
+ file_magwarn(ms, "cannot happen: m->type=%d\n",
m->type);
return ~0U;
}
m->offset = (uint32_t)strtoul(l, &t, 0);
if (l == t)
if (ms->flags & MAGIC_CHECK)
- file_magwarn("offset %s invalid", l);
+ file_magwarn(ms, "offset `%s' invalid", l);
l = t;
if (m->flag & INDIR) {
break;
default:
if (ms->flags & MAGIC_CHECK)
- file_magwarn(
- "indirect offset type %c invalid",
+ file_magwarn(ms,
+ "indirect offset type `%c' invalid",
*l);
break;
}
t = l;
if (*t++ != ')')
if (ms->flags & MAGIC_CHECK)
- file_magwarn("missing ')' in indirect offset");
+ file_magwarn(ms,
+ "missing ')' in indirect offset");
l = t;
}
l += sizeof("regex");
} else {
if (ms->flags & MAGIC_CHECK)
- file_magwarn("type %s invalid", l);
+ file_magwarn(ms, "type `%s' invalid", l);
return -1;
}
/* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
break;
default:
if (ms->flags & MAGIC_CHECK)
- file_magwarn(
- "string extension %c invalid",
+ file_magwarn(ms,
+ "string extension `%c' invalid",
*l);
return -1;
}
/* NULLBODY */;
if (ms->flags & MAGIC_CHECK) {
- if (!check_format(m))
+ if (!check_format(ms, m))
return -1;
}
#ifndef COMPILE_ONLY
* the type of the magic.
*/
private int
-check_format(struct magic *m)
+check_format(struct magic_set *ms, struct magic *m)
{
static const char *formats[] = { FILE_FORMAT_STRING };
static const char *names[] = { FILE_FORMAT_NAME };
return 1;
}
if (m->type >= sizeof(formats)/sizeof(formats[0])) {
- file_magwarn("Internal error inconsistency between m->type"
+ file_magwarn(ms, "Internal error inconsistency between m->type"
" and format strings");
return 0;
}
if (formats[m->type] == NULL) {
- file_magwarn("No format string for `%s' with description `%s'",
- m->desc, names[m->type]);
+ file_magwarn(ms, "No format string for `%s' with description "
+ "`%s'", m->desc, names[m->type]);
return 0;
}
for (; *ptr; ptr++) {
}
if (*ptr == '\0') {
/* Missing format string; bad */
- file_magwarn("Invalid format `%s' for type `%s'",
+ file_magwarn(ms, "Invalid format `%s' for type `%s'",
m->desc, names[m->type]);
return 0;
}
if (strchr(formats[m->type], *ptr) == NULL) {
- file_magwarn("Printf format `%c' is not valid for type `%s'"
+ file_magwarn(ms, "Printf format `%c' is not valid for type `%s'"
" in description `%s'",
*ptr, names[m->type], m->desc);
return 0;
*p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen);
if (*p == NULL) {
if (ms->flags & MAGIC_CHECK)
- file_magwarn("cannot get string from `%s'",
+ file_magwarn(ms, "cannot get string from `%s'",
m->value.s);
return -1;
}