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