Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | 4bfb6b7 | 2000-11-29 21:39:02 +0000 | [diff] [blame] | 2 | * rpmunpack for busybox |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 3 | * |
| 4 | * rpmunpack.c - Utility program to unpack an RPM archive |
| 5 | * |
| 6 | * Gero Kuhlmann <gero@gkminix.han.de> 1998 |
| 7 | * |
| 8 | * This program is public domain software; you can do whatever you like |
| 9 | * with this source, including modifying and redistributing it. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | */ |
| 15 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 16 | #include "busybox.h" |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame^] | 18 | #include <unistd.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Some general definitions |
| 24 | */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 25 | |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 26 | #define RPM_MAGIC "\355\253\356\333" |
| 27 | #define GZ_MAGIC_1 '\037' |
| 28 | #define GZ_MAGIC_2 '\213' |
| 29 | |
| 30 | /* |
| 31 | * Global variables |
| 32 | */ |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 33 | static char *progname; |
| 34 | static int infile, outfile; |
| 35 | |
| 36 | /* |
| 37 | * Read a specified number of bytes from input file |
| 38 | */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 39 | static void myread(int num, char *buffer) |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 40 | { |
| 41 | int err; |
| 42 | |
| 43 | if ((err = read(infile, buffer, num)) != num) { |
| 44 | if (err < 0) |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 45 | perror_msg_and_die(progname); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 46 | else |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 47 | error_msg_and_die("Unexpected end of input file!\n"); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Main program |
| 53 | */ |
Eric Andersen | 4bfb6b7 | 2000-11-29 21:39:02 +0000 | [diff] [blame] | 54 | int rpmunpack_main(int argc, char **argv) |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 55 | { |
| 56 | int len, status = 0; |
Eric Andersen | d35c215 | 2001-01-25 23:49:09 +0000 | [diff] [blame] | 57 | RESERVE_BB_BUFFER(buffer, BUFSIZ); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 58 | |
| 59 | /* Get our own program name */ |
| 60 | if ((progname = strrchr(argv[0], '/')) == NULL) |
| 61 | progname = argv[0]; |
| 62 | else |
| 63 | progname++; |
| 64 | |
| 65 | /* Check for command line parameters */ |
| 66 | if (argc>=2 && *argv[1]=='-') { |
Eric Andersen | 4bfb6b7 | 2000-11-29 21:39:02 +0000 | [diff] [blame] | 67 | usage(rpmunpack_usage); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* Open input file */ |
| 71 | if (argc == 1) |
| 72 | infile = STDIN_FILENO; |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 73 | else if ((infile = open(argv[1], O_RDONLY)) < 0) |
| 74 | perror_msg_and_die("%s", argv[1]); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 75 | |
| 76 | /* Read magic ID and output filename */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 77 | myread(4, buffer); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 78 | if (strncmp(buffer, RPM_MAGIC, 4)) { |
Eric Andersen | 4bfb6b7 | 2000-11-29 21:39:02 +0000 | [diff] [blame] | 79 | fprintf(stderr, "Input file is not in RPM format!\n"); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 80 | exit(1); |
| 81 | } |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 82 | myread(6, buffer); /* Skip flags */ |
| 83 | myread(64, buffer); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 84 | buffer[64] = '\0'; |
| 85 | |
| 86 | /* Open output file */ |
| 87 | strcat(buffer, ".cpio.gz"); |
| 88 | if (infile == STDIN_FILENO) |
| 89 | outfile = STDOUT_FILENO; |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 90 | else if ((outfile = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) |
| 91 | perror_msg_and_die("%s", buffer); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Now search for the GZIP signature. This is rather awkward, but I don't |
| 95 | * know any other way how to find out the exact starting position of the |
| 96 | * archive within the input file. There are a couple of data structures |
| 97 | * and texts (obviously descriptions, installation shell scripts etc.) |
| 98 | * coming before the archive, but even they start at different offsets |
| 99 | * with different RPM files. However, it looks like the GZIP signature |
| 100 | * never appears before offset 0x200, so we skip these first couple of |
| 101 | * bytes to make the signature scan a little more reliable. |
| 102 | */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 103 | myread(0x200 - 74, buffer); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 104 | while (status < 2) { |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 105 | myread(1, buffer); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 106 | if (status == 0 && buffer[0] == GZ_MAGIC_1) |
| 107 | status++; |
| 108 | else if (status == 1 && buffer[0] == GZ_MAGIC_2) |
| 109 | status++; |
| 110 | else |
| 111 | status = 0; |
| 112 | } |
| 113 | buffer[0] = GZ_MAGIC_1; |
| 114 | buffer[1] = GZ_MAGIC_2; |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 115 | if (write(outfile, buffer, 2) < 0) |
| 116 | perror_msg_and_die("write"); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 117 | |
| 118 | /* Now simply copy the GZIP archive into the output file */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 119 | while ((len = read(infile, buffer, BUFSIZ)) > 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 120 | if (write(outfile, buffer, len) < 0) |
| 121 | perror_msg_and_die("write"); |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 122 | } |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 123 | if (len < 0) |
| 124 | perror_msg_and_die("read"); |
| 125 | return EXIT_SUCCESS; |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 126 | } |