[AUDIT] These files are clean and simple stubs around NT apis and/or WINE code.
authorAlex Ionescu <aionescu@gmail.com>
Thu, 22 Jun 2006 23:06:14 +0000 (23:06 +0000)
committerAlex Ionescu <aionescu@gmail.com>
Thu, 22 Jun 2006 23:06:14 +0000 (23:06 +0000)
- Reformat the files.
- Fix A->W calling.
- Simpliy WriteProcessMemory to use NtProtectVirtualMemory to query the current state.
- Call some APIs with the right DesiredAccess instead of allways sending _ALL_ACCESS. (for example, only use SECTION_MAP_READ | QUERY unless the caller wants r/w.
- Flush ITLB when writing to process memory.

svn path=/trunk/; revision=22519

reactos/dll/win32/kernel32/mem/procmem.c
reactos/dll/win32/kernel32/mem/section.c
reactos/dll/win32/kernel32/mem/virtual.c

index 1cd502b..28539c7 100644 (file)
@@ -1,10 +1,9 @@
-/* $Id$
- *
- * COPYRIGHT:            See COPYING in the top level directory
- * PROJECT:              ReactOS kernel
- * FILE:                 lib/kernel32/mem/procmem.c
- * PURPOSE:
- * PROGRAMMER:           Boudewijn Dekker
+/*
+ * PROJECT:         ReactOS Win32 Base API
+ * LICENSE:         GPL - See COPYING in the top level directory
+ * FILE:            dll/win32/kernel32/mem/procmem.c
+ * PURPOSE:         Handles virtual memory APIs
+ * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
  */
 
 /* INCLUDES ******************************************************************/
@@ -12,7 +11,7 @@
 #include <k32.h>
 
 #define NDEBUG
-#include "../include/debug.h"
+#include "debug.h"
 
 /* FUNCTIONS *****************************************************************/
 
  * @implemented
  */
 BOOL
-STDCALL
-ReadProcessMemory (
-        HANDLE        hProcess,
-        LPCVOID        lpBaseAddress,
-        LPVOID        lpBuffer,
-        DWORD        nSize,
-        LPDWORD        lpNumberOfBytesRead
-        )
+NTAPI
+ReadProcessMemory(IN HANDLE hProcess,
+                  IN LPCVOID lpBaseAddress,
+                  IN LPVOID lpBuffer,
+                  IN DWORD nSize,
+                  OUT LPDWORD lpNumberOfBytesRead)
 {
-
-        NTSTATUS Status;
-
-        Status = NtReadVirtualMemory( hProcess, (PVOID)lpBaseAddress,lpBuffer, nSize,
-                (PULONG)lpNumberOfBytesRead
-                );
-
-        if (!NT_SUCCESS(Status))
-             {
-                SetLastErrorByStatus (Status);
-                return FALSE;
-             }
-        return TRUE;
+    NTSTATUS Status;
+
+    /* Do the read */
+    Status = NtReadVirtualMemory(hProcess,
+                                 (PVOID)lpBaseAddress,
+                                 lpBuffer,
+                                 nSize,
+                                 lpNumberOfBytesRead);
+    if (!NT_SUCCESS(Status))
+    {
+        /* We failed */
+        SetLastErrorByStatus (Status);
+        return FALSE;
+    }
+
+    /* Return success */
+    return TRUE;
 }
 
-
 /*
  * @implemented
  */
 BOOL
-STDCALL
-WriteProcessMemory (
-        HANDLE hProcess,
-        LPVOID lpBaseAddress,
-        LPCVOID lpBuffer,
-        SIZE_T nSize,
-        SIZE_T *lpNumberOfBytesWritten
-        )
+NTAPI
+WriteProcessMemory(IN HANDLE hProcess,
+                   IN LPVOID lpBaseAddress,
+                   IN LPCVOID lpBuffer,
+                   IN SIZE_T nSize,
+                   OUT SIZE_T *lpNumberOfBytesWritten)
 {
-        NTSTATUS Status, ProtectStatus = STATUS_SUCCESS;
-        MEMORY_BASIC_INFORMATION MemInfo;
-        ULONG Length;
-        BOOLEAN UnProtect;
-
-        if (lpNumberOfBytesWritten)
+    NTSTATUS Status;
+    ULONG OldValue;
+    SIZE_T RegionSize;
+    PVOID Base;
+    BOOLEAN UnProtect;
+
+    /* Set parameters for protect call */
+    RegionSize = nSize;
+    Base = lpBaseAddress;
+
+    /* Check the current status */
+    Status = NtProtectVirtualMemory(hProcess,
+                                    &Base,
+                                    &RegionSize,
+                                    PAGE_EXECUTE_READWRITE,
+                                    &OldValue);
+    if (NT_SUCCESS(Status))
+    {
+        /* Check if we are unprotecting */
+        UnProtect = OldValue & (PAGE_READWRITE |
+                                PAGE_WRITECOPY |
+                                PAGE_EXECUTE_READWRITE |
+                                PAGE_EXECUTE_WRITECOPY) ? FALSE : TRUE;
+        if (UnProtect)
         {
-            *lpNumberOfBytesWritten = 0;
-        }
-
-        while (nSize)
-        {
-            Status = NtQueryVirtualMemory(hProcess,
+            /* Set the new protection */
+            Status = NtProtectVirtualMemory(hProcess,
+                                            &Base,
+                                            &RegionSize,
+                                            OldValue,
+                                            &OldValue);
+
+            /* Write the memory */
+            Status = NtWriteVirtualMemory(hProcess,
                                           lpBaseAddress,
-                                          MemoryBasicInformation,
-                                          &MemInfo,
-                                          sizeof(MEMORY_BASIC_INFORMATION),
-                                          NULL);
-
+                                          (LPVOID)lpBuffer,
+                                          nSize,
+                                          lpNumberOfBytesWritten);
             if (!NT_SUCCESS(Status))
             {
+                /* We failed */
                 SetLastErrorByStatus(Status);
                 return FALSE;
             }
-            Length = MemInfo.RegionSize - ((ULONG_PTR)lpBaseAddress - (ULONG_PTR)MemInfo.BaseAddress);
-            if (Length > nSize)
-            {
-                Length = nSize;
-            }
-            UnProtect = MemInfo.Protect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY) ? FALSE : TRUE;
-            if (UnProtect)
+
+            /* Flush the ITLB */
+            NtFlushInstructionCache(hProcess, lpBaseAddress, nSize);
+            return TRUE;
+        }
+        else
+        {
+            /* Check if we were read only */
+            if ((OldValue & PAGE_NOACCESS) || (OldValue & PAGE_READONLY))
             {
-                MemInfo.BaseAddress = lpBaseAddress;
-                MemInfo.RegionSize = Length;
-                if (MemInfo.Protect & (PAGE_EXECUTE|PAGE_EXECUTE_READ))
-                {
-                    MemInfo.Protect &= ~(PAGE_EXECUTE|PAGE_EXECUTE_READ);
-                    MemInfo.Protect |= PAGE_EXECUTE_READWRITE;
-                }
-                else
-                {
-                    MemInfo.Protect &= ~(PAGE_READONLY|PAGE_NOACCESS);
-                    MemInfo.Protect |= PAGE_READWRITE;
-                }
-
-                ProtectStatus = NtProtectVirtualMemory(hProcess,
-                                                       &MemInfo.BaseAddress,
-                                                       &MemInfo.RegionSize,
-                                                       MemInfo.Protect,
-                                                       &MemInfo.Protect);
-                if (!NT_SUCCESS(ProtectStatus))
-                {
-                    SetLastErrorByStatus(ProtectStatus);
-                    return FALSE;
-                }
-                Length = MemInfo.RegionSize - ((ULONG_PTR)lpBaseAddress - (ULONG_PTR)MemInfo.BaseAddress);
-                if (Length > nSize)
-                {
-                    Length = nSize;
-                }
+                /* Restore protection and fail */
+                NtProtectVirtualMemory(hProcess,
+                                       &Base,
+                                       &RegionSize,
+                                       OldValue,
+                                       &OldValue);
+                SetLastErrorByStatus(STATUS_ACCESS_VIOLATION);
+                return FALSE;
             }
 
+            /* Otherwise, do the write */
             Status = NtWriteVirtualMemory(hProcess,
                                           lpBaseAddress,
                                           (LPVOID)lpBuffer,
-                                          Length,
-                                          &Length);
-            if (UnProtect)
-            {
-                ProtectStatus = NtProtectVirtualMemory(hProcess,
-                                                       &MemInfo.BaseAddress,
-                                                       &MemInfo.RegionSize,
-                                                       MemInfo.Protect,
-                                                       &MemInfo.Protect);
-            }
+                                          nSize,
+                                          lpNumberOfBytesWritten);
+
+            /* And restore the protection */
+            NtProtectVirtualMemory(hProcess,
+                                   &Base,
+                                   &RegionSize,
+                                   OldValue,
+                                   &OldValue);
             if (!NT_SUCCESS(Status))
-                 {
-                SetLastErrorByStatus (Status);
-                return FALSE;
-                 }
-            if (UnProtect && !NT_SUCCESS(ProtectStatus))
             {
-                SetLastErrorByStatus (ProtectStatus);
+                /* We failed */
+                SetLastErrorByStatus(STATUS_ACCESS_VIOLATION);
                 return FALSE;
             }
-            lpBaseAddress = (LPVOID)((ULONG_PTR)lpBaseAddress + Length);
-            lpBuffer = (LPCVOID)((ULONG_PTR)lpBuffer + Length);
-            nSize -= Length;
-            if (lpNumberOfBytesWritten)
-            {
-                *lpNumberOfBytesWritten += Length;
-            }
+
+            /* Flush the ITLB */
+            NtFlushInstructionCache(hProcess, lpBaseAddress, nSize);
+            return TRUE;
         }
-        return TRUE;
+    }
+    else
+    {
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return FALSE;
+    }
 }
 
 /* EOF */
