Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
archive-zip: support more than 65535 entries
Support more than 65535 entries cleanly by writing a "zip64 end of
central directory record" (with a 64-bit field for the number of
entries) before the usual "end of central directory record" (which
contains only a 16-bit field).  InfoZIP's zip does the same.
Archives with 65535 or less entries are not affected.

Programs that extract all files like InfoZIP's zip and 7-Zip
ignored the field and could extract all files already.  Software
that relies on the ZIP file directory to show a list of contained
files quickly to simulate to normal directory like Windows'
built-in ZIP functionality only saw a subset of the included files.

Windows supports ZIP64 since Vista according to
https://en.wikipedia.org/wiki/Zip_%28file_format%29#ZIP64.

Suggested-by: Johannes Schauer <josch@debian.org>
Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Aug 28, 2015
1 parent 0f747f9 commit 88329ca
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
93 changes: 90 additions & 3 deletions archive-zip.c
Expand Up @@ -16,7 +16,9 @@ static unsigned int zip_dir_size;

static unsigned int zip_offset;
static unsigned int zip_dir_offset;
static unsigned int zip_dir_entries;
static uint64_t zip_dir_entries;

static unsigned int max_creator_version;

#define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
#define ZIP_STREAM (1 << 3)
Expand Down Expand Up @@ -86,6 +88,28 @@ struct zip_extra_mtime {
unsigned char _end[1];
};

struct zip64_dir_trailer {
unsigned char magic[4];
unsigned char record_size[8];
unsigned char creator_version[2];
unsigned char version[2];
unsigned char disk[4];
unsigned char directory_start_disk[4];
unsigned char entries_on_this_disk[8];
unsigned char entries[8];
unsigned char size[8];
unsigned char offset[8];
unsigned char _end[1];
};

struct zip64_dir_trailer_locator {
unsigned char magic[4];
unsigned char disk[4];
unsigned char offset[8];
unsigned char number_of_disks[4];
unsigned char _end[1];
};

/*
* On ARM, padding is added at the end of the struct, so a simple
* sizeof(struct ...) reports two bytes more than the payload size
Expand All @@ -98,6 +122,12 @@ struct zip_extra_mtime {
#define ZIP_EXTRA_MTIME_SIZE offsetof(struct zip_extra_mtime, _end)
#define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \
(ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags))
#define ZIP64_DIR_TRAILER_SIZE offsetof(struct zip64_dir_trailer, _end)
#define ZIP64_DIR_TRAILER_RECORD_SIZE \
(ZIP64_DIR_TRAILER_SIZE - \
offsetof(struct zip64_dir_trailer, creator_version))
#define ZIP64_DIR_TRAILER_LOCATOR_SIZE \
offsetof(struct zip64_dir_trailer_locator, _end)

static void copy_le16(unsigned char *dest, unsigned int n)
{
Expand All @@ -113,6 +143,31 @@ static void copy_le32(unsigned char *dest, unsigned int n)
dest[3] = 0xff & (n >> 030);
}

static void copy_le64(unsigned char *dest, uint64_t n)
{
dest[0] = 0xff & n;
dest[1] = 0xff & (n >> 010);
dest[2] = 0xff & (n >> 020);
dest[3] = 0xff & (n >> 030);
dest[4] = 0xff & (n >> 040);
dest[5] = 0xff & (n >> 050);
dest[6] = 0xff & (n >> 060);
dest[7] = 0xff & (n >> 070);
}

static uint64_t clamp_max(uint64_t n, uint64_t max, int *clamped)
{
if (n <= max)
return n;
*clamped = 1;
return max;
}

static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped)
{
copy_le16(dest, clamp_max(n, 0xffff, clamped));
}

static void *zlib_deflate_raw(void *data, unsigned long size,
int compression_level,
unsigned long *compressed_size)
Expand Down Expand Up @@ -282,6 +337,9 @@ static int write_zip_entry(struct archiver_args *args,
sha1_to_hex(sha1));
}

if (creator_version > max_creator_version)
max_creator_version = creator_version;

if (buffer && method == 8) {
out = deflated = zlib_deflate_raw(buffer, size,
args->compression_level,
Expand Down Expand Up @@ -439,20 +497,49 @@ static int write_zip_entry(struct archiver_args *args,
return 0;
}

static void write_zip64_trailer(void)
{
struct zip64_dir_trailer trailer64;
struct zip64_dir_trailer_locator locator64;

copy_le32(trailer64.magic, 0x06064b50);
copy_le64(trailer64.record_size, ZIP64_DIR_TRAILER_RECORD_SIZE);
copy_le16(trailer64.creator_version, max_creator_version);
copy_le16(trailer64.version, 45);
copy_le32(trailer64.disk, 0);
copy_le32(trailer64.directory_start_disk, 0);
copy_le64(trailer64.entries_on_this_disk, zip_dir_entries);
copy_le64(trailer64.entries, zip_dir_entries);
copy_le64(trailer64.size, zip_dir_offset);
copy_le64(trailer64.offset, zip_offset);

copy_le32(locator64.magic, 0x07064b50);
copy_le32(locator64.disk, 0);
copy_le64(locator64.offset, zip_offset + zip_dir_offset);
copy_le32(locator64.number_of_disks, 1);

write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE);
write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
}

static void write_zip_trailer(const unsigned char *sha1)
{
struct zip_dir_trailer trailer;
int clamped = 0;

copy_le32(trailer.magic, 0x06054b50);
copy_le16(trailer.disk, 0);
copy_le16(trailer.directory_start_disk, 0);
copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
copy_le16(trailer.entries, zip_dir_entries);
copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries,
&clamped);
copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
copy_le32(trailer.size, zip_dir_offset);
copy_le32(trailer.offset, zip_offset);
copy_le16(trailer.comment_length, sha1 ? 40 : 0);

write_or_die(1, zip_dir, zip_dir_offset);
if (clamped)
write_zip64_trailer();
write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
if (sha1)
write_or_die(1, sha1_to_hex(sha1), 40);
Expand Down
2 changes: 1 addition & 1 deletion t/t5004-archive-corner-cases.sh
Expand Up @@ -122,7 +122,7 @@ test_lazy_prereq ZIPINFO '
test "x$n" = "x0"
'

test_expect_failure ZIPINFO 'zip archive with many entries' '
test_expect_success ZIPINFO 'zip archive with many entries' '
# add a directory with 256 files
mkdir 00 &&
for a in 0 1 2 3 4 5 6 7 8 9 a b c d e f
Expand Down

0 comments on commit 88329ca

Please sign in to comment.