blob: 8d4ca84dc5e153d440d8242143989431c0fb3fa1 [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 */
21
22#include "busybox.h"
23#include <netinet/in.h> /* For ntohl & htonl function */
24#include <string.h>
25
26#define RPM_MAGIC "\355\253\356\333"
27#define RPM_HEADER_MAGIC "\216\255\350"
28
29typedef unsigned char u8;
30typedef unsigned short u16;
31typedef unsigned int u32;
32
33struct rpm_lead {
34 unsigned char magic[4];
35 u8 major, minor;
36 u16 type;
37 u16 archnum;
38 char name[66];
39 u16 osnum;
40 u16 signature_type;
41 char reserved[16];
42};
43
44struct rpm_header {
45 char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
46 u8 version; /* 1 byte version number */
47 u32 reserved; /* 4 bytes reserved */
48 u32 entries; /* Number of entries in header (4 bytes) */
49 u32 size; /* Size of store (4 bytes) */
50};
51
52void skip_header(FILE *rpmfile)
53{
54 struct rpm_header header;
55
56 fread(&header, sizeof(struct rpm_header), 1, rpmfile);
57 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */
58 if (header.version != 1) error_msg_and_die("Unsupported RPM header version"); /* This program only supports v1 headers */
59 header.entries = ntohl(header.entries);
60 header.size = ntohl(header.size);
61 fseek (rpmfile, 16 * header.entries, SEEK_CUR); /* Seek past index entries */
62 fseek (rpmfile, header.size, SEEK_CUR); /* Seek past store */
63}
64
65/* No getopt required */
66extern int rpm2cpio_main(int argc, char **argv)
67{
68 struct rpm_lead lead;
69 int gunzip_pid;
70 FILE *rpmfile, *cpiofile;
71
72 if (argc == 1) {
73 rpmfile = stdin;
74 } else {
75 rpmfile = fopen(argv[1], "r");
76 if (!rpmfile) perror_msg_and_die("Can't open rpm file");
77 }
78
79 fread (&lead, sizeof(struct rpm_lead), 1, rpmfile);
80 if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */
81 /* Skip the signature header */
82 skip_header(rpmfile);
83 fseek(rpmfile, (8 - (ftell(rpmfile) % 8)) % 8, SEEK_CUR); /* Pad to 8 byte boundary */
84 /* Skip the main header */
85 skip_header(rpmfile);
86
87 cpiofile = gz_open(rpmfile, &gunzip_pid);
88 copyfd(fileno(cpiofile), fileno(stdout));
89 gz_close(gunzip_pid);
90 fclose(rpmfile);
91 return 0;
92}