index d6734e8..6f944b8 100644 (file)
@@ -1,10 +1,9 @@
-/* $Id$
- *
- * COPYRIGHT:            See COPYING in the top level directory
- * PROJECT:              ReactOS kernel
- * FILE:                 lib/kernel32/mem/section.c
- * PURPOSE:              Implementing file mapping
- * PROGRAMMER:           David Welch (welch@mcmail.com)
+/*
+ * PROJECT:         ReactOS Win32 Base API
+ * LICENSE:         GPL - See COPYING in the top level directory
+ * FILE:            dll/win32/kernel32/mem/section.c
+ * PURPOSE:         Handles virtual memory APIs
+ * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
  */
 
 /* INCLUDES ******************************************************************/
 #include <k32.h>
 
 #define NDEBUG
-#include "../include/debug.h"
+#include "debug.h"
 
 /* FUNCTIONS *****************************************************************/
 
-#define MASK_PAGE_FLAGS (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY)
-#define MASK_SEC_FLAGS  (SEC_COMMIT | SEC_IMAGE | SEC_NOCACHE | SEC_RESERVE)
-
 /*
  * @implemented
  */
-HANDLE STDCALL
-CreateFileMappingA(HANDLE hFile,
-                  LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
-                  DWORD flProtect,
-                  DWORD dwMaximumSizeHigh,
-                  DWORD dwMaximumSizeLow,
-                  LPCSTR lpName)
+HANDLE
+NTAPI
+CreateFileMappingA(IN HANDLE hFile,
+                   IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
+                   IN DWORD flProtect,
+                   IN DWORD dwMaximumSizeHigh,
+                   IN DWORD dwMaximumSizeLow,
+                   IN LPCSTR lpName)
 {
-   NTSTATUS Status;
-   HANDLE SectionHandle;
-   LARGE_INTEGER MaximumSize;
-   PLARGE_INTEGER MaximumSizePointer;
-   OBJECT_ATTRIBUTES ObjectAttributes;
-   ANSI_STRING AnsiName;
-   UNICODE_STRING UnicodeName;
-   PSECURITY_DESCRIPTOR SecurityDescriptor;
-
-   if ((flProtect & (MASK_PAGE_FLAGS | MASK_SEC_FLAGS)) != flProtect)
-     {
-        DPRINT1("Invalid flProtect 0x%08x\n", flProtect);
-        SetLastError(ERROR_INVALID_PARAMETER);
-        return NULL;
-     }
-   if (lpFileMappingAttributes)
-     {
-        SecurityDescriptor = lpFileMappingAttributes->lpSecurityDescriptor;
-     }
-   else
-     {
-        SecurityDescriptor = NULL;
-     }
-
-   if (dwMaximumSizeLow == 0 && dwMaximumSizeHigh == 0)
-     {
-        MaximumSizePointer = NULL;
-     }
-   else
-     {
-        MaximumSize.u.LowPart = dwMaximumSizeLow;
-        MaximumSize.u.HighPart = dwMaximumSizeHigh;
-        MaximumSizePointer = &MaximumSize;
-     }
-
-   if (lpName != NULL)
-     {
-        RtlInitAnsiString(&AnsiName,
-                          (LPSTR)lpName);
-        RtlAnsiStringToUnicodeString(&UnicodeName,
-                                     &AnsiName,
-                                     TRUE);
-     }
-
-   InitializeObjectAttributes(&ObjectAttributes,
-                             (lpName ? &UnicodeName : NULL),
-                             0,
-                             (lpName ? hBaseDir : NULL),
-                             SecurityDescriptor);
-
-   Status = NtCreateSection(&SectionHandle,
-                           SECTION_ALL_ACCESS,
-                           &ObjectAttributes,
-                           MaximumSizePointer,
-                           flProtect & MASK_PAGE_FLAGS,
-                           flProtect & MASK_SEC_FLAGS,
-                           ((hFile != INVALID_HANDLE_VALUE) ? hFile : NULL));
-   if (lpName != NULL)
-     {
-        RtlFreeUnicodeString(&UnicodeName);
-     }
-
-   if (!NT_SUCCESS(Status))
-     {
-       SetLastErrorByStatus(Status);
-       return NULL;
-     }
-   return SectionHandle;
+    NTSTATUS Status;
+    ANSI_STRING AnsiName;
+    PUNICODE_STRING UnicodeCache;
+    LPCWSTR UnicodeName = NULL;
+
+    /* Check for a name */
+    if (lpName)
+    {
+        /* Use TEB Cache */
+        UnicodeCache = &NtCurrentTeb()->StaticUnicodeString;
+
+        /* Convert to unicode */
+        RtlInitAnsiString(&AnsiName, lpName);
+        Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE);
+        if (!NT_SUCCESS(Status))
+        {
+            /* Conversion failed */
+            SetLastErrorByStatus(Status);
+            return NULL;
+        }
+
+        /* Otherwise, save the buffer */
+        UnicodeName = (LPCWSTR)UnicodeCache->Buffer;
+    }
+
+    /* Call the Unicode version */
+    return CreateFileMappingW(hFile,
+                              lpFileMappingAttributes,
+                              flProtect,
+                              dwMaximumSizeHigh,
+                              dwMaximumSizeLow,
+                              UnicodeName);
 }
 
