update seamless uncompression code

This change makes "tar tf hello_world.txz" work without
adding special-casing for ".txz" extension. It also removes
ever-growing magic checking code in rpm2cpio and get_header_tar -
we reuse one which lives in setup_unzip_on_fd.

function                                             old     new   delta
unpack_gz_stream                                       7     566    +559
check_signature16                                      -      70     +70
setup_unzip_on_fd                                     99     142     +43
handle_SIGCHLD                                         -      41     +41
unpack_bz2_stream                                    342     376     +34
unzip_main                                          2352    2385     +33
bbunpack                                             503     533     +30
open_transformer                                      74     102     +28
unpack_Z_stream                                     1278    1304     +26
unpack_gunzip                                        101     123     +22
init_transformer_aux_data                              -      18     +18
unpack_xz_stream                                    2388    2402     +14
open_zipped                                          131     141     +10
rpm_main                                            1358    1363      +5
get_header_tar_lzma                                   52      57      +5
get_header_tar_bz2                                    52      57      +5
unpack_lzma_stream                                  2698    2702      +4
hash_find                                            234     233      -1
get_header_tar                                      1759    1733     -26
get_header_tar_gz                                     92      57     -35
unpack_uncompress                                     51      12     -39
rpm2cpio_main                                        201     147     -54
unpack_unxz                                           67      12     -55
unpack_bz2_stream_prime                               55       -     -55
get_header_tar_Z                                      86       -     -86
unpack_gz_stream_with_info                           539       -    -539
------------------------------------------------------------------------------
(add/remove: 3/3 grow/shrink: 14/6 up/down: 947/-890)          Total: 57 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/archival/tar.c b/archival/tar.c
index 766b79b..af38ac5 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -690,31 +690,6 @@
 # define append_file_list_to_list(x) 0
 #endif
 
-#if ENABLE_FEATURE_SEAMLESS_Z
-static char FAST_FUNC get_header_tar_Z(archive_handle_t *archive_handle)
-{
-	/* Can't lseek over pipes */
-	archive_handle->seek = seek_by_read;
-
-	/* do the decompression, and cleanup */
-	if (xread_char(archive_handle->src_fd) != 0x1f
-	 || xread_char(archive_handle->src_fd) != 0x9d
-	) {
-		bb_error_msg_and_die("invalid magic");
-	}
-
-	open_transformer(archive_handle->src_fd, unpack_Z_stream, "uncompress");
-	archive_handle->offset = 0;
-	while (get_header_tar(archive_handle) == EXIT_SUCCESS)
-		continue;
-
-	/* Can only do one file at a time */
-	return EXIT_FAILURE;
-}
-#else
-# define get_header_tar_Z NULL
-#endif
-
 #ifdef CHECK_FOR_CHILD_EXITCODE
 /* Looks like it isn't needed - tar detects malformed (truncated)
  * archive if e.g. bunzip2 fails */
@@ -843,6 +818,8 @@
 	OPT_NUMERIC_OWNER   = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER  )) + 0, // numeric-owner
 	OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
 	OPT_OVERWRITE       = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE      )) + 0, // overwrite
+
+	OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_COMPRESS),
 };
 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
 static const char tar_longopts[] ALIGN1 =
@@ -901,7 +878,6 @@
 int tar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int tar_main(int argc UNUSED_PARAM, char **argv)
 {
-	char FAST_FUNC (*get_header_ptr)(archive_handle_t *) = get_header_tar;
 	archive_handle_t *tar_handle;
 	char *base_dir = NULL;
 	const char *tar_filename = "-";
@@ -1017,18 +993,6 @@
 		tar_handle->ah_flags |= ARCHIVE_O_TRUNC;
 	}
 
-	if (opt & OPT_GZIP)
-		get_header_ptr = get_header_tar_gz;
-
-	if (opt & OPT_BZIP2)
-		get_header_ptr = get_header_tar_bz2;
-
-	if (opt & OPT_LZMA)
-		get_header_ptr = get_header_tar_lzma;
-
-	if (opt & OPT_COMPRESS)
-		get_header_ptr = get_header_tar_Z;
-
 	if (opt & OPT_NOPRESERVE_TIME)
 		tar_handle->ah_flags &= ~ARCHIVE_RESTORE_DATE;
 
@@ -1081,7 +1045,7 @@
 		} else {
 			if (ENABLE_FEATURE_TAR_AUTODETECT
 			 && flags == O_RDONLY
-			 && get_header_ptr == get_header_tar
+			 && !(opt & OPT_ANY_COMPRESS)
 			) {
 				tar_handle->src_fd = open_zipped(tar_filename);
 				if (tar_handle->src_fd < 0)
@@ -1115,7 +1079,30 @@
 				tar_handle->reject, zipMode);
 	}
 
-	while (get_header_ptr(tar_handle) == EXIT_SUCCESS)
+	if (opt & OPT_ANY_COMPRESS) {
+		USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_aux_data_t *aux, int src_fd, int dst_fd);)
+		USE_FOR_NOMMU(const char *xformer_prog;)
+
+		if (opt & OPT_COMPRESS)
+			USE_FOR_MMU(xformer = unpack_Z_stream;)
+			USE_FOR_NOMMU(xformer_prog = "uncompress";)
+		if (opt & OPT_GZIP)
+			USE_FOR_MMU(xformer = unpack_gz_stream;)
+			USE_FOR_NOMMU(xformer_prog = "gunzip";)
+		if (opt & OPT_BZIP2)
+			USE_FOR_MMU(xformer = unpack_bz2_stream;)
+			USE_FOR_NOMMU(xformer_prog = "bunzip2";)
+		if (opt & OPT_LZMA)
+			USE_FOR_MMU(xformer = unpack_lzma_stream;)
+			USE_FOR_NOMMU(xformer_prog = "unlzma";)
+
+		open_transformer_with_sig(tar_handle->src_fd, xformer, xformer_prog);
+		/* Can't lseek over pipes */
+		tar_handle->seek = seek_by_read;
+		/*tar_handle->offset = 0; - already is */
+	}
+
+	while (get_header_tar(tar_handle) == EXIT_SUCCESS)
 		continue;
 
 	/* Check that every file that should have been extracted was */
@@ -1131,5 +1118,9 @@
 	if (ENABLE_FEATURE_CLEAN_UP /* && tar_handle->src_fd != STDIN_FILENO */)
 		close(tar_handle->src_fd);
 
+#ifdef CHECK_FOR_CHILD_EXITCODE
+	return bb_got_signal;
+#else
 	return EXIT_SUCCESS;
+#endif
 }