From 4c2836f5afe797f9b5cbe7efd37b9879abe623db Mon Sep 17 00:00:00 2001
From: Dmitry Timoshkov <dmitry@baikal.ru>
Date: Wed, 16 Mar 2016 20:23:28 +0800
Subject: [PATCH] kernel32: Implement K32GetMappedFileName. (v2)

---
 dlls/kernelbase/debug.c       | 87 ++++++++++++++++++++++++++++++++---
 dlls/psapi/tests/psapi_main.c | 22 ++-------
 2 files changed, 85 insertions(+), 24 deletions(-)

diff --git a/dlls/kernelbase/debug.c b/dlls/kernelbase/debug.c
index 48c949355ad7..92f5750aa580 100644
--- a/dlls/kernelbase/debug.c
+++ b/dlls/kernelbase/debug.c
@@ -1247,15 +1247,59 @@ DWORD WINAPI DECLSPEC_HOTPATCH K32GetDeviceDriverFileNameW( void *image_base, WC
     return 0;
 }
 
+static DWORD FILE_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
+{
+    DWORD ret;
+
+    if (srclen < 0) srclen = lstrlenW( src ) + 1;
+    if (!destlen)
+    {
+        if (!AreFileApisANSI())
+        {
+            UNICODE_STRING strW;
+            strW.Buffer = (WCHAR *)src;
+            strW.Length = srclen * sizeof(WCHAR);
+            ret = RtlUnicodeStringToOemSize( &strW ) - 1;
+        }
+        else
+            RtlUnicodeToMultiByteSize( &ret, src, srclen * sizeof(WCHAR) );
+    }
+    else
+    {
+        if (!AreFileApisANSI())
+            RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
+        else
+            RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
+    }
+    return ret;
+}
 
 /***********************************************************************
  *         K32GetMappedFileNameA   (kernelbase.@)
  */
 DWORD WINAPI DECLSPEC_HOTPATCH K32GetMappedFileNameA( HANDLE process, void *addr, char *name, DWORD size )
 {
-    FIXME( "(%p, %p, %p, %d): stub\n", process, addr, name, size );
-    if (name && size) name[0] = 0;
-    return 0;
+    WCHAR file_nameW[MAX_PATH];
+    DWORD ret;
+
+    TRACE("(%p, %p, %p, %d)\n", process, addr, name, size);
+
+    if (!name || !size)
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return 0;
+    }
+
+    ret = K32GetMappedFileNameW(process, addr, file_nameW, MAX_PATH);
+    if (ret)
+    {
+        ret = FILE_name_WtoA(file_nameW, -1, name, size);
+        if (ret > 1)
+            ret--; /* don't account for terminating NUL */
+        else
+            name[0] = 0;
+    }
+    return ret;
 }
 
 
@@ -1264,9 +1308,40 @@ DWORD WINAPI DECLSPEC_HOTPATCH K32GetMappedFileNameA( HANDLE process, void *addr
  */
 DWORD WINAPI DECLSPEC_HOTPATCH K32GetMappedFileNameW( HANDLE process, void *addr, WCHAR *name, DWORD size )
 {
-    FIXME( "(%p, %p, %p, %d): stub\n", process, addr, name, size );
-    if (name && size) name[0] = 0;
-    return 0;
+    MEMORY_SECTION_NAME *section;
+    SIZE_T buf_len;
+    NTSTATUS status;
+
+    TRACE("(%p, %p, %p, %d)\n", process, addr, name, size);
+
+    if (!name || !size)
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return 0;
+    }
+
+    buf_len = sizeof(*section) + size * sizeof(WCHAR);
+    section = HeapAlloc(GetProcessHeap(), 0, buf_len);
+    if (!section)
+    {
+        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        return 0;
+    }
+
+    status = NtQueryVirtualMemory(process, addr, MemorySectionName, section, buf_len, &buf_len);
+    if (status)
+    {
+        HeapFree(GetProcessHeap(), 0, section);
+        SetLastError(RtlNtStatusToDosError(status));
+        return 0;
+    }
+
+    memcpy(name, section->SectionFileName.Buffer, section->SectionFileName.MaximumLength);
+    buf_len = section->SectionFileName.Length;
+
+    HeapFree(GetProcessHeap(), 0, section);
+
+    return buf_len;
 }
 
 
