ntdll: Use nameless unions/structs for CPU-related data.
[wine.git] / dlls / ntdll / rtl.c
blob1eec1962b1ebff61edccac29a1759d80ace97581
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 WIN32_NO_STATUS
30 #include "winsock2.h"
31 #include "windef.h"
32 #include "winternl.h"
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35 #include "ntdll_misc.h"
36 #include "in6addr.h"
37 #include "ddk/ntddk.h"
38 #include "ddk/ntifs.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 WINE_DECLARE_DEBUG_CHANNEL(debugstr);
43 /* CRC polynomial 0xedb88320 */
44 static const DWORD CRC_table[256] =
46 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
47 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
48 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
49 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
50 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
51 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
52 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
53 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
54 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
55 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
56 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
57 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
58 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
59 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
60 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
61 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
62 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
63 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
64 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
65 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
66 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
67 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
68 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
69 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
70 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
71 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
72 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
73 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
74 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
75 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
76 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
77 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
78 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
79 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
80 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
81 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
82 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
83 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
84 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
85 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
86 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
87 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
88 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
91 static const int hex_table[] = {
92 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
93 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
94 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2F */
95 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
96 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4F */
97 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
98 -1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
102 * resource functions
105 /***********************************************************************
106 * RtlInitializeResource (NTDLL.@)
108 * xxxResource() functions implement multiple-reader-single-writer lock.
109 * The code is based on information published in WDJ January 1999 issue.
111 void WINAPI RtlInitializeResource(LPRTL_RWLOCK rwl)
113 if( rwl )
115 rwl->iNumberActive = 0;
116 rwl->uExclusiveWaiters = 0;
117 rwl->uSharedWaiters = 0;
118 rwl->hOwningThreadId = 0;
119 rwl->dwTimeoutBoost = 0; /* no info on this one, default value is 0 */
120 RtlInitializeCriticalSection( &rwl->rtlCS );
121 rwl->rtlCS.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RTL_RWLOCK.rtlCS");
122 NtCreateSemaphore( &rwl->hExclusiveReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
123 NtCreateSemaphore( &rwl->hSharedReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
128 /***********************************************************************
129 * RtlDeleteResource (NTDLL.@)
131 void WINAPI RtlDeleteResource(LPRTL_RWLOCK rwl)
133 if( rwl )
135 RtlEnterCriticalSection( &rwl->rtlCS );
136 if( rwl->iNumberActive || rwl->uExclusiveWaiters || rwl->uSharedWaiters )
137 ERR("Deleting active MRSW lock (%p), expect failure\n", rwl );
138 rwl->hOwningThreadId = 0;
139 rwl->uExclusiveWaiters = rwl->uSharedWaiters = 0;
140 rwl->iNumberActive = 0;
141 NtClose( rwl->hExclusiveReleaseSemaphore );
142 NtClose( rwl->hSharedReleaseSemaphore );
143 RtlLeaveCriticalSection( &rwl->rtlCS );
144 rwl->rtlCS.DebugInfo->Spare[0] = 0;
145 RtlDeleteCriticalSection( &rwl->rtlCS );
150 /***********************************************************************
151 * RtlAcquireResourceExclusive (NTDLL.@)
153 BYTE WINAPI RtlAcquireResourceExclusive(LPRTL_RWLOCK rwl, BYTE fWait)
155 BYTE retVal = 0;
156 if( !rwl ) return 0;
158 start:
159 RtlEnterCriticalSection( &rwl->rtlCS );
160 if( rwl->iNumberActive == 0 ) /* lock is free */
162 rwl->iNumberActive = -1;
163 retVal = 1;
165 else if( rwl->iNumberActive < 0 ) /* exclusive lock in progress */
167 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
169 retVal = 1;
170 rwl->iNumberActive--;
171 goto done;
173 wait:
174 if( fWait )
176 NTSTATUS status;
178 rwl->uExclusiveWaiters++;
180 RtlLeaveCriticalSection( &rwl->rtlCS );
181 status = NtWaitForSingleObject( rwl->hExclusiveReleaseSemaphore, FALSE, NULL );
182 if( HIWORD(status) )
183 goto done;
184 goto start; /* restart the acquisition to avoid deadlocks */
187 else /* one or more shared locks are in progress */
188 if( fWait )
189 goto wait;
191 if( retVal == 1 )
192 rwl->hOwningThreadId = ULongToHandle(GetCurrentThreadId());
193 done:
194 RtlLeaveCriticalSection( &rwl->rtlCS );
195 return retVal;
198 /***********************************************************************
199 * RtlAcquireResourceShared (NTDLL.@)
201 BYTE WINAPI RtlAcquireResourceShared(LPRTL_RWLOCK rwl, BYTE fWait)
203 NTSTATUS status = STATUS_UNSUCCESSFUL;
204 BYTE retVal = 0;
205 if( !rwl ) return 0;
207 start:
208 RtlEnterCriticalSection( &rwl->rtlCS );
209 if( rwl->iNumberActive < 0 )
211 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
213 rwl->iNumberActive--;
214 retVal = 1;
215 goto done;
218 if( fWait )
220 rwl->uSharedWaiters++;
221 RtlLeaveCriticalSection( &rwl->rtlCS );
222 status = NtWaitForSingleObject( rwl->hSharedReleaseSemaphore, FALSE, NULL );
223 if( HIWORD(status) )
224 goto done;
225 goto start;
228 else
230 if( status != STATUS_WAIT_0 ) /* otherwise RtlReleaseResource() has already done it */
231 rwl->iNumberActive++;
232 retVal = 1;
234 done:
235 RtlLeaveCriticalSection( &rwl->rtlCS );
236 return retVal;
240 /***********************************************************************
241 * RtlReleaseResource (NTDLL.@)
243 void WINAPI RtlReleaseResource(LPRTL_RWLOCK rwl)
245 RtlEnterCriticalSection( &rwl->rtlCS );
247 if( rwl->iNumberActive > 0 ) /* have one or more readers */
249 if( --rwl->iNumberActive == 0 )
251 if( rwl->uExclusiveWaiters )
253 wake_exclusive:
254 rwl->uExclusiveWaiters--;
255 NtReleaseSemaphore( rwl->hExclusiveReleaseSemaphore, 1, NULL );
259 else
260 if( rwl->iNumberActive < 0 ) /* have a writer, possibly recursive */
262 if( ++rwl->iNumberActive == 0 )
264 rwl->hOwningThreadId = 0;
265 if( rwl->uExclusiveWaiters )
266 goto wake_exclusive;
267 else
268 if( rwl->uSharedWaiters )
270 UINT n = rwl->uSharedWaiters;
271 rwl->iNumberActive = rwl->uSharedWaiters; /* prevent new writers from joining until
272 * all queued readers have done their thing */
273 rwl->uSharedWaiters = 0;
274 NtReleaseSemaphore( rwl->hSharedReleaseSemaphore, n, NULL );
278 RtlLeaveCriticalSection( &rwl->rtlCS );
282 /***********************************************************************
283 * RtlDumpResource (NTDLL.@)
285 void WINAPI RtlDumpResource(LPRTL_RWLOCK rwl)
287 if( rwl )
289 MESSAGE("RtlDumpResource(%p):\n\tactive count = %i\n\twaiting readers = %i\n\twaiting writers = %i\n",
290 rwl, rwl->iNumberActive, rwl->uSharedWaiters, rwl->uExclusiveWaiters );
291 if( rwl->iNumberActive )
292 MESSAGE("\towner thread = %p\n", rwl->hOwningThreadId );
297 * misc functions
300 static LONG WINAPI debug_exception_handler( EXCEPTION_POINTERS *eptr )
302 EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
303 return (rec->ExceptionCode == DBG_PRINTEXCEPTION_C) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
306 /******************************************************************************
307 * DbgPrint [NTDLL.@]
309 NTSTATUS WINAPIV DbgPrint(LPCSTR fmt, ...)
311 NTSTATUS ret;
312 va_list args;
314 va_start(args, fmt);
315 ret = vDbgPrintEx(0, DPFLTR_ERROR_LEVEL, fmt, args);
316 va_end(args);
317 return ret;
321 /******************************************************************************
322 * DbgPrintEx [NTDLL.@]
324 NTSTATUS WINAPIV DbgPrintEx(ULONG iComponentId, ULONG Level, LPCSTR fmt, ...)
326 NTSTATUS ret;
327 va_list args;
329 va_start(args, fmt);
330 ret = vDbgPrintEx(iComponentId, Level, fmt, args);
331 va_end(args);
332 return ret;
335 /******************************************************************************
336 * vDbgPrintEx [NTDLL.@]
338 NTSTATUS WINAPI vDbgPrintEx( ULONG id, ULONG level, LPCSTR fmt, va_list args )
340 return vDbgPrintExWithPrefix( "", id, level, fmt, args );
343 /******************************************************************************
344 * vDbgPrintExWithPrefix [NTDLL.@]
346 NTSTATUS WINAPI vDbgPrintExWithPrefix( LPCSTR prefix, ULONG id, ULONG level, LPCSTR fmt, va_list args )
348 ULONG level_mask = level <= 31 ? (1 << level) : level;
349 SIZE_T len = strlen( prefix );
350 char buf[1024], *end;
352 strcpy( buf, prefix );
353 len += _vsnprintf( buf + len, sizeof(buf) - len, fmt, args );
354 end = buf + len - 1;
356 WARN_(debugstr)(*end == '\n' ? "%08lx:%08lx: %s" : "%08lx:%08lx: %s\n", id, level_mask, buf);
358 if (level_mask & (1 << DPFLTR_ERROR_LEVEL) && NtCurrentTeb()->Peb->BeingDebugged)
360 __TRY
362 EXCEPTION_RECORD record;
363 record.ExceptionCode = DBG_PRINTEXCEPTION_C;
364 record.ExceptionFlags = 0;
365 record.ExceptionRecord = NULL;
366 record.ExceptionAddress = RtlRaiseException;
367 record.NumberParameters = 2;
368 record.ExceptionInformation[1] = (ULONG_PTR)buf;
369 record.ExceptionInformation[0] = strlen( buf ) + 1;
370 RtlRaiseException( &record );
372 __EXCEPT(debug_exception_handler)
375 __ENDTRY
378 return STATUS_SUCCESS;
381 /******************************************************************************
382 * RtlAcquirePebLock [NTDLL.@]
384 VOID WINAPI RtlAcquirePebLock(void)
386 RtlEnterCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
389 /******************************************************************************
390 * RtlReleasePebLock [NTDLL.@]
392 VOID WINAPI RtlReleasePebLock(void)
394 RtlLeaveCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
397 /******************************************************************************
398 * RtlInitializeGenericTable [NTDLL.@]
400 void WINAPI RtlInitializeGenericTable(RTL_GENERIC_TABLE *table, PRTL_GENERIC_COMPARE_ROUTINE compare,
401 PRTL_GENERIC_ALLOCATE_ROUTINE allocate, PRTL_GENERIC_FREE_ROUTINE free,
402 void *context)
404 TRACE("(%p, %p, %p, %p, %p)\n", table, compare, allocate, free, context);
406 table->TableRoot = NULL;
407 table->InsertOrderList.Flink = &table->InsertOrderList;
408 table->InsertOrderList.Blink = &table->InsertOrderList;
409 table->OrderedPointer = &table->InsertOrderList;
410 table->NumberGenericTableElements = 0;
411 table->WhichOrderedElement = 0;
412 table->CompareRoutine = compare;
413 table->AllocateRoutine = allocate;
414 table->FreeRoutine = free;
415 table->TableContext = context;
418 /******************************************************************************
419 * RtlEnumerateGenericTableWithoutSplaying [NTDLL.@]
421 void * WINAPI RtlEnumerateGenericTableWithoutSplaying(RTL_GENERIC_TABLE *table, void *previous)
423 static int warn_once;
425 if (!warn_once++)
426 FIXME("(%p, %p) stub!\n", table, previous);
427 return NULL;
430 /******************************************************************************
431 * RtlNumberGenericTableElements [NTDLL.@]
433 ULONG WINAPI RtlNumberGenericTableElements(RTL_GENERIC_TABLE *table)
435 TRACE("(%p)\n", table);
436 return table->NumberGenericTableElements;
439 /******************************************************************************
440 * RtlGetElementGenericTable [NTDLL.@]
442 void * WINAPI RtlGetElementGenericTable(RTL_GENERIC_TABLE *table, ULONG index)
444 FIXME("(%p, %lu) stub!\n", table, index);
445 return NULL;
448 /******************************************************************************
449 * RtlLookupElementGenericTable [NTDLL.@]
451 void * WINAPI RtlLookupElementGenericTable(RTL_GENERIC_TABLE *table, void *buffer)
453 FIXME("(%p, %p) stub!\n", table, buffer);
454 return NULL;
457 /******************************************************************************
458 * RtlMoveMemory [NTDLL.@]
460 * Move a block of memory that may overlap.
462 * PARAMS
463 * Destination [O] End destination for block
464 * Source [O] Where to start copying from
465 * Length [I] Number of bytes to copy
467 * RETURNS
468 * Nothing.
470 #undef RtlMoveMemory
471 VOID WINAPI RtlMoveMemory( void *Destination, const void *Source, SIZE_T Length )
473 memmove(Destination, Source, Length);
476 /******************************************************************************
477 * RtlFillMemory [NTDLL.@]
479 * Set a block of memory with a value.
481 * PARAMS
482 * Destination [O] Block to fill
483 * Length [I] Number of bytes to fill
484 * Fill [I] Value to set
486 * RETURNS
487 * Nothing.
489 #undef RtlFillMemory
490 VOID WINAPI RtlFillMemory( VOID *Destination, SIZE_T Length, BYTE Fill )
492 memset(Destination, Fill, Length);
495 /******************************************************************************
496 * RtlZeroMemory [NTDLL.@]
498 * Set a block of memory with 0's.
500 * PARAMS
501 * Destination [O] Block to fill
502 * Length [I] Number of bytes to fill
504 * RETURNS
505 * Nothing.
507 #undef RtlZeroMemory
508 VOID WINAPI RtlZeroMemory( VOID *Destination, SIZE_T Length )
510 memset(Destination, 0, Length);
513 /******************************************************************************
514 * RtlCompareMemory [NTDLL.@]
516 * Compare one block of memory with another
518 * PARAMS
519 * Source1 [I] Source block
520 * Source2 [I] Block to compare to Source1
521 * Length [I] Number of bytes to compare
523 * RETURNS
524 * The length of the first byte at which Source1 and Source2 differ, or Length
525 * if they are the same.
527 SIZE_T WINAPI RtlCompareMemory( const VOID *Source1, const VOID *Source2, SIZE_T Length)
529 SIZE_T i;
530 for(i=0; (i<Length) && (((const BYTE*)Source1)[i]==((const BYTE*)Source2)[i]); i++);
531 return i;
534 /******************************************************************************
535 * RtlCompareMemoryUlong [NTDLL.@]
537 * Compare a block of memory with a value, a ULONG at a time
539 * PARAMS
540 * Source1 [I] Source block. This must be ULONG aligned
541 * Length [I] Number of bytes to compare. This should be a multiple of 4
542 * dwVal [I] Value to compare to
544 * RETURNS
545 * The byte position of the first byte at which Source1 is not dwVal.
547 SIZE_T WINAPI RtlCompareMemoryUlong(VOID *Source1, SIZE_T Length, ULONG dwVal)
549 SIZE_T i;
550 for(i = 0; i < Length/sizeof(ULONG) && ((ULONG *)Source1)[i] == dwVal; i++);
551 return i * sizeof(ULONG);
554 /******************************************************************************
555 * RtlCopyMemory [NTDLL.@]
557 #undef RtlCopyMemory
558 void WINAPI RtlCopyMemory(void *dest, const void *src, SIZE_T len)
560 memcpy(dest, src, len);
563 /******************************************************************************
564 * RtlAssert [NTDLL.@]
566 * Fail a debug assertion.
568 * RETURNS
569 * Nothing. This call does not return control to its caller.
571 * NOTES
572 * Not implemented in non-debug versions.
574 void WINAPI RtlAssert(void *assertion, void *filename, ULONG linenumber, char *message)
576 FIXME("(%s, %s, %lu, %s): stub\n", debugstr_a((char*)assertion), debugstr_a((char*)filename),
577 linenumber, debugstr_a(message));
580 /*************************************************************************
581 * RtlFillMemoryUlong [NTDLL.@]
583 * Fill memory with a 32 bit (dword) value.
585 * PARAMS
586 * lpDest [I] Bitmap pointer
587 * ulCount [I] Number of dwords to write
588 * ulValue [I] Value to fill with
590 * RETURNS
591 * Nothing.
593 VOID WINAPI RtlFillMemoryUlong(ULONG* lpDest, ULONG ulCount, ULONG ulValue)
595 TRACE("(%p,%lu,%lu)\n", lpDest, ulCount, ulValue);
597 ulCount /= sizeof(ULONG);
598 while(ulCount--)
599 *lpDest++ = ulValue;
602 /*********************************************************************
603 * RtlComputeCrc32 [NTDLL.@]
605 * Calculate the CRC32 checksum of a block of bytes
607 * PARAMS
608 * dwInitial [I] Initial CRC value
609 * pData [I] Data block
610 * iLen [I] Length of the byte block
612 * RETURNS
613 * The cumulative CRC32 of dwInitial and iLen bytes of the pData block.
615 DWORD WINAPI RtlComputeCrc32(DWORD dwInitial, const BYTE *pData, INT iLen)
617 DWORD crc = ~dwInitial;
619 TRACE("(%lu,%p,%d)\n", dwInitial, pData, iLen);
621 while (iLen > 0)
623 crc = CRC_table[(crc ^ *pData) & 0xff] ^ (crc >> 8);
624 pData++;
625 iLen--;
627 return ~crc;
631 /*************************************************************************
632 * RtlUlonglongByteSwap [NTDLL.@]
634 * Swap the bytes of an unsigned long long value.
636 * PARAMS
637 * i [I] Value to swap bytes of
639 * RETURNS
640 * The value with its bytes swapped.
642 #ifdef __i386__
643 __ASM_FASTCALL_FUNC(RtlUlonglongByteSwap, 8,
644 "movl 4(%esp),%edx\n\t"
645 "bswap %edx\n\t"
646 "movl 8(%esp),%eax\n\t"
647 "bswap %eax\n\t"
648 "ret $8")
649 #endif
651 /*************************************************************************
652 * RtlUlongByteSwap [NTDLL.@]
654 * Swap the bytes of an unsigned int value.
656 * NOTES
657 * ix86 version takes argument in %ecx. Other systems use the inline version.
659 #ifdef __i386__
660 __ASM_FASTCALL_FUNC(RtlUlongByteSwap, 4,
661 "movl %ecx,%eax\n\t"
662 "bswap %eax\n\t"
663 "ret")
664 #endif
666 /*************************************************************************
667 * RtlUshortByteSwap [NTDLL.@]
669 * Swap the bytes of an unsigned short value.
671 * NOTES
672 * i386 version takes argument in %cx. Other systems use the inline version.
674 #ifdef __i386__
675 __ASM_FASTCALL_FUNC(RtlUshortByteSwap, 4,
676 "movb %ch,%al\n\t"
677 "movb %cl,%ah\n\t"
678 "ret")
679 #endif
682 /*************************************************************************
683 * RtlUniform [NTDLL.@]
685 * Generates a uniform random number
687 * PARAMS
688 * seed [O] The seed of the Random function
690 * RETURNS
691 * It returns a random number uniformly distributed over [0..MAXLONG-1].
693 * NOTES
694 * Generates a uniform random number using a linear congruential generator.
696 * DIFFERENCES
697 * The native documentation states that the random number is
698 * uniformly distributed over [0..MAXLONG]. In reality the native
699 * function and our function return a random number uniformly
700 * distributed over [0..MAXLONG-1].
702 ULONG WINAPI RtlUniform( ULONG *seed )
704 /* See the tests for details. */
705 return (*seed = ((ULONGLONG)*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff);
709 /*************************************************************************
710 * RtlRandom [NTDLL.@]
712 * Generates a random number
714 * PARAMS
715 * seed [O] The seed of the Random function
717 * RETURNS
718 * It returns a random number distributed over [0..MAXLONG-1].
720 ULONG WINAPI RtlRandom (PULONG seed)
722 static ULONG saved_value[128] =
723 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
724 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
725 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
726 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
727 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
728 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
729 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
730 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
731 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
732 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
733 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
734 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
735 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
736 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
737 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
738 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
739 ULONG rand;
740 int pos;
741 ULONG result;
743 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
744 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
745 pos = *seed & 0x7f;
746 result = saved_value[pos];
747 saved_value[pos] = rand;
748 return(result);
752 /*************************************************************************
753 * RtlRandomEx [NTDLL.@]
755 ULONG WINAPI RtlRandomEx( ULONG *seed )
757 WARN( "semi-stub: should use a different algorithm\n" );
758 return RtlRandom( seed );
761 /*************************************************************************
762 * RtlAreAllAccessesGranted [NTDLL.@]
764 * Check if all desired accesses are granted
766 * RETURNS
767 * TRUE: All desired accesses are granted
768 * FALSE: Otherwise
770 BOOLEAN WINAPI RtlAreAllAccessesGranted(
771 ACCESS_MASK GrantedAccess,
772 ACCESS_MASK DesiredAccess)
774 return (GrantedAccess & DesiredAccess) == DesiredAccess;
778 /*************************************************************************
779 * RtlAreAnyAccessesGranted [NTDLL.@]
781 * Check if at least one of the desired accesses is granted
783 * PARAMS
784 * GrantedAccess [I] Access mask of granted accesses
785 * DesiredAccess [I] Access mask of desired accesses
787 * RETURNS
788 * TRUE: At least one of the desired accesses is granted
789 * FALSE: Otherwise
791 BOOLEAN WINAPI RtlAreAnyAccessesGranted(
792 ACCESS_MASK GrantedAccess,
793 ACCESS_MASK DesiredAccess)
795 return (GrantedAccess & DesiredAccess) != 0;
799 /*************************************************************************
800 * RtlMapGenericMask [NTDLL.@]
802 * Determine the nongeneric access rights specified by an access mask
804 * RETURNS
805 * Nothing.
807 void WINAPI RtlMapGenericMask(
808 PACCESS_MASK AccessMask,
809 const GENERIC_MAPPING *GenericMapping)
811 if (*AccessMask & GENERIC_READ) {
812 *AccessMask |= GenericMapping->GenericRead;
813 } /* if */
815 if (*AccessMask & GENERIC_WRITE) {
816 *AccessMask |= GenericMapping->GenericWrite;
817 } /* if */
819 if (*AccessMask & GENERIC_EXECUTE) {
820 *AccessMask |= GenericMapping->GenericExecute;
821 } /* if */
823 if (*AccessMask & GENERIC_ALL) {
824 *AccessMask |= GenericMapping->GenericAll;
825 } /* if */
827 *AccessMask &= 0x0FFFFFFF;
831 /*************************************************************************
832 * RtlCopyLuid [NTDLL.@]
834 * Copy a local unique ID.
836 * PARAMS
837 * LuidDest [O] Destination for the copied Luid
838 * LuidSrc [I] Source Luid to copy to LuidDest
840 * RETURNS
841 * Nothing.
843 void WINAPI RtlCopyLuid (PLUID LuidDest, const LUID *LuidSrc)
845 *LuidDest = *LuidSrc;
849 /*************************************************************************
850 * RtlEqualLuid [NTDLL.@]
852 * Compare two local unique IDs.
854 * PARAMS
855 * Luid1 [I] First Luid to compare to Luid2
856 * Luid2 [I] Second Luid to compare to Luid1
858 * RETURNS
859 * TRUE: The two LUIDs are equal.
860 * FALSE: Otherwise
862 BOOLEAN WINAPI RtlEqualLuid (const LUID *Luid1, const LUID *Luid2)
864 return (Luid1->LowPart == Luid2->LowPart && Luid1->HighPart == Luid2->HighPart);
868 /*************************************************************************
869 * RtlCopyLuidAndAttributesArray [NTDLL.@]
871 * Copy an array of local unique IDs and attributes.
873 * PARAMS
874 * Count [I] Number of Luid/attributes in Src
875 * Src [I] Source Luid/attributes to copy
876 * Dest [O] Destination for copied Luid/attributes
878 * RETURNS
879 * Nothing.
881 * NOTES
882 * Dest must be large enough to hold Src.
884 void WINAPI RtlCopyLuidAndAttributesArray(
885 ULONG Count,
886 const LUID_AND_ATTRIBUTES *Src,
887 PLUID_AND_ATTRIBUTES Dest)
889 ULONG i;
891 for (i = 0; i < Count; i++) Dest[i] = Src[i];
894 static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
896 int base = 10, d;
897 WCHAR c;
898 ULONG cur_value, prev_value = 0;
899 BOOL success = FALSE;
901 if (**str == '.')
903 *str += 1;
904 return FALSE;
907 if ((*str)[0] == '0')
909 if ((*str)[1] == 'x' || (*str)[1] == 'X')
911 *str += 2;
912 if (strict) return FALSE;
913 base = 16;
915 else if ((*str)[1] >= '0' && (*str)[1] <= '9')
917 *str += 1;
918 if (strict) return FALSE;
919 base = 8;
923 for (cur_value = 0; **str; *str += 1)
925 c = **str;
926 if (c >= ARRAY_SIZE(hex_table)) break;
927 d = hex_table[c];
928 if (d == -1 || d >= base) break;
929 cur_value = cur_value * base + d;
930 success = TRUE;
931 if (cur_value < prev_value) return FALSE; /* overflow */
932 prev_value = cur_value;
935 if (success) *value = cur_value;
936 return success;
939 static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
940 const WCHAR **terminator, IN_ADDR *address, USHORT *port)
942 ULONG fields[4];
943 int n = 0;
945 for (;;)
947 if (!parse_ipv4_component(&str, strict, &fields[n]))
948 goto error;
949 n++;
950 if (*str != '.')
951 break;
952 if (n == 4)
953 goto error;
954 str++;
957 if (strict && n < 4)
958 goto error;
960 switch (n)
962 case 4:
963 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFF || fields[3] > 0xFF)
964 goto error;
965 address->S_un.S_un_b.s_b1 = fields[0];
966 address->S_un.S_un_b.s_b2 = fields[1];
967 address->S_un.S_un_b.s_b3 = fields[2];
968 address->S_un.S_un_b.s_b4 = fields[3];
969 break;
970 case 3:
971 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFFFF)
972 goto error;
973 address->S_un.S_un_b.s_b1 = fields[0];
974 address->S_un.S_un_b.s_b2 = fields[1];
975 address->S_un.S_un_b.s_b3 = (fields[2] & 0xFF00) >> 8;
976 address->S_un.S_un_b.s_b4 = (fields[2] & 0x00FF);
977 break;
978 case 2:
979 if (fields[0] > 0xFF || fields[1] > 0xFFFFFF)
980 goto error;
981 address->S_un.S_un_b.s_b1 = fields[0];
982 address->S_un.S_un_b.s_b2 = (fields[1] & 0xFF0000) >> 16;
983 address->S_un.S_un_b.s_b3 = (fields[1] & 0x00FF00) >> 8;
984 address->S_un.S_un_b.s_b4 = (fields[1] & 0x0000FF);
985 break;
986 case 1:
987 address->S_un.S_un_b.s_b1 = (fields[0] & 0xFF000000) >> 24;
988 address->S_un.S_un_b.s_b2 = (fields[0] & 0x00FF0000) >> 16;
989 address->S_un.S_un_b.s_b3 = (fields[0] & 0x0000FF00) >> 8;
990 address->S_un.S_un_b.s_b4 = (fields[0] & 0x000000FF);
991 break;
992 default:
993 goto error;
996 if (terminator) *terminator = str;
998 if (*str == ':')
1000 str++;
1001 if (!parse_ipv4_component(&str, FALSE, &fields[0]))
1002 goto error;
1003 if (!fields[0] || fields[0] > 0xFFFF || *str)
1004 goto error;
1005 if (port)
1007 *port = htons(fields[0]);
1008 if (terminator) *terminator = str;
1012 if (!terminator && *str)
1013 return STATUS_INVALID_PARAMETER;
1014 return STATUS_SUCCESS;
1016 error:
1017 if (terminator) *terminator = str;
1018 return STATUS_INVALID_PARAMETER;
1021 /***********************************************************************
1022 * RtlIpv4StringToAddressExW [NTDLL.@]
1024 NTSTATUS WINAPI RtlIpv4StringToAddressExW(const WCHAR *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1026 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, address, port);
1027 if (!str || !address || !port) return STATUS_INVALID_PARAMETER;
1028 return ipv4_string_to_address(str, strict, NULL, address, port);
1031 /***********************************************************************
1032 * RtlIpv4StringToAddressW [NTDLL.@]
1034 NTSTATUS WINAPI RtlIpv4StringToAddressW(const WCHAR *str, BOOLEAN strict, const WCHAR **terminator, IN_ADDR *address)
1036 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, terminator, address);
1037 return ipv4_string_to_address(str, strict, terminator, address, NULL);
1040 /***********************************************************************
1041 * RtlIpv4StringToAddressExA [NTDLL.@]
1043 NTSTATUS WINAPI RtlIpv4StringToAddressExA(const char *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1045 WCHAR wstr[32];
1047 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, address, port);
1049 if (!str || !address || !port)
1050 return STATUS_INVALID_PARAMETER;
1052 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1053 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1054 return ipv4_string_to_address(wstr, strict, NULL, address, port);
1057 /***********************************************************************
1058 * RtlIpv4StringToAddressA [NTDLL.@]
1060 NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const char **terminator, IN_ADDR *address)
1062 WCHAR wstr[32];
1063 const WCHAR *wterminator;
1064 NTSTATUS ret;
1066 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, terminator, address);
1068 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1069 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1070 ret = ipv4_string_to_address(wstr, strict, &wterminator, address, NULL);
1071 if (terminator) *terminator = str + (wterminator - wstr);
1072 return ret;
1075 static BOOL parse_ipv6_component(const WCHAR **str, int base, ULONG *value)
1077 WCHAR *terminator;
1078 if (**str >= ARRAY_SIZE(hex_table) || hex_table[**str] == -1) return FALSE;
1079 *value = min(wcstoul(*str, &terminator, base), 0x7FFFFFFF);
1080 if (*terminator == '0') terminator++; /* "0x" but nothing valid after */
1081 else if (terminator == *str) return FALSE;
1082 *str = terminator;
1083 return TRUE;
1086 static NTSTATUS ipv6_string_to_address(const WCHAR *str, BOOL ex,
1087 const WCHAR **terminator, IN6_ADDR *address, ULONG *scope, USHORT *port)
1089 BOOL expecting_port = FALSE, has_0x = FALSE, too_big = FALSE;
1090 int n_bytes = 0, n_ipv4_bytes = 0, gap = -1;
1091 ULONG ip_component, scope_component = 0, port_component = 0;
1092 const WCHAR *prev_str;
1094 if (str[0] == '[')
1096 if (!ex) goto error;
1097 expecting_port = TRUE;
1098 str++;
1101 if (str[0] == ':')
1103 if (str[1] != ':') goto error;
1104 str++;
1105 address->u.Word[0] = 0;
1108 for (;;)
1110 if (!n_ipv4_bytes && *str == ':')
1112 /* double colon */
1113 if (gap != -1) goto error;
1114 str++;
1115 prev_str = str;
1116 gap = n_bytes;
1117 if (n_bytes == 14 || !parse_ipv6_component(&str, 16, &ip_component)) break;
1118 str = prev_str;
1120 else
1122 prev_str = str;
1125 if (!n_ipv4_bytes && n_bytes <= (gap != -1 ? 10 : 12))
1127 if (parse_ipv6_component(&str, 10, &ip_component) && *str == '.')
1128 n_ipv4_bytes = 1;
1129 str = prev_str;
1132 if (n_ipv4_bytes)
1134 /* IPv4 component */
1135 if (!parse_ipv6_component(&str, 10, &ip_component)) goto error;
1136 if (str - prev_str > 3 || ip_component > 255)
1138 too_big = TRUE;
1140 else
1142 if (*str != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
1143 address->u.Byte[n_bytes] = ip_component;
1144 n_bytes++;
1146 if (n_ipv4_bytes == 4 || *str != '.') break;
1147 n_ipv4_bytes++;
1149 else
1151 /* IPv6 component */
1152 if (!parse_ipv6_component(&str, 16, &ip_component)) goto error;
1153 if (prev_str[0] == '0' && (prev_str[1] == 'x' || prev_str[1] == 'X'))
1155 /* Windows "feature": the last IPv6 component can start with "0x" and be longer than 4 digits */
1156 if (terminator) *terminator = prev_str + 1; /* Windows says that the "x" is the terminator */
1157 if (n_bytes < 14 && gap == -1) return STATUS_INVALID_PARAMETER;
1158 address->u.Word[n_bytes/2] = htons(ip_component);
1159 n_bytes += 2;
1160 has_0x = TRUE;
1161 goto fill_gap;
1163 if (*str != ':' && n_bytes < 14 && gap == -1) goto error;
1164 if (str - prev_str > 4)
1165 too_big = TRUE;
1166 else
1167 address->u.Word[n_bytes/2] = htons(ip_component);
1168 n_bytes += 2;
1169 if (*str != ':' || (gap != -1 && str[1] == ':')) break;
1171 if (n_bytes == (gap != -1 ? 14 : 16)) break;
1172 if (too_big) return STATUS_INVALID_PARAMETER;
1173 str++;
1176 if (terminator) *terminator = str;
1177 if (too_big) return STATUS_INVALID_PARAMETER;
1179 fill_gap:
1180 if (gap == -1)
1182 if (n_bytes < 16) goto error;
1184 else
1186 memmove(address->u.Byte + 16 - (n_bytes - gap), address->u.Byte + gap, n_bytes - gap);
1187 memset(address->u.Byte + gap, 0, 16 - n_bytes);
1190 if (ex)
1192 if (has_0x) goto error;
1194 if (*str == '%')
1196 str++;
1197 if (!parse_ipv4_component(&str, TRUE, &scope_component)) goto error;
1200 if (expecting_port)
1202 if (*str != ']') goto error;
1203 str++;
1204 if (*str == ':')
1206 str++;
1207 if (!parse_ipv4_component(&str, FALSE, &port_component)) goto error;
1208 if (!port_component || port_component > 0xFFFF || *str) goto error;
1209 port_component = htons(port_component);
1214 if (!terminator && *str) return STATUS_INVALID_PARAMETER;
1216 if (scope) *scope = scope_component;
1217 if (port) *port = port_component;
1219 return STATUS_SUCCESS;
1221 error:
1222 if (terminator) *terminator = str;
1223 return STATUS_INVALID_PARAMETER;
1226 /***********************************************************************
1227 * RtlIpv6StringToAddressExW [NTDLL.@]
1229 NTSTATUS NTAPI RtlIpv6StringToAddressExW(const WCHAR *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1231 TRACE("(%s, %p, %p, %p)\n", debugstr_w(str), address, scope, port);
1232 if (!str || !address || !scope || !port) return STATUS_INVALID_PARAMETER;
1233 return ipv6_string_to_address(str, TRUE, NULL, address, scope, port);
1236 /***********************************************************************
1237 * RtlIpv6StringToAddressW [NTDLL.@]
1239 NTSTATUS WINAPI RtlIpv6StringToAddressW(const WCHAR *str, const WCHAR **terminator, IN6_ADDR *address)
1241 TRACE("(%s, %p, %p)\n", debugstr_w(str), terminator, address);
1242 return ipv6_string_to_address(str, FALSE, terminator, address, NULL, NULL);
1245 /***********************************************************************
1246 * RtlIpv6StringToAddressExA [NTDLL.@]
1248 NTSTATUS WINAPI RtlIpv6StringToAddressExA(const char *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1250 WCHAR wstr[128];
1252 TRACE("(%s, %p, %p, %p)\n", debugstr_a(str), address, scope, port);
1254 if (!str || !address || !scope || !port)
1255 return STATUS_INVALID_PARAMETER;
1257 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1258 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1259 return ipv6_string_to_address(wstr, TRUE, NULL, address, scope, port);
1262 /***********************************************************************
1263 * RtlIpv6StringToAddressA [NTDLL.@]
1265 NTSTATUS WINAPI RtlIpv6StringToAddressA(const char *str, const char **terminator, IN6_ADDR *address)
1267 WCHAR wstr[128];
1268 const WCHAR *wterminator = NULL;
1269 NTSTATUS ret;
1271 TRACE("(%s, %p, %p)\n", debugstr_a(str), terminator, address);
1273 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1274 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1275 ret = ipv6_string_to_address(wstr, FALSE, &wterminator, address, NULL, NULL);
1276 if (terminator && wterminator) *terminator = str + (wterminator - wstr);
1277 return ret;
1280 /***********************************************************************
1281 * RtlIpv4AddressToStringExW [NTDLL.@]
1283 * Convert the given ipv4 address and optional the port to a string
1285 * PARAMS
1286 * pin [I] PTR to the ip address to convert (network byte order)
1287 * port [I] optional port to convert (network byte order)
1288 * buffer [O] destination buffer for the result
1289 * psize [IO] PTR to available/used size of the destination buffer
1291 * RETURNS
1292 * Success: STATUS_SUCCESS
1293 * Failure: STATUS_INVALID_PARAMETER
1296 NTSTATUS WINAPI RtlIpv4AddressToStringExW(const IN_ADDR *pin, USHORT port, LPWSTR buffer, PULONG psize)
1298 WCHAR tmp_ip[32];
1299 ULONG needed;
1301 if (!pin || !buffer || !psize)
1302 return STATUS_INVALID_PARAMETER;
1304 TRACE("(%p:0x%lx, %d, %p, %p:%ld)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1306 needed = swprintf(tmp_ip, ARRAY_SIZE(tmp_ip), L"%u.%u.%u.%u",
1307 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1308 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1310 if (port) needed += swprintf(tmp_ip + needed, ARRAY_SIZE(tmp_ip) - needed, L":%u", ntohs(port));
1312 if (*psize > needed) {
1313 *psize = needed + 1;
1314 wcscpy(buffer, tmp_ip);
1315 return STATUS_SUCCESS;
1318 *psize = needed + 1;
1319 return STATUS_INVALID_PARAMETER;
1322 /***********************************************************************
1323 * RtlIpv4AddressToStringExA [NTDLL.@]
1325 * Convert the given ipv4 address and optional the port to a string
1327 * See RtlIpv4AddressToStringExW
1329 NTSTATUS WINAPI RtlIpv4AddressToStringExA(const IN_ADDR *pin, USHORT port, LPSTR buffer, PULONG psize)
1331 CHAR tmp_ip[32];
1332 ULONG needed;
1334 if (!pin || !buffer || !psize)
1335 return STATUS_INVALID_PARAMETER;
1337 TRACE("(%p:0x%lx, %d, %p, %p:%ld)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1339 needed = sprintf(tmp_ip, "%u.%u.%u.%u",
1340 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1341 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1343 if (port) needed += sprintf(tmp_ip + needed, ":%u", ntohs(port));
1345 if (*psize > needed) {
1346 *psize = needed + 1;
1347 strcpy(buffer, tmp_ip);
1348 return STATUS_SUCCESS;
1351 *psize = needed + 1;
1352 return STATUS_INVALID_PARAMETER;
1355 /***********************************************************************
1356 * RtlIpv4AddressToStringW [NTDLL.@]
1358 * Convert the given ipv4 address to a string
1360 * PARAMS
1361 * pin [I] PTR to the ip address to convert (network byte order)
1362 * buffer [O] destination buffer for the result (at least 16 character)
1364 * RETURNS
1365 * PTR to the 0 character at the end of the converted string
1368 WCHAR * WINAPI RtlIpv4AddressToStringW(const IN_ADDR *pin, LPWSTR buffer)
1370 ULONG size = 16;
1372 if (RtlIpv4AddressToStringExW(pin, 0, buffer, &size)) size = 0;
1373 return buffer + size - 1;
1376 /***********************************************************************
1377 * RtlIpv4AddressToStringA [NTDLL.@]
1379 * Convert the given ipv4 address to a string
1381 * See RtlIpv4AddressToStringW
1383 CHAR * WINAPI RtlIpv4AddressToStringA(const IN_ADDR *pin, LPSTR buffer)
1385 ULONG size = 16;
1387 if (RtlIpv4AddressToStringExA(pin, 0, buffer, &size)) size = 0;
1388 return buffer + size - 1;
1391 static BOOL is_ipv4_in_ipv6(const IN6_ADDR *address)
1393 if (address->s6_words[5] == htons(0x5efe) && (address->s6_words[4] & ~htons(0x200)) == 0)
1394 return TRUE;
1395 if (*(UINT64 *)address != 0)
1396 return FALSE;
1397 if (address->s6_words[4] != 0 && address->s6_words[4] != 0xffff)
1398 return FALSE;
1399 if (address->s6_words[4] == 0 && address->s6_words[5] != 0 && address->s6_words[5] != 0xffff)
1400 return FALSE;
1401 if (address->s6_words[4] == 0xffff && address->s6_words[5] != 0)
1402 return FALSE;
1403 if (address->s6_words[6] == 0)
1404 return FALSE;
1405 return TRUE;
1408 /***********************************************************************
1409 * RtlIpv6AddressToStringExA [NTDLL.@]
1411 NTSTATUS WINAPI RtlIpv6AddressToStringExA(const IN6_ADDR *address, ULONG scope, USHORT port, char *str, ULONG *size)
1413 char buffer[64], *p = buffer;
1414 int i, len, gap = -1, gap_len = 1, ipv6_end = 8;
1415 ULONG needed;
1416 NTSTATUS ret;
1418 TRACE("(%p %lu %u %p %p)\n", address, scope, port, str, size);
1420 if (!address || !str || !size)
1421 return STATUS_INVALID_PARAMETER;
1423 if (is_ipv4_in_ipv6(address))
1424 ipv6_end = 6;
1426 for (i = 0; i < ipv6_end; i++)
1428 len = 0;
1429 while (!address->s6_words[i] && i < ipv6_end)
1431 i++;
1432 len++;
1434 if (len > gap_len)
1436 gap = i - len;
1437 gap_len = len;
1441 if (port) p += sprintf(p, "[");
1443 i = 0;
1444 while (i < ipv6_end)
1446 if (i == gap)
1448 p += sprintf(p, ":");
1449 i += gap_len;
1450 if (i == ipv6_end) p += sprintf(p, ":");
1451 continue;
1453 if (i > 0) p += sprintf(p, ":");
1454 p += sprintf(p, "%x", ntohs(address->s6_words[i]));
1455 i++;
1458 if (ipv6_end == 6)
1460 if (p[-1] != ':') p += sprintf(p, ":");
1461 p = RtlIpv4AddressToStringA((IN_ADDR *)(address->s6_words + 6), p);
1464 if (scope) p += sprintf(p, "%%%lu", scope);
1466 if (port) p += sprintf(p, "]:%u", ntohs(port));
1468 needed = p - buffer + 1;
1470 if (*size >= needed)
1472 strcpy(str, buffer);
1473 ret = STATUS_SUCCESS;
1475 else
1477 ret = STATUS_INVALID_PARAMETER;
1480 *size = needed;
1481 return ret;
1484 /***********************************************************************
1485 * RtlIpv6AddressToStringA [NTDLL.@]
1487 char * WINAPI RtlIpv6AddressToStringA(const IN6_ADDR *address, char *str)
1489 ULONG size = 46;
1490 if (!address || !str) return str - 1;
1491 str[45] = 0; /* this byte is set even though the string is always shorter */
1492 RtlIpv6AddressToStringExA(address, 0, 0, str, &size);
1493 return str + size - 1;
1496 /***********************************************************************
1497 * RtlIpv6AddressToStringExW [NTDLL.@]
1499 NTSTATUS WINAPI RtlIpv6AddressToStringExW(const IN6_ADDR *address, ULONG scope, USHORT port, WCHAR *str, ULONG *size)
1501 char cstr[64];
1502 NTSTATUS ret = RtlIpv6AddressToStringExA(address, scope, port, cstr, size);
1503 if (ret == STATUS_SUCCESS) RtlMultiByteToUnicodeN(str, *size * sizeof(WCHAR), NULL, cstr, *size);
1504 return ret;
1507 /***********************************************************************
1508 * RtlIpv6AddressToStringW [NTDLL.@]
1510 WCHAR * WINAPI RtlIpv6AddressToStringW(const IN6_ADDR *address, WCHAR *str)
1512 ULONG size = 46;
1513 if (!address || !str) return str;
1514 str[45] = 0; /* this word is set even though the string is always shorter */
1515 if (RtlIpv6AddressToStringExW(address, 0, 0, str, &size) != STATUS_SUCCESS)
1516 return str;
1517 return str + size - 1;
1520 /***********************************************************************
1521 * get_pointer_obfuscator (internal)
1523 static DWORD_PTR get_pointer_obfuscator( void )
1525 static DWORD_PTR pointer_obfuscator;
1527 if (!pointer_obfuscator)
1529 ULONG seed = NtGetTickCount();
1530 ULONG_PTR rand;
1532 /* generate a random value for the obfuscator */
1533 rand = RtlUniform( &seed );
1535 /* handle 64bit pointers */
1536 rand ^= (ULONG_PTR)RtlUniform( &seed ) << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1538 /* set the high bits so dereferencing obfuscated pointers will (usually) crash */
1539 rand |= (ULONG_PTR)0xc0000000 << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1541 InterlockedCompareExchangePointer( (void**) &pointer_obfuscator, (void*) rand, NULL );
1544 return pointer_obfuscator;
1547 /*************************************************************************
1548 * RtlEncodePointer [NTDLL.@]
1550 PVOID WINAPI RtlEncodePointer( PVOID ptr )
1552 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1553 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1556 PVOID WINAPI RtlDecodePointer( PVOID ptr )
1558 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1559 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1562 /*************************************************************************
1563 * RtlInitializeSListHead [NTDLL.@]
1565 VOID WINAPI RtlInitializeSListHead(PSLIST_HEADER list)
1567 #ifdef _WIN64
1568 list->Alignment = list->Region = 0;
1569 list->Header16.HeaderType = 1; /* we use the 16-byte header */
1570 #else
1571 list->Alignment = 0;
1572 #endif
1575 /*************************************************************************
1576 * RtlQueryDepthSList [NTDLL.@]
1578 WORD WINAPI RtlQueryDepthSList(PSLIST_HEADER list)
1580 #ifdef _WIN64
1581 return list->Header16.Depth;
1582 #else
1583 return list->Depth;
1584 #endif
1587 /*************************************************************************
1588 * RtlFirstEntrySList [NTDLL.@]
1590 PSLIST_ENTRY WINAPI RtlFirstEntrySList(const SLIST_HEADER* list)
1592 #ifdef _WIN64
1593 return (SLIST_ENTRY *)((ULONG_PTR)list->Header16.NextEntry << 4);
1594 #else
1595 return list->Next.Next;
1596 #endif
1599 /*************************************************************************
1600 * RtlInterlockedFlushSList [NTDLL.@]
1602 PSLIST_ENTRY WINAPI RtlInterlockedFlushSList(PSLIST_HEADER list)
1604 SLIST_HEADER old, new;
1606 #ifdef _WIN64
1607 if (!list->Header16.NextEntry) return NULL;
1608 new.Alignment = new.Region = 0;
1609 new.Header16.HeaderType = 1; /* we use the 16-byte header */
1612 old = *list;
1613 new.Header16.Sequence = old.Header16.Sequence + 1;
1614 } while (!InterlockedCompareExchange128((__int64 *)list, new.Region, new.Alignment, (__int64 *)&old));
1615 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1616 #else
1617 if (!list->Next.Next) return NULL;
1618 new.Alignment = 0;
1621 old = *list;
1622 new.Sequence = old.Sequence + 1;
1623 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1624 old.Alignment) != old.Alignment);
1625 return old.Next.Next;
1626 #endif
1629 /*************************************************************************
1630 * RtlInterlockedPushEntrySList [NTDLL.@]
1632 PSLIST_ENTRY WINAPI RtlInterlockedPushEntrySList(PSLIST_HEADER list, PSLIST_ENTRY entry)
1634 SLIST_HEADER old, new;
1636 #ifdef _WIN64
1637 new.Header16.NextEntry = (ULONG_PTR)entry >> 4;
1640 old = *list;
1641 entry->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1642 new.Header16.Depth = old.Header16.Depth + 1;
1643 new.Header16.Sequence = old.Header16.Sequence + 1;
1644 } while (!InterlockedCompareExchange128((__int64 *)list, new.Region, new.Alignment, (__int64 *)&old));
1645 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1646 #else
1647 new.Next.Next = entry;
1650 old = *list;
1651 entry->Next = old.Next.Next;
1652 new.Depth = old.Depth + 1;
1653 new.Sequence = old.Sequence + 1;
1654 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1655 old.Alignment) != old.Alignment);
1656 return old.Next.Next;
1657 #endif
1660 /*************************************************************************
1661 * RtlInterlockedPopEntrySList [NTDLL.@]
1663 PSLIST_ENTRY WINAPI RtlInterlockedPopEntrySList(PSLIST_HEADER list)
1665 SLIST_HEADER old, new;
1666 PSLIST_ENTRY entry;
1668 #ifdef _WIN64
1671 old = *list;
1672 if (!(entry = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4))) return NULL;
1673 /* entry could be deleted by another thread */
1674 __TRY
1676 new.Header16.NextEntry = (ULONG_PTR)entry->Next >> 4;
1677 new.Header16.Depth = old.Header16.Depth - 1;
1678 new.Header16.Sequence = old.Header16.Sequence + 1;
1680 __EXCEPT_PAGE_FAULT
1683 __ENDTRY
1684 } while (!InterlockedCompareExchange128((__int64 *)list, new.Region, new.Alignment, (__int64 *)&old));
1685 #else
1688 old = *list;
1689 if (!(entry = old.Next.Next)) return NULL;
1690 /* entry could be deleted by another thread */
1691 __TRY
1693 new.Next.Next = entry->Next;
1694 new.Depth = old.Depth - 1;
1695 new.Sequence = old.Sequence + 1;
1697 __EXCEPT_PAGE_FAULT
1700 __ENDTRY
1701 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1702 old.Alignment) != old.Alignment);
1703 #endif
1704 return entry;
1707 /*************************************************************************
1708 * RtlInterlockedPushListSListEx [NTDLL.@]
1710 PSLIST_ENTRY WINAPI RtlInterlockedPushListSListEx(PSLIST_HEADER list, PSLIST_ENTRY first,
1711 PSLIST_ENTRY last, ULONG count)
1713 SLIST_HEADER old, new;
1715 #ifdef _WIN64
1716 new.Header16.NextEntry = (ULONG_PTR)first >> 4;
1719 old = *list;
1720 new.Header16.Depth = old.Header16.Depth + count;
1721 new.Header16.Sequence = old.Header16.Sequence + 1;
1722 last->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1723 } while (!InterlockedCompareExchange128((__int64 *)list, new.Region, new.Alignment, (__int64 *)&old));
1724 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1725 #else
1726 new.Next.Next = first;
1729 old = *list;
1730 new.Depth = old.Depth + count;
1731 new.Sequence = old.Sequence + 1;
1732 last->Next = old.Next.Next;
1733 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1734 old.Alignment) != old.Alignment);
1735 return old.Next.Next;
1736 #endif
1739 /*************************************************************************
1740 * RtlInterlockedPushListSList [NTDLL.@]
1742 DEFINE_FASTCALL_WRAPPER(RtlInterlockedPushListSList, 16)
1743 PSLIST_ENTRY FASTCALL RtlInterlockedPushListSList(PSLIST_HEADER list, PSLIST_ENTRY first,
1744 PSLIST_ENTRY last, ULONG count)
1746 return RtlInterlockedPushListSListEx(list, first, last, count);
1749 /******************************************************************************
1750 * RtlGetCompressionWorkSpaceSize [NTDLL.@]
1752 NTSTATUS WINAPI RtlGetCompressionWorkSpaceSize(USHORT format, PULONG compress_workspace,
1753 PULONG decompress_workspace)
1755 FIXME("0x%04x, %p, %p: semi-stub\n", format, compress_workspace, decompress_workspace);
1757 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1759 case COMPRESSION_FORMAT_LZNT1:
1760 if (compress_workspace)
1762 /* FIXME: The current implementation of RtlCompressBuffer does not use a
1763 * workspace buffer, but Windows applications might expect a nonzero value. */
1764 *compress_workspace = 16;
1766 if (decompress_workspace)
1767 *decompress_workspace = 0x1000;
1768 return STATUS_SUCCESS;
1770 case COMPRESSION_FORMAT_NONE:
1771 case COMPRESSION_FORMAT_DEFAULT:
1772 return STATUS_INVALID_PARAMETER;
1774 default:
1775 FIXME("format %u not implemented\n", format);
1776 return STATUS_UNSUPPORTED_COMPRESSION;
1780 /* compress data using LZNT1, currently only a stub */
1781 static NTSTATUS lznt1_compress(UCHAR *src, ULONG src_size, UCHAR *dst, ULONG dst_size,
1782 ULONG chunk_size, ULONG *final_size, UCHAR *workspace)
1784 UCHAR *src_cur = src, *src_end = src + src_size;
1785 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1786 ULONG block_size;
1788 while (src_cur < src_end)
1790 /* determine size of current chunk */
1791 block_size = min(0x1000, src_end - src_cur);
1792 if (dst_cur + sizeof(WORD) + block_size > dst_end)
1793 return STATUS_BUFFER_TOO_SMALL;
1795 /* write (uncompressed) chunk header */
1796 *(WORD *)dst_cur = 0x3000 | (block_size - 1);
1797 dst_cur += sizeof(WORD);
1799 /* write chunk content */
1800 memcpy(dst_cur, src_cur, block_size);
1801 dst_cur += block_size;
1802 src_cur += block_size;
1805 if (final_size)
1806 *final_size = dst_cur - dst;
1808 return STATUS_SUCCESS;
1811 /******************************************************************************
1812 * RtlCompressBuffer [NTDLL.@]
1814 NTSTATUS WINAPI RtlCompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
1815 PUCHAR compressed, ULONG compressed_size, ULONG chunk_size,
1816 PULONG final_size, PVOID workspace)
1818 FIXME("0x%04x, %p, %lu, %p, %lu, %lu, %p, %p: semi-stub\n", format, uncompressed,
1819 uncompressed_size, compressed, compressed_size, chunk_size, final_size, workspace);
1821 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1823 case COMPRESSION_FORMAT_LZNT1:
1824 return lznt1_compress(uncompressed, uncompressed_size, compressed,
1825 compressed_size, chunk_size, final_size, workspace);
1827 case COMPRESSION_FORMAT_NONE:
1828 case COMPRESSION_FORMAT_DEFAULT:
1829 return STATUS_INVALID_PARAMETER;
1831 default:
1832 FIXME("format %u not implemented\n", format);
1833 return STATUS_UNSUPPORTED_COMPRESSION;
1837 /* decompress a single LZNT1 chunk */
1838 static UCHAR *lznt1_decompress_chunk(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size)
1840 UCHAR *src_cur = src, *src_end = src + src_size;
1841 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1842 ULONG displacement_bits, length_bits;
1843 ULONG code_displacement, code_length;
1844 WORD flags, code;
1846 while (src_cur < src_end && dst_cur < dst_end)
1848 flags = 0x8000 | *src_cur++;
1849 while ((flags & 0xff00) && src_cur < src_end)
1851 if (flags & 1)
1853 /* backwards reference */
1854 if (src_cur + sizeof(WORD) > src_end)
1855 return NULL;
1857 code = *(WORD *)src_cur;
1858 src_cur += sizeof(WORD);
1860 /* find length / displacement bits */
1861 for (displacement_bits = 12; displacement_bits > 4; displacement_bits--)
1862 if ((1 << (displacement_bits - 1)) < dst_cur - dst) break;
1864 length_bits = 16 - displacement_bits;
1865 code_length = (code & ((1 << length_bits) - 1)) + 3;
1866 code_displacement = (code >> length_bits) + 1;
1868 if (dst_cur < dst + code_displacement)
1869 return NULL;
1871 /* copy bytes of chunk - we can't use memcpy() since source and dest can
1872 * be overlapping, and the same bytes can be repeated over and over again */
1873 while (code_length--)
1875 if (dst_cur >= dst_end) return dst_cur;
1876 *dst_cur = *(dst_cur - code_displacement);
1877 dst_cur++;
1880 else
1882 /* uncompressed data */
1883 if (dst_cur >= dst_end) return dst_cur;
1884 *dst_cur++ = *src_cur++;
1886 flags >>= 1;
1890 return dst_cur;
1893 /* decompress data encoded with LZNT1 */
1894 static NTSTATUS lznt1_decompress(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size,
1895 ULONG offset, ULONG *final_size, UCHAR *workspace)
1897 UCHAR *src_cur = src, *src_end = src + src_size;
1898 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1899 ULONG chunk_size, block_size;
1900 WORD chunk_header;
1901 UCHAR *ptr;
1903 if (src_cur + sizeof(WORD) > src_end)
1904 return STATUS_BAD_COMPRESSION_BUFFER;
1906 /* skip over chunks with a distance >= 0x1000 to the destination offset */
1907 while (offset >= 0x1000 && src_cur + sizeof(WORD) <= src_end)
1909 chunk_header = *(WORD *)src_cur;
1910 src_cur += sizeof(WORD);
1911 if (!chunk_header) goto out;
1912 chunk_size = (chunk_header & 0xfff) + 1;
1914 if (src_cur + chunk_size > src_end)
1915 return STATUS_BAD_COMPRESSION_BUFFER;
1917 src_cur += chunk_size;
1918 offset -= 0x1000;
1921 /* handle partially included chunk */
1922 if (offset && src_cur + sizeof(WORD) <= src_end)
1924 chunk_header = *(WORD *)src_cur;
1925 src_cur += sizeof(WORD);
1926 if (!chunk_header) goto out;
1927 chunk_size = (chunk_header & 0xfff) + 1;
1929 if (src_cur + chunk_size > src_end)
1930 return STATUS_BAD_COMPRESSION_BUFFER;
1932 if (dst_cur >= dst_end)
1933 goto out;
1935 if (chunk_header & 0x8000)
1937 /* compressed chunk */
1938 if (!workspace) return STATUS_ACCESS_VIOLATION;
1939 ptr = lznt1_decompress_chunk(workspace, 0x1000, src_cur, chunk_size);
1940 if (!ptr) return STATUS_BAD_COMPRESSION_BUFFER;
1941 if (ptr - workspace > offset)
1943 block_size = min((ptr - workspace) - offset, dst_end - dst_cur);
1944 memcpy(dst_cur, workspace + offset, block_size);
1945 dst_cur += block_size;
1948 else
1950 /* uncompressed chunk */
1951 if (chunk_size > offset)
1953 block_size = min(chunk_size - offset, dst_end - dst_cur);
1954 memcpy(dst_cur, src_cur + offset, block_size);
1955 dst_cur += block_size;
1959 src_cur += chunk_size;
1962 /* handle remaining chunks */
1963 while (src_cur + sizeof(WORD) <= src_end)
1965 chunk_header = *(WORD *)src_cur;
1966 src_cur += sizeof(WORD);
1967 if (!chunk_header) goto out;
1968 chunk_size = (chunk_header & 0xfff) + 1;
1970 if (src_cur + chunk_size > src_end)
1971 return STATUS_BAD_COMPRESSION_BUFFER;
1973 /* fill space with padding when the previous chunk was decompressed
1974 * to less than 4096 bytes. no padding is needed for the last chunk
1975 * or when the next chunk is truncated */
1976 block_size = ((dst_cur - dst) + offset) & 0xfff;
1977 if (block_size)
1979 block_size = 0x1000 - block_size;
1980 if (dst_cur + block_size >= dst_end)
1981 goto out;
1982 memset(dst_cur, 0, block_size);
1983 dst_cur += block_size;
1986 if (dst_cur >= dst_end)
1987 goto out;
1989 if (chunk_header & 0x8000)
1991 /* compressed chunk */
1992 dst_cur = lznt1_decompress_chunk(dst_cur, dst_end - dst_cur, src_cur, chunk_size);
1993 if (!dst_cur) return STATUS_BAD_COMPRESSION_BUFFER;
1995 else
1997 /* uncompressed chunk */
1998 block_size = min(chunk_size, dst_end - dst_cur);
1999 memcpy(dst_cur, src_cur, block_size);
2000 dst_cur += block_size;
2003 src_cur += chunk_size;
2006 out:
2007 if (final_size)
2008 *final_size = dst_cur - dst;
2010 return STATUS_SUCCESS;
2014 /******************************************************************************
2015 * RtlDecompressFragment [NTDLL.@]
2017 NTSTATUS WINAPI RtlDecompressFragment(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2018 PUCHAR compressed, ULONG compressed_size, ULONG offset,
2019 PULONG final_size, PVOID workspace)
2021 TRACE("0x%04x, %p, %lu, %p, %lu, %lu, %p, %p\n", format, uncompressed,
2022 uncompressed_size, compressed, compressed_size, offset, final_size, workspace);
2024 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
2026 case COMPRESSION_FORMAT_LZNT1:
2027 return lznt1_decompress(uncompressed, uncompressed_size, compressed,
2028 compressed_size, offset, final_size, workspace);
2030 case COMPRESSION_FORMAT_NONE:
2031 case COMPRESSION_FORMAT_DEFAULT:
2032 return STATUS_INVALID_PARAMETER;
2034 default:
2035 FIXME("format %u not implemented\n", format);
2036 return STATUS_UNSUPPORTED_COMPRESSION;
2041 /******************************************************************************
2042 * RtlDecompressBuffer [NTDLL.@]
2044 NTSTATUS WINAPI RtlDecompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2045 PUCHAR compressed, ULONG compressed_size, PULONG final_size)
2047 TRACE("0x%04x, %p, %lu, %p, %lu, %p\n", format, uncompressed,
2048 uncompressed_size, compressed, compressed_size, final_size);
2050 return RtlDecompressFragment(format, uncompressed, uncompressed_size,
2051 compressed, compressed_size, 0, final_size, NULL);
2054 /***********************************************************************
2055 * RtlSetThreadErrorMode [NTDLL.@]
2057 * Set the thread local error mode.
2059 * PARAMS
2060 * mode [I] The new error mode
2061 * oldmode [O] Destination of the old error mode (may be NULL)
2063 * RETURNS
2064 * Success: STATUS_SUCCESS
2065 * Failure: STATUS_INVALID_PARAMETER_1
2067 NTSTATUS WINAPI RtlSetThreadErrorMode( DWORD mode, LPDWORD oldmode )
2069 if (mode & ~0x70)
2070 return STATUS_INVALID_PARAMETER_1;
2072 if (oldmode)
2073 *oldmode = NtCurrentTeb()->HardErrorMode;
2075 NtCurrentTeb()->HardErrorMode = mode;
2076 return STATUS_SUCCESS;
2079 /***********************************************************************
2080 * RtlGetThreadErrorMode [NTDLL.@]
2082 * Get the thread local error mode.
2084 * PARAMS
2085 * None.
2087 * RETURNS
2088 * The current thread local error mode.
2090 DWORD WINAPI RtlGetThreadErrorMode( void )
2092 return NtCurrentTeb()->HardErrorMode;
2095 /******************************************************************************
2096 * RtlGetCurrentTransaction [NTDLL.@]
2098 HANDLE WINAPI RtlGetCurrentTransaction(void)
2100 FIXME("() :stub\n");
2101 return NULL;
2104 /******************************************************************************
2105 * RtlSetCurrentTransaction [NTDLL.@]
2107 BOOL WINAPI RtlSetCurrentTransaction(HANDLE new_transaction)
2109 FIXME("(%p) :stub\n", new_transaction);
2110 return FALSE;
2113 /**********************************************************************
2114 * RtlGetCurrentProcessorNumberEx [NTDLL.@]
2116 void WINAPI RtlGetCurrentProcessorNumberEx(PROCESSOR_NUMBER *processor)
2118 FIXME("(%p) :semi-stub\n", processor);
2119 processor->Group = 0;
2120 processor->Number = NtGetCurrentProcessorNumber();
2121 processor->Reserved = 0;
2124 /***********************************************************************
2125 * RtlIsProcessorFeaturePresent [NTDLL.@]
2127 BOOLEAN WINAPI RtlIsProcessorFeaturePresent( UINT feature )
2129 #ifdef __aarch64__
2130 static const ULONGLONG arm64_features =
2131 (1ull << PF_COMPARE_EXCHANGE_DOUBLE) |
2132 (1ull << PF_NX_ENABLED) |
2133 (1ull << PF_ARM_VFP_32_REGISTERS_AVAILABLE) |
2134 (1ull << PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) |
2135 (1ull << PF_SECOND_LEVEL_ADDRESS_TRANSLATION) |
2136 (1ull << PF_FASTFAIL_AVAILABLE) |
2137 (1ull << PF_ARM_DIVIDE_INSTRUCTION_AVAILABLE) |
2138 (1ull << PF_ARM_64BIT_LOADSTORE_ATOMIC) |
2139 (1ull << PF_ARM_EXTERNAL_CACHE_AVAILABLE) |
2140 (1ull << PF_ARM_FMAC_INSTRUCTIONS_AVAILABLE) |
2141 (1ull << PF_ARM_V8_INSTRUCTIONS_AVAILABLE) |
2142 (1ull << PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) |
2143 (1ull << PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) |
2144 (1ull << PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE) |
2145 (1ull << PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE) |
2146 (1ull << PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE) |
2147 (1ull << PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE);
2149 return (feature < PROCESSOR_FEATURE_MAX && (arm64_features & (1ull << feature)) &&
2150 user_shared_data->ProcessorFeatures[feature]);
2151 #elif defined _WIN64
2152 return feature < PROCESSOR_FEATURE_MAX && user_shared_data->ProcessorFeatures[feature];
2153 #else
2154 return NtWow64IsProcessorFeaturePresent( feature );
2155 #endif
2158 /***********************************************************************
2159 * RtlInitializeGenericTableAvl (NTDLL.@)
2161 void WINAPI RtlInitializeGenericTableAvl(PRTL_AVL_TABLE table, PRTL_AVL_COMPARE_ROUTINE compare,
2162 PRTL_AVL_ALLOCATE_ROUTINE allocate, PRTL_AVL_FREE_ROUTINE free, void *context)
2164 FIXME("%p %p %p %p %p: stub\n", table, compare, allocate, free, context);
2167 /***********************************************************************
2168 * RtlInsertElementGenericTableAvl (NTDLL.@)
2170 void WINAPI RtlInsertElementGenericTableAvl(PRTL_AVL_TABLE table, void *buffer, ULONG size, BOOL *element)
2172 FIXME("%p %p %lu %p: stub\n", table, buffer, size, element);
2175 /*********************************************************************
2176 * RtlQueryPackageIdentity [NTDLL.@]
2178 NTSTATUS WINAPI RtlQueryPackageIdentity(HANDLE token, WCHAR *fullname, SIZE_T *fullname_size,
2179 WCHAR *appid, SIZE_T *appid_size, BOOLEAN *packaged)
2181 FIXME("(%p, %p, %p, %p, %p, %p): stub\n", token, fullname, fullname_size, appid, appid_size, packaged);
2182 return STATUS_NOT_FOUND;
2185 /*********************************************************************
2186 * RtlQueryProcessPlaceholderCompatibilityMode [NTDLL.@]
2188 char WINAPI RtlQueryProcessPlaceholderCompatibilityMode(void)
2190 FIXME("stub\n");
2191 return PHCM_APPLICATION_DEFAULT;