[File] [PATCH] Skip tar C path for Gentoo binary package files

Michał Górny mgorny at gentoo.org
Mon Sep 5 14:19:35 UTC 2022


Detect Gentoo GLEP 78 (GPKG) binary package files in is_tar() function
and force falling back to using the magic rules for these files.  This
permits them to be correctly detected and described, rather than being
reported as a generic tar file.
---
 src/is_tar.c              |  12 ++++++++++++
 tests/Makefile.am         |   2 ++
 tests/gpkg-1-zst.result   |   1 +
 tests/gpkg-1-zst.testfile | Bin 0 -> 1024 bytes
 4 files changed, 15 insertions(+)
 create mode 100644 tests/gpkg-1-zst.result
 create mode 100644 tests/gpkg-1-zst.testfile

Please let me know if you prefer another format for the binary test case
file.

diff --git a/src/is_tar.c b/src/is_tar.c
index 82b08051..0feb7021 100644
--- a/src/is_tar.c
+++ b/src/is_tar.c
@@ -98,15 +98,27 @@ file_is_tar(struct magic_set *ms, const struct buffer *b)
 private int
 is_tar(const unsigned char *buf, size_t nbytes)
 {
+	static const char gpkg_match[] = "/gpkg-1";
+
 	const union record *header = RCAST(const union record *,
 	    RCAST(const void *, buf));
 	size_t i;
 	int sum, recsum;
 	const unsigned char *p, *ep;
+	const char *nulp;
 
 	if (nbytes < sizeof(*header))
 		return 0;
 
+	/* If the file looks like Gentoo GLEP 78 binary package (GPKG),
+	 * don't waste time on further checks and fall back to magic rules.
+	 */
+	nulp = memchr(header->header.name, 0, sizeof(header->header.name));
+	if (nulp != NULL && nulp >= header->header.name + sizeof(gpkg_match) &&
+	    memcmp(nulp - sizeof(gpkg_match) + 1, gpkg_match,
+	    sizeof(gpkg_match)) == 0)
+			return 0;
+
 	recsum = from_oct(header->header.chksum, sizeof(header->header.chksum));
 
 	sum = 0;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 22120a8b..0fa6aec2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -29,6 +29,8 @@ fit-map-data.result \
 fit-map-data.testfile \
 gedcom.result \
 gedcom.testfile \
+gpkg-1-zst.result \
+gpkg-1-zst.testfile \
 hddrawcopytool.result \
 hddrawcopytool.testfile \
 issue311docx.result \
diff --git a/tests/gpkg-1-zst.result b/tests/gpkg-1-zst.result
new file mode 100644
index 00000000..af9a080e
--- /dev/null
+++ b/tests/gpkg-1-zst.result
@@ -0,0 +1 @@
+Gentoo GLEP 78 (GPKG) binary package using zstd compression
\ No newline at end of file
diff --git a/tests/gpkg-1-zst.testfile b/tests/gpkg-1-zst.testfile
new file mode 100644
index 0000000000000000000000000000000000000000..00c71c018b736f17270b4deac31612b18223007d
GIT binary patch
literal 1024
zcmd1I%Pvk%EJ)Qg)HBjE)Gad7HPlZp$WGTaWS|-_00J`;69y0s1n4wa%+SQxz{J$d
z(9qn7!2n1anlmUEP|XE+3 at R-yNh|_736GNDER~5R{&Q1H5>paO67_%~qgPd2LaF~j
e5sWkb4Gm09P0bik<KM)<#CSCRDGhgG^aB7t5iF1Z

literal 0
HcmV?d00001

-- 
2.37.3



More information about the File mailing list