Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 321439 | Differences between
and this patch

Collapse All | Expand All

(-)a/rpmoffset.c (-9 / +37 lines)
Lines 25-30 typedef struct { Link Here
25
	const size_t len;
25
	const size_t len;
26
} magic_t;
26
} magic_t;
27
27
28
/* LZMA is some fuzzy crap */
29
int is_magic_lzma(const char *buf)
30
{
31
	return (buf[0] == 0x5d && buf[4] < 0x20) &&
32
		(!memcmp(buf + 10, "\x00\x00\x00", 3) ||
33
		 !memcmp(buf +  5, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 8));
34
}
35
#define magic_lzma_len 13
36
28
static const unsigned char magic_gzip[]  = { '\037', '\213', '\010' };
37
static const unsigned char magic_gzip[]  = { '\037', '\213', '\010' };
29
static const unsigned char magic_bzip2[] = { 'B', 'Z', 'h' };
38
static const unsigned char magic_bzip2[] = { 'B', 'Z', 'h' };
30
static const unsigned char magic_xz[]    = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
39
static const unsigned char magic_xz[]    = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
Lines 36-47 static const magic_t magics[] = { Link Here
36
#undef DECLARE_MAGIC_T
45
#undef DECLARE_MAGIC_T
37
};
46
};
38
#define MAGIC_SIZE_MIN 3
47
#define MAGIC_SIZE_MIN 3
39
#define MAGIC_SIZE_MAX 6
48
#define MAGIC_SIZE_MAX 13
49
50
static int show_magic;
51
52
static int magic_finish(const char *magic, size_t offset)
53
{
54
	if (show_magic)
55
		printf("%s ", magic);
56
	printf("%zu\n", offset);
57
	return 0;
58
}
40
59
41
int main(int argc, char *argv[])
60
int main(int argc, char *argv[])
42
{
61
{
43
	int show_magic = 0;
62
	size_t i, read_cnt, offset, left, lzma_offset;
44
	size_t i, read_cnt, offset, left;
45
	FILE *fp = stdin;
63
	FILE *fp = stdin;
46
	char p[BUFSIZ];
64
	char p[BUFSIZ];
47
65
Lines 56-61 int main(int argc, char *argv[]) Link Here
56
	}
74
	}
57
	/* fp = fopen(argv[1], "r"); */
75
	/* fp = fopen(argv[1], "r"); */
58
76
77
	lzma_offset = 0;
59
	offset = left = 0;
78
	offset = left = 0;
60
	while (1) {
79
	while (1) {
61
		read_cnt = fread(p + left, 1, sizeof(p) - left, fp);
80
		read_cnt = fread(p + left, 1, sizeof(p) - left, fp);
Lines 69-80 int main(int argc, char *argv[]) Link Here
69
				continue;
88
				continue;
70
89
71
			needle = memmem(p, sizeof(p), magics[i].magic, magics[i].len);
90
			needle = memmem(p, sizeof(p), magics[i].magic, magics[i].len);
72
			if (needle) {
91
			if (needle)
73
				if (show_magic)
92
				return magic_finish(magics[i].type, offset + (needle - p));
74
					printf("%s ", magics[i].type);
93
		}
75
				printf("%zu\n", offset + (needle - p));
94
76
				return 0;
95
		/* Scan for LZMA magic, but don't return yet ... */
77
			}
96
		if (!lzma_offset && read_cnt + left >= magic_lzma_len) {
97
			for (i = 0; i <= read_cnt + left - magic_lzma_len; ++i)
98
				if (is_magic_lzma(p + i)) {
99
					lzma_offset = offset + i;
100
					break;
101
				}
78
		}
102
		}
79
103
80
		memmove(p, p + left + read_cnt - MAGIC_SIZE_MIN + 1, MAGIC_SIZE_MIN - 1);
104
		memmove(p, p + left + read_cnt - MAGIC_SIZE_MIN + 1, MAGIC_SIZE_MIN - 1);
Lines 86-91 int main(int argc, char *argv[]) Link Here
86
		}
110
		}
87
	}
111
	}
88
112
113
	/* Delay till the end for LZMA archives since it is too fuzzy */
114
	if (lzma_offset)
115
		return magic_finish("lzma", lzma_offset);
116
89
	if (ferror(stdin))
117
	if (ferror(stdin))
90
		perror(argv[0]);
118
		perror(argv[0]);
91
119

Return to bug 321439