Alexander Larsson 08def5
--- rpm-4.4.2/file/src/softmagic.c	2005-07-16 19:14:21.000000000 -0400
Alexander Larsson 08def5
+++ rpm/file/src/softmagic.c	2005-11-15 19:06:51.000000000 -0500
Alexander Larsson 08def5
@@ -1,3 +1,8 @@
Alexander Larsson 08def5
+/*@-compdef@*/
Alexander Larsson 08def5
+/*@-evalorder@*/
Alexander Larsson 08def5
+/*@-sizeoftype@*/
Alexander Larsson 08def5
+/*@-uniondef@*/
Alexander Larsson 08def5
+/*@-usereleased@*/
Alexander Larsson 08def5
 /*
Alexander Larsson 08def5
  * Copyright (c) Ian F. Darwin 1986-1995.
Alexander Larsson 08def5
  * Software written by Ian F. Darwin and others;
Alexander Larsson 08def5
@@ -39,7 +44,7 @@
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 #ifndef	lint
Alexander Larsson 08def5
-FILE_RCSID("@(#)$Id: softmagic.c,v 1.73 2005/03/06 05:58:22 christos Exp $")
Alexander Larsson 08def5
+FILE_RCSID("@(#)$Id: softmagic.c,v 1.76 2005/10/17 19:04:36 christos Exp $")
Alexander Larsson 08def5
 #endif	/* lint */
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 private int match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
Alexander Larsson 08def5
@@ -47,10 +52,10 @@
Alexander Larsson 08def5
 	/*@globals fileSystem @*/
Alexander Larsson 08def5
 	/*@modifies ms, magic, fileSystem @*/;
Alexander Larsson 08def5
 private int mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
Alexander Larsson 08def5
-    struct magic *m, size_t nbytes, int)
Alexander Larsson 08def5
+    struct magic *m, size_t nbytes, unsigned int cont_level)
Alexander Larsson 08def5
 	/*@globals fileSystem @*/
Alexander Larsson 08def5
 	/*@modifies ms, p, m, fileSystem @*/;
