blob: 65bb8b7f435ed5a4b9e43858f7b67e6dbb4636d0 [file] [log] [blame]
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +00003 * Common code for gunzip-like applets
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00004 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 */
7
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00008#include "libbb.h"
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00009#include "unarchive.h"
10
11enum {
Denis Vlasenko75605782007-03-14 00:07:51 +000012 OPT_STDOUT = 0x1,
13 OPT_FORCE = 0x2,
Denis Vlasenko6fa3ab32007-10-21 18:59:58 +000014/* gunzip and bunzip2 only: */
Denis Vlasenko75605782007-03-14 00:07:51 +000015 OPT_VERBOSE = 0x4,
16 OPT_DECOMPRESS = 0x8,
17 OPT_TEST = 0x10,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000018};
19
20static
21int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
22{
Denis Vlasenko50f7f442007-04-11 23:20:53 +000023 int fd = open3_or_warn(filename, flags, mode);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000024 if (fd < 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000025 return 1;
26 }
Denis Vlasenko50f7f442007-04-11 23:20:53 +000027 xmove_fd(fd, to_fd);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000028 return 0;
29}
30
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000031int FAST_FUNC bbunpack(char **argv,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000032 char* (*make_new_name)(char *filename),
33 USE_DESKTOP(long long) int (*unpacker)(void)
34)
35{
36 struct stat stat_buf;
37 USE_DESKTOP(long long) int status;
Denis Vlasenko6c939e02007-03-07 23:22:47 +000038 char *filename, *new_name;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000039 smallint exitcode = 0;
40
41 do {
Denis Vlasenko6c939e02007-03-07 23:22:47 +000042 /* NB: new_name is *maybe* malloc'ed! */
43 new_name = NULL;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000044 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
Denis Vlasenko6c939e02007-03-07 23:22:47 +000045
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000046 if (filename && LONE_DASH(filename))
47 filename = NULL;
48
49 /* Open src */
50 if (filename) {
51 if (stat(filename, &stat_buf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000052 bb_simple_perror_msg(filename);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000053 err:
54 exitcode = 1;
55 goto free_name;
56 }
57 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
58 goto err;
59 }
60
61 /* Special cases: test, stdout */
62 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
63 if (option_mask32 & OPT_TEST)
64 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
65 goto err;
66 filename = NULL;
67 }
68
Denis Vlasenko6c939e02007-03-07 23:22:47 +000069 /* Open dst if we are going to unpack to file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000070 if (filename) {
71 new_name = make_new_name(filename);
72 if (!new_name) {
73 bb_error_msg("%s: unknown suffix - ignored", filename);
74 goto err;
75 }
Denis Vlasenkoa04cc472008-06-25 20:54:45 +000076
77 /* -f: overwrite existing output files */
78 if (option_mask32 & OPT_FORCE) {
79 unlink(new_name);
80 }
81
Denis Vlasenko6c939e02007-03-07 23:22:47 +000082 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000083 /* GNU gunzip does not bail out, but goes to next file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000084 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
85 stat_buf.st_mode))
86 goto err;
87 }
88
Denis Vlasenko6c939e02007-03-07 23:22:47 +000089 /* Check that the input is sane */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000090 if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
91 bb_error_msg_and_die("compressed data not read from terminal, "
92 "use -f to force it");
93 }
94
95 status = unpacker();
96 if (status < 0)
97 exitcode = 1;
98
99 if (filename) {
100 char *del = new_name;
101 if (status >= 0) {
102 /* TODO: restore user/group/times here? */
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000103 /* Delete _compressed_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000104 del = filename;
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000105 /* restore extension (unless tgz -> tar case) */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000106 if (new_name == filename)
107 filename[strlen(filename)] = '.';
108 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000109 xunlink(del);
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000110
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000111#if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
112 /* Extreme bloat for gunzip compat */
113 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
114 fprintf(stderr, "%s: %u%% - replaced with %s\n",
115 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
116 }
117#endif
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000118
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000119 free_name:
120 if (new_name != filename)
121 free(new_name);
122 }
123 } while (*argv && *++argv);
124
125 return exitcode;
126}
127
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000128#if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS
129
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000130static
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000131char* make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000132{
133 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000134 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000135 /* Mimic GNU gunzip - "real" bunzip2 tries to */
136 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000137 return NULL;
138 }
139 *extension = '\0';
140 return filename;
141}
142
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000143#endif
144
145
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000146/*
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000147 * Modified for busybox by Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000148 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
149 *
150 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
151 */
152
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000153#if ENABLE_BUNZIP2
154
155static
156char* make_new_name_bunzip2(char *filename)
157{
158 return make_new_name_generic(filename, "bz2");
159}
160
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000161static
162USE_DESKTOP(long long) int unpack_bunzip2(void)
163{
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000164 return unpack_bz2_stream(STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000165}
166
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000167int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000168int bunzip2_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000169{
Denis Vlasenko6fa3ab32007-10-21 18:59:58 +0000170 getopt32(argv, "cfvdt");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000171 argv += optind;
172 if (applet_name[2] == 'c')
173 option_mask32 |= OPT_STDOUT;
174
Denis Vlasenko75605782007-03-14 00:07:51 +0000175 return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000176}
177
178#endif
179
180
181/*
182 * Gzip implementation for busybox
183 *
184 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
185 *
186 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
187 * based on gzip sources
188 *
189 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
190 * well as stdin/stdout, and to generally behave itself wrt command line
191 * handling.
192 *
193 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000194 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000195 *
196 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
197 *
198 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
199 * Copyright (C) 1992-1993 Jean-loup Gailly
200 * The unzip code was written and put in the public domain by Mark Adler.
201 * Portions of the lzw code are derived from the public domain 'compress'
202 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
203 * Ken Turkowski, Dave Mack and Peter Jannesen.
204 *
205 * See the license_msg below and the file COPYING for the software license.
206 * See the file algorithm.doc for the compression algorithms and file formats.
207 */
208
209#if ENABLE_GUNZIP
210
211static
212char* make_new_name_gunzip(char *filename)
213{
214 char *extension = strrchr(filename, '.');
215
216 if (!extension)
217 return NULL;
218
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000219 extension++;
220 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkodbe6e662007-08-14 16:43:01 +0000221#if ENABLE_FEATURE_GUNZIP_UNCOMPRESS
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000222 || strcmp(extension, "Z") == 0
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000223#endif
224 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000225 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000226 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000227 filename = xstrdup(filename);
228 extension = strrchr(filename, '.');
229 extension[2] = 'a';
230 extension[3] = 'r';
231 } else {
232 return NULL;
233 }
234 return filename;
235}
236
237static
238USE_DESKTOP(long long) int unpack_gunzip(void)
239{
240 USE_DESKTOP(long long) int status = -1;
241
242 /* do the decompression, and cleanup */
243 if (xread_char(STDIN_FILENO) == 0x1f) {
244 unsigned char magic2;
245
246 magic2 = xread_char(STDIN_FILENO);
247 if (ENABLE_FEATURE_GUNZIP_UNCOMPRESS && magic2 == 0x9d) {
248 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
249 } else if (magic2 == 0x8b) {
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000250 status = unpack_gz_stream(STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000251 } else {
252 goto bad_magic;
253 }
254 if (status < 0) {
255 bb_error_msg("error inflating");
256 }
257 } else {
258 bad_magic:
259 bb_error_msg("invalid magic");
260 /* status is still == -1 */
261 }
262 return status;
263}
264
Denis Vlasenko52978092008-03-30 13:11:47 +0000265/*
266 * Linux kernel build uses gzip -d -n. We accept and ignore it.
267 * Man page says:
268 * -n --no-name
269 * gzip: do not save the original file name and time stamp.
270 * (The original name is always saved if the name had to be truncated.)
271 * gunzip: do not restore the original file name/time even if present
272 * (remove only the gzip suffix from the compressed file name).
273 * This option is the default when decompressing.
274 * -N --name
275 * gzip: always save the original file name and time stamp (this is the default)
276 * gunzip: restore the original file name and time stamp if present.
277 */
278
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000279int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000280int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000281{
Denis Vlasenko52978092008-03-30 13:11:47 +0000282 getopt32(argv, "cfvdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000283 argv += optind;
284 /* if called as zcat */
285 if (applet_name[1] == 'c')
286 option_mask32 |= OPT_STDOUT;
287
Denis Vlasenko75605782007-03-14 00:07:51 +0000288 return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000289}
290
291#endif
292
293
294/*
295 * Small lzma deflate implementation.
296 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
297 *
298 * Based on bunzip.c from busybox
299 *
300 * Licensed under GPL v2, see file LICENSE in this tarball for details.
301 */
302
303#if ENABLE_UNLZMA
304
305static
306char* make_new_name_unlzma(char *filename)
307{
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000308 return make_new_name_generic(filename, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000309}
310
311static
312USE_DESKTOP(long long) int unpack_unlzma(void)
313{
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000314 return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000315}
316
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000317int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000318int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000319{
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000320 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000321 argv += optind;
322 /* lzmacat? */
323 if (applet_name[4] == 'c')
324 option_mask32 |= OPT_STDOUT;
325
Denis Vlasenko75605782007-03-14 00:07:51 +0000326 return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000327}
328
329#endif
330
331
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000332/*
333 * Uncompress applet for busybox (c) 2002 Glenn McGrath
334 *
335 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
336 */
337
338#if ENABLE_UNCOMPRESS
339
340static
341char* make_new_name_uncompress(char *filename)
342{
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000343 return make_new_name_generic(filename, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000344}
345
346static
347USE_DESKTOP(long long) int unpack_uncompress(void)
348{
349 USE_DESKTOP(long long) int status = -1;
350
351 if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
352 bb_error_msg("invalid magic");
353 } else {
354 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
355 }
356 return status;
357}
358
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000359int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000360int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000361{
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000362 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000363 argv += optind;
364
Denis Vlasenko75605782007-03-14 00:07:51 +0000365 return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000366}
367
368#endif