bbunzip.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 = 0x1,
  11. OPT_FORCE = 0x2,
  12. /* gunzip and bunzip2 only: */
  13. OPT_VERBOSE = 0x4,
  14. OPT_DECOMPRESS = 0x8,
  15. OPT_TEST = 0x10,
  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. int FAST_FUNC bbunpack(char **argv,
  28. char* (*make_new_name)(char *filename),
  29. USE_DESKTOP(long long) int (*unpacker)(void)
  30. )
  31. {
  32. struct stat stat_buf;
  33. USE_DESKTOP(long long) int status;
  34. char *filename, *new_name;
  35. smallint exitcode = 0;
  36. do {
  37. /* NB: new_name is *maybe* malloc'ed! */
  38. new_name = NULL;
  39. filename = *argv; /* can be NULL - 'streaming' bunzip2 */
  40. if (filename && LONE_DASH(filename))
  41. filename = NULL;
  42. /* Open src */
  43. if (filename) {
  44. if (stat(filename, &stat_buf) != 0) {
  45. bb_simple_perror_msg(filename);
  46. err:
  47. exitcode = 1;
  48. goto free_name;
  49. }
  50. if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
  51. goto err;
  52. }
  53. /* Special cases: test, stdout */
  54. if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
  55. if (option_mask32 & OPT_TEST)
  56. if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
  57. goto err;
  58. filename = NULL;
  59. }
  60. /* Open dst if we are going to unpack to file */
  61. if (filename) {
  62. new_name = make_new_name(filename);
  63. if (!new_name) {
  64. bb_error_msg("%s: unknown suffix - ignored", filename);
  65. goto err;
  66. }
  67. /* -f: overwrite existing output files */
  68. if (option_mask32 & OPT_FORCE) {
  69. unlink(new_name);
  70. }
  71. /* O_EXCL: "real" bunzip2 doesn't overwrite files */
  72. /* GNU gunzip does not bail out, but goes to next file */
  73. if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
  74. stat_buf.st_mode))
  75. goto err;
  76. }
  77. /* Check that the input is sane */
  78. if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
  79. bb_error_msg_and_die("compressed data not read from terminal, "
  80. "use -f to force it");
  81. }
  82. status = unpacker();
  83. if (status < 0)
  84. exitcode = 1;
  85. if (filename) {
  86. char *del = new_name;
  87. if (status >= 0) {
  88. /* TODO: restore user/group/times here? */
  89. /* Delete _compressed_ file */
  90. del = filename;
  91. /* restore extension (unless tgz -> tar case) */
  92. if (new_name == filename)
  93. filename[strlen(filename)] = '.';
  94. }
  95. xunlink(del);
  96. #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
  97. /* Extreme bloat for gunzip compat */
  98. if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
  99. fprintf(stderr, "%s: %u%% - replaced with %s\n",
  100. filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
  101. }
  102. #endif
  103. free_name:
  104. if (new_name != filename)
  105. free(new_name);
  106. }
  107. } while (*argv && *++argv);
  108. return exitcode;
  109. }
  110. #if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS
  111. static
  112. char* make_new_name_generic(char *filename, const char *expected_ext)
  113. {
  114. char *extension = strrchr(filename, '.');
  115. if (!extension || strcmp(extension + 1, expected_ext) != 0) {
  116. /* Mimic GNU gunzip - "real" bunzip2 tries to */
  117. /* unpack file anyway, to file.out */
  118. return NULL;
  119. }
  120. *extension = '\0';
  121. return filename;
  122. }
  123. #endif
  124. /*
  125. * Modified for busybox by Glenn McGrath
  126. * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
  127. *
  128. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  129. */
  130. #if ENABLE_BUNZIP2
  131. static
  132. char* make_new_name_bunzip2(char *filename)
  133. {
  134. return make_new_name_generic(filename, "bz2");
  135. }
  136. static
  137. USE_DESKTOP(long long) int unpack_bunzip2(void)
  138. {
  139. return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
  140. }
  141. int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  142. int bunzip2_main(int argc UNUSED_PARAM, char **argv)
  143. {
  144. getopt32(argv, "cfvdt");
  145. argv += optind;
  146. if (applet_name[2] == 'c')
  147. option_mask32 |= OPT_STDOUT;
  148. return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
  149. }
  150. #endif
  151. /*
  152. * Gzip implementation for busybox
  153. *
  154. * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
  155. *
  156. * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
  157. * based on gzip sources
  158. *
  159. * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
  160. * well as stdin/stdout, and to generally behave itself wrt command line
  161. * handling.
  162. *
  163. * General cleanup to better adhere to the style guide and make use of standard
  164. * busybox functions by Glenn McGrath
  165. *
  166. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  167. *
  168. * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  169. * Copyright (C) 1992-1993 Jean-loup Gailly
  170. * The unzip code was written and put in the public domain by Mark Adler.
  171. * Portions of the lzw code are derived from the public domain 'compress'
  172. * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  173. * Ken Turkowski, Dave Mack and Peter Jannesen.
  174. *
  175. * See the license_msg below and the file COPYING for the software license.
  176. * See the file algorithm.doc for the compression algorithms and file formats.
  177. */
  178. #if ENABLE_GUNZIP
  179. static
  180. char* make_new_name_gunzip(char *filename)
  181. {
  182. char *extension = strrchr(filename, '.');
  183. if (!extension)
  184. return NULL;
  185. extension++;
  186. if (strcmp(extension, "tgz" + 1) == 0
  187. #if ENABLE_FEATURE_SEAMLESS_Z
  188. || (extension[0] == 'Z' && extension[1] == '\0')
  189. #endif
  190. ) {
  191. extension[-1] = '\0';
  192. } else if (strcmp(extension, "tgz") == 0) {
  193. filename = xstrdup(filename);
  194. extension = strrchr(filename, '.');
  195. extension[2] = 'a';
  196. extension[3] = 'r';
  197. } else {
  198. return NULL;
  199. }
  200. return filename;
  201. }
  202. static
  203. USE_DESKTOP(long long) int unpack_gunzip(void)
  204. {
  205. USE_DESKTOP(long long) int status = -1;
  206. /* do the decompression, and cleanup */
  207. if (xread_char(STDIN_FILENO) == 0x1f) {
  208. unsigned char magic2;
  209. magic2 = xread_char(STDIN_FILENO);
  210. if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
  211. status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
  212. } else if (magic2 == 0x8b) {
  213. status = unpack_gz_stream(STDIN_FILENO, STDOUT_FILENO);
  214. } else {
  215. goto bad_magic;
  216. }
  217. if (status < 0) {
  218. bb_error_msg("error inflating");
  219. }
  220. } else {
  221. bad_magic:
  222. bb_error_msg("invalid magic");
  223. /* status is still == -1 */
  224. }
  225. return status;
  226. }
  227. /*
  228. * Linux kernel build uses gzip -d -n. We accept and ignore it.
  229. * Man page says:
  230. * -n --no-name
  231. * gzip: do not save the original file name and time stamp.
  232. * (The original name is always saved if the name had to be truncated.)
  233. * gunzip: do not restore the original file name/time even if present
  234. * (remove only the gzip suffix from the compressed file name).
  235. * This option is the default when decompressing.
  236. * -N --name
  237. * gzip: always save the original file name and time stamp (this is the default)
  238. * gunzip: restore the original file name and time stamp if present.
  239. */
  240. int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  241. int gunzip_main(int argc UNUSED_PARAM, char **argv)
  242. {
  243. getopt32(argv, "cfvdtn");
  244. argv += optind;
  245. /* if called as zcat */
  246. if (applet_name[1] == 'c')
  247. option_mask32 |= OPT_STDOUT;
  248. return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
  249. }
  250. #endif
  251. /*
  252. * Small lzma deflate implementation.
  253. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  254. *
  255. * Based on bunzip.c from busybox
  256. *
  257. * Licensed under GPL v2, see file LICENSE in this tarball for details.
  258. */
  259. #if ENABLE_UNLZMA
  260. static
  261. char* make_new_name_unlzma(char *filename)
  262. {
  263. return make_new_name_generic(filename, "lzma");
  264. }
  265. static
  266. USE_DESKTOP(long long) int unpack_unlzma(void)
  267. {
  268. return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
  269. }
  270. int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  271. int unlzma_main(int argc UNUSED_PARAM, char **argv)
  272. {
  273. getopt32(argv, "cf");
  274. argv += optind;
  275. /* lzmacat? */
  276. if (applet_name[4] == 'c')
  277. option_mask32 |= OPT_STDOUT;
  278. return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
  279. }
  280. #endif
  281. /*
  282. * Uncompress applet for busybox (c) 2002 Glenn McGrath
  283. *
  284. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  285. */
  286. #if ENABLE_UNCOMPRESS
  287. static
  288. char* make_new_name_uncompress(char *filename)
  289. {
  290. return make_new_name_generic(filename, "Z");
  291. }
  292. static
  293. USE_DESKTOP(long long) int unpack_uncompress(void)
  294. {
  295. USE_DESKTOP(long long) int status = -1;
  296. if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
  297. bb_error_msg("invalid magic");
  298. } else {
  299. status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
  300. }
  301. return status;
  302. }
  303. int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  304. int uncompress_main(int argc UNUSED_PARAM, char **argv)
  305. {
  306. getopt32(argv, "cf");
  307. argv += optind;
  308. return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);
  309. }
  310. #endif