092ea1
From 3951ed6ab1ba4b7d6d4d2dd5700858c470627c46 Mon Sep 17 00:00:00 2001
092ea1
From: Vincent Mihalkovic <vmihalko@redhat.com>
092ea1
Date: Thu, 9 Feb 2023 16:46:43 +0100
092ea1
Subject: [PATCH] store copy of the mode info in the magic_set
092ea1
092ea1
---
092ea1
 src/file.h      |  1 +
092ea1
 src/funcs.c     | 53 +++++++++++++++++++++++++++++++------------------
092ea1
 src/softmagic.c | 27 +++++++++++--------------
092ea1
 3 files changed, 47 insertions(+), 34 deletions(-)
092ea1
092ea1
diff --git a/src/file.h b/src/file.h
092ea1
index 66598bc..b3d015d 100644
092ea1
--- a/src/file.h
092ea1
+++ b/src/file.h
092ea1
@@ -413,6 +413,7 @@ struct magic_set {
092ea1
 #define 		EVENT_HAD_ERR		0x01
092ea1
 	const char *file;
092ea1
 	size_t line;			/* current magic line number */
092ea1
+	mode_t mode;			/* copy of current stat mode */
092ea1
 
092ea1
 	/* data for searches */
092ea1
 	struct {
092ea1
diff --git a/src/funcs.c b/src/funcs.c
092ea1
index f59f4a1..0bf92fe 100644
092ea1
--- a/src/funcs.c
092ea1
+++ b/src/funcs.c
092ea1
@@ -27,7 +27,7 @@
092ea1
 #include "file.h"
092ea1
 
092ea1
 #ifndef	lint
092ea1
-FILE_RCSID("@(#)$File: funcs.c,v 1.94 2017/11/02 20:25:39 christos Exp $")
092ea1
+FILE_RCSID("@(#)$File: funcs.c,v 1.95 2018/05/24 18:09:17 christos Exp $")
092ea1
 #endif	/* lint */
092ea1
 
092ea1
 #include "magic.h"
092ea1
@@ -183,9 +183,11 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u
092ea1
 	const char *type = "application/octet-stream";
092ea1
 	const char *def = "data";
092ea1
 	const char *ftype = NULL;
092ea1
+	char *rbuf = NULL;
092ea1
 	struct buffer b;
092ea1
 
092ea1
 	buffer_init(&b, fd, buf, nb);
092ea1
+	ms->mode = b.st.st_mode;
092ea1
 
092ea1
 	if (nb == 0) {
092ea1
 		def = "empty";
092ea1
@@ -248,31 +250,43 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u
092ea1
 				goto done;
092ea1
 		}
092ea1
 	}
092ea1
+#ifdef BUILTIN_ELF
092ea1
+	if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && nb > 5 && fd != -1) {
092ea1
+		file_pushbuf_t *pb;
092ea1
+		/*
092ea1
+		 * We matched something in the file, so this
092ea1
+		 * *might* be an ELF file, and the file is at
092ea1
+		 * least 5 bytes long, so if it's an ELF file
092ea1
+		 * it has at least one byte past the ELF magic
092ea1
+		 * number - try extracting information from the
092ea1
+		 * ELF headers that cannot easily be  extracted
092ea1
+		 * with rules in the magic file. We we don't
092ea1
+		 * print the information yet.
092ea1
+		 */
092ea1
+		if ((pb = file_push_buffer(ms)) == NULL)
092ea1
+			return -1;
092ea1
+
092ea1
+		rv = file_tryelf(ms, &b);
092ea1
+		rbuf = file_pop_buffer(ms, pb);
092ea1
+		if (rv != 1) {
092ea1
+			free(rbuf);
092ea1
+			rbuf = NULL;
092ea1
+		}
092ea1
+		if ((ms->flags & MAGIC_DEBUG) != 0)
092ea1
+			(void)fprintf(stderr, "[try elf %d]\n", m);
092ea1
+	}
092ea1
+#endif
092ea1
 
092ea1
 	/* try soft magic tests */
092ea1
 	if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
092ea1
 		m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text);
092ea1
 		if ((ms->flags & MAGIC_DEBUG) != 0)
