Deleted Added
full compact
archive_read_support_format_zip.c (346104) archive_read_support_format_zip.c (348607)
1/*-
2 * Copyright (c) 2004-2013 Tim Kientzle
3 * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
4 * Copyright (c) 2013 Konrad Kleine
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

--- 12 unchanged lines hidden (view full) ---

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#include "archive_platform.h"
1/*-
2 * Copyright (c) 2004-2013 Tim Kientzle
3 * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
4 * Copyright (c) 2013 Konrad Kleine
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

--- 12 unchanged lines hidden (view full) ---

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#include "archive_platform.h"
29__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/archive_read_support_format_zip.c 346104 2019-04-10 21:46:06Z mm $");
29__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/archive_read_support_format_zip.c 348607 2019-06-04 10:35:54Z mm $");
30
31/*
32 * The definitive documentation of the Zip file format is:
33 * http://www.pkware.com/documents/casestudies/APPNOTE.TXT
34 *
35 * The Info-Zip project has pioneered various extensions to better
36 * support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855
37 * "Ux", and 0x7875 "ux" extensions for time and ownership

--- 429 unchanged lines hidden (view full) ---

467}
468
469/*
470 * The extra data is stored as a list of
471 * id1+size1+data1 + id2+size2+data2 ...
472 * triplets. id and size are 2 bytes each.
473 */
474static int
30
31/*
32 * The definitive documentation of the Zip file format is:
33 * http://www.pkware.com/documents/casestudies/APPNOTE.TXT
34 *
35 * The Info-Zip project has pioneered various extensions to better
36 * support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855
37 * "Ux", and 0x7875 "ux" extensions for time and ownership

--- 429 unchanged lines hidden (view full) ---

467}
468
469/*
470 * The extra data is stored as a list of
471 * id1+size1+data1 + id2+size2+data2 ...
472 * triplets. id and size are 2 bytes each.
473 */
474static int
475process_extra(struct archive_read *a, const char *p, size_t extra_length, struct zip_entry* zip_entry)
475process_extra(struct archive_read *a, struct archive_entry *entry,
476 const char *p, size_t extra_length, struct zip_entry* zip_entry)
476{
477 unsigned offset = 0;
477{
478 unsigned offset = 0;
479 struct zip *zip = (struct zip *)(a->format->data);
478
479 if (extra_length == 0) {
480 return ARCHIVE_OK;
481 }
482
483 if (extra_length < 4) {
480
481 if (extra_length == 0) {
482 return ARCHIVE_OK;
483 }
484
485 if (extra_length < 4) {
484 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
485 "Too-small extra data: Need at least 4 bytes, but only found %d bytes", (int)extra_length);
486 return ARCHIVE_FAILED;
486 size_t i = 0;
487 /* Some ZIP files may have trailing 0 bytes. Let's check they
488 * are all 0 and ignore them instead of returning an error.
489 *
490 * This is not techincally correct, but some ZIP files look
491 * like this and other tools support those files - so let's
492 * also support them.
493 */
494 for (; i < extra_length; i++) {
495 if (p[i] != 0) {
496 archive_set_error(&a->archive,
497 ARCHIVE_ERRNO_FILE_FORMAT,
498 "Too-small extra data: "
499 "Need at least 4 bytes, "
500 "but only found %d bytes",
501 (int)extra_length);
502 return ARCHIVE_FAILED;
503 }
504 }
505
506 return ARCHIVE_OK;
487 }
507 }
508
488 while (offset <= extra_length - 4) {
489 unsigned short headerid = archive_le16dec(p + offset);
490 unsigned short datasize = archive_le16dec(p + offset + 2);
491
492 offset += 4;
493 if (offset + datasize > extra_length) {
509 while (offset <= extra_length - 4) {
510 unsigned short headerid = archive_le16dec(p + offset);
511 unsigned short datasize = archive_le16dec(p + offset + 2);
512
513 offset += 4;
514 if (offset + datasize > extra_length) {
494 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
495 "Extra data overflow: Need %d bytes but only found %d bytes",
515 archive_set_error(&a->archive,
516 ARCHIVE_ERRNO_FILE_FORMAT, "Extra data overflow: "
517 "Need %d bytes but only found %d bytes",
496 (int)datasize, (int)(extra_length - offset));
497 return ARCHIVE_FAILED;
498 }
499#ifdef DEBUG
500 fprintf(stderr, "Header id 0x%04x, length %d\n",
501 headerid, datasize);
502#endif
503 switch (headerid) {
504 case 0x0001:
505 /* Zip64 extended information extra field. */
506 zip_entry->flags |= LA_USED_ZIP64;
507 if (zip_entry->uncompressed_size == 0xffffffff) {
508 uint64_t t = 0;
509 if (datasize < 8
518 (int)datasize, (int)(extra_length - offset));
519 return ARCHIVE_FAILED;
520 }
521#ifdef DEBUG
522 fprintf(stderr, "Header id 0x%04x, length %d\n",
523 headerid, datasize);
524#endif
525 switch (headerid) {
526 case 0x0001:
527 /* Zip64 extended information extra field. */
528 zip_entry->flags |= LA_USED_ZIP64;
529 if (zip_entry->uncompressed_size == 0xffffffff) {
530 uint64_t t = 0;
531 if (datasize < 8
510 || (t = archive_le64dec(p + offset)) > INT64_MAX) {
511 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
512 "Malformed 64-bit uncompressed size");
532 || (t = archive_le64dec(p + offset)) >
533 INT64_MAX) {
534 archive_set_error(&a->archive,
535 ARCHIVE_ERRNO_FILE_FORMAT,
536 "Malformed 64-bit "
537 "uncompressed size");
513 return ARCHIVE_FAILED;
514 }
515 zip_entry->uncompressed_size = t;
516 offset += 8;
517 datasize -= 8;
518 }
519 if (zip_entry->compressed_size == 0xffffffff) {
520 uint64_t t = 0;
521 if (datasize < 8
538 return ARCHIVE_FAILED;
539 }
540 zip_entry->uncompressed_size = t;
541 offset += 8;
542 datasize -= 8;
543 }
544 if (zip_entry->compressed_size == 0xffffffff) {
545 uint64_t t = 0;
546 if (datasize < 8
522 || (t = archive_le64dec(p + offset)) > INT64_MAX) {
523 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
524 "Malformed 64-bit compressed size");
547 || (t = archive_le64dec(p + offset)) >
548 INT64_MAX) {
549 archive_set_error(&a->archive,
550 ARCHIVE_ERRNO_FILE_FORMAT,
551 "Malformed 64-bit "
552 "compressed size");
525 return ARCHIVE_FAILED;
526 }
527 zip_entry->compressed_size = t;
528 offset += 8;
529 datasize -= 8;
530 }
531 if (zip_entry->local_header_offset == 0xffffffff) {
532 uint64_t t = 0;
533 if (datasize < 8
553 return ARCHIVE_FAILED;
554 }
555 zip_entry->compressed_size = t;
556 offset += 8;
557 datasize -= 8;
558 }
559 if (zip_entry->local_header_offset == 0xffffffff) {
560 uint64_t t = 0;
561 if (datasize < 8
534 || (t = archive_le64dec(p + offset)) > INT64_MAX) {
535 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
536 "Malformed 64-bit local header offset");
562 || (t = archive_le64dec(p + offset)) >
563 INT64_MAX) {
564 archive_set_error(&a->archive,
565 ARCHIVE_ERRNO_FILE_FORMAT,
566 "Malformed 64-bit "
567 "local header offset");
537 return ARCHIVE_FAILED;
538 }
539 zip_entry->local_header_offset = t;
540 offset += 8;
541 datasize -= 8;
542 }
543 /* archive_le32dec(p + offset) gives disk
544 * on which file starts, but we don't handle

--- 16 unchanged lines hidden (view full) ---

561 break;
562 }
563#endif
564 case 0x5455:
565 {
566 /* Extended time field "UT". */
567 int flags;
568 if (datasize == 0) {
568 return ARCHIVE_FAILED;
569 }
570 zip_entry->local_header_offset = t;
571 offset += 8;
572 datasize -= 8;
573 }
574 /* archive_le32dec(p + offset) gives disk
575 * on which file starts, but we don't handle

--- 16 unchanged lines hidden (view full) ---

592 break;
593 }
594#endif
595 case 0x5455:
596 {
597 /* Extended time field "UT". */
598 int flags;
599 if (datasize == 0) {
569 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
600 archive_set_error(&a->archive,
601 ARCHIVE_ERRNO_FILE_FORMAT,
570 "Incomplete extended time field");
571 return ARCHIVE_FAILED;
572 }
573 flags = p[offset];
574 offset++;
575 datasize--;
576 /* Flag bits indicate which dates are present. */
577 if (flags & 0x01)

--- 65 unchanged lines hidden (view full) ---

643 *
644 * n bytes - feature bitmap: first byte has low-order
645 * 7 bits. If high-order bit is set, a subsequent
646 * byte holds the next 7 bits, etc.
647 *
648 * if bitmap & 1, 2 byte "version made by"
649 * if bitmap & 2, 2 byte "internal file attributes"
650 * if bitmap & 4, 4 byte "external file attributes"
602 "Incomplete extended time field");
603 return ARCHIVE_FAILED;
604 }
605 flags = p[offset];
606 offset++;
607 datasize--;
608 /* Flag bits indicate which dates are present. */
609 if (flags & 0x01)

