open_transformer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_simple_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_simple_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. transformer_state_t *xstate;
  148. xstate = xzalloc(sizeof(*xstate));
  149. xstate->src_fd = fd;
  150. /* .gz and .bz2 both have 2-byte signature, and their
  151. * unpack_XXX_stream wants this header skipped. */
  152. xstate->signature_skipped = 2;
  153. xread(fd, xstate->magic.b16, 2);
  154. if (ENABLE_FEATURE_SEAMLESS_GZ
  155. && xstate->magic.b16[0] == GZIP_MAGIC
  156. ) {
  157. xstate->xformer = unpack_gz_stream;
  158. USE_FOR_NOMMU(xstate->xformer_prog = "gunzip";)
  159. goto found_magic;
  160. }
  161. if (ENABLE_FEATURE_SEAMLESS_Z
  162. && xstate->magic.b16[0] == COMPRESS_MAGIC
  163. ) {
  164. xstate->xformer = unpack_Z_stream;
  165. USE_FOR_NOMMU(xstate->xformer_prog = "uncompress";)
  166. goto found_magic;
  167. }
  168. if (ENABLE_FEATURE_SEAMLESS_BZ2
  169. && xstate->magic.b16[0] == BZIP2_MAGIC
  170. ) {
  171. xstate->xformer = unpack_bz2_stream;
  172. USE_FOR_NOMMU(xstate->xformer_prog = "bunzip2";)
  173. goto found_magic;
  174. }
  175. if (ENABLE_FEATURE_SEAMLESS_XZ
  176. && xstate->magic.b16[0] == XZ_MAGIC1
  177. ) {
  178. uint32_t v32;
  179. xstate->signature_skipped = 6;
  180. xread(fd, &xstate->magic.b16[1], 4);
  181. move_from_unaligned32(v32, &xstate->magic.b16[1]);
  182. if (v32 == XZ_MAGIC2) {
  183. xstate->xformer = unpack_xz_stream;
  184. USE_FOR_NOMMU(xstate->xformer_prog = "unxz";)
  185. goto found_magic;
  186. }
  187. }
  188. /* No known magic seen */
  189. if (fail_if_not_compressed)
  190. bb_simple_error_msg_and_die("no gzip"
  191. IF_FEATURE_SEAMLESS_BZ2("/bzip2")
  192. IF_FEATURE_SEAMLESS_XZ("/xz")
  193. " magic");
  194. /* Some callers expect this function to "consume" fd
  195. * even if data is not compressed. In this case,
  196. * we return a state with trivial transformer.
  197. */
  198. // USE_FOR_MMU(xstate->xformer = copy_stream;)
  199. // USE_FOR_NOMMU(xstate->xformer_prog = "cat";)
  200. found_magic:
  201. return xstate;
  202. }
  203. static void fork_transformer_and_free(transformer_state_t *xstate)
  204. {
  205. # if BB_MMU
  206. fork_transformer_with_no_sig(xstate->src_fd, xstate->xformer);
  207. # else
  208. /* NOMMU version of fork_transformer execs
  209. * an external unzipper that wants
  210. * file position at the start of the file.
  211. */
  212. xlseek(xstate->src_fd, - xstate->signature_skipped, SEEK_CUR);
  213. xstate->signature_skipped = 0;
  214. fork_transformer_with_sig(xstate->src_fd, xstate->xformer, xstate->xformer_prog);
  215. # endif
  216. free(xstate);
  217. }
  218. /* Used by e.g. rpm which gives us a fd without filename,
  219. * thus we can't guess the format from filename's extension.
  220. */
  221. int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed)
  222. {
  223. transformer_state_t *xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
  224. if (!xstate->xformer) {
  225. free(xstate);
  226. return 1;
  227. }
  228. fork_transformer_and_free(xstate);
  229. return 0;
  230. }
  231. #if ENABLE_FEATURE_SEAMLESS_LZMA
  232. /* ...and custom version for LZMA */
  233. void FAST_FUNC setup_lzma_on_fd(int fd)
  234. {
  235. transformer_state_t *xstate = xzalloc(sizeof(*xstate));
  236. xstate->src_fd = fd;
  237. xstate->xformer = unpack_lzma_stream;
  238. USE_FOR_NOMMU(xstate->xformer_prog = "unlzma";)
  239. fork_transformer_and_free(xstate);
  240. }
  241. #endif
  242. static transformer_state_t *open_transformer(const char *fname, int fail_if_not_compressed)
  243. {
  244. transformer_state_t *xstate;
  245. int fd;
  246. fd = open(fname, O_RDONLY);
  247. if (fd < 0)
  248. return NULL;
  249. if (ENABLE_FEATURE_SEAMLESS_LZMA) {
  250. /* .lzma has no header/signature, can only detect it by extension */
  251. if (is_suffixed_with(fname, ".lzma")) {
  252. xstate = xzalloc(sizeof(*xstate));
  253. xstate->src_fd = fd;
  254. xstate->xformer = unpack_lzma_stream;
  255. USE_FOR_NOMMU(xstate->xformer_prog = "unlzma";)
  256. return xstate;
  257. }
  258. }
  259. xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
  260. return xstate;
  261. }
  262. int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed)
  263. {
  264. int fd;
  265. transformer_state_t *xstate;
  266. xstate = open_transformer(fname, fail_if_not_compressed);
  267. if (!xstate)
  268. return -1;
  269. fd = xstate->src_fd;
  270. # if BB_MMU
  271. if (xstate->xformer) {
  272. fork_transformer_with_no_sig(fd, xstate->xformer);
  273. } else {
  274. /* the file is not compressed */
  275. xlseek(fd, - xstate->signature_skipped, SEEK_CUR);
  276. xstate->signature_skipped = 0;
  277. }
  278. # else
  279. /* NOMMU can't avoid the seek :( */
  280. xlseek(fd, - xstate->signature_skipped, SEEK_CUR);
  281. xstate->signature_skipped = 0;
  282. if (xstate->xformer) {
  283. fork_transformer_with_sig(fd, xstate->xformer, xstate->xformer_prog);
  284. } /* else: the file is not compressed */
  285. # endif
  286. free(xstate);
  287. return fd;
  288. }
  289. void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
  290. {
  291. # if 1
  292. transformer_state_t *xstate;
  293. char *image;
  294. xstate = open_transformer(fname, /*fail_if_not_compressed:*/ 0);
  295. if (!xstate) /* file open error */
  296. return NULL;
  297. image = NULL;
  298. if (xstate->xformer) {
  299. /* In-memory decompression */
  300. xstate->mem_output_size_max = maxsz_p ? *maxsz_p : (size_t)(INT_MAX - 4095);
  301. xstate->xformer(xstate);
  302. if (xstate->mem_output_buf) {
  303. image = xstate->mem_output_buf;
  304. if (maxsz_p)
  305. *maxsz_p = xstate->mem_output_size;
  306. }
  307. } else {
  308. /* File is not compressed.
  309. * We already read first few bytes, account for that.
  310. * Example where it happens:
  311. * "modinfo MODULE.ko" (not compressed)
  312. * open("MODULE.ko", O_RDONLY|O_LARGEFILE) = 4
  313. * read(4, "\177E", 2) = 2
  314. * fstat64(4, ...)
  315. * mmap(...)
  316. * read(4, "LF\2\1\1\0\0\0\0"...
  317. * ...and we avoided seeking on the fd! :)
  318. */
  319. image = xmalloc_read_with_initial_buf(
  320. xstate->src_fd,
  321. maxsz_p,
  322. xmemdup(&xstate->magic, xstate->signature_skipped),
  323. xstate->signature_skipped
  324. );
  325. xstate->signature_skipped = 0;
  326. }
  327. if (!image)
  328. bb_perror_msg("read error from '%s'", fname);
  329. close(xstate->src_fd);
  330. free(xstate);
  331. return image;
  332. # else
  333. /* This version forks a subprocess - much more expensive */
  334. int fd;
  335. char *image;
  336. fd = open_zipped(fname, /*fail_if_not_compressed:*/ 0);
  337. if (fd < 0)
  338. return NULL;
  339. image = xmalloc_read(fd, maxsz_p);
  340. if (!image)
  341. bb_perror_msg("read error from '%s'", fname);
  342. close(fd);
  343. return image;
  344. # endif
  345. }
  346. #endif /* SEAMLESS_COMPRESSION */