Skip to content

Commit

Permalink
Address some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-- committed Jun 29, 2024
1 parent b5f6453 commit f44a459
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 154 deletions.
133 changes: 118 additions & 15 deletions src/miniz.h
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,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 @@ -1729,7 +1734,7 @@ MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback(
mz_zip_archive *pZip, const char *pArchive_name,
mz_file_read_func read_callback, void *callback_opaque, mz_uint64 max_size,
const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size,
mz_uint level_and_flags, const char *user_extra_data_local,
mz_uint level_and_flags, mz_uint32 ext_attributes, const char *user_extra_data_local,
mz_uint user_extra_data_local_len, const char *user_extra_data_central,
mz_uint user_extra_data_central_len);

Expand All @@ -1739,16 +1744,17 @@ MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback(
/* level_and_flags - compression level (0-10, see MZ_BEST_SPEED,
* MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or
* just set to MZ_DEFAULT_COMPRESSION. */
MINIZ_EXPORT mz_bool mz_zip_writer_add_file(
mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename,
const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags);
MINIZ_EXPORT mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name,
const char *pSrc_filename, const void *pComment,
mz_uint16 comment_size,
mz_uint level_and_flags, mz_uint32 ext_attributes);

/* Like mz_zip_writer_add_file(), except the file data is read from the
* specified FILE stream. */
MINIZ_EXPORT mz_bool mz_zip_writer_add_cfile(
mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file,
mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment,
mz_uint16 comment_size, mz_uint level_and_flags,
mz_uint16 comment_size, mz_uint level_and_flags, mz_uint32 ext_attributes,
const char *user_extra_data_local, mz_uint user_extra_data_local_len,
const char *user_extra_data_central, mz_uint user_extra_data_central_len);
#endif
Expand Down Expand Up @@ -2145,10 +2151,12 @@ int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len,
mz_stream stream;
memset(&stream, 0, sizeof(stream));

#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__)
/* In case mz_ulong is 64-bits (argh I hate longs). */
#else
if ((mz_uint64)(source_len | *pDest_len) > 0xFFFFFFFFU)
return MZ_PARAM_ERROR;

#endif
stream.next_in = pSource;
stream.avail_in = (mz_uint32)source_len;
stream.next_out = pDest;
Expand Down Expand Up @@ -2394,10 +2402,12 @@ int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len,
int status;
memset(&stream, 0, sizeof(stream));

#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__)
/* In case mz_ulong is 64-bits (argh I hate longs). */
#else
if ((mz_uint64)(*pSource_len | *pDest_len) > 0xFFFFFFFFU)
return MZ_PARAM_ERROR;

#endif
stream.next_in = pSource;
stream.avail_in = (mz_uint32)*pSource_len;
stream.next_out = pDest;
Expand Down Expand Up @@ -4965,20 +4975,36 @@ static FILE *mz_fopen(const char *pFilename, const char *pMode) {
WCHAR *wFilename = mz_utf8z_to_widechar(pFilename);
WCHAR *wMode = mz_utf8z_to_widechar(pMode);
FILE *pFile = NULL;
errno_t err = _wfopen_s(&pFile, wFilename, wMode);
#ifdef ZIP_ENABLE_SHARABLE_FILE_OPEN
pFile = _wfopen(wFilename, wMode);
#else
errno_t err = _wfopen_s(&pFile, wFilename, wMode);
#endif
free(wFilename);
free(wMode);
#ifdef ZIP_ENABLE_SHARABLE_FILE_OPEN
return pFile;
#else
return err ? NULL : pFile;
#endif
}

static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) {
WCHAR *wPath = mz_utf8z_to_widechar(pPath);
WCHAR *wMode = mz_utf8z_to_widechar(pMode);
FILE *pFile = NULL;
#ifdef ZIP_ENABLE_SHARABLE_FILE_OPEN
pFile = _wfreopen(wPath, wMode, pStream);
#else
errno_t err = _wfreopen_s(&pFile, wPath, wMode, pStream);
#endif
free(wPath);
free(wMode);
#ifdef ZIP_ENABLE_SHARABLE_FILE_OPEN
return pFile;
#else
return err ? NULL : pFile;
#endif
}

#if defined(__MINGW32__)
Expand All @@ -4997,6 +5023,13 @@ static int mz_stat64(const char *path, struct __stat64 *buffer) {
}
#endif

static int mz_mkdir(const char *pDirname) {
WCHAR *wDirname = mz_utf8z_to_widechar(pDirname);
int res = _wmkdir(wDirname);
free(wDirname);
return res;
}

