archival/*: move "kbuild:" snippets into .c files
[platform/upstream/busybox.git] / archival / bbunzip.c
index 08db275..f2d796e 100644 (file)
@@ -1,19 +1,25 @@
 /* vi: set sw=4 ts=4: */
 /*
- *  Common code for gunzip-like applets
+ * Common code for gunzip-like applets
  *
- *  Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 #include "libbb.h"
-#include "unarchive.h"
+#include "bb_archive.h"
 
+/* lzop_main() uses bbunpack(), need this: */
+//kbuild:lib-$(CONFIG_LZOP) += bbunzip.o
+
+/* Note: must be kept in sync with archival/lzop.c */
 enum {
        OPT_STDOUT     = 1 << 0,
        OPT_FORCE      = 1 << 1,
        /* only some decompressors: */
        OPT_VERBOSE    = 1 << 2,
-       OPT_DECOMPRESS = 1 << 3,
-       OPT_TEST       = 1 << 4,
+       OPT_QUIET      = 1 << 3,
+       OPT_DECOMPRESS = 1 << 4,
+       OPT_TEST       = 1 << 5,
+       SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
 };
 
 static
@@ -33,16 +39,16 @@ char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
 }
 
 int FAST_FUNC bbunpack(char **argv,
-       IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(unpack_info_t *info),
+       IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
        char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
        const char *expected_ext
 )
 {
        struct stat stat_buf;
-       IF_DESKTOP(long long) int status;
+       IF_DESKTOP(long long) int status = 0;
        char *filename, *new_name;
        smallint exitcode = 0;
-       unpack_info_t info;
+       transformer_aux_data_t aux;
 
        do {
                /* NB: new_name is *maybe* malloc'ed! */
@@ -54,13 +60,27 @@ int FAST_FUNC bbunpack(char **argv,
 
                /* Open src */
                if (filename) {
-                       if (stat(filename, &stat_buf) != 0) {
-                               bb_simple_perror_msg(filename);
+                       if (!(option_mask32 & SEAMLESS_MAGIC)) {
+                               if (stat(filename, &stat_buf) != 0) {
+ err_name:
+                                       bb_simple_perror_msg(filename);
  err:
-                               exitcode = 1;
-                               goto free_name;
+                                       exitcode = 1;
+                                       goto free_name;
+                               }
+                               if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
+                                       goto err;
+                       } else {
+                               /* "clever zcat" with FILE */
+                               int fd = open_zipped(filename);
+                               if (fd < 0)
+                                       goto err_name;
+                               xmove_fd(fd, STDIN_FILENO);
                        }
-                       if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
+               } else
+               if (option_mask32 & SEAMLESS_MAGIC) {
+                       /* "clever zcat" on stdin */
+                       if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
                                goto err;
                }
 
@@ -68,7 +88,7 @@ int FAST_FUNC bbunpack(char **argv,
                if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
                        if (option_mask32 & OPT_TEST)
                                if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
-                                       goto err;
+                                       xfunc_die();
                        filename = NULL;
                }
 
@@ -93,26 +113,37 @@ int FAST_FUNC bbunpack(char **argv,
                }
 
                /* Check that the input is sane */
-               if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
+               if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
                        bb_error_msg_and_die("compressed data not read from terminal, "
                                        "use -f to force it");
                }
 
-               /* memset(&info, 0, sizeof(info)); */
-               info.mtime = 0; /* so far it has one member only */
-               status = unpacker(&info);
-               if (status < 0)
-                       exitcode = 1;
-               xclose(STDOUT_FILENO); /* with error check! */
+               if (!(option_mask32 & SEAMLESS_MAGIC)) {
+                       init_transformer_aux_data(&aux);
+                       aux.check_signature = 1;
+                       status = unpacker(&aux);
+                       if (status < 0)
+                               exitcode = 1;
+               } else {
+                       if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
+                               /* Disk full, tty closed, etc. No point in continuing */
+                               xfunc_die();
+               }
+
+               if (!(option_mask32 & OPT_STDOUT))
+                       xclose(STDOUT_FILENO); /* with error check! */
 
                if (filename) {
                        char *del = new_name;
+
                        if (status >= 0) {
+                               unsigned new_name_len;
+
                                /* TODO: restore other things? */
-                               if (info.mtime) {
+                               if (aux.mtime != 0) {
                                        struct timeval times[2];
 
-                                       times[1].tv_sec = times[0].tv_sec = info.mtime;
+                                       times[1].tv_sec = times[0].tv_sec = aux.mtime;
                                        times[1].tv_usec = times[0].tv_usec = 0;
                                        /* Note: we closed it first.
                                         * On some systems calling utimes
@@ -121,28 +152,38 @@ int FAST_FUNC bbunpack(char **argv,
                                        utimes(new_name, times); /* ignoring errors */
                                }
 
-                               /* Delete _compressed_ file */
+                               if (ENABLE_DESKTOP)
+                                       new_name_len = strlen(new_name);
+                               /* Restore source filename (unless tgz -> tar case) */
+                               if (new_name == filename) {
+                                       new_name_len = strlen(filename);
+                                       filename[new_name_len] = '.';
+                               }
+                               /* Extreme bloat for gunzip compat */
+                               /* Some users do want this info... */
+                               if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
+                                       unsigned percent = status
+                                               ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
+                                               : 0;
+                                       fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
+                                               filename,
+                                               100u - percent,
+                                               new_name_len, new_name
+                                       );
+                               }
+                               /* Delete _source_ file */
                                del = filename;
-                               /* restore extension (unless tgz -> tar case) */
-                               if (new_name == filename)
-                                       filename[strlen(filename)] = '.';
                        }
                        xunlink(del);
-
-#if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
-                       /* Extreme bloat for gunzip compat */
-                       if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
-                               fprintf(stderr, "%s: %u%% - replaced with %s\n",
-                                       filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
-                       }
-#endif
-
  free_name:
                        if (new_name != filename)
                                free(new_name);
                }
        } while (*argv && *++argv);
 
+       if (option_mask32 & OPT_STDOUT)
+               xclose(STDOUT_FILENO); /* with error check! */
+
        return exitcode;
 }
 
