Move from read_gz to the pipe()+fork() method.
[oweals/busybox.git] / archival / rpm2cpio.c
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  */
21 #include <sys/types.h>
22 #include <netinet/in.h> /* For ntohl & htonl function */
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include "busybox.h"
27 #include "unarchive.h"
28
29 #define RPM_MAGIC "\355\253\356\333"
30 #define RPM_HEADER_MAGIC "\216\255\350"
31
32 struct rpm_lead {
33     unsigned char magic[4];
34     u_int8_t major, minor;
35     u_int16_t type;
36     u_int16_t archnum;
37     char name[66];
38     u_int16_t osnum;
39     u_int16_t signature_type;
40     char reserved[16];
41 };
42
43 struct rpm_header {
44         char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
45         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) */
49 };
50
51 void skip_header(int rpm_fd)
52 {
53         struct rpm_header header;
54
55         bb_xread_all(rpm_fd, &header, sizeof(struct rpm_header));
56         if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) {
57                 bb_error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */
58         }
59         if (header.version != 1) {
60                 bb_error_msg_and_die("Unsupported RPM header version"); /* This program only supports v1 headers */
61         }
62         header.entries = ntohl(header.entries);
63         header.size = ntohl(header.size);
64         lseek (rpm_fd, 16 * header.entries, SEEK_CUR); /* Seek past index entries */
65         lseek (rpm_fd, header.size, SEEK_CUR); /* Seek past store */
66 }
67
68 /* No getopt required */
69 extern int rpm2cpio_main(int argc, char **argv)
70 {
71         struct rpm_lead lead;
72         int rpm_fd;
73         unsigned char magic[2];
74
75         if (argc == 1) {
76                 rpm_fd = fileno(stdin);
77         } else {
78                 rpm_fd = bb_xopen(argv[1], O_RDONLY);
79         }
80
81         bb_xread_all(rpm_fd, &lead, sizeof(struct rpm_lead));
82         if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) {
83                 bb_error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */
84         }
85
86         /* Skip the signature header */
87         skip_header(rpm_fd);
88         lseek(rpm_fd, (8 - (lseek(rpm_fd, 0, SEEK_CUR) % 8)) % 8, SEEK_CUR);
89
90         /* Skip the main header */
91         skip_header(rpm_fd);
92         
93         bb_xread_all(rpm_fd, &magic, 2);
94         if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
95                 bb_error_msg_and_die("Invalid gzip magic");
96         }
97
98         check_header_gzip(rpm_fd);
99         if (inflate_gunzip(rpm_fd, fileno(stdout)) != 0) {
100                 bb_error_msg("Error inflating");
101         }
102
103         close(rpm_fd);
104
105         return 0;
106 }