Skip to content

Main: update zip/ minz to latest master #2399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 170 additions & 8 deletions OgreMain/src/zip/miniz.h
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ extern "C" {
enum {
/* Note: These enums can be reduced as needed to save memory or stack space -
they are pretty conservative. */
MZ_ZIP_MAX_IO_BUF_SIZE = 64 * 1024,
MZ_ZIP_MAX_IO_BUF_SIZE = 8 * 1024,
MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 512,
MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 512
};
Expand Down Expand Up @@ -1356,6 +1356,11 @@ MINIZ_EXPORT mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip,
mz_uint flags,
mz_uint64 file_start_ofs,
mz_uint64 archive_size);
MINIZ_EXPORT mz_bool mz_zip_reader_init_file_v2_rpb(mz_zip_archive *pZip,
const char *pFilename,
mz_uint flags,
mz_uint64 file_start_ofs,
mz_uint64 archive_size);

/* Read an archive from an already opened FILE, beginning at the current file
* position. */
Expand Down Expand Up @@ -1619,6 +1624,8 @@ MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip,
MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip,
const char *pFilename,
mz_uint flags);
MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader_v2_noreopen(
mz_zip_archive *pZip, const char *pFilename, mz_uint flags);

/* Adds the contents of a memory buffer to an archive. These functions record
* the current local time into the archive. */
Expand Down Expand Up @@ -4835,8 +4842,11 @@ static FILE *mz_fopen(const char *pFilename, const char *pMode) {
wchar_t *wFilename = str2wstr(pFilename);
wchar_t *wMode = str2wstr(pMode);

#ifdef ZIP_ENABLE_SHARABLE_FILE_OPEN
pFile = _wfopen(wFilename, wMode);
#else
_wfopen_s(&pFile, wFilename, wMode);

#endif
free(wFilename);
free(wMode);

Expand All @@ -4850,14 +4860,20 @@ static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) {
wchar_t *wPath = str2wstr(pPath);
wchar_t *wMode = str2wstr(pMode);

#ifdef ZIP_ENABLE_SHARABLE_FILE_OPEN
pFile = _wfreopen(wPath, wMode, pStream);
#else
res = _wfreopen_s(&pFile, wPath, wMode, pStream);
#endif

free(wPath);
free(wMode);

#ifndef ZIP_ENABLE_SHARABLE_FILE_OPEN
if (res) {
return NULL;
}
#endif

return pFile;
}
Expand Down Expand Up @@ -4893,7 +4909,6 @@ static int mz_mkdir(const char *pDirname) {
#define MZ_DELETE_FILE remove
#define MZ_MKDIR(d) mz_mkdir(d)


#elif defined(__MINGW32__) || defined(__MINGW64__)
#include <windows.h>
#ifndef MINIZ_NO_TIME
Expand Down Expand Up @@ -4995,6 +5010,13 @@ static int mz_mkdir(const char *pDirname) {
#endif /* #ifdef _MSC_VER */
#endif /* #ifdef MINIZ_NO_STDIO */

#ifndef CHMOD
// Upon successful completion, a value of 0 is returned.
// Otherwise, a value of -1 is returned and errno is set to indicate the error.
// int chmod(const char *path, mode_t mode);
#define CHMOD(f, m) chmod(f, m)
#endif

#define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))

/* Various ZIP archive enums. To completely avoid cross platform compiler
Expand Down Expand Up @@ -5960,6 +5982,60 @@ mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip, const char *pFilename,
return MZ_TRUE;
}

mz_bool mz_zip_reader_init_file_v2_rpb(mz_zip_archive *pZip,
const char *pFilename, mz_uint flags,
mz_uint64 file_start_ofs,
mz_uint64 archive_size) {
mz_uint64 file_size;
MZ_FILE *pFile;

if ((!pZip) || (!pFilename) ||
((archive_size) &&
(archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)))
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

pFile = MZ_FOPEN(pFilename, "r+b");
if (!pFile)
return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);

file_size = archive_size;
if (!file_size) {
if (MZ_FSEEK64(pFile, 0, SEEK_END)) {
MZ_FCLOSE(pFile);
return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
}

file_size = MZ_FTELL64(pFile);
}

/* TODO: Better sanity check archive_size and the # of actual remaining bytes
*/

if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) {
MZ_FCLOSE(pFile);
return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
}

if (!mz_zip_reader_init_internal(pZip, flags)) {
MZ_FCLOSE(pFile);
return MZ_FALSE;
}

pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
pZip->m_pRead = mz_zip_file_read_func;
pZip->m_pIO_opaque = pZip;
pZip->m_pState->m_pFile = pFile;
pZip->m_archive_size = file_size;
pZip->m_pState->m_file_archive_start_ofs = file_start_ofs;

if (!mz_zip_reader_read_central_dir(pZip, flags)) {
mz_zip_reader_end_internal(pZip, MZ_FALSE);
return MZ_FALSE;
}

return MZ_TRUE;
}

mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile,
mz_uint64 archive_size, mz_uint flags) {
mz_uint64 cur_file_ofs;
Expand Down Expand Up @@ -7986,6 +8062,88 @@ mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip,
return MZ_TRUE;
}

mz_bool mz_zip_writer_init_from_reader_v2_noreopen(mz_zip_archive *pZip,
const char *pFilename,
mz_uint flags) {
mz_zip_internal_state *pState;

if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

if (flags & MZ_ZIP_FLAG_WRITE_ZIP64) {
/* We don't support converting a non-zip64 file to zip64 - this seems like
* more trouble than it's worth. (What about the existing 32-bit data
* descriptors that could follow the compressed data?) */
if (!pZip->m_pState->m_zip64)
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
}

/* No sense in trying to write to an archive that's already at the support max
* size */
if (pZip->m_pState->m_zip64) {
if (pZip->m_total_files == MZ_UINT32_MAX)
return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
} else {
if (pZip->m_total_files == MZ_UINT16_MAX)
return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);

if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)
return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
}

pState = pZip->m_pState;

if (pState->m_pFile) {
#ifdef MINIZ_NO_STDIO
(void)pFilename;
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
#else
if (pZip->m_pIO_opaque != pZip)
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) {
if (!pFilename)
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
}

pZip->m_pWrite = mz_zip_file_write_func;
pZip->m_pNeeds_keepalive = NULL;
#endif /* #ifdef MINIZ_NO_STDIO */
} else if (pState->m_pMem) {
/* Archive lives in a memory block. Assume it's from the heap that we can
* resize using the realloc callback. */
if (pZip->m_pIO_opaque != pZip)
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

pState->m_mem_capacity = pState->m_mem_size;
pZip->m_pWrite = mz_zip_heap_write_func;
pZip->m_pNeeds_keepalive = NULL;
}
/* Archive is being read via a user provided read function - make sure the
user has specified a write function too. */
else if (!pZip->m_pWrite)
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

/* Start writing new files at the archive's current central directory
* location. */
/* TODO: We could add a flag that lets the user start writing immediately
* AFTER the existing central dir - this would be safer. */
pZip->m_archive_size = pZip->m_central_directory_file_ofs;
pZip->m_central_directory_file_ofs = 0;

/* Clear the sorted central dir offsets, they aren't useful or maintained now.
*/
/* Even though we're now in write mode, files can still be extracted and
* verified, but file locates will be slow. */
/* TODO: We could easily maintain the sorted central directory offsets. */
mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets);

pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;

return MZ_TRUE;
}

mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip,
const char *pFilename) {
return mz_zip_writer_init_from_reader_v2(pZip, pFilename, 0);
Expand Down Expand Up @@ -8218,8 +8376,7 @@ mz_bool mz_zip_writer_add_mem_ex_v2(
mz_uint user_extra_data_central_len) {
mz_uint16 method = 0, dos_time = 0, dos_date = 0;
mz_uint level, ext_attributes = 0, num_alignment_padding_bytes;
mz_uint64 local_dir_header_ofs = pZip->m_archive_size,
cur_archive_file_ofs = pZip->m_archive_size, comp_size = 0;
mz_uint64 local_dir_header_ofs = 0, cur_archive_file_ofs = 0, comp_size = 0;
size_t archive_name_size;
mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
tdefl_compressor *pComp = NULL;
Expand Down Expand Up @@ -8251,6 +8408,8 @@ mz_bool mz_zip_writer_add_mem_ex_v2(
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

pState = pZip->m_pState;
local_dir_header_ofs = pZip->m_archive_size;
cur_archive_file_ofs = pZip->m_archive_size;

if (pState->m_zip64) {
if (pZip->m_total_files == MZ_UINT32_MAX)
Expand Down Expand Up @@ -8527,8 +8686,8 @@ mz_bool mz_zip_writer_add_read_buf_callback(
: MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR;
mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes;
mz_uint16 method = 0, dos_time = 0, dos_date = 0;
mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size,
uncomp_size = 0, comp_size = 0;
mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = 0, uncomp_size = 0,
comp_size = 0;
size_t archive_name_size;
mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
mz_uint8 *pExtra_data = NULL;
Expand All @@ -8551,6 +8710,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

pState = pZip->m_pState;
cur_archive_file_ofs = pZip->m_archive_size;

if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX)) {
/* Source file is too large for non-zip64 */
Expand Down Expand Up @@ -9857,7 +10017,9 @@ const char *mz_zip_get_error_string(mz_zip_error mz_err) {
case MZ_ZIP_VALIDATION_FAILED:
return "validation failed";
case MZ_ZIP_WRITE_CALLBACK_FAILED:
return "write calledback failed";
return "write callback failed";
case MZ_ZIP_TOTAL_ERRORS:
return "total errors";
default:
break;
}
Expand Down
Loading