/
archive_read_support_format_zip.c
3062 lines (2788 loc) · 86 KB
/
archive_read_support_format_zip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2004-2013 Tim Kientzle
* Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
* Copyright (c) 2013 Konrad Kleine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "archive_platform.h"
__FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_zip.c 201102 2009-12-28 03:11:36Z kientzle $");
/*
* The definitive documentation of the Zip file format is:
* http://www.pkware.com/documents/casestudies/APPNOTE.TXT
*
* The Info-Zip project has pioneered various extensions to better
* support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855
* "Ux", and 0x7875 "ux" extensions for time and ownership
* information.
*
* History of this code: The streaming Zip reader was first added to
* libarchive in January 2005. Support for seekable input sources was
* added in Nov 2011. Zip64 support (including a significant code
* refactoring) was added in 2014.
*/
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#endif
#include "archive.h"
#include "archive_digest_private.h"
#include "archive_cryptor_private.h"
#include "archive_endian.h"
#include "archive_entry.h"
#include "archive_entry_locale.h"
#include "archive_hmac_private.h"
#include "archive_private.h"
#include "archive_rb.h"
#include "archive_read_private.h"
#ifndef HAVE_ZLIB_H
#include "archive_crc32.h"
#endif
struct zip_entry {
struct archive_rb_node node;
struct zip_entry *next;
int64_t local_header_offset;
int64_t compressed_size;
int64_t uncompressed_size;
int64_t gid;
int64_t uid;
struct archive_string rsrcname;
time_t mtime;
time_t atime;
time_t ctime;
uint32_t crc32;
uint16_t mode;
uint16_t zip_flags; /* From GP Flags Field */
unsigned char compression;
unsigned char system; /* From "version written by" */
unsigned char flags; /* Our extra markers. */
unsigned char decdat;/* Used for Decryption check */
/* WinZip AES encryption extra field should be available
* when compression is 99. */
struct {
/* Vendor version: AE-1 - 0x0001, AE-2 - 0x0002 */
unsigned vendor;
#define AES_VENDOR_AE_1 0x0001
#define AES_VENDOR_AE_2 0x0002
/* AES encryption strength:
* 1 - 128 bits, 2 - 192 bits, 2 - 256 bits. */
unsigned strength;
/* Actual compression method. */
unsigned char compression;
} aes_extra;
};
struct trad_enc_ctx {
uint32_t keys[3];
};
/* Bits used in zip_flags. */
#define ZIP_ENCRYPTED (1 << 0)
#define ZIP_LENGTH_AT_END (1 << 3)
#define ZIP_STRONG_ENCRYPTED (1 << 6)
#define ZIP_UTF8_NAME (1 << 11)
/* See "7.2 Single Password Symmetric Encryption Method"
in http://www.pkware.com/documents/casestudies/APPNOTE.TXT */
#define ZIP_CENTRAL_DIRECTORY_ENCRYPTED (1 << 13)
/* Bits used in flags. */
#define LA_USED_ZIP64 (1 << 0)
#define LA_FROM_CENTRAL_DIRECTORY (1 << 1)
/*
* See "WinZip - AES Encryption Information"
* http://www.winzip.com/aes_info.htm
*/
/* Value used in compression method. */
#define WINZIP_AES_ENCRYPTION 99
/* Authentication code size. */
#define AUTH_CODE_SIZE 10
/**/
#define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
struct zip {
/* Structural information about the archive. */
struct archive_string format_name;
int64_t central_directory_offset;
size_t central_directory_entries_total;
size_t central_directory_entries_on_this_disk;
int has_encrypted_entries;
/* List of entries (seekable Zip only) */
struct zip_entry *zip_entries;
struct archive_rb_tree tree;
struct archive_rb_tree tree_rsrc;
/* Bytes read but not yet consumed via __archive_read_consume() */
size_t unconsumed;
/* Information about entry we're currently reading. */
struct zip_entry *entry;
int64_t entry_bytes_remaining;
/* These count the number of bytes actually read for the entry. */
int64_t entry_compressed_bytes_read;
int64_t entry_uncompressed_bytes_read;
/* Running CRC32 of the decompressed data */
unsigned long entry_crc32;
unsigned long (*crc32func)(unsigned long, const void *,
size_t);
char ignore_crc32;
/* Flags to mark progress of decompression. */
char decompress_init;
char end_of_entry;
#ifdef HAVE_ZLIB_H
unsigned char *uncompressed_buffer;
size_t uncompressed_buffer_size;
z_stream stream;
char stream_valid;
#endif
struct archive_string_conv *sconv;
struct archive_string_conv *sconv_default;
struct archive_string_conv *sconv_utf8;
int init_default_conversion;
int process_mac_extensions;
char init_decryption;
/* Decryption buffer. */
/*
* The decrypted data starts at decrypted_ptr and
* extends for decrypted_bytes_remaining. Decryption
* adds new data to the end of this block, data is returned
* to clients from the beginning. When the block hits the
* end of decrypted_buffer, it has to be shuffled back to
* the beginning of the buffer.
*/
unsigned char *decrypted_buffer;
unsigned char *decrypted_ptr;
size_t decrypted_buffer_size;
size_t decrypted_bytes_remaining;
size_t decrypted_unconsumed_bytes;
/* Traditional PKWARE decryption. */
struct trad_enc_ctx tctx;
char tctx_valid;
/* WinZip AES decyption. */
/* Contexts used for AES decryption. */
archive_crypto_ctx cctx;
char cctx_valid;
archive_hmac_sha1_ctx hctx;
char hctx_valid;
/* Strong encryption's decryption header information. */
unsigned iv_size;
unsigned alg_id;
unsigned bit_len;
unsigned flags;
unsigned erd_size;
unsigned v_size;
unsigned v_crc32;
uint8_t *iv;
uint8_t *erd;
uint8_t *v_data;
};
/* Many systems define min or MIN, but not all. */
#define zipmin(a,b) ((a) < (b) ? (a) : (b))
/* ------------------------------------------------------------------------ */
/*
Traditional PKWARE Decryption functions.
*/
static void
trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
{
uint8_t t;
#define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
ctx->keys[0] = CRC32(ctx->keys[0], c);
ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
t = (ctx->keys[1] >> 24) & 0xff;
ctx->keys[2] = CRC32(ctx->keys[2], t);
#undef CRC32
}
static uint8_t
trad_enc_decypt_byte(struct trad_enc_ctx *ctx)
{
unsigned temp = ctx->keys[2] | 2;
return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
}
static void
trad_enc_decrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
size_t in_len, uint8_t *out, size_t out_len)
{
unsigned i, max;
max = (unsigned)((in_len < out_len)? in_len: out_len);
for (i = 0; i < max; i++) {
uint8_t t = in[i] ^ trad_enc_decypt_byte(ctx);
out[i] = t;
trad_enc_update_keys(ctx, t);
}
}
static int
trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len,
const uint8_t *key, size_t key_len, uint8_t *crcchk)
{
uint8_t header[12];
if (key_len < 12) {
*crcchk = 0xff;
return -1;
}
ctx->keys[0] = 305419896L;
ctx->keys[1] = 591751049L;
ctx->keys[2] = 878082192L;
for (;pw_len; --pw_len)
trad_enc_update_keys(ctx, *pw++);
trad_enc_decrypt_update(ctx, key, 12, header, 12);
/* Return the last byte for CRC check. */
*crcchk = header[11];
return 0;
}
#if 0
static void
crypt_derive_key_sha1(const void *p, int size, unsigned char *key,
int key_size)
{
#define MD_SIZE 20
archive_sha1_ctx ctx;
unsigned char md1[MD_SIZE];
unsigned char md2[MD_SIZE * 2];
unsigned char mkb[64];
int i;
archive_sha1_init(&ctx);
archive_sha1_update(&ctx, p, size);
archive_sha1_final(&ctx, md1);
memset(mkb, 0x36, sizeof(mkb));
for (i = 0; i < MD_SIZE; i++)
mkb[i] ^= md1[i];
archive_sha1_init(&ctx);
archive_sha1_update(&ctx, mkb, sizeof(mkb));
archive_sha1_final(&ctx, md2);
memset(mkb, 0x5C, sizeof(mkb));
for (i = 0; i < MD_SIZE; i++)
mkb[i] ^= md1[i];
archive_sha1_init(&ctx);
archive_sha1_update(&ctx, mkb, sizeof(mkb));
archive_sha1_final(&ctx, md2 + MD_SIZE);
if (key_size > 32)
key_size = 32;
memcpy(key, md2, key_size);
#undef MD_SIZE
}
#endif
/*
* Common code for streaming or seeking modes.
*
* Includes code to read local file headers, decompress data
* from entry bodies, and common API.
*/
static unsigned long
real_crc32(unsigned long crc, const void *buff, size_t len)
{
return crc32(crc, buff, (unsigned int)len);
}
/* Used by "ignorecrc32" option to speed up tests. */
static unsigned long
fake_crc32(unsigned long crc, const void *buff, size_t len)
{
(void)crc; /* UNUSED */
(void)buff; /* UNUSED */
(void)len; /* UNUSED */
return 0;
}
static struct {
int id;
const char * name;
} compression_methods[] = {
{0, "uncompressed"}, /* The file is stored (no compression) */
{1, "shrinking"}, /* The file is Shrunk */
{2, "reduced-1"}, /* The file is Reduced with compression factor 1 */
{3, "reduced-2"}, /* The file is Reduced with compression factor 2 */
{4, "reduced-3"}, /* The file is Reduced with compression factor 3 */
{5, "reduced-4"}, /* The file is Reduced with compression factor 4 */
{6, "imploded"}, /* The file is Imploded */
{7, "reserved"}, /* Reserved for Tokenizing compression algorithm */
{8, "deflation"}, /* The file is Deflated */
{9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */
{10, "ibm-terse"},/* PKWARE Data Compression Library Imploding
* (old IBM TERSE) */
{11, "reserved"}, /* Reserved by PKWARE */
{12, "bzip"}, /* File is compressed using BZIP2 algorithm */
{13, "reserved"}, /* Reserved by PKWARE */
{14, "lzma"}, /* LZMA (EFS) */
{15, "reserved"}, /* Reserved by PKWARE */
{16, "reserved"}, /* Reserved by PKWARE */
{17, "reserved"}, /* Reserved by PKWARE */
{18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */
{19, "ibm-lz777"},/* IBM LZ77 z Architecture (PFS) */
{97, "wav-pack"}, /* WavPack compressed data */
{98, "ppmd-1"}, /* PPMd version I, Rev 1 */
{99, "aes"} /* WinZip AES encryption */
};
static const char *
compression_name(const int compression)
{
static const int num_compression_methods =
sizeof(compression_methods)/sizeof(compression_methods[0]);
int i=0;
while(compression >= 0 && i < num_compression_methods) {
if (compression_methods[i].id == compression)
return compression_methods[i].name;
i++;
}
return "??";
}
/* Convert an MSDOS-style date/time into Unix-style time. */
static time_t
zip_time(const char *p)
{
int msTime, msDate;
struct tm ts;
msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
memset(&ts, 0, sizeof(ts));
ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
ts.tm_mday = msDate & 0x1f; /* Day of month. */
ts.tm_hour = (msTime >> 11) & 0x1f;
ts.tm_min = (msTime >> 5) & 0x3f;
ts.tm_sec = (msTime << 1) & 0x3e;
ts.tm_isdst = -1;
return mktime(&ts);
}
/*
* The extra data is stored as a list of
* id1+size1+data1 + id2+size2+data2 ...
* triplets. id and size are 2 bytes each.
*/
static void
process_extra(const char *p, size_t extra_length, struct zip_entry* zip_entry)
{
unsigned offset = 0;
while (offset < extra_length - 4) {
unsigned short headerid = archive_le16dec(p + offset);
unsigned short datasize = archive_le16dec(p + offset + 2);
offset += 4;
if (offset + datasize > extra_length) {
break;
}
#ifdef DEBUG
fprintf(stderr, "Header id 0x%04x, length %d\n",
headerid, datasize);
#endif
switch (headerid) {
case 0x0001:
/* Zip64 extended information extra field. */
zip_entry->flags |= LA_USED_ZIP64;
if (zip_entry->uncompressed_size == 0xffffffff) {
if (datasize < 8)
break;
zip_entry->uncompressed_size =
archive_le64dec(p + offset);
offset += 8;
datasize -= 8;
}
if (zip_entry->compressed_size == 0xffffffff) {
if (datasize < 8)
break;
zip_entry->compressed_size =
archive_le64dec(p + offset);
offset += 8;
datasize -= 8;
}
if (zip_entry->local_header_offset == 0xffffffff) {
if (datasize < 8)
break;
zip_entry->local_header_offset =
archive_le64dec(p + offset);
offset += 8;
datasize -= 8;
}
/* archive_le32dec(p + offset) gives disk
* on which file starts, but we don't handle
* multi-volume Zip files. */
break;
#ifdef DEBUG
case 0x0017:
{
/* Strong encryption field. */
if (archive_le16dec(p + offset) == 2) {
unsigned algId =
archive_le16dec(p + offset + 2);
unsigned bitLen =
archive_le16dec(p + offset + 4);
int flags =
archive_le16dec(p + offset + 6);
fprintf(stderr, "algId=0x%04x, bitLen=%u, "
"flgas=%d\n", algId, bitLen,flags);
}
break;
}
#endif
case 0x5455:
{
/* Extended time field "UT". */
int flags = p[offset];
offset++;
datasize--;
/* Flag bits indicate which dates are present. */
if (flags & 0x01)
{
#ifdef DEBUG
fprintf(stderr, "mtime: %lld -> %d\n",
(long long)zip_entry->mtime,
archive_le32dec(p + offset));
#endif
if (datasize < 4)
break;
zip_entry->mtime = archive_le32dec(p + offset);
offset += 4;
datasize -= 4;
}
if (flags & 0x02)
{
if (datasize < 4)
break;
zip_entry->atime = archive_le32dec(p + offset);
offset += 4;
datasize -= 4;
}
if (flags & 0x04)
{
if (datasize < 4)
break;
zip_entry->ctime = archive_le32dec(p + offset);
offset += 4;
datasize -= 4;
}
break;
}
case 0x5855:
{
/* Info-ZIP Unix Extra Field (old version) "UX". */
if (datasize >= 8) {
zip_entry->atime = archive_le32dec(p + offset);
zip_entry->mtime =
archive_le32dec(p + offset + 4);
}
if (datasize >= 12) {
zip_entry->uid =
archive_le16dec(p + offset + 8);
zip_entry->gid =
archive_le16dec(p + offset + 10);
}
break;
}
case 0x6c78:
{
/* Experimental 'xl' field */
/*
* Introduced Dec 2013 to provide a way to
* include external file attributes (and other
* fields that ordinarily appear only in
* central directory) in local file header.
* This provides file type and permission
* information necessary to support full
* streaming extraction. Currently being
* discussed with other Zip developers
* ... subject to change.
*
* Format:
* The field starts with a bitmap that specifies
* which additional fields are included. The
* bitmap is variable length and can be extended in
* the future.
*
* n bytes - feature bitmap: first byte has low-order
* 7 bits. If high-order bit is set, a subsequent
* byte holds the next 7 bits, etc.
*
* if bitmap & 1, 2 byte "version made by"
* if bitmap & 2, 2 byte "internal file attributes"
* if bitmap & 4, 4 byte "external file attributes"
* if bitmap & 8, 2 byte comment length + n byte comment
*/
int bitmap, bitmap_last;
if (datasize < 1)
break;
bitmap_last = bitmap = 0xff & p[offset];
offset += 1;
datasize -= 1;
/* We only support first 7 bits of bitmap; skip rest. */
while ((bitmap_last & 0x80) != 0
&& datasize >= 1) {
bitmap_last = p[offset];
offset += 1;
datasize -= 1;
}
if (bitmap & 1) {
/* 2 byte "version made by" */
if (datasize < 2)
break;
zip_entry->system
= archive_le16dec(p + offset) >> 8;
offset += 2;
datasize -= 2;
}
if (bitmap & 2) {
/* 2 byte "internal file attributes" */
uint32_t internal_attributes;
if (datasize < 2)
break;
internal_attributes
= archive_le16dec(p + offset);
/* Not used by libarchive at present. */
(void)internal_attributes; /* UNUSED */
offset += 2;
datasize -= 2;
}
if (bitmap & 4) {
/* 4 byte "external file attributes" */
uint32_t external_attributes;
if (datasize < 4)
break;
external_attributes
= archive_le32dec(p + offset);
if (zip_entry->system == 3) {
zip_entry->mode
= external_attributes >> 16;
} else if (zip_entry->system == 0) {
// Interpret MSDOS directory bit
if (0x10 == (external_attributes & 0x10)) {
zip_entry->mode = AE_IFDIR | 0775;
} else {
zip_entry->mode = AE_IFREG | 0664;
}
if (0x01 == (external_attributes & 0x01)) {
// Read-only bit; strip write permissions
zip_entry->mode &= 0555;
}
} else {
zip_entry->mode = 0;
}
offset += 4;
datasize -= 4;
}
if (bitmap & 8) {
/* 2 byte comment length + comment */
uint32_t comment_length;
if (datasize < 2)
break;
comment_length
= archive_le16dec(p + offset);
offset += 2;
datasize -= 2;
if (datasize < comment_length)
break;
/* Comment is not supported by libarchive */
offset += comment_length;
datasize -= comment_length;
}
break;
}
case 0x7855:
/* Info-ZIP Unix Extra Field (type 2) "Ux". */
#ifdef DEBUG
fprintf(stderr, "uid %d gid %d\n",
archive_le16dec(p + offset),
archive_le16dec(p + offset + 2));
#endif
if (datasize >= 2)
zip_entry->uid = archive_le16dec(p + offset);
if (datasize >= 4)
zip_entry->gid =
archive_le16dec(p + offset + 2);
break;
case 0x7875:
{
/* Info-Zip Unix Extra Field (type 3) "ux". */
int uidsize = 0, gidsize = 0;
/* TODO: support arbitrary uidsize/gidsize. */
if (datasize >= 1 && p[offset] == 1) {/* version=1 */
if (datasize >= 4) {
/* get a uid size. */
uidsize = 0xff & (int)p[offset+1];
if (uidsize == 2)
zip_entry->uid =
archive_le16dec(
p + offset + 2);
else if (uidsize == 4 && datasize >= 6)
zip_entry->uid =
archive_le32dec(
p + offset + 2);
}
if (datasize >= (2 + uidsize + 3)) {
/* get a gid size. */
gidsize = 0xff & (int)p[offset+2+uidsize];
if (gidsize == 2)
zip_entry->gid =
archive_le16dec(
p+offset+2+uidsize+1);
else if (gidsize == 4 &&
datasize >= (2 + uidsize + 5))
zip_entry->gid =
archive_le32dec(
p+offset+2+uidsize+1);
}
}
break;
}
case 0x9901:
/* WinZIp AES extra data field. */
if (p[offset + 2] == 'A' && p[offset + 3] == 'E') {
/* Vendor version. */
zip_entry->aes_extra.vendor =
archive_le16dec(p + offset);
/* AES encryption strength. */
zip_entry->aes_extra.strength = p[offset + 4];
/* Actual compression method. */
zip_entry->aes_extra.compression =
p[offset + 5];
}
break;
default:
break;
}
offset += datasize;
}
#ifdef DEBUG
if (offset != extra_length)
{
fprintf(stderr,
"Extra data field contents do not match reported size!\n");
}
#endif
}
/*
* Assumes file pointer is at beginning of local file header.
*/
static int
zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,
struct zip *zip)
{
const char *p;
const void *h;
const wchar_t *wp;
const char *cp;
size_t len, filename_length, extra_length;
struct archive_string_conv *sconv;
struct zip_entry *zip_entry = zip->entry;
struct zip_entry zip_entry_central_dir;
int ret = ARCHIVE_OK;
char version;
/* Save a copy of the original for consistency checks. */
zip_entry_central_dir = *zip_entry;
zip->decompress_init = 0;
zip->end_of_entry = 0;
zip->entry_uncompressed_bytes_read = 0;
zip->entry_compressed_bytes_read = 0;
zip->entry_crc32 = zip->crc32func(0, NULL, 0);
/* Setup default conversion. */
if (zip->sconv == NULL && !zip->init_default_conversion) {
zip->sconv_default =
archive_string_default_conversion_for_read(&(a->archive));
zip->init_default_conversion = 1;
}
if ((p = __archive_read_ahead(a, 30, NULL)) == NULL) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated ZIP file header");
return (ARCHIVE_FATAL);
}
if (memcmp(p, "PK\003\004", 4) != 0) {
archive_set_error(&a->archive, -1, "Damaged Zip archive");
return ARCHIVE_FATAL;
}
version = p[4];
zip_entry->system = p[5];
zip_entry->zip_flags = archive_le16dec(p + 6);
if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
zip->has_encrypted_entries = 1;
archive_entry_set_is_data_encrypted(entry, 1);
if (zip_entry->zip_flags & ZIP_CENTRAL_DIRECTORY_ENCRYPTED &&
zip_entry->zip_flags & ZIP_ENCRYPTED &&
zip_entry->zip_flags & ZIP_STRONG_ENCRYPTED) {
archive_entry_set_is_metadata_encrypted(entry, 1);
return ARCHIVE_FATAL;
}
}
zip->init_decryption = (zip_entry->zip_flags & ZIP_ENCRYPTED);
zip_entry->compression = (char)archive_le16dec(p + 8);
zip_entry->mtime = zip_time(p + 10);
zip_entry->crc32 = archive_le32dec(p + 14);
if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
zip_entry->decdat = p[11];
else
zip_entry->decdat = p[17];
zip_entry->compressed_size = archive_le32dec(p + 18);
zip_entry->uncompressed_size = archive_le32dec(p + 22);
filename_length = archive_le16dec(p + 26);
extra_length = archive_le16dec(p + 28);
__archive_read_consume(a, 30);
/* Read the filename. */
if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated ZIP file header");
return (ARCHIVE_FATAL);
}
if (zip_entry->zip_flags & ZIP_UTF8_NAME) {
/* The filename is stored to be UTF-8. */
if (zip->sconv_utf8 == NULL) {
zip->sconv_utf8 =
archive_string_conversion_from_charset(
&a->archive, "UTF-8", 1);
if (zip->sconv_utf8 == NULL)
return (ARCHIVE_FATAL);
}
sconv = zip->sconv_utf8;
} else if (zip->sconv != NULL)
sconv = zip->sconv;
else
sconv = zip->sconv_default;
if (archive_entry_copy_pathname_l(entry,
h, filename_length, sconv) != 0) {
if (errno == ENOMEM) {
archive_set_error(&a->archive, ENOMEM,
"Can't allocate memory for Pathname");
return (ARCHIVE_FATAL);
}
archive_set_error(&a->archive,
ARCHIVE_ERRNO_FILE_FORMAT,
"Pathname cannot be converted "
"from %s to current locale.",
archive_string_conversion_charset_name(sconv));
ret = ARCHIVE_WARN;
}
__archive_read_consume(a, filename_length);
/* Read the extra data. */
if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated ZIP file header");
return (ARCHIVE_FATAL);
}
process_extra(h, extra_length, zip_entry);
__archive_read_consume(a, extra_length);
/* Work around a bug in Info-Zip: When reading from a pipe, it
* stats the pipe instead of synthesizing a file entry. */
if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
zip_entry->mode &= ~ AE_IFMT;
zip_entry->mode |= AE_IFREG;
}
if ((zip_entry->mode & AE_IFMT) == 0) {
/* Especially in streaming mode, we can end up
here without having seen proper mode information.
Guess from the filename. */
wp = archive_entry_pathname_w(entry);
if (wp != NULL) {
len = wcslen(wp);
if (len > 0 && wp[len - 1] == L'/')
zip_entry->mode |= AE_IFDIR;
else
zip_entry->mode |= AE_IFREG;
} else {
cp = archive_entry_pathname(entry);
len = (cp != NULL)?strlen(cp):0;
if (len > 0 && cp[len - 1] == '/')
zip_entry->mode |= AE_IFDIR;
else
zip_entry->mode |= AE_IFREG;
}
if (zip_entry->mode == AE_IFDIR) {
zip_entry->mode |= 0775;
} else if (zip_entry->mode == AE_IFREG) {
zip_entry->mode |= 0664;
}
}
/* Make sure directories end in '/' */
if ((zip_entry->mode & AE_IFMT) == AE_IFDIR) {
wp = archive_entry_pathname_w(entry);
if (wp != NULL) {
len = wcslen(wp);
if (len > 0 && wp[len - 1] != L'/') {
struct archive_wstring s;
archive_string_init(&s);
archive_wstrcat(&s, wp);
archive_wstrappend_wchar(&s, L'/');
archive_entry_copy_pathname_w(entry, s.s);
}
} else {
cp = archive_entry_pathname(entry);
len = (cp != NULL)?strlen(cp):0;
if (len > 0 && cp[len - 1] != '/') {
struct archive_string s;
archive_string_init(&s);
archive_strcat(&s, cp);
archive_strappend_char(&s, '/');
archive_entry_set_pathname(entry, s.s);
}
}
}
if (zip_entry->flags & LA_FROM_CENTRAL_DIRECTORY) {
/* If this came from the central dir, it's size info
* is definitive, so ignore the length-at-end flag. */
zip_entry->zip_flags &= ~ZIP_LENGTH_AT_END;
/* If local header is missing a value, use the one from
the central directory. If both have it, warn about
mismatches. */
if (zip_entry->crc32 == 0) {
zip_entry->crc32 = zip_entry_central_dir.crc32;
} else if (!zip->ignore_crc32
&& zip_entry->crc32 != zip_entry_central_dir.crc32) {
archive_set_error(&a->archive,
ARCHIVE_ERRNO_FILE_FORMAT,
"Inconsistent CRC32 values");
ret = ARCHIVE_WARN;
}
if (zip_entry->compressed_size == 0) {
zip_entry->compressed_size
= zip_entry_central_dir.compressed_size;
} else if (zip_entry->compressed_size
!= zip_entry_central_dir.compressed_size) {
archive_set_error(&a->archive,
ARCHIVE_ERRNO_FILE_FORMAT,
"Inconsistent compressed size: "
"%jd in central directory, %jd in local header",
(intmax_t)zip_entry_central_dir.compressed_size,
(intmax_t)zip_entry->compressed_size);
ret = ARCHIVE_WARN;
}
if (zip_entry->uncompressed_size == 0) {
zip_entry->uncompressed_size
= zip_entry_central_dir.uncompressed_size;
} else if (zip_entry->uncompressed_size
!= zip_entry_central_dir.uncompressed_size) {
archive_set_error(&a->archive,
ARCHIVE_ERRNO_FILE_FORMAT,
"Inconsistent uncompressed size: "
"%jd in central directory, %jd in local header",
(intmax_t)zip_entry_central_dir.uncompressed_size,
(intmax_t)zip_entry->uncompressed_size);
ret = ARCHIVE_WARN;
}
}
/* Populate some additional entry fields: */
archive_entry_set_mode(entry, zip_entry->mode);
archive_entry_set_uid(entry, zip_entry->uid);
archive_entry_set_gid(entry, zip_entry->gid);
archive_entry_set_mtime(entry, zip_entry->mtime, 0);
archive_entry_set_ctime(entry, zip_entry->ctime, 0);
archive_entry_set_atime(entry, zip_entry->atime, 0);
if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) {
size_t linkname_length;
if (zip_entry->compressed_size > 64 * 1024) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"Zip file with oversized link entry");
return ARCHIVE_FATAL;
}
linkname_length = (size_t)zip_entry->compressed_size;
archive_entry_set_size(entry, 0);
p = __archive_read_ahead(a, linkname_length, NULL);
if (p == NULL) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"Truncated Zip file");
return ARCHIVE_FATAL;
}
sconv = zip->sconv;
if (sconv == NULL && (zip->entry->zip_flags & ZIP_UTF8_NAME))
sconv = zip->sconv_utf8;
if (sconv == NULL)
sconv = zip->sconv_default;
if (archive_entry_copy_symlink_l(entry, p, linkname_length,
sconv) != 0) {
if (errno != ENOMEM && sconv == zip->sconv_utf8 &&
(zip->entry->zip_flags & ZIP_UTF8_NAME))
archive_entry_copy_symlink_l(entry, p,
linkname_length, NULL);
if (errno == ENOMEM) {
archive_set_error(&a->archive, ENOMEM,
"Can't allocate memory for Symlink");
return (ARCHIVE_FATAL);
}
/*
* Since there is no character-set regulation for
* symlink name, do not report the conversion error
* in an automatic conversion.
*/
if (sconv != zip->sconv_utf8 ||
(zip->entry->zip_flags & ZIP_UTF8_NAME) == 0) {
archive_set_error(&a->archive,
ARCHIVE_ERRNO_FILE_FORMAT,
"Symlink cannot be converted "