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