@@ -165,20 +206,21 @@ char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
 /*
  * Uncompress applet for busybox (c) 2002 Glenn McGrath
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
+//usage:#define uncompress_trivial_usage
+//usage:       "[-cf] [FILE]..."
+//usage:#define uncompress_full_usage "\n\n"
+//usage:       "Decompress .Z file[s]\n"
+//usage:     "\n       -c      Write to stdout"
+//usage:     "\n       -f      Overwrite"
+
+//kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
 #if ENABLE_UNCOMPRESS
 static
-IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
 {
-       IF_DESKTOP(long long) int status = -1;
-
-       if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
-               bb_error_msg("invalid magic");
-       } else {
-               status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
-       }
-       return status;
+       return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
 }
 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int uncompress_main(int argc UNUSED_PARAM, char **argv)
@@ -206,7 +248,7 @@ int uncompress_main(int argc UNUSED_PARAM, char **argv)
  * General cleanup to better adhere to the style guide and make use of standard
  * busybox functions by Glenn McGrath
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  *
  * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  * Copyright (C) 1992-1993 Jean-loup Gailly
@@ -218,6 +260,28 @@ int uncompress_main(int argc UNUSED_PARAM, char **argv)
  * See the license_msg below and the file COPYING for the software license.
  * See the file algorithm.doc for the compression algorithms and file formats.
  */
+//usage:#define gunzip_trivial_usage
+//usage:       "[-cft] [FILE]..."
+//usage:#define gunzip_full_usage "\n\n"
+//usage:       "Decompress FILEs (or stdin)\n"
+//usage:     "\n       -c      Write to stdout"
+//usage:     "\n       -f      Force"
+//usage:     "\n       -t      Test file integrity"
+//usage:
+//usage:#define gunzip_example_usage
+//usage:       "$ ls -la /tmp/BusyBox*\n"
+//usage:       "-rw-rw-r--    1 andersen andersen   557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
+//usage:       "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
+//usage:       "$ ls -la /tmp/BusyBox*\n"
+//usage:       "-rw-rw-r--    1 andersen andersen  1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
+//usage:
+//usage:#define zcat_trivial_usage
+//usage:       "[FILE]..."
+//usage:#define zcat_full_usage "\n\n"
+//usage:       "Decompress to stdout"
+
+//kbuild:lib-$(CONFIG_GZIP) += bbunzip.o
+//kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
 #if ENABLE_GUNZIP
 static
 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
@@ -245,31 +309,9 @@ char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UN
        return filename;
 }
 static
-IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(unpack_info_t *info)
+IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
 {
-       IF_DESKTOP(long long) int status = -1;
-
-       /* do the decompression, and cleanup */
-       if (xread_char(STDIN_FILENO) == 0x1f) {
-               unsigned char magic2;
-
-               magic2 = xread_char(STDIN_FILENO);
-               if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
-                       status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
-               } else if (magic2 == 0x8b) {
-                       status = unpack_gz_stream_with_info(STDIN_FILENO, STDOUT_FILENO, info);
-               } else {
-                       goto bad_magic;
-               }
-               if (status < 0) {
-                       bb_error_msg("error inflating");
-               }
-       } else {
- bad_magic:
-               bb_error_msg("invalid magic");
-               /* status is still == -1 */
-       }
-       return status;
+       return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
 }
 /*
  * Linux kernel build uses gzip -d -n. We accept and ignore it.
@@ -287,11 +329,15 @@ IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(unpack_info_t *info)
 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int gunzip_main(int argc UNUSED_PARAM, char **argv)
 {
-       getopt32(argv, "cfvdtn");
+       getopt32(argv, "cfvqdtn");
        argv += optind;
-       /* if called as zcat */
+
+       /* If called as zcat...
+        * Normally, "zcat" is just "gunzip -c".
+        * But if seamless magic is enabled, then we are much more clever.
+        */
        if (applet_name[1] == 'c')
