1 /* vi: set sw=4 ts=4: */
3 * Mini rpm2cpio implementation for busybox
5 * Copyright (C) 2001 by Laurence Anderson
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 #include "unarchive.h"
12 #define RPM_MAGIC 0xedabeedb
13 #define RPM_MAGIC_STR "\355\253\356\333"
22 uint16_t signature_type;
26 #define RPM_HEADER_MAGICnVER 0x8eade801
27 #define RPM_HEADER_MAGIC_STR "\216\255\350"
30 uint32_t magic_and_ver; /* 3 byte magic: 0x8e 0xad 0xe8; 1 byte version */
31 uint32_t reserved; /* 4 bytes reserved */
32 uint32_t entries; /* Number of entries in header (4 bytes) */
33 uint32_t size; /* Size of store (4 bytes) */
36 enum { rpm_fd = STDIN_FILENO };
38 static unsigned skip_header(void)
40 struct rpm_header header;
43 xread(rpm_fd, &header, sizeof(header));
44 // if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
45 // bb_error_msg_and_die("invalid RPM header magic");
47 // if (header.version != 1) {
48 // bb_error_msg_and_die("unsupported RPM header version");
50 if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
51 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
52 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
55 /* Seek past index entries, and past store */
56 len = 16 * ntohl(header.entries) + ntohl(header.size);
57 seek_by_jump(rpm_fd, len);
59 return sizeof(header) + len;
62 /* No getopt required */
63 int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
64 int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
68 unsigned char magic[2];
69 IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
72 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
74 xread(rpm_fd, &lead, sizeof(lead));
76 /* Just check the magic, the rest is irrelevant */
77 if (lead.magic != htonl(RPM_MAGIC)) {
78 bb_error_msg_and_die("invalid RPM magic");
81 /* Skip the signature header, align to 8 bytes */
83 seek_by_jump(rpm_fd, (8 - pos) & 7);
85 /* Skip the main header */
88 xread(rpm_fd, &magic, 2);
89 unpack = unpack_gz_stream;
90 if (magic[0] != 0x1f || magic[1] != 0x8b) {
91 if (!ENABLE_FEATURE_SEAMLESS_BZ2
92 || magic[0] != 'B' || magic[1] != 'Z'
94 bb_error_msg_and_die("invalid gzip"
95 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
98 unpack = unpack_bz2_stream;
101 if (unpack(rpm_fd, STDOUT_FILENO) < 0) {
102 bb_error_msg_and_die("error unpacking");
105 if (ENABLE_FEATURE_CLEAN_UP) {