back to old rpmoffset

This commit is contained in:
geos_one 2011-11-20 18:33:05 +00:00
parent 411a244fbd
commit a9b5a9c25e
3 changed files with 175 additions and 102 deletions

View File

@ -1,4 +1,5 @@
AUX rpmoffset.c 2755 RMD160 00f9a7e8dd8117eded73ead581e7bce9c40ed5af SHA1 cbe8b9f226c03d9b2b988d756adf507857bc47ce SHA256 b43c6bf2c3623d3fbc304ff3a804cf568caa7cd2d702ff5043ac8eda3c4fb42d
AUX rpm5offset.c 2755 RMD160 00f9a7e8dd8117eded73ead581e7bce9c40ed5af SHA1 cbe8b9f226c03d9b2b988d756adf507857bc47ce SHA256 b43c6bf2c3623d3fbc304ff3a804cf568caa7cd2d702ff5043ac8eda3c4fb42d
AUX rpmoffset.c 1964 RMD160 acea626f5080b7ea47863cf9e3bc2ab3b381c61e SHA1 5ec35b3d37773ca4a09443c6ea687c7d3a739f34 SHA256 e1e18d68009bd4541d6c65b43f45b58d720b9c87eba612d7616e244142f80dfe
EBUILD rpm5offset-9.0-r2.ebuild 653 RMD160 2f85563f7df4e5548f241bb1ee870a311f9587bb SHA1 c242da35e6e69aa1fd8514c5a03977fd7c837f09 SHA256 74aed2a38c21a7be598b6f60ac9820807ff9ee2ab29023863faad265fb00107a
MISC ChangeLog 551 RMD160 097d5f93696e887e988fed531abda20ca9628152 SHA1 8f7cb8b55fb8f2ce542fb10cbd6e96723a472ac4 SHA256 0e1b6be4ddf6c2e697320ee1e1bdea987c5a8fa7f55b7f60147e89ff26d5ea83
MISC metadata.xml 170 RMD160 645927a396fdc21cdeb089fe42c5397332420ea6 SHA1 ac7f48a14fec325926f9ce1be8fbf1f311b4f2e4 SHA256 d797a2ec6f9dc516c9f9c1a758ee87ad3e8c43101b5dc76c2f872d5bd4639b42

View File

@ -0,0 +1,121 @@
/* Find how deeply inside an .RPM the real data is */
/* kept, and report the offset in bytes */
/* Wouldn't it be a lot more sane if we could just untar these things? */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif
#ifndef BUFSIZ
# define BUFSIZ 8192
#endif
typedef struct {
const char *type;
const unsigned char *magic;
const size_t len;
} magic_t;
/* LZMA is some fuzzy crap */
int is_magic_lzma(const char *buf)
{
return (buf[0] == 0x5d && buf[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 };
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 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[])
{
size_t i, read_cnt, offset, left, lzma_offset;
FILE *fp = stdin;
char p[BUFSIZ];
if (argc == 2 && !strcmp(argv[1], "-v")) {
show_magic = 1;
--argc;
}
if (argc != 1) {
puts("Usage: rpmoffset < rpmfile");
return 1;
}
/* fp = fopen(argv[1], "r"); */
lzma_offset = 0;
offset = left = 0;
while (1) {
read_cnt = fread(p + left, 1, sizeof(p) - left, fp);
if (read_cnt + left < MAGIC_SIZE_MIN)
break;
for (i = 0; i < ARRAY_SIZE(magics); ++i) {
const char *needle;
if (read_cnt + left < magics[i].len)
continue;
needle = memmem(p, sizeof(p), magics[i].magic, magics[i].len);
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);
offset += read_cnt;
if (left == 0) {
offset -= MAGIC_SIZE_MIN - 1;
left = MAGIC_SIZE_MIN - 1;
}
}
/* 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]);
return 1;
}

View File

@ -4,118 +4,69 @@
/* Wouldn't it be a lot more sane if we could just untar these things? */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif
#ifndef BUFSIZ
# define BUFSIZ 8192
#endif
/* These offsets keep getting bigger, so we're going to just bite a 2MB */
/* chunk of RAM right away so that we have enough. Yeah, horrible */
/* quick and dirty implementation, but hey -- it gets the job done. */
typedef struct {
const char *type;
const unsigned char *magic;
const size_t len;
} magic_t;
#define RPMBUFSIZ 3145728
/* LZMA is some fuzzy crap */
int is_magic_lzma(const char *buf)
main()
{
return (buf[0] == 0x5d && buf[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
char *buff = malloc(RPMBUFSIZ),*eb,*p;
for (p = buff, eb = buff + read(0,buff,RPMBUFSIZ); p < eb; p++)
{
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 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[])
{
size_t i, read_cnt, offset, left, lzma_offset;
FILE *fp = stdin;
char p[BUFSIZ];
if (argc == 2 && !strcmp(argv[1], "-v")) {
show_magic = 1;
--argc;
}
if (argc != 1) {
puts("Usage: rpmoffset < rpmfile");
return 1;
}
/* fp = fopen(argv[1], "r"); */
lzma_offset = 0;
offset = left = 0;
while (1) {
read_cnt = fread(p + left, 1, sizeof(p) - left, fp);
if (read_cnt + left < MAGIC_SIZE_MIN)
break;
for (i = 0; i < ARRAY_SIZE(magics); ++i) {
const char *needle;
if (read_cnt + left < magics[i].len)
continue;
needle = memmem(p, sizeof(p), magics[i].magic, magics[i].len);
if (needle)
return magic_finish(magics[i].type, offset + (needle - p));
/* gzip format */
if (*p == '\037' && p[1] == '\213' && p[2] == '\010')
{
printf("%ld\n",p - buff);
exit(0);
}
/* 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;
}
/* bzip2 format */
else if (*p == 'B' && p[1] == 'Z' && p[2] == 'h' )
{
printf("%ld\n",p - buff);
exit(0);
}
memmove(p, p + left + read_cnt - MAGIC_SIZE_MIN + 1, MAGIC_SIZE_MIN - 1);
/* LZMA files; both LZMA_Alone and LZMA utils formats. The LZMA_Alone
* format is used by the LZMA_Alone tool from LZMA SDK. The LZMA utils
* format is the default format of LZMA utils 4.32.1 and later. */
offset += read_cnt;
if (left == 0) {
offset -= MAGIC_SIZE_MIN - 1;
left = MAGIC_SIZE_MIN - 1;
/* LZMA utils format */
else if (*p == '\377' && p[1] == 'L' &&
p[2] == 'Z' && p[3] == 'M' &&
p[4] == 'A' && p[5] == '\000')
{
printf("%ld\n",p - buff);
exit(0);
}
/* The LZMA_Alone format has no magic bytes, thus we
* need to play a wizard. This can give false positives,
* thus the detection below should be removed when
* the newer LZMA utils format has got popular. */
// else if (*p == 0x5D && p[1] == 0x00 &&
else if (*p == '\135' &&
p[5] == '\377' && p[6] == '\377' &&
p[7] == '\377' && p[8] == '\377' &&
p[9] == '\377' && p[10] == '\377' &&
p[11] == '\377' && p[12] == '\377')
/* ((p[10] == 0x00 && p[11] == 0x00 &&
p[12] == 0x00) ||
(p[5] == 0xFF && p[6] == 0xFF &&
p[7] == 0xFF && p[8] == 0xFF &&
p[9] == 0xFF && p[10] == 0xFF &&
p[11] == 0xFF && p[12] == 0xFF)))
*/ {
printf("%ld\n",p - buff);
exit(0);
}
}
/* 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]);
return 1;
exit(1);
}