diff options
| author | jtc <jtc@NetBSD.org> | 1993-06-10 00:37:55 +0000 |
|---|---|---|
| committer | jtc <jtc@NetBSD.org> | 1993-06-10 00:37:55 +0000 |
| commit | ebefd0d08f67d607cf2c3de3e29eebefa732d0f6 (patch) | |
| tree | cc513781643290d7ffa886bd58cb9b81cd5f10a0 /usr.bin/file/apprentice.c | |
| parent | f6779cbdcef13546d3ccf87a68698916e9bda1d2 (diff) | |
Updated to Ian Darwin's latest release.
Adds lots of neat features from most other vendors file(1) commands,
plus (important!) little and big endian file types. This will alow
us to detect and extract meaningful information from a many more
file types.
Diffstat (limited to 'usr.bin/file/apprentice.c')
| -rw-r--r-- | usr.bin/file/apprentice.c | 325 |
1 files changed, 238 insertions, 87 deletions
diff --git a/usr.bin/file/apprentice.c b/usr.bin/file/apprentice.c index 6664945b294..42f05d56972 100644 --- a/usr.bin/file/apprentice.c +++ b/usr.bin/file/apprentice.c @@ -26,33 +26,34 @@ */ #include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <ctype.h> #include "file.h" #ifndef lint static char *moduleid = - "@(#)$Header: /cvsroot/src/usr.bin/file/Attic/apprentice.c,v 1.1.1.1 1993/03/21 09:45:37 cgd Exp $"; + "@(#)$Id: apprentice.c,v 1.2 1993/06/10 00:38:02 jtc Exp $"; #endif /* lint */ -#define MAXSTR 500 -#define EATAB {while (isascii(*l) && isspace(*l)) ++l;} +#define EATAB {while (isascii((unsigned char) *l) && \ + isspace((unsigned char) *l)) ++l;} -extern char *progname; -extern char *magicfile; -extern int debug; /* option */ -extern int nmagic; /* number of valid magic[]s */ -extern long strtol(); -struct magic magic[MAXMAGIS]; +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)); -char *getstr(); +static int maxmagic = 0; +int apprentice(fn, check) char *fn; /* name of magic file */ -int check; /* non-zero: checking-only run. */ +int check; /* non-zero? checking-only run. */ { FILE *f; - char line[MAXSTR+1]; + char line[BUFSIZ+1]; int errs = 0; f = fopen(fn, "r"); @@ -65,14 +66,24 @@ int check; /* non-zero: checking-only run. */ exit(1); } + maxmagic = MAXMAGIS; + if ((magic = (struct magic *) malloc(sizeof(struct magic) * maxmagic)) + == NULL) { + (void) fprintf(stderr, "%s: Out of memory.\n", progname); + if (check) + return -1; + else + exit(1); + } + /* parse it */ if (check) /* print silly verbose header for USG compat. */ - (void) printf("cont\toffset\ttype\topcode\tvalue\tdesc\n"); + (void) printf("cont\toffset\ttype\topcode\tmask\tvalue\tdesc\n"); - while (fgets(line, MAXSTR, f) != NULL) { + for (lineno = 1;fgets(line, BUFSIZ, f) != NULL; lineno++) { if (line[0]=='#') /* comment, do not parse */ continue; - if (strlen(line) <= 1) /* null line, garbage, etc */ + if (strlen(line) <= (unsigned)1) /* null line, garbage, etc */ continue; line[strlen(line)-1] = '\0'; /* delete newline */ if (parse(line, &nmagic, check) != 0) @@ -86,45 +97,98 @@ int check; /* non-zero: checking-only run. */ /* * parse one line from magic file, put into magic[index++] if valid */ -int +static int parse(l, ndx, check) char *l; int *ndx, check; { int i = 0, nd = *ndx; - int slen; - static int warned = 0; struct magic *m; - extern int errno; - - /* - * TODO malloc the magic structures (linked list?) so this can't happen - */ - if (nd+1 >= MAXMAGIS){ - if (warned++ == 0) - warning( -"magic table overflow - increase MAXMAGIS beyond %d in file/apprentice.c\n", - MAXMAGIS); - return -1; + char *t, *s; + + if (nd+1 >= maxmagic){ + maxmagic += 20; + if ((magic = (struct magic *) realloc(magic, + sizeof(struct magic) * + maxmagic)) == NULL) { + (void) fprintf(stderr, "%s: Out of memory.\n", progname); + if (check) + return -1; + else + exit(1); + } } m = &magic[*ndx]; + m->flag = 0; + m->cont_level = 0; - if (*l == '>') { + while (*l == '>') { ++l; /* step over */ - m->contflag = 1; - } else - m->contflag = 0; + m->cont_level++; + } + + if (m->cont_level != 0 && *l == '(') { + ++l; /* step over */ + m->flag |= INDIR; + } /* get offset, then skip over it */ - m->offset = atoi(l); - while (isascii(*l) && isdigit(*l)) + m->offset = (int) strtol(l,&t,0); + 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 == '.') { + switch (*++l) { + case 'l': + m->in.type = LONG; + break; + case 's': + m->in.type = SHORT; + break; + case 'b': + 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)) { + m->in.offset = strtol(l, &t, 0); + if (*s == '-') m->in.offset = - m->in.offset; + } + if (*t++ != ')') + magwarn("missing ')' in indirect offset"); + l = t; + } + + + while (isascii((unsigned char)*l) && isdigit((unsigned char)*l)) ++l; EATAB; -#define NBYTE 4 -#define NSHORT 5 -#define NLONG 4 -#define NSTRING 6 +#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 + /* get type, skip it */ if (strncmp(l, "byte", NBYTE)==0) { m->type = BYTE; @@ -138,52 +202,90 @@ int *ndx, check; } else if (strncmp(l, "string", NSTRING)==0) { m->type = STRING; l += NSTRING; + } 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; } else { - errno = 0; - warning("type %s invalid", l); + magwarn("type %s invalid", l); return -1; } - EATAB; - - if (*l == '>' || *l == '<' || *l == '&' || *l == '=') { - m->reln = *l; + /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */ + if (*l == '&') { ++l; + m->mask = strtol(l, &l, 0); } else - m->reln = '='; + m->mask = 0L; EATAB; - -/* - * TODO finish this macro and start using it! - * #define offsetcheck {if (offset > HOWMANY-1) warning("offset too big"); } - */ - 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: - m->value.l = (char) strtol(l,&l,0); - break; - case SHORT: - m->value.l = (short) strtol(l,&l,0); - break; - case LONG: - m->value.l = (long) strtol(l,&l,0); - break; - case STRING: - l = getstr(l, m->value.s, sizeof(m->value.s), &slen); - m->vallen = slen; + + switch (*l) { + case '>': + case '<': + /* Old-style anding: "0 byte &0x80 dynamically linked" */ + case '&': + case '^': + case '=': + m->reln = *l; + ++l; break; + case '!': + if (m->type != STRING) { + m->reln = *l; + ++l; + break; + } + /* FALL THROUGH */ default: - warning("can't happen: m->type=%d\n", m->type); - return -1; + 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; } + 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"); } + */ /* * now get last part - the description */ +GetDesc: EATAB; + if (l[0] == '\b') { + ++l; + m->nospflag = 1; + } else if ((l[0] == '\\') && (l[1] == 'b')) { + ++l; + ++l; + m->nospflag = 1; + } else + m->nospflag = 0; while ((m->desc[i++] = *l++) != '\0' && i<MAXDESC) /* NULLBODY */; @@ -194,13 +296,61 @@ int *ndx, check; return 0; } +/* + * 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) +struct magic *m; +char **p; +{ + int slen; + + if (m->type == STRING) { + *p = getstr(*p, m->value.s, sizeof(m->value.s), &slen); + m->vallen = slen; + } else { + if (m->reln != 'x') { + 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: + m->value.l = (char) strtol(*p,p,0); + break; + case SHORT: + case BESHORT: + case LESHORT: + m->value.l = (short) strtol(*p,p,0); + break; + case DATE: + case BEDATE: + case LEDATE: + case LONG: + case BELONG: + case LELONG: + m->value.l = (long) strtol(*p,p,0); + break; + default: + magwarn("can't happen: m->type=%d\n", m->type); + return -1; + } + } + } + return 0; +} + /* * 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. */ -char * +static char * getstr(s, p, plen, slen) register char *s; register char *p; @@ -211,8 +361,9 @@ int plen, *slen; register int c; register int val; - while((c = *s++) != '\0') { - if (isspace(c)) break; + while ((c = *s++) != '\0') { + if (isspace((unsigned char) c)) + break; if (p >= pmax) { fprintf(stderr, "String too long: %s\n", origs); break; @@ -224,7 +375,7 @@ int plen, *slen; goto out; default: - *p++ = c; + *p++ = (char) c; break; case 'n': @@ -272,7 +423,7 @@ int plen, *slen; } else --s; - *p++ = val; + *p++ = (char)val; break; /* \x and up to 3 hex digits */ @@ -293,28 +444,28 @@ int plen, *slen; --s; } else --s; - *p++ = val; + *p++ = (char)val; break; } } else - *p++ = c; + *p++ = (char)c; } out: *p = '\0'; *slen = p - origp; - return(s); + return s; } /* Single hex char to int; -1 if not a hex char. */ -int +static int hextoint(c) - char c; +int c; { - if (!isascii(c)) return -1; - if (isdigit(c)) return c - '0'; - if ((c>='a')&(c<='f')) return c + 10 - 'a'; - if ((c>='A')&(c<='F')) return c + 10 - 'A'; + 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; } @@ -324,12 +475,12 @@ hextoint(c) */ void showstr(s) -register char *s; +const char *s; { register char c; while((c = *s++) != '\0') { - if(c >= 040 && c <= 0176) + if(c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */ putchar(c); else { putchar('\\'); |
