Mirror of git://git.busybox.net/busybox with our patches on top
Source
9
9
*
10
10
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
11
11
*/
12
12
13
13
/* For reference see
14
14
* http://www.pkware.com/company/standards/appnote/
15
15
* http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
16
16
*/
17
17
18
18
/* TODO
19
-
* Endian issues
20
19
* Zip64 + other methods
21
-
* Improve handling of zip format, ie.
22
-
* - deferred CRC, comp. & uncomp. lengths (zip header flags bit 3)
23
-
* - unix file permissions, etc.
24
-
* - central directory
25
20
*/
26
21
27
22
#include "libbb.h"
28
23
#include "unarchive.h"
29
24
30
25
enum {
31
26
#if BB_BIG_ENDIAN
32
27
ZIP_FILEHEADER_MAGIC = 0x504b0304,
33
28
ZIP_CDS_MAGIC = 0x504b0102,
34
-
ZIP_CDS_END_MAGIC = 0x504b0506,
29
+
ZIP_CDE_MAGIC = 0x504b0506,
35
30
ZIP_DD_MAGIC = 0x504b0708,
36
31
#else
37
32
ZIP_FILEHEADER_MAGIC = 0x04034b50,
38
33
ZIP_CDS_MAGIC = 0x02014b50,
39
-
ZIP_CDS_END_MAGIC = 0x06054b50,
34
+
ZIP_CDE_MAGIC = 0x06054b50,
40
35
ZIP_DD_MAGIC = 0x08074b50,
41
36
#endif
42
37
};
43
38
44
39
#define ZIP_HEADER_LEN 26
45
40
46
41
typedef union {
47
42
uint8_t raw[ZIP_HEADER_LEN];
48
43
struct {
49
-
uint16_t version; /* 0-1 */
50
-
uint16_t flags; /* 2-3 */
51
-
uint16_t method; /* 4-5 */
52
-
uint16_t modtime; /* 6-7 */
53
-
uint16_t moddate; /* 8-9 */
54
-
uint32_t crc32 PACKED; /* 10-13 */
55
-
uint32_t cmpsize PACKED; /* 14-17 */
56
-
uint32_t ucmpsize PACKED; /* 18-21 */
57
-
uint16_t filename_len; /* 22-23 */
58
-
uint16_t extra_len; /* 24-25 */
44
+
uint16_t version; /* 0-1 */
45
+
uint16_t flags; /* 2-3 */
46
+
uint16_t method; /* 4-5 */
47
+
uint16_t modtime; /* 6-7 */
48
+
uint16_t moddate; /* 8-9 */
49
+
uint32_t crc32 PACKED; /* 10-13 */
50
+
uint32_t cmpsize PACKED; /* 14-17 */
51
+
uint32_t ucmpsize PACKED; /* 18-21 */
52
+
uint16_t filename_len; /* 22-23 */
53
+
uint16_t extra_len; /* 24-25 */
59
54
} formatted PACKED;
60
55
} zip_header_t; /* PACKED - gcc 4.2.1 doesn't like it (spews warning) */
61
56
62
57
/* Check the offset of the last element, not the length. This leniency
63
58
* allows for poor packing, whereby the overall struct may be too long,
64
59
* even though the elements are all in the right place.
65
60
*/
66
61
struct BUG_zip_header_must_be_26_bytes {
67
62
char BUG_zip_header_must_be_26_bytes[
68
-
offsetof(zip_header_t, formatted.extra_len) + 2 ==
69
-
ZIP_HEADER_LEN ? 1 : -1];
63
+
offsetof(zip_header_t, formatted.extra_len) + 2
64
+
== ZIP_HEADER_LEN ? 1 : -1];
70
65
};
71
66
72
-
#define FIX_ENDIANNESS(zip_header) do { \
67
+
#define FIX_ENDIANNESS_ZIP(zip_header) do { \
73
68
(zip_header).formatted.version = SWAP_LE16((zip_header).formatted.version ); \
74
69
(zip_header).formatted.flags = SWAP_LE16((zip_header).formatted.flags ); \
75
70
(zip_header).formatted.method = SWAP_LE16((zip_header).formatted.method ); \
76
71
(zip_header).formatted.modtime = SWAP_LE16((zip_header).formatted.modtime ); \
77
72
(zip_header).formatted.moddate = SWAP_LE16((zip_header).formatted.moddate ); \
78
73
(zip_header).formatted.crc32 = SWAP_LE32((zip_header).formatted.crc32 ); \
79
74
(zip_header).formatted.cmpsize = SWAP_LE32((zip_header).formatted.cmpsize ); \
80
75
(zip_header).formatted.ucmpsize = SWAP_LE32((zip_header).formatted.ucmpsize ); \
81
76
(zip_header).formatted.filename_len = SWAP_LE16((zip_header).formatted.filename_len); \
82
77
(zip_header).formatted.extra_len = SWAP_LE16((zip_header).formatted.extra_len ); \
83
78
} while (0)
84
79
85
-
static void unzip_skip(int fd, off_t skip)
80
+
#define CDS_HEADER_LEN 42
81
+
82
+
typedef union {
83
+
uint8_t raw[CDS_HEADER_LEN];
84
+
struct {
85
+
/* uint32_t signature; 50 4b 01 02 */
86
+
uint16_t version_made_by; /* 0-1 */
87
+
uint16_t version_needed; /* 2-3 */
88
+
uint16_t cds_flags; /* 4-5 */
89
+
uint16_t method; /* 6-7 */
90
+
uint16_t mtime; /* 8-9 */
91
+
uint16_t mdate; /* 10-11 */
92
+
uint32_t crc32; /* 12-15 */
93
+
uint32_t cmpsize; /* 16-19 */
94
+
uint32_t ucmpsize; /* 20-23 */
95
+
uint16_t file_name_length; /* 24-25 */
96
+
uint16_t extra_field_length; /* 26-27 */
97
+
uint16_t file_comment_length; /* 28-29 */
98
+
uint16_t disk_number_start; /* 30-31 */
99
+
uint16_t internal_file_attributes; /* 32-33 */
100
+
uint32_t external_file_attributes PACKED; /* 34-37 */
101
+
uint32_t relative_offset_of_local_header PACKED; /* 38-41 */
102
+
} formatted PACKED;
103
+
} cds_header_t;
104
+
105
+
struct BUG_cds_header_must_be_42_bytes {
106
+
char BUG_cds_header_must_be_42_bytes[
107
+
offsetof(cds_header_t, formatted.relative_offset_of_local_header) + 4
108
+
== CDS_HEADER_LEN ? 1 : -1];
109
+
};
110
+
111
+
#define FIX_ENDIANNESS_CDS(cds_header) do { \
112
+
(cds_header).formatted.crc32 = SWAP_LE32((cds_header).formatted.crc32 ); \
113
+
(cds_header).formatted.cmpsize = SWAP_LE32((cds_header).formatted.cmpsize ); \
114
+
(cds_header).formatted.ucmpsize = SWAP_LE32((cds_header).formatted.ucmpsize ); \
115
+
(cds_header).formatted.file_name_length = SWAP_LE16((cds_header).formatted.file_name_length); \
116
+
(cds_header).formatted.extra_field_length = SWAP_LE16((cds_header).formatted.extra_field_length); \
117
+
(cds_header).formatted.file_comment_length = SWAP_LE16((cds_header).formatted.file_comment_length); \
118
+
} while (0)
119
+
120
+
#define CDE_HEADER_LEN 16
121
+
122
+
typedef union {
123
+
uint8_t raw[CDE_HEADER_LEN];
124
+
struct {
125
+
/* uint32_t signature; 50 4b 05 06 */
126
+
uint16_t this_disk_no;
127
+
uint16_t disk_with_cds_no;
128
+
uint16_t cds_entries_on_this_disk;
129
+
uint16_t cds_entries_total;
130
+
uint32_t cds_size;
131
+
uint32_t cds_offset;
132
+
/* uint16_t file_comment_length; */
133
+
/* .ZIP file comment (variable size) */
134
+
} formatted PACKED;
135
+
} cde_header_t;
136
+
137
+
struct BUG_cde_header_must_be_16_bytes {
138
+
char BUG_cde_header_must_be_16_bytes[
139
+
sizeof(cde_header_t) == CDE_HEADER_LEN ? 1 : -1];
140
+
};
141
+
142
+
#define FIX_ENDIANNESS_CDE(cde_header) do { \
143
+
(cde_header).formatted.cds_offset = SWAP_LE16((cde_header).formatted.cds_offset); \
144
+
} while (0)
145
+
146
+
enum { zip_fd = 3 };
147
+
148
+
149
+
#if ENABLE_DESKTOP
150
+
/* NB: does not preserve file position! */
151
+
static uint32_t find_cds_offset(void)
152
+
{
153
+
unsigned char buf[1024];
154
+
cde_header_t cde_header;
155
+
unsigned char *p;
156
+
off_t end;
157
+
158
+
end = xlseek(zip_fd, 0, SEEK_END);
159
+
if (end < 1024)
160
+
end = 1024;
161
+
end -= 1024;
162
+
xlseek(zip_fd, end, SEEK_SET);
163
+
full_read(zip_fd, buf, 1024);
164
+
165
+
p = buf;
166
+
while (p <= buf + 1024 - CDE_HEADER_LEN - 4) {
167
+
if (*p != 'P') {
168
+
p++;
169
+
continue;
170
+
}
171
+
if (*++p != 'K')
172
+
continue;
173
+
if (*++p != 5)
174
+
continue;
175
+
if (*++p != 6)
176
+
continue;
177
+
/* we found CDE! */
178
+
memcpy(cde_header.raw, p + 1, CDE_HEADER_LEN);
179
+
FIX_ENDIANNESS_CDE(cde_header);
180
+
return cde_header.formatted.cds_offset;
181
+
}
182
+
bb_error_msg_and_die("can't find file table");
183
+
};
184
+
185
+
static uint32_t read_next_cds(int count_m1, uint32_t cds_offset, cds_header_t *cds_ptr)
86
186
{
87
-
bb_copyfd_exact_size(fd, -1, skip);
187
+
off_t org;
188
+
189
+
org = xlseek(zip_fd, 0, SEEK_CUR);
190
+
191
+
if (!cds_offset)
192
+
cds_offset = find_cds_offset();
193
+
194
+
while (count_m1-- >= 0) {
195
+
xlseek(zip_fd, cds_offset + 4, SEEK_SET);
196
+
xread(zip_fd, cds_ptr->raw, CDS_HEADER_LEN);
197
+
FIX_ENDIANNESS_CDS(*cds_ptr);
198
+
cds_offset += 4 + CDS_HEADER_LEN
199
+
+ cds_ptr->formatted.file_name_length
200
+
+ cds_ptr->formatted.extra_field_length
201
+
+ cds_ptr->formatted.file_comment_length;
202
+
}
203
+
204
+
xlseek(zip_fd, org, SEEK_SET);
205
+
return cds_offset;
206
+
};
207
+
#endif
208
+
209
+
static void unzip_skip(off_t skip)
210
+
{
211
+
bb_copyfd_exact_size(zip_fd, -1, skip);
88
212
}
89
213
90
214
static void unzip_create_leading_dirs(const char *fn)
91
215
{
92
216
/* Create all leading directories */
93
217
char *name = xstrdup(fn);
94
218
if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
95
219
bb_error_msg_and_die("exiting"); /* bb_make_directory is noisy */
96
220
}
97
221
free(name);
98
222
}
99
223
100
-
static void unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
224
+
static void unzip_extract(zip_header_t *zip_header, int dst_fd)
101
225
{
102
226
if (zip_header->formatted.method == 0) {
103
227
/* Method 0 - stored (not compressed) */
104
228
off_t size = zip_header->formatted.ucmpsize;
105
229
if (size)
106
-
bb_copyfd_exact_size(src_fd, dst_fd, size);
230
+
bb_copyfd_exact_size(zip_fd, dst_fd, size);
107
231
} else {
108
232
/* Method 8 - inflate */
109
233
inflate_unzip_result res;
110
-
if (inflate_unzip(&res, zip_header->formatted.cmpsize, src_fd, dst_fd) < 0)
234
+
if (inflate_unzip(&res, zip_header->formatted.cmpsize, zip_fd, dst_fd) < 0)
111
235
bb_error_msg_and_die("inflate error");
112
236
/* Validate decompression - crc */
113
237
if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) {
114
238
bb_error_msg_and_die("crc error");
115
239
}
116
240
/* Validate decompression - size */
117
241
if (zip_header->formatted.ucmpsize != res.bytes_out) {
118
242
/* Don't die. Who knows, maybe len calculation
119
243
* was botched somewhere. After all, crc matched! */
120
244
bb_error_msg("bad length");
124
248
125
249
int unzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
126
250
int unzip_main(int argc, char **argv)
127
251
{
128
252
enum { O_PROMPT, O_NEVER, O_ALWAYS };
129
253
130
254
zip_header_t zip_header;
131
255
smallint verbose = 1;
132
256
smallint listing = 0;
133
257
smallint overwrite = O_PROMPT;
258
+
#if ENABLE_DESKTOP
259
+
uint32_t cds_offset;
260
+
unsigned cds_entries;
261
+
#endif
134
262
unsigned total_size;
135
263
unsigned total_entries;
136
-
int src_fd = -1;
137
264
int dst_fd = -1;
138
265
char *src_fn = NULL;
139
266
char *dst_fn = NULL;
140
267
llist_t *zaccept = NULL;
141
268
llist_t *zreject = NULL;
142
269
char *base_dir = NULL;
143
270
int i, opt;
144
271
int opt_range = 0;
145
272
char key_buf[80];
146
273
struct stat stat_buf;
214
341
bb_show_usage();
215
342
}
216
343
}
217
344
218
345
if (src_fn == NULL) {
219
346
bb_show_usage();
220
347
}
221
348
222
349
/* Open input file */
223
350
if (LONE_DASH(src_fn)) {
224
-
src_fd = STDIN_FILENO;
351
+
xdup2(STDIN_FILENO, zip_fd);
225
352
/* Cannot use prompt mode since zip data is arriving on STDIN */
226
353
if (overwrite == O_PROMPT)
227
354
overwrite = O_NEVER;
228
355
} else {
229
356
static const char extn[][5] = {"", ".zip", ".ZIP"};
230
357
int orig_src_fn_len = strlen(src_fn);
358
+
int src_fd = -1;
231
359
232
360
for (i = 0; (i < 3) && (src_fd == -1); i++) {
233
361
strcpy(src_fn + orig_src_fn_len, extn[i]);
234
362
src_fd = open(src_fn, O_RDONLY);
235
363
}
236
364
if (src_fd == -1) {
237
365
src_fn[orig_src_fn_len] = '\0';
238
366
bb_error_msg_and_die("can't open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
239
367
}
368
+
xmove_fd(src_fd, zip_fd);
240
369
}
241
370
242
371
/* Change dir if necessary */
243
372
if (base_dir)
244
373
xchdir(base_dir);
245
374
246
375
if (verbose) {
247
376
printf("Archive: %s\n", src_fn);
248
377
if (listing){
249
378
puts(" Length Date Time Name\n"
250
379
" -------- ---- ---- ----");
251
380
}
252
381
}
253
382
254
383
total_size = 0;
255
384
total_entries = 0;
385
+
#if ENABLE_DESKTOP
386
+
cds_entries = 0;
387
+
cds_offset = 0;
388
+
#endif
256
389
while (1) {
257
390
uint32_t magic;
258
391
259
392
/* Check magic number */
260
-
xread(src_fd, &magic, 4);
393
+
xread(zip_fd, &magic, 4);
261
394
/* Central directory? It's at the end, so exit */
262
395
if (magic == ZIP_CDS_MAGIC)
263
396
break;
397
+
#if ENABLE_DESKTOP
398
+
/* Data descriptor? It was a streaming file, go on */
399
+
if (magic == ZIP_DD_MAGIC) {
400
+
/* skip over duplicate crc32, cmpsize and ucmpsize */
401
+
unzip_skip(3 * 4);
402
+
continue;
403
+
}
404
+
#endif
264
405
if (magic != ZIP_FILEHEADER_MAGIC)
265
406
bb_error_msg_and_die("invalid zip magic %08X", (int)magic);
266
407
267
408
/* Read the file header */
268
-
xread(src_fd, zip_header.raw, ZIP_HEADER_LEN);
269
-
FIX_ENDIANNESS(zip_header);
409
+
xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN);
410
+
FIX_ENDIANNESS_ZIP(zip_header);
270
411
if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
271
412
bb_error_msg_and_die("unsupported method %d", zip_header.formatted.method);
272
413
}
273
-
if (zip_header.formatted.flags & (0x0008|0x0001)) {
414
+
#if !ENABLE_DESKTOP
415
+
if (zip_header.formatted.flags & 0x0009) {
416
+
bb_error_msg_and_die("zip flags 1 and 8 are not supported");
417
+
}
418
+
#else
419
+
if (zip_header.formatted.flags & 0x0001) {
274
420
/* 0x0001 - encrypted */
421
+
bb_error_msg_and_die("zip flag 1 (encryption) is not supported");
422
+
}
423
+
if (zip_header.formatted.flags & 0x0008) {
424
+
cds_header_t cds_header;
275
425
/* 0x0008 - streaming. [u]cmpsize can be reliably gotten
276
426
* only from Central Directory. See unzip_doc.txt */
277
-
bb_error_msg_and_die("zip flags 8 and 1 are not supported");
427
+
cds_offset = read_next_cds(total_entries - cds_entries, cds_offset, &cds_header);
428
+
cds_entries = total_entries + 1;
429
+
zip_header.formatted.crc32 = cds_header.formatted.crc32;
430
+
zip_header.formatted.cmpsize = cds_header.formatted.cmpsize;
431
+
zip_header.formatted.ucmpsize = cds_header.formatted.ucmpsize;
278
432
}
433
+
#endif
279
434
280
435
/* Read filename */
281
436
free(dst_fn);
282
437
dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
283
-
xread(src_fd, dst_fn, zip_header.formatted.filename_len);
438
+
xread(zip_fd, dst_fn, zip_header.formatted.filename_len);
284
439
285
440
/* Skip extra header bytes */
286
-
unzip_skip(src_fd, zip_header.formatted.extra_len);
441
+
unzip_skip(zip_header.formatted.extra_len);
287
442
288
443
/* Filter zip entries */
289
444
if (find_list_entry(zreject, dst_fn)
290
445
|| (zaccept && !find_list_entry(zaccept, dst_fn))
291
446
) { /* Skip entry */
292
447
i = 'n';
293
448
294
449
} else { /* Extract entry */
295
450
if (listing) { /* List entry */
296
451
if (verbose) {
297
452
unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
298
453
printf("%9u %02u-%02u-%02u %02u:%02u %s\n",
299
454
zip_header.formatted.ucmpsize,
300
455
(dostime & 0x01e00000) >> 21,
301
456
(dostime & 0x001f0000) >> 16,
302
457
(((dostime & 0xfe000000) >> 25) + 1980) % 100,
303
458
(dostime & 0x0000f800) >> 11,
304
459
(dostime & 0x000007e0) >> 5,
305
460
dst_fn);
306
461
total_size += zip_header.formatted.ucmpsize;
307
-
total_entries++;
308
462
} else {
309
463
/* short listing -- filenames only */
310
464
puts(dst_fn);
311
465
}
312
466
i = 'n';
313
467
} else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
314
468
i = -1;
315
469
} else if (last_char_is(dst_fn, '/')) { /* Extract directory */
316
470
if (stat(dst_fn, &stat_buf) == -1) {
317
471
if (errno != ENOENT) {
318
-
bb_perror_msg_and_die("cannot stat '%s'", dst_fn);
472
+
bb_perror_msg_and_die("can't stat '%s'", dst_fn);
319
473
}
320
474
if (verbose) {
321
475
printf(" creating: %s\n", dst_fn);
322
476
}
323
477
unzip_create_leading_dirs(dst_fn);
324
478
if (bb_make_directory(dst_fn, 0777, 0)) {
325
479
bb_error_msg_and_die("exiting");
326
480
}
327
481
} else {
328
482
if (!S_ISDIR(stat_buf.st_mode)) {
329
483
bb_error_msg_and_die("'%s' exists but is not directory", dst_fn);
330
484
}
331
485
}
332
486
i = 'n';
333
487
334
488
} else { /* Extract file */
335
489
check_file:
336
490
if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
337
491
if (errno != ENOENT) {
338
-
bb_perror_msg_and_die("cannot stat '%s'", dst_fn);
492
+
bb_perror_msg_and_die("can't stat '%s'", dst_fn);
339
493
}
340
494
i = 'y';
341
495
} else { /* File already exists */
342
496
if (overwrite == O_NEVER) {
343
497
i = 'n';
344
498
} else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
345
499
if (overwrite == O_ALWAYS) {
346
500
i = 'y';
347
501
} else {
348
502
printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
349
503
if (!fgets(key_buf, sizeof(key_buf), stdin)) {
350
-
bb_perror_msg_and_die("cannot read input");
504
+
bb_perror_msg_and_die("can't read input");
351
505
}
352
506
i = key_buf[0];
353
507
}
354
508
} else { /* File is not regular file */
355
509
bb_error_msg_and_die("'%s' exists but is not regular file", dst_fn);
356
510
}
357
511
}
358
512
}
359
513
}
360
514
361
515
switch (i) {
362
516
case 'A':
363
517
overwrite = O_ALWAYS;
364
518
case 'y': /* Open file and fall into unzip */
365
519
unzip_create_leading_dirs(dst_fn);
366
520
dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
367
521
case -1: /* Unzip */
368
522
if (verbose) {
369
523
printf(" inflating: %s\n", dst_fn);
370
524
}
371
-
unzip_extract(&zip_header, src_fd, dst_fd);
525
+
unzip_extract(&zip_header, dst_fd);
372
526
if (dst_fd != STDOUT_FILENO) {
373
527
/* closing STDOUT is potentially bad for future business */
374
528
close(dst_fd);
375
529
}
376
530
break;
377
531
378
532
case 'N':
379
533
overwrite = O_NEVER;
380
534
case 'n':
381
535
/* Skip entry data */
382
-
unzip_skip(src_fd, zip_header.formatted.cmpsize);
536
+
unzip_skip(zip_header.formatted.cmpsize);
383
537
break;
384
538
385
539
case 'r':
386
540
/* Prompt for new name */
387
541
printf("new name: ");
388
542
if (!fgets(key_buf, sizeof(key_buf), stdin)) {
389
-
bb_perror_msg_and_die("cannot read input");
543
+
bb_perror_msg_and_die("can't read input");
390
544
}
391
545
free(dst_fn);
392
546
dst_fn = xstrdup(key_buf);
393
547
chomp(dst_fn);
394
548
goto check_file;
395
549
396
550
default:
397
551
printf("error: invalid response [%c]\n",(char)i);
398
552
goto check_file;
399
553
}
400
554
401
-
// Looks like bug (data descriptor cannot be identified this way)
402
-
// /* Data descriptor section */
403
-
// if (zip_header.formatted.flags & 4) {
404
-
// /* skip over duplicate crc, compressed size and uncompressed size */
405
-
// unzip_skip(src_fd, 12);
406
-
// }
555
+
total_entries++;
407
556
}
408
557
409
558
if (listing && verbose) {
410
559
printf(" -------- -------\n"
411
560
"%9d %d files\n",
412
561
total_size, total_entries);
413
562
}
414
563
415
564
return 0;
416
565
}