Fixed a few problems with DCX_* flags handling.
[wine] / dlls / ntdll / rtl.c
1 /*
2  * NT basis DLL
3  * 
4  * This file contains the Rtl* API functions. These should be implementable.
5  * 
6  * Copyright 1996-1998 Marcus Meissner
7  *                1999 Alex Korobka
8  */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include "debugtools.h"
14 #include "windef.h"
15 #include "winerror.h"
16 #include "stackframe.h"
17
18 #include "ntddk.h"
19 #include "winreg.h"
20
21 DEFAULT_DEBUG_CHANNEL(ntdll);
22
23
24 static RTL_CRITICAL_SECTION peb_lock = CRITICAL_SECTION_INIT;
25
26 /*
27  *      resource functions
28  */
29
30 /***********************************************************************
31  *           RtlInitializeResource      (NTDLL.@)
32  *
33  * xxxResource() functions implement multiple-reader-single-writer lock.
34  * The code is based on information published in WDJ January 1999 issue.
35  */
36 void WINAPI RtlInitializeResource(LPRTL_RWLOCK rwl)
37 {
38     if( rwl )
39     {
40         rwl->iNumberActive = 0;
41         rwl->uExclusiveWaiters = 0;
42         rwl->uSharedWaiters = 0;
43         rwl->hOwningThreadId = 0;
44         rwl->dwTimeoutBoost = 0; /* no info on this one, default value is 0 */
45         RtlInitializeCriticalSection( &rwl->rtlCS );
46         NtCreateSemaphore( &rwl->hExclusiveReleaseSemaphore, 0, NULL, 0, 65535 );
47         NtCreateSemaphore( &rwl->hSharedReleaseSemaphore, 0, NULL, 0, 65535 );
48     }
49 }
50
51
52 /***********************************************************************
53  *           RtlDeleteResource          (NTDLL.@)
54  */
55 void WINAPI RtlDeleteResource(LPRTL_RWLOCK rwl)
56 {
57     if( rwl )
58     {
59         RtlEnterCriticalSection( &rwl->rtlCS );
60         if( rwl->iNumberActive || rwl->uExclusiveWaiters || rwl->uSharedWaiters )
61             MESSAGE("Deleting active MRSW lock (%p), expect failure\n", rwl );
62         rwl->hOwningThreadId = 0;
63         rwl->uExclusiveWaiters = rwl->uSharedWaiters = 0;
64         rwl->iNumberActive = 0;
65         NtClose( rwl->hExclusiveReleaseSemaphore );
66         NtClose( rwl->hSharedReleaseSemaphore );
67         RtlLeaveCriticalSection( &rwl->rtlCS );
68         RtlDeleteCriticalSection( &rwl->rtlCS );
69     }
70 }
71
72
73 /***********************************************************************
74  *          RtlAcquireResourceExclusive (NTDLL.@)
75  */
76 BYTE WINAPI RtlAcquireResourceExclusive(LPRTL_RWLOCK rwl, BYTE fWait)
77 {
78     BYTE retVal = 0;
79     if( !rwl ) return 0;
80
81 start:
82     RtlEnterCriticalSection( &rwl->rtlCS );
83     if( rwl->iNumberActive == 0 ) /* lock is free */
84     {
85         rwl->iNumberActive = -1;
86         retVal = 1;
87     }
88     else if( rwl->iNumberActive < 0 ) /* exclusive lock in progress */
89     {
90          if( rwl->hOwningThreadId == GetCurrentThreadId() )
91          {
92              retVal = 1;
93              rwl->iNumberActive--;
94              goto done;
95          }
96 wait:
97          if( fWait )
98          {
99              rwl->uExclusiveWaiters++;
100
101              RtlLeaveCriticalSection( &rwl->rtlCS );
102              if( WaitForSingleObject( rwl->hExclusiveReleaseSemaphore, INFINITE ) == WAIT_FAILED )
103                  goto done;
104              goto start; /* restart the acquisition to avoid deadlocks */
105          }
106     }
107     else  /* one or more shared locks are in progress */
108          if( fWait )
109              goto wait;
110          
111     if( retVal == 1 )
112         rwl->hOwningThreadId = GetCurrentThreadId();
113 done:
114     RtlLeaveCriticalSection( &rwl->rtlCS );
115     return retVal;
116 }
117
118 /***********************************************************************
119  *          RtlAcquireResourceShared    (NTDLL.@)
120  */
121 BYTE WINAPI RtlAcquireResourceShared(LPRTL_RWLOCK rwl, BYTE fWait)
122 {
123     DWORD dwWait = WAIT_FAILED;
124     BYTE retVal = 0;
125     if( !rwl ) return 0;
126
127 start:
128     RtlEnterCriticalSection( &rwl->rtlCS );
129     if( rwl->iNumberActive < 0 )
130     {
131         if( rwl->hOwningThreadId == GetCurrentThreadId() )
132         {
133             rwl->iNumberActive--;
134             retVal = 1;
135             goto done;
136         }
137         
138         if( fWait )
139         {
140             rwl->uSharedWaiters++;
141             RtlLeaveCriticalSection( &rwl->rtlCS );
142             if( (dwWait = WaitForSingleObject( rwl->hSharedReleaseSemaphore, INFINITE )) == WAIT_FAILED )
143                 goto done;
144             goto start;
145         }
146     }
147     else 
148     {
149         if( dwWait != WAIT_OBJECT_0 ) /* otherwise RtlReleaseResource() has already done it */
150             rwl->iNumberActive++;
151         retVal = 1;
152     }
153 done:
154     RtlLeaveCriticalSection( &rwl->rtlCS );
155     return retVal;
156 }
157
158
159 /***********************************************************************
160  *           RtlReleaseResource         (NTDLL.@)
161  */
162 void WINAPI RtlReleaseResource(LPRTL_RWLOCK rwl)
163 {
164     RtlEnterCriticalSection( &rwl->rtlCS );
165
166     if( rwl->iNumberActive > 0 ) /* have one or more readers */
167     {
168         if( --rwl->iNumberActive == 0 )
169         {
170             if( rwl->uExclusiveWaiters )
171             {
172 wake_exclusive:
173                 rwl->uExclusiveWaiters--;
174                 NtReleaseSemaphore( rwl->hExclusiveReleaseSemaphore, 1, NULL );
175             }
176         }
177     }
178     else 
179     if( rwl->iNumberActive < 0 ) /* have a writer, possibly recursive */
180     {
181         if( ++rwl->iNumberActive == 0 )
182         {
183             rwl->hOwningThreadId = 0;
184             if( rwl->uExclusiveWaiters )
185                 goto wake_exclusive;
186             else
187                 if( rwl->uSharedWaiters )
188                 {
189                     UINT n = rwl->uSharedWaiters;
190                     rwl->iNumberActive = rwl->uSharedWaiters; /* prevent new writers from joining until
191                                                                * all queued readers have done their thing */
192                     rwl->uSharedWaiters = 0;
193                     NtReleaseSemaphore( rwl->hSharedReleaseSemaphore, n, NULL );
194                 }
195         }
196     }
197     RtlLeaveCriticalSection( &rwl->rtlCS );
198 }
199
200
201 /***********************************************************************
202  *           RtlDumpResource            (NTDLL.@)
203  */
204 void WINAPI RtlDumpResource(LPRTL_RWLOCK rwl)
205 {
206     if( rwl )
207     {
208         MESSAGE("RtlDumpResource(%p):\n\tactive count = %i\n\twaiting readers = %i\n\twaiting writers = %i\n",  
209                 rwl, rwl->iNumberActive, rwl->uSharedWaiters, rwl->uExclusiveWaiters );
210         if( rwl->iNumberActive )
211             MESSAGE("\towner thread = %08x\n", rwl->hOwningThreadId );
212     }
213 }
214
215 /*
216  *      heap functions
217  */
218
219 /******************************************************************************
220  *  RtlCreateHeap               [NTDLL.@]
221  */
222 HANDLE WINAPI RtlCreateHeap(
223         ULONG Flags,
224         PVOID BaseAddress,
225         ULONG SizeToReserve,
226         ULONG SizeToCommit,
227         PVOID Unknown,
228         PRTL_HEAP_DEFINITION Definition)
229 {
230         FIXME("(0x%08lx, %p, 0x%08lx, 0x%08lx, %p, %p) semi-stub\n",
231         Flags, BaseAddress, SizeToReserve, SizeToCommit, Unknown, Definition);
232         
233         return HeapCreate ( Flags, SizeToCommit, SizeToReserve);
234
235 }       
236 /******************************************************************************
237  *  RtlAllocateHeap             [NTDLL.@]
238  */
239 PVOID WINAPI RtlAllocateHeap(
240         HANDLE Heap,
241         ULONG Flags,
242         ULONG Size)
243 {
244         TRACE("(0x%08x, 0x%08lx, 0x%08lx) semi stub\n",
245         Heap, Flags, Size);
246         return HeapAlloc(Heap, Flags, Size);
247 }
248
249 /******************************************************************************
250  *  RtlFreeHeap         [NTDLL.@]
251  */
252 BOOLEAN WINAPI RtlFreeHeap(
253         HANDLE Heap,
254         ULONG Flags,
255         PVOID Address)
256 {
257         TRACE("(0x%08x, 0x%08lx, %p) semi stub\n",
258         Heap, Flags, Address);
259         return HeapFree(Heap, Flags, Address);
260 }
261         
262 /******************************************************************************
263  *  RtlDestroyHeap              [NTDLL.@]
264  *
265  * FIXME: prototype guessed
266  */
267 BOOLEAN WINAPI RtlDestroyHeap(
268         HANDLE Heap)
269 {
270         TRACE("(0x%08x) semi stub\n", Heap);
271         return HeapDestroy(Heap);
272 }
273         
274 /*
275  *      misc functions
276  */
277
278 /******************************************************************************
279  *      DbgPrint        [NTDLL.@]
280  */
281 void WINAPIV DbgPrint(LPCSTR fmt, ...)
282 {
283        char buf[512];
284        va_list args;
285
286        va_start(args, fmt);
287        vsprintf(buf,fmt, args);
288        va_end(args); 
289
290         MESSAGE("DbgPrint says: %s",buf);
291         /* hmm, raise exception? */
292 }
293
294 /******************************************************************************
295  *  RtlAcquirePebLock           [NTDLL.@]
296  */
297 VOID WINAPI RtlAcquirePebLock(void)
298 {
299     RtlEnterCriticalSection( &peb_lock );
300 }
301
302 /******************************************************************************
303  *  RtlReleasePebLock           [NTDLL.@]
304  */
305 VOID WINAPI RtlReleasePebLock(void)
306 {
307     RtlLeaveCriticalSection( &peb_lock );
308 }
309
310 /******************************************************************************
311  *  RtlIntegerToChar    [NTDLL.@]
312  */
313 DWORD WINAPI RtlIntegerToChar(DWORD x1,DWORD x2,DWORD x3,DWORD x4) {
314         FIXME("(0x%08lx,0x%08lx,0x%08lx,0x%08lx),stub!\n",x1,x2,x3,x4);
315         return 0;
316 }
317 /******************************************************************************
318  *  RtlSetEnvironmentVariable           [NTDLL.@]
319  */
320 DWORD WINAPI RtlSetEnvironmentVariable(DWORD x1,PUNICODE_STRING key,PUNICODE_STRING val) {
321         FIXME("(0x%08lx,%s,%s),stub!\n",x1,debugstr_w(key->Buffer),debugstr_w(val->Buffer));
322         return 0;
323 }
324
325 /******************************************************************************
326  *  RtlNewSecurityObject                [NTDLL.@]
327  */
328 DWORD WINAPI RtlNewSecurityObject(DWORD x1,DWORD x2,DWORD x3,DWORD x4,DWORD x5,DWORD x6) {
329         FIXME("(0x%08lx,0x%08lx,0x%08lx,0x%08lx,0x%08lx,0x%08lx),stub!\n",x1,x2,x3,x4,x5,x6);
330         return 0;
331 }
332
333 /******************************************************************************
334  *  RtlDeleteSecurityObject             [NTDLL.@]
335  */
336 DWORD WINAPI RtlDeleteSecurityObject(DWORD x1) {
337         FIXME("(0x%08lx),stub!\n",x1);
338         return 0;
339 }
340
341 /**************************************************************************
342  *                 RtlNormalizeProcessParams            [NTDLL.@]
343  */
344 LPVOID WINAPI RtlNormalizeProcessParams(LPVOID x)
345 {
346     FIXME("(%p), stub\n",x);
347     return x;
348 }
349
350 /**************************************************************************
351  *                 RtlGetNtProductType                  [NTDLL.@]
352  */
353 BOOLEAN WINAPI RtlGetNtProductType(LPDWORD type)
354 {
355     FIXME("(%p): stub\n", type);
356     *type=3; /* dunno. 1 for client, 3 for server? */
357     return 1;
358 }
359
360 /**************************************************************************
361  *                 _chkstk                              [NTDLL.@]
362  *
363  * Glorified "enter xxxx".
364  */
365 void WINAPI NTDLL_chkstk( CONTEXT86 *context )
366 {
367     context->Esp -= context->Eax;
368 }
369
370 /**************************************************************************
371  *                 _alloca_probe                        [NTDLL.@]
372  *
373  * Glorified "enter xxxx".
374  */
375 void WINAPI NTDLL_alloca_probe( CONTEXT86 *context )
376 {
377     context->Esp -= context->Eax;
378 }
379
380 /**************************************************************************
381  *                 RtlDosPathNameToNtPathName_U         [NTDLL.@]
382  *
383  * FIXME: convert to UNC or whatever is expected here
384  */
385 BOOLEAN  WINAPI RtlDosPathNameToNtPathName_U(
386         LPWSTR from,PUNICODE_STRING us,DWORD x2,DWORD x3)
387 {
388     FIXME("(%s,%p,%08lx,%08lx)\n",debugstr_w(from),us,x2,x3);
389     if (us) RtlCreateUnicodeString( us, from );
390     return TRUE;
391 }
392
393
394 /***********************************************************************
395  *           RtlImageNtHeader   (NTDLL.@)
396  */
397 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
398 {
399     IMAGE_NT_HEADERS *ret = NULL;
400     IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)hModule;
401
402     if (dos->e_magic == IMAGE_DOS_SIGNATURE)
403     {
404         ret = (IMAGE_NT_HEADERS *)((char *)dos + dos->e_lfanew);
405         if (ret->Signature != IMAGE_NT_SIGNATURE) ret = NULL;
406     }
407     return ret;
408 }
409
410
411 /******************************************************************************
412  *  RtlCreateEnvironment                [NTDLL.@]
413  */
414 DWORD WINAPI RtlCreateEnvironment(DWORD x1,DWORD x2) {
415         FIXME("(0x%08lx,0x%08lx),stub!\n",x1,x2);
416         return 0;
417 }
418
419
420 /******************************************************************************
421  *  RtlDestroyEnvironment               [NTDLL.@]
422  */
423 DWORD WINAPI RtlDestroyEnvironment(DWORD x) {
424         FIXME("(0x%08lx),stub!\n",x);
425         return 0;
426 }
427
428 /******************************************************************************
429  *  RtlQueryEnvironmentVariable_U               [NTDLL.@]
430  */
431 DWORD WINAPI RtlQueryEnvironmentVariable_U(DWORD x1,PUNICODE_STRING key,PUNICODE_STRING val) {
432         FIXME("(0x%08lx,%s,%p),stub!\n",x1,debugstr_w(key->Buffer),val);
433         return 0;
434 }
435 /******************************************************************************
436  *  RtlInitializeGenericTable           [NTDLL.@]
437  */
438 DWORD WINAPI RtlInitializeGenericTable(void)
439 {
440         FIXME("\n");
441         return 0;
442 }
443
444 /******************************************************************************
445  *  RtlInitializeBitMap                 [NTDLL.@]
446  * 
447  */
448 NTSTATUS WINAPI RtlInitializeBitMap(DWORD x1,DWORD x2,DWORD x3)
449 {
450         FIXME("(0x%08lx,0x%08lx,0x%08lx),stub\n",x1,x2,x3);
451         return 0;
452 }
453
454 /******************************************************************************
455  *  RtlSetBits                          [NTDLL.@]
456  * 
457  */
458 NTSTATUS WINAPI RtlSetBits(DWORD x1,DWORD x2,DWORD x3)
459 {
460         FIXME("(0x%08lx,0x%08lx,0x%08lx),stub\n",x1,x2,x3);
461         return 0;
462 }
463
464 /******************************************************************************
465  *  RtlFindClearBits                    [NTDLL.@]
466  * 
467  */
468 NTSTATUS WINAPI RtlFindClearBits(DWORD x1,DWORD x2,DWORD x3)
469 {
470         FIXME("(0x%08lx,0x%08lx,0x%08lx),stub\n",x1,x2,x3);
471         return 0;
472 }
473
474 /******************************************************************************
475  *  RtlClearBits                        [NTDLL.@]
476  * 
477  */
478 NTSTATUS WINAPI RtlClearBits(DWORD x1,DWORD x2,DWORD x3)
479 {
480         FIXME("(0x%08lx,0x%08lx,0x%08lx),stub\n",x1,x2,x3);
481         return 0;
482 }
483
484 /******************************************************************************
485  *  RtlCopyMemory   [NTDLL] 
486  * 
487  */
488 #undef RtlCopyMemory
489 VOID WINAPI RtlCopyMemory( VOID *Destination, CONST VOID *Source, SIZE_T Length )
490 {
491     memcpy(Destination, Source, Length);
492 }       
493
494 /******************************************************************************
495  *  RtlMoveMemory   [NTDLL.@]
496  */
497 #undef RtlMoveMemory
498 VOID WINAPI RtlMoveMemory( VOID *Destination, CONST VOID *Source, SIZE_T Length )
499 {
500     memmove(Destination, Source, Length);
501 }
502
503 /******************************************************************************
504  *  RtlFillMemory   [NTDLL.@]
505  */
506 #undef RtlFillMemory
507 VOID WINAPI RtlFillMemory( VOID *Destination, SIZE_T Length, BYTE Fill )
508 {
509     memset(Destination, Fill, Length);
510 }
511
512 /******************************************************************************
513  *  RtlZeroMemory   [NTDLL.@]
514  */
515 #undef RtlZeroMemory
516 VOID WINAPI RtlZeroMemory( VOID *Destination, SIZE_T Length )
517 {
518     memset(Destination, 0, Length);
519 }
520
521 /******************************************************************************
522  *  RtlCompareMemory   [NTDLL.@]
523  */
524 SIZE_T WINAPI RtlCompareMemory( const VOID *Source1, const VOID *Source2, SIZE_T Length)
525 {
526     int i;
527     for(i=0; (i<Length) && (((LPBYTE)Source1)[i]==((LPBYTE)Source2)[i]); i++);
528     return i;
529 }
530
531 /******************************************************************************
532  *  RtlAssert                           [NTDLL.@]
533  *
534  * Not implemented in non-debug versions.
535  */
536 void WINAPI RtlAssert(LPVOID x1,LPVOID x2,DWORD x3, DWORD x4)
537 {
538         FIXME("(%p,%p,0x%08lx,0x%08lx),stub\n",x1,x2,x3,x4);
539 }