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