diff --git a/mz_strm.c b/mz_strm.c index c8ca9e9..1bacb2e 100644 --- a/mz_strm.c +++ b/mz_strm.c @@ -35,7 +35,7 @@ int32_t mz_stream_read(void *stream, void *buf, int32_t size) { mz_stream *strm = (mz_stream *)stream; if (!strm || !strm->vtbl || !strm->vtbl->read) return MZ_PARAM_ERROR; - if (mz_stream_is_open(stream) != MZ_OK) + if (mz_stream_is_open(strm) != MZ_OK) return MZ_STREAM_ERROR; return strm->vtbl->read(strm, buf, size); } @@ -104,15 +104,18 @@ int32_t mz_stream_write(void *stream, const void *buf, int32_t size) { return size; if (!strm || !strm->vtbl || !strm->vtbl->write) return MZ_PARAM_ERROR; - if (mz_stream_is_open(stream) != MZ_OK) + if (mz_stream_is_open(strm) != MZ_OK) return MZ_STREAM_ERROR; return strm->vtbl->write(strm, buf, size); } static int32_t mz_stream_write_value(void *stream, uint64_t value, int32_t len) { + mz_stream *strm = (mz_stream *)stream; uint8_t buf[8]; int32_t n = 0; + if (!strm) + return MZ_PARAM_ERROR; for (n = 0; n < len; n += 1) { buf[n] = (uint8_t)(value & 0xff); value >>= 8; @@ -124,7 +127,7 @@ static int32_t mz_stream_write_value(void *stream, uint64_t value, int32_t len) buf[n] = 0xff; } - if (mz_stream_write(stream, buf, len) != len) + if (mz_stream_write(strm, buf, len) != len) return MZ_STREAM_ERROR; return MZ_OK; @@ -215,7 +218,7 @@ int64_t mz_stream_tell(void *stream) { mz_stream *strm = (mz_stream *)stream; if (!strm || !strm->vtbl || !strm->vtbl->tell) return MZ_PARAM_ERROR; - if (mz_stream_is_open(stream) != MZ_OK) + if (mz_stream_is_open(strm) != MZ_OK) return MZ_STREAM_ERROR; return strm->vtbl->tell(strm); } @@ -224,7 +227,7 @@ int32_t mz_stream_seek(void *stream, int64_t offset, int32_t origin) { mz_stream *strm = (mz_stream *)stream; if (!strm || !strm->vtbl || !strm->vtbl->seek) return MZ_PARAM_ERROR; - if (mz_stream_is_open(stream) != MZ_OK) + if (mz_stream_is_open(strm) != MZ_OK) return MZ_STREAM_ERROR; if (origin == MZ_SEEK_SET && offset < 0) return MZ_SEEK_ERROR; @@ -389,14 +392,14 @@ int32_t mz_stream_get_prop_int64(void *stream, int32_t prop, int64_t *value) { mz_stream *strm = (mz_stream *)stream; if (!strm || !strm->vtbl || !strm->vtbl->get_prop_int64) return MZ_PARAM_ERROR; - return strm->vtbl->get_prop_int64(stream, prop, value); + return strm->vtbl->get_prop_int64(strm, prop, value); } int32_t mz_stream_set_prop_int64(void *stream, int32_t prop, int64_t value) { mz_stream *strm = (mz_stream *)stream; if (!strm || !strm->vtbl || !strm->vtbl->set_prop_int64) return MZ_PARAM_ERROR; - return strm->vtbl->set_prop_int64(stream, prop, value); + return strm->vtbl->set_prop_int64(strm, prop, value); } void *mz_stream_create(mz_stream_vtbl *vtbl) { diff --git a/mz_zip.c b/mz_zip.c index 21ee10f..c72f91e 100644 --- a/mz_zip.c +++ b/mz_zip.c @@ -1258,7 +1258,9 @@ static int32_t mz_zip_recover_cd(void *handle) { mz_zip_print("Zip - Recover - Start\n"); - mz_zip_get_cd_mem_stream(handle, &cd_mem_stream); + err = mz_zip_get_cd_mem_stream(zip, &cd_mem_stream); + if (err != MZ_OK) + return err; /* Determine if we are on a split disk or not */ mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0); @@ -1398,9 +1400,9 @@ static int32_t mz_zip_recover_cd(void *handle) { mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset); /* Set new central directory info */ - mz_zip_set_cd_stream(handle, 0, cd_mem_stream); - mz_zip_set_number_entry(handle, number_entry); - mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd); + mz_zip_set_cd_stream(zip, 0, cd_mem_stream); + mz_zip_set_number_entry(zip, number_entry); + mz_zip_set_disk_number_with_cd(zip, disk_number_with_cd); return MZ_OK; } @@ -1513,11 +1515,11 @@ int32_t mz_zip_close(void *handle) { mz_zip_print("Zip - Close\n"); - if (mz_zip_entry_is_open(handle) == MZ_OK) - err = mz_zip_entry_close(handle); + if (mz_zip_entry_is_open(zip) == MZ_OK) + err = mz_zip_entry_close(zip); if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE)) - err = mz_zip_write_cd(handle); + err = mz_zip_write_cd(zip); if (zip->cd_mem_stream) { mz_stream_close(zip->cd_mem_stream); @@ -1856,7 +1858,7 @@ static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress zip->entry_opened = 1; zip->entry_crc32 = 0; } else { - mz_zip_entry_close_int(handle); + mz_zip_entry_close_int(zip); } return err; @@ -1887,7 +1889,7 @@ int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password) mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw); - err = mz_zip_entry_seek_local_header(handle); + err = mz_zip_entry_seek_local_header(zip); if (err == MZ_OK) err = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream); @@ -1907,7 +1909,7 @@ int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password) err = MZ_SUPPORT_ERROR; #endif if (err == MZ_OK) - err = mz_zip_entry_open_int(handle, raw, 0, password); + err = mz_zip_entry_open_int(zip, raw, 0, password); return err; } @@ -1930,8 +1932,8 @@ int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int1 if (!zip || !file_info || !file_info->filename) return MZ_PARAM_ERROR; - if (mz_zip_entry_is_open(handle) == MZ_OK) { - err = mz_zip_entry_close(handle); + if (mz_zip_entry_is_open(zip) == MZ_OK) { + err = mz_zip_entry_close(zip); if (err != MZ_OK) return err; } @@ -2023,7 +2025,7 @@ int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int1 if (err == MZ_OK) err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info); if (err == MZ_OK) - err = mz_zip_entry_open_int(handle, raw, compress_level, password); + err = mz_zip_entry_open_int(zip, raw, compress_level, password); return err; } @@ -2032,7 +2034,7 @@ int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len) { mz_zip *zip = (mz_zip *)handle; int32_t read = 0; - if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) + if (!zip || mz_zip_entry_is_open(zip) != MZ_OK) return MZ_PARAM_ERROR; if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */ return MZ_PARAM_ERROR; @@ -2057,7 +2059,7 @@ int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len) { mz_zip *zip = (mz_zip *)handle; int32_t written = 0; - if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) + if (!zip || mz_zip_entry_is_open(zip) != MZ_OK) return MZ_PARAM_ERROR; written = mz_stream_write(zip->compress_stream, buf, len); if (written > 0) @@ -2073,7 +2075,7 @@ int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compress int32_t err = MZ_OK; uint8_t zip64 = 0; - if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) + if (!zip || mz_zip_entry_is_open(zip) != MZ_OK) return MZ_PARAM_ERROR; mz_stream_close(zip->compress_stream); @@ -2097,7 +2099,7 @@ int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compress zip64 = 1; } - err = mz_zip_entry_seek_local_header(handle); + err = mz_zip_entry_seek_local_header(zip); /* Seek to end of compressed stream since we might have over-read during decompression */ if (err == MZ_OK) { @@ -2128,7 +2130,7 @@ int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compress } } - mz_zip_entry_close_int(handle); + mz_zip_entry_close_int(zip); return err; } @@ -2139,7 +2141,7 @@ int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compresse int32_t err = MZ_OK; uint8_t zip64 = 0; - if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) + if (!zip || mz_zip_entry_is_open(zip) != MZ_OK) return MZ_PARAM_ERROR; mz_stream_close(zip->compress_stream); @@ -2194,7 +2196,7 @@ int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compresse int64_t end_pos = mz_stream_tell(zip->stream); mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &end_disk_number); - err = mz_zip_entry_seek_local_header(handle); + err = mz_zip_entry_seek_local_header(zip); if (err == MZ_OK) { /* Seek to crc32 and sizes offset in local header */ @@ -2227,7 +2229,7 @@ int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compresse zip->number_entry += 1; - mz_zip_entry_close_int(handle); + mz_zip_entry_close_int(zip); return err; } @@ -2235,8 +2237,11 @@ int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compresse int32_t mz_zip_entry_seek_local_header(void *handle) { mz_zip *zip = (mz_zip *)handle; int64_t disk_size = 0; - uint32_t disk_number = zip->file_info.disk_number; + uint32_t disk_number = 0; + if (!zip) + return MZ_PARAM_ERROR; + disk_number = zip->file_info.disk_number; if (disk_number == zip->disk_number_with_cd) { mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size); if ((disk_size == 0) || ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)) @@ -2273,13 +2278,13 @@ int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t mz_zip *zip = (mz_zip *)handle; int32_t err = MZ_OK; - if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) + if (!zip || mz_zip_entry_is_open(zip) != MZ_OK) return MZ_PARAM_ERROR; if (zip->open_mode & MZ_OPEN_MODE_WRITE) - err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size); + err = mz_zip_entry_write_close(zip, crc32, UINT64_MAX, uncompressed_size); else - err = mz_zip_entry_read_close(handle, NULL, NULL, NULL); + err = mz_zip_entry_read_close(zip, NULL, NULL, NULL); return err; } @@ -2329,7 +2334,7 @@ int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info) { int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info) { mz_zip *zip = (mz_zip *)handle; - if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) + if (!zip || mz_zip_entry_is_open(zip) != MZ_OK) return MZ_PARAM_ERROR; *local_file_info = &zip->local_file_info; return MZ_OK; @@ -2338,7 +2343,7 @@ int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info) int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size) { mz_zip *zip = (mz_zip *)handle; - if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) + if (!zip || mz_zip_entry_is_open(zip) != MZ_OK) return MZ_PARAM_ERROR; zip->file_info.extrafield = extrafield; @@ -2385,7 +2390,7 @@ int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos) { zip->cd_current_pos = cd_pos; - return mz_zip_goto_next_entry_int(handle); + return mz_zip_goto_next_entry_int(zip); } int32_t mz_zip_goto_first_entry(void *handle) { @@ -2396,7 +2401,7 @@ int32_t mz_zip_goto_first_entry(void *handle) { zip->cd_current_pos = zip->cd_start_pos; - return mz_zip_goto_next_entry_int(handle); + return mz_zip_goto_next_entry_int(zip); } int32_t mz_zip_goto_next_entry(void *handle) { @@ -2408,7 +2413,7 @@ int32_t mz_zip_goto_next_entry(void *handle) { zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size + zip->file_info.extrafield_size + zip->file_info.comment_size; - return mz_zip_goto_next_entry_int(handle); + return mz_zip_goto_next_entry_int(zip); } int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case) { @@ -2427,13 +2432,13 @@ int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_c } /* Search all entries starting at the first */ - err = mz_zip_goto_first_entry(handle); + err = mz_zip_goto_first_entry(zip); while (err == MZ_OK) { result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case); if (result == 0) return MZ_OK; - err = mz_zip_goto_next_entry(handle); + err = mz_zip_goto_next_entry(zip); } return err; @@ -2445,15 +2450,15 @@ int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_en int32_t result = 0; /* Search first entry looking for match */ - err = mz_zip_goto_first_entry(handle); + err = mz_zip_goto_first_entry(zip); if (err != MZ_OK) return err; - result = cb(handle, userdata, &zip->file_info); + result = cb(zip, userdata, &zip->file_info); if (result == 0) return MZ_OK; - return mz_zip_locate_next_entry(handle, userdata, cb); + return mz_zip_locate_next_entry(zip, userdata, cb); } int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb) { @@ -2462,13 +2467,13 @@ int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_ent int32_t result = 0; /* Search next entries looking for match */ - err = mz_zip_goto_next_entry(handle); + err = mz_zip_goto_next_entry(zip); while (err == MZ_OK) { - result = cb(handle, userdata, &zip->file_info); + result = cb(zip, userdata, &zip->file_info); if (result == 0) return MZ_OK; - err = mz_zip_goto_next_entry(handle); + err = mz_zip_goto_next_entry(zip); } return err; diff --git a/mz_zip_rw.c b/mz_zip_rw.c index e626771..0a18457 100644 --- a/mz_zip_rw.c +++ b/mz_zip_rw.c @@ -76,6 +76,8 @@ int32_t mz_zip_reader_open(void *handle, void *stream) { mz_zip_reader *reader = (mz_zip_reader *)handle; int32_t err = MZ_OK; + if (!reader) + return MZ_PARAM_ERROR; reader->cd_verified = 0; reader->cd_zipped = 0; @@ -88,7 +90,7 @@ int32_t mz_zip_reader_open(void *handle, void *stream) { err = mz_zip_open(reader->zip_handle, stream, MZ_OPEN_MODE_READ); if (err != MZ_OK) { - mz_zip_reader_close(handle); + mz_zip_reader_close(reader); return err; } @@ -100,7 +102,9 @@ int32_t mz_zip_reader_open_file(void *handle, const char *path) { mz_zip_reader *reader = (mz_zip_reader *)handle; int32_t err = MZ_OK; - mz_zip_reader_close(handle); + if (!reader) + return MZ_PARAM_ERROR; + mz_zip_reader_close(reader); reader->file_stream = mz_stream_os_create(); if (!reader->file_stream) @@ -124,7 +128,7 @@ int32_t mz_zip_reader_open_file(void *handle, const char *path) { err = mz_stream_open(reader->split_stream, path, MZ_OPEN_MODE_READ); if (err == MZ_OK) - err = mz_zip_reader_open(handle, reader->split_stream); + err = mz_zip_reader_open(reader, reader->split_stream); return err; } @@ -134,7 +138,9 @@ int32_t mz_zip_reader_open_file_in_memory(void *handle, const char *path) { int64_t file_size = 0; int32_t err = 0; - mz_zip_reader_close(handle); + if (!reader) + return MZ_PARAM_ERROR; + mz_zip_reader_close(reader); file_stream = mz_stream_os_create(); if (!file_stream) @@ -144,7 +150,7 @@ int32_t mz_zip_reader_open_file_in_memory(void *handle, const char *path) { if (err != MZ_OK) { mz_stream_os_delete(&file_stream); - mz_zip_reader_close(handle); + mz_zip_reader_close(reader); return err; } @@ -159,7 +165,7 @@ int32_t mz_zip_reader_open_file_in_memory(void *handle, const char *path) { mz_stream_os_close(file_stream); mz_stream_os_delete(&file_stream); - mz_zip_reader_close(handle); + mz_zip_reader_close(reader); return MZ_MEM_ERROR; } @@ -172,9 +178,9 @@ int32_t mz_zip_reader_open_file_in_memory(void *handle, const char *path) { mz_stream_os_delete(&file_stream); if (err == MZ_OK) - err = mz_zip_reader_open(handle, reader->mem_stream); + err = mz_zip_reader_open(reader, reader->mem_stream); if (err != MZ_OK) - mz_zip_reader_close(handle); + mz_zip_reader_close(reader); return err; } @@ -183,7 +189,9 @@ int32_t mz_zip_reader_open_buffer(void *handle, uint8_t *buf, int32_t len, uint8 mz_zip_reader *reader = (mz_zip_reader *)handle; int32_t err = MZ_OK; - mz_zip_reader_close(handle); + if (!reader) + return MZ_PARAM_ERROR; + mz_zip_reader_close(reader); reader->mem_stream = mz_stream_mem_create(); if (!reader->mem_stream) @@ -200,7 +208,7 @@ int32_t mz_zip_reader_open_buffer(void *handle, uint8_t *buf, int32_t len, uint8 } if (err == MZ_OK) - err = mz_zip_reader_open(handle, reader->mem_stream); + err = mz_zip_reader_open(reader, reader->mem_stream); return err; } @@ -209,6 +217,8 @@ int32_t mz_zip_reader_close(void *handle) { mz_zip_reader *reader = (mz_zip_reader *)handle; int32_t err = MZ_OK; + if (!reader) + return MZ_PARAM_ERROR; if (reader->zip_handle) { err = mz_zip_close(reader->zip_handle); mz_zip_delete(&reader->zip_handle); @@ -244,17 +254,17 @@ int32_t mz_zip_reader_unzip_cd(void *handle) { uint64_t number_entry = 0; int32_t err = MZ_OK; - err = mz_zip_reader_goto_first_entry(handle); + err = mz_zip_reader_goto_first_entry(reader); if (err != MZ_OK) return err; - err = mz_zip_reader_entry_get_info(handle, &cd_info); + err = mz_zip_reader_entry_get_info(reader, &cd_info); if (err != MZ_OK) return err; if (strcmp(cd_info->filename, MZ_ZIP_CD_FILENAME) != 0) - return mz_zip_reader_goto_first_entry(handle); + return mz_zip_reader_goto_first_entry(reader); - err = mz_zip_reader_entry_open(handle); + err = mz_zip_reader_entry_open(reader); if (err != MZ_OK) return err; @@ -289,7 +299,7 @@ int32_t mz_zip_reader_unzip_cd(void *handle) { mz_zip_set_cd_stream(reader->zip_handle, 0, cd_mem_stream); mz_zip_set_number_entry(reader->zip_handle, number_entry); - err = mz_zip_reader_goto_first_entry(handle); + err = mz_zip_reader_goto_first_entry(reader); } reader->cd_verified = reader->entry_verified; @@ -312,11 +322,11 @@ int32_t mz_zip_reader_goto_first_entry(void *handle) { mz_zip_reader *reader = (mz_zip_reader *)handle; int32_t err = MZ_OK; - if (mz_zip_reader_is_open(handle) != MZ_OK) + if (mz_zip_reader_is_open(reader) != MZ_OK) return MZ_PARAM_ERROR; if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK) - mz_zip_reader_entry_close(handle); + mz_zip_reader_entry_close(reader); if (!reader->pattern) err = mz_zip_goto_first_entry(reader->zip_handle); @@ -334,11 +344,11 @@ int32_t mz_zip_reader_goto_next_entry(void *handle) { mz_zip_reader *reader = (mz_zip_reader *)handle; int32_t err = MZ_OK; - if (mz_zip_reader_is_open(handle) != MZ_OK) + if (mz_zip_reader_is_open(reader) != MZ_OK) return MZ_PARAM_ERROR; if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK) - mz_zip_reader_entry_close(handle); + mz_zip_reader_entry_close(reader); if (!reader->pattern) err = mz_zip_goto_next_entry(reader->zip_handle); @@ -356,8 +366,10 @@ int32_t mz_zip_reader_locate_entry(void *handle, const char *filename, uint8_t i mz_zip_reader *reader = (mz_zip_reader *)handle; int32_t err = MZ_OK; + if (!reader) + return MZ_PARAM_ERROR; if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK) - mz_zip_reader_entry_close(handle); + mz_zip_reader_entry_close(reader); err = mz_zip_locate_entry(reader->zip_handle, filename, ignore_case); @@ -376,6 +388,8 @@ int32_t mz_zip_reader_entry_open(void *handle) { const char *password = NULL; char password_buf[120]; + if (!reader) + return MZ_PARAM_ERROR; reader->entry_verified = 0; if (mz_zip_reader_is_open(reader) != MZ_OK) @@ -391,7 +405,7 @@ int32_t mz_zip_reader_entry_open(void *handle) { /* Check if we need a password and ask for it if we need to */ if (!password && reader->password_cb && (reader->file_info->flag & MZ_ZIP_FLAG_ENCRYPTED)) { - reader->password_cb(handle, reader->password_userdata, reader->file_info, password_buf, sizeof(password_buf)); + reader->password_cb(reader, reader->password_userdata, reader->file_info, password_buf, sizeof(password_buf)); password = password_buf; } @@ -401,7 +415,7 @@ int32_t mz_zip_reader_entry_open(void *handle) { if (err != MZ_OK) return err; - if (mz_zip_reader_entry_get_first_hash(handle, &reader->hash_algorithm, &reader->hash_digest_size) == MZ_OK) { + if (mz_zip_reader_entry_get_first_hash(reader, &reader->hash_algorithm, &reader->hash_digest_size) == MZ_OK) { reader->hash = mz_crypt_sha_create(); if (!reader->hash) return MZ_MEM_ERROR; @@ -430,13 +444,17 @@ int32_t mz_zip_reader_entry_close(void *handle) { int32_t err_hash = MZ_OK; uint8_t computed_hash[MZ_HASH_MAX_SIZE]; uint8_t expected_hash[MZ_HASH_MAX_SIZE]; +#endif + if (!reader) + return MZ_PARAM_ERROR; +#ifndef MZ_ZIP_NO_CRYPTO if (reader->hash) { mz_crypt_sha_end(reader->hash, computed_hash, sizeof(computed_hash)); mz_crypt_sha_delete(&reader->hash); err_hash = - mz_zip_reader_entry_get_hash(handle, reader->hash_algorithm, expected_hash, reader->hash_digest_size); + mz_zip_reader_entry_get_hash(reader, reader->hash_algorithm, expected_hash, reader->hash_digest_size); if (err_hash == MZ_OK) { /* Verify expected hash against computed hash */ @@ -455,6 +473,8 @@ int32_t mz_zip_reader_entry_close(void *handle) { int32_t mz_zip_reader_entry_read(void *handle, void *buf, int32_t len) { mz_zip_reader *reader = (mz_zip_reader *)handle; int32_t read = 0; + if (!reader) + return MZ_PARAM_ERROR; read = mz_zip_entry_read(reader->zip_handle, buf, len); #ifndef MZ_ZIP_NO_CRYPTO if (read > 0 && reader->hash) @@ -471,6 +491,8 @@ int32_t mz_zip_reader_entry_get_hash(void *handle, uint16_t algorithm, uint8_t * uint16_t cur_algorithm = 0; uint16_t cur_digest_size = 0; + if (!reader) + return MZ_PARAM_ERROR; file_extra_stream = mz_stream_mem_create(); if (!file_extra_stream) return MZ_MEM_ERROR; @@ -538,7 +560,7 @@ int32_t mz_zip_reader_entry_get_first_hash(void *handle, uint16_t *algorithm, ui int32_t mz_zip_reader_entry_get_info(void *handle, mz_zip_file **file_info) { mz_zip_reader *reader = (mz_zip_reader *)handle; int32_t err = MZ_OK; - if (!file_info || mz_zip_reader_is_open(handle) != MZ_OK) + if (!file_info || mz_zip_reader_is_open(reader) != MZ_OK) return MZ_PARAM_ERROR; *file_info = reader->file_info; if (!*file_info) @@ -548,7 +570,7 @@ int32_t mz_zip_reader_entry_get_info(void *handle, mz_zip_file **file_info) { int32_t mz_zip_reader_entry_is_dir(void *handle) { mz_zip_reader *reader = (mz_zip_reader *)handle; - if (mz_zip_reader_is_open(handle) != MZ_OK) + if (mz_zip_reader_is_open(reader) != MZ_OK) return MZ_PARAM_ERROR; return mz_zip_entry_is_dir(reader->zip_handle); } @@ -566,17 +588,17 @@ int32_t mz_zip_reader_entry_save_process(void *handle, void *stream, mz_stream_w /* If the entry isn't open for reading, open it */ if (mz_zip_entry_is_open(reader->zip_handle) != MZ_OK) - err = mz_zip_reader_entry_open(handle); + err = mz_zip_reader_entry_open(reader); if (err != MZ_OK) return err; /* Unzip entry in zip file */ - read = mz_zip_reader_entry_read(handle, reader->buffer, sizeof(reader->buffer)); + read = mz_zip_reader_entry_read(reader, reader->buffer, sizeof(reader->buffer)); if (read == 0) { /* If we are done close the entry */ - err = mz_zip_reader_entry_close(handle); + err = mz_zip_reader_entry_close(reader); if (err != MZ_OK) return err; @@ -609,11 +631,11 @@ int32_t mz_zip_reader_entry_save(void *handle, void *stream, mz_stream_write_cb /* Update the progress at the beginning */ if (reader->progress_cb) - reader->progress_cb(handle, reader->progress_userdata, reader->file_info, current_pos); + reader->progress_cb(reader, reader->progress_userdata, reader->file_info, current_pos); /* Write data to stream until done */ while (err == MZ_OK) { - written = mz_zip_reader_entry_save_process(handle, stream, write_cb); + written = mz_zip_reader_entry_save_process(reader, stream, write_cb); if (written == MZ_END_OF_STREAM) break; if (written > 0) @@ -625,7 +647,7 @@ int32_t mz_zip_reader_entry_save(void *handle, void *stream, mz_stream_write_cb current_time = mz_os_ms_time(); if ((current_time - update_time) > reader->progress_cb_interval_ms) { if (reader->progress_cb) - reader->progress_cb(handle, reader->progress_userdata, reader->file_info, current_pos); + reader->progress_cb(reader, reader->progress_userdata, reader->file_info, current_pos); update_pos = current_pos; update_time = current_time; @@ -634,7 +656,7 @@ int32_t mz_zip_reader_entry_save(void *handle, void *stream, mz_stream_write_cb /* Update the progress at the end */ if (reader->progress_cb && update_pos != current_pos) - reader->progress_cb(handle, reader->progress_userdata, reader->file_info, current_pos); + reader->progress_cb(reader, reader->progress_userdata, reader->file_info, current_pos); return err; } @@ -659,7 +681,7 @@ int32_t mz_zip_reader_entry_save_file(void *handle, const char *path) { return MZ_MEM_ERROR; if (reader->entry_cb) - reader->entry_cb(handle, reader->entry_userdata, reader->file_info, pathwfs); + reader->entry_cb(reader, reader->entry_userdata, reader->file_info, pathwfs); directory = (char *)strdup(pathwfs); if (!directory) { @@ -676,7 +698,7 @@ int32_t mz_zip_reader_entry_save_file(void *handle, const char *path) { /* Check if file exists and ask if we want to overwrite */ if (reader->overwrite_cb && mz_os_file_exists(pathwfs) == MZ_OK) { - err_cb = reader->overwrite_cb(handle, reader->overwrite_userdata, reader->file_info, pathwfs); + err_cb = reader->overwrite_cb(reader, reader->overwrite_userdata, reader->file_info, pathwfs); if (err_cb != MZ_OK) goto save_cleanup; /* We want to overwrite the file so we delete the existing one */ @@ -712,7 +734,7 @@ int32_t mz_zip_reader_entry_save_file(void *handle, const char *path) { err = mz_stream_mem_open(stream, NULL, MZ_OPEN_MODE_CREATE); if (err == MZ_OK) - err = mz_zip_reader_entry_save(handle, stream, mz_stream_write); + err = mz_zip_reader_entry_save(reader, stream, mz_stream_write); if (err == MZ_OK) err = mz_stream_write_uint8(stream, 0); @@ -741,7 +763,7 @@ int32_t mz_zip_reader_entry_save_file(void *handle, const char *path) { err = mz_stream_os_open(stream, pathwfs, MZ_OPEN_MODE_CREATE); if (err == MZ_OK) - err = mz_zip_reader_entry_save(handle, stream, mz_stream_write); + err = mz_zip_reader_entry_save(reader, stream, mz_stream_write); mz_stream_close(stream); mz_stream_delete(&stream); @@ -790,7 +812,7 @@ int32_t mz_zip_reader_entry_save_buffer(void *handle, void *buf, int32_t len) { err = mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_READ); if (err == MZ_OK) - err = mz_zip_reader_entry_save(handle, mem_stream, mz_stream_mem_write); + err = mz_zip_reader_entry_save(reader, mem_stream, mz_stream_mem_write); mz_stream_mem_delete(&mem_stream); return err; @@ -822,7 +844,9 @@ int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir) { char *resolved_name = NULL; char *new_alloc = NULL; - err = mz_zip_reader_goto_first_entry(handle); + if (!reader) + return MZ_PARAM_ERROR; + err = mz_zip_reader_goto_first_entry(reader); if (err == MZ_END_OF_LIST) return err; @@ -884,10 +908,10 @@ int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir) { mz_path_combine(path, resolved_name, resolved_name_size); /* Save file to disk */ - err = mz_zip_reader_entry_save_file(handle, path); + err = mz_zip_reader_entry_save_file(reader, path); if (err == MZ_OK) - err = mz_zip_reader_goto_next_entry(handle); + err = mz_zip_reader_goto_next_entry(reader); } if (err == MZ_END_OF_LIST) @@ -923,7 +947,7 @@ void mz_zip_reader_set_raw(void *handle, uint8_t raw) { int32_t mz_zip_reader_get_raw(void *handle, uint8_t *raw) { mz_zip_reader *reader = (mz_zip_reader *)handle; - if (!raw) + if (!reader || !raw) return MZ_PARAM_ERROR; *raw = reader->raw; return MZ_OK; @@ -931,7 +955,7 @@ int32_t mz_zip_reader_get_raw(void *handle, uint8_t *raw) { int32_t mz_zip_reader_get_zip_cd(void *handle, uint8_t *zip_cd) { mz_zip_reader *reader = (mz_zip_reader *)handle; - if (!zip_cd) + if (!reader || !zip_cd) return MZ_PARAM_ERROR; *zip_cd = reader->cd_zipped; return MZ_OK; @@ -990,7 +1014,7 @@ void mz_zip_reader_set_entry_cb(void *handle, void *userdata, mz_zip_reader_entr int32_t mz_zip_reader_get_zip_handle(void *handle, void **zip_handle) { mz_zip_reader *reader = (mz_zip_reader *)handle; - if (!zip_handle) + if (!reader || !zip_handle) return MZ_PARAM_ERROR; *zip_handle = reader->zip_handle; if (!*zip_handle) @@ -1098,9 +1122,9 @@ int32_t mz_zip_writer_zip_cd(void *handle) { mz_stream_mem_get_buffer_length(file_extra_stream, &extrafield_size); cd_file.extrafield_size = (uint16_t)extrafield_size; - err = mz_zip_writer_entry_open(handle, &cd_file); + err = mz_zip_writer_entry_open(writer, &cd_file); if (err == MZ_OK) { - mz_stream_copy_stream(handle, mz_zip_writer_entry_write, cd_mem_stream, NULL, (int32_t)cd_mem_length); + mz_stream_copy_stream(writer, mz_zip_writer_entry_write, cd_mem_stream, NULL, (int32_t)cd_mem_length); mz_stream_seek(cd_mem_stream, 0, MZ_SEEK_SET); mz_stream_mem_set_buffer_limit(cd_mem_stream, 0); @@ -1133,7 +1157,7 @@ static int32_t mz_zip_writer_open_int(void *handle, void *stream, int32_t mode) err = mz_zip_open(writer->zip_handle, stream, mode); if (err != MZ_OK) { - mz_zip_writer_close(handle); + mz_zip_writer_close(writer); return err; } @@ -1141,15 +1165,18 @@ static int32_t mz_zip_writer_open_int(void *handle, void *stream, int32_t mode) } int32_t mz_zip_writer_open(void *handle, void *stream, uint8_t append) { + mz_zip_writer *writer = (mz_zip_writer *)handle; int32_t mode = MZ_OPEN_MODE_WRITE; + if (!writer) + return MZ_PARAM_ERROR; if (append) { mode |= MZ_OPEN_MODE_APPEND; } else { mode |= MZ_OPEN_MODE_CREATE; } - return mz_zip_writer_open_int(handle, stream, mode); + return mz_zip_writer_open_int(writer, stream, mode); } int32_t mz_zip_writer_open_file(void *handle, const char *path, int64_t disk_size, uint8_t append) { @@ -1159,7 +1186,9 @@ int32_t mz_zip_writer_open_file(void *handle, const char *path, int64_t disk_siz int32_t err_cb = 0; char directory[320]; - mz_zip_writer_close(handle); + if (!writer) + return MZ_PARAM_ERROR; + mz_zip_writer_close(writer); if (mz_os_file_exists(path) != MZ_OK) { /* If the file doesn't exist, we don't append file */ @@ -1177,7 +1206,7 @@ int32_t mz_zip_writer_open_file(void *handle, const char *path, int64_t disk_siz mode |= MZ_OPEN_MODE_APPEND; } else { if (writer->overwrite_cb) - err_cb = writer->overwrite_cb(handle, writer->overwrite_userdata, path); + err_cb = writer->overwrite_cb(writer, writer->overwrite_userdata, path); if (err_cb == MZ_INTERNAL_ERROR) return err; @@ -1210,7 +1239,7 @@ int32_t mz_zip_writer_open_file(void *handle, const char *path, int64_t disk_siz err = mz_stream_open(writer->split_stream, path, mode); if (err == MZ_OK) - err = mz_zip_writer_open_int(handle, writer->split_stream, mode); + err = mz_zip_writer_open_int(writer, writer->split_stream, mode); return err; } @@ -1221,7 +1250,9 @@ int32_t mz_zip_writer_open_file_in_memory(void *handle, const char *path) { int64_t file_size = 0; int32_t err = 0; - mz_zip_writer_close(handle); + if (!writer) + return MZ_PARAM_ERROR; + mz_zip_writer_close(writer); file_stream = mz_stream_os_create(); if (!file_stream) @@ -1231,7 +1262,7 @@ int32_t mz_zip_writer_open_file_in_memory(void *handle, const char *path) { if (err != MZ_OK) { mz_stream_os_delete(&file_stream); - mz_zip_writer_close(handle); + mz_zip_writer_close(writer); return err; } @@ -1245,7 +1276,7 @@ int32_t mz_zip_writer_open_file_in_memory(void *handle, const char *path) { mz_stream_os_close(file_stream); mz_stream_os_delete(&file_stream); - mz_zip_writer_close(handle); + mz_zip_writer_close(writer); return MZ_MEM_ERROR; } @@ -1258,9 +1289,9 @@ int32_t mz_zip_writer_open_file_in_memory(void *handle, const char *path) { mz_stream_os_delete(&file_stream); if (err == MZ_OK) - err = mz_zip_writer_open(handle, writer->mem_stream, 1); + err = mz_zip_writer_open(writer, writer->mem_stream, 1); if (err != MZ_OK) - mz_zip_writer_close(handle); + mz_zip_writer_close(writer); return err; } @@ -1269,6 +1300,8 @@ int32_t mz_zip_writer_close(void *handle) { mz_zip_writer *writer = (mz_zip_writer *)handle; int32_t err = MZ_OK; + if (!writer) + return MZ_PARAM_ERROR; if (writer->zip_handle) { mz_zip_set_version_madeby(writer->zip_handle, MZ_VERSION_MADEBY); if (writer->comment) @@ -1307,17 +1340,19 @@ int32_t mz_zip_writer_entry_open(void *handle, mz_zip_file *file_info) { const char *password = NULL; char password_buf[120]; + if (!writer) + return MZ_PARAM_ERROR; /* Copy file info to access data upon close */ memcpy(&writer->file_info, file_info, sizeof(mz_zip_file)); if (writer->entry_cb) - writer->entry_cb(handle, writer->entry_userdata, &writer->file_info); + writer->entry_cb(writer, writer->entry_userdata, &writer->file_info); password = writer->password; /* Check if we need a password and ask for it if we need to */ if (!password && writer->password_cb && (writer->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)) { - writer->password_cb(handle, writer->password_userdata, &writer->file_info, password_buf, sizeof(password_buf)); + writer->password_cb(writer, writer->password_userdata, &writer->file_info, password_buf, sizeof(password_buf)); password = password_buf; } @@ -1355,7 +1390,11 @@ int32_t mz_zip_writer_entry_close(void *handle) { int32_t extrafield_size = 0; int16_t field_length_hash = 0; uint8_t hash_digest[MZ_HASH_MAX_SIZE]; +#endif + if (!writer) + return MZ_PARAM_ERROR; +#ifndef MZ_ZIP_NO_CRYPTO if (writer->hash) { uint16_t hash_digest_size = 0; @@ -1422,6 +1461,8 @@ int32_t mz_zip_writer_entry_close(void *handle) { int32_t mz_zip_writer_entry_write(void *handle, const void *buf, int32_t len) { mz_zip_writer *writer = (mz_zip_writer *)handle; int32_t written = 0; + if (!writer) + return MZ_PARAM_ERROR; written = mz_zip_entry_write(writer->zip_handle, buf, len); #ifndef MZ_ZIP_NO_CRYPTO if (written > 0 && writer->hash) @@ -1453,7 +1494,7 @@ int32_t mz_zip_writer_add_process(void *handle, void *stream, mz_stream_read_cb return err; } - written = mz_zip_writer_entry_write(handle, writer->buffer, read); + written = mz_zip_writer_entry_write(writer, writer->buffer, read); if (written != read) return MZ_WRITE_ERROR; @@ -1469,13 +1510,15 @@ int32_t mz_zip_writer_add(void *handle, void *stream, mz_stream_read_cb read_cb) int32_t err = MZ_OK; int32_t written = 0; + if (!writer) + return MZ_PARAM_ERROR; /* Update the progress at the beginning */ if (writer->progress_cb) - writer->progress_cb(handle, writer->progress_userdata, &writer->file_info, current_pos); + writer->progress_cb(writer, writer->progress_userdata, &writer->file_info, current_pos); /* Write data to stream until done */ while (err == MZ_OK) { - written = mz_zip_writer_add_process(handle, stream, read_cb); + written = mz_zip_writer_add_process(writer, stream, read_cb); if (written == MZ_END_OF_STREAM) break; if (written > 0) @@ -1487,7 +1530,7 @@ int32_t mz_zip_writer_add(void *handle, void *stream, mz_stream_read_cb read_cb) current_time = mz_os_ms_time(); if ((current_time - update_time) > writer->progress_cb_interval_ms) { if (writer->progress_cb) - writer->progress_cb(handle, writer->progress_userdata, &writer->file_info, current_pos); + writer->progress_cb(writer, writer->progress_userdata, &writer->file_info, current_pos); update_pos = current_pos; update_time = current_time; @@ -1496,7 +1539,7 @@ int32_t mz_zip_writer_add(void *handle, void *stream, mz_stream_read_cb read_cb) /* Update the progress at the end */ if (writer->progress_cb && update_pos != current_pos) - writer->progress_cb(handle, writer->progress_userdata, &writer->file_info, current_pos); + writer->progress_cb(writer, writer->progress_userdata, &writer->file_info, current_pos); return err; } @@ -1505,34 +1548,35 @@ int32_t mz_zip_writer_add_info(void *handle, void *stream, mz_stream_read_cb rea mz_zip_writer *writer = (mz_zip_writer *)handle; int32_t err = MZ_OK; - if (mz_zip_writer_is_open(handle) != MZ_OK) + if (mz_zip_writer_is_open(writer) != MZ_OK) return MZ_PARAM_ERROR; if (!file_info) return MZ_PARAM_ERROR; /* Add to zip */ - err = mz_zip_writer_entry_open(handle, file_info); + err = mz_zip_writer_entry_open(writer, file_info); if (err != MZ_OK) return err; if (stream) { if (mz_zip_attrib_is_dir(writer->file_info.external_fa, writer->file_info.version_madeby) != MZ_OK) { - err = mz_zip_writer_add(handle, stream, read_cb); + err = mz_zip_writer_add(writer, stream, read_cb); if (err != MZ_OK) return err; } } - err = mz_zip_writer_entry_close(handle); + err = mz_zip_writer_entry_close(writer); return err; } int32_t mz_zip_writer_add_buffer(void *handle, void *buf, int32_t len, mz_zip_file *file_info) { + mz_zip_writer *writer = (mz_zip_writer *)handle; void *mem_stream = NULL; int32_t err = MZ_OK; - if (mz_zip_writer_is_open(handle) != MZ_OK) + if (mz_zip_writer_is_open(writer) != MZ_OK) return MZ_PARAM_ERROR; if (!buf) return MZ_PARAM_ERROR; @@ -1546,7 +1590,7 @@ int32_t mz_zip_writer_add_buffer(void *handle, void *buf, int32_t len, mz_zip_fi err = mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_READ); if (err == MZ_OK) - err = mz_zip_writer_add_info(handle, mem_stream, mz_stream_mem_read, file_info); + err = mz_zip_writer_add_info(writer, mem_stream, mz_stream_mem_read, file_info); mz_stream_mem_delete(&mem_stream); return err; @@ -1563,7 +1607,7 @@ int32_t mz_zip_writer_add_file(void *handle, const char *path, const char *filen char link_path[1024]; const char *filename = filename_in_zip; - if (mz_zip_writer_is_open(handle) != MZ_OK) + if (mz_zip_writer_is_open(writer) != MZ_OK) return MZ_PARAM_ERROR; if (!path) return MZ_PARAM_ERROR; @@ -1624,7 +1668,7 @@ int32_t mz_zip_writer_add_file(void *handle, const char *path, const char *filen } if (err == MZ_OK) - err = mz_zip_writer_add_info(handle, stream, mz_stream_read, &file_info); + err = mz_zip_writer_add_info(writer, stream, mz_stream_read, &file_info); if (stream) { mz_stream_close(stream); @@ -1647,6 +1691,8 @@ int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_ char full_path[1024]; char path_dir[1024]; + if (!writer) + return MZ_PARAM_ERROR; if (strrchr(path, '*') && mz_os_file_exists(path) != MZ_OK) { strncpy(path_dir, path, sizeof(path_dir) - 1); path_dir[sizeof(path_dir) - 1] = 0; @@ -1677,7 +1723,7 @@ int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_ } if (*filenameinzip != 0) - err = mz_zip_writer_add_file(handle, path, filenameinzip); + err = mz_zip_writer_add_file(writer, path, filenameinzip); if (!is_dir) return err; @@ -1707,7 +1753,7 @@ int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_ if ((wildcard_ptr) && (mz_path_compare_wc(entry->d_name, wildcard_ptr, 1) != MZ_OK)) continue; - err = mz_zip_writer_add_path(handle, full_path, root_path, include_path, recursive); + err = mz_zip_writer_add_path(writer, full_path, root_path, include_path, recursive); if (err != MZ_OK) break; } @@ -1796,7 +1842,7 @@ void mz_zip_writer_set_raw(void *handle, uint8_t raw) { int32_t mz_zip_writer_get_raw(void *handle, uint8_t *raw) { mz_zip_writer *writer = (mz_zip_writer *)handle; - if (!raw) + if (!writer || !raw) return MZ_PARAM_ERROR; *raw = writer->raw; return MZ_OK; @@ -1871,7 +1917,7 @@ void mz_zip_writer_set_entry_cb(void *handle, void *userdata, mz_zip_writer_entr int32_t mz_zip_writer_get_zip_handle(void *handle, void **zip_handle) { mz_zip_writer *writer = (mz_zip_writer *)handle; - if (!zip_handle) + if (!writer || !zip_handle) return MZ_PARAM_ERROR; *zip_handle = writer->zip_handle; if (!*zip_handle)