if (!(option_mask32 & SEAMLESS_MAGIC)) {
init_transformer_state(&xstate);
xstate.check_signature = 1;
+ /*xstate.src_fd = STDIN_FILENO; - already is */
+ xstate.dst_fd = STDOUT_FILENO;
status = unpacker(&xstate);
if (status < 0)
exitcode = 1;
//applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
//kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
#if ENABLE_UNCOMPRESS
-static
-IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_state_t *xstate)
-{
- return unpack_Z_stream(xstate, STDIN_FILENO, STDOUT_FILENO);
-}
int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int uncompress_main(int argc UNUSED_PARAM, char **argv)
{
getopt32(argv, "cf");
argv += optind;
- return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
+ return bbunpack(argv, unpack_Z_stream, make_new_name_generic, "Z");
}
#endif
}
return filename;
}
-static
-IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_state_t *xstate)
-{
- return unpack_gz_stream(xstate, STDIN_FILENO, STDOUT_FILENO);
-}
/*
* Linux kernel build uses gzip -d -n. We accept and ignore it.
* Man page says:
if (applet_name[1] == 'c')
option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
- return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
+ return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
}
#endif
//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(transformer_state_t *xstate)
-{
- return unpack_bz2_stream(xstate, STDIN_FILENO, STDOUT_FILENO);
-}
int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int bunzip2_main(int argc UNUSED_PARAM, char **argv)
{
if (applet_name[2] == 'c') /* bzcat */
option_mask32 |= OPT_STDOUT;
- return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
+ return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
}
#endif
//applet:IF_LZMA(APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
//kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
#if ENABLE_UNLZMA
-static
-IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_state_t *xstate)
-{
- return unpack_lzma_stream(xstate, STDIN_FILENO, STDOUT_FILENO);
-}
int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int unlzma_main(int argc UNUSED_PARAM, char **argv)
{
option_mask32 |= OPT_STDOUT;
argv += optind;
- return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
+ return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
}
#endif
//applet:IF_XZ(APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
//kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
#if ENABLE_UNXZ
-static
-IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_state_t *xstate)
-{
- return unpack_xz_stream(xstate, STDIN_FILENO, STDOUT_FILENO);
-}
int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int unxz_main(int argc UNUSED_PARAM, char **argv)
{
option_mask32 |= OPT_STDOUT;
argv += optind;
- return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
+ return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");
}
#endif
/* Decompress src_fd to dst_fd. Stops at end of bzip data, not end of file. */
IF_DESKTOP(long long) int FAST_FUNC
-unpack_bz2_stream(transformer_state_t *xstate, int src_fd, int dst_fd)
+unpack_bz2_stream(transformer_state_t *xstate)
{
IF_DESKTOP(long long total_written = 0;)
bunzip_data *bd;
int i;
unsigned len;
- if (check_signature16(xstate, src_fd, BZIP2_MAGIC))
+ if (check_signature16(xstate, BZIP2_MAGIC))
return -1;
outbuf = xmalloc(IOBUF_SIZE);
len = 0;
while (1) { /* "Process one BZ... stream" loop */
- i = start_bunzip(&bd, src_fd, outbuf + 2, len);
+ i = start_bunzip(&bd, xstate->src_fd, outbuf + 2, len);
if (i == 0) {
while (1) { /* "Produce some output bytes" loop */
i = IOBUF_SIZE - i; /* number of bytes produced */
if (i == 0) /* EOF? */
break;
- if (i != full_write(dst_fd, outbuf, i)) {
- bb_error_msg("short write");
+ if (i != transformer_write(xstate, outbuf, i)) {
i = RETVAL_SHORT_WRITE;
goto release_mem;
}
len = bd->inbufCount - bd->inbufPos;
memcpy(outbuf, &bd->inbuf[bd->inbufPos], len);
if (len < 2) {
- if (safe_read(src_fd, outbuf + len, 2 - len) != 2 - len)
+ if (safe_read(xstate->src_fd, outbuf + len, 2 - len) != 2 - len)
break;
len = 2;
}
/* 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;
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;
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;
}
/* 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;
// 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;
}
}
- 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) {
}
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;
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)) {
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;
*/
IF_DESKTOP(long long) int FAST_FUNC
-unpack_Z_stream(transformer_state_t *xstate, int src_fd, int dst_fd)
+unpack_Z_stream(transformer_state_t *xstate)
{
IF_DESKTOP(long long total_written = 0;)
IF_DESKTOP(long long) int retval = -1;
/* block compress mode -C compatible with 2.0 */
int block_mode; /* = BLOCK_MODE; */
- if (check_signature16(xstate, src_fd, COMPRESS_MAGIC))
+ if (check_signature16(xstate, COMPRESS_MAGIC))
return -1;
inbuf = xzalloc(IBUFSIZ + 64);
/* xread isn't good here, we have to return - caller may want
* to do some cleanup (e.g. delete incomplete unpacked file etc) */
- if (full_read(src_fd, inbuf, 1) != 1) {
+ if (full_read(xstate->src_fd, inbuf, 1) != 1) {
bb_error_msg("short read");
goto err;
}
}
if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
- rsize = safe_read(src_fd, inbuf + insize, IBUFSIZ);
+ rsize = safe_read(xstate->src_fd, inbuf + insize, IBUFSIZ);
if (rsize < 0)
bb_error_msg_and_die(bb_msg_read_error);
insize += rsize;
}
if (outpos >= OBUFSIZ) {
- xwrite(dst_fd, outbuf, outpos);
+ xtransformer_write(xstate, outbuf, outpos);
IF_DESKTOP(total_written += outpos;)
outpos = 0;
}
} while (rsize > 0);
if (outpos > 0) {
- xwrite(dst_fd, outbuf, outpos);
+ xtransformer_write(xstate, outbuf, outpos);
IF_DESKTOP(total_written += outpos;)
}
IF_DESKTOP(long long) int FAST_FUNC
-unpack_lzma_stream(transformer_state_t *xstate UNUSED_PARAM, int src_fd, int dst_fd)
+unpack_lzma_stream(transformer_state_t *xstate)
{
IF_DESKTOP(long long total_written = 0;)
lzma_header_t header;
int state = 0;
uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
- if (full_read(src_fd, &header, sizeof(header)) != sizeof(header)
+ if (full_read(xstate->src_fd, &header, sizeof(header)) != sizeof(header)
|| header.pos >= (9 * 5 * 5)
) {
bb_error_msg("bad lzma header");
p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
}
- rc = rc_init(src_fd); /*, RC_BUFFER_SIZE); */
+ rc = rc_init(xstate->src_fd); /*, RC_BUFFER_SIZE); */
while (global_pos + buffer_pos < header.dst_size) {
int pos_state = (buffer_pos + global_pos) & pos_state_mask;
if (buffer_pos == header.dict_size) {
buffer_pos = 0;
global_pos += header.dict_size;
- if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
+ if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
goto bad;
IF_DESKTOP(total_written += header.dict_size;)
}
if (buffer_pos == header.dict_size) {
buffer_pos = 0;
global_pos += header.dict_size;
- if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
+ if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
goto bad;
IF_DESKTOP(total_written += header.dict_size;)
}
{
IF_NOT_DESKTOP(int total_written = 0; /* success */)
IF_DESKTOP(total_written += buffer_pos;)
- if (full_write(dst_fd, buffer, buffer_pos) != (ssize_t)buffer_pos) {
+ if (transformer_write(xstate, buffer, buffer_pos) != (ssize_t)buffer_pos) {
bad:
total_written = -1; /* failure */
}
#include "unxz/xz_dec_stream.c"
IF_DESKTOP(long long) int FAST_FUNC
-unpack_xz_stream(transformer_state_t *xstate, int src_fd, int dst_fd)
+unpack_xz_stream(transformer_state_t *xstate)
{
enum xz_ret xz_result;
struct xz_buf iobuf;
xz_result = X_OK;
while (1) {
if (iobuf.in_pos == iobuf.in_size) {
- int rd = safe_read(src_fd, membuf, BUFSIZ);
+ int rd = safe_read(xstate->src_fd, membuf, BUFSIZ);
if (rd < 0) {
bb_error_msg(bb_msg_read_error);
total = -1;
// bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, xz_result);
if (iobuf.out_pos) {
- xwrite(dst_fd, iobuf.out, iobuf.out_pos);
+ xtransformer_write(xstate, iobuf.out, iobuf.out_pos);
IF_DESKTOP(total += iobuf.out_pos;)
iobuf.out_pos = 0;
}
/* Can't lseek over pipes */
archive_handle->seek = seek_by_read;
- open_transformer_with_sig(archive_handle->src_fd, unpack_bz2_stream, "bunzip2");
+ fork_transformer_with_sig(archive_handle->src_fd, unpack_bz2_stream, "bunzip2");
archive_handle->offset = 0;
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
continue;
/* Can't lseek over pipes */
archive_handle->seek = seek_by_read;
- open_transformer_with_sig(archive_handle->src_fd, unpack_gz_stream, "gunzip");
+ fork_transformer_with_sig(archive_handle->src_fd, unpack_gz_stream, "gunzip");
archive_handle->offset = 0;
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
continue;
/* Can't lseek over pipes */
archive_handle->seek = seek_by_read;
- open_transformer_with_sig(archive_handle->src_fd, unpack_lzma_stream, "unlzma");
+ fork_transformer_with_sig(archive_handle->src_fd, unpack_lzma_stream, "unlzma");
archive_handle->offset = 0;
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
continue;
memset(xstate, 0, sizeof(*xstate));
}
-int FAST_FUNC check_signature16(transformer_state_t *xstate, int src_fd, unsigned magic16)
+int FAST_FUNC check_signature16(transformer_state_t *xstate, unsigned magic16)
{
- if (xstate && xstate->check_signature) {
+ if (xstate->check_signature) {
uint16_t magic2;
- if (full_read(src_fd, &magic2, 2) != 2 || magic2 != magic16) {
+ if (full_read(xstate->src_fd, &magic2, 2) != 2 || magic2 != magic16) {
bb_error_msg("invalid magic");
#if 0 /* possible future extension */
if (xstate->check_signature > 1)
return 0;
}
+ssize_t FAST_FUNC transformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize)
+{
+ ssize_t nwrote;
+
+ if (xstate->mem_output_size_max != 0) {
+ size_t pos = xstate->mem_output_size;
+ size_t size;
+
+ size = (xstate->mem_output_size += bufsize);
+ if (size > xstate->mem_output_size_max) {
+ free(xstate->mem_output_buf);
+ xstate->mem_output_buf = NULL;
+ bb_perror_msg("buffer %u too small", (unsigned)xstate->mem_output_size_max);
+ nwrote = -1;
+ goto ret;
+ }
+ xstate->mem_output_buf = xrealloc(xstate->mem_output_buf, size);
+ memcpy(xstate->mem_output_buf + pos, buf, bufsize);
+ nwrote = bufsize;
+ } else {
+ nwrote = full_write(xstate->dst_fd, buf, bufsize);
+ if (nwrote != (ssize_t)bufsize) {
+ bb_perror_msg("write");
+ nwrote = -1;
+ goto ret;
+ }
+ }
+ ret:
+ return nwrote;
+}
+
+ssize_t FAST_FUNC xtransformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize)
+{
+ ssize_t nwrote = transformer_write(xstate, buf, bufsize);
+ if (nwrote != (ssize_t)bufsize) {
+ xfunc_die();
+ }
+ return nwrote;
+}
+
void check_errors_in_children(int signo)
{
int status;
/* transformer(), more than meets the eye */
#if BB_MMU
-void FAST_FUNC open_transformer(int fd,
+void FAST_FUNC fork_transformer(int fd,
int check_signature,
- IF_DESKTOP(long long) int FAST_FUNC (*transformer)(transformer_state_t *xstate, int src_fd, int dst_fd)
+ IF_DESKTOP(long long) int FAST_FUNC (*transformer)(transformer_state_t *xstate)
)
#else
-void FAST_FUNC open_transformer(int fd, const char *transform_prog)
+void FAST_FUNC fork_transformer(int fd, const char *transform_prog)
#endif
{
struct fd_pair fd_pipe;
transformer_state_t xstate;
init_transformer_state(&xstate);
xstate.check_signature = check_signature;
- r = transformer(&xstate, fd, fd_pipe.wr);
+ xstate.src_fd = fd;
+ xstate.dst_fd = fd_pipe.wr;
+ r = transformer(&xstate);
if (ENABLE_FEATURE_CLEAN_UP) {
close(fd_pipe.wr); /* send EOF */
close(fd);
/* Used by e.g. rpm which gives us a fd without filename,
* thus we can't guess the format from filename's extension.
*/
-int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed)
+static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_compressed)
{
union {
uint8_t b[4];
uint16_t b16[2];
uint32_t b32[1];
} magic;
- int offset = -2;
- USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_state_t *xstate, int src_fd, int dst_fd);)
- USE_FOR_NOMMU(const char *xformer_prog;)
+ int offset;
+ transformer_state_t *xstate;
+
+ offset = -2;
+ xstate = xzalloc(sizeof(*xstate));
+ xstate->src_fd = fd;
/* .gz and .bz2 both have 2-byte signature, and their
* unpack_XXX_stream wants this header skipped. */
if (ENABLE_FEATURE_SEAMLESS_GZ
&& magic.b16[0] == GZIP_MAGIC
) {
- USE_FOR_MMU(xformer = unpack_gz_stream;)
- USE_FOR_NOMMU(xformer_prog = "gunzip";)
+ xstate->xformer = unpack_gz_stream;
+ USE_FOR_NOMMU(xstate->xformer_prog = "gunzip";)
goto found_magic;
}
if (ENABLE_FEATURE_SEAMLESS_BZ2
&& magic.b16[0] == BZIP2_MAGIC
) {
- USE_FOR_MMU(xformer = unpack_bz2_stream;)
- USE_FOR_NOMMU(xformer_prog = "bunzip2";)
+ xstate->xformer = unpack_bz2_stream;
+ USE_FOR_NOMMU(xstate->xformer_prog = "bunzip2";)
goto found_magic;
}
if (ENABLE_FEATURE_SEAMLESS_XZ
offset = -6;
xread(fd, magic.b32, sizeof(magic.b32[0]));
if (magic.b32[0] == XZ_MAGIC2) {
- USE_FOR_MMU(xformer = unpack_xz_stream;)
- USE_FOR_NOMMU(xformer_prog = "unxz";)
+ xstate->xformer = unpack_xz_stream;
+ USE_FOR_NOMMU(xstate->xformer_prog = "unxz";)
goto found_magic;
}
}
IF_FEATURE_SEAMLESS_BZ2("/bzip2")
IF_FEATURE_SEAMLESS_XZ("/xz")
" magic");
+
+ /* Some callers expect this function to "consume" fd
+ * even if data is not compressed. In this case,
+ * we return a state with trivial transformer.
+ */
+// USE_FOR_MMU(xstate->xformer = copy_stream;)
+// USE_FOR_NOMMU(xstate->xformer_prog = "cat";)
+ /* fall through to seeking bck over bytes we read earlier */
+
+ USE_FOR_NOMMU(found_magic:)
+ /* NOMMU version of fork_transformer execs
+ * an external unzipper that wants
+ * file position at the start of the file.
+ */
xlseek(fd, offset, SEEK_CUR);
- return 1;
- found_magic:
+ USE_FOR_MMU(found_magic:)
+ /* In MMU case, if magic was found, seeking back is not necessary */
+
+ return xstate;
+}
+
+/* Used by e.g. rpm which gives us a fd without filename,
+ * thus we can't guess the format from filename's extension.
+ */
+int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed)
+{
+ transformer_state_t *xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
+
+ if (!xstate || !xstate->xformer) {
+ free(xstate);
+ return 1;
+ }
+
# if BB_MMU
- open_transformer_with_no_sig(fd, xformer);
+ fork_transformer_with_no_sig(xstate->src_fd, xstate->xformer);
# else
- /* NOMMU version of open_transformer execs
- * an external unzipper that wants
- * file position at the start of the file */
- xlseek(fd, offset, SEEK_CUR);
- open_transformer_with_sig(fd, xformer, xformer_prog);
+ fork_transformer_with_sig(xstate->src_fd, xstate->xformer, xstate->xformer_prog);
# endif
+ free(xstate);
return 0;
}
-int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed)
+static transformer_state_t *open_transformer(const char *fname, int fail_if_not_compressed)
{
+ transformer_state_t *xstate;
int fd;
fd = open(fname, O_RDONLY);
if (fd < 0)
- return fd;
+ return NULL;
if (ENABLE_FEATURE_SEAMLESS_LZMA) {
/* .lzma has no header/signature, can only detect it by extension */
char *sfx = strrchr(fname, '.');
if (sfx && strcmp(sfx+1, "lzma") == 0) {
- open_transformer_with_sig(fd, unpack_lzma_stream, "unlzma");
- return fd;
+ xstate = xzalloc(sizeof(*xstate));
+ xstate->src_fd = fd;
+ xstate->xformer = unpack_lzma_stream;
+ USE_FOR_NOMMU(xstate->xformer_prog = "unlzma";)
+ return xstate;
}
}
- if ((ENABLE_FEATURE_SEAMLESS_GZ)
- || (ENABLE_FEATURE_SEAMLESS_BZ2)
- || (ENABLE_FEATURE_SEAMLESS_XZ)
- ) {
- setup_unzip_on_fd(fd, fail_if_not_compressed);
+
+ xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
+
+ return xstate;
+}
+
+int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed)
+{
+ int fd;
+ transformer_state_t *xstate;
+
+ xstate = open_transformer(fname, fail_if_not_compressed);
+ if (!xstate)
+ return -1;
+
+ fd = xstate->src_fd;
+ if (xstate->xformer) {
+# if BB_MMU
+ fork_transformer_with_no_sig(xstate->src_fd, xstate->xformer);
+# else
+ fork_transformer_with_sig(xstate->src_fd, xstate->xformer, xstate->xformer_prog);
+# endif
}
+ /* else: the file is not compressed */
+ free(xstate);
return fd;
}
-#endif /* SEAMLESS_COMPRESSION */
-
void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
{
+# if 1
+ transformer_state_t *xstate;
+ char *image;
+
+ xstate = open_transformer(fname, /*fail_if_not_compressed:*/ 0);
+ if (!xstate) /* file open error */
+ return NULL;
+
+ image = NULL;
+ if (xstate->xformer) {
+ /* In-memory decompression */
+ xstate->mem_output_size_max = maxsz_p ? *maxsz_p : (size_t)(INT_MAX - 4095);
+ xstate->xformer(xstate);
+ if (xstate->mem_output_buf) {
+ image = xstate->mem_output_buf;
+ if (maxsz_p)
+ *maxsz_p = xstate->mem_output_size;
+ }
+ } else {
+ /* File is not compressed */
+ image = xmalloc_read(xstate->src_fd, maxsz_p);
+ }
+
+ if (!image)
+ bb_perror_msg("read error from '%s'", fname);
+ close(xstate->src_fd);
+ free(xstate);
+ return image;
+# else
+ /* This version forks a subprocess - much more expensive */
int fd;
char *image;
if (!image)
bb_perror_msg("read error from '%s'", fname);
close(fd);
-
return image;
+# endif
}
+
+#endif /* SEAMLESS_COMPRESSION */
}
if (opt & OPT_ANY_COMPRESS) {
- USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_state_t *xstate, int src_fd, int dst_fd);)
+ USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_state_t *xstate);)
USE_FOR_NOMMU(const char *xformer_prog;)
if (opt & OPT_COMPRESS)
USE_FOR_MMU(xformer = unpack_xz_stream;)
USE_FOR_NOMMU(xformer_prog = "unxz";)
- open_transformer_with_sig(tar_handle->src_fd, xformer, xformer_prog);
+ fork_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 */
transformer_state_t xstate;
init_transformer_state(&xstate);
xstate.bytes_in = zip_header->formatted.cmpsize;
- if (inflate_unzip(&xstate, zip_fd, dst_fd) < 0)
+ xstate.src_fd = zip_fd;
+ xstate.dst_fd = dst_fd;
+ if (inflate_unzip(&xstate) < 0)
bb_error_msg_and_die("inflate error");
/* Validate decompression - crc */
if (zip_header->formatted.crc32 != (xstate.crc32 ^ 0xffffffffL)) {
/* Meaning and direction (input/output) of the fields are transformer-specific */
typedef struct transformer_state_t {
smallint check_signature; /* most often referenced member */
+
+ IF_DESKTOP(long long) int FAST_FUNC (*xformer)(struct transformer_state_t *xstate);
+ USE_FOR_NOMMU(const char *xformer_prog;)
+
+ /* Source */
+ int src_fd;
+ /* Output */
+ int dst_fd;
+ size_t mem_output_size_max; /* if non-zero, decompress to RAM instead of fd */
+ size_t mem_output_size;
+ char *mem_output_buf;
+
off_t bytes_out;
off_t bytes_in; /* used in unzip code only: needs to know packed size */
uint32_t crc32;
} transformer_state_t;
void init_transformer_state(transformer_state_t *xstate) FAST_FUNC;
-int FAST_FUNC check_signature16(transformer_state_t *xstate, int src_fd, unsigned magic16) FAST_FUNC;
+ssize_t transformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize) FAST_FUNC;
+ssize_t xtransformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize) FAST_FUNC;
+int check_signature16(transformer_state_t *xstate, unsigned magic16) FAST_FUNC;
-IF_DESKTOP(long long) int inflate_unzip(transformer_state_t *xstate, int src_fd, int dst_fd) FAST_FUNC;
-IF_DESKTOP(long long) int unpack_Z_stream(transformer_state_t *xstate, int src_fd, int dst_fd) FAST_FUNC;
-IF_DESKTOP(long long) int unpack_gz_stream(transformer_state_t *xstate, int src_fd, int dst_fd) FAST_FUNC;
-IF_DESKTOP(long long) int unpack_bz2_stream(transformer_state_t *xstate, int src_fd, int dst_fd) FAST_FUNC;
-IF_DESKTOP(long long) int unpack_lzma_stream(transformer_state_t *xstate, int src_fd, int dst_fd) FAST_FUNC;
-IF_DESKTOP(long long) int unpack_xz_stream(transformer_state_t *xstate, int src_fd, int dst_fd) FAST_FUNC;
+IF_DESKTOP(long long) int inflate_unzip(transformer_state_t *xstate) FAST_FUNC;
+IF_DESKTOP(long long) int unpack_Z_stream(transformer_state_t *xstate) FAST_FUNC;
+IF_DESKTOP(long long) int unpack_gz_stream(transformer_state_t *xstate) FAST_FUNC;
+IF_DESKTOP(long long) int unpack_bz2_stream(transformer_state_t *xstate) FAST_FUNC;
+IF_DESKTOP(long long) int unpack_lzma_stream(transformer_state_t *xstate) FAST_FUNC;
+IF_DESKTOP(long long) int unpack_xz_stream(transformer_state_t *xstate) FAST_FUNC;
char* append_ext(char *filename, const char *expected_ext) FAST_FUNC;
int bbunpack(char **argv,
void check_errors_in_children(int signo);
#if BB_MMU
-void open_transformer(int fd,
+void fork_transformer(int fd,
int check_signature,
- IF_DESKTOP(long long) int FAST_FUNC (*transformer)(transformer_state_t *xstate, int src_fd, int dst_fd)
+ IF_DESKTOP(long long) int FAST_FUNC (*transformer)(transformer_state_t *xstate)
) FAST_FUNC;
-#define open_transformer_with_sig(fd, transformer, transform_prog) open_transformer((fd), 1, (transformer))
-#define open_transformer_with_no_sig(fd, transformer) open_transformer((fd), 0, (transformer))
+#define fork_transformer_with_sig(fd, transformer, transform_prog) fork_transformer((fd), 1, (transformer))
+#define fork_transformer_with_no_sig(fd, transformer) fork_transformer((fd), 0, (transformer))
#else
-void open_transformer(int fd, const char *transform_prog) FAST_FUNC;
-#define open_transformer_with_sig(fd, transformer, transform_prog) open_transformer((fd), (transform_prog))
-/* open_transformer_with_no_sig() does not exist on NOMMU */
+void fork_transformer(int fd, const char *transform_prog) FAST_FUNC;
+#define fork_transformer_with_sig(fd, transformer, transform_prog) fork_transformer((fd), (transform_prog))
+/* fork_transformer_with_no_sig() does not exist on NOMMU */
#endif
extern int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC;
/* Autodetects .gz etc */
extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC;
+extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
#else
# define setup_unzip_on_fd(...) (0)
# define open_zipped(fname, fail_if_not_compressed) open((fname), O_RDONLY);
+# define xmalloc_open_zipped_read_close(fname, maxsz_p) xmalloc_open_read_close((fname), (maxsz_p))
#endif
-extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
extern ssize_t safe_write(int fd, const void *buf, size_t count) FAST_FUNC;
// NB: will return short write on error, not -1,