- Revert 44301
[reactos.git] / dll / ntdll / rtl / libsupp.c
1 /*
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)
7 * Gunnar Dalsnes
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ntdll.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 SIZE_T RtlpAllocDeallocQueryBufferSize = PAGE_SIZE;
17 PTEB LdrpTopLevelDllBeingLoadedTeb = NULL;
18
19 /* FUNCTIONS ***************************************************************/
20
21 /*
22 * @implemented
23 */
24 ULONG
25 NTAPI
26 RtlWalkFrameChain(OUT PVOID *Callers,
27 IN ULONG Count,
28 IN ULONG Flags)
29 {
30 /* Not implemented for user-mode */
31 return 0;
32 }
33
34 BOOLEAN
35 NTAPI
36 RtlpCheckForActiveDebugger(VOID)
37 {
38 /* Return the flag in the PEB */
39 return NtCurrentPeb()->BeingDebugged;
40 }
41
42 BOOLEAN
43 NTAPI
44 RtlpSetInDbgPrint(VOID)
45 {
46 /* Check if it's already set and return TRUE if so */
47 if (NtCurrentTeb()->InDbgPrint) return TRUE;
48
49 /* Set it and return */
50 NtCurrentTeb()->InDbgPrint = TRUE;
51 return FALSE;
52 }
53
54 VOID
55 NTAPI
56 RtlpClearInDbgPrint(VOID)
57 {
58 /* Clear the flag */
59 NtCurrentTeb()->InDbgPrint = FALSE;
60 }
61
62 KPROCESSOR_MODE
63 NTAPI
64 RtlpGetMode()
65 {
66 return UserMode;
67 }
68
69 /*
70 * @implemented
71 */
72 PPEB
73 NTAPI
74 RtlGetCurrentPeb(VOID)
75 {
76 return NtCurrentPeb();
77 }
78
79 /*
80 * @implemented
81 */
82 VOID NTAPI
83 RtlAcquirePebLock(VOID)
84 {
85 PPEB Peb = NtCurrentPeb ();
86 Peb->FastPebLockRoutine (Peb->FastPebLock);
87 }
88
89 /*
90 * @implemented
91 */
92 VOID NTAPI
93 RtlReleasePebLock(VOID)
94 {
95 PPEB Peb = NtCurrentPeb ();
96 Peb->FastPebUnlockRoutine (Peb->FastPebLock);
97 }
98
99 /*
100 * @implemented
101 */
102 ULONG
103 NTAPI
104 RtlGetNtGlobalFlags(VOID)
105 {
106 PPEB pPeb = NtCurrentPeb();
107 return pPeb->NtGlobalFlag;
108 }
109
110 NTSTATUS
111 NTAPI
112 RtlDeleteHeapLock(
113 PRTL_CRITICAL_SECTION CriticalSection)
114 {
115 return RtlDeleteCriticalSection(CriticalSection);
116 }
117
118 NTSTATUS
119 NTAPI
120 RtlEnterHeapLock(
121 PRTL_CRITICAL_SECTION CriticalSection)
122 {
123 return RtlEnterCriticalSection(CriticalSection);
124 }
125
126 NTSTATUS
127 NTAPI
128 RtlInitializeHeapLock(
129 PRTL_CRITICAL_SECTION CriticalSection)
130 {
131 return RtlInitializeCriticalSection(CriticalSection);
132 }
133
134 NTSTATUS
135 NTAPI
136 RtlLeaveHeapLock(
137 PRTL_CRITICAL_SECTION CriticalSection)
138 {
139 return RtlLeaveCriticalSection(CriticalSection );
140 }
141
142 PVOID
143 NTAPI
144 RtlpAllocateMemory(UINT Bytes,
145 ULONG Tag)
146 {
147 UNREFERENCED_PARAMETER(Tag);
148
149 return RtlAllocateHeap(RtlGetProcessHeap(),
150 0,
151 Bytes);
152 }
153
154
155 VOID
156 NTAPI
157 RtlpFreeMemory(PVOID Mem,
158 ULONG Tag)
159 {
160 UNREFERENCED_PARAMETER(Tag);
161
162 RtlFreeHeap(RtlGetProcessHeap(),
163 0,
164 Mem);
165 }
166
167
168 #if DBG
169 VOID FASTCALL
170 CHECK_PAGED_CODE_RTL(char *file, int line)
171 {
172 /* meaningless in user mode */
173 }
174 #endif
175
176 BOOLEAN
177 NTAPI
178 RtlpHandleDpcStackException(IN PEXCEPTION_REGISTRATION_RECORD RegistrationFrame,
179 IN ULONG_PTR RegistrationFrameEnd,
180 IN OUT PULONG_PTR StackLow,
181 IN OUT PULONG_PTR StackHigh)
182 {
183 /* There's no such thing as a DPC stack in user-mode */
184 return FALSE;
185 }
186
187 VOID
188 NTAPI
189 RtlpCheckLogException(IN PEXCEPTION_RECORD ExceptionRecord,
190 IN PCONTEXT ContextRecord,
191 IN PVOID ContextData,
192 IN ULONG Size)
193 {
194 /* Exception logging is not done in user-mode */
195 }
196
197 BOOLEAN
198 NTAPI
199 RtlpCaptureStackLimits(IN ULONG_PTR Ebp,
200 IN ULONG_PTR *StackBegin,
201 IN ULONG_PTR *StackEnd)
202 {
203 /* FIXME: Verify */
204 *StackBegin = (ULONG_PTR)NtCurrentTeb()->Tib.StackLimit;
205 *StackEnd = (ULONG_PTR)NtCurrentTeb()->Tib.StackBase;
206 return TRUE;
207 }
208
209 BOOLEAN
210 NTAPI
211 RtlIsThreadWithinLoaderCallout(VOID)
212 {
213 return LdrpTopLevelDllBeingLoadedTeb == NtCurrentTeb();
214 }
215
216 /* RTL Atom Tables ************************************************************/
217
218 typedef struct _RTL_ATOM_HANDLE
219 {
220 RTL_HANDLE_TABLE_ENTRY Handle;
221 PRTL_ATOM_TABLE_ENTRY AtomEntry;
222 } RTL_ATOM_HANDLE, *PRTL_ATOM_HANDLE;
223
224 NTSTATUS
225 RtlpInitAtomTableLock(PRTL_ATOM_TABLE AtomTable)
226 {
227 RtlInitializeCriticalSection(&AtomTable->CriticalSection);
228 return STATUS_SUCCESS;
229 }
230
231
232 VOID
233 RtlpDestroyAtomTableLock(PRTL_ATOM_TABLE AtomTable)
234 {
235 RtlDeleteCriticalSection(&AtomTable->CriticalSection);
236 }
237
238
239 BOOLEAN
240 RtlpLockAtomTable(PRTL_ATOM_TABLE AtomTable)
241 {
242 RtlEnterCriticalSection(&AtomTable->CriticalSection);
243 return TRUE;
244 }
245
246
247 VOID
248 RtlpUnlockAtomTable(PRTL_ATOM_TABLE AtomTable)
249 {
250 RtlLeaveCriticalSection(&AtomTable->CriticalSection);
251 }
252
253
254 /* handle functions */
255
256 BOOLEAN
257 RtlpCreateAtomHandleTable(PRTL_ATOM_TABLE AtomTable)
258 {
259 RtlInitializeHandleTable(0xCFFF,
260 sizeof(RTL_ATOM_HANDLE),
261 &AtomTable->RtlHandleTable);
262
263 return TRUE;
264 }
265
266 VOID
267 RtlpDestroyAtomHandleTable(PRTL_ATOM_TABLE AtomTable)
268 {
269 RtlDestroyHandleTable(&AtomTable->RtlHandleTable);
270 }
271
272 PRTL_ATOM_TABLE
273 RtlpAllocAtomTable(ULONG Size)
274 {
275 return (PRTL_ATOM_TABLE)RtlAllocateHeap(RtlGetProcessHeap(),
276 HEAP_ZERO_MEMORY,
277 Size);
278 }
279
280 VOID
281 RtlpFreeAtomTable(PRTL_ATOM_TABLE AtomTable)
282 {
283 RtlFreeHeap(RtlGetProcessHeap(),
284 0,
285 AtomTable);
286 }
287
288 PRTL_ATOM_TABLE_ENTRY
289 RtlpAllocAtomTableEntry(ULONG Size)
290 {
291 return (PRTL_ATOM_TABLE_ENTRY)RtlAllocateHeap(RtlGetProcessHeap(),
292 HEAP_ZERO_MEMORY,
293 Size);
294 }
295
296 VOID
297 RtlpFreeAtomTableEntry(PRTL_ATOM_TABLE_ENTRY Entry)
298 {
299 RtlFreeHeap(RtlGetProcessHeap(),
300 0,
301 Entry);
302 }
303
304 VOID
305 RtlpFreeAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
306 {
307 PRTL_HANDLE_TABLE_ENTRY RtlHandleEntry;
308
309 if (RtlIsValidIndexHandle(&AtomTable->RtlHandleTable,
310 (ULONG)Entry->HandleIndex,
311 &RtlHandleEntry))
312 {
313 RtlFreeHandle(&AtomTable->RtlHandleTable,
314 RtlHandleEntry);
315 }
316 }
317
318 BOOLEAN
319 RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
320 {
321 ULONG HandleIndex;
322 PRTL_HANDLE_TABLE_ENTRY RtlHandle;
323
324 RtlHandle = RtlAllocateHandle(&AtomTable->RtlHandleTable,
325 &HandleIndex);
326 if (RtlHandle != NULL)
327 {
328 PRTL_ATOM_HANDLE AtomHandle = (PRTL_ATOM_HANDLE)RtlHandle;
329
330 /* FIXME - Handle Indexes >= 0xC000 ?! */
331 if (HandleIndex < 0xC000)
332 {
333 Entry->HandleIndex = (USHORT)HandleIndex;
334 Entry->Atom = 0xC000 + (USHORT)HandleIndex;
335
336 AtomHandle->AtomEntry = Entry;
337 AtomHandle->Handle.Flags = RTL_HANDLE_VALID;
338
339 return TRUE;
340 }
341 else
342 {
343 /* set the valid flag, otherwise RtlFreeHandle will fail! */
344 AtomHandle->Handle.Flags = RTL_HANDLE_VALID;
345
346 RtlFreeHandle(&AtomTable->RtlHandleTable,
347 RtlHandle);
348 }
349 }
350
351 return FALSE;
352 }
353
354 PRTL_ATOM_TABLE_ENTRY
355 RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable, ULONG Index)
356 {
357 PRTL_HANDLE_TABLE_ENTRY RtlHandle;
358
359 if (RtlIsValidIndexHandle(&AtomTable->RtlHandleTable,
360 Index,
361 &RtlHandle))
362 {
363 PRTL_ATOM_HANDLE AtomHandle = (PRTL_ATOM_HANDLE)RtlHandle;
364
365 return AtomHandle->AtomEntry;
366 }
367
368 return NULL;
369 }
370
371
372 /*
373 * Ldr Resource support code
374 */
375
376 IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( IMAGE_RESOURCE_DIRECTORY *dir,
377 LPCWSTR name, void *root,
378 int want_dir );
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 );
384
385 /**********************************************************************
386 * find_entry
387 *
388 * Find a resource entry
389 */
390 NTSTATUS find_entry( PVOID BaseAddress, LDR_RESOURCE_INFO *info,
391 ULONG level, void **ret, int want_dir )
392 {
393 ULONG size;
394 void *root;
395 IMAGE_RESOURCE_DIRECTORY *resdirptr;
396 USHORT list[9]; /* list of languages to try */
397 int i, pos = 0;
398 LCID user_lcid, system_lcid;
399
400 root = RtlImageDirectoryEntryToData( BaseAddress, TRUE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size );
401 if (!root) return STATUS_RESOURCE_DATA_NOT_FOUND;
402 resdirptr = root;
403
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;
408
409 resdirptr = *ret;
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 */
414
415 /* 1. specified language */
416 pos = push_language( list, pos, info->Language );
417
418 /* 2. specified language with neutral sublanguage */
419 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(info->Language), SUBLANG_NEUTRAL ) );
420
421 /* 3. neutral language with neutral sublanguage */
422 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
423
424 /* if no explicitly specified language, try some defaults */
425 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
426 {
427 /* user defaults, unless SYS_DEFAULT sublanguage specified */
428 if (SUBLANGID(info->Language) != SUBLANG_SYS_DEFAULT)
429 {
430 /* 4. current thread locale language */
431 pos = push_language( list, pos, LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale) );
432
433 if (NT_SUCCESS(NtQueryDefaultLocale(TRUE, &user_lcid)))
434 {
435 /* 5. user locale language */
436 pos = push_language( list, pos, LANGIDFROMLCID(user_lcid) );
437
438 /* 6. user locale language with neutral sublanguage */
439 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(user_lcid), SUBLANG_NEUTRAL ) );
440 }
441 }
442
443 /* now system defaults */
444
445 if (NT_SUCCESS(NtQueryDefaultLocale(FALSE, &system_lcid)))
446 {
447 /* 7. system locale language */
448 pos = push_language( list, pos, LANGIDFROMLCID( system_lcid ) );
449
450 /* 8. system locale language with neutral sublanguage */
451 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(system_lcid), SUBLANG_NEUTRAL ) );
452 }
453
454 /* 9. English */
455 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
456 }
457
458 resdirptr = *ret;
459 for (i = 0; i < pos; i++)
460 if ((*ret = find_entry_by_id( resdirptr, list[i], root, want_dir ))) return STATUS_SUCCESS;
461
462 /* if no explicitly specified language, return the first entry */
463 if (PRIMARYLANGID(info->Language) == LANG_NEUTRAL)
464 {
465 if ((*ret = find_first_entry( resdirptr, root, want_dir ))) return STATUS_SUCCESS;
466 }
467 return STATUS_RESOURCE_LANG_NOT_FOUND;
468
469 done:
470 *ret = resdirptr;
471 return STATUS_SUCCESS;
472 }