--- 65 unchanged lines hidden (view full) ---

675 *
676 * n bytes - feature bitmap: first byte has low-order
677 * 7 bits. If high-order bit is set, a subsequent
678 * byte holds the next 7 bits, etc.
679 *
680 * if bitmap & 1, 2 byte "version made by"
681 * if bitmap & 2, 2 byte "internal file attributes"
682 * if bitmap & 4, 4 byte "external file attributes"
651 * if bitmap & 8, 2 byte comment length + n byte comment
683 * if bitmap & 8, 2 byte comment length + n byte
684 * comment
652 */
653 int bitmap, bitmap_last;
654
655 if (datasize < 1)
656 break;
657 bitmap_last = bitmap = 0xff & p[offset];
658 offset += 1;
659 datasize -= 1;

--- 34 unchanged lines hidden (view full) ---

694 break;
695 external_attributes
696 = archive_le32dec(p + offset);
697 if (zip_entry->system == 3) {
698 zip_entry->mode
699 = external_attributes >> 16;
700 } else if (zip_entry->system == 0) {
701 // Interpret MSDOS directory bit
685 */
686 int bitmap, bitmap_last;
687
688 if (datasize < 1)
689 break;
690 bitmap_last = bitmap = 0xff & p[offset];
691 offset += 1;
692 datasize -= 1;

--- 34 unchanged lines hidden (view full) ---

727 break;
728 external_attributes
729 = archive_le32dec(p + offset);
730 if (zip_entry->system == 3) {
731 zip_entry->mode
732 = external_attributes >> 16;
733 } else if (zip_entry->system == 0) {
734 // Interpret MSDOS directory bit
702 if (0x10 == (external_attributes & 0x10)) {
703 zip_entry->mode = AE_IFDIR | 0775;
735 if (0x10 == (external_attributes &
736 0x10)) {
737 zip_entry->mode =
738 AE_IFDIR | 0775;
704 } else {
739 } else {
705 zip_entry->mode = AE_IFREG | 0664;
740 zip_entry->mode =
741 AE_IFREG | 0664;
706 }
742 }
707 if (0x01 == (external_attributes & 0x01)) {
708 // Read-only bit; strip write permissions
743 if (0x01 == (external_attributes &
744 0x01)) {
745 /* Read-only bit;
746 * strip write permissions */
709 zip_entry->mode &= 0555;
710 }
711 } else {
712 zip_entry->mode = 0;
713 }
714 offset += 4;
715 datasize -= 4;
716 }

--- 10 unchanged lines hidden (view full) ---

727 if (datasize < comment_length)
728 break;
729 /* Comment is not supported by libarchive */
730 offset += comment_length;
731 datasize -= comment_length;
732 }
733 break;
734 }
747 zip_entry->mode &= 0555;
748 }
749 } else {
750 zip_entry->mode = 0;
751 }
752 offset += 4;
753 datasize -= 4;
754 }

--- 10 unchanged lines hidden (view full) ---

765 if (datasize < comment_length)
766 break;
767 /* Comment is not supported by libarchive */
768 offset += comment_length;
769 datasize -= comment_length;
770 }
771 break;
772 }
773 case 0x7075:
774 {
775 /* Info-ZIP Unicode Path Extra Field. */
776 if (datasize < 5 || entry == NULL)
777 break;
778 offset += 5;
779 datasize -= 5;
780
781 /* The path name in this field is always encoded
782 * in UTF-8. */
783 if (zip->sconv_utf8 == NULL) {
784 zip->sconv_utf8 =
785 archive_string_conversion_from_charset(
786 &a->archive, "UTF-8", 1);
787 /* If the converter from UTF-8 is not
788 * available, then the path name from the main
789 * field will more likely be correct. */
790 if (zip->sconv_utf8 == NULL)
791 break;
792 }
793
794 /* Make sure the CRC32 of the filename matches. */
795 if (!zip->ignore_crc32) {
796 const char *cp = archive_entry_pathname(entry);
797 if (cp) {
798 unsigned long file_crc =
799 zip->crc32func(0, cp, strlen(cp));
800 unsigned long utf_crc =
801 archive_le32dec(p + offset - 4);
802 if (file_crc != utf_crc) {
803#ifdef DEBUG
804 fprintf(stderr,
805 "CRC filename mismatch; "
806 "CDE is %lx, but UTF8 "
807 "is outdated with %lx\n",
808 file_crc, utf_crc);
809#endif
810 break;
811 }
812 }
813 }
814
815 if (archive_entry_copy_pathname_l(entry,
816 p + offset, datasize, zip->sconv_utf8) != 0) {
817 /* Ignore the error, and fallback to the path
818 * name from the main field. */
819#ifdef DEBUG
820 fprintf(stderr, "Failed to read the ZIP "
821 "0x7075 extra field path.\n");
822#endif
823 }
824 break;
825 }
735 case 0x7855:
736 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
737#ifdef DEBUG
738 fprintf(stderr, "uid %d gid %d\n",
739 archive_le16dec(p + offset),
740 archive_le16dec(p + offset + 2));
741#endif
742 if (datasize >= 2)

--- 18 unchanged lines hidden (view full) ---