-
 /*
  * @implemented
  */
-HANDLE STDCALL
+HANDLE
+NTAPI
 CreateFileMappingW(HANDLE hFile,
-                  LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
-                  DWORD flProtect,
-                  DWORD dwMaximumSizeHigh,
-                  DWORD dwMaximumSizeLow,
-                  LPCWSTR lpName)
+                   LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
+                   DWORD flProtect,
+                   DWORD dwMaximumSizeHigh,
+                   DWORD dwMaximumSizeLow,
+                   LPCWSTR lpName)
 {
-   NTSTATUS Status;
-   HANDLE SectionHandle;
-   LARGE_INTEGER MaximumSize;
-   PLARGE_INTEGER MaximumSizePointer;
-   OBJECT_ATTRIBUTES ObjectAttributes;
-   UNICODE_STRING UnicodeName;
-   PSECURITY_DESCRIPTOR SecurityDescriptor;
-
-   if ((flProtect & (MASK_PAGE_FLAGS | MASK_SEC_FLAGS)) != flProtect)
-     {
-        DPRINT1("Invalid flProtect 0x%08x\n", flProtect);
-        SetLastError(ERROR_INVALID_PARAMETER);
+    NTSTATUS Status;
+    HANDLE SectionHandle;
+    OBJECT_ATTRIBUTES LocalAttributes;
+    POBJECT_ATTRIBUTES ObjectAttributes;
+    UNICODE_STRING SectionName;
+    ACCESS_MASK DesiredAccess;
+    LARGE_INTEGER LocalSize;
+    PLARGE_INTEGER SectionSize = NULL;
+    ULONG Attributes;
+
+    /* Set default access */
+    DesiredAccess = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ;
+
+    /* Get the attributes for the actual allocation and cleanup flProtect */
+    Attributes = flProtect & (SEC_FILE | SEC_IMAGE | SEC_RESERVE | SEC_NOCACHE | SEC_COMMIT);
+    flProtect ^= Attributes;
+
+    /* If the caller didn't say anything, assume SEC_COMMIT */
+    if (!Attributes) Attributes = SEC_COMMIT;
+
+    /* Now check if the caller wanted write access */
+    if (flProtect == PAGE_READWRITE)
+    {
+        /* Give it */
+        DesiredAccess |= (SECTION_MAP_WRITE | SECTION_MAP_READ);
+    }
+
+    /* Now check if we got a name */
+    if (lpName) RtlInitUnicodeString(&SectionName, lpName);
+
+    /* Now convert the object attributes */
+    ObjectAttributes = BasepConvertObjectAttributes(&LocalAttributes,
+                                                    lpFileMappingAttributes,
+                                                    lpName ? &SectionName : NULL);
+
+    /* Check if we got a size */
+    if (dwMaximumSizeLow || dwMaximumSizeHigh)
+    {
+        /* Use a LARGE_INTEGER and convert */
+        SectionSize = &LocalSize;
+        SectionSize->LowPart = dwMaximumSizeLow;
+        SectionSize->HighPart = dwMaximumSizeHigh;
+    }
+
+    /* Make sure the handle is valid */
+    if (hFile == INVALID_HANDLE_VALUE)
+    {
+        /* It's not, we'll only go on if we have a size */
+        hFile = NULL;
+        if (!SectionSize)
+        {
+            /* No size, so this isn't a valid non-mapped section */
+            SetLastError(ERROR_INVALID_PARAMETER);
+            return NULL;
+        }
+    }
+
+    /* Now create the actual section */
+    Status = NtCreateSection(&SectionHandle,
+                             DesiredAccess,
+                             ObjectAttributes,
+                             SectionSize,
+                             flProtect,
+                             Attributes,
+                             hFile);
+    if (!NT_SUCCESS(Status))
+    {
+        /* We failed */
+        SetLastErrorByStatus(Status);
         return NULL;
-     }
-   if (lpFileMappingAttributes)
-     {
-        SecurityDescriptor = lpFileMappingAttributes->lpSecurityDescriptor;
-     }
-   else
-     {
-        SecurityDescriptor = NULL;
-     }
-
-   if (dwMaximumSizeLow == 0 && dwMaximumSizeHigh == 0)
-     {
-        MaximumSizePointer = NULL;
-     }
-   else
-     {
-        MaximumSize.u.LowPart = dwMaximumSizeLow;
-        MaximumSize.u.HighPart = dwMaximumSizeHigh;
-        MaximumSizePointer = &MaximumSize;
-     }
-
-   if (lpName != NULL)
-     {
-        RtlInitUnicodeString(&UnicodeName,
-                             lpName);
-     }
-
-   InitializeObjectAttributes(&ObjectAttributes,
-                             (lpName ? &UnicodeName : NULL),
-                             0,
-                             (lpName ? hBaseDir : NULL),
-                             SecurityDescriptor);
-
-   Status = NtCreateSection(&SectionHandle,
-                           SECTION_ALL_ACCESS,
-                           &ObjectAttributes,
-                           MaximumSizePointer,
-                           flProtect & MASK_PAGE_FLAGS,
-                           flProtect & MASK_SEC_FLAGS,
-                           ((hFile != INVALID_HANDLE_VALUE) ? hFile : NULL));
-   if (!NT_SUCCESS(Status))
-     {
-       SetLastErrorByStatus(Status);
-       return NULL;
-     }
-   return SectionHandle;
-}
+    }
 
