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 | */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 9 | #include "busybox.h" |
| 10 | #include "unarchive.h" |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 11 | |
| 12 | #define RPM_MAGIC "\355\253\356\333" |
| 13 | #define RPM_HEADER_MAGIC "\216\255\350" |
| 14 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 15 | struct rpm_lead { |
| 16 | unsigned char magic[4]; |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 17 | uint8_t major, minor; |
| 18 | uint16_t type; |
| 19 | uint16_t archnum; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 20 | char name[66]; |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 21 | uint16_t osnum; |
| 22 | uint16_t signature_type; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 23 | char reserved[16]; |
| 24 | }; |
| 25 | |
| 26 | struct rpm_header { |
| 27 | char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */ |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 28 | uint8_t version; /* 1 byte version number */ |
| 29 | uint32_t reserved; /* 4 bytes reserved */ |
| 30 | uint32_t entries; /* Number of entries in header (4 bytes) */ |
| 31 | uint32_t size; /* Size of store (4 bytes) */ |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 34 | static void skip_header(int rpm_fd) |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 35 | { |
| 36 | struct rpm_header header; |
| 37 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 38 | xread(rpm_fd, &header, sizeof(struct rpm_header)); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 39 | if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 40 | bb_error_msg_and_die("invalid RPM header magic"); /* Invalid magic */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 41 | } |
| 42 | if (header.version != 1) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 43 | bb_error_msg_and_die("unsupported RPM header version"); /* This program only supports v1 headers */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 44 | } |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 45 | header.entries = ntohl(header.entries); |
| 46 | header.size = ntohl(header.size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 47 | lseek (rpm_fd, 16 * header.entries, SEEK_CUR); /* Seek past index entries */ |
| 48 | lseek (rpm_fd, header.size, SEEK_CUR); /* Seek past store */ |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* No getopt required */ |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 52 | int rpm2cpio_main(int argc, char **argv) |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 53 | { |
| 54 | struct rpm_lead lead; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 55 | int rpm_fd; |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 56 | unsigned char magic[2]; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 57 | |
| 58 | if (argc == 1) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 59 | rpm_fd = STDIN_FILENO; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 60 | } else { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 61 | rpm_fd = xopen(argv[1], O_RDONLY); |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 64 | xread(rpm_fd, &lead, sizeof(struct rpm_lead)); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 65 | if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 66 | bb_error_msg_and_die("invalid RPM magic"); /* Just check the magic, the rest is irrelevant */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 69 | /* Skip the signature header */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 70 | skip_header(rpm_fd); |
Glenn L McGrath | f92caa7 | 2002-11-03 14:14:53 +0000 | [diff] [blame] | 71 | lseek(rpm_fd, (8 - (lseek(rpm_fd, 0, SEEK_CUR) % 8)) % 8, SEEK_CUR); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 72 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 73 | /* Skip the main header */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 74 | skip_header(rpm_fd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 75 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 76 | xread(rpm_fd, &magic, 2); |
Eric Andersen | 2a2ab14 | 2002-10-18 22:31:02 +0000 | [diff] [blame] | 77 | if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 78 | bb_error_msg_and_die("invalid gzip magic"); |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 79 | } |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 80 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 81 | check_header_gzip(rpm_fd); |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 82 | if (inflate_gunzip(rpm_fd, STDOUT_FILENO) < 0) { |
| 83 | bb_error_msg("error inflating"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 84 | } |
Glenn L McGrath | 26a0d9a | 2001-07-13 06:49:18 +0000 | [diff] [blame] | 85 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 86 | close(rpm_fd); |
| 87 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 88 | return 0; |
| 89 | } |