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