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 void FAST_FUNC init_transformer_aux_data(transformer_aux_data_t *aux)
11 memset(aux, 0, sizeof(*aux));
14 int FAST_FUNC check_signature16(transformer_aux_data_t *aux, int src_fd, unsigned magic16)
16 if (aux && aux->check_signature) {
18 if (full_read(src_fd, &magic2, 2) != 2 || magic2 != magic16) {
19 bb_error_msg("invalid magic");
20 #if 0 /* possible future extension */
21 if (aux->check_signature > 1)
30 void check_errors_in_children(int signo)
35 /* block waiting for any child */
36 if (wait(&status) < 0)
37 return; /* probably there are no children */
41 /* Wait for any child without blocking */
43 if (wait_any_nohang(&status) < 0)
44 /* wait failed?! I'm confused... */
47 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
48 /* this child exited with 0 */
51 if (!WIFSIGNALED(status) && !WIFEXITED(status)) ???; */
56 /* transformer(), more than meets the eye */
58 void FAST_FUNC open_transformer(int fd,
60 IF_DESKTOP(long long) int FAST_FUNC (*transformer)(transformer_aux_data_t *aux, int src_fd, int dst_fd)
63 void FAST_FUNC open_transformer(int fd, const char *transform_prog)
66 struct fd_pair fd_pipe;
70 pid = BB_MMU ? xfork() : xvfork();
73 close(fd_pipe.rd); /* we don't want to read from the parent */
74 // FIXME: error check?
77 transformer_aux_data_t aux;
78 init_transformer_aux_data(&aux);
79 aux.check_signature = check_signature;
80 transformer(&aux, fd, fd_pipe.wr);
81 if (ENABLE_FEATURE_CLEAN_UP) {
82 close(fd_pipe.wr); /* send EOF */
85 /* must be _exit! bug was actually seen here */
92 xmove_fd(fd_pipe.wr, 1);
93 argv[0] = (char*)transform_prog;
94 argv[1] = (char*)"-cf";
97 BB_EXECVP(transform_prog, argv);
98 bb_perror_msg_and_die("can't execute '%s'", transform_prog);
105 close(fd_pipe.wr); /* don't want to write to the child */
106 xmove_fd(fd_pipe.rd, fd);
110 #if SEAMLESS_COMPRESSION
112 /* Used by e.g. rpm which gives us a fd without filename,
113 * thus we can't guess the format from filename's extension.
115 int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_detected)
123 USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_aux_data_t *aux, int src_fd, int dst_fd);)
124 USE_FOR_NOMMU(const char *xformer_prog;)
126 /* .gz and .bz2 both have 2-byte signature, and their
127 * unpack_XXX_stream wants this header skipped. */
128 xread(fd, magic.b16, sizeof(magic.b16[0]));
129 if (ENABLE_FEATURE_SEAMLESS_GZ
130 && magic.b16[0] == GZIP_MAGIC
132 USE_FOR_MMU(xformer = unpack_gz_stream;)
133 USE_FOR_NOMMU(xformer_prog = "gunzip";)
136 if (ENABLE_FEATURE_SEAMLESS_BZ2
137 && magic.b16[0] == BZIP2_MAGIC
139 USE_FOR_MMU(xformer = unpack_bz2_stream;)
140 USE_FOR_NOMMU(xformer_prog = "bunzip2";)
143 if (ENABLE_FEATURE_SEAMLESS_XZ
144 && magic.b16[0] == XZ_MAGIC1
147 xread(fd, magic.b32, sizeof(magic.b32[0]));
148 if (magic.b32[0] == XZ_MAGIC2) {
149 USE_FOR_MMU(xformer = unpack_xz_stream;)
150 USE_FOR_NOMMU(xformer_prog = "unxz";)
155 /* No known magic seen */
156 if (fail_if_not_detected)
157 bb_error_msg_and_die("no gzip"
158 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
159 IF_FEATURE_SEAMLESS_XZ("/xz")
161 xlseek(fd, offset, SEEK_CUR);
166 open_transformer_with_no_sig(fd, xformer);
168 /* NOMMU version of open_transformer execs
169 * an external unzipper that wants
170 * file position at the start of the file */
171 xlseek(fd, offset, SEEK_CUR);
172 open_transformer_with_sig(fd, xformer, xformer_prog);
177 int FAST_FUNC open_zipped(const char *fname)
182 fd = open(fname, O_RDONLY);
186 sfx = strrchr(fname, '.');
189 if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0)
190 /* .lzma has no header/signature, just trust it */
191 open_transformer_with_sig(fd, unpack_lzma_stream, "unlzma");
193 if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0)
194 || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0)
195 || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0)
197 setup_unzip_on_fd(fd, /*fail_if_not_detected:*/ 1);
204 #endif /* SEAMLESS_COMPRESSION */
206 void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
211 fd = open_zipped(fname);
215 image = xmalloc_read(fd, maxsz_p);
217 bb_perror_msg("read error from '%s'", fname);