bbunzip.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 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. /* O_EXCL: "real" bunzip2 doesn't overwrite files */
  68. /* GNU gunzip does 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
  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 unpack_bz2_stream(STDIN_FILENO, STDOUT_FILENO);
  136. }
  137. int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  138. int bunzip2_main(int argc ATTRIBUTE_UNUSED, char **argv)
  139. {
  140. getopt32(argv, "cfvdt");
  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
  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. #if ENABLE_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. status = unpack_gz_stream(STDIN_FILENO, STDOUT_FILENO);
  210. } else {
  211. goto bad_magic;
  212. }
  213. if (status < 0) {
  214. bb_error_msg("error inflating");
  215. }
  216. } else {
  217. bad_magic:
  218. bb_error_msg("invalid magic");
  219. /* status is still == -1 */
  220. }
  221. return status;
  222. }
  223. /*
  224. * Linux kernel build uses gzip -d -n. We accept and ignore it.
  225. * Man page says:
  226. * -n --no-name
  227. * gzip: do not save the original file name and time stamp.
  228. * (The original name is always saved if the name had to be truncated.)
  229. * gunzip: do not restore the original file name/time even if present
  230. * (remove only the gzip suffix from the compressed file name).
  231. * This option is the default when decompressing.
  232. * -N --name
  233. * gzip: always save the original file name and time stamp (this is the default)
  234. * gunzip: restore the original file name and time stamp if present.
  235. */
  236. int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  237. int gunzip_main(int argc ATTRIBUTE_UNUSED, char **argv)
  238. {
  239. getopt32(argv, "cfvdtn");
  240. argv += optind;
  241. /* if called as zcat */
  242. if (applet_name[1] == 'c')
  243. option_mask32 |= OPT_STDOUT;
  244. return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
  245. }
  246. #endif
  247. /*
  248. * Small lzma deflate implementation.
  249. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  250. *
  251. * Based on bunzip.c from busybox
  252. *
  253. * Licensed under GPL v2, see file LICENSE in this tarball for details.
  254. */
  255. #if ENABLE_UNLZMA
  256. static
  257. char* make_new_name_unlzma(char *filename)
  258. {
  259. return make_new_name_generic(filename, "lzma");
  260. }
  261. static
  262. USE_DESKTOP(long long) int unpack_unlzma(void)
  263. {
  264. return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
  265. }
  266. int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  267. int unlzma_main(int argc ATTRIBUTE_UNUSED, char **argv)
  268. {
  269. getopt32(argv, "cf");
  270. argv += optind;
  271. /* lzmacat? */
  272. if (applet_name[4] == 'c')
  273. option_mask32 |= OPT_STDOUT;
  274. return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
  275. }
  276. #endif
  277. /*
  278. * Uncompress applet for busybox (c) 2002 Glenn McGrath
  279. *
  280. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  281. */
  282. #if ENABLE_UNCOMPRESS
  283. static
  284. char* make_new_name_uncompress(char *filename)
  285. {
  286. return make_new_name_generic(filename, "Z");
  287. }
  288. static
  289. USE_DESKTOP(long long) int unpack_uncompress(void)
  290. {
  291. USE_DESKTOP(long long) int status = -1;
  292. if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
  293. bb_error_msg("invalid magic");
  294. } else {
  295. status = uncompress(STDIN_FILENO, STDOUT_FILENO);
  296. }
  297. return status;
  298. }
  299. int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  300. int uncompress_main(int argc ATTRIBUTE_UNUSED, char **argv)
  301. {
  302. getopt32(argv, "cf");
  303. argv += optind;
  304. return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);
  305. }
  306. #endif