092ea1
 			(void)fprintf(stderr, "[try softmagic %d]\n", m);
092ea1
+		if (m == 1 && rbuf) {
092ea1
+			if (file_printf(ms, "%s", rbuf) == -1)
092ea1
+				goto done;
092ea1
+		}
092ea1
 		if (m) {
092ea1
-#ifdef BUILTIN_ELF
092ea1
-			if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
092ea1
-			    nb > 5 && fd != -1) {
092ea1
-				/*
092ea1
-				 * We matched something in the file, so this
092ea1
-				 * *might* be an ELF file, and the file is at
092ea1
-				 * least 5 bytes long, so if it's an ELF file
092ea1
-				 * it has at least one byte past the ELF magic
092ea1
-				 * number - try extracting information from the
092ea1
-				 * ELF headers that cannot easily * be
092ea1
-				 * extracted with rules in the magic file.
092ea1
-				 */
092ea1
-				m = file_tryelf(ms, &b);
092ea1
-				if ((ms->flags & MAGIC_DEBUG) != 0)
092ea1
-					(void)fprintf(stderr, "[try elf %d]\n",
092ea1
-					    m);
092ea1
-			}
092ea1
-#endif
092ea1
 			if (checkdone(ms, &rv))
092ea1
 				goto done;
092ea1
 		}
092ea1
@@ -318,6 +332,7 @@ simple:
092ea1
 #if HAVE_FORK
092ea1
  done_encoding:
092ea1
 #endif
092ea1
+	free(rbuf);
092ea1
 	buffer_fini(&b);
092ea1
 	if (rv)
092ea1
 		return rv;
092ea1
diff --git a/src/softmagic.c b/src/softmagic.c
092ea1
index 57b4677..0197ec4 100644
092ea1
--- a/src/softmagic.c
092ea1
+++ b/src/softmagic.c
092ea1
@@ -54,8 +54,7 @@ private int mget(struct magic_set *, struct magic *, const struct buffer *,
092ea1
 private int msetoffset(struct magic_set *, struct magic *, struct buffer *,
092ea1
     const struct buffer *, size_t, unsigned int);
092ea1
 private int magiccheck(struct magic_set *, struct magic *);
092ea1
-private int32_t mprint(struct magic_set *, struct magic *,
092ea1
-    const struct buffer *);
092ea1
+private int32_t mprint(struct magic_set *, struct magic *);
092ea1
 private int moffset(struct magic_set *, struct magic *, const struct buffer *,
092ea1
     int32_t *);
092ea1
 private void mdebug(uint32_t, const char *, size_t);
092ea1
@@ -63,8 +62,7 @@ private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
092ea1
     const unsigned char *, uint32_t, size_t, struct magic *);
092ea1
 private int mconvert(struct magic_set *, struct magic *, int);
092ea1
 private int print_sep(struct magic_set *, int);
092ea1
-private int handle_annotation(struct magic_set *, struct magic *,
092ea1
-    const struct buffer *, int);
092ea1
+private int handle_annotation(struct magic_set *, struct magic *, int);
092ea1
 private int cvt_8(union VALUETYPE *, const struct magic *);
092ea1
 private int cvt_16(union VALUETYPE *, const struct magic *);
092ea1
 private int cvt_32(union VALUETYPE *, const struct magic *);
092ea1
@@ -241,7 +239,7 @@ flush:
092ea1
 			goto flush;
092ea1
 		}
092ea1
 
