bbunzip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 tarball for details.
  6. */
  7. #include "libbb.h"
  8. #include "unarchive.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 tarball for details.
  147. */
  148. #if ENABLE_UNCOMPRESS
  149. static
  150. IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
  151. {
  152. IF_DESKTOP(long long) int status = -1;
  153. if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
  154. bb_error_msg("invalid magic");
  155. } else {
  156. status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
  157. }
  158. return status;
  159. }
  160. int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  161. int uncompress_main(int argc UNUSED_PARAM, char **argv)
  162. {
  163. getopt32(argv, "cf");
  164. argv += optind;
  165. return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
  166. }
  167. #endif
  168. /*
  169. * Gzip implementation for busybox
  170. *
  171. * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
  172. *
  173. * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
  174. * based on gzip sources
  175. *
  176. * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
  177. * well as stdin/stdout, and to generally behave itself wrt command line
  178. * handling.
  179. *
  180. * General cleanup to better adhere to the style guide and make use of standard
  181. * busybox functions by Glenn McGrath
  182. *
  183. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  184. *
  185. * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  186. * Copyright (C) 1992-1993 Jean-loup Gailly
  187. * The unzip code was written and put in the public domain by Mark Adler.
  188. * Portions of the lzw code are derived from the public domain 'compress'
  189. * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  190. * Ken Turkowski, Dave Mack and Peter Jannesen.
  191. *
  192. * See the license_msg below and the file COPYING for the software license.
  193. * See the file algorithm.doc for the compression algorithms and file formats.
  194. */
  195. #if ENABLE_GUNZIP
  196. static
  197. char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
  198. {
  199. char *extension = strrchr(filename, '.');
  200. if (!extension)
  201. return NULL;
  202. extension++;
  203. if (strcmp(extension, "tgz" + 1) == 0
  204. #if ENABLE_FEATURE_SEAMLESS_Z
  205. || (extension[0] == 'Z' && extension[1] == '\0')
  206. #endif
  207. ) {
  208. extension[-1] = '\0';
  209. } else if (strcmp(extension, "tgz") == 0) {
  210. filename = xstrdup(filename);
  211. extension = strrchr(filename, '.');
  212. extension[2] = 'a';
  213. extension[3] = 'r';
  214. } else {
  215. return NULL;
  216. }
  217. return filename;
  218. }
  219. static
  220. IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(unpack_info_t *info)
  221. {
  222. IF_DESKTOP(long long) int status = -1;
  223. /* do the decompression, and cleanup */
  224. if (xread_char(STDIN_FILENO) == 0x1f) {
  225. unsigned char magic2;
  226. magic2 = xread_char(STDIN_FILENO);
  227. if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
  228. status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
  229. } else if (magic2 == 0x8b) {
  230. status = unpack_gz_stream_with_info(STDIN_FILENO, STDOUT_FILENO, info);
  231. } else {
  232. goto bad_magic;
  233. }
  234. if (status < 0) {
  235. bb_error_msg("error inflating");
  236. }
  237. } else {
  238. bad_magic:
  239. bb_error_msg("invalid magic");
  240. /* status is still == -1 */
  241. }
  242. return status;
  243. }
  244. /*
  245. * Linux kernel build uses gzip -d -n. We accept and ignore it.
  246. * Man page says:
  247. * -n --no-name
  248. * gzip: do not save the original file name and time stamp.
  249. * (The original name is always saved if the name had to be truncated.)
  250. * gunzip: do not restore the original file name/time even if present
  251. * (remove only the gzip suffix from the compressed file name).
  252. * This option is the default when decompressing.
  253. * -N --name
  254. * gzip: always save the original file name and time stamp (this is the default)
  255. * gunzip: restore the original file name and time stamp if present.
  256. */
  257. int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  258. int gunzip_main(int argc UNUSED_PARAM, char **argv)
  259. {
  260. getopt32(argv, "cfvdtn");
  261. argv += optind;
  262. /* if called as zcat */
  263. if (applet_name[1] == 'c')
  264. option_mask32 |= OPT_STDOUT;
  265. return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
  266. }
  267. #endif
  268. /*
  269. * Modified for busybox by Glenn McGrath
  270. * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
  271. *
  272. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  273. */
  274. //usage:#define bunzip2_trivial_usage
  275. //usage: "[OPTIONS] [FILE]..."
  276. //usage:#define bunzip2_full_usage "\n\n"
  277. //usage: "Decompress FILEs (or stdin)\n"
  278. //usage: "\nOptions:"
  279. //usage: "\n -c Write to stdout"
  280. //usage: "\n -f Force"
  281. //usage:#define bzcat_trivial_usage
  282. //usage: "FILE"
  283. //usage:#define bzcat_full_usage "\n\n"
  284. //usage: "Decompress to stdout"
  285. //applet:IF_BUNZIP2(APPLET(bunzip2, _BB_DIR_USR_BIN, _BB_SUID_DROP))
  286. //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, _BB_DIR_USR_BIN, _BB_SUID_DROP, bzcat))
  287. #if ENABLE_BUNZIP2
  288. static
  289. IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
  290. {
  291. return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
  292. }
  293. int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  294. int bunzip2_main(int argc UNUSED_PARAM, char **argv)
  295. {
  296. getopt32(argv, "cfvdt");
  297. argv += optind;
  298. if (applet_name[2] == 'c') /* bzcat */
  299. option_mask32 |= OPT_STDOUT;
  300. return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
  301. }
  302. #endif
  303. /*
  304. * Small lzma deflate implementation.
  305. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  306. *
  307. * Based on bunzip.c from busybox
  308. *
  309. * Licensed under GPL v2, see file LICENSE in this tarball for details.
  310. */
  311. #if ENABLE_UNLZMA
  312. static
  313. IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
  314. {
  315. return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
  316. }
  317. int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  318. int unlzma_main(int argc UNUSED_PARAM, char **argv)
  319. {
  320. IF_LZMA(int opts =) getopt32(argv, "cfvdt");
  321. # if ENABLE_LZMA
  322. /* lzma without -d or -t? */
  323. if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
  324. bb_show_usage();
  325. # endif
  326. /* lzcat? */
  327. if (applet_name[2] == 'c')
  328. option_mask32 |= OPT_STDOUT;
  329. argv += optind;
  330. return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
  331. }
  332. #endif
  333. #if ENABLE_UNXZ
  334. static
  335. IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(unpack_info_t *info UNUSED_PARAM)
  336. {
  337. struct {
  338. uint32_t v1;
  339. uint16_t v2;
  340. } magic;
  341. xread(STDIN_FILENO, &magic, 6);
  342. if (magic.v1 != XZ_MAGIC1a || magic.v2 != XZ_MAGIC2a) {
  343. bb_error_msg("invalid magic");
  344. return -1;
  345. }
  346. return unpack_xz_stream(STDIN_FILENO, STDOUT_FILENO);
  347. }
  348. int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  349. int unxz_main(int argc UNUSED_PARAM, char **argv)
  350. {
  351. IF_XZ(int opts =) getopt32(argv, "cfvdt");
  352. # if ENABLE_XZ
  353. /* xz without -d or -t? */
  354. if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
  355. bb_show_usage();
  356. # endif
  357. /* xzcat? */
  358. if (applet_name[2] == 'c')
  359. option_mask32 |= OPT_STDOUT;
  360. argv += optind;
  361. return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
  362. }
  363. #endif