28b43a1817db33ea437cf3a0f1a865a22a5e90a8
[platform/upstream/busybox.git] / archival / rpm2cpio.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini rpm2cpio implementation for busybox
4  *
5  * Copyright (C) 2001 by Laurence Anderson
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
10 //usage:#define rpm2cpio_trivial_usage
11 //usage:       "package.rpm"
12 //usage:#define rpm2cpio_full_usage "\n\n"
13 //usage:       "Output a cpio archive of the rpm file"
14
15 #include "libbb.h"
16 #include "bb_archive.h"
17 #include "rpm.h"
18
19 enum { rpm_fd = STDIN_FILENO };
20
21 static unsigned skip_header(void)
22 {
23         struct rpm_header header;
24         unsigned len;
25
26         xread(rpm_fd, &header, sizeof(header));
27 //      if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
28 //              bb_error_msg_and_die("invalid RPM header magic");
29 //      }
30 //      if (header.version != 1) {
31 //              bb_error_msg_and_die("unsupported RPM header version");
32 //      }
33         if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
34                 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
35                 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
36         }
37
38         /* Seek past index entries, and past store */
39         len = 16 * ntohl(header.entries) + ntohl(header.size);
40         seek_by_jump(rpm_fd, len);
41
42         return sizeof(header) + len;
43 }
44
45 /* No getopt required */
46 int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
47 int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
48 {
49         struct rpm_lead lead;
50         unsigned pos;
51
52         if (argv[1]) {
53                 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
54         }
55         xread(rpm_fd, &lead, sizeof(lead));
56
57         /* Just check the magic, the rest is irrelevant */
58         if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
59                 bb_error_msg_and_die("invalid RPM magic");
60         }
61
62         /* Skip the signature header, align to 8 bytes */
63         pos = skip_header();
64         seek_by_jump(rpm_fd, (-(int)pos) & 7);
65
66         /* Skip the main header */
67         skip_header();
68
69 #if 0
70         /* This works, but doesn't report uncompress errors (they happen in child) */
71         setup_unzip_on_fd(rpm_fd /*fail_if_not_detected: 1*/);
72         if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
73                 bb_error_msg_and_die("error unpacking");
74 #else
75         /* BLOAT */
76         {
77                 union {
78                         uint8_t b[4];
79                         uint16_t b16[2];
80                         uint32_t b32[1];
81                 } magic;
82                 IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
83
84                 xread(rpm_fd, magic.b16, sizeof(magic.b16[0]));
85                 if (magic.b16[0] == GZIP_MAGIC) {
86                         unpack = unpack_gz_stream;
87                 } else
88                 if (ENABLE_FEATURE_SEAMLESS_BZ2
89                  && magic.b16[0] == BZIP2_MAGIC
90                 ) {
91                         unpack = unpack_bz2_stream;
92                 } else
93                 if (ENABLE_FEATURE_SEAMLESS_XZ
94                  && magic.b16[0] == XZ_MAGIC1
95                 ) {
96                         xread(rpm_fd, magic.b32, sizeof(magic.b32[0]));
97                         if (magic.b32[0] != XZ_MAGIC2)
98                                 goto no_magic;
99                         /* unpack_xz_stream wants fd at position 6, no need to seek */
100                         //xlseek(rpm_fd, -6, SEEK_CUR);
101                         unpack = unpack_xz_stream;
102                 } else {
103  no_magic:
104                         bb_error_msg_and_die("no gzip"
105                                         IF_FEATURE_SEAMLESS_BZ2("/bzip2")
106                                         IF_FEATURE_SEAMLESS_XZ("/xz")
107                                         " magic");
108                 }
109                 if (unpack(rpm_fd, STDOUT_FILENO) < 0)
110                         bb_error_msg_and_die("error unpacking");
111         }
112 #endif
113
114         if (ENABLE_FEATURE_CLEAN_UP) {
115                 close(rpm_fd);
116         }
117
118         return 0;
119 }