Fixed appending zip to executable and added unit test.

Renamed MZ_STREAM_OPEN_* to MZ_OPEN_MODE_*.
Renamed MZ_STREAM_SEEK_* to MZ_SEEK_*.
This commit is contained in:
Nathan Moinvaziri 2017-10-23 17:22:36 -07:00
parent bf1b974388
commit 79ac3385f1
17 changed files with 163 additions and 160 deletions

View File

@ -138,7 +138,7 @@ int32_t minizip_add_file(void *handle, const char *path, const char *password, m
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ);
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
if (err == MZ_OK)
{
@ -404,7 +404,7 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
if ((skip == 0) && (err == MZ_OK))
{
// Some zips don't contain directory alone before file
if ((mz_stream_os_open(stream, out_path, MZ_STREAM_MODE_CREATE) != MZ_OK) &&
if ((mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE) != MZ_OK) &&
(options->exclude_path == 0) && (filename != file_info->filename))
{
// Create the directory of the output path
@ -422,7 +422,7 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
mz_make_dir(directory);
mz_stream_os_open(stream, out_path, MZ_STREAM_MODE_CREATE);
mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE);
}
}
@ -553,7 +553,7 @@ int main(int argc, char *argv[])
options.compress_level = MZ_COMPRESS_LEVEL_DEFAULT;
// Parse command line options
for (i = 1; i < argc; i++)
for (i = 1; i < argc; i += 1)
{
if ((*argv[i]) == '-')
{
@ -623,17 +623,20 @@ int main(int argc, char *argv[])
path = argv[path_arg];
mode = MZ_STREAM_MODE_READ;
mode = MZ_OPEN_MODE_READ;
if ((do_list == 0) && (do_extract == 0))
{
mode |= MZ_STREAM_MODE_WRITE;
mode |= MZ_OPEN_MODE_WRITE;
if (mz_file_exists(path) != MZ_OK)
{
// If the file doesn't exist, we don't append file
if (append)
mode |= MZ_STREAM_MODE_APPEND;
mode |= MZ_OPEN_MODE_CREATE;
}
else if (append == 1)
{
mode |= MZ_OPEN_MODE_APPEND;
}
else if (options.overwrite == 0)
{
@ -654,7 +657,11 @@ int main(int argc, char *argv[])
if (rep == 'A')
{
mode |= MZ_STREAM_MODE_APPEND;
mode |= MZ_OPEN_MODE_APPEND;
}
else if (rep == 'Y')
{
mode |= MZ_OPEN_MODE_CREATE;
}
else if (rep == 'N')
{
@ -662,9 +669,6 @@ int main(int argc, char *argv[])
return 0;
}
}
if ((mode & MZ_STREAM_MODE_APPEND) == 0)
mode |= MZ_STREAM_MODE_CREATE;
}
mz_stream_os_create(&file_stream);

View File

@ -20,6 +20,9 @@ extern "C" {
/***************************************************************************/
// MZ_VERSION
#define MZ_VERSION ("2.2.0")
// MZ_ERROR
#define MZ_OK (0)
#define MZ_STREAM_ERROR (-1)
@ -34,17 +37,28 @@ extern "C" {
#define MZ_CRYPT_ERROR (-106)
#define MZ_EXIST_ERROR (-107)
// MZ_COMPRESS_METHOD
// MZ_OPEN
#define MZ_OPEN_MODE_READ (0x01)
#define MZ_OPEN_MODE_WRITE (0x02)
#define MZ_OPEN_MODE_READWRITE (MZ_OPEN_MODE_READ | MZ_OPEN_MODE_WRITE)
#define MZ_OPEN_MODE_APPEND (0x04)
#define MZ_OPEN_MODE_CREATE (0x08)
#define MZ_OPEN_MODE_EXISTING (0x10)
// MZ_SEEK
#define MZ_SEEK_CUR (1)
#define MZ_SEEK_END (2)
#define MZ_SEEK_SET (0)
// MZ_COMPRESS
#define MZ_COMPRESS_METHOD_RAW (0)
#define MZ_COMPRESS_METHOD_DEFLATE (8)
#define MZ_COMPRESS_METHOD_BZIP2 (12)
#define MZ_COMPRESS_METHOD_LZMA (14)
#define MZ_COMPRESS_METHOD_AES (99)
// MZ_COMPRESS_OPTIONS
#define MZ_COMPRESS_LEVEL_DEFAULT (-1)
// MZ_ZIP_FLAG
// MZ_ZIP
#define MZ_ZIP_FLAG_ENCRYPTED (1 << 0)
#define MZ_ZIP_FLAG_LZMA_EOS_MARKER (1 << 1)
#define MZ_ZIP_FLAG_DEFLATE_MAX (1 << 1)
@ -60,9 +74,6 @@ extern "C" {
#define MZ_AES_ENCRYPTION_MODE_192 (0x02)
#define MZ_AES_ENCRYPTION_MODE_256 (0x03)
// MZ_VERSION
#define MZ_VERSION ("2.2.0")
/***************************************************************************/
#ifdef __cplusplus

View File

@ -51,7 +51,7 @@ extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **gl
zlib_filefunc64_def *pzlib_filefunc_def)
{
mz_compat *compat = NULL;
int32_t mode = MZ_STREAM_MODE_READWRITE;
int32_t mode = MZ_OPEN_MODE_READWRITE;
int32_t open_existing = 0;
void *handle = NULL;
void *stream = NULL;
@ -62,10 +62,10 @@ extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **gl
switch (append)
{
case APPEND_STATUS_CREATE:
mode |= MZ_STREAM_MODE_CREATE;
mode |= MZ_OPEN_MODE_CREATE;
break;
case APPEND_STATUS_CREATEAFTER:
mode |= MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_APPEND;
mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND;
break;
case APPEND_STATUS_ADDINZIP:
open_existing = 1;
@ -253,7 +253,7 @@ extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filef
extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def)
{
mz_compat *compat = NULL;
int32_t mode = MZ_STREAM_MODE_READ;
int32_t mode = MZ_OPEN_MODE_READ;
void *handle = NULL;
void *stream = NULL;
@ -579,10 +579,10 @@ int get_file_crc(const char *path, void *buf, uint32_t buf_size, uint32_t *resul
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ);
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
mz_stream_crc32_create(&crc32_stream);
mz_stream_crc32_open(crc32_stream, NULL, MZ_STREAM_MODE_READ);
mz_stream_crc32_open(crc32_stream, NULL, MZ_OPEN_MODE_READ);
mz_stream_set_base(crc32_stream, stream);

View File

@ -27,13 +27,16 @@ int32_t mz_file_exists(const char *path)
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ);
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
if (err == MZ_OK)
mz_stream_os_close(stream);
mz_stream_os_delete(&stream);
return err;
if (err == MZ_EXIST_ERROR)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int64_t mz_file_get_size(const char *path)
@ -43,9 +46,9 @@ int64_t mz_file_get_size(const char *path)
mz_stream_os_create(&stream);
if (mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ) == MZ_OK)
if (mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ) == MZ_OK)
{
mz_stream_os_seek(stream, 0, MZ_STREAM_SEEK_END);
mz_stream_os_seek(stream, 0, MZ_SEEK_END);
size = mz_stream_os_tell(stream);
mz_stream_os_close(stream);
}
@ -103,8 +106,6 @@ int32_t mz_make_dir(const char *path)
return err;
}
/***************************************************************************/
int32_t mz_path_combine(char *path, const char *join, int32_t max_path)
{
int32_t path_len = 0;

View File

@ -20,17 +20,6 @@ extern "C" {
/***************************************************************************/
#define MZ_STREAM_SEEK_CUR (1)
#define MZ_STREAM_SEEK_END (2)
#define MZ_STREAM_SEEK_SET (0)
#define MZ_STREAM_MODE_READ (0x01)
#define MZ_STREAM_MODE_WRITE (0x02)
#define MZ_STREAM_MODE_READWRITE (MZ_STREAM_MODE_READ | MZ_STREAM_MODE_WRITE)
#define MZ_STREAM_MODE_APPEND (0x04)
#define MZ_STREAM_MODE_CREATE (0x08)
#define MZ_STREAM_MODE_EXISTING (0x10)
#define MZ_STREAM_PROP_TOTAL_IN (1)
#define MZ_STREAM_PROP_TOTAL_IN_MAX (2)
#define MZ_STREAM_PROP_TOTAL_OUT (3)

View File

@ -83,7 +83,7 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int32_t mode)
salt_length = SALT_LENGTH(aes->encryption_mode);
if (mode & MZ_STREAM_MODE_WRITE)
if (mode & MZ_OPEN_MODE_WRITE)
{
mz_os_rand(salt_value, salt_length);
@ -101,7 +101,7 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int32_t mode)
aes->total_out += MZ_AES_PWVERIFYSIZE;
}
else if (mode & MZ_STREAM_MODE_READ)
else if (mode & MZ_OPEN_MODE_READ)
{
if (mz_stream_read(aes->stream.base, salt_value, salt_length) != salt_length)
return MZ_STREAM_ERROR;
@ -178,7 +178,7 @@ int32_t mz_stream_aes_close(void *stream)
unsigned char authcode[MZ_AES_AUTHCODESIZE];
unsigned char rauthcode[MZ_AES_AUTHCODESIZE];
if (aes->mode & MZ_STREAM_MODE_WRITE)
if (aes->mode & MZ_OPEN_MODE_WRITE)
{
fcrypt_end(authcode, &aes->crypt_ctx);
@ -187,7 +187,7 @@ int32_t mz_stream_aes_close(void *stream)
aes->total_out += MZ_AES_AUTHCODESIZE;
}
else if (aes->mode & MZ_STREAM_MODE_READ)
else if (aes->mode & MZ_OPEN_MODE_READ)
{
if (mz_stream_read(aes->stream.base, authcode, MZ_AES_AUTHCODESIZE) != MZ_AES_AUTHCODESIZE)
return MZ_STREAM_ERROR;

View File

@ -192,7 +192,7 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
mz_stream_buffered_print(opaque, stream, "switch from read to write [%lld]\n", buffered->position);
if (mz_stream_seek(buffered->stream.base, buffered->position, MZ_STREAM_SEEK_SET) != MZ_OK)
if (mz_stream_seek(buffered->stream.base, buffered->position, MZ_SEEK_SET) != MZ_OK)
return MZ_STREAM_ERROR;
}
@ -262,7 +262,7 @@ int mz_stream_buffered_seekinternal(void *stream, int64_t offset, int32_t origin
switch (origin)
{
case MZ_STREAM_SEEK_SET:
case MZ_SEEK_SET:
if (buffered->writebuf_len > 0)
{
@ -286,7 +286,7 @@ int mz_stream_buffered_seekinternal(void *stream, int64_t offset, int32_t origin
buffered->position = offset;
break;
case MZ_STREAM_SEEK_CUR:
case MZ_SEEK_CUR:
if (buffered->readbuf_len > 0)
{
@ -313,7 +313,7 @@ int mz_stream_buffered_seekinternal(void *stream, int64_t offset, int32_t origin
break;
case MZ_STREAM_SEEK_END:
case MZ_SEEK_END:
if (buffered->writebuf_len > 0)
{

View File

@ -73,14 +73,14 @@ int32_t mz_stream_bzip_open(void *stream, const char *path, int32_t mode)
bzip->total_in = 0;
bzip->total_out = 0;
if (mode & MZ_STREAM_MODE_WRITE)
if (mode & MZ_OPEN_MODE_WRITE)
{
bzip->bzstream.next_out = (char *)bzip->buffer;
bzip->bzstream.avail_out = sizeof(bzip->buffer);
bzip->error = BZ2_bzCompressInit(&bzip->bzstream, bzip->level, 0, 0);
}
else if (mode & MZ_STREAM_MODE_READ)
else if (mode & MZ_OPEN_MODE_READ)
{
bzip->bzstream.next_in = (char *)bzip->buffer;
bzip->bzstream.avail_in = 0;
@ -275,14 +275,14 @@ int32_t mz_stream_bzip_close(void *stream)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
if (bzip->mode & MZ_STREAM_MODE_WRITE)
if (bzip->mode & MZ_OPEN_MODE_WRITE)
{
mz_stream_bzip_compress(stream, BZ_FINISH);
mz_stream_bzip_flush(stream);
BZ2_bzCompressEnd(&bzip->bzstream);
}
else if (bzip->mode & MZ_STREAM_MODE_READ)
else if (bzip->mode & MZ_OPEN_MODE_READ)
{
BZ2_bzDecompressEnd(&bzip->bzstream);
}

View File

@ -145,7 +145,7 @@ int32_t mz_stream_crypt_open(void *stream, const char *path, int32_t mode)
mz_stream_crypt_init_keys(password, crypt->keys, crypt->crc_32_tab);
if (mode & MZ_STREAM_MODE_WRITE)
if (mode & MZ_OPEN_MODE_WRITE)
{
// First generate RAND_HEAD_LEN - 2 random bytes.
mz_os_rand(header, RAND_HEAD_LEN - 2);
@ -162,7 +162,7 @@ int32_t mz_stream_crypt_open(void *stream, const char *path, int32_t mode)
crypt->total_out += RAND_HEAD_LEN;
}
else if (mode & MZ_STREAM_MODE_READ)
else if (mode & MZ_OPEN_MODE_READ)
{
if (mz_stream_read(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;

View File

@ -76,7 +76,7 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
lzma->total_in = 0;
lzma->total_out = 0;
if (mode & MZ_STREAM_MODE_WRITE)
if (mode & MZ_OPEN_MODE_WRITE)
{
lzma->lstream.next_out = lzma->buffer;
lzma->lstream.avail_out = sizeof(lzma->buffer);
@ -100,7 +100,7 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
lzma->error = lzma_alone_encoder(&lzma->lstream, &opt_lzma);
}
else if (mode & MZ_STREAM_MODE_READ)
else if (mode & MZ_OPEN_MODE_READ)
{
lzma->lstream.next_in = lzma->buffer;
lzma->lstream.avail_in = 0;
@ -289,14 +289,14 @@ int32_t mz_stream_lzma_close(void *stream)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
if (lzma->mode & MZ_STREAM_MODE_WRITE)
if (lzma->mode & MZ_OPEN_MODE_WRITE)
{
mz_stream_lzma_code(stream, LZMA_FINISH);
mz_stream_lzma_flush(stream);
lzma_end(&lzma->lstream);
}
else if (lzma->mode & MZ_STREAM_MODE_READ)
else if (lzma->mode & MZ_OPEN_MODE_READ)
{
lzma_end(&lzma->lstream);
}

View File

@ -82,7 +82,7 @@ int32_t mz_stream_mem_open(void *stream, const char *path, int32_t mode)
mem->limit = 0;
mem->position = 0;
if (mem->mode & MZ_STREAM_MODE_CREATE)
if (mem->mode & MZ_OPEN_MODE_CREATE)
mz_stream_mem_set_size(stream, mem->grow_size);
else
mem->limit = mem->size;
@ -124,7 +124,7 @@ int32_t mz_stream_mem_write(void *stream, const void *buf, int32_t size)
if (size > mem->size - mem->position)
{
if (mem->mode & MZ_STREAM_MODE_CREATE)
if (mem->mode & MZ_OPEN_MODE_CREATE)
{
new_size = mem->size;
if (size < mem->grow_size)
@ -162,13 +162,13 @@ int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin)
switch (origin)
{
case MZ_STREAM_SEEK_CUR:
case MZ_SEEK_CUR:
new_pos = mem->position + offset;
break;
case MZ_STREAM_SEEK_END:
case MZ_SEEK_END:
new_pos = mem->limit + offset;
break;
case MZ_STREAM_SEEK_SET:
case MZ_SEEK_SET:
new_pos = offset;
break;
default:
@ -177,7 +177,7 @@ int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin)
if (new_pos > mem->size)
{
if ((mem->mode & MZ_STREAM_MODE_CREATE) == 0)
if ((mem->mode & MZ_OPEN_MODE_CREATE) == 0)
return MZ_STREAM_ERROR;
mz_stream_mem_set_size(stream, (int32_t)new_pos);
@ -252,7 +252,7 @@ void mz_stream_mem_delete(void **stream)
mem = (mz_stream_mem *)*stream;
if (mem != NULL)
{
if ((mem->mode & MZ_STREAM_MODE_CREATE) && (mem->buffer != NULL))
if ((mem->mode & MZ_OPEN_MODE_CREATE) && (mem->buffer != NULL))
free(mem->buffer);
free(mem);
}

View File

@ -82,11 +82,11 @@ int32_t mz_stream_posix_open(void *stream, const char *path, int32_t mode)
if (path == NULL)
return MZ_STREAM_ERROR;
if ((mode & MZ_STREAM_MODE_READWRITE) == MZ_STREAM_MODE_READ)
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
mode_fopen = "rb";
else if (mode & MZ_STREAM_MODE_APPEND)
else if (mode & MZ_OPEN_MODE_APPEND)
mode_fopen = "ab";
else if (mode & MZ_STREAM_MODE_CREATE)
else if (mode & MZ_OPEN_MODE_CREATE)
mode_fopen = "wb";
else
return MZ_STREAM_ERROR;
@ -154,13 +154,13 @@ int32_t mz_stream_posix_seek(void *stream, int64_t offset, int32_t origin)
switch (origin)
{
case MZ_STREAM_SEEK_CUR:
case MZ_SEEK_CUR:
fseek_origin = SEEK_CUR;
break;
case MZ_STREAM_SEEK_END:
case MZ_SEEK_END:
fseek_origin = SEEK_END;
break;
case MZ_STREAM_SEEK_SET:
case MZ_SEEK_SET:
fseek_origin = SEEK_SET;
break;
default:

View File

@ -68,8 +68,8 @@ int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
int32_t i = 0;
int32_t err = MZ_OK;
if ((((split->disk_size > 0) && (split->mode & MZ_STREAM_MODE_WRITE)) ||
((split->mode & MZ_STREAM_MODE_WRITE) == 0)) && (number_disk >= 0))
if ((((split->disk_size > 0) && (split->mode & MZ_OPEN_MODE_WRITE)) ||
((split->mode & MZ_OPEN_MODE_WRITE) == 0)) && (number_disk >= 0))
{
for (i = strlen(split->path_disk) - 1; i >= 0; i -= 1)
{
@ -92,7 +92,7 @@ int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
split->total_out_disk = 0;
split->current_disk = number_disk;
if (split->mode & MZ_STREAM_MODE_WRITE)
if (split->mode & MZ_OPEN_MODE_WRITE)
{
if ((split->current_disk == 0) && (split->disk_size > 0))
{
@ -101,7 +101,7 @@ int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
split->total_out += split->total_out_disk;
}
}
else if (split->mode & MZ_STREAM_MODE_READ)
else if (split->mode & MZ_OPEN_MODE_READ)
{
if (split->current_disk == 0)
{
@ -133,7 +133,7 @@ int32_t mz_stream_split_goto_disk(void *stream, int32_t number_disk)
mz_stream_split *split = (mz_stream_split *)stream;
int32_t err = MZ_OK;
if ((split->disk_size == 0) && (split->mode & MZ_STREAM_MODE_WRITE))
if ((split->disk_size == 0) && (split->mode & MZ_OPEN_MODE_WRITE))
{
if (mz_stream_is_open(split->stream.base) != MZ_OK)
err = mz_stream_split_open_disk(stream, number_disk);
@ -167,12 +167,12 @@ int32_t mz_stream_split_open(void *stream, const char *path, int32_t mode)
split->path_disk = (char *)malloc(split->path_disk_size);
strncpy(split->path_disk, path, split->path_disk_size);
if (mode & MZ_STREAM_MODE_WRITE)
if (mode & MZ_OPEN_MODE_WRITE)
{
number_disk = 0;
split->current_disk = -1;
}
else if (mode & MZ_STREAM_MODE_READ)
else if (mode & MZ_OPEN_MODE_READ)
{
number_disk = -1;
split->current_disk = 0;

View File

@ -78,18 +78,18 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
if (path == NULL)
return MZ_STREAM_ERROR;
if ((mode & MZ_STREAM_MODE_READWRITE) == MZ_STREAM_MODE_READ)
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
{
desired_access = GENERIC_READ;
creation_disposition = OPEN_EXISTING;
share_mode &= FILE_SHARE_WRITE;
}
else if (mode & MZ_STREAM_MODE_APPEND)
else if (mode & MZ_OPEN_MODE_APPEND)
{
desired_access = GENERIC_WRITE | GENERIC_READ;
creation_disposition = OPEN_EXISTING;
}
else if (mode & MZ_STREAM_MODE_CREATE)
else if (mode & MZ_OPEN_MODE_CREATE)
{
desired_access = GENERIC_WRITE | GENERIC_READ;
creation_disposition = CREATE_ALWAYS;
@ -121,8 +121,8 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
return MZ_STREAM_ERROR;
}
if (mode & MZ_STREAM_MODE_APPEND)
return mz_stream_win32_seek(stream, 0, MZ_STREAM_SEEK_END);
if (mode & MZ_OPEN_MODE_APPEND)
return mz_stream_win32_seek(stream, 0, MZ_SEEK_END);
return MZ_OK;
}
@ -220,13 +220,13 @@ int32_t mz_stream_win32_seek(void *stream, int64_t offset, int32_t origin)
switch (origin)
{
case MZ_STREAM_SEEK_CUR:
case MZ_SEEK_CUR:
move_method = FILE_CURRENT;
break;
case MZ_STREAM_SEEK_END:
case MZ_SEEK_END:
move_method = FILE_END;
break;
case MZ_STREAM_SEEK_SET:
case MZ_SEEK_SET:
move_method = FILE_BEGIN;
break;
default:

View File

@ -81,14 +81,14 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode)
zlib->total_in = 0;
zlib->total_out = 0;
if (mode & MZ_STREAM_MODE_WRITE)
if (mode & MZ_OPEN_MODE_WRITE)
{
zlib->zstream.next_out = zlib->buffer;
zlib->zstream.avail_out = sizeof(zlib->buffer);
zlib->error = deflateInit2(&zlib->zstream, (int8_t)zlib->level, Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
}
else if (mode & MZ_STREAM_MODE_READ)
else if (mode & MZ_OPEN_MODE_READ)
{
zlib->zstream.next_in = zlib->buffer;
zlib->zstream.avail_in = 0;
@ -278,14 +278,14 @@ int32_t mz_stream_zlib_close(void *stream)
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
if (zlib->mode & MZ_STREAM_MODE_WRITE)
if (zlib->mode & MZ_OPEN_MODE_WRITE)
{
mz_stream_zlib_deflate(stream, Z_FINISH);
mz_stream_zlib_flush(stream);
deflateEnd(&zlib->zstream);
}
else if (zlib->mode & MZ_STREAM_MODE_READ)
else if (zlib->mode & MZ_OPEN_MODE_READ)
{
inflateEnd(&zlib->zstream);
}

View File

@ -78,12 +78,10 @@ typedef struct mz_zip_s
int32_t open_mode;
uint64_t disk_offset; // byte before the zip file, (>0 for sfx)
uint32_t disk_number_with_cd; // number of the disk with the central dir
uint64_t cd_start_pos; // pos of the first file in the central dir stream
uint64_t cd_current_pos; // pos of the current file in the central dir
uint64_t cd_pos; // position of the beginning of the central dir
uint64_t cd_offset; // offset of start of central directory
uint64_t cd_size; // size of the central directory
@ -102,7 +100,7 @@ typedef struct mz_zip_s
/***************************************************************************/
// Locate the central directory of a zip file (at the end, just before the global comment)
static int32_t mz_zip_search_cd(void *stream, uint64_t *central_pos)
static int32_t mz_zip_search_eocd(void *stream, uint64_t *central_pos)
{
uint8_t buf[1024 + 4];
int64_t file_size = 0;
@ -114,7 +112,7 @@ static int32_t mz_zip_search_cd(void *stream, uint64_t *central_pos)
*central_pos = 0;
if (mz_stream_seek(stream, 0, MZ_STREAM_SEEK_END) != MZ_OK)
if (mz_stream_seek(stream, 0, MZ_SEEK_END) != MZ_OK)
return MZ_STREAM_ERROR;
file_size = mz_stream_tell(stream);
@ -132,7 +130,7 @@ static int32_t mz_zip_search_cd(void *stream, uint64_t *central_pos)
if (read_size > (file_size - read_pos))
read_size = (uint32_t)(file_size - read_pos);
if (mz_stream_seek(stream, read_pos, MZ_STREAM_SEEK_SET) != MZ_OK)
if (mz_stream_seek(stream, read_pos, MZ_SEEK_SET) != MZ_OK)
break;
if (mz_stream_read(stream, buf, read_size) != read_size)
break;
@ -157,7 +155,7 @@ static int32_t mz_zip_search_cd(void *stream, uint64_t *central_pos)
}
// Locate the central directory 64 of a zip file (at the end, just before the global comment)
static int32_t mz_zip_search_zip64_cd(void *stream, const uint64_t end_central_offset, uint64_t *central_pos)
static int32_t mz_zip_search_zip64_eocd(void *stream, const uint64_t end_central_offset, uint64_t *central_pos)
{
uint64_t offset = 0;
uint32_t value32 = 0;
@ -167,7 +165,7 @@ static int32_t mz_zip_search_zip64_cd(void *stream, const uint64_t end_central_o
*central_pos = 0;
// Zip64 end of central directory locator
err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_STREAM_SEEK_SET);
err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
// Read locator signature
if (err == MZ_OK)
{
@ -186,7 +184,7 @@ static int32_t mz_zip_search_zip64_cd(void *stream, const uint64_t end_central_o
err = mz_stream_read_uint32(stream, &value32);
// Goto end of central directory record
if (err == MZ_OK)
err = mz_stream_seek(stream, offset, MZ_STREAM_SEEK_SET);
err = mz_stream_seek(stream, offset, MZ_SEEK_SET);
// The signature
if (err == MZ_OK)
{
@ -207,8 +205,8 @@ static int32_t mz_zip_read_cd(void *handle)
int64_t number_entry_cd = 0;
uint64_t number_entry_cd64 = 0;
uint64_t number_entry = 0;
uint64_t central_pos = 0;
uint64_t central_pos64 = 0;
uint64_t eocd_pos = 0;
uint64_t eocd_pos64 = 0;
uint16_t value16 = 0;
uint32_t value32 = 0;
uint64_t value64 = 0;
@ -220,10 +218,10 @@ static int32_t mz_zip_read_cd(void *handle)
return MZ_PARAM_ERROR;
// Read and cache central directory records
if (mz_zip_search_cd(zip->stream, &central_pos) == MZ_OK)
if (mz_zip_search_eocd(zip->stream, &eocd_pos) == MZ_OK)
{
// Read end of central directory info
err = mz_stream_seek(zip->stream, central_pos, MZ_STREAM_SEEK_SET);
err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
// The signature, already checked
if (err == MZ_OK)
err = mz_stream_read_uint32(zip->stream, &value32);
@ -260,11 +258,11 @@ static int32_t mz_zip_read_cd(void *handle)
if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
{
// Format should be Zip64, as the central directory or file size is too large
if (mz_zip_search_zip64_cd(zip->stream, central_pos, &central_pos64) == MZ_OK)
if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
{
central_pos = central_pos64;
eocd_pos = eocd_pos64;
err = mz_stream_seek(zip->stream, central_pos, MZ_STREAM_SEEK_SET);
err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
// The signature, already checked
if (err == MZ_OK)
err = mz_stream_read_uint32(zip->stream, &value32);
@ -308,7 +306,7 @@ static int32_t mz_zip_read_cd(void *handle)
if (err == MZ_OK)
{
if (central_pos < zip->cd_offset + zip->cd_size)
if (eocd_pos < zip->cd_offset + zip->cd_size)
err = MZ_FORMAT_ERROR;
}
@ -323,12 +321,6 @@ static int32_t mz_zip_read_cd(void *handle)
}
}
if (err == MZ_OK)
{
zip->disk_offset = central_pos - (zip->cd_offset + zip->cd_size);
zip->cd_pos = zip->disk_offset;
}
return err;
}
@ -337,8 +329,6 @@ static int32_t mz_zip_write_cd(void *handle)
mz_zip *zip = (mz_zip *)handle;
uint16_t comment_size = 0;
uint64_t zip64_eocd_pos_inzip = 0;
uint64_t pos = 0;
uint64_t cd_pos = 0;
int64_t disk_number = 0;
int64_t disk_size = 0;
int32_t err = MZ_OK;
@ -353,15 +343,15 @@ static int32_t mz_zip_write_cd(void *handle)
zip->disk_number_with_cd += 1;
mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
zip->cd_pos = mz_stream_tell(zip->stream);
mz_stream_seek(zip->cd_mem_stream, 0, MZ_STREAM_SEEK_END);
zip->cd_offset = mz_stream_tell(zip->stream);
mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
mz_stream_seek(zip->cd_mem_stream, 0, MZ_STREAM_SEEK_SET);
mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
// Write the ZIP64 central directory header
if (pos >= UINT32_MAX || zip->number_entry > UINT32_MAX)
if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT32_MAX)
{
zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
@ -393,21 +383,16 @@ static int32_t mz_zip_write_cd(void *handle)
err = mz_stream_write_uint64(zip->stream, (uint64_t)zip->cd_size);
// Offset of start of central directory with respect to the starting disk number
if (err == MZ_OK)
{
cd_pos = zip->cd_pos - zip->disk_offset;
err = mz_stream_write_uint64(zip->stream, cd_pos);
}
err = mz_stream_write_uint64(zip->stream, zip->cd_offset);
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
// Number of the disk with the start of the central directory
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
// Relative offset to the end of zip64 central directory
if (err == MZ_OK)
{
cd_pos = zip64_eocd_pos_inzip - zip->cd_pos;
err = mz_stream_write_uint64(zip->stream, cd_pos);
}
err = mz_stream_write_uint64(zip->stream, zip64_eocd_pos_inzip);
// Number of the disk with the start of the central directory
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
@ -446,11 +431,10 @@ static int32_t mz_zip_write_cd(void *handle)
// Offset of start of central directory with respect to the starting disk number
if (err == MZ_OK)
{
cd_pos = zip->cd_pos - zip->disk_offset;
if (pos >= UINT32_MAX)
if (zip->cd_offset >= UINT32_MAX)
err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
else
err = mz_stream_write_uint32(zip->stream, (uint32_t)cd_pos);
err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
}
// Write global comment
@ -481,10 +465,10 @@ extern void* ZEXPORT mz_zip_open(void *stream, int32_t mode)
zip->stream = stream;
zip->open_mode = mode;
if (zip->open_mode & MZ_STREAM_MODE_WRITE)
if (zip->open_mode & MZ_OPEN_MODE_WRITE)
{
mz_stream_mem_create(&zip->cd_mem_stream);
mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_STREAM_MODE_CREATE);
mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
zip->cd_stream = zip->cd_mem_stream;
}
@ -493,32 +477,39 @@ extern void* ZEXPORT mz_zip_open(void *stream, int32_t mode)
zip->cd_stream = stream;
}
if ((zip->open_mode & MZ_STREAM_MODE_READ) || (mode & MZ_STREAM_MODE_APPEND))
if ((zip->open_mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
{
err = mz_zip_read_cd(zip);
if ((err == MZ_OK) && (mode & MZ_STREAM_MODE_APPEND))
if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
{
// Store central directory in memory
if (mz_stream_seek(zip->stream, zip->cd_offset + zip->disk_offset, MZ_STREAM_SEEK_SET) != MZ_OK)
err = MZ_STREAM_ERROR;
if (mz_stream_copy(zip->cd_mem_stream, zip->stream, (uint32_t)zip->cd_size) != MZ_OK)
err = MZ_STREAM_ERROR;
if (mz_stream_seek(zip->stream, zip->cd_offset + zip->disk_offset, MZ_STREAM_SEEK_SET) != MZ_OK)
err = MZ_STREAM_ERROR;
if (zip->cd_size > 0)
{
// Store central directory in memory
err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
if (err == MZ_OK)
err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (uint32_t)zip->cd_size);
if (err == MZ_OK)
err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
}
else
{
// If no central directory, append new zip to end of file
err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
}
}
else
{
zip->cd_start_pos = zip->disk_offset + zip->cd_offset;
zip->cd_start_pos = zip->cd_offset;
}
}
if (err == MZ_OK)
{
mz_stream_mem_create(&zip->file_info_stream);
mz_stream_mem_open(zip->file_info_stream, NULL, MZ_STREAM_MODE_CREATE);
mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
mz_stream_mem_create(&zip->local_file_info_stream);
mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_STREAM_MODE_CREATE);
mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
}
if (err != MZ_OK)
@ -558,7 +549,7 @@ extern int32_t ZEXPORT mz_zip_close(void *handle)
return err;
}
if (zip->open_mode & MZ_STREAM_MODE_WRITE)
if (zip->open_mode & MZ_OPEN_MODE_WRITE)
err = mz_zip_write_cd(handle);
if (zip->cd_mem_stream != NULL)
@ -725,9 +716,9 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
max_seek = file_info->filename_size + file_info->extrafield_size + file_info->comment_size + 3;
if (err == MZ_OK)
err = mz_stream_seek(file_info_stream, max_seek, MZ_STREAM_SEEK_SET);
err = mz_stream_seek(file_info_stream, max_seek, MZ_SEEK_SET);
if (err == MZ_OK)
err = mz_stream_seek(file_info_stream, 0, MZ_STREAM_SEEK_SET);
err = mz_stream_seek(file_info_stream, 0, MZ_SEEK_SET);
if ((err == MZ_OK) && (file_info->filename_size > 0))
{
@ -750,7 +741,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
// Seek back and parse the extra field
if (err == MZ_OK)
err = mz_stream_seek(file_info_stream, seek, MZ_STREAM_SEEK_SET);
err = mz_stream_seek(file_info_stream, seek, MZ_SEEK_SET);
seek += file_info->extrafield_size + 1;
@ -803,7 +794,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
}
else
{
err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_STREAM_SEEK_CUR);
err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
}
extra_data_size_read += ntfs_attrib_size + 4;
@ -839,7 +830,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
#endif
else
{
err = mz_stream_seek(file_info_stream, extra_data_size, MZ_STREAM_SEEK_CUR);
err = mz_stream_seek(file_info_stream, extra_data_size, MZ_SEEK_CUR);
}
extra_pos += 4 + extra_data_size;
@ -1159,7 +1150,7 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
if (err == MZ_OK)
{
if (zip->open_mode & MZ_STREAM_MODE_WRITE)
if (zip->open_mode & MZ_OPEN_MODE_WRITE)
{
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
}
@ -1208,7 +1199,7 @@ extern int32_t ZEXPORT mz_zip_entry_read_open(void *handle, int16_t raw, const c
#endif
if (zip == NULL)
return MZ_PARAM_ERROR;
if ((zip->open_mode & MZ_STREAM_MODE_READ) == 0)
if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
return MZ_PARAM_ERROR;
if (zip->entry_scanned == 0)
return MZ_PARAM_ERROR;
@ -1220,7 +1211,7 @@ extern int32_t ZEXPORT mz_zip_entry_read_open(void *handle, int16_t raw, const c
else
mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
err = mz_stream_seek(zip->stream, zip->disk_offset + zip->file_info.disk_offset, MZ_STREAM_SEEK_SET);
err = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
if (err == MZ_OK)
err = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
@ -1275,13 +1266,13 @@ extern int32_t ZEXPORT mz_zip_entry_write_open(void *handle, const mz_zip_file *
else
zip->file_info.flag &= ~MZ_ZIP_FLAG_ENCRYPTED;
if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
zip->file_info.disk_number = (uint32_t)disk_number;
zip->file_info.disk_offset = mz_stream_tell(zip->stream);
zip->file_info.crc = 0;
zip->file_info.compressed_size = 0;
if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
zip->file_info.disk_number = (uint32_t)disk_number;
#ifdef HAVE_AES
if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
@ -1355,7 +1346,7 @@ extern int32_t ZEXPORT mz_zip_entry_close_raw(void *handle, uint64_t uncompresse
if (crc32 == 0)
crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
if ((zip->open_mode & MZ_STREAM_MODE_WRITE) == 0)
if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
{
#ifdef HAVE_AES
// AES zip version AE-1 will expect a valid crc as well
@ -1387,7 +1378,7 @@ extern int32_t ZEXPORT mz_zip_entry_close_raw(void *handle, uint64_t uncompresse
mz_stream_delete(&zip->compress_stream);
mz_stream_crc32_delete(&zip->crc32_stream);
if (zip->open_mode & MZ_STREAM_MODE_WRITE)
if (zip->open_mode & MZ_OPEN_MODE_WRITE)
{
if (err == MZ_OK)
{
@ -1442,7 +1433,7 @@ static int32_t mz_zip_goto_next_entry_int(void *handle)
mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_STREAM_SEEK_SET);
err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
if (err == MZ_OK)
err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
if (err == MZ_OK)

View File

@ -184,11 +184,18 @@ def encryption_tests():
def empty_zip_test():
unzip('empty.zip', 'out')
def sfx_zip_test():
org_exe = get_exec('minizip')
sfx_exe = org_exe.replace('.exe', '') + '_sfx.exe'
shutil.copyfile(org_exe, sfx_exe)
zip_list_unzip(sfx_exe, 'out', '-a', '', [ 'test.c', 'test.h' ])
if not os.path.exists('repo'):
os.system('git clone https://github.com/nmoinvaz/minizip repo')
# Run tests
empty_zip_test()
sfx_zip_test()
compression_method_tests()
encryption_tests()
compression_method_tests('Disk Span', '-k 1024', '')