ReactOS 0.4.15-dev-8093-g3285f69
filemap.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/client/file/filemap.c
5 * PURPOSE: Handles virtual memory APIs
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9/* INCLUDES *******************************************************************/
10
11#include <k32.h>
12
13#define NDEBUG
14#include <debug.h>
15
16/* FUNCTIONS ******************************************************************/
17
18/*
19 * @implemented
20 */
24 IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
25 IN DWORD flProtect,
26 IN DWORD dwMaximumSizeHigh,
27 IN DWORD dwMaximumSizeLow,
29{
30 /* Call the W(ide) function */
32 lpName,
33 hFile,
34 lpFileMappingAttributes,
35 flProtect,
36 dwMaximumSizeHigh,
37 dwMaximumSizeLow);
38}
39
40/*
41 * @implemented
42 */
46 LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
47 DWORD flProtect,
48 DWORD dwMaximumSizeHigh,
49 DWORD dwMaximumSizeLow,
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}
156
157/*
158 * @implemented
159 */
160LPVOID
161NTAPI
162MapViewOfFileEx(HANDLE hFileMappingObject,
163 DWORD dwDesiredAccess,
164 DWORD dwFileOffsetHigh,
165 DWORD dwFileOffsetLow,
166 SIZE_T dwNumberOfBytesToMap,
167 LPVOID lpBaseAddress)
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}
224
225/*
226 * @implemented
227 */
228LPVOID
229NTAPI
230MapViewOfFile(HANDLE hFileMappingObject,
231 DWORD dwDesiredAccess,
232 DWORD dwFileOffsetHigh,
233 DWORD dwFileOffsetLow,
234 SIZE_T dwNumberOfBytesToMap)
235{
236 /* Call the extended API */
237 return MapViewOfFileEx(hFileMappingObject,
238 dwDesiredAccess,
239 dwFileOffsetHigh,
240 dwFileOffsetLow,
241 dwNumberOfBytesToMap,
242 NULL);
243}
244
245/*
246 * @implemented
247 */
248BOOL
249NTAPI
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}
278
279/*
280 * @implemented
281 */
282HANDLE
283NTAPI
284OpenFileMappingA(IN DWORD dwDesiredAccess,
287{
289}
290
291/*
292 * @implemented
293 */
294 /* FIXME: Convert to the new macros */
295HANDLE
296NTAPI
297OpenFileMappingW(IN DWORD dwDesiredAccess,
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}
346
347/*
348 * @implemented
349 */
350BOOL
351NTAPI
353 IN SIZE_T dwNumberOfBytesToFlush)
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}
375
376/* EOF */
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3848
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
NTSTATUS NTAPI NtOpenSection(OUT PHANDLE SectionHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
Definition: section.c:3569
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 NtCurrentPeb()
Definition: FLS.c:22
LONG NTSTATUS
Definition: precomp.h:26
#define ConvertWin32AnsiObjectApiToUnicodeApi(obj, name,...)
Definition: base_x.h:54
#define ConvertOpenWin32AnsiObjectApiToUnicodeApi(obj, acc, inh, name)
Definition: base_x.h:86
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 TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#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 UnmapViewOfFile
Definition: compat.h:746
#define SetLastError(x)
Definition: compat.h:752
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define CreateFileMappingW(a, b, c, d, e, f)
Definition: compat.h:744
#define FILE_MAP_READ
Definition: compat.h:776
#define MapViewOfFile
Definition: compat.h:745
HANDLE WINAPI BaseGetNamedObjectDirectory(VOID)
Definition: utils.c:63
POBJECT_ATTRIBUTES WINAPI BaseFormatObjectAttributes(OUT POBJECT_ATTRIBUTES ObjectAttributes, IN PSECURITY_ATTRIBUTES SecurityAttributes OPTIONAL, IN PUNICODE_STRING ObjectName)
Definition: utils.c:304
IN PDCB IN POEM_STRING IN PUNICODE_STRING UnicodeName
Definition: fatprocs.h:1305
HANDLE NTAPI OpenFileMappingW(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName)
Definition: filemap.c:297
LPVOID NTAPI MapViewOfFileEx(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress)
Definition: filemap.c:162
BOOL NTAPI FlushViewOfFile(IN LPCVOID lpBaseAddress, IN SIZE_T dwNumberOfBytesToFlush)
Definition: filemap.c:352
HANDLE NTAPI CreateFileMappingA(IN HANDLE hFile, IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes, IN DWORD flProtect, IN DWORD dwMaximumSizeHigh, IN DWORD dwMaximumSizeLow, IN LPCSTR lpName)
Definition: filemap.c:23
HANDLE NTAPI OpenFileMappingA(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCSTR lpName)
Definition: filemap.c:284
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
SIZE_T NTAPI LocalSize(HLOCAL hMem)
Definition: heapmem.c:1794
#define OBJ_INHERIT
Definition: winternl.h:225
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
static BOOL bInheritHandle
Definition: pipe.c:82
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
_In_ HANDLE hFile
Definition: mswsock.h:90
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
_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
#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 NtCurrentProcess()
Definition: nt_native.h:1657
@ ViewShare
Definition: nt_native.h:1278
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1309
#define PAGE_NOACCESS
Definition: nt_native.h:1302
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define STANDARD_RIGHTS_REQUIRED
Definition: nt_native.h:63
NTSTATUS NTAPI NtFlushVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToFlush, OUT PIO_STATUS_BLOCK IoStatusBlock)
Definition: virtual.c:4035
#define STATUS_OBJECT_NAME_EXISTS
Definition: ntstatus.h:114
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:305
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
BOOLEAN NTAPI RtlFlushSecureMemoryCache(IN PVOID MemoryCache, IN OPTIONAL SIZE_T MemoryLength)
Definition: security.c:830
#define NTAPI
Definition: typedefs.h:36
void * PVOID
Definition: typedefs.h:50
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_NOT_MAPPED_DATA
Definition: udferr_usr.h:157
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
#define FILE_MAP_WRITE
Definition: winbase.h:154
_In_ LPCSTR lpName
Definition: winbase.h:2789
#define FILE_MAP_COPY
Definition: winbase.h:153
#define FILE_MAP_EXECUTE
Definition: winbase.h:157
CONST void * LPCVOID
Definition: windef.h:191
const char * LPCSTR
Definition: xmlstorage.h:183
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
_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