"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 4 | */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 5 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 6 | #include "bb_archive.h" |
Bernhard Reutner-Fischer | cfb53df | 2006-04-02 21:50:01 +0000 | [diff] [blame] | 7 | |
Denys Vlasenko | e7800f3 | 2014-12-07 00:42:49 +0100 | [diff] [blame] | 8 | void FAST_FUNC init_transformer_state(transformer_state_t *xstate) |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 9 | { |
Denys Vlasenko | e7800f3 | 2014-12-07 00:42:49 +0100 | [diff] [blame] | 10 | memset(xstate, 0, sizeof(*xstate)); |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 11 | } |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 12 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 13 | int FAST_FUNC check_signature16(transformer_state_t *xstate, unsigned magic16) |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 14 | { |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 15 | if (!xstate->signature_skipped) { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 16 | uint16_t magic2; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 17 | if (full_read(xstate->src_fd, &magic2, 2) != 2 || magic2 != magic16) { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 18 | bb_error_msg("invalid magic"); |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 19 | return -1; |
| 20 | } |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 21 | xstate->signature_skipped = 2; |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 22 | } |
| 23 | return 0; |
| 24 | } |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 25 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 26 | ssize_t FAST_FUNC transformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize) |
| 27 | { |
| 28 | ssize_t nwrote; |
| 29 | |
| 30 | if (xstate->mem_output_size_max != 0) { |
| 31 | size_t pos = xstate->mem_output_size; |
| 32 | size_t size; |
| 33 | |
| 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); |
| 39 | nwrote = -1; |
| 40 | goto ret; |
| 41 | } |
Denys Vlasenko | cfcd239 | 2014-12-07 00:49:55 +0100 | [diff] [blame] | 42 | xstate->mem_output_buf = xrealloc(xstate->mem_output_buf, size + 1); |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 43 | memcpy(xstate->mem_output_buf + pos, buf, bufsize); |
Denys Vlasenko | cfcd239 | 2014-12-07 00:49:55 +0100 | [diff] [blame] | 44 | xstate->mem_output_buf[size] = '\0'; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 45 | nwrote = bufsize; |
| 46 | } else { |
| 47 | nwrote = full_write(xstate->dst_fd, buf, bufsize); |
| 48 | if (nwrote != (ssize_t)bufsize) { |
| 49 | bb_perror_msg("write"); |
| 50 | nwrote = -1; |
| 51 | goto ret; |
| 52 | } |
| 53 | } |
| 54 | ret: |
| 55 | return nwrote; |
| 56 | } |
| 57 | |
| 58 | ssize_t FAST_FUNC xtransformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize) |
| 59 | { |
| 60 | ssize_t nwrote = transformer_write(xstate, buf, bufsize); |
| 61 | if (nwrote != (ssize_t)bufsize) { |
| 62 | xfunc_die(); |
| 63 | } |
| 64 | return nwrote; |
| 65 | } |
| 66 | |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 67 | void check_errors_in_children(int signo) |
| 68 | { |
| 69 | int status; |
| 70 | |
| 71 | if (!signo) { |
| 72 | /* block waiting for any child */ |
| 73 | if (wait(&status) < 0) |
Denys Vlasenko | 577235d | 2013-02-28 16:38:25 +0100 | [diff] [blame] | 74 | //FIXME: check EINTR? |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 75 | return; /* probably there are no children */ |
| 76 | goto check_status; |
| 77 | } |
| 78 | |
| 79 | /* Wait for any child without blocking */ |
| 80 | for (;;) { |
| 81 | if (wait_any_nohang(&status) < 0) |
Denys Vlasenko | 577235d | 2013-02-28 16:38:25 +0100 | [diff] [blame] | 82 | //FIXME: check EINTR? |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 83 | /* wait failed?! I'm confused... */ |
| 84 | return; |
| 85 | check_status: |
Denys Vlasenko | 577235d | 2013-02-28 16:38:25 +0100 | [diff] [blame] | 86 | /*if (WIFEXITED(status) && WEXITSTATUS(status) == 0)*/ |
| 87 | /* On Linux, the above can be checked simply as: */ |
| 88 | if (status == 0) |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 89 | /* this child exited with 0 */ |
| 90 | continue; |
Denys Vlasenko | 577235d | 2013-02-28 16:38:25 +0100 | [diff] [blame] | 91 | /* Cannot happen: |
| 92 | if (!WIFSIGNALED(status) && !WIFEXITED(status)) ???; |
| 93 | */ |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 94 | bb_got_signal = 1; |
| 95 | } |
| 96 | } |
| 97 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 98 | /* transformer(), more than meets the eye */ |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 99 | #if BB_MMU |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 100 | void FAST_FUNC fork_transformer(int fd, |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 101 | int signature_skipped, |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 102 | IF_DESKTOP(long long) int FAST_FUNC (*transformer)(transformer_state_t *xstate) |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 103 | ) |
| 104 | #else |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 105 | void FAST_FUNC fork_transformer(int fd, const char *transform_prog) |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 106 | #endif |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 107 | { |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 108 | struct fd_pair fd_pipe; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 109 | int pid; |
| 110 | |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 111 | xpiped_pair(fd_pipe); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 112 | pid = BB_MMU ? xfork() : xvfork(); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 113 | if (pid == 0) { |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 114 | /* Child */ |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 115 | close(fd_pipe.rd); /* we don't want to read from the parent */ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 116 | // FIXME: error check? |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 117 | #if BB_MMU |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 118 | { |
Denys Vlasenko | ebfa9b5 | 2013-11-19 14:44:04 +0100 | [diff] [blame] | 119 | IF_DESKTOP(long long) int r; |
Denys Vlasenko | e7800f3 | 2014-12-07 00:42:49 +0100 | [diff] [blame] | 120 | transformer_state_t xstate; |
| 121 | init_transformer_state(&xstate); |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 122 | xstate.signature_skipped = signature_skipped; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 123 | xstate.src_fd = fd; |
| 124 | xstate.dst_fd = fd_pipe.wr; |
| 125 | r = transformer(&xstate); |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 126 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 127 | close(fd_pipe.wr); /* send EOF */ |
| 128 | close(fd); |
| 129 | } |
| 130 | /* must be _exit! bug was actually seen here */ |
Denys Vlasenko | ebfa9b5 | 2013-11-19 14:44:04 +0100 | [diff] [blame] | 131 | _exit(/*error if:*/ r < 0); |
Denis Vlasenko | 7e0fbf9 | 2007-09-04 19:33:22 +0000 | [diff] [blame] | 132 | } |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 133 | #else |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame] | 134 | { |
| 135 | char *argv[4]; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 136 | xmove_fd(fd, 0); |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 137 | xmove_fd(fd_pipe.wr, 1); |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame] | 138 | argv[0] = (char*)transform_prog; |
| 139 | argv[1] = (char*)"-cf"; |
| 140 | argv[2] = (char*)"-"; |
| 141 | argv[3] = NULL; |
| 142 | BB_EXECVP(transform_prog, argv); |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 143 | bb_perror_msg_and_die("can't execute '%s'", transform_prog); |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame] | 144 | } |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 145 | #endif |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 146 | /* notreached */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 147 | } |
Glenn L McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 148 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 149 | /* parent process */ |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 150 | close(fd_pipe.wr); /* don't want to write to the child */ |
| 151 | xmove_fd(fd_pipe.rd, fd); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 152 | } |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 153 | |
| 154 | |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 155 | #if SEAMLESS_COMPRESSION |
| 156 | |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 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. |
| 159 | */ |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 160 | static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_compressed) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 161 | { |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 162 | union { |
| 163 | uint8_t b[4]; |
| 164 | uint16_t b16[2]; |
| 165 | uint32_t b32[1]; |
| 166 | } magic; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 167 | transformer_state_t *xstate; |
| 168 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 169 | xstate = xzalloc(sizeof(*xstate)); |
| 170 | xstate->src_fd = fd; |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 171 | xstate->signature_skipped = 2; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 172 | |
| 173 | /* .gz and .bz2 both have 2-byte signature, and their |
| 174 | * unpack_XXX_stream wants this header skipped. */ |
| 175 | xread(fd, magic.b16, sizeof(magic.b16[0])); |
| 176 | if (ENABLE_FEATURE_SEAMLESS_GZ |
| 177 | && magic.b16[0] == GZIP_MAGIC |
| 178 | ) { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 179 | xstate->xformer = unpack_gz_stream; |
| 180 | USE_FOR_NOMMU(xstate->xformer_prog = "gunzip";) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 181 | goto found_magic; |
| 182 | } |
Thiago Jung Bauermann | b4059f6 | 2015-05-03 18:40:12 +0200 | [diff] [blame] | 183 | if (ENABLE_FEATURE_SEAMLESS_Z |
| 184 | && magic.b16[0] == COMPRESS_MAGIC |
| 185 | ) { |
| 186 | xstate->xformer = unpack_Z_stream; |
| 187 | USE_FOR_NOMMU(xstate->xformer_prog = "uncompress";) |
| 188 | goto found_magic; |
| 189 | } |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 190 | if (ENABLE_FEATURE_SEAMLESS_BZ2 |
| 191 | && magic.b16[0] == BZIP2_MAGIC |
| 192 | ) { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 193 | xstate->xformer = unpack_bz2_stream; |
| 194 | USE_FOR_NOMMU(xstate->xformer_prog = "bunzip2";) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 195 | goto found_magic; |
| 196 | } |
| 197 | if (ENABLE_FEATURE_SEAMLESS_XZ |
| 198 | && magic.b16[0] == XZ_MAGIC1 |
| 199 | ) { |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 200 | xstate->signature_skipped = 6; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 201 | xread(fd, magic.b32, sizeof(magic.b32[0])); |
| 202 | if (magic.b32[0] == XZ_MAGIC2) { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 203 | xstate->xformer = unpack_xz_stream; |
| 204 | USE_FOR_NOMMU(xstate->xformer_prog = "unxz";) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 205 | goto found_magic; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* No known magic seen */ |
Denys Vlasenko | 640ce3d | 2014-02-02 02:06:38 +0100 | [diff] [blame] | 210 | if (fail_if_not_compressed) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 211 | bb_error_msg_and_die("no gzip" |
| 212 | IF_FEATURE_SEAMLESS_BZ2("/bzip2") |
| 213 | IF_FEATURE_SEAMLESS_XZ("/xz") |
| 214 | " magic"); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 215 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 216 | /* Some callers expect this function to "consume" fd |
| 217 | * even if data is not compressed. In this case, |
| 218 | * we return a state with trivial transformer. |
| 219 | */ |
| 220 | // USE_FOR_MMU(xstate->xformer = copy_stream;) |
| 221 | // USE_FOR_NOMMU(xstate->xformer_prog = "cat";) |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 222 | |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 223 | found_magic: |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 224 | return xstate; |
| 225 | } |
| 226 | |
Denys Vlasenko | 0cf64c8 | 2017-08-10 10:35:08 +0200 | [diff] [blame] | 227 | static void fork_transformer_and_free(transformer_state_t *xstate) |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 228 | { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 229 | # if BB_MMU |
| 230 | fork_transformer_with_no_sig(xstate->src_fd, xstate->xformer); |
| 231 | # else |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 232 | /* NOMMU version of fork_transformer execs |
| 233 | * an external unzipper that wants |
| 234 | * file position at the start of the file. |
| 235 | */ |
Ron Yorston | e837a0d | 2017-08-22 11:34:03 +0100 | [diff] [blame] | 236 | xlseek(xstate->src_fd, - xstate->signature_skipped, SEEK_CUR); |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 237 | xstate->signature_skipped = 0; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 238 | fork_transformer_with_sig(xstate->src_fd, xstate->xformer, xstate->xformer_prog); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 239 | # endif |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 240 | free(xstate); |
Denys Vlasenko | 0cf64c8 | 2017-08-10 10:35:08 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* Used by e.g. rpm which gives us a fd without filename, |
| 244 | * thus we can't guess the format from filename's extension. |
| 245 | */ |
| 246 | int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed) |
| 247 | { |
| 248 | transformer_state_t *xstate = setup_transformer_on_fd(fd, fail_if_not_compressed); |
| 249 | |
| 250 | if (!xstate->xformer) { |
| 251 | free(xstate); |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | fork_transformer_and_free(xstate); |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 256 | return 0; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 257 | } |
Denys Vlasenko | 0cf64c8 | 2017-08-10 10:35:08 +0200 | [diff] [blame] | 258 | #if ENABLE_FEATURE_SEAMLESS_LZMA |
| 259 | /* ...and custom version for LZMA */ |
| 260 | void FAST_FUNC setup_lzma_on_fd(int fd) |
| 261 | { |
| 262 | transformer_state_t *xstate = xzalloc(sizeof(*xstate)); |
| 263 | xstate->src_fd = fd; |
| 264 | xstate->xformer = unpack_lzma_stream; |
| 265 | USE_FOR_NOMMU(xstate->xformer_prog = "unlzma";) |
| 266 | fork_transformer_and_free(xstate); |
| 267 | } |
| 268 | #endif |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 269 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 270 | static transformer_state_t *open_transformer(const char *fname, int fail_if_not_compressed) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 271 | { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 272 | transformer_state_t *xstate; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 273 | int fd; |
| 274 | |
| 275 | fd = open(fname, O_RDONLY); |
| 276 | if (fd < 0) |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 277 | return NULL; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 278 | |
Denys Vlasenko | 7c47b56 | 2014-01-10 14:06:57 +0100 | [diff] [blame] | 279 | if (ENABLE_FEATURE_SEAMLESS_LZMA) { |
| 280 | /* .lzma has no header/signature, can only detect it by extension */ |
| 281 | char *sfx = strrchr(fname, '.'); |
| 282 | if (sfx && strcmp(sfx+1, "lzma") == 0) { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 283 | xstate = xzalloc(sizeof(*xstate)); |
| 284 | xstate->src_fd = fd; |
| 285 | xstate->xformer = unpack_lzma_stream; |
| 286 | USE_FOR_NOMMU(xstate->xformer_prog = "unlzma";) |
| 287 | return xstate; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 291 | xstate = setup_transformer_on_fd(fd, fail_if_not_compressed); |
| 292 | |
| 293 | return xstate; |
| 294 | } |
| 295 | |
| 296 | int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed) |
| 297 | { |
| 298 | int fd; |
| 299 | transformer_state_t *xstate; |
| 300 | |
| 301 | xstate = open_transformer(fname, fail_if_not_compressed); |
| 302 | if (!xstate) |
| 303 | return -1; |
| 304 | |
| 305 | fd = xstate->src_fd; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 306 | # if BB_MMU |
Denys Vlasenko | df3ec0e | 2016-06-20 11:42:00 +0200 | [diff] [blame] | 307 | if (xstate->xformer) { |
| 308 | fork_transformer_with_no_sig(fd, xstate->xformer); |
| 309 | } else { |
| 310 | /* the file is not compressed */ |
Denys Vlasenko | 984b0a6 | 2016-06-20 11:06:42 +0200 | [diff] [blame] | 311 | xlseek(fd, - xstate->signature_skipped, SEEK_CUR); |
| 312 | xstate->signature_skipped = 0; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 313 | } |
Denys Vlasenko | df3ec0e | 2016-06-20 11:42:00 +0200 | [diff] [blame] | 314 | # else |
| 315 | /* NOMMU can't avoid the seek :( */ |
| 316 | xlseek(fd, - xstate->signature_skipped, SEEK_CUR); |
| 317 | xstate->signature_skipped = 0; |
| 318 | if (xstate->xformer) { |
| 319 | fork_transformer_with_sig(fd, xstate->xformer, xstate->xformer_prog); |
Denys Vlasenko | e24e886 | 2016-06-20 16:28:53 +0200 | [diff] [blame] | 320 | } /* else: the file is not compressed */ |
Denys Vlasenko | df3ec0e | 2016-06-20 11:42:00 +0200 | [diff] [blame] | 321 | # endif |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 322 | |
| 323 | free(xstate); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 324 | return fd; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) |
| 328 | { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 329 | # if 1 |
| 330 | transformer_state_t *xstate; |
| 331 | char *image; |
| 332 | |
| 333 | xstate = open_transformer(fname, /*fail_if_not_compressed:*/ 0); |
| 334 | if (!xstate) /* file open error */ |
| 335 | return NULL; |
| 336 | |
| 337 | image = NULL; |
| 338 | if (xstate->xformer) { |
| 339 | /* In-memory decompression */ |
| 340 | xstate->mem_output_size_max = maxsz_p ? *maxsz_p : (size_t)(INT_MAX - 4095); |
| 341 | xstate->xformer(xstate); |
| 342 | if (xstate->mem_output_buf) { |
| 343 | image = xstate->mem_output_buf; |
| 344 | if (maxsz_p) |
| 345 | *maxsz_p = xstate->mem_output_size; |
| 346 | } |
| 347 | } else { |
| 348 | /* File is not compressed */ |
Denys Vlasenko | 10c0e91 | 2016-06-21 02:04:16 +0200 | [diff] [blame] | 349 | //FIXME: avoid seek |
| 350 | xlseek(xstate->src_fd, - xstate->signature_skipped, SEEK_CUR); |
| 351 | xstate->signature_skipped = 0; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 352 | image = xmalloc_read(xstate->src_fd, maxsz_p); |
| 353 | } |
| 354 | |
| 355 | if (!image) |
| 356 | bb_perror_msg("read error from '%s'", fname); |
| 357 | close(xstate->src_fd); |
| 358 | free(xstate); |
| 359 | return image; |
| 360 | # else |
| 361 | /* This version forks a subprocess - much more expensive */ |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 362 | int fd; |
| 363 | char *image; |
| 364 | |
Denys Vlasenko | 640ce3d | 2014-02-02 02:06:38 +0100 | [diff] [blame] | 365 | fd = open_zipped(fname, /*fail_if_not_compressed:*/ 0); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 366 | if (fd < 0) |
| 367 | return NULL; |
| 368 | |
| 369 | image = xmalloc_read(fd, maxsz_p); |
| 370 | if (!image) |
| 371 | bb_perror_msg("read error from '%s'", fname); |
| 372 | close(fd); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 373 | return image; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 374 | # endif |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 375 | } |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 376 | |
| 377 | #endif /* SEAMLESS_COMPRESSION */ |