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 tarball for details. 8 */ 9#include "libbb.h" 10#include "unarchive.h" 11 12#define RPM_MAGIC "\355\253\356\333" 13#define RPM_HEADER_MAGIC "\216\255\350" 14 15struct rpm_lead { 16 unsigned char magic[4]; 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]; 24}; 25 26struct rpm_header { 27 char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */ 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) */ 32}; 33 34static void skip_header(int rpm_fd) 35{ 36 struct rpm_header header; 37 38 xread(rpm_fd, &header, sizeof(struct rpm_header)); 39 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) { 40 bb_error_msg_and_die("invalid RPM header magic"); /* Invalid magic */ 41 } 42 if (header.version != 1) { 43 bb_error_msg_and_die("unsupported RPM header version"); /* This program only supports v1 headers */ 44 } 45 header.entries = ntohl(header.entries); 46 header.size = ntohl(header.size); 47 lseek (rpm_fd, 16 * header.entries, SEEK_CUR); /* Seek past index entries */ 48 lseek (rpm_fd, header.size, SEEK_CUR); /* Seek past store */ 49} 50 51/* No getopt required */ 52int rpm2cpio_main(int argc, char **argv); 53int rpm2cpio_main(int argc, char **argv) 54{ 55 struct rpm_lead lead; 56 int rpm_fd; 57 unsigned char magic[2]; 58 59 if (argc == 1) { 60 rpm_fd = STDIN_FILENO; 61 } else { 62 rpm_fd = xopen(argv[1], O_RDONLY); 63 } 64 65 xread(rpm_fd, &lead, sizeof(struct rpm_lead)); 66 if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) { 67 bb_error_msg_and_die("invalid RPM magic"); /* Just check the magic, the rest is irrelevant */ 68 } 69 70 /* Skip the signature header */ 71 skip_header(rpm_fd); 72 lseek(rpm_fd, (8 - (lseek(rpm_fd, 0, SEEK_CUR) % 8)) % 8, SEEK_CUR); 73 74 /* Skip the main header */ 75 skip_header(rpm_fd); 76 77 xread(rpm_fd, &magic, 2); 78 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { 79 bb_error_msg_and_die("invalid gzip magic"); 80 } 81 82 check_header_gzip_or_die(rpm_fd); 83 if (unpack_gz_stream(rpm_fd, STDOUT_FILENO) < 0) { 84 bb_error_msg("error inflating"); 85 } 86 87 close(rpm_fd); 88 89 return 0; 90} 91