unzip.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini unzip implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Laurence Anderson
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* For reference see
  23. * http://www.pkware.com/products/enterprise/white_papers/appnote.txt
  24. * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
  25. */
  26. /* TODO Endian issues, exclude, should we accept input from stdin ? */
  27. #include <fcntl.h>
  28. #include <getopt.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include "unarchive.h"
  33. #include "busybox.h"
  34. #define ZIP_FILEHEADER_MAGIC 0x04034b50
  35. #define ZIP_CDS_MAGIC 0x02014b50
  36. #define ZIP_CDS_END_MAGIC 0x06054b50
  37. #define ZIP_DD_MAGIC 0x08074b50
  38. extern unsigned int gunzip_crc;
  39. extern unsigned int gunzip_bytes_out;
  40. static void header_list_unzip(const file_header_t *file_header)
  41. {
  42. printf(" inflating: %s\n", file_header->name);
  43. }
  44. static void header_verbose_list_unzip(const file_header_t *file_header)
  45. {
  46. unsigned int dostime = (unsigned int) file_header->mtime;
  47. /* can printf arguments cut of the decade component ? */
  48. unsigned short year = 1980 + ((dostime & 0xfe000000) >> 25);
  49. while (year >= 100) {
  50. year -= 100;
  51. }
  52. printf("%9u %02u-%02u-%02u %02u:%02u %s\n",
  53. (unsigned int) file_header->size,
  54. (dostime & 0x01e00000) >> 21,
  55. (dostime & 0x001f0000) >> 16,
  56. year,
  57. (dostime & 0x0000f800) >> 11,
  58. (dostime & 0x000007e0) >> 5,
  59. file_header->name);
  60. }
  61. extern int unzip_main(int argc, char **argv)
  62. {
  63. union {
  64. unsigned char raw[26];
  65. struct {
  66. unsigned short version; /* 0-1 */
  67. unsigned short flags; /* 2-3 */
  68. unsigned short method; /* 4-5 */
  69. unsigned short modtime; /* 6-7 */
  70. unsigned short moddate; /* 8-9 */
  71. unsigned int crc32 __attribute__ ((packed)); /* 10-13 */
  72. unsigned int cmpsize __attribute__ ((packed));; /* 14-17 */
  73. unsigned int ucmpsize __attribute__ ((packed));; /* 18-21 */
  74. unsigned short filename_len; /* 22-23 */
  75. unsigned short extra_len; /* 24-25 */
  76. } formated __attribute__ ((packed));
  77. } zip_header;
  78. archive_handle_t *archive_handle;
  79. unsigned int total_size = 0;
  80. unsigned int total_entries = 0;
  81. char *base_dir = NULL;
  82. int opt = 0;
  83. /* Initialise */
  84. archive_handle = init_handle();
  85. archive_handle->action_data = NULL;
  86. archive_handle->action_header = header_list_unzip;
  87. while ((opt = getopt(argc, argv, "lnopqd:")) != -1) {
  88. switch (opt) {
  89. case 'l': /* list */
  90. archive_handle->action_header = header_verbose_list_unzip;
  91. archive_handle->action_data = data_skip;
  92. break;
  93. case 'n': /* never overwright existing files */
  94. break;
  95. case 'o':
  96. archive_handle->flags = ARCHIVE_EXTRACT_UNCONDITIONAL;
  97. break;
  98. case 'p': /* extract files to stdout */
  99. archive_handle->action_data = data_extract_to_stdout;
  100. break;
  101. case 'q': /* Extract files quietly */
  102. archive_handle->action_header = header_skip;
  103. break;
  104. case 'd': /* Extract files to specified base directory*/
  105. base_dir = optarg;
  106. break;
  107. #if 0
  108. case 'x': /* Exclude the specified files */
  109. archive_handle->filter = filter_accept_reject_list;
  110. break;
  111. #endif
  112. default:
  113. bb_show_usage();
  114. }
  115. }
  116. if (argc == optind) {
  117. bb_show_usage();
  118. }
  119. printf("Archive: %s\n", argv[optind]);
  120. if (archive_handle->action_header == header_verbose_list_unzip) {
  121. printf(" Length Date Time Name\n");
  122. printf(" -------- ---- ---- ----\n");
  123. }
  124. if (*argv[optind] == '-') {
  125. archive_handle->src_fd = STDIN_FILENO;
  126. archive_handle->seek = seek_by_char;
  127. } else {
  128. archive_handle->src_fd = bb_xopen(argv[optind++], O_RDONLY);
  129. }
  130. if ((base_dir) && (chdir(base_dir))) {
  131. bb_perror_msg_and_die("Couldnt chdir");
  132. }
  133. while (optind < argc) {
  134. archive_handle->filter = filter_accept_list;
  135. archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]);
  136. optind++;
  137. }
  138. while (1) {
  139. unsigned int magic;
  140. int dst_fd;
  141. /* TODO Endian issues */
  142. archive_xread_all(archive_handle, &magic, 4);
  143. archive_handle->offset += 4;
  144. if (magic == ZIP_CDS_MAGIC) {
  145. break;
  146. }
  147. else if (magic != ZIP_FILEHEADER_MAGIC) {
  148. bb_error_msg_and_die("Invlaide zip magic");
  149. }
  150. /* Read the file header */
  151. archive_xread_all(archive_handle, zip_header.raw, 26);
  152. archive_handle->offset += 26;
  153. archive_handle->file_header->mode = S_IFREG | 0777;
  154. if (zip_header.formated.method != 8) {
  155. bb_error_msg_and_die("Unsupported compression method %d\n", zip_header.formated.method);
  156. }
  157. /* Read filename */
  158. archive_handle->file_header->name = xmalloc(zip_header.formated.filename_len + 1);
  159. archive_xread_all(archive_handle, archive_handle->file_header->name, zip_header.formated.filename_len);
  160. archive_handle->offset += zip_header.formated.filename_len;
  161. archive_handle->file_header->name[zip_header.formated.filename_len] = '\0';
  162. /* Skip extra header bits */
  163. archive_handle->file_header->size = zip_header.formated.extra_len;
  164. data_skip(archive_handle);
  165. archive_handle->offset += zip_header.formated.extra_len;
  166. /* Handle directories */
  167. archive_handle->file_header->mode = S_IFREG | 0777;
  168. if (last_char_is(archive_handle->file_header->name, '/')) {
  169. archive_handle->file_header->mode ^= S_IFREG;
  170. archive_handle->file_header->mode |= S_IFDIR;
  171. }
  172. /* Data section */
  173. archive_handle->file_header->size = zip_header.formated.cmpsize;
  174. if (archive_handle->action_data) {
  175. archive_handle->action_data(archive_handle);
  176. } else {
  177. dst_fd = bb_xopen(archive_handle->file_header->name, O_WRONLY | O_CREAT);
  178. inflate_init(zip_header.formated.cmpsize);
  179. inflate_unzip(archive_handle->src_fd, dst_fd);
  180. close(dst_fd);
  181. chmod(archive_handle->file_header->name, archive_handle->file_header->mode);
  182. /* Validate decompression - crc */
  183. if (zip_header.formated.crc32 != (gunzip_crc ^ 0xffffffffL)) {
  184. bb_error_msg("Invalid compressed data--crc error");
  185. }
  186. /* Validate decompression - size */
  187. if (gunzip_bytes_out != zip_header.formated.ucmpsize) {
  188. bb_error_msg("Invalid compressed data--length error");
  189. }
  190. }
  191. /* local file descriptor section */
  192. archive_handle->offset += zip_header.formated.cmpsize;
  193. /* This ISNT unix time */
  194. archive_handle->file_header->mtime = zip_header.formated.modtime | (zip_header.formated.moddate << 16);
  195. archive_handle->file_header->size = zip_header.formated.ucmpsize;
  196. total_size += archive_handle->file_header->size;
  197. total_entries++;
  198. archive_handle->action_header(archive_handle->file_header);
  199. /* Data descriptor section */
  200. if (zip_header.formated.flags & 4) {
  201. /* skip over duplicate crc, compressed size and uncompressed size */
  202. unsigned char data_description[12];
  203. archive_xread_all(archive_handle, data_description, 12);
  204. archive_handle->offset += 12;
  205. }
  206. }
  207. /* Central directory section */
  208. if (archive_handle->action_header == header_verbose_list_unzip) {
  209. printf(" -------- -------\n");
  210. printf("%9d %d files\n", total_size, total_entries);
  211. }
  212. return(EXIT_SUCCESS);
  213. }