+    /* Return the section */
+    return SectionHandle;
+}
 
 /*
  * @implemented
  */
-LPVOID STDCALL
+LPVOID
+NTAPI
 MapViewOfFileEx(HANDLE hFileMappingObject,
-               DWORD dwDesiredAccess,
-               DWORD dwFileOffsetHigh,
-               DWORD dwFileOffsetLow,
-               DWORD dwNumberOfBytesToMap,
-               LPVOID lpBaseAddress)
+                DWORD dwDesiredAccess,
+                DWORD dwFileOffsetHigh,
+                DWORD dwFileOffsetLow,
+                DWORD dwNumberOfBytesToMap,
+                LPVOID lpBaseAddress)
 {
-   NTSTATUS Status;
-   LARGE_INTEGER SectionOffset;
-   ULONG ViewSize;
-   ULONG Protect;
-   LPVOID BaseAddress;
-
-   SectionOffset.u.LowPart = dwFileOffsetLow;
-   SectionOffset.u.HighPart = dwFileOffsetHigh;
-
-   if ( ( dwDesiredAccess & FILE_MAP_WRITE) == FILE_MAP_WRITE)
-       Protect  = PAGE_READWRITE;
-   else if ((dwDesiredAccess & FILE_MAP_READ) == FILE_MAP_READ)
-       Protect = PAGE_READONLY;
-   else if ((dwDesiredAccess & FILE_MAP_ALL_ACCESS) == FILE_MAP_ALL_ACCESS)
-       Protect  = PAGE_READWRITE;
-   else if ((dwDesiredAccess & FILE_MAP_COPY) == FILE_MAP_COPY)
-       Protect = PAGE_WRITECOPY;
-   else
-       Protect = PAGE_READWRITE;
-
-   if (lpBaseAddress == NULL)
-     {
-       BaseAddress = NULL;
-     }
-   else
-     {
-       BaseAddress = lpBaseAddress;
-     }
-
-   ViewSize = (ULONG) dwNumberOfBytesToMap;
-
-   Status = ZwMapViewOfSection(hFileMappingObject,
-                              NtCurrentProcess(),
-                              &BaseAddress,
-                              0,
-                              dwNumberOfBytesToMap,
-                              &SectionOffset,
-                              &ViewSize,
-                              ViewShare,
-                              0,
-                              Protect);
-   if (!NT_SUCCESS(Status))
-     {
-       SetLastErrorByStatus(Status);
-       return NULL;
-     }
-   return BaseAddress;
-}
+    NTSTATUS Status;
+    LARGE_INTEGER SectionOffset;
+    ULONG ViewSize;
+    ULONG Protect;
+    LPVOID ViewBase;
+
+    /* Convert the offset */
+    SectionOffset.LowPart = dwFileOffsetLow;
+    SectionOffset.HighPart = dwFileOffsetHigh;
+
+    /* Save the size and base */
+    ViewBase = lpBaseAddress;
+    ViewSize = dwNumberOfBytesToMap;
+
+    /* Convert flags to NT Protection Attributes */
+    if (dwDesiredAccess & FILE_MAP_WRITE)
+    {
+        Protect  = PAGE_READWRITE;
+    }
+    else if (dwDesiredAccess & FILE_MAP_READ)
+    {
+        Protect = PAGE_READONLY;
+    }
+    else if (dwDesiredAccess & FILE_MAP_COPY)
+    {
+        Protect = PAGE_WRITECOPY;
+    }
+    else
+    {
+        Protect = PAGE_NOACCESS;
+    }
+
+    /* Map the section */
+    Status = ZwMapViewOfSection(hFileMappingObject,
+                                NtCurrentProcess(),
+                                &ViewBase,
+                                0,
+                                0,
+                                &SectionOffset,
+                                &ViewSize,
+                                ViewShare,
+                                0,
+                                Protect);
+    if (!NT_SUCCESS(Status))
+    {
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return NULL;
+    }
 