761 p + offset + 2);
762 else if (uidsize == 4 && datasize >= 6)
763 zip_entry->uid =
764 archive_le32dec(
765 p + offset + 2);
766 }
767 if (datasize >= (2 + uidsize + 3)) {
768 /* get a gid size. */
826 case 0x7855:
827 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
828#ifdef DEBUG
829 fprintf(stderr, "uid %d gid %d\n",
830 archive_le16dec(p + offset),
831 archive_le16dec(p + offset + 2));
832#endif
833 if (datasize >= 2)

--- 18 unchanged lines hidden (view full) ---

852 p + offset + 2);
853 else if (uidsize == 4 && datasize >= 6)
854 zip_entry->uid =
855 archive_le32dec(
856 p + offset + 2);
857 }
858 if (datasize >= (2 + uidsize + 3)) {
859 /* get a gid size. */
769 gidsize = 0xff & (int)p[offset+2+uidsize];
860 gidsize = 0xff &
861 (int)p[offset+2+uidsize];
770 if (gidsize == 2)
771 zip_entry->gid =
772 archive_le16dec(
773 p+offset+2+uidsize+1);
774 else if (gidsize == 4 &&
775 datasize >= (2 + uidsize + 5))
776 zip_entry->gid =
777 archive_le32dec(
778 p+offset+2+uidsize+1);
779 }
780 }
781 break;
782 }
783 case 0x9901:
784 /* WinZip AES extra data field. */
785 if (datasize < 6) {
862 if (gidsize == 2)
863 zip_entry->gid =
864 archive_le16dec(
865 p+offset+2+uidsize+1);
866 else if (gidsize == 4 &&
867 datasize >= (2 + uidsize + 5))
868 zip_entry->gid =
869 archive_le32dec(
870 p+offset+2+uidsize+1);
871 }
872 }
873 break;
874 }
875 case 0x9901:
876 /* WinZip AES extra data field. */
877 if (datasize < 6) {
786 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
878 archive_set_error(&a->archive,
879 ARCHIVE_ERRNO_FILE_FORMAT,
787 "Incomplete AES field");
788 return ARCHIVE_FAILED;
789 }
790 if (p[offset + 2] == 'A' && p[offset + 3] == 'E') {
791 /* Vendor version. */
792 zip_entry->aes_extra.vendor =
793 archive_le16dec(p + offset);
794 /* AES encryption strength. */
795 zip_entry->aes_extra.strength = p[offset + 4];
796 /* Actual compression method. */
797 zip_entry->aes_extra.compression =
798 p[offset + 5];
799 }
800 break;
801 default:
802 break;
803 }
804 offset += datasize;
805 }
880 "Incomplete AES field");
881 return ARCHIVE_FAILED;
882 }
883 if (p[offset + 2] == 'A' && p[offset + 3] == 'E') {
884 /* Vendor version. */
885 zip_entry->aes_extra.vendor =
886 archive_le16dec(p + offset);
887 /* AES encryption strength. */
888 zip_entry->aes_extra.strength = p[offset + 4];
889 /* Actual compression method. */
890 zip_entry->aes_extra.compression =
891 p[offset + 5];
892 }
893 break;
894 default:
895 break;
896 }
897 offset += datasize;
898 }
806 if (offset != extra_length) {
807 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
808 "Malformed extra data: Consumed %d bytes of %d bytes",
809 (int)offset, (int)extra_length);
810 return ARCHIVE_FAILED;
811 }
812 return ARCHIVE_OK;
813}
814
815/*
816 * Assumes file pointer is at beginning of local file header.
817 */
818static int
819zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,

--- 103 unchanged lines hidden (view full) ---

923
924 /* Read the extra data. */
925 if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
926 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
927 "Truncated ZIP file header");
928 return (ARCHIVE_FATAL);
929 }
930
899 return ARCHIVE_OK;
900}
901
902/*
903 * Assumes file pointer is at beginning of local file header.
904 */
905static int
906zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,

--- 103 unchanged lines hidden (view full) ---

1010
1011 /* Read the extra data. */
1012 if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
1013 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1014 "Truncated ZIP file header");
1015 return (ARCHIVE_FATAL);
1016 }
1017
931 if (ARCHIVE_OK != process_extra(a, h, extra_length, zip_entry)) {
1018 if (ARCHIVE_OK != process_extra(a, entry, h, extra_length,
1019 zip_entry)) {
932 return ARCHIVE_FATAL;
933 }
934 __archive_read_consume(a, extra_length);
935
936 /* Work around a bug in Info-Zip: When reading from a pipe, it
937 * stats the pipe instead of synthesizing a file entry. */
938 if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
939 zip_entry->mode &= ~ AE_IFMT;
940 zip_entry->mode |= AE_IFREG;
941 }
942
943 /* If the mode is totally empty, set some sane default. */
944 if (zip_entry->mode == 0) {
945 zip_entry->mode |= 0664;
946 }
947
1020 return ARCHIVE_FATAL;
1021 }
1022 __archive_read_consume(a, extra_length);
1023
1024 /* Work around a bug in Info-Zip: When reading from a pipe, it
1025 * stats the pipe instead of synthesizing a file entry. */
1026 if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
1027 zip_entry->mode &= ~ AE_IFMT;
1028 zip_entry->mode |= AE_IFREG;
1029 }
1030
1031 /* If the mode is totally empty, set some sane default. */
1032 if (zip_entry->mode == 0) {
1033 zip_entry->mode |= 0664;
1034 }
1035
948 /* Windows archivers sometimes use backslash as the directory separator.
949 Normalize to slash. */
1036 /* Windows archivers sometimes use backslash as the directory
1037 * separator. Normalize to slash. */
950 if (zip_entry->system == 0 &&
951 (wp = archive_entry_pathname_w(entry)) != NULL) {
952 if (wcschr(wp, L'/') == NULL && wcschr(wp, L'\\') != NULL) {
953 size_t i;
954 struct archive_wstring s;
955 archive_string_init(&s);
956 archive_wstrcpy(&s, wp);
957 for (i = 0; i < archive_strlen(&s); i++) {

--- 292 unchanged lines hidden (view full) ---

1250 || zip->ignore_crc32
1251 || (zip->hctx_valid
1252 && zip->entry->aes_extra.vendor == AES_VENDOR_AE_2))) {
1253 if (zip->entry->flags & LA_USED_ZIP64) {
1254 uint64_t compressed, uncompressed;
1255 zip->entry->crc32 = archive_le32dec(p + 4);
1256 compressed = archive_le64dec(p + 8);
1257 uncompressed = archive_le64dec(p + 16);
1038 if (zip_entry->system == 0 &&
1039 (wp = archive_entry_pathname_w(entry)) != NULL) {
1040 if (wcschr(wp, L'/') == NULL && wcschr(wp, L'\\') != NULL) {
1041 size_t i;
1042 struct archive_wstring s;
1043 archive_string_init(&s);
1044 archive_wstrcpy(&s, wp);
1045 for (i = 0; i < archive_strlen(&s); i++) {

--- 292 unchanged lines hidden (view full) ---

1338 || zip->ignore_crc32
1339 || (zip->hctx_valid
1340 && zip->entry->aes_extra.vendor == AES_VENDOR_AE_2))) {
1341 if (zip->entry->flags & LA_USED_ZIP64) {
1342 uint64_t compressed, uncompressed;
1343 zip->entry->crc32 = archive_le32dec(p + 4);
1344 compressed = archive_le64dec(p + 8);
1345 uncompressed = archive_le64dec(p + 16);
1258 if (compressed > INT64_MAX || uncompressed > INT64_MAX) {
1346 if (compressed > INT64_MAX || uncompressed >
1347 INT64_MAX) {
1259 archive_set_error(&a->archive,
1260 ARCHIVE_ERRNO_FILE_FORMAT,
1261 "Overflow of 64-bit file sizes");
1262 return ARCHIVE_FAILED;
1263 }
1264 zip->entry->compressed_size = compressed;
1265 zip->entry->uncompressed_size = uncompressed;
1266 zip->unconsumed = 24;

--- 100 unchanged lines hidden (view full) ---

1367 p += 4;
1368 zip->unconsumed = 4;
1369 }
1370 if (zip->entry->flags & LA_USED_ZIP64) {
1371 uint64_t compressed, uncompressed;
1372 zip->entry->crc32 = archive_le32dec(p);
1373 compressed = archive_le64dec(p + 4);
1374 uncompressed = archive_le64dec(p + 12);
1348 archive_set_error(&a->archive,
1349 ARCHIVE_ERRNO_FILE_FORMAT,
1350 "Overflow of 64-bit file sizes");
1351 return ARCHIVE_FAILED;
1352 }
1353 zip->entry->compressed_size = compressed;
1354 zip->entry->uncompressed_size = uncompressed;
1355 zip->unconsumed = 24;

--- 100 unchanged lines hidden (view full) ---

1456 p += 4;
1457 zip->unconsumed = 4;
1458 }
1459 if (zip->entry->flags & LA_USED_ZIP64) {
1460 uint64_t compressed, uncompressed;
1461 zip->entry->crc32 = archive_le32dec(p);
1462 compressed = archive_le64dec(p + 4);
1463 uncompressed = archive_le64dec(p + 12);
1375 if (compressed > INT64_MAX || uncompressed > INT64_MAX) {
1464 if (compressed > INT64_MAX ||
1465 uncompressed > INT64_MAX) {
1376 archive_set_error(&a->archive,
1377 ARCHIVE_ERRNO_FILE_FORMAT,
1378 "Overflow of 64-bit file sizes");
1379 return ARCHIVE_FAILED;
1380 }
1381 zip->entry->compressed_size = compressed;
1382 zip->entry->uncompressed_size = uncompressed;
1383 zip->unconsumed += 20;

--- 55 unchanged lines hidden (view full) ---

1439#pragma pack(push)
1440#pragma pack(1)
1441 struct _alone_header {
1442 uint8_t bytes[5];
1443 uint64_t uncompressed_size;
1444 } alone_header;
1445#pragma pack(pop)
1446
1466 archive_set_error(&a->archive,
1467 ARCHIVE_ERRNO_FILE_FORMAT,
1468 "Overflow of 64-bit file sizes");
1469 return ARCHIVE_FAILED;
1470 }
1471 zip->entry->compressed_size = compressed;
1472 zip->entry->uncompressed_size = uncompressed;
1473 zip->unconsumed += 20;

--- 55 unchanged lines hidden (view full) ---

1529#pragma pack(push)
1530#pragma pack(1)
1531 struct _alone_header {
1532 uint8_t bytes[5];
1533 uint64_t uncompressed_size;
1534 } alone_header;
1535#pragma pack(pop)
1536
1447 /* To unpack ZIPX's "LZMA" (id 14) stream we can use standard liblzma that
1448 * is a part of XZ Utils. The stream format stored inside ZIPX file is a
1449 * modified "lzma alone" file format, that was used by the `lzma` utility
1450 * which was later deprecated in favour of `xz` utility. Since those
1451 * formats are nearly the same, we can use a standard "lzma alone" decoder
1452 * from XZ Utils. */
1537 if(zip->zipx_lzma_valid) {
1538 lzma_end(&zip->zipx_lzma_stream);
1539 zip->zipx_lzma_valid = 0;
1540 }
1453
1541
1542 /* To unpack ZIPX's "LZMA" (id 14) stream we can use standard liblzma
1543 * that is a part of XZ Utils. The stream format stored inside ZIPX
1544 * file is a modified "lzma alone" file format, that was used by the
1545 * `lzma` utility which was later deprecated in favour of `xz` utility. * Since those formats are nearly the same, we can use a standard
1546 * "lzma alone" decoder from XZ Utils. */
1547
1454 memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream));
1455 r = lzma_alone_decoder(&zip->zipx_lzma_stream, UINT64_MAX);
1456 if (r != LZMA_OK) {
1457 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1458 "lzma initialization failed(%d)", r);
1459
1460 return (ARCHIVE_FAILED);
1461 }

--- 10 unchanged lines hidden (view full) ---

1472 * 00000000: 5d00 0080 00ff ffff ffff ffff ff00 2814
1473 *
1474 * 5 bytes 8 bytes n bytes
1475 * <lzma_params><uncompressed_size><data...>
1476 *
1477 * lzma_params is a 5-byte blob that has to be decoded to extract
1478 * parameters of this LZMA stream. The uncompressed_size field is an
1479 * uint64_t value that contains information about the size of the
1548 memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream));
1549 r = lzma_alone_decoder(&zip->zipx_lzma_stream, UINT64_MAX);
1550 if (r != LZMA_OK) {
1551 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1552 "lzma initialization failed(%d)", r);
1553
1554 return (ARCHIVE_FAILED);
1555 }

