blob: 5403aee0221eefacd3b39ef80c8cc8bf44aac333 [file] [log] [blame]
Glenn L McGrathf8736d22001-06-26 01:19:34 +00001/* 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"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathf8736d22001-06-26 01:19:34 +00008 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000010#include "unarchive.h"
Glenn L McGrathf8736d22001-06-26 01:19:34 +000011
Pascal Bellard7f214942009-08-28 06:20:33 +020012#define RPM_MAGIC 0xedabeedb
13#define RPM_MAGIC_STR "\355\253\356\333"
Glenn L McGrathf8736d22001-06-26 01:19:34 +000014
Glenn L McGrathf8736d22001-06-26 01:19:34 +000015struct rpm_lead {
Pascal Bellard7f214942009-08-28 06:20:33 +020016 uint32_t magic;
Denis Vlasenko2570b2e2008-03-28 01:00:09 +000017 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 McGrathf8736d22001-06-26 01:19:34 +000024};
25
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020026#define RPM_HEADER_MAGICnVER 0x8eade801
Pascal Bellard7f214942009-08-28 06:20:33 +020027#define RPM_HEADER_MAGIC_STR "\216\255\350"
28
Glenn L McGrathf8736d22001-06-26 01:19:34 +000029struct rpm_header {
Pascal Bellard7f214942009-08-28 06:20:33 +020030 uint32_t magic_and_ver; /* 3 byte magic: 0x8e 0xad 0xe8; 1 byte version */
Eric Andersendfcb5b02004-01-30 22:54:20 +000031 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 McGrathf8736d22001-06-26 01:19:34 +000034};
35
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020036enum { rpm_fd = STDIN_FILENO };
37
38static unsigned skip_header(void)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000039{
40 struct rpm_header header;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020041 unsigned len;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000042
Pascal Bellard7f214942009-08-28 06:20:33 +020043 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 Vlasenkoe6c483e2009-08-28 21:15:24 +020050 if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
Pascal Bellard7f214942009-08-28 06:20:33 +020051 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020052 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000053 }
Denys Vlasenko0a130d52009-08-28 21:09:51 +020054
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 McGrathf8736d22001-06-26 01:19:34 +000060}
61
62/* No getopt required */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000063int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Pascal Bellard7f214942009-08-28 06:20:33 +020064int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000065{
66 struct rpm_lead lead;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020067 unsigned pos;
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000068 unsigned char magic[2];
Pascal Bellard7f214942009-08-28 06:20:33 +020069 IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
Glenn L McGrathf8736d22001-06-26 01:19:34 +000070
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020071 if (argv[1]) {
72 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
Glenn L McGrathf8736d22001-06-26 01:19:34 +000073 }
Pascal Bellard7f214942009-08-28 06:20:33 +020074 xread(rpm_fd, &lead, sizeof(lead));
Glenn L McGrathf8736d22001-06-26 01:19:34 +000075
Pascal Bellard7f214942009-08-28 06:20:33 +020076 /* 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 McGrath7ca04f32002-09-25 02:47:48 +000079 }
80
Pascal Bellard7f214942009-08-28 06:20:33 +020081 /* Skip the signature header, align to 8 bytes */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020082 pos = skip_header();
Denys Vlasenko0a130d52009-08-28 21:09:51 +020083 seek_by_jump(rpm_fd, (8 - pos) & 7);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000084
Glenn L McGrathf8736d22001-06-26 01:19:34 +000085 /* Skip the main header */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020086 skip_header();
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087
Rob Landley53437472006-07-16 08:14:35 +000088 xread(rpm_fd, &magic, 2);
Pascal Bellard7f214942009-08-28 06:20:33 +020089 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 McGrath2e41d0c2002-09-27 06:46:02 +000099 }
Glenn L McGrathf8736d22001-06-26 01:19:34 +0000100
Pascal Bellard7f214942009-08-28 06:20:33 +0200101 if (unpack(rpm_fd, STDOUT_FILENO) < 0) {
102 bb_error_msg_and_die("error unpacking");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000103 }
Glenn L McGrath26a0d9a2001-07-13 06:49:18 +0000104
Pascal Bellard7f214942009-08-28 06:20:33 +0200105 if (ENABLE_FEATURE_CLEAN_UP) {
106 close(rpm_fd);
107 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000108
Glenn L McGrathf8736d22001-06-26 01:19:34 +0000109 return 0;
110}