foxygit / doom Log in
commit e49be7669f9072e2e27e67e29c097591a83bbf06
Author:     Roman Fomin <rfomin@gmail.com>
AuthorDate: Sat Aug 13 12:29:53 2022 +0700
Commit:     GitHub <noreply@github.com>
CommitDate: Sat Aug 13 07:29:53 2022 +0200

    Fix memory mapping WAD files on Windows with non-Latin file paths. (#1481)

    * Fix memory mapping WAD files on Windows with non-Latin file paths.

    * try to satisfy the linter

    * rename M_ConvertToUtf8 to M_ConvertUtf8ToWide
---
 src/m_misc.c       | 18 +++++++++---------
 src/m_misc.h       |  4 ++++
 src/w_file_win32.c | 13 +++++++++----
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/src/m_misc.c b/src/m_misc.c
index fb75a772..5b8f22b3 100644
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -48,7 +48,7 @@
 #include "z_zone.h"

 #ifdef _WIN32
-static wchar_t* ConvertToUtf8(const char *str)
+wchar_t *M_ConvertUtf8ToWide(const char *str)
 {
     wchar_t *wstr = NULL;
     int wlen = 0;
@@ -66,7 +66,7 @@ static wchar_t* ConvertToUtf8(const char *str)

     if (!wstr)
     {
-        I_Error("ConvertToUtf8: Failed to allocate new string");
+        I_Error("M_ConvertUtf8ToWide: Failed to allocate new string");
         return NULL;
     }

@@ -89,14 +89,14 @@ FILE* M_fopen(const char *filename, const char *mode)
     wchar_t *wname = NULL;
     wchar_t *wmode = NULL;

-    wname = ConvertToUtf8(filename);
+    wname = M_ConvertUtf8ToWide(filename);

     if (!wname)
     {
         return NULL;
     }

-    wmode = ConvertToUtf8(mode);
+    wmode = M_ConvertUtf8ToWide(mode);

     if (!wmode)
     {
@@ -121,7 +121,7 @@ int M_remove(const char *path)
     wchar_t *wpath = NULL;
     int ret;

-    wpath = ConvertToUtf8(path);
+    wpath = M_ConvertUtf8ToWide(path);

     if (!wpath)
     {
@@ -145,14 +145,14 @@ int M_rename(const char *oldname, const char *newname)
     wchar_t *wnew = NULL;
     int ret;

-    wold = ConvertToUtf8(oldname);
+    wold = M_ConvertUtf8ToWide(oldname);

     if (!wold)
     {
         return 0;
     }

-    wnew = ConvertToUtf8(newname);
+    wnew = M_ConvertUtf8ToWide(newname);

     if (!wnew)
     {
@@ -178,7 +178,7 @@ int M_stat(const char *path, struct stat *buf)
     struct _stat wbuf;
     int ret;

-    wpath = ConvertToUtf8(path);
+    wpath = M_ConvertUtf8ToWide(path);

     if (!wpath)
     {
@@ -209,7 +209,7 @@ void M_MakeDirectory(const char *path)
 #ifdef _WIN32
     wchar_t *wdir;

-    wdir = ConvertToUtf8(path);
+    wdir = M_ConvertUtf8ToWide(path);

     if (!wdir)
     {
diff --git a/src/m_misc.h b/src/m_misc.h
index 202db51a..f9a6beff 100644
--- a/src/m_misc.h
+++ b/src/m_misc.h
@@ -26,6 +26,10 @@

 #include "doomtype.h"

+#ifdef _WIN32
+wchar_t *M_ConvertUtf8ToWide(const char *str);
+#endif
+
 FILE* M_fopen(const char *filename, const char *mode);
 int M_remove(const char *path);
 int M_rename(const char *oldname, const char *newname);
diff --git a/src/w_file_win32.c b/src/w_file_win32.c
index 4257f0db..d4cb7435 100644
--- a/src/w_file_win32.c
+++ b/src/w_file_win32.c
@@ -89,14 +89,17 @@ unsigned int GetFileLength(HANDLE handle)
 static wad_file_t *W_Win32_OpenFile(const char *path)
 {
     win32_wad_file_t *result;
-    wchar_t wpath[MAX_PATH + 1];
+    wchar_t *wpath = NULL;
     HANDLE handle;

     // Open the file:

-    MultiByteToWideChar(CP_OEMCP, 0,
-                        path, strlen(path) + 1,
-                        wpath, sizeof(wpath));
+    wpath = M_ConvertUtf8ToWide(path);
+
+    if (wpath == NULL)
+    {
+       return NULL;
+    }

     handle = CreateFileW(wpath,
                          GENERIC_READ,
@@ -106,6 +109,8 @@ static wad_file_t *W_Win32_OpenFile(const char *path)
                          FILE_ATTRIBUTE_NORMAL,
                          NULL);

+    free(wpath);
+
     if (handle == INVALID_HANDLE_VALUE)
     {
         return NULL;