--- 10 unchanged lines hidden (view full) ---

1566 * 00000000: 5d00 0080 00ff ffff ffff ffff ff00 2814
1567 *
1568 * 5 bytes 8 bytes n bytes
1569 * <lzma_params><uncompressed_size><data...>
1570 *
1571 * lzma_params is a 5-byte blob that has to be decoded to extract
1572 * parameters of this LZMA stream. The uncompressed_size field is an
1573 * uint64_t value that contains information about the size of the
1480 * uncompressed file, or UINT64_MAX if this value is unknown. The <data...>
1481 * part is the actual lzma-compressed data stream.
1574 * uncompressed file, or UINT64_MAX if this value is unknown.
1575 * The <data...> part is the actual lzma-compressed data stream.
1482 *
1483 * Now here's the structure of the stream inside the ZIPX file:
1484 *
1485 * $ cat stream_inside_zipx | xxd | head -n 1
1486 * 00000000: 0914 0500 5d00 8000 0000 2814 .... ....
1487 *
1488 * 2byte 2byte 5 bytes n bytes
1489 * <magic1><magic2><lzma_params><data...>
1490 *
1576 *
1577 * Now here's the structure of the stream inside the ZIPX file:
1578 *
1579 * $ cat stream_inside_zipx | xxd | head -n 1
1580 * 00000000: 0914 0500 5d00 8000 0000 2814 .... ....
1581 *
1582 * 2byte 2byte 5 bytes n bytes
1583 * <magic1><magic2><lzma_params><data...>
1584 *
1491 * This means that the ZIPX file contains an additional magic1 and magic2
1492 * headers, the lzma_params field contains the same parameter set as in the
1493 * "lzma alone" format, and the <data...> field is the same as in the "lzma
1494 * alone" format as well. Note that also the zipx format is missing the
1495 * uncompressed_size field.
1585 * This means that the ZIPX file contains an additional magic1 and
1586 * magic2 headers, the lzma_params field contains the same parameter
1587 * set as in the "lzma alone" format, and the <data...> field is the
1588 * same as in the "lzma alone" format as well. Note that also the zipx
1589 * format is missing the uncompressed_size field.
1496 *
1590 *
1497 * So, in order to use the "lzma alone" decoder for the zipx lzma stream,
1498 * we simply need to shuffle around some fields, prepare a new lzma alone
1499 * header, feed it into lzma alone decoder so it will initialize itself
1500 * properly, and then we can start feeding normal zipx lzma stream into the
1501 * decoder.
1591 * So, in order to use the "lzma alone" decoder for the zipx lzma
1592 * stream, we simply need to shuffle around some fields, prepare a new
1593 * lzma alone header, feed it into lzma alone decoder so it will
1594 * initialize itself properly, and then we can start feeding normal
1595 * zipx lzma stream into the decoder.
1502 */
1503
1504 /* Read magic1,magic2,lzma_params from the ZIPX stream. */
1505 if((p = __archive_read_ahead(a, 9, NULL)) == NULL) {
1506 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1507 "Truncated lzma data");
1508 return (ARCHIVE_FATAL);
1509 }
1510
1511 if(p[2] != 0x05 || p[3] != 0x00) {
1512 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1513 "Invalid lzma data");
1514 return (ARCHIVE_FATAL);
1515 }
1516
1596 */
1597
1598 /* Read magic1,magic2,lzma_params from the ZIPX stream. */
1599 if((p = __archive_read_ahead(a, 9, NULL)) == NULL) {
1600 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1601 "Truncated lzma data");
1602 return (ARCHIVE_FATAL);
1603 }
1604
1605 if(p[2] != 0x05 || p[3] != 0x00) {
1606 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1607 "Invalid lzma data");
1608 return (ARCHIVE_FATAL);
1609 }
1610
1517 /* Prepare an lzma alone header: copy the lzma_params blob into a proper
1518 * place into the lzma alone header. */
1611 /* Prepare an lzma alone header: copy the lzma_params blob into
1612 * a proper place into the lzma alone header. */
1519 memcpy(&alone_header.bytes[0], p + 4, 5);
1520
1521 /* Initialize the 'uncompressed size' field to unknown; we'll manually
1522 * monitor how many bytes there are still to be uncompressed. */
1523 alone_header.uncompressed_size = UINT64_MAX;
1524
1525 if(!zip->uncompressed_buffer) {
1526 zip->uncompressed_buffer_size = 256 * 1024;

--- 9 unchanged lines hidden (view full) ---

1536
1537 zip->zipx_lzma_stream.next_in = (void*) &alone_header;
1538 zip->zipx_lzma_stream.avail_in = sizeof(alone_header);
1539 zip->zipx_lzma_stream.total_in = 0;
1540 zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
1541 zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size;
1542 zip->zipx_lzma_stream.total_out = 0;
1543
1613 memcpy(&alone_header.bytes[0], p + 4, 5);
1614
1615 /* Initialize the 'uncompressed size' field to unknown; we'll manually
1616 * monitor how many bytes there are still to be uncompressed. */
1617 alone_header.uncompressed_size = UINT64_MAX;
1618
1619 if(!zip->uncompressed_buffer) {
1620 zip->uncompressed_buffer_size = 256 * 1024;

--- 9 unchanged lines hidden (view full) ---

1630
1631 zip->zipx_lzma_stream.next_in = (void*) &alone_header;
1632 zip->zipx_lzma_stream.avail_in = sizeof(alone_header);
1633 zip->zipx_lzma_stream.total_in = 0;
1634 zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
1635 zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size;
1636 zip->zipx_lzma_stream.total_out = 0;
1637
1544 /* Feed only the header into the lzma alone decoder. This will effectively
1545 * initialize the decoder, and will not produce any output bytes yet. */
1638 /* Feed only the header into the lzma alone decoder. This will
1639 * effectively initialize the decoder, and will not produce any
1640 * output bytes yet. */
1546 r = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
1547 if (r != LZMA_OK) {
1548 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1549 "lzma stream initialization error");
1550 return ARCHIVE_FATAL;
1551 }
1552
1553 /* We've already consumed some bytes, so take this into account. */

--- 58 unchanged lines hidden (view full) ---

1612
1613 case LZMA_STREAM_END:
1614 lzma_end(&zip->zipx_lzma_stream);
1615 zip->zipx_lzma_valid = 0;
1616
1617 if((int64_t) zip->zipx_lzma_stream.total_in !=
1618 zip->entry_bytes_remaining)
1619 {
1641 r = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
1642 if (r != LZMA_OK) {
1643 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1644 "lzma stream initialization error");
1645 return ARCHIVE_FATAL;
1646 }
1647
1648 /* We've already consumed some bytes, so take this into account. */

--- 58 unchanged lines hidden (view full) ---

1707
1708 case LZMA_STREAM_END:
1709 lzma_end(&zip->zipx_lzma_stream);
1710 zip->zipx_lzma_valid = 0;
1711
1712 if((int64_t) zip->zipx_lzma_stream.total_in !=
1713 zip->entry_bytes_remaining)
1714 {
1620 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1715 archive_set_error(&a->archive,
1716 ARCHIVE_ERRNO_MISC,
1621 "xz premature end of stream");
1622 return (ARCHIVE_FATAL);
1623 }
1624
1625 zip->end_of_entry = 1;
1626 break;
1627 }
1628

