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 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_zip.c 201168 2009-12-29 06:15:32Z kientzle $");
34 
35 #ifdef HAVE_ERRNO_H
36 #include <errno.h>
37 #endif
38 #ifdef HAVE_LANGINFO_H
39 #include <langinfo.h>
40 #endif
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
47 #ifdef HAVE_ZLIB_H
48 #include <zlib.h>
49 #endif
50 
51 #include "archive.h"
52 #include "archive_cryptor_private.h"
53 #include "archive_endian.h"
54 #include "archive_entry.h"
55 #include "archive_entry_locale.h"
56 #include "archive_hmac_private.h"
57 #include "archive_private.h"
58 #include "archive_random_private.h"
59 #include "archive_write_private.h"
60 #include "archive_write_set_format_private.h"
61 
62 #ifndef HAVE_ZLIB_H
63 #include "archive_crc32.h"
64 #endif
65 
66 #define ZIP_ENTRY_FLAG_ENCRYPTED	(1<<0)
67 #define ZIP_ENTRY_FLAG_LENGTH_AT_END	(1<<3)
68 #define ZIP_ENTRY_FLAG_UTF8_NAME	(1 << 11)
69 
70 #define ZIP_4GB_MAX ARCHIVE_LITERAL_LL(0xffffffff)
71 #define ZIP_4GB_MAX_UNCOMPRESSED ARCHIVE_LITERAL_LL(0xff000000)
72 
73 enum compression {
74 	COMPRESSION_UNSPECIFIED = -1,
75 	COMPRESSION_STORE = 0,
76 	COMPRESSION_DEFLATE = 8
77 };
78 
79 #ifdef HAVE_ZLIB_H
80 #define COMPRESSION_DEFAULT	COMPRESSION_DEFLATE
81 #else
82 #define COMPRESSION_DEFAULT	COMPRESSION_STORE
83 #endif
84 
85 enum encryption {
86 	ENCRYPTION_NONE	= 0,
87 	ENCRYPTION_TRADITIONAL, /* Traditional PKWARE encryption. */
88 	ENCRYPTION_WINZIP_AES128, /* WinZIP AES-128 encryption. */
89 	ENCRYPTION_WINZIP_AES256, /* WinZIP AES-256 encryption. */
90 };
91 
92 #define TRAD_HEADER_SIZE	12
93 /*
94  * See "WinZip - AES Encryption Information"
95  *     http://www.winzip.com/aes_info.htm
96  */
97 /* Value used in compression method. */
98 #define WINZIP_AES_ENCRYPTION	99
99 /* A WinZip AES header size which is stored at the beginning of
100  * file contents. */
101 #define WINZIP_AES128_HEADER_SIZE	(8 + 2)
102 #define WINZIP_AES256_HEADER_SIZE	(16 + 2)
103 /* AES vendor version. */
104 #define AES_VENDOR_AE_1 0x0001
105 #define AES_VENDOR_AE_2 0x0002
106 /* Authentication code size. */
107 #define AUTH_CODE_SIZE		10
108 /**/
109 #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
110 
111 struct cd_segment {
112 	struct cd_segment *next;
113 	size_t buff_size;
114 	unsigned char *buff;
115 	unsigned char *p;
116 };
117 
118 struct trad_enc_ctx {
119 	uint32_t keys[3];
120 };
121 
122 struct zip {
123 
124 	int64_t entry_offset;
125 	int64_t entry_compressed_size;
126 	int64_t entry_uncompressed_size;
127 	int64_t entry_compressed_written;
128 	int64_t entry_uncompressed_written;
129 	int64_t entry_uncompressed_limit;
130 	struct archive_entry *entry;
131 	uint32_t entry_crc32;
132 	enum compression entry_compression;
133 	enum encryption  entry_encryption;
134 	int entry_flags;
135 	int entry_uses_zip64;
136 	int experiments;
137 	struct trad_enc_ctx tctx;
138 	char tctx_valid;
139 	unsigned char trad_chkdat;
140 	unsigned aes_vendor;
141 	archive_crypto_ctx cctx;
142 	char cctx_valid;
143 	archive_hmac_sha1_ctx hctx;
144 	char hctx_valid;
145 
146 	unsigned char *file_header;
147 	size_t file_header_extra_offset;
148 	unsigned long (*crc32func)(unsigned long crc, const void *buff, size_t len);
149 
150 	struct cd_segment *central_directory;
151 	struct cd_segment *central_directory_last;
152 	size_t central_directory_bytes;
153 	size_t central_directory_entries;
154 
155 	int64_t written_bytes; /* Overall position in file. */
156 
157 	struct archive_string_conv *opt_sconv;
158 	struct archive_string_conv *sconv_default;
159 	enum compression requested_compression;
160 	int deflate_compression_level;
161 	int init_default_conversion;
162 	enum encryption  encryption_type;
163 
164 #define ZIP_FLAG_AVOID_ZIP64 1
165 #define ZIP_FLAG_FORCE_ZIP64 2
166 #define ZIP_FLAG_EXPERIMENT_xl 4
167 	int flags;
168 
169 #ifdef HAVE_ZLIB_H
170 	z_stream stream;
171 #endif
172 	size_t len_buf;
173 	unsigned char *buf;
174 };
175 
176 /* Don't call this min or MIN, since those are already defined
177    on lots of platforms (but not all). */
178 #define zipmin(a, b) ((a) > (b) ? (b) : (a))
179 
180 static ssize_t archive_write_zip_data(struct archive_write *,
181 		   const void *buff, size_t s);
182 static int archive_write_zip_close(struct archive_write *);
183 static int archive_write_zip_free(struct archive_write *);
184 static int archive_write_zip_finish_entry(struct archive_write *);
185 static int archive_write_zip_header(struct archive_write *,
186 	      struct archive_entry *);
187 static int archive_write_zip_options(struct archive_write *,
188 	      const char *, const char *);
189 static unsigned int dos_time(const time_t);
190 static size_t path_length(struct archive_entry *);
191 static int write_path(struct archive_entry *, struct archive_write *);
192 static void copy_path(struct archive_entry *, unsigned char *);
193 static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
194 static int trad_enc_init(struct trad_enc_ctx *, const char *, size_t);
195 static unsigned trad_enc_encrypt_update(struct trad_enc_ctx *, const uint8_t *,
196     size_t, uint8_t *, size_t);
197 static int init_traditional_pkware_encryption(struct archive_write *);
198 static int is_traditional_pkware_encryption_supported(void);
199 static int init_winzip_aes_encryption(struct archive_write *);
200 static int is_winzip_aes_encryption_supported(int encryption);
201 
202 static unsigned char *
203 cd_alloc(struct zip *zip, size_t length)
204 {
205 	unsigned char *p;
206 
207 	if (zip->central_directory == NULL
208 	    || (zip->central_directory_last->p + length
209 		> zip->central_directory_last->buff + zip->central_directory_last->buff_size)) {
210 		struct cd_segment *segment = calloc(1, sizeof(*segment));
211 		if (segment == NULL)
212 			return NULL;
213 		segment->buff_size = 64 * 1024;
214 		segment->buff = malloc(segment->buff_size);
215 		if (segment->buff == NULL) {
216 			free(segment);
217 			return NULL;
218 		}
219 		segment->p = segment->buff;
220 
221 		if (zip->central_directory == NULL) {
222 			zip->central_directory
223 			    = zip->central_directory_last
224 			    = segment;
225 		} else {
226 			zip->central_directory_last->next = segment;
227 			zip->central_directory_last = segment;
228 		}
229 	}
230 
231 	p = zip->central_directory_last->p;
232 	zip->central_directory_last->p += length;
233 	zip->central_directory_bytes += length;
234 	return (p);
235 }
236 
237 static unsigned long
238 real_crc32(unsigned long crc, const void *buff, size_t len)
239 {
240 	return crc32(crc, buff, (unsigned int)len);
241 }
242 
243 static unsigned long
244 fake_crc32(unsigned long crc, const void *buff, size_t len)
245 {
246 	(void)crc; /* UNUSED */
247 	(void)buff; /* UNUSED */
248 	(void)len; /* UNUSED */
249 	return 0;
250 }
251 
252 static int
253 archive_write_zip_options(struct archive_write *a, const char *key,
254     const char *val)
255 {
256 	struct zip *zip = a->format_data;
257 	int ret = ARCHIVE_FAILED;
258 
259 	if (strcmp(key, "compression") == 0) {
260 		/*
261 		 * Set compression to use on all future entries.
262 		 * This only affects regular files.
263 		 */
264 		if (val == NULL || val[0] == 0) {
265 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
266 			    "%s: compression option needs a compression name",
267 			    a->format_name);
268 		} else if (strcmp(val, "deflate") == 0) {
269 #ifdef HAVE_ZLIB_H
270 			zip->requested_compression = COMPRESSION_DEFLATE;
271 			ret = ARCHIVE_OK;
272 #else
273 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
274 			    "deflate compression not supported");
275 #endif
276 		} else if (strcmp(val, "store") == 0) {
277 			zip->requested_compression = COMPRESSION_STORE;
278 			ret = ARCHIVE_OK;
279 		}
280 		return (ret);
281 	} else if (strcmp(key, "compression-level") == 0) {
282 		if (val == NULL || !(val[0] >= '0' && val[0] <= '9') || val[1] != '\0') {
283 			return ARCHIVE_WARN;
284 		}
285 
286 		if (val[0] == '0') {
287 			zip->requested_compression = COMPRESSION_STORE;
288 			return ARCHIVE_OK;
289 		} else {
290 #ifdef HAVE_ZLIB_H
291 			zip->requested_compression = COMPRESSION_DEFLATE;
292 			zip->deflate_compression_level = val[0] - '0';
293 			return ARCHIVE_OK;
294 #else
295 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
296 			    "deflate compression not supported");
297 #endif
298 		}
299 	} else if (strcmp(key, "encryption") == 0) {
300 		if (val == NULL) {
301 			zip->encryption_type = ENCRYPTION_NONE;
302 			ret = ARCHIVE_OK;
303 		} else if (val[0] == '1' || strcmp(val, "traditional") == 0
304 		    || strcmp(val, "zipcrypt") == 0
305 		    || strcmp(val, "ZipCrypt") == 0) {
306 			if (is_traditional_pkware_encryption_supported()) {
307 				zip->encryption_type = ENCRYPTION_TRADITIONAL;
308 				ret = ARCHIVE_OK;
309 			} else {
310 				archive_set_error(&a->archive,
311 				    ARCHIVE_ERRNO_MISC,
312 				    "encryption not supported");
313 			}
314 		} else if (strcmp(val, "aes128") == 0) {
315 			if (is_winzip_aes_encryption_supported(
316 			    ENCRYPTION_WINZIP_AES128)) {
317 				zip->encryption_type = ENCRYPTION_WINZIP_AES128;
318 				ret = ARCHIVE_OK;
319 			} else {
320 				archive_set_error(&a->archive,
321 				    ARCHIVE_ERRNO_MISC,
322 				    "encryption not supported");
323 			}
324 		} else if (strcmp(val, "aes256") == 0) {
325 			if (is_winzip_aes_encryption_supported(
326 			    ENCRYPTION_WINZIP_AES256)) {
327 				zip->encryption_type = ENCRYPTION_WINZIP_AES256;
328 				ret = ARCHIVE_OK;
329 			} else {
330 				archive_set_error(&a->archive,
331 				    ARCHIVE_ERRNO_MISC,
332 				    "encryption not supported");
333 			}
334 		} else {
335 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
336 			    "%s: unknown encryption '%s'",
337 			    a->format_name, val);
338 		}
339 		return (ret);
340 	} else if (strcmp(key, "experimental") == 0) {
341 		if (val == NULL || val[0] == 0) {
342 			zip->flags &= ~ ZIP_FLAG_EXPERIMENT_xl;
343 		} else {
344 			zip->flags |= ZIP_FLAG_EXPERIMENT_xl;
345 		}
346 		return (ARCHIVE_OK);
347 	} else if (strcmp(key, "fakecrc32") == 0) {
348 		/*
349 		 * FOR TESTING ONLY:  disable CRC calculation to speed up
350 		 * certain complex tests.
351 		 */
352 		if (val == NULL || val[0] == 0) {
353 			zip->crc32func = real_crc32;
354 		} else {
355 			zip->crc32func = fake_crc32;
356 		}
357 		return (ARCHIVE_OK);
358 	} else if (strcmp(key, "hdrcharset")  == 0) {
359 		/*
360 		 * Set the character set used in translating filenames.
361 		 */
362 		if (val == NULL || val[0] == 0) {
363 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
364 			    "%s: hdrcharset option needs a character-set name",
365 			    a->format_name);
366 		} else {
367 			zip->opt_sconv = archive_string_conversion_to_charset(
368 			    &a->archive, val, 0);
369 			if (zip->opt_sconv != NULL)
370 				ret = ARCHIVE_OK;
371 			else
372 				ret = ARCHIVE_FATAL;
373 		}
374 		return (ret);
375 	} else if (strcmp(key, "zip64") == 0) {
376 		/*
377 		 * Bias decisions about Zip64: force them to be
378 		 * generated in certain cases where they are not
379 		 * forbidden or avoid them in certain cases where they
380 		 * are not strictly required.
381 		 */
382 		if (val != NULL && *val != '\0') {
383 			zip->flags |= ZIP_FLAG_FORCE_ZIP64;
384 			zip->flags &= ~ZIP_FLAG_AVOID_ZIP64;
385 		} else {
386 			zip->flags &= ~ZIP_FLAG_FORCE_ZIP64;
387 			zip->flags |= ZIP_FLAG_AVOID_ZIP64;
388 		}
389 		return (ARCHIVE_OK);
390 	}
391 
392 	/* Note: The "warn" return is just to inform the options
393 	 * supervisor that we didn't handle it.  It will generate
394 	 * a suitable error if no one used this option. */
395 	return (ARCHIVE_WARN);
396 }
397 
398 int
399 archive_write_zip_set_compression_deflate(struct archive *_a)
400 {
401 	struct archive_write *a = (struct archive_write *)_a;
402 	int ret = ARCHIVE_FAILED;
403 
404 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
405 		ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
406 		"archive_write_zip_set_compression_deflate");
407 	if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
408 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
409 		"Can only use archive_write_zip_set_compression_deflate"
410 		" with zip format");
411 		ret = ARCHIVE_FATAL;
412 	} else {
413 #ifdef HAVE_ZLIB_H
414 		struct zip *zip = a->format_data;
415 		zip->requested_compression = COMPRESSION_DEFLATE;
416 		ret = ARCHIVE_OK;
417 #else
418 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
419 			"deflate compression not supported");
420 		ret = ARCHIVE_FAILED;
421 #endif
422 	}
423 	return (ret);
424 }
425 
426 int
427 archive_write_zip_set_compression_store(struct archive *_a)
428 {
429 	struct archive_write *a = (struct archive_write *)_a;
430 	struct zip *zip = a->format_data;
431 	int ret = ARCHIVE_FAILED;
432 
433 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
434 		ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
435 		"archive_write_zip_set_compression_deflate");
436 	if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
437 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
438 			"Can only use archive_write_zip_set_compression_store"
439 			" with zip format");
440 		ret = ARCHIVE_FATAL;
441 	} else {
442 		zip->requested_compression = COMPRESSION_STORE;
443 		ret = ARCHIVE_OK;
444 	}
445 	return (ret);
446 }
447 
448 int
449 archive_write_set_format_zip(struct archive *_a)
450 {
451 	struct archive_write *a = (struct archive_write *)_a;
452 	struct zip *zip;
453 
454 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
455 	    ARCHIVE_STATE_NEW, "archive_write_set_format_zip");
456 
457 	/* If another format was already registered, unregister it. */
458 	if (a->format_free != NULL)
459 		(a->format_free)(a);
460 
461 	zip = (struct zip *) calloc(1, sizeof(*zip));
462 	if (zip == NULL) {
463 		archive_set_error(&a->archive, ENOMEM,
464 		    "Can't allocate zip data");
465 		return (ARCHIVE_FATAL);
466 	}
467 
468 	/* "Unspecified" lets us choose the appropriate compression. */
469 	zip->requested_compression = COMPRESSION_UNSPECIFIED;
470 #ifdef HAVE_ZLIB_H
471 	zip->deflate_compression_level = Z_DEFAULT_COMPRESSION;
472 #endif
473 	zip->crc32func = real_crc32;
474 
475 	/* A buffer used for both compression and encryption. */
476 	zip->len_buf = 65536;
477 	zip->buf = malloc(zip->len_buf);
478 	if (zip->buf == NULL) {
479 		free(zip);
480 		archive_set_error(&a->archive, ENOMEM,
481 		    "Can't allocate compression buffer");
482 		return (ARCHIVE_FATAL);
483 	}
484 
485 	a->format_data = zip;
486 	a->format_name = "zip";
487 	a->format_options = archive_write_zip_options;
488 	a->format_write_header = archive_write_zip_header;
489 	a->format_write_data = archive_write_zip_data;
490 	a->format_finish_entry = archive_write_zip_finish_entry;
491 	a->format_close = archive_write_zip_close;
492 	a->format_free = archive_write_zip_free;
493 	a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
494 	a->archive.archive_format_name = "ZIP";
495 
496 	return (ARCHIVE_OK);
497 }
498 
499 static int
500 is_all_ascii(const char *p)
501 {
502 	const unsigned char *pp = (const unsigned char *)p;
503 
504 	while (*pp) {
505 		if (*pp++ > 127)
506 			return (0);
507 	}
508 	return (1);
509 }
510 
511 static int
512 archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
513 {
514 	unsigned char local_header[32];
515 	unsigned char local_extra[144];
516 	struct zip *zip = a->format_data;
517 	unsigned char *e;
518 	unsigned char *cd_extra;
519 	size_t filename_length;
520 	const char *slink = NULL;
521 	size_t slink_size = 0;
522 	struct archive_string_conv *sconv = get_sconv(a, zip);
523 	int ret, ret2 = ARCHIVE_OK;
524 	mode_t type;
525 	int version_needed = 10;
526 
527 	/* Ignore types of entries that we don't support. */
528 	type = archive_entry_filetype(entry);
529 	if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) {
530 		__archive_write_entry_filetype_unsupported(
531 		    &a->archive, entry, "zip");
532 		return ARCHIVE_FAILED;
533 	};
534 
535 	/* If we're not using Zip64, reject large files. */
536 	if (zip->flags & ZIP_FLAG_AVOID_ZIP64) {
537 		/* Reject entries over 4GB. */
538 		if (archive_entry_size_is_set(entry)
539 		    && (archive_entry_size(entry) > ZIP_4GB_MAX)) {
540 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
541 			    "Files > 4GB require Zip64 extensions");
542 			return ARCHIVE_FAILED;
543 		}
544 		/* Reject entries if archive is > 4GB. */
545 		if (zip->written_bytes > ZIP_4GB_MAX) {
546 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
547 			    "Archives > 4GB require Zip64 extensions");
548 			return ARCHIVE_FAILED;
549 		}
550 	}
551 
552 	/* Only regular files can have size > 0. */
553 	if (type != AE_IFREG)
554 		archive_entry_set_size(entry, 0);
555 
556 
557 	/* Reset information from last entry. */
558 	zip->entry_offset = zip->written_bytes;
559 	zip->entry_uncompressed_limit = INT64_MAX;
560 	zip->entry_compressed_size = 0;
561 	zip->entry_uncompressed_size = 0;
562 	zip->entry_compressed_written = 0;
563 	zip->entry_uncompressed_written = 0;
564 	zip->entry_flags = 0;
565 	zip->entry_uses_zip64 = 0;
566 	zip->entry_crc32 = zip->crc32func(0, NULL, 0);
567 	zip->entry_encryption = 0;
568 	archive_entry_free(zip->entry);
569 	zip->entry = NULL;
570 
571 	if (zip->cctx_valid)
572 		archive_encrypto_aes_ctr_release(&zip->cctx);
573 	if (zip->hctx_valid)
574 		archive_hmac_sha1_cleanup(&zip->hctx);
575 	zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
576 
577 	if (type == AE_IFREG
578 		    &&(!archive_entry_size_is_set(entry)
579 			|| archive_entry_size(entry) > 0)) {
580 		switch (zip->encryption_type) {
581 		case ENCRYPTION_TRADITIONAL:
582 		case ENCRYPTION_WINZIP_AES128:
583 		case ENCRYPTION_WINZIP_AES256:
584 			zip->entry_flags |= ZIP_ENTRY_FLAG_ENCRYPTED;
585 			zip->entry_encryption = zip->encryption_type;
586 			break;
587 		default:
588 			break;
589 		}
590 	}
591 
592 
593 #if defined(_WIN32) && !defined(__CYGWIN__)
594 	/* Make sure the path separators in pathname, hardlink and symlink
595 	 * are all slash '/', not the Windows path separator '\'. */
596 	zip->entry = __la_win_entry_in_posix_pathseparator(entry);
597 	if (zip->entry == entry)
598 		zip->entry = archive_entry_clone(entry);
599 #else
600 	zip->entry = archive_entry_clone(entry);
601 #endif
602 	if (zip->entry == NULL) {
603 		archive_set_error(&a->archive, ENOMEM,
604 		    "Can't allocate zip header data");
605 		return (ARCHIVE_FATAL);
606 	}
607 
608 	if (sconv != NULL) {
609 		const char *p;
610 		size_t len;
611 
612 		if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) {
613 			if (errno == ENOMEM) {
614 				archive_set_error(&a->archive, ENOMEM,
615 				    "Can't allocate memory for Pathname");
616 				return (ARCHIVE_FATAL);
617 			}
618 			archive_set_error(&a->archive,
619 			    ARCHIVE_ERRNO_FILE_FORMAT,
620 			    "Can't translate Pathname '%s' to %s",
621 			    archive_entry_pathname(entry),
622 			    archive_string_conversion_charset_name(sconv));
623 			ret2 = ARCHIVE_WARN;
624 		}
625 		if (len > 0)
626 			archive_entry_set_pathname(zip->entry, p);
627 
628 		/*
629 		 * There is no standard for symlink handling; we convert
630 		 * it using the same character-set translation that we use
631 		 * for filename.
632 		 */
633 		if (type == AE_IFLNK) {
634 			if (archive_entry_symlink_l(entry, &p, &len, sconv)) {
635 				if (errno == ENOMEM) {
636 					archive_set_error(&a->archive, ENOMEM,
637 					    "Can't allocate memory "
638 					    " for Symlink");
639 					return (ARCHIVE_FATAL);
640 				}
641 				/* No error if we can't convert. */
642 			} else if (len > 0)
643 				archive_entry_set_symlink(zip->entry, p);
644 		}
645 	}
646 
647 	/* If filename isn't ASCII and we can use UTF-8, set the UTF-8 flag. */
648 	if (!is_all_ascii(archive_entry_pathname(zip->entry))) {
649 		if (zip->opt_sconv != NULL) {
650 			if (strcmp(archive_string_conversion_charset_name(
651 					zip->opt_sconv), "UTF-8") == 0)
652 				zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
653 #if HAVE_NL_LANGINFO
654 		} else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
655 			zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
656 #endif
657 		}
658 	}
659 	filename_length = path_length(zip->entry);
660 
661 	/* Determine appropriate compression and size for this entry. */
662 	if (type == AE_IFLNK) {
663 		slink = archive_entry_symlink(zip->entry);
664 		if (slink != NULL)
665 			slink_size = strlen(slink);
666 		else
667 			slink_size = 0;
668 		zip->entry_uncompressed_limit = slink_size;
669 		zip->entry_compressed_size = slink_size;
670 		zip->entry_uncompressed_size = slink_size;
671 		zip->entry_crc32 = zip->crc32func(zip->entry_crc32,
672 		    (const unsigned char *)slink, slink_size);
673 		zip->entry_compression = COMPRESSION_STORE;
674 		version_needed = 20;
675 	} else if (type != AE_IFREG) {
676 		zip->entry_compression = COMPRESSION_STORE;
677 		zip->entry_uncompressed_limit = 0;
678 		version_needed = 20;
679 	} else if (archive_entry_size_is_set(zip->entry)) {
680 		int64_t size = archive_entry_size(zip->entry);
681 		int64_t additional_size = 0;
682 
683 		zip->entry_uncompressed_limit = size;
684 		zip->entry_compression = zip->requested_compression;
685 		if (zip->entry_compression == COMPRESSION_UNSPECIFIED) {
686 			zip->entry_compression = COMPRESSION_DEFAULT;
687 		}
688 		if (zip->entry_compression == COMPRESSION_STORE) {
689 			zip->entry_compressed_size = size;
690 			zip->entry_uncompressed_size = size;
691 			version_needed = 10;
692 		} else {
693 			zip->entry_uncompressed_size = size;
694 			version_needed = 20;
695 		}
696 
697 		if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
698 			switch (zip->entry_encryption) {
699 			case ENCRYPTION_TRADITIONAL:
700 				additional_size = TRAD_HEADER_SIZE;
701 				version_needed = 20;
702 				break;
703 			case ENCRYPTION_WINZIP_AES128:
704 				additional_size = WINZIP_AES128_HEADER_SIZE
705 				    + AUTH_CODE_SIZE;
706 				version_needed = 20;
707 				break;
708 			case ENCRYPTION_WINZIP_AES256:
709 				additional_size = WINZIP_AES256_HEADER_SIZE
710 				    + AUTH_CODE_SIZE;
711 				version_needed = 20;
712 				break;
713 			default:
714 				break;
715 			}
716 			if (zip->entry_compression == COMPRESSION_STORE)
717 				zip->entry_compressed_size += additional_size;
718 		}
719 
720 		/*
721 		 * Set Zip64 extension in any of the following cases
722 		 * (this was suggested by discussion on info-zip-dev
723 		 * mailing list):
724 		 *  = Zip64 is being forced by user
725 		 *  = File is over 4GiB uncompressed
726 		 *    (including encryption header, if any)
727 		 *  = File is close to 4GiB and is being compressed
728 		 *    (compression might make file larger)
729 		 */
730 		if ((zip->flags & ZIP_FLAG_FORCE_ZIP64)
731 		    || (zip->entry_uncompressed_size + additional_size > ZIP_4GB_MAX)
732 		    || (zip->entry_uncompressed_size > ZIP_4GB_MAX_UNCOMPRESSED
733 			&& zip->entry_compression != COMPRESSION_STORE)) {
734 			zip->entry_uses_zip64 = 1;
735 			version_needed = 45;
736 		}
737 
738 		/* We may know the size, but never the CRC. */
739 		zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
740 	} else {
741 		/* We don't know the size.  In this case, we prefer
742 		 * deflate (it has a clear end-of-data marker which
743 		 * makes length-at-end more reliable) and will
744 		 * enable Zip64 extensions unless we're told not to.
745 		 */
746 		zip->entry_compression = COMPRESSION_DEFAULT;
747 		zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
748 		if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) {
749 			zip->entry_uses_zip64 = 1;
750 			version_needed = 45;
751 		} else if (zip->entry_compression == COMPRESSION_STORE) {
752 			version_needed = 10;
753 		} else {
754 			version_needed = 20;
755 		}
756 
757 		if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
758 			switch (zip->entry_encryption) {
759 			case ENCRYPTION_TRADITIONAL:
760 			case ENCRYPTION_WINZIP_AES128:
761 			case ENCRYPTION_WINZIP_AES256:
762 				if (version_needed < 20)
763 					version_needed = 20;
764 				break;
765 			default:
766 				break;
767 			}
768 		}
769 	}
770 
771 	/* Format the local header. */
772 	memset(local_header, 0, sizeof(local_header));
773 	memcpy(local_header, "PK\003\004", 4);
774 	archive_le16enc(local_header + 4, version_needed);
775 	archive_le16enc(local_header + 6, zip->entry_flags);
776 	if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
777 	    || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
778 		archive_le16enc(local_header + 8, WINZIP_AES_ENCRYPTION);
779 	else
780 		archive_le16enc(local_header + 8, zip->entry_compression);
781 	archive_le32enc(local_header + 10,
782 		dos_time(archive_entry_mtime(zip->entry)));
783 	archive_le32enc(local_header + 14, zip->entry_crc32);
784 	if (zip->entry_uses_zip64) {
785 		/* Zip64 data in the local header "must" include both
786 		 * compressed and uncompressed sizes AND those fields
787 		 * are included only if these are 0xffffffff;
788 		 * THEREFORE these must be set this way, even if we
789 		 * know one of them is smaller. */
790 		archive_le32enc(local_header + 18, ZIP_4GB_MAX);
791 		archive_le32enc(local_header + 22, ZIP_4GB_MAX);
792 	} else {
793 		archive_le32enc(local_header + 18, (uint32_t)zip->entry_compressed_size);
794 		archive_le32enc(local_header + 22, (uint32_t)zip->entry_uncompressed_size);
795 	}
796 	archive_le16enc(local_header + 26, (uint16_t)filename_length);
797 
798 	if (zip->entry_encryption == ENCRYPTION_TRADITIONAL) {
799 		if (zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END)
800 			zip->trad_chkdat = local_header[11];
801 		else
802 			zip->trad_chkdat = local_header[17];
803 	}
804 
805 	/* Format as much of central directory file header as we can: */
806 	zip->file_header = cd_alloc(zip, 46);
807 	/* If (zip->file_header == NULL) XXXX */
808 	++zip->central_directory_entries;
809 	memset(zip->file_header, 0, 46);
810 	memcpy(zip->file_header, "PK\001\002", 4);
811 	/* "Made by PKZip 2.0 on Unix." */
812 	archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed);
813 	archive_le16enc(zip->file_header + 6, version_needed);
814 	archive_le16enc(zip->file_header + 8, zip->entry_flags);
815 	if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
816 	    || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
817 		archive_le16enc(zip->file_header + 10, WINZIP_AES_ENCRYPTION);
818 	else
819 		archive_le16enc(zip->file_header + 10, zip->entry_compression);
820 	archive_le32enc(zip->file_header + 12,
821 		dos_time(archive_entry_mtime(zip->entry)));
822 	archive_le16enc(zip->file_header + 28, (uint16_t)filename_length);
823 	/* Following Info-Zip, store mode in the "external attributes" field. */
824 	archive_le32enc(zip->file_header + 38,
825 	    ((uint32_t)archive_entry_mode(zip->entry)) << 16);
826 	e = cd_alloc(zip, filename_length);
827 	/* If (e == NULL) XXXX */
828 	copy_path(zip->entry, e);
829 
830 	/* Format extra data. */
831 	memset(local_extra, 0, sizeof(local_extra));
832 	e = local_extra;
833 
834 	/* First, extra blocks that are the same between
835 	 * the local file header and the central directory.
836 	 * We format them once and then duplicate them. */
837 
838 	/* UT timestamp, length depends on what timestamps are set. */
839 	memcpy(e, "UT", 2);
840 	archive_le16enc(e + 2,
841 	    1
842 	    + (archive_entry_mtime_is_set(entry) ? 4 : 0)
843 	    + (archive_entry_atime_is_set(entry) ? 4 : 0)
844 	    + (archive_entry_ctime_is_set(entry) ? 4 : 0));
845 	e += 4;
846 	*e++ =
847 	    (archive_entry_mtime_is_set(entry) ? 1 : 0)
848 	    | (archive_entry_atime_is_set(entry) ? 2 : 0)
849 	    | (archive_entry_ctime_is_set(entry) ? 4 : 0);
850 	if (archive_entry_mtime_is_set(entry)) {
851 		archive_le32enc(e, (uint32_t)archive_entry_mtime(entry));
852 		e += 4;
853 	}
854 	if (archive_entry_atime_is_set(entry)) {
855 		archive_le32enc(e, (uint32_t)archive_entry_atime(entry));
856 		e += 4;
857 	}
858 	if (archive_entry_ctime_is_set(entry)) {
859 		archive_le32enc(e, (uint32_t)archive_entry_ctime(entry));
860 		e += 4;
861 	}
862 
863 	/* ux Unix extra data, length 11, version 1 */
864 	/* TODO: If uid < 64k, use 2 bytes, ditto for gid. */
865 	memcpy(e, "ux\013\000\001", 5);
866 	e += 5;
867 	*e++ = 4; /* Length of following UID */
868 	archive_le32enc(e, (uint32_t)archive_entry_uid(entry));
869 	e += 4;
870 	*e++ = 4; /* Length of following GID */
871 	archive_le32enc(e, (uint32_t)archive_entry_gid(entry));
872 	e += 4;
873 
874 	/* AES extra data field: WinZIP AES information, ID=0x9901 */
875 	if ((zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED)
876 	    && (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
877 	        || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)) {
878 
879 		memcpy(e, "\001\231\007\000\001\000AE", 8);
880 		/* AES vendor version AE-2 does not store a CRC.
881 		 * WinZip 11 uses AE-1, which does store the CRC,
882 		 * but it does not store the CRC when the file size
883 		 * is less than 20 bytes. So we simulate what
884 		 * WinZip 11 does.
885 		 * NOTE: WinZip 9.0 and 10.0 uses AE-2 by default. */
886 		if (archive_entry_size_is_set(zip->entry)
887 		    && archive_entry_size(zip->entry) < 20) {
888 			archive_le16enc(e+4, AES_VENDOR_AE_2);
889 			zip->aes_vendor = AES_VENDOR_AE_2;/* no CRC. */
890 		} else
891 			zip->aes_vendor = AES_VENDOR_AE_1;
892 		e += 8;
893 		/* AES encryption strength. */
894 		*e++ = (zip->entry_encryption == ENCRYPTION_WINZIP_AES128)?1:3;
895 		/* Actual compression method. */
896 		archive_le16enc(e, zip->entry_compression);
897 		e += 2;
898 	}
899 
900 	/* Copy UT ,ux, and AES-extra into central directory as well. */
901 	zip->file_header_extra_offset = zip->central_directory_bytes;
902 	cd_extra = cd_alloc(zip, e - local_extra);
903 	memcpy(cd_extra, local_extra, e - local_extra);
904 
905 	/*
906 	 * Following extra blocks vary between local header and
907 	 * central directory. These are the local header versions.
908 	 * Central directory versions get formatted in
909 	 * archive_write_zip_finish_entry() below.
910 	 */
911 
912 	/* "[Zip64 entry] in the local header MUST include BOTH
913 	 * original [uncompressed] and compressed size fields." */
914 	if (zip->entry_uses_zip64) {
915 		unsigned char *zip64_start = e;
916 		memcpy(e, "\001\000\020\000", 4);
917 		e += 4;
918 		archive_le64enc(e, zip->entry_uncompressed_size);
919 		e += 8;
920 		archive_le64enc(e, zip->entry_compressed_size);
921 		e += 8;
922 		archive_le16enc(zip64_start + 2, (uint16_t)(e - (zip64_start + 4)));
923 	}
924 
925 	if (zip->flags & ZIP_FLAG_EXPERIMENT_xl) {
926 		/* Experimental 'xl' extension to improve streaming. */
927 		unsigned char *external_info = e;
928 		int included = 7;
929 		memcpy(e, "xl\000\000", 4); // 0x6c65 + 2-byte length
930 		e += 4;
931 		e[0] = included; /* bitmap of included fields */
932 		e += 1;
933 		if (included & 1) {
934 			archive_le16enc(e, /* "Version created by" */
935 			    3 * 256 + version_needed);
936 			e += 2;
937 		}
938 		if (included & 2) {
939 			archive_le16enc(e, 0); /* internal file attributes */
940 			e += 2;
941 		}
942 		if (included & 4) {
943 			archive_le32enc(e,  /* external file attributes */
944 			    ((uint32_t)archive_entry_mode(zip->entry)) << 16);
945 			e += 4;
946 		}
947 		if (included & 8) {
948 			// Libarchive does not currently support file comments.
949 		}
950 		archive_le16enc(external_info + 2, (uint16_t)(e - (external_info + 4)));
951 	}
952 
953 	/* Update local header with size of extra data and write it all out: */
954 	archive_le16enc(local_header + 28, (uint16_t)(e - local_extra));
955 
956 	ret = __archive_write_output(a, local_header, 30);
957 	if (ret != ARCHIVE_OK)
958 		return (ARCHIVE_FATAL);
959 	zip->written_bytes += 30;
960 
961 	ret = write_path(zip->entry, a);
962 	if (ret <= ARCHIVE_OK)
963 		return (ARCHIVE_FATAL);
964 	zip->written_bytes += ret;
965 
966 	ret = __archive_write_output(a, local_extra, e - local_extra);
967 	if (ret != ARCHIVE_OK)
968 		return (ARCHIVE_FATAL);
969 	zip->written_bytes += e - local_extra;
970 
971 	/* For symlinks, write the body now. */
972 	if (slink != NULL) {
973 		ret = __archive_write_output(a, slink, slink_size);
974 		if (ret != ARCHIVE_OK)
975 			return (ARCHIVE_FATAL);
976 		zip->entry_compressed_written += slink_size;
977 		zip->entry_uncompressed_written += slink_size;
978 		zip->written_bytes += slink_size;
979 	}
980 
981 #ifdef HAVE_ZLIB_H
982 	if (zip->entry_compression == COMPRESSION_DEFLATE) {
983 		zip->stream.zalloc = Z_NULL;
984 		zip->stream.zfree = Z_NULL;
985 		zip->stream.opaque = Z_NULL;
986 		zip->stream.next_out = zip->buf;
987 		zip->stream.avail_out = (uInt)zip->len_buf;
988 		if (deflateInit2(&zip->stream, zip->deflate_compression_level,
989 		    Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
990 			archive_set_error(&a->archive, ENOMEM,
991 			    "Can't init deflate compressor");
992 			return (ARCHIVE_FATAL);
993 		}
994 	}
995 #endif
996 
997 	return (ret2);
998 }
999 
1000 static ssize_t
1001 archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
1002 {
1003 	int ret;
1004 	struct zip *zip = a->format_data;
1005 
1006 	if ((int64_t)s > zip->entry_uncompressed_limit)
1007 		s = (size_t)zip->entry_uncompressed_limit;
1008 	zip->entry_uncompressed_written += s;
1009 
1010 	if (s == 0) return 0;
1011 
1012 	if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
1013 		switch (zip->entry_encryption) {
1014 		case ENCRYPTION_TRADITIONAL:
1015 			/* Initialize traditional PKWARE encryption context. */
1016 			if (!zip->tctx_valid) {
1017 				ret = init_traditional_pkware_encryption(a);
1018 				if (ret != ARCHIVE_OK)
1019 					return (ret);
1020 				zip->tctx_valid = 1;
1021 			}
1022 			break;
1023 		case ENCRYPTION_WINZIP_AES128:
1024 		case ENCRYPTION_WINZIP_AES256:
1025 			if (!zip->cctx_valid) {
1026 				ret = init_winzip_aes_encryption(a);
1027 				if (ret != ARCHIVE_OK)
1028 					return (ret);
1029 				zip->cctx_valid = zip->hctx_valid = 1;
1030 			}
1031 			break;
1032 		default:
1033 			break;
1034 		}
1035 	}
1036 
1037 	switch (zip->entry_compression) {
1038 	case COMPRESSION_STORE:
1039 		if (zip->tctx_valid || zip->cctx_valid) {
1040 			const uint8_t *rb = (const uint8_t *)buff;
1041 			const uint8_t * const re = rb + s;
1042 
1043 			while (rb < re) {
1044 				size_t l;
1045 
1046 				if (zip->tctx_valid) {
1047 					l = trad_enc_encrypt_update(&zip->tctx,
1048 					    rb, re - rb,
1049 					    zip->buf, zip->len_buf);
1050 				} else {
1051 					l = zip->len_buf;
1052 					ret = archive_encrypto_aes_ctr_update(
1053 					    &zip->cctx,
1054 					    rb, re - rb, zip->buf, &l);
1055 					if (ret < 0) {
1056 						archive_set_error(&a->archive,
1057 						    ARCHIVE_ERRNO_MISC,
1058 						    "Failed to encrypt file");
1059 						return (ARCHIVE_FAILED);
1060 					}
1061 					archive_hmac_sha1_update(&zip->hctx,
1062 					    zip->buf, l);
1063 				}
1064 				ret = __archive_write_output(a, zip->buf, l);
1065 				if (ret != ARCHIVE_OK)
1066 					return (ret);
1067 				zip->entry_compressed_written += l;
1068 				zip->written_bytes += l;
1069 				rb += l;
1070 			}
1071 		} else {
1072 			ret = __archive_write_output(a, buff, s);
1073 			if (ret != ARCHIVE_OK)
1074 				return (ret);
1075 			zip->written_bytes += s;
1076 			zip->entry_compressed_written += s;
1077 		}
1078 		break;
1079 #if HAVE_ZLIB_H
1080 	case COMPRESSION_DEFLATE:
1081 		zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
1082 		zip->stream.avail_in = (uInt)s;
1083 		do {
1084 			ret = deflate(&zip->stream, Z_NO_FLUSH);
1085 			if (ret == Z_STREAM_ERROR)
1086 				return (ARCHIVE_FATAL);
1087 			if (zip->stream.avail_out == 0) {
1088 				if (zip->tctx_valid) {
1089 					trad_enc_encrypt_update(&zip->tctx,
1090 					    zip->buf, zip->len_buf,
1091 					    zip->buf, zip->len_buf);
1092 				} else if (zip->cctx_valid) {
1093 					size_t outl = zip->len_buf;
1094 					ret = archive_encrypto_aes_ctr_update(
1095 					    &zip->cctx,
1096 					    zip->buf, zip->len_buf,
1097 					    zip->buf, &outl);
1098 					if (ret < 0) {
1099 						archive_set_error(&a->archive,
1100 						    ARCHIVE_ERRNO_MISC,
1101 						    "Failed to encrypt file");
1102 						return (ARCHIVE_FAILED);
1103 					}
1104 					archive_hmac_sha1_update(&zip->hctx,
1105 					    zip->buf, zip->len_buf);
1106 				}
1107 				ret = __archive_write_output(a, zip->buf,
1108 					zip->len_buf);
1109 				if (ret != ARCHIVE_OK)
1110 					return (ret);
1111 				zip->entry_compressed_written += zip->len_buf;
1112 				zip->written_bytes += zip->len_buf;
1113 				zip->stream.next_out = zip->buf;
1114 				zip->stream.avail_out = (uInt)zip->len_buf;
1115 			}
1116 		} while (zip->stream.avail_in != 0);
1117 		break;
1118 #endif
1119 
1120 	default:
1121 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1122 		    "Invalid ZIP compression type");
1123 		return ARCHIVE_FATAL;
1124 	}
1125 
1126 	zip->entry_uncompressed_limit -= s;
1127 	if (!zip->cctx_valid || zip->aes_vendor != AES_VENDOR_AE_2)
1128 		zip->entry_crc32 =
1129 		    zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
1130 	return (s);
1131 
1132 }
1133 
1134 static int
1135 archive_write_zip_finish_entry(struct archive_write *a)
1136 {
1137 	struct zip *zip = a->format_data;
1138 	int ret;
1139 
1140 #if HAVE_ZLIB_H
1141 	if (zip->entry_compression == COMPRESSION_DEFLATE) {
1142 		for (;;) {
1143 			size_t remainder;
1144 
1145 			ret = deflate(&zip->stream, Z_FINISH);
1146 			if (ret == Z_STREAM_ERROR)
1147 				return (ARCHIVE_FATAL);
1148 			remainder = zip->len_buf - zip->stream.avail_out;
1149 			if (zip->tctx_valid) {
1150 				trad_enc_encrypt_update(&zip->tctx,
1151 				    zip->buf, remainder, zip->buf, remainder);
1152 			} else if (zip->cctx_valid) {
1153 				size_t outl = remainder;
1154 				ret = archive_encrypto_aes_ctr_update(
1155 				    &zip->cctx, zip->buf, remainder,
1156 				    zip->buf, &outl);
1157 				if (ret < 0) {
1158 					archive_set_error(&a->archive,
1159 					    ARCHIVE_ERRNO_MISC,
1160 					    "Failed to encrypt file");
1161 					return (ARCHIVE_FAILED);
1162 				}
1163 				archive_hmac_sha1_update(&zip->hctx,
1164 				    zip->buf, remainder);
1165 			}
1166 			ret = __archive_write_output(a, zip->buf, remainder);
1167 			if (ret != ARCHIVE_OK)
1168 				return (ret);
1169 			zip->entry_compressed_written += remainder;
1170 			zip->written_bytes += remainder;
1171 			zip->stream.next_out = zip->buf;
1172 			if (zip->stream.avail_out != 0)
1173 				break;
1174 			zip->stream.avail_out = (uInt)zip->len_buf;
1175 		}
1176 		deflateEnd(&zip->stream);
1177 	}
1178 #endif
1179 	if (zip->hctx_valid) {
1180 		uint8_t hmac[20];
1181 		size_t hmac_len = 20;
1182 
1183 		archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
1184 		ret = __archive_write_output(a, hmac, AUTH_CODE_SIZE);
1185 		if (ret != ARCHIVE_OK)
1186 			return (ret);
1187 		zip->entry_compressed_written += AUTH_CODE_SIZE;
1188 		zip->written_bytes += AUTH_CODE_SIZE;
1189 	}
1190 
1191 	/* Write trailing data descriptor. */
1192 	if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
1193 		char d[24];
1194 		memcpy(d, "PK\007\010", 4);
1195 		if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
1196 			archive_le32enc(d + 4, 0);/* no CRC.*/
1197 		else
1198 			archive_le32enc(d + 4, zip->entry_crc32);
1199 		if (zip->entry_uses_zip64) {
1200 			archive_le64enc(d + 8,
1201 				(uint64_t)zip->entry_compressed_written);
1202 			archive_le64enc(d + 16,
1203 				(uint64_t)zip->entry_uncompressed_written);
1204 			ret = __archive_write_output(a, d, 24);
1205 			zip->written_bytes += 24;
1206 		} else {
1207 			archive_le32enc(d + 8,
1208 				(uint32_t)zip->entry_compressed_written);
1209 			archive_le32enc(d + 12,
1210 				(uint32_t)zip->entry_uncompressed_written);
1211 			ret = __archive_write_output(a, d, 16);
1212 			zip->written_bytes += 16;
1213 		}
1214 		if (ret != ARCHIVE_OK)
1215 			return (ARCHIVE_FATAL);
1216 	}
1217 
1218 	/* Append Zip64 extra data to central directory information. */
1219 	if (zip->entry_compressed_written > ZIP_4GB_MAX
1220 	    || zip->entry_uncompressed_written > ZIP_4GB_MAX
1221 	    || zip->entry_offset > ZIP_4GB_MAX) {
1222 		unsigned char zip64[32];
1223 		unsigned char *z = zip64, *zd;
1224 		memcpy(z, "\001\000\000\000", 4);
1225 		z += 4;
1226 		if (zip->entry_uncompressed_written >= ZIP_4GB_MAX) {
1227 			archive_le64enc(z, zip->entry_uncompressed_written);
1228 			z += 8;
1229 		}
1230 		if (zip->entry_compressed_written >= ZIP_4GB_MAX) {
1231 			archive_le64enc(z, zip->entry_compressed_written);
1232 			z += 8;
1233 		}
1234 		if (zip->entry_offset >= ZIP_4GB_MAX) {
1235 			archive_le64enc(z, zip->entry_offset);
1236 			z += 8;
1237 		}
1238 		archive_le16enc(zip64 + 2, (uint16_t)(z - (zip64 + 4)));
1239 		zd = cd_alloc(zip, z - zip64);
1240 		if (zd == NULL) {
1241 			archive_set_error(&a->archive, ENOMEM,
1242 				"Can't allocate zip data");
1243 			return (ARCHIVE_FATAL);
1244 		}
1245 		memcpy(zd, zip64, z - zip64);
1246 		/* Zip64 means version needs to be set to at least 4.5 */
1247 		if (archive_le16dec(zip->file_header + 6) < 45)
1248 			archive_le16enc(zip->file_header + 6, 45);
1249 	}
1250 
1251 	/* Fix up central directory file header. */
1252 	if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
1253 		archive_le32enc(zip->file_header + 16, 0);/* no CRC.*/
1254 	else
1255 		archive_le32enc(zip->file_header + 16, zip->entry_crc32);
1256 	archive_le32enc(zip->file_header + 20,
1257 		(uint32_t)zipmin(zip->entry_compressed_written,
1258 				 ZIP_4GB_MAX));
1259 	archive_le32enc(zip->file_header + 24,
1260 		(uint32_t)zipmin(zip->entry_uncompressed_written,
1261 				 ZIP_4GB_MAX));
1262 	archive_le16enc(zip->file_header + 30,
1263 	    (uint16_t)(zip->central_directory_bytes - zip->file_header_extra_offset));
1264 	archive_le32enc(zip->file_header + 42,
1265 		(uint32_t)zipmin(zip->entry_offset,
1266 				 ZIP_4GB_MAX));
1267 
1268 	return (ARCHIVE_OK);
1269 }
1270 
1271 static int
1272 archive_write_zip_close(struct archive_write *a)
1273 {
1274 	uint8_t buff[64];
1275 	int64_t offset_start, offset_end;
1276 	struct zip *zip = a->format_data;
1277 	struct cd_segment *segment;
1278 	int ret;
1279 
1280 	offset_start = zip->written_bytes;
1281 	segment = zip->central_directory;
1282 	while (segment != NULL) {
1283 		ret = __archive_write_output(a,
1284 		    segment->buff, segment->p - segment->buff);
1285 		if (ret != ARCHIVE_OK)
1286 			return (ARCHIVE_FATAL);
1287 		zip->written_bytes += segment->p - segment->buff;
1288 		segment = segment->next;
1289 	}
1290 	offset_end = zip->written_bytes;
1291 
1292 	/* If central dir info is too large, write Zip64 end-of-cd */
1293 	if (offset_end - offset_start > ZIP_4GB_MAX
1294 	    || offset_start > ZIP_4GB_MAX
1295 	    || zip->central_directory_entries > 0xffffUL
1296 	    || (zip->flags & ZIP_FLAG_FORCE_ZIP64)) {
1297 	  /* Zip64 end-of-cd record */
1298 	  memset(buff, 0, 56);
1299 	  memcpy(buff, "PK\006\006", 4);
1300 	  archive_le64enc(buff + 4, 44);
1301 	  archive_le16enc(buff + 12, 45);
1302 	  archive_le16enc(buff + 14, 45);
1303 	  /* This is disk 0 of 0. */
1304 	  archive_le64enc(buff + 24, zip->central_directory_entries);
1305 	  archive_le64enc(buff + 32, zip->central_directory_entries);
1306 	  archive_le64enc(buff + 40, offset_end - offset_start);
1307 	  archive_le64enc(buff + 48, offset_start);
1308 	  ret = __archive_write_output(a, buff, 56);
1309 	  if (ret != ARCHIVE_OK)
1310 		  return (ARCHIVE_FATAL);
1311 	  zip->written_bytes += 56;
1312 
1313 	  /* Zip64 end-of-cd locator record. */
1314 	  memset(buff, 0, 20);
1315 	  memcpy(buff, "PK\006\007", 4);
1316 	  archive_le32enc(buff + 4, 0);
1317 	  archive_le64enc(buff + 8, offset_end);
1318 	  archive_le32enc(buff + 16, 1);
1319 	  ret = __archive_write_output(a, buff, 20);
1320 	  if (ret != ARCHIVE_OK)
1321 		  return (ARCHIVE_FATAL);
1322 	  zip->written_bytes += 20;
1323 
1324 	}
1325 
1326 	/* Format and write end of central directory. */
1327 	memset(buff, 0, sizeof(buff));
1328 	memcpy(buff, "PK\005\006", 4);
1329 	archive_le16enc(buff + 8, (uint16_t)zipmin(0xffffU,
1330 		zip->central_directory_entries));
1331 	archive_le16enc(buff + 10, (uint16_t)zipmin(0xffffU,
1332 		zip->central_directory_entries));
1333 	archive_le32enc(buff + 12,
1334 		(uint32_t)zipmin(ZIP_4GB_MAX, (offset_end - offset_start)));
1335 	archive_le32enc(buff + 16,
1336 		(uint32_t)zipmin(ZIP_4GB_MAX, offset_start));
1337 	ret = __archive_write_output(a, buff, 22);
1338 	if (ret != ARCHIVE_OK)
1339 		return (ARCHIVE_FATAL);
1340 	zip->written_bytes += 22;
1341 	return (ARCHIVE_OK);
1342 }
1343 
1344 static int
1345 archive_write_zip_free(struct archive_write *a)
1346 {
1347 	struct zip *zip;
1348 	struct cd_segment *segment;
1349 
1350 	zip = a->format_data;
1351 	while (zip->central_directory != NULL) {
1352 		segment = zip->central_directory;
1353 		zip->central_directory = segment->next;
1354 		free(segment->buff);
1355 		free(segment);
1356 	}
1357 	free(zip->buf);
1358 	archive_entry_free(zip->entry);
1359 	if (zip->cctx_valid)
1360 		archive_encrypto_aes_ctr_release(&zip->cctx);
1361 	if (zip->hctx_valid)
1362 		archive_hmac_sha1_cleanup(&zip->hctx);
1363 	/* TODO: Free opt_sconv, sconv_default */
1364 
1365 	free(zip);
1366 	a->format_data = NULL;
1367 	return (ARCHIVE_OK);
1368 }
1369 
1370 /* Convert into MSDOS-style date/time. */
1371 static unsigned int
1372 dos_time(const time_t unix_time)
1373 {
1374 	struct tm *t;
1375 	unsigned int dt;
1376 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
1377 	struct tm tmbuf;
1378 #endif
1379 #if defined(HAVE__LOCALTIME64_S)
1380 	errno_t terr;
1381 	__time64_t tmptime;
1382 #endif
1383 
1384 	/* This will not preserve time when creating/extracting the archive
1385 	 * on two systems with different time zones. */
1386 #if defined(HAVE_LOCALTIME_R)
1387 	t = localtime_r(&unix_time, &tmbuf);
1388 #elif defined(HAVE__LOCALTIME64_S)
1389 	tmptime = unix_time;
1390 	terr = _localtime64_s(&tmbuf, &tmptime);
1391 	if (terr)
1392 		t = NULL;
1393 	else
1394 		t = &tmbuf;
1395 #else
1396 	t = localtime(&unix_time);
1397 #endif
1398 
1399 	/* MSDOS-style date/time is only between 1980-01-01 and 2107-12-31 */
1400 	if (t->tm_year < 1980 - 1900)
1401 		/* Set minimum date/time '1980-01-01 00:00:00'. */
1402 		dt = 0x00210000U;
1403 	else if (t->tm_year > 2107 - 1900)
1404 		/* Set maximum date/time '2107-12-31 23:59:58'. */
1405 		dt = 0xff9fbf7dU;
1406 	else {
1407 		dt = 0;
1408 		dt += ((t->tm_year - 80) & 0x7f) << 9;
1409 		dt += ((t->tm_mon + 1) & 0x0f) << 5;
1410 		dt += (t->tm_mday & 0x1f);
1411 		dt <<= 16;
1412 		dt += (t->tm_hour & 0x1f) << 11;
1413 		dt += (t->tm_min & 0x3f) << 5;
1414 		dt += (t->tm_sec & 0x3e) >> 1; /* Only counting every 2 seconds. */
1415 	}
1416 	return dt;
1417 }
1418 
1419 static size_t
1420 path_length(struct archive_entry *entry)
1421 {
1422 	mode_t type;
1423 	const char *path;
1424 	size_t len;
1425 
1426 	type = archive_entry_filetype(entry);
1427 	path = archive_entry_pathname(entry);
1428 
1429 	if (path == NULL)
1430 		return (0);
1431 	len = strlen(path);
1432 	if (type == AE_IFDIR && (path[0] == '\0' || path[len - 1] != '/'))
1433 		++len; /* Space for the trailing / */
1434 	return len;
1435 }
1436 
1437 static int
1438 write_path(struct archive_entry *entry, struct archive_write *archive)
1439 {
1440 	int ret;
1441 	const char *path;
1442 	mode_t type;
1443 	size_t written_bytes;
1444 
1445 	path = archive_entry_pathname(entry);
1446 	type = archive_entry_filetype(entry);
1447 	written_bytes = 0;
1448 
1449 	if (path == NULL)
1450 		return (ARCHIVE_FATAL);
1451 
1452 	ret = __archive_write_output(archive, path, strlen(path));
1453 	if (ret != ARCHIVE_OK)
1454 		return (ARCHIVE_FATAL);
1455 	written_bytes += strlen(path);
1456 
1457 	/* Folders are recognized by a trailing slash. */
1458 	if ((type == AE_IFDIR) & (path[strlen(path) - 1] != '/')) {
1459 		ret = __archive_write_output(archive, "/", 1);
1460 		if (ret != ARCHIVE_OK)
1461 			return (ARCHIVE_FATAL);
1462 		written_bytes += 1;
1463 	}
1464 
1465 	return ((int)written_bytes);
1466 }
1467 
1468 static void
1469 copy_path(struct archive_entry *entry, unsigned char *p)
1470 {
1471 	const char *path;
1472 	size_t pathlen;
1473 	mode_t type;
1474 
1475 	path = archive_entry_pathname(entry);
1476 	pathlen = strlen(path);
1477 	type = archive_entry_filetype(entry);
1478 
1479 	memcpy(p, path, pathlen);
1480 
1481 	/* Folders are recognized by a trailing slash. */
1482 	if ((type == AE_IFDIR) && (path[pathlen - 1] != '/'))
1483 		p[pathlen] = '/';
1484 }
1485 
1486 
1487 static struct archive_string_conv *
1488 get_sconv(struct archive_write *a, struct zip *zip)
1489 {
1490 	if (zip->opt_sconv != NULL)
1491 		return (zip->opt_sconv);
1492 
1493 	if (!zip->init_default_conversion) {
1494 		zip->sconv_default =
1495 		    archive_string_default_conversion_for_write(&(a->archive));
1496 		zip->init_default_conversion = 1;
1497 	}
1498 	return (zip->sconv_default);
1499 }
1500 
1501 /*
1502   Traditional PKWARE Decryption functions.
1503  */
1504 
1505 static void
1506 trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
1507 {
1508 	uint8_t t;
1509 #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
1510 
1511 	ctx->keys[0] = CRC32(ctx->keys[0], c);
1512 	ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
1513 	t = (ctx->keys[1] >> 24) & 0xff;
1514 	ctx->keys[2] = CRC32(ctx->keys[2], t);
1515 #undef CRC32
1516 }
1517 
1518 static uint8_t
1519 trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
1520 {
1521 	unsigned temp = ctx->keys[2] | 2;
1522 	return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
1523 }
1524 
1525 static unsigned
1526 trad_enc_encrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
1527     size_t in_len, uint8_t *out, size_t out_len)
1528 {
1529 	unsigned i, max;
1530 
1531 	max = (unsigned)((in_len < out_len)? in_len: out_len);
1532 
1533 	for (i = 0; i < max; i++) {
1534 		uint8_t t = in[i];
1535 		out[i] = t ^ trad_enc_decrypt_byte(ctx);
1536 		trad_enc_update_keys(ctx, t);
1537 	}
1538 	return i;
1539 }
1540 
1541 static int
1542 trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len)
1543 {
1544 
1545 	ctx->keys[0] = 305419896L;
1546 	ctx->keys[1] = 591751049L;
1547 	ctx->keys[2] = 878082192L;
1548 
1549 	for (;pw_len; --pw_len)
1550 		trad_enc_update_keys(ctx, *pw++);
1551 	return 0;
1552 }
1553 
1554 static int
1555 is_traditional_pkware_encryption_supported(void)
1556 {
1557 	uint8_t key[TRAD_HEADER_SIZE];
1558 
1559 	if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK)
1560 		return (0);
1561 	return (1);
1562 }
1563 
1564 static int
1565 init_traditional_pkware_encryption(struct archive_write *a)
1566 {
1567 	struct zip *zip = a->format_data;
1568 	const char *passphrase;
1569 	uint8_t key[TRAD_HEADER_SIZE];
1570 	uint8_t key_encrypted[TRAD_HEADER_SIZE];
1571 	int ret;
1572 
1573 	passphrase = __archive_write_get_passphrase(a);
1574 	if (passphrase == NULL) {
1575 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1576 		    "Encryption needs passphrase");
1577 		return ARCHIVE_FAILED;
1578 	}
1579 	if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK) {
1580 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1581 		    "Can't generate random number for encryption");
1582 		return ARCHIVE_FATAL;
1583 	}
1584 	trad_enc_init(&zip->tctx, passphrase, strlen(passphrase));
1585 	/* Set the last key code which will be used as a check code
1586 	 * for verifying passphrase in decryption. */
1587 	key[TRAD_HEADER_SIZE-1] = zip->trad_chkdat;
1588 	trad_enc_encrypt_update(&zip->tctx, key, TRAD_HEADER_SIZE,
1589 	    key_encrypted, TRAD_HEADER_SIZE);
1590 	/* Write encrypted keys in the top of the file content. */
1591 	ret = __archive_write_output(a, key_encrypted, TRAD_HEADER_SIZE);
1592 	if (ret != ARCHIVE_OK)
1593 		return (ret);
1594 	zip->written_bytes += TRAD_HEADER_SIZE;
1595 	zip->entry_compressed_written += TRAD_HEADER_SIZE;
1596 	return (ret);
1597 }
1598 
1599 static int
1600 init_winzip_aes_encryption(struct archive_write *a)
1601 {
1602 	struct zip *zip = a->format_data;
1603 	const char *passphrase;
1604 	size_t key_len, salt_len;
1605 	uint8_t salt[16 + 2];
1606 	uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
1607 	int ret;
1608 
1609 	passphrase = __archive_write_get_passphrase(a);
1610 	if (passphrase == NULL) {
1611 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1612 		    "Encryption needs passphrase");
1613 		return (ARCHIVE_FAILED);
1614 	}
1615 	if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128) {
1616 		salt_len = 8;
1617 		key_len = 16;
1618 	} else {
1619 		/* AES 256 */
1620 		salt_len = 16;
1621 		key_len = 32;
1622 	}
1623 	if (archive_random(salt, salt_len) != ARCHIVE_OK) {
1624 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1625 		    "Can't generate random number for encryption");
1626 		return (ARCHIVE_FATAL);
1627 	}
1628 	archive_pbkdf2_sha1(passphrase, strlen(passphrase),
1629 	    salt, salt_len, 1000, derived_key, key_len * 2 + 2);
1630 
1631 	ret = archive_encrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
1632 	if (ret != 0) {
1633 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1634 		    "Decryption is unsupported due to lack of crypto library");
1635 		return (ARCHIVE_FAILED);
1636 	}
1637 	ret = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len,
1638 	    key_len);
1639 	if (ret != 0) {
1640 		archive_encrypto_aes_ctr_release(&zip->cctx);
1641 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1642 		    "Failed to initialize HMAC-SHA1");
1643 		return (ARCHIVE_FAILED);
1644         }
1645 
1646 	/* Set a password verification value after the 'salt'. */
1647 	salt[salt_len] = derived_key[key_len * 2];
1648 	salt[salt_len + 1] = derived_key[key_len * 2 + 1];
1649 
1650 	/* Write encrypted keys in the top of the file content. */
1651 	ret = __archive_write_output(a, salt, salt_len + 2);
1652 	if (ret != ARCHIVE_OK)
1653 		return (ret);
1654 	zip->written_bytes += salt_len + 2;
1655 	zip->entry_compressed_written += salt_len + 2;
1656 
1657 	return (ARCHIVE_OK);
1658 }
1659 
1660 static int
1661 is_winzip_aes_encryption_supported(int encryption)
1662 {
1663 	size_t key_len, salt_len;
1664 	uint8_t salt[16 + 2];
1665 	uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
1666 	archive_crypto_ctx cctx;
1667 	archive_hmac_sha1_ctx hctx;
1668 	int ret;
1669 
1670 	if (encryption == ENCRYPTION_WINZIP_AES128) {
1671 		salt_len = 8;
1672 		key_len = 16;
1673 	} else {
1674 		/* AES 256 */
1675 		salt_len = 16;
1676 		key_len = 32;
1677 	}
1678 	if (archive_random(salt, salt_len) != ARCHIVE_OK)
1679 		return (0);
1680 	ret = archive_pbkdf2_sha1("p", 1, salt, salt_len, 1000,
1681 	    derived_key, key_len * 2 + 2);
1682 	if (ret != 0)
1683 		return (0);
1684 
1685 	ret = archive_encrypto_aes_ctr_init(&cctx, derived_key, key_len);
1686 	if (ret != 0)
1687 		return (0);
1688 	ret = archive_hmac_sha1_init(&hctx, derived_key + key_len,
1689 	    key_len);
1690 	archive_encrypto_aes_ctr_release(&cctx);
1691 	if (ret != 0)
1692 		return (0);
1693 	archive_hmac_sha1_cleanup(&hctx);
1694 	return (1);
1695 }
1696