Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini rpm2cpio implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2001 by Laurence Anderson |
| 6 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 8 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 10 | #include "unarchive.h" |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 11 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 12 | #define RPM_MAGIC 0xedabeedb |
| 13 | #define RPM_MAGIC_STR "\355\253\356\333" |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 14 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 15 | struct rpm_lead { |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 16 | uint32_t magic; |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 17 | uint8_t major, minor; |
| 18 | uint16_t type; |
| 19 | uint16_t archnum; |
| 20 | char name[66]; |
| 21 | uint16_t osnum; |
| 22 | uint16_t signature_type; |
| 23 | char reserved[16]; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame^] | 26 | #define RPM_HEADER_MAGICnVER 0x8eade801 |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 27 | #define RPM_HEADER_MAGIC_STR "\216\255\350" |
| 28 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 29 | struct rpm_header { |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 30 | uint32_t magic_and_ver; /* 3 byte magic: 0x8e 0xad 0xe8; 1 byte version */ |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 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) */ |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame^] | 36 | enum { rpm_fd = STDIN_FILENO }; |
| 37 | |
| 38 | static unsigned skip_header(void) |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 39 | { |
| 40 | struct rpm_header header; |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 41 | unsigned len; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 42 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 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"); |
| 46 | // } |
| 47 | // if (header.version != 1) { |
| 48 | // bb_error_msg_and_die("unsupported RPM header version"); |
| 49 | // } |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame^] | 50 | if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) { |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 51 | bb_error_msg_and_die("invalid RPM header magic or unsupported version"); |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame^] | 52 | // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER)); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 53 | } |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 54 | |
| 55 | /* Seek past index entries, and past store */ |
| 56 | len = 16 * ntohl(header.entries) + ntohl(header.size); |
| 57 | seek_by_jump(rpm_fd, len); |
| 58 | |
| 59 | return sizeof(header) + len; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* No getopt required */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 63 | int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 64 | int rpm2cpio_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 65 | { |
| 66 | struct rpm_lead lead; |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 67 | unsigned pos; |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 68 | unsigned char magic[2]; |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 69 | IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd); |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 70 | |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame^] | 71 | if (argv[1]) { |
| 72 | xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd); |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 73 | } |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 74 | xread(rpm_fd, &lead, sizeof(lead)); |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 75 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 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"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 81 | /* Skip the signature header, align to 8 bytes */ |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame^] | 82 | pos = skip_header(); |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 83 | seek_by_jump(rpm_fd, (8 - pos) & 7); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 84 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 85 | /* Skip the main header */ |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame^] | 86 | skip_header(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 88 | xread(rpm_fd, &magic, 2); |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 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' |
| 93 | ) { |
| 94 | bb_error_msg_and_die("invalid gzip" |
| 95 | IF_FEATURE_SEAMLESS_BZ2("/bzip2") |
| 96 | " magic"); |
| 97 | } |
| 98 | unpack = unpack_bz2_stream; |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 99 | } |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 100 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 101 | if (unpack(rpm_fd, STDOUT_FILENO) < 0) { |
| 102 | bb_error_msg_and_die("error unpacking"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 103 | } |
Glenn L McGrath | 26a0d9a | 2001-07-13 06:49:18 +0000 | [diff] [blame] | 104 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 105 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 106 | close(rpm_fd); |
| 107 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 108 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 109 | return 0; |
| 110 | } |