2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NT User-Mode DLL
4 * FILE: lib/ntdll/rtl/libsup.c
5 * PURPOSE: RTL Support Routines
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
10 /* INCLUDES *****************************************************************/
16 SIZE_T RtlpAllocDeallocQueryBufferSize
= PAGE_SIZE
;
17 PTEB LdrpTopLevelDllBeingLoadedTeb
= NULL
;
19 /* FUNCTIONS ***************************************************************/
26 RtlWalkFrameChain(OUT PVOID
*Callers
,
30 /* Not implemented for user-mode */
36 RtlpCheckForActiveDebugger(VOID
)
38 /* Return the flag in the PEB */
39 return NtCurrentPeb()->BeingDebugged
;
44 RtlpSetInDbgPrint(IN BOOLEAN NewValue
)
46 /* If we're setting it to false, do it and return */
47 if (NewValue
== FALSE
)
49 NtCurrentTeb()->InDbgPrint
= FALSE
;
53 /* Setting to true; check if it's not already */
54 if (NtCurrentTeb()->InDbgPrint
) return TRUE
;
56 /* Set it and return */
57 NtCurrentTeb()->InDbgPrint
= TRUE
;
73 RtlGetCurrentPeb(VOID
)
75 return NtCurrentPeb();
82 RtlAcquirePebLock(VOID
)
84 PPEB Peb
= NtCurrentPeb ();
85 Peb
->FastPebLockRoutine (Peb
->FastPebLock
);
92 RtlReleasePebLock(VOID
)
94 PPEB Peb
= NtCurrentPeb ();
95 Peb
->FastPebUnlockRoutine (Peb
->FastPebLock
);
103 RtlGetNtGlobalFlags(VOID
)
105 PPEB pPeb
= NtCurrentPeb();
106 return pPeb
->NtGlobalFlag
;
112 PRTL_CRITICAL_SECTION CriticalSection
)
114 return RtlDeleteCriticalSection(CriticalSection
);
120 PRTL_CRITICAL_SECTION CriticalSection
)
122 return RtlEnterCriticalSection(CriticalSection
);
127 RtlInitializeHeapLock(
128 PRTL_CRITICAL_SECTION CriticalSection
)
130 return RtlInitializeCriticalSection(CriticalSection
);
136 PRTL_CRITICAL_SECTION CriticalSection
)
138 return RtlLeaveCriticalSection(CriticalSection
);
143 RtlpAllocateMemory(UINT Bytes
,
146 UNREFERENCED_PARAMETER(Tag
);
148 return RtlAllocateHeap(RtlGetProcessHeap(),
156 RtlpFreeMemory(PVOID Mem
,
159 UNREFERENCED_PARAMETER(Tag
);
161 RtlFreeHeap(RtlGetProcessHeap(),
169 CHECK_PAGED_CODE_RTL(char *file
, int line
)
171 /* meaningless in user mode */
177 RtlpHandleDpcStackException(IN PEXCEPTION_REGISTRATION_RECORD RegistrationFrame
,
178 IN ULONG_PTR RegistrationFrameEnd
,
179 IN OUT PULONG_PTR StackLow
,
180 IN OUT PULONG_PTR StackHigh
)
182 /* There's no such thing as a DPC stack in user-mode */
188 RtlpCheckLogException(IN PEXCEPTION_RECORD ExceptionRecord
,
189 IN PCONTEXT ContextRecord
,
190 IN PVOID ContextData
,
193 /* Exception logging is not done in user-mode */
198 RtlpCaptureStackLimits(IN ULONG_PTR Ebp
,
199 IN ULONG_PTR
*StackBegin
,
200 IN ULONG_PTR
*StackEnd
)
203 *StackBegin
= (ULONG_PTR
)NtCurrentTeb()->Tib
.StackLimit
;
204 *StackEnd
= (ULONG_PTR
)NtCurrentTeb()->Tib
.StackBase
;
210 RtlIsThreadWithinLoaderCallout(VOID
)
212 return LdrpTopLevelDllBeingLoadedTeb
== NtCurrentTeb();
215 /* RTL Atom Tables ************************************************************/
217 typedef struct _RTL_ATOM_HANDLE
219 RTL_HANDLE_TABLE_ENTRY Handle
;
220 PRTL_ATOM_TABLE_ENTRY AtomEntry
;
221 } RTL_ATOM_HANDLE
, *PRTL_ATOM_HANDLE
;
224 RtlpInitAtomTableLock(PRTL_ATOM_TABLE AtomTable
)
226 RtlInitializeCriticalSection(&AtomTable
->CriticalSection
);
227 return STATUS_SUCCESS
;
232 RtlpDestroyAtomTableLock(PRTL_ATOM_TABLE AtomTable
)
234 RtlDeleteCriticalSection(&AtomTable
->CriticalSection
);
239 RtlpLockAtomTable(PRTL_ATOM_TABLE AtomTable
)
241 RtlEnterCriticalSection(&AtomTable
->CriticalSection
);
247 RtlpUnlockAtomTable(PRTL_ATOM_TABLE AtomTable
)
249 RtlLeaveCriticalSection(&AtomTable
->CriticalSection
);
253 /* handle functions */
256 RtlpCreateAtomHandleTable(PRTL_ATOM_TABLE AtomTable
)
258 RtlInitializeHandleTable(0xCFFF,
259 sizeof(RTL_ATOM_HANDLE
),
260 &AtomTable
->RtlHandleTable
);
266 RtlpDestroyAtomHandleTable(PRTL_ATOM_TABLE AtomTable
)
268 RtlDestroyHandleTable(&AtomTable
->RtlHandleTable
);
272 RtlpAllocAtomTable(ULONG Size
)
274 return (PRTL_ATOM_TABLE
)RtlAllocateHeap(RtlGetProcessHeap(),
280 RtlpFreeAtomTable(PRTL_ATOM_TABLE AtomTable
)
282 RtlFreeHeap(RtlGetProcessHeap(),
287 PRTL_ATOM_TABLE_ENTRY
288 RtlpAllocAtomTableEntry(ULONG Size
)
290 return (PRTL_ATOM_TABLE_ENTRY
)RtlAllocateHeap(RtlGetProcessHeap(),
296 RtlpFreeAtomTableEntry(PRTL_ATOM_TABLE_ENTRY Entry
)
298 RtlFreeHeap(RtlGetProcessHeap(),
304 RtlpFreeAtomHandle(PRTL_ATOM_TABLE AtomTable
, PRTL_ATOM_TABLE_ENTRY Entry
)
306 PRTL_HANDLE_TABLE_ENTRY RtlHandleEntry
;
308 if (RtlIsValidIndexHandle(&AtomTable
->RtlHandleTable
,
309 (ULONG
)Entry
->HandleIndex
,
312 RtlFreeHandle(&AtomTable
->RtlHandleTable
,
318 RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable
, PRTL_ATOM_TABLE_ENTRY Entry
)
321 PRTL_HANDLE_TABLE_ENTRY RtlHandle
;
323 RtlHandle
= RtlAllocateHandle(&AtomTable
->RtlHandleTable
,
325 if (RtlHandle
!= NULL
)
327 PRTL_ATOM_HANDLE AtomHandle
= (PRTL_ATOM_HANDLE
)RtlHandle
;
329 /* FIXME - Handle Indexes >= 0xC000 ?! */
330 if (HandleIndex
< 0xC000)
332 Entry
->HandleIndex
= (USHORT
)HandleIndex
;
333 Entry
->Atom
= 0xC000 + (USHORT
)HandleIndex
;
335 AtomHandle
->AtomEntry
= Entry
;
336 AtomHandle
->Handle
.Flags
= RTL_HANDLE_VALID
;
342 /* set the valid flag, otherwise RtlFreeHandle will fail! */
343 AtomHandle
->Handle
.Flags
= RTL_HANDLE_VALID
;
345 RtlFreeHandle(&AtomTable
->RtlHandleTable
,
353 PRTL_ATOM_TABLE_ENTRY
354 RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable
, ULONG Index
)
356 PRTL_HANDLE_TABLE_ENTRY RtlHandle
;
358 if (RtlIsValidIndexHandle(&AtomTable
->RtlHandleTable
,
362 PRTL_ATOM_HANDLE AtomHandle
= (PRTL_ATOM_HANDLE
)RtlHandle
;
364 return AtomHandle
->AtomEntry
;
372 * Ldr Resource support code
375 IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( IMAGE_RESOURCE_DIRECTORY
*dir
,
376 LPCWSTR name
, void *root
,
378 IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( IMAGE_RESOURCE_DIRECTORY
*dir
,
379 WORD id
, void *root
, int want_dir
);
380 IMAGE_RESOURCE_DIRECTORY
*find_first_entry( IMAGE_RESOURCE_DIRECTORY
*dir
,
381 void *root
, int want_dir
);
382 int push_language( USHORT
*list
, ULONG pos
, WORD lang
);
384 /**********************************************************************
387 * Find a resource entry
389 NTSTATUS
find_entry( PVOID BaseAddress
, LDR_RESOURCE_INFO
*info
,
390 ULONG level
, void **ret
, int want_dir
)
394 IMAGE_RESOURCE_DIRECTORY
*resdirptr
;
395 USHORT list
[9]; /* list of languages to try */
397 LCID user_lcid
, system_lcid
;
399 root
= RtlImageDirectoryEntryToData( BaseAddress
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &size
);
400 if (!root
) return STATUS_RESOURCE_DATA_NOT_FOUND
;
403 if (!level
--) goto done
;
404 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Type
, root
, want_dir
|| level
)))
405 return STATUS_RESOURCE_TYPE_NOT_FOUND
;
406 if (!level
--) return STATUS_SUCCESS
;
409 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Name
, root
, want_dir
|| level
)))
410 return STATUS_RESOURCE_NAME_NOT_FOUND
;
411 if (!level
--) return STATUS_SUCCESS
;
412 if (level
) return STATUS_INVALID_PARAMETER
; /* level > 3 */
414 /* 1. specified language */
415 pos
= push_language( list
, pos
, info
->Language
);
417 /* 2. specified language with neutral sublanguage */
418 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(info
->Language
), SUBLANG_NEUTRAL
) );
420 /* 3. neutral language with neutral sublanguage */
421 pos
= push_language( list
, pos
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
423 /* if no explicitly specified language, try some defaults */
424 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
426 /* user defaults, unless SYS_DEFAULT sublanguage specified */
427 if (SUBLANGID(info
->Language
) != SUBLANG_SYS_DEFAULT
)
429 /* 4. current thread locale language */
430 pos
= push_language( list
, pos
, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale
) );
432 if (NT_SUCCESS(NtQueryDefaultLocale(TRUE
, &user_lcid
)))
434 /* 5. user locale language */
435 pos
= push_language( list
, pos
, LANGIDFROMLCID(user_lcid
) );
437 /* 6. user locale language with neutral sublanguage */
438 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(user_lcid
), SUBLANG_NEUTRAL
) );
442 /* now system defaults */
444 if (NT_SUCCESS(NtQueryDefaultLocale(FALSE
, &system_lcid
)))
446 /* 7. system locale language */
447 pos
= push_language( list
, pos
, LANGIDFROMLCID( system_lcid
) );
449 /* 8. system locale language with neutral sublanguage */
450 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(system_lcid
), SUBLANG_NEUTRAL
) );
454 pos
= push_language( list
, pos
, MAKELANGID( LANG_ENGLISH
, SUBLANG_DEFAULT
) );
458 for (i
= 0; i
< pos
; i
++)
459 if ((*ret
= find_entry_by_id( resdirptr
, list
[i
], root
, want_dir
))) return STATUS_SUCCESS
;
461 /* if no explicitly specified language, return the first entry */
462 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
464 if ((*ret
= find_first_entry( resdirptr
, root
, want_dir
))) return STATUS_SUCCESS
;
466 return STATUS_RESOURCE_LANG_NOT_FOUND
;
470 return STATUS_SUCCESS
;