2 * Copyright (c) 2008 Anselm Strauss
3 * Copyright (c) 2009 Joerg Sonnenberger
4 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Development supported by Google Summer of Code 2008.
33 * The current implementation is very limited:
35 * - No encryption support.
37 * - No support for splitting and spanning.
38 * - Only supports regular file and folder entries.
40 * Note that generally data in ZIP files is little-endian encoded,
41 * with some exceptions.
43 * TODO: Since Libarchive is generally 64bit oriented, but this implementation
44 * does not yet support sizes exceeding 32bit, it is highly fragile for
45 * big archives. This should change when ZIP64 is finally implemented, otherwise
46 * some serious checking has to be done.
50 #include "archive_platform.h"
51 __FBSDID("$FreeBSD$");
56 #ifdef HAVE_LANGINFO_H
70 #include "archive_endian.h"
71 #include "archive_entry.h"
72 #include "archive_entry_locale.h"
73 #include "archive_private.h"
74 #include "archive_write_private.h"
77 #include "archive_crc32.h"
80 #define ZIP_SIGNATURE_LOCAL_FILE_HEADER 0x04034b50
81 #define ZIP_SIGNATURE_DATA_DESCRIPTOR 0x08074b50
82 #define ZIP_SIGNATURE_FILE_HEADER 0x02014b50
83 #define ZIP_SIGNATURE_CENTRAL_DIRECTORY_END 0x06054b50
84 #define ZIP_SIGNATURE_EXTRA_TIMESTAMP 0x5455
85 #define ZIP_SIGNATURE_EXTRA_NEW_UNIX 0x7875
86 #define ZIP_VERSION_EXTRACT 0x0014 /* ZIP version 2.0 is needed. */
87 #define ZIP_VERSION_BY 0x0314 /* Made by UNIX, using ZIP version 2.0. */
88 #define ZIP_FLAGS 0x08 /* Flagging bit 3 (count from 0) for using data descriptor. */
89 #define ZIP_FLAGS_UTF8_NAME (1 << 11)
95 COMPRESSION_DEFLATE = 8
99 static ssize_t archive_write_zip_data(struct archive_write *,
100 const void *buff, size_t s);
101 static int archive_write_zip_close(struct archive_write *);
102 static int archive_write_zip_free(struct archive_write *);
103 static int archive_write_zip_finish_entry(struct archive_write *);
104 static int archive_write_zip_header(struct archive_write *,
105 struct archive_entry *);
106 static int archive_write_zip_options(struct archive_write *,
107 const char *, const char *);
108 static unsigned int dos_time(const time_t);
109 static size_t path_length(struct archive_entry *);
110 static int write_path(struct archive_entry *, struct archive_write *);
112 #define LOCAL_FILE_HEADER_SIGNATURE 0
113 #define LOCAL_FILE_HEADER_VERSION 4
114 #define LOCAL_FILE_HEADER_FLAGS 6
115 #define LOCAL_FILE_HEADER_COMPRESSION 8
116 #define LOCAL_FILE_HEADER_TIMEDATE 10
117 #define LOCAL_FILE_HEADER_CRC32 14
118 #define LOCAL_FILE_HEADER_COMPRESSED_SIZE 18
119 #define LOCAL_FILE_HEADER_UNCOMPRESSED_SIZE 22
120 #define LOCAL_FILE_HEADER_FILENAME_LENGTH 26
121 #define LOCAL_FILE_HEADER_EXTRA_LENGTH 28
122 #define SIZE_LOCAL_FILE_HEADER 30
124 #define FILE_HEADER_SIGNATURE 0
125 #define FILE_HEADER_VERSION_BY 4
126 #define FILE_HEADER_VERSION_EXTRACT 6
127 #define FILE_HEADER_FLAGS 8
128 #define FILE_HEADER_COMPRESSION 10
129 #define FILE_HEADER_TIMEDATE 12
130 #define FILE_HEADER_CRC32 16
131 #define FILE_HEADER_COMPRESSED_SIZE 20
132 #define FILE_HEADER_UNCOMPRESSED_SIZE 24
133 #define FILE_HEADER_FILENAME_LENGTH 28
134 #define FILE_HEADER_EXTRA_LENGTH 30
135 #define FILE_HEADER_COMMENT_LENGTH 32
136 #define FILE_HEADER_DISK_NUMBER 34
137 #define FILE_HEADER_ATTRIBUTES_INTERNAL 36
138 #define FILE_HEADER_ATTRIBUTES_EXTERNAL 38
139 #define FILE_HEADER_OFFSET 42
140 #define SIZE_FILE_HEADER 46
142 /* Not mandatory, but recommended by specification. */
143 #define DATA_DESCRIPTOR_SIGNATURE 0
144 #define DATA_DESCRIPTOR_CRC32 4
145 #define DATA_DESCRIPTOR_COMPRESSED_SIZE 8
146 #define DATA_DESCRIPTOR_UNCOMPRESSED_SIZE 12
147 #define SIZE_DATA_DESCRIPTOR 16
149 #define EXTRA_DATA_LOCAL_TIME_ID 0
150 #define EXTRA_DATA_LOCAL_TIME_SIZE 2
151 #define EXTRA_DATA_LOCAL_TIME_FLAG 4
152 #define EXTRA_DATA_LOCAL_MTIME 5
153 #define EXTRA_DATA_LOCAL_ATIME 9
154 #define EXTRA_DATA_LOCAL_CTIME 13
155 #define EXTRA_DATA_LOCAL_UNIX_ID 17
156 #define EXTRA_DATA_LOCAL_UNIX_SIZE 19
157 #define EXTRA_DATA_LOCAL_UNIX_VERSION 21
158 #define EXTRA_DATA_LOCAL_UNIX_UID_SIZE 22
159 #define EXTRA_DATA_LOCAL_UNIX_UID 23
160 #define EXTRA_DATA_LOCAL_UNIX_GID_SIZE 27
161 #define EXTRA_DATA_LOCAL_UNIX_GID 28
162 #define SIZE_EXTRA_DATA_LOCAL 32
164 #define EXTRA_DATA_CENTRAL_TIME_ID 0
165 #define EXTRA_DATA_CENTRAL_TIME_SIZE 2
166 #define EXTRA_DATA_CENTRAL_TIME_FLAG 4
167 #define EXTRA_DATA_CENTRAL_MTIME 5
168 #define EXTRA_DATA_CENTRAL_UNIX_ID 9
169 #define EXTRA_DATA_CENTRAL_UNIX_SIZE 11
170 #define SIZE_EXTRA_DATA_CENTRAL 13
172 #define CENTRAL_DIRECTORY_END_SIGNATURE 0
173 #define CENTRAL_DIRECTORY_END_DISK 4
174 #define CENTRAL_DIRECTORY_END_START_DISK 6
175 #define CENTRAL_DIRECTORY_END_ENTRIES_DISK 8
176 #define CENTRAL_DIRECTORY_END_ENTRIES 10
177 #define CENTRAL_DIRECTORY_END_SIZE 12
178 #define CENTRAL_DIRECTORY_END_OFFSET 16
179 #define CENTRAL_DIRECTORY_END_COMMENT_LENGTH 20
180 #define SIZE_CENTRAL_DIRECTORY_END 22
182 struct zip_file_header_link {
183 struct zip_file_header_link *next;
184 struct archive_entry *entry;
187 int64_t compressed_size;
188 enum compression compression;
193 uint8_t data_descriptor[SIZE_DATA_DESCRIPTOR];
194 struct zip_file_header_link *central_directory;
195 struct zip_file_header_link *central_directory_end;
197 int64_t written_bytes;
198 int64_t remaining_data_bytes;
199 enum compression compression;
201 struct archive_string_conv *opt_sconv;
202 struct archive_string_conv *sconv_default;
203 int init_default_conversion;
213 archive_write_zip_options(struct archive_write *a, const char *key,
216 struct zip *zip = a->format_data;
217 int ret = ARCHIVE_FAILED;
219 if (strcmp(key, "compression") == 0) {
220 if (val == NULL || val[0] == 0) {
221 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
222 "%s: compression option needs a compression name",
224 } else if (strcmp(val, "deflate") == 0) {
226 zip->compression = COMPRESSION_DEFLATE;
229 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
230 "deflate compression not supported");
232 } else if (strcmp(val, "store") == 0) {
233 zip->compression = COMPRESSION_STORE;
237 } else if (strcmp(key, "hdrcharset") == 0) {
238 if (val == NULL || val[0] == 0) {
239 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
240 "%s: hdrcharset option needs a character-set name",
243 zip->opt_sconv = archive_string_conversion_to_charset(
244 &a->archive, val, 0);
245 if (zip->opt_sconv != NULL)
253 /* Note: The "warn" return is just to inform the options
254 * supervisor that we didn't handle it. It will generate
255 * a suitable error if no one used this option. */
256 return (ARCHIVE_WARN);
260 archive_write_zip_set_compression_deflate(struct archive *_a)
262 struct archive_write *a = (struct archive_write *)_a;
263 int ret = ARCHIVE_FAILED;
265 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
266 ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER,
267 "archive_write_zip_set_compression_deflate");
268 if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
269 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
270 "Can only use archive_write_zip_set_compression_deflate"
275 struct zip *zip = a->format_data;
276 zip->compression = COMPRESSION_DEFLATE;
279 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
280 "deflate compression not supported");
287 archive_write_zip_set_compression_store(struct archive *_a)
289 struct archive_write *a = (struct archive_write *)_a;
290 struct zip *zip = a->format_data;
291 int ret = ARCHIVE_FAILED;
293 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
294 ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER,
295 "archive_write_zip_set_compression_deflate");
296 if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
297 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
298 "Can only use archive_write_zip_set_compression_store"
302 zip->compression = COMPRESSION_STORE;
309 archive_write_set_format_zip(struct archive *_a)
311 struct archive_write *a = (struct archive_write *)_a;
314 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
315 ARCHIVE_STATE_NEW, "archive_write_set_format_zip");
317 /* If another format was already registered, unregister it. */
318 if (a->format_free != NULL)
321 zip = (struct zip *) calloc(1, sizeof(*zip));
323 archive_set_error(&a->archive, ENOMEM,
324 "Can't allocate zip data");
325 return (ARCHIVE_FATAL);
327 zip->central_directory = NULL;
328 zip->central_directory_end = NULL;
330 zip->written_bytes = 0;
331 zip->remaining_data_bytes = 0;
334 zip->compression = COMPRESSION_DEFLATE;
335 zip->len_buf = 65536;
336 zip->buf = malloc(zip->len_buf);
337 if (zip->buf == NULL) {
339 archive_set_error(&a->archive, ENOMEM,
340 "Can't allocate compression buffer");
341 return (ARCHIVE_FATAL);
344 zip->compression = COMPRESSION_STORE;
347 a->format_data = zip;
348 a->format_name = "zip";
349 a->format_options = archive_write_zip_options;
350 a->format_write_header = archive_write_zip_header;
351 a->format_write_data = archive_write_zip_data;
352 a->format_finish_entry = archive_write_zip_finish_entry;
353 a->format_close = archive_write_zip_close;
354 a->format_free = archive_write_zip_free;
355 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
356 a->archive.archive_format_name = "ZIP";
358 archive_le32enc(&zip->data_descriptor[DATA_DESCRIPTOR_SIGNATURE],
359 ZIP_SIGNATURE_DATA_DESCRIPTOR);
365 is_all_ascii(const char *p)
367 const unsigned char *pp = (const unsigned char *)p;
377 archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
380 uint8_t h[SIZE_LOCAL_FILE_HEADER];
381 uint8_t e[SIZE_EXTRA_DATA_LOCAL];
383 struct zip_file_header_link *l;
384 struct archive_string_conv *sconv;
385 int ret, ret2 = ARCHIVE_OK;
389 /* Entries other than a regular file or a folder are skipped. */
390 type = archive_entry_filetype(entry);
391 if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) {
392 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
393 "Filetype not supported");
394 return ARCHIVE_FAILED;
397 /* Directory entries should have a size of 0. */
398 if (type == AE_IFDIR)
399 archive_entry_set_size(entry, 0);
401 zip = a->format_data;
402 /* Setup default conversion. */
403 if (zip->opt_sconv == NULL && !zip->init_default_conversion) {
405 archive_string_default_conversion_for_write(&(a->archive));
406 zip->init_default_conversion = 1;
409 if (zip->flags == 0) {
410 /* Initialize the general purpose flags. */
411 zip->flags = ZIP_FLAGS;
412 if (zip->opt_sconv != NULL) {
413 if (strcmp(archive_string_conversion_charset_name(
414 zip->opt_sconv), "UTF-8") == 0)
415 zip->flags |= ZIP_FLAGS_UTF8_NAME;
417 } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
418 zip->flags |= ZIP_FLAGS_UTF8_NAME;
422 d = zip->data_descriptor;
423 size = archive_entry_size(entry);
424 zip->remaining_data_bytes = size;
426 /* Append archive entry to the central directory data. */
427 l = (struct zip_file_header_link *) malloc(sizeof(*l));
429 archive_set_error(&a->archive, ENOMEM,
430 "Can't allocate zip header data");
431 return (ARCHIVE_FATAL);
433 #if defined(_WIN32) && !defined(__CYGWIN__)
434 /* Make sure the path separators in pahtname, hardlink and symlink
435 * are all slash '/', not the Windows path separator '\'. */
436 l->entry = __la_win_entry_in_posix_pathseparator(entry);
437 if (l->entry == entry)
438 l->entry = archive_entry_clone(entry);
440 l->entry = archive_entry_clone(entry);
442 if (l->entry == NULL) {
443 archive_set_error(&a->archive, ENOMEM,
444 "Can't allocate zip header data");
446 return (ARCHIVE_FATAL);
448 l->flags = zip->flags;
449 if (zip->opt_sconv != NULL)
450 sconv = zip->opt_sconv;
452 sconv = zip->sconv_default;
457 if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) {
458 if (errno == ENOMEM) {
459 archive_entry_free(l->entry);
461 archive_set_error(&a->archive, ENOMEM,
462 "Can't allocate memory for Pathname");
463 return (ARCHIVE_FATAL);
465 archive_set_error(&a->archive,
466 ARCHIVE_ERRNO_FILE_FORMAT,
467 "Can't translate Pathname '%s' to %s",
468 archive_entry_pathname(entry),
469 archive_string_conversion_charset_name(sconv));
473 archive_entry_set_pathname(l->entry, p);
476 * Although there is no character-set regulation for Symlink,
477 * it is suitable to convert a character-set of Symlinke to
478 * what those of the Pathname has been converted to.
480 if (type == AE_IFLNK) {
481 if (archive_entry_symlink_l(entry, &p, &len, sconv)) {
482 if (errno == ENOMEM) {
483 archive_entry_free(l->entry);
485 archive_set_error(&a->archive, ENOMEM,
486 "Can't allocate memory "
488 return (ARCHIVE_FATAL);
491 * Even if the strng conversion failed,
492 * we should not report the error since
493 * thre is no regulation for.
496 archive_entry_set_symlink(l->entry, p);
499 /* If all characters in a filename are ASCII, Reset UTF-8 Name flag. */
500 if ((l->flags & ZIP_FLAGS_UTF8_NAME) != 0 &&
501 is_all_ascii(archive_entry_pathname(l->entry)))
502 l->flags &= ~ZIP_FLAGS_UTF8_NAME;
504 /* Initialize the CRC variable and potentially the local crc32(). */
505 l->crc32 = crc32(0, NULL, 0);
506 if (type == AE_IFLNK) {
507 const char *p = archive_entry_symlink(l->entry);
512 zip->remaining_data_bytes = 0;
513 archive_entry_set_size(l->entry, size);
514 l->compression = COMPRESSION_STORE;
515 l->compressed_size = size;
517 l->compression = zip->compression;
518 l->compressed_size = 0;
521 if (zip->central_directory == NULL) {
522 zip->central_directory = l;
524 zip->central_directory_end->next = l;
526 zip->central_directory_end = l;
528 /* Store the offset of this header for later use in central
530 l->offset = zip->written_bytes;
532 memset(h, 0, sizeof(h));
533 archive_le32enc(&h[LOCAL_FILE_HEADER_SIGNATURE],
534 ZIP_SIGNATURE_LOCAL_FILE_HEADER);
535 archive_le16enc(&h[LOCAL_FILE_HEADER_VERSION], ZIP_VERSION_EXTRACT);
536 archive_le16enc(&h[LOCAL_FILE_HEADER_FLAGS], l->flags);
537 archive_le16enc(&h[LOCAL_FILE_HEADER_COMPRESSION], l->compression);
538 archive_le32enc(&h[LOCAL_FILE_HEADER_TIMEDATE],
539 dos_time(archive_entry_mtime(entry)));
540 archive_le16enc(&h[LOCAL_FILE_HEADER_FILENAME_LENGTH],
541 (uint16_t)path_length(l->entry));
543 switch (l->compression) {
544 case COMPRESSION_STORE:
545 /* Setting compressed and uncompressed sizes even when
546 * specification says to set to zero when using data
547 * descriptors. Otherwise the end of the data for an
548 * entry is rather difficult to find. */
549 archive_le32enc(&h[LOCAL_FILE_HEADER_COMPRESSED_SIZE],
551 archive_le32enc(&h[LOCAL_FILE_HEADER_UNCOMPRESSED_SIZE],
555 case COMPRESSION_DEFLATE:
556 archive_le32enc(&h[LOCAL_FILE_HEADER_UNCOMPRESSED_SIZE],
559 zip->stream.zalloc = Z_NULL;
560 zip->stream.zfree = Z_NULL;
561 zip->stream.opaque = Z_NULL;
562 zip->stream.next_out = zip->buf;
563 zip->stream.avail_out = (uInt)zip->len_buf;
564 if (deflateInit2(&zip->stream, Z_DEFAULT_COMPRESSION,
565 Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
566 archive_set_error(&a->archive, ENOMEM,
567 "Can't init deflate compressor");
568 return (ARCHIVE_FATAL);
574 /* Formatting extra data. */
575 archive_le16enc(&h[LOCAL_FILE_HEADER_EXTRA_LENGTH], sizeof(e));
576 archive_le16enc(&e[EXTRA_DATA_LOCAL_TIME_ID],
577 ZIP_SIGNATURE_EXTRA_TIMESTAMP);
578 archive_le16enc(&e[EXTRA_DATA_LOCAL_TIME_SIZE], 1 + 4 * 3);
579 e[EXTRA_DATA_LOCAL_TIME_FLAG] = 0x07;
580 archive_le32enc(&e[EXTRA_DATA_LOCAL_MTIME],
581 (uint32_t)archive_entry_mtime(entry));
582 archive_le32enc(&e[EXTRA_DATA_LOCAL_ATIME],
583 (uint32_t)archive_entry_atime(entry));
584 archive_le32enc(&e[EXTRA_DATA_LOCAL_CTIME],
585 (uint32_t)archive_entry_ctime(entry));
587 archive_le16enc(&e[EXTRA_DATA_LOCAL_UNIX_ID],
588 ZIP_SIGNATURE_EXTRA_NEW_UNIX);
589 archive_le16enc(&e[EXTRA_DATA_LOCAL_UNIX_SIZE], 1 + (1 + 4) * 2);
590 e[EXTRA_DATA_LOCAL_UNIX_VERSION] = 1;
591 e[EXTRA_DATA_LOCAL_UNIX_UID_SIZE] = 4;
592 archive_le32enc(&e[EXTRA_DATA_LOCAL_UNIX_UID],
593 (uint32_t)archive_entry_uid(entry));
594 e[EXTRA_DATA_LOCAL_UNIX_GID_SIZE] = 4;
595 archive_le32enc(&e[EXTRA_DATA_LOCAL_UNIX_GID],
596 (uint32_t)archive_entry_gid(entry));
598 archive_le32enc(&d[DATA_DESCRIPTOR_UNCOMPRESSED_SIZE],
601 ret = __archive_write_output(a, h, sizeof(h));
602 if (ret != ARCHIVE_OK)
603 return (ARCHIVE_FATAL);
604 zip->written_bytes += sizeof(h);
606 ret = write_path(l->entry, a);
607 if (ret <= ARCHIVE_OK)
608 return (ARCHIVE_FATAL);
609 zip->written_bytes += ret;
611 ret = __archive_write_output(a, e, sizeof(e));
612 if (ret != ARCHIVE_OK)
613 return (ARCHIVE_FATAL);
614 zip->written_bytes += sizeof(e);
616 if (type == AE_IFLNK) {
617 const unsigned char *p;
619 p = (const unsigned char *)archive_entry_symlink(l->entry);
620 ret = __archive_write_output(a, p, (size_t)size);
621 if (ret != ARCHIVE_OK)
622 return (ARCHIVE_FATAL);
623 zip->written_bytes += size;
624 l->crc32 = crc32(l->crc32, p, (unsigned)size);
627 if (ret2 != ARCHIVE_OK)
633 archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
636 struct zip *zip = a->format_data;
637 struct zip_file_header_link *l = zip->central_directory_end;
639 if ((int64_t)s > zip->remaining_data_bytes)
640 s = (size_t)zip->remaining_data_bytes;
642 if (s == 0) return 0;
644 switch (l->compression) {
645 case COMPRESSION_STORE:
646 ret = __archive_write_output(a, buff, s);
647 if (ret != ARCHIVE_OK) return (ret);
648 zip->written_bytes += s;
649 zip->remaining_data_bytes -= s;
650 l->compressed_size += s;
651 l->crc32 = crc32(l->crc32, buff, (unsigned)s);
654 case COMPRESSION_DEFLATE:
655 zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
656 zip->stream.avail_in = (uInt)s;
658 ret = deflate(&zip->stream, Z_NO_FLUSH);
659 if (ret == Z_STREAM_ERROR)
660 return (ARCHIVE_FATAL);
661 if (zip->stream.avail_out == 0) {
662 ret = __archive_write_output(a, zip->buf,
664 if (ret != ARCHIVE_OK)
666 l->compressed_size += zip->len_buf;
667 zip->written_bytes += zip->len_buf;
668 zip->stream.next_out = zip->buf;
669 zip->stream.avail_out = (uInt)zip->len_buf;
671 } while (zip->stream.avail_in != 0);
672 zip->remaining_data_bytes -= s;
673 /* If we have it, use zlib's fast crc32() */
674 l->crc32 = crc32(l->crc32, buff, (uInt)s);
679 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
680 "Invalid ZIP compression type");
681 return ARCHIVE_FATAL;
686 archive_write_zip_finish_entry(struct archive_write *a)
688 /* Write the data descripter after file data has been written. */
690 struct zip *zip = a->format_data;
691 uint8_t *d = zip->data_descriptor;
692 struct zip_file_header_link *l = zip->central_directory_end;
697 switch(l->compression) {
698 case COMPRESSION_STORE:
701 case COMPRESSION_DEFLATE:
703 ret = deflate(&zip->stream, Z_FINISH);
704 if (ret == Z_STREAM_ERROR)
705 return (ARCHIVE_FATAL);
706 reminder = zip->len_buf - zip->stream.avail_out;
707 ret = __archive_write_output(a, zip->buf, reminder);
708 if (ret != ARCHIVE_OK)
710 l->compressed_size += reminder;
711 zip->written_bytes += reminder;
712 zip->stream.next_out = zip->buf;
713 if (zip->stream.avail_out != 0)
715 zip->stream.avail_out = (uInt)zip->len_buf;
717 deflateEnd(&zip->stream);
722 archive_le32enc(&d[DATA_DESCRIPTOR_CRC32], l->crc32);
723 archive_le32enc(&d[DATA_DESCRIPTOR_COMPRESSED_SIZE],
724 (uint32_t)l->compressed_size);
725 ret = __archive_write_output(a, d, SIZE_DATA_DESCRIPTOR);
726 if (ret != ARCHIVE_OK)
727 return (ARCHIVE_FATAL);
728 zip->written_bytes += SIZE_DATA_DESCRIPTOR;
733 archive_write_zip_close(struct archive_write *a)
736 struct zip_file_header_link *l;
737 uint8_t h[SIZE_FILE_HEADER];
738 uint8_t end[SIZE_CENTRAL_DIRECTORY_END];
739 uint8_t e[SIZE_EXTRA_DATA_CENTRAL];
740 int64_t offset_start, offset_end;
744 zip = a->format_data;
745 l = zip->central_directory;
748 * Formatting central directory file header fields that are
749 * fixed for all entries.
750 * Fields not used (and therefor 0) are:
754 * - attributes_internal
756 memset(h, 0, sizeof(h));
757 archive_le32enc(&h[FILE_HEADER_SIGNATURE], ZIP_SIGNATURE_FILE_HEADER);
758 archive_le16enc(&h[FILE_HEADER_VERSION_BY], ZIP_VERSION_BY);
759 archive_le16enc(&h[FILE_HEADER_VERSION_EXTRACT], ZIP_VERSION_EXTRACT);
762 offset_start = zip->written_bytes;
764 /* Formatting individual header fields per entry and
765 * writing each entry. */
767 archive_le16enc(&h[FILE_HEADER_FLAGS], l->flags);
768 archive_le16enc(&h[FILE_HEADER_COMPRESSION], l->compression);
769 archive_le32enc(&h[FILE_HEADER_TIMEDATE],
770 dos_time(archive_entry_mtime(l->entry)));
771 archive_le32enc(&h[FILE_HEADER_CRC32], l->crc32);
772 archive_le32enc(&h[FILE_HEADER_COMPRESSED_SIZE],
773 (uint32_t)l->compressed_size);
774 archive_le32enc(&h[FILE_HEADER_UNCOMPRESSED_SIZE],
775 (uint32_t)archive_entry_size(l->entry));
776 archive_le16enc(&h[FILE_HEADER_FILENAME_LENGTH],
777 (uint16_t)path_length(l->entry));
778 archive_le16enc(&h[FILE_HEADER_EXTRA_LENGTH], sizeof(e));
779 archive_le16enc(&h[FILE_HEADER_ATTRIBUTES_EXTERNAL+2],
780 archive_entry_mode(l->entry));
781 archive_le32enc(&h[FILE_HEADER_OFFSET], (uint32_t)l->offset);
783 /* Formatting extra data. */
784 archive_le16enc(&e[EXTRA_DATA_CENTRAL_TIME_ID],
785 ZIP_SIGNATURE_EXTRA_TIMESTAMP);
786 archive_le16enc(&e[EXTRA_DATA_CENTRAL_TIME_SIZE], 1 + 4);
787 e[EXTRA_DATA_CENTRAL_TIME_FLAG] = 0x07;
788 archive_le32enc(&e[EXTRA_DATA_CENTRAL_MTIME],
789 (uint32_t)archive_entry_mtime(l->entry));
790 archive_le16enc(&e[EXTRA_DATA_CENTRAL_UNIX_ID],
791 ZIP_SIGNATURE_EXTRA_NEW_UNIX);
792 archive_le16enc(&e[EXTRA_DATA_CENTRAL_UNIX_SIZE], 0x0000);
794 ret = __archive_write_output(a, h, sizeof(h));
795 if (ret != ARCHIVE_OK)
796 return (ARCHIVE_FATAL);
797 zip->written_bytes += sizeof(h);
799 ret = write_path(l->entry, a);
800 if (ret <= ARCHIVE_OK)
801 return (ARCHIVE_FATAL);
802 zip->written_bytes += ret;
804 ret = __archive_write_output(a, e, sizeof(e));
805 if (ret != ARCHIVE_OK)
806 return (ARCHIVE_FATAL);
807 zip->written_bytes += sizeof(e);
812 offset_end = zip->written_bytes;
814 /* Formatting end of central directory. */
815 memset(end, 0, sizeof(end));
816 archive_le32enc(&end[CENTRAL_DIRECTORY_END_SIGNATURE],
817 ZIP_SIGNATURE_CENTRAL_DIRECTORY_END);
818 archive_le16enc(&end[CENTRAL_DIRECTORY_END_ENTRIES_DISK], entries);
819 archive_le16enc(&end[CENTRAL_DIRECTORY_END_ENTRIES], entries);
820 archive_le32enc(&end[CENTRAL_DIRECTORY_END_SIZE],
821 (uint32_t)(offset_end - offset_start));
822 archive_le32enc(&end[CENTRAL_DIRECTORY_END_OFFSET],
823 (uint32_t)offset_start);
825 /* Writing end of central directory. */
826 ret = __archive_write_output(a, end, sizeof(end));
827 if (ret != ARCHIVE_OK)
828 return (ARCHIVE_FATAL);
829 zip->written_bytes += sizeof(end);
834 archive_write_zip_free(struct archive_write *a)
837 struct zip_file_header_link *l;
839 zip = a->format_data;
840 while (zip->central_directory != NULL) {
841 l = zip->central_directory;
842 zip->central_directory = l->next;
843 archive_entry_free(l->entry);
850 a->format_data = NULL;
854 /* Convert into MSDOS-style date/time. */
856 dos_time(const time_t unix_time)
861 /* This will not preserve time when creating/extracting the archive
862 * on two systems with different time zones. */
863 t = localtime(&unix_time);
865 /* MSDOS-style date/time is only between 1980-01-01 and 2107-12-31 */
866 if (t->tm_year < 1980 - 1900)
867 /* Set minimum date/time '1980-01-01 00:00:00'. */
869 else if (t->tm_year > 2107 - 1900)
870 /* Set maximum date/time '2107-12-31 23:59:58'. */
874 dt += ((t->tm_year - 80) & 0x7f) << 9;
875 dt += ((t->tm_mon + 1) & 0x0f) << 5;
876 dt += (t->tm_mday & 0x1f);
878 dt += (t->tm_hour & 0x1f) << 11;
879 dt += (t->tm_min & 0x3f) << 5;
880 dt += (t->tm_sec & 0x3e) >> 1; /* Only counting every 2 seconds. */
886 path_length(struct archive_entry *entry)
891 type = archive_entry_filetype(entry);
892 path = archive_entry_pathname(entry);
896 if (type == AE_IFDIR &&
897 (path[0] == '\0' || path[strlen(path) - 1] != '/')) {
898 return strlen(path) + 1;
905 write_path(struct archive_entry *entry, struct archive_write *archive)
910 size_t written_bytes;
912 path = archive_entry_pathname(entry);
913 type = archive_entry_filetype(entry);
916 ret = __archive_write_output(archive, path, strlen(path));
917 if (ret != ARCHIVE_OK)
918 return (ARCHIVE_FATAL);
919 written_bytes += strlen(path);
921 /* Folders are recognized by a traling slash. */
922 if ((type == AE_IFDIR) & (path[strlen(path) - 1] != '/')) {
923 ret = __archive_write_output(archive, "/", 1);
924 if (ret != ARCHIVE_OK)
925 return (ARCHIVE_FATAL);
929 return ((int)written_bytes);