092ea1
-		if ((e = handle_annotation(ms, m, b, firstline)) != 0) {
092ea1
+		if ((e = handle_annotation(ms, m, firstline)) != 0) {
092ea1
 			*need_separator = 1;
092ea1
 			*printed_something = 1;
092ea1
 			*returnval = 1;
092ea1
@@ -259,7 +257,7 @@ flush:
092ea1
 				return -1;
092ea1
 		}
092ea1
 
092ea1
-		if (print && mprint(ms, m, b) == -1)
092ea1
+		if (print && mprint(ms, m) == -1)
092ea1
 			return -1;
092ea1
 
092ea1
 		switch (moffset(ms, m, &bb, &ms->c.li[cont_level].off)) {
092ea1
@@ -340,7 +338,7 @@ flush:
092ea1
 				} else
092ea1
 					ms->c.li[cont_level].got_match = 1;
092ea1
 
092ea1
-				if ((e = handle_annotation(ms, m, b, firstline))
092ea1
+				if ((e = handle_annotation(ms, m, firstline))
092ea1
 				    != 0) {
092ea1
 					*need_separator = 1;
092ea1
 					*printed_something = 1;
092ea1
@@ -374,7 +372,7 @@ flush:
092ea1
 						return -1;
092ea1
 					*need_separator = 0;
092ea1
 				}
092ea1
-				if (print && mprint(ms, m, b) == -1)
092ea1
+				if (print && mprint(ms, m) == -1)
092ea1
 					return -1;
092ea1
 
092ea1
 				switch (moffset(ms, m, &bb,
092ea1
@@ -454,7 +452,7 @@ strndup(const char *str, size_t n)
092ea1
 #endif /* HAVE_STRNDUP */
092ea1
 
092ea1
 static int
092ea1
-varexpand(char *buf, size_t len, const struct buffer *b, const char *str)
092ea1
+varexpand(struct magic_set *ms, char *buf, size_t len, const char *str)
092ea1
 {
092ea1
 	const char *ptr, *sptr, *e, *t, *ee, *et;
092ea1
 	size_t l;
092ea1
@@ -479,7 +477,7 @@ varexpand(char *buf, size_t len, const struct buffer *b, const char *str)
092ea1
 			return -1;
092ea1
 		switch (*ptr) {
092ea1
 		case 'x':
092ea1
-			if (b->st.st_mode & 0111) {
092ea1
+			if (ms->mode & 0111) {
092ea1
 				ptr = t;
092ea1
 				l = et - t;
092ea1
 			} else {
092ea1
@@ -509,7 +507,7 @@ varexpand(char *buf, size_t len, const struct buffer *b, const char *str)
092ea1
 
092ea1
 
092ea1
 private int32_t
092ea1
-mprint(struct magic_set *ms, struct magic *m, const struct buffer *b)
092ea1
+mprint(struct magic_set *ms, struct magic *m)
092ea1
 {
092ea1
 	uint64_t v;
092ea1
 	float vf;
092ea1
@@ -519,7 +517,7 @@ mprint(struct magic_set *ms, struct magic *m, const struct buffer *b)
092ea1
 	const char *desc;
092ea1
 	union VALUETYPE *p = &ms->ms_value;
092ea1
 
092ea1
-	if (varexpand(ebuf, sizeof(ebuf), b, m->desc) == -1)
092ea1
+	if (varexpand(ms, ebuf, sizeof(ebuf), m->desc) == -1)
092ea1
 		desc = m->desc;
092ea1
 	else
092ea1
 		desc = ebuf;
092ea1
@@ -2166,8 +2164,7 @@ magiccheck(struct magic_set *ms, struct magic *m)
092ea1
 }
092ea1
 
092ea1
 private int
092ea1
-handle_annotation(struct magic_set *ms, struct magic *m, const struct buffer *b,
092ea1
-    int firstline)
092ea1
+handle_annotation(struct magic_set *ms, struct magic *m, int firstline)
092ea1
 {
092ea1
 	if ((ms->flags & MAGIC_APPLE) && m->apple[0]) {
092ea1
 		if (!firstline && file_printf(ms, "\n- ") == -1)
092ea1
@@ -2188,7 +2185,7 @@ handle_annotation(struct magic_set *ms, struct magic *m, const struct buffer *b,
092ea1
 		const char *p;
092ea1
 		if (!firstline && file_printf(ms, "\n- ") == -1)
092ea1
 			return -1;
092ea1
-		if (varexpand(buf, sizeof(buf), b, m->mimetype) == -1)
092ea1
+		if (varexpand(ms, buf, sizeof(buf), m->mimetype) == -1)
092ea1
 			p = m->mimetype;
092ea1
 		else
092ea1
 			p = buf;
092ea1
-- 
092ea1
2.39.1
092ea1