aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2009-11-06 07:52:52 -0500
committerMike Frysinger <vapier@gentoo.org>2009-11-06 08:05:12 -0500
commit2e70251ab81778cffe7cef9bd87d7a3d056c7534 (patch)
tree262aa0a3f49188aa4178aa8fa5023f75acb6e7b1 /rpmoffset.c
parentrpm2targz: make coding/testing easier (diff)
downloadrpm2targz-2e70251ab81778cffe7cef9bd87d7a3d056c7534.tar.gz
rpm2targz-2e70251ab81778cffe7cef9bd87d7a3d056c7534.tar.bz2
rpm2targz-2e70251ab81778cffe7cef9bd87d7a3d056c7534.zip
add support for xz compressed rpmsv9.0.0.4g
Newer RPM distros are moving to XZ compression, so supporting that would probably be a good thing. URL: http://bugs.gentoo.org/292057 Reported-by: Rafał Mużyło <galtgendo@o2.pl> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'rpmoffset.c')
-rw-r--r--rpmoffset.c45
1 files changed, 35 insertions, 10 deletions
diff --git a/rpmoffset.c b/rpmoffset.c
index b8f5a04..0a3c488 100644
--- a/rpmoffset.c
+++ b/rpmoffset.c
@@ -19,18 +19,36 @@
# define BUFSIZ 8192
#endif
-#define MAGIC_SIZE 3
+typedef struct {
+ const char *type;
+ const unsigned char *magic;
+ const size_t len;
+} magic_t;
+
+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 };
+static const magic_t magics[] = {
+#define DECLARE_MAGIC_T(t) { .type = #t, .magic = magic_##t, .len = sizeof(magic_##t), },
+ DECLARE_MAGIC_T(gzip)
+ DECLARE_MAGIC_T(bzip2)
+ DECLARE_MAGIC_T(xz)
+#undef DECLARE_MAGIC_T
+};
+#define MAGIC_SIZE_MIN 3
+#define MAGIC_SIZE_MAX 6
int main(int argc, char *argv[])
{
+ int show_magic = 0;
size_t i, read_cnt, offset, left;
FILE *fp = stdin;
char p[BUFSIZ];
- const char magics[][MAGIC_SIZE] = {
- { '\037', '\213', '\010' }, /* gzip */
- { 'B', 'Z', 'h' }, /* bzip */
- };
+ if (argc == 2 && !strcmp(argv[1], "-v")) {
+ show_magic = 1;
+ --argc;
+ }
if (argc != 1) {
puts("Usage: rpmoffset < rpmfile");
@@ -41,23 +59,30 @@ int main(int argc, char *argv[])
offset = left = 0;
while (1) {
read_cnt = fread(p + left, 1, sizeof(p) - left, fp);
- if (read_cnt + left < MAGIC_SIZE)
+ if (read_cnt + left < MAGIC_SIZE_MIN)
break;
for (i = 0; i < ARRAY_SIZE(magics); ++i) {
- char *needle = memmem(p, sizeof(p), magics[i], MAGIC_SIZE);
+ const char *needle;
+
+ if (read_cnt + left < magics[i].len)
+ 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;
}
}
- memmove(p, p + left + read_cnt - MAGIC_SIZE + 1, MAGIC_SIZE - 1);
+ memmove(p, p + left + read_cnt - MAGIC_SIZE_MIN + 1, MAGIC_SIZE_MIN - 1);
offset += read_cnt;
if (left == 0) {
- offset -= MAGIC_SIZE - 1;
- left = MAGIC_SIZE - 1;
+ offset -= MAGIC_SIZE_MIN - 1;
+ left = MAGIC_SIZE_MIN - 1;
}
}