open_transformer.c 9.2 KB

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