gunzip.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <getopt.h>
  65. #include <sys/types.h>
  66. #include <sys/stat.h>
  67. #include <fcntl.h>
  68. #include "busybox.h"
  69. #include "unarchive.h"
  70. #define GUNZIP_OPT_STDOUT 1
  71. #define GUNZIP_OPT_FORCE 2
  72. #define GUNZIP_OPT_TEST 4
  73. #define GUNZIP_OPT_DECOMPRESS 8
  74. extern int gunzip_main(int argc, char **argv)
  75. {
  76. char status = EXIT_SUCCESS;
  77. unsigned long opt;
  78. opt = bb_getopt_ulflags(argc, argv, "cftd");
  79. /* if called as zcat */
  80. if (strcmp(bb_applet_name, "zcat") == 0) {
  81. opt |= GUNZIP_OPT_STDOUT;
  82. }
  83. do {
  84. struct stat stat_buf;
  85. const char *old_path = argv[optind];
  86. const char *delete_path = NULL;
  87. char *new_path = NULL;
  88. int src_fd;
  89. int dst_fd;
  90. optind++;
  91. if (old_path == NULL || strcmp(old_path, "-") == 0) {
  92. src_fd = STDIN_FILENO;
  93. opt |= GUNZIP_OPT_STDOUT;
  94. } else {
  95. src_fd = bb_xopen(old_path, O_RDONLY);
  96. /* Get the time stamp on the input file. */
  97. if (stat(old_path, &stat_buf) < 0) {
  98. bb_error_msg_and_die("Couldn't stat file %s", old_path);
  99. }
  100. }
  101. /* Check that the input is sane. */
  102. if (isatty(src_fd) && ((opt & GUNZIP_OPT_FORCE) == 0)) {
  103. bb_error_msg_and_die
  104. ("compressed data not read from terminal. Use -f to force it.");
  105. }
  106. /* Set output filename and number */
  107. if (opt & GUNZIP_OPT_TEST) {
  108. dst_fd = bb_xopen("/dev/null", O_WRONLY); /* why does test use filenum 2 ? */
  109. } else if (opt & GUNZIP_OPT_STDOUT) {
  110. dst_fd = STDOUT_FILENO;
  111. } else {
  112. char *extension;
  113. new_path = bb_xstrdup(old_path);
  114. extension = strrchr(new_path, '.');
  115. #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
  116. if (extension && (strcmp(extension, ".Z") == 0)) {
  117. *extension = '\0';
  118. } else
  119. #endif
  120. if (extension && (strcmp(extension, ".gz") == 0)) {
  121. *extension = '\0';
  122. } else if (extension && (strcmp(extension, ".tgz") == 0)) {
  123. extension[2] = 'a';
  124. extension[3] = 'r';
  125. } else {
  126. bb_error_msg_and_die("Invalid extension");
  127. }
  128. /* Open output file */
  129. dst_fd = bb_xopen(new_path, O_WRONLY | O_CREAT);
  130. /* Set permissions on the file */
  131. chmod(new_path, stat_buf.st_mode);
  132. /* If unzip succeeds remove the old file */
  133. delete_path = old_path;
  134. }
  135. /* do the decompression, and cleanup */
  136. if (bb_xread_char(src_fd) == 0x1f) {
  137. unsigned char magic2;
  138. magic2 = bb_xread_char(src_fd);
  139. #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
  140. if (magic2 == 0x9d) {
  141. status = uncompress(src_fd, dst_fd);
  142. } else
  143. #endif
  144. if (magic2 == 0x8b) {
  145. check_header_gzip(src_fd);
  146. status = inflate_gunzip(src_fd, dst_fd);
  147. if (status != 0) {
  148. bb_error_msg_and_die("Error inflating");
  149. }
  150. } else {
  151. bb_error_msg_and_die("Invalid magic");
  152. }
  153. } else {
  154. bb_error_msg_and_die("Invalid magic");
  155. }
  156. if ((status != EXIT_SUCCESS) && (new_path)) {
  157. /* Unzip failed, remove new path instead of old path */
  158. delete_path = new_path;
  159. }
  160. if (dst_fd != STDOUT_FILENO) {
  161. close(dst_fd);
  162. }
  163. if (src_fd != STDIN_FILENO) {
  164. close(src_fd);
  165. }
  166. /* delete_path will be NULL if in test mode or from stdin */
  167. if (delete_path && (unlink(delete_path) == -1)) {
  168. bb_error_msg_and_die("Couldn't remove %s", delete_path);
  169. }
  170. free(new_path);
  171. } while (optind < argc);
  172. return status;
  173. }