1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 #include "bb_archive.h"
9 #define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \
10 || ENABLE_FEATURE_SEAMLESS_BZ2 \
11 || ENABLE_FEATURE_SEAMLESS_GZ \
12 /* || ENABLE_FEATURE_SEAMLESS_Z */ \
16 # include "bb_archive.h"
19 /* transformer(), more than meets the eye */
21 * On MMU machine, the transform_prog is removed by macro magic
22 * in include/archive.h. On NOMMU, transformer is removed.
24 void FAST_FUNC open_transformer(int fd,
25 IF_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd),
26 const char *transform_prog)
28 struct fd_pair fd_pipe;
32 pid = BB_MMU ? xfork() : xvfork();
35 close(fd_pipe.rd); /* we don't want to read from the parent */
36 // FIXME: error check?
38 transformer(fd, fd_pipe.wr);
39 if (ENABLE_FEATURE_CLEAN_UP) {
40 close(fd_pipe.wr); /* send EOF */
43 /* must be _exit! bug was actually seen here */
49 xmove_fd(fd_pipe.wr, 1);
50 argv[0] = (char*)transform_prog;
51 argv[1] = (char*)"-cf";
54 BB_EXECVP(transform_prog, argv);
55 bb_perror_msg_and_die("can't execute '%s'", transform_prog);
62 close(fd_pipe.wr); /* don't want to write to the child */
63 xmove_fd(fd_pipe.rd, fd);
67 /* Used by e.g. rpm which gives us a fd without filename,
68 * thus we can't guess the format from filename's extension.
71 void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
73 const int fail_if_not_detected = 1;
81 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
82 enum { xformer_prog = 0 };
85 const char *xformer_prog;
88 /* .gz and .bz2 both have 2-byte signature, and their
89 * unpack_XXX_stream wants this header skipped. */
90 xread(fd, magic.b16, sizeof(magic.b16[0]));
91 if (ENABLE_FEATURE_SEAMLESS_GZ
92 && magic.b16[0] == GZIP_MAGIC
95 xformer = unpack_gz_stream;
97 xformer_prog = "gunzip";
101 if (ENABLE_FEATURE_SEAMLESS_BZ2
102 && magic.b16[0] == BZIP2_MAGIC
105 xformer = unpack_bz2_stream;
107 xformer_prog = "bunzip2";
111 if (ENABLE_FEATURE_SEAMLESS_XZ
112 && magic.b16[0] == XZ_MAGIC1
115 xread(fd, magic.b32, sizeof(magic.b32[0]));
116 if (magic.b32[0] == XZ_MAGIC2) {
118 xformer = unpack_xz_stream;
119 /* unpack_xz_stream wants fd at position 6, no need to seek */
120 //xlseek(fd, offset, SEEK_CUR);
122 xformer_prog = "unxz";
128 /* No known magic seen */
129 if (fail_if_not_detected)
130 bb_error_msg_and_die("no gzip"
131 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
132 IF_FEATURE_SEAMLESS_XZ("/xz")
134 xlseek(fd, offset, SEEK_CUR);
139 /* NOMMU version of open_transformer execs
140 * an external unzipper that wants
141 * file position at the start of the file */
142 xlseek(fd, offset, SEEK_CUR);
144 open_transformer(fd, xformer, xformer_prog);
148 int FAST_FUNC open_zipped(const char *fname)
151 return open(fname, O_RDONLY);
156 fd = open(fname, O_RDONLY);
160 sfx = strrchr(fname, '.');
163 if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0)
164 /* .lzma has no header/signature, just trust it */
165 open_transformer(fd, unpack_lzma_stream, "unlzma");
167 if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0)
168 || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0)
169 || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0)
171 setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/);
179 void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
184 fd = open_zipped(fname);
188 image = xmalloc_read(fd, maxsz_p);
190 bb_perror_msg("read error from '%s'", fname);