#ifndef MINIZ_NO_TIME
#include <sys/utime.h>
#endif
Expand All @@ -5016,8 +5049,9 @@ static int mz_stat64(const char *path, struct __stat64 *buffer) {
#define MZ_FFLUSH fflush
#define MZ_FREOPEN mz_freopen
#define MZ_DELETE_FILE remove
#define MZ_MKDIR(d) mz_mkdir(d)

#elif defined(__WATCOMC__)
#elif defined(__MINGW32__) || defined(__WATCOMC__)
#ifndef MINIZ_NO_TIME
#include <sys/utime.h>
#endif
Expand All @@ -5032,6 +5066,7 @@ static int mz_stat64(const char *path, struct __stat64 *buffer) {
#define MZ_FFLUSH fflush
#define MZ_FREOPEN(f, m, s) freopen(f, m, s)
#define MZ_DELETE_FILE remove
#define MZ_MKDIR(d) _mkdir(d)

#elif defined(__TINYC__)
#ifndef MINIZ_NO_TIME
Expand All @@ -5048,6 +5083,11 @@ static int mz_stat64(const char *path, struct __stat64 *buffer) {
#define MZ_FFLUSH fflush
#define MZ_FREOPEN(f, m, s) freopen(f, m, s)
#define MZ_DELETE_FILE remove
#if defined(_WIN32) || defined(_WIN64)
#define MZ_MKDIR(d) _mkdir(d)
#else
#define MZ_MKDIR(d) mkdir(d, 0755)
#endif

#elif defined(__USE_LARGEFILE64) /* gcc, clang */
#ifndef MINIZ_NO_TIME
Expand All @@ -5064,6 +5104,7 @@ static int mz_stat64(const char *path, struct __stat64 *buffer) {
#define MZ_FFLUSH fflush
#define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
#define MZ_DELETE_FILE remove
#define MZ_MKDIR(d) mkdir(d, 0755)

#elif defined(__APPLE__) || defined(__FreeBSD__) || \
(defined(__linux__) && defined(__x86_64__))
Expand All @@ -5081,6 +5122,7 @@ static int mz_stat64(const char *path, struct __stat64 *buffer) {
#define MZ_FFLUSH fflush
#define MZ_FREOPEN(p, m, s) freopen(p, m, s)
#define MZ_DELETE_FILE remove
#define MZ_MKDIR(d) mkdir(d, 0755)

#else
#pragma message( \
Expand All @@ -5104,9 +5146,17 @@ static int mz_stat64(const char *path, struct __stat64 *buffer) {
#define MZ_FFLUSH fflush
#define MZ_FREOPEN(f, m, s) freopen(f, m, s)
#define MZ_DELETE_FILE remove
#define MZ_MKDIR(d) mkdir(d, 0755)
#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 @@ -6123,6 +6173,59 @@ 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) {
Expand Down Expand Up @@ -8687,12 +8790,12 @@ mz_bool mz_zip_writer_add_read_buf_callback(
mz_zip_archive *pZip, const char *pArchive_name,
mz_file_read_func read_callback, void *callback_opaque, mz_uint64 max_size,
const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size,
mz_uint level_and_flags, const char *user_extra_data,
mz_uint level_and_flags, mz_uint32 ext_attributes, const char *user_extra_data,
mz_uint user_extra_data_len, const char *user_extra_data_central,
mz_uint user_extra_data_central_len) {
mz_uint16 gen_flags;
mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes;
mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0;
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;
size_t archive_name_size;
Expand Down Expand Up @@ -9083,20 +9186,20 @@ static size_t mz_file_read_func_stdio(void *pOpaque, mz_uint64 file_ofs,
mz_bool mz_zip_writer_add_cfile(
mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file,
mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment,
mz_uint16 comment_size, mz_uint level_and_flags,
mz_uint16 comment_size, mz_uint level_and_flags,mz_uint32 ext_attributes,
const char *user_extra_data, mz_uint user_extra_data_len,
const char *user_extra_data_central, mz_uint user_extra_data_central_len) {
return mz_zip_writer_add_read_buf_callback(
pZip, pArchive_name, mz_file_read_func_stdio, pSrc_file, max_size,
pFile_time, pComment, comment_size, level_and_flags, user_extra_data,
pFile_time, pComment, comment_size, level_and_flags, ext_attributes,user_extra_data,
user_extra_data_len, user_extra_data_central,
user_extra_data_central_len);
}

mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name,
const char *pSrc_filename, const void *pComment,
mz_uint16 comment_size,
mz_uint level_and_flags) {
mz_uint level_and_flags, mz_uint32 ext_attributes) {
MZ_FILE *pSrc_file = NULL;
mz_uint64 uncomp_size = 0;
MZ_TIME_T file_modified_time;
Expand All @@ -9121,7 +9224,7 @@ mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name,

status = mz_zip_writer_add_cfile(pZip, pArchive_name, pSrc_file, uncomp_size,
pFile_time, pComment, comment_size,
level_and_flags, NULL, 0, NULL, 0);
level_and_flags, ext_attributes, NULL, 0, NULL, 0);

MZ_FCLOSE(pSrc_file);

Expand Down
Loading

0 comments on commit f44a459

Please sign in to comment.