public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Mike Frysinger" <vapier@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/rpm2targz:master commit in: /
Date: Thu, 17 May 2012 04:59:16 +0000 (UTC)	[thread overview]
Message-ID: <1337230421.de35a1cd59302a1cc6d3766fcc053b130ab4efa0.vapier@gentoo> (raw)

commit:     de35a1cd59302a1cc6d3766fcc053b130ab4efa0
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Thu May 17 04:53:41 2012 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Thu May 17 04:53:41 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/rpm2targz.git;a=commit;h=de35a1cd

rpmoffset: add support for LZMA archives

The LZMA format lacks proper magic markings (hence one of the driving
forces for the new XZ format), so we have to rework things to deal with
this fuzzy matching without throwing false positives.

URL: https://bugs.gentoo.org/321439
Reported-by: Peter Volkov <pva <AT> gentoo.org>
Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org>

---
 rpmoffset.c |   47 ++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/rpmoffset.c b/rpmoffset.c
index 628af93..bd1f456 100644
--- a/rpmoffset.c
+++ b/rpmoffset.c
@@ -45,6 +45,16 @@ typedef struct {
 	const size_t len;
 } magic_t;
 
+/* LZMA is some fuzzy crap */
+int is_magic_lzma(const char *buf)
+{
+	const unsigned char *ubuf = (const void *)buf;
+	return (ubuf[0] == 0x5d && ubuf[4] < 0x20) &&
+		(!memcmp(buf + 10, "\x00\x00\x00", 3) ||
+		 !memcmp(buf +  5, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 8));
+}
+#define magic_lzma_len 13
+
 static const unsigned char magic_gzip[]  = { '\037', '\213', '\010' };
 static const unsigned char magic_bzip2[] = { 'B', 'Z', 'h' };
 static const unsigned char magic_xz[]    = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
@@ -56,12 +66,21 @@ static const magic_t magics[] = {
 #undef DECLARE_MAGIC_T
 };
 #define MAGIC_SIZE_MIN 3
-#define MAGIC_SIZE_MAX 6
+#define MAGIC_SIZE_MAX 13
+
+static int show_magic;
+
+static int magic_finish(const char *magic, size_t offset)
+{
+	if (show_magic)
+		printf("%s ", magic);
+	printf("%zu\n", offset);
+	return 0;
+}
 
 int main(int argc, char *argv[])
 {
-	int show_magic = 0;
-	size_t i, read_cnt, offset, left;
+	size_t i, read_cnt, offset, left, lzma_offset;
 	FILE *fp = stdin;
 	char p[BUFSIZ];
 
@@ -76,6 +95,7 @@ int main(int argc, char *argv[])
 	}
 	/* fp = fopen(argv[1], "r"); */
 
+	lzma_offset = 0;
 	offset = left = 0;
 	while (1) {
 		read_cnt = fread(p + left, 1, sizeof(p) - left, fp);
@@ -89,12 +109,17 @@ int main(int argc, char *argv[])
 				continue;
 
 			needle = memmem(p, sizeof(p), magics[i].magic, magics[i].len);
-			if (needle) {
-				if (show_magic)
-					printf("%s ", magics[i].type);
-				printf("%zu\n", offset + (needle - p));
-				return 0;
-			}
+			if (needle)
+				return magic_finish(magics[i].type, offset + (needle - p));
+		}
+
+		/* Scan for LZMA magic, but don't return yet ... */
+		if (!lzma_offset && read_cnt + left >= magic_lzma_len) {
+			for (i = 0; i <= read_cnt + left - magic_lzma_len; ++i)
+				if (is_magic_lzma(p + i)) {
+					lzma_offset = offset + i;
+					break;
+				}
 		}
 
 		memmove(p, p + left + read_cnt - MAGIC_SIZE_MIN + 1, MAGIC_SIZE_MIN - 1);
@@ -106,6 +131,10 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	/* Delay till the end for LZMA archives since it is too fuzzy */
+	if (lzma_offset)
+		return magic_finish("lzma", lzma_offset);
+
 	if (ferror(stdin))
 		perror(argv[0]);
 



             reply	other threads:[~2012-05-17  4:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-17  4:59 Mike Frysinger [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-03-16 23:33 [gentoo-commits] proj/rpm2targz:master commit in: / Mike Frysinger
2021-03-16 23:33 Mike Frysinger
2021-03-16 23:33 Mike Frysinger
2021-03-16 23:33 Mike Frysinger
2021-03-16 23:33 Mike Frysinger
2021-03-16 23:33 Mike Frysinger
2021-03-16 23:33 Mike Frysinger
2021-03-16 23:33 Mike Frysinger
2012-05-17  4:59 Mike Frysinger
2012-05-17  4:44 Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1337230421.de35a1cd59302a1cc6d3766fcc053b130ab4efa0.vapier@gentoo \
    --to=vapier@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox