bbunzip.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 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 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_perror_msg("%s", 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. /* O_EXCL: "real" bunzip2 doesn't overwrite files */
  68. /* GNU gunzip goes not bail out, but goes to next file */
  69. if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
  70. stat_buf.st_mode))
  71. goto err;
  72. }
  73. /* Check that the input is sane */
  74. if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
  75. bb_error_msg_and_die("compressed data not read from terminal, "
  76. "use -f to force it");
  77. }
  78. status = unpacker();
  79. if (status < 0)
  80. exitcode = 1;
  81. if (filename) {
  82. char *del = new_name;
  83. if (status >= 0) {
  84. /* TODO: restore user/group/times here? */
  85. /* Delete _compressed_ file */
  86. del = filename;
  87. /* restore extension (unless tgz -> tar case) */
  88. if (new_name == filename)
  89. filename[strlen(filename)] = '.';
  90. }
  91. xunlink(del);
  92. #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
  93. /* Extreme bloat for gunzip compat */
  94. if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
  95. fprintf(stderr, "%s: %u%% - replaced with %s\n",
  96. filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
  97. }
  98. #endif
  99. free_name:
  100. if (new_name != filename)
  101. free(new_name);
  102. }
  103. } while (*argv && *++argv);
  104. return exitcode;
  105. }
  106. #if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS
  107. static
  108. char* make_new_name_generic(char *filename, const char *expected_ext)
  109. {
  110. char *extension = strrchr(filename, '.');
  111. if (!extension || strcmp(extension + 1, expected_ext) != 0) {
  112. /* Mimic GNU gunzip - "real" bunzip2 tries to */
  113. /* unpack file anyway, to file.out */
  114. return NULL;
  115. }
  116. *extension = '\0';
  117. return filename;
  118. }
  119. #endif
  120. /*
  121. * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
  122. * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
  123. *
  124. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  125. */
  126. #if ENABLE_BUNZIP2
  127. static
  128. char* make_new_name_bunzip2(char *filename)
  129. {
  130. return make_new_name_generic(filename, "bz2");
  131. }
  132. static
  133. USE_DESKTOP(long long) int unpack_bunzip2(void)
  134. {
  135. return uncompressStream(STDIN_FILENO, STDOUT_FILENO);
  136. }
  137. int bunzip2_main(int argc, char **argv);
  138. int bunzip2_main(int argc, char **argv)
  139. {
  140. getopt32(argc, argv, "cf");
  141. argv += optind;
  142. if (applet_name[2] == 'c')
  143. option_mask32 |= OPT_STDOUT;
  144. return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
  145. }
  146. #endif
  147. /*
  148. * Gzip implementation for busybox
  149. *
  150. * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
  151. *
  152. * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
  153. * based on gzip sources
  154. *
  155. * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
  156. * well as stdin/stdout, and to generally behave itself wrt command line
  157. * handling.
  158. *
  159. * General cleanup to better adhere to the style guide and make use of standard
  160. * busybox functions by Glenn McGrath <bug1@iinet.net.au>
  161. *
  162. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  163. *
  164. * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  165. * Copyright (C) 1992-1993 Jean-loup Gailly
  166. * The unzip code was written and put in the public domain by Mark Adler.
  167. * Portions of the lzw code are derived from the public domain 'compress'
  168. * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  169. * Ken Turkowski, Dave Mack and Peter Jannesen.
  170. *
  171. * See the license_msg below and the file COPYING for the software license.
  172. * See the file algorithm.doc for the compression algorithms and file formats.
  173. */
  174. #if ENABLE_GUNZIP
  175. static
  176. char* make_new_name_gunzip(char *filename)
  177. {
  178. char *extension = strrchr(filename, '.');
  179. if (!extension)
  180. return NULL;
  181. extension++;
  182. if (strcmp(extension, "tgz" + 1) == 0
  183. #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
  184. || strcmp(extension, "Z") == 0
  185. #endif
  186. ) {
  187. extension[-1] = '\0';
  188. } else if (strcmp(extension, "tgz") == 0) {
  189. filename = xstrdup(filename);
  190. extension = strrchr(filename, '.');
  191. extension[2] = 'a';
  192. extension[3] = 'r';
  193. } else {
  194. return NULL;
  195. }
  196. return filename;
  197. }
  198. static
  199. USE_DESKTOP(long long) int unpack_gunzip(void)
  200. {
  201. USE_DESKTOP(long long) int status = -1;
  202. /* do the decompression, and cleanup */
  203. if (xread_char(STDIN_FILENO) == 0x1f) {
  204. unsigned char magic2;
  205. magic2 = xread_char(STDIN_FILENO);
  206. if (ENABLE_FEATURE_GUNZIP_UNCOMPRESS && magic2 == 0x9d) {
  207. status = uncompress(STDIN_FILENO, STDOUT_FILENO);
  208. } else if (magic2 == 0x8b) {
  209. check_header_gzip_or_die(STDIN_FILENO);
  210. status = inflate_gunzip(STDIN_FILENO, STDOUT_FILENO);
  211. } else {
  212. goto bad_magic;
  213. }
  214. if (status < 0) {
  215. bb_error_msg("error inflating");
  216. }
  217. } else {
  218. bad_magic:
  219. bb_error_msg("invalid magic");
  220. /* status is still == -1 */
  221. }
  222. return status;
  223. }
  224. int gunzip_main(int argc, char **argv);
  225. int gunzip_main(int argc, char **argv)
  226. {
  227. getopt32(argc, argv, "cfvdt");
  228. argv += optind;
  229. /* if called as zcat */
  230. if (applet_name[1] == 'c')
  231. option_mask32 |= OPT_STDOUT;
  232. return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
  233. }
  234. #endif
  235. /*
  236. * Small lzma deflate implementation.
  237. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  238. *
  239. * Based on bunzip.c from busybox
  240. *
  241. * Licensed under GPL v2, see file LICENSE in this tarball for details.
  242. */
  243. #if ENABLE_UNLZMA
  244. static
  245. char* make_new_name_unlzma(char *filename)
  246. {
  247. return make_new_name_generic(filename, "lzma");
  248. }
  249. static
  250. USE_DESKTOP(long long) int unpack_unlzma(void)
  251. {
  252. return unlzma(STDIN_FILENO, STDOUT_FILENO);
  253. }
  254. int unlzma_main(int argc, char **argv);
  255. int unlzma_main(int argc, char **argv)
  256. {
  257. getopt32(argc, argv, "c");
  258. argv += optind;
  259. /* lzmacat? */
  260. if (applet_name[4] == 'c')
  261. option_mask32 |= OPT_STDOUT;
  262. return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
  263. }
  264. #endif
  265. /*
  266. * Uncompress applet for busybox (c) 2002 Glenn McGrath
  267. *
  268. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  269. */
  270. #if ENABLE_UNCOMPRESS
  271. static
  272. char* make_new_name_uncompress(char *filename)
  273. {
  274. return make_new_name_generic(filename, "Z");
  275. }
  276. static
  277. USE_DESKTOP(long long) int unpack_uncompress(void)
  278. {
  279. USE_DESKTOP(long long) int status = -1;
  280. if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
  281. bb_error_msg("invalid magic");
  282. } else {
  283. status = uncompress(STDIN_FILENO, STDOUT_FILENO);
  284. }
  285. return status;
  286. }
  287. int uncompress_main(int argc, char **argv);
  288. int uncompress_main(int argc, char **argv)
  289. {
  290. getopt32(argc, argv, "cf");
  291. argv += optind;
  292. return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);
  293. }
  294. #endif