blob: f2d796e679496a8299248c2b85ffed524dea8ff8 [file] [log] [blame]
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Common code for gunzip-like applets
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00006 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00007#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +02008#include "bb_archive.h"
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00009
Denys Vlasenko66620fa2013-11-14 09:53:52 +010010/* lzop_main() uses bbunpack(), need this: */
11//kbuild:lib-$(CONFIG_LZOP) += bbunzip.o
12
Mike Frysinger93b51812013-03-03 00:48:53 -050013/* Note: must be kept in sync with archival/lzop.c */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000014enum {
Denys Vlasenkoccb88a62010-05-27 02:22:54 +020015 OPT_STDOUT = 1 << 0,
16 OPT_FORCE = 1 << 1,
17 /* only some decompressors: */
18 OPT_VERBOSE = 1 << 2,
Mike Frysinger920c1ba2013-02-28 17:21:50 -050019 OPT_QUIET = 1 << 3,
20 OPT_DECOMPRESS = 1 << 4,
21 OPT_TEST = 1 << 5,
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010022 SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000023};
24
25static
26int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
27{
Denis Vlasenko50f7f442007-04-11 23:20:53 +000028 int fd = open3_or_warn(filename, flags, mode);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000029 if (fd < 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000030 return 1;
31 }
Denis Vlasenko50f7f442007-04-11 23:20:53 +000032 xmove_fd(fd, to_fd);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000033 return 0;
34}
35
Denys Vlasenko39a04f72010-05-31 14:18:57 +020036char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
37{
38 return xasprintf("%s.%s", filename, expected_ext);
39}
40
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000041int FAST_FUNC bbunpack(char **argv,
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010042 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
Denys Vlasenko39a04f72010-05-31 14:18:57 +020043 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
44 const char *expected_ext
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000045)
46{
47 struct stat stat_buf;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010048 IF_DESKTOP(long long) int status = 0;
Denis Vlasenko6c939e02007-03-07 23:22:47 +000049 char *filename, *new_name;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000050 smallint exitcode = 0;
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010051 transformer_aux_data_t aux;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000052
53 do {
Denis Vlasenko6c939e02007-03-07 23:22:47 +000054 /* NB: new_name is *maybe* malloc'ed! */
55 new_name = NULL;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000056 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
Denis Vlasenko6c939e02007-03-07 23:22:47 +000057
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000058 if (filename && LONE_DASH(filename))
59 filename = NULL;
60
61 /* Open src */
62 if (filename) {
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010063 if (!(option_mask32 & SEAMLESS_MAGIC)) {
64 if (stat(filename, &stat_buf) != 0) {
65 err_name:
66 bb_simple_perror_msg(filename);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000067 err:
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010068 exitcode = 1;
69 goto free_name;
70 }
71 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
72 goto err;
73 } else {
Denys Vlasenko41655432013-02-28 18:37:04 +010074 /* "clever zcat" with FILE */
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010075 int fd = open_zipped(filename);
76 if (fd < 0)
77 goto err_name;
78 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000079 }
Denys Vlasenko41655432013-02-28 18:37:04 +010080 } else
81 if (option_mask32 & SEAMLESS_MAGIC) {
82 /* "clever zcat" on stdin */
83 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
84 goto err;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000085 }
86
87 /* Special cases: test, stdout */
88 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
89 if (option_mask32 & OPT_TEST)
90 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
Denys Vlasenkof2d84782013-02-28 18:04:22 +010091 xfunc_die();
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000092 filename = NULL;
93 }
94
Denis Vlasenko6c939e02007-03-07 23:22:47 +000095 /* Open dst if we are going to unpack to file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000096 if (filename) {
Denys Vlasenko39a04f72010-05-31 14:18:57 +020097 new_name = make_new_name(filename, expected_ext);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000098 if (!new_name) {
99 bb_error_msg("%s: unknown suffix - ignored", filename);
100 goto err;
101 }
Denis Vlasenkoa04cc472008-06-25 20:54:45 +0000102
103 /* -f: overwrite existing output files */
104 if (option_mask32 & OPT_FORCE) {
105 unlink(new_name);
106 }
107
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000108 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000109 /* GNU gunzip does not bail out, but goes to next file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000110 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
111 stat_buf.st_mode))
112 goto err;
113 }
114
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000115 /* Check that the input is sane */
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100116 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000117 bb_error_msg_and_die("compressed data not read from terminal, "
118 "use -f to force it");
119 }
120
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100121 if (!(option_mask32 & SEAMLESS_MAGIC)) {
122 init_transformer_aux_data(&aux);
123 aux.check_signature = 1;
124 status = unpacker(&aux);
125 if (status < 0)
126 exitcode = 1;
127 } else {
Denys Vlasenko41655432013-02-28 18:37:04 +0100128 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100129 /* Disk full, tty closed, etc. No point in continuing */
130 xfunc_die();
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100131 }
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100132
133 if (!(option_mask32 & OPT_STDOUT))
134 xclose(STDOUT_FILENO); /* with error check! */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000135
136 if (filename) {
137 char *del = new_name;
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200138
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000139 if (status >= 0) {
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200140 unsigned new_name_len;
141
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000142 /* TODO: restore other things? */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100143 if (aux.mtime != 0) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100144 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100145
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100146 times[1].tv_sec = times[0].tv_sec = aux.mtime;
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100147 times[1].tv_usec = times[0].tv_usec = 0;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200148 /* Note: we closed it first.
Denys Vlasenko389cca42009-11-15 02:28:56 +0100149 * On some systems calling utimes
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200150 * then closing resets the mtime
151 * back to current time. */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100152 utimes(new_name, times); /* ignoring errors */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000153 }
154
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200155 if (ENABLE_DESKTOP)
156 new_name_len = strlen(new_name);
157 /* Restore source filename (unless tgz -> tar case) */
158 if (new_name == filename) {
159 new_name_len = strlen(filename);
160 filename[new_name_len] = '.';
161 }
162 /* Extreme bloat for gunzip compat */
163 /* Some users do want this info... */
164 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
165 unsigned percent = status
166 ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
167 : 0;
168 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
169 filename,
170 100u - percent,
171 new_name_len, new_name
172 );
173 }
174 /* Delete _source_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000175 del = filename;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000176 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000177 xunlink(del);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000178 free_name:
179 if (new_name != filename)
180 free(new_name);
181 }
182 } while (*argv && *++argv);
183
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100184 if (option_mask32 & OPT_STDOUT)
185 xclose(STDOUT_FILENO); /* with error check! */
186
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000187 return exitcode;
188}
189
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200190#if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000191static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200192char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000193{
194 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000195 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000196 /* Mimic GNU gunzip - "real" bunzip2 tries to */
197 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000198 return NULL;
199 }
200 *extension = '\0';
201 return filename;
202}
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000203#endif
204
205
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000206/*
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200207 * Uncompress applet for busybox (c) 2002 Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000208 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200209 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000210 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200211//usage:#define uncompress_trivial_usage
212//usage: "[-cf] [FILE]..."
213//usage:#define uncompress_full_usage "\n\n"
214//usage: "Decompress .Z file[s]\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200215//usage: "\n -c Write to stdout"
216//usage: "\n -f Overwrite"
217
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100218//kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200219#if ENABLE_UNCOMPRESS
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000220static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100221IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000222{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100223 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200224}
225int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
226int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000227{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200228 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000229 argv += optind;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000230
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200231 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000232}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000233#endif
234
235
236/*
237 * Gzip implementation for busybox
238 *
239 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
240 *
241 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
242 * based on gzip sources
243 *
244 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
245 * well as stdin/stdout, and to generally behave itself wrt command line
246 * handling.
247 *
248 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000249 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000250 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200251 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000252 *
253 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
254 * Copyright (C) 1992-1993 Jean-loup Gailly
255 * The unzip code was written and put in the public domain by Mark Adler.
256 * Portions of the lzw code are derived from the public domain 'compress'
257 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
258 * Ken Turkowski, Dave Mack and Peter Jannesen.
259 *
260 * See the license_msg below and the file COPYING for the software license.
261 * See the file algorithm.doc for the compression algorithms and file formats.
262 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200263//usage:#define gunzip_trivial_usage
264//usage: "[-cft] [FILE]..."
265//usage:#define gunzip_full_usage "\n\n"
266//usage: "Decompress FILEs (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200267//usage: "\n -c Write to stdout"
268//usage: "\n -f Force"
269//usage: "\n -t Test file integrity"
270//usage:
271//usage:#define gunzip_example_usage
272//usage: "$ ls -la /tmp/BusyBox*\n"
273//usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
274//usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
275//usage: "$ ls -la /tmp/BusyBox*\n"
276//usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
277//usage:
278//usage:#define zcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100279//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200280//usage:#define zcat_full_usage "\n\n"
281//usage: "Decompress to stdout"
282
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100283//kbuild:lib-$(CONFIG_GZIP) += bbunzip.o
284//kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000285#if ENABLE_GUNZIP
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000286static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200287char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000288{
289 char *extension = strrchr(filename, '.');
290
291 if (!extension)
292 return NULL;
293
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000294 extension++;
295 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000296#if ENABLE_FEATURE_SEAMLESS_Z
297 || (extension[0] == 'Z' && extension[1] == '\0')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000298#endif
299 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000300 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000301 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000302 filename = xstrdup(filename);
303 extension = strrchr(filename, '.');
304 extension[2] = 'a';
305 extension[3] = 'r';
306 } else {
307 return NULL;
308 }
309 return filename;
310}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000311static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100312IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000313{
Denys Vlasenko02c3c382012-03-06 16:32:06 +0100314 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000315}
Denis Vlasenko52978092008-03-30 13:11:47 +0000316/*
317 * Linux kernel build uses gzip -d -n. We accept and ignore it.
318 * Man page says:
319 * -n --no-name
320 * gzip: do not save the original file name and time stamp.
321 * (The original name is always saved if the name had to be truncated.)
322 * gunzip: do not restore the original file name/time even if present
323 * (remove only the gzip suffix from the compressed file name).
324 * This option is the default when decompressing.
325 * -N --name
326 * gzip: always save the original file name and time stamp (this is the default)
327 * gunzip: restore the original file name and time stamp if present.
328 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000329int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000330int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000331{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500332 getopt32(argv, "cfvqdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000333 argv += optind;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100334
335 /* If called as zcat...
336 * Normally, "zcat" is just "gunzip -c".
337 * But if seamless magic is enabled, then we are much more clever.
338 */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000339 if (applet_name[1] == 'c')
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100340 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000341
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200342 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000343}
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200344#endif
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000345
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200346
347/*
348 * Modified for busybox by Glenn McGrath
349 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
350 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200351 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200352 */
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200353//usage:#define bunzip2_trivial_usage
Denys Vlasenko3b2acb72010-10-09 21:10:32 +0200354//usage: "[-cf] [FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200355//usage:#define bunzip2_full_usage "\n\n"
356//usage: "Decompress FILEs (or stdin)\n"
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200357//usage: "\n -c Write to stdout"
358//usage: "\n -f Force"
359//usage:#define bzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100360//usage: "[FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200361//usage:#define bzcat_full_usage "\n\n"
362//usage: "Decompress to stdout"
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100363
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100364//applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
365//applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100366//kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
367//kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200368#if ENABLE_BUNZIP2
369static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100370IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200371{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100372 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200373}
374int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
375int bunzip2_main(int argc UNUSED_PARAM, char **argv)
376{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500377 getopt32(argv, "cfvqdt");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200378 argv += optind;
379 if (applet_name[2] == 'c') /* bzcat */
380 option_mask32 |= OPT_STDOUT;
381
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200382 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200383}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000384#endif
385
386
387/*
388 * Small lzma deflate implementation.
389 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
390 *
391 * Based on bunzip.c from busybox
392 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200393 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000394 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200395//usage:#define unlzma_trivial_usage
396//usage: "[-cf] [FILE]..."
397//usage:#define unlzma_full_usage "\n\n"
398//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200399//usage: "\n -c Write to stdout"
400//usage: "\n -f Force"
401//usage:
402//usage:#define lzma_trivial_usage
403//usage: "-d [-cf] [FILE]..."
404//usage:#define lzma_full_usage "\n\n"
405//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200406//usage: "\n -d Decompress"
407//usage: "\n -c Write to stdout"
408//usage: "\n -f Force"
409//usage:
410//usage:#define lzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100411//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200412//usage:#define lzcat_full_usage "\n\n"
413//usage: "Decompress to stdout"
414//usage:
415//usage:#define unxz_trivial_usage
416//usage: "[-cf] [FILE]..."
417//usage:#define unxz_full_usage "\n\n"
418//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200419//usage: "\n -c Write to stdout"
420//usage: "\n -f Force"
421//usage:
422//usage:#define xz_trivial_usage
423//usage: "-d [-cf] [FILE]..."
424//usage:#define xz_full_usage "\n\n"
425//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200426//usage: "\n -d Decompress"
427//usage: "\n -c Write to stdout"
428//usage: "\n -f Force"
429//usage:
430//usage:#define xzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100431//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200432//usage:#define xzcat_full_usage "\n\n"
433//usage: "Decompress to stdout"
434
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100435//kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000436#if ENABLE_UNLZMA
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000437static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100438IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000439{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100440 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000441}
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000442int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000443int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000444{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500445 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenkoe04c8672010-05-30 03:33:50 +0200446# if ENABLE_LZMA
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200447 /* lzma without -d or -t? */
448 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
449 bb_show_usage();
450# endif
451 /* lzcat? */
452 if (applet_name[2] == 'c')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000453 option_mask32 |= OPT_STDOUT;
454
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200455 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200456 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000457}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000458#endif
459
460
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100461//kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
Denys Vlasenko602ce692010-05-30 03:35:18 +0200462#if ENABLE_UNXZ
Denys Vlasenko602ce692010-05-30 03:35:18 +0200463static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100464IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200465{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100466 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenko602ce692010-05-30 03:35:18 +0200467}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200468int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
469int unxz_main(int argc UNUSED_PARAM, char **argv)
470{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500471 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200472# if ENABLE_XZ
473 /* xz without -d or -t? */
474 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
475 bb_show_usage();
476# endif
477 /* xzcat? */
478 if (applet_name[2] == 'c')
479 option_mask32 |= OPT_STDOUT;
480
481 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200482 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200483}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200484#endif