kernelbase: Let GetModuleBaseName succeed on 64bit modules in wow64.
[wine.git] / dlls / ntdll / rtl.c
blob84488ea1d812ac8b5c769b95c66da3b270982838
1 /*
2 * NT basis DLL
4 * This file contains the Rtl* API functions. These should be implementable.
6 * Copyright 1996-1998 Marcus Meissner
7 * Copyright 1999 Alex Korobka
8 * Copyright 2003 Thomas Mertes
9 * Crc32 code Copyright 1986 Gary S. Brown (Public domain)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <stdarg.h>
28 #include "ntstatus.h"
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #define WIN32_NO_STATUS
32 #include "winsock2.h"
33 #include "windef.h"
34 #include "winternl.h"
35 #include "wine/debug.h"
36 #include "wine/exception.h"
37 #include "ntdll_misc.h"
38 #include "in6addr.h"
39 #include "ddk/ntddk.h"
40 #include "ddk/ntifs.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
43 WINE_DECLARE_DEBUG_CHANNEL(debugstr);
45 /* CRC polynomial 0xedb88320 */
46 static const DWORD CRC_table[256] =
48 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
49 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
50 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
51 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
52 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
53 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
54 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
55 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
56 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
57 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
58 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
59 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
60 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
61 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
62 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
63 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
64 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
65 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
66 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
67 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
68 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
69 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
70 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
71 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
72 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
73 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
74 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
75 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
76 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
77 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
78 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
79 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
80 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
81 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
82 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
83 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
84 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
85 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
86 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
87 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
88 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
89 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
90 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
93 static const int hex_table[] = {
94 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
95 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
96 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2F */
97 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
98 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4F */
99 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
100 -1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
104 * resource functions
107 /***********************************************************************
108 * RtlInitializeResource (NTDLL.@)
110 * xxxResource() functions implement multiple-reader-single-writer lock.
111 * The code is based on information published in WDJ January 1999 issue.
113 void WINAPI RtlInitializeResource(LPRTL_RWLOCK rwl)
115 if( rwl )
117 rwl->iNumberActive = 0;
118 rwl->uExclusiveWaiters = 0;
119 rwl->uSharedWaiters = 0;
120 rwl->hOwningThreadId = 0;
121 rwl->dwTimeoutBoost = 0; /* no info on this one, default value is 0 */
122 RtlInitializeCriticalSection( &rwl->rtlCS );
123 rwl->rtlCS.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RTL_RWLOCK.rtlCS");
124 NtCreateSemaphore( &rwl->hExclusiveReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
125 NtCreateSemaphore( &rwl->hSharedReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
130 /***********************************************************************
131 * RtlDeleteResource (NTDLL.@)
133 void WINAPI RtlDeleteResource(LPRTL_RWLOCK rwl)
135 if( rwl )
137 RtlEnterCriticalSection( &rwl->rtlCS );
138 if( rwl->iNumberActive || rwl->uExclusiveWaiters || rwl->uSharedWaiters )
139 ERR("Deleting active MRSW lock (%p), expect failure\n", rwl );
140 rwl->hOwningThreadId = 0;
141 rwl->uExclusiveWaiters = rwl->uSharedWaiters = 0;
142 rwl->iNumberActive = 0;
143 NtClose( rwl->hExclusiveReleaseSemaphore );
144 NtClose( rwl->hSharedReleaseSemaphore );
145 RtlLeaveCriticalSection( &rwl->rtlCS );
146 rwl->rtlCS.DebugInfo->Spare[0] = 0;
147 RtlDeleteCriticalSection( &rwl->rtlCS );
152 /***********************************************************************
153 * RtlAcquireResourceExclusive (NTDLL.@)
155 BYTE WINAPI RtlAcquireResourceExclusive(LPRTL_RWLOCK rwl, BYTE fWait)
157 BYTE retVal = 0;
158 if( !rwl ) return 0;
160 start:
161 RtlEnterCriticalSection( &rwl->rtlCS );
162 if( rwl->iNumberActive == 0 ) /* lock is free */
164 rwl->iNumberActive = -1;
165 retVal = 1;
167 else if( rwl->iNumberActive < 0 ) /* exclusive lock in progress */
169 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
171 retVal = 1;
172 rwl->iNumberActive--;
173 goto done;
175 wait:
176 if( fWait )
178 NTSTATUS status;
180 rwl->uExclusiveWaiters++;
182 RtlLeaveCriticalSection( &rwl->rtlCS );
183 status = NtWaitForSingleObject( rwl->hExclusiveReleaseSemaphore, FALSE, NULL );
184 if( HIWORD(status) )
185 goto done;
186 goto start; /* restart the acquisition to avoid deadlocks */
189 else /* one or more shared locks are in progress */
190 if( fWait )
191 goto wait;
193 if( retVal == 1 )
194 rwl->hOwningThreadId = ULongToHandle(GetCurrentThreadId());
195 done:
196 RtlLeaveCriticalSection( &rwl->rtlCS );
197 return retVal;
200 /***********************************************************************
201 * RtlAcquireResourceShared (NTDLL.@)
203 BYTE WINAPI RtlAcquireResourceShared(LPRTL_RWLOCK rwl, BYTE fWait)
205 NTSTATUS status = STATUS_UNSUCCESSFUL;
206 BYTE retVal = 0;
207 if( !rwl ) return 0;
209 start:
210 RtlEnterCriticalSection( &rwl->rtlCS );
211 if( rwl->iNumberActive < 0 )
213 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
215 rwl->iNumberActive--;
216 retVal = 1;
217 goto done;
220 if( fWait )
222 rwl->uSharedWaiters++;
223 RtlLeaveCriticalSection( &rwl->rtlCS );
224 status = NtWaitForSingleObject( rwl->hSharedReleaseSemaphore, FALSE, NULL );
225 if( HIWORD(status) )
226 goto done;
227 goto start;
230 else
232 if( status != STATUS_WAIT_0 ) /* otherwise RtlReleaseResource() has already done it */
233 rwl->iNumberActive++;
234 retVal = 1;
236 done:
237 RtlLeaveCriticalSection( &rwl->rtlCS );
238 return retVal;
242 /***********************************************************************
243 * RtlReleaseResource (NTDLL.@)
245 void WINAPI RtlReleaseResource(LPRTL_RWLOCK rwl)
247 RtlEnterCriticalSection( &rwl->rtlCS );
249 if( rwl->iNumberActive > 0 ) /* have one or more readers */
251 if( --rwl->iNumberActive == 0 )
253 if( rwl->uExclusiveWaiters )
255 wake_exclusive:
256 rwl->uExclusiveWaiters--;
257 NtReleaseSemaphore( rwl->hExclusiveReleaseSemaphore, 1, NULL );
261 else
262 if( rwl->iNumberActive < 0 ) /* have a writer, possibly recursive */
264 if( ++rwl->iNumberActive == 0 )
266 rwl->hOwningThreadId = 0;
267 if( rwl->uExclusiveWaiters )
268 goto wake_exclusive;
269 else
270 if( rwl->uSharedWaiters )
272 UINT n = rwl->uSharedWaiters;
273 rwl->iNumberActive = rwl->uSharedWaiters; /* prevent new writers from joining until
274 * all queued readers have done their thing */
275 rwl->uSharedWaiters = 0;
276 NtReleaseSemaphore( rwl->hSharedReleaseSemaphore, n, NULL );
280 RtlLeaveCriticalSection( &rwl->rtlCS );
284 /***********************************************************************
285 * RtlDumpResource (NTDLL.@)
287 void WINAPI RtlDumpResource(LPRTL_RWLOCK rwl)
289 if( rwl )
291 MESSAGE("RtlDumpResource(%p):\n\tactive count = %i\n\twaiting readers = %i\n\twaiting writers = %i\n",
292 rwl, rwl->iNumberActive, rwl->uSharedWaiters, rwl->uExclusiveWaiters );
293 if( rwl->iNumberActive )
294 MESSAGE("\towner thread = %p\n", rwl->hOwningThreadId );
299 * misc functions
302 static LONG WINAPI debug_exception_handler( EXCEPTION_POINTERS *eptr )
304 EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
305 return (rec->ExceptionCode == DBG_PRINTEXCEPTION_C) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
308 /******************************************************************************
309 * DbgPrint [NTDLL.@]
311 NTSTATUS WINAPIV DbgPrint(LPCSTR fmt, ...)
313 NTSTATUS ret;
314 va_list args;
316 va_start(args, fmt);
317 ret = vDbgPrintEx(0, DPFLTR_ERROR_LEVEL, fmt, args);
318 va_end(args);
319 return ret;
323 /******************************************************************************
324 * DbgPrintEx [NTDLL.@]
326 NTSTATUS WINAPIV DbgPrintEx(ULONG iComponentId, ULONG Level, LPCSTR fmt, ...)
328 NTSTATUS ret;
329 va_list args;
331 va_start(args, fmt);
332 ret = vDbgPrintEx(iComponentId, Level, fmt, args);
333 va_end(args);
334 return ret;
337 /******************************************************************************
338 * vDbgPrintEx [NTDLL.@]
340 NTSTATUS WINAPI vDbgPrintEx( ULONG id, ULONG level, LPCSTR fmt, va_list args )
342 return vDbgPrintExWithPrefix( "", id, level, fmt, args );
345 /******************************************************************************
346 * vDbgPrintExWithPrefix [NTDLL.@]
348 NTSTATUS WINAPI vDbgPrintExWithPrefix( LPCSTR prefix, ULONG id, ULONG level, LPCSTR fmt, va_list args )
350 ULONG level_mask = level <= 31 ? (1 << level) : level;
351 SIZE_T len = strlen( prefix );
352 char buf[1024], *end;
354 strcpy( buf, prefix );
355 len += _vsnprintf( buf + len, sizeof(buf) - len, fmt, args );
356 end = buf + len - 1;
358 WARN_(debugstr)(*end == '\n' ? "%08lx:%08lx: %s" : "%08lx:%08lx: %s\n", id, level_mask, buf);
360 if (level_mask & (1 << DPFLTR_ERROR_LEVEL) && NtCurrentTeb()->Peb->BeingDebugged)
362 __TRY
364 EXCEPTION_RECORD record;
365 record.ExceptionCode = DBG_PRINTEXCEPTION_C;
366 record.ExceptionFlags = 0;
367 record.ExceptionRecord = NULL;
368 record.ExceptionAddress = RtlRaiseException;
369 record.NumberParameters = 2;
370 record.ExceptionInformation[1] = (ULONG_PTR)buf;
371 record.ExceptionInformation[0] = strlen( buf ) + 1;
372 RtlRaiseException( &record );
374 __EXCEPT(debug_exception_handler)
377 __ENDTRY
380 return STATUS_SUCCESS;
383 /******************************************************************************
384 * RtlAcquirePebLock [NTDLL.@]
386 VOID WINAPI RtlAcquirePebLock(void)
388 RtlEnterCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
391 /******************************************************************************
392 * RtlReleasePebLock [NTDLL.@]
394 VOID WINAPI RtlReleasePebLock(void)
396 RtlLeaveCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
399 /******************************************************************************
400 * RtlInitializeGenericTable [NTDLL.@]
402 void WINAPI RtlInitializeGenericTable(RTL_GENERIC_TABLE *table, PRTL_GENERIC_COMPARE_ROUTINE compare,
403 PRTL_GENERIC_ALLOCATE_ROUTINE allocate, PRTL_GENERIC_FREE_ROUTINE free,
404 void *context)
406 TRACE("(%p, %p, %p, %p, %p)\n", table, compare, allocate, free, context);
408 table->TableRoot = NULL;
409 table->InsertOrderList.Flink = &table->InsertOrderList;
410 table->InsertOrderList.Blink = &table->InsertOrderList;
411 table->OrderedPointer = &table->InsertOrderList;
412 table->NumberGenericTableElements = 0;
413 table->WhichOrderedElement = 0;
414 table->CompareRoutine = compare;
415 table->AllocateRoutine = allocate;
416 table->FreeRoutine = free;
417 table->TableContext = context;
420 /******************************************************************************
421 * RtlEnumerateGenericTableWithoutSplaying [NTDLL.@]
423 void * WINAPI RtlEnumerateGenericTableWithoutSplaying(RTL_GENERIC_TABLE *table, void *previous)
425 static int warn_once;
427 if (!warn_once++)
428 FIXME("(%p, %p) stub!\n", table, previous);
429 return NULL;
432 /******************************************************************************
433 * RtlNumberGenericTableElements [NTDLL.@]
435 ULONG WINAPI RtlNumberGenericTableElements(RTL_GENERIC_TABLE *table)
437 TRACE("(%p)\n", table);
438 return table->NumberGenericTableElements;
441 /******************************************************************************
442 * RtlGetElementGenericTable [NTDLL.@]
444 void * WINAPI RtlGetElementGenericTable(RTL_GENERIC_TABLE *table, ULONG index)
446 FIXME("(%p, %lu) stub!\n", table, index);
447 return NULL;
450 /******************************************************************************
451 * RtlLookupElementGenericTable [NTDLL.@]
453 void * WINAPI RtlLookupElementGenericTable(RTL_GENERIC_TABLE *table, void *buffer)
455 FIXME("(%p, %p) stub!\n", table, buffer);
456 return NULL;
459 /******************************************************************************
460 * RtlMoveMemory [NTDLL.@]
462 * Move a block of memory that may overlap.
464 * PARAMS
465 * Destination [O] End destination for block
466 * Source [O] Where to start copying from
467 * Length [I] Number of bytes to copy
469 * RETURNS
470 * Nothing.
472 #undef RtlMoveMemory
473 VOID WINAPI RtlMoveMemory( void *Destination, const void *Source, SIZE_T Length )
475 memmove(Destination, Source, Length);
478 /******************************************************************************
479 * RtlFillMemory [NTDLL.@]
481 * Set a block of memory with a value.
483 * PARAMS
484 * Destination [O] Block to fill
485 * Length [I] Number of bytes to fill
486 * Fill [I] Value to set
488 * RETURNS
489 * Nothing.
491 #undef RtlFillMemory
492 VOID WINAPI RtlFillMemory( VOID *Destination, SIZE_T Length, BYTE Fill )
494 memset(Destination, Fill, Length);
497 /******************************************************************************
498 * RtlZeroMemory [NTDLL.@]
500 * Set a block of memory with 0's.
502 * PARAMS
503 * Destination [O] Block to fill
504 * Length [I] Number of bytes to fill
506 * RETURNS
507 * Nothing.
509 #undef RtlZeroMemory
510 VOID WINAPI RtlZeroMemory( VOID *Destination, SIZE_T Length )
512 memset(Destination, 0, Length);
515 /******************************************************************************
516 * RtlCompareMemory [NTDLL.@]
518 * Compare one block of memory with another
520 * PARAMS
521 * Source1 [I] Source block
522 * Source2 [I] Block to compare to Source1
523 * Length [I] Number of bytes to compare
525 * RETURNS
526 * The length of the first byte at which Source1 and Source2 differ, or Length
527 * if they are the same.
529 SIZE_T WINAPI RtlCompareMemory( const VOID *Source1, const VOID *Source2, SIZE_T Length)
531 SIZE_T i;
532 for(i=0; (i<Length) && (((const BYTE*)Source1)[i]==((const BYTE*)Source2)[i]); i++);
533 return i;
536 /******************************************************************************
537 * RtlCompareMemoryUlong [NTDLL.@]
539 * Compare a block of memory with a value, a ULONG at a time
541 * PARAMS
542 * Source1 [I] Source block. This must be ULONG aligned
543 * Length [I] Number of bytes to compare. This should be a multiple of 4
544 * dwVal [I] Value to compare to
546 * RETURNS
547 * The byte position of the first byte at which Source1 is not dwVal.
549 SIZE_T WINAPI RtlCompareMemoryUlong(VOID *Source1, SIZE_T Length, ULONG dwVal)
551 SIZE_T i;
552 for(i = 0; i < Length/sizeof(ULONG) && ((ULONG *)Source1)[i] == dwVal; i++);
553 return i * sizeof(ULONG);
556 /******************************************************************************
557 * RtlCopyMemory [NTDLL.@]
559 #undef RtlCopyMemory
560 void WINAPI RtlCopyMemory(void *dest, const void *src, SIZE_T len)
562 memcpy(dest, src, len);
565 /******************************************************************************
566 * RtlAssert [NTDLL.@]
568 * Fail a debug assertion.
570 * RETURNS
571 * Nothing. This call does not return control to its caller.
573 * NOTES
574 * Not implemented in non-debug versions.
576 void WINAPI RtlAssert(void *assertion, void *filename, ULONG linenumber, char *message)
578 FIXME("(%s, %s, %lu, %s): stub\n", debugstr_a((char*)assertion), debugstr_a((char*)filename),
579 linenumber, debugstr_a(message));
582 /*************************************************************************
583 * RtlFillMemoryUlong [NTDLL.@]
585 * Fill memory with a 32 bit (dword) value.
587 * PARAMS
588 * lpDest [I] Bitmap pointer
589 * ulCount [I] Number of dwords to write
590 * ulValue [I] Value to fill with
592 * RETURNS
593 * Nothing.
595 VOID WINAPI RtlFillMemoryUlong(ULONG* lpDest, ULONG ulCount, ULONG ulValue)
597 TRACE("(%p,%lu,%lu)\n", lpDest, ulCount, ulValue);
599 ulCount /= sizeof(ULONG);
600 while(ulCount--)
601 *lpDest++ = ulValue;
604 /*********************************************************************
605 * RtlComputeCrc32 [NTDLL.@]
607 * Calculate the CRC32 checksum of a block of bytes
609 * PARAMS
610 * dwInitial [I] Initial CRC value
611 * pData [I] Data block
612 * iLen [I] Length of the byte block
614 * RETURNS
615 * The cumulative CRC32 of dwInitial and iLen bytes of the pData block.
617 DWORD WINAPI RtlComputeCrc32(DWORD dwInitial, const BYTE *pData, INT iLen)
619 DWORD crc = ~dwInitial;
621 TRACE("(%lu,%p,%d)\n", dwInitial, pData, iLen);
623 while (iLen > 0)
625 crc = CRC_table[(crc ^ *pData) & 0xff] ^ (crc >> 8);
626 pData++;
627 iLen--;
629 return ~crc;
633 /*************************************************************************
634 * RtlUlonglongByteSwap [NTDLL.@]
636 * Swap the bytes of an unsigned long long value.
638 * PARAMS
639 * i [I] Value to swap bytes of
641 * RETURNS
642 * The value with its bytes swapped.
644 #ifdef __i386__
645 __ASM_FASTCALL_FUNC(RtlUlonglongByteSwap, 8,
646 "movl 4(%esp),%edx\n\t"
647 "bswap %edx\n\t"
648 "movl 8(%esp),%eax\n\t"
649 "bswap %eax\n\t"
650 "ret $8")
651 #endif
653 /*************************************************************************
654 * RtlUlongByteSwap [NTDLL.@]
656 * Swap the bytes of an unsigned int value.
658 * NOTES
659 * ix86 version takes argument in %ecx. Other systems use the inline version.
661 #ifdef __i386__
662 __ASM_FASTCALL_FUNC(RtlUlongByteSwap, 4,
663 "movl %ecx,%eax\n\t"
664 "bswap %eax\n\t"
665 "ret")
666 #endif
668 /*************************************************************************
669 * RtlUshortByteSwap [NTDLL.@]
671 * Swap the bytes of an unsigned short value.
673 * NOTES
674 * i386 version takes argument in %cx. Other systems use the inline version.
676 #ifdef __i386__
677 __ASM_FASTCALL_FUNC(RtlUshortByteSwap, 4,
678 "movb %ch,%al\n\t"
679 "movb %cl,%ah\n\t"
680 "ret")
681 #endif
684 /*************************************************************************
685 * RtlUniform [NTDLL.@]
687 * Generates an uniform random number
689 * PARAMS
690 * seed [O] The seed of the Random function
692 * RETURNS
693 * It returns a random number uniformly distributed over [0..MAXLONG-1].
695 * NOTES
696 * Generates an uniform random number using D.H. Lehmer's 1948 algorithm.
697 * In our case the algorithm is:
699 *| result = (*seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
701 *| *seed = result;
703 * DIFFERENCES
704 * The native documentation states that the random number is
705 * uniformly distributed over [0..MAXLONG]. In reality the native
706 * function and our function return a random number uniformly
707 * distributed over [0..MAXLONG-1].
709 ULONG WINAPI RtlUniform (PULONG seed)
711 ULONG result;
714 * Instead of the algorithm stated above, we use the algorithm
715 * below, which is totally equivalent (see the tests), but does
716 * not use a division and therefore is faster.
718 result = *seed * 0xffffffed + 0x7fffffc3;
719 if (result == 0xffffffff || result == 0x7ffffffe) {
720 result = (result + 2) & MAXLONG;
721 } else if (result == 0x7fffffff) {
722 result = 0;
723 } else if ((result & 0x80000000) == 0) {
724 result = result + (~result & 1);
725 } else {
726 result = (result + (result & 1)) & MAXLONG;
727 } /* if */
728 *seed = result;
729 return result;
733 /*************************************************************************
734 * RtlRandom [NTDLL.@]
736 * Generates a random number
738 * PARAMS
739 * seed [O] The seed of the Random function
741 * RETURNS
742 * It returns a random number distributed over [0..MAXLONG-1].
744 ULONG WINAPI RtlRandom (PULONG seed)
746 static ULONG saved_value[128] =
747 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
748 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
749 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
750 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
751 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
752 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
753 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
754 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
755 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
756 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
757 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
758 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
759 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
760 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
761 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
762 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
763 ULONG rand;
764 int pos;
765 ULONG result;
767 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
768 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
769 pos = *seed & 0x7f;
770 result = saved_value[pos];
771 saved_value[pos] = rand;
772 return(result);
776 /*************************************************************************
777 * RtlRandomEx [NTDLL.@]
779 ULONG WINAPI RtlRandomEx( ULONG *seed )
781 WARN( "semi-stub: should use a different algorithm\n" );
782 return RtlRandom( seed );
785 /*************************************************************************
786 * RtlAreAllAccessesGranted [NTDLL.@]
788 * Check if all desired accesses are granted
790 * RETURNS
791 * TRUE: All desired accesses are granted
792 * FALSE: Otherwise
794 BOOLEAN WINAPI RtlAreAllAccessesGranted(
795 ACCESS_MASK GrantedAccess,
796 ACCESS_MASK DesiredAccess)
798 return (GrantedAccess & DesiredAccess) == DesiredAccess;
802 /*************************************************************************
803 * RtlAreAnyAccessesGranted [NTDLL.@]
805 * Check if at least one of the desired accesses is granted
807 * PARAMS
808 * GrantedAccess [I] Access mask of granted accesses
809 * DesiredAccess [I] Access mask of desired accesses
811 * RETURNS
812 * TRUE: At least one of the desired accesses is granted
813 * FALSE: Otherwise
815 BOOLEAN WINAPI RtlAreAnyAccessesGranted(
816 ACCESS_MASK GrantedAccess,
817 ACCESS_MASK DesiredAccess)
819 return (GrantedAccess & DesiredAccess) != 0;
823 /*************************************************************************
824 * RtlMapGenericMask [NTDLL.@]
826 * Determine the nongeneric access rights specified by an access mask
828 * RETURNS
829 * Nothing.
831 void WINAPI RtlMapGenericMask(
832 PACCESS_MASK AccessMask,
833 const GENERIC_MAPPING *GenericMapping)
835 if (*AccessMask & GENERIC_READ) {
836 *AccessMask |= GenericMapping->GenericRead;
837 } /* if */
839 if (*AccessMask & GENERIC_WRITE) {
840 *AccessMask |= GenericMapping->GenericWrite;
841 } /* if */
843 if (*AccessMask & GENERIC_EXECUTE) {
844 *AccessMask |= GenericMapping->GenericExecute;
845 } /* if */
847 if (*AccessMask & GENERIC_ALL) {
848 *AccessMask |= GenericMapping->GenericAll;
849 } /* if */
851 *AccessMask &= 0x0FFFFFFF;
855 /*************************************************************************
856 * RtlCopyLuid [NTDLL.@]
858 * Copy a local unique ID.
860 * PARAMS
861 * LuidDest [O] Destination for the copied Luid
862 * LuidSrc [I] Source Luid to copy to LuidDest
864 * RETURNS
865 * Nothing.
867 void WINAPI RtlCopyLuid (PLUID LuidDest, const LUID *LuidSrc)
869 *LuidDest = *LuidSrc;
873 /*************************************************************************
874 * RtlEqualLuid [NTDLL.@]
876 * Compare two local unique IDs.
878 * PARAMS
879 * Luid1 [I] First Luid to compare to Luid2
880 * Luid2 [I] Second Luid to compare to Luid1
882 * RETURNS
883 * TRUE: The two LUIDs are equal.
884 * FALSE: Otherwise
886 BOOLEAN WINAPI RtlEqualLuid (const LUID *Luid1, const LUID *Luid2)
888 return (Luid1->LowPart == Luid2->LowPart && Luid1->HighPart == Luid2->HighPart);
892 /*************************************************************************
893 * RtlCopyLuidAndAttributesArray [NTDLL.@]
895 * Copy an array of local unique IDs and attributes.
897 * PARAMS
898 * Count [I] Number of Luid/attributes in Src
899 * Src [I] Source Luid/attributes to copy
900 * Dest [O] Destination for copied Luid/attributes
902 * RETURNS
903 * Nothing.
905 * NOTES
906 * Dest must be large enough to hold Src.
908 void WINAPI RtlCopyLuidAndAttributesArray(
909 ULONG Count,
910 const LUID_AND_ATTRIBUTES *Src,
911 PLUID_AND_ATTRIBUTES Dest)
913 ULONG i;
915 for (i = 0; i < Count; i++) Dest[i] = Src[i];
918 static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
920 int base = 10, d;
921 WCHAR c;
922 ULONG cur_value, prev_value = 0;
923 BOOL success = FALSE;
925 if (**str == '.')
927 *str += 1;
928 return FALSE;
931 if ((*str)[0] == '0')
933 if ((*str)[1] == 'x' || (*str)[1] == 'X')
935 *str += 2;
936 if (strict) return FALSE;
937 base = 16;
939 else if ((*str)[1] >= '0' && (*str)[1] <= '9')
941 *str += 1;
942 if (strict) return FALSE;
943 base = 8;
947 for (cur_value = 0; **str; *str += 1)
949 c = **str;
950 if (c >= ARRAY_SIZE(hex_table)) break;
951 d = hex_table[c];
952 if (d == -1 || d >= base) break;
953 cur_value = cur_value * base + d;
954 success = TRUE;
955 if (cur_value < prev_value) return FALSE; /* overflow */
956 prev_value = cur_value;
959 if (success) *value = cur_value;
960 return success;
963 static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
964 const WCHAR **terminator, IN_ADDR *address, USHORT *port)
966 ULONG fields[4];
967 int n = 0;
969 for (;;)
971 if (!parse_ipv4_component(&str, strict, &fields[n]))
972 goto error;
973 n++;
974 if (*str != '.')
975 break;
976 if (n == 4)
977 goto error;
978 str++;
981 if (strict && n < 4)
982 goto error;
984 switch (n)
986 case 4:
987 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFF || fields[3] > 0xFF)
988 goto error;
989 address->S_un.S_un_b.s_b1 = fields[0];
990 address->S_un.S_un_b.s_b2 = fields[1];
991 address->S_un.S_un_b.s_b3 = fields[2];
992 address->S_un.S_un_b.s_b4 = fields[3];
993 break;
994 case 3:
995 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFFFF)
996 goto error;
997 address->S_un.S_un_b.s_b1 = fields[0];
998 address->S_un.S_un_b.s_b2 = fields[1];
999 address->S_un.S_un_b.s_b3 = (fields[2] & 0xFF00) >> 8;
1000 address->S_un.S_un_b.s_b4 = (fields[2] & 0x00FF);
1001 break;
1002 case 2:
1003 if (fields[0] > 0xFF || fields[1] > 0xFFFFFF)
1004 goto error;
1005 address->S_un.S_un_b.s_b1 = fields[0];
1006 address->S_un.S_un_b.s_b2 = (fields[1] & 0xFF0000) >> 16;
1007 address->S_un.S_un_b.s_b3 = (fields[1] & 0x00FF00) >> 8;
1008 address->S_un.S_un_b.s_b4 = (fields[1] & 0x0000FF);
1009 break;
1010 case 1:
1011 address->S_un.S_un_b.s_b1 = (fields[0] & 0xFF000000) >> 24;
1012 address->S_un.S_un_b.s_b2 = (fields[0] & 0x00FF0000) >> 16;
1013 address->S_un.S_un_b.s_b3 = (fields[0] & 0x0000FF00) >> 8;
1014 address->S_un.S_un_b.s_b4 = (fields[0] & 0x000000FF);
1015 break;
1016 default:
1017 goto error;
1020 if (terminator) *terminator = str;
1022 if (*str == ':')
1024 str++;
1025 if (!parse_ipv4_component(&str, FALSE, &fields[0]))
1026 goto error;
1027 if (!fields[0] || fields[0] > 0xFFFF || *str)
1028 goto error;
1029 if (port)
1031 *port = htons(fields[0]);
1032 if (terminator) *terminator = str;
1036 if (!terminator && *str)
1037 return STATUS_INVALID_PARAMETER;
1038 return STATUS_SUCCESS;
1040 error:
1041 if (terminator) *terminator = str;
1042 return STATUS_INVALID_PARAMETER;
1045 /***********************************************************************
1046 * RtlIpv4StringToAddressExW [NTDLL.@]
1048 NTSTATUS WINAPI RtlIpv4StringToAddressExW(const WCHAR *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1050 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, address, port);
1051 if (!str || !address || !port) return STATUS_INVALID_PARAMETER;
1052 return ipv4_string_to_address(str, strict, NULL, address, port);
1055 /***********************************************************************
1056 * RtlIpv4StringToAddressW [NTDLL.@]
1058 NTSTATUS WINAPI RtlIpv4StringToAddressW(const WCHAR *str, BOOLEAN strict, const WCHAR **terminator, IN_ADDR *address)
1060 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, terminator, address);
1061 return ipv4_string_to_address(str, strict, terminator, address, NULL);
1064 /***********************************************************************
1065 * RtlIpv4StringToAddressExA [NTDLL.@]
1067 NTSTATUS WINAPI RtlIpv4StringToAddressExA(const char *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1069 WCHAR wstr[32];
1071 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, address, port);
1073 if (!str || !address || !port)
1074 return STATUS_INVALID_PARAMETER;
1076 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1077 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1078 return ipv4_string_to_address(wstr, strict, NULL, address, port);
1081 /***********************************************************************
1082 * RtlIpv4StringToAddressA [NTDLL.@]
1084 NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const char **terminator, IN_ADDR *address)
1086 WCHAR wstr[32];
1087 const WCHAR *wterminator;
1088 NTSTATUS ret;
1090 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, terminator, address);
1092 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1093 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1094 ret = ipv4_string_to_address(wstr, strict, &wterminator, address, NULL);
1095 if (terminator) *terminator = str + (wterminator - wstr);
1096 return ret;
1099 static BOOL parse_ipv6_component(const WCHAR **str, int base, ULONG *value)
1101 WCHAR *terminator;
1102 if (**str >= ARRAY_SIZE(hex_table) || hex_table[**str] == -1) return FALSE;
1103 *value = min(wcstoul(*str, &terminator, base), 0x7FFFFFFF);
1104 if (*terminator == '0') terminator++; /* "0x" but nothing valid after */
1105 else if (terminator == *str) return FALSE;
1106 *str = terminator;
1107 return TRUE;
1110 static NTSTATUS ipv6_string_to_address(const WCHAR *str, BOOL ex,
1111 const WCHAR **terminator, IN6_ADDR *address, ULONG *scope, USHORT *port)
1113 BOOL expecting_port = FALSE, has_0x = FALSE, too_big = FALSE;
1114 int n_bytes = 0, n_ipv4_bytes = 0, gap = -1;
1115 ULONG ip_component, scope_component = 0, port_component = 0;
1116 const WCHAR *prev_str;
1118 if (str[0] == '[')
1120 if (!ex) goto error;
1121 expecting_port = TRUE;
1122 str++;
1125 if (str[0] == ':')
1127 if (str[1] != ':') goto error;
1128 str++;
1129 address->u.Word[0] = 0;
1132 for (;;)
1134 if (!n_ipv4_bytes && *str == ':')
1136 /* double colon */
1137 if (gap != -1) goto error;
1138 str++;
1139 prev_str = str;
1140 gap = n_bytes;
1141 if (n_bytes == 14 || !parse_ipv6_component(&str, 16, &ip_component)) break;
1142 str = prev_str;
1144 else
1146 prev_str = str;
1149 if (!n_ipv4_bytes && n_bytes <= (gap != -1 ? 10 : 12))
1151 if (parse_ipv6_component(&str, 10, &ip_component) && *str == '.')
1152 n_ipv4_bytes = 1;
1153 str = prev_str;
1156 if (n_ipv4_bytes)
1158 /* IPv4 component */
1159 if (!parse_ipv6_component(&str, 10, &ip_component)) goto error;
1160 if (str - prev_str > 3 || ip_component > 255)
1162 too_big = TRUE;
1164 else
1166 if (*str != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
1167 address->u.Byte[n_bytes] = ip_component;
1168 n_bytes++;
1170 if (n_ipv4_bytes == 4 || *str != '.') break;
1171 n_ipv4_bytes++;
1173 else
1175 /* IPv6 component */
1176 if (!parse_ipv6_component(&str, 16, &ip_component)) goto error;
1177 if (prev_str[0] == '0' && (prev_str[1] == 'x' || prev_str[1] == 'X'))
1179 /* Windows "feature": the last IPv6 component can start with "0x" and be longer than 4 digits */
1180 if (terminator) *terminator = prev_str + 1; /* Windows says that the "x" is the terminator */
1181 if (n_bytes < 14 && gap == -1) return STATUS_INVALID_PARAMETER;
1182 address->u.Word[n_bytes/2] = htons(ip_component);
1183 n_bytes += 2;
1184 has_0x = TRUE;
1185 goto fill_gap;
1187 if (*str != ':' && n_bytes < 14 && gap == -1) goto error;
1188 if (str - prev_str > 4)
1189 too_big = TRUE;
1190 else
1191 address->u.Word[n_bytes/2] = htons(ip_component);
1192 n_bytes += 2;
1193 if (*str != ':' || (gap != -1 && str[1] == ':')) break;
1195 if (n_bytes == (gap != -1 ? 14 : 16)) break;
1196 if (too_big) return STATUS_INVALID_PARAMETER;
1197 str++;
1200 if (terminator) *terminator = str;
1201 if (too_big) return STATUS_INVALID_PARAMETER;
1203 fill_gap:
1204 if (gap == -1)
1206 if (n_bytes < 16) goto error;
1208 else
1210 memmove(address->u.Byte + 16 - (n_bytes - gap), address->u.Byte + gap, n_bytes - gap);
1211 memset(address->u.Byte + gap, 0, 16 - n_bytes);
1214 if (ex)
1216 if (has_0x) goto error;
1218 if (*str == '%')
1220 str++;
1221 if (!parse_ipv4_component(&str, TRUE, &scope_component)) goto error;
1224 if (expecting_port)
1226 if (*str != ']') goto error;
1227 str++;
1228 if (*str == ':')
1230 str++;
1231 if (!parse_ipv4_component(&str, FALSE, &port_component)) goto error;
1232 if (!port_component || port_component > 0xFFFF || *str) goto error;
1233 port_component = htons(port_component);
1238 if (!terminator && *str) return STATUS_INVALID_PARAMETER;
1240 if (scope) *scope = scope_component;
1241 if (port) *port = port_component;
1243 return STATUS_SUCCESS;
1245 error:
1246 if (terminator) *terminator = str;
1247 return STATUS_INVALID_PARAMETER;
1250 /***********************************************************************
1251 * RtlIpv6StringToAddressExW [NTDLL.@]
1253 NTSTATUS NTAPI RtlIpv6StringToAddressExW(const WCHAR *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1255 TRACE("(%s, %p, %p, %p)\n", debugstr_w(str), address, scope, port);
1256 if (!str || !address || !scope || !port) return STATUS_INVALID_PARAMETER;
1257 return ipv6_string_to_address(str, TRUE, NULL, address, scope, port);
1260 /***********************************************************************
1261 * RtlIpv6StringToAddressW [NTDLL.@]
1263 NTSTATUS WINAPI RtlIpv6StringToAddressW(const WCHAR *str, const WCHAR **terminator, IN6_ADDR *address)
1265 TRACE("(%s, %p, %p)\n", debugstr_w(str), terminator, address);
1266 return ipv6_string_to_address(str, FALSE, terminator, address, NULL, NULL);
1269 /***********************************************************************
1270 * RtlIpv6StringToAddressExA [NTDLL.@]
1272 NTSTATUS WINAPI RtlIpv6StringToAddressExA(const char *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1274 WCHAR wstr[128];
1276 TRACE("(%s, %p, %p, %p)\n", debugstr_a(str), address, scope, port);
1278 if (!str || !address || !scope || !port)
1279 return STATUS_INVALID_PARAMETER;
1281 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1282 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1283 return ipv6_string_to_address(wstr, TRUE, NULL, address, scope, port);
1286 /***********************************************************************
1287 * RtlIpv6StringToAddressA [NTDLL.@]
1289 NTSTATUS WINAPI RtlIpv6StringToAddressA(const char *str, const char **terminator, IN6_ADDR *address)
1291 WCHAR wstr[128];
1292 const WCHAR *wterminator = NULL;
1293 NTSTATUS ret;
1295 TRACE("(%s, %p, %p)\n", debugstr_a(str), terminator, address);
1297 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1298 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1299 ret = ipv6_string_to_address(wstr, FALSE, &wterminator, address, NULL, NULL);
1300 if (terminator && wterminator) *terminator = str + (wterminator - wstr);
1301 return ret;
1304 /***********************************************************************
1305 * RtlIpv4AddressToStringExW [NTDLL.@]
1307 * Convert the given ipv4 address and optional the port to a string
1309 * PARAMS
1310 * pin [I] PTR to the ip address to convert (network byte order)
1311 * port [I] optional port to convert (network byte order)
1312 * buffer [O] destination buffer for the result
1313 * psize [IO] PTR to available/used size of the destination buffer
1315 * RETURNS
1316 * Success: STATUS_SUCCESS
1317 * Failure: STATUS_INVALID_PARAMETER
1320 NTSTATUS WINAPI RtlIpv4AddressToStringExW(const IN_ADDR *pin, USHORT port, LPWSTR buffer, PULONG psize)
1322 WCHAR tmp_ip[32];
1323 ULONG needed;
1325 if (!pin || !buffer || !psize)
1326 return STATUS_INVALID_PARAMETER;
1328 TRACE("(%p:0x%lx, %d, %p, %p:%ld)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1330 needed = swprintf(tmp_ip, ARRAY_SIZE(tmp_ip), L"%u.%u.%u.%u",
1331 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1332 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1334 if (port) needed += swprintf(tmp_ip + needed, ARRAY_SIZE(tmp_ip) - needed, L":%u", ntohs(port));
1336 if (*psize > needed) {
1337 *psize = needed + 1;
1338 wcscpy(buffer, tmp_ip);
1339 return STATUS_SUCCESS;
1342 *psize = needed + 1;
1343 return STATUS_INVALID_PARAMETER;
1346 /***********************************************************************
1347 * RtlIpv4AddressToStringExA [NTDLL.@]
1349 * Convert the given ipv4 address and optional the port to a string
1351 * See RtlIpv4AddressToStringExW
1353 NTSTATUS WINAPI RtlIpv4AddressToStringExA(const IN_ADDR *pin, USHORT port, LPSTR buffer, PULONG psize)
1355 CHAR tmp_ip[32];
1356 ULONG needed;
1358 if (!pin || !buffer || !psize)
1359 return STATUS_INVALID_PARAMETER;
1361 TRACE("(%p:0x%lx, %d, %p, %p:%ld)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1363 needed = sprintf(tmp_ip, "%u.%u.%u.%u",
1364 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1365 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1367 if (port) needed += sprintf(tmp_ip + needed, ":%u", ntohs(port));
1369 if (*psize > needed) {
1370 *psize = needed + 1;
1371 strcpy(buffer, tmp_ip);
1372 return STATUS_SUCCESS;
1375 *psize = needed + 1;
1376 return STATUS_INVALID_PARAMETER;
1379 /***********************************************************************
1380 * RtlIpv4AddressToStringW [NTDLL.@]
1382 * Convert the given ipv4 address to a string
1384 * PARAMS
1385 * pin [I] PTR to the ip address to convert (network byte order)
1386 * buffer [O] destination buffer for the result (at least 16 character)
1388 * RETURNS
1389 * PTR to the 0 character at the end of the converted string
1392 WCHAR * WINAPI RtlIpv4AddressToStringW(const IN_ADDR *pin, LPWSTR buffer)
1394 ULONG size = 16;
1396 if (RtlIpv4AddressToStringExW(pin, 0, buffer, &size)) size = 0;
1397 return buffer + size - 1;
1400 /***********************************************************************
1401 * RtlIpv4AddressToStringA [NTDLL.@]
1403 * Convert the given ipv4 address to a string
1405 * See RtlIpv4AddressToStringW
1407 CHAR * WINAPI RtlIpv4AddressToStringA(const IN_ADDR *pin, LPSTR buffer)
1409 ULONG size = 16;
1411 if (RtlIpv4AddressToStringExA(pin, 0, buffer, &size)) size = 0;
1412 return buffer + size - 1;
1415 static BOOL is_ipv4_in_ipv6(const IN6_ADDR *address)
1417 if (address->s6_words[5] == htons(0x5efe) && (address->s6_words[4] & ~htons(0x200)) == 0)
1418 return TRUE;
1419 if (*(UINT64 *)address != 0)
1420 return FALSE;
1421 if (address->s6_words[4] != 0 && address->s6_words[4] != 0xffff)
1422 return FALSE;
1423 if (address->s6_words[4] == 0 && address->s6_words[5] != 0 && address->s6_words[5] != 0xffff)
1424 return FALSE;
1425 if (address->s6_words[4] == 0xffff && address->s6_words[5] != 0)
1426 return FALSE;
1427 if (address->s6_words[6] == 0)
1428 return FALSE;
1429 return TRUE;
1432 /***********************************************************************
1433 * RtlIpv6AddressToStringExA [NTDLL.@]
1435 NTSTATUS WINAPI RtlIpv6AddressToStringExA(const IN6_ADDR *address, ULONG scope, USHORT port, char *str, ULONG *size)
1437 char buffer[64], *p = buffer;
1438 int i, len, gap = -1, gap_len = 1, ipv6_end = 8;
1439 ULONG needed;
1440 NTSTATUS ret;
1442 TRACE("(%p %lu %u %p %p)\n", address, scope, port, str, size);
1444 if (!address || !str || !size)
1445 return STATUS_INVALID_PARAMETER;
1447 if (is_ipv4_in_ipv6(address))
1448 ipv6_end = 6;
1450 for (i = 0; i < ipv6_end; i++)
1452 len = 0;
1453 while (!address->s6_words[i] && i < ipv6_end)
1455 i++;
1456 len++;
1458 if (len > gap_len)
1460 gap = i - len;
1461 gap_len = len;
1465 if (port) p += sprintf(p, "[");
1467 i = 0;
1468 while (i < ipv6_end)
1470 if (i == gap)
1472 p += sprintf(p, ":");
1473 i += gap_len;
1474 if (i == ipv6_end) p += sprintf(p, ":");
1475 continue;
1477 if (i > 0) p += sprintf(p, ":");
1478 p += sprintf(p, "%x", ntohs(address->s6_words[i]));
1479 i++;
1482 if (ipv6_end == 6)
1484 if (p[-1] != ':') p += sprintf(p, ":");
1485 p = RtlIpv4AddressToStringA((IN_ADDR *)(address->s6_words + 6), p);
1488 if (scope) p += sprintf(p, "%%%lu", scope);
1490 if (port) p += sprintf(p, "]:%u", ntohs(port));
1492 needed = p - buffer + 1;
1494 if (*size >= needed)
1496 strcpy(str, buffer);
1497 ret = STATUS_SUCCESS;
1499 else
1501 ret = STATUS_INVALID_PARAMETER;
1504 *size = needed;
1505 return ret;
1508 /***********************************************************************
1509 * RtlIpv6AddressToStringA [NTDLL.@]
1511 char * WINAPI RtlIpv6AddressToStringA(const IN6_ADDR *address, char *str)
1513 ULONG size = 46;
1514 if (!address || !str) return str - 1;
1515 str[45] = 0; /* this byte is set even though the string is always shorter */
1516 RtlIpv6AddressToStringExA(address, 0, 0, str, &size);
1517 return str + size - 1;
1520 /***********************************************************************
1521 * RtlIpv6AddressToStringExW [NTDLL.@]
1523 NTSTATUS WINAPI RtlIpv6AddressToStringExW(const IN6_ADDR *address, ULONG scope, USHORT port, WCHAR *str, ULONG *size)
1525 char cstr[64];
1526 NTSTATUS ret = RtlIpv6AddressToStringExA(address, scope, port, cstr, size);
1527 if (ret == STATUS_SUCCESS) RtlMultiByteToUnicodeN(str, *size * sizeof(WCHAR), NULL, cstr, *size);
1528 return ret;
1531 /***********************************************************************
1532 * RtlIpv6AddressToStringW [NTDLL.@]
1534 WCHAR * WINAPI RtlIpv6AddressToStringW(const IN6_ADDR *address, WCHAR *str)
1536 ULONG size = 46;
1537 if (!address || !str) return str;
1538 str[45] = 0; /* this word is set even though the string is always shorter */
1539 if (RtlIpv6AddressToStringExW(address, 0, 0, str, &size) != STATUS_SUCCESS)
1540 return str;
1541 return str + size - 1;
1544 /***********************************************************************
1545 * get_pointer_obfuscator (internal)
1547 static DWORD_PTR get_pointer_obfuscator( void )
1549 static DWORD_PTR pointer_obfuscator;
1551 if (!pointer_obfuscator)
1553 ULONG seed = NtGetTickCount();
1554 ULONG_PTR rand;
1556 /* generate a random value for the obfuscator */
1557 rand = RtlUniform( &seed );
1559 /* handle 64bit pointers */
1560 rand ^= (ULONG_PTR)RtlUniform( &seed ) << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1562 /* set the high bits so dereferencing obfuscated pointers will (usually) crash */
1563 rand |= (ULONG_PTR)0xc0000000 << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1565 InterlockedCompareExchangePointer( (void**) &pointer_obfuscator, (void*) rand, NULL );
1568 return pointer_obfuscator;
1571 /*************************************************************************
1572 * RtlEncodePointer [NTDLL.@]
1574 PVOID WINAPI RtlEncodePointer( PVOID ptr )
1576 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1577 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1580 PVOID WINAPI RtlDecodePointer( PVOID ptr )
1582 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1583 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1586 /*************************************************************************
1587 * RtlInitializeSListHead [NTDLL.@]
1589 VOID WINAPI RtlInitializeSListHead(PSLIST_HEADER list)
1591 #ifdef _WIN64
1592 list->s.Alignment = list->s.Region = 0;
1593 list->Header16.HeaderType = 1; /* we use the 16-byte header */
1594 #else
1595 list->Alignment = 0;
1596 #endif
1599 /*************************************************************************
1600 * RtlQueryDepthSList [NTDLL.@]
1602 WORD WINAPI RtlQueryDepthSList(PSLIST_HEADER list)
1604 #ifdef _WIN64
1605 return list->Header16.Depth;
1606 #else
1607 return list->s.Depth;
1608 #endif
1611 /*************************************************************************
1612 * RtlFirstEntrySList [NTDLL.@]
1614 PSLIST_ENTRY WINAPI RtlFirstEntrySList(const SLIST_HEADER* list)
1616 #ifdef _WIN64
1617 return (SLIST_ENTRY *)((ULONG_PTR)list->Header16.NextEntry << 4);
1618 #else
1619 return list->s.Next.Next;
1620 #endif
1623 /*************************************************************************
1624 * RtlInterlockedFlushSList [NTDLL.@]
1626 PSLIST_ENTRY WINAPI RtlInterlockedFlushSList(PSLIST_HEADER list)
1628 SLIST_HEADER old, new;
1630 #ifdef _WIN64
1631 if (!list->Header16.NextEntry) return NULL;
1632 new.s.Alignment = new.s.Region = 0;
1633 new.Header16.HeaderType = 1; /* we use the 16-byte header */
1636 old = *list;
1637 new.Header16.Sequence = old.Header16.Sequence + 1;
1638 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1639 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1640 #else
1641 if (!list->s.Next.Next) return NULL;
1642 new.Alignment = 0;
1645 old = *list;
1646 new.s.Sequence = old.s.Sequence + 1;
1647 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1648 old.Alignment) != old.Alignment);
1649 return old.s.Next.Next;
1650 #endif
1653 /*************************************************************************
1654 * RtlInterlockedPushEntrySList [NTDLL.@]
1656 PSLIST_ENTRY WINAPI RtlInterlockedPushEntrySList(PSLIST_HEADER list, PSLIST_ENTRY entry)
1658 SLIST_HEADER old, new;
1660 #ifdef _WIN64
1661 new.Header16.NextEntry = (ULONG_PTR)entry >> 4;
1664 old = *list;
1665 entry->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1666 new.Header16.Depth = old.Header16.Depth + 1;
1667 new.Header16.Sequence = old.Header16.Sequence + 1;
1668 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1669 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1670 #else
1671 new.s.Next.Next = entry;
1674 old = *list;
1675 entry->Next = old.s.Next.Next;
1676 new.s.Depth = old.s.Depth + 1;
1677 new.s.Sequence = old.s.Sequence + 1;
1678 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1679 old.Alignment) != old.Alignment);
1680 return old.s.Next.Next;
1681 #endif
1684 /*************************************************************************
1685 * RtlInterlockedPopEntrySList [NTDLL.@]
1687 PSLIST_ENTRY WINAPI RtlInterlockedPopEntrySList(PSLIST_HEADER list)
1689 SLIST_HEADER old, new;
1690 PSLIST_ENTRY entry;
1692 #ifdef _WIN64
1695 old = *list;
1696 if (!(entry = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4))) return NULL;
1697 /* entry could be deleted by another thread */
1698 __TRY
1700 new.Header16.NextEntry = (ULONG_PTR)entry->Next >> 4;
1701 new.Header16.Depth = old.Header16.Depth - 1;
1702 new.Header16.Sequence = old.Header16.Sequence + 1;
1704 __EXCEPT_PAGE_FAULT
1707 __ENDTRY
1708 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1709 #else
1712 old = *list;
1713 if (!(entry = old.s.Next.Next)) return NULL;
1714 /* entry could be deleted by another thread */
1715 __TRY
1717 new.s.Next.Next = entry->Next;
1718 new.s.Depth = old.s.Depth - 1;
1719 new.s.Sequence = old.s.Sequence + 1;
1721 __EXCEPT_PAGE_FAULT
1724 __ENDTRY
1725 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1726 old.Alignment) != old.Alignment);
1727 #endif
1728 return entry;
1731 /*************************************************************************
1732 * RtlInterlockedPushListSListEx [NTDLL.@]
1734 PSLIST_ENTRY WINAPI RtlInterlockedPushListSListEx(PSLIST_HEADER list, PSLIST_ENTRY first,
1735 PSLIST_ENTRY last, ULONG count)
1737 SLIST_HEADER old, new;
1739 #ifdef _WIN64
1740 new.Header16.NextEntry = (ULONG_PTR)first >> 4;
1743 old = *list;
1744 new.Header16.Depth = old.Header16.Depth + count;
1745 new.Header16.Sequence = old.Header16.Sequence + 1;
1746 last->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1747 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1748 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1749 #else
1750 new.s.Next.Next = first;
1753 old = *list;
1754 new.s.Depth = old.s.Depth + count;
1755 new.s.Sequence = old.s.Sequence + 1;
1756 last->Next = old.s.Next.Next;
1757 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1758 old.Alignment) != old.Alignment);
1759 return old.s.Next.Next;
1760 #endif
1763 /*************************************************************************
1764 * RtlInterlockedPushListSList [NTDLL.@]
1766 DEFINE_FASTCALL_WRAPPER(RtlInterlockedPushListSList, 16)
1767 PSLIST_ENTRY FASTCALL RtlInterlockedPushListSList(PSLIST_HEADER list, PSLIST_ENTRY first,
1768 PSLIST_ENTRY last, ULONG count)
1770 return RtlInterlockedPushListSListEx(list, first, last, count);
1773 /******************************************************************************
1774 * RtlGetCompressionWorkSpaceSize [NTDLL.@]
1776 NTSTATUS WINAPI RtlGetCompressionWorkSpaceSize(USHORT format, PULONG compress_workspace,
1777 PULONG decompress_workspace)
1779 FIXME("0x%04x, %p, %p: semi-stub\n", format, compress_workspace, decompress_workspace);
1781 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1783 case COMPRESSION_FORMAT_LZNT1:
1784 if (compress_workspace)
1786 /* FIXME: The current implementation of RtlCompressBuffer does not use a
1787 * workspace buffer, but Windows applications might expect a nonzero value. */
1788 *compress_workspace = 16;
1790 if (decompress_workspace)
1791 *decompress_workspace = 0x1000;
1792 return STATUS_SUCCESS;
1794 case COMPRESSION_FORMAT_NONE:
1795 case COMPRESSION_FORMAT_DEFAULT:
1796 return STATUS_INVALID_PARAMETER;
1798 default:
1799 FIXME("format %u not implemented\n", format);
1800 return STATUS_UNSUPPORTED_COMPRESSION;
1804 /* compress data using LZNT1, currently only a stub */
1805 static NTSTATUS lznt1_compress(UCHAR *src, ULONG src_size, UCHAR *dst, ULONG dst_size,
1806 ULONG chunk_size, ULONG *final_size, UCHAR *workspace)
1808 UCHAR *src_cur = src, *src_end = src + src_size;
1809 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1810 ULONG block_size;
1812 while (src_cur < src_end)
1814 /* determine size of current chunk */
1815 block_size = min(0x1000, src_end - src_cur);
1816 if (dst_cur + sizeof(WORD) + block_size > dst_end)
1817 return STATUS_BUFFER_TOO_SMALL;
1819 /* write (uncompressed) chunk header */
1820 *(WORD *)dst_cur = 0x3000 | (block_size - 1);
1821 dst_cur += sizeof(WORD);
1823 /* write chunk content */
1824 memcpy(dst_cur, src_cur, block_size);
1825 dst_cur += block_size;
1826 src_cur += block_size;
1829 if (final_size)
1830 *final_size = dst_cur - dst;
1832 return STATUS_SUCCESS;
1835 /******************************************************************************
1836 * RtlCompressBuffer [NTDLL.@]
1838 NTSTATUS WINAPI RtlCompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
1839 PUCHAR compressed, ULONG compressed_size, ULONG chunk_size,
1840 PULONG final_size, PVOID workspace)
1842 FIXME("0x%04x, %p, %lu, %p, %lu, %lu, %p, %p: semi-stub\n", format, uncompressed,
1843 uncompressed_size, compressed, compressed_size, chunk_size, final_size, workspace);
1845 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1847 case COMPRESSION_FORMAT_LZNT1:
1848 return lznt1_compress(uncompressed, uncompressed_size, compressed,
1849 compressed_size, chunk_size, final_size, workspace);
1851 case COMPRESSION_FORMAT_NONE:
1852 case COMPRESSION_FORMAT_DEFAULT:
1853 return STATUS_INVALID_PARAMETER;
1855 default:
1856 FIXME("format %u not implemented\n", format);
1857 return STATUS_UNSUPPORTED_COMPRESSION;
1861 /* decompress a single LZNT1 chunk */
1862 static UCHAR *lznt1_decompress_chunk(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size)
1864 UCHAR *src_cur = src, *src_end = src + src_size;
1865 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1866 ULONG displacement_bits, length_bits;
1867 ULONG code_displacement, code_length;
1868 WORD flags, code;
1870 while (src_cur < src_end && dst_cur < dst_end)
1872 flags = 0x8000 | *src_cur++;
1873 while ((flags & 0xff00) && src_cur < src_end)
1875 if (flags & 1)
1877 /* backwards reference */
1878 if (src_cur + sizeof(WORD) > src_end)
1879 return NULL;
1881 code = *(WORD *)src_cur;
1882 src_cur += sizeof(WORD);
1884 /* find length / displacement bits */
1885 for (displacement_bits = 12; displacement_bits > 4; displacement_bits--)
1886 if ((1 << (displacement_bits - 1)) < dst_cur - dst) break;
1888 length_bits = 16 - displacement_bits;
1889 code_length = (code & ((1 << length_bits) - 1)) + 3;
1890 code_displacement = (code >> length_bits) + 1;
1892 if (dst_cur < dst + code_displacement)
1893 return NULL;
1895 /* copy bytes of chunk - we can't use memcpy() since source and dest can
1896 * be overlapping, and the same bytes can be repeated over and over again */
1897 while (code_length--)
1899 if (dst_cur >= dst_end) return dst_cur;
1900 *dst_cur = *(dst_cur - code_displacement);
1901 dst_cur++;
1904 else
1906 /* uncompressed data */
1907 if (dst_cur >= dst_end) return dst_cur;
1908 *dst_cur++ = *src_cur++;
1910 flags >>= 1;
1914 return dst_cur;
1917 /* decompress data encoded with LZNT1 */
1918 static NTSTATUS lznt1_decompress(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size,
1919 ULONG offset, ULONG *final_size, UCHAR *workspace)
1921 UCHAR *src_cur = src, *src_end = src + src_size;
1922 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1923 ULONG chunk_size, block_size;
1924 WORD chunk_header;
1925 UCHAR *ptr;
1927 if (src_cur + sizeof(WORD) > src_end)
1928 return STATUS_BAD_COMPRESSION_BUFFER;
1930 /* skip over chunks with a distance >= 0x1000 to the destination offset */
1931 while (offset >= 0x1000 && src_cur + sizeof(WORD) <= src_end)
1933 chunk_header = *(WORD *)src_cur;
1934 src_cur += sizeof(WORD);
1935 if (!chunk_header) goto out;
1936 chunk_size = (chunk_header & 0xfff) + 1;
1938 if (src_cur + chunk_size > src_end)
1939 return STATUS_BAD_COMPRESSION_BUFFER;
1941 src_cur += chunk_size;
1942 offset -= 0x1000;
1945 /* handle partially included chunk */
1946 if (offset && src_cur + sizeof(WORD) <= src_end)
1948 chunk_header = *(WORD *)src_cur;
1949 src_cur += sizeof(WORD);
1950 if (!chunk_header) goto out;
1951 chunk_size = (chunk_header & 0xfff) + 1;
1953 if (src_cur + chunk_size > src_end)
1954 return STATUS_BAD_COMPRESSION_BUFFER;
1956 if (dst_cur >= dst_end)
1957 goto out;
1959 if (chunk_header & 0x8000)
1961 /* compressed chunk */
1962 if (!workspace) return STATUS_ACCESS_VIOLATION;
1963 ptr = lznt1_decompress_chunk(workspace, 0x1000, src_cur, chunk_size);
1964 if (!ptr) return STATUS_BAD_COMPRESSION_BUFFER;
1965 if (ptr - workspace > offset)
1967 block_size = min((ptr - workspace) - offset, dst_end - dst_cur);
1968 memcpy(dst_cur, workspace + offset, block_size);
1969 dst_cur += block_size;
1972 else
1974 /* uncompressed chunk */
1975 if (chunk_size > offset)
1977 block_size = min(chunk_size - offset, dst_end - dst_cur);
1978 memcpy(dst_cur, src_cur + offset, block_size);
1979 dst_cur += block_size;
1983 src_cur += chunk_size;
1986 /* handle remaining chunks */
1987 while (src_cur + sizeof(WORD) <= src_end)
1989 chunk_header = *(WORD *)src_cur;
1990 src_cur += sizeof(WORD);
1991 if (!chunk_header) goto out;
1992 chunk_size = (chunk_header & 0xfff) + 1;
1994 if (src_cur + chunk_size > src_end)
1995 return STATUS_BAD_COMPRESSION_BUFFER;
1997 /* fill space with padding when the previous chunk was decompressed
1998 * to less than 4096 bytes. no padding is needed for the last chunk
1999 * or when the next chunk is truncated */
2000 block_size = ((dst_cur - dst) + offset) & 0xfff;
2001 if (block_size)
2003 block_size = 0x1000 - block_size;
2004 if (dst_cur + block_size >= dst_end)
2005 goto out;
2006 memset(dst_cur, 0, block_size);
2007 dst_cur += block_size;
2010 if (dst_cur >= dst_end)
2011 goto out;
2013 if (chunk_header & 0x8000)
2015 /* compressed chunk */
2016 dst_cur = lznt1_decompress_chunk(dst_cur, dst_end - dst_cur, src_cur, chunk_size);
2017 if (!dst_cur) return STATUS_BAD_COMPRESSION_BUFFER;
2019 else
2021 /* uncompressed chunk */
2022 block_size = min(chunk_size, dst_end - dst_cur);
2023 memcpy(dst_cur, src_cur, block_size);
2024 dst_cur += block_size;
2027 src_cur += chunk_size;
2030 out:
2031 if (final_size)
2032 *final_size = dst_cur - dst;
2034 return STATUS_SUCCESS;
2038 /******************************************************************************
2039 * RtlDecompressFragment [NTDLL.@]
2041 NTSTATUS WINAPI RtlDecompressFragment(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2042 PUCHAR compressed, ULONG compressed_size, ULONG offset,
2043 PULONG final_size, PVOID workspace)
2045 TRACE("0x%04x, %p, %lu, %p, %lu, %lu, %p, %p\n", format, uncompressed,
2046 uncompressed_size, compressed, compressed_size, offset, final_size, workspace);
2048 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
2050 case COMPRESSION_FORMAT_LZNT1:
2051 return lznt1_decompress(uncompressed, uncompressed_size, compressed,
2052 compressed_size, offset, final_size, workspace);
2054 case COMPRESSION_FORMAT_NONE:
2055 case COMPRESSION_FORMAT_DEFAULT:
2056 return STATUS_INVALID_PARAMETER;
2058 default:
2059 FIXME("format %u not implemented\n", format);
2060 return STATUS_UNSUPPORTED_COMPRESSION;
2065 /******************************************************************************
2066 * RtlDecompressBuffer [NTDLL.@]
2068 NTSTATUS WINAPI RtlDecompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2069 PUCHAR compressed, ULONG compressed_size, PULONG final_size)
2071 TRACE("0x%04x, %p, %lu, %p, %lu, %p\n", format, uncompressed,
2072 uncompressed_size, compressed, compressed_size, final_size);
2074 return RtlDecompressFragment(format, uncompressed, uncompressed_size,
2075 compressed, compressed_size, 0, final_size, NULL);
2078 /***********************************************************************
2079 * RtlSetThreadErrorMode [NTDLL.@]
2081 * Set the thread local error mode.
2083 * PARAMS
2084 * mode [I] The new error mode
2085 * oldmode [O] Destination of the old error mode (may be NULL)
2087 * RETURNS
2088 * Success: STATUS_SUCCESS
2089 * Failure: STATUS_INVALID_PARAMETER_1
2091 NTSTATUS WINAPI RtlSetThreadErrorMode( DWORD mode, LPDWORD oldmode )
2093 if (mode & ~0x70)
2094 return STATUS_INVALID_PARAMETER_1;
2096 if (oldmode)
2097 *oldmode = NtCurrentTeb()->HardErrorDisabled;
2099 NtCurrentTeb()->HardErrorDisabled = mode;
2100 return STATUS_SUCCESS;
2103 /***********************************************************************
2104 * RtlGetThreadErrorMode [NTDLL.@]
2106 * Get the thread local error mode.
2108 * PARAMS
2109 * None.
2111 * RETURNS
2112 * The current thread local error mode.
2114 DWORD WINAPI RtlGetThreadErrorMode( void )
2116 return NtCurrentTeb()->HardErrorDisabled;
2119 /******************************************************************************
2120 * RtlGetCurrentTransaction [NTDLL.@]
2122 HANDLE WINAPI RtlGetCurrentTransaction(void)
2124 FIXME("() :stub\n");
2125 return NULL;
2128 /******************************************************************************
2129 * RtlSetCurrentTransaction [NTDLL.@]
2131 BOOL WINAPI RtlSetCurrentTransaction(HANDLE new_transaction)
2133 FIXME("(%p) :stub\n", new_transaction);
2134 return FALSE;
2137 /**********************************************************************
2138 * RtlGetCurrentProcessorNumberEx [NTDLL.@]
2140 void WINAPI RtlGetCurrentProcessorNumberEx(PROCESSOR_NUMBER *processor)
2142 FIXME("(%p) :semi-stub\n", processor);
2143 processor->Group = 0;
2144 processor->Number = NtGetCurrentProcessorNumber();
2145 processor->Reserved = 0;
2148 /***********************************************************************
2149 * RtlIsProcessorFeaturePresent [NTDLL.@]
2151 BOOLEAN WINAPI RtlIsProcessorFeaturePresent( UINT feature )
2153 return feature < PROCESSOR_FEATURE_MAX && user_shared_data->ProcessorFeatures[feature];
2156 /***********************************************************************
2157 * RtlInitializeGenericTableAvl (NTDLL.@)
2159 void WINAPI RtlInitializeGenericTableAvl(PRTL_AVL_TABLE table, PRTL_AVL_COMPARE_ROUTINE compare,
2160 PRTL_AVL_ALLOCATE_ROUTINE allocate, PRTL_AVL_FREE_ROUTINE free, void *context)
2162 FIXME("%p %p %p %p %p: stub\n", table, compare, allocate, free, context);
2165 /***********************************************************************
2166 * RtlInsertElementGenericTableAvl (NTDLL.@)
2168 void WINAPI RtlInsertElementGenericTableAvl(PRTL_AVL_TABLE table, void *buffer, ULONG size, BOOL *element)
2170 FIXME("%p %p %lu %p: stub\n", table, buffer, size, element);
2173 /*********************************************************************
2174 * RtlQueryPackageIdentity [NTDLL.@]
2176 NTSTATUS WINAPI RtlQueryPackageIdentity(HANDLE token, WCHAR *fullname, SIZE_T *fullname_size,
2177 WCHAR *appid, SIZE_T *appid_size, BOOLEAN *packaged)
2179 FIXME("(%p, %p, %p, %p, %p, %p): stub\n", token, fullname, fullname_size, appid, appid_size, packaged);
2180 return STATUS_NOT_FOUND;
2183 /*********************************************************************
2184 * RtlQueryProcessPlaceholderCompatibilityMode [NTDLL.@]
2186 char WINAPI RtlQueryProcessPlaceholderCompatibilityMode(void)
2188 FIXME("stub\n");
2189 return PHCM_APPLICATION_DEFAULT;