-               option_mask32 |= OPT_STDOUT;
+               option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
 
        return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
 }
@@ -302,18 +348,33 @@ int gunzip_main(int argc UNUSED_PARAM, char **argv)
  * Modified for busybox by Glenn McGrath
  * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
+//usage:#define bunzip2_trivial_usage
+//usage:       "[-cf] [FILE]..."
+//usage:#define bunzip2_full_usage "\n\n"
+//usage:       "Decompress FILEs (or stdin)\n"
+//usage:     "\n       -c      Write to stdout"
+//usage:     "\n       -f      Force"
+//usage:#define bzcat_trivial_usage
+//usage:       "[FILE]..."
+//usage:#define bzcat_full_usage "\n\n"
+//usage:       "Decompress to stdout"
+
+//applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
+//applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
+//kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
+//kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
 #if ENABLE_BUNZIP2
 static
-IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
 {
-       return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
+       return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
 }
 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
 {
-       getopt32(argv, "cfvdt");
+       getopt32(argv, "cfvqdt");
        argv += optind;
        if (applet_name[2] == 'c') /* bzcat */
                option_mask32 |= OPT_STDOUT;
@@ -329,18 +390,59 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
  *
  * Based on bunzip.c from busybox
  *
- * Licensed under GPL v2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
+//usage:#define unlzma_trivial_usage
+//usage:       "[-cf] [FILE]..."
+//usage:#define unlzma_full_usage "\n\n"
+//usage:       "Decompress FILE (or stdin)\n"
+//usage:     "\n       -c      Write to stdout"
+//usage:     "\n       -f      Force"
+//usage:
+//usage:#define lzma_trivial_usage
+//usage:       "-d [-cf] [FILE]..."
+//usage:#define lzma_full_usage "\n\n"
+//usage:       "Decompress FILE (or stdin)\n"
+//usage:     "\n       -d      Decompress"
+//usage:     "\n       -c      Write to stdout"
+//usage:     "\n       -f      Force"
+//usage:
+//usage:#define lzcat_trivial_usage
+//usage:       "[FILE]..."
+//usage:#define lzcat_full_usage "\n\n"
+//usage:       "Decompress to stdout"
+//usage:
+//usage:#define unxz_trivial_usage
+//usage:       "[-cf] [FILE]..."
+//usage:#define unxz_full_usage "\n\n"
+//usage:       "Decompress FILE (or stdin)\n"
+//usage:     "\n       -c      Write to stdout"
+//usage:     "\n       -f      Force"
+//usage:
+//usage:#define xz_trivial_usage
+//usage:       "-d [-cf] [FILE]..."
+//usage:#define xz_full_usage "\n\n"
+//usage:       "Decompress FILE (or stdin)\n"
+//usage:     "\n       -d      Decompress"
+//usage:     "\n       -c      Write to stdout"
+//usage:     "\n       -f      Force"
+//usage:
+//usage:#define xzcat_trivial_usage
+//usage:       "[FILE]..."
+//usage:#define xzcat_full_usage "\n\n"
+//usage:       "Decompress to stdout"
+
+//kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
 #if ENABLE_UNLZMA
 static
-IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
 {
-       return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
+       return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
 }
 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int unlzma_main(int argc UNUSED_PARAM, char **argv)
 {
-       IF_LZMA(int opts =) getopt32(argv, "cfvdt");
+       IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
 # if ENABLE_LZMA
        /* lzma without -d or -t? */
        if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
@@ -356,16 +458,17 @@ int unlzma_main(int argc UNUSED_PARAM, char **argv)
 #endif
 
 
+//kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
 #if ENABLE_UNXZ
 static
-IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(unpack_info_t *info UNUSED_PARAM)
+IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
 {
-       return unpack_xz_stream(STDIN_FILENO, STDOUT_FILENO);
+       return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
 }
 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int unxz_main(int argc UNUSED_PARAM, char **argv)
 {
-       int opts = getopt32(argv, "cfvdt");
+       IF_XZ(int opts =) getopt32(argv, "cfvqdt");
 # if ENABLE_XZ
        /* xz without -d or -t? */
        if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))