shithub: choc

Download patch

ref: e49be7669f9072e2e27e67e29c097591a83bbf06
parent: d18f5fc27da373c0711b365dcfe22144faebb767
author: Roman Fomin <[email protected]>
date: Sat Aug 13 08:29:53 EDT 2022

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

--- 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 @@
 
     if (!wstr)
     {
-        I_Error("ConvertToUtf8: Failed to allocate new string");
+        I_Error("M_ConvertUtf8ToWide: Failed to allocate new string");
         return NULL;
     }
 
@@ -89,7 +89,7 @@
     wchar_t *wname = NULL;
     wchar_t *wmode = NULL;
 
-    wname = ConvertToUtf8(filename);
+    wname = M_ConvertUtf8ToWide(filename);
 
     if (!wname)
     {
@@ -96,7 +96,7 @@
         return NULL;
     }
 
-    wmode = ConvertToUtf8(mode);
+    wmode = M_ConvertUtf8ToWide(mode);
 
     if (!wmode)
     {
@@ -121,7 +121,7 @@
     wchar_t *wpath = NULL;
     int ret;
 
-    wpath = ConvertToUtf8(path);
+    wpath = M_ConvertUtf8ToWide(path);
 
     if (!wpath)
     {
@@ -145,7 +145,7 @@
     wchar_t *wnew = NULL;
     int ret;
 
-    wold = ConvertToUtf8(oldname);
+    wold = M_ConvertUtf8ToWide(oldname);
 
     if (!wold)
     {
@@ -152,7 +152,7 @@
         return 0;
     }
 
-    wnew = ConvertToUtf8(newname);
+    wnew = M_ConvertUtf8ToWide(newname);
 
     if (!wnew)
     {
@@ -178,7 +178,7 @@
     struct _stat wbuf;
     int ret;
 
-    wpath = ConvertToUtf8(path);
+    wpath = M_ConvertUtf8ToWide(path);
 
     if (!wpath)
     {
@@ -209,7 +209,7 @@
 #ifdef _WIN32
     wchar_t *wdir;
 
-    wdir = ConvertToUtf8(path);
+    wdir = M_ConvertUtf8ToWide(path);
 
     if (!wdir)
     {
--- 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);
--- a/src/w_file_win32.c
+++ b/src/w_file_win32.c
@@ -89,15 +89,18 @@
 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,
                          FILE_SHARE_READ,
@@ -105,6 +108,8 @@
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          NULL);
+
+    free(wpath);
 
     if (handle == INVALID_HANDLE_VALUE)
     {