ntdll: Implement RtlIpv6AddressToString(Ex)[AW].
[wine.git] / dlls / ntdll / rtl.c
blobbf91423eba953b5cc4b68a9cc507aab9654c39a5
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 "config.h"
27 #include "wine/port.h"
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36 #include "ntstatus.h"
37 #define NONAMELESSUNION
38 #define NONAMELESSSTRUCT
39 #define WIN32_NO_STATUS
40 #define USE_WS_PREFIX
41 #include "windef.h"
42 #include "winternl.h"
43 #include "wine/debug.h"
44 #include "wine/exception.h"
45 #include "ntdll_misc.h"
46 #include "inaddr.h"
47 #include "in6addr.h"
48 #include "ddk/ntddk.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
52 /* CRC polynomial 0xedb88320 */
53 static const DWORD CRC_table[256] =
55 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
56 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
57 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
58 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
59 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
60 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
61 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
62 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
63 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
64 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
65 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
66 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
67 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
68 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
69 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
70 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
71 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
72 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
73 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
74 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
75 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
76 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
77 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
78 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
79 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
80 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
81 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
82 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
83 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
84 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
85 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
86 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
87 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
88 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
89 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
90 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
91 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
92 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
93 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
94 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
95 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
96 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
97 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
100 static const int hex_table[] = {
101 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
102 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
103 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2F */
104 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
105 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4F */
106 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
107 -1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
110 #if defined(_WIN64) && !defined(_MSC_VER)
111 static inline unsigned char _InterlockedCompareExchange128(__int64 *dest, __int64 xchg_high, __int64 xchg_low, __int64 *compare)
113 unsigned char ret;
114 #ifdef __x86_64__
115 __asm__ __volatile__( "lock cmpxchg16b %0; setz %b2"
116 : "=m" (dest[0]), "=m" (dest[1]), "=r" (ret),
117 "=a" (compare[0]), "=d" (compare[1])
118 : "m" (dest[0]), "m" (dest[1]), "3" (compare[0]), "4" (compare[1]),
119 "c" (xchg_high), "b" (xchg_low) );
120 #else
121 ret = __sync_bool_compare_and_swap( (__int128 *)dest, *(__int128 *)compare,
122 ((__int128)xchg_high << 64) | xchg_low );
123 #endif
124 return ret;
126 #endif
129 * resource functions
132 /***********************************************************************
133 * RtlInitializeResource (NTDLL.@)
135 * xxxResource() functions implement multiple-reader-single-writer lock.
136 * The code is based on information published in WDJ January 1999 issue.
138 void WINAPI RtlInitializeResource(LPRTL_RWLOCK rwl)
140 if( rwl )
142 rwl->iNumberActive = 0;
143 rwl->uExclusiveWaiters = 0;
144 rwl->uSharedWaiters = 0;
145 rwl->hOwningThreadId = 0;
146 rwl->dwTimeoutBoost = 0; /* no info on this one, default value is 0 */
147 RtlInitializeCriticalSection( &rwl->rtlCS );
148 rwl->rtlCS.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RTL_RWLOCK.rtlCS");
149 NtCreateSemaphore( &rwl->hExclusiveReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
150 NtCreateSemaphore( &rwl->hSharedReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
155 /***********************************************************************
156 * RtlDeleteResource (NTDLL.@)
158 void WINAPI RtlDeleteResource(LPRTL_RWLOCK rwl)
160 if( rwl )
162 RtlEnterCriticalSection( &rwl->rtlCS );
163 if( rwl->iNumberActive || rwl->uExclusiveWaiters || rwl->uSharedWaiters )
164 ERR("Deleting active MRSW lock (%p), expect failure\n", rwl );
165 rwl->hOwningThreadId = 0;
166 rwl->uExclusiveWaiters = rwl->uSharedWaiters = 0;
167 rwl->iNumberActive = 0;
168 NtClose( rwl->hExclusiveReleaseSemaphore );
169 NtClose( rwl->hSharedReleaseSemaphore );
170 RtlLeaveCriticalSection( &rwl->rtlCS );
171 rwl->rtlCS.DebugInfo->Spare[0] = 0;
172 RtlDeleteCriticalSection( &rwl->rtlCS );
177 /***********************************************************************
178 * RtlAcquireResourceExclusive (NTDLL.@)
180 BYTE WINAPI RtlAcquireResourceExclusive(LPRTL_RWLOCK rwl, BYTE fWait)
182 BYTE retVal = 0;
183 if( !rwl ) return 0;
185 start:
186 RtlEnterCriticalSection( &rwl->rtlCS );
187 if( rwl->iNumberActive == 0 ) /* lock is free */
189 rwl->iNumberActive = -1;
190 retVal = 1;
192 else if( rwl->iNumberActive < 0 ) /* exclusive lock in progress */
194 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
196 retVal = 1;
197 rwl->iNumberActive--;
198 goto done;
200 wait:
201 if( fWait )
203 NTSTATUS status;
205 rwl->uExclusiveWaiters++;
207 RtlLeaveCriticalSection( &rwl->rtlCS );
208 status = NtWaitForSingleObject( rwl->hExclusiveReleaseSemaphore, FALSE, NULL );
209 if( HIWORD(status) )
210 goto done;
211 goto start; /* restart the acquisition to avoid deadlocks */
214 else /* one or more shared locks are in progress */
215 if( fWait )
216 goto wait;
218 if( retVal == 1 )
219 rwl->hOwningThreadId = ULongToHandle(GetCurrentThreadId());
220 done:
221 RtlLeaveCriticalSection( &rwl->rtlCS );
222 return retVal;
225 /***********************************************************************
226 * RtlAcquireResourceShared (NTDLL.@)
228 BYTE WINAPI RtlAcquireResourceShared(LPRTL_RWLOCK rwl, BYTE fWait)
230 NTSTATUS status = STATUS_UNSUCCESSFUL;
231 BYTE retVal = 0;
232 if( !rwl ) return 0;
234 start:
235 RtlEnterCriticalSection( &rwl->rtlCS );
236 if( rwl->iNumberActive < 0 )
238 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
240 rwl->iNumberActive--;
241 retVal = 1;
242 goto done;
245 if( fWait )
247 rwl->uSharedWaiters++;
248 RtlLeaveCriticalSection( &rwl->rtlCS );
249 status = NtWaitForSingleObject( rwl->hSharedReleaseSemaphore, FALSE, NULL );
250 if( HIWORD(status) )
251 goto done;
252 goto start;
255 else
257 if( status != STATUS_WAIT_0 ) /* otherwise RtlReleaseResource() has already done it */
258 rwl->iNumberActive++;
259 retVal = 1;
261 done:
262 RtlLeaveCriticalSection( &rwl->rtlCS );
263 return retVal;
267 /***********************************************************************
268 * RtlReleaseResource (NTDLL.@)
270 void WINAPI RtlReleaseResource(LPRTL_RWLOCK rwl)
272 RtlEnterCriticalSection( &rwl->rtlCS );
274 if( rwl->iNumberActive > 0 ) /* have one or more readers */
276 if( --rwl->iNumberActive == 0 )
278 if( rwl->uExclusiveWaiters )
280 wake_exclusive:
281 rwl->uExclusiveWaiters--;
282 NtReleaseSemaphore( rwl->hExclusiveReleaseSemaphore, 1, NULL );
286 else
287 if( rwl->iNumberActive < 0 ) /* have a writer, possibly recursive */
289 if( ++rwl->iNumberActive == 0 )
291 rwl->hOwningThreadId = 0;
292 if( rwl->uExclusiveWaiters )
293 goto wake_exclusive;
294 else
295 if( rwl->uSharedWaiters )
297 UINT n = rwl->uSharedWaiters;
298 rwl->iNumberActive = rwl->uSharedWaiters; /* prevent new writers from joining until
299 * all queued readers have done their thing */
300 rwl->uSharedWaiters = 0;
301 NtReleaseSemaphore( rwl->hSharedReleaseSemaphore, n, NULL );
305 RtlLeaveCriticalSection( &rwl->rtlCS );
309 /***********************************************************************
310 * RtlDumpResource (NTDLL.@)
312 void WINAPI RtlDumpResource(LPRTL_RWLOCK rwl)
314 if( rwl )
316 MESSAGE("RtlDumpResource(%p):\n\tactive count = %i\n\twaiting readers = %i\n\twaiting writers = %i\n",
317 rwl, rwl->iNumberActive, rwl->uSharedWaiters, rwl->uExclusiveWaiters );
318 if( rwl->iNumberActive )
319 MESSAGE("\towner thread = %p\n", rwl->hOwningThreadId );
324 * misc functions
327 /******************************************************************************
328 * DbgPrint [NTDLL.@]
330 NTSTATUS WINAPIV DbgPrint(LPCSTR fmt, ...)
332 char buf[512];
333 __ms_va_list args;
335 __ms_va_start(args, fmt);
336 NTDLL__vsnprintf(buf, sizeof(buf), fmt, args);
337 __ms_va_end(args);
339 MESSAGE("DbgPrint says: %s",buf);
340 /* hmm, raise exception? */
341 return STATUS_SUCCESS;
345 /******************************************************************************
346 * DbgPrintEx [NTDLL.@]
348 NTSTATUS WINAPIV DbgPrintEx(ULONG iComponentId, ULONG Level, LPCSTR fmt, ...)
350 NTSTATUS ret;
351 __ms_va_list args;
353 __ms_va_start(args, fmt);
354 ret = vDbgPrintEx(iComponentId, Level, fmt, args);
355 __ms_va_end(args);
356 return ret;
359 /******************************************************************************
360 * vDbgPrintEx [NTDLL.@]
362 NTSTATUS WINAPI vDbgPrintEx( ULONG id, ULONG level, LPCSTR fmt, __ms_va_list args )
364 return vDbgPrintExWithPrefix( "", id, level, fmt, args );
367 /******************************************************************************
368 * vDbgPrintExWithPrefix [NTDLL.@]
370 NTSTATUS WINAPI vDbgPrintExWithPrefix( LPCSTR prefix, ULONG id, ULONG level, LPCSTR fmt, __ms_va_list args )
372 char buf[1024];
374 NTDLL__vsnprintf(buf, sizeof(buf), fmt, args);
376 switch (level & DPFLTR_MASK)
378 case DPFLTR_ERROR_LEVEL: ERR("%s%x: %s", prefix, id, buf); break;
379 case DPFLTR_WARNING_LEVEL: WARN("%s%x: %s", prefix, id, buf); break;
380 case DPFLTR_TRACE_LEVEL:
381 case DPFLTR_INFO_LEVEL:
382 default: TRACE("%s%x: %s", prefix, id, buf); break;
384 return STATUS_SUCCESS;
387 /******************************************************************************
388 * RtlAcquirePebLock [NTDLL.@]
390 VOID WINAPI RtlAcquirePebLock(void)
392 RtlEnterCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
395 /******************************************************************************
396 * RtlReleasePebLock [NTDLL.@]
398 VOID WINAPI RtlReleasePebLock(void)
400 RtlLeaveCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
403 /******************************************************************************
404 * RtlNewSecurityObject [NTDLL.@]
406 NTSTATUS WINAPI
407 RtlNewSecurityObject( PSECURITY_DESCRIPTOR ParentDescriptor,
408 PSECURITY_DESCRIPTOR CreatorDescriptor,
409 PSECURITY_DESCRIPTOR *NewDescriptor,
410 BOOLEAN IsDirectoryObject,
411 HANDLE Token,
412 PGENERIC_MAPPING GenericMapping )
414 FIXME("(%p %p %p %d %p %p) stub!\n", ParentDescriptor, CreatorDescriptor,
415 NewDescriptor, IsDirectoryObject, Token, GenericMapping);
416 return STATUS_NOT_IMPLEMENTED;
419 /******************************************************************************
420 * RtlDeleteSecurityObject [NTDLL.@]
422 NTSTATUS WINAPI
423 RtlDeleteSecurityObject( PSECURITY_DESCRIPTOR *ObjectDescriptor )
425 FIXME("(%p) stub!\n", ObjectDescriptor);
426 return STATUS_NOT_IMPLEMENTED;
429 /******************************************************************************
430 * RtlInitializeGenericTable [NTDLL.@]
432 PVOID WINAPI RtlInitializeGenericTable(PVOID pTable, PVOID arg2, PVOID arg3, PVOID arg4, PVOID arg5)
434 FIXME("(%p,%p,%p,%p,%p) stub!\n", pTable, arg2, arg3, arg4, arg5);
435 return NULL;
438 /******************************************************************************
439 * RtlEnumerateGenericTableWithoutSplaying [NTDLL.@]
441 PVOID RtlEnumerateGenericTableWithoutSplaying(PVOID pTable, PVOID *RestartKey)
443 static int warn_once;
445 if (!warn_once++)
446 FIXME("(%p,%p) stub!\n", pTable, RestartKey);
447 return NULL;
450 /******************************************************************************
451 * RtlNumberGenericTableElements [NTDLL.@]
453 ULONG RtlNumberGenericTableElements(PVOID pTable)
455 FIXME("(%p) stub!\n", pTable);
456 return 0;
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, %u, %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,%d,%d)\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("(%d,%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 ULONGLONG __cdecl RtlUlonglongByteSwap(ULONGLONG i)
646 return ((ULONGLONG)RtlUlongByteSwap(i) << 32) | RtlUlongByteSwap(i>>32);
649 /*************************************************************************
650 * RtlUlongByteSwap [NTDLL.@]
652 * Swap the bytes of an unsigned int value.
654 * NOTES
655 * ix86 version takes argument in %ecx. Other systems use the inline version.
657 #ifdef __i386__
658 __ASM_GLOBAL_FUNC(NTDLL_RtlUlongByteSwap,
659 "movl %ecx,%eax\n\t"
660 "bswap %eax\n\t"
661 "ret")
662 #endif
664 /*************************************************************************
665 * RtlUshortByteSwap [NTDLL.@]
667 * Swap the bytes of an unsigned short value.
669 * NOTES
670 * i386 version takes argument in %cx. Other systems use the inline version.
672 #ifdef __i386__
673 __ASM_GLOBAL_FUNC(NTDLL_RtlUshortByteSwap,
674 "movb %ch,%al\n\t"
675 "movb %cl,%ah\n\t"
676 "ret")
677 #endif
680 /*************************************************************************
681 * RtlUniform [NTDLL.@]
683 * Generates an uniform random number
685 * PARAMS
686 * seed [O] The seed of the Random function
688 * RETURNS
689 * It returns a random number uniformly distributed over [0..MAXLONG-1].
691 * NOTES
692 * Generates an uniform random number using D.H. Lehmer's 1948 algorithm.
693 * In our case the algorithm is:
695 *| result = (*seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
697 *| *seed = result;
699 * DIFFERENCES
700 * The native documentation states that the random number is
701 * uniformly distributed over [0..MAXLONG]. In reality the native
702 * function and our function return a random number uniformly
703 * distributed over [0..MAXLONG-1].
705 ULONG WINAPI RtlUniform (PULONG seed)
707 ULONG result;
710 * Instead of the algorithm stated above, we use the algorithm
711 * below, which is totally equivalent (see the tests), but does
712 * not use a division and therefore is faster.
714 result = *seed * 0xffffffed + 0x7fffffc3;
715 if (result == 0xffffffff || result == 0x7ffffffe) {
716 result = (result + 2) & MAXLONG;
717 } else if (result == 0x7fffffff) {
718 result = 0;
719 } else if ((result & 0x80000000) == 0) {
720 result = result + (~result & 1);
721 } else {
722 result = (result + (result & 1)) & MAXLONG;
723 } /* if */
724 *seed = result;
725 return result;
729 /*************************************************************************
730 * RtlRandom [NTDLL.@]
732 * Generates a random number
734 * PARAMS
735 * seed [O] The seed of the Random function
737 * RETURNS
738 * It returns a random number distributed over [0..MAXLONG-1].
740 ULONG WINAPI RtlRandom (PULONG seed)
742 static ULONG saved_value[128] =
743 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
744 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
745 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
746 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
747 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
748 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
749 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
750 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
751 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
752 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
753 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
754 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
755 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
756 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
757 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
758 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
759 ULONG rand;
760 int pos;
761 ULONG result;
763 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
764 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
765 pos = *seed & 0x7f;
766 result = saved_value[pos];
767 saved_value[pos] = rand;
768 return(result);
772 /*************************************************************************
773 * RtlRandomEx [NTDLL.@]
775 ULONG WINAPI RtlRandomEx( ULONG *seed )
777 WARN( "semi-stub: should use a different algorithm\n" );
778 return RtlRandom( seed );
781 /*************************************************************************
782 * RtlAreAllAccessesGranted [NTDLL.@]
784 * Check if all desired accesses are granted
786 * RETURNS
787 * TRUE: All desired accesses are granted
788 * FALSE: Otherwise
790 BOOLEAN WINAPI RtlAreAllAccessesGranted(
791 ACCESS_MASK GrantedAccess,
792 ACCESS_MASK DesiredAccess)
794 return (GrantedAccess & DesiredAccess) == DesiredAccess;
798 /*************************************************************************
799 * RtlAreAnyAccessesGranted [NTDLL.@]
801 * Check if at least one of the desired accesses is granted
803 * PARAMS
804 * GrantedAccess [I] Access mask of granted accesses
805 * DesiredAccess [I] Access mask of desired accesses
807 * RETURNS
808 * TRUE: At least one of the desired accesses is granted
809 * FALSE: Otherwise
811 BOOLEAN WINAPI RtlAreAnyAccessesGranted(
812 ACCESS_MASK GrantedAccess,
813 ACCESS_MASK DesiredAccess)
815 return (GrantedAccess & DesiredAccess) != 0;
819 /*************************************************************************
820 * RtlMapGenericMask [NTDLL.@]
822 * Determine the nongeneric access rights specified by an access mask
824 * RETURNS
825 * Nothing.
827 void WINAPI RtlMapGenericMask(
828 PACCESS_MASK AccessMask,
829 const GENERIC_MAPPING *GenericMapping)
831 if (*AccessMask & GENERIC_READ) {
832 *AccessMask |= GenericMapping->GenericRead;
833 } /* if */
835 if (*AccessMask & GENERIC_WRITE) {
836 *AccessMask |= GenericMapping->GenericWrite;
837 } /* if */
839 if (*AccessMask & GENERIC_EXECUTE) {
840 *AccessMask |= GenericMapping->GenericExecute;
841 } /* if */
843 if (*AccessMask & GENERIC_ALL) {
844 *AccessMask |= GenericMapping->GenericAll;
845 } /* if */
847 *AccessMask &= 0x0FFFFFFF;
851 /*************************************************************************
852 * RtlCopyLuid [NTDLL.@]
854 * Copy a local unique ID.
856 * PARAMS
857 * LuidDest [O] Destination for the copied Luid
858 * LuidSrc [I] Source Luid to copy to LuidDest
860 * RETURNS
861 * Nothing.
863 void WINAPI RtlCopyLuid (PLUID LuidDest, const LUID *LuidSrc)
865 *LuidDest = *LuidSrc;
869 /*************************************************************************
870 * RtlEqualLuid [NTDLL.@]
872 * Compare two local unique IDs.
874 * PARAMS
875 * Luid1 [I] First Luid to compare to Luid2
876 * Luid2 [I] Second Luid to compare to Luid1
878 * RETURNS
879 * TRUE: The two LUIDs are equal.
880 * FALSE: Otherwise
882 BOOLEAN WINAPI RtlEqualLuid (const LUID *Luid1, const LUID *Luid2)
884 return (Luid1->LowPart == Luid2->LowPart && Luid1->HighPart == Luid2->HighPart);
888 /*************************************************************************
889 * RtlCopyLuidAndAttributesArray [NTDLL.@]
891 * Copy an array of local unique IDs and attributes.
893 * PARAMS
894 * Count [I] Number of Luid/attributes in Src
895 * Src [I] Source Luid/attributes to copy
896 * Dest [O] Destination for copied Luid/attributes
898 * RETURNS
899 * Nothing.
901 * NOTES
902 * Dest must be large enough to hold Src.
904 void WINAPI RtlCopyLuidAndAttributesArray(
905 ULONG Count,
906 const LUID_AND_ATTRIBUTES *Src,
907 PLUID_AND_ATTRIBUTES Dest)
909 ULONG i;
911 for (i = 0; i < Count; i++) Dest[i] = Src[i];
914 static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
916 int base = 10, d;
917 WCHAR c;
918 ULONG cur_value, prev_value = 0;
919 BOOL success = FALSE;
921 if (**str == '.')
923 *str += 1;
924 return FALSE;
927 if ((*str)[0] == '0')
929 if ((*str)[1] == 'x' || (*str)[1] == 'X')
931 *str += 2;
932 if (strict) return FALSE;
933 base = 16;
935 else if ((*str)[1] >= '0' && (*str)[1] <= '9')
937 *str += 1;
938 if (strict) return FALSE;
939 base = 8;
943 for (cur_value = 0; **str; *str += 1)
945 c = **str;
946 if (c >= ARRAY_SIZE(hex_table)) break;
947 d = hex_table[c];
948 if (d == -1 || d >= base) break;
949 cur_value = cur_value * base + d;
950 success = TRUE;
951 if (cur_value < prev_value) return FALSE; /* overflow */
952 prev_value = cur_value;
955 if (success) *value = cur_value;
956 return success;
959 static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
960 const WCHAR **terminator, IN_ADDR *address, USHORT *port)
962 ULONG fields[4];
963 int n = 0;
965 for (;;)
967 if (!parse_ipv4_component(&str, strict, &fields[n]))
968 goto error;
969 n++;
970 if (*str != '.')
971 break;
972 if (n == 4)
973 goto error;
974 str++;
977 if (strict && n < 4)
978 goto error;
980 switch (n)
982 case 4:
983 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFF || fields[3] > 0xFF)
984 goto error;
985 address->S_un.S_un_b.s_b1 = fields[0];
986 address->S_un.S_un_b.s_b2 = fields[1];
987 address->S_un.S_un_b.s_b3 = fields[2];
988 address->S_un.S_un_b.s_b4 = fields[3];
989 break;
990 case 3:
991 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFFFF)
992 goto error;
993 address->S_un.S_un_b.s_b1 = fields[0];
994 address->S_un.S_un_b.s_b2 = fields[1];
995 address->S_un.S_un_b.s_b3 = (fields[2] & 0xFF00) >> 8;
996 address->S_un.S_un_b.s_b4 = (fields[2] & 0x00FF);
997 break;
998 case 2:
999 if (fields[0] > 0xFF || fields[1] > 0xFFFFFF)
1000 goto error;
1001 address->S_un.S_un_b.s_b1 = fields[0];
1002 address->S_un.S_un_b.s_b2 = (fields[1] & 0xFF0000) >> 16;
1003 address->S_un.S_un_b.s_b3 = (fields[1] & 0x00FF00) >> 8;
1004 address->S_un.S_un_b.s_b4 = (fields[1] & 0x0000FF);
1005 break;
1006 case 1:
1007 address->S_un.S_un_b.s_b1 = (fields[0] & 0xFF000000) >> 24;
1008 address->S_un.S_un_b.s_b2 = (fields[0] & 0x00FF0000) >> 16;
1009 address->S_un.S_un_b.s_b3 = (fields[0] & 0x0000FF00) >> 8;
1010 address->S_un.S_un_b.s_b4 = (fields[0] & 0x000000FF);
1011 break;
1012 default:
1013 goto error;
1016 if (terminator) *terminator = str;
1018 if (*str == ':')
1020 str++;
1021 if (!parse_ipv4_component(&str, FALSE, &fields[0]))
1022 goto error;
1023 if (!fields[0] || fields[0] > 0xFFFF || *str)
1024 goto error;
1025 if (port)
1027 *port = htons(fields[0]);
1028 if (terminator) *terminator = str;
1032 if (!terminator && *str)
1033 return STATUS_INVALID_PARAMETER;
1034 return STATUS_SUCCESS;
1036 error:
1037 if (terminator) *terminator = str;
1038 return STATUS_INVALID_PARAMETER;
1041 /***********************************************************************
1042 * RtlIpv4StringToAddressExW [NTDLL.@]
1044 NTSTATUS WINAPI RtlIpv4StringToAddressExW(const WCHAR *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1046 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, address, port);
1047 if (!str || !address || !port) return STATUS_INVALID_PARAMETER;
1048 return ipv4_string_to_address(str, strict, NULL, address, port);
1051 /***********************************************************************
1052 * RtlIpv4StringToAddressW [NTDLL.@]
1054 NTSTATUS WINAPI RtlIpv4StringToAddressW(const WCHAR *str, BOOLEAN strict, const WCHAR **terminator, IN_ADDR *address)
1056 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, terminator, address);
1057 return ipv4_string_to_address(str, strict, terminator, address, NULL);
1060 /***********************************************************************
1061 * RtlIpv4StringToAddressExA [NTDLL.@]
1063 NTSTATUS WINAPI RtlIpv4StringToAddressExA(const char *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1065 WCHAR wstr[32];
1067 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, address, port);
1069 if (!str || !address || !port)
1070 return STATUS_INVALID_PARAMETER;
1072 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1073 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1074 return ipv4_string_to_address(wstr, strict, NULL, address, port);
1077 /***********************************************************************
1078 * RtlIpv4StringToAddressA [NTDLL.@]
1080 NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const char **terminator, IN_ADDR *address)
1082 WCHAR wstr[32];
1083 const WCHAR *wterminator;
1084 NTSTATUS ret;
1086 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, terminator, address);
1088 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1089 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1090 ret = ipv4_string_to_address(wstr, strict, &wterminator, address, NULL);
1091 if (terminator) *terminator = str + (wterminator - wstr);
1092 return ret;
1095 static BOOL parse_ipv6_component(const WCHAR **str, int base, ULONG *value)
1097 WCHAR *terminator;
1098 if (**str >= ARRAY_SIZE(hex_table) || hex_table[**str] == -1) return FALSE;
1099 *value = min(wcstoul(*str, &terminator, base), 0x7FFFFFFF);
1100 if (*terminator == '0') terminator++; /* "0x" but nothing valid after */
1101 else if (terminator == *str) return FALSE;
1102 *str = terminator;
1103 return TRUE;
1106 static NTSTATUS ipv6_string_to_address(const WCHAR *str, BOOL ex,
1107 const WCHAR **terminator, IN6_ADDR *address, ULONG *scope, USHORT *port)
1109 BOOL expecting_port = FALSE, has_0x = FALSE, too_big = FALSE;
1110 int n_bytes = 0, n_ipv4_bytes = 0, gap = -1;
1111 ULONG ip_component, scope_component = 0, port_component = 0;
1112 const WCHAR *prev_str;
1114 if (str[0] == '[')
1116 if (!ex) goto error;
1117 expecting_port = TRUE;
1118 str++;
1121 if (str[0] == ':')
1123 if (str[1] != ':') goto error;
1124 str++;
1125 /* Windows bug: a double colon at the beginning is treated as 4 bytes of zeros instead of 2 */
1126 address->u.Word[0] = 0;
1127 n_bytes = 2;
1130 for (;;)
1132 if (!n_ipv4_bytes && *str == ':')
1134 /* double colon */
1135 if (gap != -1) goto error;
1136 str++;
1137 prev_str = str;
1138 gap = n_bytes;
1139 if (n_bytes == 14 || !parse_ipv6_component(&str, 16, &ip_component)) break;
1140 str = prev_str;
1142 else
1144 prev_str = str;
1147 if (!n_ipv4_bytes && n_bytes <= (gap != -1 ? 10 : 12))
1149 if (parse_ipv6_component(&str, 10, &ip_component) && *str == '.')
1150 n_ipv4_bytes = 1;
1151 str = prev_str;
1154 if (n_ipv4_bytes)
1156 /* IPv4 component */
1157 if (!parse_ipv6_component(&str, 10, &ip_component)) goto error;
1158 if (str - prev_str > 3 || ip_component > 255)
1160 too_big = TRUE;
1162 else
1164 if (*str != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
1165 address->u.Byte[n_bytes] = ip_component;
1166 n_bytes++;
1168 if (n_ipv4_bytes == 4 || *str != '.') break;
1169 n_ipv4_bytes++;
1171 else
1173 /* IPv6 component */
1174 if (!parse_ipv6_component(&str, 16, &ip_component)) goto error;
1175 if (prev_str[0] == '0' && (prev_str[1] == 'x' || prev_str[1] == 'X'))
1177 /* Windows "feature": the last IPv6 component can start with "0x" and be longer than 4 digits */
1178 if (terminator) *terminator = prev_str + 1; /* Windows says that the "x" is the terminator */
1179 if (n_bytes < 14 && gap == -1) return STATUS_INVALID_PARAMETER;
1180 address->u.Word[n_bytes/2] = htons(ip_component);
1181 n_bytes += 2;
1182 has_0x = TRUE;
1183 goto fill_gap;
1185 if (*str != ':' && n_bytes < 14 && gap == -1) goto error;
1186 if (str - prev_str > 4)
1187 too_big = TRUE;
1188 else
1189 address->u.Word[n_bytes/2] = htons(ip_component);
1190 n_bytes += 2;
1191 if (*str != ':' || (gap != -1 && str[1] == ':')) break;
1193 if (n_bytes == (gap != -1 ? 14 : 16)) break;
1194 if (too_big) return STATUS_INVALID_PARAMETER;
1195 str++;
1198 if (terminator) *terminator = str;
1199 if (too_big) return STATUS_INVALID_PARAMETER;
1201 fill_gap:
1202 if (gap == -1)
1204 if (n_bytes < 16) goto error;
1206 else
1208 memmove(address->u.Byte + 16 - (n_bytes - gap), address->u.Byte + gap, n_bytes - gap);
1209 memset(address->u.Byte + gap, 0, 16 - n_bytes);
1212 if (ex)
1214 if (has_0x) goto error;
1216 if (*str == '%')
1218 str++;
1219 if (!parse_ipv4_component(&str, TRUE, &scope_component)) goto error;
1222 if (expecting_port)
1224 if (*str != ']') goto error;
1225 str++;
1226 if (*str == ':')
1228 str++;
1229 if (!parse_ipv4_component(&str, FALSE, &port_component)) goto error;
1230 if (!port_component || port_component > 0xFFFF || *str) goto error;
1231 port_component = htons(port_component);
1236 if (!terminator && *str) return STATUS_INVALID_PARAMETER;
1238 if (scope) *scope = scope_component;
1239 if (port) *port = port_component;
1241 return STATUS_SUCCESS;
1243 error:
1244 if (terminator) *terminator = str;
1245 return STATUS_INVALID_PARAMETER;
1248 /***********************************************************************
1249 * RtlIpv6StringToAddressExW [NTDLL.@]
1251 NTSTATUS NTAPI RtlIpv6StringToAddressExW(const WCHAR *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1253 TRACE("(%s, %p, %p, %p)\n", debugstr_w(str), address, scope, port);
1254 if (!str || !address || !scope || !port) return STATUS_INVALID_PARAMETER;
1255 return ipv6_string_to_address(str, TRUE, NULL, address, scope, port);
1258 /***********************************************************************
1259 * RtlIpv6StringToAddressW [NTDLL.@]
1261 NTSTATUS WINAPI RtlIpv6StringToAddressW(const WCHAR *str, const WCHAR **terminator, IN6_ADDR *address)
1263 TRACE("(%s, %p, %p)\n", debugstr_w(str), terminator, address);
1264 return ipv6_string_to_address(str, FALSE, terminator, address, NULL, NULL);
1267 /***********************************************************************
1268 * RtlIpv6StringToAddressExA [NTDLL.@]
1270 NTSTATUS WINAPI RtlIpv6StringToAddressExA(const char *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1272 WCHAR wstr[128];
1274 TRACE("(%s, %p, %p, %p)\n", debugstr_a(str), address, scope, port);
1276 if (!str || !address || !scope || !port)
1277 return STATUS_INVALID_PARAMETER;
1279 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1280 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1281 return ipv6_string_to_address(wstr, TRUE, NULL, address, scope, port);
1284 /***********************************************************************
1285 * RtlIpv6StringToAddressA [NTDLL.@]
1287 NTSTATUS WINAPI RtlIpv6StringToAddressA(const char *str, const char **terminator, IN6_ADDR *address)
1289 WCHAR wstr[128];
1290 const WCHAR *wterminator = NULL;
1291 NTSTATUS ret;
1293 TRACE("(%s, %p, %p)\n", debugstr_a(str), terminator, address);
1295 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1296 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1297 ret = ipv6_string_to_address(wstr, FALSE, &wterminator, address, NULL, NULL);
1298 if (terminator && wterminator) *terminator = str + (wterminator - wstr);
1299 return ret;
1302 /***********************************************************************
1303 * RtlIpv4AddressToStringExW [NTDLL.@]
1305 * Convert the given ipv4 address and optional the port to a string
1307 * PARAMS
1308 * pin [I] PTR to the ip address to convert (network byte order)
1309 * port [I] optional port to convert (network byte order)
1310 * buffer [O] destination buffer for the result
1311 * psize [IO] PTR to available/used size of the destination buffer
1313 * RETURNS
1314 * Success: STATUS_SUCCESS
1315 * Failure: STATUS_INVALID_PARAMETER
1318 NTSTATUS WINAPI RtlIpv4AddressToStringExW(const IN_ADDR *pin, USHORT port, LPWSTR buffer, PULONG psize)
1320 WCHAR tmp_ip[32];
1321 static const WCHAR fmt_ip[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1322 static const WCHAR fmt_port[] = {':','%','u',0};
1323 ULONG needed;
1325 if (!pin || !buffer || !psize)
1326 return STATUS_INVALID_PARAMETER;
1328 TRACE("(%p:0x%x, %d, %p, %p:%d)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1330 needed = NTDLL_swprintf(tmp_ip, fmt_ip,
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 += NTDLL_swprintf(tmp_ip + needed, fmt_port, 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%x, %d, %p, %p:%d)\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 %u %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, "%%%u", 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, %u, %p, %u, %u, %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, %u, %p, %u, %u, %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, %u, %p, %u, %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 * RtlInitializeGenericTableAvl (NTDLL.@)
2151 void WINAPI RtlInitializeGenericTableAvl(PRTL_AVL_TABLE table, PRTL_AVL_COMPARE_ROUTINE compare,
2152 PRTL_AVL_ALLOCATE_ROUTINE allocate, PRTL_AVL_FREE_ROUTINE free, void *context)
2154 FIXME("%p %p %p %p %p: stub\n", table, compare, allocate, free, context);
2157 /***********************************************************************
2158 * RtlInsertElementGenericTableAvl (NTDLL.@)
2160 void WINAPI RtlInsertElementGenericTableAvl(PRTL_AVL_TABLE table, void *buffer, ULONG size, BOOL *element)
2162 FIXME("%p %p %u %p: stub\n", table, buffer, size, element);
2165 /*********************************************************************
2166 * RtlQueryPackageIdentity [NTDLL.@]
2168 NTSTATUS WINAPI RtlQueryPackageIdentity(HANDLE token, WCHAR *fullname, SIZE_T *fullname_size,
2169 WCHAR *appid, SIZE_T *appid_size, BOOLEAN *packaged)
2171 FIXME("(%p, %p, %p, %p, %p, %p): stub\n", token, fullname, fullname_size, appid, appid_size, packaged);
2172 return STATUS_NOT_FOUND;