Home | History | Annotate | Line # | Download | only in libarchive
      1 /*-
      2  * Copyright (c) 2008 Anselm Strauss
      3  * Copyright (c) 2009 Joerg Sonnenberger
      4  * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     15  *
     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.
     26  */
     27 
     28 /*
     29  * Development supported by Google Summer of Code 2008.
     30  */
     31 
     32 #include "archive_platform.h"
     33 
     34 #ifdef HAVE_ERRNO_H
     35 #include <errno.h>
     36 #endif
     37 #ifdef HAVE_LANGINFO_H
     38 #include <langinfo.h>
     39 #endif
     40 #ifdef HAVE_STDLIB_H
     41 #include <stdlib.h>
     42 #endif
     43 #ifdef HAVE_STRING_H
     44 #include <string.h>
     45 #endif
     46 #ifdef HAVE_LIMITS_H
     47 #include <limits.h>
     48 #endif
     49 #ifdef HAVE_UNISTD_H
     50 #include <unistd.h>
     51 #endif
     52 #ifdef HAVE_ZLIB_H
     53 #include <zlib.h>
     54 #endif
     55 #ifdef HAVE_LZMA_H
     56 #include <lzma.h>
     57 #endif
     58 #ifdef HAVE_BZLIB_H
     59 #include <bzlib.h>
     60 #endif
     61 #ifdef HAVE_ZSTD_H
     62 #include <zstd.h>
     63 #endif
     64 
     65 #include "archive.h"
     66 #include "archive_cryptor_private.h"
     67 #include "archive_endian.h"
     68 #include "archive_entry.h"
     69 #include "archive_entry_locale.h"
     70 #include "archive_hmac_private.h"
     71 #include "archive_private.h"
     72 #include "archive_random_private.h"
     73 #include "archive_time_private.h"
     74 #include "archive_write_private.h"
     75 #include "archive_write_set_format_private.h"
     76 
     77 #ifndef HAVE_ZLIB_H
     78 #include "archive_crc32.h"
     79 #endif
     80 
     81 #define ZIP_ENTRY_FLAG_ENCRYPTED	(1 << 0)
     82 #define ZIP_ENTRY_FLAG_LZMA_EOPM	(1 << 1)
     83 #define ZIP_ENTRY_FLAG_DEFLATE_MAX	(1 << 1) /* i.e. compression levels 8 & 9 */
     84 #define ZIP_ENTRY_FLAG_DEFLATE_FAST	(1 << 2) /* i.e. compression levels 3 & 4 */
     85 #define ZIP_ENTRY_FLAG_DEFLATE_SUPER_FAST	(1 << 1) | (1 << 2) /* i.e. compression levels 1 & 2 */
     86 #define ZIP_ENTRY_FLAG_LENGTH_AT_END	(1 << 3)
     87 #define ZIP_ENTRY_FLAG_UTF8_NAME	(1 << 11)
     88 
     89 #define ZIP_4GB_MAX ARCHIVE_LITERAL_LL(0xffffffff)
     90 #define ZIP_4GB_MAX_UNCOMPRESSED ARCHIVE_LITERAL_LL(0xff000000)
     91 
     92 enum compression {
     93 	COMPRESSION_UNSPECIFIED = -1,
     94 	COMPRESSION_STORE = 0,
     95 	COMPRESSION_DEFLATE = 8,
     96 	COMPRESSION_BZIP2 = 12,
     97 	COMPRESSION_LZMA = 14,
     98 	COMPRESSION_ZSTD = 93,
     99 	COMPRESSION_XZ = 95
    100 };
    101 
    102 #ifdef HAVE_ZLIB_H
    103 #define COMPRESSION_DEFAULT	COMPRESSION_DEFLATE
    104 #else
    105 #define COMPRESSION_DEFAULT	COMPRESSION_STORE
    106 #endif
    107 
    108 enum encryption {
    109 	ENCRYPTION_NONE	= 0,
    110 	ENCRYPTION_TRADITIONAL, /* Traditional PKWARE encryption. */
    111 	ENCRYPTION_WINZIP_AES128, /* WinZIP AES-128 encryption. */
    112 	ENCRYPTION_WINZIP_AES256, /* WinZIP AES-256 encryption. */
    113 };
    114 
    115 #define TRAD_HEADER_SIZE	12
    116 /*
    117  * See "WinZip - AES Encryption Information"
    118  *     http://www.winzip.com/aes_info.htm
    119  */
    120 /* Value used in compression method. */
    121 #define WINZIP_AES_ENCRYPTION	99
    122 /* A WinZip AES header size which is stored at the beginning of
    123  * file contents. */
    124 #define WINZIP_AES128_HEADER_SIZE	(8 + 2)
    125 #define WINZIP_AES256_HEADER_SIZE	(16 + 2)
    126 /* AES vendor version. */
    127 #define AES_VENDOR_AE_1 0x0001
    128 #define AES_VENDOR_AE_2 0x0002
    129 /* Authentication code size. */
    130 #define AUTH_CODE_SIZE		10
    131 /**/
    132 #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
    133 
    134 struct cd_segment {
    135 	struct cd_segment *next;
    136 	size_t buff_size;
    137 	unsigned char *buff;
    138 	unsigned char *p;
    139 };
    140 
    141 struct trad_enc_ctx {
    142 	uint32_t keys[3];
    143 };
    144 
    145 struct zip {
    146 	int64_t entry_offset;
    147 	int64_t entry_compressed_size;
    148 	int64_t entry_uncompressed_size;
    149 	int64_t entry_compressed_written;
    150 	int64_t entry_uncompressed_written;
    151 	int64_t entry_uncompressed_limit;
    152 	struct archive_entry *entry;
    153 	uint32_t entry_crc32;
    154 	enum compression entry_compression;
    155 	enum encryption  entry_encryption;
    156 	int entry_flags;
    157 	int experiments;
    158 	struct trad_enc_ctx tctx;
    159 	char tctx_valid;
    160 	unsigned char trad_chkdat;
    161 	unsigned aes_vendor;
    162 	archive_crypto_ctx cctx;
    163 	char cctx_valid;
    164 	archive_hmac_sha1_ctx hctx;
    165 	char hctx_valid;
    166 
    167 	unsigned char *file_header;
    168 	size_t file_header_extra_offset;
    169 	unsigned long (*crc32func)(unsigned long crc, const void *buff, size_t len);
    170 
    171 	struct cd_segment *central_directory;
    172 	struct cd_segment *central_directory_last;
    173 	size_t central_directory_bytes;
    174 	size_t central_directory_entries;
    175 
    176 	int64_t written_bytes; /* Overall position in file. */
    177 
    178 	struct archive_string_conv *opt_sconv;
    179 	struct archive_string_conv *sconv_default;
    180 	enum compression requested_compression;
    181 	short compression_level;
    182 	int init_default_conversion;
    183 	enum encryption encryption_type;
    184 	short threads;
    185 
    186 #define ZIP_FLAG_AVOID_ZIP64 1
    187 #define ZIP_FLAG_FORCE_ZIP64 2
    188 #define ZIP_FLAG_EXPERIMENT_xl 4
    189 	int flags;
    190 #if defined(HAVE_LZMA_H) || defined(HAVE_ZLIB_H) || defined(HAVE_BZLIB_H) || defined(HAVE_ZSTD_H)
    191 	union {
    192 #ifdef HAVE_LZMA_H
    193 		/* ZIP's XZ format (id 95) is easy enough: copy Deflate, mutatis
    194 		 * mutandis the library changes. ZIP's LZMA format (id 14),
    195 		 * however, is rather more involved, starting here: it being a
    196 		 * modified LZMA Alone format requires a bit more
    197 		 * book-keeping. */
    198 		struct {
    199 			char headers_to_write;
    200 			lzma_options_lzma options;
    201 			lzma_stream context;
    202 		} lzma;
    203 #endif
    204 #ifdef HAVE_ZLIB_H
    205 		z_stream deflate;
    206 #endif
    207 #ifdef HAVE_BZLIB_H
    208 		bz_stream bzip2;
    209 #endif
    210 #if defined(HAVE_ZSTD_H) && HAVE_ZSTD_compressStream
    211 		struct {
    212 			/* Libzstd's init function gives a pointer to a memory area
    213 			 * it manages rather than asking for memory to initialise. */
    214 			ZSTD_CStream* context;
    215 			ZSTD_inBuffer in;
    216 			ZSTD_outBuffer out;
    217 		} zstd;
    218 #endif
    219 	} stream;
    220 #endif
    221 	size_t len_buf;
    222 	unsigned char *buf;
    223 };
    224 
    225 /* Don't call this min or MIN, since those are already defined
    226    on lots of platforms (but not all). */
    227 #define zipmin(a, b) ((a) > (b) ? (b) : (a))
    228 
    229 static ssize_t archive_write_zip_data(struct archive_write *,
    230 		   const void *buff, size_t s);
    231 static int archive_write_zip_close(struct archive_write *);
    232 static int archive_write_zip_free(struct archive_write *);
    233 static int archive_write_zip_finish_entry(struct archive_write *);
    234 static int archive_write_zip_header(struct archive_write *,
    235 	      struct archive_entry *);
    236 static int archive_write_zip_options(struct archive_write *,
    237 	      const char *, const char *);
    238 static size_t path_length(struct archive_entry *);
    239 static int write_path(struct archive_entry *, struct archive_write *);
    240 static void copy_path(struct archive_entry *, unsigned char *);
    241 static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
    242 static int trad_enc_init(struct trad_enc_ctx *, const char *, size_t);
    243 static unsigned trad_enc_encrypt_update(struct trad_enc_ctx *, const uint8_t *,
    244     size_t, uint8_t *, size_t);
    245 static int init_traditional_pkware_encryption(struct archive_write *);
    246 static int is_traditional_pkware_encryption_supported(void);
    247 static int init_winzip_aes_encryption(struct archive_write *);
    248 static int is_winzip_aes_encryption_supported(int encryption);
    249 
    250 #ifdef HAVE_LZMA_H
    251 /* ZIP's LZMA format requires the use of a alas not exposed in LibLZMA
    252  * function to write the ZIP header. Given our internal version never
    253  * fails, no need for a non-void return type. */
    254 static void
    255 lzma_lzma_props_encode(const lzma_options_lzma* options, uint8_t* out)
    256 {
    257 	out[0] = (options->pb * 5 + options->lp) * 9 + options->lc;
    258 	archive_le32enc(out + 1, options->dict_size);
    259 }
    260 #endif
    261 
    262 #if defined(HAVE_LZMA_H) && !defined(HAVE_LZMA_STREAM_ENCODER_MT)
    263 /* Dummy mt declarations, to avoid spaghetti includes below. Defined with
    264  * macros being renamed afterwards to shadow liblzma's types in order to
    265  * avoid some compiler errors. */
    266 #define lzma_stream_encoder_mt(str, opt) dummy_mt(str, opt)
    267 #define lzma_mt dummy_options
    268 
    269 typedef struct {
    270 	void* filters;
    271 	uint32_t preset;
    272 	lzma_check check;
    273 	short threads;
    274 	char flags;
    275 	char block_size;
    276 	char timeout;
    277 } dummy_options;
    278 
    279 static inline lzma_ret
    280 dummy_mt(lzma_stream* stream, const lzma_mt* options)
    281 {
    282 	(void)stream; /* UNUSED */
    283 	(void)options; /* UNUSED */
    284 	return LZMA_PROG_ERROR;
    285 }
    286 #endif
    287 
    288 static unsigned char *
    289 cd_alloc(struct zip *zip, size_t length)
    290 {
    291 	unsigned char *p;
    292 
    293 	if (zip->central_directory == NULL
    294 	    || (zip->central_directory_last->p + length
    295 		> zip->central_directory_last->buff + zip->central_directory_last->buff_size)) {
    296 		struct cd_segment *segment = calloc(1, sizeof(*segment));
    297 		if (segment == NULL)
    298 			return NULL;
    299 		segment->buff_size = 64 * 1024;
    300 		segment->buff = malloc(segment->buff_size);
    301 		if (segment->buff == NULL) {
    302 			free(segment);
    303 			return NULL;
    304 		}
    305 		segment->p = segment->buff;
    306 
    307 		if (zip->central_directory == NULL) {
    308 			zip->central_directory
    309 			    = zip->central_directory_last
    310 			    = segment;
    311 		} else {
    312 			zip->central_directory_last->next = segment;
    313 			zip->central_directory_last = segment;
    314 		}
    315 	}
    316 
    317 	p = zip->central_directory_last->p;
    318 	zip->central_directory_last->p += length;
    319 	zip->central_directory_bytes += length;
    320 	return (p);
    321 }
    322 
    323 static unsigned long
    324 real_crc32(unsigned long crc, const void *buff, size_t len)
    325 {
    326 	return crc32(crc, buff, (unsigned int)len);
    327 }
    328 
    329 static unsigned long
    330 fake_crc32(unsigned long crc, const void *buff, size_t len)
    331 {
    332 	(void)crc; /* UNUSED */
    333 	(void)buff; /* UNUSED */
    334 	(void)len; /* UNUSED */
    335 	return 0;
    336 }
    337 
    338 static int
    339 archive_write_zip_options(struct archive_write *a, const char *key,
    340     const char *val)
    341 {
    342 	struct zip *zip = a->format_data;
    343 	int ret = ARCHIVE_FAILED;
    344 
    345 	if (strcmp(key, "compression") == 0) {
    346 		/*
    347 		 * Set compression to use on all future entries.
    348 		 * This only affects regular files.
    349 		 */
    350 		if (val == NULL || val[0] == 0) {
    351 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    352 			    "%s: compression option needs a compression name",
    353 			    a->format_name);
    354 		} else if (strcmp(val, "deflate") == 0) {
    355 #ifdef HAVE_ZLIB_H
    356 			zip->requested_compression = COMPRESSION_DEFLATE;
    357 			ret = ARCHIVE_OK;
    358 #else
    359 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    360 			    "deflate compression not supported");
    361 #endif
    362 		} else if (strcmp(val, "store") == 0) {
    363 			zip->requested_compression = COMPRESSION_STORE;
    364 			ret = ARCHIVE_OK;
    365 		} else if (strcmp(val, "bzip2") == 0) {
    366 #ifdef HAVE_BZLIB_H
    367 			zip->requested_compression = COMPRESSION_BZIP2;
    368 			ret = ARCHIVE_OK;
    369 #else
    370 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    371 			    "bzip2 compression not supported");
    372 #endif
    373 		} else if (strcmp(val, "lzma") == 0) {
    374 #ifdef HAVE_LZMA_H
    375 			zip->requested_compression = COMPRESSION_LZMA;
    376 			ret = ARCHIVE_OK;
    377 #else
    378 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    379 			    "lzma compression not supported");
    380 #endif
    381 		} else if (strcmp(val, "xz") == 0) {
    382 #ifdef HAVE_LZMA_H
    383 			zip->requested_compression = COMPRESSION_XZ;
    384 			ret = ARCHIVE_OK;
    385 #else
    386 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    387 			    "xz compression not supported");
    388 #endif
    389 		} else if (strcmp(val, "zstd") == 0) {
    390 #if defined(HAVE_ZSTD_H) && HAVE_ZSTD_compressStream
    391 			zip->requested_compression = COMPRESSION_ZSTD;
    392 			ret = ARCHIVE_OK;
    393 #else
    394 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    395 			    "zstd compression not supported");
    396 #endif
    397 		}
    398 		return (ret);
    399 	} else if (strcmp(key, "compression-level") == 0) {
    400 		char *endptr;
    401 		unsigned long v;
    402 
    403 		if (val == NULL)
    404 			return (ARCHIVE_WARN);
    405 		errno = 0;
    406 		v = strtoul(val, &endptr, 10);
    407 		if (errno != 0 || *endptr != '\0' || v > 9) {
    408 			zip->compression_level = 6; // set to default
    409 			return (ARCHIVE_WARN);
    410 		}
    411 		zip->compression_level = (short)v;
    412 
    413 		if (zip->compression_level == 0) {
    414 			zip->requested_compression = COMPRESSION_STORE;
    415 			return ARCHIVE_OK;
    416 		} else {
    417 #if defined(HAVE_ZLIB_H) || defined(HAVE_LZMA_H) || defined(HAVE_BZLIB_H) || (defined(HAVE_ZSTD_H) && HAVE_ZSTD_compressStream)
    418 			// Not forcing an already specified compression algorithm
    419 			if (zip->requested_compression == COMPRESSION_UNSPECIFIED) {
    420 #ifdef HAVE_ZLIB_H
    421 				zip->requested_compression = COMPRESSION_DEFLATE;
    422 #elif defined(HAVE_BZLIB_H)
    423 				zip->requested_compression = COMPRESSION_BZIP2;
    424 #elif defined(HAVE_LZMA_H)
    425 				// Arbitrarily choosing LZMA of the two LZMA methods
    426 				zip->requested_compression = COMPRESSION_LZMA;
    427 #else
    428 				zip->requested_compression = COMPRESSION_ZSTD;
    429 #endif
    430 			}
    431 			return ARCHIVE_OK;
    432 #else
    433 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    434 			    "compression not supported");
    435 #endif
    436 		}
    437 	} else if (strcmp(key, "threads") == 0) {
    438 		char *endptr;
    439 		unsigned long v;
    440 
    441 		if (val == NULL)
    442 			return (ARCHIVE_FAILED);
    443 		errno = 0;
    444 		v = strtoul(val, &endptr, 10);
    445 		if (errno != 0 || *endptr != '\0' || v > SHRT_MAX) {
    446 			zip->threads = 1;
    447 			archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
    448 			    "Illegal value `%s'", val);
    449 			return (ARCHIVE_FAILED);
    450 		}
    451 		zip->threads = (short)v;
    452 		if (zip->threads == 0) {
    453 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
    454 			zip->threads = lzma_cputhreads();
    455 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
    456 			zip->threads = sysconf(_SC_NPROCESSORS_ONLN);
    457 #elif !defined(__CYGWIN__) && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0601
    458 			/* Windows 7 and up */
    459 			DWORD activeProcs = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
    460 			zip->threads = activeProcs <= SHRT_MAX ? (short)activeProcs : SHRT_MAX;
    461 #else
    462 			zip->threads = 1;
    463 #endif
    464 		}
    465 		return (ARCHIVE_OK);
    466 	} else if (strcmp(key, "encryption") == 0) {
    467 		if (val == NULL) {
    468 			zip->encryption_type = ENCRYPTION_NONE;
    469 			ret = ARCHIVE_OK;
    470 		} else if (val[0] == '1' || strcmp(val, "traditional") == 0
    471 		    || strcmp(val, "zipcrypt") == 0
    472 		    || strcmp(val, "ZipCrypt") == 0) {
    473 			if (is_traditional_pkware_encryption_supported()) {
    474 				zip->encryption_type = ENCRYPTION_TRADITIONAL;
    475 				ret = ARCHIVE_OK;
    476 			} else {
    477 				archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    478 				    "encryption not supported");
    479 			}
    480 		} else if (strcmp(val, "aes128") == 0) {
    481 			if (is_winzip_aes_encryption_supported(
    482 			    ENCRYPTION_WINZIP_AES128)) {
    483 				zip->encryption_type = ENCRYPTION_WINZIP_AES128;
    484 				ret = ARCHIVE_OK;
    485 			} else {
    486 				archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    487 				    "encryption not supported");
    488 			}
    489 		} else if (strcmp(val, "aes256") == 0) {
    490 			if (is_winzip_aes_encryption_supported(
    491 			    ENCRYPTION_WINZIP_AES256)) {
    492 				zip->encryption_type = ENCRYPTION_WINZIP_AES256;
    493 				ret = ARCHIVE_OK;
    494 			} else {
    495 				archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    496 				    "encryption not supported");
    497 			}
    498 		} else {
    499 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    500 			    "%s: unknown encryption '%s'", a->format_name, val);
    501 		}
    502 		return (ret);
    503 	} else if (strcmp(key, "experimental") == 0) {
    504 		if (val == NULL || val[0] == 0) {
    505 			zip->flags &= ~ ZIP_FLAG_EXPERIMENT_xl;
    506 		} else {
    507 			zip->flags |= ZIP_FLAG_EXPERIMENT_xl;
    508 		}
    509 		return (ARCHIVE_OK);
    510 	} else if (strcmp(key, "fakecrc32") == 0) {
    511 		/*
    512 		 * FOR TESTING ONLY:  disable CRC calculation to speed up
    513 		 * certain complex tests.
    514 		 */
    515 		if (val == NULL || val[0] == 0) {
    516 			zip->crc32func = real_crc32;
    517 		} else {
    518 			zip->crc32func = fake_crc32;
    519 		}
    520 		return (ARCHIVE_OK);
    521 	} else if (strcmp(key, "hdrcharset")  == 0) {
    522 		/*
    523 		 * Set the character set used in translating filenames.
    524 		 */
    525 		if (val == NULL || val[0] == 0) {
    526 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    527 			    "%s: hdrcharset option needs a character-set name",
    528 			    a->format_name);
    529 		} else {
    530 			zip->opt_sconv = archive_string_conversion_to_charset(
    531 			    &a->archive, val, 0);
    532 			if (zip->opt_sconv != NULL)
    533 				ret = ARCHIVE_OK;
    534 			else
    535 				ret = ARCHIVE_FATAL;
    536 		}
    537 		return (ret);
    538 	} else if (strcmp(key, "zip64") == 0) {
    539 		/*
    540 		 * Bias decisions about Zip64: force them to be
    541 		 * generated in certain cases where they are not
    542 		 * forbidden or avoid them in certain cases where they
    543 		 * are not strictly required.
    544 		 */
    545 		if (val != NULL && *val != '\0') {
    546 			zip->flags |= ZIP_FLAG_FORCE_ZIP64;
    547 			zip->flags &= ~ZIP_FLAG_AVOID_ZIP64;
    548 		} else {
    549 			zip->flags &= ~ZIP_FLAG_FORCE_ZIP64;
    550 			zip->flags |= ZIP_FLAG_AVOID_ZIP64;
    551 		}
    552 		return (ARCHIVE_OK);
    553 	}
    554 
    555 	/* Note: The "warn" return is just to inform the options
    556 	 * supervisor that we didn't handle it.  It will generate
    557 	 * a suitable error if no one used this option. */
    558 	return (ARCHIVE_WARN);
    559 }
    560 
    561 int
    562 archive_write_zip_set_compression_deflate(struct archive *_a)
    563 {
    564 	struct archive_write *a = (struct archive_write *)_a;
    565 	int ret = ARCHIVE_FAILED;
    566 
    567 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
    568 		ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
    569 		"archive_write_zip_set_compression_deflate");
    570 	if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
    571 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    572 		"Can only use archive_write_zip_set_compression_deflate"
    573 		" with zip format");
    574 		ret = ARCHIVE_FATAL;
    575 	} else {
    576 #ifdef HAVE_ZLIB_H
    577 		struct zip *zip = a->format_data;
    578 		zip->requested_compression = COMPRESSION_DEFLATE;
    579 		ret = ARCHIVE_OK;
    580 #else
    581 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    582 			"deflate compression not supported");
    583 		ret = ARCHIVE_FAILED;
    584 #endif
    585 	}
    586 	return (ret);
    587 }
    588 
    589 int
    590 archive_write_zip_set_compression_bzip2(struct archive *_a)
    591 {
    592 	struct archive_write *a = (struct archive_write *)_a;
    593 	int ret = ARCHIVE_FAILED;
    594 
    595 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
    596 		ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
    597 		"archive_write_zip_set_compression_bzip2");
    598 	if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
    599 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    600 		"Can only use archive_write_zip_set_compression_bzip2"
    601 		" with zip format");
    602 		ret = ARCHIVE_FATAL;
    603 	} else {
    604 #ifdef HAVE_BZLIB_H
    605 		struct zip *zip = a->format_data;
    606 		zip->requested_compression = COMPRESSION_BZIP2;
    607 		ret = ARCHIVE_OK;
    608 #else
    609 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    610 			"bzip2 compression not supported");
    611 		ret = ARCHIVE_FAILED;
    612 #endif
    613 	}
    614 	return (ret);
    615 }
    616 
    617 int
    618 archive_write_zip_set_compression_zstd(struct archive *_a)
    619 {
    620 	struct archive_write *a = (struct archive_write *)_a;
    621 	int ret = ARCHIVE_FAILED;
    622 
    623 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
    624 		ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
    625 		"archive_write_zip_set_compression_zstd");
    626 	if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
    627 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    628 		"Can only use archive_write_zip_set_compression_zstd"
    629 		" with zip format");
    630 		ret = ARCHIVE_FATAL;
    631 	} else {
    632 #if defined(HAVE_ZSTD_H) && HAVE_ZSTD_compressStream
    633 		struct zip *zip = a->format_data;
    634 		zip->requested_compression = COMPRESSION_ZSTD;
    635 		ret = ARCHIVE_OK;
    636 #else
    637 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    638 			"zstd compression not supported");
    639 		ret = ARCHIVE_FAILED;
    640 #endif
    641 	}
    642 	return (ret);
    643 }
    644 
    645 int
    646 archive_write_zip_set_compression_lzma(struct archive *_a)
    647 {
    648 	struct archive_write *a = (struct archive_write *)_a;
    649 	int ret = ARCHIVE_FAILED;
    650 
    651 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
    652 		ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
    653 		"archive_write_zip_set_compression_lzma");
    654 	if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
    655 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    656 		"Can only use archive_write_zip_set_compression_lzma"
    657 		" with zip format");
    658 		ret = ARCHIVE_FATAL;
    659 	} else {
    660 #ifdef HAVE_LZMA_H
    661 		struct zip *zip = a->format_data;
    662 		zip->requested_compression = COMPRESSION_LZMA;
    663 		ret = ARCHIVE_OK;
    664 #else
    665 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    666 			"lzma compression not supported");
    667 		ret = ARCHIVE_FAILED;
    668 #endif
    669 	}
    670 	return (ret);
    671 }
    672 
    673 int
    674 archive_write_zip_set_compression_xz(struct archive *_a)
    675 {
    676 	struct archive_write *a = (struct archive_write *)_a;
    677 	int ret = ARCHIVE_FAILED;
    678 
    679 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
    680 		ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
    681 		"archive_write_zip_set_compression_xz");
    682 	if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
    683 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    684 		"Can only use archive_write_zip_set_compression_xz"
    685 		" with zip format");
    686 		ret = ARCHIVE_FATAL;
    687 	} else {
    688 #ifdef HAVE_LZMA_H
    689 		struct zip *zip = a->format_data;
    690 		zip->requested_compression = COMPRESSION_XZ;
    691 		ret = ARCHIVE_OK;
    692 #else
    693 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    694 			"xz compression not supported");
    695 		ret = ARCHIVE_FAILED;
    696 #endif
    697 	}
    698 	return (ret);
    699 }
    700 
    701 int
    702 archive_write_zip_set_compression_store(struct archive *_a)
    703 {
    704 	struct archive_write *a = (struct archive_write *)_a;
    705 	struct zip *zip = a->format_data;
    706 	int ret = ARCHIVE_FAILED;
    707 
    708 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
    709 		ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
    710 		"archive_write_zip_set_compression_store");
    711 	if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
    712 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    713 			"Can only use archive_write_zip_set_compression_store"
    714 			" with zip format");
    715 		ret = ARCHIVE_FATAL;
    716 	} else {
    717 		zip->requested_compression = COMPRESSION_STORE;
    718 		ret = ARCHIVE_OK;
    719 	}
    720 	return (ret);
    721 }
    722 
    723 int
    724 archive_write_set_format_zip(struct archive *_a)
    725 {
    726 	struct archive_write *a = (struct archive_write *)_a;
    727 	struct zip *zip;
    728 
    729 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
    730 	    ARCHIVE_STATE_NEW, "archive_write_set_format_zip");
    731 
    732 	/* If another format was already registered, unregister it. */
    733 	if (a->format_free != NULL)
    734 		(a->format_free)(a);
    735 
    736 	zip = calloc(1, sizeof(*zip));
    737 	if (zip == NULL) {
    738 		archive_set_error(&a->archive, ENOMEM,
    739 		    "Can't allocate zip data");
    740 		return (ARCHIVE_FATAL);
    741 	}
    742 
    743 	/* "Unspecified" lets us choose the appropriate compression. */
    744 	zip->requested_compression = COMPRESSION_UNSPECIFIED;
    745 	/* Following the 7-zip write support's lead, setting the default
    746 	 * compression level explicitly to 6 no matter what. */
    747 	zip->compression_level = 6;
    748 	/* Following the xar write support's lead, the default number of
    749 	 * threads is 1 (i.e. the xz compression, the only one caring about
    750 	 * that, not being multi-threaded even if the multi-threaded encoder
    751 	 * were available) */
    752 	zip->threads = 1;
    753 	zip->crc32func = real_crc32;
    754 
    755 	/* A buffer used for both compression and encryption. */
    756 	zip->len_buf = 65536;
    757 	zip->buf = malloc(zip->len_buf);
    758 	if (zip->buf == NULL) {
    759 		free(zip);
    760 		archive_set_error(&a->archive, ENOMEM,
    761 		    "Can't allocate compression buffer");
    762 		return (ARCHIVE_FATAL);
    763 	}
    764 
    765 	a->format_data = zip;
    766 	a->format_name = "zip";
    767 	a->format_options = archive_write_zip_options;
    768 	a->format_write_header = archive_write_zip_header;
    769 	a->format_write_data = archive_write_zip_data;
    770 	a->format_finish_entry = archive_write_zip_finish_entry;
    771 	a->format_close = archive_write_zip_close;
    772 	a->format_free = archive_write_zip_free;
    773 	a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
    774 	a->archive.archive_format_name = "ZIP";
    775 
    776 	return (ARCHIVE_OK);
    777 }
    778 
    779 static int
    780 is_all_ascii(const char *p)
    781 {
    782 	const unsigned char *pp = (const unsigned char *)p;
    783 
    784 	while (*pp) {
    785 		if (*pp++ > 127)
    786 			return (0);
    787 	}
    788 	return (1);
    789 }
    790 
    791 static int
    792 archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
    793 {
    794 	unsigned char local_header[32];
    795 	unsigned char local_extra[144];
    796 	struct zip *zip = a->format_data;
    797 	unsigned char *e;
    798 	unsigned char *cd_extra;
    799 	size_t filename_length;
    800 	const char *slink = NULL;
    801 	size_t slink_size = 0;
    802 	struct archive_string_conv *sconv = get_sconv(a, zip);
    803 	int ret, ret2 = ARCHIVE_OK;
    804 	mode_t type;
    805 	int version_needed = 10;
    806 #define MIN_VERSION_NEEDED(x) do { if (version_needed < x) { version_needed = x; } } while (0)
    807 
    808 	/* Sanity check. */
    809 	if (archive_entry_pathname(entry) == NULL
    810 #if defined(_WIN32) && !defined(__CYGWIN__)
    811 	    && archive_entry_pathname_w(entry) == NULL
    812 #endif
    813 	    ) {
    814 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    815 		    "Can't record entry in zip file without pathname");
    816 		return ARCHIVE_FAILED;
    817 	}
    818 
    819 	/* Ignore types of entries that we don't support. */
    820 	type = archive_entry_filetype(entry);
    821 	if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) {
    822 		__archive_write_entry_filetype_unsupported(
    823 		    &a->archive, entry, "zip");
    824 		return ARCHIVE_FAILED;
    825 	}
    826 
    827 	/* If we're not using Zip64, reject large files. */
    828 	if (zip->flags & ZIP_FLAG_AVOID_ZIP64) {
    829 		/* Reject entries over 4GB. */
    830 		if (archive_entry_size_is_set(entry)
    831 		    && (archive_entry_size(entry) > ZIP_4GB_MAX)) {
    832 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    833 			    "Files > 4GB require Zip64 extensions");
    834 			return ARCHIVE_FAILED;
    835 		}
    836 		/* Reject entries if archive is > 4GB. */
    837 		if (zip->written_bytes > ZIP_4GB_MAX) {
    838 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    839 			    "Archives > 4GB require Zip64 extensions");
    840 			return ARCHIVE_FAILED;
    841 		}
    842 	}
    843 
    844 	/* Only regular files can have size > 0. */
    845 	if (type != AE_IFREG)
    846 		archive_entry_set_size(entry, 0);
    847 
    848 	/* Reset information from last entry. */
    849 	zip->entry_offset = zip->written_bytes;
    850 	zip->entry_uncompressed_limit = INT64_MAX;
    851 	/* Zero size values implies that we're using a trailing data descriptor */
    852 	zip->entry_compressed_size = 0;
    853 	zip->entry_uncompressed_size = 0;
    854 	zip->entry_compressed_written = 0;
    855 	zip->entry_uncompressed_written = 0;
    856 	zip->entry_flags = 0;
    857 	zip->entry_crc32 = zip->crc32func(0, NULL, 0);
    858 	zip->entry_encryption = 0;
    859 	archive_entry_free(zip->entry);
    860 	zip->entry = NULL;
    861 
    862 	if (zip->cctx_valid)
    863 		archive_encrypto_aes_ctr_release(&zip->cctx);
    864 	if (zip->hctx_valid)
    865 		archive_hmac_sha1_cleanup(&zip->hctx);
    866 	zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
    867 
    868 	if (type == AE_IFREG
    869 		    &&(!archive_entry_size_is_set(entry)
    870 			|| archive_entry_size(entry) > 0)) {
    871 		switch (zip->encryption_type) {
    872 		case ENCRYPTION_TRADITIONAL:
    873 		case ENCRYPTION_WINZIP_AES128:
    874 		case ENCRYPTION_WINZIP_AES256:
    875 			zip->entry_flags |= ZIP_ENTRY_FLAG_ENCRYPTED;
    876 			zip->entry_encryption = zip->encryption_type;
    877 			break;
    878 		case ENCRYPTION_NONE:
    879 		default:
    880 			break;
    881 		}
    882 	}
    883 
    884 #if defined(_WIN32) && !defined(__CYGWIN__)
    885 	/* Make sure the path separators in pathname, hardlink and symlink
    886 	 * are all slash '/', not the Windows path separator '\'. */
    887 	zip->entry = __la_win_entry_in_posix_pathseparator(entry);
    888 	if (zip->entry == entry)
    889 		zip->entry = archive_entry_clone(entry);
    890 #else
    891 	zip->entry = archive_entry_clone(entry);
    892 #endif
    893 	if (zip->entry == NULL) {
    894 		archive_set_error(&a->archive, ENOMEM,
    895 		    "Can't allocate zip header data");
    896 		return (ARCHIVE_FATAL);
    897 	}
    898 
    899 	{
    900 		const char *p;
    901 		size_t len;
    902 
    903 		if (archive_entry_pathname_l(zip->entry, &p, &len, sconv) != 0) {
    904 			const char* p_mbs;
    905 			if (errno == ENOMEM) {
    906 				archive_set_error(&a->archive, ENOMEM,
    907 				    "Can't allocate memory for Pathname");
    908 				return (ARCHIVE_FATAL);
    909 			}
    910 			p_mbs = archive_entry_pathname(zip->entry);
    911 			if (p_mbs) {
    912 				/* We have a wrongly-encoded MBS pathname.  Warn and use it.  */
    913 				archive_set_error(&a->archive,
    914 				    ARCHIVE_ERRNO_FILE_FORMAT,
    915 				    "Can't translate pathname '%s' to %s", p_mbs,
    916 				    archive_string_conversion_charset_name(sconv));
    917 				ret2 = ARCHIVE_WARN;
    918 			} else {
    919 				/* We have no MBS pathname.  Fail.  */
    920 				archive_set_error(&a->archive,
    921 				    ARCHIVE_ERRNO_FILE_FORMAT,
    922 				    "Can't translate pathname to %s",
    923 				    archive_string_conversion_charset_name(sconv));
    924 				return ARCHIVE_FAILED;
    925 			}
    926 		}
    927 		if (len > 0)
    928 			archive_entry_set_pathname(zip->entry, p);
    929 
    930 		/*
    931 		 * There is no standard for symlink handling; we convert
    932 		 * it using the same character-set translation that we use
    933 		 * for filename.
    934 		 */
    935 		if (type == AE_IFLNK) {
    936 			if (archive_entry_symlink_l(zip->entry, &p, &len, sconv)) {
    937 				if (errno == ENOMEM) {
    938 					archive_set_error(&a->archive, ENOMEM,
    939 					    "Can't allocate memory "
    940 					    " for Symlink");
    941 					return (ARCHIVE_FATAL);
    942 				}
    943 				/* No error if we can't convert. */
    944 			} else if (len > 0)
    945 				archive_entry_set_symlink(zip->entry, p);
    946 		}
    947 	}
    948 
    949 	/* If filename isn't ASCII and we can use UTF-8, set the UTF-8 flag. */
    950 	if (!is_all_ascii(archive_entry_pathname(zip->entry))) {
    951 		if (zip->opt_sconv != NULL) {
    952 			if (strcmp(archive_string_conversion_charset_name(
    953 					zip->opt_sconv), "UTF-8") == 0)
    954 				zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
    955 #if HAVE_NL_LANGINFO
    956 		} else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
    957 			zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
    958 #endif
    959 		}
    960 	}
    961 	filename_length = path_length(zip->entry);
    962 
    963 	/* Determine appropriate compression and size for this entry. */
    964 	if (type == AE_IFLNK) {
    965 		slink = archive_entry_symlink(zip->entry);
    966 		if (slink != NULL)
    967 			slink_size = strlen(slink);
    968 		else
    969 			slink_size = 0;
    970 		zip->entry_uncompressed_limit = slink_size;
    971 		zip->entry_compressed_size = slink_size;
    972 		zip->entry_uncompressed_size = slink_size;
    973 		zip->entry_crc32 = zip->crc32func(zip->entry_crc32,
    974 		    (const unsigned char *)slink, slink_size);
    975 		zip->entry_compression = COMPRESSION_STORE;
    976 		MIN_VERSION_NEEDED(20);
    977 	} else if (type != AE_IFREG) {
    978 		zip->entry_compression = COMPRESSION_STORE;
    979 		zip->entry_uncompressed_limit = 0;
    980 		MIN_VERSION_NEEDED(20);
    981 	} else if (archive_entry_size_is_set(zip->entry)) {
    982 		int64_t size = archive_entry_size(zip->entry);
    983 		int64_t additional_size = 0;
    984 
    985 		zip->entry_uncompressed_limit = size;
    986 		zip->entry_compression = zip->requested_compression;
    987 		if (zip->entry_compression == COMPRESSION_UNSPECIFIED) {
    988 			zip->entry_compression = COMPRESSION_DEFAULT;
    989 		}
    990 		switch (zip->entry_compression) {
    991 		case COMPRESSION_STORE:
    992 			zip->entry_compressed_size = size;
    993 			zip->entry_uncompressed_size = size;
    994 			MIN_VERSION_NEEDED(10);
    995 			break;
    996 		case COMPRESSION_ZSTD:
    997 			zip->entry_uncompressed_size = size;
    998 			MIN_VERSION_NEEDED(63);
    999 			break;
   1000 		case COMPRESSION_LZMA:
   1001 			zip->entry_uncompressed_size = size;
   1002 			zip->entry_flags |= ZIP_ENTRY_FLAG_LZMA_EOPM;
   1003 			MIN_VERSION_NEEDED(63);
   1004 			break;
   1005 		case COMPRESSION_XZ:
   1006 			zip->entry_uncompressed_size = size;
   1007 			MIN_VERSION_NEEDED(63);
   1008 			break;
   1009 		case COMPRESSION_BZIP2:
   1010 			zip->entry_uncompressed_size = size;
   1011 			MIN_VERSION_NEEDED(46);
   1012 			break;
   1013 		default: // i.e. deflate compression
   1014 			zip->entry_uncompressed_size = size;
   1015 			switch (zip->compression_level) {
   1016 			case 1:
   1017 			case 2:
   1018 				zip->entry_flags |= ZIP_ENTRY_FLAG_DEFLATE_SUPER_FAST;
   1019 				break;
   1020 			case 3:
   1021 			case 4:
   1022 				zip->entry_flags |= ZIP_ENTRY_FLAG_DEFLATE_FAST;
   1023 				break;
   1024 			case 8:
   1025 			case 9:
   1026 				zip->entry_flags |= ZIP_ENTRY_FLAG_DEFLATE_MAX;
   1027 				break;
   1028 			default:
   1029 				break;
   1030 			}
   1031 			MIN_VERSION_NEEDED(20);
   1032 			break;
   1033 		}
   1034 
   1035 		if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
   1036 			switch (zip->entry_encryption) {
   1037 			case ENCRYPTION_TRADITIONAL:
   1038 				additional_size = TRAD_HEADER_SIZE;
   1039 				MIN_VERSION_NEEDED(20);
   1040 				break;
   1041 			case ENCRYPTION_WINZIP_AES128:
   1042 				additional_size = WINZIP_AES128_HEADER_SIZE
   1043 				    + AUTH_CODE_SIZE;
   1044 				MIN_VERSION_NEEDED(20);
   1045 				break;
   1046 			case ENCRYPTION_WINZIP_AES256:
   1047 				additional_size = WINZIP_AES256_HEADER_SIZE
   1048 				    + AUTH_CODE_SIZE;
   1049 				MIN_VERSION_NEEDED(20);
   1050 				break;
   1051 			case ENCRYPTION_NONE:
   1052 			default:
   1053 				break;
   1054 			}
   1055 			if (zip->entry_compression == COMPRESSION_STORE)
   1056 				zip->entry_compressed_size += additional_size;
   1057 		}
   1058 
   1059 		/*
   1060 		 * Set Zip64 extension in any of the following cases
   1061 		 * (this was suggested by discussion on info-zip-dev
   1062 		 * mailing list):
   1063 		 *  = Zip64 is being forced by user
   1064 		 *  = File is over 4GiB uncompressed
   1065 		 *    (including encryption header, if any)
   1066 		 *  = File is close to 4GiB and is being compressed
   1067 		 *    (compression might make file larger)
   1068 		 */
   1069 		if ((zip->flags & ZIP_FLAG_FORCE_ZIP64)
   1070 		    || (zip->entry_uncompressed_size + additional_size > ZIP_4GB_MAX)
   1071 		    || (zip->entry_uncompressed_size > ZIP_4GB_MAX_UNCOMPRESSED
   1072 			&& zip->entry_compression != COMPRESSION_STORE)) {
   1073 			MIN_VERSION_NEEDED(45);
   1074 		}
   1075 
   1076 		/* We may know the size, but never the CRC. */
   1077 		zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
   1078 	} else {
   1079 		/* We don't know the size. Use the default
   1080 		 * compression unless specified otherwise.
   1081 		 */
   1082 		zip->entry_compression = zip->requested_compression;
   1083 		if (zip->entry_compression == COMPRESSION_UNSPECIFIED) {
   1084 			zip->entry_compression = COMPRESSION_DEFAULT;
   1085 		}
   1086 
   1087 		zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
   1088 		if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) {
   1089 			/* We might use zip64 extensions, so require 4.5 */
   1090 			MIN_VERSION_NEEDED(45);
   1091 		}
   1092 		switch (zip->entry_compression) {
   1093 		case COMPRESSION_STORE:
   1094 			MIN_VERSION_NEEDED(10);
   1095 			break;
   1096 		case COMPRESSION_ZSTD:
   1097 			MIN_VERSION_NEEDED(63);
   1098 			break;
   1099 		case COMPRESSION_LZMA:
   1100 			zip->entry_flags |= ZIP_ENTRY_FLAG_LZMA_EOPM;
   1101 			MIN_VERSION_NEEDED(63);
   1102 			break;
   1103 		case COMPRESSION_XZ:
   1104 			MIN_VERSION_NEEDED(63);
   1105 			break;
   1106 		case COMPRESSION_BZIP2:
   1107 			MIN_VERSION_NEEDED(46);
   1108 			break;
   1109 		default: // i.e. deflate compression
   1110 			switch (zip->compression_level) {
   1111 			case 1:
   1112 			case 2:
   1113 				zip->entry_flags |= ZIP_ENTRY_FLAG_DEFLATE_SUPER_FAST;
   1114 				break;
   1115 			case 3:
   1116 			case 4:
   1117 				zip->entry_flags |= ZIP_ENTRY_FLAG_DEFLATE_FAST;
   1118 				break;
   1119 			case 8:
   1120 			case 9:
   1121 				zip->entry_flags |= ZIP_ENTRY_FLAG_DEFLATE_MAX;
   1122 				break;
   1123 			default:
   1124 				break;
   1125 			}
   1126 			MIN_VERSION_NEEDED(20);
   1127 			break;
   1128 		}
   1129 
   1130 		if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
   1131 			switch (zip->entry_encryption) {
   1132 			case ENCRYPTION_TRADITIONAL:
   1133 			case ENCRYPTION_WINZIP_AES128:
   1134 			case ENCRYPTION_WINZIP_AES256:
   1135 				MIN_VERSION_NEEDED(20);
   1136 				break;
   1137 			case ENCRYPTION_NONE:
   1138 			default:
   1139 				break;
   1140 			}
   1141 		}
   1142 	}
   1143 
   1144 	/* Format the local header. */
   1145 	memset(local_header, 0, sizeof(local_header));
   1146 	memcpy(local_header, "PK\003\004", 4);
   1147 	archive_le16enc(local_header + 4, version_needed);
   1148 	archive_le16enc(local_header + 6, zip->entry_flags);
   1149 	if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
   1150 	    || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
   1151 		archive_le16enc(local_header + 8, WINZIP_AES_ENCRYPTION);
   1152 	else
   1153 		archive_le16enc(local_header + 8, zip->entry_compression);
   1154 	archive_le32enc(local_header + 10,
   1155 		unix_to_dos(archive_entry_mtime(zip->entry)));
   1156 	if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) == 0) {
   1157 		archive_le32enc(local_header + 14, zip->entry_crc32);
   1158 		archive_le32enc(local_header + 18, (uint32_t)zip->entry_compressed_size);
   1159 		archive_le32enc(local_header + 22, (uint32_t)zip->entry_uncompressed_size);
   1160 	}
   1161 	archive_le16enc(local_header + 26, (uint16_t)filename_length);
   1162 
   1163 	if (zip->entry_encryption == ENCRYPTION_TRADITIONAL) {
   1164 		if (zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END)
   1165 			zip->trad_chkdat = local_header[11];
   1166 		else
   1167 			zip->trad_chkdat = local_header[17];
   1168 	}
   1169 
   1170 	/* Format as much of central directory file header as we can: */
   1171 	zip->file_header = cd_alloc(zip, 46);
   1172 	/* If (zip->file_header == NULL) XXXX */
   1173 	++zip->central_directory_entries;
   1174 	memset(zip->file_header, 0, 46);
   1175 	memcpy(zip->file_header, "PK\001\002", 4);
   1176 	/* "Made by PKZip 2.0 on Unix." */
   1177 	archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed);
   1178 	archive_le16enc(zip->file_header + 6, version_needed);
   1179 	archive_le16enc(zip->file_header + 8, zip->entry_flags);
   1180 	if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
   1181 	    || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
   1182 		archive_le16enc(zip->file_header + 10, WINZIP_AES_ENCRYPTION);
   1183 	else
   1184 		archive_le16enc(zip->file_header + 10, zip->entry_compression);
   1185 	archive_le32enc(zip->file_header + 12,
   1186 		unix_to_dos(archive_entry_mtime(zip->entry)));
   1187 	archive_le16enc(zip->file_header + 28, (uint16_t)filename_length);
   1188 	/* Following Info-Zip, store mode in the "external attributes" field. */
   1189 	archive_le32enc(zip->file_header + 38,
   1190 	    ((uint32_t)archive_entry_mode(zip->entry)) << 16);
   1191 	e = cd_alloc(zip, filename_length);
   1192 	/* If (e == NULL) XXXX */
   1193 	copy_path(zip->entry, e);
   1194 
   1195 	/* Format extra data. */
   1196 	memset(local_extra, 0, sizeof(local_extra));
   1197 	e = local_extra;
   1198 
   1199 	/* First, extra blocks that are the same between
   1200 	 * the local file header and the central directory.
   1201 	 * We format them once and then duplicate them. */
   1202 
   1203 	/* ux Unix extra data, length 11, version 1 */
   1204 	if (archive_entry_uid_is_set(entry) || archive_entry_gid_is_set(entry)) {
   1205 		/* TODO: If uid < 64k, use 2 bytes, ditto for gid. */
   1206 		memcpy(e, "ux\013\000\001", 5);
   1207 		e += 5;
   1208 		*e++ = 4; /* Length of following UID */
   1209 		archive_le32enc(e, (uint32_t)archive_entry_uid(entry));
   1210 		e += 4;
   1211 		*e++ = 4; /* Length of following GID */
   1212 		archive_le32enc(e, (uint32_t)archive_entry_gid(entry));
   1213 		e += 4;
   1214 	}
   1215 
   1216 	/* AES extra data field: WinZIP AES information, ID=0x9901 */
   1217 	if ((zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED)
   1218 	    && (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
   1219 	        || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)) {
   1220 
   1221 		memcpy(e, "\001\231\007\000\001\000AE", 8);
   1222 		/* AES vendor version AE-2 does not store a CRC.
   1223 		 * WinZip 11 uses AE-1, which does store the CRC,
   1224 		 * but it does not store the CRC when the file size
   1225 		 * is less than 20 bytes. So we simulate what
   1226 		 * WinZip 11 does.
   1227 		 * NOTE: WinZip 9.0 and 10.0 uses AE-2 by default. */
   1228 		if (archive_entry_size_is_set(zip->entry)
   1229 		    && archive_entry_size(zip->entry) < 20) {
   1230 			archive_le16enc(e+4, AES_VENDOR_AE_2);
   1231 			zip->aes_vendor = AES_VENDOR_AE_2;/* no CRC. */
   1232 		} else
   1233 			zip->aes_vendor = AES_VENDOR_AE_1;
   1234 		e += 8;
   1235 		/* AES encryption strength. */
   1236 		*e++ = (zip->entry_encryption == ENCRYPTION_WINZIP_AES128)?1:3;
   1237 		/* Actual compression method. */
   1238 		archive_le16enc(e, zip->entry_compression);
   1239 		e += 2;
   1240 	}
   1241 
   1242 	/* Copy ux, AES-extra into central directory as well. */
   1243 	zip->file_header_extra_offset = zip->central_directory_bytes;
   1244 	cd_extra = cd_alloc(zip, e - local_extra);
   1245 	memcpy(cd_extra, local_extra, e - local_extra);
   1246 
   1247 	/*
   1248 	 * Following extra blocks vary between local header and
   1249 	 * central directory. These are the local header versions.
   1250 	 * Central directory versions get formatted in
   1251 	 * archive_write_zip_finish_entry() below.
   1252 	 */
   1253 
   1254 	/* UT timestamp: length depends on what timestamps are set.
   1255 	 * This header appears in the Central Directory also, but
   1256 	 * according to Info-Zip specification, the CD form
   1257 	 * only holds mtime, so we format it separately. */
   1258 	if (archive_entry_mtime_is_set(entry)
   1259 	    || archive_entry_atime_is_set(entry)
   1260 	    || archive_entry_ctime_is_set(entry)) {
   1261 		unsigned char *ut = e;
   1262 		memcpy(e, "UT\000\000", 4);
   1263 		e += 4;
   1264 		*e++ = (archive_entry_mtime_is_set(entry) ? 1 : 0)
   1265 			| (archive_entry_atime_is_set(entry) ? 2 : 0)
   1266 			| (archive_entry_ctime_is_set(entry) ? 4 : 0);
   1267 		if (archive_entry_mtime_is_set(entry)) {
   1268 			archive_le32enc(e, (uint32_t)archive_entry_mtime(entry));
   1269 			e += 4;
   1270 		}
   1271 		if (archive_entry_atime_is_set(entry)) {
   1272 			archive_le32enc(e, (uint32_t)archive_entry_atime(entry));
   1273 			e += 4;
   1274 		}
   1275 		if (archive_entry_ctime_is_set(entry)) {
   1276 			archive_le32enc(e, (uint32_t)archive_entry_ctime(entry));
   1277 			e += 4;
   1278 		}
   1279 		archive_le16enc(ut + 2, (uint16_t)(e - ut - 4));
   1280 	}
   1281 
   1282 	/*
   1283 	 * Note about Zip64 Extended Information Extra Field:
   1284 	 * Because libarchive always writes in a streaming
   1285 	 * fashion, we never know the CRC when we're writing
   1286 	 * the local header.  So we have to use length-at-end, which
   1287 	 * prevents us from putting size information into a Zip64
   1288 	 * extra field.  However, apparently some readers find it
   1289 	 * a helpful clue to have an empty such field so they
   1290 	 * can expect a 64-bit length-at-end marker.
   1291 	 */
   1292 	if (archive_entry_size_is_set(zip->entry)
   1293 	    && (zip->entry_uncompressed_size > ZIP_4GB_MAX
   1294 		|| zip->entry_compressed_size > ZIP_4GB_MAX)) {
   1295 		/* Header ID 0x0001, size 0 */
   1296 		memcpy(e, "\001\000\000\000", 4);
   1297 		e += 4;
   1298 	}
   1299 
   1300 	if (zip->flags & ZIP_FLAG_EXPERIMENT_xl) {
   1301 		/* Experimental 'xl' extension to improve streaming. */
   1302 		unsigned char *external_info = e;
   1303 		int included = 7;
   1304 		memcpy(e, "xl\000\000", 4); // 0x6c65 + 2-byte length
   1305 		e += 4;
   1306 		e[0] = included; /* bitmap of included fields */
   1307 		e += 1;
   1308 		if (included & 1) {
   1309 			archive_le16enc(e, /* "Version created by" */
   1310 			    3 * 256 + version_needed);
   1311 			e += 2;
   1312 		}
   1313 		if (included & 2) {
   1314 			archive_le16enc(e, 0); /* internal file attributes */
   1315 			e += 2;
   1316 		}
   1317 		if (included & 4) {
   1318 			archive_le32enc(e,  /* external file attributes */
   1319 			    ((uint32_t)archive_entry_mode(zip->entry)) << 16);
   1320 			e += 4;
   1321 		}
   1322 		if (included & 8) {
   1323 			// Libarchive does not currently support file comments.
   1324 		}
   1325 		archive_le16enc(external_info + 2, (uint16_t)(e - (external_info + 4)));
   1326 	}
   1327 
   1328 	/* Update local header with size of extra data and write it all out: */
   1329 	archive_le16enc(local_header + 28, (uint16_t)(e - local_extra));
   1330 
   1331 	ret = __archive_write_output(a, local_header, 30);
   1332 	if (ret != ARCHIVE_OK)
   1333 		return (ARCHIVE_FATAL);
   1334 	zip->written_bytes += 30;
   1335 
   1336 	ret = write_path(zip->entry, a);
   1337 	if (ret <= ARCHIVE_OK)
   1338 		return (ARCHIVE_FATAL);
   1339 	zip->written_bytes += ret;
   1340 
   1341 	ret = __archive_write_output(a, local_extra, e - local_extra);
   1342 	if (ret != ARCHIVE_OK)
   1343 		return (ARCHIVE_FATAL);
   1344 	zip->written_bytes += e - local_extra;
   1345 
   1346 	/* For symlinks, write the body now. */
   1347 	if (slink != NULL) {
   1348 		ret = __archive_write_output(a, slink, slink_size);
   1349 		if (ret != ARCHIVE_OK)
   1350 			return (ARCHIVE_FATAL);
   1351 		zip->entry_compressed_written += slink_size;
   1352 		zip->entry_uncompressed_written += slink_size;
   1353 		zip->written_bytes += slink_size;
   1354 	}
   1355 
   1356 	switch (zip->entry_compression) {
   1357 #ifdef HAVE_ZLIB_H
   1358 	case COMPRESSION_DEFLATE:
   1359 		zip->stream.deflate.zalloc = Z_NULL;
   1360 		zip->stream.deflate.zfree = Z_NULL;
   1361 		zip->stream.deflate.opaque = Z_NULL;
   1362 		zip->stream.deflate.next_out = zip->buf;
   1363 		zip->stream.deflate.avail_out = (uInt)zip->len_buf;
   1364 		if (deflateInit2(&zip->stream.deflate, zip->compression_level,
   1365 		    Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
   1366 			archive_set_error(&a->archive, ENOMEM,
   1367 			    "Can't init deflate compressor");
   1368 			return (ARCHIVE_FATAL);
   1369 		}
   1370 		break;
   1371 #endif
   1372 #ifdef HAVE_BZLIB_H
   1373 	case COMPRESSION_BZIP2:
   1374 		memset(&zip->stream.bzip2, 0, sizeof(bz_stream));
   1375 		zip->stream.bzip2.next_out = (char*)zip->buf;
   1376 		zip->stream.bzip2.avail_out = (unsigned int)zip->len_buf;
   1377 		if (BZ2_bzCompressInit(&zip->stream.bzip2, zip->compression_level, 0, 0) != BZ_OK) {
   1378 			archive_set_error(&a->archive, ENOMEM,
   1379 			    "Can't init bzip2 compressor");
   1380 			return (ARCHIVE_FATAL);
   1381 		}
   1382 		break;
   1383 #endif
   1384 #if defined(HAVE_ZSTD_H) && HAVE_ZSTD_compressStream
   1385 	case COMPRESSION_ZSTD:
   1386 		{/* Libzstd, contrary to many compression libraries, doesn't use
   1387 		 * zlib's 0 to 9 scale and its negative scale is way bigger than
   1388 		 * its positive one. So setting 1 as the lowest allowed compression
   1389 		 * level and rescaling to 2 to 9 to libzstd's positive scale. */
   1390 		int zstd_compression_level = zip->compression_level == 1
   1391 			? ZSTD_minCLevel() // ZSTD_minCLevel is negative !
   1392 			: (zip->compression_level - 1) * ZSTD_maxCLevel() / 8;
   1393 		zip->stream.zstd.context = ZSTD_createCStream();
   1394 		size_t zret = ZSTD_initCStream(zip->stream.zstd.context, zstd_compression_level);
   1395 		if (ZSTD_isError(zret)) {
   1396 			archive_set_error(&a->archive, ENOMEM,
   1397 			    "Can't init zstd compressor");
   1398 			return (ARCHIVE_FATAL);
   1399 		}
   1400 		/* Asking for the multi-threaded compressor is a no-op in zstd if
   1401 		 * it's not supported, so no need to explicitly check for it */
   1402 		ZSTD_CCtx_setParameter(zip->stream.zstd.context, ZSTD_c_nbWorkers, zip->threads);
   1403 		zip->stream.zstd.out.dst = zip->buf;
   1404 		zip->stream.zstd.out.size = zip->len_buf;
   1405 		zip->stream.zstd.out.pos = 0;
   1406 		break;}
   1407 #endif
   1408 #ifdef HAVE_LZMA_H
   1409 	case COMPRESSION_LZMA:
   1410 		{/* Set compression level 9 as the no-holds barred one */
   1411 		uint32_t lzma_compression_level = zip->compression_level == 9
   1412 			? LZMA_PRESET_EXTREME | zip->compression_level
   1413 			: (uint32_t)zip->compression_level;
   1414 		/* Forcibly setting up the encoder to use the LZMA1 variant, as
   1415 		 * it is the one LZMA Alone uses. */
   1416 		lzma_filter filters[2] = {
   1417 			{
   1418 				.id = LZMA_FILTER_LZMA1,
   1419 				.options = &zip->stream.lzma.options
   1420 			},
   1421 			{
   1422 				.id = LZMA_VLI_UNKNOWN
   1423 			}
   1424 		};
   1425 		memset(&zip->stream.lzma.context, 0, sizeof(lzma_stream));
   1426 		lzma_lzma_preset(&zip->stream.lzma.options, lzma_compression_level);
   1427 		zip->stream.lzma.headers_to_write = 1;
   1428 		/* We'll be writing the headers ourselves, so using the raw
   1429 		 * encoder */
   1430 		if (lzma_raw_encoder(&zip->stream.lzma.context, filters) != LZMA_OK) {
   1431 			archive_set_error(&a->archive, ENOMEM,
   1432 			    "Can't init lzma compressor");
   1433 			return (ARCHIVE_FATAL);
   1434 		}
   1435 		zip->stream.lzma.context.next_out = zip->buf;
   1436 		zip->stream.lzma.context.avail_out = (unsigned int)zip->len_buf;
   1437 		break;}
   1438 	case COMPRESSION_XZ:
   1439 		{/* Set compression level 9 as the no-holds barred one */
   1440 		uint32_t lzma_compression_level = zip->compression_level == 9
   1441 			? LZMA_PRESET_EXTREME | zip->compression_level
   1442 			: (uint32_t)zip->compression_level;
   1443 		lzma_ret retval;
   1444 #ifndef HAVE_LZMA_STREAM_ENCODER_MT
   1445 		/* Force the number of threads to one, and thus to a mono-threaded
   1446 		 * encoder in case we don't have the multi-threaded one */
   1447 		zip->threads = 1;
   1448 #endif
   1449 		memset(&zip->stream.lzma.context, 0, sizeof(lzma_stream));
   1450 		/* The XZ check will be arbitrarily set to none: ZIP already has
   1451 		 * a CRC-32 check of its own */
   1452 		if (zip->threads == 1) {
   1453 			/* XZ uses LZMA2. */
   1454 			lzma_filter filters[2] = {
   1455 				{
   1456 					.id = LZMA_FILTER_LZMA2,
   1457 					.options = &zip->stream.lzma.options
   1458 				},
   1459 				{
   1460 					.id = LZMA_VLI_UNKNOWN
   1461 				}
   1462 			};
   1463 			/* Might as well use the lzma_options we already allocated,
   1464 			 * even if we'll never use it after the initialisation */
   1465 			lzma_lzma_preset(&zip->stream.lzma.options, lzma_compression_level);
   1466 			/* 1 thread requested, so non multi-threaded encoder */
   1467 			retval = lzma_stream_encoder(&zip->stream.lzma.context,
   1468 				filters, LZMA_CHECK_NONE);
   1469 		}
   1470 		else {
   1471 			lzma_mt options = {
   1472 				.flags = 0,
   1473 				.block_size = 0,
   1474 				.timeout = 0,
   1475 				.filters = NULL,
   1476 				.check = LZMA_CHECK_NONE,
   1477 				.preset = lzma_compression_level,
   1478 				.threads = zip->threads
   1479 			};
   1480 			/* More than 1 thread requested, so multi-threaded encoder
   1481 			 * which always outputs XZ */
   1482 			retval = lzma_stream_encoder_mt(&zip->stream.lzma.context,
   1483 				&options);
   1484 		}
   1485 		if (retval != LZMA_OK) {
   1486 			archive_set_error(&a->archive, ENOMEM,
   1487 			    "Can't init xz compressor");
   1488 			return (ARCHIVE_FATAL);
   1489 		}
   1490 		zip->stream.lzma.context.next_out = zip->buf;
   1491 		zip->stream.lzma.context.avail_out = (unsigned int)zip->len_buf;
   1492 		break;}
   1493 #endif
   1494 	default:
   1495 		break;
   1496 	}
   1497 
   1498 	return (ret2);
   1499 }
   1500 
   1501 static ssize_t
   1502 archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
   1503 {
   1504 	int ret;
   1505 	struct zip *zip = a->format_data;
   1506 
   1507 	if ((int64_t)s > zip->entry_uncompressed_limit)
   1508 		s = (size_t)zip->entry_uncompressed_limit;
   1509 	zip->entry_uncompressed_written += s;
   1510 
   1511 	if (s == 0) return 0;
   1512 
   1513 	if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
   1514 		switch (zip->entry_encryption) {
   1515 		case ENCRYPTION_TRADITIONAL:
   1516 			/* Initialize traditional PKWARE encryption context. */
   1517 			if (!zip->tctx_valid) {
   1518 				ret = init_traditional_pkware_encryption(a);
   1519 				if (ret != ARCHIVE_OK)
   1520 					return (ret);
   1521 				zip->tctx_valid = 1;
   1522 			}
   1523 			break;
   1524 		case ENCRYPTION_WINZIP_AES128:
   1525 		case ENCRYPTION_WINZIP_AES256:
   1526 			if (!zip->cctx_valid) {
   1527 				ret = init_winzip_aes_encryption(a);
   1528 				if (ret != ARCHIVE_OK)
   1529 					return (ret);
   1530 				zip->cctx_valid = zip->hctx_valid = 1;
   1531 			}
   1532 			break;
   1533 		case ENCRYPTION_NONE:
   1534 		default:
   1535 			break;
   1536 		}
   1537 	}
   1538 
   1539 	switch (zip->entry_compression) {
   1540 	case COMPRESSION_STORE:
   1541 		if (zip->tctx_valid || zip->cctx_valid) {
   1542 			const uint8_t *rb = (const uint8_t *)buff;
   1543 			const uint8_t * const re = rb + s;
   1544 
   1545 			while (rb < re) {
   1546 				size_t l;
   1547 
   1548 				if (zip->tctx_valid) {
   1549 					l = trad_enc_encrypt_update(&zip->tctx,
   1550 					    rb, re - rb,
   1551 					    zip->buf, zip->len_buf);
   1552 				} else {
   1553 					l = zip->len_buf;
   1554 					ret = archive_encrypto_aes_ctr_update(
   1555 					    &zip->cctx,
   1556 					    rb, re - rb, zip->buf, &l);
   1557 					if (ret < 0) {
   1558 						archive_set_error(&a->archive,
   1559 						    ARCHIVE_ERRNO_MISC,
   1560 						    "Failed to encrypt file");
   1561 						return (ARCHIVE_FAILED);
   1562 					}
   1563 					archive_hmac_sha1_update(&zip->hctx,
   1564 					    zip->buf, l);
   1565 				}
   1566 				ret = __archive_write_output(a, zip->buf, l);
   1567 				if (ret != ARCHIVE_OK)
   1568 					return (ret);
   1569 				zip->entry_compressed_written += l;
   1570 				zip->written_bytes += l;
   1571 				rb += l;
   1572 			}
   1573 		} else {
   1574 			ret = __archive_write_output(a, buff, s);
   1575 			if (ret != ARCHIVE_OK)
   1576 				return (ret);
   1577 			zip->written_bytes += s;
   1578 			zip->entry_compressed_written += s;
   1579 		}
   1580 		break;
   1581 #ifdef HAVE_ZLIB_H
   1582 	case COMPRESSION_DEFLATE:
   1583 		zip->stream.deflate.next_in = (unsigned char*)(uintptr_t)buff;
   1584 		zip->stream.deflate.avail_in = (uInt)s;
   1585 		do {
   1586 			ret = deflate(&zip->stream.deflate, Z_NO_FLUSH);
   1587 			if (ret == Z_STREAM_ERROR)
   1588 				return (ARCHIVE_FATAL);
   1589 			if (zip->stream.deflate.avail_out == 0) {
   1590 				if (zip->tctx_valid) {
   1591 					trad_enc_encrypt_update(&zip->tctx,
   1592 					    zip->buf, zip->len_buf,
   1593 					    zip->buf, zip->len_buf);
   1594 				} else if (zip->cctx_valid) {
   1595 					size_t outl = zip->len_buf;
   1596 					ret = archive_encrypto_aes_ctr_update(
   1597 					    &zip->cctx,
   1598 					    zip->buf, zip->len_buf,
   1599 					    zip->buf, &outl);
   1600 					if (ret < 0) {
   1601 						archive_set_error(&a->archive,
   1602 						    ARCHIVE_ERRNO_MISC,
   1603 						    "Failed to encrypt file");
   1604 						return (ARCHIVE_FAILED);
   1605 					}
   1606 					archive_hmac_sha1_update(&zip->hctx,
   1607 					    zip->buf, zip->len_buf);
   1608 				}
   1609 				ret = __archive_write_output(a, zip->buf,
   1610 					zip->len_buf);
   1611 				if (ret != ARCHIVE_OK)
   1612 					return (ret);
   1613 				zip->entry_compressed_written += zip->len_buf;
   1614 				zip->written_bytes += zip->len_buf;
   1615 				zip->stream.deflate.next_out = zip->buf;
   1616 				zip->stream.deflate.avail_out = (uInt)zip->len_buf;
   1617 			}
   1618 		} while (zip->stream.deflate.avail_in != 0);
   1619 		break;
   1620 #endif
   1621 #if defined(HAVE_ZSTD_H) && HAVE_ZSTD_compressStream
   1622 	case COMPRESSION_ZSTD:
   1623 		zip->stream.zstd.in.src = buff;
   1624 		zip->stream.zstd.in.size = s;
   1625 		zip->stream.zstd.in.pos = 0;
   1626 		do {
   1627 			size_t zret = ZSTD_compressStream(zip->stream.zstd.context,
   1628 				&zip->stream.zstd.out, &zip->stream.zstd.in);
   1629 			if (ZSTD_isError(zret))
   1630 				return (ARCHIVE_FATAL);
   1631 			if (zip->stream.zstd.out.pos == zip->stream.zstd.out.size) {
   1632 				if (zip->tctx_valid) {
   1633 					trad_enc_encrypt_update(&zip->tctx,
   1634 						zip->buf, zip->len_buf,
   1635 						zip->buf, zip->len_buf);
   1636 				} else if (zip->cctx_valid) {
   1637 					size_t outl = zip->len_buf;
   1638 					ret = archive_encrypto_aes_ctr_update(
   1639 						&zip->cctx,
   1640 						zip->buf, zip->len_buf,
   1641 						zip->buf, &outl);
   1642 					if (ret < 0) {
   1643 						archive_set_error(&a->archive,
   1644 							ARCHIVE_ERRNO_MISC,
   1645 							"Failed to encrypt file");
   1646 						return (ARCHIVE_FAILED);
   1647 					}
   1648 					archive_hmac_sha1_update(&zip->hctx,
   1649 						zip->buf, zip->len_buf);
   1650 				}
   1651 				ret = __archive_write_output(a, zip->buf,
   1652 					zip->len_buf);
   1653 				if (ret != ARCHIVE_OK)
   1654 					return (ret);
   1655 				zip->entry_compressed_written += zip->len_buf;
   1656 				zip->written_bytes += zip->len_buf;
   1657 				zip->stream.zstd.out.dst = zip->buf;
   1658 				zip->stream.zstd.out.size = zip->len_buf;
   1659 				zip->stream.zstd.out.pos = 0;
   1660 			}
   1661 		} while (zip->stream.zstd.in.pos != zip->stream.zstd.in.size);
   1662 		break;
   1663 #endif
   1664 #ifdef HAVE_BZLIB_H
   1665 	case COMPRESSION_BZIP2:
   1666 		zip->stream.bzip2.next_in = (char*)(uintptr_t)buff;
   1667 		zip->stream.bzip2.avail_in = (unsigned int)s;
   1668 		do {
   1669 			ret = BZ2_bzCompress(&zip->stream.bzip2, BZ_RUN);
   1670 			if (ret != BZ_RUN_OK)
   1671 				return (ARCHIVE_FATAL);
   1672 			if (zip->stream.bzip2.avail_out == 0) {
   1673 				if (zip->tctx_valid) {
   1674 					trad_enc_encrypt_update(&zip->tctx,
   1675 						zip->buf, zip->len_buf,
   1676 						zip->buf, zip->len_buf);
   1677 				} else if (zip->cctx_valid) {
   1678 					size_t outl = zip->len_buf;
   1679 					ret = archive_encrypto_aes_ctr_update(
   1680 						&zip->cctx,
   1681 						zip->buf, zip->len_buf,
   1682 						zip->buf, &outl);
   1683 					if (ret < 0) {
   1684 						archive_set_error(&a->archive,
   1685 							ARCHIVE_ERRNO_MISC,
   1686 							"Failed to encrypt file");
   1687 						return (ARCHIVE_FAILED);
   1688 					}
   1689 					archive_hmac_sha1_update(&zip->hctx,
   1690 						zip->buf, zip->len_buf);
   1691 				}
   1692 				ret = __archive_write_output(a, zip->buf,
   1693 					zip->len_buf);
   1694 				if (ret != ARCHIVE_OK)
   1695 					return (ret);
   1696 				zip->entry_compressed_written += zip->len_buf;
   1697 				zip->written_bytes += zip->len_buf;
   1698 				zip->stream.bzip2.next_out = (char*)zip->buf;
   1699 				zip->stream.bzip2.avail_out = (unsigned int)zip->len_buf;
   1700 			}
   1701 		} while (zip->stream.bzip2.avail_in != 0);
   1702 		break;
   1703 #endif
   1704 #ifdef HAVE_LZMA_H
   1705 	case COMPRESSION_LZMA:
   1706 		if (zip->stream.lzma.headers_to_write) {
   1707 			/* LZMA Alone and ZIP's LZMA format (i.e. id 14) are almost
   1708 			 * the same. Here's an example of a structure of LZMA Alone:
   1709 			 *
   1710 			 * $ cat /bin/ls | lzma | xxd | head -n 1
   1711 			 * 00000000: 5d00 0080 00ff ffff ffff ffff ff00 2814
   1712 			 *
   1713 			 *    5 bytes        8 bytes        n bytes
   1714 			 * <lzma_params><uncompressed_size><data...>
   1715 			 *
   1716 			 * lzma_params is a 5-byte blob that has to be decoded to
   1717 			 * extract parameters of this LZMA stream. The
   1718 			 * uncompressed_size field is an uint64_t value that contains
   1719 			 * information about the size of the uncompressed file, or
   1720 			 * UINT64_MAX if this value is unknown. The <data...> part is
   1721 			 * the actual LZMA-compressed data stream.
   1722 			 *
   1723 			 * Now here's the structure of ZIP's LZMA format:
   1724 			 *
   1725 			 * $ cat stream_inside_zipx | xxd | head -n 1
   1726 			 * 00000000: 0914 0500 5d00 8000 0000 2814 .... ....
   1727 			 *
   1728 			 *  2byte   2byte    5 bytes     n bytes
   1729 			 * <magic1><magic2><lzma_params><data...>
   1730 			 *
   1731 			 * This means that ZIP's LZMA format contains an additional
   1732 			 * magic1 and magic2 headers, the lzma_params field contains
   1733 			 * the same parameter set as in LZMA Alone, and the <data...>
   1734 			 * field is the same as in LZMA Alone as well. However, note
   1735 			 * that ZIP's format is missing the uncompressed_size field.
   1736 			 *
   1737 			 * So we need to write a raw LZMA stream, set up for LZMA1
   1738 			 * (i.e. the algorithm variant LZMA Alone uses), which was
   1739 			 * done above in the initialisation but first we need to
   1740 			 * write ZIP's LZMA header, as if it were Stored data. Then
   1741 			 * we can use the raw stream as if it were any other. magic1
   1742 			 * being version numbers and magic2 being lzma_params's size,
   1743 			 * they get written in without further ado but lzma_params
   1744 			 * requires to use other functions than the usual lzma_stream
   1745 			 * manipulating ones, hence the additional book-keeping
   1746 			 * required alongside the lzma_stream.
   1747 			 */
   1748 			uint8_t buf[9] = { LZMA_VERSION_MAJOR, LZMA_VERSION_MINOR, 5, 0 };
   1749 			lzma_lzma_props_encode(&zip->stream.lzma.options, buf + 4);
   1750 			const size_t sh = 9;
   1751 			if (zip->tctx_valid || zip->cctx_valid) {
   1752 				uint8_t* header = buf;
   1753 				const uint8_t * const rh = header + sh;
   1754 
   1755 				while (header < rh) {
   1756 					size_t l;
   1757 
   1758 					if (zip->tctx_valid) {
   1759 						l = trad_enc_encrypt_update(&zip->tctx,
   1760 							header, rh - header,
   1761 							zip->buf, zip->len_buf);
   1762 					} else {
   1763 						l = zip->len_buf;
   1764 						ret = archive_encrypto_aes_ctr_update(
   1765 							&zip->cctx,
   1766 							header, rh - header, zip->buf, &l);
   1767 						if (ret < 0) {
   1768 							archive_set_error(&a->archive,
   1769 								ARCHIVE_ERRNO_MISC,
   1770 								"Failed to encrypt file");
   1771 							return (ARCHIVE_FAILED);
   1772 						}
   1773 						archive_hmac_sha1_update(&zip->hctx,
   1774 							zip->buf, l);
   1775 					}
   1776 					ret = __archive_write_output(a, zip->buf, l);
   1777 					if (ret != ARCHIVE_OK)
   1778 						return (ret);
   1779 					zip->entry_compressed_written += l;
   1780 					zip->written_bytes += l;
   1781 					header += l;
   1782 				}
   1783 			} else {
   1784 				ret = __archive_write_output(a, buf, sh);
   1785 				if (ret != ARCHIVE_OK)
   1786 					return (ret);
   1787 				zip->written_bytes += sh;
   1788 				zip->entry_compressed_written += sh;
   1789 			}
   1790 			zip->stream.lzma.headers_to_write = 0;
   1791 		}
   1792 		/* FALLTHROUGH */
   1793 	case COMPRESSION_XZ:
   1794 		zip->stream.lzma.context.next_in = (unsigned char*)(uintptr_t)buff;
   1795 		zip->stream.lzma.context.avail_in = (unsigned int)s;
   1796 		do {
   1797 			ret = lzma_code(&zip->stream.lzma.context, LZMA_RUN);
   1798 			if (ret == LZMA_MEM_ERROR)
   1799 				return (ARCHIVE_FATAL);
   1800 			if (zip->stream.lzma.context.avail_out == 0) {
   1801 				if (zip->tctx_valid) {
   1802 					trad_enc_encrypt_update(&zip->tctx,
   1803 						zip->buf, zip->len_buf,
   1804 						zip->buf, zip->len_buf);
   1805 				} else if (zip->cctx_valid) {
   1806 					size_t outl = zip->len_buf;
   1807 					ret = archive_encrypto_aes_ctr_update(
   1808 						&zip->cctx,
   1809 						zip->buf, zip->len_buf,
   1810 						zip->buf, &outl);
   1811 					if (ret < 0) {
   1812 						archive_set_error(&a->archive,
   1813 							ARCHIVE_ERRNO_MISC,
   1814 							"Failed to encrypt file");
   1815 						return (ARCHIVE_FAILED);
   1816 					}
   1817 					archive_hmac_sha1_update(&zip->hctx,
   1818 						zip->buf, zip->len_buf);
   1819 				}
   1820 				ret = __archive_write_output(a, zip->buf,
   1821 					zip->len_buf);
   1822 				if (ret != ARCHIVE_OK)
   1823 					return (ret);
   1824 				zip->entry_compressed_written += zip->len_buf;
   1825 				zip->written_bytes += zip->len_buf;
   1826 				zip->stream.lzma.context.next_out = zip->buf;
   1827 				zip->stream.lzma.context.avail_out = (unsigned int)zip->len_buf;
   1828 			}
   1829 		} while (zip->stream.lzma.context.avail_in != 0);
   1830 		break;
   1831 #endif
   1832 	case COMPRESSION_UNSPECIFIED:
   1833 	default:
   1834 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
   1835 		    "Invalid ZIP compression type");
   1836 		return ARCHIVE_FATAL;
   1837 	}
   1838 
   1839 	zip->entry_uncompressed_limit -= s;
   1840 	if (!zip->cctx_valid || zip->aes_vendor != AES_VENDOR_AE_2)
   1841 		zip->entry_crc32 =
   1842 		    zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
   1843 	return (s);
   1844 }
   1845 
   1846 static int
   1847 archive_write_zip_finish_entry(struct archive_write *a)
   1848 {
   1849 	struct zip *zip = a->format_data;
   1850 	int ret;
   1851 #if defined(HAVE_BZLIB_H) || (defined(HAVE_ZSTD_H) && HAVE_ZSTD_compressStream) || HAVE_LZMA_H
   1852 	char finishing;
   1853 #endif
   1854 
   1855 	switch (zip->entry_compression) {
   1856 #ifdef HAVE_ZLIB_H
   1857 	case COMPRESSION_DEFLATE:
   1858 		for (;;) {
   1859 			size_t remainder;
   1860 
   1861 			ret = deflate(&zip->stream.deflate, Z_FINISH);
   1862 			if (ret == Z_STREAM_ERROR)
   1863 				return (ARCHIVE_FATAL);
   1864 			remainder = zip->len_buf - zip->stream.deflate.avail_out;
   1865 			if (zip->tctx_valid) {
   1866 				trad_enc_encrypt_update(&zip->tctx,
   1867 				    zip->buf, remainder, zip->buf, remainder);
   1868 			} else if (zip->cctx_valid) {
   1869 				size_t outl = remainder;
   1870 				ret = archive_encrypto_aes_ctr_update(
   1871 				    &zip->cctx, zip->buf, remainder,
   1872 				    zip->buf, &outl);
   1873 				if (ret < 0) {
   1874 					archive_set_error(&a->archive,
   1875 					    ARCHIVE_ERRNO_MISC,
   1876 					    "Failed to encrypt file");
   1877 					return (ARCHIVE_FAILED);
   1878 				}
   1879 				archive_hmac_sha1_update(&zip->hctx,
   1880 				    zip->buf, remainder);
   1881 			}
   1882 			ret = __archive_write_output(a, zip->buf, remainder);
   1883 			if (ret != ARCHIVE_OK)
   1884 			{
   1885 				deflateEnd(&zip->stream.deflate);
   1886 				return (ret);
   1887 			}
   1888 			zip->entry_compressed_written += remainder;
   1889 			zip->written_bytes += remainder;
   1890 			zip->stream.deflate.next_out = zip->buf;
   1891 			if (zip->stream.deflate.avail_out != 0)
   1892 				break;
   1893 			zip->stream.deflate.avail_out = (uInt)zip->len_buf;
   1894 		}
   1895 		deflateEnd(&zip->stream.deflate);
   1896 		break;
   1897 #endif
   1898 #ifdef HAVE_BZLIB_H
   1899 	case COMPRESSION_BZIP2:
   1900 		finishing = 1;
   1901 		do {
   1902 			size_t remainder;
   1903 
   1904 			ret = BZ2_bzCompress(&zip->stream.bzip2, BZ_FINISH);
   1905 			if (ret == BZ_STREAM_END)
   1906 				finishing = 0;
   1907 			else if (ret != BZ_RUN_OK && ret != BZ_FINISH_OK)
   1908 				return (ARCHIVE_FATAL);
   1909 			remainder = zip->len_buf - zip->stream.bzip2.avail_out;
   1910 			if (zip->tctx_valid) {
   1911 				trad_enc_encrypt_update(&zip->tctx,
   1912 				    zip->buf, remainder, zip->buf, remainder);
   1913 			} else if (zip->cctx_valid) {
   1914 				size_t outl = remainder;
   1915 				ret = archive_encrypto_aes_ctr_update(
   1916 				    &zip->cctx, zip->buf, remainder,
   1917 				    zip->buf, &outl);
   1918 				if (ret < 0) {
   1919 					archive_set_error(&a->archive,
   1920 					    ARCHIVE_ERRNO_MISC,
   1921 					    "Failed to encrypt file");
   1922 					return (ARCHIVE_FAILED);
   1923 				}
   1924 				archive_hmac_sha1_update(&zip->hctx,
   1925 				    zip->buf, remainder);
   1926 			}
   1927 			ret = __archive_write_output(a, zip->buf, remainder);
   1928 			if (ret != ARCHIVE_OK)
   1929 			{
   1930 				BZ2_bzCompressEnd(&zip->stream.bzip2);
   1931 				return (ret);
   1932 			}
   1933 			zip->entry_compressed_written += remainder;
   1934 			zip->written_bytes += remainder;
   1935 			zip->stream.bzip2.next_out = (char*)zip->buf;
   1936 			if (zip->stream.bzip2.avail_out != 0)
   1937 				finishing = 0;
   1938 			zip->stream.bzip2.avail_out = (unsigned int)zip->len_buf;
   1939 		} while (finishing);
   1940 		BZ2_bzCompressEnd(&zip->stream.bzip2);
   1941 		break;
   1942 #endif
   1943 #if defined(HAVE_ZSTD_H) && HAVE_ZSTD_compressStream
   1944 	case COMPRESSION_ZSTD:
   1945 		finishing = 1;
   1946 		do {
   1947 			size_t remainder;
   1948 
   1949 			size_t zret = ZSTD_endStream(zip->stream.zstd.context, &zip->stream.zstd.out);
   1950 			if (zret == 0)
   1951 				finishing = 0;
   1952 			else if (ZSTD_isError(zret))
   1953 				return (ARCHIVE_FATAL);
   1954 			remainder = zip->len_buf - (zip->stream.zstd.out.size - zip->stream.zstd.out.pos);
   1955 			if (zip->tctx_valid) {
   1956 				trad_enc_encrypt_update(&zip->tctx,
   1957 				    zip->buf, remainder, zip->buf, remainder);
   1958 			} else if (zip->cctx_valid) {
   1959 				size_t outl = remainder;
   1960 				ret = archive_encrypto_aes_ctr_update(
   1961 				    &zip->cctx, zip->buf, remainder,
   1962 				    zip->buf, &outl);
   1963 				if (ret < 0) {
   1964 					archive_set_error(&a->archive,
   1965 					    ARCHIVE_ERRNO_MISC,
   1966 					    "Failed to encrypt file");
   1967 					return (ARCHIVE_FAILED);
   1968 				}
   1969 				archive_hmac_sha1_update(&zip->hctx,
   1970 				    zip->buf, remainder);
   1971 			}
   1972 			ret = __archive_write_output(a, zip->buf, remainder);
   1973 			if (ret != ARCHIVE_OK)
   1974 			{
   1975 				ZSTD_freeCStream(zip->stream.zstd.context);
   1976 				return (ret);
   1977 			}
   1978 			zip->entry_compressed_written += remainder;
   1979 			zip->written_bytes += remainder;
   1980 			if (zip->stream.zstd.out.pos != zip->stream.zstd.out.size)
   1981 				finishing = 0;
   1982 			zip->stream.zstd.out.dst = zip->buf;
   1983 			zip->stream.zstd.out.size = zip->len_buf;
   1984 			zip->stream.zstd.out.pos = 0;
   1985 		} while (finishing);
   1986 		ZSTD_freeCStream(zip->stream.zstd.context);
   1987 		break;
   1988 #endif
   1989 #ifdef HAVE_LZMA_H
   1990 	/* XZ and LZMA share clean-up code */
   1991 	case COMPRESSION_LZMA:
   1992 	case COMPRESSION_XZ:
   1993 		finishing = 1;
   1994 		do {
   1995 			size_t remainder;
   1996 
   1997 			ret = lzma_code(&zip->stream.lzma.context, LZMA_FINISH);
   1998 			if (ret == LZMA_STREAM_END)
   1999 				finishing = 0;
   2000 			else if (ret == LZMA_MEM_ERROR)
   2001 				return (ARCHIVE_FATAL);
   2002 			remainder = zip->len_buf - zip->stream.lzma.context.avail_out;
   2003 			if (zip->tctx_valid) {
   2004 				trad_enc_encrypt_update(&zip->tctx,
   2005 				    zip->buf, remainder, zip->buf, remainder);
   2006 			} else if (zip->cctx_valid) {
   2007 				size_t outl = remainder;
   2008 				ret = archive_encrypto_aes_ctr_update(
   2009 				    &zip->cctx, zip->buf, remainder,
   2010 				    zip->buf, &outl);
   2011 				if (ret < 0) {
   2012 					archive_set_error(&a->archive,
   2013 					    ARCHIVE_ERRNO_MISC,
   2014 					    "Failed to encrypt file");
   2015 					return (ARCHIVE_FAILED);
   2016 				}
   2017 				archive_hmac_sha1_update(&zip->hctx,
   2018 				    zip->buf, remainder);
   2019 			}
   2020 			ret = __archive_write_output(a, zip->buf, remainder);
   2021 			if (ret != ARCHIVE_OK)
   2022 			{
   2023 				lzma_end(&zip->stream.lzma.context);
   2024 				return (ret);
   2025 			}
   2026 			zip->entry_compressed_written += remainder;
   2027 			zip->written_bytes += remainder;
   2028 			zip->stream.lzma.context.next_out = zip->buf;
   2029 			if (zip->stream.lzma.context.avail_out != 0)
   2030 				finishing = 0;
   2031 			zip->stream.lzma.context.avail_out = (unsigned int)zip->len_buf;
   2032 		} while (finishing);
   2033 		lzma_end(&zip->stream.lzma.context);
   2034 		break;
   2035 #endif
   2036 	default:
   2037 		break;
   2038 	}
   2039 	if (zip->hctx_valid) {
   2040 		uint8_t hmac[20];
   2041 		size_t hmac_len = 20;
   2042 
   2043 		archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
   2044 		ret = __archive_write_output(a, hmac, AUTH_CODE_SIZE);
   2045 		if (ret != ARCHIVE_OK)
   2046 			return (ret);
   2047 		zip->entry_compressed_written += AUTH_CODE_SIZE;
   2048 		zip->written_bytes += AUTH_CODE_SIZE;
   2049 	}
   2050 
   2051 	/* Write trailing data descriptor. */
   2052 	if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
   2053 		char d[24];
   2054 		memcpy(d, "PK\007\010", 4);
   2055 		if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
   2056 			archive_le32enc(d + 4, 0);/* no CRC.*/
   2057 		else
   2058 			archive_le32enc(d + 4, zip->entry_crc32);
   2059 		if (zip->entry_compressed_written > ZIP_4GB_MAX
   2060 		    || zip->entry_uncompressed_written > ZIP_4GB_MAX
   2061 		    || zip->flags & ZIP_FLAG_FORCE_ZIP64) {
   2062 			archive_le64enc(d + 8,
   2063 				(uint64_t)zip->entry_compressed_written);
   2064 			archive_le64enc(d + 16,
   2065 				(uint64_t)zip->entry_uncompressed_written);
   2066 			ret = __archive_write_output(a, d, 24);
   2067 			zip->written_bytes += 24;
   2068 		} else {
   2069 			archive_le32enc(d + 8,
   2070 				(uint32_t)zip->entry_compressed_written);
   2071 			archive_le32enc(d + 12,
   2072 				(uint32_t)zip->entry_uncompressed_written);
   2073 			ret = __archive_write_output(a, d, 16);
   2074 			zip->written_bytes += 16;
   2075 		}
   2076 		if (ret != ARCHIVE_OK)
   2077 			return (ARCHIVE_FATAL);
   2078 	}
   2079 
   2080 	/* UT timestamp: Info-Zip specifies that _only_ the mtime should
   2081 	 * be recorded here; ctime and atime are also included in the
   2082 	 * local file descriptor. */
   2083 	if (archive_entry_mtime_is_set(zip->entry)) {
   2084 		unsigned char ut[9];
   2085 		unsigned char *u = ut, *ud;
   2086 		memcpy(u, "UT\005\000\001", 5);
   2087 		u += 5;
   2088 		archive_le32enc(u, (uint32_t)archive_entry_mtime(zip->entry));
   2089 		u += 4;
   2090 		ud = cd_alloc(zip, u - ut);
   2091 		if (ud == NULL) {
   2092 			archive_set_error(&a->archive, ENOMEM,
   2093 					  "Can't allocate zip data");
   2094 			return (ARCHIVE_FATAL);
   2095 		}
   2096 		memcpy(ud, ut, u - ut);
   2097 	}
   2098 
   2099 	/* Fill in size information in the central directory entry. */
   2100 	/* Fix up central directory file header. */
   2101 	if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
   2102 		archive_le32enc(zip->file_header + 16, 0);/* no CRC.*/
   2103 	else
   2104 		archive_le32enc(zip->file_header + 16, zip->entry_crc32);
   2105 	/* Truncate to 32 bits; we'll fix up below. */
   2106 	archive_le32enc(zip->file_header + 20, (uint32_t)zip->entry_compressed_written);
   2107 	archive_le32enc(zip->file_header + 24, (uint32_t)zip->entry_uncompressed_written);
   2108 	archive_le16enc(zip->file_header + 30,
   2109 	    (uint16_t)(zip->central_directory_bytes - zip->file_header_extra_offset));
   2110 	archive_le32enc(zip->file_header + 42, (uint32_t)zip->entry_offset);
   2111 
   2112 	/* If any of the values immediately above are too large, we'll
   2113 	 * need to put the corresponding value in a Zip64 extra field
   2114 	 * and set the central directory value to 0xffffffff as a flag. */
   2115 	if (zip->entry_compressed_written >= ZIP_4GB_MAX
   2116 	    || zip->entry_uncompressed_written >= ZIP_4GB_MAX
   2117 	    || zip->entry_offset > ZIP_4GB_MAX) {
   2118 		unsigned char zip64[32];
   2119 		unsigned char *z = zip64, *zd;
   2120 		memcpy(z, "\001\000\000\000", 4);
   2121 		z += 4;
   2122 		if (zip->entry_uncompressed_written >= ZIP_4GB_MAX) {
   2123 			archive_le32enc(zip->file_header + 24, ZIP_4GB_MAX);
   2124 			archive_le64enc(z, zip->entry_uncompressed_written);
   2125 			z += 8;
   2126 		}
   2127 		if (zip->entry_compressed_written >= ZIP_4GB_MAX) {
   2128 			archive_le32enc(zip->file_header + 20, ZIP_4GB_MAX);
   2129 			archive_le64enc(z, zip->entry_compressed_written);
   2130 			z += 8;
   2131 		}
   2132 		if (zip->entry_offset >= ZIP_4GB_MAX) {
   2133 			archive_le32enc(zip->file_header + 42, ZIP_4GB_MAX);
   2134 			archive_le64enc(z, zip->entry_offset);
   2135 			z += 8;
   2136 		}
   2137 		archive_le16enc(zip64 + 2, (uint16_t)(z - (zip64 + 4)));
   2138 		zd = cd_alloc(zip, z - zip64);
   2139 		if (zd == NULL) {
   2140 			archive_set_error(&a->archive, ENOMEM,
   2141 				"Can't allocate zip data");
   2142 			return (ARCHIVE_FATAL);
   2143 		}
   2144 		memcpy(zd, zip64, z - zip64);
   2145 		/* Zip64 means version needs to be set to at least 4.5 */
   2146 		if (archive_le16dec(zip->file_header + 6) < 45)
   2147 			archive_le16enc(zip->file_header + 6, 45);
   2148 	}
   2149 
   2150 	/* Fix up central directory file header. */
   2151 	if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
   2152 		archive_le32enc(zip->file_header + 16, 0);/* no CRC.*/
   2153 	else
   2154 		archive_le32enc(zip->file_header + 16, zip->entry_crc32);
   2155 	archive_le32enc(zip->file_header + 20,
   2156 		(uint32_t)zipmin(zip->entry_compressed_written,
   2157 				 ZIP_4GB_MAX));
   2158 	archive_le32enc(zip->file_header + 24,
   2159 		(uint32_t)zipmin(zip->entry_uncompressed_written,
   2160 				 ZIP_4GB_MAX));
   2161 	archive_le16enc(zip->file_header + 30,
   2162 	    (uint16_t)(zip->central_directory_bytes - zip->file_header_extra_offset));
   2163 	archive_le32enc(zip->file_header + 42,
   2164 		(uint32_t)zipmin(zip->entry_offset,
   2165 				 ZIP_4GB_MAX));
   2166 
   2167 	return (ARCHIVE_OK);
   2168 }
   2169 
   2170 static int
   2171 archive_write_zip_close(struct archive_write *a)
   2172 {
   2173 	uint8_t buff[64];
   2174 	int64_t offset_start, offset_end;
   2175 	struct zip *zip = a->format_data;
   2176 	struct cd_segment *segment;
   2177 	int ret;
   2178 
   2179 	offset_start = zip->written_bytes;
   2180 	segment = zip->central_directory;
   2181 	while (segment != NULL) {
   2182 		ret = __archive_write_output(a,
   2183 		    segment->buff, segment->p - segment->buff);
   2184 		if (ret != ARCHIVE_OK)
   2185 			return (ARCHIVE_FATAL);
   2186 		zip->written_bytes += segment->p - segment->buff;
   2187 		segment = segment->next;
   2188 	}
   2189 	offset_end = zip->written_bytes;
   2190 
   2191 	/* If central dir info is too large, write Zip64 end-of-cd */
   2192 	if (offset_end - offset_start > ZIP_4GB_MAX
   2193 	    || offset_start > ZIP_4GB_MAX
   2194 	    || zip->central_directory_entries > 0xffffUL
   2195 	    || (zip->flags & ZIP_FLAG_FORCE_ZIP64)) {
   2196 	  /* Zip64 end-of-cd record */
   2197 	  memset(buff, 0, 56);
   2198 	  memcpy(buff, "PK\006\006", 4);
   2199 	  archive_le64enc(buff + 4, 44);
   2200 	  archive_le16enc(buff + 12, 45);
   2201 	  archive_le16enc(buff + 14, 45);
   2202 	  /* This is disk 0 of 0. */
   2203 	  archive_le64enc(buff + 24, zip->central_directory_entries);
   2204 	  archive_le64enc(buff + 32, zip->central_directory_entries);
   2205 	  archive_le64enc(buff + 40, offset_end - offset_start);
   2206 	  archive_le64enc(buff + 48, offset_start);
   2207 	  ret = __archive_write_output(a, buff, 56);
   2208 	  if (ret != ARCHIVE_OK)
   2209 		  return (ARCHIVE_FATAL);
   2210 	  zip->written_bytes += 56;
   2211 
   2212 	  /* Zip64 end-of-cd locator record. */
   2213 	  memset(buff, 0, 20);
   2214 	  memcpy(buff, "PK\006\007", 4);
   2215 	  archive_le32enc(buff + 4, 0);
   2216 	  archive_le64enc(buff + 8, offset_end);
   2217 	  archive_le32enc(buff + 16, 1);
   2218 	  ret = __archive_write_output(a, buff, 20);
   2219 	  if (ret != ARCHIVE_OK)
   2220 		  return (ARCHIVE_FATAL);
   2221 	  zip->written_bytes += 20;
   2222 	}
   2223 
   2224 	/* Format and write end of central directory. */
   2225 	memset(buff, 0, sizeof(buff));
   2226 	memcpy(buff, "PK\005\006", 4);
   2227 	archive_le16enc(buff + 8, (uint16_t)zipmin(0xffffU,
   2228 		zip->central_directory_entries));
   2229 	archive_le16enc(buff + 10, (uint16_t)zipmin(0xffffU,
   2230 		zip->central_directory_entries));
   2231 	archive_le32enc(buff + 12,
   2232 		(uint32_t)zipmin(ZIP_4GB_MAX, (offset_end - offset_start)));
   2233 	archive_le32enc(buff + 16,
   2234 		(uint32_t)zipmin(ZIP_4GB_MAX, offset_start));
   2235 	ret = __archive_write_output(a, buff, 22);
   2236 	if (ret != ARCHIVE_OK)
   2237 		return (ARCHIVE_FATAL);
   2238 	zip->written_bytes += 22;
   2239 	return (ARCHIVE_OK);
   2240 }
   2241 
   2242 static int
   2243 archive_write_zip_free(struct archive_write *a)
   2244 {
   2245 	struct zip *zip;
   2246 	struct cd_segment *segment;
   2247 
   2248 	zip = a->format_data;
   2249 	while (zip->central_directory != NULL) {
   2250 		segment = zip->central_directory;
   2251 		zip->central_directory = segment->next;
   2252 		free(segment->buff);
   2253 		free(segment);
   2254 	}
   2255 	free(zip->buf);
   2256 	archive_entry_free(zip->entry);
   2257 	if (zip->cctx_valid)
   2258 		archive_encrypto_aes_ctr_release(&zip->cctx);
   2259 	if (zip->hctx_valid)
   2260 		archive_hmac_sha1_cleanup(&zip->hctx);
   2261 	/* TODO: Free opt_sconv, sconv_default */
   2262 
   2263 	free(zip);
   2264 	a->format_data = NULL;
   2265 	return (ARCHIVE_OK);
   2266 }
   2267 
   2268 static size_t
   2269 path_length(struct archive_entry *entry)
   2270 {
   2271 	mode_t type;
   2272 	const char *path;
   2273 	size_t len;
   2274 
   2275 	type = archive_entry_filetype(entry);
   2276 	path = archive_entry_pathname(entry);
   2277 
   2278 	if (path == NULL)
   2279 		return (0);
   2280 	len = strlen(path);
   2281 	if (type == AE_IFDIR && (path[0] == '\0' || path[len - 1] != '/'))
   2282 		++len; /* Space for the trailing / */
   2283 	return len;
   2284 }
   2285 
   2286 static int
   2287 write_path(struct archive_entry *entry, struct archive_write *archive)
   2288 {
   2289 	int ret;
   2290 	const char *path;
   2291 	mode_t type;
   2292 	size_t written_bytes;
   2293 
   2294 	path = archive_entry_pathname(entry);
   2295 	type = archive_entry_filetype(entry);
   2296 	written_bytes = 0;
   2297 
   2298 	if (path == NULL)
   2299 		return (ARCHIVE_FATAL);
   2300 
   2301 	ret = __archive_write_output(archive, path, strlen(path));
   2302 	if (ret != ARCHIVE_OK)
   2303 		return (ARCHIVE_FATAL);
   2304 	written_bytes += strlen(path);
   2305 
   2306 	/* Folders are recognized by a trailing slash. */
   2307 	if ((type == AE_IFDIR) & (path[strlen(path) - 1] != '/')) {
   2308 		ret = __archive_write_output(archive, "/", 1);
   2309 		if (ret != ARCHIVE_OK)
   2310 			return (ARCHIVE_FATAL);
   2311 		written_bytes += 1;
   2312 	}
   2313 
   2314 	return ((int)written_bytes);
   2315 }
   2316 
   2317 static void
   2318 copy_path(struct archive_entry *entry, unsigned char *p)
   2319 {
   2320 	const char *path;
   2321 	size_t pathlen;
   2322 	mode_t type;
   2323 
   2324 	path = archive_entry_pathname(entry);
   2325 	pathlen = strlen(path);
   2326 	type = archive_entry_filetype(entry);
   2327 
   2328 	memcpy(p, path, pathlen);
   2329 
   2330 	/* Folders are recognized by a trailing slash. */
   2331 	if ((type == AE_IFDIR) && (path[pathlen - 1] != '/'))
   2332 		p[pathlen] = '/';
   2333 }
   2334 
   2335 static struct archive_string_conv *
   2336 get_sconv(struct archive_write *a, struct zip *zip)
   2337 {
   2338 	if (zip->opt_sconv != NULL)
   2339 		return (zip->opt_sconv);
   2340 
   2341 	if (!zip->init_default_conversion) {
   2342 		zip->sconv_default =
   2343 		    archive_string_default_conversion_for_write(&(a->archive));
   2344 		zip->init_default_conversion = 1;
   2345 	}
   2346 	return (zip->sconv_default);
   2347 }
   2348 
   2349 /*
   2350   Traditional PKWARE Decryption functions.
   2351  */
   2352 
   2353 static void
   2354 trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
   2355 {
   2356 	uint8_t t;
   2357 #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
   2358 
   2359 	ctx->keys[0] = CRC32(ctx->keys[0], c);
   2360 	ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
   2361 	t = (ctx->keys[1] >> 24) & 0xff;
   2362 	ctx->keys[2] = CRC32(ctx->keys[2], t);
   2363 #undef CRC32
   2364 }
   2365 
   2366 static uint8_t
   2367 trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
   2368 {
   2369 	unsigned temp = ctx->keys[2] | 2;
   2370 	return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
   2371 }
   2372 
   2373 static unsigned
   2374 trad_enc_encrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
   2375     size_t in_len, uint8_t *out, size_t out_len)
   2376 {
   2377 	unsigned i, max;
   2378 
   2379 	max = (unsigned)((in_len < out_len)? in_len: out_len);
   2380 
   2381 	for (i = 0; i < max; i++) {
   2382 		uint8_t t = in[i];
   2383 		out[i] = t ^ trad_enc_decrypt_byte(ctx);
   2384 		trad_enc_update_keys(ctx, t);
   2385 	}
   2386 	return i;
   2387 }
   2388 
   2389 static int
   2390 trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len)
   2391 {
   2392 	ctx->keys[0] = 305419896L;
   2393 	ctx->keys[1] = 591751049L;
   2394 	ctx->keys[2] = 878082192L;
   2395 
   2396 	for (;pw_len; --pw_len)
   2397 		trad_enc_update_keys(ctx, *pw++);
   2398 	return 0;
   2399 }
   2400 
   2401 static int
   2402 is_traditional_pkware_encryption_supported(void)
   2403 {
   2404 	uint8_t key[TRAD_HEADER_SIZE];
   2405 
   2406 	if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK)
   2407 		return (0);
   2408 	return (1);
   2409 }
   2410 
   2411 static int
   2412 init_traditional_pkware_encryption(struct archive_write *a)
   2413 {
   2414 	struct zip *zip = a->format_data;
   2415 	const char *passphrase;
   2416 	uint8_t key[TRAD_HEADER_SIZE];
   2417 	uint8_t key_encrypted[TRAD_HEADER_SIZE];
   2418 	int ret;
   2419 
   2420 	passphrase = __archive_write_get_passphrase(a);
   2421 	if (passphrase == NULL) {
   2422 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
   2423 		    "Encryption needs passphrase");
   2424 		return ARCHIVE_FAILED;
   2425 	}
   2426 	if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK) {
   2427 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
   2428 		    "Can't generate random number for encryption");
   2429 		return ARCHIVE_FATAL;
   2430 	}
   2431 	trad_enc_init(&zip->tctx, passphrase, strlen(passphrase));
   2432 	/* Set the last key code which will be used as a check code
   2433 	 * for verifying passphrase in decryption. */
   2434 	key[TRAD_HEADER_SIZE-1] = zip->trad_chkdat;
   2435 	trad_enc_encrypt_update(&zip->tctx, key, TRAD_HEADER_SIZE,
   2436 	    key_encrypted, TRAD_HEADER_SIZE);
   2437 	/* Write encrypted keys in the top of the file content. */
   2438 	ret = __archive_write_output(a, key_encrypted, TRAD_HEADER_SIZE);
   2439 	if (ret != ARCHIVE_OK)
   2440 		return (ret);
   2441 	zip->written_bytes += TRAD_HEADER_SIZE;
   2442 	zip->entry_compressed_written += TRAD_HEADER_SIZE;
   2443 	return (ret);
   2444 }
   2445 
   2446 static int
   2447 init_winzip_aes_encryption(struct archive_write *a)
   2448 {
   2449 	struct zip *zip = a->format_data;
   2450 	const char *passphrase;
   2451 	size_t key_len, salt_len;
   2452 	uint8_t salt[16 + 2];
   2453 	uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
   2454 	int ret;
   2455 
   2456 	passphrase = __archive_write_get_passphrase(a);
   2457 	if (passphrase == NULL) {
   2458 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
   2459 		    "Encryption needs passphrase");
   2460 		return (ARCHIVE_FAILED);
   2461 	}
   2462 	if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128) {
   2463 		salt_len = 8;
   2464 		key_len = 16;
   2465 	} else {
   2466 		/* AES 256 */
   2467 		salt_len = 16;
   2468 		key_len = 32;
   2469 	}
   2470 	if (archive_random(salt, salt_len) != ARCHIVE_OK) {
   2471 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
   2472 		    "Can't generate random number for encryption");
   2473 		return (ARCHIVE_FATAL);
   2474 	}
   2475 	ret = archive_pbkdf2_sha1(passphrase, strlen(passphrase),
   2476 	    salt, salt_len, 1000, derived_key, key_len * 2 + 2);
   2477 	if (ret != 0) {
   2478 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
   2479 		    ret == CRYPTOR_STUB_FUNCTION ? "Encryption is unsupported due to "
   2480 			"lack of crypto library" : "Failed to process passphrase");
   2481 		return (ARCHIVE_FAILED);
   2482 	}
   2483 
   2484 	ret = archive_encrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
   2485 	if (ret != 0) {
   2486 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
   2487 		    "Failed to initialize AES CTR mode");
   2488 		return (ARCHIVE_FAILED);
   2489 	}
   2490 	ret = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len,
   2491 	    key_len);
   2492 	if (ret != 0) {
   2493 		archive_encrypto_aes_ctr_release(&zip->cctx);
   2494 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
   2495 		    "Failed to initialize HMAC-SHA1");
   2496 		return (ARCHIVE_FAILED);
   2497         }
   2498 
   2499 	/* Set a password verification value after the 'salt'. */
   2500 	salt[salt_len] = derived_key[key_len * 2];
   2501 	salt[salt_len + 1] = derived_key[key_len * 2 + 1];
   2502 
   2503 	/* Write encrypted keys in the top of the file content. */
   2504 	ret = __archive_write_output(a, salt, salt_len + 2);
   2505 	if (ret != ARCHIVE_OK)
   2506 		return (ret);
   2507 	zip->written_bytes += salt_len + 2;
   2508 	zip->entry_compressed_written += salt_len + 2;
   2509 
   2510 	return (ARCHIVE_OK);
   2511 }
   2512 
   2513 static int
   2514 is_winzip_aes_encryption_supported(int encryption)
   2515 {
   2516 	size_t key_len, salt_len;
   2517 	uint8_t salt[16 + 2];
   2518 	uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
   2519 	archive_crypto_ctx cctx;
   2520 	archive_hmac_sha1_ctx hctx;
   2521 	int ret;
   2522 
   2523 	if (encryption == ENCRYPTION_WINZIP_AES128) {
   2524 		salt_len = 8;
   2525 		key_len = 16;
   2526 	} else {
   2527 		/* AES 256 */
   2528 		salt_len = 16;
   2529 		key_len = 32;
   2530 	}
   2531 	if (archive_random(salt, salt_len) != ARCHIVE_OK)
   2532 		return (0);
   2533 	ret = archive_pbkdf2_sha1("p", 1, salt, salt_len, 1000,
   2534 	    derived_key, key_len * 2 + 2);
   2535 	if (ret != 0)
   2536 		return (0);
   2537 
   2538 	ret = archive_encrypto_aes_ctr_init(&cctx, derived_key, key_len);
   2539 	if (ret != 0)
   2540 		return (0);
   2541 	ret = archive_hmac_sha1_init(&hctx, derived_key + key_len,
   2542 	    key_len);
   2543 	archive_encrypto_aes_ctr_release(&cctx);
   2544 	if (ret != 0)
   2545 		return (0);
   2546 	archive_hmac_sha1_cleanup(&hctx);
   2547 	return (1);
   2548 }
   2549