2 * Copyright (c) 2008 Anselm Strauss
3 * Copyright (c) 2009 Joerg Sonnenberger
4 * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Development supported by Google Summer of Code 2008.
32 #include "archive_platform.h"
33 __FBSDID("$FreeBSD$");
38 #ifdef HAVE_LANGINFO_H
52 #include "archive_cryptor_private.h"
53 #include "archive_endian.h"
54 #include "archive_entry.h"
55 #include "archive_entry_locale.h"
56 #include "archive_hmac_private.h"
57 #include "archive_private.h"
58 #include "archive_random_private.h"
59 #include "archive_write_private.h"
60 #include "archive_write_set_format_private.h"
63 #include "archive_crc32.h"
66 #define ZIP_ENTRY_FLAG_ENCRYPTED (1<<0)
67 #define ZIP_ENTRY_FLAG_LENGTH_AT_END (1<<3)
68 #define ZIP_ENTRY_FLAG_UTF8_NAME (1 << 11)
70 #define ZIP_4GB_MAX ARCHIVE_LITERAL_LL(0xffffffff)
71 #define ZIP_4GB_MAX_UNCOMPRESSED ARCHIVE_LITERAL_LL(0xff000000)
74 COMPRESSION_UNSPECIFIED = -1,
75 COMPRESSION_STORE = 0,
76 COMPRESSION_DEFLATE = 8
80 #define COMPRESSION_DEFAULT COMPRESSION_DEFLATE
82 #define COMPRESSION_DEFAULT COMPRESSION_STORE
87 ENCRYPTION_TRADITIONAL, /* Traditional PKWARE encryption. */
88 ENCRYPTION_WINZIP_AES128, /* WinZIP AES-128 encryption. */
89 ENCRYPTION_WINZIP_AES256, /* WinZIP AES-256 encryption. */
92 #define TRAD_HEADER_SIZE 12
94 * See "WinZip - AES Encryption Information"
95 * http://www.winzip.com/aes_info.htm
97 /* Value used in compression method. */
98 #define WINZIP_AES_ENCRYPTION 99
99 /* A WinZip AES header size which is stored at the beginning of
101 #define WINZIP_AES128_HEADER_SIZE (8 + 2)
102 #define WINZIP_AES256_HEADER_SIZE (16 + 2)
103 /* AES vendor version. */
104 #define AES_VENDOR_AE_1 0x0001
105 #define AES_VENDOR_AE_2 0x0002
106 /* Authentication code size. */
107 #define AUTH_CODE_SIZE 10
109 #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
112 struct cd_segment *next;
118 struct trad_enc_ctx {
124 int64_t entry_offset;
125 int64_t entry_compressed_size;
126 int64_t entry_uncompressed_size;
127 int64_t entry_compressed_written;
128 int64_t entry_uncompressed_written;
129 int64_t entry_uncompressed_limit;
130 struct archive_entry *entry;
131 uint32_t entry_crc32;
132 enum compression entry_compression;
133 enum encryption entry_encryption;
135 int entry_uses_zip64;
137 struct trad_enc_ctx tctx;
139 unsigned char trad_chkdat;
141 archive_crypto_ctx cctx;
143 archive_hmac_sha1_ctx hctx;
146 unsigned char *file_header;
147 size_t file_header_extra_offset;
148 unsigned long (*crc32func)(unsigned long crc, const void *buff, size_t len);
150 struct cd_segment *central_directory;
151 struct cd_segment *central_directory_last;
152 size_t central_directory_bytes;
153 size_t central_directory_entries;
155 int64_t written_bytes; /* Overall position in file. */
157 struct archive_string_conv *opt_sconv;
158 struct archive_string_conv *sconv_default;
159 enum compression requested_compression;
160 int deflate_compression_level;
161 int init_default_conversion;
162 enum encryption encryption_type;
164 #define ZIP_FLAG_AVOID_ZIP64 1
165 #define ZIP_FLAG_FORCE_ZIP64 2
166 #define ZIP_FLAG_EXPERIMENT_xl 4
176 /* Don't call this min or MIN, since those are already defined
177 on lots of platforms (but not all). */
178 #define zipmin(a, b) ((a) > (b) ? (b) : (a))
180 static ssize_t archive_write_zip_data(struct archive_write *,
181 const void *buff, size_t s);
182 static int archive_write_zip_close(struct archive_write *);
183 static int archive_write_zip_free(struct archive_write *);
184 static int archive_write_zip_finish_entry(struct archive_write *);
185 static int archive_write_zip_header(struct archive_write *,
186 struct archive_entry *);
187 static int archive_write_zip_options(struct archive_write *,
188 const char *, const char *);
189 static unsigned int dos_time(const time_t);
190 static size_t path_length(struct archive_entry *);
191 static int write_path(struct archive_entry *, struct archive_write *);
192 static void copy_path(struct archive_entry *, unsigned char *);
193 static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
194 static int trad_enc_init(struct trad_enc_ctx *, const char *, size_t);
195 static unsigned trad_enc_encrypt_update(struct trad_enc_ctx *, const uint8_t *,
196 size_t, uint8_t *, size_t);
197 static int init_traditional_pkware_encryption(struct archive_write *);
198 static int is_traditional_pkware_encryption_supported(void);
199 static int init_winzip_aes_encryption(struct archive_write *);
200 static int is_winzip_aes_encryption_supported(int encryption);
202 static unsigned char *
203 cd_alloc(struct zip *zip, size_t length)
207 if (zip->central_directory == NULL
208 || (zip->central_directory_last->p + length
209 > zip->central_directory_last->buff + zip->central_directory_last->buff_size)) {
210 struct cd_segment *segment = calloc(1, sizeof(*segment));
213 segment->buff_size = 64 * 1024;
214 segment->buff = malloc(segment->buff_size);
215 if (segment->buff == NULL) {
219 segment->p = segment->buff;
221 if (zip->central_directory == NULL) {
222 zip->central_directory
223 = zip->central_directory_last
226 zip->central_directory_last->next = segment;
227 zip->central_directory_last = segment;
231 p = zip->central_directory_last->p;
232 zip->central_directory_last->p += length;
233 zip->central_directory_bytes += length;
238 real_crc32(unsigned long crc, const void *buff, size_t len)
240 return crc32(crc, buff, (unsigned int)len);
244 fake_crc32(unsigned long crc, const void *buff, size_t len)
246 (void)crc; /* UNUSED */
247 (void)buff; /* UNUSED */
248 (void)len; /* UNUSED */
253 archive_write_zip_options(struct archive_write *a, const char *key,
256 struct zip *zip = a->format_data;
257 int ret = ARCHIVE_FAILED;
259 if (strcmp(key, "compression") == 0) {
261 * Set compression to use on all future entries.
262 * This only affects regular files.
264 if (val == NULL || val[0] == 0) {
265 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
266 "%s: compression option needs a compression name",
268 } else if (strcmp(val, "deflate") == 0) {
270 zip->requested_compression = COMPRESSION_DEFLATE;
273 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
274 "deflate compression not supported");
276 } else if (strcmp(val, "store") == 0) {
277 zip->requested_compression = COMPRESSION_STORE;
281 } else if (strcmp(key, "compression-level") == 0) {
282 if (val == NULL || !(val[0] >= '0' && val[0] <= '9') || val[1] != '\0') {
287 zip->requested_compression = COMPRESSION_STORE;
291 zip->requested_compression = COMPRESSION_DEFLATE;
292 zip->deflate_compression_level = val[0] - '0';
295 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
296 "deflate compression not supported");
299 } else if (strcmp(key, "encryption") == 0) {
301 zip->encryption_type = ENCRYPTION_NONE;
303 } else if (val[0] == '1' || strcmp(val, "traditional") == 0
304 || strcmp(val, "zipcrypt") == 0
305 || strcmp(val, "ZipCrypt") == 0) {
306 if (is_traditional_pkware_encryption_supported()) {
307 zip->encryption_type = ENCRYPTION_TRADITIONAL;
310 archive_set_error(&a->archive,
312 "encryption not supported");
314 } else if (strcmp(val, "aes128") == 0) {
315 if (is_winzip_aes_encryption_supported(
316 ENCRYPTION_WINZIP_AES128)) {
317 zip->encryption_type = ENCRYPTION_WINZIP_AES128;
320 archive_set_error(&a->archive,
322 "encryption not supported");
324 } else if (strcmp(val, "aes256") == 0) {
325 if (is_winzip_aes_encryption_supported(
326 ENCRYPTION_WINZIP_AES256)) {
327 zip->encryption_type = ENCRYPTION_WINZIP_AES256;
330 archive_set_error(&a->archive,
332 "encryption not supported");
335 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
336 "%s: unknown encryption '%s'",
337 a->format_name, val);
340 } else if (strcmp(key, "experimental") == 0) {
341 if (val == NULL || val[0] == 0) {
342 zip->flags &= ~ ZIP_FLAG_EXPERIMENT_xl;
344 zip->flags |= ZIP_FLAG_EXPERIMENT_xl;
347 } else if (strcmp(key, "fakecrc32") == 0) {
349 * FOR TESTING ONLY: disable CRC calculation to speed up
350 * certain complex tests.
352 if (val == NULL || val[0] == 0) {
353 zip->crc32func = real_crc32;
355 zip->crc32func = fake_crc32;
358 } else if (strcmp(key, "hdrcharset") == 0) {
360 * Set the character set used in translating filenames.
362 if (val == NULL || val[0] == 0) {
363 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
364 "%s: hdrcharset option needs a character-set name",
367 zip->opt_sconv = archive_string_conversion_to_charset(
368 &a->archive, val, 0);
369 if (zip->opt_sconv != NULL)
375 } else if (strcmp(key, "zip64") == 0) {
377 * Bias decisions about Zip64: force them to be
378 * generated in certain cases where they are not
379 * forbidden or avoid them in certain cases where they
380 * are not strictly required.
382 if (val != NULL && *val != '\0') {
383 zip->flags |= ZIP_FLAG_FORCE_ZIP64;
384 zip->flags &= ~ZIP_FLAG_AVOID_ZIP64;
386 zip->flags &= ~ZIP_FLAG_FORCE_ZIP64;
387 zip->flags |= ZIP_FLAG_AVOID_ZIP64;
392 /* Note: The "warn" return is just to inform the options
393 * supervisor that we didn't handle it. It will generate
394 * a suitable error if no one used this option. */
395 return (ARCHIVE_WARN);
399 archive_write_zip_set_compression_deflate(struct archive *_a)
401 struct archive_write *a = (struct archive_write *)_a;
402 int ret = ARCHIVE_FAILED;
404 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
405 ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
406 "archive_write_zip_set_compression_deflate");
407 if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
408 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
409 "Can only use archive_write_zip_set_compression_deflate"
414 struct zip *zip = a->format_data;
415 zip->requested_compression = COMPRESSION_DEFLATE;
418 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
419 "deflate compression not supported");
420 ret = ARCHIVE_FAILED;
427 archive_write_zip_set_compression_store(struct archive *_a)
429 struct archive_write *a = (struct archive_write *)_a;
430 struct zip *zip = a->format_data;
431 int ret = ARCHIVE_FAILED;
433 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
434 ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
435 "archive_write_zip_set_compression_deflate");
436 if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
437 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
438 "Can only use archive_write_zip_set_compression_store"
442 zip->requested_compression = COMPRESSION_STORE;
449 archive_write_set_format_zip(struct archive *_a)
451 struct archive_write *a = (struct archive_write *)_a;
454 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
455 ARCHIVE_STATE_NEW, "archive_write_set_format_zip");
457 /* If another format was already registered, unregister it. */
458 if (a->format_free != NULL)
461 zip = (struct zip *) calloc(1, sizeof(*zip));
463 archive_set_error(&a->archive, ENOMEM,
464 "Can't allocate zip data");
465 return (ARCHIVE_FATAL);
468 /* "Unspecified" lets us choose the appropriate compression. */
469 zip->requested_compression = COMPRESSION_UNSPECIFIED;
471 zip->deflate_compression_level = Z_DEFAULT_COMPRESSION;
473 zip->crc32func = real_crc32;
475 /* A buffer used for both compression and encryption. */
476 zip->len_buf = 65536;
477 zip->buf = malloc(zip->len_buf);
478 if (zip->buf == NULL) {
480 archive_set_error(&a->archive, ENOMEM,
481 "Can't allocate compression buffer");
482 return (ARCHIVE_FATAL);
485 a->format_data = zip;
486 a->format_name = "zip";
487 a->format_options = archive_write_zip_options;
488 a->format_write_header = archive_write_zip_header;
489 a->format_write_data = archive_write_zip_data;
490 a->format_finish_entry = archive_write_zip_finish_entry;
491 a->format_close = archive_write_zip_close;
492 a->format_free = archive_write_zip_free;
493 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
494 a->archive.archive_format_name = "ZIP";
500 is_all_ascii(const char *p)
502 const unsigned char *pp = (const unsigned char *)p;
512 archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
514 unsigned char local_header[32];
515 unsigned char local_extra[144];
516 struct zip *zip = a->format_data;
518 unsigned char *cd_extra;
519 size_t filename_length;
520 const char *slink = NULL;
521 size_t slink_size = 0;
522 struct archive_string_conv *sconv = get_sconv(a, zip);
523 int ret, ret2 = ARCHIVE_OK;
525 int version_needed = 10;
527 /* Ignore types of entries that we don't support. */
528 type = archive_entry_filetype(entry);
529 if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) {
530 __archive_write_entry_filetype_unsupported(
531 &a->archive, entry, "zip");
532 return ARCHIVE_FAILED;
535 /* If we're not using Zip64, reject large files. */
536 if (zip->flags & ZIP_FLAG_AVOID_ZIP64) {
537 /* Reject entries over 4GB. */
538 if (archive_entry_size_is_set(entry)
539 && (archive_entry_size(entry) > ZIP_4GB_MAX)) {
540 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
541 "Files > 4GB require Zip64 extensions");
542 return ARCHIVE_FAILED;
544 /* Reject entries if archive is > 4GB. */
545 if (zip->written_bytes > ZIP_4GB_MAX) {
546 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
547 "Archives > 4GB require Zip64 extensions");
548 return ARCHIVE_FAILED;
552 /* Only regular files can have size > 0. */
553 if (type != AE_IFREG)
554 archive_entry_set_size(entry, 0);
557 /* Reset information from last entry. */
558 zip->entry_offset = zip->written_bytes;
559 zip->entry_uncompressed_limit = INT64_MAX;
560 zip->entry_compressed_size = 0;
561 zip->entry_uncompressed_size = 0;
562 zip->entry_compressed_written = 0;
563 zip->entry_uncompressed_written = 0;
564 zip->entry_flags = 0;
565 zip->entry_uses_zip64 = 0;
566 zip->entry_crc32 = zip->crc32func(0, NULL, 0);
567 zip->entry_encryption = 0;
568 archive_entry_free(zip->entry);
572 archive_encrypto_aes_ctr_release(&zip->cctx);
574 archive_hmac_sha1_cleanup(&zip->hctx);
575 zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
578 &&(!archive_entry_size_is_set(entry)
579 || archive_entry_size(entry) > 0)) {
580 switch (zip->encryption_type) {
581 case ENCRYPTION_TRADITIONAL:
582 case ENCRYPTION_WINZIP_AES128:
583 case ENCRYPTION_WINZIP_AES256:
584 zip->entry_flags |= ZIP_ENTRY_FLAG_ENCRYPTED;
585 zip->entry_encryption = zip->encryption_type;
593 #if defined(_WIN32) && !defined(__CYGWIN__)
594 /* Make sure the path separators in pathname, hardlink and symlink
595 * are all slash '/', not the Windows path separator '\'. */
596 zip->entry = __la_win_entry_in_posix_pathseparator(entry);
597 if (zip->entry == entry)
598 zip->entry = archive_entry_clone(entry);
600 zip->entry = archive_entry_clone(entry);
602 if (zip->entry == NULL) {
603 archive_set_error(&a->archive, ENOMEM,
604 "Can't allocate zip header data");
605 return (ARCHIVE_FATAL);
612 if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) {
613 if (errno == ENOMEM) {
614 archive_set_error(&a->archive, ENOMEM,
615 "Can't allocate memory for Pathname");
616 return (ARCHIVE_FATAL);
618 archive_set_error(&a->archive,
619 ARCHIVE_ERRNO_FILE_FORMAT,
620 "Can't translate Pathname '%s' to %s",
621 archive_entry_pathname(entry),
622 archive_string_conversion_charset_name(sconv));
626 archive_entry_set_pathname(zip->entry, p);
629 * There is no standard for symlink handling; we convert
630 * it using the same character-set translation that we use
633 if (type == AE_IFLNK) {
634 if (archive_entry_symlink_l(entry, &p, &len, sconv)) {
635 if (errno == ENOMEM) {
636 archive_set_error(&a->archive, ENOMEM,
637 "Can't allocate memory "
639 return (ARCHIVE_FATAL);
641 /* No error if we can't convert. */
643 archive_entry_set_symlink(zip->entry, p);
647 /* If filename isn't ASCII and we can use UTF-8, set the UTF-8 flag. */
648 if (!is_all_ascii(archive_entry_pathname(zip->entry))) {
649 if (zip->opt_sconv != NULL) {
650 if (strcmp(archive_string_conversion_charset_name(
651 zip->opt_sconv), "UTF-8") == 0)
652 zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
654 } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
655 zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
659 filename_length = path_length(zip->entry);
661 /* Determine appropriate compression and size for this entry. */
662 if (type == AE_IFLNK) {
663 slink = archive_entry_symlink(zip->entry);
665 slink_size = strlen(slink);
668 zip->entry_uncompressed_limit = slink_size;
669 zip->entry_compressed_size = slink_size;
670 zip->entry_uncompressed_size = slink_size;
671 zip->entry_crc32 = zip->crc32func(zip->entry_crc32,
672 (const unsigned char *)slink, slink_size);
673 zip->entry_compression = COMPRESSION_STORE;
675 } else if (type != AE_IFREG) {
676 zip->entry_compression = COMPRESSION_STORE;
677 zip->entry_uncompressed_limit = 0;
679 } else if (archive_entry_size_is_set(zip->entry)) {
680 int64_t size = archive_entry_size(zip->entry);
681 int64_t additional_size = 0;
683 zip->entry_uncompressed_limit = size;
684 zip->entry_compression = zip->requested_compression;
685 if (zip->entry_compression == COMPRESSION_UNSPECIFIED) {
686 zip->entry_compression = COMPRESSION_DEFAULT;
688 if (zip->entry_compression == COMPRESSION_STORE) {
689 zip->entry_compressed_size = size;
690 zip->entry_uncompressed_size = size;
693 zip->entry_uncompressed_size = size;
697 if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
698 switch (zip->entry_encryption) {
699 case ENCRYPTION_TRADITIONAL:
700 additional_size = TRAD_HEADER_SIZE;
703 case ENCRYPTION_WINZIP_AES128:
704 additional_size = WINZIP_AES128_HEADER_SIZE
708 case ENCRYPTION_WINZIP_AES256:
709 additional_size = WINZIP_AES256_HEADER_SIZE
716 if (zip->entry_compression == COMPRESSION_STORE)
717 zip->entry_compressed_size += additional_size;
721 * Set Zip64 extension in any of the following cases
722 * (this was suggested by discussion on info-zip-dev
724 * = Zip64 is being forced by user
725 * = File is over 4GiB uncompressed
726 * (including encryption header, if any)
727 * = File is close to 4GiB and is being compressed
728 * (compression might make file larger)
730 if ((zip->flags & ZIP_FLAG_FORCE_ZIP64)
731 || (zip->entry_uncompressed_size + additional_size > ZIP_4GB_MAX)
732 || (zip->entry_uncompressed_size > ZIP_4GB_MAX_UNCOMPRESSED
733 && zip->entry_compression != COMPRESSION_STORE)) {
734 zip->entry_uses_zip64 = 1;
738 /* We may know the size, but never the CRC. */
739 zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
741 /* We don't know the size. In this case, we prefer
742 * deflate (it has a clear end-of-data marker which
743 * makes length-at-end more reliable) and will
744 * enable Zip64 extensions unless we're told not to.
746 zip->entry_compression = COMPRESSION_DEFAULT;
747 zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
748 if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) {
749 zip->entry_uses_zip64 = 1;
751 } else if (zip->entry_compression == COMPRESSION_STORE) {
757 if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
758 switch (zip->entry_encryption) {
759 case ENCRYPTION_TRADITIONAL:
760 case ENCRYPTION_WINZIP_AES128:
761 case ENCRYPTION_WINZIP_AES256:
762 if (version_needed < 20)
771 /* Format the local header. */
772 memset(local_header, 0, sizeof(local_header));
773 memcpy(local_header, "PK\003\004", 4);
774 archive_le16enc(local_header + 4, version_needed);
775 archive_le16enc(local_header + 6, zip->entry_flags);
776 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
777 || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
778 archive_le16enc(local_header + 8, WINZIP_AES_ENCRYPTION);
780 archive_le16enc(local_header + 8, zip->entry_compression);
781 archive_le32enc(local_header + 10,
782 dos_time(archive_entry_mtime(zip->entry)));
783 archive_le32enc(local_header + 14, zip->entry_crc32);
784 if (zip->entry_uses_zip64) {
785 /* Zip64 data in the local header "must" include both
786 * compressed and uncompressed sizes AND those fields
787 * are included only if these are 0xffffffff;
788 * THEREFORE these must be set this way, even if we
789 * know one of them is smaller. */
790 archive_le32enc(local_header + 18, ZIP_4GB_MAX);
791 archive_le32enc(local_header + 22, ZIP_4GB_MAX);
793 archive_le32enc(local_header + 18, (uint32_t)zip->entry_compressed_size);
794 archive_le32enc(local_header + 22, (uint32_t)zip->entry_uncompressed_size);
796 archive_le16enc(local_header + 26, (uint16_t)filename_length);
798 if (zip->entry_encryption == ENCRYPTION_TRADITIONAL) {
799 if (zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END)
800 zip->trad_chkdat = local_header[11];
802 zip->trad_chkdat = local_header[17];
805 /* Format as much of central directory file header as we can: */
806 zip->file_header = cd_alloc(zip, 46);
807 /* If (zip->file_header == NULL) XXXX */
808 ++zip->central_directory_entries;
809 memset(zip->file_header, 0, 46);
810 memcpy(zip->file_header, "PK\001\002", 4);
811 /* "Made by PKZip 2.0 on Unix." */
812 archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed);
813 archive_le16enc(zip->file_header + 6, version_needed);
814 archive_le16enc(zip->file_header + 8, zip->entry_flags);
815 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
816 || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
817 archive_le16enc(zip->file_header + 10, WINZIP_AES_ENCRYPTION);
819 archive_le16enc(zip->file_header + 10, zip->entry_compression);
820 archive_le32enc(zip->file_header + 12,
821 dos_time(archive_entry_mtime(zip->entry)));
822 archive_le16enc(zip->file_header + 28, (uint16_t)filename_length);
823 /* Following Info-Zip, store mode in the "external attributes" field. */
824 archive_le32enc(zip->file_header + 38,
825 ((uint32_t)archive_entry_mode(zip->entry)) << 16);
826 e = cd_alloc(zip, filename_length);
827 /* If (e == NULL) XXXX */
828 copy_path(zip->entry, e);
830 /* Format extra data. */
831 memset(local_extra, 0, sizeof(local_extra));
834 /* First, extra blocks that are the same between
835 * the local file header and the central directory.
836 * We format them once and then duplicate them. */
838 /* UT timestamp, length depends on what timestamps are set. */
840 archive_le16enc(e + 2,
842 + (archive_entry_mtime_is_set(entry) ? 4 : 0)
843 + (archive_entry_atime_is_set(entry) ? 4 : 0)
844 + (archive_entry_ctime_is_set(entry) ? 4 : 0));
847 (archive_entry_mtime_is_set(entry) ? 1 : 0)
848 | (archive_entry_atime_is_set(entry) ? 2 : 0)
849 | (archive_entry_ctime_is_set(entry) ? 4 : 0);
850 if (archive_entry_mtime_is_set(entry)) {
851 archive_le32enc(e, (uint32_t)archive_entry_mtime(entry));
854 if (archive_entry_atime_is_set(entry)) {
855 archive_le32enc(e, (uint32_t)archive_entry_atime(entry));
858 if (archive_entry_ctime_is_set(entry)) {
859 archive_le32enc(e, (uint32_t)archive_entry_ctime(entry));
863 /* ux Unix extra data, length 11, version 1 */
864 /* TODO: If uid < 64k, use 2 bytes, ditto for gid. */
865 memcpy(e, "ux\013\000\001", 5);
867 *e++ = 4; /* Length of following UID */
868 archive_le32enc(e, (uint32_t)archive_entry_uid(entry));
870 *e++ = 4; /* Length of following GID */
871 archive_le32enc(e, (uint32_t)archive_entry_gid(entry));
874 /* AES extra data field: WinZIP AES information, ID=0x9901 */
875 if ((zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED)
876 && (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
877 || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)) {
879 memcpy(e, "\001\231\007\000\001\000AE", 8);
880 /* AES vendor version AE-2 does not store a CRC.
881 * WinZip 11 uses AE-1, which does store the CRC,
882 * but it does not store the CRC when the file size
883 * is less than 20 bytes. So we simulate what
885 * NOTE: WinZip 9.0 and 10.0 uses AE-2 by default. */
886 if (archive_entry_size_is_set(zip->entry)
887 && archive_entry_size(zip->entry) < 20) {
888 archive_le16enc(e+4, AES_VENDOR_AE_2);
889 zip->aes_vendor = AES_VENDOR_AE_2;/* no CRC. */
891 zip->aes_vendor = AES_VENDOR_AE_1;
893 /* AES encryption strength. */
894 *e++ = (zip->entry_encryption == ENCRYPTION_WINZIP_AES128)?1:3;
895 /* Actual compression method. */
896 archive_le16enc(e, zip->entry_compression);
900 /* Copy UT ,ux, and AES-extra into central directory as well. */
901 zip->file_header_extra_offset = zip->central_directory_bytes;
902 cd_extra = cd_alloc(zip, e - local_extra);
903 memcpy(cd_extra, local_extra, e - local_extra);
906 * Following extra blocks vary between local header and
907 * central directory. These are the local header versions.
908 * Central directory versions get formatted in
909 * archive_write_zip_finish_entry() below.
912 /* "[Zip64 entry] in the local header MUST include BOTH
913 * original [uncompressed] and compressed size fields." */
914 if (zip->entry_uses_zip64) {
915 unsigned char *zip64_start = e;
916 memcpy(e, "\001\000\020\000", 4);
918 archive_le64enc(e, zip->entry_uncompressed_size);
920 archive_le64enc(e, zip->entry_compressed_size);
922 archive_le16enc(zip64_start + 2, (uint16_t)(e - (zip64_start + 4)));
925 if (zip->flags & ZIP_FLAG_EXPERIMENT_xl) {
926 /* Experimental 'xl' extension to improve streaming. */
927 unsigned char *external_info = e;
929 memcpy(e, "xl\000\000", 4); // 0x6c65 + 2-byte length
931 e[0] = included; /* bitmap of included fields */
934 archive_le16enc(e, /* "Version created by" */
935 3 * 256 + version_needed);
939 archive_le16enc(e, 0); /* internal file attributes */
943 archive_le32enc(e, /* external file attributes */
944 ((uint32_t)archive_entry_mode(zip->entry)) << 16);
948 // Libarchive does not currently support file comments.
950 archive_le16enc(external_info + 2, (uint16_t)(e - (external_info + 4)));
953 /* Update local header with size of extra data and write it all out: */
954 archive_le16enc(local_header + 28, (uint16_t)(e - local_extra));
956 ret = __archive_write_output(a, local_header, 30);
957 if (ret != ARCHIVE_OK)
958 return (ARCHIVE_FATAL);
959 zip->written_bytes += 30;
961 ret = write_path(zip->entry, a);
962 if (ret <= ARCHIVE_OK)
963 return (ARCHIVE_FATAL);
964 zip->written_bytes += ret;
966 ret = __archive_write_output(a, local_extra, e - local_extra);
967 if (ret != ARCHIVE_OK)
968 return (ARCHIVE_FATAL);
969 zip->written_bytes += e - local_extra;
971 /* For symlinks, write the body now. */
973 ret = __archive_write_output(a, slink, slink_size);
974 if (ret != ARCHIVE_OK)
975 return (ARCHIVE_FATAL);
976 zip->entry_compressed_written += slink_size;
977 zip->entry_uncompressed_written += slink_size;
978 zip->written_bytes += slink_size;
982 if (zip->entry_compression == COMPRESSION_DEFLATE) {
983 zip->stream.zalloc = Z_NULL;
984 zip->stream.zfree = Z_NULL;
985 zip->stream.opaque = Z_NULL;
986 zip->stream.next_out = zip->buf;
987 zip->stream.avail_out = (uInt)zip->len_buf;
988 if (deflateInit2(&zip->stream, zip->deflate_compression_level,
989 Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
990 archive_set_error(&a->archive, ENOMEM,
991 "Can't init deflate compressor");
992 return (ARCHIVE_FATAL);
1001 archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
1004 struct zip *zip = a->format_data;
1006 if ((int64_t)s > zip->entry_uncompressed_limit)
1007 s = (size_t)zip->entry_uncompressed_limit;
1008 zip->entry_uncompressed_written += s;
1010 if (s == 0) return 0;
1012 if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
1013 switch (zip->entry_encryption) {
1014 case ENCRYPTION_TRADITIONAL:
1015 /* Initialize traditional PKWARE encryption context. */
1016 if (!zip->tctx_valid) {
1017 ret = init_traditional_pkware_encryption(a);
1018 if (ret != ARCHIVE_OK)
1020 zip->tctx_valid = 1;
1023 case ENCRYPTION_WINZIP_AES128:
1024 case ENCRYPTION_WINZIP_AES256:
1025 if (!zip->cctx_valid) {
1026 ret = init_winzip_aes_encryption(a);
1027 if (ret != ARCHIVE_OK)
1029 zip->cctx_valid = zip->hctx_valid = 1;
1037 switch (zip->entry_compression) {
1038 case COMPRESSION_STORE:
1039 if (zip->tctx_valid || zip->cctx_valid) {
1040 const uint8_t *rb = (const uint8_t *)buff;
1041 const uint8_t * const re = rb + s;
1046 if (zip->tctx_valid) {
1047 l = trad_enc_encrypt_update(&zip->tctx,
1049 zip->buf, zip->len_buf);
1052 ret = archive_encrypto_aes_ctr_update(
1054 rb, re - rb, zip->buf, &l);
1056 archive_set_error(&a->archive,
1058 "Failed to encrypt file");
1059 return (ARCHIVE_FAILED);
1061 archive_hmac_sha1_update(&zip->hctx,
1064 ret = __archive_write_output(a, zip->buf, l);
1065 if (ret != ARCHIVE_OK)
1067 zip->entry_compressed_written += l;
1068 zip->written_bytes += l;
1072 ret = __archive_write_output(a, buff, s);
1073 if (ret != ARCHIVE_OK)
1075 zip->written_bytes += s;
1076 zip->entry_compressed_written += s;
1080 case COMPRESSION_DEFLATE:
1081 zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
1082 zip->stream.avail_in = (uInt)s;
1084 ret = deflate(&zip->stream, Z_NO_FLUSH);
1085 if (ret == Z_STREAM_ERROR)
1086 return (ARCHIVE_FATAL);
1087 if (zip->stream.avail_out == 0) {
1088 if (zip->tctx_valid) {
1089 trad_enc_encrypt_update(&zip->tctx,
1090 zip->buf, zip->len_buf,
1091 zip->buf, zip->len_buf);
1092 } else if (zip->cctx_valid) {
1093 size_t outl = zip->len_buf;
1094 ret = archive_encrypto_aes_ctr_update(
1096 zip->buf, zip->len_buf,
1099 archive_set_error(&a->archive,
1101 "Failed to encrypt file");
1102 return (ARCHIVE_FAILED);
1104 archive_hmac_sha1_update(&zip->hctx,
1105 zip->buf, zip->len_buf);
1107 ret = __archive_write_output(a, zip->buf,
1109 if (ret != ARCHIVE_OK)
1111 zip->entry_compressed_written += zip->len_buf;
1112 zip->written_bytes += zip->len_buf;
1113 zip->stream.next_out = zip->buf;
1114 zip->stream.avail_out = (uInt)zip->len_buf;
1116 } while (zip->stream.avail_in != 0);
1121 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1122 "Invalid ZIP compression type");
1123 return ARCHIVE_FATAL;
1126 zip->entry_uncompressed_limit -= s;
1127 if (!zip->cctx_valid || zip->aes_vendor != AES_VENDOR_AE_2)
1129 zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
1135 archive_write_zip_finish_entry(struct archive_write *a)
1137 struct zip *zip = a->format_data;
1141 if (zip->entry_compression == COMPRESSION_DEFLATE) {
1145 ret = deflate(&zip->stream, Z_FINISH);
1146 if (ret == Z_STREAM_ERROR)
1147 return (ARCHIVE_FATAL);
1148 remainder = zip->len_buf - zip->stream.avail_out;
1149 if (zip->tctx_valid) {
1150 trad_enc_encrypt_update(&zip->tctx,
1151 zip->buf, remainder, zip->buf, remainder);
1152 } else if (zip->cctx_valid) {
1153 size_t outl = remainder;
1154 ret = archive_encrypto_aes_ctr_update(
1155 &zip->cctx, zip->buf, remainder,
1158 archive_set_error(&a->archive,
1160 "Failed to encrypt file");
1161 return (ARCHIVE_FAILED);
1163 archive_hmac_sha1_update(&zip->hctx,
1164 zip->buf, remainder);
1166 ret = __archive_write_output(a, zip->buf, remainder);
1167 if (ret != ARCHIVE_OK)
1169 zip->entry_compressed_written += remainder;
1170 zip->written_bytes += remainder;
1171 zip->stream.next_out = zip->buf;
1172 if (zip->stream.avail_out != 0)
1174 zip->stream.avail_out = (uInt)zip->len_buf;
1176 deflateEnd(&zip->stream);
1179 if (zip->hctx_valid) {
1181 size_t hmac_len = 20;
1183 archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
1184 ret = __archive_write_output(a, hmac, AUTH_CODE_SIZE);
1185 if (ret != ARCHIVE_OK)
1187 zip->entry_compressed_written += AUTH_CODE_SIZE;
1188 zip->written_bytes += AUTH_CODE_SIZE;
1191 /* Write trailing data descriptor. */
1192 if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
1194 memcpy(d, "PK\007\010", 4);
1195 if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
1196 archive_le32enc(d + 4, 0);/* no CRC.*/
1198 archive_le32enc(d + 4, zip->entry_crc32);
1199 if (zip->entry_uses_zip64) {
1200 archive_le64enc(d + 8,
1201 (uint64_t)zip->entry_compressed_written);
1202 archive_le64enc(d + 16,
1203 (uint64_t)zip->entry_uncompressed_written);
1204 ret = __archive_write_output(a, d, 24);
1205 zip->written_bytes += 24;
1207 archive_le32enc(d + 8,
1208 (uint32_t)zip->entry_compressed_written);
1209 archive_le32enc(d + 12,
1210 (uint32_t)zip->entry_uncompressed_written);
1211 ret = __archive_write_output(a, d, 16);
1212 zip->written_bytes += 16;
1214 if (ret != ARCHIVE_OK)
1215 return (ARCHIVE_FATAL);
1218 /* Append Zip64 extra data to central directory information. */
1219 if (zip->entry_compressed_written > ZIP_4GB_MAX
1220 || zip->entry_uncompressed_written > ZIP_4GB_MAX
1221 || zip->entry_offset > ZIP_4GB_MAX) {
1222 unsigned char zip64[32];
1223 unsigned char *z = zip64, *zd;
1224 memcpy(z, "\001\000\000\000", 4);
1226 if (zip->entry_uncompressed_written >= ZIP_4GB_MAX) {
1227 archive_le64enc(z, zip->entry_uncompressed_written);
1230 if (zip->entry_compressed_written >= ZIP_4GB_MAX) {
1231 archive_le64enc(z, zip->entry_compressed_written);
1234 if (zip->entry_offset >= ZIP_4GB_MAX) {
1235 archive_le64enc(z, zip->entry_offset);
1238 archive_le16enc(zip64 + 2, (uint16_t)(z - (zip64 + 4)));
1239 zd = cd_alloc(zip, z - zip64);
1241 archive_set_error(&a->archive, ENOMEM,
1242 "Can't allocate zip data");
1243 return (ARCHIVE_FATAL);
1245 memcpy(zd, zip64, z - zip64);
1246 /* Zip64 means version needs to be set to at least 4.5 */
1247 if (archive_le16dec(zip->file_header + 6) < 45)
1248 archive_le16enc(zip->file_header + 6, 45);
1251 /* Fix up central directory file header. */
1252 if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
1253 archive_le32enc(zip->file_header + 16, 0);/* no CRC.*/
1255 archive_le32enc(zip->file_header + 16, zip->entry_crc32);
1256 archive_le32enc(zip->file_header + 20,
1257 (uint32_t)zipmin(zip->entry_compressed_written,
1259 archive_le32enc(zip->file_header + 24,
1260 (uint32_t)zipmin(zip->entry_uncompressed_written,
1262 archive_le16enc(zip->file_header + 30,
1263 (uint16_t)(zip->central_directory_bytes - zip->file_header_extra_offset));
1264 archive_le32enc(zip->file_header + 42,
1265 (uint32_t)zipmin(zip->entry_offset,
1268 return (ARCHIVE_OK);
1272 archive_write_zip_close(struct archive_write *a)
1275 int64_t offset_start, offset_end;
1276 struct zip *zip = a->format_data;
1277 struct cd_segment *segment;
1280 offset_start = zip->written_bytes;
1281 segment = zip->central_directory;
1282 while (segment != NULL) {
1283 ret = __archive_write_output(a,
1284 segment->buff, segment->p - segment->buff);
1285 if (ret != ARCHIVE_OK)
1286 return (ARCHIVE_FATAL);
1287 zip->written_bytes += segment->p - segment->buff;
1288 segment = segment->next;
1290 offset_end = zip->written_bytes;
1292 /* If central dir info is too large, write Zip64 end-of-cd */
1293 if (offset_end - offset_start > ZIP_4GB_MAX
1294 || offset_start > ZIP_4GB_MAX
1295 || zip->central_directory_entries > 0xffffUL
1296 || (zip->flags & ZIP_FLAG_FORCE_ZIP64)) {
1297 /* Zip64 end-of-cd record */
1298 memset(buff, 0, 56);
1299 memcpy(buff, "PK\006\006", 4);
1300 archive_le64enc(buff + 4, 44);
1301 archive_le16enc(buff + 12, 45);
1302 archive_le16enc(buff + 14, 45);
1303 /* This is disk 0 of 0. */
1304 archive_le64enc(buff + 24, zip->central_directory_entries);
1305 archive_le64enc(buff + 32, zip->central_directory_entries);
1306 archive_le64enc(buff + 40, offset_end - offset_start);
1307 archive_le64enc(buff + 48, offset_start);
1308 ret = __archive_write_output(a, buff, 56);
1309 if (ret != ARCHIVE_OK)
1310 return (ARCHIVE_FATAL);
1311 zip->written_bytes += 56;
1313 /* Zip64 end-of-cd locator record. */
1314 memset(buff, 0, 20);
1315 memcpy(buff, "PK\006\007", 4);
1316 archive_le32enc(buff + 4, 0);
1317 archive_le64enc(buff + 8, offset_end);
1318 archive_le32enc(buff + 16, 1);
1319 ret = __archive_write_output(a, buff, 20);
1320 if (ret != ARCHIVE_OK)
1321 return (ARCHIVE_FATAL);
1322 zip->written_bytes += 20;
1326 /* Format and write end of central directory. */
1327 memset(buff, 0, sizeof(buff));
1328 memcpy(buff, "PK\005\006", 4);
1329 archive_le16enc(buff + 8, (uint16_t)zipmin(0xffffU,
1330 zip->central_directory_entries));
1331 archive_le16enc(buff + 10, (uint16_t)zipmin(0xffffU,
1332 zip->central_directory_entries));
1333 archive_le32enc(buff + 12,
1334 (uint32_t)zipmin(ZIP_4GB_MAX, (offset_end - offset_start)));
1335 archive_le32enc(buff + 16,
1336 (uint32_t)zipmin(ZIP_4GB_MAX, offset_start));
1337 ret = __archive_write_output(a, buff, 22);
1338 if (ret != ARCHIVE_OK)
1339 return (ARCHIVE_FATAL);
1340 zip->written_bytes += 22;
1341 return (ARCHIVE_OK);
1345 archive_write_zip_free(struct archive_write *a)
1348 struct cd_segment *segment;
1350 zip = a->format_data;
1351 while (zip->central_directory != NULL) {
1352 segment = zip->central_directory;
1353 zip->central_directory = segment->next;
1354 free(segment->buff);
1358 archive_entry_free(zip->entry);
1359 if (zip->cctx_valid)
1360 archive_encrypto_aes_ctr_release(&zip->cctx);
1361 if (zip->hctx_valid)
1362 archive_hmac_sha1_cleanup(&zip->hctx);
1363 /* TODO: Free opt_sconv, sconv_default */
1366 a->format_data = NULL;
1367 return (ARCHIVE_OK);
1370 /* Convert into MSDOS-style date/time. */
1372 dos_time(const time_t unix_time)
1376 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
1379 #if defined(HAVE__LOCALTIME64_S)
1384 /* This will not preserve time when creating/extracting the archive
1385 * on two systems with different time zones. */
1386 #if defined(HAVE_LOCALTIME_R)
1387 t = localtime_r(&unix_time, &tmbuf);
1388 #elif defined(HAVE__LOCALTIME64_S)
1389 tmptime = unix_time;
1390 terr = _localtime64_s(&tmbuf, &tmptime);
1396 t = localtime(&unix_time);
1399 /* MSDOS-style date/time is only between 1980-01-01 and 2107-12-31 */
1400 if (t->tm_year < 1980 - 1900)
1401 /* Set minimum date/time '1980-01-01 00:00:00'. */
1403 else if (t->tm_year > 2107 - 1900)
1404 /* Set maximum date/time '2107-12-31 23:59:58'. */
1408 dt += ((t->tm_year - 80) & 0x7f) << 9;
1409 dt += ((t->tm_mon + 1) & 0x0f) << 5;
1410 dt += (t->tm_mday & 0x1f);
1412 dt += (t->tm_hour & 0x1f) << 11;
1413 dt += (t->tm_min & 0x3f) << 5;
1414 dt += (t->tm_sec & 0x3e) >> 1; /* Only counting every 2 seconds. */
1420 path_length(struct archive_entry *entry)
1426 type = archive_entry_filetype(entry);
1427 path = archive_entry_pathname(entry);
1432 if (type == AE_IFDIR && (path[0] == '\0' || path[len - 1] != '/'))
1433 ++len; /* Space for the trailing / */
1438 write_path(struct archive_entry *entry, struct archive_write *archive)
1443 size_t written_bytes;
1445 path = archive_entry_pathname(entry);
1446 type = archive_entry_filetype(entry);
1450 return (ARCHIVE_FATAL);
1452 ret = __archive_write_output(archive, path, strlen(path));
1453 if (ret != ARCHIVE_OK)
1454 return (ARCHIVE_FATAL);
1455 written_bytes += strlen(path);
1457 /* Folders are recognized by a trailing slash. */
1458 if ((type == AE_IFDIR) & (path[strlen(path) - 1] != '/')) {
1459 ret = __archive_write_output(archive, "/", 1);
1460 if (ret != ARCHIVE_OK)
1461 return (ARCHIVE_FATAL);
1465 return ((int)written_bytes);
1469 copy_path(struct archive_entry *entry, unsigned char *p)
1475 path = archive_entry_pathname(entry);
1476 pathlen = strlen(path);
1477 type = archive_entry_filetype(entry);
1479 memcpy(p, path, pathlen);
1481 /* Folders are recognized by a trailing slash. */
1482 if ((type == AE_IFDIR) && (path[pathlen - 1] != '/'))
1487 static struct archive_string_conv *
1488 get_sconv(struct archive_write *a, struct zip *zip)
1490 if (zip->opt_sconv != NULL)
1491 return (zip->opt_sconv);
1493 if (!zip->init_default_conversion) {
1494 zip->sconv_default =
1495 archive_string_default_conversion_for_write(&(a->archive));
1496 zip->init_default_conversion = 1;
1498 return (zip->sconv_default);
1502 Traditional PKWARE Decryption functions.
1506 trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
1509 #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
1511 ctx->keys[0] = CRC32(ctx->keys[0], c);
1512 ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
1513 t = (ctx->keys[1] >> 24) & 0xff;
1514 ctx->keys[2] = CRC32(ctx->keys[2], t);
1519 trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
1521 unsigned temp = ctx->keys[2] | 2;
1522 return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
1526 trad_enc_encrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
1527 size_t in_len, uint8_t *out, size_t out_len)
1531 max = (unsigned)((in_len < out_len)? in_len: out_len);
1533 for (i = 0; i < max; i++) {
1535 out[i] = t ^ trad_enc_decrypt_byte(ctx);
1536 trad_enc_update_keys(ctx, t);
1542 trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len)
1545 ctx->keys[0] = 305419896L;
1546 ctx->keys[1] = 591751049L;
1547 ctx->keys[2] = 878082192L;
1549 for (;pw_len; --pw_len)
1550 trad_enc_update_keys(ctx, *pw++);
1555 is_traditional_pkware_encryption_supported(void)
1557 uint8_t key[TRAD_HEADER_SIZE];
1559 if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK)
1565 init_traditional_pkware_encryption(struct archive_write *a)
1567 struct zip *zip = a->format_data;
1568 const char *passphrase;
1569 uint8_t key[TRAD_HEADER_SIZE];
1570 uint8_t key_encrypted[TRAD_HEADER_SIZE];
1573 passphrase = __archive_write_get_passphrase(a);
1574 if (passphrase == NULL) {
1575 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1576 "Encryption needs passphrase");
1577 return ARCHIVE_FAILED;
1579 if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK) {
1580 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1581 "Can't generate random number for encryption");
1582 return ARCHIVE_FATAL;
1584 trad_enc_init(&zip->tctx, passphrase, strlen(passphrase));
1585 /* Set the last key code which will be used as a check code
1586 * for verifying passphrase in decryption. */
1587 key[TRAD_HEADER_SIZE-1] = zip->trad_chkdat;
1588 trad_enc_encrypt_update(&zip->tctx, key, TRAD_HEADER_SIZE,
1589 key_encrypted, TRAD_HEADER_SIZE);
1590 /* Write encrypted keys in the top of the file content. */
1591 ret = __archive_write_output(a, key_encrypted, TRAD_HEADER_SIZE);
1592 if (ret != ARCHIVE_OK)
1594 zip->written_bytes += TRAD_HEADER_SIZE;
1595 zip->entry_compressed_written += TRAD_HEADER_SIZE;
1600 init_winzip_aes_encryption(struct archive_write *a)
1602 struct zip *zip = a->format_data;
1603 const char *passphrase;
1604 size_t key_len, salt_len;
1605 uint8_t salt[16 + 2];
1606 uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
1609 passphrase = __archive_write_get_passphrase(a);
1610 if (passphrase == NULL) {
1611 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1612 "Encryption needs passphrase");
1613 return (ARCHIVE_FAILED);
1615 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128) {
1623 if (archive_random(salt, salt_len) != ARCHIVE_OK) {
1624 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1625 "Can't generate random number for encryption");
1626 return (ARCHIVE_FATAL);
1628 archive_pbkdf2_sha1(passphrase, strlen(passphrase),
1629 salt, salt_len, 1000, derived_key, key_len * 2 + 2);
1631 ret = archive_encrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
1633 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1634 "Decryption is unsupported due to lack of crypto library");
1635 return (ARCHIVE_FAILED);
1637 ret = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len,
1640 archive_encrypto_aes_ctr_release(&zip->cctx);
1641 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1642 "Failed to initialize HMAC-SHA1");
1643 return (ARCHIVE_FAILED);
1646 /* Set a password verification value after the 'salt'. */
1647 salt[salt_len] = derived_key[key_len * 2];
1648 salt[salt_len + 1] = derived_key[key_len * 2 + 1];
1650 /* Write encrypted keys in the top of the file content. */
1651 ret = __archive_write_output(a, salt, salt_len + 2);
1652 if (ret != ARCHIVE_OK)
1654 zip->written_bytes += salt_len + 2;
1655 zip->entry_compressed_written += salt_len + 2;
1657 return (ARCHIVE_OK);
1661 is_winzip_aes_encryption_supported(int encryption)
1663 size_t key_len, salt_len;
1664 uint8_t salt[16 + 2];
1665 uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
1666 archive_crypto_ctx cctx;
1667 archive_hmac_sha1_ctx hctx;
1670 if (encryption == ENCRYPTION_WINZIP_AES128) {
1678 if (archive_random(salt, salt_len) != ARCHIVE_OK)
1680 ret = archive_pbkdf2_sha1("p", 1, salt, salt_len, 1000,
1681 derived_key, key_len * 2 + 2);
1685 ret = archive_encrypto_aes_ctr_init(&cctx, derived_key, key_len);
1688 ret = archive_hmac_sha1_init(&hctx, derived_key + key_len,
1690 archive_encrypto_aes_ctr_release(&cctx);
1693 archive_hmac_sha1_cleanup(&hctx);