blob: bb9f69573cb3042020e1ded0bd3e0eb9771c2465 [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 *
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 McGrath7ca04f32002-09-25 02:47:48 +000021#include <sys/types.h>
Glenn L McGrathf8736d22001-06-26 01:19:34 +000022#include <netinet/in.h> /* For ntohl & htonl function */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000023#include <fcntl.h>
24#include <unistd.h>
Glenn L McGrathf8736d22001-06-26 01:19:34 +000025#include <string.h>
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000026#include "busybox.h"
27#include "unarchive.h"
Glenn L McGrathf8736d22001-06-26 01:19:34 +000028
29#define RPM_MAGIC "\355\253\356\333"
30#define RPM_HEADER_MAGIC "\216\255\350"
31
Glenn L McGrathf8736d22001-06-26 01:19:34 +000032struct rpm_lead {
33 unsigned char magic[4];
Eric Andersendcbca622001-08-29 19:02:26 +000034 u_int8_t major, minor;
35 u_int16_t type;
36 u_int16_t archnum;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000037 char name[66];
Eric Andersendcbca622001-08-29 19:02:26 +000038 u_int16_t osnum;
39 u_int16_t signature_type;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000040 char reserved[16];
41};
42
43struct rpm_header {
44 char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
Eric Andersendcbca622001-08-29 19:02:26 +000045 u_int8_t version; /* 1 byte version number */
46 u_int32_t reserved; /* 4 bytes reserved */
47 u_int32_t entries; /* Number of entries in header (4 bytes) */
48 u_int32_t size; /* Size of store (4 bytes) */
Glenn L McGrathf8736d22001-06-26 01:19:34 +000049};
50
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000051void skip_header(int rpm_fd)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000052{
53 struct rpm_header header;
54
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 bb_xread_all(rpm_fd, &header, sizeof(struct rpm_header));
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000056 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 bb_error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000058 }
59 if (header.version != 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 bb_error_msg_and_die("Unsupported RPM header version"); /* This program only supports v1 headers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000061 }
Glenn L McGrathf8736d22001-06-26 01:19:34 +000062 header.entries = ntohl(header.entries);
63 header.size = ntohl(header.size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000064 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 McGrathf8736d22001-06-26 01:19:34 +000066}
67
68/* No getopt required */
69extern int rpm2cpio_main(int argc, char **argv)
70{
71 struct rpm_lead lead;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000072 int rpm_fd;
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000073 unsigned char magic[2];
Glenn L McGrathf8736d22001-06-26 01:19:34 +000074
75 if (argc == 1) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000076 rpm_fd = fileno(stdin);
Glenn L McGrathf8736d22001-06-26 01:19:34 +000077 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 rpm_fd = bb_xopen(argv[1], O_RDONLY);
Glenn L McGrathf8736d22001-06-26 01:19:34 +000079 }
80
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 bb_xread_all(rpm_fd, &lead, sizeof(struct rpm_lead));
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000082 if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 bb_error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000084 }
85
Glenn L McGrathf8736d22001-06-26 01:19:34 +000086 /* Skip the signature header */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000087 skip_header(rpm_fd);
Glenn L McGrathf92caa72002-11-03 14:14:53 +000088 lseek(rpm_fd, (8 - (lseek(rpm_fd, 0, SEEK_CUR) % 8)) % 8, SEEK_CUR);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000089
Glenn L McGrathf8736d22001-06-26 01:19:34 +000090 /* Skip the main header */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000091 skip_header(rpm_fd);
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000092
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 bb_xread_all(rpm_fd, &magic, 2);
Eric Andersen2a2ab142002-10-18 22:31:02 +000094 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 bb_error_msg_and_die("Invalid gzip magic");
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000096 }
Glenn L McGrathf8736d22001-06-26 01:19:34 +000097
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000098 check_header_gzip(rpm_fd);
99 if (inflate(rpm_fd, fileno(stdout)) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 bb_error_msg("Error inflating");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000101 }
102 check_trailer_gzip(rpm_fd);
Glenn L McGrath26a0d9a2001-07-13 06:49:18 +0000103
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000104 close(rpm_fd);
105
Glenn L McGrathf8736d22001-06-26 01:19:34 +0000106 return 0;
107}