diff options
Diffstat (limited to 'contrib/file/softmagic.c')
-rw-r--r-- | contrib/file/softmagic.c | 592 |
1 files changed, 559 insertions, 33 deletions
diff --git a/contrib/file/softmagic.c b/contrib/file/softmagic.c index a32e2323fa25..1085dee4d693 100644 --- a/contrib/file/softmagic.c +++ b/contrib/file/softmagic.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$Id: softmagic.c,v 1.44 2001/03/17 19:32:50 christos Exp $") +FILE_RCSID("@(#)$Id: softmagic.c,v 1.45 2001/07/22 21:04:15 christos Exp $") #endif /* lint */ static int match __P((struct magic *, uint32, unsigned char *, int)); @@ -157,7 +157,7 @@ match(magic, nmagic, s, nbytes) */ cont_level = magic[magindex].cont_level; } - if (magic[magindex].flag & ADD) { + if (magic[magindex].flag & OFFADD) { oldoff=magic[magindex].offset; magic[magindex].offset += tmpoff[cont_level-1]; @@ -195,7 +195,7 @@ match(magic, nmagic, s, nbytes) tmplen += 20)) == NULL) error("out of memory\n"); } - if (magic[magindex].flag & ADD) { + if (magic[magindex].flag & OFFADD) { magic[magindex].offset = oldoff; } } @@ -215,6 +215,7 @@ mprint(p, m) struct magic *m; { char *pp, *rt; + char *oldtz, tz[16]; uint32 v; time_t curtime; int32 t=0 ; @@ -222,8 +223,7 @@ mprint(p, m) switch (m->type) { case BYTE: - v = p->b; - v = signextend(m, v) & m->mask; + v = signextend(m, p->b); (void) printf(m->desc, (unsigned char) v); t = m->offset + sizeof(char); break; @@ -231,8 +231,7 @@ mprint(p, m) case SHORT: case BESHORT: case LESHORT: - v = p->h; - v = signextend(m, v) & m->mask; + v = signextend(m, p->h); (void) printf(m->desc, (unsigned short) v); t = m->offset + sizeof(short); break; @@ -240,13 +239,13 @@ mprint(p, m) case LONG: case BELONG: case LELONG: - v = p->l; - v = signextend(m, v) & m->mask; + v = signextend(m, p->l); (void) printf(m->desc, (uint32) v); t = m->offset + sizeof(int32); break; case STRING: + case PSTRING: if (m->reln == '=') { (void) printf(m->desc, m->value.s); t = m->offset + strlen(m->value.s); @@ -265,11 +264,14 @@ mprint(p, m) case DATE: case BEDATE: case LEDATE: - curtime = p->l; - pp = ctime(&curtime); - if ((rt = strchr(pp, '\n')) != NULL) - *rt = '\0'; - (void) printf(m->desc, pp); + (void) printf(m->desc, fmttime(p->l, 1)); + t = m->offset + sizeof(time_t); + break; + + case LDATE: + case BELDATE: + case LELDATE: + (void) printf(m->desc, fmttime(p->l, 0)); t = m->offset + sizeof(time_t); break; @@ -282,6 +284,8 @@ mprint(p, m) /* * Convert the byte order of the data we are looking at + * While we're here, let's apply the mask operation + * (unless you have a better idea) */ static int mconvert(p, m) @@ -290,35 +294,259 @@ mconvert(p, m) { switch (m->type) { case BYTE: + if (m->mask) + switch (m->mask_op&0x7F) { + case OPAND: + p->b &= m->mask; + break; + case OPOR: + p->b |= m->mask; + break; + case OPXOR: + p->b ^= m->mask; + break; + case OPADD: + p->b += m->mask; + break; + case OPMINUS: + p->b -= m->mask; + break; + case OPMULTIPLY: + p->b *= m->mask; + break; + case OPDIVIDE: + p->b /= m->mask; + break; + case OPMODULO: + p->b %= m->mask; + break; + } + if (m->mask_op & OPINVERSE) + p->b = ~p->b; + return 1; case SHORT: + if (m->mask) + switch (m->mask_op&0x7F) { + case OPAND: + p->h &= m->mask; + break; + case OPOR: + p->h |= m->mask; + break; + case OPXOR: + p->h ^= m->mask; + break; + case OPADD: + p->h += m->mask; + break; + case OPMINUS: + p->h -= m->mask; + break; + case OPMULTIPLY: + p->h *= m->mask; + break; + case OPDIVIDE: + p->h /= m->mask; + break; + case OPMODULO: + p->h %= m->mask; + break; + } + if (m->mask_op & OPINVERSE) + p->h = ~p->h; + return 1; case LONG: case DATE: + case LDATE: + if (m->mask) + switch (m->mask_op&0x7F) { + case OPAND: + p->l &= m->mask; + break; + case OPOR: + p->l |= m->mask; + break; + case OPXOR: + p->l ^= m->mask; + break; + case OPADD: + p->l += m->mask; + break; + case OPMINUS: + p->l -= m->mask; + break; + case OPMULTIPLY: + p->l *= m->mask; + break; + case OPDIVIDE: + p->l /= m->mask; + break; + case OPMODULO: + p->l %= m->mask; + break; + } + if (m->mask_op & OPINVERSE) + p->l = ~p->l; return 1; case STRING: { char *ptr; + int n; - /* Null terminate and eat the return */ + /* Null terminate and eat *trailing* return */ p->s[sizeof(p->s) - 1] = '\0'; - if ((ptr = strchr(p->s, '\n')) != NULL) - *ptr = '\0'; + n = strlen(p->s) - 1; + if (p->s[n] == '\n') + p->s[n] = '\0'; + return 1; + } + case PSTRING: + { + char *ptr1 = p->s, *ptr2 = ptr1 + 1; + int n = *p->s; + if (n >= sizeof(p->s)) + n = sizeof(p->s) - 1; + while (n--) + *ptr1++ = *ptr2++; + *ptr1 = '\0'; + n = strlen(p->s) - 1; + if (p->s[n] == '\n') + p->s[n] = '\0'; return 1; } case BESHORT: p->h = (short)((p->hs[0]<<8)|(p->hs[1])); + if (m->mask) + switch (m->mask_op&0x7F) { + case OPAND: + p->h &= m->mask; + break; + case OPOR: + p->h |= m->mask; + break; + case OPXOR: + p->h ^= m->mask; + break; + case OPADD: + p->h += m->mask; + break; + case OPMINUS: + p->h -= m->mask; + break; + case OPMULTIPLY: + p->h *= m->mask; + break; + case OPDIVIDE: + p->h /= m->mask; + break; + case OPMODULO: + p->h %= m->mask; + break; + } + if (m->mask_op & OPINVERSE) + p->h = ~p->h; return 1; case BELONG: case BEDATE: + case BELDATE: p->l = (int32) ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3])); + if (m->mask) + switch (m->mask_op&0x7F) { + case OPAND: + p->l &= m->mask; + break; + case OPOR: + p->l |= m->mask; + break; + case OPXOR: + p->l ^= m->mask; + break; + case OPADD: + p->l += m->mask; + break; + case OPMINUS: + p->l -= m->mask; + break; + case OPMULTIPLY: + p->l *= m->mask; + break; + case OPDIVIDE: + p->l /= m->mask; + break; + case OPMODULO: + p->l %= m->mask; + break; + } + if (m->mask_op & OPINVERSE) + p->l = ~p->l; return 1; case LESHORT: p->h = (short)((p->hs[1]<<8)|(p->hs[0])); + if (m->mask) + switch (m->mask_op&0x7F) { + case OPAND: + p->h &= m->mask; + break; + case OPOR: + p->h |= m->mask; + break; + case OPXOR: + p->h ^= m->mask; + break; + case OPADD: + p->h += m->mask; + break; + case OPMINUS: + p->h -= m->mask; + break; + case OPMULTIPLY: + p->h *= m->mask; + break; + case OPDIVIDE: + p->h /= m->mask; + break; + case OPMODULO: + p->h %= m->mask; + break; + } + if (m->mask_op & OPINVERSE) + p->h = ~p->h; return 1; case LELONG: case LEDATE: + case LELDATE: p->l = (int32) ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0])); + if (m->mask) + switch (m->mask_op&0x7F) { + case OPAND: + p->l &= m->mask; + break; + case OPOR: + p->l |= m->mask; + break; + case OPXOR: + p->l ^= m->mask; + break; + case OPADD: + p->l += m->mask; + break; + case OPMINUS: + p->l -= m->mask; + break; + case OPMULTIPLY: + p->l *= m->mask; + break; + case OPDIVIDE: + p->l /= m->mask; + break; + case OPMODULO: + p->l %= m->mask; + break; + } + if (m->mask_op & OPINVERSE) + p->l = ~p->l; return 1; default: error("invalid type %d in mconvert().\n", m->type); @@ -371,31 +599,324 @@ mget(p, s, m, nbytes) switch (m->in_type) { case BYTE: - offset = p->b + m->in_offset; + if (m->in_offset) + switch (m->in_op&0x7F) { + case OPAND: + offset = p->b & m->in_offset; + break; + case OPOR: + offset = p->b | m->in_offset; + break; + case OPXOR: + offset = p->b ^ m->in_offset; + break; + case OPADD: + offset = p->b + m->in_offset; + break; + case OPMINUS: + offset = p->b - m->in_offset; + break; + case OPMULTIPLY: + offset = p->b * m->in_offset; + break; + case OPDIVIDE: + offset = p->b / m->in_offset; + break; + case OPMODULO: + offset = p->b % m->in_offset; + break; + } + if (m->in_op & OPINVERSE) + offset = ~offset; break; case BESHORT: - offset = (short)((p->hs[0]<<8)|(p->hs[1]))+ - m->in_offset; + if (m->in_offset) + switch (m->in_op&0x7F) { + case OPAND: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) & + m->in_offset; + break; + case OPOR: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) | + m->in_offset; + break; + case OPXOR: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) ^ + m->in_offset; + break; + case OPADD: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) + + m->in_offset; + break; + case OPMINUS: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) - + m->in_offset; + break; + case OPMULTIPLY: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) * + m->in_offset; + break; + case OPDIVIDE: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) / + m->in_offset; + break; + case OPMODULO: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) % + m->in_offset; + break; + } + if (m->in_op & OPINVERSE) + offset = ~offset; break; case LESHORT: - offset = (short)((p->hs[1]<<8)|(p->hs[0]))+ - m->in_offset; + if (m->in_offset) + switch (m->in_op&0x7F) { + case OPAND: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) & + m->in_offset; + break; + case OPOR: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) | + m->in_offset; + break; + case OPXOR: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) ^ + m->in_offset; + break; + case OPADD: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) + + m->in_offset; + break; + case OPMINUS: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) - + m->in_offset; + break; + case OPMULTIPLY: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) * + m->in_offset; + break; + case OPDIVIDE: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) / + m->in_offset; + break; + case OPMODULO: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) % + m->in_offset; + break; + } + if (m->in_op & OPINVERSE) + offset = ~offset; break; case SHORT: - offset = p->h + m->in_offset; + if (m->in_offset) + switch (m->in_op&0x7F) { + case OPAND: + offset = p->h & m->in_offset; + break; + case OPOR: + offset = p->h | m->in_offset; + break; + case OPXOR: + offset = p->h ^ m->in_offset; + break; + case OPADD: + offset = p->h + m->in_offset; + break; + case OPMINUS: + offset = p->h - m->in_offset; + break; + case OPMULTIPLY: + offset = p->h * m->in_offset; + break; + case OPDIVIDE: + offset = p->h / m->in_offset; + break; + case OPMODULO: + offset = p->h % m->in_offset; + break; + } + if (m->in_op & OPINVERSE) + offset = ~offset; break; case BELONG: - offset = (int32)((p->hl[0]<<24)|(p->hl[1]<<16)| - (p->hl[2]<<8)|(p->hl[3]))+ - m->in_offset; + if (m->in_offset) + switch (m->in_op&0x7F) { + case OPAND: + offset = (int32)((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)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) | + m->in_offset; + break; + case OPXOR: + offset = (int32)((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)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) + + m->in_offset; + break; + case OPMINUS: + offset = (int32)((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)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) * + m->in_offset; + break; + case OPDIVIDE: + offset = (int32)((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)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) % + m->in_offset; + break; + } + if (m->in_op & OPINVERSE) + offset = ~offset; break; case LELONG: - offset = (int32)((p->hl[3]<<24)|(p->hl[2]<<16)| - (p->hl[1]<<8)|(p->hl[0]))+ - m->in_offset; + if (m->in_offset) + switch (m->in_op&0x7F) { + case OPAND: + offset = (int32)((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)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) | + m->in_offset; + break; + case OPXOR: + offset = (int32)((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)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) + + m->in_offset; + break; + case OPMINUS: + offset = (int32)((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)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) * + m->in_offset; + break; + case OPDIVIDE: + offset = (int32)((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)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) % + m->in_offset; + break; + } + if (m->in_op & OPINVERSE) + offset = ~offset; break; case LONG: - offset = p->l + m->in_offset; + if (m->in_offset) + switch (m->in_op&0x7F) { + case OPAND: + offset = p->l & m->in_offset; + break; + case OPOR: + offset = p->l | m->in_offset; + break; + case OPXOR: + offset = p->l ^ m->in_offset; + break; + case OPADD: + offset = p->l + m->in_offset; + break; + case OPMINUS: + offset = p->l - m->in_offset; + break; + case OPMULTIPLY: + offset = p->l * m->in_offset; + break; + case OPDIVIDE: + offset = p->l / m->in_offset; + break; + case OPMODULO: + offset = p->l % m->in_offset; + break; + /* case TOOMANYSWITCHBLOCKS: + * ugh = p->eye % m->strain; + * rub; + * case BEER: + * off = p->tab & m->in_gest; + * sleep; + */ + } + if (m->in_op & OPINVERSE) + offset = ~offset; break; } @@ -446,10 +967,15 @@ mcheck(p, m) case DATE: case BEDATE: case LEDATE: + case LDATE: + case BELDATE: + case LELDATE: v = p->l; break; - case STRING: { + case STRING: + case PSTRING: + { /* * What we want here is: * v = strncmp(m->value.s, p->s, m->vallen); @@ -499,8 +1025,8 @@ mcheck(p, m) return 0;/*NOTREACHED*/ } - if(m->type != STRING) - v = signextend(m, v) & m->mask; + if(m->type != STRING && m->type != PSTRING) + v = signextend(m, v); switch (m->reln) { case 'x': |