:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / kernel32 / mem / section.c
1 /* $Id$
2  *
3  * COPYRIGHT:            See COPYING in the top level directory
4  * PROJECT:              ReactOS kernel
5  * FILE:                 lib/kernel32/mem/section.c
6  * PURPOSE:              Implementing file mapping
7  * PROGRAMMER:           David Welch (welch@mcmail.com)
8  */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <ddk/ntddk.h>
13 #include <kernel32/error.h>
14 #include <windows.h>
15
16 #include <kernel32/kernel32.h>
17
18 /* FUNCTIONS *****************************************************************/
19
20 HANDLE STDCALL
21 CreateFileMappingA(HANDLE hFile,
22                    LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
23                    DWORD flProtect,
24                    DWORD dwMaximumSizeHigh,
25                    DWORD dwMaximumSizeLow,
26                    LPCSTR lpName)
27 {
28    NTSTATUS Status;
29    HANDLE SectionHandle;
30    LARGE_INTEGER MaximumSize;
31    OBJECT_ATTRIBUTES ObjectAttributes;
32    ANSI_STRING AnsiName;
33    UNICODE_STRING UnicodeName;
34    PSECURITY_DESCRIPTOR SecurityDescriptor;
35
36    if (lpFileMappingAttributes)
37      {
38         SecurityDescriptor = lpFileMappingAttributes->lpSecurityDescriptor;
39      }
40    else
41      {
42         SecurityDescriptor = NULL;
43      }
44
45    MaximumSize.u.LowPart = dwMaximumSizeLow;
46    MaximumSize.u.HighPart = dwMaximumSizeHigh;
47    RtlInitAnsiString(&AnsiName,
48                      (LPSTR)lpName);
49    RtlAnsiStringToUnicodeString(&UnicodeName,
50                                 &AnsiName,
51                                 TRUE);
52    InitializeObjectAttributes(&ObjectAttributes,
53                               &UnicodeName,
54                               0,
55                               hBaseDir,
56                               SecurityDescriptor);
57    Status = NtCreateSection(&SectionHandle,
58                             SECTION_ALL_ACCESS,
59                             &ObjectAttributes,
60                             &MaximumSize,
61                             flProtect,
62                             0,
63                             hFile);
64    RtlFreeUnicodeString(&UnicodeName);
65    if (!NT_SUCCESS(Status))
66      {
67         SetLastErrorByStatus(Status);
68         return NULL;
69      }
70    return SectionHandle;
71 }
72
73
74 HANDLE STDCALL
75 CreateFileMappingW(HANDLE hFile,
76                    LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
77                    DWORD flProtect,
78                    DWORD dwMaximumSizeHigh,
79                    DWORD dwMaximumSizeLow,
80                    LPCWSTR lpName)
81 {
82    NTSTATUS Status;
83    HANDLE SectionHandle;
84    LARGE_INTEGER MaximumSize;
85    OBJECT_ATTRIBUTES ObjectAttributes;
86    UNICODE_STRING UnicodeName;
87    PSECURITY_DESCRIPTOR SecurityDescriptor;
88
89    if (lpFileMappingAttributes)
90      {
91         SecurityDescriptor = lpFileMappingAttributes->lpSecurityDescriptor;
92      }
93    else
94      {
95         SecurityDescriptor = NULL;
96      }
97
98    MaximumSize.u.LowPart = dwMaximumSizeLow;
99    MaximumSize.u.HighPart = dwMaximumSizeHigh;
100    RtlInitUnicodeString(&UnicodeName,
101                         lpName);
102    InitializeObjectAttributes(&ObjectAttributes,
103                               &UnicodeName,
104                               0,
105                               hBaseDir,
106                               SecurityDescriptor);
107    Status = NtCreateSection(&SectionHandle,
108                             SECTION_ALL_ACCESS,
109                             &ObjectAttributes,
110                             &MaximumSize,
111                             flProtect,
112                             0,
113                             hFile);
114    if (!NT_SUCCESS(Status))
115      {
116         SetLastErrorByStatus(Status);
117         return NULL;
118      }
119    return SectionHandle;
120 }
121
122
123 LPVOID STDCALL
124 MapViewOfFileEx(HANDLE hFileMappingObject,
125                 DWORD dwDesiredAccess,
126                 DWORD dwFileOffsetHigh,
127                 DWORD dwFileOffsetLow,
128                 DWORD dwNumberOfBytesToMap,
129                 LPVOID lpBaseAddress)
130 {
131    NTSTATUS Status;
132    LARGE_INTEGER SectionOffset;
133    ULONG ViewSize;
134    ULONG Protect;
135    LPVOID BaseAddress;
136
137    SectionOffset.u.LowPart = dwFileOffsetLow;
138    SectionOffset.u.HighPart = dwFileOffsetHigh;
139
140    if ( ( dwDesiredAccess & FILE_MAP_WRITE) == FILE_MAP_WRITE)
141         Protect  = PAGE_READWRITE;
142    else if ((dwDesiredAccess & FILE_MAP_READ) == FILE_MAP_READ)
143         Protect = PAGE_READONLY;
144    else if ((dwDesiredAccess & FILE_MAP_ALL_ACCESS) == FILE_MAP_ALL_ACCESS)
145         Protect  = PAGE_READWRITE;
146    else if ((dwDesiredAccess & FILE_MAP_COPY) == FILE_MAP_COPY)
147         Protect = PAGE_WRITECOPY;
148    else
149         Protect = PAGE_READWRITE;
150    
151    if (lpBaseAddress == NULL)
152      {
153         BaseAddress = NULL;
154      }
155    else
156      {
157         BaseAddress = lpBaseAddress;
158      }
159
160    ViewSize = (ULONG) dwNumberOfBytesToMap;
161
162    Status = ZwMapViewOfSection(hFileMappingObject,
163                                NtCurrentProcess(),
164                                &BaseAddress,
165                                0,
166                                dwNumberOfBytesToMap,
167                                &SectionOffset,
168                                &ViewSize,
169                                ViewShare,
170                                0,
171                                Protect);
172    if (!NT_SUCCESS(Status))
173      {
174         SetLastErrorByStatus(Status);
175         return NULL;
176      }
177    return BaseAddress;
178 }
179
180
181 LPVOID STDCALL
182 MapViewOfFile(HANDLE hFileMappingObject,
183               DWORD dwDesiredAccess,
184               DWORD dwFileOffsetHigh,
185               DWORD dwFileOffsetLow,
186               DWORD dwNumberOfBytesToMap)
187 {
188    return MapViewOfFileEx(hFileMappingObject,
189                           dwDesiredAccess,
190                           dwFileOffsetHigh,
191                           dwFileOffsetLow,
192                           dwNumberOfBytesToMap,
193                           NULL);
194 }
195
196
197 WINBOOL STDCALL
198 UnmapViewOfFile(LPVOID lpBaseAddress)
199 {
200    NTSTATUS Status;
201
202    Status = NtUnmapViewOfSection(NtCurrentProcess(),
203                                  lpBaseAddress);
204    if (!NT_SUCCESS(Status))
205      {
206         SetLastErrorByStatus(Status);
207         return FALSE;
208      }
209    return TRUE;
210 }
211
212
213 HANDLE STDCALL
214 OpenFileMappingA(DWORD dwDesiredAccess,
215                  WINBOOL bInheritHandle,
216                  LPCSTR lpName)
217 {
218    NTSTATUS Status;
219    HANDLE SectionHandle;
220    OBJECT_ATTRIBUTES ObjectAttributes;
221    ANSI_STRING AnsiName;
222    UNICODE_STRING UnicodeName;
223
224    ULONG Attributes = 0;
225
226    if (bInheritHandle)
227      {
228         Attributes = OBJ_INHERIT;
229      }
230
231    RtlInitAnsiString(&AnsiName,
232                      (LPSTR)lpName);
233    RtlAnsiStringToUnicodeString(&UnicodeName,
234                                 &AnsiName,
235                                 TRUE);
236
237    InitializeObjectAttributes(&ObjectAttributes,
238                               &UnicodeName,
239                               Attributes,
240                               hBaseDir,
241                               NULL);
242    Status = NtOpenSection(&SectionHandle,
243                             SECTION_ALL_ACCESS,
244                             &ObjectAttributes);
245    RtlFreeUnicodeString (&UnicodeName);
246    if (!NT_SUCCESS(Status))
247      {
248         SetLastErrorByStatus (Status);
249         return NULL;
250      }
251    return SectionHandle;
252 }
253
254
255 HANDLE STDCALL
256 OpenFileMappingW(DWORD dwDesiredAccess,
257                  WINBOOL bInheritHandle,
258                  LPCWSTR lpName)
259 {
260    NTSTATUS Status;
261    HANDLE SectionHandle;
262    OBJECT_ATTRIBUTES ObjectAttributes;
263    UNICODE_STRING UnicodeName;
264    ULONG Attributes = 0;
265
266    if (bInheritHandle)
267      {
268         Attributes = OBJ_INHERIT;
269      }
270
271    RtlInitUnicodeString(&UnicodeName,
272                         lpName);
273    InitializeObjectAttributes(&ObjectAttributes,
274                               &UnicodeName,
275                               Attributes,
276                               hBaseDir,
277                               NULL);
278    Status = ZwOpenSection(&SectionHandle,
279                             SECTION_ALL_ACCESS,
280                             &ObjectAttributes);
281    if (!NT_SUCCESS(Status))
282      {
283         SetLastErrorByStatus(Status);
284         return NULL;
285      }
286    return SectionHandle;
287 }
288
289
290 WINBOOL STDCALL
291 FlushViewOfFile(LPCVOID lpBaseAddress,
292                 DWORD dwNumberOfBytesToFlush)
293 {
294    NTSTATUS Status;
295    ULONG NumberOfBytesFlushed;
296
297    Status = NtFlushVirtualMemory(NtCurrentProcess(),
298                                  (LPVOID)lpBaseAddress,
299                                  dwNumberOfBytesToFlush,
300                                  &NumberOfBytesFlushed);
301    if (!NT_SUCCESS(Status))
302      {
303         SetLastErrorByStatus(Status);
304         return FALSE;
305      }
306    return TRUE;
307 }
308
309 /* EOF */