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