bbunzip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 "archive.h"
  9. enum {
  10. OPT_STDOUT = 1 << 0,
  11. OPT_FORCE = 1 << 1,
  12. /* only some decompressors: */
  13. OPT_VERBOSE = 1 << 2,
  14. OPT_DECOMPRESS = 1 << 3,
  15. OPT_TEST = 1 << 4,
  16. };
  17. static
  18. int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
  19. {
  20. int fd = open3_or_warn(filename, flags, mode);
  21. if (fd < 0) {
  22. return 1;
  23. }
  24. xmove_fd(fd, to_fd);
  25. return 0;
  26. }
  27. char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
  28. {
  29. return xasprintf("%s.%s", filename, expected_ext);
  30. }
  31. int FAST_FUNC bbunpack(char **argv,
  32. IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(unpack_info_t *info),
  33. char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
  34. const char *expected_ext
  35. )
  36. {
  37. struct stat stat_buf;
  38. IF_DESKTOP(long long) int status;
  39. char *filename, *new_name;
  40. smallint exitcode = 0;
  41. unpack_info_t info;
  42. do {
  43. /* NB: new_name is *maybe* malloc'ed! */
  44. new_name = NULL;
  45. filename = *argv; /* can be NULL - 'streaming' bunzip2 */
  46. if (filename && LONE_DASH(filename))
  47. filename = NULL;
  48. /* Open src */
  49. if (filename) {
  50. if (stat(filename, &stat_buf) != 0) {
  51. bb_simple_perror_msg(filename);
  52. err:
  53. exitcode = 1;
  54. goto free_name;
  55. }
  56. if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
  57. goto err;
  58. }
  59. /* Special cases: test, stdout */
  60. if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
  61. if (option_mask32 & OPT_TEST)
  62. if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
  63. goto err;
  64. filename = NULL;
  65. }
  66. /* Open dst if we are going to unpack to file */
  67. if (filename) {
  68. new_name = make_new_name(filename, expected_ext);
  69. if (!new_name) {
  70. bb_error_msg("%s: unknown suffix - ignored", filename);
  71. goto err;
  72. }
  73. /* -f: overwrite existing output files */
  74. if (option_mask32 & OPT_FORCE) {
  75. unlink(new_name);
  76. }
  77. /* O_EXCL: "real" bunzip2 doesn't overwrite files */
  78. /* GNU gunzip does not bail out, but goes to next file */
  79. if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
  80. stat_buf.st_mode))
  81. goto err;
  82. }
  83. /* Check that the input is sane */
  84. if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
  85. bb_error_msg_and_die("compressed data not read from terminal, "
  86. "use -f to force it");
  87. }
  88. /* memset(&info, 0, sizeof(info)); */
  89. info.mtime = 0; /* so far it has one member only */
  90. status = unpacker(&info);
  91. if (status < 0)
  92. exitcode = 1;
  93. xclose(STDOUT_FILENO); /* with error check! */
  94. if (filename) {
  95. char *del = new_name;
  96. if (status >= 0) {
  97. /* TODO: restore other things? */
  98. if (info.mtime) {
  99. struct timeval times[2];
  100. times[1].tv_sec = times[0].tv_sec = info.mtime;
  101. times[1].tv_usec = times[0].tv_usec = 0;
  102. /* Note: we closed it first.
  103. * On some systems calling utimes
  104. * then closing resets the mtime
  105. * back to current time. */
  106. utimes(new_name, times); /* ignoring errors */
  107. }
  108. /* Delete _compressed_ file */
  109. del = filename;
  110. /* restore extension (unless tgz -> tar case) */
  111. if (new_name == filename)
  112. filename[strlen(filename)] = '.';
  113. }
  114. xunlink(del);
  115. #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
  116. /* Extreme bloat for gunzip compat */
  117. if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
  118. fprintf(stderr, "%s: %u%% - replaced with %s\n",
  119. filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
  120. }
  121. #endif
  122. free_name:
  123. if (new_name != filename)
  124. free(new_name);
  125. }
  126. } while (*argv && *++argv);
  127. return exitcode;
  128. }
  129. #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
  130. static
  131. char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
  132. {
  133. char *extension = strrchr(filename, '.');
  134. if (!extension || strcmp(extension + 1, expected_ext) != 0) {
  135. /* Mimic GNU gunzip - "real" bunzip2 tries to */
  136. /* unpack file anyway, to file.out */
  137. return NULL;
  138. }
  139. *extension = '\0';
  140. return filename;
  141. }
  142. #endif
  143. /*
  144. * Uncompress applet for busybox (c) 2002 Glenn McGrath
  145. *
  146. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  147. */
  148. //usage:#define uncompress_trivial_usage
  149. //usage: "[-cf] [FILE]..."
  150. //usage:#define uncompress_full_usage "\n\n"
  151. //usage: "Decompress .Z file[s]\n"
  152. //usage: "\n -c Write to stdout"
  153. //usage: "\n -f Overwrite"
  154. #if ENABLE_UNCOMPRESS
  155. static
  156. IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
  157. {
  158. IF_DESKTOP(long long) int status = -1;
  159. if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
  160. bb_error_msg("invalid magic");
  161. } else {
  162. status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
  163. }
  164. return status;
  165. }
  166. int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  167. int uncompress_main(int argc UNUSED_PARAM, char **argv)
  168. {
  169. getopt32(argv, "cf");
  170. argv += optind;
  171. return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
  172. }
  173. #endif
  174. /*
  175. * Gzip implementation for busybox
  176. *
  177. * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
  178. *
  179. * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
  180. * based on gzip sources
  181. *
  182. * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
  183. * well as stdin/stdout, and to generally behave itself wrt command line
  184. * handling.
  185. *
  186. * General cleanup to better adhere to the style guide and make use of standard
  187. * busybox functions by Glenn McGrath
  188. *
  189. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  190. *
  191. * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  192. * Copyright (C) 1992-1993 Jean-loup Gailly
  193. * The unzip code was written and put in the public domain by Mark Adler.
  194. * Portions of the lzw code are derived from the public domain 'compress'
  195. * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  196. * Ken Turkowski, Dave Mack and Peter Jannesen.
  197. *
  198. * See the license_msg below and the file COPYING for the software license.
  199. * See the file algorithm.doc for the compression algorithms and file formats.
  200. */
  201. //usage:#define gunzip_trivial_usage
  202. //usage: "[-cft] [FILE]..."
  203. //usage:#define gunzip_full_usage "\n\n"
  204. //usage: "Decompress FILEs (or stdin)\n"
  205. //usage: "\n -c Write to stdout"
  206. //usage: "\n -f Force"
  207. //usage: "\n -t Test file integrity"
  208. //usage:
  209. //usage:#define gunzip_example_usage
  210. //usage: "$ ls -la /tmp/BusyBox*\n"
  211. //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
  212. //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
  213. //usage: "$ ls -la /tmp/BusyBox*\n"
  214. //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
  215. //usage:
  216. //usage:#define zcat_trivial_usage
  217. //usage: "FILE"
  218. //usage:#define zcat_full_usage "\n\n"
  219. //usage: "Decompress to stdout"
  220. #if ENABLE_GUNZIP
  221. static
  222. char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
  223. {
  224. char *extension = strrchr(filename, '.');
  225. if (!extension)
  226. return NULL;
  227. extension++;
  228. if (strcmp(extension, "tgz" + 1) == 0
  229. #if ENABLE_FEATURE_SEAMLESS_Z
  230. || (extension[0] == 'Z' && extension[1] == '\0')
  231. #endif
  232. ) {
  233. extension[-1] = '\0';
  234. } else if (strcmp(extension, "tgz") == 0) {
  235. filename = xstrdup(filename);
  236. extension = strrchr(filename, '.');
  237. extension[2] = 'a';
  238. extension[3] = 'r';
  239. } else {
  240. return NULL;
  241. }
  242. return filename;
  243. }
  244. static
  245. IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(unpack_info_t *info)
  246. {
  247. IF_DESKTOP(long long) int status = -1;
  248. /* do the decompression, and cleanup */
  249. if (xread_char(STDIN_FILENO) == 0x1f) {
  250. unsigned char magic2;
  251. magic2 = xread_char(STDIN_FILENO);
  252. if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
  253. status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
  254. } else if (magic2 == 0x8b) {
  255. status = unpack_gz_stream_with_info(STDIN_FILENO, STDOUT_FILENO, info);
  256. } else {
  257. goto bad_magic;
  258. }
  259. if (status < 0) {
  260. bb_error_msg("error inflating");
  261. }
  262. } else {
  263. bad_magic:
  264. bb_error_msg("invalid magic");
  265. /* status is still == -1 */
  266. }
  267. return status;
  268. }
  269. /*
  270. * Linux kernel build uses gzip -d -n. We accept and ignore it.
  271. * Man page says:
  272. * -n --no-name
  273. * gzip: do not save the original file name and time stamp.
  274. * (The original name is always saved if the name had to be truncated.)
  275. * gunzip: do not restore the original file name/time even if present
  276. * (remove only the gzip suffix from the compressed file name).
  277. * This option is the default when decompressing.
  278. * -N --name
  279. * gzip: always save the original file name and time stamp (this is the default)
  280. * gunzip: restore the original file name and time stamp if present.
  281. */
  282. int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  283. int gunzip_main(int argc UNUSED_PARAM, char **argv)
  284. {
  285. getopt32(argv, "cfvdtn");
  286. argv += optind;
  287. /* if called as zcat */
  288. if (applet_name[1] == 'c')
  289. option_mask32 |= OPT_STDOUT;
  290. return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
  291. }
  292. #endif
  293. /*
  294. * Modified for busybox by Glenn McGrath
  295. * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
  296. *
  297. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  298. */
  299. //usage:#define bunzip2_trivial_usage
  300. //usage: "[-cf] [FILE]..."
  301. //usage:#define bunzip2_full_usage "\n\n"
  302. //usage: "Decompress FILEs (or stdin)\n"
  303. //usage: "\n -c Write to stdout"
  304. //usage: "\n -f Force"
  305. //usage:#define bzcat_trivial_usage
  306. //usage: "FILE"
  307. //usage:#define bzcat_full_usage "\n\n"
  308. //usage: "Decompress to stdout"
  309. //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
  310. //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
  311. #if ENABLE_BUNZIP2
  312. static
  313. IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
  314. {
  315. return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
  316. }
  317. int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  318. int bunzip2_main(int argc UNUSED_PARAM, char **argv)
  319. {
  320. getopt32(argv, "cfvdt");
  321. argv += optind;
  322. if (applet_name[2] == 'c') /* bzcat */
  323. option_mask32 |= OPT_STDOUT;
  324. return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
  325. }
  326. #endif
  327. /*
  328. * Small lzma deflate implementation.
  329. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  330. *
  331. * Based on bunzip.c from busybox
  332. *
  333. * Licensed under GPLv2, see file LICENSE in this source tree.
  334. */
  335. //usage:#define unlzma_trivial_usage
  336. //usage: "[-cf] [FILE]..."
  337. //usage:#define unlzma_full_usage "\n\n"
  338. //usage: "Decompress FILE (or stdin)\n"
  339. //usage: "\n -c Write to stdout"
  340. //usage: "\n -f Force"
  341. //usage:
  342. //usage:#define lzma_trivial_usage
  343. //usage: "-d [-cf] [FILE]..."
  344. //usage:#define lzma_full_usage "\n\n"
  345. //usage: "Decompress FILE (or stdin)\n"
  346. //usage: "\n -d Decompress"
  347. //usage: "\n -c Write to stdout"
  348. //usage: "\n -f Force"
  349. //usage:
  350. //usage:#define lzcat_trivial_usage
  351. //usage: "FILE"
  352. //usage:#define lzcat_full_usage "\n\n"
  353. //usage: "Decompress to stdout"
  354. //usage:
  355. //usage:#define unxz_trivial_usage
  356. //usage: "[-cf] [FILE]..."
  357. //usage:#define unxz_full_usage "\n\n"
  358. //usage: "Decompress FILE (or stdin)\n"
  359. //usage: "\n -c Write to stdout"
  360. //usage: "\n -f Force"
  361. //usage:
  362. //usage:#define xz_trivial_usage
  363. //usage: "-d [-cf] [FILE]..."
  364. //usage:#define xz_full_usage "\n\n"
  365. //usage: "Decompress FILE (or stdin)\n"
  366. //usage: "\n -d Decompress"
  367. //usage: "\n -c Write to stdout"
  368. //usage: "\n -f Force"
  369. //usage:
  370. //usage:#define xzcat_trivial_usage
  371. //usage: "FILE"
  372. //usage:#define xzcat_full_usage "\n\n"
  373. //usage: "Decompress to stdout"
  374. #if ENABLE_UNLZMA
  375. static
  376. IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
  377. {
  378. return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
  379. }
  380. int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  381. int unlzma_main(int argc UNUSED_PARAM, char **argv)
  382. {
  383. IF_LZMA(int opts =) getopt32(argv, "cfvdt");
  384. # if ENABLE_LZMA
  385. /* lzma without -d or -t? */
  386. if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
  387. bb_show_usage();
  388. # endif
  389. /* lzcat? */
  390. if (applet_name[2] == 'c')
  391. option_mask32 |= OPT_STDOUT;
  392. argv += optind;
  393. return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
  394. }
  395. #endif
  396. #if ENABLE_UNXZ
  397. static
  398. IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(unpack_info_t *info UNUSED_PARAM)
  399. {
  400. struct {
  401. uint32_t v1;
  402. uint16_t v2;
  403. } magic;
  404. xread(STDIN_FILENO, &magic, 6);
  405. if (magic.v1 != XZ_MAGIC1a || magic.v2 != XZ_MAGIC2a) {
  406. bb_error_msg("invalid magic");
  407. return -1;
  408. }
  409. return unpack_xz_stream(STDIN_FILENO, STDOUT_FILENO);
  410. }
  411. int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  412. int unxz_main(int argc UNUSED_PARAM, char **argv)
  413. {
  414. IF_XZ(int opts =) getopt32(argv, "cfvdt");
  415. # if ENABLE_XZ
  416. /* xz without -d or -t? */
  417. if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
  418. bb_show_usage();
  419. # endif
  420. /* xzcat? */
  421. if (applet_name[2] == 'c')
  422. option_mask32 |= OPT_STDOUT;
  423. argv += optind;
  424. return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
  425. }
  426. #endif