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