summaryrefslogtreecommitdiffstats
path: root/contrib/file/softmagic.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/file/softmagic.c')
-rw-r--r--contrib/file/softmagic.c113
1 files changed, 76 insertions, 37 deletions
diff --git a/contrib/file/softmagic.c b/contrib/file/softmagic.c
index 4e063b9..1202f72 100644
--- a/contrib/file/softmagic.c
+++ b/contrib/file/softmagic.c
@@ -12,11 +12,6 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by Ian F. Darwin and others.
- * 4. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@@ -44,7 +39,7 @@
#ifndef lint
-FILE_RCSID("@(#)$Id: softmagic.c,v 1.66 2004/07/24 20:38:56 christos Exp $")
+FILE_RCSID("@(#)$Id: softmagic.c,v 1.72 2004/11/24 17:38:25 christos Exp $")
#endif /* lint */
private int match(struct magic_set *, struct magic *, uint32_t,
@@ -54,6 +49,8 @@ private int mget(struct magic_set *, union VALUETYPE *, const unsigned char *,
private int mcheck(struct magic_set *, union VALUETYPE *, struct magic *);
private int32_t mprint(struct magic_set *, union VALUETYPE *, struct magic *);
private void mdebug(uint32_t, const char *, size_t);
+private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
+ const unsigned char *, size_t, size_t);
private int mconvert(struct magic_set *, union VALUETYPE *, struct magic *);
private int check_mem(struct magic_set *, unsigned int);
@@ -276,6 +273,8 @@ mprint(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
case FILE_STRING:
case FILE_PSTRING:
+ case FILE_BESTRING16:
+ case FILE_LESTRING16:
if (m->reln == '=') {
if (file_printf(ms, m->desc, m->value.s) == -1)
return -1;
@@ -426,28 +425,30 @@ mconvert(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
p->l = ~p->l;
return 1;
case FILE_STRING:
+ case FILE_BESTRING16:
+ case FILE_LESTRING16:
{
- int n;
+ size_t len;
/* Null terminate and eat *trailing* return */
p->s[sizeof(p->s) - 1] = '\0';
- n = strlen(p->s) - 1;
- if (p->s[n] == '\n')
- p->s[n] = '\0';
+ len = strlen(p->s);
+ if (len-- && p->s[len] == '\n')
+ p->s[len] = '\0';
return 1;
}
case FILE_PSTRING:
{
char *ptr1 = p->s, *ptr2 = ptr1 + 1;
- unsigned int n = *p->s;
- if (n >= sizeof(p->s))
- n = sizeof(p->s) - 1;
- while (n--)
+ size_t len = *p->s;
+ if (len >= sizeof(p->s))
+ len = sizeof(p->s) - 1;
+ while (len--)
*ptr1++ = *ptr2++;
*ptr1 = '\0';
- n = strlen(p->s) - 1;
- if (p->s[n] == '\n')
- p->s[n] = '\0';
+ len = strlen(p->s);
+ if (len-- && p->s[len] == '\n')
+ p->s[len] = '\0';
return 1;
}
case FILE_BESHORT:
@@ -603,12 +604,10 @@ mdebug(uint32_t offset, const char *str, size_t len)
}
private int
-mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
- struct magic *m, size_t nbytes)
+mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
+ const unsigned char *s, size_t offset, size_t nbytes)
{
- uint32_t offset = m->offset;
-
- if (m->type == FILE_REGEX) {
+ if (type == FILE_REGEX && indir == 0) {
/*
* offset is interpreted as last line to search,
* (starting at 1), not as bytes-from start-of-file
@@ -624,18 +623,59 @@ mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
last = b;
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
- */
- memset(p, 0, sizeof(union VALUETYPE));
- if (offset < nbytes)
- memcpy(p, s + offset, nbytes - offset);
+ return 0;
}
+ if (indir == 0 && (type == FILE_BESTRING16 || type == FILE_LESTRING16))
+ {
+ const char *src = s + offset;
+ const char *esrc = s + nbytes;
+ char *dst = p->s, *edst = &p->s[sizeof(p->s) - 1];
+
+ if (type == FILE_BESTRING16)
+ src++;
+
+ for (;src < esrc; src++, dst++) {
+ if (dst < edst)
+ *dst = *src++;
+ else
+ break;
+ if (*dst == '\0')
+ *dst = ' ';
+ }
+ *edst = '\0';
+ return 0;
+ }
+
+ if (offset >= nbytes) {
+ (void)memset(p, '\0', sizeof(*p));
+ return 0;
+ }
+ if (nbytes - offset < sizeof(*p))
+ nbytes = nbytes - offset;
+ else
+ nbytes = sizeof(*p);
+
+ (void)memcpy(p, s + offset, nbytes);
+
+ /*
+ * the usefulness of padding with zeroes eludes me, it
+ * might even cause problems
+ */
+ if (nbytes < sizeof(*p))
+ (void)memset(((char *)p) + nbytes, '\0', sizeof(*p) - nbytes);
+ return 0;
+}
+
+private int
+mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
+ struct magic *m, size_t nbytes)
+{
+ uint32_t offset = m->offset;
+
+ if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes) == -1)
+ return -1;
+
/* Verify we have enough data to match magic type */
switch (m->type) {
case FILE_BYTE:
@@ -1022,11 +1062,8 @@ mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
break;
}
- if (nbytes < sizeof(union VALUETYPE) ||
- nbytes - sizeof(union VALUETYPE) < offset)
- return 0;
-
- memcpy(p, s + offset, sizeof(union VALUETYPE));
+ if (mcopy(ms, p, m->type, 0, s, offset, nbytes) == -1)
+ return -1;
if ((ms->flags & MAGIC_DEBUG) != 0) {
mdebug(offset, (char *)(void *)p,
@@ -1075,6 +1112,8 @@ mcheck(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
break;
case FILE_STRING:
+ case FILE_BESTRING16:
+ case FILE_LESTRING16:
case FILE_PSTRING:
{
/*
OpenPOWER on IntegriCloud