Lines Matching refs:zip

122 struct zip {
193 static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
203 cd_alloc(struct zip *zip, size_t length)
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)) {
221 if (zip->central_directory == NULL) {
222 zip->central_directory
223 = zip->central_directory_last
226 zip->central_directory_last->next = segment;
227 zip->central_directory_last = segment;
231 p = zip->central_directory_last->p;
232 zip->central_directory_last->p += length;
233 zip->central_directory_bytes += length;
256 struct zip *zip = a->format_data;
270 zip->requested_compression = COMPRESSION_DEFLATE;
277 zip->requested_compression = COMPRESSION_STORE;
287 zip->requested_compression = COMPRESSION_STORE;
291 zip->requested_compression = COMPRESSION_DEFLATE;
292 zip->deflate_compression_level = val[0] - '0';
301 zip->encryption_type = ENCRYPTION_NONE;
307 zip->encryption_type = ENCRYPTION_TRADITIONAL;
317 zip->encryption_type = ENCRYPTION_WINZIP_AES128;
327 zip->encryption_type = ENCRYPTION_WINZIP_AES256;
342 zip->flags &= ~ ZIP_FLAG_EXPERIMENT_xl;
344 zip->flags |= ZIP_FLAG_EXPERIMENT_xl;
353 zip->crc32func = real_crc32;
355 zip->crc32func = fake_crc32;
367 zip->opt_sconv = archive_string_conversion_to_charset(
369 if (zip->opt_sconv != NULL)
383 zip->flags |= ZIP_FLAG_FORCE_ZIP64;
384 zip->flags &= ~ZIP_FLAG_AVOID_ZIP64;
386 zip->flags &= ~ZIP_FLAG_FORCE_ZIP64;
387 zip->flags |= ZIP_FLAG_AVOID_ZIP64;
410 " with zip format");
414 struct zip *zip = a->format_data;
415 zip->requested_compression = COMPRESSION_DEFLATE;
430 struct zip *zip = a->format_data;
439 " with zip format");
442 zip->requested_compression = COMPRESSION_STORE;
452 struct zip *zip;
461 zip = (struct zip *) calloc(1, sizeof(*zip));
462 if (zip == NULL) {
464 "Can't allocate zip data");
469 zip->requested_compression = COMPRESSION_UNSPECIFIED;
471 zip->deflate_compression_level = Z_DEFAULT_COMPRESSION;
473 zip->crc32func = real_crc32;
476 zip->len_buf = 65536;
477 zip->buf = malloc(zip->len_buf);
478 if (zip->buf == NULL) {
479 free(zip);
485 a->format_data = zip;
486 a->format_name = "zip";
516 struct zip *zip = a->format_data;
522 struct archive_string_conv *sconv = get_sconv(a, zip);
531 &a->archive, entry, "zip");
536 if (zip->flags & ZIP_FLAG_AVOID_ZIP64) {
545 if (zip->written_bytes > ZIP_4GB_MAX) {
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;
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;
580 switch (zip->encryption_type) {
584 zip->entry_flags |= ZIP_ENTRY_FLAG_ENCRYPTED;
585 zip->entry_encryption = zip->encryption_type;
597 zip->entry = __la_win_entry_in_posix_pathseparator(entry);
598 if (zip->entry == entry)
599 zip->entry = archive_entry_clone(entry);
601 zip->entry = archive_entry_clone(entry);
603 if (zip->entry == NULL) {
605 "Can't allocate zip header data");
627 archive_entry_set_pathname(zip->entry, p);
644 archive_entry_set_symlink(zip->entry, p);
649 if (!is_all_ascii(archive_entry_pathname(zip->entry))) {
650 if (zip->opt_sconv != NULL) {
652 zip->opt_sconv), "UTF-8") == 0)
653 zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
656 zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
660 filename_length = path_length(zip->entry);
664 slink = archive_entry_symlink(zip->entry);
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,
674 zip->entry_compression = COMPRESSION_STORE;
677 zip->entry_compression = COMPRESSION_STORE;
678 zip->entry_uncompressed_limit = 0;
680 } else if (archive_entry_size_is_set(zip->entry)) {
681 int64_t size = archive_entry_size(zip->entry);
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;
689 if (zip->entry_compression == COMPRESSION_STORE) {
690 zip->entry_compressed_size = size;
691 zip->entry_uncompressed_size = size;
694 zip->entry_uncompressed_size = size;
698 if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
699 switch (zip->entry_encryption) {
718 if (zip->entry_compression == COMPRESSION_STORE)
719 zip->entry_compressed_size += additional_size;
724 * (this was suggested by discussion on info-zip-dev
732 if ((zip->flags & ZIP_FLAG_FORCE_ZIP64)
733 || (zip->entry_uncompressed_size + additional_size > ZIP_4GB_MAX)
734 || (zip->entry_uncompressed_size > ZIP_4GB_MAX_UNCOMPRESSED
735 && zip->entry_compression != COMPRESSION_STORE)) {
736 zip->entry_uses_zip64 = 1;
741 zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
748 zip->entry_compression = COMPRESSION_DEFAULT;
749 zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
750 if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) {
751 zip->entry_uses_zip64 = 1;
753 } else if (zip->entry_compression == COMPRESSION_STORE) {
759 if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
760 switch (zip->entry_encryption) {
778 archive_le16enc(local_header + 6, zip->entry_flags);
779 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
780 || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
783 archive_le16enc(local_header + 8, zip->entry_compression);
785 dos_time(archive_entry_mtime(zip->entry)));
786 archive_le32enc(local_header + 14, zip->entry_crc32);
787 if (zip->entry_uses_zip64) {
796 archive_le32enc(local_header + 18, (uint32_t)zip->entry_compressed_size);
797 archive_le32enc(local_header + 22, (uint32_t)zip->entry_uncompressed_size);
801 if (zip->entry_encryption == ENCRYPTION_TRADITIONAL) {
802 if (zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END)
803 zip->trad_chkdat = local_header[11];
805 zip->trad_chkdat = local_header[17];
809 zip->file_header = cd_alloc(zip, 46);
810 /* If (zip->file_header == NULL) XXXX */
811 ++zip->central_directory_entries;
812 memset(zip->file_header, 0, 46);
813 memcpy(zip->file_header, "PK\001\002", 4);
815 archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed);
816 archive_le16enc(zip->file_header + 6, version_needed);
817 archive_le16enc(zip->file_header + 8, zip->entry_flags);
818 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
819 || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
820 archive_le16enc(zip->file_header + 10, WINZIP_AES_ENCRYPTION);
822 archive_le16enc(zip->file_header + 10, zip->entry_compression);
823 archive_le32enc(zip->file_header + 12,
824 dos_time(archive_entry_mtime(zip->entry)));
825 archive_le16enc(zip->file_header + 28, (uint16_t)filename_length);
827 archive_le32enc(zip->file_header + 38,
828 ((uint32_t)archive_entry_mode(zip->entry)) << 16);
829 e = cd_alloc(zip, filename_length);
831 copy_path(zip->entry, e);
878 if ((zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED)
879 && (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
880 || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)) {
889 if (archive_entry_size_is_set(zip->entry)
890 && archive_entry_size(zip->entry) < 20) {
892 zip->aes_vendor = AES_VENDOR_AE_2;/* no CRC. */
894 zip->aes_vendor = AES_VENDOR_AE_1;
897 *e++ = (zip->entry_encryption == ENCRYPTION_WINZIP_AES128)?1:3;
899 archive_le16enc(e, zip->entry_compression);
904 zip->file_header_extra_offset = zip->central_directory_bytes;
905 cd_extra = cd_alloc(zip, e - local_extra);
917 if (zip->entry_uses_zip64) {
921 archive_le64enc(e, zip->entry_uncompressed_size);
923 archive_le64enc(e, zip->entry_compressed_size);
928 if (zip->flags & ZIP_FLAG_EXPERIMENT_xl) {
947 ((uint32_t)archive_entry_mode(zip->entry)) << 16);
962 zip->written_bytes += 30;
964 ret = write_path(zip->entry, a);
967 zip->written_bytes += ret;
972 zip->written_bytes += e - local_extra;
979 zip->entry_compressed_written += slink_size;
980 zip->entry_uncompressed_written += slink_size;
981 zip->written_bytes += slink_size;
985 if (zip->entry_compression == COMPRESSION_DEFLATE) {
986 zip->stream.zalloc = Z_NULL;
987 zip->stream.zfree = Z_NULL;
988 zip->stream.opaque = Z_NULL;
989 zip->stream.next_out = zip->buf;
990 zip->stream.avail_out = (uInt)zip->len_buf;
991 if (deflateInit2(&zip->stream, zip->deflate_compression_level,
1007 struct zip *zip = a->format_data;
1009 if ((int64_t)s > zip->entry_uncompressed_limit)
1010 s = (size_t)zip->entry_uncompressed_limit;
1011 zip->entry_uncompressed_written += s;
1015 if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
1016 switch (zip->entry_encryption) {
1019 if (!zip->tctx_valid) {
1023 zip->tctx_valid = 1;
1028 if (!zip->cctx_valid) {
1032 zip->cctx_valid = zip->hctx_valid = 1;
1041 switch (zip->entry_compression) {
1043 if (zip->tctx_valid || zip->cctx_valid) {
1050 if (zip->tctx_valid) {
1051 l = trad_enc_encrypt_update(&zip->tctx,
1053 zip->buf, zip->len_buf);
1055 l = zip->len_buf;
1057 &zip->cctx,
1058 rb, re - rb, zip->buf, &l);
1065 archive_hmac_sha1_update(&zip->hctx,
1066 zip->buf, l);
1068 ret = __archive_write_output(a, zip->buf, l);
1071 zip->entry_compressed_written += l;
1072 zip->written_bytes += l;
1079 zip->written_bytes += s;
1080 zip->entry_compressed_written += s;
1085 zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
1086 zip->stream.avail_in = (uInt)s;
1088 ret = deflate(&zip->stream, Z_NO_FLUSH);
1091 if (zip->stream.avail_out == 0) {
1092 if (zip->tctx_valid) {
1093 trad_enc_encrypt_update(&zip->tctx,
1094 zip->buf, zip->len_buf,
1095 zip->buf, zip->len_buf);
1096 } else if (zip->cctx_valid) {
1097 size_t outl = zip->len_buf;
1099 &zip->cctx,
1100 zip->buf, zip->len_buf,
1101 zip->buf, &outl);
1108 archive_hmac_sha1_update(&zip->hctx,
1109 zip->buf, zip->len_buf);
1111 ret = __archive_write_output(a, zip->buf,
1112 zip->len_buf);
1115 zip->entry_compressed_written += zip->len_buf;
1116 zip->written_bytes += zip->len_buf;
1117 zip->stream.next_out = zip->buf;
1118 zip->stream.avail_out = (uInt)zip->len_buf;
1120 } while (zip->stream.avail_in != 0);
1131 zip->entry_uncompressed_limit -= s;
1132 if (!zip->cctx_valid || zip->aes_vendor != AES_VENDOR_AE_2)
1133 zip->entry_crc32 =
1134 zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
1142 struct zip *zip = a->format_data;
1146 if (zip->entry_compression == COMPRESSION_DEFLATE) {
1150 ret = deflate(&zip->stream, Z_FINISH);
1153 remainder = zip->len_buf - zip->stream.avail_out;
1154 if (zip->tctx_valid) {
1155 trad_enc_encrypt_update(&zip->tctx,
1156 zip->buf, remainder, zip->buf, remainder);
1157 } else if (zip->cctx_valid) {
1160 &zip->cctx, zip->buf, remainder,
1161 zip->buf, &outl);
1168 archive_hmac_sha1_update(&zip->hctx,
1169 zip->buf, remainder);
1171 ret = __archive_write_output(a, zip->buf, remainder);
1174 zip->entry_compressed_written += remainder;
1175 zip->written_bytes += remainder;
1176 zip->stream.next_out = zip->buf;
1177 if (zip->stream.avail_out != 0)
1179 zip->stream.avail_out = (uInt)zip->len_buf;
1181 deflateEnd(&zip->stream);
1184 if (zip->hctx_valid) {
1188 archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
1192 zip->entry_compressed_written += AUTH_CODE_SIZE;
1193 zip->written_bytes += AUTH_CODE_SIZE;
1197 if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
1200 if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
1203 archive_le32enc(d + 4, zip->entry_crc32);
1204 if (zip->entry_uses_zip64) {
1206 (uint64_t)zip->entry_compressed_written);
1208 (uint64_t)zip->entry_uncompressed_written);
1210 zip->written_bytes += 24;
1213 (uint32_t)zip->entry_compressed_written);
1215 (uint32_t)zip->entry_uncompressed_written);
1217 zip->written_bytes += 16;
1224 if (zip->entry_compressed_written > ZIP_4GB_MAX
1225 || zip->entry_uncompressed_written > ZIP_4GB_MAX
1226 || zip->entry_offset > ZIP_4GB_MAX) {
1231 if (zip->entry_uncompressed_written >= ZIP_4GB_MAX) {
1232 archive_le64enc(z, zip->entry_uncompressed_written);
1235 if (zip->entry_compressed_written >= ZIP_4GB_MAX) {
1236 archive_le64enc(z, zip->entry_compressed_written);
1239 if (zip->entry_offset >= ZIP_4GB_MAX) {
1240 archive_le64enc(z, zip->entry_offset);
1244 zd = cd_alloc(zip, z - zip64);
1247 "Can't allocate zip data");
1252 if (archive_le16dec(zip->file_header + 6) < 45)
1253 archive_le16enc(zip->file_header + 6, 45);
1257 if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
1258 archive_le32enc(zip->file_header + 16, 0);/* no CRC.*/
1260 archive_le32enc(zip->file_header + 16, zip->entry_crc32);
1261 archive_le32enc(zip->file_header + 20,
1262 (uint32_t)zipmin(zip->entry_compressed_written,
1264 archive_le32enc(zip->file_header + 24,
1265 (uint32_t)zipmin(zip->entry_uncompressed_written,
1267 archive_le16enc(zip->file_header + 30,
1268 (uint16_t)(zip->central_directory_bytes - zip->file_header_extra_offset));
1269 archive_le32enc(zip->file_header + 42,
1270 (uint32_t)zipmin(zip->entry_offset,
1281 struct zip *zip = a->format_data;
1285 offset_start = zip->written_bytes;
1286 segment = zip->central_directory;
1292 zip->written_bytes += segment->p - segment->buff;
1295 offset_end = zip->written_bytes;
1300 || zip->central_directory_entries > 0xffffUL
1301 || (zip->flags & ZIP_FLAG_FORCE_ZIP64)) {
1309 archive_le64enc(buff + 24, zip->central_directory_entries);
1310 archive_le64enc(buff + 32, zip->central_directory_entries);
1316 zip->written_bytes += 56;
1327 zip->written_bytes += 20;
1335 zip->central_directory_entries));
1337 zip->central_directory_entries));
1345 zip->written_bytes += 22;
1352 struct zip *zip;
1355 zip = a->format_data;
1356 while (zip->central_directory != NULL) {
1357 segment = zip->central_directory;
1358 zip->central_directory = segment->next;
1362 free(zip->buf);
1363 archive_entry_free(zip->entry);
1364 if (zip->cctx_valid)
1365 archive_encrypto_aes_ctr_release(&zip->cctx);
1366 if (zip->hctx_valid)
1367 archive_hmac_sha1_cleanup(&zip->hctx);
1370 free(zip);
1493 get_sconv(struct archive_write *a, struct zip *zip)
1495 if (zip->opt_sconv != NULL)
1496 return (zip->opt_sconv);
1498 if (!zip->init_default_conversion) {
1499 zip->sconv_default =
1501 zip->init_default_conversion = 1;
1503 return (zip->sconv_default);
1572 struct zip *zip = a->format_data;
1589 trad_enc_init(&zip->tctx, passphrase, strlen(passphrase));
1592 key[TRAD_HEADER_SIZE-1] = zip->trad_chkdat;
1593 trad_enc_encrypt_update(&zip->tctx, key, TRAD_HEADER_SIZE,
1599 zip->written_bytes += TRAD_HEADER_SIZE;
1600 zip->entry_compressed_written += TRAD_HEADER_SIZE;
1607 struct zip *zip = a->format_data;
1620 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128) {
1636 ret = archive_encrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
1642 ret = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len,
1645 archive_encrypto_aes_ctr_release(&zip->cctx);
1659 zip->written_bytes += salt_len + 2;
1660 zip->entry_compressed_written += salt_len + 2;