19c82a63eSPeter Avalos /*-
29c82a63eSPeter Avalos * Copyright (c) 2008 Anselm Strauss
39c82a63eSPeter Avalos * Copyright (c) 2009 Joerg Sonnenberger
46b384f39SPeter Avalos * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
59c82a63eSPeter Avalos * All rights reserved.
69c82a63eSPeter Avalos *
79c82a63eSPeter Avalos * Redistribution and use in source and binary forms, with or without
89c82a63eSPeter Avalos * modification, are permitted provided that the following conditions
99c82a63eSPeter Avalos * are met:
109c82a63eSPeter Avalos * 1. Redistributions of source code must retain the above copyright
119c82a63eSPeter Avalos * notice, this list of conditions and the following disclaimer.
129c82a63eSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
139c82a63eSPeter Avalos * notice, this list of conditions and the following disclaimer in the
149c82a63eSPeter Avalos * documentation and/or other materials provided with the distribution.
159c82a63eSPeter Avalos *
169c82a63eSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
179c82a63eSPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
189c82a63eSPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199c82a63eSPeter Avalos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
209c82a63eSPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
219c82a63eSPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
229c82a63eSPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
239c82a63eSPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
249c82a63eSPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
259c82a63eSPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
269c82a63eSPeter Avalos */
279c82a63eSPeter Avalos
289c82a63eSPeter Avalos /*
299c82a63eSPeter Avalos * Development supported by Google Summer of Code 2008.
309c82a63eSPeter Avalos */
319c82a63eSPeter Avalos
329c82a63eSPeter Avalos #include "archive_platform.h"
339c82a63eSPeter Avalos __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_zip.c 201168 2009-12-29 06:15:32Z kientzle $");
349c82a63eSPeter Avalos
359c82a63eSPeter Avalos #ifdef HAVE_ERRNO_H
369c82a63eSPeter Avalos #include <errno.h>
379c82a63eSPeter Avalos #endif
38c09f92d2SPeter Avalos #ifdef HAVE_LANGINFO_H
39c09f92d2SPeter Avalos #include <langinfo.h>
40c09f92d2SPeter Avalos #endif
419c82a63eSPeter Avalos #ifdef HAVE_STDLIB_H
429c82a63eSPeter Avalos #include <stdlib.h>
439c82a63eSPeter Avalos #endif
449c82a63eSPeter Avalos #ifdef HAVE_STRING_H
459c82a63eSPeter Avalos #include <string.h>
469c82a63eSPeter Avalos #endif
479c82a63eSPeter Avalos #ifdef HAVE_ZLIB_H
489c82a63eSPeter Avalos #include <zlib.h>
499c82a63eSPeter Avalos #endif
509c82a63eSPeter Avalos
519c82a63eSPeter Avalos #include "archive.h"
526b384f39SPeter Avalos #include "archive_cryptor_private.h"
539c82a63eSPeter Avalos #include "archive_endian.h"
549c82a63eSPeter Avalos #include "archive_entry.h"
55c09f92d2SPeter Avalos #include "archive_entry_locale.h"
566b384f39SPeter Avalos #include "archive_hmac_private.h"
579c82a63eSPeter Avalos #include "archive_private.h"
586b384f39SPeter Avalos #include "archive_random_private.h"
599c82a63eSPeter Avalos #include "archive_write_private.h"
60085658deSDaniel Fojt #include "archive_write_set_format_private.h"
619c82a63eSPeter Avalos
629c82a63eSPeter Avalos #ifndef HAVE_ZLIB_H
639c82a63eSPeter Avalos #include "archive_crc32.h"
649c82a63eSPeter Avalos #endif
659c82a63eSPeter Avalos
666b384f39SPeter Avalos #define ZIP_ENTRY_FLAG_ENCRYPTED (1<<0)
676b384f39SPeter Avalos #define ZIP_ENTRY_FLAG_LENGTH_AT_END (1<<3)
686b384f39SPeter Avalos #define ZIP_ENTRY_FLAG_UTF8_NAME (1 << 11)
696b384f39SPeter Avalos
706b384f39SPeter Avalos #define ZIP_4GB_MAX ARCHIVE_LITERAL_LL(0xffffffff)
716b384f39SPeter Avalos #define ZIP_4GB_MAX_UNCOMPRESSED ARCHIVE_LITERAL_LL(0xff000000)
729c82a63eSPeter Avalos
739c82a63eSPeter Avalos enum compression {
746b384f39SPeter Avalos COMPRESSION_UNSPECIFIED = -1,
756b384f39SPeter Avalos COMPRESSION_STORE = 0,
769c82a63eSPeter Avalos COMPRESSION_DEFLATE = 8
779c82a63eSPeter Avalos };
789c82a63eSPeter Avalos
796b384f39SPeter Avalos #ifdef HAVE_ZLIB_H
806b384f39SPeter Avalos #define COMPRESSION_DEFAULT COMPRESSION_DEFLATE
816b384f39SPeter Avalos #else
826b384f39SPeter Avalos #define COMPRESSION_DEFAULT COMPRESSION_STORE
836b384f39SPeter Avalos #endif
846b384f39SPeter Avalos
856b384f39SPeter Avalos enum encryption {
866b384f39SPeter Avalos ENCRYPTION_NONE = 0,
876b384f39SPeter Avalos ENCRYPTION_TRADITIONAL, /* Traditional PKWARE encryption. */
886b384f39SPeter Avalos ENCRYPTION_WINZIP_AES128, /* WinZIP AES-128 encryption. */
896b384f39SPeter Avalos ENCRYPTION_WINZIP_AES256, /* WinZIP AES-256 encryption. */
906b384f39SPeter Avalos };
916b384f39SPeter Avalos
926b384f39SPeter Avalos #define TRAD_HEADER_SIZE 12
936b384f39SPeter Avalos /*
946b384f39SPeter Avalos * See "WinZip - AES Encryption Information"
956b384f39SPeter Avalos * http://www.winzip.com/aes_info.htm
966b384f39SPeter Avalos */
976b384f39SPeter Avalos /* Value used in compression method. */
986b384f39SPeter Avalos #define WINZIP_AES_ENCRYPTION 99
996b384f39SPeter Avalos /* A WinZip AES header size which is stored at the beginning of
1006b384f39SPeter Avalos * file contents. */
1016b384f39SPeter Avalos #define WINZIP_AES128_HEADER_SIZE (8 + 2)
1026b384f39SPeter Avalos #define WINZIP_AES256_HEADER_SIZE (16 + 2)
1036b384f39SPeter Avalos /* AES vendor version. */
1046b384f39SPeter Avalos #define AES_VENDOR_AE_1 0x0001
1056b384f39SPeter Avalos #define AES_VENDOR_AE_2 0x0002
1066b384f39SPeter Avalos /* Authentication code size. */
1076b384f39SPeter Avalos #define AUTH_CODE_SIZE 10
1086b384f39SPeter Avalos /**/
1096b384f39SPeter Avalos #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
1106b384f39SPeter Avalos
1116b384f39SPeter Avalos struct cd_segment {
1126b384f39SPeter Avalos struct cd_segment *next;
1136b384f39SPeter Avalos size_t buff_size;
1146b384f39SPeter Avalos unsigned char *buff;
1156b384f39SPeter Avalos unsigned char *p;
1166b384f39SPeter Avalos };
1176b384f39SPeter Avalos
1186b384f39SPeter Avalos struct trad_enc_ctx {
1196b384f39SPeter Avalos uint32_t keys[3];
1206b384f39SPeter Avalos };
1216b384f39SPeter Avalos
1226b384f39SPeter Avalos struct zip {
1236b384f39SPeter Avalos
1246b384f39SPeter Avalos int64_t entry_offset;
1256b384f39SPeter Avalos int64_t entry_compressed_size;
1266b384f39SPeter Avalos int64_t entry_uncompressed_size;
1276b384f39SPeter Avalos int64_t entry_compressed_written;
1286b384f39SPeter Avalos int64_t entry_uncompressed_written;
1296b384f39SPeter Avalos int64_t entry_uncompressed_limit;
1306b384f39SPeter Avalos struct archive_entry *entry;
1316b384f39SPeter Avalos uint32_t entry_crc32;
1326b384f39SPeter Avalos enum compression entry_compression;
1336b384f39SPeter Avalos enum encryption entry_encryption;
1346b384f39SPeter Avalos int entry_flags;
1356b384f39SPeter Avalos int entry_uses_zip64;
1366b384f39SPeter Avalos int experiments;
1376b384f39SPeter Avalos struct trad_enc_ctx tctx;
1386b384f39SPeter Avalos char tctx_valid;
1396b384f39SPeter Avalos unsigned char trad_chkdat;
1406b384f39SPeter Avalos unsigned aes_vendor;
1416b384f39SPeter Avalos archive_crypto_ctx cctx;
1426b384f39SPeter Avalos char cctx_valid;
1436b384f39SPeter Avalos archive_hmac_sha1_ctx hctx;
1446b384f39SPeter Avalos char hctx_valid;
1456b384f39SPeter Avalos
1466b384f39SPeter Avalos unsigned char *file_header;
1476b384f39SPeter Avalos size_t file_header_extra_offset;
1486b384f39SPeter Avalos unsigned long (*crc32func)(unsigned long crc, const void *buff, size_t len);
1496b384f39SPeter Avalos
1506b384f39SPeter Avalos struct cd_segment *central_directory;
1516b384f39SPeter Avalos struct cd_segment *central_directory_last;
1526b384f39SPeter Avalos size_t central_directory_bytes;
1536b384f39SPeter Avalos size_t central_directory_entries;
1546b384f39SPeter Avalos
1556b384f39SPeter Avalos int64_t written_bytes; /* Overall position in file. */
1566b384f39SPeter Avalos
1576b384f39SPeter Avalos struct archive_string_conv *opt_sconv;
1586b384f39SPeter Avalos struct archive_string_conv *sconv_default;
1596b384f39SPeter Avalos enum compression requested_compression;
1606b384f39SPeter Avalos int deflate_compression_level;
1616b384f39SPeter Avalos int init_default_conversion;
1626b384f39SPeter Avalos enum encryption encryption_type;
1636b384f39SPeter Avalos
1646b384f39SPeter Avalos #define ZIP_FLAG_AVOID_ZIP64 1
1656b384f39SPeter Avalos #define ZIP_FLAG_FORCE_ZIP64 2
1666b384f39SPeter Avalos #define ZIP_FLAG_EXPERIMENT_xl 4
1676b384f39SPeter Avalos int flags;
1686b384f39SPeter Avalos
1696b384f39SPeter Avalos #ifdef HAVE_ZLIB_H
1706b384f39SPeter Avalos z_stream stream;
1716b384f39SPeter Avalos #endif
1726b384f39SPeter Avalos size_t len_buf;
1736b384f39SPeter Avalos unsigned char *buf;
1746b384f39SPeter Avalos };
1756b384f39SPeter Avalos
1766b384f39SPeter Avalos /* Don't call this min or MIN, since those are already defined
1776b384f39SPeter Avalos on lots of platforms (but not all). */
1786b384f39SPeter Avalos #define zipmin(a, b) ((a) > (b) ? (b) : (a))
1796b384f39SPeter Avalos
180c09f92d2SPeter Avalos static ssize_t archive_write_zip_data(struct archive_write *,
181c09f92d2SPeter Avalos const void *buff, size_t s);
182c09f92d2SPeter Avalos static int archive_write_zip_close(struct archive_write *);
183c09f92d2SPeter Avalos static int archive_write_zip_free(struct archive_write *);
1849c82a63eSPeter Avalos static int archive_write_zip_finish_entry(struct archive_write *);
185c09f92d2SPeter Avalos static int archive_write_zip_header(struct archive_write *,
186c09f92d2SPeter Avalos struct archive_entry *);
187c09f92d2SPeter Avalos static int archive_write_zip_options(struct archive_write *,
188c09f92d2SPeter Avalos const char *, const char *);
1899c82a63eSPeter Avalos static unsigned int dos_time(const time_t);
1909c82a63eSPeter Avalos static size_t path_length(struct archive_entry *);
1919c82a63eSPeter Avalos static int write_path(struct archive_entry *, struct archive_write *);
1926b384f39SPeter Avalos static void copy_path(struct archive_entry *, unsigned char *);
1936b384f39SPeter Avalos static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
1946b384f39SPeter Avalos static int trad_enc_init(struct trad_enc_ctx *, const char *, size_t);
1956b384f39SPeter Avalos static unsigned trad_enc_encrypt_update(struct trad_enc_ctx *, const uint8_t *,
1966b384f39SPeter Avalos size_t, uint8_t *, size_t);
1976b384f39SPeter Avalos static int init_traditional_pkware_encryption(struct archive_write *);
1986b384f39SPeter Avalos static int is_traditional_pkware_encryption_supported(void);
1996b384f39SPeter Avalos static int init_winzip_aes_encryption(struct archive_write *);
2006b384f39SPeter Avalos static int is_winzip_aes_encryption_supported(int encryption);
2019c82a63eSPeter Avalos
2026b384f39SPeter Avalos static unsigned char *
cd_alloc(struct zip * zip,size_t length)2036b384f39SPeter Avalos cd_alloc(struct zip *zip, size_t length)
2046b384f39SPeter Avalos {
2056b384f39SPeter Avalos unsigned char *p;
2069c82a63eSPeter Avalos
2076b384f39SPeter Avalos if (zip->central_directory == NULL
2086b384f39SPeter Avalos || (zip->central_directory_last->p + length
2096b384f39SPeter Avalos > zip->central_directory_last->buff + zip->central_directory_last->buff_size)) {
2106b384f39SPeter Avalos struct cd_segment *segment = calloc(1, sizeof(*segment));
2116b384f39SPeter Avalos if (segment == NULL)
2126b384f39SPeter Avalos return NULL;
2136b384f39SPeter Avalos segment->buff_size = 64 * 1024;
2146b384f39SPeter Avalos segment->buff = malloc(segment->buff_size);
2156b384f39SPeter Avalos if (segment->buff == NULL) {
2166b384f39SPeter Avalos free(segment);
2176b384f39SPeter Avalos return NULL;
2186b384f39SPeter Avalos }
2196b384f39SPeter Avalos segment->p = segment->buff;
2209c82a63eSPeter Avalos
2216b384f39SPeter Avalos if (zip->central_directory == NULL) {
2226b384f39SPeter Avalos zip->central_directory
2236b384f39SPeter Avalos = zip->central_directory_last
2246b384f39SPeter Avalos = segment;
2256b384f39SPeter Avalos } else {
2266b384f39SPeter Avalos zip->central_directory_last->next = segment;
2276b384f39SPeter Avalos zip->central_directory_last = segment;
2286b384f39SPeter Avalos }
2296b384f39SPeter Avalos }
2309c82a63eSPeter Avalos
2316b384f39SPeter Avalos p = zip->central_directory_last->p;
2326b384f39SPeter Avalos zip->central_directory_last->p += length;
2336b384f39SPeter Avalos zip->central_directory_bytes += length;
2346b384f39SPeter Avalos return (p);
2356b384f39SPeter Avalos }
2369c82a63eSPeter Avalos
2376b384f39SPeter Avalos static unsigned long
real_crc32(unsigned long crc,const void * buff,size_t len)2386b384f39SPeter Avalos real_crc32(unsigned long crc, const void *buff, size_t len)
2396b384f39SPeter Avalos {
2406b384f39SPeter Avalos return crc32(crc, buff, (unsigned int)len);
2416b384f39SPeter Avalos }
24259bf7050SPeter Avalos
2436b384f39SPeter Avalos static unsigned long
fake_crc32(unsigned long crc,const void * buff,size_t len)2446b384f39SPeter Avalos fake_crc32(unsigned long crc, const void *buff, size_t len)
2456b384f39SPeter Avalos {
2466b384f39SPeter Avalos (void)crc; /* UNUSED */
2476b384f39SPeter Avalos (void)buff; /* UNUSED */
2486b384f39SPeter Avalos (void)len; /* UNUSED */
2496b384f39SPeter Avalos return 0;
2506b384f39SPeter Avalos }
2519c82a63eSPeter Avalos
2529c82a63eSPeter Avalos static int
archive_write_zip_options(struct archive_write * a,const char * key,const char * val)2539c82a63eSPeter Avalos archive_write_zip_options(struct archive_write *a, const char *key,
254c09f92d2SPeter Avalos const char *val)
2559c82a63eSPeter Avalos {
2569c82a63eSPeter Avalos struct zip *zip = a->format_data;
257c09f92d2SPeter Avalos int ret = ARCHIVE_FAILED;
2589c82a63eSPeter Avalos
2599c82a63eSPeter Avalos if (strcmp(key, "compression") == 0) {
2606b384f39SPeter Avalos /*
2616b384f39SPeter Avalos * Set compression to use on all future entries.
2626b384f39SPeter Avalos * This only affects regular files.
2636b384f39SPeter Avalos */
264c09f92d2SPeter Avalos if (val == NULL || val[0] == 0) {
265c09f92d2SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
266c09f92d2SPeter Avalos "%s: compression option needs a compression name",
267c09f92d2SPeter Avalos a->format_name);
268c09f92d2SPeter Avalos } else if (strcmp(val, "deflate") == 0) {
2699c82a63eSPeter Avalos #ifdef HAVE_ZLIB_H
2706b384f39SPeter Avalos zip->requested_compression = COMPRESSION_DEFLATE;
271c09f92d2SPeter Avalos ret = ARCHIVE_OK;
2729c82a63eSPeter Avalos #else
2739c82a63eSPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2749c82a63eSPeter Avalos "deflate compression not supported");
2759c82a63eSPeter Avalos #endif
276c09f92d2SPeter Avalos } else if (strcmp(val, "store") == 0) {
2776b384f39SPeter Avalos zip->requested_compression = COMPRESSION_STORE;
278c09f92d2SPeter Avalos ret = ARCHIVE_OK;
2799c82a63eSPeter Avalos }
28059bf7050SPeter Avalos return (ret);
2816b384f39SPeter Avalos } else if (strcmp(key, "compression-level") == 0) {
2826b384f39SPeter Avalos if (val == NULL || !(val[0] >= '0' && val[0] <= '9') || val[1] != '\0') {
2836b384f39SPeter Avalos return ARCHIVE_WARN;
2846b384f39SPeter Avalos }
2856b384f39SPeter Avalos
2866b384f39SPeter Avalos if (val[0] == '0') {
2876b384f39SPeter Avalos zip->requested_compression = COMPRESSION_STORE;
2886b384f39SPeter Avalos return ARCHIVE_OK;
2896b384f39SPeter Avalos } else {
2906b384f39SPeter Avalos #ifdef HAVE_ZLIB_H
2916b384f39SPeter Avalos zip->requested_compression = COMPRESSION_DEFLATE;
2926b384f39SPeter Avalos zip->deflate_compression_level = val[0] - '0';
2936b384f39SPeter Avalos return ARCHIVE_OK;
2946b384f39SPeter Avalos #else
2956b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2966b384f39SPeter Avalos "deflate compression not supported");
2976b384f39SPeter Avalos #endif
2986b384f39SPeter Avalos }
2996b384f39SPeter Avalos } else if (strcmp(key, "encryption") == 0) {
3006b384f39SPeter Avalos if (val == NULL) {
3016b384f39SPeter Avalos zip->encryption_type = ENCRYPTION_NONE;
3026b384f39SPeter Avalos ret = ARCHIVE_OK;
3036b384f39SPeter Avalos } else if (val[0] == '1' || strcmp(val, "traditional") == 0
3046b384f39SPeter Avalos || strcmp(val, "zipcrypt") == 0
3056b384f39SPeter Avalos || strcmp(val, "ZipCrypt") == 0) {
3066b384f39SPeter Avalos if (is_traditional_pkware_encryption_supported()) {
3076b384f39SPeter Avalos zip->encryption_type = ENCRYPTION_TRADITIONAL;
3086b384f39SPeter Avalos ret = ARCHIVE_OK;
3096b384f39SPeter Avalos } else {
3106b384f39SPeter Avalos archive_set_error(&a->archive,
3116b384f39SPeter Avalos ARCHIVE_ERRNO_MISC,
3126b384f39SPeter Avalos "encryption not supported");
3136b384f39SPeter Avalos }
3146b384f39SPeter Avalos } else if (strcmp(val, "aes128") == 0) {
3156b384f39SPeter Avalos if (is_winzip_aes_encryption_supported(
3166b384f39SPeter Avalos ENCRYPTION_WINZIP_AES128)) {
3176b384f39SPeter Avalos zip->encryption_type = ENCRYPTION_WINZIP_AES128;
3186b384f39SPeter Avalos ret = ARCHIVE_OK;
3196b384f39SPeter Avalos } else {
3206b384f39SPeter Avalos archive_set_error(&a->archive,
3216b384f39SPeter Avalos ARCHIVE_ERRNO_MISC,
3226b384f39SPeter Avalos "encryption not supported");
3236b384f39SPeter Avalos }
3246b384f39SPeter Avalos } else if (strcmp(val, "aes256") == 0) {
3256b384f39SPeter Avalos if (is_winzip_aes_encryption_supported(
3266b384f39SPeter Avalos ENCRYPTION_WINZIP_AES256)) {
3276b384f39SPeter Avalos zip->encryption_type = ENCRYPTION_WINZIP_AES256;
3286b384f39SPeter Avalos ret = ARCHIVE_OK;
3296b384f39SPeter Avalos } else {
3306b384f39SPeter Avalos archive_set_error(&a->archive,
3316b384f39SPeter Avalos ARCHIVE_ERRNO_MISC,
3326b384f39SPeter Avalos "encryption not supported");
3336b384f39SPeter Avalos }
3346b384f39SPeter Avalos } else {
3356b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3366b384f39SPeter Avalos "%s: unknown encryption '%s'",
3376b384f39SPeter Avalos a->format_name, val);
3386b384f39SPeter Avalos }
3396b384f39SPeter Avalos return (ret);
3406b384f39SPeter Avalos } else if (strcmp(key, "experimental") == 0) {
3416b384f39SPeter Avalos if (val == NULL || val[0] == 0) {
3426b384f39SPeter Avalos zip->flags &= ~ ZIP_FLAG_EXPERIMENT_xl;
3436b384f39SPeter Avalos } else {
3446b384f39SPeter Avalos zip->flags |= ZIP_FLAG_EXPERIMENT_xl;
3456b384f39SPeter Avalos }
3466b384f39SPeter Avalos return (ARCHIVE_OK);
3476b384f39SPeter Avalos } else if (strcmp(key, "fakecrc32") == 0) {
3486b384f39SPeter Avalos /*
3496b384f39SPeter Avalos * FOR TESTING ONLY: disable CRC calculation to speed up
3506b384f39SPeter Avalos * certain complex tests.
3516b384f39SPeter Avalos */
3526b384f39SPeter Avalos if (val == NULL || val[0] == 0) {
3536b384f39SPeter Avalos zip->crc32func = real_crc32;
3546b384f39SPeter Avalos } else {
3556b384f39SPeter Avalos zip->crc32func = fake_crc32;
3566b384f39SPeter Avalos }
3576b384f39SPeter Avalos return (ARCHIVE_OK);
358c09f92d2SPeter Avalos } else if (strcmp(key, "hdrcharset") == 0) {
3596b384f39SPeter Avalos /*
3606b384f39SPeter Avalos * Set the character set used in translating filenames.
3616b384f39SPeter Avalos */
362c09f92d2SPeter Avalos if (val == NULL || val[0] == 0) {
363c09f92d2SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
364c09f92d2SPeter Avalos "%s: hdrcharset option needs a character-set name",
365c09f92d2SPeter Avalos a->format_name);
366c09f92d2SPeter Avalos } else {
367c09f92d2SPeter Avalos zip->opt_sconv = archive_string_conversion_to_charset(
368c09f92d2SPeter Avalos &a->archive, val, 0);
369c09f92d2SPeter Avalos if (zip->opt_sconv != NULL)
370c09f92d2SPeter Avalos ret = ARCHIVE_OK;
371c09f92d2SPeter Avalos else
372c09f92d2SPeter Avalos ret = ARCHIVE_FATAL;
373c09f92d2SPeter Avalos }
374c09f92d2SPeter Avalos return (ret);
3756b384f39SPeter Avalos } else if (strcmp(key, "zip64") == 0) {
3766b384f39SPeter Avalos /*
3776b384f39SPeter Avalos * Bias decisions about Zip64: force them to be
3786b384f39SPeter Avalos * generated in certain cases where they are not
3796b384f39SPeter Avalos * forbidden or avoid them in certain cases where they
3806b384f39SPeter Avalos * are not strictly required.
3816b384f39SPeter Avalos */
3826b384f39SPeter Avalos if (val != NULL && *val != '\0') {
3836b384f39SPeter Avalos zip->flags |= ZIP_FLAG_FORCE_ZIP64;
3846b384f39SPeter Avalos zip->flags &= ~ZIP_FLAG_AVOID_ZIP64;
3856b384f39SPeter Avalos } else {
3866b384f39SPeter Avalos zip->flags &= ~ZIP_FLAG_FORCE_ZIP64;
3876b384f39SPeter Avalos zip->flags |= ZIP_FLAG_AVOID_ZIP64;
3886b384f39SPeter Avalos }
3896b384f39SPeter Avalos return (ARCHIVE_OK);
3909c82a63eSPeter Avalos }
3919c82a63eSPeter Avalos
39259bf7050SPeter Avalos /* Note: The "warn" return is just to inform the options
39359bf7050SPeter Avalos * supervisor that we didn't handle it. It will generate
39459bf7050SPeter Avalos * a suitable error if no one used this option. */
39559bf7050SPeter Avalos return (ARCHIVE_WARN);
39659bf7050SPeter Avalos }
39759bf7050SPeter Avalos
3989c82a63eSPeter Avalos int
archive_write_zip_set_compression_deflate(struct archive * _a)399d4d8193eSPeter Avalos archive_write_zip_set_compression_deflate(struct archive *_a)
400d4d8193eSPeter Avalos {
401d4d8193eSPeter Avalos struct archive_write *a = (struct archive_write *)_a;
402d4d8193eSPeter Avalos int ret = ARCHIVE_FAILED;
403d4d8193eSPeter Avalos
404d4d8193eSPeter Avalos archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
4056b384f39SPeter Avalos ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
406d4d8193eSPeter Avalos "archive_write_zip_set_compression_deflate");
407d4d8193eSPeter Avalos if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
408d4d8193eSPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
409d4d8193eSPeter Avalos "Can only use archive_write_zip_set_compression_deflate"
410d4d8193eSPeter Avalos " with zip format");
411d4d8193eSPeter Avalos ret = ARCHIVE_FATAL;
412d4d8193eSPeter Avalos } else {
413d4d8193eSPeter Avalos #ifdef HAVE_ZLIB_H
414d4d8193eSPeter Avalos struct zip *zip = a->format_data;
4156b384f39SPeter Avalos zip->requested_compression = COMPRESSION_DEFLATE;
416d4d8193eSPeter Avalos ret = ARCHIVE_OK;
417d4d8193eSPeter Avalos #else
418d4d8193eSPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
419d4d8193eSPeter Avalos "deflate compression not supported");
4206b384f39SPeter Avalos ret = ARCHIVE_FAILED;
421d4d8193eSPeter Avalos #endif
422d4d8193eSPeter Avalos }
423d4d8193eSPeter Avalos return (ret);
424d4d8193eSPeter Avalos }
425d4d8193eSPeter Avalos
426d4d8193eSPeter Avalos int
archive_write_zip_set_compression_store(struct archive * _a)427d4d8193eSPeter Avalos archive_write_zip_set_compression_store(struct archive *_a)
428d4d8193eSPeter Avalos {
429d4d8193eSPeter Avalos struct archive_write *a = (struct archive_write *)_a;
430d4d8193eSPeter Avalos struct zip *zip = a->format_data;
431d4d8193eSPeter Avalos int ret = ARCHIVE_FAILED;
432d4d8193eSPeter Avalos
433d4d8193eSPeter Avalos archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
4346b384f39SPeter Avalos ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
435d4d8193eSPeter Avalos "archive_write_zip_set_compression_deflate");
436d4d8193eSPeter Avalos if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
437d4d8193eSPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
438d4d8193eSPeter Avalos "Can only use archive_write_zip_set_compression_store"
439d4d8193eSPeter Avalos " with zip format");
440d4d8193eSPeter Avalos ret = ARCHIVE_FATAL;
441d4d8193eSPeter Avalos } else {
4426b384f39SPeter Avalos zip->requested_compression = COMPRESSION_STORE;
443d4d8193eSPeter Avalos ret = ARCHIVE_OK;
444d4d8193eSPeter Avalos }
445d4d8193eSPeter Avalos return (ret);
446d4d8193eSPeter Avalos }
447d4d8193eSPeter Avalos
448d4d8193eSPeter Avalos int
archive_write_set_format_zip(struct archive * _a)4499c82a63eSPeter Avalos archive_write_set_format_zip(struct archive *_a)
4509c82a63eSPeter Avalos {
4519c82a63eSPeter Avalos struct archive_write *a = (struct archive_write *)_a;
4529c82a63eSPeter Avalos struct zip *zip;
4539c82a63eSPeter Avalos
454c09f92d2SPeter Avalos archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
455c09f92d2SPeter Avalos ARCHIVE_STATE_NEW, "archive_write_set_format_zip");
456c09f92d2SPeter Avalos
4579c82a63eSPeter Avalos /* If another format was already registered, unregister it. */
458c09f92d2SPeter Avalos if (a->format_free != NULL)
459c09f92d2SPeter Avalos (a->format_free)(a);
4609c82a63eSPeter Avalos
4619c82a63eSPeter Avalos zip = (struct zip *) calloc(1, sizeof(*zip));
4629c82a63eSPeter Avalos if (zip == NULL) {
463c09f92d2SPeter Avalos archive_set_error(&a->archive, ENOMEM,
464c09f92d2SPeter Avalos "Can't allocate zip data");
4659c82a63eSPeter Avalos return (ARCHIVE_FATAL);
4669c82a63eSPeter Avalos }
4679c82a63eSPeter Avalos
4686b384f39SPeter Avalos /* "Unspecified" lets us choose the appropriate compression. */
4696b384f39SPeter Avalos zip->requested_compression = COMPRESSION_UNSPECIFIED;
4709c82a63eSPeter Avalos #ifdef HAVE_ZLIB_H
4716b384f39SPeter Avalos zip->deflate_compression_level = Z_DEFAULT_COMPRESSION;
4726b384f39SPeter Avalos #endif
4736b384f39SPeter Avalos zip->crc32func = real_crc32;
4746b384f39SPeter Avalos
4756b384f39SPeter Avalos /* A buffer used for both compression and encryption. */
4769c82a63eSPeter Avalos zip->len_buf = 65536;
4779c82a63eSPeter Avalos zip->buf = malloc(zip->len_buf);
4789c82a63eSPeter Avalos if (zip->buf == NULL) {
47959bf7050SPeter Avalos free(zip);
480c09f92d2SPeter Avalos archive_set_error(&a->archive, ENOMEM,
481c09f92d2SPeter Avalos "Can't allocate compression buffer");
4829c82a63eSPeter Avalos return (ARCHIVE_FATAL);
4839c82a63eSPeter Avalos }
4849c82a63eSPeter Avalos
4859c82a63eSPeter Avalos a->format_data = zip;
4869c82a63eSPeter Avalos a->format_name = "zip";
4879c82a63eSPeter Avalos a->format_options = archive_write_zip_options;
4889c82a63eSPeter Avalos a->format_write_header = archive_write_zip_header;
4899c82a63eSPeter Avalos a->format_write_data = archive_write_zip_data;
4909c82a63eSPeter Avalos a->format_finish_entry = archive_write_zip_finish_entry;
491c09f92d2SPeter Avalos a->format_close = archive_write_zip_close;
492c09f92d2SPeter Avalos a->format_free = archive_write_zip_free;
4939c82a63eSPeter Avalos a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
4949c82a63eSPeter Avalos a->archive.archive_format_name = "ZIP";
4959c82a63eSPeter Avalos
4969c82a63eSPeter Avalos return (ARCHIVE_OK);
4979c82a63eSPeter Avalos }
4989c82a63eSPeter Avalos
4999c82a63eSPeter Avalos static int
is_all_ascii(const char * p)500c09f92d2SPeter Avalos is_all_ascii(const char *p)
501c09f92d2SPeter Avalos {
502c09f92d2SPeter Avalos const unsigned char *pp = (const unsigned char *)p;
503c09f92d2SPeter Avalos
504c09f92d2SPeter Avalos while (*pp) {
505c09f92d2SPeter Avalos if (*pp++ > 127)
506c09f92d2SPeter Avalos return (0);
507c09f92d2SPeter Avalos }
508c09f92d2SPeter Avalos return (1);
509c09f92d2SPeter Avalos }
510c09f92d2SPeter Avalos
511c09f92d2SPeter Avalos static int
archive_write_zip_header(struct archive_write * a,struct archive_entry * entry)5129c82a63eSPeter Avalos archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
5139c82a63eSPeter Avalos {
5146b384f39SPeter Avalos unsigned char local_header[32];
5156b384f39SPeter Avalos unsigned char local_extra[144];
5166b384f39SPeter Avalos struct zip *zip = a->format_data;
5176b384f39SPeter Avalos unsigned char *e;
5186b384f39SPeter Avalos unsigned char *cd_extra;
5196b384f39SPeter Avalos size_t filename_length;
5206b384f39SPeter Avalos const char *slink = NULL;
5216b384f39SPeter Avalos size_t slink_size = 0;
5226b384f39SPeter Avalos struct archive_string_conv *sconv = get_sconv(a, zip);
523c09f92d2SPeter Avalos int ret, ret2 = ARCHIVE_OK;
5249c82a63eSPeter Avalos mode_t type;
5256b384f39SPeter Avalos int version_needed = 10;
5269c82a63eSPeter Avalos
5276b384f39SPeter Avalos /* Ignore types of entries that we don't support. */
5289c82a63eSPeter Avalos type = archive_entry_filetype(entry);
529c09f92d2SPeter Avalos if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) {
530085658deSDaniel Fojt __archive_write_entry_filetype_unsupported(
531085658deSDaniel Fojt &a->archive, entry, "zip");
5329c82a63eSPeter Avalos return ARCHIVE_FAILED;
5339c82a63eSPeter Avalos };
5349c82a63eSPeter Avalos
5356b384f39SPeter Avalos /* If we're not using Zip64, reject large files. */
5366b384f39SPeter Avalos if (zip->flags & ZIP_FLAG_AVOID_ZIP64) {
5376b384f39SPeter Avalos /* Reject entries over 4GB. */
5386b384f39SPeter Avalos if (archive_entry_size_is_set(entry)
5396b384f39SPeter Avalos && (archive_entry_size(entry) > ZIP_4GB_MAX)) {
5406b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
5416b384f39SPeter Avalos "Files > 4GB require Zip64 extensions");
5426b384f39SPeter Avalos return ARCHIVE_FAILED;
5436b384f39SPeter Avalos }
5446b384f39SPeter Avalos /* Reject entries if archive is > 4GB. */
5456b384f39SPeter Avalos if (zip->written_bytes > ZIP_4GB_MAX) {
5466b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
5476b384f39SPeter Avalos "Archives > 4GB require Zip64 extensions");
5486b384f39SPeter Avalos return ARCHIVE_FAILED;
5496b384f39SPeter Avalos }
5506b384f39SPeter Avalos }
5516b384f39SPeter Avalos
5526b384f39SPeter Avalos /* Only regular files can have size > 0. */
5536b384f39SPeter Avalos if (type != AE_IFREG)
5549c82a63eSPeter Avalos archive_entry_set_size(entry, 0);
5559c82a63eSPeter Avalos
5566b384f39SPeter Avalos
5576b384f39SPeter Avalos /* Reset information from last entry. */
5586b384f39SPeter Avalos zip->entry_offset = zip->written_bytes;
5596b384f39SPeter Avalos zip->entry_uncompressed_limit = INT64_MAX;
5606b384f39SPeter Avalos zip->entry_compressed_size = 0;
5616b384f39SPeter Avalos zip->entry_uncompressed_size = 0;
5626b384f39SPeter Avalos zip->entry_compressed_written = 0;
5636b384f39SPeter Avalos zip->entry_uncompressed_written = 0;
5646b384f39SPeter Avalos zip->entry_flags = 0;
5656b384f39SPeter Avalos zip->entry_uses_zip64 = 0;
5666b384f39SPeter Avalos zip->entry_crc32 = zip->crc32func(0, NULL, 0);
5676b384f39SPeter Avalos zip->entry_encryption = 0;
5686b384f39SPeter Avalos archive_entry_free(zip->entry);
5696b384f39SPeter Avalos zip->entry = NULL;
570c09f92d2SPeter Avalos
5716b384f39SPeter Avalos if (zip->cctx_valid)
5726b384f39SPeter Avalos archive_encrypto_aes_ctr_release(&zip->cctx);
5736b384f39SPeter Avalos if (zip->hctx_valid)
5746b384f39SPeter Avalos archive_hmac_sha1_cleanup(&zip->hctx);
5756b384f39SPeter Avalos zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
5769c82a63eSPeter Avalos
5776b384f39SPeter Avalos if (type == AE_IFREG
5786b384f39SPeter Avalos &&(!archive_entry_size_is_set(entry)
5796b384f39SPeter Avalos || archive_entry_size(entry) > 0)) {
5806b384f39SPeter Avalos switch (zip->encryption_type) {
5816b384f39SPeter Avalos case ENCRYPTION_TRADITIONAL:
5826b384f39SPeter Avalos case ENCRYPTION_WINZIP_AES128:
5836b384f39SPeter Avalos case ENCRYPTION_WINZIP_AES256:
5846b384f39SPeter Avalos zip->entry_flags |= ZIP_ENTRY_FLAG_ENCRYPTED;
5856b384f39SPeter Avalos zip->entry_encryption = zip->encryption_type;
5866b384f39SPeter Avalos break;
587*50f8aa9cSAntonio Huete Jimenez case ENCRYPTION_NONE:
5886b384f39SPeter Avalos default:
5896b384f39SPeter Avalos break;
5909c82a63eSPeter Avalos }
5916b384f39SPeter Avalos }
5926b384f39SPeter Avalos
5936b384f39SPeter Avalos
59459bf7050SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
595e95abc47Szrj /* Make sure the path separators in pathname, hardlink and symlink
59659bf7050SPeter Avalos * are all slash '/', not the Windows path separator '\'. */
5976b384f39SPeter Avalos zip->entry = __la_win_entry_in_posix_pathseparator(entry);
5986b384f39SPeter Avalos if (zip->entry == entry)
5996b384f39SPeter Avalos zip->entry = archive_entry_clone(entry);
60059bf7050SPeter Avalos #else
6016b384f39SPeter Avalos zip->entry = archive_entry_clone(entry);
60259bf7050SPeter Avalos #endif
6036b384f39SPeter Avalos if (zip->entry == NULL) {
60459bf7050SPeter Avalos archive_set_error(&a->archive, ENOMEM,
60559bf7050SPeter Avalos "Can't allocate zip header data");
60659bf7050SPeter Avalos return (ARCHIVE_FATAL);
60759bf7050SPeter Avalos }
6086b384f39SPeter Avalos
609c09f92d2SPeter Avalos if (sconv != NULL) {
610c09f92d2SPeter Avalos const char *p;
611c09f92d2SPeter Avalos size_t len;
612c09f92d2SPeter Avalos
613c09f92d2SPeter Avalos if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) {
614c09f92d2SPeter Avalos if (errno == ENOMEM) {
615c09f92d2SPeter Avalos archive_set_error(&a->archive, ENOMEM,
616c09f92d2SPeter Avalos "Can't allocate memory for Pathname");
617c09f92d2SPeter Avalos return (ARCHIVE_FATAL);
618c09f92d2SPeter Avalos }
619c09f92d2SPeter Avalos archive_set_error(&a->archive,
620c09f92d2SPeter Avalos ARCHIVE_ERRNO_FILE_FORMAT,
62159bf7050SPeter Avalos "Can't translate Pathname '%s' to %s",
622c09f92d2SPeter Avalos archive_entry_pathname(entry),
623c09f92d2SPeter Avalos archive_string_conversion_charset_name(sconv));
624c09f92d2SPeter Avalos ret2 = ARCHIVE_WARN;
625c09f92d2SPeter Avalos }
626c09f92d2SPeter Avalos if (len > 0)
6276b384f39SPeter Avalos archive_entry_set_pathname(zip->entry, p);
62859bf7050SPeter Avalos
62959bf7050SPeter Avalos /*
6306b384f39SPeter Avalos * There is no standard for symlink handling; we convert
6316b384f39SPeter Avalos * it using the same character-set translation that we use
6326b384f39SPeter Avalos * for filename.
63359bf7050SPeter Avalos */
63459bf7050SPeter Avalos if (type == AE_IFLNK) {
63559bf7050SPeter Avalos if (archive_entry_symlink_l(entry, &p, &len, sconv)) {
63659bf7050SPeter Avalos if (errno == ENOMEM) {
63759bf7050SPeter Avalos archive_set_error(&a->archive, ENOMEM,
63859bf7050SPeter Avalos "Can't allocate memory "
63959bf7050SPeter Avalos " for Symlink");
64059bf7050SPeter Avalos return (ARCHIVE_FATAL);
641c09f92d2SPeter Avalos }
6426b384f39SPeter Avalos /* No error if we can't convert. */
64359bf7050SPeter Avalos } else if (len > 0)
6446b384f39SPeter Avalos archive_entry_set_symlink(zip->entry, p);
64559bf7050SPeter Avalos }
64659bf7050SPeter Avalos }
647c09f92d2SPeter Avalos
6486b384f39SPeter Avalos /* If filename isn't ASCII and we can use UTF-8, set the UTF-8 flag. */
6496b384f39SPeter Avalos if (!is_all_ascii(archive_entry_pathname(zip->entry))) {
6506b384f39SPeter Avalos if (zip->opt_sconv != NULL) {
6516b384f39SPeter Avalos if (strcmp(archive_string_conversion_charset_name(
6526b384f39SPeter Avalos zip->opt_sconv), "UTF-8") == 0)
6536b384f39SPeter Avalos zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
6546b384f39SPeter Avalos #if HAVE_NL_LANGINFO
6556b384f39SPeter Avalos } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
6566b384f39SPeter Avalos zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
6576b384f39SPeter Avalos #endif
6586b384f39SPeter Avalos }
6596b384f39SPeter Avalos }
6606b384f39SPeter Avalos filename_length = path_length(zip->entry);
6616b384f39SPeter Avalos
6626b384f39SPeter Avalos /* Determine appropriate compression and size for this entry. */
663c09f92d2SPeter Avalos if (type == AE_IFLNK) {
6646b384f39SPeter Avalos slink = archive_entry_symlink(zip->entry);
6656b384f39SPeter Avalos if (slink != NULL)
6666b384f39SPeter Avalos slink_size = strlen(slink);
667c09f92d2SPeter Avalos else
6686b384f39SPeter Avalos slink_size = 0;
6696b384f39SPeter Avalos zip->entry_uncompressed_limit = slink_size;
6706b384f39SPeter Avalos zip->entry_compressed_size = slink_size;
6716b384f39SPeter Avalos zip->entry_uncompressed_size = slink_size;
6726b384f39SPeter Avalos zip->entry_crc32 = zip->crc32func(zip->entry_crc32,
6736b384f39SPeter Avalos (const unsigned char *)slink, slink_size);
6746b384f39SPeter Avalos zip->entry_compression = COMPRESSION_STORE;
6756b384f39SPeter Avalos version_needed = 20;
6766b384f39SPeter Avalos } else if (type != AE_IFREG) {
6776b384f39SPeter Avalos zip->entry_compression = COMPRESSION_STORE;
6786b384f39SPeter Avalos zip->entry_uncompressed_limit = 0;
6796b384f39SPeter Avalos version_needed = 20;
6806b384f39SPeter Avalos } else if (archive_entry_size_is_set(zip->entry)) {
6816b384f39SPeter Avalos int64_t size = archive_entry_size(zip->entry);
6826b384f39SPeter Avalos int64_t additional_size = 0;
6836b384f39SPeter Avalos
6846b384f39SPeter Avalos zip->entry_uncompressed_limit = size;
6856b384f39SPeter Avalos zip->entry_compression = zip->requested_compression;
6866b384f39SPeter Avalos if (zip->entry_compression == COMPRESSION_UNSPECIFIED) {
6876b384f39SPeter Avalos zip->entry_compression = COMPRESSION_DEFAULT;
688c09f92d2SPeter Avalos }
6896b384f39SPeter Avalos if (zip->entry_compression == COMPRESSION_STORE) {
6906b384f39SPeter Avalos zip->entry_compressed_size = size;
6916b384f39SPeter Avalos zip->entry_uncompressed_size = size;
6926b384f39SPeter Avalos version_needed = 10;
6939c82a63eSPeter Avalos } else {
6946b384f39SPeter Avalos zip->entry_uncompressed_size = size;
6956b384f39SPeter Avalos version_needed = 20;
6969c82a63eSPeter Avalos }
6979c82a63eSPeter Avalos
6986b384f39SPeter Avalos if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
6996b384f39SPeter Avalos switch (zip->entry_encryption) {
7006b384f39SPeter Avalos case ENCRYPTION_TRADITIONAL:
7016b384f39SPeter Avalos additional_size = TRAD_HEADER_SIZE;
7026b384f39SPeter Avalos version_needed = 20;
7039c82a63eSPeter Avalos break;
7046b384f39SPeter Avalos case ENCRYPTION_WINZIP_AES128:
7056b384f39SPeter Avalos additional_size = WINZIP_AES128_HEADER_SIZE
7066b384f39SPeter Avalos + AUTH_CODE_SIZE;
7076b384f39SPeter Avalos version_needed = 20;
7086b384f39SPeter Avalos break;
7096b384f39SPeter Avalos case ENCRYPTION_WINZIP_AES256:
7106b384f39SPeter Avalos additional_size = WINZIP_AES256_HEADER_SIZE
7116b384f39SPeter Avalos + AUTH_CODE_SIZE;
7126b384f39SPeter Avalos version_needed = 20;
7136b384f39SPeter Avalos break;
714*50f8aa9cSAntonio Huete Jimenez case ENCRYPTION_NONE:
7156b384f39SPeter Avalos default:
7166b384f39SPeter Avalos break;
7176b384f39SPeter Avalos }
7186b384f39SPeter Avalos if (zip->entry_compression == COMPRESSION_STORE)
7196b384f39SPeter Avalos zip->entry_compressed_size += additional_size;
7206b384f39SPeter Avalos }
7219c82a63eSPeter Avalos
7226b384f39SPeter Avalos /*
7236b384f39SPeter Avalos * Set Zip64 extension in any of the following cases
7246b384f39SPeter Avalos * (this was suggested by discussion on info-zip-dev
7256b384f39SPeter Avalos * mailing list):
7266b384f39SPeter Avalos * = Zip64 is being forced by user
7276b384f39SPeter Avalos * = File is over 4GiB uncompressed
7286b384f39SPeter Avalos * (including encryption header, if any)
7296b384f39SPeter Avalos * = File is close to 4GiB and is being compressed
7306b384f39SPeter Avalos * (compression might make file larger)
7316b384f39SPeter Avalos */
7326b384f39SPeter Avalos if ((zip->flags & ZIP_FLAG_FORCE_ZIP64)
7336b384f39SPeter Avalos || (zip->entry_uncompressed_size + additional_size > ZIP_4GB_MAX)
7346b384f39SPeter Avalos || (zip->entry_uncompressed_size > ZIP_4GB_MAX_UNCOMPRESSED
7356b384f39SPeter Avalos && zip->entry_compression != COMPRESSION_STORE)) {
7366b384f39SPeter Avalos zip->entry_uses_zip64 = 1;
7376b384f39SPeter Avalos version_needed = 45;
7386b384f39SPeter Avalos }
7396b384f39SPeter Avalos
7406b384f39SPeter Avalos /* We may know the size, but never the CRC. */
7416b384f39SPeter Avalos zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
7426b384f39SPeter Avalos } else {
743*50f8aa9cSAntonio Huete Jimenez /* We don't know the size. Use the default
744*50f8aa9cSAntonio Huete Jimenez * compression unless specified otherwise.
745*50f8aa9cSAntonio Huete Jimenez * We enable Zip64 extensions unless we're told not to.
7466b384f39SPeter Avalos */
747*50f8aa9cSAntonio Huete Jimenez
748*50f8aa9cSAntonio Huete Jimenez zip->entry_compression = zip->requested_compression;
749*50f8aa9cSAntonio Huete Jimenez if(zip->entry_compression == COMPRESSION_UNSPECIFIED){
7506b384f39SPeter Avalos zip->entry_compression = COMPRESSION_DEFAULT;
751*50f8aa9cSAntonio Huete Jimenez }
752*50f8aa9cSAntonio Huete Jimenez
7536b384f39SPeter Avalos zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
7546b384f39SPeter Avalos if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) {
7556b384f39SPeter Avalos zip->entry_uses_zip64 = 1;
7566b384f39SPeter Avalos version_needed = 45;
7576b384f39SPeter Avalos } else if (zip->entry_compression == COMPRESSION_STORE) {
7586b384f39SPeter Avalos version_needed = 10;
7596b384f39SPeter Avalos } else {
7606b384f39SPeter Avalos version_needed = 20;
7616b384f39SPeter Avalos }
7626b384f39SPeter Avalos
7636b384f39SPeter Avalos if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
7646b384f39SPeter Avalos switch (zip->entry_encryption) {
7656b384f39SPeter Avalos case ENCRYPTION_TRADITIONAL:
7666b384f39SPeter Avalos case ENCRYPTION_WINZIP_AES128:
7676b384f39SPeter Avalos case ENCRYPTION_WINZIP_AES256:
7686b384f39SPeter Avalos if (version_needed < 20)
7696b384f39SPeter Avalos version_needed = 20;
7706b384f39SPeter Avalos break;
771*50f8aa9cSAntonio Huete Jimenez case ENCRYPTION_NONE:
7726b384f39SPeter Avalos default:
7736b384f39SPeter Avalos break;
7746b384f39SPeter Avalos }
7756b384f39SPeter Avalos }
7766b384f39SPeter Avalos }
7776b384f39SPeter Avalos
7786b384f39SPeter Avalos /* Format the local header. */
7796b384f39SPeter Avalos memset(local_header, 0, sizeof(local_header));
7806b384f39SPeter Avalos memcpy(local_header, "PK\003\004", 4);
7816b384f39SPeter Avalos archive_le16enc(local_header + 4, version_needed);
7826b384f39SPeter Avalos archive_le16enc(local_header + 6, zip->entry_flags);
7836b384f39SPeter Avalos if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
7846b384f39SPeter Avalos || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
7856b384f39SPeter Avalos archive_le16enc(local_header + 8, WINZIP_AES_ENCRYPTION);
7866b384f39SPeter Avalos else
7876b384f39SPeter Avalos archive_le16enc(local_header + 8, zip->entry_compression);
7886b384f39SPeter Avalos archive_le32enc(local_header + 10,
7896b384f39SPeter Avalos dos_time(archive_entry_mtime(zip->entry)));
7906b384f39SPeter Avalos archive_le32enc(local_header + 14, zip->entry_crc32);
7916b384f39SPeter Avalos if (zip->entry_uses_zip64) {
7926b384f39SPeter Avalos /* Zip64 data in the local header "must" include both
7936b384f39SPeter Avalos * compressed and uncompressed sizes AND those fields
7946b384f39SPeter Avalos * are included only if these are 0xffffffff;
7956b384f39SPeter Avalos * THEREFORE these must be set this way, even if we
7966b384f39SPeter Avalos * know one of them is smaller. */
7976b384f39SPeter Avalos archive_le32enc(local_header + 18, ZIP_4GB_MAX);
7986b384f39SPeter Avalos archive_le32enc(local_header + 22, ZIP_4GB_MAX);
7996b384f39SPeter Avalos } else {
8006b384f39SPeter Avalos archive_le32enc(local_header + 18, (uint32_t)zip->entry_compressed_size);
8016b384f39SPeter Avalos archive_le32enc(local_header + 22, (uint32_t)zip->entry_uncompressed_size);
8026b384f39SPeter Avalos }
8036b384f39SPeter Avalos archive_le16enc(local_header + 26, (uint16_t)filename_length);
8046b384f39SPeter Avalos
8056b384f39SPeter Avalos if (zip->entry_encryption == ENCRYPTION_TRADITIONAL) {
8066b384f39SPeter Avalos if (zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END)
8076b384f39SPeter Avalos zip->trad_chkdat = local_header[11];
8086b384f39SPeter Avalos else
8096b384f39SPeter Avalos zip->trad_chkdat = local_header[17];
8106b384f39SPeter Avalos }
8116b384f39SPeter Avalos
8126b384f39SPeter Avalos /* Format as much of central directory file header as we can: */
8136b384f39SPeter Avalos zip->file_header = cd_alloc(zip, 46);
8146b384f39SPeter Avalos /* If (zip->file_header == NULL) XXXX */
8156b384f39SPeter Avalos ++zip->central_directory_entries;
8166b384f39SPeter Avalos memset(zip->file_header, 0, 46);
8176b384f39SPeter Avalos memcpy(zip->file_header, "PK\001\002", 4);
8186b384f39SPeter Avalos /* "Made by PKZip 2.0 on Unix." */
8196b384f39SPeter Avalos archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed);
8206b384f39SPeter Avalos archive_le16enc(zip->file_header + 6, version_needed);
8216b384f39SPeter Avalos archive_le16enc(zip->file_header + 8, zip->entry_flags);
8226b384f39SPeter Avalos if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
8236b384f39SPeter Avalos || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
8246b384f39SPeter Avalos archive_le16enc(zip->file_header + 10, WINZIP_AES_ENCRYPTION);
8256b384f39SPeter Avalos else
8266b384f39SPeter Avalos archive_le16enc(zip->file_header + 10, zip->entry_compression);
8276b384f39SPeter Avalos archive_le32enc(zip->file_header + 12,
8286b384f39SPeter Avalos dos_time(archive_entry_mtime(zip->entry)));
8296b384f39SPeter Avalos archive_le16enc(zip->file_header + 28, (uint16_t)filename_length);
8306b384f39SPeter Avalos /* Following Info-Zip, store mode in the "external attributes" field. */
8316b384f39SPeter Avalos archive_le32enc(zip->file_header + 38,
8326b384f39SPeter Avalos ((uint32_t)archive_entry_mode(zip->entry)) << 16);
8336b384f39SPeter Avalos e = cd_alloc(zip, filename_length);
8346b384f39SPeter Avalos /* If (e == NULL) XXXX */
8356b384f39SPeter Avalos copy_path(zip->entry, e);
8366b384f39SPeter Avalos
8376b384f39SPeter Avalos /* Format extra data. */
8386b384f39SPeter Avalos memset(local_extra, 0, sizeof(local_extra));
8396b384f39SPeter Avalos e = local_extra;
8406b384f39SPeter Avalos
8416b384f39SPeter Avalos /* First, extra blocks that are the same between
8426b384f39SPeter Avalos * the local file header and the central directory.
8436b384f39SPeter Avalos * We format them once and then duplicate them. */
8446b384f39SPeter Avalos
8456b384f39SPeter Avalos /* UT timestamp, length depends on what timestamps are set. */
8466b384f39SPeter Avalos memcpy(e, "UT", 2);
8476b384f39SPeter Avalos archive_le16enc(e + 2,
8486b384f39SPeter Avalos 1
8496b384f39SPeter Avalos + (archive_entry_mtime_is_set(entry) ? 4 : 0)
8506b384f39SPeter Avalos + (archive_entry_atime_is_set(entry) ? 4 : 0)
8516b384f39SPeter Avalos + (archive_entry_ctime_is_set(entry) ? 4 : 0));
8526b384f39SPeter Avalos e += 4;
8536b384f39SPeter Avalos *e++ =
8546b384f39SPeter Avalos (archive_entry_mtime_is_set(entry) ? 1 : 0)
8556b384f39SPeter Avalos | (archive_entry_atime_is_set(entry) ? 2 : 0)
8566b384f39SPeter Avalos | (archive_entry_ctime_is_set(entry) ? 4 : 0);
8576b384f39SPeter Avalos if (archive_entry_mtime_is_set(entry)) {
8586b384f39SPeter Avalos archive_le32enc(e, (uint32_t)archive_entry_mtime(entry));
8596b384f39SPeter Avalos e += 4;
8606b384f39SPeter Avalos }
8616b384f39SPeter Avalos if (archive_entry_atime_is_set(entry)) {
8626b384f39SPeter Avalos archive_le32enc(e, (uint32_t)archive_entry_atime(entry));
8636b384f39SPeter Avalos e += 4;
8646b384f39SPeter Avalos }
8656b384f39SPeter Avalos if (archive_entry_ctime_is_set(entry)) {
8666b384f39SPeter Avalos archive_le32enc(e, (uint32_t)archive_entry_ctime(entry));
8676b384f39SPeter Avalos e += 4;
8686b384f39SPeter Avalos }
8696b384f39SPeter Avalos
8706b384f39SPeter Avalos /* ux Unix extra data, length 11, version 1 */
8716b384f39SPeter Avalos /* TODO: If uid < 64k, use 2 bytes, ditto for gid. */
8726b384f39SPeter Avalos memcpy(e, "ux\013\000\001", 5);
8736b384f39SPeter Avalos e += 5;
8746b384f39SPeter Avalos *e++ = 4; /* Length of following UID */
8756b384f39SPeter Avalos archive_le32enc(e, (uint32_t)archive_entry_uid(entry));
8766b384f39SPeter Avalos e += 4;
8776b384f39SPeter Avalos *e++ = 4; /* Length of following GID */
8786b384f39SPeter Avalos archive_le32enc(e, (uint32_t)archive_entry_gid(entry));
8796b384f39SPeter Avalos e += 4;
8806b384f39SPeter Avalos
8816b384f39SPeter Avalos /* AES extra data field: WinZIP AES information, ID=0x9901 */
8826b384f39SPeter Avalos if ((zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED)
8836b384f39SPeter Avalos && (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
8846b384f39SPeter Avalos || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)) {
8856b384f39SPeter Avalos
8866b384f39SPeter Avalos memcpy(e, "\001\231\007\000\001\000AE", 8);
887e95abc47Szrj /* AES vendor version AE-2 does not store a CRC.
8886b384f39SPeter Avalos * WinZip 11 uses AE-1, which does store the CRC,
8896b384f39SPeter Avalos * but it does not store the CRC when the file size
8906b384f39SPeter Avalos * is less than 20 bytes. So we simulate what
8916b384f39SPeter Avalos * WinZip 11 does.
8926b384f39SPeter Avalos * NOTE: WinZip 9.0 and 10.0 uses AE-2 by default. */
8936b384f39SPeter Avalos if (archive_entry_size_is_set(zip->entry)
8946b384f39SPeter Avalos && archive_entry_size(zip->entry) < 20) {
8956b384f39SPeter Avalos archive_le16enc(e+4, AES_VENDOR_AE_2);
8966b384f39SPeter Avalos zip->aes_vendor = AES_VENDOR_AE_2;/* no CRC. */
8976b384f39SPeter Avalos } else
8986b384f39SPeter Avalos zip->aes_vendor = AES_VENDOR_AE_1;
8996b384f39SPeter Avalos e += 8;
9006b384f39SPeter Avalos /* AES encryption strength. */
9016b384f39SPeter Avalos *e++ = (zip->entry_encryption == ENCRYPTION_WINZIP_AES128)?1:3;
9026b384f39SPeter Avalos /* Actual compression method. */
9036b384f39SPeter Avalos archive_le16enc(e, zip->entry_compression);
9046b384f39SPeter Avalos e += 2;
9056b384f39SPeter Avalos }
9066b384f39SPeter Avalos
9076b384f39SPeter Avalos /* Copy UT ,ux, and AES-extra into central directory as well. */
9086b384f39SPeter Avalos zip->file_header_extra_offset = zip->central_directory_bytes;
9096b384f39SPeter Avalos cd_extra = cd_alloc(zip, e - local_extra);
9106b384f39SPeter Avalos memcpy(cd_extra, local_extra, e - local_extra);
9116b384f39SPeter Avalos
9126b384f39SPeter Avalos /*
9136b384f39SPeter Avalos * Following extra blocks vary between local header and
9146b384f39SPeter Avalos * central directory. These are the local header versions.
9156b384f39SPeter Avalos * Central directory versions get formatted in
9166b384f39SPeter Avalos * archive_write_zip_finish_entry() below.
9176b384f39SPeter Avalos */
9186b384f39SPeter Avalos
9196b384f39SPeter Avalos /* "[Zip64 entry] in the local header MUST include BOTH
9206b384f39SPeter Avalos * original [uncompressed] and compressed size fields." */
9216b384f39SPeter Avalos if (zip->entry_uses_zip64) {
9226b384f39SPeter Avalos unsigned char *zip64_start = e;
9236b384f39SPeter Avalos memcpy(e, "\001\000\020\000", 4);
9246b384f39SPeter Avalos e += 4;
9256b384f39SPeter Avalos archive_le64enc(e, zip->entry_uncompressed_size);
9266b384f39SPeter Avalos e += 8;
9276b384f39SPeter Avalos archive_le64enc(e, zip->entry_compressed_size);
9286b384f39SPeter Avalos e += 8;
9296b384f39SPeter Avalos archive_le16enc(zip64_start + 2, (uint16_t)(e - (zip64_start + 4)));
9306b384f39SPeter Avalos }
9316b384f39SPeter Avalos
9326b384f39SPeter Avalos if (zip->flags & ZIP_FLAG_EXPERIMENT_xl) {
9336b384f39SPeter Avalos /* Experimental 'xl' extension to improve streaming. */
9346b384f39SPeter Avalos unsigned char *external_info = e;
9356b384f39SPeter Avalos int included = 7;
9366b384f39SPeter Avalos memcpy(e, "xl\000\000", 4); // 0x6c65 + 2-byte length
9376b384f39SPeter Avalos e += 4;
9386b384f39SPeter Avalos e[0] = included; /* bitmap of included fields */
9396b384f39SPeter Avalos e += 1;
9406b384f39SPeter Avalos if (included & 1) {
9416b384f39SPeter Avalos archive_le16enc(e, /* "Version created by" */
9426b384f39SPeter Avalos 3 * 256 + version_needed);
9436b384f39SPeter Avalos e += 2;
9446b384f39SPeter Avalos }
9456b384f39SPeter Avalos if (included & 2) {
9466b384f39SPeter Avalos archive_le16enc(e, 0); /* internal file attributes */
9476b384f39SPeter Avalos e += 2;
9486b384f39SPeter Avalos }
9496b384f39SPeter Avalos if (included & 4) {
9506b384f39SPeter Avalos archive_le32enc(e, /* external file attributes */
9516b384f39SPeter Avalos ((uint32_t)archive_entry_mode(zip->entry)) << 16);
9526b384f39SPeter Avalos e += 4;
9536b384f39SPeter Avalos }
9546b384f39SPeter Avalos if (included & 8) {
9556b384f39SPeter Avalos // Libarchive does not currently support file comments.
9566b384f39SPeter Avalos }
9576b384f39SPeter Avalos archive_le16enc(external_info + 2, (uint16_t)(e - (external_info + 4)));
9586b384f39SPeter Avalos }
9596b384f39SPeter Avalos
9606b384f39SPeter Avalos /* Update local header with size of extra data and write it all out: */
9616b384f39SPeter Avalos archive_le16enc(local_header + 28, (uint16_t)(e - local_extra));
9626b384f39SPeter Avalos
9636b384f39SPeter Avalos ret = __archive_write_output(a, local_header, 30);
9646b384f39SPeter Avalos if (ret != ARCHIVE_OK)
9656b384f39SPeter Avalos return (ARCHIVE_FATAL);
9666b384f39SPeter Avalos zip->written_bytes += 30;
9676b384f39SPeter Avalos
9686b384f39SPeter Avalos ret = write_path(zip->entry, a);
9696b384f39SPeter Avalos if (ret <= ARCHIVE_OK)
9706b384f39SPeter Avalos return (ARCHIVE_FATAL);
9716b384f39SPeter Avalos zip->written_bytes += ret;
9726b384f39SPeter Avalos
9736b384f39SPeter Avalos ret = __archive_write_output(a, local_extra, e - local_extra);
9746b384f39SPeter Avalos if (ret != ARCHIVE_OK)
9756b384f39SPeter Avalos return (ARCHIVE_FATAL);
9766b384f39SPeter Avalos zip->written_bytes += e - local_extra;
9776b384f39SPeter Avalos
9786b384f39SPeter Avalos /* For symlinks, write the body now. */
9796b384f39SPeter Avalos if (slink != NULL) {
9806b384f39SPeter Avalos ret = __archive_write_output(a, slink, slink_size);
9816b384f39SPeter Avalos if (ret != ARCHIVE_OK)
9826b384f39SPeter Avalos return (ARCHIVE_FATAL);
9836b384f39SPeter Avalos zip->entry_compressed_written += slink_size;
9846b384f39SPeter Avalos zip->entry_uncompressed_written += slink_size;
9856b384f39SPeter Avalos zip->written_bytes += slink_size;
9866b384f39SPeter Avalos }
9876b384f39SPeter Avalos
9886b384f39SPeter Avalos #ifdef HAVE_ZLIB_H
9896b384f39SPeter Avalos if (zip->entry_compression == COMPRESSION_DEFLATE) {
9909c82a63eSPeter Avalos zip->stream.zalloc = Z_NULL;
9919c82a63eSPeter Avalos zip->stream.zfree = Z_NULL;
9929c82a63eSPeter Avalos zip->stream.opaque = Z_NULL;
9939c82a63eSPeter Avalos zip->stream.next_out = zip->buf;
994d4d8193eSPeter Avalos zip->stream.avail_out = (uInt)zip->len_buf;
9956b384f39SPeter Avalos if (deflateInit2(&zip->stream, zip->deflate_compression_level,
996c09f92d2SPeter Avalos Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
997c09f92d2SPeter Avalos archive_set_error(&a->archive, ENOMEM,
998c09f92d2SPeter Avalos "Can't init deflate compressor");
9999c82a63eSPeter Avalos return (ARCHIVE_FATAL);
10009c82a63eSPeter Avalos }
10016b384f39SPeter Avalos }
10029c82a63eSPeter Avalos #endif
10039c82a63eSPeter Avalos
1004c09f92d2SPeter Avalos return (ret2);
10059c82a63eSPeter Avalos }
10069c82a63eSPeter Avalos
10079c82a63eSPeter Avalos static ssize_t
archive_write_zip_data(struct archive_write * a,const void * buff,size_t s)10089c82a63eSPeter Avalos archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
10099c82a63eSPeter Avalos {
10109c82a63eSPeter Avalos int ret;
10119c82a63eSPeter Avalos struct zip *zip = a->format_data;
10129c82a63eSPeter Avalos
10136b384f39SPeter Avalos if ((int64_t)s > zip->entry_uncompressed_limit)
10146b384f39SPeter Avalos s = (size_t)zip->entry_uncompressed_limit;
10156b384f39SPeter Avalos zip->entry_uncompressed_written += s;
10169c82a63eSPeter Avalos
10179c82a63eSPeter Avalos if (s == 0) return 0;
10189c82a63eSPeter Avalos
10196b384f39SPeter Avalos if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
10206b384f39SPeter Avalos switch (zip->entry_encryption) {
10216b384f39SPeter Avalos case ENCRYPTION_TRADITIONAL:
1022e95abc47Szrj /* Initialize traditional PKWARE encryption context. */
10236b384f39SPeter Avalos if (!zip->tctx_valid) {
10246b384f39SPeter Avalos ret = init_traditional_pkware_encryption(a);
10256b384f39SPeter Avalos if (ret != ARCHIVE_OK)
10266b384f39SPeter Avalos return (ret);
10276b384f39SPeter Avalos zip->tctx_valid = 1;
10286b384f39SPeter Avalos }
10296b384f39SPeter Avalos break;
10306b384f39SPeter Avalos case ENCRYPTION_WINZIP_AES128:
10316b384f39SPeter Avalos case ENCRYPTION_WINZIP_AES256:
10326b384f39SPeter Avalos if (!zip->cctx_valid) {
10336b384f39SPeter Avalos ret = init_winzip_aes_encryption(a);
10346b384f39SPeter Avalos if (ret != ARCHIVE_OK)
10356b384f39SPeter Avalos return (ret);
10366b384f39SPeter Avalos zip->cctx_valid = zip->hctx_valid = 1;
10376b384f39SPeter Avalos }
10386b384f39SPeter Avalos break;
1039*50f8aa9cSAntonio Huete Jimenez case ENCRYPTION_NONE:
10406b384f39SPeter Avalos default:
10416b384f39SPeter Avalos break;
10426b384f39SPeter Avalos }
10436b384f39SPeter Avalos }
10446b384f39SPeter Avalos
10456b384f39SPeter Avalos switch (zip->entry_compression) {
10469c82a63eSPeter Avalos case COMPRESSION_STORE:
10476b384f39SPeter Avalos if (zip->tctx_valid || zip->cctx_valid) {
10486b384f39SPeter Avalos const uint8_t *rb = (const uint8_t *)buff;
10496b384f39SPeter Avalos const uint8_t * const re = rb + s;
10506b384f39SPeter Avalos
10516b384f39SPeter Avalos while (rb < re) {
10526b384f39SPeter Avalos size_t l;
10536b384f39SPeter Avalos
10546b384f39SPeter Avalos if (zip->tctx_valid) {
10556b384f39SPeter Avalos l = trad_enc_encrypt_update(&zip->tctx,
10566b384f39SPeter Avalos rb, re - rb,
10576b384f39SPeter Avalos zip->buf, zip->len_buf);
10586b384f39SPeter Avalos } else {
10596b384f39SPeter Avalos l = zip->len_buf;
10606b384f39SPeter Avalos ret = archive_encrypto_aes_ctr_update(
10616b384f39SPeter Avalos &zip->cctx,
10626b384f39SPeter Avalos rb, re - rb, zip->buf, &l);
10636b384f39SPeter Avalos if (ret < 0) {
10646b384f39SPeter Avalos archive_set_error(&a->archive,
10656b384f39SPeter Avalos ARCHIVE_ERRNO_MISC,
10666b384f39SPeter Avalos "Failed to encrypt file");
10676b384f39SPeter Avalos return (ARCHIVE_FAILED);
10686b384f39SPeter Avalos }
10696b384f39SPeter Avalos archive_hmac_sha1_update(&zip->hctx,
10706b384f39SPeter Avalos zip->buf, l);
10716b384f39SPeter Avalos }
10726b384f39SPeter Avalos ret = __archive_write_output(a, zip->buf, l);
10736b384f39SPeter Avalos if (ret != ARCHIVE_OK)
10746b384f39SPeter Avalos return (ret);
10756b384f39SPeter Avalos zip->entry_compressed_written += l;
10766b384f39SPeter Avalos zip->written_bytes += l;
10776b384f39SPeter Avalos rb += l;
10786b384f39SPeter Avalos }
10796b384f39SPeter Avalos } else {
1080c09f92d2SPeter Avalos ret = __archive_write_output(a, buff, s);
10816b384f39SPeter Avalos if (ret != ARCHIVE_OK)
10826b384f39SPeter Avalos return (ret);
10839c82a63eSPeter Avalos zip->written_bytes += s;
10846b384f39SPeter Avalos zip->entry_compressed_written += s;
10856b384f39SPeter Avalos }
10866b384f39SPeter Avalos break;
10879c82a63eSPeter Avalos #if HAVE_ZLIB_H
10889c82a63eSPeter Avalos case COMPRESSION_DEFLATE:
10899c82a63eSPeter Avalos zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
1090d4d8193eSPeter Avalos zip->stream.avail_in = (uInt)s;
10919c82a63eSPeter Avalos do {
10929c82a63eSPeter Avalos ret = deflate(&zip->stream, Z_NO_FLUSH);
10939c82a63eSPeter Avalos if (ret == Z_STREAM_ERROR)
10949c82a63eSPeter Avalos return (ARCHIVE_FATAL);
10959c82a63eSPeter Avalos if (zip->stream.avail_out == 0) {
10966b384f39SPeter Avalos if (zip->tctx_valid) {
10976b384f39SPeter Avalos trad_enc_encrypt_update(&zip->tctx,
10986b384f39SPeter Avalos zip->buf, zip->len_buf,
10996b384f39SPeter Avalos zip->buf, zip->len_buf);
11006b384f39SPeter Avalos } else if (zip->cctx_valid) {
11016b384f39SPeter Avalos size_t outl = zip->len_buf;
11026b384f39SPeter Avalos ret = archive_encrypto_aes_ctr_update(
11036b384f39SPeter Avalos &zip->cctx,
11046b384f39SPeter Avalos zip->buf, zip->len_buf,
11056b384f39SPeter Avalos zip->buf, &outl);
11066b384f39SPeter Avalos if (ret < 0) {
11076b384f39SPeter Avalos archive_set_error(&a->archive,
11086b384f39SPeter Avalos ARCHIVE_ERRNO_MISC,
11096b384f39SPeter Avalos "Failed to encrypt file");
11106b384f39SPeter Avalos return (ARCHIVE_FAILED);
11116b384f39SPeter Avalos }
11126b384f39SPeter Avalos archive_hmac_sha1_update(&zip->hctx,
11136b384f39SPeter Avalos zip->buf, zip->len_buf);
11146b384f39SPeter Avalos }
1115c09f92d2SPeter Avalos ret = __archive_write_output(a, zip->buf,
1116c09f92d2SPeter Avalos zip->len_buf);
11179c82a63eSPeter Avalos if (ret != ARCHIVE_OK)
11189c82a63eSPeter Avalos return (ret);
11196b384f39SPeter Avalos zip->entry_compressed_written += zip->len_buf;
11209c82a63eSPeter Avalos zip->written_bytes += zip->len_buf;
11219c82a63eSPeter Avalos zip->stream.next_out = zip->buf;
1122d4d8193eSPeter Avalos zip->stream.avail_out = (uInt)zip->len_buf;
11239c82a63eSPeter Avalos }
11249c82a63eSPeter Avalos } while (zip->stream.avail_in != 0);
11256b384f39SPeter Avalos break;
11269c82a63eSPeter Avalos #endif
11279c82a63eSPeter Avalos
1128*50f8aa9cSAntonio Huete Jimenez case COMPRESSION_UNSPECIFIED:
11299c82a63eSPeter Avalos default:
11309c82a63eSPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
11319c82a63eSPeter Avalos "Invalid ZIP compression type");
11329c82a63eSPeter Avalos return ARCHIVE_FATAL;
11339c82a63eSPeter Avalos }
11346b384f39SPeter Avalos
11356b384f39SPeter Avalos zip->entry_uncompressed_limit -= s;
11366b384f39SPeter Avalos if (!zip->cctx_valid || zip->aes_vendor != AES_VENDOR_AE_2)
11376b384f39SPeter Avalos zip->entry_crc32 =
11386b384f39SPeter Avalos zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
11396b384f39SPeter Avalos return (s);
11406b384f39SPeter Avalos
11419c82a63eSPeter Avalos }
11429c82a63eSPeter Avalos
11439c82a63eSPeter Avalos static int
archive_write_zip_finish_entry(struct archive_write * a)11449c82a63eSPeter Avalos archive_write_zip_finish_entry(struct archive_write *a)
11459c82a63eSPeter Avalos {
11469c82a63eSPeter Avalos struct zip *zip = a->format_data;
11476b384f39SPeter Avalos int ret;
11489c82a63eSPeter Avalos
11499c82a63eSPeter Avalos #if HAVE_ZLIB_H
11506b384f39SPeter Avalos if (zip->entry_compression == COMPRESSION_DEFLATE) {
11519c82a63eSPeter Avalos for (;;) {
11526b384f39SPeter Avalos size_t remainder;
11536b384f39SPeter Avalos
11549c82a63eSPeter Avalos ret = deflate(&zip->stream, Z_FINISH);
11559c82a63eSPeter Avalos if (ret == Z_STREAM_ERROR)
11569c82a63eSPeter Avalos return (ARCHIVE_FATAL);
11576b384f39SPeter Avalos remainder = zip->len_buf - zip->stream.avail_out;
11586b384f39SPeter Avalos if (zip->tctx_valid) {
11596b384f39SPeter Avalos trad_enc_encrypt_update(&zip->tctx,
11606b384f39SPeter Avalos zip->buf, remainder, zip->buf, remainder);
11616b384f39SPeter Avalos } else if (zip->cctx_valid) {
11626b384f39SPeter Avalos size_t outl = remainder;
11636b384f39SPeter Avalos ret = archive_encrypto_aes_ctr_update(
11646b384f39SPeter Avalos &zip->cctx, zip->buf, remainder,
11656b384f39SPeter Avalos zip->buf, &outl);
11666b384f39SPeter Avalos if (ret < 0) {
11676b384f39SPeter Avalos archive_set_error(&a->archive,
11686b384f39SPeter Avalos ARCHIVE_ERRNO_MISC,
11696b384f39SPeter Avalos "Failed to encrypt file");
11706b384f39SPeter Avalos return (ARCHIVE_FAILED);
11716b384f39SPeter Avalos }
11726b384f39SPeter Avalos archive_hmac_sha1_update(&zip->hctx,
11736b384f39SPeter Avalos zip->buf, remainder);
11746b384f39SPeter Avalos }
11756b384f39SPeter Avalos ret = __archive_write_output(a, zip->buf, remainder);
11769c82a63eSPeter Avalos if (ret != ARCHIVE_OK)
11779c82a63eSPeter Avalos return (ret);
11786b384f39SPeter Avalos zip->entry_compressed_written += remainder;
11796b384f39SPeter Avalos zip->written_bytes += remainder;
11809c82a63eSPeter Avalos zip->stream.next_out = zip->buf;
11819c82a63eSPeter Avalos if (zip->stream.avail_out != 0)
11829c82a63eSPeter Avalos break;
1183d4d8193eSPeter Avalos zip->stream.avail_out = (uInt)zip->len_buf;
11849c82a63eSPeter Avalos }
11859c82a63eSPeter Avalos deflateEnd(&zip->stream);
11866b384f39SPeter Avalos }
11879c82a63eSPeter Avalos #endif
11886b384f39SPeter Avalos if (zip->hctx_valid) {
11896b384f39SPeter Avalos uint8_t hmac[20];
11906b384f39SPeter Avalos size_t hmac_len = 20;
11916b384f39SPeter Avalos
11926b384f39SPeter Avalos archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
11936b384f39SPeter Avalos ret = __archive_write_output(a, hmac, AUTH_CODE_SIZE);
11946b384f39SPeter Avalos if (ret != ARCHIVE_OK)
11956b384f39SPeter Avalos return (ret);
11966b384f39SPeter Avalos zip->entry_compressed_written += AUTH_CODE_SIZE;
11976b384f39SPeter Avalos zip->written_bytes += AUTH_CODE_SIZE;
11989c82a63eSPeter Avalos }
11999c82a63eSPeter Avalos
12006b384f39SPeter Avalos /* Write trailing data descriptor. */
12016b384f39SPeter Avalos if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
12026b384f39SPeter Avalos char d[24];
12036b384f39SPeter Avalos memcpy(d, "PK\007\010", 4);
12046b384f39SPeter Avalos if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
12056b384f39SPeter Avalos archive_le32enc(d + 4, 0);/* no CRC.*/
12066b384f39SPeter Avalos else
12076b384f39SPeter Avalos archive_le32enc(d + 4, zip->entry_crc32);
12086b384f39SPeter Avalos if (zip->entry_uses_zip64) {
12096b384f39SPeter Avalos archive_le64enc(d + 8,
12106b384f39SPeter Avalos (uint64_t)zip->entry_compressed_written);
12116b384f39SPeter Avalos archive_le64enc(d + 16,
12126b384f39SPeter Avalos (uint64_t)zip->entry_uncompressed_written);
12136b384f39SPeter Avalos ret = __archive_write_output(a, d, 24);
12146b384f39SPeter Avalos zip->written_bytes += 24;
12156b384f39SPeter Avalos } else {
12166b384f39SPeter Avalos archive_le32enc(d + 8,
12176b384f39SPeter Avalos (uint32_t)zip->entry_compressed_written);
12186b384f39SPeter Avalos archive_le32enc(d + 12,
12196b384f39SPeter Avalos (uint32_t)zip->entry_uncompressed_written);
12206b384f39SPeter Avalos ret = __archive_write_output(a, d, 16);
12216b384f39SPeter Avalos zip->written_bytes += 16;
12226b384f39SPeter Avalos }
12239c82a63eSPeter Avalos if (ret != ARCHIVE_OK)
12249c82a63eSPeter Avalos return (ARCHIVE_FATAL);
12256b384f39SPeter Avalos }
12266b384f39SPeter Avalos
12276b384f39SPeter Avalos /* Append Zip64 extra data to central directory information. */
12286b384f39SPeter Avalos if (zip->entry_compressed_written > ZIP_4GB_MAX
12296b384f39SPeter Avalos || zip->entry_uncompressed_written > ZIP_4GB_MAX
12306b384f39SPeter Avalos || zip->entry_offset > ZIP_4GB_MAX) {
12316b384f39SPeter Avalos unsigned char zip64[32];
12326b384f39SPeter Avalos unsigned char *z = zip64, *zd;
12336b384f39SPeter Avalos memcpy(z, "\001\000\000\000", 4);
12346b384f39SPeter Avalos z += 4;
12356b384f39SPeter Avalos if (zip->entry_uncompressed_written >= ZIP_4GB_MAX) {
12366b384f39SPeter Avalos archive_le64enc(z, zip->entry_uncompressed_written);
12376b384f39SPeter Avalos z += 8;
12386b384f39SPeter Avalos }
12396b384f39SPeter Avalos if (zip->entry_compressed_written >= ZIP_4GB_MAX) {
12406b384f39SPeter Avalos archive_le64enc(z, zip->entry_compressed_written);
12416b384f39SPeter Avalos z += 8;
12426b384f39SPeter Avalos }
12436b384f39SPeter Avalos if (zip->entry_offset >= ZIP_4GB_MAX) {
12446b384f39SPeter Avalos archive_le64enc(z, zip->entry_offset);
12456b384f39SPeter Avalos z += 8;
12466b384f39SPeter Avalos }
12476b384f39SPeter Avalos archive_le16enc(zip64 + 2, (uint16_t)(z - (zip64 + 4)));
12486b384f39SPeter Avalos zd = cd_alloc(zip, z - zip64);
12496b384f39SPeter Avalos if (zd == NULL) {
12506b384f39SPeter Avalos archive_set_error(&a->archive, ENOMEM,
12516b384f39SPeter Avalos "Can't allocate zip data");
12526b384f39SPeter Avalos return (ARCHIVE_FATAL);
12536b384f39SPeter Avalos }
12546b384f39SPeter Avalos memcpy(zd, zip64, z - zip64);
12556b384f39SPeter Avalos /* Zip64 means version needs to be set to at least 4.5 */
12566b384f39SPeter Avalos if (archive_le16dec(zip->file_header + 6) < 45)
12576b384f39SPeter Avalos archive_le16enc(zip->file_header + 6, 45);
12586b384f39SPeter Avalos }
12596b384f39SPeter Avalos
12606b384f39SPeter Avalos /* Fix up central directory file header. */
12616b384f39SPeter Avalos if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
12626b384f39SPeter Avalos archive_le32enc(zip->file_header + 16, 0);/* no CRC.*/
12636b384f39SPeter Avalos else
12646b384f39SPeter Avalos archive_le32enc(zip->file_header + 16, zip->entry_crc32);
12656b384f39SPeter Avalos archive_le32enc(zip->file_header + 20,
12666b384f39SPeter Avalos (uint32_t)zipmin(zip->entry_compressed_written,
12676b384f39SPeter Avalos ZIP_4GB_MAX));
12686b384f39SPeter Avalos archive_le32enc(zip->file_header + 24,
12696b384f39SPeter Avalos (uint32_t)zipmin(zip->entry_uncompressed_written,
12706b384f39SPeter Avalos ZIP_4GB_MAX));
12716b384f39SPeter Avalos archive_le16enc(zip->file_header + 30,
12726b384f39SPeter Avalos (uint16_t)(zip->central_directory_bytes - zip->file_header_extra_offset));
12736b384f39SPeter Avalos archive_le32enc(zip->file_header + 42,
12746b384f39SPeter Avalos (uint32_t)zipmin(zip->entry_offset,
12756b384f39SPeter Avalos ZIP_4GB_MAX));
12766b384f39SPeter Avalos
12779c82a63eSPeter Avalos return (ARCHIVE_OK);
12789c82a63eSPeter Avalos }
12799c82a63eSPeter Avalos
12809c82a63eSPeter Avalos static int
archive_write_zip_close(struct archive_write * a)1281c09f92d2SPeter Avalos archive_write_zip_close(struct archive_write *a)
12829c82a63eSPeter Avalos {
12836b384f39SPeter Avalos uint8_t buff[64];
1284c09f92d2SPeter Avalos int64_t offset_start, offset_end;
12856b384f39SPeter Avalos struct zip *zip = a->format_data;
12866b384f39SPeter Avalos struct cd_segment *segment;
12879c82a63eSPeter Avalos int ret;
12889c82a63eSPeter Avalos
12899c82a63eSPeter Avalos offset_start = zip->written_bytes;
12906b384f39SPeter Avalos segment = zip->central_directory;
12916b384f39SPeter Avalos while (segment != NULL) {
12926b384f39SPeter Avalos ret = __archive_write_output(a,
12936b384f39SPeter Avalos segment->buff, segment->p - segment->buff);
12949c82a63eSPeter Avalos if (ret != ARCHIVE_OK)
12959c82a63eSPeter Avalos return (ARCHIVE_FATAL);
12966b384f39SPeter Avalos zip->written_bytes += segment->p - segment->buff;
12976b384f39SPeter Avalos segment = segment->next;
12989c82a63eSPeter Avalos }
12999c82a63eSPeter Avalos offset_end = zip->written_bytes;
13009c82a63eSPeter Avalos
13016b384f39SPeter Avalos /* If central dir info is too large, write Zip64 end-of-cd */
13026b384f39SPeter Avalos if (offset_end - offset_start > ZIP_4GB_MAX
13036b384f39SPeter Avalos || offset_start > ZIP_4GB_MAX
13046b384f39SPeter Avalos || zip->central_directory_entries > 0xffffUL
13056b384f39SPeter Avalos || (zip->flags & ZIP_FLAG_FORCE_ZIP64)) {
13066b384f39SPeter Avalos /* Zip64 end-of-cd record */
13076b384f39SPeter Avalos memset(buff, 0, 56);
13086b384f39SPeter Avalos memcpy(buff, "PK\006\006", 4);
13096b384f39SPeter Avalos archive_le64enc(buff + 4, 44);
13106b384f39SPeter Avalos archive_le16enc(buff + 12, 45);
13116b384f39SPeter Avalos archive_le16enc(buff + 14, 45);
13126b384f39SPeter Avalos /* This is disk 0 of 0. */
13136b384f39SPeter Avalos archive_le64enc(buff + 24, zip->central_directory_entries);
13146b384f39SPeter Avalos archive_le64enc(buff + 32, zip->central_directory_entries);
13156b384f39SPeter Avalos archive_le64enc(buff + 40, offset_end - offset_start);
13166b384f39SPeter Avalos archive_le64enc(buff + 48, offset_start);
13176b384f39SPeter Avalos ret = __archive_write_output(a, buff, 56);
13189c82a63eSPeter Avalos if (ret != ARCHIVE_OK)
13199c82a63eSPeter Avalos return (ARCHIVE_FATAL);
13206b384f39SPeter Avalos zip->written_bytes += 56;
13216b384f39SPeter Avalos
13226b384f39SPeter Avalos /* Zip64 end-of-cd locator record. */
13236b384f39SPeter Avalos memset(buff, 0, 20);
13246b384f39SPeter Avalos memcpy(buff, "PK\006\007", 4);
13256b384f39SPeter Avalos archive_le32enc(buff + 4, 0);
13266b384f39SPeter Avalos archive_le64enc(buff + 8, offset_end);
13276b384f39SPeter Avalos archive_le32enc(buff + 16, 1);
13286b384f39SPeter Avalos ret = __archive_write_output(a, buff, 20);
13296b384f39SPeter Avalos if (ret != ARCHIVE_OK)
13306b384f39SPeter Avalos return (ARCHIVE_FATAL);
13316b384f39SPeter Avalos zip->written_bytes += 20;
13326b384f39SPeter Avalos
13336b384f39SPeter Avalos }
13346b384f39SPeter Avalos
13356b384f39SPeter Avalos /* Format and write end of central directory. */
13366b384f39SPeter Avalos memset(buff, 0, sizeof(buff));
13376b384f39SPeter Avalos memcpy(buff, "PK\005\006", 4);
13386b384f39SPeter Avalos archive_le16enc(buff + 8, (uint16_t)zipmin(0xffffU,
13396b384f39SPeter Avalos zip->central_directory_entries));
13406b384f39SPeter Avalos archive_le16enc(buff + 10, (uint16_t)zipmin(0xffffU,
13416b384f39SPeter Avalos zip->central_directory_entries));
13426b384f39SPeter Avalos archive_le32enc(buff + 12,
13436b384f39SPeter Avalos (uint32_t)zipmin(ZIP_4GB_MAX, (offset_end - offset_start)));
13446b384f39SPeter Avalos archive_le32enc(buff + 16,
13456b384f39SPeter Avalos (uint32_t)zipmin(ZIP_4GB_MAX, offset_start));
13466b384f39SPeter Avalos ret = __archive_write_output(a, buff, 22);
13476b384f39SPeter Avalos if (ret != ARCHIVE_OK)
13486b384f39SPeter Avalos return (ARCHIVE_FATAL);
13496b384f39SPeter Avalos zip->written_bytes += 22;
13509c82a63eSPeter Avalos return (ARCHIVE_OK);
13519c82a63eSPeter Avalos }
13529c82a63eSPeter Avalos
13539c82a63eSPeter Avalos static int
archive_write_zip_free(struct archive_write * a)1354c09f92d2SPeter Avalos archive_write_zip_free(struct archive_write *a)
13559c82a63eSPeter Avalos {
13569c82a63eSPeter Avalos struct zip *zip;
13576b384f39SPeter Avalos struct cd_segment *segment;
13589c82a63eSPeter Avalos
13599c82a63eSPeter Avalos zip = a->format_data;
13609c82a63eSPeter Avalos while (zip->central_directory != NULL) {
13616b384f39SPeter Avalos segment = zip->central_directory;
13626b384f39SPeter Avalos zip->central_directory = segment->next;
13636b384f39SPeter Avalos free(segment->buff);
13646b384f39SPeter Avalos free(segment);
13659c82a63eSPeter Avalos }
13669c82a63eSPeter Avalos free(zip->buf);
13676b384f39SPeter Avalos archive_entry_free(zip->entry);
13686b384f39SPeter Avalos if (zip->cctx_valid)
13696b384f39SPeter Avalos archive_encrypto_aes_ctr_release(&zip->cctx);
13706b384f39SPeter Avalos if (zip->hctx_valid)
13716b384f39SPeter Avalos archive_hmac_sha1_cleanup(&zip->hctx);
13726b384f39SPeter Avalos /* TODO: Free opt_sconv, sconv_default */
13736b384f39SPeter Avalos
13749c82a63eSPeter Avalos free(zip);
13759c82a63eSPeter Avalos a->format_data = NULL;
13769c82a63eSPeter Avalos return (ARCHIVE_OK);
13779c82a63eSPeter Avalos }
13789c82a63eSPeter Avalos
13799c82a63eSPeter Avalos /* Convert into MSDOS-style date/time. */
13809c82a63eSPeter Avalos static unsigned int
dos_time(const time_t unix_time)13819c82a63eSPeter Avalos dos_time(const time_t unix_time)
13829c82a63eSPeter Avalos {
13839c82a63eSPeter Avalos struct tm *t;
13849c82a63eSPeter Avalos unsigned int dt;
1385085658deSDaniel Fojt #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
1386085658deSDaniel Fojt struct tm tmbuf;
1387085658deSDaniel Fojt #endif
1388085658deSDaniel Fojt #if defined(HAVE__LOCALTIME64_S)
1389085658deSDaniel Fojt errno_t terr;
1390085658deSDaniel Fojt __time64_t tmptime;
1391085658deSDaniel Fojt #endif
13929c82a63eSPeter Avalos
13939c82a63eSPeter Avalos /* This will not preserve time when creating/extracting the archive
13949c82a63eSPeter Avalos * on two systems with different time zones. */
1395085658deSDaniel Fojt #if defined(HAVE_LOCALTIME_R)
1396085658deSDaniel Fojt t = localtime_r(&unix_time, &tmbuf);
1397085658deSDaniel Fojt #elif defined(HAVE__LOCALTIME64_S)
1398085658deSDaniel Fojt tmptime = unix_time;
1399085658deSDaniel Fojt terr = _localtime64_s(&tmbuf, &tmptime);
1400085658deSDaniel Fojt if (terr)
1401085658deSDaniel Fojt t = NULL;
1402085658deSDaniel Fojt else
1403085658deSDaniel Fojt t = &tmbuf;
1404085658deSDaniel Fojt #else
14059c82a63eSPeter Avalos t = localtime(&unix_time);
1406085658deSDaniel Fojt #endif
14079c82a63eSPeter Avalos
1408c09f92d2SPeter Avalos /* MSDOS-style date/time is only between 1980-01-01 and 2107-12-31 */
1409c09f92d2SPeter Avalos if (t->tm_year < 1980 - 1900)
1410c09f92d2SPeter Avalos /* Set minimum date/time '1980-01-01 00:00:00'. */
1411c09f92d2SPeter Avalos dt = 0x00210000U;
1412c09f92d2SPeter Avalos else if (t->tm_year > 2107 - 1900)
1413c09f92d2SPeter Avalos /* Set maximum date/time '2107-12-31 23:59:58'. */
1414c09f92d2SPeter Avalos dt = 0xff9fbf7dU;
1415c09f92d2SPeter Avalos else {
14169c82a63eSPeter Avalos dt = 0;
14179c82a63eSPeter Avalos dt += ((t->tm_year - 80) & 0x7f) << 9;
14189c82a63eSPeter Avalos dt += ((t->tm_mon + 1) & 0x0f) << 5;
14199c82a63eSPeter Avalos dt += (t->tm_mday & 0x1f);
14209c82a63eSPeter Avalos dt <<= 16;
14219c82a63eSPeter Avalos dt += (t->tm_hour & 0x1f) << 11;
14229c82a63eSPeter Avalos dt += (t->tm_min & 0x3f) << 5;
14239c82a63eSPeter Avalos dt += (t->tm_sec & 0x3e) >> 1; /* Only counting every 2 seconds. */
1424c09f92d2SPeter Avalos }
14259c82a63eSPeter Avalos return dt;
14269c82a63eSPeter Avalos }
14279c82a63eSPeter Avalos
14289c82a63eSPeter Avalos static size_t
path_length(struct archive_entry * entry)14299c82a63eSPeter Avalos path_length(struct archive_entry *entry)
14309c82a63eSPeter Avalos {
14319c82a63eSPeter Avalos mode_t type;
14329c82a63eSPeter Avalos const char *path;
1433085658deSDaniel Fojt size_t len;
14349c82a63eSPeter Avalos
14359c82a63eSPeter Avalos type = archive_entry_filetype(entry);
14369c82a63eSPeter Avalos path = archive_entry_pathname(entry);
14379c82a63eSPeter Avalos
1438d4d8193eSPeter Avalos if (path == NULL)
1439d4d8193eSPeter Avalos return (0);
1440085658deSDaniel Fojt len = strlen(path);
1441085658deSDaniel Fojt if (type == AE_IFDIR && (path[0] == '\0' || path[len - 1] != '/'))
1442085658deSDaniel Fojt ++len; /* Space for the trailing / */
1443085658deSDaniel Fojt return len;
14449c82a63eSPeter Avalos }
14459c82a63eSPeter Avalos
14469c82a63eSPeter Avalos static int
write_path(struct archive_entry * entry,struct archive_write * archive)14479c82a63eSPeter Avalos write_path(struct archive_entry *entry, struct archive_write *archive)
14489c82a63eSPeter Avalos {
14499c82a63eSPeter Avalos int ret;
14509c82a63eSPeter Avalos const char *path;
14519c82a63eSPeter Avalos mode_t type;
14529c82a63eSPeter Avalos size_t written_bytes;
14539c82a63eSPeter Avalos
14549c82a63eSPeter Avalos path = archive_entry_pathname(entry);
14559c82a63eSPeter Avalos type = archive_entry_filetype(entry);
14569c82a63eSPeter Avalos written_bytes = 0;
14579c82a63eSPeter Avalos
1458085658deSDaniel Fojt if (path == NULL)
1459085658deSDaniel Fojt return (ARCHIVE_FATAL);
1460085658deSDaniel Fojt
1461c09f92d2SPeter Avalos ret = __archive_write_output(archive, path, strlen(path));
14629c82a63eSPeter Avalos if (ret != ARCHIVE_OK)
14639c82a63eSPeter Avalos return (ARCHIVE_FATAL);
14649c82a63eSPeter Avalos written_bytes += strlen(path);
14659c82a63eSPeter Avalos
14666b384f39SPeter Avalos /* Folders are recognized by a trailing slash. */
14679c82a63eSPeter Avalos if ((type == AE_IFDIR) & (path[strlen(path) - 1] != '/')) {
1468c09f92d2SPeter Avalos ret = __archive_write_output(archive, "/", 1);
14699c82a63eSPeter Avalos if (ret != ARCHIVE_OK)
14709c82a63eSPeter Avalos return (ARCHIVE_FATAL);
14719c82a63eSPeter Avalos written_bytes += 1;
14729c82a63eSPeter Avalos }
14739c82a63eSPeter Avalos
14749c82a63eSPeter Avalos return ((int)written_bytes);
14759c82a63eSPeter Avalos }
14766b384f39SPeter Avalos
14776b384f39SPeter Avalos static void
copy_path(struct archive_entry * entry,unsigned char * p)14786b384f39SPeter Avalos copy_path(struct archive_entry *entry, unsigned char *p)
14796b384f39SPeter Avalos {
14806b384f39SPeter Avalos const char *path;
14816b384f39SPeter Avalos size_t pathlen;
14826b384f39SPeter Avalos mode_t type;
14836b384f39SPeter Avalos
14846b384f39SPeter Avalos path = archive_entry_pathname(entry);
14856b384f39SPeter Avalos pathlen = strlen(path);
14866b384f39SPeter Avalos type = archive_entry_filetype(entry);
14876b384f39SPeter Avalos
14886b384f39SPeter Avalos memcpy(p, path, pathlen);
14896b384f39SPeter Avalos
14906b384f39SPeter Avalos /* Folders are recognized by a trailing slash. */
1491085658deSDaniel Fojt if ((type == AE_IFDIR) && (path[pathlen - 1] != '/'))
14926b384f39SPeter Avalos p[pathlen] = '/';
14936b384f39SPeter Avalos }
14946b384f39SPeter Avalos
14956b384f39SPeter Avalos
14966b384f39SPeter Avalos static struct archive_string_conv *
get_sconv(struct archive_write * a,struct zip * zip)14976b384f39SPeter Avalos get_sconv(struct archive_write *a, struct zip *zip)
14986b384f39SPeter Avalos {
14996b384f39SPeter Avalos if (zip->opt_sconv != NULL)
15006b384f39SPeter Avalos return (zip->opt_sconv);
15016b384f39SPeter Avalos
15026b384f39SPeter Avalos if (!zip->init_default_conversion) {
15036b384f39SPeter Avalos zip->sconv_default =
15046b384f39SPeter Avalos archive_string_default_conversion_for_write(&(a->archive));
15056b384f39SPeter Avalos zip->init_default_conversion = 1;
15066b384f39SPeter Avalos }
15076b384f39SPeter Avalos return (zip->sconv_default);
15086b384f39SPeter Avalos }
15096b384f39SPeter Avalos
15106b384f39SPeter Avalos /*
15116b384f39SPeter Avalos Traditional PKWARE Decryption functions.
15126b384f39SPeter Avalos */
15136b384f39SPeter Avalos
15146b384f39SPeter Avalos static void
trad_enc_update_keys(struct trad_enc_ctx * ctx,uint8_t c)15156b384f39SPeter Avalos trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
15166b384f39SPeter Avalos {
15176b384f39SPeter Avalos uint8_t t;
15186b384f39SPeter Avalos #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
15196b384f39SPeter Avalos
15206b384f39SPeter Avalos ctx->keys[0] = CRC32(ctx->keys[0], c);
15216b384f39SPeter Avalos ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
15226b384f39SPeter Avalos t = (ctx->keys[1] >> 24) & 0xff;
15236b384f39SPeter Avalos ctx->keys[2] = CRC32(ctx->keys[2], t);
15246b384f39SPeter Avalos #undef CRC32
15256b384f39SPeter Avalos }
15266b384f39SPeter Avalos
15276b384f39SPeter Avalos static uint8_t
trad_enc_decrypt_byte(struct trad_enc_ctx * ctx)1528e95abc47Szrj trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
15296b384f39SPeter Avalos {
15306b384f39SPeter Avalos unsigned temp = ctx->keys[2] | 2;
15316b384f39SPeter Avalos return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
15326b384f39SPeter Avalos }
15336b384f39SPeter Avalos
15346b384f39SPeter Avalos static unsigned
trad_enc_encrypt_update(struct trad_enc_ctx * ctx,const uint8_t * in,size_t in_len,uint8_t * out,size_t out_len)15356b384f39SPeter Avalos trad_enc_encrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
15366b384f39SPeter Avalos size_t in_len, uint8_t *out, size_t out_len)
15376b384f39SPeter Avalos {
15386b384f39SPeter Avalos unsigned i, max;
15396b384f39SPeter Avalos
15406b384f39SPeter Avalos max = (unsigned)((in_len < out_len)? in_len: out_len);
15416b384f39SPeter Avalos
15426b384f39SPeter Avalos for (i = 0; i < max; i++) {
15436b384f39SPeter Avalos uint8_t t = in[i];
1544e95abc47Szrj out[i] = t ^ trad_enc_decrypt_byte(ctx);
15456b384f39SPeter Avalos trad_enc_update_keys(ctx, t);
15466b384f39SPeter Avalos }
15476b384f39SPeter Avalos return i;
15486b384f39SPeter Avalos }
15496b384f39SPeter Avalos
15506b384f39SPeter Avalos static int
trad_enc_init(struct trad_enc_ctx * ctx,const char * pw,size_t pw_len)15516b384f39SPeter Avalos trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len)
15526b384f39SPeter Avalos {
15536b384f39SPeter Avalos
15546b384f39SPeter Avalos ctx->keys[0] = 305419896L;
15556b384f39SPeter Avalos ctx->keys[1] = 591751049L;
15566b384f39SPeter Avalos ctx->keys[2] = 878082192L;
15576b384f39SPeter Avalos
15586b384f39SPeter Avalos for (;pw_len; --pw_len)
15596b384f39SPeter Avalos trad_enc_update_keys(ctx, *pw++);
15606b384f39SPeter Avalos return 0;
15616b384f39SPeter Avalos }
15626b384f39SPeter Avalos
15636b384f39SPeter Avalos static int
is_traditional_pkware_encryption_supported(void)15646b384f39SPeter Avalos is_traditional_pkware_encryption_supported(void)
15656b384f39SPeter Avalos {
15666b384f39SPeter Avalos uint8_t key[TRAD_HEADER_SIZE];
15676b384f39SPeter Avalos
15686b384f39SPeter Avalos if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK)
15696b384f39SPeter Avalos return (0);
15706b384f39SPeter Avalos return (1);
15716b384f39SPeter Avalos }
15726b384f39SPeter Avalos
15736b384f39SPeter Avalos static int
init_traditional_pkware_encryption(struct archive_write * a)15746b384f39SPeter Avalos init_traditional_pkware_encryption(struct archive_write *a)
15756b384f39SPeter Avalos {
15766b384f39SPeter Avalos struct zip *zip = a->format_data;
15776b384f39SPeter Avalos const char *passphrase;
15786b384f39SPeter Avalos uint8_t key[TRAD_HEADER_SIZE];
15796b384f39SPeter Avalos uint8_t key_encrypted[TRAD_HEADER_SIZE];
15806b384f39SPeter Avalos int ret;
15816b384f39SPeter Avalos
15826b384f39SPeter Avalos passphrase = __archive_write_get_passphrase(a);
15836b384f39SPeter Avalos if (passphrase == NULL) {
15846b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
15856b384f39SPeter Avalos "Encryption needs passphrase");
15866b384f39SPeter Avalos return ARCHIVE_FAILED;
15876b384f39SPeter Avalos }
15886b384f39SPeter Avalos if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK) {
15896b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
15906b384f39SPeter Avalos "Can't generate random number for encryption");
15916b384f39SPeter Avalos return ARCHIVE_FATAL;
15926b384f39SPeter Avalos }
15936b384f39SPeter Avalos trad_enc_init(&zip->tctx, passphrase, strlen(passphrase));
15946b384f39SPeter Avalos /* Set the last key code which will be used as a check code
15956b384f39SPeter Avalos * for verifying passphrase in decryption. */
15966b384f39SPeter Avalos key[TRAD_HEADER_SIZE-1] = zip->trad_chkdat;
15976b384f39SPeter Avalos trad_enc_encrypt_update(&zip->tctx, key, TRAD_HEADER_SIZE,
15986b384f39SPeter Avalos key_encrypted, TRAD_HEADER_SIZE);
15996b384f39SPeter Avalos /* Write encrypted keys in the top of the file content. */
16006b384f39SPeter Avalos ret = __archive_write_output(a, key_encrypted, TRAD_HEADER_SIZE);
16016b384f39SPeter Avalos if (ret != ARCHIVE_OK)
16026b384f39SPeter Avalos return (ret);
16036b384f39SPeter Avalos zip->written_bytes += TRAD_HEADER_SIZE;
16046b384f39SPeter Avalos zip->entry_compressed_written += TRAD_HEADER_SIZE;
16056b384f39SPeter Avalos return (ret);
16066b384f39SPeter Avalos }
16076b384f39SPeter Avalos
16086b384f39SPeter Avalos static int
init_winzip_aes_encryption(struct archive_write * a)16096b384f39SPeter Avalos init_winzip_aes_encryption(struct archive_write *a)
16106b384f39SPeter Avalos {
16116b384f39SPeter Avalos struct zip *zip = a->format_data;
16126b384f39SPeter Avalos const char *passphrase;
16136b384f39SPeter Avalos size_t key_len, salt_len;
16146b384f39SPeter Avalos uint8_t salt[16 + 2];
16156b384f39SPeter Avalos uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
16166b384f39SPeter Avalos int ret;
16176b384f39SPeter Avalos
16186b384f39SPeter Avalos passphrase = __archive_write_get_passphrase(a);
16196b384f39SPeter Avalos if (passphrase == NULL) {
16206b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
16216b384f39SPeter Avalos "Encryption needs passphrase");
16226b384f39SPeter Avalos return (ARCHIVE_FAILED);
16236b384f39SPeter Avalos }
16246b384f39SPeter Avalos if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128) {
16256b384f39SPeter Avalos salt_len = 8;
16266b384f39SPeter Avalos key_len = 16;
16276b384f39SPeter Avalos } else {
16286b384f39SPeter Avalos /* AES 256 */
16296b384f39SPeter Avalos salt_len = 16;
16306b384f39SPeter Avalos key_len = 32;
16316b384f39SPeter Avalos }
16326b384f39SPeter Avalos if (archive_random(salt, salt_len) != ARCHIVE_OK) {
16336b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
16346b384f39SPeter Avalos "Can't generate random number for encryption");
16356b384f39SPeter Avalos return (ARCHIVE_FATAL);
16366b384f39SPeter Avalos }
16376b384f39SPeter Avalos archive_pbkdf2_sha1(passphrase, strlen(passphrase),
16386b384f39SPeter Avalos salt, salt_len, 1000, derived_key, key_len * 2 + 2);
16396b384f39SPeter Avalos
16406b384f39SPeter Avalos ret = archive_encrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
16416b384f39SPeter Avalos if (ret != 0) {
16426b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
16436b384f39SPeter Avalos "Decryption is unsupported due to lack of crypto library");
16446b384f39SPeter Avalos return (ARCHIVE_FAILED);
16456b384f39SPeter Avalos }
16466b384f39SPeter Avalos ret = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len,
16476b384f39SPeter Avalos key_len);
16486b384f39SPeter Avalos if (ret != 0) {
16496b384f39SPeter Avalos archive_encrypto_aes_ctr_release(&zip->cctx);
16506b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
16516b384f39SPeter Avalos "Failed to initialize HMAC-SHA1");
16526b384f39SPeter Avalos return (ARCHIVE_FAILED);
16536b384f39SPeter Avalos }
16546b384f39SPeter Avalos
1655e95abc47Szrj /* Set a password verification value after the 'salt'. */
16566b384f39SPeter Avalos salt[salt_len] = derived_key[key_len * 2];
16576b384f39SPeter Avalos salt[salt_len + 1] = derived_key[key_len * 2 + 1];
16586b384f39SPeter Avalos
16596b384f39SPeter Avalos /* Write encrypted keys in the top of the file content. */
16606b384f39SPeter Avalos ret = __archive_write_output(a, salt, salt_len + 2);
16616b384f39SPeter Avalos if (ret != ARCHIVE_OK)
16626b384f39SPeter Avalos return (ret);
16636b384f39SPeter Avalos zip->written_bytes += salt_len + 2;
16646b384f39SPeter Avalos zip->entry_compressed_written += salt_len + 2;
16656b384f39SPeter Avalos
16666b384f39SPeter Avalos return (ARCHIVE_OK);
16676b384f39SPeter Avalos }
16686b384f39SPeter Avalos
16696b384f39SPeter Avalos static int
is_winzip_aes_encryption_supported(int encryption)16706b384f39SPeter Avalos is_winzip_aes_encryption_supported(int encryption)
16716b384f39SPeter Avalos {
16726b384f39SPeter Avalos size_t key_len, salt_len;
16736b384f39SPeter Avalos uint8_t salt[16 + 2];
16746b384f39SPeter Avalos uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
16756b384f39SPeter Avalos archive_crypto_ctx cctx;
16766b384f39SPeter Avalos archive_hmac_sha1_ctx hctx;
16776b384f39SPeter Avalos int ret;
16786b384f39SPeter Avalos
16796b384f39SPeter Avalos if (encryption == ENCRYPTION_WINZIP_AES128) {
16806b384f39SPeter Avalos salt_len = 8;
16816b384f39SPeter Avalos key_len = 16;
16826b384f39SPeter Avalos } else {
16836b384f39SPeter Avalos /* AES 256 */
16846b384f39SPeter Avalos salt_len = 16;
16856b384f39SPeter Avalos key_len = 32;
16866b384f39SPeter Avalos }
16876b384f39SPeter Avalos if (archive_random(salt, salt_len) != ARCHIVE_OK)
16886b384f39SPeter Avalos return (0);
16896b384f39SPeter Avalos ret = archive_pbkdf2_sha1("p", 1, salt, salt_len, 1000,
16906b384f39SPeter Avalos derived_key, key_len * 2 + 2);
16916b384f39SPeter Avalos if (ret != 0)
16926b384f39SPeter Avalos return (0);
16936b384f39SPeter Avalos
16946b384f39SPeter Avalos ret = archive_encrypto_aes_ctr_init(&cctx, derived_key, key_len);
16956b384f39SPeter Avalos if (ret != 0)
16966b384f39SPeter Avalos return (0);
16976b384f39SPeter Avalos ret = archive_hmac_sha1_init(&hctx, derived_key + key_len,
16986b384f39SPeter Avalos key_len);
16996b384f39SPeter Avalos archive_encrypto_aes_ctr_release(&cctx);
17006b384f39SPeter Avalos if (ret != 0)
17016b384f39SPeter Avalos return (0);
17026b384f39SPeter Avalos archive_hmac_sha1_cleanup(&hctx);
17036b384f39SPeter Avalos return (1);
17046b384f39SPeter Avalos }
1705