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