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(VOID
)
46 /* Check if it's already set and return TRUE if so */
47 if (NtCurrentTeb()->InDbgPrint
) return TRUE
;
49 /* Set it and return */
50 NtCurrentTeb()->InDbgPrint
= TRUE
;
56 RtlpClearInDbgPrint(VOID
)
59 NtCurrentTeb()->InDbgPrint
= FALSE
;
74 RtlGetCurrentPeb(VOID
)
76 return NtCurrentPeb();
83 RtlAcquirePebLock(VOID
)
85 PPEB Peb
= NtCurrentPeb ();
86 Peb
->FastPebLockRoutine (Peb
->FastPebLock
);
93 RtlReleasePebLock(VOID
)
95 PPEB Peb
= NtCurrentPeb ();
96 Peb
->FastPebUnlockRoutine (Peb
->FastPebLock
);
104 RtlGetNtGlobalFlags(VOID
)
106 PPEB pPeb
= NtCurrentPeb();
107 return pPeb
->NtGlobalFlag
;
113 PRTL_CRITICAL_SECTION CriticalSection
)
115 return RtlDeleteCriticalSection(CriticalSection
);
121 PRTL_CRITICAL_SECTION CriticalSection
)
123 return RtlEnterCriticalSection(CriticalSection
);
128 RtlInitializeHeapLock(
129 PRTL_CRITICAL_SECTION CriticalSection
)
131 return RtlInitializeCriticalSection(CriticalSection
);
137 PRTL_CRITICAL_SECTION CriticalSection
)
139 return RtlLeaveCriticalSection(CriticalSection
);
144 RtlpAllocateMemory(UINT Bytes
,
147 UNREFERENCED_PARAMETER(Tag
);
149 return RtlAllocateHeap(RtlGetProcessHeap(),
157 RtlpFreeMemory(PVOID Mem
,
160 UNREFERENCED_PARAMETER(Tag
);
162 RtlFreeHeap(RtlGetProcessHeap(),
170 CHECK_PAGED_CODE_RTL(char *file
, int line
)
172 /* meaningless in user mode */
178 RtlpHandleDpcStackException(IN PEXCEPTION_REGISTRATION_RECORD RegistrationFrame
,
179 IN ULONG_PTR RegistrationFrameEnd
,
180 IN OUT PULONG_PTR StackLow
,
181 IN OUT PULONG_PTR StackHigh
)
183 /* There's no such thing as a DPC stack in user-mode */
189 RtlpCheckLogException(IN PEXCEPTION_RECORD ExceptionRecord
,
190 IN PCONTEXT ContextRecord
,
191 IN PVOID ContextData
,
194 /* Exception logging is not done in user-mode */
199 RtlpCaptureStackLimits(IN ULONG_PTR Ebp
,
200 IN ULONG_PTR
*StackBegin
,
201 IN ULONG_PTR
*StackEnd
)
204 *StackBegin
= (ULONG_PTR
)NtCurrentTeb()->Tib
.StackLimit
;
205 *StackEnd
= (ULONG_PTR
)NtCurrentTeb()->Tib
.StackBase
;
211 RtlIsThreadWithinLoaderCallout(VOID
)
213 return LdrpTopLevelDllBeingLoadedTeb
== NtCurrentTeb();
216 /* RTL Atom Tables ************************************************************/
218 typedef struct _RTL_ATOM_HANDLE
220 RTL_HANDLE_TABLE_ENTRY Handle
;
221 PRTL_ATOM_TABLE_ENTRY AtomEntry
;
222 } RTL_ATOM_HANDLE
, *PRTL_ATOM_HANDLE
;
225 RtlpInitAtomTableLock(PRTL_ATOM_TABLE AtomTable
)
227 RtlInitializeCriticalSection(&AtomTable
->CriticalSection
);
228 return STATUS_SUCCESS
;
233 RtlpDestroyAtomTableLock(PRTL_ATOM_TABLE AtomTable
)
235 RtlDeleteCriticalSection(&AtomTable
->CriticalSection
);
240 RtlpLockAtomTable(PRTL_ATOM_TABLE AtomTable
)
242 RtlEnterCriticalSection(&AtomTable
->CriticalSection
);
248 RtlpUnlockAtomTable(PRTL_ATOM_TABLE AtomTable
)
250 RtlLeaveCriticalSection(&AtomTable
->CriticalSection
);
254 /* handle functions */
257 RtlpCreateAtomHandleTable(PRTL_ATOM_TABLE AtomTable
)
259 RtlInitializeHandleTable(0xCFFF,
260 sizeof(RTL_ATOM_HANDLE
),
261 &AtomTable
->RtlHandleTable
);
267 RtlpDestroyAtomHandleTable(PRTL_ATOM_TABLE AtomTable
)
269 RtlDestroyHandleTable(&AtomTable
->RtlHandleTable
);
273 RtlpAllocAtomTable(ULONG Size
)
275 return (PRTL_ATOM_TABLE
)RtlAllocateHeap(RtlGetProcessHeap(),
281 RtlpFreeAtomTable(PRTL_ATOM_TABLE AtomTable
)
283 RtlFreeHeap(RtlGetProcessHeap(),
288 PRTL_ATOM_TABLE_ENTRY
289 RtlpAllocAtomTableEntry(ULONG Size
)
291 return (PRTL_ATOM_TABLE_ENTRY
)RtlAllocateHeap(RtlGetProcessHeap(),
297 RtlpFreeAtomTableEntry(PRTL_ATOM_TABLE_ENTRY Entry
)
299 RtlFreeHeap(RtlGetProcessHeap(),
305 RtlpFreeAtomHandle(PRTL_ATOM_TABLE AtomTable
, PRTL_ATOM_TABLE_ENTRY Entry
)
307 PRTL_HANDLE_TABLE_ENTRY RtlHandleEntry
;
309 if (RtlIsValidIndexHandle(&AtomTable
->RtlHandleTable
,
310 (ULONG
)Entry
->HandleIndex
,
313 RtlFreeHandle(&AtomTable
->RtlHandleTable
,
319 RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable
, PRTL_ATOM_TABLE_ENTRY Entry
)
322 PRTL_HANDLE_TABLE_ENTRY RtlHandle
;
324 RtlHandle
= RtlAllocateHandle(&AtomTable
->RtlHandleTable
,
326 if (RtlHandle
!= NULL
)
328 PRTL_ATOM_HANDLE AtomHandle
= (PRTL_ATOM_HANDLE
)RtlHandle
;
330 /* FIXME - Handle Indexes >= 0xC000 ?! */
331 if (HandleIndex
< 0xC000)
333 Entry
->HandleIndex
= (USHORT
)HandleIndex
;
334 Entry
->Atom
= 0xC000 + (USHORT
)HandleIndex
;
336 AtomHandle
->AtomEntry
= Entry
;
337 AtomHandle
->Handle
.Flags
= RTL_HANDLE_VALID
;
343 /* set the valid flag, otherwise RtlFreeHandle will fail! */
344 AtomHandle
->Handle
.Flags
= RTL_HANDLE_VALID
;
346 RtlFreeHandle(&AtomTable
->RtlHandleTable
,
354 PRTL_ATOM_TABLE_ENTRY
355 RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable
, ULONG Index
)
357 PRTL_HANDLE_TABLE_ENTRY RtlHandle
;
359 if (RtlIsValidIndexHandle(&AtomTable
->RtlHandleTable
,
363 PRTL_ATOM_HANDLE AtomHandle
= (PRTL_ATOM_HANDLE
)RtlHandle
;
365 return AtomHandle
->AtomEntry
;
373 * Ldr Resource support code
376 IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( IMAGE_RESOURCE_DIRECTORY
*dir
,
377 LPCWSTR name
, void *root
,
379 IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( IMAGE_RESOURCE_DIRECTORY
*dir
,
380 WORD id
, void *root
, int want_dir
);
381 IMAGE_RESOURCE_DIRECTORY
*find_first_entry( IMAGE_RESOURCE_DIRECTORY
*dir
,
382 void *root
, int want_dir
);
383 int push_language( USHORT
*list
, ULONG pos
, WORD lang
);
385 /**********************************************************************
388 * Find a resource entry
390 NTSTATUS
find_entry( PVOID BaseAddress
, LDR_RESOURCE_INFO
*info
,
391 ULONG level
, void **ret
, int want_dir
)
395 IMAGE_RESOURCE_DIRECTORY
*resdirptr
;
396 USHORT list
[9]; /* list of languages to try */
398 LCID user_lcid
, system_lcid
;
400 root
= RtlImageDirectoryEntryToData( BaseAddress
, TRUE
, IMAGE_DIRECTORY_ENTRY_RESOURCE
, &size
);
401 if (!root
) return STATUS_RESOURCE_DATA_NOT_FOUND
;
404 if (!level
--) goto done
;
405 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Type
, root
, want_dir
|| level
)))
406 return STATUS_RESOURCE_TYPE_NOT_FOUND
;
407 if (!level
--) return STATUS_SUCCESS
;
410 if (!(*ret
= find_entry_by_name( resdirptr
, (LPCWSTR
)info
->Name
, root
, want_dir
|| level
)))
411 return STATUS_RESOURCE_NAME_NOT_FOUND
;
412 if (!level
--) return STATUS_SUCCESS
;
413 if (level
) return STATUS_INVALID_PARAMETER
; /* level > 3 */
415 /* 1. specified language */
416 pos
= push_language( list
, pos
, info
->Language
);
418 /* 2. specified language with neutral sublanguage */
419 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(info
->Language
), SUBLANG_NEUTRAL
) );
421 /* 3. neutral language with neutral sublanguage */
422 pos
= push_language( list
, pos
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
424 /* if no explicitly specified language, try some defaults */
425 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
427 /* user defaults, unless SYS_DEFAULT sublanguage specified */
428 if (SUBLANGID(info
->Language
) != SUBLANG_SYS_DEFAULT
)
430 /* 4. current thread locale language */
431 pos
= push_language( list
, pos
, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale
) );
433 if (NT_SUCCESS(NtQueryDefaultLocale(TRUE
, &user_lcid
)))
435 /* 5. user locale language */
436 pos
= push_language( list
, pos
, LANGIDFROMLCID(user_lcid
) );
438 /* 6. user locale language with neutral sublanguage */
439 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(user_lcid
), SUBLANG_NEUTRAL
) );
443 /* now system defaults */
445 if (NT_SUCCESS(NtQueryDefaultLocale(FALSE
, &system_lcid
)))
447 /* 7. system locale language */
448 pos
= push_language( list
, pos
, LANGIDFROMLCID( system_lcid
) );
450 /* 8. system locale language with neutral sublanguage */
451 pos
= push_language( list
, pos
, MAKELANGID( PRIMARYLANGID(system_lcid
), SUBLANG_NEUTRAL
) );
455 pos
= push_language( list
, pos
, MAKELANGID( LANG_ENGLISH
, SUBLANG_DEFAULT
) );
459 for (i
= 0; i
< pos
; i
++)
460 if ((*ret
= find_entry_by_id( resdirptr
, list
[i
], root
, want_dir
))) return STATUS_SUCCESS
;
462 /* if no explicitly specified language, return the first entry */
463 if (PRIMARYLANGID(info
->Language
) == LANG_NEUTRAL
)
465 if ((*ret
= find_first_entry( resdirptr
, root
, want_dir
))) return STATUS_SUCCESS
;
467 return STATUS_RESOURCE_LANG_NOT_FOUND
;
471 return STATUS_SUCCESS
;