--- 28 unchanged lines hidden (view full) ---

1657
1658 /* Initialize decompressor if not yet initialized. */
1659 if (!zip->decompress_init) {
1660 ret = zipx_lzma_alone_init(a, zip);
1661 if (ret != ARCHIVE_OK)
1662 return (ret);
1663 }
1664
1717 "xz premature end of stream");
1718 return (ARCHIVE_FATAL);
1719 }
1720
1721 zip->end_of_entry = 1;
1722 break;
1723 }
1724

--- 28 unchanged lines hidden (view full) ---

1753
1754 /* Initialize decompressor if not yet initialized. */
1755 if (!zip->decompress_init) {
1756 ret = zipx_lzma_alone_init(a, zip);
1757 if (ret != ARCHIVE_OK)
1758 return (ret);
1759 }
1760
1665 /* Fetch more compressed data. The same note as in deflate handler applies
1666 * here as well:
1761 /* Fetch more compressed data. The same note as in deflate handler
1762 * applies here as well:
1667 *
1668 * Note: '1' here is a performance optimization. Recall that the
1763 *
1764 * Note: '1' here is a performance optimization. Recall that the
1669 * decompression layer returns a count of available bytes; asking for more
1670 * than that forces the decompressor to combine reads by copying data.
1765 * decompression layer returns a count of available bytes; asking for
1766 * more than that forces the decompressor to combine reads by copying
1767 * data.
1671 */
1672 compressed_buf = __archive_read_ahead(a, 1, &bytes_avail);
1673 if (bytes_avail < 0) {
1674 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1675 "Truncated lzma file body");
1676 return (ARCHIVE_FATAL);
1677 }
1678
1679 /* Set decompressor parameters. */
1680 in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail);
1681
1682 zip->zipx_lzma_stream.next_in = compressed_buf;
1683 zip->zipx_lzma_stream.avail_in = in_bytes;
1684 zip->zipx_lzma_stream.total_in = 0;
1685 zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
1686 zip->zipx_lzma_stream.avail_out =
1768 */
1769 compressed_buf = __archive_read_ahead(a, 1, &bytes_avail);
1770 if (bytes_avail < 0) {
1771 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1772 "Truncated lzma file body");
1773 return (ARCHIVE_FATAL);
1774 }
1775
1776 /* Set decompressor parameters. */
1777 in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail);
1778
1779 zip->zipx_lzma_stream.next_in = compressed_buf;
1780 zip->zipx_lzma_stream.avail_in = in_bytes;
1781 zip->zipx_lzma_stream.total_in = 0;
1782 zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
1783 zip->zipx_lzma_stream.avail_out =
1687 /* These lzma_alone streams lack end of stream marker, so let's make
1688 * sure the unpacker won't try to unpack more than it's supposed to. */
1784 /* These lzma_alone streams lack end of stream marker, so let's
1785 * make sure the unpacker won't try to unpack more than it's
1786 * supposed to. */
1689 zipmin((int64_t) zip->uncompressed_buffer_size,
1690 zip->entry->uncompressed_size -
1691 zip->entry_uncompressed_bytes_read);
1692 zip->zipx_lzma_stream.total_out = 0;
1693
1694 /* Perform the decompression. */
1695 lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
1696 switch(lz_ret) {

--- 108 unchanged lines hidden (view full) ---

1805
1806 /* Perform further Ppmd8 initialization. */
1807 if(!__archive_ppmd8_functions.Ppmd8_RangeDec_Init(&zip->ppmd8)) {
1808 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1809 "PPMd8 stream range decoder initialization error");
1810 return (ARCHIVE_FATAL);
1811 }
1812
1787 zipmin((int64_t) zip->uncompressed_buffer_size,
1788 zip->entry->uncompressed_size -
1789 zip->entry_uncompressed_bytes_read);
1790 zip->zipx_lzma_stream.total_out = 0;
1791
1792 /* Perform the decompression. */
1793 lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
1794 switch(lz_ret) {

--- 108 unchanged lines hidden (view full) ---

1903
1904 /* Perform further Ppmd8 initialization. */
1905 if(!__archive_ppmd8_functions.Ppmd8_RangeDec_Init(&zip->ppmd8)) {
1906 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1907 "PPMd8 stream range decoder initialization error");
1908 return (ARCHIVE_FATAL);
1909 }
1910
1813 __archive_ppmd8_functions.Ppmd8_Init(&zip->ppmd8, order, restore_method);
1911 __archive_ppmd8_functions.Ppmd8_Init(&zip->ppmd8, order,
1912 restore_method);
1814
1815 /* Allocate the buffer that will hold uncompressed data. */
1816 free(zip->uncompressed_buffer);
1817
1818 zip->uncompressed_buffer_size = 256 * 1024;
1819 zip->uncompressed_buffer =
1820 (uint8_t*) malloc(zip->uncompressed_buffer_size);
1821

