Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Common code for gunzip-like applets |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 6 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 7 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 8 | #include "bb_archive.h" |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 9 | |
| 10 | enum { |
Denys Vlasenko | ccb88a6 | 2010-05-27 02:22:54 +0200 | [diff] [blame] | 11 | OPT_STDOUT = 1 << 0, |
| 12 | OPT_FORCE = 1 << 1, |
| 13 | /* only some decompressors: */ |
| 14 | OPT_VERBOSE = 1 << 2, |
| 15 | OPT_DECOMPRESS = 1 << 3, |
| 16 | OPT_TEST = 1 << 4, |
Denys Vlasenko | 8e96efa | 2013-02-28 18:06:09 +0100 | [diff] [blame^] | 17 | SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION, |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 18 | }; |
| 19 | |
| 20 | static |
| 21 | int open_to_or_warn(int to_fd, const char *filename, int flags, int mode) |
| 22 | { |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 23 | int fd = open3_or_warn(filename, flags, mode); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 24 | if (fd < 0) { |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 25 | return 1; |
| 26 | } |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 27 | xmove_fd(fd, to_fd); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 28 | return 0; |
| 29 | } |
| 30 | |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 31 | char* FAST_FUNC append_ext(char *filename, const char *expected_ext) |
| 32 | { |
| 33 | return xasprintf("%s.%s", filename, expected_ext); |
| 34 | } |
| 35 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 36 | int FAST_FUNC bbunpack(char **argv, |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 37 | IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux), |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 38 | char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext), |
| 39 | const char *expected_ext |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 40 | ) |
| 41 | { |
| 42 | struct stat stat_buf; |
Denys Vlasenko | 8e96efa | 2013-02-28 18:06:09 +0100 | [diff] [blame^] | 43 | IF_DESKTOP(long long) int status = 0; |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 44 | char *filename, *new_name; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 45 | smallint exitcode = 0; |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 46 | transformer_aux_data_t aux; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 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) { |
Denys Vlasenko | 8e96efa | 2013-02-28 18:06:09 +0100 | [diff] [blame^] | 58 | if (!(option_mask32 & SEAMLESS_MAGIC)) { |
| 59 | if (stat(filename, &stat_buf) != 0) { |
| 60 | err_name: |
| 61 | bb_simple_perror_msg(filename); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 62 | err: |
Denys Vlasenko | 8e96efa | 2013-02-28 18:06:09 +0100 | [diff] [blame^] | 63 | exitcode = 1; |
| 64 | goto free_name; |
| 65 | } |
| 66 | if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0)) |
| 67 | goto err; |
| 68 | } else { |
| 69 | /* "clever zcat" */ |
| 70 | int fd = open_zipped(filename); |
| 71 | if (fd < 0) |
| 72 | goto err_name; |
| 73 | xmove_fd(fd, STDIN_FILENO); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 74 | } |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* Special cases: test, stdout */ |
| 78 | if (option_mask32 & (OPT_STDOUT|OPT_TEST)) { |
| 79 | if (option_mask32 & OPT_TEST) |
| 80 | if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0)) |
Denys Vlasenko | f2d8478 | 2013-02-28 18:04:22 +0100 | [diff] [blame] | 81 | xfunc_die(); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 82 | filename = NULL; |
| 83 | } |
| 84 | |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 85 | /* Open dst if we are going to unpack to file */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 86 | if (filename) { |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 87 | new_name = make_new_name(filename, expected_ext); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 88 | if (!new_name) { |
| 89 | bb_error_msg("%s: unknown suffix - ignored", filename); |
| 90 | goto err; |
| 91 | } |
Denis Vlasenko | a04cc47 | 2008-06-25 20:54:45 +0000 | [diff] [blame] | 92 | |
| 93 | /* -f: overwrite existing output files */ |
| 94 | if (option_mask32 & OPT_FORCE) { |
| 95 | unlink(new_name); |
| 96 | } |
| 97 | |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 98 | /* O_EXCL: "real" bunzip2 doesn't overwrite files */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 99 | /* GNU gunzip does not bail out, but goes to next file */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 100 | if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL, |
| 101 | stat_buf.st_mode)) |
| 102 | goto err; |
| 103 | } |
| 104 | |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 105 | /* Check that the input is sane */ |
Denys Vlasenko | f2d8478 | 2013-02-28 18:04:22 +0100 | [diff] [blame] | 106 | if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) { |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 107 | bb_error_msg_and_die("compressed data not read from terminal, " |
| 108 | "use -f to force it"); |
| 109 | } |
| 110 | |
Denys Vlasenko | 8e96efa | 2013-02-28 18:06:09 +0100 | [diff] [blame^] | 111 | if (!(option_mask32 & SEAMLESS_MAGIC)) { |
| 112 | init_transformer_aux_data(&aux); |
| 113 | aux.check_signature = 1; |
| 114 | status = unpacker(&aux); |
| 115 | if (status < 0) |
| 116 | exitcode = 1; |
| 117 | } else { |
| 118 | /* "clever zcat" */ |
| 119 | if (!filename) { |
| 120 | if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0)) |
| 121 | goto err; |
| 122 | } |
| 123 | if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0) { |
| 124 | /* Disk full, tty closed, etc. No point in continuing */ |
| 125 | xfunc_die(); |
| 126 | } |
| 127 | } |
Denys Vlasenko | c531b9a | 2011-10-31 01:05:16 +0100 | [diff] [blame] | 128 | |
| 129 | if (!(option_mask32 & OPT_STDOUT)) |
| 130 | xclose(STDOUT_FILENO); /* with error check! */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 131 | |
| 132 | if (filename) { |
| 133 | char *del = new_name; |
| 134 | if (status >= 0) { |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 135 | /* TODO: restore other things? */ |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 136 | if (aux.mtime != 0) { |
Denys Vlasenko | dcbfaba | 2009-11-29 19:40:36 +0100 | [diff] [blame] | 137 | struct timeval times[2]; |
Denys Vlasenko | 389cca4 | 2009-11-15 02:28:56 +0100 | [diff] [blame] | 138 | |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 139 | times[1].tv_sec = times[0].tv_sec = aux.mtime; |
Denys Vlasenko | dcbfaba | 2009-11-29 19:40:36 +0100 | [diff] [blame] | 140 | times[1].tv_usec = times[0].tv_usec = 0; |
Denys Vlasenko | dcd27ab | 2009-10-05 03:03:07 +0200 | [diff] [blame] | 141 | /* Note: we closed it first. |
Denys Vlasenko | 389cca4 | 2009-11-15 02:28:56 +0100 | [diff] [blame] | 142 | * On some systems calling utimes |
Denys Vlasenko | dcd27ab | 2009-10-05 03:03:07 +0200 | [diff] [blame] | 143 | * then closing resets the mtime |
| 144 | * back to current time. */ |
Denys Vlasenko | dcbfaba | 2009-11-29 19:40:36 +0100 | [diff] [blame] | 145 | utimes(new_name, times); /* ignoring errors */ |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 148 | /* Delete _compressed_ file */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 149 | del = filename; |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 150 | /* restore extension (unless tgz -> tar case) */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 151 | if (new_name == filename) |
| 152 | filename[strlen(filename)] = '.'; |
| 153 | } |
Denis Vlasenko | 1bb552b | 2007-04-05 21:25:15 +0000 | [diff] [blame] | 154 | xunlink(del); |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 155 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 156 | #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */ |
| 157 | /* Extreme bloat for gunzip compat */ |
| 158 | if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) { |
| 159 | fprintf(stderr, "%s: %u%% - replaced with %s\n", |
| 160 | filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name); |
| 161 | } |
| 162 | #endif |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 163 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 164 | free_name: |
| 165 | if (new_name != filename) |
| 166 | free(new_name); |
| 167 | } |
| 168 | } while (*argv && *++argv); |
| 169 | |
Denys Vlasenko | c531b9a | 2011-10-31 01:05:16 +0100 | [diff] [blame] | 170 | if (option_mask32 & OPT_STDOUT) |
| 171 | xclose(STDOUT_FILENO); /* with error check! */ |
| 172 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 173 | return exitcode; |
| 174 | } |
| 175 | |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 176 | #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 177 | static |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 178 | char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 179 | { |
| 180 | char *extension = strrchr(filename, '.'); |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 181 | if (!extension || strcmp(extension + 1, expected_ext) != 0) { |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 182 | /* Mimic GNU gunzip - "real" bunzip2 tries to */ |
| 183 | /* unpack file anyway, to file.out */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 184 | return NULL; |
| 185 | } |
| 186 | *extension = '\0'; |
| 187 | return filename; |
| 188 | } |
Denis Vlasenko | a4688bf | 2007-03-11 10:56:37 +0000 | [diff] [blame] | 189 | #endif |
| 190 | |
| 191 | |
Denis Vlasenko | a4688bf | 2007-03-11 10:56:37 +0000 | [diff] [blame] | 192 | /* |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 193 | * Uncompress applet for busybox (c) 2002 Glenn McGrath |
Denis Vlasenko | a4688bf | 2007-03-11 10:56:37 +0000 | [diff] [blame] | 194 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 195 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | a4688bf | 2007-03-11 10:56:37 +0000 | [diff] [blame] | 196 | */ |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 197 | |
| 198 | //usage:#define uncompress_trivial_usage |
| 199 | //usage: "[-cf] [FILE]..." |
| 200 | //usage:#define uncompress_full_usage "\n\n" |
| 201 | //usage: "Decompress .Z file[s]\n" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 202 | //usage: "\n -c Write to stdout" |
| 203 | //usage: "\n -f Overwrite" |
| 204 | |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 205 | #if ENABLE_UNCOMPRESS |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 206 | static |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 207 | IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 208 | { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 209 | return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO); |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 210 | } |
| 211 | int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 212 | int uncompress_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 213 | { |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 214 | getopt32(argv, "cf"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 215 | argv += optind; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 216 | |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 217 | return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 218 | } |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 219 | #endif |
| 220 | |
| 221 | |
| 222 | /* |
| 223 | * Gzip implementation for busybox |
| 224 | * |
| 225 | * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly. |
| 226 | * |
| 227 | * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de> |
| 228 | * based on gzip sources |
| 229 | * |
| 230 | * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as |
| 231 | * well as stdin/stdout, and to generally behave itself wrt command line |
| 232 | * handling. |
| 233 | * |
| 234 | * General cleanup to better adhere to the style guide and make use of standard |
Denis Vlasenko | 0beaff8 | 2007-09-21 13:16:32 +0000 | [diff] [blame] | 235 | * busybox functions by Glenn McGrath |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 236 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 237 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 238 | * |
| 239 | * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface |
| 240 | * Copyright (C) 1992-1993 Jean-loup Gailly |
| 241 | * The unzip code was written and put in the public domain by Mark Adler. |
| 242 | * Portions of the lzw code are derived from the public domain 'compress' |
| 243 | * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies, |
| 244 | * Ken Turkowski, Dave Mack and Peter Jannesen. |
| 245 | * |
| 246 | * See the license_msg below and the file COPYING for the software license. |
| 247 | * See the file algorithm.doc for the compression algorithms and file formats. |
| 248 | */ |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 249 | |
| 250 | //usage:#define gunzip_trivial_usage |
| 251 | //usage: "[-cft] [FILE]..." |
| 252 | //usage:#define gunzip_full_usage "\n\n" |
| 253 | //usage: "Decompress FILEs (or stdin)\n" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 254 | //usage: "\n -c Write to stdout" |
| 255 | //usage: "\n -f Force" |
| 256 | //usage: "\n -t Test file integrity" |
| 257 | //usage: |
| 258 | //usage:#define gunzip_example_usage |
| 259 | //usage: "$ ls -la /tmp/BusyBox*\n" |
| 260 | //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n" |
| 261 | //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n" |
| 262 | //usage: "$ ls -la /tmp/BusyBox*\n" |
| 263 | //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n" |
| 264 | //usage: |
| 265 | //usage:#define zcat_trivial_usage |
Denys Vlasenko | f2d8478 | 2013-02-28 18:04:22 +0100 | [diff] [blame] | 266 | //usage: "[FILE]..." |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 267 | //usage:#define zcat_full_usage "\n\n" |
| 268 | //usage: "Decompress to stdout" |
| 269 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 270 | #if ENABLE_GUNZIP |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 271 | static |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 272 | char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 273 | { |
| 274 | char *extension = strrchr(filename, '.'); |
| 275 | |
| 276 | if (!extension) |
| 277 | return NULL; |
| 278 | |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 279 | extension++; |
| 280 | if (strcmp(extension, "tgz" + 1) == 0 |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 281 | #if ENABLE_FEATURE_SEAMLESS_Z |
| 282 | || (extension[0] == 'Z' && extension[1] == '\0') |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 283 | #endif |
| 284 | ) { |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 285 | extension[-1] = '\0'; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 286 | } else if (strcmp(extension, "tgz") == 0) { |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 287 | filename = xstrdup(filename); |
| 288 | extension = strrchr(filename, '.'); |
| 289 | extension[2] = 'a'; |
| 290 | extension[3] = 'r'; |
| 291 | } else { |
| 292 | return NULL; |
| 293 | } |
| 294 | return filename; |
| 295 | } |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 296 | static |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 297 | IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 298 | { |
Denys Vlasenko | 02c3c38 | 2012-03-06 16:32:06 +0100 | [diff] [blame] | 299 | return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 300 | } |
Denis Vlasenko | 5297809 | 2008-03-30 13:11:47 +0000 | [diff] [blame] | 301 | /* |
| 302 | * Linux kernel build uses gzip -d -n. We accept and ignore it. |
| 303 | * Man page says: |
| 304 | * -n --no-name |
| 305 | * gzip: do not save the original file name and time stamp. |
| 306 | * (The original name is always saved if the name had to be truncated.) |
| 307 | * gunzip: do not restore the original file name/time even if present |
| 308 | * (remove only the gzip suffix from the compressed file name). |
| 309 | * This option is the default when decompressing. |
| 310 | * -N --name |
| 311 | * gzip: always save the original file name and time stamp (this is the default) |
| 312 | * gunzip: restore the original file name and time stamp if present. |
| 313 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 314 | int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 315 | int gunzip_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 316 | { |
Denis Vlasenko | 5297809 | 2008-03-30 13:11:47 +0000 | [diff] [blame] | 317 | getopt32(argv, "cfvdtn"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 318 | argv += optind; |
Denys Vlasenko | 8e96efa | 2013-02-28 18:06:09 +0100 | [diff] [blame^] | 319 | |
| 320 | /* If called as zcat... |
| 321 | * Normally, "zcat" is just "gunzip -c". |
| 322 | * But if seamless magic is enabled, then we are much more clever. |
| 323 | */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 324 | if (applet_name[1] == 'c') |
Denys Vlasenko | 8e96efa | 2013-02-28 18:06:09 +0100 | [diff] [blame^] | 325 | option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 326 | |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 327 | return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 328 | } |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 329 | #endif |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 330 | |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * Modified for busybox by Glenn McGrath |
| 334 | * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no> |
| 335 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 336 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 337 | */ |
Denys Vlasenko | f0f9470 | 2010-06-06 01:53:38 +0200 | [diff] [blame] | 338 | //usage:#define bunzip2_trivial_usage |
Denys Vlasenko | 3b2acb7 | 2010-10-09 21:10:32 +0200 | [diff] [blame] | 339 | //usage: "[-cf] [FILE]..." |
Denys Vlasenko | f0f9470 | 2010-06-06 01:53:38 +0200 | [diff] [blame] | 340 | //usage:#define bunzip2_full_usage "\n\n" |
| 341 | //usage: "Decompress FILEs (or stdin)\n" |
Denys Vlasenko | f0f9470 | 2010-06-06 01:53:38 +0200 | [diff] [blame] | 342 | //usage: "\n -c Write to stdout" |
| 343 | //usage: "\n -f Force" |
| 344 | //usage:#define bzcat_trivial_usage |
Denys Vlasenko | f2d8478 | 2013-02-28 18:04:22 +0100 | [diff] [blame] | 345 | //usage: "[FILE]..." |
Denys Vlasenko | f0f9470 | 2010-06-06 01:53:38 +0200 | [diff] [blame] | 346 | //usage:#define bzcat_full_usage "\n\n" |
| 347 | //usage: "Decompress to stdout" |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame] | 348 | //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 349 | //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat)) |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 350 | #if ENABLE_BUNZIP2 |
| 351 | static |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 352 | IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux) |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 353 | { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 354 | return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO); |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 355 | } |
| 356 | int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 357 | int bunzip2_main(int argc UNUSED_PARAM, char **argv) |
| 358 | { |
| 359 | getopt32(argv, "cfvdt"); |
| 360 | argv += optind; |
| 361 | if (applet_name[2] == 'c') /* bzcat */ |
| 362 | option_mask32 |= OPT_STDOUT; |
| 363 | |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 364 | return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2"); |
Denys Vlasenko | d93f19e | 2010-05-30 03:46:54 +0200 | [diff] [blame] | 365 | } |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 366 | #endif |
| 367 | |
| 368 | |
| 369 | /* |
| 370 | * Small lzma deflate implementation. |
| 371 | * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> |
| 372 | * |
| 373 | * Based on bunzip.c from busybox |
| 374 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 375 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 376 | */ |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 377 | |
| 378 | //usage:#define unlzma_trivial_usage |
| 379 | //usage: "[-cf] [FILE]..." |
| 380 | //usage:#define unlzma_full_usage "\n\n" |
| 381 | //usage: "Decompress FILE (or stdin)\n" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 382 | //usage: "\n -c Write to stdout" |
| 383 | //usage: "\n -f Force" |
| 384 | //usage: |
| 385 | //usage:#define lzma_trivial_usage |
| 386 | //usage: "-d [-cf] [FILE]..." |
| 387 | //usage:#define lzma_full_usage "\n\n" |
| 388 | //usage: "Decompress FILE (or stdin)\n" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 389 | //usage: "\n -d Decompress" |
| 390 | //usage: "\n -c Write to stdout" |
| 391 | //usage: "\n -f Force" |
| 392 | //usage: |
| 393 | //usage:#define lzcat_trivial_usage |
Denys Vlasenko | f2d8478 | 2013-02-28 18:04:22 +0100 | [diff] [blame] | 394 | //usage: "[FILE]..." |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 395 | //usage:#define lzcat_full_usage "\n\n" |
| 396 | //usage: "Decompress to stdout" |
| 397 | //usage: |
| 398 | //usage:#define unxz_trivial_usage |
| 399 | //usage: "[-cf] [FILE]..." |
| 400 | //usage:#define unxz_full_usage "\n\n" |
| 401 | //usage: "Decompress FILE (or stdin)\n" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 402 | //usage: "\n -c Write to stdout" |
| 403 | //usage: "\n -f Force" |
| 404 | //usage: |
| 405 | //usage:#define xz_trivial_usage |
| 406 | //usage: "-d [-cf] [FILE]..." |
| 407 | //usage:#define xz_full_usage "\n\n" |
| 408 | //usage: "Decompress FILE (or stdin)\n" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 409 | //usage: "\n -d Decompress" |
| 410 | //usage: "\n -c Write to stdout" |
| 411 | //usage: "\n -f Force" |
| 412 | //usage: |
| 413 | //usage:#define xzcat_trivial_usage |
Denys Vlasenko | f2d8478 | 2013-02-28 18:04:22 +0100 | [diff] [blame] | 414 | //usage: "[FILE]..." |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 415 | //usage:#define xzcat_full_usage "\n\n" |
| 416 | //usage: "Decompress to stdout" |
| 417 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 418 | #if ENABLE_UNLZMA |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 419 | static |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 420 | IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 421 | { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 422 | return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 423 | } |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 424 | int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 425 | int unlzma_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 426 | { |
Denys Vlasenko | e04c867 | 2010-05-30 03:33:50 +0200 | [diff] [blame] | 427 | IF_LZMA(int opts =) getopt32(argv, "cfvdt"); |
| 428 | # if ENABLE_LZMA |
Denys Vlasenko | ccb88a6 | 2010-05-27 02:22:54 +0200 | [diff] [blame] | 429 | /* lzma without -d or -t? */ |
| 430 | if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST))) |
| 431 | bb_show_usage(); |
| 432 | # endif |
| 433 | /* lzcat? */ |
| 434 | if (applet_name[2] == 'c') |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 435 | option_mask32 |= OPT_STDOUT; |
| 436 | |
Denys Vlasenko | ccb88a6 | 2010-05-27 02:22:54 +0200 | [diff] [blame] | 437 | argv += optind; |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 438 | return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 439 | } |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 440 | #endif |
| 441 | |
| 442 | |
Denys Vlasenko | 602ce69 | 2010-05-30 03:35:18 +0200 | [diff] [blame] | 443 | #if ENABLE_UNXZ |
Denys Vlasenko | 602ce69 | 2010-05-30 03:35:18 +0200 | [diff] [blame] | 444 | static |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 445 | IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux) |
Denys Vlasenko | 602ce69 | 2010-05-30 03:35:18 +0200 | [diff] [blame] | 446 | { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 447 | return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO); |
Denys Vlasenko | 602ce69 | 2010-05-30 03:35:18 +0200 | [diff] [blame] | 448 | } |
Denys Vlasenko | 602ce69 | 2010-05-30 03:35:18 +0200 | [diff] [blame] | 449 | int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 450 | int unxz_main(int argc UNUSED_PARAM, char **argv) |
| 451 | { |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 452 | IF_XZ(int opts =) getopt32(argv, "cfvdt"); |
Denys Vlasenko | 602ce69 | 2010-05-30 03:35:18 +0200 | [diff] [blame] | 453 | # if ENABLE_XZ |
| 454 | /* xz without -d or -t? */ |
| 455 | if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST))) |
| 456 | bb_show_usage(); |
| 457 | # endif |
| 458 | /* xzcat? */ |
| 459 | if (applet_name[2] == 'c') |
| 460 | option_mask32 |= OPT_STDOUT; |
| 461 | |
| 462 | argv += optind; |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 463 | return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz"); |
Denys Vlasenko | 602ce69 | 2010-05-30 03:35:18 +0200 | [diff] [blame] | 464 | } |
Denys Vlasenko | 602ce69 | 2010-05-30 03:35:18 +0200 | [diff] [blame] | 465 | #endif |