diff --git a/dlls/psapi/tests/psapi_main.c b/dlls/psapi/tests/psapi_main.c
index 9886bc476740..99e87db9543e 100644
--- a/dlls/psapi/tests/psapi_main.c
+++ b/dlls/psapi/tests/psapi_main.c
@@ -418,27 +418,22 @@ static void test_GetMappedFileName(void)
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(NULL, hMod, szMapPath, sizeof(szMapPath));
     ok(!ret, "GetMappedFileName should fail\n");
-todo_wine
     ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
 
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(hpSR, hMod, szMapPath, sizeof(szMapPath));
     ok(!ret, "GetMappedFileName should fail\n");
-todo_wine
     ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
 
     SetLastError( 0xdeadbeef );
     ret = GetMappedFileNameA(hpQI, hMod, szMapPath, sizeof(szMapPath));
-todo_wine
     ok( ret || broken(GetLastError() == ERROR_UNEXP_NET_ERR), /* win2k */
         "GetMappedFileNameA failed with error %u\n", GetLastError() );
     if (ret)
     {
         ok(ret == strlen(szMapPath), "szMapPath=\"%s\" ret=%d\n", szMapPath, ret);
-        todo_wine
         ok(szMapPath[0] == '\\', "szMapPath=\"%s\"\n", szMapPath);
         szMapBaseName = strrchr(szMapPath, '\\'); /* That's close enough for us */
-        todo_wine
         ok(szMapBaseName && *szMapBaseName, "szMapPath=\"%s\"\n", szMapPath);
         if (szMapBaseName)
         {
@@ -476,29 +471,25 @@ todo_wine
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, 0);
     ok(!ret, "GetMappedFileName should fail\n");
-todo_wine
     ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INSUFFICIENT_BUFFER,
        "wrong error %d\n", GetLastError());
 
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(GetCurrentProcess(), base, 0, sizeof(map_name));
     ok(!ret, "GetMappedFileName should fail\n");
-todo_wine
     ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
 
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, 1);
-todo_wine
     ok(ret == 1, "GetMappedFileName error %d\n", GetLastError());
     ok(!map_name[0] || broken(map_name[0] == device_name[0]) /* before win2k */, "expected 0, got %c\n", map_name[0]);
 
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
-todo_wine {
     ok(ret, "GetMappedFileName error %d\n", GetLastError());
     ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
+todo_wine
     ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
-}
 
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameW(GetCurrentProcess(), base, map_nameW, ARRAY_SIZE(map_nameW));
@@ -516,10 +507,9 @@ todo_wine
 
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameW(current_process, base, map_nameW, sizeof(map_nameW)/sizeof(map_nameW[0]));
-todo_wine {
     ok(ret, "GetMappedFileNameW error %d\n", GetLastError());
     ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
-}
+
     if (nt_get_mapped_file_name(current_process, base, nt_map_name, sizeof(nt_map_name)/sizeof(nt_map_name[0])))
     {
         ok(memcmp(map_nameW, nt_map_name, lstrlenW(map_nameW)) == 0, "map name does not start with a device name: %s\n", map_name);
@@ -530,16 +520,14 @@ todo_wine
 
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(GetCurrentProcess(), base + 0x2000, map_name, sizeof(map_name));
-todo_wine {
     ok(ret, "GetMappedFileName error %d\n", GetLastError());
     ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
+todo_wine
     ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
-}
 
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(GetCurrentProcess(), base + 0x4000, map_name, sizeof(map_name));
     ok(!ret, "GetMappedFileName should fail\n");
-todo_wine
     ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
 
     SetLastError(0xdeadbeef);
@@ -551,7 +539,6 @@ todo_wine
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(0, base, map_name, sizeof(map_name));
     ok(!ret, "GetMappedFileName should fail\n");
-todo_wine
     ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
 
     UnmapViewOfFile(base);
@@ -570,7 +557,6 @@ todo_wine
     SetLastError(0xdeadbeef);
     ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
     ok(!ret, "GetMappedFileName should fail\n");
-todo_wine
     ok(GetLastError() == ERROR_FILE_INVALID, "expected ERROR_FILE_INVALID, got %d\n", GetLastError());
 
     CloseHandle(current_process);
@@ -617,7 +603,7 @@ static void test_GetProcessImageFileName(void)
     if(ret && ret1)
     {
         /* Windows returns 2*strlen-1 */
-        todo_wine ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%d\n", szImgPath, ret);
+        ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%d\n", szImgPath, ret);
         todo_wine ok(!strcmp(szImgPath, szMapPath), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath, szMapPath);
     }
 
-- 
2.26.2