--- 29 unchanged lines hidden (view full) ---

1851 /* If we're here for the first time, initialize Ppmd8 decompression
1852 * context first. */
1853 if(!zip->decompress_init) {
1854 ret = zipx_ppmd8_init(a, zip);
1855 if(ret != ARCHIVE_OK)
1856 return ret;
1857 }
1858
1913
1914 /* Allocate the buffer that will hold uncompressed data. */
1915 free(zip->uncompressed_buffer);
1916
1917 zip->uncompressed_buffer_size = 256 * 1024;
1918 zip->uncompressed_buffer =
1919 (uint8_t*) malloc(zip->uncompressed_buffer_size);
1920

--- 29 unchanged lines hidden (view full) ---

1950 /* If we're here for the first time, initialize Ppmd8 decompression
1951 * context first. */
1952 if(!zip->decompress_init) {
1953 ret = zipx_ppmd8_init(a, zip);
1954 if(ret != ARCHIVE_OK)
1955 return ret;
1956 }
1957
1859 /* Fetch for more data. We're reading 1 byte here, but libarchive should
1860 * prefetch more bytes. */
1958 /* Fetch for more data. We're reading 1 byte here, but libarchive
1959 * should prefetch more bytes. */
1861 (void) __archive_read_ahead(a, 1, &bytes_avail);
1862 if(bytes_avail < 0) {
1863 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1864 "Truncated PPMd8 file body");
1865 return (ARCHIVE_FATAL);
1866 }
1867
1868 /* This counter will be updated inside ppmd_read(), which at one
1869 * point will be called by Ppmd8_DecodeSymbol. */
1870 zip->zipx_ppmd_read_compressed = 0;
1871
1872 /* Decompression loop. */
1873 do {
1960 (void) __archive_read_ahead(a, 1, &bytes_avail);
1961 if(bytes_avail < 0) {
1962 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1963 "Truncated PPMd8 file body");
1964 return (ARCHIVE_FATAL);
1965 }
1966
1967 /* This counter will be updated inside ppmd_read(), which at one
1968 * point will be called by Ppmd8_DecodeSymbol. */
1969 zip->zipx_ppmd_read_compressed = 0;
1970
1971 /* Decompression loop. */
1972 do {
1874 int sym = __archive_ppmd8_functions.Ppmd8_DecodeSymbol(&zip->ppmd8);
1973 int sym = __archive_ppmd8_functions.Ppmd8_DecodeSymbol(
1974 &zip->ppmd8);
1875 if(sym < 0) {
1876 zip->end_of_entry = 1;
1877 break;
1878 }
1879
1880 /* This field is set by ppmd_read() when there was no more data
1881 * to be read. */
1882 if(zip->ppmd8_stream_failed) {
1975 if(sym < 0) {
1976 zip->end_of_entry = 1;
1977 break;
1978 }
1979
1980 /* This field is set by ppmd_read() when there was no more data
1981 * to be read. */
1982 if(zip->ppmd8_stream_failed) {
1883 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1884 "Truncated PPMd8 file body");
1983 archive_set_error(&a->archive,
1984 ARCHIVE_ERRNO_FILE_FORMAT,
1985 "Truncated PPMd8 file body");
1885 return (ARCHIVE_FATAL);
1886 }
1887
1888 zip->uncompressed_buffer[consumed_bytes] = (uint8_t) sym;
1889 ++consumed_bytes;
1890 } while(consumed_bytes < zip->uncompressed_buffer_size);
1891
1892 /* Update pointers for libarchive. */

--- 87 unchanged lines hidden (view full) ---

1980 if(bytes_avail < 0) {
1981 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1982 "Truncated bzip2 file body");
1983 return (ARCHIVE_FATAL);
1984 }
1985
1986 in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail);
1987 if(in_bytes < 1) {
1986 return (ARCHIVE_FATAL);
1987 }
1988
1989 zip->uncompressed_buffer[consumed_bytes] = (uint8_t) sym;
1990 ++consumed_bytes;
1991 } while(consumed_bytes < zip->uncompressed_buffer_size);
1992
1993 /* Update pointers for libarchive. */

--- 87 unchanged lines hidden (view full) ---

2081 if(bytes_avail < 0) {
2082 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2083 "Truncated bzip2 file body");
2084 return (ARCHIVE_FATAL);
2085 }
2086
2087 in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail);
2088 if(in_bytes < 1) {
1988 /* libbz2 doesn't complain when caller feeds avail_in == 0. It will
1989 * actually return success in this case, which is undesirable. This is
1990 * why we need to make this check manually. */
2089 /* libbz2 doesn't complain when caller feeds avail_in == 0.
2090 * It will actually return success in this case, which is
2091 * undesirable. This is why we need to make this check
2092 * manually. */
1991
1992 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1993 "Truncated bzip2 file body");
1994 return (ARCHIVE_FATAL);
1995 }
1996
1997 /* Setup buffer boundaries. */
1998 zip->bzstream.next_in = (char*)(uintptr_t) compressed_buff;

--- 10 unchanged lines hidden (view full) ---

2009 switch(r) {
2010 case BZ_STREAM_END:
2011 /* If we're at the end of the stream, deinitialize the
2012 * decompression context now. */
2013 switch(BZ2_bzDecompressEnd(&zip->bzstream)) {
2014 case BZ_OK:
2015 break;
2016 default:
2093
2094 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2095 "Truncated bzip2 file body");
2096 return (ARCHIVE_FATAL);
2097 }
2098
2099 /* Setup buffer boundaries. */
2100 zip->bzstream.next_in = (char*)(uintptr_t) compressed_buff;

--- 10 unchanged lines hidden (view full) ---

2111 switch(r) {
2112 case BZ_STREAM_END:
2113 /* If we're at the end of the stream, deinitialize the
2114 * decompression context now. */
2115 switch(BZ2_bzDecompressEnd(&zip->bzstream)) {
2116 case BZ_OK:
2117 break;
2118 default:
2017 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2018 "Failed to clean up bzip2 decompressor");
2119 archive_set_error(&a->archive,
2120 ARCHIVE_ERRNO_MISC,
2121 "Failed to clean up bzip2 "
2122 "decompressor");
2019 return ARCHIVE_FATAL;
2020 }
2021
2022 zip->end_of_entry = 1;
2023 break;
2024 case BZ_OK:
2123 return ARCHIVE_FATAL;
2124 }
2125
2126 zip->end_of_entry = 1;
2127 break;
2128 case BZ_OK:
2025 /* The decompressor has successfully decoded this chunk of
2026 * data, but more data is still in queue. */
2129 /* The decompressor has successfully decoded this
2130 * chunk of data, but more data is still in queue. */
2027 break;
2028 default:
2029 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2030 "bzip2 decompression failed");
2031 return ARCHIVE_FATAL;
2032 }
2033
2034 /* Update the pointers so decompressor can continue decoding. */

--- 91 unchanged lines hidden (view full) ---

2126 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2127 "Truncated ZIP file body");
2128 return (ARCHIVE_FATAL);
2129 }
2130
2131 if (zip->tctx_valid || zip->cctx_valid) {
2132 if (zip->decrypted_bytes_remaining < (size_t)bytes_avail) {
2133 size_t buff_remaining =
2131 break;
2132 default:
2133 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2134 "bzip2 decompression failed");
2135 return ARCHIVE_FATAL;
2136 }
2137
2138 /* Update the pointers so decompressor can continue decoding. */

--- 91 unchanged lines hidden (view full) ---

