From 70c79cc9d501ca226796ba30faaafed23b329499 Mon Sep 17 00:00:00 2001 From: Zsombor Fazekas Date: Thu, 16 Sep 2021 12:00:44 +0200 Subject: [PATCH 1/2] Use wfopen on windows --- miniz_zip.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index dce07fe..e1e0d47 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -40,19 +40,40 @@ extern "C" { #include #if defined(_MSC_VER) || defined(__MINGW64__) + +#define WIN32_LEAN_AND_MEAN +#include + +static WCHAR* mz_utf8z_to_widechar(const char* str) +{ + int reqChars = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); + WCHAR* wStr = (WCHAR*)malloc(reqChars * sizeof(WCHAR)); + MultiByteToWideChar(CP_UTF8, 0, str, -1, wStr, sizeof(WCHAR) * reqChars); + return wStr; +} + static FILE *mz_fopen(const char *pFilename, const char *pMode) { - FILE *pFile = NULL; - fopen_s(&pFile, pFilename, pMode); - return pFile; + 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); + free(wFilename); + free(wMode); + return err ? NULL : pFile; } + static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) { - FILE *pFile = NULL; - if (freopen_s(&pFile, pPath, pMode, pStream)) - return NULL; - return pFile; + WCHAR* wPath = mz_utf8z_to_widechar(pPath); + WCHAR* wMode = mz_utf8z_to_widechar(pMode); + FILE* pFile = NULL; + errno_t err = _wfreopen_s(&pFile, wPath, wMode, pStream); + free(wPath); + free(wMode); + return err ? NULL : pFile; } + #ifndef MINIZ_NO_TIME #include #endif From d9c899cea49cacd6caa344628aa8518765960020 Mon Sep 17 00:00:00 2001 From: Zsombor Fazekas Date: Thu, 16 Sep 2021 12:12:07 +0200 Subject: [PATCH 2/2] Use _wstat64 instead _stat64 on windows --- miniz_zip.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/miniz_zip.c b/miniz_zip.c index e1e0d47..5b8f7c1 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -74,6 +74,14 @@ static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) return err ? NULL : pFile; } +static int mz_stat64(const char *path, struct __stat64 *buffer) +{ + WCHAR* wPath = mz_utf8z_to_widechar(path); + int res = _wstat64(wPath, buffer); + free(wPath); + return res; +} + #ifndef MINIZ_NO_TIME #include #endif @@ -84,7 +92,7 @@ static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) #define MZ_FTELL64 _ftelli64 #define MZ_FSEEK64 _fseeki64 #define MZ_FILE_STAT_STRUCT _stat64 -#define MZ_FILE_STAT _stat64 +#define MZ_FILE_STAT mz_stat64 #define MZ_FFLUSH fflush #define MZ_FREOPEN mz_freopen #define MZ_DELETE_FILE remove