blob: ac7e5db95f5aa7db9184b6400d50477141be8aa1 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath5699b852003-11-15 23:19:05 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath5699b852003-11-15 23:19:05 +00004 */
5
Glenn L McGrath5699b852003-11-15 23:19:05 +00006#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +02007#include "bb_archive.h"
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +00008
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01009void FAST_FUNC init_transformer_state(transformer_state_t *xstate)
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010010{
Denys Vlasenkoe7800f32014-12-07 00:42:49 +010011 memset(xstate, 0, sizeof(*xstate));
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010012}
Denys Vlasenko59655072012-03-06 16:23:50 +010013
Denys Vlasenkob4c11c12014-12-07 00:44:00 +010014int FAST_FUNC check_signature16(transformer_state_t *xstate, unsigned magic16)
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010015{
Denys Vlasenko984b0a62016-06-20 11:06:42 +020016 if (!xstate->signature_skipped) {
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010017 uint16_t magic2;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +010018 if (full_read(xstate->src_fd, &magic2, 2) != 2 || magic2 != magic16) {
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010019 bb_error_msg("invalid magic");
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010020 return -1;
21 }
Denys Vlasenko984b0a62016-06-20 11:06:42 +020022 xstate->signature_skipped = 2;
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010023 }
24 return 0;
25}
Denys Vlasenko59655072012-03-06 16:23:50 +010026
Denys Vlasenkob4c11c12014-12-07 00:44:00 +010027ssize_t FAST_FUNC transformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize)
28{
29 ssize_t nwrote;
30
31 if (xstate->mem_output_size_max != 0) {
32 size_t pos = xstate->mem_output_size;
33 size_t size;
34
35 size = (xstate->mem_output_size += bufsize);
36 if (size > xstate->mem_output_size_max) {
37 free(xstate->mem_output_buf);
38 xstate->mem_output_buf = NULL;
39 bb_perror_msg("buffer %u too small", (unsigned)xstate->mem_output_size_max);
40 nwrote = -1;
41 goto ret;
42 }
Denys Vlasenkocfcd2392014-12-07 00:49:55 +010043 xstate->mem_output_buf = xrealloc(xstate->mem_output_buf, size + 1);
Denys Vlasenkob4c11c12014-12-07 00:44:00 +010044 memcpy(xstate->mem_output_buf + pos, buf, bufsize);
Denys Vlasenkocfcd2392014-12-07 00:49:55 +010045 xstate->mem_output_buf[size] = '\0';
Denys Vlasenkob4c11c12014-12-07 00:44:00 +010046 nwrote = bufsize;
47 } else {
48 nwrote = full_write(xstate->dst_fd, buf, bufsize);
49 if (nwrote != (ssize_t)bufsize) {
50 bb_perror_msg("write");
51 nwrote = -1;
52 goto ret;
53 }
54 }
55 ret:
56 return nwrote;
57}
58
59ssize_t FAST_FUNC xtransformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize)
60{
61 ssize_t nwrote = transformer_write(xstate, buf, bufsize);
62 if (nwrote != (ssize_t)bufsize) {
63 xfunc_die();
64 }
65 return nwrote;
66}
67
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010068void check_errors_in_children(int signo)
69{
70 int status;
71
72 if (!signo) {
73 /* block waiting for any child */
74 if (wait(&status) < 0)
Denys Vlasenko577235d2013-02-28 16:38:25 +010075//FIXME: check EINTR?
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010076 return; /* probably there are no children */
77 goto check_status;
78 }
79
80 /* Wait for any child without blocking */
81 for (;;) {
82 if (wait_any_nohang(&status) < 0)
Denys Vlasenko577235d2013-02-28 16:38:25 +010083//FIXME: check EINTR?
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010084 /* wait failed?! I'm confused... */
85 return;
86 check_status:
Denys Vlasenko577235d2013-02-28 16:38:25 +010087 /*if (WIFEXITED(status) && WEXITSTATUS(status) == 0)*/
88 /* On Linux, the above can be checked simply as: */
89 if (status == 0)
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010090 /* this child exited with 0 */
91 continue;
Denys Vlasenko577235d2013-02-28 16:38:25 +010092 /* Cannot happen:
93 if (!WIFSIGNALED(status) && !WIFEXITED(status)) ???;
94 */
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010095 bb_got_signal = 1;
96 }
97}
98
Glenn L McGrath5699b852003-11-15 23:19:05 +000099/* transformer(), more than meets the eye */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100100#if BB_MMU
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100101void FAST_FUNC fork_transformer(int fd,
Denys Vlasenko984b0a62016-06-20 11:06:42 +0200102 int signature_skipped,
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100103 IF_DESKTOP(long long) int FAST_FUNC (*transformer)(transformer_state_t *xstate)
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100104)
105#else
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100106void FAST_FUNC fork_transformer(int fd, const char *transform_prog)
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100107#endif
Glenn L McGrath5699b852003-11-15 23:19:05 +0000108{
Denis Vlasenko37188322008-02-16 13:20:56 +0000109 struct fd_pair fd_pipe;
Glenn L McGrath5699b852003-11-15 23:19:05 +0000110 int pid;
111
Denis Vlasenko37188322008-02-16 13:20:56 +0000112 xpiped_pair(fd_pipe);
Pascal Bellard926031b2010-07-04 15:32:38 +0200113 pid = BB_MMU ? xfork() : xvfork();
Glenn L McGrath5699b852003-11-15 23:19:05 +0000114 if (pid == 0) {
Pascal Bellard926031b2010-07-04 15:32:38 +0200115 /* Child */
Denis Vlasenkob6052722008-07-10 17:43:01 +0000116 close(fd_pipe.rd); /* we don't want to read from the parent */
Denis Vlasenkoea620772006-10-14 02:23:43 +0000117 // FIXME: error check?
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000118#if BB_MMU
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100119 {
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +0100120 IF_DESKTOP(long long) int r;
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100121 transformer_state_t xstate;
122 init_transformer_state(&xstate);
Denys Vlasenko984b0a62016-06-20 11:06:42 +0200123 xstate.signature_skipped = signature_skipped;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100124 xstate.src_fd = fd;
125 xstate.dst_fd = fd_pipe.wr;
126 r = transformer(&xstate);
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100127 if (ENABLE_FEATURE_CLEAN_UP) {
128 close(fd_pipe.wr); /* send EOF */
129 close(fd);
130 }
131 /* must be _exit! bug was actually seen here */
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +0100132 _exit(/*error if:*/ r < 0);
Denis Vlasenko7e0fbf92007-09-04 19:33:22 +0000133 }
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000134#else
Denis Vlasenko059c9172007-11-12 02:13:12 +0000135 {
136 char *argv[4];
Denis Vlasenkob6052722008-07-10 17:43:01 +0000137 xmove_fd(fd, 0);
Denis Vlasenko37188322008-02-16 13:20:56 +0000138 xmove_fd(fd_pipe.wr, 1);
Denis Vlasenko059c9172007-11-12 02:13:12 +0000139 argv[0] = (char*)transform_prog;
140 argv[1] = (char*)"-cf";
141 argv[2] = (char*)"-";
142 argv[3] = NULL;
143 BB_EXECVP(transform_prog, argv);
Denis Vlasenkof9d4fc32009-04-21 20:40:51 +0000144 bb_perror_msg_and_die("can't execute '%s'", transform_prog);
Denis Vlasenko059c9172007-11-12 02:13:12 +0000145 }
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000146#endif
Denis Vlasenkoea620772006-10-14 02:23:43 +0000147 /* notreached */
Glenn L McGrath5699b852003-11-15 23:19:05 +0000148 }
Glenn L McGrath20872be2003-11-18 21:31:19 +0000149
Glenn L McGrath5699b852003-11-15 23:19:05 +0000150 /* parent process */
Denis Vlasenkob6052722008-07-10 17:43:01 +0000151 close(fd_pipe.wr); /* don't want to write to the child */
152 xmove_fd(fd_pipe.rd, fd);
Glenn L McGrath5699b852003-11-15 23:19:05 +0000153}
Denys Vlasenko59655072012-03-06 16:23:50 +0100154
155
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100156#if SEAMLESS_COMPRESSION
157
Denys Vlasenko59655072012-03-06 16:23:50 +0100158/* Used by e.g. rpm which gives us a fd without filename,
159 * thus we can't guess the format from filename's extension.
160 */
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100161static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_compressed)
Denys Vlasenko59655072012-03-06 16:23:50 +0100162{
Denys Vlasenko59655072012-03-06 16:23:50 +0100163 union {
164 uint8_t b[4];
165 uint16_t b16[2];
166 uint32_t b32[1];
167 } magic;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100168 transformer_state_t *xstate;
169
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100170 xstate = xzalloc(sizeof(*xstate));
171 xstate->src_fd = fd;
Denys Vlasenko984b0a62016-06-20 11:06:42 +0200172 xstate->signature_skipped = 2;
Denys Vlasenko59655072012-03-06 16:23:50 +0100173
174 /* .gz and .bz2 both have 2-byte signature, and their
175 * unpack_XXX_stream wants this header skipped. */
176 xread(fd, magic.b16, sizeof(magic.b16[0]));
177 if (ENABLE_FEATURE_SEAMLESS_GZ
178 && magic.b16[0] == GZIP_MAGIC
179 ) {
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100180 xstate->xformer = unpack_gz_stream;
181 USE_FOR_NOMMU(xstate->xformer_prog = "gunzip";)
Denys Vlasenko59655072012-03-06 16:23:50 +0100182 goto found_magic;
183 }
Thiago Jung Bauermannb4059f62015-05-03 18:40:12 +0200184 if (ENABLE_FEATURE_SEAMLESS_Z
185 && magic.b16[0] == COMPRESS_MAGIC
186 ) {
187 xstate->xformer = unpack_Z_stream;
188 USE_FOR_NOMMU(xstate->xformer_prog = "uncompress";)
189 goto found_magic;
190 }
Denys Vlasenko59655072012-03-06 16:23:50 +0100191 if (ENABLE_FEATURE_SEAMLESS_BZ2
192 && magic.b16[0] == BZIP2_MAGIC
193 ) {
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100194 xstate->xformer = unpack_bz2_stream;
195 USE_FOR_NOMMU(xstate->xformer_prog = "bunzip2";)
Denys Vlasenko59655072012-03-06 16:23:50 +0100196 goto found_magic;
197 }
198 if (ENABLE_FEATURE_SEAMLESS_XZ
199 && magic.b16[0] == XZ_MAGIC1
200 ) {
Denys Vlasenko984b0a62016-06-20 11:06:42 +0200201 xstate->signature_skipped = 6;
Denys Vlasenko59655072012-03-06 16:23:50 +0100202 xread(fd, magic.b32, sizeof(magic.b32[0]));
203 if (magic.b32[0] == XZ_MAGIC2) {
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100204 xstate->xformer = unpack_xz_stream;
205 USE_FOR_NOMMU(xstate->xformer_prog = "unxz";)
Denys Vlasenko59655072012-03-06 16:23:50 +0100206 goto found_magic;
207 }
208 }
209
210 /* No known magic seen */
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100211 if (fail_if_not_compressed)
Denys Vlasenko59655072012-03-06 16:23:50 +0100212 bb_error_msg_and_die("no gzip"
213 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
214 IF_FEATURE_SEAMLESS_XZ("/xz")
215 " magic");
Denys Vlasenko59655072012-03-06 16:23:50 +0100216
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100217 /* Some callers expect this function to "consume" fd
218 * even if data is not compressed. In this case,
219 * we return a state with trivial transformer.
220 */
221// USE_FOR_MMU(xstate->xformer = copy_stream;)
222// USE_FOR_NOMMU(xstate->xformer_prog = "cat";)
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100223
Denys Vlasenko984b0a62016-06-20 11:06:42 +0200224 found_magic:
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100225 return xstate;
226}
227
228/* Used by e.g. rpm which gives us a fd without filename,
229 * thus we can't guess the format from filename's extension.
230 */
231int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed)
232{
233 transformer_state_t *xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
234
235 if (!xstate || !xstate->xformer) {
236 free(xstate);
237 return 1;
238 }
239
240# if BB_MMU
241 fork_transformer_with_no_sig(xstate->src_fd, xstate->xformer);
242# else
Denys Vlasenko984b0a62016-06-20 11:06:42 +0200243 /* NOMMU version of fork_transformer execs
244 * an external unzipper that wants
245 * file position at the start of the file.
246 */
247 xlseek(fd, - xstate->signature_skipped, SEEK_CUR);
248 xstate->signature_skipped = 0;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100249 fork_transformer_with_sig(xstate->src_fd, xstate->xformer, xstate->xformer_prog);
Denys Vlasenko59655072012-03-06 16:23:50 +0100250# endif
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100251 free(xstate);
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100252 return 0;
Denys Vlasenko59655072012-03-06 16:23:50 +0100253}
Denys Vlasenko59655072012-03-06 16:23:50 +0100254
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100255static transformer_state_t *open_transformer(const char *fname, int fail_if_not_compressed)
Denys Vlasenko59655072012-03-06 16:23:50 +0100256{
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100257 transformer_state_t *xstate;
Denys Vlasenko59655072012-03-06 16:23:50 +0100258 int fd;
259
260 fd = open(fname, O_RDONLY);
261 if (fd < 0)
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100262 return NULL;
Denys Vlasenko59655072012-03-06 16:23:50 +0100263
Denys Vlasenko7c47b562014-01-10 14:06:57 +0100264 if (ENABLE_FEATURE_SEAMLESS_LZMA) {
265 /* .lzma has no header/signature, can only detect it by extension */
266 char *sfx = strrchr(fname, '.');
267 if (sfx && strcmp(sfx+1, "lzma") == 0) {
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100268 xstate = xzalloc(sizeof(*xstate));
269 xstate->src_fd = fd;
270 xstate->xformer = unpack_lzma_stream;
271 USE_FOR_NOMMU(xstate->xformer_prog = "unlzma";)
272 return xstate;
Denys Vlasenko59655072012-03-06 16:23:50 +0100273 }
274 }
275
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100276 xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
277
278 return xstate;
279}
280
281int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed)
282{
283 int fd;
284 transformer_state_t *xstate;
285
286 xstate = open_transformer(fname, fail_if_not_compressed);
287 if (!xstate)
288 return -1;
289
290 fd = xstate->src_fd;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100291# if BB_MMU
Denys Vlasenkodf3ec0e2016-06-20 11:42:00 +0200292 if (xstate->xformer) {
293 fork_transformer_with_no_sig(fd, xstate->xformer);
294 } else {
295 /* the file is not compressed */
Denys Vlasenko984b0a62016-06-20 11:06:42 +0200296 xlseek(fd, - xstate->signature_skipped, SEEK_CUR);
297 xstate->signature_skipped = 0;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100298 }
Denys Vlasenkodf3ec0e2016-06-20 11:42:00 +0200299# else
300 /* NOMMU can't avoid the seek :( */
301 xlseek(fd, - xstate->signature_skipped, SEEK_CUR);
302 xstate->signature_skipped = 0;
303 if (xstate->xformer) {
304 fork_transformer_with_sig(fd, xstate->xformer, xstate->xformer_prog);
Denys Vlasenkoe24e8862016-06-20 16:28:53 +0200305 } /* else: the file is not compressed */
Denys Vlasenkodf3ec0e2016-06-20 11:42:00 +0200306# endif
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100307
308 free(xstate);
Denys Vlasenko59655072012-03-06 16:23:50 +0100309 return fd;
Denys Vlasenko59655072012-03-06 16:23:50 +0100310}
311
312void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
313{
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100314# if 1
315 transformer_state_t *xstate;
316 char *image;
317
318 xstate = open_transformer(fname, /*fail_if_not_compressed:*/ 0);
319 if (!xstate) /* file open error */
320 return NULL;
321
322 image = NULL;
323 if (xstate->xformer) {
324 /* In-memory decompression */
325 xstate->mem_output_size_max = maxsz_p ? *maxsz_p : (size_t)(INT_MAX - 4095);
326 xstate->xformer(xstate);
327 if (xstate->mem_output_buf) {
328 image = xstate->mem_output_buf;
329 if (maxsz_p)
330 *maxsz_p = xstate->mem_output_size;
331 }
332 } else {
333 /* File is not compressed */
Denys Vlasenko10c0e912016-06-21 02:04:16 +0200334//FIXME: avoid seek
335 xlseek(xstate->src_fd, - xstate->signature_skipped, SEEK_CUR);
336 xstate->signature_skipped = 0;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100337 image = xmalloc_read(xstate->src_fd, maxsz_p);
338 }
339
340 if (!image)
341 bb_perror_msg("read error from '%s'", fname);
342 close(xstate->src_fd);
343 free(xstate);
344 return image;
345# else
346 /* This version forks a subprocess - much more expensive */
Denys Vlasenko59655072012-03-06 16:23:50 +0100347 int fd;
348 char *image;
349
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100350 fd = open_zipped(fname, /*fail_if_not_compressed:*/ 0);
Denys Vlasenko59655072012-03-06 16:23:50 +0100351 if (fd < 0)
352 return NULL;
353
354 image = xmalloc_read(fd, maxsz_p);
355 if (!image)
356 bb_perror_msg("read error from '%s'", fname);
357 close(fd);
Denys Vlasenko59655072012-03-06 16:23:50 +0100358 return image;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100359# endif
Denys Vlasenko59655072012-03-06 16:23:50 +0100360}
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100361
362#endif /* SEAMLESS_COMPRESSION */