diff options
Diffstat (limited to 'src/Common/libzip/zip_open.c')
-rw-r--r-- | src/Common/libzip/zip_open.c | 427 |
1 files changed, 207 insertions, 220 deletions
diff --git a/src/Common/libzip/zip_open.c b/src/Common/libzip/zip_open.c index f62f95f6..1886c9d2 100644 --- a/src/Common/libzip/zip_open.c +++ b/src/Common/libzip/zip_open.c @@ -2,3 +2,3 @@ zip_open.c -- open zip archive by name - Copyright (C) 1999-2016 Dieter Baron and Thomas Klausner + Copyright (C) 1999-2017 Dieter Baron and Thomas Klausner @@ -34,3 +34,2 @@ -#include <sys/stat.h> #include <limits.h> @@ -39,2 +38,3 @@ #include <string.h> +#include <sys/stat.h> @@ -60,4 +60,3 @@ static zip_cdir_t *_zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip ZIP_EXTERN zip_t * -zip_open(const char *fn, int _flags, int *zep) -{ +zip_open(const char *fn, int _flags, int *zep) { zip_t *za; @@ -86,4 +85,3 @@ zip_open(const char *fn, int _flags, int *zep) ZIP_EXTERN zip_t * -zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) -{ +zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) { static zip_int64_t needed_support_read = -1; @@ -97,3 +95,3 @@ zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) zip_error_set(error, ZIP_ER_INVAL, 0); - return NULL; + return NULL; } @@ -103,14 +101,14 @@ zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) if (needed_support_read == -1) { - needed_support_read = zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_SEEK, ZIP_SOURCE_TELL, ZIP_SOURCE_STAT, -1); - needed_support_write = zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_REMOVE, -1); + needed_support_read = zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_SEEK, ZIP_SOURCE_TELL, ZIP_SOURCE_STAT, -1); + needed_support_write = zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_REMOVE, -1); } if ((supported & needed_support_read) != needed_support_read) { - zip_error_set(error, ZIP_ER_OPNOTSUPP, 0); - return NULL; + zip_error_set(error, ZIP_ER_OPNOTSUPP, 0); + return NULL; } if ((supported & needed_support_write) != needed_support_write) { - flags |= ZIP_RDONLY; + flags |= ZIP_RDONLY; } - if ((flags & (ZIP_RDONLY|ZIP_TRUNCATE)) == (ZIP_RDONLY|ZIP_TRUNCATE)) { + if ((flags & (ZIP_RDONLY | ZIP_TRUNCATE)) == (ZIP_RDONLY | ZIP_TRUNCATE)) { zip_error_set(error, ZIP_ER_RDONLY, 0); @@ -159,12 +157,5 @@ zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) -ZIP_EXTERN void -zip_register_progress_callback(zip_t *za, zip_progress_callback_t progress_callback) -{ - za->progress_callback = progress_callback; -} - zip_t * -_zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) -{ +_zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) { zip_t *za; @@ -187,3 +178,3 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) if (len == 0) { - if ((za=_zip_allocate_new(src, flags, error)) == NULL) { + if ((za = _zip_allocate_new(src, flags, error)) == NULL) { zip_source_free(src); @@ -195,4 +186,4 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) - if ((za=_zip_allocate_new(src, flags, error)) == NULL) { - return NULL; + if ((za = _zip_allocate_new(src, flags, error)) == NULL) { + return NULL; } @@ -200,3 +191,3 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) if ((cdir = _zip_find_central_dir(za, len)) == NULL) { - _zip_error_copy(error, &za->error); + _zip_error_copy(error, &za->error); /* keep src so discard does not get rid of it */ @@ -214,2 +205,4 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) + _zip_hash_reserve_capacity(za->names, za->nentry, &za->error); + for (idx = 0; idx < za->nentry; idx++) { @@ -217,6 +210,6 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) if (name == NULL) { - /* keep src so discard does not get rid of it */ - zip_source_keep(src); - zip_discard(za); - return NULL; + /* keep src so discard does not get rid of it */ + zip_source_keep(src); + zip_discard(za); + return NULL; } @@ -241,4 +234,3 @@ _zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) void -_zip_set_open_error(int *zep, const zip_error_t *err, int ze) -{ +_zip_set_open_error(int *zep, const zip_error_t *err, int ze) { if (err) { @@ -262,4 +254,3 @@ _zip_set_open_error(int *zep, const zip_error_t *err, int ze) static zip_cdir_t * -_zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_error_t *error) -{ +_zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_error_t *error) { zip_cdir_t *cd; @@ -283,8 +274,8 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err if (eocd_offset >= EOCD64LOCLEN && memcmp(_zip_buffer_data(buffer) + eocd_offset - EOCD64LOCLEN, EOCD64LOC_MAGIC, 4) == 0) { - _zip_buffer_set_offset(buffer, eocd_offset - EOCD64LOCLEN); - cd = _zip_read_eocd64(za->src, buffer, buf_offset, za->flags, error); + _zip_buffer_set_offset(buffer, eocd_offset - EOCD64LOCLEN); + cd = _zip_read_eocd64(za->src, buffer, buf_offset, za->flags, error); } else { - _zip_buffer_set_offset(buffer, eocd_offset); - cd = _zip_read_eocd(buffer, buf_offset, za->flags, error); + _zip_buffer_set_offset(buffer, eocd_offset); + cd = _zip_read_eocd(buffer, buf_offset, za->flags, error); } @@ -305,19 +296,19 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err if (comment_len || (za->open_flags & ZIP_CHECKCONS)) { - zip_uint64_t tail_len; + zip_uint64_t tail_len; - _zip_buffer_set_offset(buffer, eocd_offset + EOCDLEN); - tail_len = _zip_buffer_left(buffer); + _zip_buffer_set_offset(buffer, eocd_offset + EOCDLEN); + tail_len = _zip_buffer_left(buffer); - if (tail_len < comment_len || ((za->open_flags & ZIP_CHECKCONS) && tail_len != comment_len)) { - zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_cdir_free(cd); - return NULL; - } + if (tail_len < comment_len || ((za->open_flags & ZIP_CHECKCONS) && tail_len != comment_len)) { + zip_error_set(error, ZIP_ER_INCONS, 0); + _zip_cdir_free(cd); + return NULL; + } - if (comment_len) { - if ((cd->comment=_zip_string_new(_zip_buffer_get(buffer, comment_len), comment_len, ZIP_FL_ENC_GUESS, error)) == NULL) { - _zip_cdir_free(cd); - return NULL; - } - } + if (comment_len) { + if ((cd->comment = _zip_string_new(_zip_buffer_get(buffer, comment_len), comment_len, ZIP_FL_ENC_GUESS, error)) == NULL) { + _zip_cdir_free(cd); + return NULL; + } + } } @@ -325,25 +316,25 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err if (cd->offset >= buf_offset) { - zip_uint8_t *data; + zip_uint8_t *data; /* if buffer already read in, use it */ - _zip_buffer_set_offset(buffer, cd->offset - buf_offset); - - if ((data = _zip_buffer_get(buffer, cd->size)) == NULL) { - zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_cdir_free(cd); - return NULL; - } - if ((cd_buffer = _zip_buffer_new(data, cd->size)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - _zip_cdir_free(cd); - return NULL; - } + _zip_buffer_set_offset(buffer, cd->offset - buf_offset); + + if ((data = _zip_buffer_get(buffer, cd->size)) == NULL) { + zip_error_set(error, ZIP_ER_INCONS, 0); + _zip_cdir_free(cd); + return NULL; + } + if ((cd_buffer = _zip_buffer_new(data, cd->size)) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + _zip_cdir_free(cd); + return NULL; + } } else { - cd_buffer = NULL; + cd_buffer = NULL; - if (zip_source_seek(za->src, (zip_int64_t)cd->offset, SEEK_SET) < 0) { - _zip_error_set_from_source(error, za->src); - _zip_cdir_free(cd); - return NULL; - } + if (zip_source_seek(za->src, (zip_int64_t)cd->offset, SEEK_SET) < 0) { + _zip_error_set_from_source(error, za->src); + _zip_cdir_free(cd); + return NULL; + } @@ -351,3 +342,3 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err if (zip_source_tell(za->src) != (zip_int64_t)cd->offset) { - zip_error_set(error, ZIP_ER_NOZIP, 0); + zip_error_set(error, ZIP_ER_NOZIP, 0); _zip_cdir_free(cd); @@ -358,6 +349,6 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err left = (zip_uint64_t)cd->size; - i=0; + i = 0; while (left > 0) { bool grown = false; - zip_int64_t entry_size; + zip_int64_t entry_size; @@ -379,3 +370,3 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err - if ((cd->entry[i].orig=_zip_dirent_new()) == NULL || (entry_size = _zip_dirent_read(cd->entry[i].orig, za->src, cd_buffer, false, error)) < 0) { + if ((cd->entry[i].orig = _zip_dirent_new()) == NULL || (entry_size = _zip_dirent_read(cd->entry[i].orig, za->src, cd_buffer, false, error)) < 0) { if (grown && zip_error_code_zip(error) == ZIP_ER_NOZIP) { @@ -384,3 +375,3 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err _zip_cdir_free(cd); - _zip_buffer_free(cd_buffer); + _zip_buffer_free(cd_buffer); return NULL; @@ -388,3 +379,3 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err i++; - left -= (zip_uint64_t)entry_size; + left -= (zip_uint64_t)entry_size; } @@ -392,6 +383,6 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err if (i != cd->nentry || left > 0) { - zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_buffer_free(cd_buffer); - _zip_cdir_free(cd); - return NULL; + zip_error_set(error, ZIP_ER_INCONS, 0); + _zip_buffer_free(cd_buffer); + _zip_cdir_free(cd); + return NULL; } @@ -399,25 +390,24 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err if (za->open_flags & ZIP_CHECKCONS) { - bool ok; - - if (cd_buffer) { - ok = _zip_buffer_eof(cd_buffer); - } - else { - zip_int64_t offset = zip_source_tell(za->src); - - if (offset < 0) { - _zip_error_set_from_source(error, za->src); - _zip_buffer_free(cd_buffer); - _zip_cdir_free(cd); - return NULL; - } - ok = ((zip_uint64_t)offset == cd->offset + cd->size); - } - - if (!ok) { - zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_buffer_free(cd_buffer); - _zip_cdir_free(cd); - return NULL; - } + bool ok; + + if (cd_buffer) { + ok = _zip_buffer_eof(cd_buffer); + } + else { + zip_int64_t offset = zip_source_tell(za->src); + + if (offset < 0) { + _zip_error_set_from_source(error, za->src); + _zip_cdir_free(cd); + return NULL; + } + ok = ((zip_uint64_t)offset == cd->offset + cd->size); + } + + if (!ok) { + zip_error_set(error, ZIP_ER_INCONS, 0); + _zip_buffer_free(cd_buffer); + _zip_cdir_free(cd); + return NULL; + } } @@ -436,4 +426,3 @@ _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_err static zip_int64_t -_zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) -{ +_zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) { zip_uint64_t i; @@ -450,3 +439,3 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) - for (i=0; i<cd->nentry; i++) { + for (i = 0; i < cd->nentry; i++) { if (cd->entry[i].orig->offset < min) @@ -458,4 +447,3 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) - j = cd->entry[i].orig->offset + cd->entry[i].orig->comp_size - + _zip_string_length(cd->entry[i].orig->filename) + LENTRYSIZE; + j = cd->entry[i].orig->offset + cd->entry[i].orig->comp_size + _zip_string_length(cd->entry[i].orig->filename) + LENTRYSIZE; if (j > max) @@ -467,5 +455,5 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) - if (zip_source_seek(za->src, (zip_int64_t)cd->entry[i].orig->offset, SEEK_SET) < 0) { - _zip_error_set_from_source(error, za->src); - return -1; + if (zip_source_seek(za->src, (zip_int64_t)cd->entry[i].orig->offset, SEEK_SET) < 0) { + _zip_error_set_from_source(error, za->src); + return -1; } @@ -490,3 +478,3 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) - return (max-min) < ZIP_INT64_MAX ? (zip_int64_t)(max-min) : ZIP_INT64_MAX; + return (max - min) < ZIP_INT64_MAX ? (zip_int64_t)(max - min) : ZIP_INT64_MAX; } @@ -499,4 +487,3 @@ _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) static int -_zip_headercomp(const zip_dirent_t *central, const zip_dirent_t *local) -{ +_zip_headercomp(const zip_dirent_t *central, const zip_dirent_t *local) { if ((central->version_needed < local->version_needed) @@ -507,13 +494,9 @@ _zip_headercomp(const zip_dirent_t *central, const zip_dirent_t *local) #endif - || (central->comp_method != local->comp_method) - || (central->last_mod != local->last_mod) - || !_zip_string_equal(central->filename, local->filename)) + || (central->comp_method != local->comp_method) || (central->last_mod != local->last_mod) || !_zip_string_equal(central->filename, local->filename)) return -1; - if ((central->crc != local->crc) || (central->comp_size != local->comp_size) - || (central->uncomp_size != local->uncomp_size)) { + if ((central->crc != local->crc) || (central->comp_size != local->comp_size) || (central->uncomp_size != local->uncomp_size)) { /* InfoZip stores valid values in local header even when data descriptor is used. This is in violation of the appnote. */ - if (((local->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 - || local->crc != 0 || local->comp_size != 0 || local->uncomp_size != 0)) + if (((local->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local->crc != 0 || local->comp_size != 0 || local->uncomp_size != 0)) return -1; @@ -526,4 +509,3 @@ _zip_headercomp(const zip_dirent_t *central, const zip_dirent_t *local) static zip_t * -_zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error) -{ +_zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error) { zip_t *za; @@ -537,4 +519,4 @@ _zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error) if (flags & ZIP_RDONLY) { - za->flags |= ZIP_AFL_RDONLY; - za->ch_flags |= ZIP_AFL_RDONLY; + za->flags |= ZIP_AFL_RDONLY; + za->ch_flags |= ZIP_AFL_RDONLY; } @@ -548,4 +530,3 @@ _zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error) static exists_t -_zip_file_exists(zip_source_t *src, zip_error_t *error) -{ +_zip_file_exists(zip_source_t *src, zip_error_t *error) { struct zip_stat st; @@ -554,4 +535,4 @@ _zip_file_exists(zip_source_t *src, zip_error_t *error) if (zip_source_stat(src, &st) != 0) { - zip_error_t *src_error = zip_source_error(src); - if (zip_error_code_zip(src_error) == ZIP_ER_READ && zip_error_code_system(src_error) == ENOENT) { + zip_error_t *src_error = zip_source_error(src); + if (zip_error_code_zip(src_error) == ZIP_ER_READ && zip_error_code_system(src_error) == ENOENT) { return EXISTS_NOT; @@ -567,4 +548,3 @@ _zip_file_exists(zip_source_t *src, zip_error_t *error) static zip_cdir_t * -_zip_find_central_dir(zip_t *za, zip_uint64_t len) -{ +_zip_find_central_dir(zip_t *za, zip_uint64_t len) { zip_cdir_t *cdir, *cdirnew; @@ -580,3 +560,3 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) zip_error_set(&za->error, ZIP_ER_NOZIP, 0); - return NULL; + return NULL; } @@ -593,4 +573,4 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) if ((buf_offset = zip_source_tell(za->src)) < 0) { - _zip_error_set_from_source(&za->error, za->src); - return NULL; + _zip_error_set_from_source(&za->error, za->src); + return NULL; } @@ -598,3 +578,3 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) if ((buffer = _zip_buffer_new_from_source(za->src, buflen, NULL, &za->error)) == NULL) { - return NULL; + return NULL; } @@ -604,4 +584,4 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) if (buflen >= CDBUFSIZE) { - /* EOCD64 locator is before EOCD, so leave place for it */ - _zip_buffer_set_offset(buffer, EOCD64LOCLEN); + /* EOCD64 locator is before EOCD, so leave place for it */ + _zip_buffer_set_offset(buffer, EOCD64LOCLEN); } @@ -610,33 +590,33 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) match = _zip_buffer_get(buffer, 0); - while ((match=_zip_memmem(match, _zip_buffer_left(buffer)-(EOCDLEN-4), (const unsigned char *)EOCD_MAGIC, 4)) != NULL) { - _zip_buffer_set_offset(buffer, (zip_uint64_t)(match - _zip_buffer_data(buffer))); - if ((cdirnew = _zip_read_cdir(za, buffer, (zip_uint64_t)buf_offset, &error)) != NULL) { - if (cdir) { - if (best <= 0) { - best = _zip_checkcons(za, cdir, &error); - } - - a = _zip_checkcons(za, cdirnew, &error); - if (best < a) { - _zip_cdir_free(cdir); - cdir = cdirnew; - best = a; - } - else { - _zip_cdir_free(cdirnew); - } - } - else { - cdir = cdirnew; - if (za->open_flags & ZIP_CHECKCONS) - best = _zip_checkcons(za, cdir, &error); - else { - best = 0; - } - } - cdirnew = NULL; - } - - match++; - _zip_buffer_set_offset(buffer, (zip_uint64_t)(match - _zip_buffer_data(buffer))); + while ((match = _zip_memmem(match, _zip_buffer_left(buffer) - (EOCDLEN - 4), (const unsigned char *)EOCD_MAGIC, 4)) != NULL) { + _zip_buffer_set_offset(buffer, (zip_uint64_t)(match - _zip_buffer_data(buffer))); + if ((cdirnew = _zip_read_cdir(za, buffer, (zip_uint64_t)buf_offset, &error)) != NULL) { + if (cdir) { + if (best <= 0) { + best = _zip_checkcons(za, cdir, &error); + } + + a = _zip_checkcons(za, cdirnew, &error); + if (best < a) { + _zip_cdir_free(cdir); + cdir = cdirnew; + best = a; + } + else { + _zip_cdir_free(cdirnew); + } + } + else { + cdir = cdirnew; + if (za->open_flags & ZIP_CHECKCONS) + best = _zip_checkcons(za, cdir, &error); + else { + best = 0; + } + } + cdirnew = NULL; + } + + match++; + _zip_buffer_set_offset(buffer, (zip_uint64_t)(match - _zip_buffer_data(buffer))); } @@ -646,5 +626,5 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) if (best < 0) { - _zip_error_copy(&za->error, &error); - _zip_cdir_free(cdir); - return NULL; + _zip_error_copy(&za->error, &error); + _zip_cdir_free(cdir); + return NULL; } @@ -656,4 +636,3 @@ _zip_find_central_dir(zip_t *za, zip_uint64_t len) static unsigned char * -_zip_memmem(const unsigned char *big, size_t biglen, const unsigned char *little, size_t littlelen) -{ +_zip_memmem(const unsigned char *big, size_t biglen, const unsigned char *little, size_t littlelen) { const unsigned char *p; @@ -662,6 +641,5 @@ _zip_memmem(const unsigned char *big, size_t biglen, const unsigned char *little return NULL; - p = big-1; - while ((p=(const unsigned char *) - memchr(p+1, little[0], (size_t)(big-(p+1))+(size_t)(biglen-littlelen)+1)) != NULL) { - if (memcmp(p+1, little+1, littlelen-1)==0) + p = big - 1; + while ((p = (const unsigned char *)memchr(p + 1, little[0], (size_t)(big - (p + 1)) + (size_t)(biglen - littlelen) + 1)) != NULL) { + if (memcmp(p + 1, little + 1, littlelen - 1) == 0) return (unsigned char *)p; @@ -674,4 +652,3 @@ _zip_memmem(const unsigned char *big, size_t biglen, const unsigned char *little static zip_cdir_t * -_zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error) -{ +_zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error) { zip_cdir_t *cd; @@ -706,8 +683,8 @@ _zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags - if (offset+size < offset) { - zip_error_set(error, ZIP_ER_SEEK, EFBIG); - return NULL; + if (offset + size < offset) { + zip_error_set(error, ZIP_ER_SEEK, EFBIG); + return NULL; } - if (offset+size > buf_offset + eocd_offset) { + if (offset + size > buf_offset + eocd_offset) { /* cdir spans past EOCD record */ @@ -717,3 +694,3 @@ _zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags - if ((flags & ZIP_CHECKCONS) && offset+size != buf_offset + eocd_offset) { + if ((flags & ZIP_CHECKCONS) && offset + size != buf_offset + eocd_offset) { zip_error_set(error, ZIP_ER_INCONS, 0); @@ -722,3 +699,3 @@ _zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags - if ((cd=_zip_cdir_new(nentry, error)) == NULL) + if ((cd = _zip_cdir_new(nentry, error)) == NULL) return NULL; @@ -734,4 +711,3 @@ _zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags static zip_cdir_t * -_zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error) -{ +_zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error) { zip_cdir_t *cd; @@ -753,4 +729,4 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse if (eocd_offset > ZIP_INT64_MAX || eocd_offset + EOCD64LEN < eocd_offset) { - zip_error_set(error, ZIP_ER_SEEK, EFBIG); - return NULL; + zip_error_set(error, ZIP_ER_SEEK, EFBIG); + return NULL; } @@ -763,14 +739,14 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse if (eocd_offset >= buf_offset && eocd_offset + EOCD64LEN <= buf_offset + _zip_buffer_size(buffer)) { - _zip_buffer_set_offset(buffer, eocd_offset - buf_offset); - free_buffer = false; + _zip_buffer_set_offset(buffer, eocd_offset - buf_offset); + free_buffer = false; } else { - if (zip_source_seek(src, (zip_int64_t)eocd_offset, SEEK_SET) < 0) { - _zip_error_set_from_source(error, src); - return NULL; - } - if ((buffer = _zip_buffer_new_from_source(src, EOCD64LEN, eocd, error)) == NULL) { - return NULL; - } - free_buffer = true; + if (zip_source_seek(src, (zip_int64_t)eocd_offset, SEEK_SET) < 0) { + _zip_error_set_from_source(error, src); + return NULL; + } + if ((buffer = _zip_buffer_new_from_source(src, EOCD64LEN, eocd, error)) == NULL) { + return NULL; + } + free_buffer = true; } @@ -779,5 +755,5 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse zip_error_set(error, ZIP_ER_INCONS, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } + if (free_buffer) { + _zip_buffer_free(buffer); + } return NULL; @@ -789,6 +765,6 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse zip_error_set(error, ZIP_ER_INCONS, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } - return NULL; + if (free_buffer) { + _zip_buffer_free(buffer); + } + return NULL; } @@ -811,2 +787,5 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse zip_error_set(error, ZIP_ER_INCONS, 0); + if (free_buffer) { + _zip_buffer_free(buffer); + } return NULL; @@ -815,2 +794,5 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse zip_error_set(error, ZIP_ER_MULTIDISK, 0); + if (free_buffer) { + _zip_buffer_free(buffer); + } return NULL; @@ -823,5 +805,5 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse zip_error_set(error, ZIP_ER_MULTIDISK, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } + if (free_buffer) { + _zip_buffer_free(buffer); + } return NULL; @@ -833,7 +815,7 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse if (!_zip_buffer_ok(buffer)) { - zip_error_set(error, ZIP_ER_INTERNAL, 0); - if (free_buffer) { - _zip_buffer_free(buffer); - } - return NULL; + zip_error_set(error, ZIP_ER_INTERNAL, 0); + if (free_buffer) { + _zip_buffer_free(buffer); + } + return NULL; } @@ -841,10 +823,15 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse if (free_buffer) { - _zip_buffer_free(buffer); + _zip_buffer_free(buffer); } - if (offset > ZIP_INT64_MAX || offset+size < offset) { - zip_error_set(error, ZIP_ER_SEEK, EFBIG); - return NULL; + if (offset > ZIP_INT64_MAX || offset + size < offset) { + zip_error_set(error, ZIP_ER_SEEK, EFBIG); + return NULL; + } + if (offset + size > buf_offset + eocd_offset) { + /* cdir spans past EOCD record */ + zip_error_set(error, ZIP_ER_INCONS, 0); + return NULL; } - if ((flags & ZIP_CHECKCONS) && offset+size != eocd_offset) { + if ((flags & ZIP_CHECKCONS) && offset + size != buf_offset + eocd_offset) { zip_error_set(error, ZIP_ER_INCONS, 0); @@ -853,3 +840,3 @@ _zip_read_eocd64(zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offse - if ((cd=_zip_cdir_new(nentry, error)) == NULL) + if ((cd = _zip_cdir_new(nentry, error)) == NULL) return NULL; |