libarchive: add capability to unpack to mem.buffer

The performance and number of processes for a "depmod -a" with gzipped
modules was abysmal. This patch adds a code path without fork,
benefiting all users of xmalloc_open_zipped_read_close.

"modinfo radeon.ko.gz", a single-file reader, got 30% faster.
"depmod -a", which used to fork over 800 times, got 20% faster.

Heavily based on a patch by Lauri Kasanen <curaga@operamail.com>

function                                             old     new   delta
setup_transformer_on_fd                                -     159    +159
transformer_write                                      -     122    +122
fork_transformer                                       -     112    +112
xmalloc_open_zipped_read_close                        63     118     +55
read_bunzip                                         1866    1896     +30
xtransformer_write                                     -      19     +19
unzip_main                                          2449    2462     +13
bbunpack                                             755     766     +11
unpack_lzma_stream                                  2717    2723      +6
unpack_xz_stream                                    2393    2397      +4
unpack_Z_stream                                     1173    1175      +2
inflate_unzip                                        111     105      -6
check_signature16                                     70      63      -7
unpack_bz2_stream                                    359     349     -10
unpack_unxz                                           12       -     -12
unpack_unlzma                                         12       -     -12
unpack_uncompress                                     12       -     -12
unpack_gunzip                                         12       -     -12
unpack_bunzip2                                        12       -     -12
open_transformer                                     106      92     -14
inflate_unzip_internal                              1945    1916     -29
unpack_gz_stream                                     693     655     -38
open_zipped                                           89      47     -42
setup_unzip_on_fd                                    142      53     -89
------------------------------------------------------------------------------
(add/remove: 4/5 grow/shrink: 7/8 up/down: 533/-295)          Total: 238 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/archival/libarchive/decompress_gunzip.c b/archival/libarchive/decompress_gunzip.c
index 62a3d78..1360abe 100644
--- a/archival/libarchive/decompress_gunzip.c
+++ b/archival/libarchive/decompress_gunzip.c
@@ -971,7 +971,7 @@
 
 /* Called from unpack_gz_stream() and inflate_unzip() */
 static IF_DESKTOP(long long) int
-inflate_unzip_internal(STATE_PARAM int in, int out)
+inflate_unzip_internal(STATE_PARAM transformer_state_t *xstate)
 {
 	IF_DESKTOP(long long) int n = 0;
 	ssize_t nwrote;
@@ -980,7 +980,7 @@
 	gunzip_window = xmalloc(GUNZIP_WSIZE);
 	gunzip_outbuf_count = 0;
 	gunzip_bytes_out = 0;
-	gunzip_src_fd = in;
+	gunzip_src_fd = xstate->src_fd;
 
 	/* (re) initialize state */
 	method = -1;
@@ -1002,9 +1002,8 @@
 
 	while (1) {
 		int r = inflate_get_next_window(PASS_STATE_ONLY);
-		nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
-		if (nwrote != (ssize_t)gunzip_outbuf_count) {
-			bb_perror_msg("write");
+		nwrote = transformer_write(xstate, gunzip_window, gunzip_outbuf_count);
+		if (nwrote == (ssize_t)-1) {
 			n = -1;
 			goto ret;
 		}
@@ -1034,7 +1033,7 @@
 /* For unzip */
 
 IF_DESKTOP(long long) int FAST_FUNC
-inflate_unzip(transformer_state_t *xstate, int in, int out)
+inflate_unzip(transformer_state_t *xstate)
 {
 	IF_DESKTOP(long long) int n;
 	DECLARE_STATE;
@@ -1045,7 +1044,7 @@
 //	bytebuffer_max = 0x8000;
 	bytebuffer_offset = 4;
 	bytebuffer = xmalloc(bytebuffer_max);
-	n = inflate_unzip_internal(PASS_STATE in, out);
+	n = inflate_unzip_internal(PASS_STATE xstate);
 	free(bytebuffer);
 
 	xstate->crc32 = gunzip_crc;
@@ -1169,8 +1168,7 @@
 		}
 	}
 
-	if (xstate)
-		xstate->mtime = SWAP_LE32(header.formatted.mtime);
+	xstate->mtime = SWAP_LE32(header.formatted.mtime);
 
 	/* Read the header checksum */
 	if (header.formatted.flags & 0x02) {
@@ -1182,27 +1180,27 @@
 }
 
 IF_DESKTOP(long long) int FAST_FUNC
-unpack_gz_stream(transformer_state_t *xstate, int src_fd, int dst_fd)
+unpack_gz_stream(transformer_state_t *xstate)
 {
 	uint32_t v32;
 	IF_DESKTOP(long long) int total, n;
 	DECLARE_STATE;
 
 #if !ENABLE_FEATURE_SEAMLESS_Z
-	if (check_signature16(xstate, src_fd, GZIP_MAGIC))
+	if (check_signature16(xstate, GZIP_MAGIC))
 		return -1;
 #else
-	if (xstate && xstate->check_signature) {
+	if (xstate->check_signature) {
 		uint16_t magic2;
 
-		if (full_read(src_fd, &magic2, 2) != 2) {
+		if (full_read(xstate->src_fd, &magic2, 2) != 2) {
  bad_magic:
 			bb_error_msg("invalid magic");
 			return -1;
 		}
 		if (magic2 == COMPRESS_MAGIC) {
 			xstate->check_signature = 0;
-			return unpack_Z_stream(xstate, src_fd, dst_fd);
+			return unpack_Z_stream(xstate);
 		}
 		if (magic2 != GZIP_MAGIC)
 			goto bad_magic;
@@ -1215,7 +1213,7 @@
 	to_read = -1;
 //	bytebuffer_max = 0x8000;
 	bytebuffer = xmalloc(bytebuffer_max);
-	gunzip_src_fd = src_fd;
+	gunzip_src_fd = xstate->src_fd;
 
  again:
 	if (!check_header_gzip(PASS_STATE xstate)) {
@@ -1224,7 +1222,7 @@
 		goto ret;
 	}
 
-	n = inflate_unzip_internal(PASS_STATE src_fd, dst_fd);
+	n = inflate_unzip_internal(PASS_STATE xstate);
 	if (n < 0) {
 		total = -1;
 		goto ret;