2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/kernel32/misc/utils.c
5 * PURPOSE: Utility and Support Functions
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 * Pierre Schweitzer (pierre.schweitzer@reactos.org)
10 /* INCLUDES ****************************************************************/
14 #include "i386/ketypes.h"
15 #elif defined _M_AMD64
16 #include "amd64/ketypes.h"
22 /* GLOBALS ******************************************************************/
24 PRTL_CONVERT_STRING Basep8BitStringToUnicodeString
;
26 /* FUNCTIONS ****************************************************************/
30 * Converts an ANSI or OEM String to the TEB StaticUnicodeString
34 Basep8BitStringToStaticUnicodeString(IN LPCSTR String
)
36 PUNICODE_STRING StaticString
= &(NtCurrentTeb()->StaticUnicodeString
);
37 ANSI_STRING AnsiString
;
40 /* Initialize an ANSI String */
41 if (!NT_SUCCESS(RtlInitAnsiStringEx(&AnsiString
, String
)))
43 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
48 Status
= Basep8BitStringToUnicodeString(StaticString
, &AnsiString
, FALSE
);
49 if (!NT_SUCCESS(Status
))
51 BaseSetLastNTError(Status
);
59 * Allocates space from the Heap and converts an Unicode String into it
63 Basep8BitStringToDynamicUnicodeString(OUT PUNICODE_STRING UnicodeString
,
66 ANSI_STRING AnsiString
;
69 DPRINT("Basep8BitStringToDynamicUnicodeString\n");
71 /* Initialize an ANSI String */
72 if (!NT_SUCCESS(RtlInitAnsiStringEx(&AnsiString
, String
)))
74 SetLastError(ERROR_BUFFER_OVERFLOW
);
79 Status
= Basep8BitStringToUnicodeString(UnicodeString
, &AnsiString
, TRUE
);
82 if (!NT_SUCCESS(Status
))
84 SetLastErrorByStatus(Status
);
93 * Allocates space from the Heap and converts an Ansi String into it
97 BasepAnsiStringToHeapUnicodeString(IN LPCSTR AnsiString
,
98 OUT LPWSTR
* UnicodeString
)
100 ANSI_STRING AnsiTemp
;
101 UNICODE_STRING UnicodeTemp
;
103 DPRINT("BasepAnsiStringToHeapUnicodeString\n");
105 /* First create the ANSI_STRING */
106 RtlInitAnsiString(&AnsiTemp
, AnsiString
);
108 if (NT_SUCCESS(RtlAnsiStringToUnicodeString(&UnicodeTemp
,
112 *UnicodeString
= UnicodeTemp
.Buffer
;
116 *UnicodeString
= NULL
;
121 * Converts lpSecurityAttributes + Object Name into ObjectAttributes.
125 BasepConvertObjectAttributes(OUT POBJECT_ATTRIBUTES ObjectAttributes
,
126 IN PSECURITY_ATTRIBUTES SecurityAttributes OPTIONAL
,
127 IN PUNICODE_STRING ObjectName
)
129 ULONG Attributes
= 0;
130 HANDLE RootDirectory
= 0;
131 PVOID SecurityDescriptor
= NULL
;
132 BOOLEAN NeedOba
= FALSE
;
134 DPRINT("BasepConvertObjectAttributes. Security: %p, Name: %p\n",
135 SecurityAttributes
, ObjectName
);
137 /* Get the attributes if present */
138 if (SecurityAttributes
)
140 Attributes
= SecurityAttributes
->bInheritHandle
? OBJ_INHERIT
: 0;
141 SecurityDescriptor
= SecurityAttributes
->lpSecurityDescriptor
;
147 Attributes
|= OBJ_OPENIF
;
148 RootDirectory
= hBaseDir
;
152 DPRINT("Attributes: %lx, RootDirectory: %lx, SecurityDescriptor: %p\n",
153 Attributes
, RootDirectory
, SecurityDescriptor
);
155 /* Create the Object Attributes */
158 InitializeObjectAttributes(ObjectAttributes
,
163 return ObjectAttributes
;
166 /* Nothing to return */
171 * Creates a stack for a thread or fiber
175 BasepCreateStack(HANDLE hProcess
,
178 PINITIAL_TEB InitialTeb
)
181 SYSTEM_BASIC_INFORMATION SystemBasicInfo
;
182 PIMAGE_NT_HEADERS Headers
;
184 BOOLEAN UseGuard
= FALSE
;
186 DPRINT("BasepCreateStack (hProcess: %lx, Max: %lx, Current: %lx)\n",
187 hProcess
, StackReserve
, StackCommit
);
189 /* Get some memory information */
190 Status
= NtQuerySystemInformation(SystemBasicInformation
,
192 sizeof(SYSTEM_BASIC_INFORMATION
),
194 if (!NT_SUCCESS(Status
))
196 DPRINT1("Failure to query system info\n");
200 /* Use the Image Settings if we are dealing with the current Process */
201 if (hProcess
== NtCurrentProcess())
203 /* Get the Image Headers */
204 Headers
= RtlImageNtHeader(NtCurrentPeb()->ImageBaseAddress
);
206 /* If we didn't get the parameters, find them ourselves */
207 StackReserve
= (StackReserve
) ?
208 StackReserve
: Headers
->OptionalHeader
.SizeOfStackReserve
;
209 StackCommit
= (StackCommit
) ?
210 StackCommit
: Headers
->OptionalHeader
.SizeOfStackCommit
;
214 /* Use the System Settings if needed */
215 StackReserve
= (StackReserve
) ? StackReserve
:
216 SystemBasicInfo
.AllocationGranularity
;
217 StackCommit
= (StackCommit
) ? StackCommit
: SystemBasicInfo
.PageSize
;
220 /* Align everything to Page Size */
221 StackReserve
= ROUND_UP(StackReserve
, SystemBasicInfo
.AllocationGranularity
);
222 StackCommit
= ROUND_UP(StackCommit
, SystemBasicInfo
.PageSize
);
223 #if 1 // FIXME: Remove once Guard Page support is here
224 StackCommit
= StackReserve
;
226 DPRINT("StackReserve: %lx, StackCommit: %lx\n", StackReserve
, StackCommit
);
228 /* Reserve memory for the stack */
229 Status
= ZwAllocateVirtualMemory(hProcess
,
235 if (!NT_SUCCESS(Status
))
237 DPRINT1("Failure to reserve stack\n");
241 /* Now set up some basic Initial TEB Parameters */
242 InitialTeb
->AllocatedStackBase
= (PVOID
)Stack
;
243 InitialTeb
->StackBase
= (PVOID
)(Stack
+ StackReserve
);
244 InitialTeb
->PreviousStackBase
= NULL
;
245 InitialTeb
->PreviousStackLimit
= NULL
;
247 /* Update the Stack Position */
248 Stack
+= StackReserve
- StackCommit
;
250 /* Check if we will need a guard page */
251 if (StackReserve
> StackCommit
)
253 Stack
-= SystemBasicInfo
.PageSize
;
254 StackCommit
+= SystemBasicInfo
.PageSize
;
258 /* Allocate memory for the stack */
259 Status
= ZwAllocateVirtualMemory(hProcess
,
265 if (!NT_SUCCESS(Status
))
267 DPRINT1("Failure to allocate stack\n");
271 /* Now set the current Stack Limit */
272 InitialTeb
->StackLimit
= (PVOID
)Stack
;
274 /* Create a guard page */
277 SIZE_T GuardPageSize
= SystemBasicInfo
.PageSize
;
280 /* Attempt maximum space possible */
281 Status
= ZwProtectVirtualMemory(hProcess
,
284 PAGE_GUARD
| PAGE_READWRITE
,
286 if (!NT_SUCCESS(Status
))
288 DPRINT1("Failure to create guard page\n");
292 /* Update the Stack Limit keeping in mind the Guard Page */
293 InitialTeb
->StackLimit
= (PVOID
)((ULONG_PTR
)InitialTeb
->StackLimit
- GuardPageSize
);
297 return STATUS_SUCCESS
;
302 BasepFreeStack(HANDLE hProcess
,
303 PINITIAL_TEB InitialTeb
)
308 NtFreeVirtualMemory(hProcess
,
309 &InitialTeb
->AllocatedStackBase
,
315 * Creates the Initial Context for a Thread or Fiber
319 BasepInitializeContext(IN PCONTEXT Context
,
321 IN PVOID StartAddress
,
322 IN PVOID StackAddress
,
323 IN ULONG ContextType
)
326 DPRINT("BasepInitializeContext: %p\n", Context
);
328 /* Setup the Initial Win32 Thread Context */
329 Context
->Eax
= (ULONG
)StartAddress
;
330 Context
->Ebx
= (ULONG
)Parameter
;
331 Context
->Esp
= (ULONG
)StackAddress
;
332 /* The other registers are undefined */
334 /* Setup the Segments */
335 Context
->SegFs
= KGDT_R3_TEB
| RPL_MASK
;
336 Context
->SegEs
= KGDT_R3_DATA
| RPL_MASK
;
337 Context
->SegDs
= KGDT_R3_DATA
| RPL_MASK
;
338 Context
->SegCs
= KGDT_R3_CODE
| RPL_MASK
;
339 Context
->SegSs
= KGDT_R3_DATA
| RPL_MASK
;
343 Context
->EFlags
= 0x3000; /* IOPL 3 */
345 if (ContextType
== 1) /* For Threads */
347 Context
->Eip
= (ULONG
)BaseThreadStartupThunk
;
349 else if (ContextType
== 2) /* For Fibers */
351 Context
->Eip
= (ULONG
)BaseFiberStartup
;
353 else /* For first thread in a Process */
355 Context
->Eip
= (ULONG
)BaseProcessStartThunk
;
358 /* Set the Context Flags */
359 Context
->ContextFlags
= CONTEXT_FULL
;
361 /* Give it some room for the Parameter */
362 Context
->Esp
-= sizeof(PVOID
);
363 #elif defined(_M_AMD64)
364 DPRINT("BasepInitializeContext: %p\n", Context
);
366 /* Setup the Initial Win32 Thread Context */
367 Context
->Rax
= (ULONG_PTR
)StartAddress
;
368 Context
->Rbx
= (ULONG_PTR
)Parameter
;
369 Context
->Rsp
= (ULONG_PTR
)StackAddress
;
370 /* The other registers are undefined */
372 /* Setup the Segments */
373 Context
->SegGs
= KGDT64_R3_DATA
| RPL_MASK
;
374 Context
->SegEs
= KGDT64_R3_DATA
| RPL_MASK
;
375 Context
->SegDs
= KGDT64_R3_DATA
| RPL_MASK
;
376 Context
->SegCs
= KGDT64_R3_CODE
| RPL_MASK
;
377 Context
->SegSs
= KGDT64_R3_DATA
| RPL_MASK
;
378 Context
->SegFs
= KGDT64_R3_CMTEB
| RPL_MASK
;
381 Context
->EFlags
= 0x3000; /* IOPL 3 */
383 if (ContextType
== 1) /* For Threads */
385 Context
->Rip
= (ULONG_PTR
)BaseThreadStartupThunk
;
387 else if (ContextType
== 2) /* For Fibers */
389 Context
->Rip
= (ULONG_PTR
)BaseFiberStartup
;
391 else /* For first thread in a Process */
393 Context
->Rip
= (ULONG_PTR
)BaseProcessStartThunk
;
396 /* Set the Context Flags */
397 Context
->ContextFlags
= CONTEXT_FULL
;
399 /* Give it some room for the Parameter */
400 Context
->Rsp
-= sizeof(PVOID
);
402 #warning Unknown architecture
409 * Checks if the privilege for Real-Time Priority is there
413 BasepCheckRealTimePrivilege(VOID
)
419 * Maps an image file into a section
423 BasepMapFile(IN LPCWSTR lpApplicationName
,
424 OUT PHANDLE hSection
,
425 IN PUNICODE_STRING ApplicationName
)
428 OBJECT_ATTRIBUTES ObjectAttributes
;
431 IO_STATUS_BLOCK IoStatusBlock
;
433 DPRINT("BasepMapFile\n");
435 /* Zero out the Relative Directory */
436 RelativeName
.Handle
= NULL
;
438 /* Find the application name */
439 if (!RtlDosPathNameToNtPathName_U(lpApplicationName
,
444 return STATUS_OBJECT_PATH_NOT_FOUND
;
447 DPRINT("ApplicationName %wZ\n", ApplicationName
);
448 DPRINT("RelativeName %wZ\n", &RelativeName
.DosPath
);
450 /* Did we get a relative name? */
451 if (RelativeName
.DosPath
.Length
)
453 ApplicationName
= &RelativeName
.DosPath
;
456 /* Initialize the Object Attributes */
457 InitializeObjectAttributes(&ObjectAttributes
,
459 OBJ_CASE_INSENSITIVE
,
463 /* Try to open the executable */
464 Status
= NtOpenFile(&hFile
,
465 SYNCHRONIZE
| FILE_EXECUTE
| FILE_READ_DATA
,
468 FILE_SHARE_DELETE
| FILE_SHARE_READ
,
469 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
470 if (!NT_SUCCESS(Status
))
472 DPRINT1("Failed to open file\n");
473 SetLastErrorByStatus(Status
);
477 /* Create a section for this file */
478 Status
= NtCreateSection(hSection
,
488 DPRINT("Section: %lx for file: %lx\n", *hSection
, hFile
);