+    /* Return the base */
+    return ViewBase;
+}
 
 /*
  * @implemented
  */
-LPVOID STDCALL
+LPVOID
+NTAPI
 MapViewOfFile(HANDLE hFileMappingObject,
-             DWORD dwDesiredAccess,
-             DWORD dwFileOffsetHigh,
-             DWORD dwFileOffsetLow,
-             DWORD dwNumberOfBytesToMap)
+              DWORD dwDesiredAccess,
+              DWORD dwFileOffsetHigh,
+              DWORD dwFileOffsetLow,
+              DWORD dwNumberOfBytesToMap)
 {
-   return MapViewOfFileEx(hFileMappingObject,
-                         dwDesiredAccess,
-                         dwFileOffsetHigh,
-                         dwFileOffsetLow,
-                         dwNumberOfBytesToMap,
-                         NULL);
+    /* Call the extended API */
+    return MapViewOfFileEx(hFileMappingObject,
+                           dwDesiredAccess,
+                           dwFileOffsetHigh,
+                           dwFileOffsetLow,
+                           dwNumberOfBytesToMap,
+                           NULL);
 }
 
-
 /*
  * @implemented
  */
-BOOL STDCALL
+BOOL
+NTAPI
 UnmapViewOfFile(LPVOID lpBaseAddress)
 {
-   NTSTATUS Status;
-
-   Status = NtUnmapViewOfSection(NtCurrentProcess(),
-                                lpBaseAddress);
-   if (!NT_SUCCESS(Status))
-     {
-       SetLastErrorByStatus(Status);
-       return FALSE;
-     }
-   return TRUE;
+    NTSTATUS Status;
+
+    /* Unmap the section */
+    Status = NtUnmapViewOfSection(NtCurrentProcess(), lpBaseAddress);
+    if (!NT_SUCCESS(Status))
+    {
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return FALSE;
+    }
+
+    /* Otherwise, return sucess */
+    return TRUE;
 }
 
-
 /*
  * @implemented
  */
