diff options
author | obrien <obrien@FreeBSD.org> | 2002-09-16 05:54:26 +0000 |
---|---|---|
committer | obrien <obrien@FreeBSD.org> | 2002-09-16 05:54:26 +0000 |
commit | 2ef757c3c642f5ce8353e106c1fbbd3cea2b14c1 (patch) | |
tree | be5585d7b830cb36fbe379de5dc133810141410c /contrib/file/softmagic.c | |
parent | 7860fd7670d66336ca32454f171db9c04eb45f02 (diff) | |
download | FreeBSD-src-2ef757c3c642f5ce8353e106c1fbbd3cea2b14c1.zip FreeBSD-src-2ef757c3c642f5ce8353e106c1fbbd3cea2b14c1.tar.gz |
Virgin import of Christos Zoulas's FILE 3.39.
Diffstat (limited to 'contrib/file/softmagic.c')
-rw-r--r-- | contrib/file/softmagic.c | 161 |
1 files changed, 85 insertions, 76 deletions
diff --git a/contrib/file/softmagic.c b/contrib/file/softmagic.c index fc91615..891910c 100644 --- a/contrib/file/softmagic.c +++ b/contrib/file/softmagic.c @@ -25,39 +25,35 @@ * 4. This notice may not be removed or altered. */ -#include <stdio.h> +#include "file.h" #include <string.h> #include <ctype.h> #include <stdlib.h> #include <time.h> -#include <sys/types.h> +#include <regex.h> -#include "file.h" #ifndef lint -FILE_RCSID("@(#)$Id: softmagic.c,v 1.46 2001/07/23 00:02:32 christos Exp $") +FILE_RCSID("@(#)$Id: softmagic.c,v 1.51 2002/07/03 18:26:38 christos Exp $") #endif /* lint */ -static int match __P((struct magic *, uint32, unsigned char *, int)); -static int mget __P((union VALUETYPE *, - unsigned char *, struct magic *, int)); -static int mcheck __P((union VALUETYPE *, struct magic *)); -static int32 mprint __P((union VALUETYPE *, struct magic *)); -static void mdebug __P((int32, char *, int)); -static int mconvert __P((union VALUETYPE *, struct magic *)); +static int match(struct magic *, uint32_t, unsigned char *, int); +static int mget(union VALUETYPE *, unsigned char *, struct magic *, int); +static int mcheck(union VALUETYPE *, struct magic *); +static int32_t mprint(union VALUETYPE *, struct magic *); +static void mdebug(int32_t, char *, int); +static int mconvert(union VALUETYPE *, struct magic *); extern int kflag; /* * softmagic - lookup one file in database - * (already read from /etc/magic by apprentice.c). + * (already read from MAGIC by apprentice.c). * Passed the name and FILE * of one file to be typed. */ /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */ int -softmagic(buf, nbytes) - unsigned char *buf; - int nbytes; +softmagic(unsigned char *buf, int nbytes) { struct mlist *ml; @@ -96,24 +92,20 @@ softmagic(buf, nbytes) * so that higher-level continuations are processed. */ static int -match(magic, nmagic, s, nbytes) - struct magic *magic; - uint32 nmagic; - unsigned char *s; - int nbytes; +match(struct magic *magic, uint32_t nmagic, unsigned char *s, int nbytes) { int magindex = 0; int cont_level = 0; int need_separator = 0; union VALUETYPE p; - static int32 *tmpoff = NULL; + static int32_t *tmpoff = NULL; static size_t tmplen = 0; - int32 oldoff = 0; + int32_t oldoff = 0; int returnval = 0; /* if a match is found it is set to 1*/ int firstline = 1; /* a flag to print X\n X\n- X */ if (tmpoff == NULL) - if ((tmpoff = (int32 *) malloc(tmplen = 20)) == NULL) + if ((tmpoff = (int32_t *) malloc(tmplen = 20)) == NULL) error("out of memory\n"); for (magindex = 0; magindex < nmagic; magindex++) { @@ -144,7 +136,7 @@ match(magic, nmagic, s, nbytes) need_separator = 1; /* and any continuations that match */ if (++cont_level >= tmplen) - if ((tmpoff = (int32 *) realloc(tmpoff, + if ((tmpoff = (int32_t *) realloc(tmpoff, tmplen += 20)) == NULL) error("out of memory\n"); while (magic[magindex+1].cont_level != 0 && @@ -191,7 +183,7 @@ match(magic, nmagic, s, nbytes) */ if (++cont_level >= tmplen) if ((tmpoff = - (int32 *) realloc(tmpoff, + (int32_t *) realloc(tmpoff, tmplen += 20)) == NULL) error("out of memory\n"); } @@ -209,13 +201,11 @@ match(magic, nmagic, s, nbytes) return returnval; /* This is hit if -k is set or there is no match */ } -static int32 -mprint(p, m) - union VALUETYPE *p; - struct magic *m; +static int32_t +mprint(union VALUETYPE *p, struct magic *m) { - uint32 v; - int32 t=0 ; + uint32_t v; + int32_t t=0 ; switch (m->type) { @@ -237,8 +227,8 @@ mprint(p, m) case BELONG: case LELONG: v = signextend(m, p->l); - (void) printf(m->desc, (uint32) v); - t = m->offset + sizeof(int32); + (void) printf(m->desc, (uint32_t) v); + t = m->offset + sizeof(int32_t); break; case STRING: @@ -271,6 +261,10 @@ mprint(p, m) (void) printf(m->desc, fmttime(p->l, 0)); t = m->offset + sizeof(time_t); break; + case REGEX: + (void) printf(m->desc, p->s); + t = m->offset + strlen(p->s); + break; default: error("invalid m->type (%d) in mprint().\n", m->type); @@ -285,9 +279,7 @@ mprint(p, m) * (unless you have a better idea) */ static int -mconvert(p, m) - union VALUETYPE *p; - struct magic *m; +mconvert(union VALUETYPE *p, struct magic *m) { switch (m->type) { case BYTE: @@ -445,7 +437,7 @@ mconvert(p, m) case BELONG: case BEDATE: case BELDATE: - p->l = (int32) + p->l = (int32_t) ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3])); if (m->mask) switch (m->mask_op&0x7F) { @@ -512,7 +504,7 @@ mconvert(p, m) case LELONG: case LEDATE: case LELDATE: - p->l = (int32) + p->l = (int32_t) ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0])); if (m->mask) switch (m->mask_op&0x7F) { @@ -544,6 +536,8 @@ mconvert(p, m) if (m->mask_op & OPINVERSE) p->l = ~p->l; return 1; + case REGEX: + return 1; default: error("invalid type %d in mconvert().\n", m->type); return 0; @@ -552,10 +546,7 @@ mconvert(p, m) static void -mdebug(offset, str, len) - int32 offset; - char *str; - int len; +mdebug(int32_t offset, char *str, int len) { (void) fprintf(stderr, "mget @%d: ", offset); showstr(stderr, (char *) str, len); @@ -564,35 +555,40 @@ mdebug(offset, str, len) } static int -mget(p, s, m, nbytes) - union VALUETYPE* p; - unsigned char *s; - struct magic *m; - int nbytes; +mget(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes) { - int32 offset = m->offset; + int32_t offset = m->offset; - if (offset + sizeof(union VALUETYPE) <= nbytes) + if (m->type == REGEX) { + /* + * offset is interpreted as last line to search, + * (starting at 1), not as bytes-from start-of-file + */ + char *last = NULL; + p->buf = s; + for (; offset && (s = strchr(s, '\n')) != NULL; offset--, s++) + last = s; + if (last != NULL) + *last = '\0'; + } else if (offset + sizeof(union VALUETYPE) <= nbytes) memcpy(p, s + offset, sizeof(union VALUETYPE)); else { /* * the usefulness of padding with zeroes eludes me, it * might even cause problems */ - int32 have = nbytes - offset; + int32_t have = nbytes - offset; memset(p, 0, sizeof(union VALUETYPE)); if (have > 0) memcpy(p, s + offset, have); } - if (debug) { mdebug(offset, (char *) p, sizeof(union VALUETYPE)); mdump(m); } if (m->flag & INDIR) { - switch (m->in_type) { case BYTE: if (m->in_offset) @@ -754,56 +750,56 @@ mget(p, s, m, nbytes) if (m->in_offset) switch (m->in_op&0x7F) { case OPAND: - offset = (int32)((p->hl[0]<<24)| + offset = (int32_t)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) & m->in_offset; break; case OPOR: - offset = (int32)((p->hl[0]<<24)| + offset = (int32_t)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) | m->in_offset; break; case OPXOR: - offset = (int32)((p->hl[0]<<24)| + offset = (int32_t)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) ^ m->in_offset; break; case OPADD: - offset = (int32)((p->hl[0]<<24)| + offset = (int32_t)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) + m->in_offset; break; case OPMINUS: - offset = (int32)((p->hl[0]<<24)| + offset = (int32_t)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) - m->in_offset; break; case OPMULTIPLY: - offset = (int32)((p->hl[0]<<24)| + offset = (int32_t)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) * m->in_offset; break; case OPDIVIDE: - offset = (int32)((p->hl[0]<<24)| + offset = (int32_t)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) / m->in_offset; break; case OPMODULO: - offset = (int32)((p->hl[0]<<24)| + offset = (int32_t)((p->hl[0]<<24)| (p->hl[1]<<16)| (p->hl[2]<<8)| (p->hl[3])) % @@ -817,56 +813,56 @@ mget(p, s, m, nbytes) if (m->in_offset) switch (m->in_op&0x7F) { case OPAND: - offset = (int32)((p->hl[3]<<24)| + offset = (int32_t)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) & m->in_offset; break; case OPOR: - offset = (int32)((p->hl[3]<<24)| + offset = (int32_t)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) | m->in_offset; break; case OPXOR: - offset = (int32)((p->hl[3]<<24)| + offset = (int32_t)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) ^ m->in_offset; break; case OPADD: - offset = (int32)((p->hl[3]<<24)| + offset = (int32_t)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) + m->in_offset; break; case OPMINUS: - offset = (int32)((p->hl[3]<<24)| + offset = (int32_t)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) - m->in_offset; break; case OPMULTIPLY: - offset = (int32)((p->hl[3]<<24)| + offset = (int32_t)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) * m->in_offset; break; case OPDIVIDE: - offset = (int32)((p->hl[3]<<24)| + offset = (int32_t)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) / m->in_offset; break; case OPMODULO: - offset = (int32)((p->hl[3]<<24)| + offset = (int32_t)((p->hl[3]<<24)| (p->hl[2]<<16)| (p->hl[1]<<8)| (p->hl[0])) % @@ -932,12 +928,10 @@ mget(p, s, m, nbytes) } static int -mcheck(p, m) - union VALUETYPE* p; - struct magic *m; +mcheck(union VALUETYPE *p, struct magic *m) { - uint32 l = m->value.l; - uint32 v; + uint32_t l = m->value.l; + uint32_t v; int matched; if ( (m->value.s[0] == 'x') && (m->value.s[1] == '\0') ) { @@ -971,7 +965,7 @@ mcheck(p, m) case STRING: case PSTRING: - { + { /* * What we want here is: * v = strncmp(m->value.s, p->s, m->vallen); @@ -1016,6 +1010,21 @@ mcheck(p, m) } break; } + case REGEX: + { + int rc; + regex_t rx; + char errmsg[512]; + + rc = regcomp(&rx, m->value.s, REG_EXTENDED|REG_NOSUB); + if (rc) { + regerror(rc, &rx, errmsg, sizeof(errmsg)); + error("regex error %d, (%s)\n", rc, errmsg); + } else { + rc = regexec(&rx, p->buf, 0, 0, 0); + return !rc; + } + } default: error("invalid type %d in mcheck().\n", m->type); return 0;/*NOTREACHED*/ @@ -1053,7 +1062,7 @@ mcheck(p, m) v, l, matched); } else { - matched = (int32) v > (int32) l; + matched = (int32_t) v > (int32_t) l; if (debug) (void) fprintf(stderr, "%d > %d = %d\n", v, l, matched); @@ -1068,7 +1077,7 @@ mcheck(p, m) v, l, matched); } else { - matched = (int32) v < (int32) l; + matched = (int32_t) v < (int32_t) l; if (debug) (void) fprintf(stderr, "%d < %d = %d\n", v, l, matched); |