bbunzip.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Common code for gunzip-like applets
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. */
  7. #include "libbb.h"
  8. #include "bb_archive.h"
  9. /* lzop_main() uses bbunpack(), need this: */
  10. //kbuild:lib-$(CONFIG_LZOP) += bbunzip.o
  11. /* Note: must be kept in sync with archival/lzop.c */
  12. enum {
  13. OPT_STDOUT = 1 << 0,
  14. OPT_FORCE = 1 << 1,
  15. /* only some decompressors: */
  16. OPT_VERBOSE = 1 << 2,
  17. OPT_QUIET = 1 << 3,
  18. OPT_DECOMPRESS = 1 << 4,
  19. OPT_TEST = 1 << 5,
  20. SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
  21. };
  22. static
  23. int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
  24. {
  25. int fd = open3_or_warn(filename, flags, mode);
  26. if (fd < 0) {
  27. return 1;
  28. }
  29. xmove_fd(fd, to_fd);
  30. return 0;
  31. }
  32. char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
  33. {
  34. return xasprintf("%s.%s", filename, expected_ext);
  35. }
  36. int FAST_FUNC bbunpack(char **argv,
  37. IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_state_t *xstate),
  38. char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
  39. const char *expected_ext
  40. )
  41. {
  42. struct stat stat_buf;
  43. IF_DESKTOP(long long) int status = 0;
  44. char *filename, *new_name;
  45. smallint exitcode = 0;
  46. transformer_state_t xstate;
  47. do {
  48. /* NB: new_name is *maybe* malloc'ed! */
  49. new_name = NULL;
  50. filename = *argv; /* can be NULL - 'streaming' bunzip2 */
  51. if (filename && LONE_DASH(filename))
  52. filename = NULL;
  53. /* Open src */
  54. if (filename) {
  55. if (!(option_mask32 & SEAMLESS_MAGIC)) {
  56. if (stat(filename, &stat_buf) != 0) {
  57. err_name:
  58. bb_simple_perror_msg(filename);
  59. err:
  60. exitcode = 1;
  61. goto free_name;
  62. }
  63. if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
  64. goto err;
  65. } else {
  66. /* "clever zcat" with FILE */
  67. /* fail_if_not_compressed because zcat refuses uncompressed input */
  68. int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
  69. if (fd < 0)
  70. goto err_name;
  71. xmove_fd(fd, STDIN_FILENO);
  72. }
  73. } else
  74. if (option_mask32 & SEAMLESS_MAGIC) {
  75. /* "clever zcat" on stdin */
  76. if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
  77. goto err;
  78. }
  79. /* Special cases: test, stdout */
  80. if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
  81. if (option_mask32 & OPT_TEST)
  82. if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
  83. xfunc_die();
  84. filename = NULL;
  85. }
  86. /* Open dst if we are going to unpack to file */
  87. if (filename) {
  88. new_name = make_new_name(filename, expected_ext);
  89. if (!new_name) {
  90. bb_error_msg("%s: unknown suffix - ignored", filename);
  91. goto err;
  92. }
  93. /* -f: overwrite existing output files */
  94. if (option_mask32 & OPT_FORCE) {
  95. unlink(new_name);
  96. }
  97. /* O_EXCL: "real" bunzip2 doesn't overwrite files */
  98. /* GNU gunzip does not bail out, but goes to next file */
  99. if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
  100. stat_buf.st_mode))
  101. goto err;
  102. }
  103. /* Check that the input is sane */
  104. if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
  105. bb_error_msg_and_die("compressed data not read from terminal, "
  106. "use -f to force it");
  107. }
  108. if (!(option_mask32 & SEAMLESS_MAGIC)) {
  109. init_transformer_state(&xstate);
  110. xstate.check_signature = 1;
  111. /*xstate.src_fd = STDIN_FILENO; - already is */
  112. xstate.dst_fd = STDOUT_FILENO;
  113. status = unpacker(&xstate);
  114. if (status < 0)
  115. exitcode = 1;
  116. } else {
  117. if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
  118. /* Disk full, tty closed, etc. No point in continuing */
  119. xfunc_die();
  120. }
  121. if (!(option_mask32 & OPT_STDOUT))
  122. xclose(STDOUT_FILENO); /* with error check! */
  123. if (filename) {
  124. char *del = new_name;
  125. if (status >= 0) {
  126. unsigned new_name_len;
  127. /* TODO: restore other things? */
  128. if (xstate.mtime != 0) {
  129. struct timeval times[2];
  130. times[1].tv_sec = times[0].tv_sec = xstate.mtime;
  131. times[1].tv_usec = times[0].tv_usec = 0;
  132. /* Note: we closed it first.
  133. * On some systems calling utimes
  134. * then closing resets the mtime
  135. * back to current time. */
  136. utimes(new_name, times); /* ignoring errors */
  137. }
  138. if (ENABLE_DESKTOP)
  139. new_name_len = strlen(new_name);
  140. /* Restore source filename (unless tgz -> tar case) */
  141. if (new_name == filename) {
  142. new_name_len = strlen(filename);
  143. filename[new_name_len] = '.';
  144. }
  145. /* Extreme bloat for gunzip compat */
  146. /* Some users do want this info... */
  147. if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
  148. unsigned percent = status
  149. ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
  150. : 0;
  151. fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
  152. filename,
  153. 100u - percent,
  154. new_name_len, new_name
  155. );
  156. }
  157. /* Delete _source_ file */
  158. del = filename;
  159. }
  160. xunlink(del);
  161. free_name:
  162. if (new_name != filename)
  163. free(new_name);
  164. }
  165. } while (*argv && *++argv);
  166. if (option_mask32 & OPT_STDOUT)
  167. xclose(STDOUT_FILENO); /* with error check! */
  168. return exitcode;
  169. }
  170. #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
  171. static
  172. char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
  173. {
  174. char *extension = strrchr(filename, '.');
  175. if (!extension || strcmp(extension + 1, expected_ext) != 0) {
  176. /* Mimic GNU gunzip - "real" bunzip2 tries to */
  177. /* unpack file anyway, to file.out */
  178. return NULL;
  179. }
  180. *extension = '\0';
  181. return filename;
  182. }
  183. #endif
  184. /*
  185. * Uncompress applet for busybox (c) 2002 Glenn McGrath
  186. *
  187. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  188. */
  189. //usage:#define uncompress_trivial_usage
  190. //usage: "[-cf] [FILE]..."
  191. //usage:#define uncompress_full_usage "\n\n"
  192. //usage: "Decompress .Z file[s]\n"
  193. //usage: "\n -c Write to stdout"
  194. //usage: "\n -f Overwrite"
  195. //config:config UNCOMPRESS
  196. //config: bool "uncompress"
  197. //config: default n
  198. //config: help
  199. //config: uncompress is used to decompress archives created by compress.
  200. //config: Not much used anymore, replaced by gzip/gunzip.
  201. //applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
  202. //kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
  203. #if ENABLE_UNCOMPRESS
  204. int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  205. int uncompress_main(int argc UNUSED_PARAM, char **argv)
  206. {
  207. getopt32(argv, "cf");
  208. argv += optind;
  209. return bbunpack(argv, unpack_Z_stream, make_new_name_generic, "Z");
  210. }
  211. #endif
  212. /*
  213. * Gzip implementation for busybox
  214. *
  215. * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
  216. *
  217. * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
  218. * based on gzip sources
  219. *
  220. * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
  221. * well as stdin/stdout, and to generally behave itself wrt command line
  222. * handling.
  223. *
  224. * General cleanup to better adhere to the style guide and make use of standard
  225. * busybox functions by Glenn McGrath
  226. *
  227. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  228. *
  229. * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  230. * Copyright (C) 1992-1993 Jean-loup Gailly
  231. * The unzip code was written and put in the public domain by Mark Adler.
  232. * Portions of the lzw code are derived from the public domain 'compress'
  233. * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  234. * Ken Turkowski, Dave Mack and Peter Jannesen.
  235. */
  236. //usage:#define gunzip_trivial_usage
  237. //usage: "[-cft] [FILE]..."
  238. //usage:#define gunzip_full_usage "\n\n"
  239. //usage: "Decompress FILEs (or stdin)\n"
  240. //usage: "\n -c Write to stdout"
  241. //usage: "\n -f Force"
  242. //usage: "\n -t Test file integrity"
  243. //usage:
  244. //usage:#define gunzip_example_usage
  245. //usage: "$ ls -la /tmp/BusyBox*\n"
  246. //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
  247. //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
  248. //usage: "$ ls -la /tmp/BusyBox*\n"
  249. //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
  250. //usage:
  251. //usage:#define zcat_trivial_usage
  252. //usage: "[FILE]..."
  253. //usage:#define zcat_full_usage "\n\n"
  254. //usage: "Decompress to stdout"
  255. //config:config GUNZIP
  256. //config: bool "gunzip"
  257. //config: default y
  258. //config: help
  259. //config: gunzip is used to decompress archives created by gzip.
  260. //config: You can use the `-t' option to test the integrity of
  261. //config: an archive, without decompressing it.
  262. //applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
  263. //applet:IF_GUNZIP(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
  264. //kbuild:lib-$(CONFIG_GZIP) += bbunzip.o
  265. //kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
  266. #if ENABLE_GUNZIP
  267. static
  268. char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
  269. {
  270. char *extension = strrchr(filename, '.');
  271. if (!extension)
  272. return NULL;
  273. extension++;
  274. if (strcmp(extension, "tgz" + 1) == 0
  275. #if ENABLE_FEATURE_SEAMLESS_Z
  276. || (extension[0] == 'Z' && extension[1] == '\0')
  277. #endif
  278. ) {
  279. extension[-1] = '\0';
  280. } else if (strcmp(extension, "tgz") == 0) {
  281. filename = xstrdup(filename);
  282. extension = strrchr(filename, '.');
  283. extension[2] = 'a';
  284. extension[3] = 'r';
  285. } else {
  286. return NULL;
  287. }
  288. return filename;
  289. }
  290. /*
  291. * Linux kernel build uses gzip -d -n. We accept and ignore it.
  292. * Man page says:
  293. * -n --no-name
  294. * gzip: do not save the original file name and time stamp.
  295. * (The original name is always saved if the name had to be truncated.)
  296. * gunzip: do not restore the original file name/time even if present
  297. * (remove only the gzip suffix from the compressed file name).
  298. * This option is the default when decompressing.
  299. * -N --name
  300. * gzip: always save the original file name and time stamp (this is the default)
  301. * gunzip: restore the original file name and time stamp if present.
  302. */
  303. int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  304. int gunzip_main(int argc UNUSED_PARAM, char **argv)
  305. {
  306. getopt32(argv, "cfvqdtn");
  307. argv += optind;
  308. /* If called as zcat...
  309. * Normally, "zcat" is just "gunzip -c".
  310. * But if seamless magic is enabled, then we are much more clever.
  311. */
  312. if (applet_name[1] == 'c')
  313. option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
  314. return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
  315. }
  316. #endif
  317. /*
  318. * Modified for busybox by Glenn McGrath
  319. * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
  320. *
  321. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  322. */
  323. //usage:#define bunzip2_trivial_usage
  324. //usage: "[-cf] [FILE]..."
  325. //usage:#define bunzip2_full_usage "\n\n"
  326. //usage: "Decompress FILEs (or stdin)\n"
  327. //usage: "\n -c Write to stdout"
  328. //usage: "\n -f Force"
  329. //usage:#define bzcat_trivial_usage
  330. //usage: "[FILE]..."
  331. //usage:#define bzcat_full_usage "\n\n"
  332. //usage: "Decompress to stdout"
  333. //config:config BUNZIP2
  334. //config: bool "bunzip2"
  335. //config: default y
  336. //config: help
  337. //config: bunzip2 is a compression utility using the Burrows-Wheeler block
  338. //config: sorting text compression algorithm, and Huffman coding. Compression
  339. //config: is generally considerably better than that achieved by more
  340. //config: conventional LZ77/LZ78-based compressors, and approaches the
  341. //config: performance of the PPM family of statistical compressors.
  342. //config:
  343. //config: Unless you have a specific application which requires bunzip2, you
  344. //config: should probably say N here.
  345. //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
  346. //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
  347. //kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
  348. //kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
  349. #if ENABLE_BUNZIP2
  350. int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  351. int bunzip2_main(int argc UNUSED_PARAM, char **argv)
  352. {
  353. getopt32(argv, "cfvqdt");
  354. argv += optind;
  355. if (applet_name[2] == 'c') /* bzcat */
  356. option_mask32 |= OPT_STDOUT;
  357. return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
  358. }
  359. #endif
  360. /*
  361. * Small lzma deflate implementation.
  362. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  363. *
  364. * Based on bunzip.c from busybox
  365. *
  366. * Licensed under GPLv2, see file LICENSE in this source tree.
  367. */
  368. //usage:#define unlzma_trivial_usage
  369. //usage: "[-cf] [FILE]..."
  370. //usage:#define unlzma_full_usage "\n\n"
  371. //usage: "Decompress FILE (or stdin)\n"
  372. //usage: "\n -c Write to stdout"
  373. //usage: "\n -f Force"
  374. //usage:
  375. //usage:#define lzma_trivial_usage
  376. //usage: "-d [-cf] [FILE]..."
  377. //usage:#define lzma_full_usage "\n\n"
  378. //usage: "Decompress FILE (or stdin)\n"
  379. //usage: "\n -d Decompress"
  380. //usage: "\n -c Write to stdout"
  381. //usage: "\n -f Force"
  382. //usage:
  383. //usage:#define lzcat_trivial_usage
  384. //usage: "[FILE]..."
  385. //usage:#define lzcat_full_usage "\n\n"
  386. //usage: "Decompress to stdout"
  387. //usage:
  388. //usage:#define unxz_trivial_usage
  389. //usage: "[-cf] [FILE]..."
  390. //usage:#define unxz_full_usage "\n\n"
  391. //usage: "Decompress FILE (or stdin)\n"
  392. //usage: "\n -c Write to stdout"
  393. //usage: "\n -f Force"
  394. //usage:
  395. //usage:#define xz_trivial_usage
  396. //usage: "-d [-cf] [FILE]..."
  397. //usage:#define xz_full_usage "\n\n"
  398. //usage: "Decompress FILE (or stdin)\n"
  399. //usage: "\n -d Decompress"
  400. //usage: "\n -c Write to stdout"
  401. //usage: "\n -f Force"
  402. //usage:
  403. //usage:#define xzcat_trivial_usage
  404. //usage: "[FILE]..."
  405. //usage:#define xzcat_full_usage "\n\n"
  406. //usage: "Decompress to stdout"
  407. //config:config UNLZMA
  408. //config: bool "unlzma"
  409. //config: default y
  410. //config: help
  411. //config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
  412. //config: compression algorithm, and range coding. Compression
  413. //config: is generally considerably better than that achieved by the bzip2
  414. //config: compressors.
  415. //config:
  416. //config: The BusyBox unlzma applet is limited to decompression only.
  417. //config: On an x86 system, this applet adds about 4K.
  418. //config:
  419. //config:config FEATURE_LZMA_FAST
  420. //config: bool "Optimize unlzma for speed"
  421. //config: default n
  422. //config: depends on UNLZMA
  423. //config: help
  424. //config: This option reduces decompression time by about 25% at the cost of
  425. //config: a 1K bigger binary.
  426. //config:
  427. //config:config LZMA
  428. //config: bool "Provide lzma alias which supports only unpacking"
  429. //config: default y
  430. //config: depends on UNLZMA
  431. //config: help
  432. //config: Enable this option if you want commands like "lzma -d" to work.
  433. //config: IOW: you'll get lzma applet, but it will always require -d option.
  434. //applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
  435. //applet:IF_UNLZMA(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
  436. //applet:IF_LZMA(APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
  437. //kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
  438. #if ENABLE_UNLZMA
  439. int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  440. int unlzma_main(int argc UNUSED_PARAM, char **argv)
  441. {
  442. IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
  443. # if ENABLE_LZMA
  444. /* lzma without -d or -t? */
  445. if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
  446. bb_show_usage();
  447. # endif
  448. /* lzcat? */
  449. if (applet_name[2] == 'c')
  450. option_mask32 |= OPT_STDOUT;
  451. argv += optind;
  452. return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
  453. }
  454. #endif
  455. //config:config UNXZ
  456. //config: bool "unxz"
  457. //config: default y
  458. //config: help
  459. //config: unxz is a unlzma successor.
  460. //config:
  461. //config:config XZ
  462. //config: bool "Provide xz alias which supports only unpacking"
  463. //config: default y
  464. //config: depends on UNXZ
  465. //config: help
  466. //config: Enable this option if you want commands like "xz -d" to work.
  467. //config: IOW: you'll get xz applet, but it will always require -d option.
  468. //applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
  469. //applet:IF_UNXZ(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
  470. //applet:IF_XZ(APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
  471. //kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
  472. #if ENABLE_UNXZ
  473. int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  474. int unxz_main(int argc UNUSED_PARAM, char **argv)
  475. {
  476. IF_XZ(int opts =) getopt32(argv, "cfvqdt");
  477. # if ENABLE_XZ
  478. /* xz without -d or -t? */
  479. if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
  480. bb_show_usage();
  481. # endif
  482. /* xzcat? */
  483. if (applet_name[2] == 'c')
  484. option_mask32 |= OPT_STDOUT;
  485. argv += optind;
  486. return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");
  487. }
  488. #endif