-HANDLE STDCALL
+HANDLE
+NTAPI
 OpenFileMappingA(DWORD dwDesiredAccess,
-                BOOL bInheritHandle,
-                LPCSTR lpName)
+                 BOOL bInheritHandle,
+                 LPCSTR lpName)
 {
-   NTSTATUS Status;
-   HANDLE SectionHandle;
-   OBJECT_ATTRIBUTES ObjectAttributes;
-   ANSI_STRING AnsiName;
-   UNICODE_STRING UnicodeName;
-
-   if (lpName == NULL)
-     {
+    NTSTATUS Status;
+    ANSI_STRING AnsiName;
+    PUNICODE_STRING UnicodeCache;
+
+    /* Check for a name */
+    if (lpName)
+    {
+        /* Use TEB Cache */
+        UnicodeCache = &NtCurrentTeb()->StaticUnicodeString;
+
+        /* Convert to unicode */
+        RtlInitAnsiString(&AnsiName, lpName);
+        Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE);
+        if (!NT_SUCCESS(Status))
+        {
+            /* Conversion failed */
+            SetLastErrorByStatus(Status);
+            return NULL;
+        }
+    }
+    else
+    {
+        /* We need a name */
         SetLastError(ERROR_INVALID_PARAMETER);
         return NULL;
-     }
-
-   RtlInitAnsiString(&AnsiName,
-                    (LPSTR)lpName);
-   RtlAnsiStringToUnicodeString(&UnicodeName,
-                               &AnsiName,
-                               TRUE);
-
-   InitializeObjectAttributes(&ObjectAttributes,
-                             &UnicodeName,
-                             (bInheritHandle ? OBJ_INHERIT : 0),
-                             hBaseDir,
-                             NULL);
-   Status = NtOpenSection(&SectionHandle,
-                           SECTION_ALL_ACCESS,
-                           &ObjectAttributes);
-   RtlFreeUnicodeString (&UnicodeName);
-   if (!NT_SUCCESS(Status))
-     {
-       SetLastErrorByStatus (Status);
-       return NULL;
-     }
-
-   return SectionHandle;
-}
+    }
 
+    /* Call the Unicode version */
+    return OpenFileMappingW(dwDesiredAccess,
+                            bInheritHandle,
+                            (LPCWSTR)UnicodeCache->Buffer);
+}
 
 /*
  * @implemented
  */
-HANDLE STDCALL
+HANDLE
+NTAPI
 OpenFileMappingW(DWORD dwDesiredAccess,
-                BOOL bInheritHandle,
-                LPCWSTR lpName)
+                 BOOL bInheritHandle,
+                 LPCWSTR lpName)
 {
-   NTSTATUS Status;
-   HANDLE SectionHandle;
-   OBJECT_ATTRIBUTES ObjectAttributes;
-   UNICODE_STRING UnicodeName;
-
-   if (lpName == NULL)
-     {
+    NTSTATUS Status;
+    HANDLE SectionHandle;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    UNICODE_STRING UnicodeName;
+
+    /* We need a name */
+    if (!lpName)
+    {
+        /* Otherwise, fail */
         SetLastError(ERROR_INVALID_PARAMETER);
         return NULL;
-     }
-
-   RtlInitUnicodeString(&UnicodeName,
-                       lpName);
-   InitializeObjectAttributes(&ObjectAttributes,
-                             &UnicodeName,
-                             (bInheritHandle ? OBJ_INHERIT : 0),
-                             hBaseDir,
-                             NULL);
-   Status = ZwOpenSection(&SectionHandle,
-                           SECTION_ALL_ACCESS,
-                           &ObjectAttributes);
-   if (!NT_SUCCESS(Status))
-     {
-       SetLastErrorByStatus(Status);
-       return NULL;
-     }
-
-   return SectionHandle;
-}
+    }
+
+    /* Convert attributes */
+    RtlInitUnicodeString(&UnicodeName, lpName);
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &UnicodeName,
+                               (bInheritHandle ? OBJ_INHERIT : 0),
+                               hBaseDir,
+                               NULL);
+
+    /* Convert COPY to READ */
+    if (dwDesiredAccess == FILE_MAP_COPY) dwDesiredAccess = FILE_MAP_READ;
+
+    /* Open the section */
+    Status = ZwOpenSection(&SectionHandle,
+                           dwDesiredAccess,
+                           &ObjectAttributes);
+    if (!NT_SUCCESS(Status))
+    {
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return NULL;
+    }
 
+    /* Otherwise, return the handle */
+    return SectionHandle;
+}
 
 /*
  * @implemented
  */
-BOOL STDCALL
+BOOL
+NTAPI
 FlushViewOfFile(LPCVOID lpBaseAddress,
-               DWORD dwNumberOfBytesToFlush)
+                DWORD dwNumberOfBytesToFlush)
 {
-   NTSTATUS Status;
-   ULONG NumberOfBytesFlushed;
-
-   Status = NtFlushVirtualMemory(NtCurrentProcess(),
-                                (LPVOID)lpBaseAddress,
-                                dwNumberOfBytesToFlush,
-                                &NumberOfBytesFlushed);
-   if (!NT_SUCCESS(Status))
-     {
-       SetLastErrorByStatus(Status);
-       return FALSE;
-     }
-   return TRUE;
+    NTSTATUS Status;
+    ULONG NumberOfBytesFlushed;
+
+    /* Flush the view */
+    Status = NtFlushVirtualMemory(NtCurrentProcess(),
+                                  (LPVOID)lpBaseAddress,
+                                  dwNumberOfBytesToFlush,
+                                  &NumberOfBytesFlushed);
+    if (!NT_SUCCESS(Status))
+    {
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return FALSE;
+    }
+
+    /* Return success */
+    return TRUE;
 }
 
 /* EOF */
index 6a85bb8..39e1a7f 100644 (file)
@@ -1,10 +1,9 @@
-/* $Id$
- *
- * COPYRIGHT:            See COPYING in the top level directory
- * PROJECT:              ReactOS kernel
- * FILE:                 lib/kernel32/mem/virtual.c
- * PURPOSE:              Passing the Virtualxxx functions onto the kernel
- * PROGRAMMER:           David Welch (welch@mcmail.com)
+/*
+ * PROJECT:         ReactOS Win32 Base API
+ * LICENSE:         GPL - See COPYING in the top level directory
+ * FILE:            dll/win32/kernel32/mem/virtual.c
+ * PURPOSE:         Handles virtual memory APIs
+ * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
  */
 
 /* INCLUDES ******************************************************************/
 #include <k32.h>
 
 #define NDEBUG
