gunzip.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Gzip implementation for busybox
  4. *
  5. * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
  6. *
  7. * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
  8. * based on gzip sources
  9. *
  10. * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
  11. * well as stdin/stdout, and to generally behave itself wrt command line
  12. * handling.
  13. *
  14. * General cleanup to better adhere to the style guide and make use of standard
  15. * busybox functions by Glenn McGrath <bug1@iinet.net.au>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. *
  32. * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  33. * Copyright (C) 1992-1993 Jean-loup Gailly
  34. * The unzip code was written and put in the public domain by Mark Adler.
  35. * Portions of the lzw code are derived from the public domain 'compress'
  36. * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  37. * Ken Turkowski, Dave Mack and Peter Jannesen.
  38. *
  39. * See the license_msg below and the file COPYING for the software license.
  40. * See the file algorithm.doc for the compression algorithms and file formats.
  41. */
  42. #if 0
  43. static char *license_msg[] = {
  44. " Copyright (C) 1992-1993 Jean-loup Gailly",
  45. " This program is free software; you can redistribute it and/or modify",
  46. " it under the terms of the GNU General Public License as published by",
  47. " the Free Software Foundation; either version 2, or (at your option)",
  48. " any later version.",
  49. "",
  50. " This program is distributed in the hope that it will be useful,",
  51. " but WITHOUT ANY WARRANTY; without even the implied warranty of",
  52. " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
  53. " GNU General Public License for more details.",
  54. "",
  55. " You should have received a copy of the GNU General Public License",
  56. " along with this program; if not, write to the Free Software",
  57. " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
  58. 0
  59. };
  60. #endif
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #include <unistd.h>
  64. #include <sys/types.h>
  65. #include <sys/stat.h>
  66. #include <fcntl.h>
  67. #include "busybox.h"
  68. #include "unarchive.h"
  69. #define GUNZIP_OPT_STDOUT 1
  70. #define GUNZIP_OPT_FORCE 2
  71. #define GUNZIP_OPT_TEST 4
  72. #define GUNZIP_OPT_DECOMPRESS 8
  73. int gunzip_main(int argc, char **argv)
  74. {
  75. char status = EXIT_SUCCESS;
  76. unsigned long opt;
  77. opt = bb_getopt_ulflags(argc, argv, "cftd");
  78. /* if called as zcat */
  79. if (strcmp(bb_applet_name, "zcat") == 0) {
  80. opt |= GUNZIP_OPT_STDOUT;
  81. }
  82. do {
  83. struct stat stat_buf;
  84. const char *old_path = argv[optind];
  85. const char *delete_path = NULL;
  86. char *new_path = NULL;
  87. int src_fd;
  88. int dst_fd;
  89. optind++;
  90. if (old_path == NULL || strcmp(old_path, "-") == 0) {
  91. src_fd = STDIN_FILENO;
  92. opt |= GUNZIP_OPT_STDOUT;
  93. } else {
  94. src_fd = bb_xopen(old_path, O_RDONLY);
  95. /* Get the time stamp on the input file. */
  96. xstat(old_path, &stat_buf);
  97. }
  98. /* Check that the input is sane. */
  99. if (isatty(src_fd) && ((opt & GUNZIP_OPT_FORCE) == 0)) {
  100. bb_error_msg_and_die
  101. ("compressed data not read from terminal. Use -f to force it.");
  102. }
  103. /* Set output filename and number */
  104. if (opt & GUNZIP_OPT_TEST) {
  105. dst_fd = bb_xopen(bb_dev_null, O_WRONLY); /* why does test use filenum 2 ? */
  106. } else if (opt & GUNZIP_OPT_STDOUT) {
  107. dst_fd = STDOUT_FILENO;
  108. } else {
  109. char *extension;
  110. new_path = bb_xstrdup(old_path);
  111. extension = strrchr(new_path, '.');
  112. #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
  113. if (extension && (strcmp(extension, ".Z") == 0)) {
  114. *extension = '\0';
  115. } else
  116. #endif
  117. if (extension && (strcmp(extension, ".gz") == 0)) {
  118. *extension = '\0';
  119. } else if (extension && (strcmp(extension, ".tgz") == 0)) {
  120. extension[2] = 'a';
  121. extension[3] = 'r';
  122. } else {
  123. bb_error_msg_and_die("Invalid extension");
  124. }
  125. /* Open output file */
  126. dst_fd = bb_xopen(new_path, O_WRONLY | O_CREAT);
  127. /* Set permissions on the file */
  128. chmod(new_path, stat_buf.st_mode);
  129. /* If unzip succeeds remove the old file */
  130. delete_path = old_path;
  131. }
  132. /* do the decompression, and cleanup */
  133. if (bb_xread_char(src_fd) == 0x1f) {
  134. unsigned char magic2;
  135. magic2 = bb_xread_char(src_fd);
  136. #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
  137. if (magic2 == 0x9d) {
  138. status = uncompress(src_fd, dst_fd);
  139. } else
  140. #endif
  141. if (magic2 == 0x8b) {
  142. check_header_gzip(src_fd);
  143. status = inflate_gunzip(src_fd, dst_fd);
  144. if (status != 0) {
  145. bb_error_msg_and_die("Error inflating");
  146. }
  147. } else {
  148. bb_error_msg_and_die("Invalid magic");
  149. }
  150. } else {
  151. bb_error_msg_and_die("Invalid magic");
  152. }
  153. if ((status != EXIT_SUCCESS) && (new_path)) {
  154. /* Unzip failed, remove new path instead of old path */
  155. delete_path = new_path;
  156. }
  157. if (dst_fd != STDOUT_FILENO) {
  158. close(dst_fd);
  159. }
  160. if (src_fd != STDIN_FILENO) {
  161. close(src_fd);
  162. }
  163. /* delete_path will be NULL if in test mode or from stdin */
  164. if (delete_path && (unlink(delete_path) == -1)) {
  165. bb_error_msg_and_die("Couldn't remove %s", delete_path);
  166. }
  167. free(new_path);
  168. } while (optind < argc);
  169. return status;
  170. }