2230 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2231 "Truncated ZIP file body");
2232 return (ARCHIVE_FATAL);
2233 }
2234
2235 if (zip->tctx_valid || zip->cctx_valid) {
2236 if (zip->decrypted_bytes_remaining < (size_t)bytes_avail) {
2237 size_t buff_remaining =
2134 (zip->decrypted_buffer + zip->decrypted_buffer_size)
2135 - (zip->decrypted_ptr + zip->decrypted_bytes_remaining);
2238 (zip->decrypted_buffer +
2239 zip->decrypted_buffer_size)
2240 - (zip->decrypted_ptr +
2241 zip->decrypted_bytes_remaining);
2136
2137 if (buff_remaining > (size_t)bytes_avail)
2138 buff_remaining = (size_t)bytes_avail;
2139
2140 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) &&
2141 zip->entry_bytes_remaining > 0) {
2142 if ((int64_t)(zip->decrypted_bytes_remaining
2143 + buff_remaining)
2144 > zip->entry_bytes_remaining) {
2145 if (zip->entry_bytes_remaining <
2242
2243 if (buff_remaining > (size_t)bytes_avail)
2244 buff_remaining = (size_t)bytes_avail;
2245
2246 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) &&
2247 zip->entry_bytes_remaining > 0) {
2248 if ((int64_t)(zip->decrypted_bytes_remaining
2249 + buff_remaining)
2250 > zip->entry_bytes_remaining) {
2251 if (zip->entry_bytes_remaining <
2146 (int64_t)zip->decrypted_bytes_remaining)
2252 (int64_t)zip->decrypted_bytes_remaining)
2147 buff_remaining = 0;
2148 else
2149 buff_remaining =
2150 (size_t)zip->entry_bytes_remaining
2253 buff_remaining = 0;
2254 else
2255 buff_remaining =
2256 (size_t)zip->entry_bytes_remaining
2151 - zip->decrypted_bytes_remaining;
2257 - zip->decrypted_bytes_remaining;
2152 }
2153 }
2154 if (buff_remaining > 0) {
2155 if (zip->tctx_valid) {
2156 trad_enc_decrypt_update(&zip->tctx,
2157 compressed_buff, buff_remaining,
2158 zip->decrypted_ptr
2159 + zip->decrypted_bytes_remaining,
2160 buff_remaining);
2161 } else {
2162 size_t dsize = buff_remaining;
2163 archive_decrypto_aes_ctr_update(
2164 &zip->cctx,
2165 compressed_buff, buff_remaining,
2166 zip->decrypted_ptr
2167 + zip->decrypted_bytes_remaining,
2168 &dsize);
2169 }
2258 }
2259 }
2260 if (buff_remaining > 0) {
2261 if (zip->tctx_valid) {
2262 trad_enc_decrypt_update(&zip->tctx,
2263 compressed_buff, buff_remaining,
2264 zip->decrypted_ptr
2265 + zip->decrypted_bytes_remaining,
2266 buff_remaining);
2267 } else {
2268 size_t dsize = buff_remaining;
2269 archive_decrypto_aes_ctr_update(
2270 &zip->cctx,
2271 compressed_buff, buff_remaining,
2272 zip->decrypted_ptr
2273 + zip->decrypted_bytes_remaining,
2274 &dsize);
2275 }
2170 zip->decrypted_bytes_remaining += buff_remaining;
2276 zip->decrypted_bytes_remaining +=
2277 buff_remaining;
2171 }
2172 }
2173 bytes_avail = zip->decrypted_bytes_remaining;
2174 compressed_buff = (const char *)zip->decrypted_ptr;
2175 }
2176
2177 /*
2178 * A bug in zlib.h: stream.next_in should be marked 'const'

--- 567 unchanged lines hidden (view full) ---

2746
2747 zip = (struct zip *)(a->format->data);
2748
2749#ifdef HAVE_ZLIB_H
2750 if (zip->stream_valid)
2751 inflateEnd(&zip->stream);
2752#endif
2753
2278 }
2279 }
2280 bytes_avail = zip->decrypted_bytes_remaining;
2281 compressed_buff = (const char *)zip->decrypted_ptr;
2282 }
2283
2284 /*
2285 * A bug in zlib.h: stream.next_in should be marked 'const'

--- 567 unchanged lines hidden (view full) ---

2853
2854 zip = (struct zip *)(a->format->data);
2855
2856#ifdef HAVE_ZLIB_H
2857 if (zip->stream_valid)
2858 inflateEnd(&zip->stream);
2859#endif
2860
2754#if HAVA_LZMA_H && HAVE_LIBLZMA
2861#if HAVE_LZMA_H && HAVE_LIBLZMA
2755 if (zip->zipx_lzma_valid) {
2756 lzma_end(&zip->zipx_lzma_stream);
2757 }
2758#endif
2759
2760#ifdef HAVE_BZLIB_H
2761 if (zip->bzstream_valid) {
2762 BZ2_bzDecompressEnd(&zip->bzstream);

--- 623 unchanged lines hidden (view full) ---

3386 __archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
3387 archive_string_free(&dir->rsrcname);
3388 __archive_rb_tree_insert_node(&zip->tree, &dir->node);
3389 }
3390 archive_string_free(&str);
3391}
3392
3393static int
2862 if (zip->zipx_lzma_valid) {
2863 lzma_end(&zip->zipx_lzma_stream);
2864 }
2865#endif
2866
2867#ifdef HAVE_BZLIB_H
2868 if (zip->bzstream_valid) {
2869 BZ2_bzDecompressEnd(&zip->bzstream);

--- 623 unchanged lines hidden (view full) ---

3493 __archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
3494 archive_string_free(&dir->rsrcname);
3495 __archive_rb_tree_insert_node(&zip->tree, &dir->node);
3496 }
3497 archive_string_free(&str);
3498}
3499
3500static int
3394slurp_central_directory(struct archive_read *a, struct zip *zip)
3501slurp_central_directory(struct archive_read *a, struct archive_entry* entry,
3502 struct zip *zip)
3395{
3396 ssize_t i;
3397 unsigned found;
3398 int64_t correction;
3399 ssize_t bytes_avail;
3400 const char *p;
3401
3402 /*

--- 93 unchanged lines hidden (view full) ---

3496 zip_entry->decdat = p[13];
3497 else
3498 zip_entry->decdat = p[19];
3499 zip_entry->compressed_size = archive_le32dec(p + 20);
3500 zip_entry->uncompressed_size = archive_le32dec(p + 24);
3501 filename_length = archive_le16dec(p + 28);
3502 extra_length = archive_le16dec(p + 30);
3503 comment_length = archive_le16dec(p + 32);
3503{
3504 ssize_t i;
3505 unsigned found;
3506 int64_t correction;
3507 ssize_t bytes_avail;
3508 const char *p;
3509
3510 /*

--- 93 unchanged lines hidden (view full) ---

3604 zip_entry->decdat = p[13];
3605 else
3606 zip_entry->decdat = p[19];
3607 zip_entry->compressed_size = archive_le32dec(p + 20);
3608 zip_entry->uncompressed_size = archive_le32dec(p + 24);
3609 filename_length = archive_le16dec(p + 28);
3610 extra_length = archive_le16dec(p + 30);
3611 comment_length = archive_le16dec(p + 32);
3504 /* disk_start = archive_le16dec(p + 34); */ /* Better be zero. */
3505 /* internal_attributes = archive_le16dec(p + 36); */ /* text bit */
3612 /* disk_start = archive_le16dec(p + 34);
3613 * Better be zero.
3614 * internal_attributes = archive_le16dec(p + 36);
3615 * text bit */
3506 external_attributes = archive_le32dec(p + 38);
3507 zip_entry->local_header_offset =
3508 archive_le32dec(p + 42) + correction;
3509
3510 /* If we can't guess the mode, leave it zero here;
3511 when we read the local file header we might get
3512 more information. */
3513 if (zip_entry->system == 3) {

--- 19 unchanged lines hidden (view full) ---

3533 p = __archive_read_ahead(a, filename_length + extra_length,
3534 NULL);
3535 if (p == NULL) {
3536 archive_set_error(&a->archive,
3537 ARCHIVE_ERRNO_FILE_FORMAT,
3538 "Truncated ZIP file header");
3539 return ARCHIVE_FATAL;
3540 }
3616 external_attributes = archive_le32dec(p + 38);
3617 zip_entry->local_header_offset =
3618 archive_le32dec(p + 42) + correction;
3619
3620 /* If we can't guess the mode, leave it zero here;
3621 when we read the local file header we might get
3622 more information. */
3623 if (zip_entry->system == 3) {

--- 19 unchanged lines hidden (view full) ---

3643 p = __archive_read_ahead(a, filename_length + extra_length,
3644 NULL);
3645 if (p == NULL) {
3646 archive_set_error(&a->archive,
3647 ARCHIVE_ERRNO_FILE_FORMAT,
3648 "Truncated ZIP file header");
3649 return ARCHIVE_FATAL;
3650 }
3541 if (ARCHIVE_OK != process_extra(a, p + filename_length, extra_length, zip_entry)) {
3651 if (ARCHIVE_OK != process_extra(a, entry, p + filename_length,
3652 extra_length, zip_entry)) {
3542 return ARCHIVE_FATAL;
3543 }
3544
3545 /*
3546 * Mac resource fork files are stored under the
3547 * "__MACOSX/" directory, so we should check if
3548 * it is.
3549 */

--- 5 unchanged lines hidden (view full) ---

3555 name = p;
3556 r = rsrc_basename(name, filename_length);
3557 if (filename_length >= 9 &&
3558 strncmp("__MACOSX/", name, 9) == 0) {
3559 /* If this file is not a resource fork nor
3560 * a directory. We should treat it as a non
3561 * resource fork file to expose it. */
3562 if (name[filename_length-1] != '/' &&
3653 return ARCHIVE_FATAL;
3654 }
3655
3656 /*
3657 * Mac resource fork files are stored under the
3658 * "__MACOSX/" directory, so we should check if
3659 * it is.
3660 */

--- 5 unchanged lines hidden (view full) ---

3666 name = p;
3667 r = rsrc_basename(name, filename_length);
3668 if (filename_length >= 9 &&
3669 strncmp("__MACOSX/", name, 9) == 0) {
3670 /* If this file is not a resource fork nor
3671 * a directory. We should treat it as a non
3672 * resource fork file to expose it. */
3673 if (name[filename_length-1] != '/' &&
3563 (r - name < 3 || r[0] != '.' || r[1] != '_')) {
3674 (r - name < 3 || r[0] != '.' ||
3675 r[1] != '_')) {
3564 __archive_rb_tree_insert_node(
3565 &zip->tree, &zip_entry->node);
3566 /* Expose its parent directories. */
3567 expose_parent_dirs(zip, name,
3568 filename_length);
3569 } else {
3570 /* This file is a resource fork file or
3571 * a directory. */

--- 60 unchanged lines hidden (view full) ---

3632 int64_t offset = archive_filter_bytes(&a->archive, 0);
3633 size_t remaining_bytes, metadata_bytes;
3634 ssize_t hsize;
3635 int ret = ARCHIVE_OK, eof;
3636
3637 switch(rsrc->compression) {
3638 case 0: /* No compression. */
3639 if (rsrc->uncompressed_size != rsrc->compressed_size) {
3676 __archive_rb_tree_insert_node(
3677 &zip->tree, &zip_entry->node);
3678 /* Expose its parent directories. */
3679 expose_parent_dirs(zip, name,
3680 filename_length);
3681 } else {
3682 /* This file is a resource fork file or
3683 * a directory. */

--- 60 unchanged lines hidden (view full) ---

3744 int64_t offset = archive_filter_bytes(&a->archive, 0);
3745 size_t remaining_bytes, metadata_bytes;
3746 ssize_t hsize;
3747 int ret = ARCHIVE_OK, eof;
3748
3749 switch(rsrc->compression) {
3750 case 0: /* No compression. */
3751 if (rsrc->uncompressed_size != rsrc->compressed_size) {
3640 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3641 "Malformed OS X metadata entry: inconsistent size");
3752 archive_set_error(&a->archive,
3753 ARCHIVE_ERRNO_FILE_FORMAT,
3754 "Malformed OS X metadata entry: "
3755 "inconsistent size");
3642 return (ARCHIVE_FATAL);
3643 }
3644#ifdef HAVE_ZLIB_H
3645 case 8: /* Deflate compression. */
3646#endif
3647 break;
3648 default: /* Unsupported compression. */
3649 /* Return a warning. */

--- 142 unchanged lines hidden (view full) ---

3792 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
3793 zip->has_encrypted_entries = 0;
3794
3795 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
3796 if (a->archive.archive_format_name == NULL)
3797 a->archive.archive_format_name = "ZIP";
3798
3799 if (zip->zip_entries == NULL) {
3756 return (ARCHIVE_FATAL);
3757 }
3758#ifdef HAVE_ZLIB_H
3759 case 8: /* Deflate compression. */
3760#endif
3761 break;
3762 default: /* Unsupported compression. */
3763 /* Return a warning. */

--- 142 unchanged lines hidden (view full) ---

3906 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
3907 zip->has_encrypted_entries = 0;
3908
3909 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
3910 if (a->archive.archive_format_name == NULL)
3911 a->archive.archive_format_name = "ZIP";
3912
3913 if (zip->zip_entries == NULL) {
3800 r = slurp_central_directory(a, zip);
3914 r = slurp_central_directory(a, entry, zip);
3801 if (r != ARCHIVE_OK)
3802 return r;
3803 /* Get first entry whose local header offset is lower than
3804 * other entries in the archive file. */
3805 zip->entry =
3806 (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
3807 } else if (zip->entry != NULL) {
3808 /* Get next entry in local header offset order. */

--- 13 unchanged lines hidden (view full) ---

3822 if (zip->cctx_valid)
3823 archive_decrypto_aes_ctr_release(&zip->cctx);
3824 if (zip->hctx_valid)
3825 archive_hmac_sha1_cleanup(&zip->hctx);
3826 zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
3827 __archive_read_reset_passphrase(a);
3828
3829 /* File entries are sorted by the header offset, we should mostly
3915 if (r != ARCHIVE_OK)
3916 return r;
3917 /* Get first entry whose local header offset is lower than
3918 * other entries in the archive file. */
3919 zip->entry =
3920 (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
3921 } else if (zip->entry != NULL) {
3922 /* Get next entry in local header offset order. */

--- 13 unchanged lines hidden (view full) ---

3936 if (zip->cctx_valid)
3937 archive_decrypto_aes_ctr_release(&zip->cctx);
3938 if (zip->hctx_valid)
3939 archive_hmac_sha1_cleanup(&zip->hctx);
3940 zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
3941 __archive_read_reset_passphrase(a);
3942
3943 /* File entries are sorted by the header offset, we should mostly
3830 * use __archive_read_consume to advance a read point to avoid redundant
3831 * data reading. */
3944 * use __archive_read_consume to advance a read point to avoid
3945 * redundant data reading. */
3832 offset = archive_filter_bytes(&a->archive, 0);
3833 if (offset < zip->entry->local_header_offset)
3834 __archive_read_consume(a,
3835 zip->entry->local_header_offset - offset);
3836 else if (offset != zip->entry->local_header_offset) {
3837 __archive_read_seek(a, zip->entry->local_header_offset,
3838 SEEK_SET);
3839 }

--- 74 unchanged lines hidden ---
3946 offset = archive_filter_bytes(&a->archive, 0);
3947 if (offset < zip->entry->local_header_offset)
3948 __archive_read_consume(a,
3949 zip->entry->local_header_offset - offset);
3950 else if (offset != zip->entry->local_header_offset) {
3951 __archive_read_seek(a, zip->entry->local_header_offset,
3952 SEEK_SET);
3953 }

--- 74 unchanged lines hidden ---