-#include "../include/debug.h"
+#include "debug.h"
 
 /* FUNCTIONS *****************************************************************/
 
 /*
  * @implemented
  */
-LPVOID STDCALL
-VirtualAllocEx(HANDLE hProcess,
-              LPVOID lpAddress,
-              SIZE_T dwSize,
-              DWORD flAllocationType,
-              DWORD flProtect)
+LPVOID
+NTAPI
+VirtualAllocEx(IN HANDLE hProcess,
+               IN LPVOID lpAddress,
+               IN SIZE_T dwSize,
+               IN DWORD flAllocationType,
+               IN DWORD flProtect)
 {
-  NTSTATUS Status;
-
-  Status = NtAllocateVirtualMemory(hProcess,
-                                  (PVOID *)&lpAddress,
-                                  0,
-                                  &dwSize,
-                                  flAllocationType,
-                                  flProtect);
-  if (!NT_SUCCESS(Status))
+    NTSTATUS Status;
+
+    /* Allocate the memory */
+    Status = NtAllocateVirtualMemory(hProcess,
+                                     (PVOID *)&lpAddress,
+                                     0,
+                                     &dwSize,
+                                     flAllocationType,
+                                     flProtect);
+    if (!NT_SUCCESS(Status))
     {
-      SetLastErrorByStatus(Status);
-      return(NULL);
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return NULL;
     }
-  return(lpAddress);
-}
 
+    /* Return the allocated address */
+    return lpAddress;
+}
 
 /*
  * @implemented
  */
-LPVOID STDCALL
-VirtualAlloc(LPVOID lpAddress,
-            SIZE_T dwSize,
-            DWORD flAllocationType,
-            DWORD flProtect)
+LPVOID
+NTAPI
+VirtualAlloc(IN LPVOID lpAddress,
+             IN SIZE_T dwSize,
+             IN DWORD flAllocationType,
+             IN DWORD flProtect)
 {
-  return(VirtualAllocEx(GetCurrentProcess(),
-                       lpAddress,
-                       dwSize,
-                       flAllocationType,
-                       flProtect));
+    /* Call the extended API */
+    return VirtualAllocEx(GetCurrentProcess(),
+                          lpAddress,
+                          dwSize,
+                          flAllocationType,
+                          flProtect);
 }
 
-
 /*
  * @implemented
  */
-BOOL STDCALL
-VirtualFreeEx(HANDLE hProcess,
-             LPVOID lpAddress,
-             SIZE_T dwSize,
-             DWORD dwFreeType)
+BOOL
+NTAPI
+VirtualFreeEx(IN HANDLE hProcess,
+              IN LPVOID lpAddress,
+              IN SIZE_T dwSize,
+              IN DWORD dwFreeType)
 {
-  NTSTATUS Status;
+    NTSTATUS Status;
 
-  Status = NtFreeVirtualMemory(hProcess,
-                              (PVOID *)&lpAddress,
-                              (PULONG)&dwSize,
-                              dwFreeType);
-  if (!NT_SUCCESS(Status))
+    /* Free the memory */
+    Status = NtFreeVirtualMemory(hProcess,
+                                 (PVOID *)&lpAddress,
+                                 (PULONG)&dwSize,
+                                 dwFreeType);
+    if (!NT_SUCCESS(Status))
     {
-      SetLastErrorByStatus(Status);
-      return(FALSE);
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return FALSE;
     }
-  return(TRUE);
-}
 
+    /* Return success */
+    return TRUE;
+}
 
 /*
  * @implemented
  */
-BOOL STDCALL
-VirtualFree(LPVOID lpAddress,
-           SIZE_T dwSize,
-           DWORD dwFreeType)
+BOOL
+NTAPI
+VirtualFree(IN LPVOID lpAddress,
+            IN SIZE_T dwSize,
+            IN DWORD dwFreeType)
 {
-  return(VirtualFreeEx(GetCurrentProcess(),
-                      lpAddress,
-                      dwSize,
-                      dwFreeType));
+    /* Call the extended API */
+    return VirtualFreeEx(GetCurrentProcess(),
+                         lpAddress,
+                         dwSize,
+                         dwFreeType);
 }
 
-
 /*
  * @implemented
  */
-BOOL STDCALL
-VirtualProtect(LPVOID lpAddress,
-              SIZE_T dwSize,
-              DWORD flNewProtect,
-              PDWORD lpflOldProtect)
+BOOL
+NTAPI
+VirtualProtect(IN LPVOID lpAddress,
+               IN SIZE_T dwSize,
+               IN DWORD flNewProtect,
+               OUT PDWORD lpflOldProtect)
 {
-  return(VirtualProtectEx(GetCurrentProcess(),
-                         lpAddress,
-                         dwSize,
-                         flNewProtect,
-                         lpflOldProtect));
+    /* Call the extended API */
+    return VirtualProtectEx(GetCurrentProcess(),
+                            lpAddress,
+                            dwSize,
+                            flNewProtect,
+                            lpflOldProtect);
 }
 
-
 /*
  * @implemented
  */
-BOOL STDCALL
-VirtualProtectEx(HANDLE hProcess,
-                LPVOID lpAddress,
-                SIZE_T dwSize,
-                DWORD flNewProtect,
-                PDWORD lpflOldProtect)
+BOOL
+NTAPI
+VirtualProtectEx(IN HANDLE hProcess,
+                 IN LPVOID lpAddress,
+                 IN SIZE_T dwSize,
+                 IN DWORD flNewProtect,
+                 OUT PDWORD lpflOldProtect)
 {
-  NTSTATUS Status;
-
-  Status = NtProtectVirtualMemory(hProcess,
-                                 &lpAddress,
-                                 &dwSize,
-                                 flNewProtect,
-                                 (PULONG)lpflOldProtect);
-  if (!NT_SUCCESS(Status))
+    NTSTATUS Status;
+
+    /* Change the protection */
+    Status = NtProtectVirtualMemory(hProcess,
+                                    &lpAddress,
+                                    &dwSize,
+                                    flNewProtect,
+                                    (PULONG)lpflOldProtect);
+    if (!NT_SUCCESS(Status))
     {
-      SetLastErrorByStatus(Status);
-      return(FALSE);
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return FALSE;
     }
-  return(TRUE);
-}
 
+    /* Return success */
+    return TRUE;
+}
 
 /*
  * @implemented
  */
-BOOL STDCALL
-VirtualLock(LPVOID lpAddress,
-           SIZE_T dwSize)
+BOOL
+NTAPI
+VirtualLock(IN LPVOID lpAddress,
+            IN SIZE_T dwSize)
 {
-  ULONG BytesLocked;
-  NTSTATUS Status;
-
-  Status = NtLockVirtualMemory(NtCurrentProcess(),
-                              lpAddress,
-                              dwSize,
-                              &BytesLocked);
-  if (!NT_SUCCESS(Status))
+    ULONG BytesLocked;
+    NTSTATUS Status;
+
+    /* Lock the memory */
+    Status = NtLockVirtualMemory(NtCurrentProcess(),
+                                 lpAddress,
+                                 dwSize,
+                                 &BytesLocked);
+    if (!NT_SUCCESS(Status))
     {
-      SetLastErrorByStatus(Status);
-      return(FALSE);
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return FALSE;
     }
-  return(TRUE);
-}
 
+    /* Return success */
+    return TRUE;
+}
 
 /*
  * @implemented
  */
-DWORD STDCALL
-VirtualQuery(LPCVOID lpAddress,
-            PMEMORY_BASIC_INFORMATION lpBuffer,
-            SIZE_T dwLength)
+DWORD
+NTAPI
+VirtualQuery(IN LPCVOID lpAddress,
+             OUT PMEMORY_BASIC_INFORMATION lpBuffer,
+             IN SIZE_T dwLength)
 {
-  return(VirtualQueryEx(NtCurrentProcess(),
-                       lpAddress,
-                       lpBuffer,
-                       dwLength));
+    /* Call the extended API */
+    return VirtualQueryEx(NtCurrentProcess(),
+                          lpAddress,
+                          lpBuffer,
+                          dwLength);
 }
 
-
 /*
  * @implemented
  */
-DWORD STDCALL
-VirtualQueryEx(HANDLE hProcess,
-              LPCVOID lpAddress,
-              PMEMORY_BASIC_INFORMATION lpBuffer,
-              SIZE_T dwLength)
+DWORD
+NTAPI
+VirtualQueryEx(IN HANDLE hProcess,
+               IN LPCVOID lpAddress,
+               OUT PMEMORY_BASIC_INFORMATION lpBuffer,
+               IN SIZE_T dwLength)
 {
-  NTSTATUS Status;
-  ULONG ResultLength;
-
-  Status = NtQueryVirtualMemory(hProcess,
-                               (LPVOID)lpAddress,
-                               MemoryBasicInformation,
-                               lpBuffer,
-                               sizeof(MEMORY_BASIC_INFORMATION),
-                               &ResultLength );
-  if (!NT_SUCCESS(Status))
+    NTSTATUS Status;
+    ULONG ResultLength;
+
+    /* Query basic information */
+    Status = NtQueryVirtualMemory(hProcess,
+                                  (LPVOID)lpAddress,
+                                  MemoryBasicInformation,
+                                  lpBuffer,
+                                  sizeof(MEMORY_BASIC_INFORMATION),
+                                  &ResultLength);
+    if (!NT_SUCCESS(Status))
     {
-      SetLastErrorByStatus(Status);
-      return 0;
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return 0;
     }
-  return(ResultLength);
-}
 
+    /* Return the length returned */
+    return ResultLength;
+}
 
 /*
  * @implemented
  */
-BOOL STDCALL
-VirtualUnlock(LPVOID lpAddress,
-             SIZE_T dwSize)
+BOOL
+NTAPI
+VirtualUnlock(IN LPVOID lpAddress,
+              IN SIZE_T dwSize)
 {
-  ULONG BytesLocked;
-  NTSTATUS Status;
-
-  Status = NtUnlockVirtualMemory(NtCurrentProcess(),
-                                lpAddress,
-                                dwSize,
-                                &BytesLocked);
-  if (!NT_SUCCESS(Status))
+    ULONG BytesLocked;
+    NTSTATUS Status;
+
+    /* Unlock the memory */
+    Status = NtUnlockVirtualMemory(NtCurrentProcess(),
+                                   lpAddress,
+                                   dwSize,
+                                   &BytesLocked);
+    if (!NT_SUCCESS(Status))
     {
-      SetLastErrorByStatus(Status);
-      return(FALSE);
+        /* We failed */
+        SetLastErrorByStatus(Status);
+        return FALSE;
     }
-  return(TRUE);
+
+    /* Return success */
+    return TRUE;
 }
 
 /* EOF */