2000-09-20 19:22:26 +00:00
|
|
|
/*
|
2000-11-29 21:39:02 +00:00
|
|
|
* rpmunpack for busybox
|
2000-09-20 19:22:26 +00:00
|
|
|
*
|
|
|
|
* rpmunpack.c - Utility program to unpack an RPM archive
|
|
|
|
*
|
|
|
|
* Gero Kuhlmann <gero@gkminix.han.de> 1998
|
|
|
|
*
|
|
|
|
* This program is public domain software; you can do whatever you like
|
|
|
|
* with this source, including modifying and redistributing it.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2001-01-27 08:24:39 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2001-02-20 06:14:08 +00:00
|
|
|
#include "busybox.h"
|
2000-09-20 19:22:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Some general definitions
|
|
|
|
*/
|
2001-01-23 22:30:04 +00:00
|
|
|
|
2000-09-20 19:22:26 +00:00
|
|
|
#define RPM_MAGIC "\355\253\356\333"
|
|
|
|
#define GZ_MAGIC_1 '\037'
|
|
|
|
#define GZ_MAGIC_2 '\213'
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
|
|
|
static char *progname;
|
|
|
|
static int infile, outfile;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a specified number of bytes from input file
|
|
|
|
*/
|
2001-01-23 22:30:04 +00:00
|
|
|
static void myread(int num, char *buffer)
|
2000-09-20 19:22:26 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = read(infile, buffer, num)) != num) {
|
|
|
|
if (err < 0)
|
2000-12-22 01:48:07 +00:00
|
|
|
perror_msg_and_die(progname);
|
2000-09-20 19:22:26 +00:00
|
|
|
else
|
2001-01-31 19:00:21 +00:00
|
|
|
error_msg_and_die("Unexpected end of input file!");
|
2000-09-20 19:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main program
|
|
|
|
*/
|
2000-11-29 21:39:02 +00:00
|
|
|
int rpmunpack_main(int argc, char **argv)
|
2000-09-20 19:22:26 +00:00
|
|
|
{
|
|
|
|
int len, status = 0;
|
2001-01-25 23:49:09 +00:00
|
|
|
RESERVE_BB_BUFFER(buffer, BUFSIZ);
|
2000-09-20 19:22:26 +00:00
|
|
|
|
|
|
|
/* Get our own program name */
|
|
|
|
if ((progname = strrchr(argv[0], '/')) == NULL)
|
|
|
|
progname = argv[0];
|
|
|
|
else
|
|
|
|
progname++;
|
|
|
|
|
|
|
|
/* Check for command line parameters */
|
|
|
|
if (argc>=2 && *argv[1]=='-') {
|
2001-02-14 21:23:06 +00:00
|
|
|
show_usage();
|
2000-09-20 19:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open input file */
|
|
|
|
if (argc == 1)
|
|
|
|
infile = STDIN_FILENO;
|
2000-12-22 01:48:07 +00:00
|
|
|
else if ((infile = open(argv[1], O_RDONLY)) < 0)
|
|
|
|
perror_msg_and_die("%s", argv[1]);
|
2000-09-20 19:22:26 +00:00
|
|
|
|
|
|
|
/* Read magic ID and output filename */
|
2001-01-23 22:30:04 +00:00
|
|
|
myread(4, buffer);
|
2000-09-20 19:22:26 +00:00
|
|
|
if (strncmp(buffer, RPM_MAGIC, 4)) {
|
2000-11-29 21:39:02 +00:00
|
|
|
fprintf(stderr, "Input file is not in RPM format!\n");
|
2000-09-20 19:22:26 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2001-01-23 22:30:04 +00:00
|
|
|
myread(6, buffer); /* Skip flags */
|
|
|
|
myread(64, buffer);
|
2000-09-20 19:22:26 +00:00
|
|
|
buffer[64] = '\0';
|
|
|
|
|
|
|
|
/* Open output file */
|
|
|
|
strcat(buffer, ".cpio.gz");
|
|
|
|
if (infile == STDIN_FILENO)
|
|
|
|
outfile = STDOUT_FILENO;
|
2000-12-22 01:48:07 +00:00
|
|
|
else if ((outfile = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
|
|
|
|
perror_msg_and_die("%s", buffer);
|
2000-09-20 19:22:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now search for the GZIP signature. This is rather awkward, but I don't
|
|
|
|
* know any other way how to find out the exact starting position of the
|
|
|
|
* archive within the input file. There are a couple of data structures
|
|
|
|
* and texts (obviously descriptions, installation shell scripts etc.)
|
|
|
|
* coming before the archive, but even they start at different offsets
|
|
|
|
* with different RPM files. However, it looks like the GZIP signature
|
|
|
|
* never appears before offset 0x200, so we skip these first couple of
|
|
|
|
* bytes to make the signature scan a little more reliable.
|
|
|
|
*/
|
2001-01-23 22:30:04 +00:00
|
|
|
myread(0x200 - 74, buffer);
|
2000-09-20 19:22:26 +00:00
|
|
|
while (status < 2) {
|
2001-01-23 22:30:04 +00:00
|
|
|
myread(1, buffer);
|
2000-09-20 19:22:26 +00:00
|
|
|
if (status == 0 && buffer[0] == GZ_MAGIC_1)
|
|
|
|
status++;
|
|
|
|
else if (status == 1 && buffer[0] == GZ_MAGIC_2)
|
|
|
|
status++;
|
|
|
|
else
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
buffer[0] = GZ_MAGIC_1;
|
|
|
|
buffer[1] = GZ_MAGIC_2;
|
2000-12-22 01:48:07 +00:00
|
|
|
if (write(outfile, buffer, 2) < 0)
|
|
|
|
perror_msg_and_die("write");
|
2000-09-20 19:22:26 +00:00
|
|
|
|
|
|
|
/* Now simply copy the GZIP archive into the output file */
|
2001-01-23 22:30:04 +00:00
|
|
|
while ((len = read(infile, buffer, BUFSIZ)) > 0) {
|
2000-12-22 01:48:07 +00:00
|
|
|
if (write(outfile, buffer, len) < 0)
|
|
|
|
perror_msg_and_die("write");
|
2000-09-20 19:22:26 +00:00
|
|
|
}
|
2000-12-22 01:48:07 +00:00
|
|
|
if (len < 0)
|
|
|
|
perror_msg_and_die("read");
|
|
|
|
return EXIT_SUCCESS;
|
2000-09-20 19:22:26 +00:00
|
|
|
}
|