bbunzip.c 9.4 KB

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