2000-09-22 20:34:59 +04:00
|
|
|
/* $NetBSD: apprentice.c,v 1.20 2000/09/22 16:34:59 pooka Exp $ */
|
1997-01-09 23:18:21 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* apprentice - make one pass through /etc/magic, learning its secrets.
|
|
|
|
*
|
|
|
|
* Copyright (c) Ian F. Darwin, 1987.
|
|
|
|
* Written by Ian F. Darwin.
|
|
|
|
*
|
|
|
|
* This software is not subject to any license of the American Telephone
|
|
|
|
* and Telegraph Company or of the Regents of the University of California.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose on
|
|
|
|
* any computer system, and to alter it and redistribute it freely, subject
|
|
|
|
* to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The author is not responsible for the consequences of use of this
|
|
|
|
* software, no matter how awful, even if they arise from flaws in it.
|
|
|
|
*
|
|
|
|
* 2. The origin of this software must not be misrepresented, either by
|
|
|
|
* explicit claim or by omission. Since few users ever read sources,
|
|
|
|
* credits must appear in the documentation.
|
|
|
|
*
|
|
|
|
* 3. Altered versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software. Since few users
|
|
|
|
* ever read sources, credits must appear in the documentation.
|
|
|
|
*
|
|
|
|
* 4. This notice may not be removed or altered.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1993-06-10 04:37:55 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <ctype.h>
|
1995-10-28 02:33:14 +03:00
|
|
|
#include <errno.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "file.h"
|
|
|
|
|
1997-10-18 18:53:48 +04:00
|
|
|
#include <sys/cdefs.h>
|
1995-03-26 01:35:28 +03:00
|
|
|
#ifndef lint
|
1998-09-20 19:27:15 +04:00
|
|
|
#if 0
|
2000-05-15 02:53:37 +04:00
|
|
|
FILE_RCSID("@(#)Id: apprentice.c,v 1.32 2000/04/23 04:32:19 christos Exp ")
|
1998-09-20 19:27:15 +04:00
|
|
|
#else
|
2000-09-22 20:34:59 +04:00
|
|
|
__RCSID("$NetBSD: apprentice.c,v 1.20 2000/09/22 16:34:59 pooka Exp $");
|
1998-09-20 19:27:15 +04:00
|
|
|
#endif
|
1995-03-26 01:35:28 +03:00
|
|
|
#endif /* lint */
|
|
|
|
|
1993-06-10 04:37:55 +04:00
|
|
|
#define EATAB {while (isascii((unsigned char) *l) && \
|
|
|
|
isspace((unsigned char) *l)) ++l;}
|
1995-04-28 23:23:38 +04:00
|
|
|
#define LOWCASE(l) (isupper((unsigned char) (l)) ? \
|
|
|
|
tolower((unsigned char) (l)) : (l))
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
2000-05-15 02:53:37 +04:00
|
|
|
#ifdef __EMX__
|
|
|
|
char PATHSEP=';';
|
|
|
|
#else
|
|
|
|
char PATHSEP=':';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1995-03-26 01:35:28 +03:00
|
|
|
static int getvalue __P((struct magic *, char **));
|
|
|
|
static int hextoint __P((int));
|
|
|
|
static char *getstr __P((char *, char *, int, int *));
|
|
|
|
static int parse __P((char *, int *, int));
|
1995-04-28 23:23:38 +04:00
|
|
|
static void eatsize __P((char **));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-06-10 04:37:55 +04:00
|
|
|
static int maxmagic = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-09-20 19:27:15 +04:00
|
|
|
static int apprentice_1 __P((const char *, int));
|
1995-04-28 23:23:38 +04:00
|
|
|
|
1993-06-10 04:37:55 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
apprentice(fn, check)
|
2000-09-22 20:34:59 +04:00
|
|
|
const char *fn; /* list of magic files */
|
|
|
|
int check; /* non-zero? checking-only run. */
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1995-10-28 02:33:14 +03:00
|
|
|
char *p, *mfn;
|
|
|
|
int file_err, errs = -1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-06-10 04:37:55 +04:00
|
|
|
maxmagic = MAXMAGIS;
|
1995-10-28 02:33:14 +03:00
|
|
|
magic = (struct magic *) calloc(sizeof(struct magic), maxmagic);
|
|
|
|
mfn = malloc(strlen(fn)+1);
|
|
|
|
if (magic == NULL || mfn == NULL) {
|
1993-06-10 04:37:55 +04:00
|
|
|
(void) fprintf(stderr, "%s: Out of memory.\n", progname);
|
|
|
|
if (check)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
exit(1);
|
|
|
|
}
|
1995-10-28 02:33:14 +03:00
|
|
|
fn = strcpy(mfn, fn);
|
1995-03-26 01:35:28 +03:00
|
|
|
|
1995-10-28 02:33:14 +03:00
|
|
|
while (fn) {
|
2000-05-15 02:53:37 +04:00
|
|
|
p = strchr(fn, PATHSEP);
|
1995-10-28 02:33:14 +03:00
|
|
|
if (p)
|
|
|
|
*p++ = '\0';
|
|
|
|
file_err = apprentice_1(fn, check);
|
|
|
|
if (file_err > errs)
|
|
|
|
errs = file_err;
|
|
|
|
fn = p;
|
|
|
|
}
|
|
|
|
if (errs == -1)
|
|
|
|
(void) fprintf(stderr, "%s: couldn't find any magic files!\n",
|
|
|
|
progname);
|
|
|
|
if (!check && errs)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
free(mfn);
|
|
|
|
return errs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
apprentice_1(fn, check)
|
2000-09-22 20:34:59 +04:00
|
|
|
const char *fn; /* name of magic file */
|
|
|
|
int check; /* non-zero? checking-only run. */
|
1995-10-28 02:33:14 +03:00
|
|
|
{
|
|
|
|
static const char hdr[] =
|
|
|
|
"cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
|
|
|
|
FILE *f;
|
|
|
|
char line[BUFSIZ+1];
|
|
|
|
int errs = 0;
|
|
|
|
|
|
|
|
f = fopen(fn, "r");
|
|
|
|
if (f==NULL) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
(void) fprintf(stderr,
|
2000-09-22 20:34:59 +04:00
|
|
|
"%s: can't read magic file %s (%s)\n",
|
|
|
|
progname, fn, strerror(errno));
|
1995-10-28 02:33:14 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1995-03-26 01:35:28 +03:00
|
|
|
/* parse it */
|
|
|
|
if (check) /* print silly verbose header for USG compat. */
|
1995-10-28 02:33:14 +03:00
|
|
|
(void) printf("%s\n", hdr);
|
1993-06-10 04:37:55 +04:00
|
|
|
|
|
|
|
for (lineno = 1;fgets(line, BUFSIZ, f) != NULL; lineno++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (line[0]=='#') /* comment, do not parse */
|
|
|
|
continue;
|
1993-06-10 04:37:55 +04:00
|
|
|
if (strlen(line) <= (unsigned)1) /* null line, garbage, etc */
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
line[strlen(line)-1] = '\0'; /* delete newline */
|
|
|
|
if (parse(line, &nmagic, check) != 0)
|
1995-10-28 02:33:14 +03:00
|
|
|
errs = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
(void) fclose(f);
|
1995-10-28 02:33:14 +03:00
|
|
|
return errs;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1993-11-03 07:04:19 +03:00
|
|
|
/*
|
|
|
|
* extend the sign bit if the comparison is to be signed
|
|
|
|
*/
|
1997-01-28 03:49:36 +03:00
|
|
|
uint32
|
1993-11-03 07:04:19 +03:00
|
|
|
signextend(m, v)
|
2000-09-22 20:34:59 +04:00
|
|
|
struct magic *m;
|
|
|
|
uint32 v;
|
1993-11-03 07:04:19 +03:00
|
|
|
{
|
|
|
|
if (!(m->flag & UNSIGNED))
|
|
|
|
switch(m->type) {
|
|
|
|
/*
|
|
|
|
* Do not remove the casts below. They are
|
|
|
|
* vital. When later compared with the data,
|
|
|
|
* the sign extension must have happened.
|
|
|
|
*/
|
|
|
|
case BYTE:
|
|
|
|
v = (char) v;
|
|
|
|
break;
|
|
|
|
case SHORT:
|
|
|
|
case BESHORT:
|
|
|
|
case LESHORT:
|
|
|
|
v = (short) v;
|
|
|
|
break;
|
|
|
|
case DATE:
|
|
|
|
case BEDATE:
|
|
|
|
case LEDATE:
|
|
|
|
case LONG:
|
|
|
|
case BELONG:
|
|
|
|
case LELONG:
|
1997-01-28 03:49:36 +03:00
|
|
|
v = (int32) v;
|
1993-11-03 07:04:19 +03:00
|
|
|
break;
|
1993-11-03 07:40:04 +03:00
|
|
|
case STRING:
|
|
|
|
break;
|
1993-11-03 07:04:19 +03:00
|
|
|
default:
|
|
|
|
magwarn("can't happen: m->type=%d\n",
|
|
|
|
m->type);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* parse one line from magic file, put into magic[index++] if valid
|
|
|
|
*/
|
1993-06-10 04:37:55 +04:00
|
|
|
static int
|
1993-03-21 12:45:37 +03:00
|
|
|
parse(l, ndx, check)
|
2000-09-22 20:34:59 +04:00
|
|
|
char *l;
|
|
|
|
int *ndx, check;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int i = 0, nd = *ndx;
|
|
|
|
struct magic *m;
|
1993-06-10 04:37:55 +04:00
|
|
|
char *t, *s;
|
1999-12-31 16:07:59 +03:00
|
|
|
struct magic *newmagic;
|
1993-06-10 04:37:55 +04:00
|
|
|
|
|
|
|
if (nd+1 >= maxmagic){
|
1999-12-31 16:07:59 +03:00
|
|
|
if ((newmagic = (struct magic *) realloc(magic,
|
1993-06-10 04:37:55 +04:00
|
|
|
sizeof(struct magic) *
|
1999-09-04 23:51:11 +04:00
|
|
|
maxmagic * 2)) == NULL) {
|
1993-06-10 04:37:55 +04:00
|
|
|
(void) fprintf(stderr, "%s: Out of memory.\n", progname);
|
2000-05-15 02:53:37 +04:00
|
|
|
if (magic)
|
|
|
|
free(magic);
|
1993-06-10 04:37:55 +04:00
|
|
|
if (check)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-12-31 16:07:59 +03:00
|
|
|
magic = newmagic;
|
1999-09-04 23:51:11 +04:00
|
|
|
memset(&magic[maxmagic], 0, sizeof(struct magic) * maxmagic);
|
|
|
|
maxmagic *= 2;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
m = &magic[*ndx];
|
1993-06-10 04:37:55 +04:00
|
|
|
m->flag = 0;
|
|
|
|
m->cont_level = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-06-10 04:37:55 +04:00
|
|
|
while (*l == '>') {
|
1993-03-21 12:45:37 +03:00
|
|
|
++l; /* step over */
|
1993-06-10 04:37:55 +04:00
|
|
|
m->cont_level++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->cont_level != 0 && *l == '(') {
|
|
|
|
++l; /* step over */
|
|
|
|
m->flag |= INDIR;
|
|
|
|
}
|
1996-10-06 00:20:24 +04:00
|
|
|
if (m->cont_level != 0 && *l == '&') {
|
|
|
|
++l; /* step over */
|
|
|
|
m->flag |= ADD;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* get offset, then skip over it */
|
1995-03-26 01:35:28 +03:00
|
|
|
m->offset = (int) strtoul(l,&t,0);
|
1993-06-10 04:37:55 +04:00
|
|
|
if (l == t)
|
|
|
|
magwarn("offset %s invalid", l);
|
|
|
|
l = t;
|
|
|
|
|
|
|
|
if (m->flag & INDIR) {
|
|
|
|
m->in.type = LONG;
|
|
|
|
m->in.offset = 0;
|
|
|
|
/*
|
|
|
|
* read [.lbs][+-]nnnnn)
|
|
|
|
*/
|
|
|
|
if (*l == '.') {
|
1995-04-28 23:23:38 +04:00
|
|
|
l++;
|
1998-09-20 19:27:15 +04:00
|
|
|
switch (*l) {
|
1993-06-10 04:37:55 +04:00
|
|
|
case 'l':
|
1998-09-20 19:27:15 +04:00
|
|
|
m->in.type = LELONG;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
m->in.type = BELONG;
|
1993-06-10 04:37:55 +04:00
|
|
|
break;
|
1995-04-28 23:23:38 +04:00
|
|
|
case 'h':
|
1993-06-10 04:37:55 +04:00
|
|
|
case 's':
|
1998-09-20 19:27:15 +04:00
|
|
|
m->in.type = LESHORT;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
case 'S':
|
|
|
|
m->in.type = BESHORT;
|
1993-06-10 04:37:55 +04:00
|
|
|
break;
|
1995-04-28 23:23:38 +04:00
|
|
|
case 'c':
|
1993-06-10 04:37:55 +04:00
|
|
|
case 'b':
|
1998-09-20 19:27:15 +04:00
|
|
|
case 'C':
|
|
|
|
case 'B':
|
1993-06-10 04:37:55 +04:00
|
|
|
m->in.type = BYTE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
magwarn("indirect offset type %c invalid", *l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
s = l;
|
|
|
|
if (*l == '+' || *l == '-') l++;
|
|
|
|
if (isdigit((unsigned char)*l)) {
|
1995-03-26 01:35:28 +03:00
|
|
|
m->in.offset = strtoul(l, &t, 0);
|
|
|
|
if (*s == '-') m->in.offset = - m->in.offset;
|
1993-06-10 04:37:55 +04:00
|
|
|
}
|
1995-03-26 01:35:28 +03:00
|
|
|
else
|
|
|
|
t = l;
|
1993-06-10 04:37:55 +04:00
|
|
|
if (*t++ != ')')
|
|
|
|
magwarn("missing ')' in indirect offset");
|
|
|
|
l = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (isascii((unsigned char)*l) && isdigit((unsigned char)*l))
|
1993-03-21 12:45:37 +03:00
|
|
|
++l;
|
|
|
|
EATAB;
|
|
|
|
|
1993-06-10 04:37:55 +04:00
|
|
|
#define NBYTE 4
|
|
|
|
#define NSHORT 5
|
|
|
|
#define NLONG 4
|
|
|
|
#define NSTRING 6
|
|
|
|
#define NDATE 4
|
|
|
|
#define NBESHORT 7
|
|
|
|
#define NBELONG 6
|
|
|
|
#define NBEDATE 6
|
|
|
|
#define NLESHORT 7
|
|
|
|
#define NLELONG 6
|
|
|
|
#define NLEDATE 6
|
|
|
|
|
1993-11-03 07:04:19 +03:00
|
|
|
if (*l == 'u') {
|
|
|
|
++l;
|
|
|
|
m->flag |= UNSIGNED;
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/* get type, skip it */
|
1999-11-01 20:39:26 +03:00
|
|
|
if (strncmp(l, "char", NBYTE)==0) { /* HP/UX compat */
|
|
|
|
m->type = BYTE;
|
|
|
|
l += NBYTE;
|
|
|
|
} else if (strncmp(l, "byte", NBYTE)==0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
m->type = BYTE;
|
|
|
|
l += NBYTE;
|
|
|
|
} else if (strncmp(l, "short", NSHORT)==0) {
|
|
|
|
m->type = SHORT;
|
|
|
|
l += NSHORT;
|
|
|
|
} else if (strncmp(l, "long", NLONG)==0) {
|
|
|
|
m->type = LONG;
|
|
|
|
l += NLONG;
|
|
|
|
} else if (strncmp(l, "string", NSTRING)==0) {
|
|
|
|
m->type = STRING;
|
|
|
|
l += NSTRING;
|
1993-06-10 04:37:55 +04:00
|
|
|
} else if (strncmp(l, "date", NDATE)==0) {
|
|
|
|
m->type = DATE;
|
|
|
|
l += NDATE;
|
|
|
|
} else if (strncmp(l, "beshort", NBESHORT)==0) {
|
|
|
|
m->type = BESHORT;
|
|
|
|
l += NBESHORT;
|
|
|
|
} else if (strncmp(l, "belong", NBELONG)==0) {
|
|
|
|
m->type = BELONG;
|
|
|
|
l += NBELONG;
|
|
|
|
} else if (strncmp(l, "bedate", NBEDATE)==0) {
|
|
|
|
m->type = BEDATE;
|
|
|
|
l += NBEDATE;
|
|
|
|
} else if (strncmp(l, "leshort", NLESHORT)==0) {
|
|
|
|
m->type = LESHORT;
|
|
|
|
l += NLESHORT;
|
|
|
|
} else if (strncmp(l, "lelong", NLELONG)==0) {
|
|
|
|
m->type = LELONG;
|
|
|
|
l += NLELONG;
|
|
|
|
} else if (strncmp(l, "ledate", NLEDATE)==0) {
|
|
|
|
m->type = LEDATE;
|
|
|
|
l += NLEDATE;
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
1993-06-10 04:37:55 +04:00
|
|
|
magwarn("type %s invalid", l);
|
1993-03-21 12:45:37 +03:00
|
|
|
return -1;
|
|
|
|
}
|
1993-06-10 04:37:55 +04:00
|
|
|
/* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
|
|
|
|
if (*l == '&') {
|
1993-03-21 12:45:37 +03:00
|
|
|
++l;
|
1995-03-26 01:35:28 +03:00
|
|
|
m->mask = signextend(m, strtoul(l, &l, 0));
|
1995-04-28 23:23:38 +04:00
|
|
|
eatsize(&l);
|
2000-05-15 02:53:37 +04:00
|
|
|
} else if (STRING == m->type) {
|
|
|
|
m->mask = 0L;
|
|
|
|
if (*l == '/') {
|
|
|
|
while (!isspace(*++l)) {
|
|
|
|
switch (*l) {
|
|
|
|
case CHAR_IGNORE_LOWERCASE:
|
|
|
|
m->mask |= STRING_IGNORE_LOWERCASE;
|
|
|
|
break;
|
|
|
|
case CHAR_COMPACT_BLANK:
|
|
|
|
m->mask |= STRING_COMPACT_BLANK;
|
|
|
|
break;
|
|
|
|
case CHAR_COMPACT_OPTIONAL_BLANK:
|
|
|
|
m->mask |=
|
|
|
|
STRING_COMPACT_OPTIONAL_BLANK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
magwarn("string extension %c invalid",
|
|
|
|
*l);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
} else
|
1995-03-26 01:35:28 +03:00
|
|
|
m->mask = ~0L;
|
1993-03-21 12:45:37 +03:00
|
|
|
EATAB;
|
1993-06-10 04:37:55 +04:00
|
|
|
|
|
|
|
switch (*l) {
|
|
|
|
case '>':
|
|
|
|
case '<':
|
|
|
|
/* Old-style anding: "0 byte &0x80 dynamically linked" */
|
|
|
|
case '&':
|
|
|
|
case '^':
|
|
|
|
case '=':
|
|
|
|
m->reln = *l;
|
|
|
|
++l;
|
1999-11-01 20:39:26 +03:00
|
|
|
if (*l == '=') {
|
|
|
|
/* HP compat: ignore &= etc. */
|
|
|
|
++l;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1993-06-10 04:37:55 +04:00
|
|
|
case '!':
|
|
|
|
if (m->type != STRING) {
|
|
|
|
m->reln = *l;
|
|
|
|
++l;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALL THROUGH */
|
1993-03-21 12:45:37 +03:00
|
|
|
default:
|
1993-06-10 04:37:55 +04:00
|
|
|
if (*l == 'x' && isascii((unsigned char)l[1]) &&
|
|
|
|
isspace((unsigned char)l[1])) {
|
|
|
|
m->reln = *l;
|
|
|
|
++l;
|
|
|
|
goto GetDesc; /* Bill The Cat */
|
|
|
|
}
|
|
|
|
m->reln = '=';
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1993-06-10 04:37:55 +04:00
|
|
|
EATAB;
|
|
|
|
|
|
|
|
if (getvalue(m, &l))
|
|
|
|
return -1;
|
|
|
|
/*
|
|
|
|
* TODO finish this macro and start using it!
|
|
|
|
* #define offsetcheck {if (offset > HOWMANY-1)
|
|
|
|
* magwarn("offset too big"); }
|
|
|
|
*/
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* now get last part - the description
|
|
|
|
*/
|
1993-06-10 04:37:55 +04:00
|
|
|
GetDesc:
|
1993-03-21 12:45:37 +03:00
|
|
|
EATAB;
|
1993-06-10 04:37:55 +04:00
|
|
|
if (l[0] == '\b') {
|
|
|
|
++l;
|
|
|
|
m->nospflag = 1;
|
|
|
|
} else if ((l[0] == '\\') && (l[1] == 'b')) {
|
|
|
|
++l;
|
|
|
|
++l;
|
|
|
|
m->nospflag = 1;
|
|
|
|
} else
|
|
|
|
m->nospflag = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
while ((m->desc[i++] = *l++) != '\0' && i<MAXDESC)
|
|
|
|
/* NULLBODY */;
|
|
|
|
|
|
|
|
if (check) {
|
|
|
|
mdump(m);
|
|
|
|
}
|
|
|
|
++(*ndx); /* make room for next */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-06-10 04:37:55 +04:00
|
|
|
/*
|
|
|
|
* Read a numeric value from a pointer, into the value union of a magic
|
|
|
|
* pointer, according to the magic type. Update the string pointer to point
|
|
|
|
* just after the number read. Return 0 for success, non-zero for failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
getvalue(m, p)
|
2000-09-22 20:34:59 +04:00
|
|
|
struct magic *m;
|
|
|
|
char **p;
|
1993-06-10 04:37:55 +04:00
|
|
|
{
|
|
|
|
int slen;
|
|
|
|
|
|
|
|
if (m->type == STRING) {
|
|
|
|
*p = getstr(*p, m->value.s, sizeof(m->value.s), &slen);
|
|
|
|
m->vallen = slen;
|
1993-11-03 07:04:19 +03:00
|
|
|
} else
|
1995-04-28 23:23:38 +04:00
|
|
|
if (m->reln != 'x') {
|
1995-03-26 01:35:28 +03:00
|
|
|
m->value.l = signextend(m, strtoul(*p, p, 0));
|
1995-04-30 07:28:14 +04:00
|
|
|
eatsize(p);
|
1995-04-28 23:23:38 +04:00
|
|
|
}
|
1993-06-10 04:37:55 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Convert a string containing C character escapes. Stop at an unescaped
|
|
|
|
* space or tab.
|
|
|
|
* Copy the converted version to "p", returning its length in *slen.
|
|
|
|
* Return updated scan pointer as function result.
|
|
|
|
*/
|
1993-06-10 04:37:55 +04:00
|
|
|
static char *
|
1993-03-21 12:45:37 +03:00
|
|
|
getstr(s, p, plen, slen)
|
2000-09-22 20:34:59 +04:00
|
|
|
char *s;
|
|
|
|
char *p;
|
|
|
|
int plen, *slen;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char *origs = s, *origp = p;
|
|
|
|
char *pmax = p + plen - 1;
|
2000-09-22 20:34:59 +04:00
|
|
|
int c;
|
|
|
|
int val;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-06-10 04:37:55 +04:00
|
|
|
while ((c = *s++) != '\0') {
|
|
|
|
if (isspace((unsigned char) c))
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (p >= pmax) {
|
|
|
|
fprintf(stderr, "String too long: %s\n", origs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(c == '\\') {
|
|
|
|
switch(c = *s++) {
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
default:
|
1993-06-10 04:37:55 +04:00
|
|
|
*p++ = (char) c;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
*p++ = '\n';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
*p++ = '\r';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
*p++ = '\b';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
*p++ = '\t';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
*p++ = '\f';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
*p++ = '\v';
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* \ and up to 3 octal digits */
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
val = c - '0';
|
|
|
|
c = *s++; /* try for 2 */
|
|
|
|
if(c >= '0' && c <= '7') {
|
|
|
|
val = (val<<3) | (c - '0');
|
|
|
|
c = *s++; /* try for 3 */
|
|
|
|
if(c >= '0' && c <= '7')
|
|
|
|
val = (val<<3) | (c-'0');
|
|
|
|
else
|
|
|
|
--s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
--s;
|
1993-06-10 04:37:55 +04:00
|
|
|
*p++ = (char)val;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
1997-01-28 03:49:36 +03:00
|
|
|
/* \x and up to 2 hex digits */
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'x':
|
|
|
|
val = 'x'; /* Default if no digits */
|
|
|
|
c = hextoint(*s++); /* Get next char */
|
|
|
|
if (c >= 0) {
|
|
|
|
val = c;
|
|
|
|
c = hextoint(*s++);
|
1997-01-28 03:49:36 +03:00
|
|
|
if (c >= 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
val = (val << 4) + c;
|
1997-01-28 03:49:36 +03:00
|
|
|
else
|
1993-03-21 12:45:37 +03:00
|
|
|
--s;
|
|
|
|
} else
|
|
|
|
--s;
|
1993-06-10 04:37:55 +04:00
|
|
|
*p++ = (char)val;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
1993-06-10 04:37:55 +04:00
|
|
|
*p++ = (char)c;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
out:
|
|
|
|
*p = '\0';
|
|
|
|
*slen = p - origp;
|
1993-06-10 04:37:55 +04:00
|
|
|
return s;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Single hex char to int; -1 if not a hex char. */
|
1993-06-10 04:37:55 +04:00
|
|
|
static int
|
1993-03-21 12:45:37 +03:00
|
|
|
hextoint(c)
|
2000-09-22 20:34:59 +04:00
|
|
|
int c;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2000-09-22 20:34:59 +04:00
|
|
|
if (!isascii((unsigned char) c))
|
|
|
|
return -1;
|
|
|
|
if (isdigit((unsigned char) c))
|
|
|
|
return c - '0';
|
|
|
|
if ((c >= 'a')&&(c <= 'f'))
|
|
|
|
return c + 10 - 'a';
|
|
|
|
if (( c>= 'A')&&(c <= 'F'))
|
|
|
|
return c + 10 - 'A';
|
|
|
|
return -1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a string containing C character escapes.
|
|
|
|
*/
|
|
|
|
void
|
1995-03-26 01:35:28 +03:00
|
|
|
showstr(fp, s, len)
|
2000-09-22 20:34:59 +04:00
|
|
|
FILE *fp;
|
|
|
|
const char *s;
|
|
|
|
int len;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2000-09-22 20:34:59 +04:00
|
|
|
char c;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-26 01:35:28 +03:00
|
|
|
for (;;) {
|
|
|
|
c = *s++;
|
|
|
|
if (len == -1) {
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (len-- == 0)
|
|
|
|
break;
|
|
|
|
}
|
1993-06-10 04:37:55 +04:00
|
|
|
if(c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
|
1995-03-26 01:35:28 +03:00
|
|
|
(void) fputc(c, fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
else {
|
1995-03-26 01:35:28 +03:00
|
|
|
(void) fputc('\\', fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case '\n':
|
1995-03-26 01:35:28 +03:00
|
|
|
(void) fputc('n', fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
1995-03-26 01:35:28 +03:00
|
|
|
(void) fputc('r', fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\b':
|
1995-03-26 01:35:28 +03:00
|
|
|
(void) fputc('b', fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\t':
|
1995-03-26 01:35:28 +03:00
|
|
|
(void) fputc('t', fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\f':
|
1995-03-26 01:35:28 +03:00
|
|
|
(void) fputc('f', fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\v':
|
1995-03-26 01:35:28 +03:00
|
|
|
(void) fputc('v', fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1995-03-26 01:35:28 +03:00
|
|
|
(void) fprintf(fp, "%.3o", c & 0377);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-04-28 23:23:38 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* eatsize(): Eat the size spec from a number [eg. 10UL]
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
eatsize(p)
|
2000-09-22 20:34:59 +04:00
|
|
|
char **p;
|
1995-04-28 23:23:38 +04:00
|
|
|
{
|
|
|
|
char *l = *p;
|
|
|
|
|
|
|
|
if (LOWCASE(*l) == 'u')
|
|
|
|
l++;
|
|
|
|
|
|
|
|
switch (LOWCASE(*l)) {
|
|
|
|
case 'l': /* long */
|
|
|
|
case 's': /* short */
|
|
|
|
case 'h': /* short */
|
|
|
|
case 'b': /* char/byte */
|
|
|
|
case 'c': /* char/byte */
|
|
|
|
l++;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = l;
|
|
|
|
}
|