Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Modified for busybox by Glenn McGrath <bug1@iinet.net.au> |
| 4 | * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no> |
| 5 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 7 | */ |
| 8 | |
| 9 | #include "busybox.h" |
| 10 | #include "unarchive.h" |
| 11 | |
| 12 | enum { |
| 13 | OPT_STDOUT = 1, |
| 14 | OPT_FORCE = 2, |
| 15 | /* gunzip only: */ |
| 16 | OPT_TEST = 4, |
| 17 | OPT_DECOMPRESS = 8, |
| 18 | OPT_VERBOSE = 0x10, |
| 19 | }; |
| 20 | |
| 21 | static |
| 22 | int open_to_or_warn(int to_fd, const char *filename, int flags, int mode) |
| 23 | { |
| 24 | int fd = open(filename, flags, mode); |
| 25 | if (fd < 0) { |
| 26 | bb_perror_msg("%s", filename); |
| 27 | return 1; |
| 28 | } |
| 29 | if (fd != to_fd) { |
| 30 | if (dup2(fd, to_fd) < 0) |
| 31 | bb_perror_msg_and_die("cannot dup"); |
| 32 | close(fd); |
| 33 | } |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static |
| 38 | int unpack(char **argv, |
| 39 | char* (*make_new_name)(char *filename), |
| 40 | USE_DESKTOP(long long) int (*unpacker)(void) |
| 41 | ) |
| 42 | { |
| 43 | struct stat stat_buf; |
| 44 | USE_DESKTOP(long long) int status; |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 45 | char *filename, *new_name; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 46 | smallint exitcode = 0; |
| 47 | |
| 48 | do { |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 49 | /* NB: new_name is *maybe* malloc'ed! */ |
| 50 | new_name = NULL; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 51 | filename = *argv; /* can be NULL - 'streaming' bunzip2 */ |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 53 | if (filename && LONE_DASH(filename)) |
| 54 | filename = NULL; |
| 55 | |
| 56 | /* Open src */ |
| 57 | if (filename) { |
| 58 | if (stat(filename, &stat_buf) != 0) { |
| 59 | bb_perror_msg("%s", filename); |
| 60 | err: |
| 61 | exitcode = 1; |
| 62 | goto free_name; |
| 63 | } |
| 64 | if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0)) |
| 65 | goto err; |
| 66 | } |
| 67 | |
| 68 | /* Special cases: test, stdout */ |
| 69 | if (option_mask32 & (OPT_STDOUT|OPT_TEST)) { |
| 70 | if (option_mask32 & OPT_TEST) |
| 71 | if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0)) |
| 72 | goto err; |
| 73 | filename = NULL; |
| 74 | } |
| 75 | |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 76 | /* Open dst if we are going to unpack to file */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 77 | if (filename) { |
| 78 | new_name = make_new_name(filename); |
| 79 | if (!new_name) { |
| 80 | bb_error_msg("%s: unknown suffix - ignored", filename); |
| 81 | goto err; |
| 82 | } |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 83 | /* O_EXCL: "real" bunzip2 doesn't overwrite files */ |
| 84 | /* GNU gunzip goes not bail out, but goes to next file */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 85 | if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL, |
| 86 | stat_buf.st_mode)) |
| 87 | goto err; |
| 88 | } |
| 89 | |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 90 | /* Check that the input is sane */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 91 | if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) { |
| 92 | bb_error_msg_and_die("compressed data not read from terminal, " |
| 93 | "use -f to force it"); |
| 94 | } |
| 95 | |
| 96 | status = unpacker(); |
| 97 | if (status < 0) |
| 98 | exitcode = 1; |
| 99 | |
| 100 | if (filename) { |
| 101 | char *del = new_name; |
| 102 | if (status >= 0) { |
| 103 | /* TODO: restore user/group/times here? */ |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 104 | /* Delete _compressed_ file */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 105 | del = filename; |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 106 | /* restore extension (unless tgz -> tar case) */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 107 | if (new_name == filename) |
| 108 | filename[strlen(filename)] = '.'; |
| 109 | } |
| 110 | if (unlink(del) < 0) |
| 111 | bb_perror_msg_and_die("cannot remove %s", del); |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 113 | #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */ |
| 114 | /* Extreme bloat for gunzip compat */ |
| 115 | if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) { |
| 116 | fprintf(stderr, "%s: %u%% - replaced with %s\n", |
| 117 | filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name); |
| 118 | } |
| 119 | #endif |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 120 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 121 | free_name: |
| 122 | if (new_name != filename) |
| 123 | free(new_name); |
| 124 | } |
| 125 | } while (*argv && *++argv); |
| 126 | |
| 127 | return exitcode; |
| 128 | } |
| 129 | |
| 130 | #if ENABLE_BUNZIP2 |
| 131 | |
| 132 | static |
| 133 | char* make_new_name_bunzip2(char *filename) |
| 134 | { |
| 135 | char *extension = strrchr(filename, '.'); |
| 136 | if (!extension || strcmp(extension, ".bz2") != 0) { |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 137 | /* Mimic GNU gunzip - "real" bunzip2 tries to */ |
| 138 | /* unpack file anyway, to file.out */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 139 | return NULL; |
| 140 | } |
| 141 | *extension = '\0'; |
| 142 | return filename; |
| 143 | } |
| 144 | |
| 145 | static |
| 146 | USE_DESKTOP(long long) int unpack_bunzip2(void) |
| 147 | { |
| 148 | return uncompressStream(STDIN_FILENO, STDOUT_FILENO); |
| 149 | } |
| 150 | |
| 151 | int bunzip2_main(int argc, char **argv); |
| 152 | int bunzip2_main(int argc, char **argv) |
| 153 | { |
| 154 | getopt32(argc, argv, "cf"); |
| 155 | argv += optind; |
| 156 | if (applet_name[2] == 'c') |
| 157 | option_mask32 |= OPT_STDOUT; |
| 158 | |
| 159 | return unpack(argv, make_new_name_bunzip2, unpack_bunzip2); |
| 160 | } |
| 161 | |
| 162 | #endif |
| 163 | |
| 164 | |
| 165 | /* |
| 166 | * Gzip implementation for busybox |
| 167 | * |
| 168 | * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly. |
| 169 | * |
| 170 | * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de> |
| 171 | * based on gzip sources |
| 172 | * |
| 173 | * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as |
| 174 | * well as stdin/stdout, and to generally behave itself wrt command line |
| 175 | * handling. |
| 176 | * |
| 177 | * General cleanup to better adhere to the style guide and make use of standard |
| 178 | * busybox functions by Glenn McGrath <bug1@iinet.net.au> |
| 179 | * |
| 180 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 181 | * |
| 182 | * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface |
| 183 | * Copyright (C) 1992-1993 Jean-loup Gailly |
| 184 | * The unzip code was written and put in the public domain by Mark Adler. |
| 185 | * Portions of the lzw code are derived from the public domain 'compress' |
| 186 | * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies, |
| 187 | * Ken Turkowski, Dave Mack and Peter Jannesen. |
| 188 | * |
| 189 | * See the license_msg below and the file COPYING for the software license. |
| 190 | * See the file algorithm.doc for the compression algorithms and file formats. |
| 191 | */ |
| 192 | |
| 193 | #if ENABLE_GUNZIP |
| 194 | |
| 195 | static |
| 196 | char* make_new_name_gunzip(char *filename) |
| 197 | { |
| 198 | char *extension = strrchr(filename, '.'); |
| 199 | |
| 200 | if (!extension) |
| 201 | return NULL; |
| 202 | |
| 203 | if (strcmp(extension, ".gz") == 0 |
| 204 | #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS |
| 205 | || strcmp(extension, ".Z") == 0 |
| 206 | #endif |
| 207 | ) { |
| 208 | *extension = '\0'; |
| 209 | } else if(strcmp(extension, ".tgz") == 0) { |
| 210 | filename = xstrdup(filename); |
| 211 | extension = strrchr(filename, '.'); |
| 212 | extension[2] = 'a'; |
| 213 | extension[3] = 'r'; |
| 214 | } else { |
| 215 | return NULL; |
| 216 | } |
| 217 | return filename; |
| 218 | } |
| 219 | |
| 220 | static |
| 221 | USE_DESKTOP(long long) int unpack_gunzip(void) |
| 222 | { |
| 223 | USE_DESKTOP(long long) int status = -1; |
| 224 | |
| 225 | /* do the decompression, and cleanup */ |
| 226 | if (xread_char(STDIN_FILENO) == 0x1f) { |
| 227 | unsigned char magic2; |
| 228 | |
| 229 | magic2 = xread_char(STDIN_FILENO); |
| 230 | if (ENABLE_FEATURE_GUNZIP_UNCOMPRESS && magic2 == 0x9d) { |
| 231 | status = uncompress(STDIN_FILENO, STDOUT_FILENO); |
| 232 | } else if (magic2 == 0x8b) { |
| 233 | check_header_gzip_or_die(STDIN_FILENO); |
| 234 | status = inflate_gunzip(STDIN_FILENO, STDOUT_FILENO); |
| 235 | } else { |
| 236 | goto bad_magic; |
| 237 | } |
| 238 | if (status < 0) { |
| 239 | bb_error_msg("error inflating"); |
| 240 | } |
| 241 | } else { |
| 242 | bad_magic: |
| 243 | bb_error_msg("invalid magic"); |
| 244 | /* status is still == -1 */ |
| 245 | } |
| 246 | return status; |
| 247 | } |
| 248 | |
| 249 | int gunzip_main(int argc, char **argv); |
| 250 | int gunzip_main(int argc, char **argv) |
| 251 | { |
| 252 | getopt32(argc, argv, "cftdv"); |
| 253 | argv += optind; |
| 254 | /* if called as zcat */ |
| 255 | if (applet_name[1] == 'c') |
| 256 | option_mask32 |= OPT_STDOUT; |
| 257 | |
| 258 | return unpack(argv, make_new_name_gunzip, unpack_gunzip); |
| 259 | } |
| 260 | |
| 261 | #endif |
| 262 | |
| 263 | |
| 264 | /* |
| 265 | * Small lzma deflate implementation. |
| 266 | * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> |
| 267 | * |
| 268 | * Based on bunzip.c from busybox |
| 269 | * |
| 270 | * Licensed under GPL v2, see file LICENSE in this tarball for details. |
| 271 | */ |
| 272 | |
| 273 | #if ENABLE_UNLZMA |
| 274 | |
| 275 | static |
| 276 | char* make_new_name_unlzma(char *filename) |
| 277 | { |
| 278 | char *extension = strrchr(filename, '.'); |
| 279 | if (!extension || strcmp(extension, ".lzma") != 0) |
| 280 | return NULL; |
| 281 | *extension = '\0'; |
| 282 | return filename; |
| 283 | } |
| 284 | |
| 285 | static |
| 286 | USE_DESKTOP(long long) int unpack_unlzma(void) |
| 287 | { |
| 288 | return unlzma(STDIN_FILENO, STDOUT_FILENO); |
| 289 | } |
| 290 | |
| 291 | int unlzma_main(int argc, char **argv); |
| 292 | int unlzma_main(int argc, char **argv) |
| 293 | { |
| 294 | getopt32(argc, argv, "c"); |
| 295 | argv += optind; |
| 296 | /* lzmacat? */ |
| 297 | if (applet_name[4] == 'c') |
| 298 | option_mask32 |= OPT_STDOUT; |
| 299 | |
| 300 | return unpack(argv, make_new_name_unlzma, unpack_unlzma); |
| 301 | } |
| 302 | |
| 303 | #endif |
| 304 | |
| 305 | |
| 306 | /* vi: set sw=4 ts=4: */ |
| 307 | /* |
| 308 | * Uncompress applet for busybox (c) 2002 Glenn McGrath |
| 309 | * |
| 310 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 311 | */ |
| 312 | |
| 313 | #if ENABLE_UNCOMPRESS |
| 314 | |
| 315 | static |
| 316 | char* make_new_name_uncompress(char *filename) |
| 317 | { |
| 318 | char *extension = strrchr(filename, '.'); |
| 319 | if (!extension || strcmp(extension, ".Z") != 0) |
| 320 | return NULL; |
| 321 | *extension = '\0'; |
| 322 | return filename; |
| 323 | } |
| 324 | |
| 325 | static |
| 326 | USE_DESKTOP(long long) int unpack_uncompress(void) |
| 327 | { |
| 328 | USE_DESKTOP(long long) int status = -1; |
| 329 | |
| 330 | if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) { |
| 331 | bb_error_msg("invalid magic"); |
| 332 | } else { |
| 333 | status = uncompress(STDIN_FILENO, STDOUT_FILENO); |
| 334 | } |
| 335 | return status; |
| 336 | } |
| 337 | |
| 338 | int uncompress_main(int argc, char **argv); |
| 339 | int uncompress_main(int argc, char **argv) |
| 340 | { |
| 341 | getopt32(argc, argv, "cf"); |
| 342 | argv += optind; |
| 343 | |
| 344 | return unpack(argv, make_new_name_uncompress, unpack_uncompress); |
| 345 | } |
| 346 | |
| 347 | #endif |