Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | a4688bf | 2007-03-11 10:56:37 +0000 | [diff] [blame] | 3 | * Common code for gunzip-like applets |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 6 | */ |
| 7 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 8 | #include "libbb.h" |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 9 | #include "unarchive.h" |
| 10 | |
| 11 | enum { |
Denis Vlasenko | 7560578 | 2007-03-14 00:07:51 +0000 | [diff] [blame] | 12 | OPT_STDOUT = 0x1, |
| 13 | OPT_FORCE = 0x2, |
Denis Vlasenko | 6fa3ab3 | 2007-10-21 18:59:58 +0000 | [diff] [blame] | 14 | /* gunzip and bunzip2 only: */ |
Denis Vlasenko | 7560578 | 2007-03-14 00:07:51 +0000 | [diff] [blame] | 15 | OPT_VERBOSE = 0x4, |
| 16 | OPT_DECOMPRESS = 0x8, |
| 17 | OPT_TEST = 0x10, |
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 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 31 | int FAST_FUNC bbunpack(char **argv, |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 32 | char* (*make_new_name)(char *filename), |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 33 | IF_DESKTOP(long long) int (*unpacker)(unpack_info_t *info) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 34 | ) |
| 35 | { |
| 36 | struct stat stat_buf; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 37 | IF_DESKTOP(long long) int status; |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 38 | char *filename, *new_name; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 39 | smallint exitcode = 0; |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 40 | unpack_info_t info; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 41 | |
| 42 | do { |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 43 | /* NB: new_name is *maybe* malloc'ed! */ |
| 44 | new_name = NULL; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 45 | filename = *argv; /* can be NULL - 'streaming' bunzip2 */ |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 47 | if (filename && LONE_DASH(filename)) |
| 48 | filename = NULL; |
| 49 | |
| 50 | /* Open src */ |
| 51 | if (filename) { |
| 52 | if (stat(filename, &stat_buf) != 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 53 | bb_simple_perror_msg(filename); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 54 | err: |
| 55 | exitcode = 1; |
| 56 | goto free_name; |
| 57 | } |
| 58 | if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0)) |
| 59 | goto err; |
| 60 | } |
| 61 | |
| 62 | /* Special cases: test, stdout */ |
| 63 | if (option_mask32 & (OPT_STDOUT|OPT_TEST)) { |
| 64 | if (option_mask32 & OPT_TEST) |
| 65 | if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0)) |
| 66 | goto err; |
| 67 | filename = NULL; |
| 68 | } |
| 69 | |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 70 | /* Open dst if we are going to unpack to file */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 71 | if (filename) { |
| 72 | new_name = make_new_name(filename); |
| 73 | if (!new_name) { |
| 74 | bb_error_msg("%s: unknown suffix - ignored", filename); |
| 75 | goto err; |
| 76 | } |
Denis Vlasenko | a04cc47 | 2008-06-25 20:54:45 +0000 | [diff] [blame] | 77 | |
| 78 | /* -f: overwrite existing output files */ |
| 79 | if (option_mask32 & OPT_FORCE) { |
| 80 | unlink(new_name); |
| 81 | } |
| 82 | |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 83 | /* O_EXCL: "real" bunzip2 doesn't overwrite files */ |
Denis Vlasenko | 5dd8a03 | 2007-10-05 15:26:08 +0000 | [diff] [blame] | 84 | /* GNU gunzip does 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 | |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 96 | /* memset(&info, 0, sizeof(info)); */ |
| 97 | info.mtime = 0; /* so far it has one member only */ |
| 98 | status = unpacker(&info); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 99 | if (status < 0) |
| 100 | exitcode = 1; |
Denys Vlasenko | dcd27ab | 2009-10-05 03:03:07 +0200 | [diff] [blame] | 101 | xclose(STDOUT_FILENO); /* with error check! */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 102 | |
| 103 | if (filename) { |
| 104 | char *del = new_name; |
| 105 | if (status >= 0) { |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 106 | /* TODO: restore other things? */ |
| 107 | if (info.mtime) { |
Denys Vlasenko | dcbfaba | 2009-11-29 19:40:36 +0100 | [diff] [blame] | 108 | struct timeval times[2]; |
Denys Vlasenko | 389cca4 | 2009-11-15 02:28:56 +0100 | [diff] [blame] | 109 | |
Denys Vlasenko | dcbfaba | 2009-11-29 19:40:36 +0100 | [diff] [blame] | 110 | times[1].tv_sec = times[0].tv_sec = info.mtime; |
| 111 | times[1].tv_usec = times[0].tv_usec = 0; |
Denys Vlasenko | dcd27ab | 2009-10-05 03:03:07 +0200 | [diff] [blame] | 112 | /* Note: we closed it first. |
Denys Vlasenko | 389cca4 | 2009-11-15 02:28:56 +0100 | [diff] [blame] | 113 | * On some systems calling utimes |
Denys Vlasenko | dcd27ab | 2009-10-05 03:03:07 +0200 | [diff] [blame] | 114 | * then closing resets the mtime |
| 115 | * back to current time. */ |
Denys Vlasenko | dcbfaba | 2009-11-29 19:40:36 +0100 | [diff] [blame] | 116 | utimes(new_name, times); /* ignoring errors */ |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 119 | /* Delete _compressed_ file */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 120 | del = filename; |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 121 | /* restore extension (unless tgz -> tar case) */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 122 | if (new_name == filename) |
| 123 | filename[strlen(filename)] = '.'; |
| 124 | } |
Denis Vlasenko | 1bb552b | 2007-04-05 21:25:15 +0000 | [diff] [blame] | 125 | xunlink(del); |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 126 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 127 | #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */ |
| 128 | /* Extreme bloat for gunzip compat */ |
| 129 | if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) { |
| 130 | fprintf(stderr, "%s: %u%% - replaced with %s\n", |
| 131 | filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name); |
| 132 | } |
| 133 | #endif |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 134 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 135 | free_name: |
| 136 | if (new_name != filename) |
| 137 | free(new_name); |
| 138 | } |
| 139 | } while (*argv && *++argv); |
| 140 | |
| 141 | return exitcode; |
| 142 | } |
| 143 | |
Denis Vlasenko | a4688bf | 2007-03-11 10:56:37 +0000 | [diff] [blame] | 144 | #if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS |
| 145 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 146 | static |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 147 | char* make_new_name_generic(char *filename, const char *expected_ext) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 148 | { |
| 149 | char *extension = strrchr(filename, '.'); |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 150 | if (!extension || strcmp(extension + 1, expected_ext) != 0) { |
Denis Vlasenko | 6c939e0 | 2007-03-07 23:22:47 +0000 | [diff] [blame] | 151 | /* Mimic GNU gunzip - "real" bunzip2 tries to */ |
| 152 | /* unpack file anyway, to file.out */ |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 153 | return NULL; |
| 154 | } |
| 155 | *extension = '\0'; |
| 156 | return filename; |
| 157 | } |
| 158 | |
Denis Vlasenko | a4688bf | 2007-03-11 10:56:37 +0000 | [diff] [blame] | 159 | #endif |
| 160 | |
| 161 | |
Denis Vlasenko | a4688bf | 2007-03-11 10:56:37 +0000 | [diff] [blame] | 162 | /* |
Denis Vlasenko | 0beaff8 | 2007-09-21 13:16:32 +0000 | [diff] [blame] | 163 | * Modified for busybox by Glenn McGrath |
Denis Vlasenko | a4688bf | 2007-03-11 10:56:37 +0000 | [diff] [blame] | 164 | * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no> |
| 165 | * |
| 166 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 167 | */ |
| 168 | |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 169 | #if ENABLE_BUNZIP2 |
| 170 | |
| 171 | static |
| 172 | char* make_new_name_bunzip2(char *filename) |
| 173 | { |
| 174 | return make_new_name_generic(filename, "bz2"); |
| 175 | } |
| 176 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 177 | static |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 178 | IF_DESKTOP(long long) int unpack_bunzip2(unpack_info_t *info UNUSED_PARAM) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 179 | { |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 180 | return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 183 | int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 184 | int bunzip2_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 185 | { |
Denis Vlasenko | 6fa3ab3 | 2007-10-21 18:59:58 +0000 | [diff] [blame] | 186 | getopt32(argv, "cfvdt"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 187 | argv += optind; |
| 188 | if (applet_name[2] == 'c') |
| 189 | option_mask32 |= OPT_STDOUT; |
| 190 | |
Denis Vlasenko | 7560578 | 2007-03-14 00:07:51 +0000 | [diff] [blame] | 191 | return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | #endif |
| 195 | |
| 196 | |
| 197 | /* |
| 198 | * Gzip implementation for busybox |
| 199 | * |
| 200 | * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly. |
| 201 | * |
| 202 | * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de> |
| 203 | * based on gzip sources |
| 204 | * |
| 205 | * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as |
| 206 | * well as stdin/stdout, and to generally behave itself wrt command line |
| 207 | * handling. |
| 208 | * |
| 209 | * 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] | 210 | * busybox functions by Glenn McGrath |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 211 | * |
| 212 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 213 | * |
| 214 | * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface |
| 215 | * Copyright (C) 1992-1993 Jean-loup Gailly |
| 216 | * The unzip code was written and put in the public domain by Mark Adler. |
| 217 | * Portions of the lzw code are derived from the public domain 'compress' |
| 218 | * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies, |
| 219 | * Ken Turkowski, Dave Mack and Peter Jannesen. |
| 220 | * |
| 221 | * See the license_msg below and the file COPYING for the software license. |
| 222 | * See the file algorithm.doc for the compression algorithms and file formats. |
| 223 | */ |
| 224 | |
| 225 | #if ENABLE_GUNZIP |
| 226 | |
| 227 | static |
| 228 | char* make_new_name_gunzip(char *filename) |
| 229 | { |
| 230 | char *extension = strrchr(filename, '.'); |
| 231 | |
| 232 | if (!extension) |
| 233 | return NULL; |
| 234 | |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 235 | extension++; |
| 236 | if (strcmp(extension, "tgz" + 1) == 0 |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 237 | #if ENABLE_FEATURE_SEAMLESS_Z |
| 238 | || (extension[0] == 'Z' && extension[1] == '\0') |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 239 | #endif |
| 240 | ) { |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 241 | extension[-1] = '\0'; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 242 | } else if (strcmp(extension, "tgz") == 0) { |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 243 | filename = xstrdup(filename); |
| 244 | extension = strrchr(filename, '.'); |
| 245 | extension[2] = 'a'; |
| 246 | extension[3] = 'r'; |
| 247 | } else { |
| 248 | return NULL; |
| 249 | } |
| 250 | return filename; |
| 251 | } |
| 252 | |
| 253 | static |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 254 | IF_DESKTOP(long long) int unpack_gunzip(unpack_info_t *info) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 255 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 256 | IF_DESKTOP(long long) int status = -1; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 257 | |
| 258 | /* do the decompression, and cleanup */ |
| 259 | if (xread_char(STDIN_FILENO) == 0x1f) { |
| 260 | unsigned char magic2; |
| 261 | |
| 262 | magic2 = xread_char(STDIN_FILENO); |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 263 | if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) { |
| 264 | status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 265 | } else if (magic2 == 0x8b) { |
Denis Vlasenko | 1a9e9bd | 2008-11-01 12:54:56 +0000 | [diff] [blame] | 266 | status = unpack_gz_stream_with_info(STDIN_FILENO, STDOUT_FILENO, info); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 267 | } else { |
| 268 | goto bad_magic; |
| 269 | } |
| 270 | if (status < 0) { |
| 271 | bb_error_msg("error inflating"); |
| 272 | } |
| 273 | } else { |
| 274 | bad_magic: |
| 275 | bb_error_msg("invalid magic"); |
| 276 | /* status is still == -1 */ |
| 277 | } |
| 278 | return status; |
| 279 | } |
| 280 | |
Denis Vlasenko | 5297809 | 2008-03-30 13:11:47 +0000 | [diff] [blame] | 281 | /* |
| 282 | * Linux kernel build uses gzip -d -n. We accept and ignore it. |
| 283 | * Man page says: |
| 284 | * -n --no-name |
| 285 | * gzip: do not save the original file name and time stamp. |
| 286 | * (The original name is always saved if the name had to be truncated.) |
| 287 | * gunzip: do not restore the original file name/time even if present |
| 288 | * (remove only the gzip suffix from the compressed file name). |
| 289 | * This option is the default when decompressing. |
| 290 | * -N --name |
| 291 | * gzip: always save the original file name and time stamp (this is the default) |
| 292 | * gunzip: restore the original file name and time stamp if present. |
| 293 | */ |
| 294 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 295 | int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 296 | int gunzip_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 297 | { |
Denis Vlasenko | 5297809 | 2008-03-30 13:11:47 +0000 | [diff] [blame] | 298 | getopt32(argv, "cfvdtn"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 299 | argv += optind; |
| 300 | /* if called as zcat */ |
| 301 | if (applet_name[1] == 'c') |
| 302 | option_mask32 |= OPT_STDOUT; |
| 303 | |
Denis Vlasenko | 7560578 | 2007-03-14 00:07:51 +0000 | [diff] [blame] | 304 | return bbunpack(argv, make_new_name_gunzip, unpack_gunzip); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | #endif |
| 308 | |
| 309 | |
| 310 | /* |
| 311 | * Small lzma deflate implementation. |
| 312 | * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> |
| 313 | * |
| 314 | * Based on bunzip.c from busybox |
| 315 | * |
| 316 | * Licensed under GPL v2, see file LICENSE in this tarball for details. |
| 317 | */ |
| 318 | |
| 319 | #if ENABLE_UNLZMA |
| 320 | |
| 321 | static |
| 322 | char* make_new_name_unlzma(char *filename) |
| 323 | { |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 324 | return make_new_name_generic(filename, "lzma"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 328 | IF_DESKTOP(long long) int unpack_unlzma(unpack_info_t *info UNUSED_PARAM) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 329 | { |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 330 | return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 333 | int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 334 | int unlzma_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 335 | { |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 336 | getopt32(argv, "cf"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 337 | argv += optind; |
| 338 | /* lzmacat? */ |
| 339 | if (applet_name[4] == 'c') |
| 340 | option_mask32 |= OPT_STDOUT; |
| 341 | |
Denis Vlasenko | 7560578 | 2007-03-14 00:07:51 +0000 | [diff] [blame] | 342 | return bbunpack(argv, make_new_name_unlzma, unpack_unlzma); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | #endif |
| 346 | |
| 347 | |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 348 | /* |
| 349 | * Uncompress applet for busybox (c) 2002 Glenn McGrath |
| 350 | * |
| 351 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 352 | */ |
| 353 | |
| 354 | #if ENABLE_UNCOMPRESS |
| 355 | |
| 356 | static |
| 357 | char* make_new_name_uncompress(char *filename) |
| 358 | { |
Denis Vlasenko | bebbd8c | 2007-03-09 20:49:55 +0000 | [diff] [blame] | 359 | return make_new_name_generic(filename, "Z"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 363 | IF_DESKTOP(long long) int unpack_uncompress(unpack_info_t *info UNUSED_PARAM) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 364 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 365 | IF_DESKTOP(long long) int status = -1; |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 366 | |
| 367 | if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) { |
| 368 | bb_error_msg("invalid magic"); |
| 369 | } else { |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 370 | status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 371 | } |
| 372 | return status; |
| 373 | } |
| 374 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 375 | int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 376 | int uncompress_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 377 | { |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 378 | getopt32(argv, "cf"); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 379 | argv += optind; |
| 380 | |
Denis Vlasenko | 7560578 | 2007-03-14 00:07:51 +0000 | [diff] [blame] | 381 | return bbunpack(argv, make_new_name_uncompress, unpack_uncompress); |
Denis Vlasenko | ab9eef2 | 2007-03-07 22:02:23 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | #endif |