From 54c3d415c8082ae0bfa89d9325176578a6ed557d Mon Sep 17 00:00:00 2001
From: Roman Dadkov <romand@etersoft.ru>
Date: Mon, 16 Jul 2012 21:10:00 +0400
Subject: [PATCH] mpr : changes for WNetGetUniversalNameA (eterbug #3766)

---
 dlls/mpr/wnet.c |   42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/dlls/mpr/wnet.c b/dlls/mpr/wnet.c
index ac4571c..a75bef7 100644
--- a/dlls/mpr/wnet.c
+++ b/dlls/mpr/wnet.c
@@ -1900,7 +1900,9 @@ DWORD WINAPI WNetGetUniversalNameA ( LPCSTR lpLocalPath, DWORD dwInfoLevel,
         DWORD str_size = *lpBufferSize - sizeof(*info);
         CHAR lpDrive[3];
 
-        if (GetDriveTypeA(lpLocalPath) != DRIVE_REMOTE)
+        lstrcpynA(lpDrive, lpLocalPath, 3);
+
+        if (GetDriveTypeA(lpDrive) != DRIVE_REMOTE)
         {
             err = ERROR_NOT_CONNECTED;
             break;
@@ -1913,7 +1915,6 @@ DWORD WINAPI WNetGetUniversalNameA ( LPCSTR lpLocalPath, DWORD dwInfoLevel,
             break;
         }
 
-        lstrcpynA(lpDrive, lpLocalPath, 3);
         info->lpUniversalName = (char *)info + sizeof(*info);
         WNetGetConnectionA((LPCSTR)lpDrive, info->lpUniversalName, &str_size);
         lstrcatA(info->lpUniversalName, lpLocalPath + 2 * sizeof(CHAR));
@@ -1923,28 +1924,35 @@ DWORD WINAPI WNetGetUniversalNameA ( LPCSTR lpLocalPath, DWORD dwInfoLevel,
     case REMOTE_NAME_INFO_LEVEL:
     {
         LPREMOTE_NAME_INFOA info = lpBuffer;
-        size = *lpBufferSize - sizeof(*info);
-        DWORD cn_size, un_size;
-        LPSTR lpRemoteName = (char*)info + sizeof(*info);
-        LPSTR lpDrive = lpRemoteName - 3 * sizeof(CHAR);
+        DWORD str_size  = *lpBufferSize - sizeof(*info);
+        CHAR lpDrive[3];
 
         lstrcpynA(lpDrive, lpLocalPath, 3);
-        WNetGetConnectionA((LPCSTR)lpDrive, lpRemoteName, lpBufferSize);
 
-        cn_size = lstrlenA(lpRemoteName);
-        un_size = cn_size + lstrlenA(lpLocalPath) - (2 * sizeof(CHAR));
+        if (GetDriveTypeA(lpDrive) != DRIVE_REMOTE)
+        {
+            err = ERROR_NOT_CONNECTED;
+            break;
+        }
 
-        *lpBufferSize = sizeof(lpBuffer) + size;
-        info->lpUniversalName = (char *)info + sizeof(*info);
+        size = sizeof(*info) + lstrlenA(lpLocalPath) + 1;
+        if (*lpBufferSize < size)
+        {
+            err = WN_MORE_DATA;
+            break;
+        }
 
-        info->lpConnectionName = (char*)info->lpUniversalName + ++un_size;
-        lstrcpyA(info->lpConnectionName, lpRemoteName);
+        info->lpConnectionName = (char *)info + sizeof(*info);
+        WNetGetConnectionA((LPCSTR)lpDrive, info->lpConnectionName, &str_size);
 
-        info->lpRemainingPath = (char*)info->lpConnectionName + ++cn_size;
-        lstrcpyA(info->lpRemainingPath, lpLocalPath + (2 * sizeof(CHAR)));
+        info->lpUniversalName = (char*)info->lpConnectionName +
+                                lstrlenA(info->lpConnectionName) + 1;
+        lstrcpyA(info->lpUniversalName, info->lpConnectionName);
+        lstrcatA(info->lpUniversalName, lpLocalPath + 2 * sizeof(CHAR));
 
-        lstrcpyA(info->lpUniversalName, lpRemoteName);
-        lstrcatA(info->lpUniversalName, lpLocalPath + (2 * sizeof(CHAR)));
+        info->lpRemainingPath = (char*)info->lpUniversalName +
+                                lstrlenA(info->lpUniversalName) + 1;
+        lstrcpyA(info->lpRemainingPath, lpLocalPath + (2 * sizeof(CHAR)));
         err = WN_NO_ERROR;
         break;
     }
-- 
1.7.10.5
