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)
9 /* INCLUDES ****************************************************************/
13 #include "i386/ketypes.h"
14 #elif defined _M_AMD64
15 #include "amd64/ketypes.h"
21 /* GLOBALS ******************************************************************/
23 PRTL_CONVERT_STRING Basep8BitStringToUnicodeString
;
25 /* FUNCTIONS ****************************************************************/
28 * Converts an ANSI or OEM String to the specified Unicode String
32 Basep8BitStringToLiveUnicodeString(OUT PUNICODE_STRING UnicodeString
,
35 ANSI_STRING AnsiString
;
38 DPRINT("Basep8BitStringToLiveUnicodeString\n");
40 /* Create the ANSI String */
41 RtlInitAnsiString(&AnsiString
, String
);
43 /* Convert from OEM or ANSI */
44 Status
= Basep8BitStringToUnicodeString(UnicodeString
, &AnsiString
, FALSE
);
47 if (!NT_SUCCESS(Status
))
49 SetLastErrorByStatus(Status
);
56 * Converts an ANSI or OEM String to the TEB StaticUnicodeString
60 Basep8BitStringToCachedUnicodeString(IN LPCSTR String
)
62 PUNICODE_STRING StaticString
= &NtCurrentTeb()->StaticUnicodeString
;
63 ANSI_STRING AnsiString
;
66 DPRINT("Basep8BitStringToCachedUnicodeString\n");
68 /* Initialize an ANSI String */
69 RtlInitAnsiString(&AnsiString
, String
);
72 Status
= Basep8BitStringToUnicodeString(StaticString
, &AnsiString
, FALSE
);
75 if (!NT_SUCCESS(Status
))
77 SetLastErrorByStatus(Status
);
81 /* Return pointer to the string */
87 Basep8BitStringToHeapUnicodeString(OUT PUNICODE_STRING UnicodeString
,
90 ANSI_STRING AnsiString
;
93 DPRINT("Basep8BitStringToCachedUnicodeString\n");
95 /* Initialize an ANSI String */
96 RtlInitAnsiString(&AnsiString
, String
);
99 Status
= Basep8BitStringToUnicodeString(UnicodeString
, &AnsiString
, TRUE
);
102 if (!NT_SUCCESS(Status
))
104 SetLastErrorByStatus(Status
);
111 * Allocates space from the Heap and converts an Ansi String into it
115 BasepAnsiStringToHeapUnicodeString(IN LPCSTR AnsiString
,
116 OUT LPWSTR
* UnicodeString
)
118 ANSI_STRING AnsiTemp
;
119 UNICODE_STRING UnicodeTemp
;
121 DPRINT("BasepAnsiStringToHeapUnicodeString\n");
123 /* First create the ANSI_STRING */
124 RtlInitAnsiString(&AnsiTemp
, AnsiString
);
126 if (NT_SUCCESS(RtlAnsiStringToUnicodeString(&UnicodeTemp
,
130 *UnicodeString
= UnicodeTemp
.Buffer
;
134 *UnicodeString
= NULL
;
139 * Converts lpSecurityAttributes + Object Name into ObjectAttributes.
143 BasepConvertObjectAttributes(OUT POBJECT_ATTRIBUTES ObjectAttributes
,
144 IN PSECURITY_ATTRIBUTES SecurityAttributes OPTIONAL
,
145 IN PUNICODE_STRING ObjectName
)
147 ULONG Attributes
= 0;
148 HANDLE RootDirectory
= 0;
149 PVOID SecurityDescriptor
= NULL
;
150 BOOLEAN NeedOba
= FALSE
;
152 DPRINT("BasepConvertObjectAttributes. Security: %p, Name: %p\n",
153 SecurityAttributes
, ObjectName
);
155 /* Get the attributes if present */
156 if (SecurityAttributes
)
158 Attributes
= SecurityAttributes
->bInheritHandle
? OBJ_INHERIT
: 0;
159 SecurityDescriptor
= SecurityAttributes
->lpSecurityDescriptor
;
165 Attributes
|= OBJ_OPENIF
;
166 RootDirectory
= hBaseDir
;
170 DPRINT("Attributes: %lx, RootDirectory: %lx, SecurityDescriptor: %p\n",
171 Attributes
, RootDirectory
, SecurityDescriptor
);
173 /* Create the Object Attributes */
176 InitializeObjectAttributes(ObjectAttributes
,
181 return ObjectAttributes
;
184 /* Nothing to return */
189 * Creates a stack for a thread or fiber
193 BasepCreateStack(HANDLE hProcess
,
196 PINITIAL_TEB InitialTeb
)
199 SYSTEM_BASIC_INFORMATION SystemBasicInfo
;
200 PIMAGE_NT_HEADERS Headers
;
202 BOOLEAN UseGuard
= FALSE
;
204 DPRINT("BasepCreateStack (hProcess: %lx, Max: %lx, Current: %lx)\n",
205 hProcess
, StackReserve
, StackCommit
);
207 /* Get some memory information */
208 Status
= NtQuerySystemInformation(SystemBasicInformation
,
210 sizeof(SYSTEM_BASIC_INFORMATION
),
212 if (!NT_SUCCESS(Status
))
214 DPRINT1("Failure to query system info\n");
218 /* Use the Image Settings if we are dealing with the current Process */
219 if (hProcess
== NtCurrentProcess())
221 /* Get the Image Headers */
222 Headers
= RtlImageNtHeader(NtCurrentPeb()->ImageBaseAddress
);
224 /* If we didn't get the parameters, find them ourselves */
225 StackReserve
= (StackReserve
) ?
226 StackReserve
: Headers
->OptionalHeader
.SizeOfStackReserve
;
227 StackCommit
= (StackCommit
) ?
228 StackCommit
: Headers
->OptionalHeader
.SizeOfStackCommit
;
232 /* Use the System Settings if needed */
233 StackReserve
= (StackReserve
) ? StackReserve
:
234 SystemBasicInfo
.AllocationGranularity
;
235 StackCommit
= (StackCommit
) ? StackCommit
: SystemBasicInfo
.PageSize
;
238 /* Align everything to Page Size */
239 StackReserve
= ROUND_UP(StackReserve
, SystemBasicInfo
.AllocationGranularity
);
240 StackCommit
= ROUND_UP(StackCommit
, SystemBasicInfo
.PageSize
);
241 #if 1 // FIXME: Remove once Guard Page support is here
242 StackCommit
= StackReserve
;
244 DPRINT("StackReserve: %lx, StackCommit: %lx\n", StackReserve
, StackCommit
);
246 /* Reserve memory for the stack */
247 Status
= ZwAllocateVirtualMemory(hProcess
,
253 if (!NT_SUCCESS(Status
))
255 DPRINT1("Failure to reserve stack\n");
259 /* Now set up some basic Initial TEB Parameters */
260 InitialTeb
->AllocatedStackBase
= (PVOID
)Stack
;
261 InitialTeb
->StackBase
= (PVOID
)(Stack
+ StackReserve
);
263 /* Update the Stack Position */
264 Stack
+= StackReserve
- StackCommit
;
266 /* Check if we will need a guard page */
267 if (StackReserve
> StackCommit
)
269 Stack
-= SystemBasicInfo
.PageSize
;
270 StackCommit
+= SystemBasicInfo
.PageSize
;
274 /* Allocate memory for the stack */
275 Status
= ZwAllocateVirtualMemory(hProcess
,
281 if (!NT_SUCCESS(Status
))
283 DPRINT1("Failure to allocate stack\n");
287 /* Now set the current Stack Limit */
288 InitialTeb
->StackLimit
= (PVOID
)Stack
;
290 /* Create a guard page */
293 ULONG GuardPageSize
= SystemBasicInfo
.PageSize
;
296 /* Attempt maximum space possible */
297 Status
= ZwProtectVirtualMemory(hProcess
,
300 PAGE_GUARD
| PAGE_READWRITE
,
302 if (!NT_SUCCESS(Status
))
304 DPRINT1("Failure to create guard page\n");
308 /* Update the Stack Limit keeping in mind the Guard Page */
309 InitialTeb
->StackLimit
= (PVOID
)((ULONG_PTR
)InitialTeb
->StackLimit
- GuardPageSize
);
313 return STATUS_SUCCESS
;
318 BasepFreeStack(HANDLE hProcess
,
319 PINITIAL_TEB InitialTeb
)
324 NtFreeVirtualMemory(hProcess
,
325 &InitialTeb
->AllocatedStackBase
,
331 * Creates the Initial Context for a Thread or Fiber
335 BasepInitializeContext(IN PCONTEXT Context
,
337 IN PVOID StartAddress
,
338 IN PVOID StackAddress
,
339 IN ULONG ContextType
)
342 DPRINT("BasepInitializeContext: %p\n", Context
);
344 /* Setup the Initial Win32 Thread Context */
345 Context
->Eax
= (ULONG
)StartAddress
;
346 Context
->Ebx
= (ULONG
)Parameter
;
347 Context
->Esp
= (ULONG
)StackAddress
;
348 /* The other registers are undefined */
350 /* Setup the Segments */
351 Context
->SegFs
= KGDT_R3_TEB
| RPL_MASK
;
352 Context
->SegEs
= KGDT_R3_DATA
| RPL_MASK
;
353 Context
->SegDs
= KGDT_R3_DATA
| RPL_MASK
;
354 Context
->SegCs
= KGDT_R3_CODE
| RPL_MASK
;
355 Context
->SegSs
= KGDT_R3_DATA
| RPL_MASK
;
359 Context
->EFlags
= 0x3000; /* IOPL 3 */
361 if (ContextType
== 1) /* For Threads */
363 Context
->Eip
= (ULONG
)BaseThreadStartupThunk
;
365 else if (ContextType
== 2) /* For Fibers */
367 Context
->Eip
= (ULONG
)BaseFiberStartup
;
369 else /* For first thread in a Process */
371 Context
->Eip
= (ULONG
)BaseProcessStartThunk
;
374 /* Set the Context Flags */
375 Context
->ContextFlags
= CONTEXT_FULL
;
377 /* Give it some room for the Parameter */
378 Context
->Esp
-= sizeof(PVOID
);
379 #elif defined(__x86_64__)
380 DPRINT("BasepInitializeContext: %p\n", Context
);
382 /* Setup the Initial Win32 Thread Context */
383 Context
->Rax
= (ULONG_PTR
)StartAddress
;
384 Context
->Rbx
= (ULONG_PTR
)Parameter
;
385 Context
->Rsp
= (ULONG_PTR
)StackAddress
;
386 /* The other registers are undefined */
388 /* Setup the Segments */
389 Context
->SegGs
= KGDT_64_DATA
| RPL_MASK
;
390 Context
->SegEs
= KGDT_64_DATA
| RPL_MASK
;
391 Context
->SegDs
= KGDT_64_DATA
| RPL_MASK
;
392 Context
->SegCs
= KGDT_64_R3_CODE
| RPL_MASK
;
393 Context
->SegSs
= KGDT_64_DATA
| RPL_MASK
;
394 Context
->SegFs
= KGDT_32_R3_TEB
;
397 Context
->EFlags
= 0x3000; /* IOPL 3 */
399 if (ContextType
== 1) /* For Threads */
401 Context
->Rip
= (ULONG_PTR
)BaseThreadStartupThunk
;
403 else if (ContextType
== 2) /* For Fibers */
405 Context
->Rip
= (ULONG_PTR
)BaseFiberStartup
;
407 else /* For first thread in a Process */
409 Context
->Rip
= (ULONG_PTR
)BaseProcessStartThunk
;
412 /* Set the Context Flags */
413 Context
->ContextFlags
= CONTEXT_FULL
;
415 /* Give it some room for the Parameter */
416 Context
->Rsp
-= sizeof(PVOID
);
418 #warning Unknown architecture
425 * Checks if the privilege for Real-Time Priority is there
429 BasepCheckRealTimePrivilege(VOID
)
435 * Maps an image file into a section
439 BasepMapFile(IN LPCWSTR lpApplicationName
,
440 OUT PHANDLE hSection
,
441 IN PUNICODE_STRING ApplicationName
)
444 OBJECT_ATTRIBUTES ObjectAttributes
;
447 IO_STATUS_BLOCK IoStatusBlock
;
449 DPRINT("BasepMapFile\n");
451 /* Zero out the Relative Directory */
452 RelativeName
.Handle
= NULL
;
454 /* Find the application name */
455 RtlDosPathNameToNtPathName_U(lpApplicationName
,
459 DPRINT("ApplicationName %wZ\n", ApplicationName
);
460 DPRINT("RelativeName %wZ\n", &RelativeName
.DosPath
);
462 /* Did we get a relative name? */
463 if (RelativeName
.DosPath
.Length
)
465 ApplicationName
= &RelativeName
.DosPath
;
468 /* Initialize the Object Attributes */
469 InitializeObjectAttributes(&ObjectAttributes
,
471 OBJ_CASE_INSENSITIVE
,
475 /* Try to open the executable */
476 Status
= NtOpenFile(&hFile
,
477 SYNCHRONIZE
| FILE_EXECUTE
| FILE_READ_DATA
,
480 FILE_SHARE_DELETE
| FILE_SHARE_READ
,
481 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
482 if (!NT_SUCCESS(Status
))
484 DPRINT1("Failed to open file\n");
485 SetLastErrorByStatus (Status
);
489 /* Create a section for this file */
490 Status
= NtCreateSection(hSection
,
500 DPRINT("Section: %lx for file: %lx\n", *hSection
, hFile
);