kernelbase: Use the correct machine type for ARM.
[wine/zf.git] / dlls / ntdll / rtl.c
blobcf21c980a45ca8c3de51ba3b87283cbef67602bf
1 /*
2 * NT basis DLL
4 * This file contains the Rtl* API functions. These should be implementable.
6 * Copyright 1996-1998 Marcus Meissner
7 * Copyright 1999 Alex Korobka
8 * Copyright 2003 Thomas Mertes
9 * Crc32 code Copyright 1986 Gary S. Brown (Public domain)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <stdarg.h>
28 #include "ntstatus.h"
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #define WIN32_NO_STATUS
32 #include "winsock2.h"
33 #include "windef.h"
34 #include "winternl.h"
35 #include "wine/debug.h"
36 #include "wine/exception.h"
37 #include "ntdll_misc.h"
38 #include "in6addr.h"
39 #include "ddk/ntddk.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
42 WINE_DECLARE_DEBUG_CHANNEL(debugstr);
44 /* CRC polynomial 0xedb88320 */
45 static const DWORD CRC_table[256] =
47 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
48 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
49 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
50 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
51 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
52 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
53 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
54 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
55 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
56 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
57 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
58 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
59 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
60 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
61 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
62 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
63 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
64 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
65 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
66 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
67 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
68 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
69 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
70 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
71 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
72 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
73 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
74 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
75 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
76 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
77 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
78 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
79 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
80 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
81 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
82 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
83 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
84 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
85 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
86 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
87 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
88 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
89 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
92 static const int hex_table[] = {
93 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
94 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
95 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2F */
96 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
97 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4F */
98 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
99 -1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
103 * resource functions
106 /***********************************************************************
107 * RtlInitializeResource (NTDLL.@)
109 * xxxResource() functions implement multiple-reader-single-writer lock.
110 * The code is based on information published in WDJ January 1999 issue.
112 void WINAPI RtlInitializeResource(LPRTL_RWLOCK rwl)
114 if( rwl )
116 rwl->iNumberActive = 0;
117 rwl->uExclusiveWaiters = 0;
118 rwl->uSharedWaiters = 0;
119 rwl->hOwningThreadId = 0;
120 rwl->dwTimeoutBoost = 0; /* no info on this one, default value is 0 */
121 RtlInitializeCriticalSection( &rwl->rtlCS );
122 rwl->rtlCS.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RTL_RWLOCK.rtlCS");
123 NtCreateSemaphore( &rwl->hExclusiveReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
124 NtCreateSemaphore( &rwl->hSharedReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
129 /***********************************************************************
130 * RtlDeleteResource (NTDLL.@)
132 void WINAPI RtlDeleteResource(LPRTL_RWLOCK rwl)
134 if( rwl )
136 RtlEnterCriticalSection( &rwl->rtlCS );
137 if( rwl->iNumberActive || rwl->uExclusiveWaiters || rwl->uSharedWaiters )
138 ERR("Deleting active MRSW lock (%p), expect failure\n", rwl );
139 rwl->hOwningThreadId = 0;
140 rwl->uExclusiveWaiters = rwl->uSharedWaiters = 0;
141 rwl->iNumberActive = 0;
142 NtClose( rwl->hExclusiveReleaseSemaphore );
143 NtClose( rwl->hSharedReleaseSemaphore );
144 RtlLeaveCriticalSection( &rwl->rtlCS );
145 rwl->rtlCS.DebugInfo->Spare[0] = 0;
146 RtlDeleteCriticalSection( &rwl->rtlCS );
151 /***********************************************************************
152 * RtlAcquireResourceExclusive (NTDLL.@)
154 BYTE WINAPI RtlAcquireResourceExclusive(LPRTL_RWLOCK rwl, BYTE fWait)
156 BYTE retVal = 0;
157 if( !rwl ) return 0;
159 start:
160 RtlEnterCriticalSection( &rwl->rtlCS );
161 if( rwl->iNumberActive == 0 ) /* lock is free */
163 rwl->iNumberActive = -1;
164 retVal = 1;
166 else if( rwl->iNumberActive < 0 ) /* exclusive lock in progress */
168 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
170 retVal = 1;
171 rwl->iNumberActive--;
172 goto done;
174 wait:
175 if( fWait )
177 NTSTATUS status;
179 rwl->uExclusiveWaiters++;
181 RtlLeaveCriticalSection( &rwl->rtlCS );
182 status = NtWaitForSingleObject( rwl->hExclusiveReleaseSemaphore, FALSE, NULL );
183 if( HIWORD(status) )
184 goto done;
185 goto start; /* restart the acquisition to avoid deadlocks */
188 else /* one or more shared locks are in progress */
189 if( fWait )
190 goto wait;
192 if( retVal == 1 )
193 rwl->hOwningThreadId = ULongToHandle(GetCurrentThreadId());
194 done:
195 RtlLeaveCriticalSection( &rwl->rtlCS );
196 return retVal;
199 /***********************************************************************
200 * RtlAcquireResourceShared (NTDLL.@)
202 BYTE WINAPI RtlAcquireResourceShared(LPRTL_RWLOCK rwl, BYTE fWait)
204 NTSTATUS status = STATUS_UNSUCCESSFUL;
205 BYTE retVal = 0;
206 if( !rwl ) return 0;
208 start:
209 RtlEnterCriticalSection( &rwl->rtlCS );
210 if( rwl->iNumberActive < 0 )
212 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
214 rwl->iNumberActive--;
215 retVal = 1;
216 goto done;
219 if( fWait )
221 rwl->uSharedWaiters++;
222 RtlLeaveCriticalSection( &rwl->rtlCS );
223 status = NtWaitForSingleObject( rwl->hSharedReleaseSemaphore, FALSE, NULL );
224 if( HIWORD(status) )
225 goto done;
226 goto start;
229 else
231 if( status != STATUS_WAIT_0 ) /* otherwise RtlReleaseResource() has already done it */
232 rwl->iNumberActive++;
233 retVal = 1;
235 done:
236 RtlLeaveCriticalSection( &rwl->rtlCS );
237 return retVal;
241 /***********************************************************************
242 * RtlReleaseResource (NTDLL.@)
244 void WINAPI RtlReleaseResource(LPRTL_RWLOCK rwl)
246 RtlEnterCriticalSection( &rwl->rtlCS );
248 if( rwl->iNumberActive > 0 ) /* have one or more readers */
250 if( --rwl->iNumberActive == 0 )
252 if( rwl->uExclusiveWaiters )
254 wake_exclusive:
255 rwl->uExclusiveWaiters--;
256 NtReleaseSemaphore( rwl->hExclusiveReleaseSemaphore, 1, NULL );
260 else
261 if( rwl->iNumberActive < 0 ) /* have a writer, possibly recursive */
263 if( ++rwl->iNumberActive == 0 )
265 rwl->hOwningThreadId = 0;
266 if( rwl->uExclusiveWaiters )
267 goto wake_exclusive;
268 else
269 if( rwl->uSharedWaiters )
271 UINT n = rwl->uSharedWaiters;
272 rwl->iNumberActive = rwl->uSharedWaiters; /* prevent new writers from joining until
273 * all queued readers have done their thing */
274 rwl->uSharedWaiters = 0;
275 NtReleaseSemaphore( rwl->hSharedReleaseSemaphore, n, NULL );
279 RtlLeaveCriticalSection( &rwl->rtlCS );
283 /***********************************************************************
284 * RtlDumpResource (NTDLL.@)
286 void WINAPI RtlDumpResource(LPRTL_RWLOCK rwl)
288 if( rwl )
290 MESSAGE("RtlDumpResource(%p):\n\tactive count = %i\n\twaiting readers = %i\n\twaiting writers = %i\n",
291 rwl, rwl->iNumberActive, rwl->uSharedWaiters, rwl->uExclusiveWaiters );
292 if( rwl->iNumberActive )
293 MESSAGE("\towner thread = %p\n", rwl->hOwningThreadId );
298 * misc functions
301 static LONG WINAPI debug_exception_handler( EXCEPTION_POINTERS *eptr )
303 EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
304 return (rec->ExceptionCode == DBG_PRINTEXCEPTION_C) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
307 /******************************************************************************
308 * DbgPrint [NTDLL.@]
310 NTSTATUS WINAPIV DbgPrint(LPCSTR fmt, ...)
312 NTSTATUS ret;
313 __ms_va_list args;
315 __ms_va_start(args, fmt);
316 ret = vDbgPrintEx(0, DPFLTR_ERROR_LEVEL, fmt, args);
317 __ms_va_end(args);
318 return ret;
322 /******************************************************************************
323 * DbgPrintEx [NTDLL.@]
325 NTSTATUS WINAPIV DbgPrintEx(ULONG iComponentId, ULONG Level, LPCSTR fmt, ...)
327 NTSTATUS ret;
328 __ms_va_list args;
330 __ms_va_start(args, fmt);
331 ret = vDbgPrintEx(iComponentId, Level, fmt, args);
332 __ms_va_end(args);
333 return ret;
336 /******************************************************************************
337 * vDbgPrintEx [NTDLL.@]
339 NTSTATUS WINAPI vDbgPrintEx( ULONG id, ULONG level, LPCSTR fmt, __ms_va_list args )
341 return vDbgPrintExWithPrefix( "", id, level, fmt, args );
344 /******************************************************************************
345 * vDbgPrintExWithPrefix [NTDLL.@]
347 NTSTATUS WINAPI vDbgPrintExWithPrefix( LPCSTR prefix, ULONG id, ULONG level, LPCSTR fmt, __ms_va_list args )
349 ULONG level_mask = level <= 31 ? (1 << level) : level;
350 SIZE_T len = strlen( prefix );
351 char buf[1024], *end;
353 strcpy( buf, prefix );
354 len += _vsnprintf( buf + len, sizeof(buf) - len, fmt, args );
355 end = buf + len - 1;
357 WARN_(debugstr)(*end == '\n' ? "%08x:%08x: %s" : "%08x:%08x: %s\n", id, level_mask, buf);
359 if (level_mask & (1 << DPFLTR_ERROR_LEVEL) && NtCurrentTeb()->Peb->BeingDebugged)
361 __TRY
363 EXCEPTION_RECORD record;
364 record.ExceptionCode = DBG_PRINTEXCEPTION_C;
365 record.ExceptionFlags = 0;
366 record.ExceptionRecord = NULL;
367 record.ExceptionAddress = RtlRaiseException;
368 record.NumberParameters = 2;
369 record.ExceptionInformation[1] = (ULONG_PTR)buf;
370 record.ExceptionInformation[0] = strlen( buf ) + 1;
371 RtlRaiseException( &record );
373 __EXCEPT(debug_exception_handler)
376 __ENDTRY
379 return STATUS_SUCCESS;
382 /******************************************************************************
383 * RtlAcquirePebLock [NTDLL.@]
385 VOID WINAPI RtlAcquirePebLock(void)
387 RtlEnterCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
390 /******************************************************************************
391 * RtlReleasePebLock [NTDLL.@]
393 VOID WINAPI RtlReleasePebLock(void)
395 RtlLeaveCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
398 /******************************************************************************
399 * RtlNewSecurityObject [NTDLL.@]
401 NTSTATUS WINAPI
402 RtlNewSecurityObject( PSECURITY_DESCRIPTOR ParentDescriptor,
403 PSECURITY_DESCRIPTOR CreatorDescriptor,
404 PSECURITY_DESCRIPTOR *NewDescriptor,
405 BOOLEAN IsDirectoryObject,
406 HANDLE Token,
407 PGENERIC_MAPPING GenericMapping )
409 FIXME("(%p %p %p %d %p %p) stub!\n", ParentDescriptor, CreatorDescriptor,
410 NewDescriptor, IsDirectoryObject, Token, GenericMapping);
411 return STATUS_NOT_IMPLEMENTED;
414 /******************************************************************************
415 * RtlDeleteSecurityObject [NTDLL.@]
417 NTSTATUS WINAPI
418 RtlDeleteSecurityObject( PSECURITY_DESCRIPTOR *ObjectDescriptor )
420 FIXME("(%p) stub!\n", ObjectDescriptor);
421 return STATUS_NOT_IMPLEMENTED;
424 /******************************************************************************
425 * RtlInitializeGenericTable [NTDLL.@]
427 void WINAPI RtlInitializeGenericTable(RTL_GENERIC_TABLE *table, PRTL_GENERIC_COMPARE_ROUTINE compare,
428 PRTL_GENERIC_ALLOCATE_ROUTINE allocate, PRTL_GENERIC_FREE_ROUTINE free,
429 void *context)
431 FIXME("(%p, %p, %p, %p, %p) stub!\n", table, compare, allocate, free, context);
434 /******************************************************************************
435 * RtlEnumerateGenericTableWithoutSplaying [NTDLL.@]
437 void * WINAPI RtlEnumerateGenericTableWithoutSplaying(RTL_GENERIC_TABLE *table, void *previous)
439 static int warn_once;
441 if (!warn_once++)
442 FIXME("(%p, %p) stub!\n", table, previous);
443 return NULL;
446 /******************************************************************************
447 * RtlNumberGenericTableElements [NTDLL.@]
449 ULONG WINAPI RtlNumberGenericTableElements(RTL_GENERIC_TABLE *table)
451 FIXME("(%p) stub!\n", table);
452 return 0;
455 /******************************************************************************
456 * RtlMoveMemory [NTDLL.@]
458 * Move a block of memory that may overlap.
460 * PARAMS
461 * Destination [O] End destination for block
462 * Source [O] Where to start copying from
463 * Length [I] Number of bytes to copy
465 * RETURNS
466 * Nothing.
468 #undef RtlMoveMemory
469 VOID WINAPI RtlMoveMemory( void *Destination, const void *Source, SIZE_T Length )
471 memmove(Destination, Source, Length);
474 /******************************************************************************
475 * RtlFillMemory [NTDLL.@]
477 * Set a block of memory with a value.
479 * PARAMS
480 * Destination [O] Block to fill
481 * Length [I] Number of bytes to fill
482 * Fill [I] Value to set
484 * RETURNS
485 * Nothing.
487 #undef RtlFillMemory
488 VOID WINAPI RtlFillMemory( VOID *Destination, SIZE_T Length, BYTE Fill )
490 memset(Destination, Fill, Length);
493 /******************************************************************************
494 * RtlZeroMemory [NTDLL.@]
496 * Set a block of memory with 0's.
498 * PARAMS
499 * Destination [O] Block to fill
500 * Length [I] Number of bytes to fill
502 * RETURNS
503 * Nothing.
505 #undef RtlZeroMemory
506 VOID WINAPI RtlZeroMemory( VOID *Destination, SIZE_T Length )
508 memset(Destination, 0, Length);
511 /******************************************************************************
512 * RtlCompareMemory [NTDLL.@]
514 * Compare one block of memory with another
516 * PARAMS
517 * Source1 [I] Source block
518 * Source2 [I] Block to compare to Source1
519 * Length [I] Number of bytes to compare
521 * RETURNS
522 * The length of the first byte at which Source1 and Source2 differ, or Length
523 * if they are the same.
525 SIZE_T WINAPI RtlCompareMemory( const VOID *Source1, const VOID *Source2, SIZE_T Length)
527 SIZE_T i;
528 for(i=0; (i<Length) && (((const BYTE*)Source1)[i]==((const BYTE*)Source2)[i]); i++);
529 return i;
532 /******************************************************************************
533 * RtlCompareMemoryUlong [NTDLL.@]
535 * Compare a block of memory with a value, a ULONG at a time
537 * PARAMS
538 * Source1 [I] Source block. This must be ULONG aligned
539 * Length [I] Number of bytes to compare. This should be a multiple of 4
540 * dwVal [I] Value to compare to
542 * RETURNS
543 * The byte position of the first byte at which Source1 is not dwVal.
545 SIZE_T WINAPI RtlCompareMemoryUlong(VOID *Source1, SIZE_T Length, ULONG dwVal)
547 SIZE_T i;
548 for(i = 0; i < Length/sizeof(ULONG) && ((ULONG *)Source1)[i] == dwVal; i++);
549 return i * sizeof(ULONG);
552 /******************************************************************************
553 * RtlCopyMemory [NTDLL.@]
555 #undef RtlCopyMemory
556 void WINAPI RtlCopyMemory(void *dest, const void *src, SIZE_T len)
558 memcpy(dest, src, len);
561 /******************************************************************************
562 * RtlAssert [NTDLL.@]
564 * Fail a debug assertion.
566 * RETURNS
567 * Nothing. This call does not return control to its caller.
569 * NOTES
570 * Not implemented in non-debug versions.
572 void WINAPI RtlAssert(void *assertion, void *filename, ULONG linenumber, char *message)
574 FIXME("(%s, %s, %u, %s): stub\n", debugstr_a((char*)assertion), debugstr_a((char*)filename),
575 linenumber, debugstr_a(message));
578 /*************************************************************************
579 * RtlFillMemoryUlong [NTDLL.@]
581 * Fill memory with a 32 bit (dword) value.
583 * PARAMS
584 * lpDest [I] Bitmap pointer
585 * ulCount [I] Number of dwords to write
586 * ulValue [I] Value to fill with
588 * RETURNS
589 * Nothing.
591 VOID WINAPI RtlFillMemoryUlong(ULONG* lpDest, ULONG ulCount, ULONG ulValue)
593 TRACE("(%p,%d,%d)\n", lpDest, ulCount, ulValue);
595 ulCount /= sizeof(ULONG);
596 while(ulCount--)
597 *lpDest++ = ulValue;
600 /*********************************************************************
601 * RtlComputeCrc32 [NTDLL.@]
603 * Calculate the CRC32 checksum of a block of bytes
605 * PARAMS
606 * dwInitial [I] Initial CRC value
607 * pData [I] Data block
608 * iLen [I] Length of the byte block
610 * RETURNS
611 * The cumulative CRC32 of dwInitial and iLen bytes of the pData block.
613 DWORD WINAPI RtlComputeCrc32(DWORD dwInitial, const BYTE *pData, INT iLen)
615 DWORD crc = ~dwInitial;
617 TRACE("(%d,%p,%d)\n", dwInitial, pData, iLen);
619 while (iLen > 0)
621 crc = CRC_table[(crc ^ *pData) & 0xff] ^ (crc >> 8);
622 pData++;
623 iLen--;
625 return ~crc;
629 /*************************************************************************
630 * RtlUlonglongByteSwap [NTDLL.@]
632 * Swap the bytes of an unsigned long long value.
634 * PARAMS
635 * i [I] Value to swap bytes of
637 * RETURNS
638 * The value with its bytes swapped.
640 ULONGLONG __cdecl RtlUlonglongByteSwap(ULONGLONG i)
642 return ((ULONGLONG)RtlUlongByteSwap(i) << 32) | RtlUlongByteSwap(i>>32);
645 /*************************************************************************
646 * RtlUlongByteSwap [NTDLL.@]
648 * Swap the bytes of an unsigned int value.
650 * NOTES
651 * ix86 version takes argument in %ecx. Other systems use the inline version.
653 #ifdef __i386__
654 __ASM_GLOBAL_FUNC(NTDLL_RtlUlongByteSwap,
655 "movl %ecx,%eax\n\t"
656 "bswap %eax\n\t"
657 "ret")
658 #endif
660 /*************************************************************************
661 * RtlUshortByteSwap [NTDLL.@]
663 * Swap the bytes of an unsigned short value.
665 * NOTES
666 * i386 version takes argument in %cx. Other systems use the inline version.
668 #ifdef __i386__
669 __ASM_GLOBAL_FUNC(NTDLL_RtlUshortByteSwap,
670 "movb %ch,%al\n\t"
671 "movb %cl,%ah\n\t"
672 "ret")
673 #endif
676 /*************************************************************************
677 * RtlUniform [NTDLL.@]
679 * Generates an uniform random number
681 * PARAMS
682 * seed [O] The seed of the Random function
684 * RETURNS
685 * It returns a random number uniformly distributed over [0..MAXLONG-1].
687 * NOTES
688 * Generates an uniform random number using D.H. Lehmer's 1948 algorithm.
689 * In our case the algorithm is:
691 *| result = (*seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
693 *| *seed = result;
695 * DIFFERENCES
696 * The native documentation states that the random number is
697 * uniformly distributed over [0..MAXLONG]. In reality the native
698 * function and our function return a random number uniformly
699 * distributed over [0..MAXLONG-1].
701 ULONG WINAPI RtlUniform (PULONG seed)
703 ULONG result;
706 * Instead of the algorithm stated above, we use the algorithm
707 * below, which is totally equivalent (see the tests), but does
708 * not use a division and therefore is faster.
710 result = *seed * 0xffffffed + 0x7fffffc3;
711 if (result == 0xffffffff || result == 0x7ffffffe) {
712 result = (result + 2) & MAXLONG;
713 } else if (result == 0x7fffffff) {
714 result = 0;
715 } else if ((result & 0x80000000) == 0) {
716 result = result + (~result & 1);
717 } else {
718 result = (result + (result & 1)) & MAXLONG;
719 } /* if */
720 *seed = result;
721 return result;
725 /*************************************************************************
726 * RtlRandom [NTDLL.@]
728 * Generates a random number
730 * PARAMS
731 * seed [O] The seed of the Random function
733 * RETURNS
734 * It returns a random number distributed over [0..MAXLONG-1].
736 ULONG WINAPI RtlRandom (PULONG seed)
738 static ULONG saved_value[128] =
739 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
740 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
741 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
742 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
743 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
744 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
745 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
746 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
747 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
748 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
749 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
750 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
751 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
752 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
753 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
754 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
755 ULONG rand;
756 int pos;
757 ULONG result;
759 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
760 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
761 pos = *seed & 0x7f;
762 result = saved_value[pos];
763 saved_value[pos] = rand;
764 return(result);
768 /*************************************************************************
769 * RtlRandomEx [NTDLL.@]
771 ULONG WINAPI RtlRandomEx( ULONG *seed )
773 WARN( "semi-stub: should use a different algorithm\n" );
774 return RtlRandom( seed );
777 /*************************************************************************
778 * RtlAreAllAccessesGranted [NTDLL.@]
780 * Check if all desired accesses are granted
782 * RETURNS
783 * TRUE: All desired accesses are granted
784 * FALSE: Otherwise
786 BOOLEAN WINAPI RtlAreAllAccessesGranted(
787 ACCESS_MASK GrantedAccess,
788 ACCESS_MASK DesiredAccess)
790 return (GrantedAccess & DesiredAccess) == DesiredAccess;
794 /*************************************************************************
795 * RtlAreAnyAccessesGranted [NTDLL.@]
797 * Check if at least one of the desired accesses is granted
799 * PARAMS
800 * GrantedAccess [I] Access mask of granted accesses
801 * DesiredAccess [I] Access mask of desired accesses
803 * RETURNS
804 * TRUE: At least one of the desired accesses is granted
805 * FALSE: Otherwise
807 BOOLEAN WINAPI RtlAreAnyAccessesGranted(
808 ACCESS_MASK GrantedAccess,
809 ACCESS_MASK DesiredAccess)
811 return (GrantedAccess & DesiredAccess) != 0;
815 /*************************************************************************
816 * RtlMapGenericMask [NTDLL.@]
818 * Determine the nongeneric access rights specified by an access mask
820 * RETURNS
821 * Nothing.
823 void WINAPI RtlMapGenericMask(
824 PACCESS_MASK AccessMask,
825 const GENERIC_MAPPING *GenericMapping)
827 if (*AccessMask & GENERIC_READ) {
828 *AccessMask |= GenericMapping->GenericRead;
829 } /* if */
831 if (*AccessMask & GENERIC_WRITE) {
832 *AccessMask |= GenericMapping->GenericWrite;
833 } /* if */
835 if (*AccessMask & GENERIC_EXECUTE) {
836 *AccessMask |= GenericMapping->GenericExecute;
837 } /* if */
839 if (*AccessMask & GENERIC_ALL) {
840 *AccessMask |= GenericMapping->GenericAll;
841 } /* if */
843 *AccessMask &= 0x0FFFFFFF;
847 /*************************************************************************
848 * RtlCopyLuid [NTDLL.@]
850 * Copy a local unique ID.
852 * PARAMS
853 * LuidDest [O] Destination for the copied Luid
854 * LuidSrc [I] Source Luid to copy to LuidDest
856 * RETURNS
857 * Nothing.
859 void WINAPI RtlCopyLuid (PLUID LuidDest, const LUID *LuidSrc)
861 *LuidDest = *LuidSrc;
865 /*************************************************************************
866 * RtlEqualLuid [NTDLL.@]
868 * Compare two local unique IDs.
870 * PARAMS
871 * Luid1 [I] First Luid to compare to Luid2
872 * Luid2 [I] Second Luid to compare to Luid1
874 * RETURNS
875 * TRUE: The two LUIDs are equal.
876 * FALSE: Otherwise
878 BOOLEAN WINAPI RtlEqualLuid (const LUID *Luid1, const LUID *Luid2)
880 return (Luid1->LowPart == Luid2->LowPart && Luid1->HighPart == Luid2->HighPart);
884 /*************************************************************************
885 * RtlCopyLuidAndAttributesArray [NTDLL.@]
887 * Copy an array of local unique IDs and attributes.
889 * PARAMS
890 * Count [I] Number of Luid/attributes in Src
891 * Src [I] Source Luid/attributes to copy
892 * Dest [O] Destination for copied Luid/attributes
894 * RETURNS
895 * Nothing.
897 * NOTES
898 * Dest must be large enough to hold Src.
900 void WINAPI RtlCopyLuidAndAttributesArray(
901 ULONG Count,
902 const LUID_AND_ATTRIBUTES *Src,
903 PLUID_AND_ATTRIBUTES Dest)
905 ULONG i;
907 for (i = 0; i < Count; i++) Dest[i] = Src[i];
910 static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
912 int base = 10, d;
913 WCHAR c;
914 ULONG cur_value, prev_value = 0;
915 BOOL success = FALSE;
917 if (**str == '.')
919 *str += 1;
920 return FALSE;
923 if ((*str)[0] == '0')
925 if ((*str)[1] == 'x' || (*str)[1] == 'X')
927 *str += 2;
928 if (strict) return FALSE;
929 base = 16;
931 else if ((*str)[1] >= '0' && (*str)[1] <= '9')
933 *str += 1;
934 if (strict) return FALSE;
935 base = 8;
939 for (cur_value = 0; **str; *str += 1)
941 c = **str;
942 if (c >= ARRAY_SIZE(hex_table)) break;
943 d = hex_table[c];
944 if (d == -1 || d >= base) break;
945 cur_value = cur_value * base + d;
946 success = TRUE;
947 if (cur_value < prev_value) return FALSE; /* overflow */
948 prev_value = cur_value;
951 if (success) *value = cur_value;
952 return success;
955 static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
956 const WCHAR **terminator, IN_ADDR *address, USHORT *port)
958 ULONG fields[4];
959 int n = 0;
961 for (;;)
963 if (!parse_ipv4_component(&str, strict, &fields[n]))
964 goto error;
965 n++;
966 if (*str != '.')
967 break;
968 if (n == 4)
969 goto error;
970 str++;
973 if (strict && n < 4)
974 goto error;
976 switch (n)
978 case 4:
979 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFF || fields[3] > 0xFF)
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];
983 address->S_un.S_un_b.s_b3 = fields[2];
984 address->S_un.S_un_b.s_b4 = fields[3];
985 break;
986 case 3:
987 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFFFF)
988 goto error;
989 address->S_un.S_un_b.s_b1 = fields[0];
990 address->S_un.S_un_b.s_b2 = fields[1];
991 address->S_un.S_un_b.s_b3 = (fields[2] & 0xFF00) >> 8;
992 address->S_un.S_un_b.s_b4 = (fields[2] & 0x00FF);
993 break;
994 case 2:
995 if (fields[0] > 0xFF || fields[1] > 0xFFFFFF)
996 goto error;
997 address->S_un.S_un_b.s_b1 = fields[0];
998 address->S_un.S_un_b.s_b2 = (fields[1] & 0xFF0000) >> 16;
999 address->S_un.S_un_b.s_b3 = (fields[1] & 0x00FF00) >> 8;
1000 address->S_un.S_un_b.s_b4 = (fields[1] & 0x0000FF);
1001 break;
1002 case 1:
1003 address->S_un.S_un_b.s_b1 = (fields[0] & 0xFF000000) >> 24;
1004 address->S_un.S_un_b.s_b2 = (fields[0] & 0x00FF0000) >> 16;
1005 address->S_un.S_un_b.s_b3 = (fields[0] & 0x0000FF00) >> 8;
1006 address->S_un.S_un_b.s_b4 = (fields[0] & 0x000000FF);
1007 break;
1008 default:
1009 goto error;
1012 if (terminator) *terminator = str;
1014 if (*str == ':')
1016 str++;
1017 if (!parse_ipv4_component(&str, FALSE, &fields[0]))
1018 goto error;
1019 if (!fields[0] || fields[0] > 0xFFFF || *str)
1020 goto error;
1021 if (port)
1023 *port = htons(fields[0]);
1024 if (terminator) *terminator = str;
1028 if (!terminator && *str)
1029 return STATUS_INVALID_PARAMETER;
1030 return STATUS_SUCCESS;
1032 error:
1033 if (terminator) *terminator = str;
1034 return STATUS_INVALID_PARAMETER;
1037 /***********************************************************************
1038 * RtlIpv4StringToAddressExW [NTDLL.@]
1040 NTSTATUS WINAPI RtlIpv4StringToAddressExW(const WCHAR *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1042 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, address, port);
1043 if (!str || !address || !port) return STATUS_INVALID_PARAMETER;
1044 return ipv4_string_to_address(str, strict, NULL, address, port);
1047 /***********************************************************************
1048 * RtlIpv4StringToAddressW [NTDLL.@]
1050 NTSTATUS WINAPI RtlIpv4StringToAddressW(const WCHAR *str, BOOLEAN strict, const WCHAR **terminator, IN_ADDR *address)
1052 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, terminator, address);
1053 return ipv4_string_to_address(str, strict, terminator, address, NULL);
1056 /***********************************************************************
1057 * RtlIpv4StringToAddressExA [NTDLL.@]
1059 NTSTATUS WINAPI RtlIpv4StringToAddressExA(const char *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1061 WCHAR wstr[32];
1063 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, address, port);
1065 if (!str || !address || !port)
1066 return STATUS_INVALID_PARAMETER;
1068 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1069 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1070 return ipv4_string_to_address(wstr, strict, NULL, address, port);
1073 /***********************************************************************
1074 * RtlIpv4StringToAddressA [NTDLL.@]
1076 NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const char **terminator, IN_ADDR *address)
1078 WCHAR wstr[32];
1079 const WCHAR *wterminator;
1080 NTSTATUS ret;
1082 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, terminator, address);
1084 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1085 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1086 ret = ipv4_string_to_address(wstr, strict, &wterminator, address, NULL);
1087 if (terminator) *terminator = str + (wterminator - wstr);
1088 return ret;
1091 static BOOL parse_ipv6_component(const WCHAR **str, int base, ULONG *value)
1093 WCHAR *terminator;
1094 if (**str >= ARRAY_SIZE(hex_table) || hex_table[**str] == -1) return FALSE;
1095 *value = min(wcstoul(*str, &terminator, base), 0x7FFFFFFF);
1096 if (*terminator == '0') terminator++; /* "0x" but nothing valid after */
1097 else if (terminator == *str) return FALSE;
1098 *str = terminator;
1099 return TRUE;
1102 static NTSTATUS ipv6_string_to_address(const WCHAR *str, BOOL ex,
1103 const WCHAR **terminator, IN6_ADDR *address, ULONG *scope, USHORT *port)
1105 BOOL expecting_port = FALSE, has_0x = FALSE, too_big = FALSE;
1106 int n_bytes = 0, n_ipv4_bytes = 0, gap = -1;
1107 ULONG ip_component, scope_component = 0, port_component = 0;
1108 const WCHAR *prev_str;
1110 if (str[0] == '[')
1112 if (!ex) goto error;
1113 expecting_port = TRUE;
1114 str++;
1117 if (str[0] == ':')
1119 if (str[1] != ':') goto error;
1120 str++;
1121 /* Windows bug: a double colon at the beginning is treated as 4 bytes of zeros instead of 2 */
1122 address->u.Word[0] = 0;
1123 n_bytes = 2;
1126 for (;;)
1128 if (!n_ipv4_bytes && *str == ':')
1130 /* double colon */
1131 if (gap != -1) goto error;
1132 str++;
1133 prev_str = str;
1134 gap = n_bytes;
1135 if (n_bytes == 14 || !parse_ipv6_component(&str, 16, &ip_component)) break;
1136 str = prev_str;
1138 else
1140 prev_str = str;
1143 if (!n_ipv4_bytes && n_bytes <= (gap != -1 ? 10 : 12))
1145 if (parse_ipv6_component(&str, 10, &ip_component) && *str == '.')
1146 n_ipv4_bytes = 1;
1147 str = prev_str;
1150 if (n_ipv4_bytes)
1152 /* IPv4 component */
1153 if (!parse_ipv6_component(&str, 10, &ip_component)) goto error;
1154 if (str - prev_str > 3 || ip_component > 255)
1156 too_big = TRUE;
1158 else
1160 if (*str != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
1161 address->u.Byte[n_bytes] = ip_component;
1162 n_bytes++;
1164 if (n_ipv4_bytes == 4 || *str != '.') break;
1165 n_ipv4_bytes++;
1167 else
1169 /* IPv6 component */
1170 if (!parse_ipv6_component(&str, 16, &ip_component)) goto error;
1171 if (prev_str[0] == '0' && (prev_str[1] == 'x' || prev_str[1] == 'X'))
1173 /* Windows "feature": the last IPv6 component can start with "0x" and be longer than 4 digits */
1174 if (terminator) *terminator = prev_str + 1; /* Windows says that the "x" is the terminator */
1175 if (n_bytes < 14 && gap == -1) return STATUS_INVALID_PARAMETER;
1176 address->u.Word[n_bytes/2] = htons(ip_component);
1177 n_bytes += 2;
1178 has_0x = TRUE;
1179 goto fill_gap;
1181 if (*str != ':' && n_bytes < 14 && gap == -1) goto error;
1182 if (str - prev_str > 4)
1183 too_big = TRUE;
1184 else
1185 address->u.Word[n_bytes/2] = htons(ip_component);
1186 n_bytes += 2;
1187 if (*str != ':' || (gap != -1 && str[1] == ':')) break;
1189 if (n_bytes == (gap != -1 ? 14 : 16)) break;
1190 if (too_big) return STATUS_INVALID_PARAMETER;
1191 str++;
1194 if (terminator) *terminator = str;
1195 if (too_big) return STATUS_INVALID_PARAMETER;
1197 fill_gap:
1198 if (gap == -1)
1200 if (n_bytes < 16) goto error;
1202 else
1204 memmove(address->u.Byte + 16 - (n_bytes - gap), address->u.Byte + gap, n_bytes - gap);
1205 memset(address->u.Byte + gap, 0, 16 - n_bytes);
1208 if (ex)
1210 if (has_0x) goto error;
1212 if (*str == '%')
1214 str++;
1215 if (!parse_ipv4_component(&str, TRUE, &scope_component)) goto error;
1218 if (expecting_port)
1220 if (*str != ']') goto error;
1221 str++;
1222 if (*str == ':')
1224 str++;
1225 if (!parse_ipv4_component(&str, FALSE, &port_component)) goto error;
1226 if (!port_component || port_component > 0xFFFF || *str) goto error;
1227 port_component = htons(port_component);
1232 if (!terminator && *str) return STATUS_INVALID_PARAMETER;
1234 if (scope) *scope = scope_component;
1235 if (port) *port = port_component;
1237 return STATUS_SUCCESS;
1239 error:
1240 if (terminator) *terminator = str;
1241 return STATUS_INVALID_PARAMETER;
1244 /***********************************************************************
1245 * RtlIpv6StringToAddressExW [NTDLL.@]
1247 NTSTATUS NTAPI RtlIpv6StringToAddressExW(const WCHAR *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1249 TRACE("(%s, %p, %p, %p)\n", debugstr_w(str), address, scope, port);
1250 if (!str || !address || !scope || !port) return STATUS_INVALID_PARAMETER;
1251 return ipv6_string_to_address(str, TRUE, NULL, address, scope, port);
1254 /***********************************************************************
1255 * RtlIpv6StringToAddressW [NTDLL.@]
1257 NTSTATUS WINAPI RtlIpv6StringToAddressW(const WCHAR *str, const WCHAR **terminator, IN6_ADDR *address)
1259 TRACE("(%s, %p, %p)\n", debugstr_w(str), terminator, address);
1260 return ipv6_string_to_address(str, FALSE, terminator, address, NULL, NULL);
1263 /***********************************************************************
1264 * RtlIpv6StringToAddressExA [NTDLL.@]
1266 NTSTATUS WINAPI RtlIpv6StringToAddressExA(const char *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1268 WCHAR wstr[128];
1270 TRACE("(%s, %p, %p, %p)\n", debugstr_a(str), address, scope, port);
1272 if (!str || !address || !scope || !port)
1273 return STATUS_INVALID_PARAMETER;
1275 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1276 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1277 return ipv6_string_to_address(wstr, TRUE, NULL, address, scope, port);
1280 /***********************************************************************
1281 * RtlIpv6StringToAddressA [NTDLL.@]
1283 NTSTATUS WINAPI RtlIpv6StringToAddressA(const char *str, const char **terminator, IN6_ADDR *address)
1285 WCHAR wstr[128];
1286 const WCHAR *wterminator = NULL;
1287 NTSTATUS ret;
1289 TRACE("(%s, %p, %p)\n", debugstr_a(str), terminator, address);
1291 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1292 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1293 ret = ipv6_string_to_address(wstr, FALSE, &wterminator, address, NULL, NULL);
1294 if (terminator && wterminator) *terminator = str + (wterminator - wstr);
1295 return ret;
1298 /***********************************************************************
1299 * RtlIpv4AddressToStringExW [NTDLL.@]
1301 * Convert the given ipv4 address and optional the port to a string
1303 * PARAMS
1304 * pin [I] PTR to the ip address to convert (network byte order)
1305 * port [I] optional port to convert (network byte order)
1306 * buffer [O] destination buffer for the result
1307 * psize [IO] PTR to available/used size of the destination buffer
1309 * RETURNS
1310 * Success: STATUS_SUCCESS
1311 * Failure: STATUS_INVALID_PARAMETER
1314 NTSTATUS WINAPI RtlIpv4AddressToStringExW(const IN_ADDR *pin, USHORT port, LPWSTR buffer, PULONG psize)
1316 WCHAR tmp_ip[32];
1317 ULONG needed;
1319 if (!pin || !buffer || !psize)
1320 return STATUS_INVALID_PARAMETER;
1322 TRACE("(%p:0x%x, %d, %p, %p:%d)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1324 needed = swprintf(tmp_ip, ARRAY_SIZE(tmp_ip), L"%u.%u.%u.%u",
1325 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1326 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1328 if (port) needed += swprintf(tmp_ip + needed, ARRAY_SIZE(tmp_ip) - needed, L":%u", ntohs(port));
1330 if (*psize > needed) {
1331 *psize = needed + 1;
1332 wcscpy(buffer, tmp_ip);
1333 return STATUS_SUCCESS;
1336 *psize = needed + 1;
1337 return STATUS_INVALID_PARAMETER;
1340 /***********************************************************************
1341 * RtlIpv4AddressToStringExA [NTDLL.@]
1343 * Convert the given ipv4 address and optional the port to a string
1345 * See RtlIpv4AddressToStringExW
1347 NTSTATUS WINAPI RtlIpv4AddressToStringExA(const IN_ADDR *pin, USHORT port, LPSTR buffer, PULONG psize)
1349 CHAR tmp_ip[32];
1350 ULONG needed;
1352 if (!pin || !buffer || !psize)
1353 return STATUS_INVALID_PARAMETER;
1355 TRACE("(%p:0x%x, %d, %p, %p:%d)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1357 needed = sprintf(tmp_ip, "%u.%u.%u.%u",
1358 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1359 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1361 if (port) needed += sprintf(tmp_ip + needed, ":%u", ntohs(port));
1363 if (*psize > needed) {
1364 *psize = needed + 1;
1365 strcpy(buffer, tmp_ip);
1366 return STATUS_SUCCESS;
1369 *psize = needed + 1;
1370 return STATUS_INVALID_PARAMETER;
1373 /***********************************************************************
1374 * RtlIpv4AddressToStringW [NTDLL.@]
1376 * Convert the given ipv4 address to a string
1378 * PARAMS
1379 * pin [I] PTR to the ip address to convert (network byte order)
1380 * buffer [O] destination buffer for the result (at least 16 character)
1382 * RETURNS
1383 * PTR to the 0 character at the end of the converted string
1386 WCHAR * WINAPI RtlIpv4AddressToStringW(const IN_ADDR *pin, LPWSTR buffer)
1388 ULONG size = 16;
1390 if (RtlIpv4AddressToStringExW(pin, 0, buffer, &size)) size = 0;
1391 return buffer + size - 1;
1394 /***********************************************************************
1395 * RtlIpv4AddressToStringA [NTDLL.@]
1397 * Convert the given ipv4 address to a string
1399 * See RtlIpv4AddressToStringW
1401 CHAR * WINAPI RtlIpv4AddressToStringA(const IN_ADDR *pin, LPSTR buffer)
1403 ULONG size = 16;
1405 if (RtlIpv4AddressToStringExA(pin, 0, buffer, &size)) size = 0;
1406 return buffer + size - 1;
1409 static BOOL is_ipv4_in_ipv6(const IN6_ADDR *address)
1411 if (address->s6_words[5] == htons(0x5efe) && (address->s6_words[4] & ~htons(0x200)) == 0)
1412 return TRUE;
1413 if (*(UINT64 *)address != 0)
1414 return FALSE;
1415 if (address->s6_words[4] != 0 && address->s6_words[4] != 0xffff)
1416 return FALSE;
1417 if (address->s6_words[4] == 0 && address->s6_words[5] != 0 && address->s6_words[5] != 0xffff)
1418 return FALSE;
1419 if (address->s6_words[4] == 0xffff && address->s6_words[5] != 0)
1420 return FALSE;
1421 if (address->s6_words[6] == 0)
1422 return FALSE;
1423 return TRUE;
1426 /***********************************************************************
1427 * RtlIpv6AddressToStringExA [NTDLL.@]
1429 NTSTATUS WINAPI RtlIpv6AddressToStringExA(const IN6_ADDR *address, ULONG scope, USHORT port, char *str, ULONG *size)
1431 char buffer[64], *p = buffer;
1432 int i, len, gap = -1, gap_len = 1, ipv6_end = 8;
1433 ULONG needed;
1434 NTSTATUS ret;
1436 TRACE("(%p %u %u %p %p)\n", address, scope, port, str, size);
1438 if (!address || !str || !size)
1439 return STATUS_INVALID_PARAMETER;
1441 if (is_ipv4_in_ipv6(address))
1442 ipv6_end = 6;
1444 for (i = 0; i < ipv6_end; i++)
1446 len = 0;
1447 while (!address->s6_words[i] && i < ipv6_end)
1449 i++;
1450 len++;
1452 if (len > gap_len)
1454 gap = i - len;
1455 gap_len = len;
1459 if (port) p += sprintf(p, "[");
1461 i = 0;
1462 while (i < ipv6_end)
1464 if (i == gap)
1466 p += sprintf(p, ":");
1467 i += gap_len;
1468 if (i == ipv6_end) p += sprintf(p, ":");
1469 continue;
1471 if (i > 0) p += sprintf(p, ":");
1472 p += sprintf(p, "%x", ntohs(address->s6_words[i]));
1473 i++;
1476 if (ipv6_end == 6)
1478 if (p[-1] != ':') p += sprintf(p, ":");
1479 p = RtlIpv4AddressToStringA((IN_ADDR *)(address->s6_words + 6), p);
1482 if (scope) p += sprintf(p, "%%%u", scope);
1484 if (port) p += sprintf(p, "]:%u", ntohs(port));
1486 needed = p - buffer + 1;
1488 if (*size >= needed)
1490 strcpy(str, buffer);
1491 ret = STATUS_SUCCESS;
1493 else
1495 ret = STATUS_INVALID_PARAMETER;
1498 *size = needed;
1499 return ret;
1502 /***********************************************************************
1503 * RtlIpv6AddressToStringA [NTDLL.@]
1505 char * WINAPI RtlIpv6AddressToStringA(const IN6_ADDR *address, char *str)
1507 ULONG size = 46;
1508 if (!address || !str) return str - 1;
1509 str[45] = 0; /* this byte is set even though the string is always shorter */
1510 RtlIpv6AddressToStringExA(address, 0, 0, str, &size);
1511 return str + size - 1;
1514 /***********************************************************************
1515 * RtlIpv6AddressToStringExW [NTDLL.@]
1517 NTSTATUS WINAPI RtlIpv6AddressToStringExW(const IN6_ADDR *address, ULONG scope, USHORT port, WCHAR *str, ULONG *size)
1519 char cstr[64];
1520 NTSTATUS ret = RtlIpv6AddressToStringExA(address, scope, port, cstr, size);
1521 if (ret == STATUS_SUCCESS) RtlMultiByteToUnicodeN(str, *size * sizeof(WCHAR), NULL, cstr, *size);
1522 return ret;
1525 /***********************************************************************
1526 * RtlIpv6AddressToStringW [NTDLL.@]
1528 WCHAR * WINAPI RtlIpv6AddressToStringW(const IN6_ADDR *address, WCHAR *str)
1530 ULONG size = 46;
1531 if (!address || !str) return str;
1532 str[45] = 0; /* this word is set even though the string is always shorter */
1533 if (RtlIpv6AddressToStringExW(address, 0, 0, str, &size) != STATUS_SUCCESS)
1534 return str;
1535 return str + size - 1;
1538 /***********************************************************************
1539 * get_pointer_obfuscator (internal)
1541 static DWORD_PTR get_pointer_obfuscator( void )
1543 static DWORD_PTR pointer_obfuscator;
1545 if (!pointer_obfuscator)
1547 ULONG seed = NtGetTickCount();
1548 ULONG_PTR rand;
1550 /* generate a random value for the obfuscator */
1551 rand = RtlUniform( &seed );
1553 /* handle 64bit pointers */
1554 rand ^= (ULONG_PTR)RtlUniform( &seed ) << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1556 /* set the high bits so dereferencing obfuscated pointers will (usually) crash */
1557 rand |= (ULONG_PTR)0xc0000000 << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1559 InterlockedCompareExchangePointer( (void**) &pointer_obfuscator, (void*) rand, NULL );
1562 return pointer_obfuscator;
1565 /*************************************************************************
1566 * RtlEncodePointer [NTDLL.@]
1568 PVOID WINAPI RtlEncodePointer( PVOID ptr )
1570 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1571 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1574 PVOID WINAPI RtlDecodePointer( PVOID ptr )
1576 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1577 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1580 /*************************************************************************
1581 * RtlInitializeSListHead [NTDLL.@]
1583 VOID WINAPI RtlInitializeSListHead(PSLIST_HEADER list)
1585 #ifdef _WIN64
1586 list->s.Alignment = list->s.Region = 0;
1587 list->Header16.HeaderType = 1; /* we use the 16-byte header */
1588 #else
1589 list->Alignment = 0;
1590 #endif
1593 /*************************************************************************
1594 * RtlQueryDepthSList [NTDLL.@]
1596 WORD WINAPI RtlQueryDepthSList(PSLIST_HEADER list)
1598 #ifdef _WIN64
1599 return list->Header16.Depth;
1600 #else
1601 return list->s.Depth;
1602 #endif
1605 /*************************************************************************
1606 * RtlFirstEntrySList [NTDLL.@]
1608 PSLIST_ENTRY WINAPI RtlFirstEntrySList(const SLIST_HEADER* list)
1610 #ifdef _WIN64
1611 return (SLIST_ENTRY *)((ULONG_PTR)list->Header16.NextEntry << 4);
1612 #else
1613 return list->s.Next.Next;
1614 #endif
1617 /*************************************************************************
1618 * RtlInterlockedFlushSList [NTDLL.@]
1620 PSLIST_ENTRY WINAPI RtlInterlockedFlushSList(PSLIST_HEADER list)
1622 SLIST_HEADER old, new;
1624 #ifdef _WIN64
1625 if (!list->Header16.NextEntry) return NULL;
1626 new.s.Alignment = new.s.Region = 0;
1627 new.Header16.HeaderType = 1; /* we use the 16-byte header */
1630 old = *list;
1631 new.Header16.Sequence = old.Header16.Sequence + 1;
1632 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1633 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1634 #else
1635 if (!list->s.Next.Next) return NULL;
1636 new.Alignment = 0;
1639 old = *list;
1640 new.s.Sequence = old.s.Sequence + 1;
1641 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1642 old.Alignment) != old.Alignment);
1643 return old.s.Next.Next;
1644 #endif
1647 /*************************************************************************
1648 * RtlInterlockedPushEntrySList [NTDLL.@]
1650 PSLIST_ENTRY WINAPI RtlInterlockedPushEntrySList(PSLIST_HEADER list, PSLIST_ENTRY entry)
1652 SLIST_HEADER old, new;
1654 #ifdef _WIN64
1655 new.Header16.NextEntry = (ULONG_PTR)entry >> 4;
1658 old = *list;
1659 entry->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1660 new.Header16.Depth = old.Header16.Depth + 1;
1661 new.Header16.Sequence = old.Header16.Sequence + 1;
1662 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1663 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1664 #else
1665 new.s.Next.Next = entry;
1668 old = *list;
1669 entry->Next = old.s.Next.Next;
1670 new.s.Depth = old.s.Depth + 1;
1671 new.s.Sequence = old.s.Sequence + 1;
1672 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1673 old.Alignment) != old.Alignment);
1674 return old.s.Next.Next;
1675 #endif
1678 /*************************************************************************
1679 * RtlInterlockedPopEntrySList [NTDLL.@]
1681 PSLIST_ENTRY WINAPI RtlInterlockedPopEntrySList(PSLIST_HEADER list)
1683 SLIST_HEADER old, new;
1684 PSLIST_ENTRY entry;
1686 #ifdef _WIN64
1689 old = *list;
1690 if (!(entry = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4))) return NULL;
1691 /* entry could be deleted by another thread */
1692 __TRY
1694 new.Header16.NextEntry = (ULONG_PTR)entry->Next >> 4;
1695 new.Header16.Depth = old.Header16.Depth - 1;
1696 new.Header16.Sequence = old.Header16.Sequence + 1;
1698 __EXCEPT_PAGE_FAULT
1701 __ENDTRY
1702 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1703 #else
1706 old = *list;
1707 if (!(entry = old.s.Next.Next)) return NULL;
1708 /* entry could be deleted by another thread */
1709 __TRY
1711 new.s.Next.Next = entry->Next;
1712 new.s.Depth = old.s.Depth - 1;
1713 new.s.Sequence = old.s.Sequence + 1;
1715 __EXCEPT_PAGE_FAULT
1718 __ENDTRY
1719 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1720 old.Alignment) != old.Alignment);
1721 #endif
1722 return entry;
1725 /*************************************************************************
1726 * RtlInterlockedPushListSListEx [NTDLL.@]
1728 PSLIST_ENTRY WINAPI RtlInterlockedPushListSListEx(PSLIST_HEADER list, PSLIST_ENTRY first,
1729 PSLIST_ENTRY last, ULONG count)
1731 SLIST_HEADER old, new;
1733 #ifdef _WIN64
1734 new.Header16.NextEntry = (ULONG_PTR)first >> 4;
1737 old = *list;
1738 new.Header16.Depth = old.Header16.Depth + count;
1739 new.Header16.Sequence = old.Header16.Sequence + 1;
1740 last->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1741 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1742 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1743 #else
1744 new.s.Next.Next = first;
1747 old = *list;
1748 new.s.Depth = old.s.Depth + count;
1749 new.s.Sequence = old.s.Sequence + 1;
1750 last->Next = old.s.Next.Next;
1751 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1752 old.Alignment) != old.Alignment);
1753 return old.s.Next.Next;
1754 #endif
1757 /*************************************************************************
1758 * RtlInterlockedPushListSList [NTDLL.@]
1760 DEFINE_FASTCALL_WRAPPER(RtlInterlockedPushListSList, 16)
1761 PSLIST_ENTRY FASTCALL RtlInterlockedPushListSList(PSLIST_HEADER list, PSLIST_ENTRY first,
1762 PSLIST_ENTRY last, ULONG count)
1764 return RtlInterlockedPushListSListEx(list, first, last, count);
1767 /******************************************************************************
1768 * RtlGetCompressionWorkSpaceSize [NTDLL.@]
1770 NTSTATUS WINAPI RtlGetCompressionWorkSpaceSize(USHORT format, PULONG compress_workspace,
1771 PULONG decompress_workspace)
1773 FIXME("0x%04x, %p, %p: semi-stub\n", format, compress_workspace, decompress_workspace);
1775 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1777 case COMPRESSION_FORMAT_LZNT1:
1778 if (compress_workspace)
1780 /* FIXME: The current implementation of RtlCompressBuffer does not use a
1781 * workspace buffer, but Windows applications might expect a nonzero value. */
1782 *compress_workspace = 16;
1784 if (decompress_workspace)
1785 *decompress_workspace = 0x1000;
1786 return STATUS_SUCCESS;
1788 case COMPRESSION_FORMAT_NONE:
1789 case COMPRESSION_FORMAT_DEFAULT:
1790 return STATUS_INVALID_PARAMETER;
1792 default:
1793 FIXME("format %u not implemented\n", format);
1794 return STATUS_UNSUPPORTED_COMPRESSION;
1798 /* compress data using LZNT1, currently only a stub */
1799 static NTSTATUS lznt1_compress(UCHAR *src, ULONG src_size, UCHAR *dst, ULONG dst_size,
1800 ULONG chunk_size, ULONG *final_size, UCHAR *workspace)
1802 UCHAR *src_cur = src, *src_end = src + src_size;
1803 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1804 ULONG block_size;
1806 while (src_cur < src_end)
1808 /* determine size of current chunk */
1809 block_size = min(0x1000, src_end - src_cur);
1810 if (dst_cur + sizeof(WORD) + block_size > dst_end)
1811 return STATUS_BUFFER_TOO_SMALL;
1813 /* write (uncompressed) chunk header */
1814 *(WORD *)dst_cur = 0x3000 | (block_size - 1);
1815 dst_cur += sizeof(WORD);
1817 /* write chunk content */
1818 memcpy(dst_cur, src_cur, block_size);
1819 dst_cur += block_size;
1820 src_cur += block_size;
1823 if (final_size)
1824 *final_size = dst_cur - dst;
1826 return STATUS_SUCCESS;
1829 /******************************************************************************
1830 * RtlCompressBuffer [NTDLL.@]
1832 NTSTATUS WINAPI RtlCompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
1833 PUCHAR compressed, ULONG compressed_size, ULONG chunk_size,
1834 PULONG final_size, PVOID workspace)
1836 FIXME("0x%04x, %p, %u, %p, %u, %u, %p, %p: semi-stub\n", format, uncompressed,
1837 uncompressed_size, compressed, compressed_size, chunk_size, final_size, workspace);
1839 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1841 case COMPRESSION_FORMAT_LZNT1:
1842 return lznt1_compress(uncompressed, uncompressed_size, compressed,
1843 compressed_size, chunk_size, final_size, workspace);
1845 case COMPRESSION_FORMAT_NONE:
1846 case COMPRESSION_FORMAT_DEFAULT:
1847 return STATUS_INVALID_PARAMETER;
1849 default:
1850 FIXME("format %u not implemented\n", format);
1851 return STATUS_UNSUPPORTED_COMPRESSION;
1855 /* decompress a single LZNT1 chunk */
1856 static UCHAR *lznt1_decompress_chunk(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size)
1858 UCHAR *src_cur = src, *src_end = src + src_size;
1859 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1860 ULONG displacement_bits, length_bits;
1861 ULONG code_displacement, code_length;
1862 WORD flags, code;
1864 while (src_cur < src_end && dst_cur < dst_end)
1866 flags = 0x8000 | *src_cur++;
1867 while ((flags & 0xff00) && src_cur < src_end)
1869 if (flags & 1)
1871 /* backwards reference */
1872 if (src_cur + sizeof(WORD) > src_end)
1873 return NULL;
1875 code = *(WORD *)src_cur;
1876 src_cur += sizeof(WORD);
1878 /* find length / displacement bits */
1879 for (displacement_bits = 12; displacement_bits > 4; displacement_bits--)
1880 if ((1 << (displacement_bits - 1)) < dst_cur - dst) break;
1882 length_bits = 16 - displacement_bits;
1883 code_length = (code & ((1 << length_bits) - 1)) + 3;
1884 code_displacement = (code >> length_bits) + 1;
1886 if (dst_cur < dst + code_displacement)
1887 return NULL;
1889 /* copy bytes of chunk - we can't use memcpy() since source and dest can
1890 * be overlapping, and the same bytes can be repeated over and over again */
1891 while (code_length--)
1893 if (dst_cur >= dst_end) return dst_cur;
1894 *dst_cur = *(dst_cur - code_displacement);
1895 dst_cur++;
1898 else
1900 /* uncompressed data */
1901 if (dst_cur >= dst_end) return dst_cur;
1902 *dst_cur++ = *src_cur++;
1904 flags >>= 1;
1908 return dst_cur;
1911 /* decompress data encoded with LZNT1 */
1912 static NTSTATUS lznt1_decompress(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size,
1913 ULONG offset, ULONG *final_size, UCHAR *workspace)
1915 UCHAR *src_cur = src, *src_end = src + src_size;
1916 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1917 ULONG chunk_size, block_size;
1918 WORD chunk_header;
1919 UCHAR *ptr;
1921 if (src_cur + sizeof(WORD) > src_end)
1922 return STATUS_BAD_COMPRESSION_BUFFER;
1924 /* skip over chunks with a distance >= 0x1000 to the destination offset */
1925 while (offset >= 0x1000 && src_cur + sizeof(WORD) <= src_end)
1927 chunk_header = *(WORD *)src_cur;
1928 src_cur += sizeof(WORD);
1929 if (!chunk_header) goto out;
1930 chunk_size = (chunk_header & 0xfff) + 1;
1932 if (src_cur + chunk_size > src_end)
1933 return STATUS_BAD_COMPRESSION_BUFFER;
1935 src_cur += chunk_size;
1936 offset -= 0x1000;
1939 /* handle partially included chunk */
1940 if (offset && src_cur + sizeof(WORD) <= src_end)
1942 chunk_header = *(WORD *)src_cur;
1943 src_cur += sizeof(WORD);
1944 if (!chunk_header) goto out;
1945 chunk_size = (chunk_header & 0xfff) + 1;
1947 if (src_cur + chunk_size > src_end)
1948 return STATUS_BAD_COMPRESSION_BUFFER;
1950 if (dst_cur >= dst_end)
1951 goto out;
1953 if (chunk_header & 0x8000)
1955 /* compressed chunk */
1956 if (!workspace) return STATUS_ACCESS_VIOLATION;
1957 ptr = lznt1_decompress_chunk(workspace, 0x1000, src_cur, chunk_size);
1958 if (!ptr) return STATUS_BAD_COMPRESSION_BUFFER;
1959 if (ptr - workspace > offset)
1961 block_size = min((ptr - workspace) - offset, dst_end - dst_cur);
1962 memcpy(dst_cur, workspace + offset, block_size);
1963 dst_cur += block_size;
1966 else
1968 /* uncompressed chunk */
1969 if (chunk_size > offset)
1971 block_size = min(chunk_size - offset, dst_end - dst_cur);
1972 memcpy(dst_cur, src_cur + offset, block_size);
1973 dst_cur += block_size;
1977 src_cur += chunk_size;
1980 /* handle remaining chunks */
1981 while (src_cur + sizeof(WORD) <= src_end)
1983 chunk_header = *(WORD *)src_cur;
1984 src_cur += sizeof(WORD);
1985 if (!chunk_header) goto out;
1986 chunk_size = (chunk_header & 0xfff) + 1;
1988 if (src_cur + chunk_size > src_end)
1989 return STATUS_BAD_COMPRESSION_BUFFER;
1991 /* fill space with padding when the previous chunk was decompressed
1992 * to less than 4096 bytes. no padding is needed for the last chunk
1993 * or when the next chunk is truncated */
1994 block_size = ((dst_cur - dst) + offset) & 0xfff;
1995 if (block_size)
1997 block_size = 0x1000 - block_size;
1998 if (dst_cur + block_size >= dst_end)
1999 goto out;
2000 memset(dst_cur, 0, block_size);
2001 dst_cur += block_size;
2004 if (dst_cur >= dst_end)
2005 goto out;
2007 if (chunk_header & 0x8000)
2009 /* compressed chunk */
2010 dst_cur = lznt1_decompress_chunk(dst_cur, dst_end - dst_cur, src_cur, chunk_size);
2011 if (!dst_cur) return STATUS_BAD_COMPRESSION_BUFFER;
2013 else
2015 /* uncompressed chunk */
2016 block_size = min(chunk_size, dst_end - dst_cur);
2017 memcpy(dst_cur, src_cur, block_size);
2018 dst_cur += block_size;
2021 src_cur += chunk_size;
2024 out:
2025 if (final_size)
2026 *final_size = dst_cur - dst;
2028 return STATUS_SUCCESS;
2032 /******************************************************************************
2033 * RtlDecompressFragment [NTDLL.@]
2035 NTSTATUS WINAPI RtlDecompressFragment(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2036 PUCHAR compressed, ULONG compressed_size, ULONG offset,
2037 PULONG final_size, PVOID workspace)
2039 TRACE("0x%04x, %p, %u, %p, %u, %u, %p, %p\n", format, uncompressed,
2040 uncompressed_size, compressed, compressed_size, offset, final_size, workspace);
2042 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
2044 case COMPRESSION_FORMAT_LZNT1:
2045 return lznt1_decompress(uncompressed, uncompressed_size, compressed,
2046 compressed_size, offset, final_size, workspace);
2048 case COMPRESSION_FORMAT_NONE:
2049 case COMPRESSION_FORMAT_DEFAULT:
2050 return STATUS_INVALID_PARAMETER;
2052 default:
2053 FIXME("format %u not implemented\n", format);
2054 return STATUS_UNSUPPORTED_COMPRESSION;
2059 /******************************************************************************
2060 * RtlDecompressBuffer [NTDLL.@]
2062 NTSTATUS WINAPI RtlDecompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2063 PUCHAR compressed, ULONG compressed_size, PULONG final_size)
2065 TRACE("0x%04x, %p, %u, %p, %u, %p\n", format, uncompressed,
2066 uncompressed_size, compressed, compressed_size, final_size);
2068 return RtlDecompressFragment(format, uncompressed, uncompressed_size,
2069 compressed, compressed_size, 0, final_size, NULL);
2072 /***********************************************************************
2073 * RtlSetThreadErrorMode [NTDLL.@]
2075 * Set the thread local error mode.
2077 * PARAMS
2078 * mode [I] The new error mode
2079 * oldmode [O] Destination of the old error mode (may be NULL)
2081 * RETURNS
2082 * Success: STATUS_SUCCESS
2083 * Failure: STATUS_INVALID_PARAMETER_1
2085 NTSTATUS WINAPI RtlSetThreadErrorMode( DWORD mode, LPDWORD oldmode )
2087 if (mode & ~0x70)
2088 return STATUS_INVALID_PARAMETER_1;
2090 if (oldmode)
2091 *oldmode = NtCurrentTeb()->HardErrorDisabled;
2093 NtCurrentTeb()->HardErrorDisabled = mode;
2094 return STATUS_SUCCESS;
2097 /***********************************************************************
2098 * RtlGetThreadErrorMode [NTDLL.@]
2100 * Get the thread local error mode.
2102 * PARAMS
2103 * None.
2105 * RETURNS
2106 * The current thread local error mode.
2108 DWORD WINAPI RtlGetThreadErrorMode( void )
2110 return NtCurrentTeb()->HardErrorDisabled;
2113 /******************************************************************************
2114 * RtlGetCurrentTransaction [NTDLL.@]
2116 HANDLE WINAPI RtlGetCurrentTransaction(void)
2118 FIXME("() :stub\n");
2119 return NULL;
2122 /******************************************************************************
2123 * RtlSetCurrentTransaction [NTDLL.@]
2125 BOOL WINAPI RtlSetCurrentTransaction(HANDLE new_transaction)
2127 FIXME("(%p) :stub\n", new_transaction);
2128 return FALSE;
2131 /**********************************************************************
2132 * RtlGetCurrentProcessorNumberEx [NTDLL.@]
2134 void WINAPI RtlGetCurrentProcessorNumberEx(PROCESSOR_NUMBER *processor)
2136 FIXME("(%p) :semi-stub\n", processor);
2137 processor->Group = 0;
2138 processor->Number = NtGetCurrentProcessorNumber();
2139 processor->Reserved = 0;
2142 /***********************************************************************
2143 * RtlInitializeGenericTableAvl (NTDLL.@)
2145 void WINAPI RtlInitializeGenericTableAvl(PRTL_AVL_TABLE table, PRTL_AVL_COMPARE_ROUTINE compare,
2146 PRTL_AVL_ALLOCATE_ROUTINE allocate, PRTL_AVL_FREE_ROUTINE free, void *context)
2148 FIXME("%p %p %p %p %p: stub\n", table, compare, allocate, free, context);
2151 /***********************************************************************
2152 * RtlInsertElementGenericTableAvl (NTDLL.@)
2154 void WINAPI RtlInsertElementGenericTableAvl(PRTL_AVL_TABLE table, void *buffer, ULONG size, BOOL *element)
2156 FIXME("%p %p %u %p: stub\n", table, buffer, size, element);
2159 /*********************************************************************
2160 * RtlQueryPackageIdentity [NTDLL.@]
2162 NTSTATUS WINAPI RtlQueryPackageIdentity(HANDLE token, WCHAR *fullname, SIZE_T *fullname_size,
2163 WCHAR *appid, SIZE_T *appid_size, BOOLEAN *packaged)
2165 FIXME("(%p, %p, %p, %p, %p, %p): stub\n", token, fullname, fullname_size, appid, appid_size, packaged);
2166 return STATUS_NOT_FOUND;