1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 #include "bb_archive.h"
8 void FAST_FUNC init_transformer_state(transformer_state_t *xstate)
10 memset(xstate, 0, sizeof(*xstate));
13 int FAST_FUNC check_signature16(transformer_state_t *xstate, unsigned magic16)
15 if (!xstate->signature_skipped) {
17 if (full_read(xstate->src_fd, &magic2, 2) != 2 || magic2 != magic16) {
18 bb_simple_error_msg("invalid magic");
21 xstate->signature_skipped = 2;
26 ssize_t FAST_FUNC transformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize)
30 if (xstate->mem_output_size_max != 0) {
31 size_t pos = xstate->mem_output_size;
34 size = (xstate->mem_output_size += bufsize);
35 if (size > xstate->mem_output_size_max) {
36 free(xstate->mem_output_buf);
37 xstate->mem_output_buf = NULL;
38 bb_perror_msg("buffer %u too small", (unsigned)xstate->mem_output_size_max);
42 xstate->mem_output_buf = xrealloc(xstate->mem_output_buf, size + 1);
43 memcpy(xstate->mem_output_buf + pos, buf, bufsize);
44 xstate->mem_output_buf[size] = '\0';
47 nwrote = full_write(xstate->dst_fd, buf, bufsize);
48 if (nwrote != (ssize_t)bufsize) {
49 bb_simple_perror_msg("write");
58 ssize_t FAST_FUNC xtransformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize)
60 ssize_t nwrote = transformer_write(xstate, buf, bufsize);
61 if (nwrote != (ssize_t)bufsize) {
67 void check_errors_in_children(int signo)
72 /* block waiting for any child */
73 if (wait(&status) < 0)
75 return; /* probably there are no children */
79 /* Wait for any child without blocking */
81 if (wait_any_nohang(&status) < 0)
83 /* wait failed?! I'm confused... */
86 /*if (WIFEXITED(status) && WEXITSTATUS(status) == 0)*/
87 /* On Linux, the above can be checked simply as: */
89 /* this child exited with 0 */
92 if (!WIFSIGNALED(status) && !WIFEXITED(status)) ???;
98 /* transformer(), more than meets the eye */
100 void FAST_FUNC fork_transformer(int fd,
101 int signature_skipped,
102 IF_DESKTOP(long long) int FAST_FUNC (*transformer)(transformer_state_t *xstate)
105 void FAST_FUNC fork_transformer(int fd, const char *transform_prog)
108 struct fd_pair fd_pipe;
111 xpiped_pair(fd_pipe);
112 pid = BB_MMU ? xfork() : xvfork();
115 close(fd_pipe.rd); /* we don't want to read from the parent */
116 // FIXME: error check?
119 IF_DESKTOP(long long) int r;
120 transformer_state_t xstate;
121 init_transformer_state(&xstate);
122 xstate.signature_skipped = signature_skipped;
124 xstate.dst_fd = fd_pipe.wr;
125 r = transformer(&xstate);
126 if (ENABLE_FEATURE_CLEAN_UP) {
127 close(fd_pipe.wr); /* send EOF */
130 /* must be _exit! bug was actually seen here */
131 _exit(/*error if:*/ r < 0);
137 xmove_fd(fd_pipe.wr, 1);
138 argv[0] = (char*)transform_prog;
139 argv[1] = (char*)"-cf";
140 argv[2] = (char*)"-";
142 BB_EXECVP(transform_prog, argv);
143 bb_perror_msg_and_die("can't execute '%s'", transform_prog);
150 close(fd_pipe.wr); /* don't want to write to the child */
151 xmove_fd(fd_pipe.rd, fd);
155 #if SEAMLESS_COMPRESSION
157 /* Used by e.g. rpm which gives us a fd without filename,
158 * thus we can't guess the format from filename's extension.
160 static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_compressed)
162 transformer_state_t *xstate;
164 xstate = xzalloc(sizeof(*xstate));
167 /* .gz and .bz2 both have 2-byte signature, and their
168 * unpack_XXX_stream wants this header skipped. */
169 xstate->signature_skipped = 2;
170 xread(fd, xstate->magic.b16, 2);
171 if (ENABLE_FEATURE_SEAMLESS_GZ
172 && xstate->magic.b16[0] == GZIP_MAGIC
174 xstate->xformer = unpack_gz_stream;
175 USE_FOR_NOMMU(xstate->xformer_prog = "gunzip";)
178 if (ENABLE_FEATURE_SEAMLESS_Z
179 && xstate->magic.b16[0] == COMPRESS_MAGIC
181 xstate->xformer = unpack_Z_stream;
182 USE_FOR_NOMMU(xstate->xformer_prog = "uncompress";)
185 if (ENABLE_FEATURE_SEAMLESS_BZ2
186 && xstate->magic.b16[0] == BZIP2_MAGIC
188 xstate->xformer = unpack_bz2_stream;
189 USE_FOR_NOMMU(xstate->xformer_prog = "bunzip2";)
192 if (ENABLE_FEATURE_SEAMLESS_XZ
193 && xstate->magic.b16[0] == XZ_MAGIC1
196 xstate->signature_skipped = 6;
197 xread(fd, &xstate->magic.b16[1], 4);
198 move_from_unaligned32(v32, &xstate->magic.b16[1]);
199 if (v32 == XZ_MAGIC2) {
200 xstate->xformer = unpack_xz_stream;
201 USE_FOR_NOMMU(xstate->xformer_prog = "unxz";)
206 /* No known magic seen */
207 if (fail_if_not_compressed)
208 bb_simple_error_msg_and_die("no gzip"
209 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
210 IF_FEATURE_SEAMLESS_XZ("/xz")
213 /* Some callers expect this function to "consume" fd
214 * even if data is not compressed. In this case,
215 * we return a state with trivial transformer.
217 // USE_FOR_MMU(xstate->xformer = copy_stream;)
218 // USE_FOR_NOMMU(xstate->xformer_prog = "cat";)
224 static void fork_transformer_and_free(transformer_state_t *xstate)
227 fork_transformer_with_no_sig(xstate->src_fd, xstate->xformer);
229 /* NOMMU version of fork_transformer execs
230 * an external unzipper that wants
231 * file position at the start of the file.
233 xlseek(xstate->src_fd, - xstate->signature_skipped, SEEK_CUR);
234 xstate->signature_skipped = 0;
235 fork_transformer_with_sig(xstate->src_fd, xstate->xformer, xstate->xformer_prog);
240 /* Used by e.g. rpm which gives us a fd without filename,
241 * thus we can't guess the format from filename's extension.
243 int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed)
245 transformer_state_t *xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
247 if (!xstate->xformer) {
252 fork_transformer_and_free(xstate);
255 #if ENABLE_FEATURE_SEAMLESS_LZMA
256 /* ...and custom version for LZMA */
257 void FAST_FUNC setup_lzma_on_fd(int fd)
259 transformer_state_t *xstate = xzalloc(sizeof(*xstate));
261 xstate->xformer = unpack_lzma_stream;
262 USE_FOR_NOMMU(xstate->xformer_prog = "unlzma";)
263 fork_transformer_and_free(xstate);
267 static transformer_state_t *open_transformer(const char *fname, int fail_if_not_compressed)
269 transformer_state_t *xstate;
272 fd = open(fname, O_RDONLY);
276 if (ENABLE_FEATURE_SEAMLESS_LZMA) {
277 /* .lzma has no header/signature, can only detect it by extension */
278 if (is_suffixed_with(fname, ".lzma")) {
279 xstate = xzalloc(sizeof(*xstate));
281 xstate->xformer = unpack_lzma_stream;
282 USE_FOR_NOMMU(xstate->xformer_prog = "unlzma";)
287 xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
292 int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed)
295 transformer_state_t *xstate;
297 xstate = open_transformer(fname, fail_if_not_compressed);
303 if (xstate->xformer) {
304 fork_transformer_with_no_sig(fd, xstate->xformer);
306 /* the file is not compressed */
307 xlseek(fd, - xstate->signature_skipped, SEEK_CUR);
308 xstate->signature_skipped = 0;
311 /* NOMMU can't avoid the seek :( */
312 xlseek(fd, - xstate->signature_skipped, SEEK_CUR);
313 xstate->signature_skipped = 0;
314 if (xstate->xformer) {
315 fork_transformer_with_sig(fd, xstate->xformer, xstate->xformer_prog);
316 } /* else: the file is not compressed */
323 void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
326 transformer_state_t *xstate;
329 xstate = open_transformer(fname, /*fail_if_not_compressed:*/ 0);
330 if (!xstate) /* file open error */
334 if (xstate->xformer) {
335 /* In-memory decompression */
336 xstate->mem_output_size_max = maxsz_p ? *maxsz_p : (size_t)(INT_MAX - 4095);
337 xstate->xformer(xstate);
338 if (xstate->mem_output_buf) {
339 image = xstate->mem_output_buf;
341 *maxsz_p = xstate->mem_output_size;
344 /* File is not compressed.
345 * We already read first few bytes, account for that.
346 * Example where it happens:
347 * "modinfo MODULE.ko" (not compressed)
348 * open("MODULE.ko", O_RDONLY|O_LARGEFILE) = 4
349 * read(4, "\177E", 2) = 2
352 * read(4, "LF\2\1\1\0\0\0\0"...
353 * ...and we avoided seeking on the fd! :)
355 image = xmalloc_read_with_initial_buf(
358 xmemdup(&xstate->magic, xstate->signature_skipped),
359 xstate->signature_skipped
361 xstate->signature_skipped = 0;
365 bb_perror_msg("read error from '%s'", fname);
366 close(xstate->src_fd);
370 /* This version forks a subprocess - much more expensive */
374 fd = open_zipped(fname, /*fail_if_not_compressed:*/ 0);
378 image = xmalloc_read(fd, maxsz_p);
380 bb_perror_msg("read error from '%s'", fname);
386 #endif /* SEAMLESS_COMPRESSION */