Alexander Larsson 08def5
-private int mcheck(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
Alexander Larsson 08def5
+private int mymcheck(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
Alexander Larsson 08def5
 	/*@globals fileSystem @*/
Alexander Larsson 08def5
 	/*@modifies ms, p, m, fileSystem @*/;
Alexander Larsson 08def5
 private int32_t mprint(struct magic_set *ms, union VALUETYPE *p, struct magic *m)	
Alexander Larsson 08def5
@@ -133,7 +138,7 @@
Alexander Larsson 08def5
 		if (flush) {
Alexander Larsson 08def5
 			if (magic[magindex].reln == '!') flush = 0;
Alexander Larsson 08def5
 		} else {	
Alexander Larsson 08def5
-			switch (mcheck(ms, &p, &magic[magindex])) {
Alexander Larsson 08def5
+			switch (mymcheck(ms, &p, &magic[magindex])) {
Alexander Larsson 08def5
 			case -1:
Alexander Larsson 08def5
 				return -1;
Alexander Larsson 08def5
 			case 0:
Alexander Larsson 08def5
@@ -195,7 +200,7 @@
Alexander Larsson 08def5
 			if (flush && magic[magindex].reln != '!')
Alexander Larsson 08def5
 				goto done;
Alexander Larsson 08def5
 				
Alexander Larsson 08def5
-			switch (flush ? 1 : mcheck(ms, &p, &magic[magindex])) {
Alexander Larsson 08def5
+			switch (flush ? 1 : mymcheck(ms, &p, &magic[magindex])) {
Alexander Larsson 08def5
 			case -1:
Alexander Larsson 08def5
 				return -1;
Alexander Larsson 08def5
 			case 0:
Alexander Larsson 08def5
@@ -639,25 +644,26 @@
Alexander Larsson 08def5
 		 * (starting at 1), not as bytes-from start-of-file
Alexander Larsson 08def5
 		 */
Alexander Larsson 08def5
 		char *b, *c, *last = NULL;
Alexander Larsson 08def5
-		if ((p->buf = strdup((const char *)s)) == NULL) {
Alexander Larsson 08def5
+		if ((p->search.buf = strdup((const char *)s)) == NULL) {
Alexander Larsson 08def5
 			file_oomem(ms);
Alexander Larsson 08def5
 			return -1;
Alexander Larsson 08def5
 		}
Alexander Larsson 08def5
-		for (b = p->buf; offset && 
Alexander Larsson 08def5
-		    ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
Alexander Larsson 08def5
+		for (b = p->search.buf; offset && 
Alexander Larsson 08def5
+		    ((b = strchr(c = b, '\n')) != NULL || (b = strchr(c, '\r')) != NULL);
Alexander Larsson 08def5
 		    offset--, b++) {
Alexander Larsson 08def5
 			last = b;
Alexander Larsson 08def5
 			if (b[0] == '\r' && b[1] == '\n') b++;
Alexander Larsson 08def5
 		}
Alexander Larsson 08def5
 		if (last != NULL)
Alexander Larsson 08def5
 			*last = '\0';
Alexander Larsson 08def5
+		p->search.buflen = last - p->search.buf;
Alexander Larsson 08def5
 		return 0;
Alexander Larsson 08def5
 	}
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 	if (indir == 0 && (type == FILE_BESTRING16 || type == FILE_LESTRING16))
Alexander Larsson 08def5
 	{
Alexander Larsson 08def5
-		const char *src = s + offset;
Alexander Larsson 08def5
-		const char *esrc = s + nbytes;
Alexander Larsson 08def5
+		const unsigned char *src = s + offset;
Alexander Larsson 08def5
+		const unsigned char *esrc = s + nbytes;
Alexander Larsson 08def5
 		char *dst = p->s, *edst = &p->s[sizeof(p->s) - 1];
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 		if (type == FILE_BESTRING16)
Alexander Larsson 08def5
@@ -691,13 +697,14 @@
Alexander Larsson 08def5
 	 * might even cause problems
Alexander Larsson 08def5
 	 */
Alexander Larsson 08def5
 	if (nbytes < sizeof(*p))
Alexander Larsson 08def5
-		(void)memset(((char *)p) + nbytes, '\0', sizeof(*p) - nbytes);
Alexander Larsson 08def5
+		(void)memset(((char *)(void *)p) + nbytes, '\0',
Alexander Larsson 08def5
+		    sizeof(*p) - nbytes);
Alexander Larsson 08def5
 	return 0;
Alexander Larsson 08def5
 }
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 private int
Alexander Larsson 08def5
 mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
Alexander Larsson 08def5
-    struct magic *m, size_t nbytes, int cont_level)
Alexander Larsson 08def5
+    struct magic *m, size_t nbytes, unsigned int cont_level)
Alexander Larsson 08def5
 {
Alexander Larsson 08def5
 	uint32_t offset = m->offset;
Alexander Larsson 08def5
 
Alexander Larsson 08def5
@@ -713,7 +720,7 @@
Alexander Larsson 08def5
 		int off = m->in_offset;
Alexander Larsson 08def5
 		if (m->in_op & FILE_OPINDIRECT) {
Alexander Larsson 08def5
 			const union VALUETYPE *q =
Alexander Larsson 08def5
-			    ((const union VALUETYPE *)(s + offset + off));
Alexander Larsson 08def5
+			    ((const void *)(s + offset + off));
Alexander Larsson 08def5
 			switch (m->in_type) {
Alexander Larsson 08def5
 			case FILE_BYTE:
Alexander Larsson 08def5
 				off = q->b;
Alexander Larsson 08def5
@@ -1148,12 +1155,18 @@
Alexander Larsson 08def5
 	}
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 	if (m->type == FILE_SEARCH) {
Alexander Larsson 08def5
-		p->buf = malloc(m->mask + m->vallen);
Alexander Larsson 08def5
-		if (p->buf == NULL) {
Alexander Larsson 08def5
+		size_t mlen = m->mask + m->vallen;
Alexander Larsson 08def5
+		size_t flen = nbytes - offset;
Alexander Larsson 08def5
+		if (flen < mlen)
Alexander Larsson 08def5
+			mlen = flen;
Alexander Larsson 08def5
+		p->search.buflen = mlen;
Alexander Larsson 08def5
+		p->search.buf = malloc(mlen + 1);
Alexander Larsson 08def5
+		if (p->search.buf == NULL) {
Alexander Larsson 08def5
 			file_error(ms, errno, "Cannot allocate search buffer");
Alexander Larsson 08def5
 			return 0;
Alexander Larsson 08def5
 		}
Alexander Larsson 08def5
-		(void)memcpy(p->buf, s + offset, m->mask + m->vallen);
Alexander Larsson 08def5
+		(void)memcpy(p->search.buf, s + offset, mlen);
Alexander Larsson 08def5
+		p->search.buf[mlen] = '\0';
Alexander Larsson 08def5
 	}
Alexander Larsson 08def5
 	if (!mconvert(ms, p, m))
Alexander Larsson 08def5
 		return 0;
Alexander Larsson 08def5
@@ -1161,7 +1174,7 @@
Alexander Larsson 08def5
 }
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 private int
Alexander Larsson 08def5
-mcheck(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
Alexander Larsson 08def5
+mymcheck(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
Alexander Larsson 08def5
 {
Alexander Larsson 08def5
 	uint32_t l = m->value.l;
Alexander Larsson 08def5
 	uint32_t v;
Alexander Larsson 08def5
@@ -1250,20 +1263,23 @@
Alexander Larsson 08def5
 		regex_t rx;
Alexander Larsson 08def5
 		char errmsg[512];
Alexander Larsson 08def5
 
Alexander Larsson 08def5
+		if (p->search.buf == NULL)
Alexander Larsson 08def5
+			return 0;
Alexander Larsson 08def5
+
Alexander Larsson 08def5
 		rc = regcomp(&rx, m->value.s,
Alexander Larsson 08def5
 		    REG_EXTENDED|REG_NOSUB|REG_NEWLINE|
Alexander Larsson 08def5
 		    ((m->mask & STRING_IGNORE_LOWERCASE) ? REG_ICASE : 0));
Alexander Larsson 08def5
 		if (rc) {
Alexander Larsson 08def5
-			free(p->buf);
Alexander Larsson 08def5
-			regerror(rc, &rx, errmsg, sizeof(errmsg));
Alexander Larsson 08def5
+			free(p->search.buf);
Alexander Larsson 08def5
+			p->search.buf = NULL;
Alexander Larsson 08def5
+			(void) regerror(rc, &rx, errmsg, sizeof(errmsg));
Alexander Larsson 08def5
 			file_error(ms, 0, "regex error %d, (%s)", rc, errmsg);
Alexander Larsson 08def5
 			return -1;
Alexander Larsson 08def5
 		} else {
Alexander Larsson 08def5
-/*@-immediatetrans -moduncon -noeffectuncon @*/	/* regfree annotate bogus only @*/
Alexander Larsson 08def5
-			rc = regexec(&rx, p->buf, 0, 0, 0);
Alexander Larsson 08def5
-			regfree(&rx);
Alexander Larsson 08def5
-/*@=immediatetrans =moduncon =noeffectuncon @*/
Alexander Larsson 08def5
-			free(p->buf);
Alexander Larsson 08def5
+/*@i@*/			rc = regexec(&rx, p->search.buf, 0, 0, 0);
Alexander Larsson 08def5
+/*@i@*/			regfree(&rx);
Alexander Larsson 08def5
+			free(p->search.buf);
Alexander Larsson 08def5
+			p->search.buf = NULL;
Alexander Larsson 08def5
 			return !rc;
Alexander Larsson 08def5
 		}
Alexander Larsson 08def5
 	}
Alexander Larsson 08def5
@@ -1273,23 +1289,31 @@
Alexander Larsson 08def5
 		 * search for a string in a certain range
Alexander Larsson 08def5
 		 */
Alexander Larsson 08def5
 		unsigned char *a = (unsigned char*)m->value.s;
Alexander Larsson 08def5
-		unsigned char *b = (unsigned char*)p->buf;
Alexander Larsson 08def5
-		int len = m->vallen;
Alexander Larsson 08def5
-		int range = 0;
Alexander Larsson 08def5
+		unsigned char *b = (unsigned char*)p->search.buf;
Alexander Larsson 08def5
+		size_t len, slen = m->vallen;
Alexander Larsson 08def5
+		size_t range = 0;
Alexander Larsson 08def5
+		if (slen > sizeof(m->value.s))
Alexander Larsson 08def5
+			slen = sizeof(m->value.s);
Alexander Larsson 08def5
 		l = 0;
Alexander Larsson 08def5
 		v = 0;
Alexander Larsson 08def5
+		if (b == NULL)
Alexander Larsson 08def5
+			return 0;
Alexander Larsson 08def5
+		len = slen;
Alexander Larsson 08def5
 		while (++range <= m->mask) {
Alexander Larsson 08def5
 			while (len-- > 0 && (v = *b++ - *a++) == 0)
Alexander Larsson 08def5
 				/*@innercontinue@*/ continue;
Alexander Larsson 08def5
 			if (!v) {
Alexander Larsson 08def5
-				m->offset += range-1;
Alexander Larsson 08def5
+				m->offset += range - 1;
Alexander Larsson 08def5
 				/*@loopbreak@*/ break;
Alexander Larsson 08def5
 			}
Alexander Larsson 08def5
-			len = m->vallen;
Alexander Larsson 08def5
+			if (range + slen >= p->search.buflen)
Alexander Larsson 08def5
+				/*@loopbreak@*/ break;
Alexander Larsson 08def5
+			len = slen;
Alexander Larsson 08def5
 			a = (unsigned char*)m->value.s;
Alexander Larsson 08def5
-			b = (unsigned char*)p->buf + range;
Alexander Larsson 08def5
+			b = (unsigned char*)p->search.buf + range;
Alexander Larsson 08def5
 		}
Alexander Larsson 08def5
-		free(p->buf);
Alexander Larsson 08def5
+		free(p->search.buf);
Alexander Larsson 08def5
+		p->search.buf = NULL;
Alexander Larsson 08def5
 		break;
Alexander Larsson 08def5
 	}
Alexander Larsson 08def5
 	default:
Alexander Larsson 08def5
@@ -1374,3 +1398,8 @@
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 	return matched;
Alexander Larsson 08def5
 }
Alexander Larsson 08def5
+/*@=usereleased@*/
Alexander Larsson 08def5
+/*@=uniondef@*/
Alexander Larsson 08def5
+/*@=sizeoftype@*/
Alexander Larsson 08def5
+/*@=evalorder@*/
Alexander Larsson 08def5
+/*@=compdef@*/ 
Alexander Larsson 08def5
--- rpm-4.4.2/file/src/file.h	2005-07-16 19:14:21.000000000 -0400
Alexander Larsson 08def5
+++ rpm/file/src/file.h	2005-11-15 19:06:51.000000000 -0500
Alexander Larsson 08def5
@@ -27,7 +27,7 @@
Alexander Larsson 08def5
  */
Alexander Larsson 08def5
 /*
Alexander Larsson 08def5
  * file.h - definitions for file(1) program
Alexander Larsson 08def5
- * @(#)$Id: file.h,v 1.68 2005/06/25 15:52:14 christos Exp $
Alexander Larsson 08def5
+ * @(#)$Id: file.h,v 1.72 2005/10/17 15:36:22 christos Exp $
Alexander Larsson 08def5
  */
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 #ifndef __file_h__
Alexander Larsson 08def5
@@ -39,6 +39,7 @@
Alexander Larsson 08def5
 
Alexander Larsson 08def5
 #include <stdio.h>	/* Include that here, to make sure __P gets defined */
Alexander Larsson 08def5
 #include <errno.h>
Alexander Larsson 08def5
+#include <fcntl.h>	/* For open and flags */
Alexander Larsson 08def5
 #ifdef HAVE_STDINT_H
Alexander Larsson 08def5
 #include <stdint.h>
Alexander Larsson 08def5
 #endif
Alexander Larsson 08def5
@@ -194,8 +195,11 @@
Alexander Larsson 08def5
 		uint16_t h;
Alexander Larsson 08def5
 		uint32_t l;
Alexander Larsson 08def5
 		char s[MAXstring];
Alexander Larsson 08def5
+		struct {
Alexander Larsson 08def5
 /*@relnull@*/
Alexander Larsson 08def5
-		char *buf;
Alexander Larsson 08def5
+			char *buf;
Alexander Larsson 08def5
+			size_t buflen;
Alexander Larsson 08def5
+		} search;
Alexander Larsson 08def5
 		uint8_t hs[2];	/* 2 bytes of a fixed-endian "short" */
Alexander Larsson 08def5
 		uint8_t hl[4];	/* 4 bytes of a fixed-endian "long" */
Alexander Larsson 08def5
 	} value;		/* either number or string */
Alexander Larsson 08def5
@@ -240,6 +244,7 @@
Alexander Larsson 08def5
 	size_t len;
Alexander Larsson 08def5
 	size_t size;
Alexander Larsson 08def5
 	/* Printable buffer */
Alexander Larsson 08def5
+/*@only@*/
Alexander Larsson 08def5
 	char *pbuf;
Alexander Larsson 08def5
 	size_t psize;
Alexander Larsson 08def5
     } o;
Alexander Larsson 08def5
@@ -295,9 +300,9 @@
Alexander Larsson 08def5
 	/*@modifies ms @*/;
Alexander Larsson 08def5
 protected void file_oomem(struct magic_set *ms)
Alexander Larsson 08def5
 	/*@modifies ms @*/;
Alexander Larsson 08def5
-protected void file_error(struct magic_set *ms, int, const char *, ...)
Alexander Larsson 08def5
+protected void file_error(struct magic_set *ms, int, /*@null@*/ const char *, ...)
Alexander Larsson 08def5
 	/*@modifies ms @*/;
Alexander Larsson 08def5
-protected void file_magwarn(struct magic_set *ms, const char *, ...)
Alexander Larsson 08def5
+protected void file_magwarn(struct magic_set *ms, /*@null@*/ const char *, ...)
Alexander Larsson 08def5
 	/*@globals fileSystem @*/
Alexander Larsson 08def5
 	/*@modifies fileSystem @*/;
Alexander Larsson 08def5
 protected void file_mdump(struct magic *m)
Alexander Larsson 08def5
@@ -323,10 +328,18 @@
Alexander Larsson 08def5
 #define strtoul(a, b, c)	strtol(a, b, c)
Alexander Larsson 08def5
 #endif
Alexander Larsson 08def5
 
Alexander Larsson 08def5
+#if 0 /* HACK -- we have snprintf and I don't want to plumb the autofoo */
Alexander Larsson 08def5
+int snprintf(char *, size_t, const char *, ...);
Alexander Larsson 08def5
+#endif
Alexander Larsson 08def5
+
Alexander Larsson 08def5
 #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
Alexander Larsson 08def5
 #define QUICK
Alexander Larsson 08def5
 #endif
Alexander Larsson 08def5
 
Alexander Larsson 08def5
+#ifndef O_BINARY
Alexander Larsson 08def5
+#define O_BINARY	0
Alexander Larsson 08def5
+#endif
Alexander Larsson 08def5
+
Alexander Larsson 08def5
 #define FILE_RCSID(id) \
Alexander Larsson 08def5
 static const char *rcsid(const char *p) { \
Alexander Larsson 08def5
 	return rcsid(p = id); \