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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 21 | #include <sys/types.h> |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 22 | #include <netinet/in.h> /* For ntohl & htonl function */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 23 | #include <fcntl.h> |
| 24 | #include <unistd.h> |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 25 | #include <string.h> |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 26 | #include "busybox.h" |
| 27 | #include "unarchive.h" |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 28 | |
| 29 | #define RPM_MAGIC "\355\253\356\333" |
| 30 | #define RPM_HEADER_MAGIC "\216\255\350" |
| 31 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 32 | struct rpm_lead { |
| 33 | unsigned char magic[4]; |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 34 | uint8_t major, minor; |
| 35 | uint16_t type; |
| 36 | uint16_t archnum; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 37 | char name[66]; |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 38 | uint16_t osnum; |
| 39 | uint16_t signature_type; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 40 | char reserved[16]; |
| 41 | }; |
| 42 | |
| 43 | struct rpm_header { |
| 44 | char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */ |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 45 | uint8_t version; /* 1 byte version number */ |
| 46 | uint32_t reserved; /* 4 bytes reserved */ |
| 47 | uint32_t entries; /* Number of entries in header (4 bytes) */ |
| 48 | uint32_t size; /* Size of store (4 bytes) */ |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 51 | void skip_header(int rpm_fd) |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 52 | { |
| 53 | struct rpm_header header; |
| 54 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | bb_xread_all(rpm_fd, &header, sizeof(struct rpm_header)); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 56 | if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | bb_error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 58 | } |
| 59 | if (header.version != 1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | 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] | 61 | } |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 62 | header.entries = ntohl(header.entries); |
| 63 | header.size = ntohl(header.size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 64 | lseek (rpm_fd, 16 * header.entries, SEEK_CUR); /* Seek past index entries */ |
| 65 | lseek (rpm_fd, header.size, SEEK_CUR); /* Seek past store */ |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* No getopt required */ |
| 69 | extern int rpm2cpio_main(int argc, char **argv) |
| 70 | { |
| 71 | struct rpm_lead lead; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 72 | int rpm_fd; |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 73 | unsigned char magic[2]; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 74 | |
| 75 | if (argc == 1) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame^] | 76 | rpm_fd = STDIN_FILENO; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 77 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | rpm_fd = bb_xopen(argv[1], O_RDONLY); |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | bb_xread_all(rpm_fd, &lead, sizeof(struct rpm_lead)); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 82 | if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | 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] | 84 | } |
| 85 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 86 | /* Skip the signature header */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 87 | skip_header(rpm_fd); |
Glenn L McGrath | f92caa7 | 2002-11-03 14:14:53 +0000 | [diff] [blame] | 88 | 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] | 89 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 90 | /* Skip the main header */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 91 | skip_header(rpm_fd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 92 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | bb_xread_all(rpm_fd, &magic, 2); |
Eric Andersen | 2a2ab14 | 2002-10-18 22:31:02 +0000 | [diff] [blame] | 94 | if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | bb_error_msg_and_die("Invalid gzip magic"); |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 96 | } |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 97 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 98 | check_header_gzip(rpm_fd); |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame^] | 99 | if (inflate_gunzip(rpm_fd, STDOUT_FILENO) != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 100 | bb_error_msg("Error inflating"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 101 | } |
Glenn L McGrath | 26a0d9a | 2001-07-13 06:49:18 +0000 | [diff] [blame] | 102 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 103 | close(rpm_fd); |
| 104 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |