ReactOS 0.4.15-dev-8100-g1887773
filemap.c File Reference
#include <k32.h>
#include <debug.h>
Include dependency graph for filemap.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

HANDLE NTAPI CreateFileMappingA (IN HANDLE hFile, IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes, IN DWORD flProtect, IN DWORD dwMaximumSizeHigh, IN DWORD dwMaximumSizeLow, IN LPCSTR lpName)
 
HANDLE NTAPI CreateFileMappingW (HANDLE hFile, LPSECURITY_ATTRIBUTES lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCWSTR lpName)
 
LPVOID NTAPI MapViewOfFileEx (HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress)
 
LPVOID NTAPI MapViewOfFile (HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap)
 
BOOL NTAPI UnmapViewOfFile (LPCVOID lpBaseAddress)
 
HANDLE NTAPI OpenFileMappingA (IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCSTR lpName)
 
HANDLE NTAPI OpenFileMappingW (IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName)
 
BOOL NTAPI FlushViewOfFile (IN LPCVOID lpBaseAddress, IN SIZE_T dwNumberOfBytesToFlush)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file filemap.c.

Function Documentation

◆ CreateFileMappingA()

HANDLE NTAPI CreateFileMappingA ( IN HANDLE  hFile,
IN LPSECURITY_ATTRIBUTES  lpFileMappingAttributes,
IN DWORD  flProtect,
IN DWORD  dwMaximumSizeHigh,
IN DWORD  dwMaximumSizeLow,
IN LPCSTR  lpName 
)

◆ CreateFileMappingW()

HANDLE NTAPI CreateFileMappingW ( HANDLE  hFile,
LPSECURITY_ATTRIBUTES  lpFileMappingAttributes,
DWORD  flProtect,
DWORD  dwMaximumSizeHigh,
DWORD  dwMaximumSizeLow,
LPCWSTR  lpName 
)

Definition at line 45 of file filemap.c.

51{
53 HANDLE SectionHandle;
54 OBJECT_ATTRIBUTES LocalAttributes;
56 UNICODE_STRING SectionName;
59 PLARGE_INTEGER SectionSize = NULL;
61
62 /* Set default access */
64
65 /* Get the attributes for the actual allocation and cleanup flProtect */
67 flProtect ^= Attributes;
68
69 /* If the caller didn't say anything, assume SEC_COMMIT */
71
72 /* Now check if the caller wanted write access */
73 if (flProtect == PAGE_READWRITE)
74 {
75 /* Give it */
77 }
78 else if (flProtect == PAGE_EXECUTE_READWRITE)
79 {
80 /* Give it */
82 }
83 else if (flProtect == PAGE_EXECUTE_READ)
84 {
85 /* Give it */
87 }
88 else if ((flProtect == PAGE_EXECUTE_WRITECOPY) &&
89 (NtCurrentPeb()->OSMajorVersion >= 6))
90 {
91 /* Give it */
93 }
94 else if ((flProtect != PAGE_READONLY) && (flProtect != PAGE_WRITECOPY))
95 {
97 return NULL;
98 }
99
100 /* Now check if we got a name */
101 if (lpName) RtlInitUnicodeString(&SectionName, lpName);
102
103 /* Now convert the object attributes */
105 lpFileMappingAttributes,
106 lpName ? &SectionName : NULL);
107
108 /* Check if we got a size */
109 if (dwMaximumSizeLow || dwMaximumSizeHigh)
110 {
111 /* Use a LARGE_INTEGER and convert */
112 SectionSize = &LocalSize;
113 SectionSize->LowPart = dwMaximumSizeLow;
114 SectionSize->HighPart = dwMaximumSizeHigh;
115 }
116
117 /* Make sure the handle is valid */
119 {
120 /* It's not, we'll only go on if we have a size */
121 hFile = NULL;
122 if (!SectionSize)
123 {
124 /* No size, so this isn't a valid non-mapped section */
126 return NULL;
127 }
128 }
129
130 /* Now create the actual section */
131 Status = NtCreateSection(&SectionHandle,
134 SectionSize,
135 flProtect,
137 hFile);
138 if (!NT_SUCCESS(Status))
139 {
140 /* We failed */
142 return NULL;
143 }
145 {
147 }
148 else
149 {
151 }
152
153 /* Return the section */
154 return SectionHandle;
155}
NTSTATUS NTAPI NtCreateSection(OUT PHANDLE SectionHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN PLARGE_INTEGER MaximumSize OPTIONAL, IN ULONG SectionPageProtection OPTIONAL, IN ULONG AllocationAttributes, IN HANDLE FileHandle OPTIONAL)
Definition: section.c:3441
#define NtCurrentPeb()
Definition: FLS.c:22
LONG NTSTATUS
Definition: precomp.h:26
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define PAGE_READONLY
Definition: compat.h:138
#define SECTION_MAP_READ
Definition: compat.h:139
#define SetLastError(x)
Definition: compat.h:752
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
POBJECT_ATTRIBUTES WINAPI BaseFormatObjectAttributes(OUT POBJECT_ATTRIBUTES ObjectAttributes, IN PSECURITY_ATTRIBUTES SecurityAttributes OPTIONAL, IN PUNICODE_STRING ObjectName)
Definition: utils.c:304
Status
Definition: gdiplustypes.h:25
SIZE_T NTAPI LocalSize(HLOCAL hMem)
Definition: heapmem.c:1794
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
#define SEC_NOCACHE
Definition: mmtypes.h:101
#define SEC_COMMIT
Definition: mmtypes.h:100
#define SEC_IMAGE
Definition: mmtypes.h:97
#define SEC_FILE
Definition: mmtypes.h:96
#define SEC_LARGE_PAGES
Definition: mmtypes.h:103
#define SECTION_MAP_EXECUTE
Definition: nt_native.h:1290
#define PAGE_WRITECOPY
Definition: nt_native.h:1305
#define SECTION_MAP_WRITE
Definition: nt_native.h:1288
ULONG ACCESS_MASK
Definition: nt_native.h:40
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define PAGE_EXECUTE_READ
Definition: nt_native.h:1307
#define SECTION_QUERY
Definition: nt_native.h:1287
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define SEC_RESERVE
Definition: nt_native.h:1323
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1309
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define STANDARD_RIGHTS_REQUIRED
Definition: nt_native.h:63
#define STATUS_OBJECT_NAME_EXISTS
Definition: ntstatus.h:114
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
uint32_t ULONG
Definition: typedefs.h:59
ULONG LowPart
Definition: typedefs.h:106
_Must_inspect_result_ _In_ WDFDMAENABLER _In_ _In_opt_ PWDF_OBJECT_ATTRIBUTES Attributes
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2658

◆ FlushViewOfFile()

BOOL NTAPI FlushViewOfFile ( IN LPCVOID  lpBaseAddress,
IN SIZE_T  dwNumberOfBytesToFlush 
)

Definition at line 352 of file filemap.c.

354{
356 PVOID BaseAddress = (PVOID)lpBaseAddress;
357 SIZE_T NumberOfBytesToFlush = dwNumberOfBytesToFlush;
359
360 /* Flush the view */
363 &NumberOfBytesToFlush,
366 {
367 /* We failed */
369 return FALSE;
370 }
371
372 /* Return success */
373 return TRUE;
374}
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define NtCurrentProcess()
Definition: nt_native.h:1657
NTSTATUS NTAPI NtFlushVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToFlush, OUT PIO_STATUS_BLOCK IoStatusBlock)
Definition: virtual.c:4035
void * PVOID
Definition: typedefs.h:50
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define STATUS_NOT_MAPPED_DATA
Definition: udferr_usr.h:157

Referenced by DPLAYX_ConstructData().

◆ MapViewOfFile()

LPVOID NTAPI MapViewOfFile ( HANDLE  hFileMappingObject,
DWORD  dwDesiredAccess,
DWORD  dwFileOffsetHigh,
DWORD  dwFileOffsetLow,
SIZE_T  dwNumberOfBytesToMap 
)

Definition at line 230 of file filemap.c.

235{
236 /* Call the extended API */
237 return MapViewOfFileEx(hFileMappingObject,
238 dwDesiredAccess,
239 dwFileOffsetHigh,
240 dwFileOffsetLow,
241 dwNumberOfBytesToMap,
242 NULL);
243}
LPVOID NTAPI MapViewOfFileEx(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress)
Definition: filemap.c:162

◆ MapViewOfFileEx()

LPVOID NTAPI MapViewOfFileEx ( HANDLE  hFileMappingObject,
DWORD  dwDesiredAccess,
DWORD  dwFileOffsetHigh,
DWORD  dwFileOffsetLow,
SIZE_T  dwNumberOfBytesToMap,
LPVOID  lpBaseAddress 
)

Definition at line 162 of file filemap.c.

168{
173 LPVOID ViewBase;
174
175 /* Convert the offset */
176 SectionOffset.LowPart = dwFileOffsetLow;
177 SectionOffset.HighPart = dwFileOffsetHigh;
178
179 /* Save the size and base */
180 ViewBase = lpBaseAddress;
181 ViewSize = dwNumberOfBytesToMap;
182
183 /* Convert flags to NT Protection Attributes */
184 if (dwDesiredAccess == FILE_MAP_COPY)
185 {
187 }
188 else if (dwDesiredAccess & FILE_MAP_WRITE)
189 {
190 Protect = (dwDesiredAccess & FILE_MAP_EXECUTE) ?
192 }
193 else if (dwDesiredAccess & FILE_MAP_READ)
194 {
195 Protect = (dwDesiredAccess & FILE_MAP_EXECUTE) ?
197 }
198 else
199 {
201 }
202
203 /* Map the section */
204 Status = NtMapViewOfSection(hFileMappingObject,
206 &ViewBase,
207 0,
208 0,
210 &ViewSize,
211 ViewShare,
212 0,
213 Protect);
214 if (!NT_SUCCESS(Status))
215 {
216 /* We failed */
218 return NULL;
219 }
220
221 /* Return the base */
222 return ViewBase;
223}
NTSTATUS NTAPI NtMapViewOfSection(IN HANDLE SectionHandle, IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
Definition: section.c:3622
#define FILE_MAP_READ
Definition: compat.h:776
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:407
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:408
@ ViewShare
Definition: nt_native.h:1278
#define PAGE_NOACCESS
Definition: nt_native.h:1302
#define FILE_MAP_WRITE
Definition: winbase.h:154
#define FILE_MAP_COPY
Definition: winbase.h:153
#define FILE_MAP_EXECUTE
Definition: winbase.h:157
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG _In_ ULONG Protect
Definition: zwfuncs.h:221

Referenced by DPLAYX_ConstructData(), and MapViewOfFile().

◆ OpenFileMappingA()

HANDLE NTAPI OpenFileMappingA ( IN DWORD  dwDesiredAccess,
IN BOOL  bInheritHandle,
IN LPCSTR  lpName 
)

Definition at line 284 of file filemap.c.

287{
289}
#define ConvertOpenWin32AnsiObjectApiToUnicodeApi(obj, acc, inh, name)
Definition: base_x.h:86
static BOOL bInheritHandle
Definition: pipe.c:82

Referenced by doChild(), test_CreateFileMapping(), test_filemap_security(), and test_MapViewOfFile().

◆ OpenFileMappingW()

HANDLE NTAPI OpenFileMappingW ( IN DWORD  dwDesiredAccess,
IN BOOL  bInheritHandle,
IN LPCWSTR  lpName 
)

Definition at line 297 of file filemap.c.

300{
302 HANDLE SectionHandle;
305
306 /* We need a name */
307 if (!lpName)
308 {
309 /* Otherwise, fail */
311 return NULL;
312 }
313
314 /* Convert attributes */
320 NULL);
321
322 /* Convert COPY to READ */
323 if (dwDesiredAccess == FILE_MAP_COPY)
324 {
325 /* Fixup copy */
326 dwDesiredAccess = SECTION_MAP_READ;
327 }
328 else if (dwDesiredAccess & FILE_MAP_EXECUTE)
329 {
330 /* Fixup execute */
331 dwDesiredAccess = (dwDesiredAccess & ~FILE_MAP_EXECUTE) | SECTION_MAP_EXECUTE;
332 }
333
334 /* Open the section */
335 Status = NtOpenSection(&SectionHandle, dwDesiredAccess, &ObjectAttributes);
336 if (!NT_SUCCESS(Status))
337 {
338 /* We failed */
340 return NULL;
341 }
342
343 /* Otherwise, return the handle */
344 return SectionHandle;
345}
NTSTATUS NTAPI NtOpenSection(OUT PHANDLE SectionHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
Definition: section.c:3569
HANDLE WINAPI BaseGetNamedObjectDirectory(VOID)
Definition: utils.c:63
IN PDCB IN POEM_STRING IN PUNICODE_STRING UnicodeName
Definition: fatprocs.h:1305
#define OBJ_INHERIT
Definition: winternl.h:225
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106

Referenced by cache_container_map_index(), ImmPutImeMenuItemsIntoMappedFile(), main(), map_memory(), and OutputDebugStringA().

◆ UnmapViewOfFile()

BOOL NTAPI UnmapViewOfFile ( LPCVOID  lpBaseAddress)

Definition at line 250 of file filemap.c.

251{
253
254 /* Unmap the section */
256 if (!NT_SUCCESS(Status))
257 {
258 /* Check if the pages were protected */
260 {
261 /* Flush the region if it was a "secure memory cache" */
262 if (RtlFlushSecureMemoryCache((PVOID)lpBaseAddress, 0))
263 {
264 /* Now try to unmap again */
266 if (NT_SUCCESS(Status)) return TRUE;
267 }
268 }
269
270 /* We failed */
272 return FALSE;
273 }
274
275 /* Otherwise, return success */
276 return TRUE;
277}
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3848
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:305
BOOLEAN NTAPI RtlFlushSecureMemoryCache(IN PVOID MemoryCache, IN OPTIONAL SIZE_T MemoryLength)
Definition: security.c:830