diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-09-25 02:47:48 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-09-25 02:47:48 +0000 |
commit | 7ca04f328e22fcbee4659d73f9a72dfdf1dd6a23 (patch) | |
tree | f38c7ef4317eea28c6abdb0adbbb286fe041711e /archival/unzip.c | |
parent | ecfa290cfd4953598e6d91989bd66ac16e135f84 (diff) | |
download | busybox-w32-7ca04f328e22fcbee4659d73f9a72dfdf1dd6a23.tar.gz busybox-w32-7ca04f328e22fcbee4659d73f9a72dfdf1dd6a23.tar.bz2 busybox-w32-7ca04f328e22fcbee4659d73f9a72dfdf1dd6a23.zip |
New common unarchive code.
Diffstat (limited to 'archival/unzip.c')
-rw-r--r-- | archival/unzip.c | 228 |
1 files changed, 189 insertions, 39 deletions
diff --git a/archival/unzip.c b/archival/unzip.c index ae0d7c1dc..5a22d242a 100644 --- a/archival/unzip.c +++ b/archival/unzip.c | |||
@@ -20,6 +20,10 @@ | |||
20 | * | 20 | * |
21 | */ | 21 | */ |
22 | 22 | ||
23 | /* For reference to format see http://www.pkware.com/support/appnote.html */ | ||
24 | |||
25 | /* TODO Endian issues, exclude, should we accept input from stdin ? */ | ||
26 | |||
23 | #include <fcntl.h> | 27 | #include <fcntl.h> |
24 | #include <getopt.h> | 28 | #include <getopt.h> |
25 | #include <stdlib.h> | 29 | #include <stdlib.h> |
@@ -28,67 +32,213 @@ | |||
28 | #include "unarchive.h" | 32 | #include "unarchive.h" |
29 | #include "busybox.h" | 33 | #include "busybox.h" |
30 | 34 | ||
35 | #define ZIP_FILEHEADER_MAGIC 0x04034b50 | ||
36 | #define ZIP_CDS_MAGIC 0x02014b50 | ||
37 | #define ZIP_CDS_END_MAGIC 0x06054b50 | ||
38 | #define ZIP_DD_MAGIC 0x08074b50 | ||
39 | |||
40 | extern unsigned int gunzip_crc; | ||
41 | extern unsigned int gunzip_bytes_out; | ||
42 | |||
43 | static void header_list_unzip(const file_header_t *file_header) | ||
44 | { | ||
45 | printf(" inflating: %s\n", file_header->name); | ||
46 | } | ||
47 | |||
48 | static void header_verbose_list_unzip(const file_header_t *file_header) | ||
49 | { | ||
50 | unsigned int dostime = (unsigned int) file_header->mtime; | ||
51 | |||
52 | /* can printf arguments cut of the decade component ? */ | ||
53 | unsigned short year = 1980 + ((dostime & 0xfe000000) >> 25); | ||
54 | while (year >= 100) { | ||
55 | year -= 100; | ||
56 | } | ||
57 | |||
58 | printf("%9u %02u-%02u-%02u %02u:%02u %s\n", | ||
59 | (unsigned int) file_header->size, | ||
60 | (dostime & 0x01e00000) >> 21, | ||
61 | (dostime & 0x001f0000) >> 16, | ||
62 | year, | ||
63 | (dostime & 0x0000f800) >> 11, | ||
64 | (dostime & 0x000007e0) >> 5, | ||
65 | file_header->name); | ||
66 | } | ||
67 | |||
31 | extern int unzip_main(int argc, char **argv) | 68 | extern int unzip_main(int argc, char **argv) |
32 | { | 69 | { |
33 | FILE *src_stream; | 70 | union { |
34 | int extract_function = extract_all_to_fs | extract_create_leading_dirs; | 71 | unsigned char raw[26]; |
35 | char **extract_names = NULL; | 72 | struct { |
36 | char **exclude_names = NULL; | 73 | unsigned short version; /* 0-1 */ |
74 | unsigned short flags; /* 2-3 */ | ||
75 | unsigned short method; /* 4-5 */ | ||
76 | unsigned short modtime; /* 6-7 */ | ||
77 | unsigned short moddate; /* 8-9 */ | ||
78 | unsigned int crc32 __attribute__ ((packed)); /* 10-13 */ | ||
79 | unsigned int cmpsize __attribute__ ((packed));; /* 14-17 */ | ||
80 | unsigned int ucmpsize __attribute__ ((packed));; /* 18-21 */ | ||
81 | unsigned short filename_len; /* 22-23 */ | ||
82 | unsigned short extra_len; /* 24-25 */ | ||
83 | } formated __attribute__ ((packed)); | ||
84 | } zip_header; | ||
85 | |||
86 | archive_handle_t *archive_handle; | ||
87 | unsigned int total_size = 0; | ||
88 | unsigned int total_entries = 0; | ||
89 | char *base_dir = NULL; | ||
37 | int opt = 0; | 90 | int opt = 0; |
38 | int num_of_entries = 0; | ||
39 | int exclude = 0; | ||
40 | char *outdir = "./"; | ||
41 | FILE *msgout = stdout; | ||
42 | 91 | ||
43 | while ((opt = getopt(argc, argv, "lnopqxd:")) != -1) { | 92 | /* Initialise */ |
93 | archive_handle = init_handle(); | ||
94 | archive_handle->action_data = NULL; | ||
95 | archive_handle->action_header = header_list_unzip; | ||
96 | |||
97 | while ((opt = getopt(argc, argv, "lnopqd:")) != -1) { | ||
44 | switch (opt) { | 98 | switch (opt) { |
45 | case 'l': | 99 | case 'l': /* list */ |
46 | extract_function |= extract_verbose_list; | 100 | archive_handle->action_header = header_verbose_list_unzip; |
47 | extract_function ^= extract_all_to_fs; | 101 | archive_handle->action_data = data_skip; |
48 | break; | 102 | break; |
49 | case 'n': | 103 | case 'n': /* never overwright existing files */ |
50 | break; | 104 | break; |
51 | case 'o': | 105 | case 'o': |
52 | extract_function |= extract_unconditional; | 106 | archive_handle->flags = ARCHIVE_EXTRACT_UNCONDITIONAL; |
53 | break; | 107 | break; |
54 | case 'p': | 108 | case 'p': /* extract files to stdout */ |
55 | extract_function |= extract_to_stdout; | 109 | archive_handle->action_data = data_extract_to_stdout; |
56 | extract_function ^= extract_all_to_fs; | ||
57 | /* FALLTHROUGH */ | ||
58 | case 'q': | ||
59 | msgout = xfopen("/dev/null", "w"); | ||
60 | break; | 110 | break; |
61 | case 'd': | 111 | case 'q': /* Extract files quietly */ |
62 | outdir = xstrdup(optarg); | 112 | archive_handle->action_header = header_skip; |
63 | strcat(outdir, "/"); | ||
64 | break; | 113 | break; |
65 | case 'x': | 114 | case 'd': /* Extract files to specified base directory*/ |
66 | exclude = 1; | 115 | base_dir = optarg; |
67 | break; | 116 | break; |
117 | #if 0 | ||
118 | case 'x': /* Exclude the specified files */ | ||
119 | archive_handle->filter = filter_accept_reject_list; | ||
120 | break; | ||
121 | #endif | ||
122 | default: | ||
123 | show_usage(); | ||
68 | } | 124 | } |
69 | } | 125 | } |
70 | 126 | ||
71 | if (optind == argc) { | 127 | if (argc == optind) { |
72 | show_usage(); | 128 | show_usage(); |
73 | } | 129 | } |
74 | 130 | ||
75 | if (*argv[optind] == '-') src_stream = stdin; | 131 | printf("Archive: %s\n", argv[optind]); |
76 | else src_stream = xfopen(argv[optind++], "r"); | 132 | if (archive_handle->action_header == header_verbose_list_unzip) { |
133 | printf(" Length Date Time Name\n"); | ||
134 | printf(" -------- ---- ---- ----\n"); | ||
135 | } | ||
77 | 136 | ||
78 | while (optind < argc) { | 137 | if (*argv[optind] == '-') { |
79 | if (exclude) { | 138 | archive_handle->src_fd = fileno(stdin); |
80 | exclude_names = xrealloc(exclude_names, sizeof(char *) * (num_of_entries + 2)); | ||
81 | exclude_names[num_of_entries] = xstrdup(argv[optind]); | ||
82 | } else { | 139 | } else { |
83 | extract_names = xrealloc(extract_names, sizeof(char *) * (num_of_entries + 2)); | 140 | archive_handle->src_fd = xopen(argv[optind++], O_RDONLY); |
84 | extract_names[num_of_entries] = xstrdup(argv[optind]); | ||
85 | } | 141 | } |
86 | num_of_entries++; | 142 | |
87 | if (exclude) exclude_names[num_of_entries] = NULL; | 143 | if ((base_dir) && (chdir(base_dir))) { |
88 | else extract_names[num_of_entries] = NULL; | 144 | perror_msg_and_die("Couldnt chdir"); |
145 | } | ||
146 | |||
147 | while (optind < argc) { | ||
148 | archive_handle->filter = filter_accept_list; | ||
149 | archive_handle->accept = add_to_list(archive_handle->accept, argv[optind]); | ||
89 | optind++; | 150 | optind++; |
90 | } | 151 | } |
91 | 152 | ||
92 | unarchive(src_stream, msgout, &get_header_zip, extract_function, outdir, extract_names, exclude_names); | 153 | while (1) { |
93 | return EXIT_SUCCESS; | 154 | unsigned int magic; |
155 | int dst_fd; | ||
156 | |||
157 | /* TODO Endian issues */ | ||
158 | xread_all(archive_handle->src_fd, &magic, 4); | ||
159 | archive_handle->offset += 4; | ||
160 | |||
161 | if (magic == ZIP_CDS_MAGIC) { | ||
162 | break; | ||
163 | } | ||
164 | else if (magic != ZIP_FILEHEADER_MAGIC) { | ||
165 | error_msg_and_die("Invlaide zip magic"); | ||
166 | } | ||
167 | |||
168 | /* Read the file header */ | ||
169 | xread_all(archive_handle->src_fd, zip_header.raw, 26); | ||
170 | archive_handle->offset += 26; | ||
171 | archive_handle->file_header->mode = S_IFREG | 0777; | ||
172 | |||
173 | if (zip_header.formated.method != 8) { | ||
174 | error_msg_and_die("Unsupported compression method %d\n", zip_header.formated.method); | ||
175 | } | ||
176 | |||
177 | /* Read filename */ | ||
178 | archive_handle->file_header->name = xmalloc(zip_header.formated.filename_len + 1); | ||
179 | xread_all(archive_handle->src_fd, archive_handle->file_header->name, zip_header.formated.filename_len); | ||
180 | archive_handle->offset += zip_header.formated.filename_len; | ||
181 | archive_handle->file_header->name[zip_header.formated.filename_len] = '\0'; | ||
182 | |||
183 | /* Skip extra header bits */ | ||
184 | archive_handle->file_header->size = zip_header.formated.extra_len; | ||
185 | data_skip(archive_handle); | ||
186 | archive_handle->offset += zip_header.formated.extra_len; | ||
187 | |||
188 | /* Handle directories */ | ||
189 | archive_handle->file_header->mode = S_IFREG | 0777; | ||
190 | if (last_char_is(archive_handle->file_header->name, '/')) { | ||
191 | archive_handle->file_header->mode ^= S_IFREG; | ||
192 | archive_handle->file_header->mode |= S_IFDIR; | ||
193 | } | ||
194 | |||
195 | /* Data section */ | ||
196 | archive_handle->file_header->size = zip_header.formated.cmpsize; | ||
197 | if (archive_handle->action_data) { | ||
198 | archive_handle->action_data(archive_handle); | ||
199 | } else { | ||
200 | dst_fd = xopen(archive_handle->file_header->name, O_WRONLY | O_CREAT); | ||
201 | inflate(archive_handle->src_fd, dst_fd); | ||
202 | close(dst_fd); | ||
203 | chmod(archive_handle->file_header->name, archive_handle->file_header->mode); | ||
204 | |||
205 | /* Validate decompression - crc */ | ||
206 | if (zip_header.formated.crc32 != (gunzip_crc ^ 0xffffffffL)) { | ||
207 | error_msg("Invalid compressed data--crc error"); | ||
208 | } | ||
209 | |||
210 | /* Validate decompression - size */ | ||
211 | if (gunzip_bytes_out != zip_header.formated.ucmpsize) { | ||
212 | error_msg("Invalid compressed data--length error"); | ||
213 | } | ||
214 | } | ||
215 | |||
216 | /* local file descriptor section */ | ||
217 | archive_handle->offset += zip_header.formated.cmpsize; | ||
218 | /* This ISNT unix time */ | ||
219 | archive_handle->file_header->mtime = zip_header.formated.modtime | (zip_header.formated.moddate << 16); | ||
220 | archive_handle->file_header->size = zip_header.formated.ucmpsize; | ||
221 | total_size += archive_handle->file_header->size; | ||
222 | total_entries++; | ||
223 | |||
224 | archive_handle->action_header(archive_handle->file_header); | ||
225 | |||
226 | /* Data descriptor section */ | ||
227 | if (zip_header.formated.flags & 4) { | ||
228 | /* skip over duplicate crc, compressed size and uncompressed size */ | ||
229 | unsigned short i; | ||
230 | for (i = 0; i != 12; i++) { | ||
231 | xread_char(archive_handle->src_fd); | ||
232 | } | ||
233 | archive_handle->offset += 12; | ||
234 | } | ||
235 | } | ||
236 | /* Central directory section */ | ||
237 | |||
238 | if (archive_handle->action_header == header_verbose_list_unzip) { | ||
239 | printf(" -------- -------\n"); | ||
240 | printf("%9d %d files\n", total_size, total_entries); | ||
241 | } | ||
242 | |||
243 | return(EXIT_SUCCESS); | ||
94 | } | 244 | } |