include: Add transact.idl to oledb.idl.
[wine.git] / dlls / ntdll / rtl.c
blob1e359e4a043adb9455cfb6cf90068d42c1e221ef
1 /*
2 * NT basis DLL
4 * This file contains the Rtl* API functions. These should be implementable.
6 * Copyright 1996-1998 Marcus Meissner
7 * Copyright 1999 Alex Korobka
8 * Copyright 2003 Thomas Mertes
9 * Crc32 code Copyright 1986 Gary S. Brown (Public domain)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <stdarg.h>
28 #include "ntstatus.h"
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #define WIN32_NO_STATUS
32 #include "winsock2.h"
33 #include "windef.h"
34 #include "winternl.h"
35 #include "wine/debug.h"
36 #include "wine/exception.h"
37 #include "ntdll_misc.h"
38 #include "in6addr.h"
39 #include "ddk/ntddk.h"
40 #include "ddk/ntifs.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
43 WINE_DECLARE_DEBUG_CHANNEL(debugstr);
45 /* CRC polynomial 0xedb88320 */
46 static const DWORD CRC_table[256] =
48 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
49 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
50 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
51 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
52 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
53 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
54 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
55 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
56 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
57 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
58 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
59 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
60 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
61 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
62 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
63 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
64 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
65 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
66 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
67 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
68 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
69 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
70 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
71 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
72 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
73 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
74 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
75 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
76 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
77 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
78 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
79 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
80 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
81 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
82 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
83 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
84 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
85 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
86 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
87 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
88 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
89 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
90 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
93 static const int hex_table[] = {
94 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
95 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
96 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2F */
97 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
98 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4F */
99 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
100 -1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
104 * resource functions
107 /***********************************************************************
108 * RtlInitializeResource (NTDLL.@)
110 * xxxResource() functions implement multiple-reader-single-writer lock.
111 * The code is based on information published in WDJ January 1999 issue.
113 void WINAPI RtlInitializeResource(LPRTL_RWLOCK rwl)
115 if( rwl )
117 rwl->iNumberActive = 0;
118 rwl->uExclusiveWaiters = 0;
119 rwl->uSharedWaiters = 0;
120 rwl->hOwningThreadId = 0;
121 rwl->dwTimeoutBoost = 0; /* no info on this one, default value is 0 */
122 RtlInitializeCriticalSection( &rwl->rtlCS );
123 rwl->rtlCS.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RTL_RWLOCK.rtlCS");
124 NtCreateSemaphore( &rwl->hExclusiveReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
125 NtCreateSemaphore( &rwl->hSharedReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
130 /***********************************************************************
131 * RtlDeleteResource (NTDLL.@)
133 void WINAPI RtlDeleteResource(LPRTL_RWLOCK rwl)
135 if( rwl )
137 RtlEnterCriticalSection( &rwl->rtlCS );
138 if( rwl->iNumberActive || rwl->uExclusiveWaiters || rwl->uSharedWaiters )
139 ERR("Deleting active MRSW lock (%p), expect failure\n", rwl );
140 rwl->hOwningThreadId = 0;
141 rwl->uExclusiveWaiters = rwl->uSharedWaiters = 0;
142 rwl->iNumberActive = 0;
143 NtClose( rwl->hExclusiveReleaseSemaphore );
144 NtClose( rwl->hSharedReleaseSemaphore );
145 RtlLeaveCriticalSection( &rwl->rtlCS );
146 rwl->rtlCS.DebugInfo->Spare[0] = 0;
147 RtlDeleteCriticalSection( &rwl->rtlCS );
152 /***********************************************************************
153 * RtlAcquireResourceExclusive (NTDLL.@)
155 BYTE WINAPI RtlAcquireResourceExclusive(LPRTL_RWLOCK rwl, BYTE fWait)
157 BYTE retVal = 0;
158 if( !rwl ) return 0;
160 start:
161 RtlEnterCriticalSection( &rwl->rtlCS );
162 if( rwl->iNumberActive == 0 ) /* lock is free */
164 rwl->iNumberActive = -1;
165 retVal = 1;
167 else if( rwl->iNumberActive < 0 ) /* exclusive lock in progress */
169 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
171 retVal = 1;
172 rwl->iNumberActive--;
173 goto done;
175 wait:
176 if( fWait )
178 NTSTATUS status;
180 rwl->uExclusiveWaiters++;
182 RtlLeaveCriticalSection( &rwl->rtlCS );
183 status = NtWaitForSingleObject( rwl->hExclusiveReleaseSemaphore, FALSE, NULL );
184 if( HIWORD(status) )
185 goto done;
186 goto start; /* restart the acquisition to avoid deadlocks */
189 else /* one or more shared locks are in progress */
190 if( fWait )
191 goto wait;
193 if( retVal == 1 )
194 rwl->hOwningThreadId = ULongToHandle(GetCurrentThreadId());
195 done:
196 RtlLeaveCriticalSection( &rwl->rtlCS );
197 return retVal;
200 /***********************************************************************
201 * RtlAcquireResourceShared (NTDLL.@)
203 BYTE WINAPI RtlAcquireResourceShared(LPRTL_RWLOCK rwl, BYTE fWait)
205 NTSTATUS status = STATUS_UNSUCCESSFUL;
206 BYTE retVal = 0;
207 if( !rwl ) return 0;
209 start:
210 RtlEnterCriticalSection( &rwl->rtlCS );
211 if( rwl->iNumberActive < 0 )
213 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
215 rwl->iNumberActive--;
216 retVal = 1;
217 goto done;
220 if( fWait )
222 rwl->uSharedWaiters++;
223 RtlLeaveCriticalSection( &rwl->rtlCS );
224 status = NtWaitForSingleObject( rwl->hSharedReleaseSemaphore, FALSE, NULL );
225 if( HIWORD(status) )
226 goto done;
227 goto start;
230 else
232 if( status != STATUS_WAIT_0 ) /* otherwise RtlReleaseResource() has already done it */
233 rwl->iNumberActive++;
234 retVal = 1;
236 done:
237 RtlLeaveCriticalSection( &rwl->rtlCS );
238 return retVal;
242 /***********************************************************************
243 * RtlReleaseResource (NTDLL.@)
245 void WINAPI RtlReleaseResource(LPRTL_RWLOCK rwl)
247 RtlEnterCriticalSection( &rwl->rtlCS );
249 if( rwl->iNumberActive > 0 ) /* have one or more readers */
251 if( --rwl->iNumberActive == 0 )
253 if( rwl->uExclusiveWaiters )
255 wake_exclusive:
256 rwl->uExclusiveWaiters--;
257 NtReleaseSemaphore( rwl->hExclusiveReleaseSemaphore, 1, NULL );
261 else
262 if( rwl->iNumberActive < 0 ) /* have a writer, possibly recursive */
264 if( ++rwl->iNumberActive == 0 )
266 rwl->hOwningThreadId = 0;
267 if( rwl->uExclusiveWaiters )
268 goto wake_exclusive;
269 else
270 if( rwl->uSharedWaiters )
272 UINT n = rwl->uSharedWaiters;
273 rwl->iNumberActive = rwl->uSharedWaiters; /* prevent new writers from joining until
274 * all queued readers have done their thing */
275 rwl->uSharedWaiters = 0;
276 NtReleaseSemaphore( rwl->hSharedReleaseSemaphore, n, NULL );
280 RtlLeaveCriticalSection( &rwl->rtlCS );
284 /***********************************************************************
285 * RtlDumpResource (NTDLL.@)
287 void WINAPI RtlDumpResource(LPRTL_RWLOCK rwl)
289 if( rwl )
291 MESSAGE("RtlDumpResource(%p):\n\tactive count = %i\n\twaiting readers = %i\n\twaiting writers = %i\n",
292 rwl, rwl->iNumberActive, rwl->uSharedWaiters, rwl->uExclusiveWaiters );
293 if( rwl->iNumberActive )
294 MESSAGE("\towner thread = %p\n", rwl->hOwningThreadId );
299 * misc functions
302 static LONG WINAPI debug_exception_handler( EXCEPTION_POINTERS *eptr )
304 EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
305 return (rec->ExceptionCode == DBG_PRINTEXCEPTION_C) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
308 /******************************************************************************
309 * DbgPrint [NTDLL.@]
311 NTSTATUS WINAPIV DbgPrint(LPCSTR fmt, ...)
313 NTSTATUS ret;
314 va_list args;
316 va_start(args, fmt);
317 ret = vDbgPrintEx(0, DPFLTR_ERROR_LEVEL, fmt, args);
318 va_end(args);
319 return ret;
323 /******************************************************************************
324 * DbgPrintEx [NTDLL.@]
326 NTSTATUS WINAPIV DbgPrintEx(ULONG iComponentId, ULONG Level, LPCSTR fmt, ...)
328 NTSTATUS ret;
329 va_list args;
331 va_start(args, fmt);
332 ret = vDbgPrintEx(iComponentId, Level, fmt, args);
333 va_end(args);
334 return ret;
337 /******************************************************************************
338 * vDbgPrintEx [NTDLL.@]
340 NTSTATUS WINAPI vDbgPrintEx( ULONG id, ULONG level, LPCSTR fmt, va_list args )
342 return vDbgPrintExWithPrefix( "", id, level, fmt, args );
345 /******************************************************************************
346 * vDbgPrintExWithPrefix [NTDLL.@]
348 NTSTATUS WINAPI vDbgPrintExWithPrefix( LPCSTR prefix, ULONG id, ULONG level, LPCSTR fmt, va_list args )
350 ULONG level_mask = level <= 31 ? (1 << level) : level;
351 SIZE_T len = strlen( prefix );
352 char buf[1024], *end;
354 strcpy( buf, prefix );
355 len += _vsnprintf( buf + len, sizeof(buf) - len, fmt, args );
356 end = buf + len - 1;
358 WARN_(debugstr)(*end == '\n' ? "%08x:%08x: %s" : "%08x:%08x: %s\n", id, level_mask, buf);
360 if (level_mask & (1 << DPFLTR_ERROR_LEVEL) && NtCurrentTeb()->Peb->BeingDebugged)
362 __TRY
364 EXCEPTION_RECORD record;
365 record.ExceptionCode = DBG_PRINTEXCEPTION_C;
366 record.ExceptionFlags = 0;
367 record.ExceptionRecord = NULL;
368 record.ExceptionAddress = RtlRaiseException;
369 record.NumberParameters = 2;
370 record.ExceptionInformation[1] = (ULONG_PTR)buf;
371 record.ExceptionInformation[0] = strlen( buf ) + 1;
372 RtlRaiseException( &record );
374 __EXCEPT(debug_exception_handler)
377 __ENDTRY
380 return STATUS_SUCCESS;
383 /******************************************************************************
384 * RtlAcquirePebLock [NTDLL.@]
386 VOID WINAPI RtlAcquirePebLock(void)
388 RtlEnterCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
391 /******************************************************************************
392 * RtlReleasePebLock [NTDLL.@]
394 VOID WINAPI RtlReleasePebLock(void)
396 RtlLeaveCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
399 /******************************************************************************
400 * RtlNewSecurityObject [NTDLL.@]
402 NTSTATUS WINAPI
403 RtlNewSecurityObject( PSECURITY_DESCRIPTOR ParentDescriptor,
404 PSECURITY_DESCRIPTOR CreatorDescriptor,
405 PSECURITY_DESCRIPTOR *NewDescriptor,
406 BOOLEAN IsDirectoryObject,
407 HANDLE Token,
408 PGENERIC_MAPPING GenericMapping )
410 FIXME("(%p %p %p %d %p %p) stub!\n", ParentDescriptor, CreatorDescriptor,
411 NewDescriptor, IsDirectoryObject, Token, GenericMapping);
412 return STATUS_NOT_IMPLEMENTED;
415 /******************************************************************************
416 * RtlDeleteSecurityObject [NTDLL.@]
418 NTSTATUS WINAPI
419 RtlDeleteSecurityObject( PSECURITY_DESCRIPTOR *ObjectDescriptor )
421 FIXME("(%p) stub!\n", ObjectDescriptor);
422 return STATUS_NOT_IMPLEMENTED;
425 /******************************************************************************
426 * RtlInitializeGenericTable [NTDLL.@]
428 void WINAPI RtlInitializeGenericTable(RTL_GENERIC_TABLE *table, PRTL_GENERIC_COMPARE_ROUTINE compare,
429 PRTL_GENERIC_ALLOCATE_ROUTINE allocate, PRTL_GENERIC_FREE_ROUTINE free,
430 void *context)
432 FIXME("(%p, %p, %p, %p, %p) stub!\n", table, compare, allocate, free, context);
435 /******************************************************************************
436 * RtlEnumerateGenericTableWithoutSplaying [NTDLL.@]
438 void * WINAPI RtlEnumerateGenericTableWithoutSplaying(RTL_GENERIC_TABLE *table, void *previous)
440 static int warn_once;
442 if (!warn_once++)
443 FIXME("(%p, %p) stub!\n", table, previous);
444 return NULL;
447 /******************************************************************************
448 * RtlNumberGenericTableElements [NTDLL.@]
450 ULONG WINAPI RtlNumberGenericTableElements(RTL_GENERIC_TABLE *table)
452 FIXME("(%p) stub!\n", table);
453 return 0;
456 /******************************************************************************
457 * RtlMoveMemory [NTDLL.@]
459 * Move a block of memory that may overlap.
461 * PARAMS
462 * Destination [O] End destination for block
463 * Source [O] Where to start copying from
464 * Length [I] Number of bytes to copy
466 * RETURNS
467 * Nothing.
469 #undef RtlMoveMemory
470 VOID WINAPI RtlMoveMemory( void *Destination, const void *Source, SIZE_T Length )
472 memmove(Destination, Source, Length);
475 /******************************************************************************
476 * RtlFillMemory [NTDLL.@]
478 * Set a block of memory with a value.
480 * PARAMS
481 * Destination [O] Block to fill
482 * Length [I] Number of bytes to fill
483 * Fill [I] Value to set
485 * RETURNS
486 * Nothing.
488 #undef RtlFillMemory
489 VOID WINAPI RtlFillMemory( VOID *Destination, SIZE_T Length, BYTE Fill )
491 memset(Destination, Fill, Length);
494 /******************************************************************************
495 * RtlZeroMemory [NTDLL.@]
497 * Set a block of memory with 0's.
499 * PARAMS
500 * Destination [O] Block to fill
501 * Length [I] Number of bytes to fill
503 * RETURNS
504 * Nothing.
506 #undef RtlZeroMemory
507 VOID WINAPI RtlZeroMemory( VOID *Destination, SIZE_T Length )
509 memset(Destination, 0, Length);
512 /******************************************************************************
513 * RtlCompareMemory [NTDLL.@]
515 * Compare one block of memory with another
517 * PARAMS
518 * Source1 [I] Source block
519 * Source2 [I] Block to compare to Source1
520 * Length [I] Number of bytes to compare
522 * RETURNS
523 * The length of the first byte at which Source1 and Source2 differ, or Length
524 * if they are the same.
526 SIZE_T WINAPI RtlCompareMemory( const VOID *Source1, const VOID *Source2, SIZE_T Length)
528 SIZE_T i;
529 for(i=0; (i<Length) && (((const BYTE*)Source1)[i]==((const BYTE*)Source2)[i]); i++);
530 return i;
533 /******************************************************************************
534 * RtlCompareMemoryUlong [NTDLL.@]
536 * Compare a block of memory with a value, a ULONG at a time
538 * PARAMS
539 * Source1 [I] Source block. This must be ULONG aligned
540 * Length [I] Number of bytes to compare. This should be a multiple of 4
541 * dwVal [I] Value to compare to
543 * RETURNS
544 * The byte position of the first byte at which Source1 is not dwVal.
546 SIZE_T WINAPI RtlCompareMemoryUlong(VOID *Source1, SIZE_T Length, ULONG dwVal)
548 SIZE_T i;
549 for(i = 0; i < Length/sizeof(ULONG) && ((ULONG *)Source1)[i] == dwVal; i++);
550 return i * sizeof(ULONG);
553 /******************************************************************************
554 * RtlCopyMemory [NTDLL.@]
556 #undef RtlCopyMemory
557 void WINAPI RtlCopyMemory(void *dest, const void *src, SIZE_T len)
559 memcpy(dest, src, len);
562 /******************************************************************************
563 * RtlAssert [NTDLL.@]
565 * Fail a debug assertion.
567 * RETURNS
568 * Nothing. This call does not return control to its caller.
570 * NOTES
571 * Not implemented in non-debug versions.
573 void WINAPI RtlAssert(void *assertion, void *filename, ULONG linenumber, char *message)
575 FIXME("(%s, %s, %u, %s): stub\n", debugstr_a((char*)assertion), debugstr_a((char*)filename),
576 linenumber, debugstr_a(message));
579 /*************************************************************************
580 * RtlFillMemoryUlong [NTDLL.@]
582 * Fill memory with a 32 bit (dword) value.
584 * PARAMS
585 * lpDest [I] Bitmap pointer
586 * ulCount [I] Number of dwords to write
587 * ulValue [I] Value to fill with
589 * RETURNS
590 * Nothing.
592 VOID WINAPI RtlFillMemoryUlong(ULONG* lpDest, ULONG ulCount, ULONG ulValue)
594 TRACE("(%p,%d,%d)\n", lpDest, ulCount, ulValue);
596 ulCount /= sizeof(ULONG);
597 while(ulCount--)
598 *lpDest++ = ulValue;
601 /*********************************************************************
602 * RtlComputeCrc32 [NTDLL.@]
604 * Calculate the CRC32 checksum of a block of bytes
606 * PARAMS
607 * dwInitial [I] Initial CRC value
608 * pData [I] Data block
609 * iLen [I] Length of the byte block
611 * RETURNS
612 * The cumulative CRC32 of dwInitial and iLen bytes of the pData block.
614 DWORD WINAPI RtlComputeCrc32(DWORD dwInitial, const BYTE *pData, INT iLen)
616 DWORD crc = ~dwInitial;
618 TRACE("(%d,%p,%d)\n", dwInitial, pData, iLen);
620 while (iLen > 0)
622 crc = CRC_table[(crc ^ *pData) & 0xff] ^ (crc >> 8);
623 pData++;
624 iLen--;
626 return ~crc;
630 /*************************************************************************
631 * RtlUlonglongByteSwap [NTDLL.@]
633 * Swap the bytes of an unsigned long long value.
635 * PARAMS
636 * i [I] Value to swap bytes of
638 * RETURNS
639 * The value with its bytes swapped.
641 ULONGLONG __cdecl RtlUlonglongByteSwap(ULONGLONG i)
643 return ((ULONGLONG)RtlUlongByteSwap(i) << 32) | RtlUlongByteSwap(i>>32);
646 /*************************************************************************
647 * RtlUlongByteSwap [NTDLL.@]
649 * Swap the bytes of an unsigned int value.
651 * NOTES
652 * ix86 version takes argument in %ecx. Other systems use the inline version.
654 #ifdef __i386__
655 __ASM_GLOBAL_FUNC(NTDLL_RtlUlongByteSwap,
656 "movl %ecx,%eax\n\t"
657 "bswap %eax\n\t"
658 "ret")
659 #endif
661 /*************************************************************************
662 * RtlUshortByteSwap [NTDLL.@]
664 * Swap the bytes of an unsigned short value.
666 * NOTES
667 * i386 version takes argument in %cx. Other systems use the inline version.
669 #ifdef __i386__
670 __ASM_GLOBAL_FUNC(NTDLL_RtlUshortByteSwap,
671 "movb %ch,%al\n\t"
672 "movb %cl,%ah\n\t"
673 "ret")
674 #endif
677 /*************************************************************************
678 * RtlUniform [NTDLL.@]
680 * Generates an uniform random number
682 * PARAMS
683 * seed [O] The seed of the Random function
685 * RETURNS
686 * It returns a random number uniformly distributed over [0..MAXLONG-1].
688 * NOTES
689 * Generates an uniform random number using D.H. Lehmer's 1948 algorithm.
690 * In our case the algorithm is:
692 *| result = (*seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
694 *| *seed = result;
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 (PULONG seed)
704 ULONG result;
707 * Instead of the algorithm stated above, we use the algorithm
708 * below, which is totally equivalent (see the tests), but does
709 * not use a division and therefore is faster.
711 result = *seed * 0xffffffed + 0x7fffffc3;
712 if (result == 0xffffffff || result == 0x7ffffffe) {
713 result = (result + 2) & MAXLONG;
714 } else if (result == 0x7fffffff) {
715 result = 0;
716 } else if ((result & 0x80000000) == 0) {
717 result = result + (~result & 1);
718 } else {
719 result = (result + (result & 1)) & MAXLONG;
720 } /* if */
721 *seed = result;
722 return result;
726 /*************************************************************************
727 * RtlRandom [NTDLL.@]
729 * Generates a random number
731 * PARAMS
732 * seed [O] The seed of the Random function
734 * RETURNS
735 * It returns a random number distributed over [0..MAXLONG-1].
737 ULONG WINAPI RtlRandom (PULONG seed)
739 static ULONG saved_value[128] =
740 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
741 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
742 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
743 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
744 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
745 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
746 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
747 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
748 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
749 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
750 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
751 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
752 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
753 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
754 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
755 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
756 ULONG rand;
757 int pos;
758 ULONG result;
760 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
761 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
762 pos = *seed & 0x7f;
763 result = saved_value[pos];
764 saved_value[pos] = rand;
765 return(result);
769 /*************************************************************************
770 * RtlRandomEx [NTDLL.@]
772 ULONG WINAPI RtlRandomEx( ULONG *seed )
774 WARN( "semi-stub: should use a different algorithm\n" );
775 return RtlRandom( seed );
778 /*************************************************************************
779 * RtlAreAllAccessesGranted [NTDLL.@]
781 * Check if all desired accesses are granted
783 * RETURNS
784 * TRUE: All desired accesses are granted
785 * FALSE: Otherwise
787 BOOLEAN WINAPI RtlAreAllAccessesGranted(
788 ACCESS_MASK GrantedAccess,
789 ACCESS_MASK DesiredAccess)
791 return (GrantedAccess & DesiredAccess) == DesiredAccess;
795 /*************************************************************************
796 * RtlAreAnyAccessesGranted [NTDLL.@]
798 * Check if at least one of the desired accesses is granted
800 * PARAMS
801 * GrantedAccess [I] Access mask of granted accesses
802 * DesiredAccess [I] Access mask of desired accesses
804 * RETURNS
805 * TRUE: At least one of the desired accesses is granted
806 * FALSE: Otherwise
808 BOOLEAN WINAPI RtlAreAnyAccessesGranted(
809 ACCESS_MASK GrantedAccess,
810 ACCESS_MASK DesiredAccess)
812 return (GrantedAccess & DesiredAccess) != 0;
816 /*************************************************************************
817 * RtlMapGenericMask [NTDLL.@]
819 * Determine the nongeneric access rights specified by an access mask
821 * RETURNS
822 * Nothing.
824 void WINAPI RtlMapGenericMask(
825 PACCESS_MASK AccessMask,
826 const GENERIC_MAPPING *GenericMapping)
828 if (*AccessMask & GENERIC_READ) {
829 *AccessMask |= GenericMapping->GenericRead;
830 } /* if */
832 if (*AccessMask & GENERIC_WRITE) {
833 *AccessMask |= GenericMapping->GenericWrite;
834 } /* if */
836 if (*AccessMask & GENERIC_EXECUTE) {
837 *AccessMask |= GenericMapping->GenericExecute;
838 } /* if */
840 if (*AccessMask & GENERIC_ALL) {
841 *AccessMask |= GenericMapping->GenericAll;
842 } /* if */
844 *AccessMask &= 0x0FFFFFFF;
848 /*************************************************************************
849 * RtlCopyLuid [NTDLL.@]
851 * Copy a local unique ID.
853 * PARAMS
854 * LuidDest [O] Destination for the copied Luid
855 * LuidSrc [I] Source Luid to copy to LuidDest
857 * RETURNS
858 * Nothing.
860 void WINAPI RtlCopyLuid (PLUID LuidDest, const LUID *LuidSrc)
862 *LuidDest = *LuidSrc;
866 /*************************************************************************
867 * RtlEqualLuid [NTDLL.@]
869 * Compare two local unique IDs.
871 * PARAMS
872 * Luid1 [I] First Luid to compare to Luid2
873 * Luid2 [I] Second Luid to compare to Luid1
875 * RETURNS
876 * TRUE: The two LUIDs are equal.
877 * FALSE: Otherwise
879 BOOLEAN WINAPI RtlEqualLuid (const LUID *Luid1, const LUID *Luid2)
881 return (Luid1->LowPart == Luid2->LowPart && Luid1->HighPart == Luid2->HighPart);
885 /*************************************************************************
886 * RtlCopyLuidAndAttributesArray [NTDLL.@]
888 * Copy an array of local unique IDs and attributes.
890 * PARAMS
891 * Count [I] Number of Luid/attributes in Src
892 * Src [I] Source Luid/attributes to copy
893 * Dest [O] Destination for copied Luid/attributes
895 * RETURNS
896 * Nothing.
898 * NOTES
899 * Dest must be large enough to hold Src.
901 void WINAPI RtlCopyLuidAndAttributesArray(
902 ULONG Count,
903 const LUID_AND_ATTRIBUTES *Src,
904 PLUID_AND_ATTRIBUTES Dest)
906 ULONG i;
908 for (i = 0; i < Count; i++) Dest[i] = Src[i];
911 static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
913 int base = 10, d;
914 WCHAR c;
915 ULONG cur_value, prev_value = 0;
916 BOOL success = FALSE;
918 if (**str == '.')
920 *str += 1;
921 return FALSE;
924 if ((*str)[0] == '0')
926 if ((*str)[1] == 'x' || (*str)[1] == 'X')
928 *str += 2;
929 if (strict) return FALSE;
930 base = 16;
932 else if ((*str)[1] >= '0' && (*str)[1] <= '9')
934 *str += 1;
935 if (strict) return FALSE;
936 base = 8;
940 for (cur_value = 0; **str; *str += 1)
942 c = **str;
943 if (c >= ARRAY_SIZE(hex_table)) break;
944 d = hex_table[c];
945 if (d == -1 || d >= base) break;
946 cur_value = cur_value * base + d;
947 success = TRUE;
948 if (cur_value < prev_value) return FALSE; /* overflow */
949 prev_value = cur_value;
952 if (success) *value = cur_value;
953 return success;
956 static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
957 const WCHAR **terminator, IN_ADDR *address, USHORT *port)
959 ULONG fields[4];
960 int n = 0;
962 for (;;)
964 if (!parse_ipv4_component(&str, strict, &fields[n]))
965 goto error;
966 n++;
967 if (*str != '.')
968 break;
969 if (n == 4)
970 goto error;
971 str++;
974 if (strict && n < 4)
975 goto error;
977 switch (n)
979 case 4:
980 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFF || fields[3] > 0xFF)
981 goto error;
982 address->S_un.S_un_b.s_b1 = fields[0];
983 address->S_un.S_un_b.s_b2 = fields[1];
984 address->S_un.S_un_b.s_b3 = fields[2];
985 address->S_un.S_un_b.s_b4 = fields[3];
986 break;
987 case 3:
988 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFFFF)
989 goto error;
990 address->S_un.S_un_b.s_b1 = fields[0];
991 address->S_un.S_un_b.s_b2 = fields[1];
992 address->S_un.S_un_b.s_b3 = (fields[2] & 0xFF00) >> 8;
993 address->S_un.S_un_b.s_b4 = (fields[2] & 0x00FF);
994 break;
995 case 2:
996 if (fields[0] > 0xFF || fields[1] > 0xFFFFFF)
997 goto error;
998 address->S_un.S_un_b.s_b1 = fields[0];
999 address->S_un.S_un_b.s_b2 = (fields[1] & 0xFF0000) >> 16;
1000 address->S_un.S_un_b.s_b3 = (fields[1] & 0x00FF00) >> 8;
1001 address->S_un.S_un_b.s_b4 = (fields[1] & 0x0000FF);
1002 break;
1003 case 1:
1004 address->S_un.S_un_b.s_b1 = (fields[0] & 0xFF000000) >> 24;
1005 address->S_un.S_un_b.s_b2 = (fields[0] & 0x00FF0000) >> 16;
1006 address->S_un.S_un_b.s_b3 = (fields[0] & 0x0000FF00) >> 8;
1007 address->S_un.S_un_b.s_b4 = (fields[0] & 0x000000FF);
1008 break;
1009 default:
1010 goto error;
1013 if (terminator) *terminator = str;
1015 if (*str == ':')
1017 str++;
1018 if (!parse_ipv4_component(&str, FALSE, &fields[0]))
1019 goto error;
1020 if (!fields[0] || fields[0] > 0xFFFF || *str)
1021 goto error;
1022 if (port)
1024 *port = htons(fields[0]);
1025 if (terminator) *terminator = str;
1029 if (!terminator && *str)
1030 return STATUS_INVALID_PARAMETER;
1031 return STATUS_SUCCESS;
1033 error:
1034 if (terminator) *terminator = str;
1035 return STATUS_INVALID_PARAMETER;
1038 /***********************************************************************
1039 * RtlIpv4StringToAddressExW [NTDLL.@]
1041 NTSTATUS WINAPI RtlIpv4StringToAddressExW(const WCHAR *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1043 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, address, port);
1044 if (!str || !address || !port) return STATUS_INVALID_PARAMETER;
1045 return ipv4_string_to_address(str, strict, NULL, address, port);
1048 /***********************************************************************
1049 * RtlIpv4StringToAddressW [NTDLL.@]
1051 NTSTATUS WINAPI RtlIpv4StringToAddressW(const WCHAR *str, BOOLEAN strict, const WCHAR **terminator, IN_ADDR *address)
1053 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, terminator, address);
1054 return ipv4_string_to_address(str, strict, terminator, address, NULL);
1057 /***********************************************************************
1058 * RtlIpv4StringToAddressExA [NTDLL.@]
1060 NTSTATUS WINAPI RtlIpv4StringToAddressExA(const char *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1062 WCHAR wstr[32];
1064 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, address, port);
1066 if (!str || !address || !port)
1067 return STATUS_INVALID_PARAMETER;
1069 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1070 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1071 return ipv4_string_to_address(wstr, strict, NULL, address, port);
1074 /***********************************************************************
1075 * RtlIpv4StringToAddressA [NTDLL.@]
1077 NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const char **terminator, IN_ADDR *address)
1079 WCHAR wstr[32];
1080 const WCHAR *wterminator;
1081 NTSTATUS ret;
1083 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, terminator, address);
1085 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1086 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1087 ret = ipv4_string_to_address(wstr, strict, &wterminator, address, NULL);
1088 if (terminator) *terminator = str + (wterminator - wstr);
1089 return ret;
1092 static BOOL parse_ipv6_component(const WCHAR **str, int base, ULONG *value)
1094 WCHAR *terminator;
1095 if (**str >= ARRAY_SIZE(hex_table) || hex_table[**str] == -1) return FALSE;
1096 *value = min(wcstoul(*str, &terminator, base), 0x7FFFFFFF);
1097 if (*terminator == '0') terminator++; /* "0x" but nothing valid after */
1098 else if (terminator == *str) return FALSE;
1099 *str = terminator;
1100 return TRUE;
1103 static NTSTATUS ipv6_string_to_address(const WCHAR *str, BOOL ex,
1104 const WCHAR **terminator, IN6_ADDR *address, ULONG *scope, USHORT *port)
1106 BOOL expecting_port = FALSE, has_0x = FALSE, too_big = FALSE;
1107 int n_bytes = 0, n_ipv4_bytes = 0, gap = -1;
1108 ULONG ip_component, scope_component = 0, port_component = 0;
1109 const WCHAR *prev_str;
1111 if (str[0] == '[')
1113 if (!ex) goto error;
1114 expecting_port = TRUE;
1115 str++;
1118 if (str[0] == ':')
1120 if (str[1] != ':') goto error;
1121 str++;
1122 /* Windows bug: a double colon at the beginning is treated as 4 bytes of zeros instead of 2 */
1123 address->u.Word[0] = 0;
1124 n_bytes = 2;
1127 for (;;)
1129 if (!n_ipv4_bytes && *str == ':')
1131 /* double colon */
1132 if (gap != -1) goto error;
1133 str++;
1134 prev_str = str;
1135 gap = n_bytes;
1136 if (n_bytes == 14 || !parse_ipv6_component(&str, 16, &ip_component)) break;
1137 str = prev_str;
1139 else
1141 prev_str = str;
1144 if (!n_ipv4_bytes && n_bytes <= (gap != -1 ? 10 : 12))
1146 if (parse_ipv6_component(&str, 10, &ip_component) && *str == '.')
1147 n_ipv4_bytes = 1;
1148 str = prev_str;
1151 if (n_ipv4_bytes)
1153 /* IPv4 component */
1154 if (!parse_ipv6_component(&str, 10, &ip_component)) goto error;
1155 if (str - prev_str > 3 || ip_component > 255)
1157 too_big = TRUE;
1159 else
1161 if (*str != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
1162 address->u.Byte[n_bytes] = ip_component;
1163 n_bytes++;
1165 if (n_ipv4_bytes == 4 || *str != '.') break;
1166 n_ipv4_bytes++;
1168 else
1170 /* IPv6 component */
1171 if (!parse_ipv6_component(&str, 16, &ip_component)) goto error;
1172 if (prev_str[0] == '0' && (prev_str[1] == 'x' || prev_str[1] == 'X'))
1174 /* Windows "feature": the last IPv6 component can start with "0x" and be longer than 4 digits */
1175 if (terminator) *terminator = prev_str + 1; /* Windows says that the "x" is the terminator */
1176 if (n_bytes < 14 && gap == -1) return STATUS_INVALID_PARAMETER;
1177 address->u.Word[n_bytes/2] = htons(ip_component);
1178 n_bytes += 2;
1179 has_0x = TRUE;
1180 goto fill_gap;
1182 if (*str != ':' && n_bytes < 14 && gap == -1) goto error;
1183 if (str - prev_str > 4)
1184 too_big = TRUE;
1185 else
1186 address->u.Word[n_bytes/2] = htons(ip_component);
1187 n_bytes += 2;
1188 if (*str != ':' || (gap != -1 && str[1] == ':')) break;
1190 if (n_bytes == (gap != -1 ? 14 : 16)) break;
1191 if (too_big) return STATUS_INVALID_PARAMETER;
1192 str++;
1195 if (terminator) *terminator = str;
1196 if (too_big) return STATUS_INVALID_PARAMETER;
1198 fill_gap:
1199 if (gap == -1)
1201 if (n_bytes < 16) goto error;
1203 else
1205 memmove(address->u.Byte + 16 - (n_bytes - gap), address->u.Byte + gap, n_bytes - gap);
1206 memset(address->u.Byte + gap, 0, 16 - n_bytes);
1209 if (ex)
1211 if (has_0x) goto error;
1213 if (*str == '%')
1215 str++;
1216 if (!parse_ipv4_component(&str, TRUE, &scope_component)) goto error;
1219 if (expecting_port)
1221 if (*str != ']') goto error;
1222 str++;
1223 if (*str == ':')
1225 str++;
1226 if (!parse_ipv4_component(&str, FALSE, &port_component)) goto error;
1227 if (!port_component || port_component > 0xFFFF || *str) goto error;
1228 port_component = htons(port_component);
1233 if (!terminator && *str) return STATUS_INVALID_PARAMETER;
1235 if (scope) *scope = scope_component;
1236 if (port) *port = port_component;
1238 return STATUS_SUCCESS;
1240 error:
1241 if (terminator) *terminator = str;
1242 return STATUS_INVALID_PARAMETER;
1245 /***********************************************************************
1246 * RtlIpv6StringToAddressExW [NTDLL.@]
1248 NTSTATUS NTAPI RtlIpv6StringToAddressExW(const WCHAR *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1250 TRACE("(%s, %p, %p, %p)\n", debugstr_w(str), address, scope, port);
1251 if (!str || !address || !scope || !port) return STATUS_INVALID_PARAMETER;
1252 return ipv6_string_to_address(str, TRUE, NULL, address, scope, port);
1255 /***********************************************************************
1256 * RtlIpv6StringToAddressW [NTDLL.@]
1258 NTSTATUS WINAPI RtlIpv6StringToAddressW(const WCHAR *str, const WCHAR **terminator, IN6_ADDR *address)
1260 TRACE("(%s, %p, %p)\n", debugstr_w(str), terminator, address);
1261 return ipv6_string_to_address(str, FALSE, terminator, address, NULL, NULL);
1264 /***********************************************************************
1265 * RtlIpv6StringToAddressExA [NTDLL.@]
1267 NTSTATUS WINAPI RtlIpv6StringToAddressExA(const char *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1269 WCHAR wstr[128];
1271 TRACE("(%s, %p, %p, %p)\n", debugstr_a(str), address, scope, port);
1273 if (!str || !address || !scope || !port)
1274 return STATUS_INVALID_PARAMETER;
1276 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1277 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1278 return ipv6_string_to_address(wstr, TRUE, NULL, address, scope, port);
1281 /***********************************************************************
1282 * RtlIpv6StringToAddressA [NTDLL.@]
1284 NTSTATUS WINAPI RtlIpv6StringToAddressA(const char *str, const char **terminator, IN6_ADDR *address)
1286 WCHAR wstr[128];
1287 const WCHAR *wterminator = NULL;
1288 NTSTATUS ret;
1290 TRACE("(%s, %p, %p)\n", debugstr_a(str), terminator, address);
1292 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1293 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1294 ret = ipv6_string_to_address(wstr, FALSE, &wterminator, address, NULL, NULL);
1295 if (terminator && wterminator) *terminator = str + (wterminator - wstr);
1296 return ret;
1299 /***********************************************************************
1300 * RtlIpv4AddressToStringExW [NTDLL.@]
1302 * Convert the given ipv4 address and optional the port to a string
1304 * PARAMS
1305 * pin [I] PTR to the ip address to convert (network byte order)
1306 * port [I] optional port to convert (network byte order)
1307 * buffer [O] destination buffer for the result
1308 * psize [IO] PTR to available/used size of the destination buffer
1310 * RETURNS
1311 * Success: STATUS_SUCCESS
1312 * Failure: STATUS_INVALID_PARAMETER
1315 NTSTATUS WINAPI RtlIpv4AddressToStringExW(const IN_ADDR *pin, USHORT port, LPWSTR buffer, PULONG psize)
1317 WCHAR tmp_ip[32];
1318 ULONG needed;
1320 if (!pin || !buffer || !psize)
1321 return STATUS_INVALID_PARAMETER;
1323 TRACE("(%p:0x%x, %d, %p, %p:%d)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1325 needed = swprintf(tmp_ip, ARRAY_SIZE(tmp_ip), L"%u.%u.%u.%u",
1326 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1327 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1329 if (port) needed += swprintf(tmp_ip + needed, ARRAY_SIZE(tmp_ip) - needed, L":%u", ntohs(port));
1331 if (*psize > needed) {
1332 *psize = needed + 1;
1333 wcscpy(buffer, tmp_ip);
1334 return STATUS_SUCCESS;
1337 *psize = needed + 1;
1338 return STATUS_INVALID_PARAMETER;
1341 /***********************************************************************
1342 * RtlIpv4AddressToStringExA [NTDLL.@]
1344 * Convert the given ipv4 address and optional the port to a string
1346 * See RtlIpv4AddressToStringExW
1348 NTSTATUS WINAPI RtlIpv4AddressToStringExA(const IN_ADDR *pin, USHORT port, LPSTR buffer, PULONG psize)
1350 CHAR tmp_ip[32];
1351 ULONG needed;
1353 if (!pin || !buffer || !psize)
1354 return STATUS_INVALID_PARAMETER;
1356 TRACE("(%p:0x%x, %d, %p, %p:%d)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1358 needed = sprintf(tmp_ip, "%u.%u.%u.%u",
1359 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1360 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1362 if (port) needed += sprintf(tmp_ip + needed, ":%u", ntohs(port));
1364 if (*psize > needed) {
1365 *psize = needed + 1;
1366 strcpy(buffer, tmp_ip);
1367 return STATUS_SUCCESS;
1370 *psize = needed + 1;
1371 return STATUS_INVALID_PARAMETER;
1374 /***********************************************************************
1375 * RtlIpv4AddressToStringW [NTDLL.@]
1377 * Convert the given ipv4 address to a string
1379 * PARAMS
1380 * pin [I] PTR to the ip address to convert (network byte order)
1381 * buffer [O] destination buffer for the result (at least 16 character)
1383 * RETURNS
1384 * PTR to the 0 character at the end of the converted string
1387 WCHAR * WINAPI RtlIpv4AddressToStringW(const IN_ADDR *pin, LPWSTR buffer)
1389 ULONG size = 16;
1391 if (RtlIpv4AddressToStringExW(pin, 0, buffer, &size)) size = 0;
1392 return buffer + size - 1;
1395 /***********************************************************************
1396 * RtlIpv4AddressToStringA [NTDLL.@]
1398 * Convert the given ipv4 address to a string
1400 * See RtlIpv4AddressToStringW
1402 CHAR * WINAPI RtlIpv4AddressToStringA(const IN_ADDR *pin, LPSTR buffer)
1404 ULONG size = 16;
1406 if (RtlIpv4AddressToStringExA(pin, 0, buffer, &size)) size = 0;
1407 return buffer + size - 1;
1410 static BOOL is_ipv4_in_ipv6(const IN6_ADDR *address)
1412 if (address->s6_words[5] == htons(0x5efe) && (address->s6_words[4] & ~htons(0x200)) == 0)
1413 return TRUE;
1414 if (*(UINT64 *)address != 0)
1415 return FALSE;
1416 if (address->s6_words[4] != 0 && address->s6_words[4] != 0xffff)
1417 return FALSE;
1418 if (address->s6_words[4] == 0 && address->s6_words[5] != 0 && address->s6_words[5] != 0xffff)
1419 return FALSE;
1420 if (address->s6_words[4] == 0xffff && address->s6_words[5] != 0)
1421 return FALSE;
1422 if (address->s6_words[6] == 0)
1423 return FALSE;
1424 return TRUE;
1427 /***********************************************************************
1428 * RtlIpv6AddressToStringExA [NTDLL.@]
1430 NTSTATUS WINAPI RtlIpv6AddressToStringExA(const IN6_ADDR *address, ULONG scope, USHORT port, char *str, ULONG *size)
1432 char buffer[64], *p = buffer;
1433 int i, len, gap = -1, gap_len = 1, ipv6_end = 8;
1434 ULONG needed;
1435 NTSTATUS ret;
1437 TRACE("(%p %u %u %p %p)\n", address, scope, port, str, size);
1439 if (!address || !str || !size)
1440 return STATUS_INVALID_PARAMETER;
1442 if (is_ipv4_in_ipv6(address))
1443 ipv6_end = 6;
1445 for (i = 0; i < ipv6_end; i++)
1447 len = 0;
1448 while (!address->s6_words[i] && i < ipv6_end)
1450 i++;
1451 len++;
1453 if (len > gap_len)
1455 gap = i - len;
1456 gap_len = len;
1460 if (port) p += sprintf(p, "[");
1462 i = 0;
1463 while (i < ipv6_end)
1465 if (i == gap)
1467 p += sprintf(p, ":");
1468 i += gap_len;
1469 if (i == ipv6_end) p += sprintf(p, ":");
1470 continue;
1472 if (i > 0) p += sprintf(p, ":");
1473 p += sprintf(p, "%x", ntohs(address->s6_words[i]));
1474 i++;
1477 if (ipv6_end == 6)
1479 if (p[-1] != ':') p += sprintf(p, ":");
1480 p = RtlIpv4AddressToStringA((IN_ADDR *)(address->s6_words + 6), p);
1483 if (scope) p += sprintf(p, "%%%u", scope);
1485 if (port) p += sprintf(p, "]:%u", ntohs(port));
1487 needed = p - buffer + 1;
1489 if (*size >= needed)
1491 strcpy(str, buffer);
1492 ret = STATUS_SUCCESS;
1494 else
1496 ret = STATUS_INVALID_PARAMETER;
1499 *size = needed;
1500 return ret;
1503 /***********************************************************************
1504 * RtlIpv6AddressToStringA [NTDLL.@]
1506 char * WINAPI RtlIpv6AddressToStringA(const IN6_ADDR *address, char *str)
1508 ULONG size = 46;
1509 if (!address || !str) return str - 1;
1510 str[45] = 0; /* this byte is set even though the string is always shorter */
1511 RtlIpv6AddressToStringExA(address, 0, 0, str, &size);
1512 return str + size - 1;
1515 /***********************************************************************
1516 * RtlIpv6AddressToStringExW [NTDLL.@]
1518 NTSTATUS WINAPI RtlIpv6AddressToStringExW(const IN6_ADDR *address, ULONG scope, USHORT port, WCHAR *str, ULONG *size)
1520 char cstr[64];
1521 NTSTATUS ret = RtlIpv6AddressToStringExA(address, scope, port, cstr, size);
1522 if (ret == STATUS_SUCCESS) RtlMultiByteToUnicodeN(str, *size * sizeof(WCHAR), NULL, cstr, *size);
1523 return ret;
1526 /***********************************************************************
1527 * RtlIpv6AddressToStringW [NTDLL.@]
1529 WCHAR * WINAPI RtlIpv6AddressToStringW(const IN6_ADDR *address, WCHAR *str)
1531 ULONG size = 46;
1532 if (!address || !str) return str;
1533 str[45] = 0; /* this word is set even though the string is always shorter */
1534 if (RtlIpv6AddressToStringExW(address, 0, 0, str, &size) != STATUS_SUCCESS)
1535 return str;
1536 return str + size - 1;
1539 /***********************************************************************
1540 * get_pointer_obfuscator (internal)
1542 static DWORD_PTR get_pointer_obfuscator( void )
1544 static DWORD_PTR pointer_obfuscator;
1546 if (!pointer_obfuscator)
1548 ULONG seed = NtGetTickCount();
1549 ULONG_PTR rand;
1551 /* generate a random value for the obfuscator */
1552 rand = RtlUniform( &seed );
1554 /* handle 64bit pointers */
1555 rand ^= (ULONG_PTR)RtlUniform( &seed ) << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1557 /* set the high bits so dereferencing obfuscated pointers will (usually) crash */
1558 rand |= (ULONG_PTR)0xc0000000 << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1560 InterlockedCompareExchangePointer( (void**) &pointer_obfuscator, (void*) rand, NULL );
1563 return pointer_obfuscator;
1566 /*************************************************************************
1567 * RtlEncodePointer [NTDLL.@]
1569 PVOID WINAPI RtlEncodePointer( PVOID ptr )
1571 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1572 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1575 PVOID WINAPI RtlDecodePointer( PVOID ptr )
1577 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1578 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1581 /*************************************************************************
1582 * RtlInitializeSListHead [NTDLL.@]
1584 VOID WINAPI RtlInitializeSListHead(PSLIST_HEADER list)
1586 #ifdef _WIN64
1587 list->s.Alignment = list->s.Region = 0;
1588 list->Header16.HeaderType = 1; /* we use the 16-byte header */
1589 #else
1590 list->Alignment = 0;
1591 #endif
1594 /*************************************************************************
1595 * RtlQueryDepthSList [NTDLL.@]
1597 WORD WINAPI RtlQueryDepthSList(PSLIST_HEADER list)
1599 #ifdef _WIN64
1600 return list->Header16.Depth;
1601 #else
1602 return list->s.Depth;
1603 #endif
1606 /*************************************************************************
1607 * RtlFirstEntrySList [NTDLL.@]
1609 PSLIST_ENTRY WINAPI RtlFirstEntrySList(const SLIST_HEADER* list)
1611 #ifdef _WIN64
1612 return (SLIST_ENTRY *)((ULONG_PTR)list->Header16.NextEntry << 4);
1613 #else
1614 return list->s.Next.Next;
1615 #endif
1618 /*************************************************************************
1619 * RtlInterlockedFlushSList [NTDLL.@]
1621 PSLIST_ENTRY WINAPI RtlInterlockedFlushSList(PSLIST_HEADER list)
1623 SLIST_HEADER old, new;
1625 #ifdef _WIN64
1626 if (!list->Header16.NextEntry) return NULL;
1627 new.s.Alignment = new.s.Region = 0;
1628 new.Header16.HeaderType = 1; /* we use the 16-byte header */
1631 old = *list;
1632 new.Header16.Sequence = old.Header16.Sequence + 1;
1633 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1634 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1635 #else
1636 if (!list->s.Next.Next) return NULL;
1637 new.Alignment = 0;
1640 old = *list;
1641 new.s.Sequence = old.s.Sequence + 1;
1642 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1643 old.Alignment) != old.Alignment);
1644 return old.s.Next.Next;
1645 #endif
1648 /*************************************************************************
1649 * RtlInterlockedPushEntrySList [NTDLL.@]
1651 PSLIST_ENTRY WINAPI RtlInterlockedPushEntrySList(PSLIST_HEADER list, PSLIST_ENTRY entry)
1653 SLIST_HEADER old, new;
1655 #ifdef _WIN64
1656 new.Header16.NextEntry = (ULONG_PTR)entry >> 4;
1659 old = *list;
1660 entry->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1661 new.Header16.Depth = old.Header16.Depth + 1;
1662 new.Header16.Sequence = old.Header16.Sequence + 1;
1663 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1664 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1665 #else
1666 new.s.Next.Next = entry;
1669 old = *list;
1670 entry->Next = old.s.Next.Next;
1671 new.s.Depth = old.s.Depth + 1;
1672 new.s.Sequence = old.s.Sequence + 1;
1673 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1674 old.Alignment) != old.Alignment);
1675 return old.s.Next.Next;
1676 #endif
1679 /*************************************************************************
1680 * RtlInterlockedPopEntrySList [NTDLL.@]
1682 PSLIST_ENTRY WINAPI RtlInterlockedPopEntrySList(PSLIST_HEADER list)
1684 SLIST_HEADER old, new;
1685 PSLIST_ENTRY entry;
1687 #ifdef _WIN64
1690 old = *list;
1691 if (!(entry = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4))) return NULL;
1692 /* entry could be deleted by another thread */
1693 __TRY
1695 new.Header16.NextEntry = (ULONG_PTR)entry->Next >> 4;
1696 new.Header16.Depth = old.Header16.Depth - 1;
1697 new.Header16.Sequence = old.Header16.Sequence + 1;
1699 __EXCEPT_PAGE_FAULT
1702 __ENDTRY
1703 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1704 #else
1707 old = *list;
1708 if (!(entry = old.s.Next.Next)) return NULL;
1709 /* entry could be deleted by another thread */
1710 __TRY
1712 new.s.Next.Next = entry->Next;
1713 new.s.Depth = old.s.Depth - 1;
1714 new.s.Sequence = old.s.Sequence + 1;
1716 __EXCEPT_PAGE_FAULT
1719 __ENDTRY
1720 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1721 old.Alignment) != old.Alignment);
1722 #endif
1723 return entry;
1726 /*************************************************************************
1727 * RtlInterlockedPushListSListEx [NTDLL.@]
1729 PSLIST_ENTRY WINAPI RtlInterlockedPushListSListEx(PSLIST_HEADER list, PSLIST_ENTRY first,
1730 PSLIST_ENTRY last, ULONG count)
1732 SLIST_HEADER old, new;
1734 #ifdef _WIN64
1735 new.Header16.NextEntry = (ULONG_PTR)first >> 4;
1738 old = *list;
1739 new.Header16.Depth = old.Header16.Depth + count;
1740 new.Header16.Sequence = old.Header16.Sequence + 1;
1741 last->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1742 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1743 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1744 #else
1745 new.s.Next.Next = first;
1748 old = *list;
1749 new.s.Depth = old.s.Depth + count;
1750 new.s.Sequence = old.s.Sequence + 1;
1751 last->Next = old.s.Next.Next;
1752 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1753 old.Alignment) != old.Alignment);
1754 return old.s.Next.Next;
1755 #endif
1758 /*************************************************************************
1759 * RtlInterlockedPushListSList [NTDLL.@]
1761 DEFINE_FASTCALL_WRAPPER(RtlInterlockedPushListSList, 16)
1762 PSLIST_ENTRY FASTCALL RtlInterlockedPushListSList(PSLIST_HEADER list, PSLIST_ENTRY first,
1763 PSLIST_ENTRY last, ULONG count)
1765 return RtlInterlockedPushListSListEx(list, first, last, count);
1768 /******************************************************************************
1769 * RtlGetCompressionWorkSpaceSize [NTDLL.@]
1771 NTSTATUS WINAPI RtlGetCompressionWorkSpaceSize(USHORT format, PULONG compress_workspace,
1772 PULONG decompress_workspace)
1774 FIXME("0x%04x, %p, %p: semi-stub\n", format, compress_workspace, decompress_workspace);
1776 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1778 case COMPRESSION_FORMAT_LZNT1:
1779 if (compress_workspace)
1781 /* FIXME: The current implementation of RtlCompressBuffer does not use a
1782 * workspace buffer, but Windows applications might expect a nonzero value. */
1783 *compress_workspace = 16;
1785 if (decompress_workspace)
1786 *decompress_workspace = 0x1000;
1787 return STATUS_SUCCESS;
1789 case COMPRESSION_FORMAT_NONE:
1790 case COMPRESSION_FORMAT_DEFAULT:
1791 return STATUS_INVALID_PARAMETER;
1793 default:
1794 FIXME("format %u not implemented\n", format);
1795 return STATUS_UNSUPPORTED_COMPRESSION;
1799 /* compress data using LZNT1, currently only a stub */
1800 static NTSTATUS lznt1_compress(UCHAR *src, ULONG src_size, UCHAR *dst, ULONG dst_size,
1801 ULONG chunk_size, ULONG *final_size, UCHAR *workspace)
1803 UCHAR *src_cur = src, *src_end = src + src_size;
1804 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1805 ULONG block_size;
1807 while (src_cur < src_end)
1809 /* determine size of current chunk */
1810 block_size = min(0x1000, src_end - src_cur);
1811 if (dst_cur + sizeof(WORD) + block_size > dst_end)
1812 return STATUS_BUFFER_TOO_SMALL;
1814 /* write (uncompressed) chunk header */
1815 *(WORD *)dst_cur = 0x3000 | (block_size - 1);
1816 dst_cur += sizeof(WORD);
1818 /* write chunk content */
1819 memcpy(dst_cur, src_cur, block_size);
1820 dst_cur += block_size;
1821 src_cur += block_size;
1824 if (final_size)
1825 *final_size = dst_cur - dst;
1827 return STATUS_SUCCESS;
1830 /******************************************************************************
1831 * RtlCompressBuffer [NTDLL.@]
1833 NTSTATUS WINAPI RtlCompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
1834 PUCHAR compressed, ULONG compressed_size, ULONG chunk_size,
1835 PULONG final_size, PVOID workspace)
1837 FIXME("0x%04x, %p, %u, %p, %u, %u, %p, %p: semi-stub\n", format, uncompressed,
1838 uncompressed_size, compressed, compressed_size, chunk_size, final_size, workspace);
1840 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1842 case COMPRESSION_FORMAT_LZNT1:
1843 return lznt1_compress(uncompressed, uncompressed_size, compressed,
1844 compressed_size, chunk_size, final_size, workspace);
1846 case COMPRESSION_FORMAT_NONE:
1847 case COMPRESSION_FORMAT_DEFAULT:
1848 return STATUS_INVALID_PARAMETER;
1850 default:
1851 FIXME("format %u not implemented\n", format);
1852 return STATUS_UNSUPPORTED_COMPRESSION;
1856 /* decompress a single LZNT1 chunk */
1857 static UCHAR *lznt1_decompress_chunk(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size)
1859 UCHAR *src_cur = src, *src_end = src + src_size;
1860 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1861 ULONG displacement_bits, length_bits;
1862 ULONG code_displacement, code_length;
1863 WORD flags, code;
1865 while (src_cur < src_end && dst_cur < dst_end)
1867 flags = 0x8000 | *src_cur++;
1868 while ((flags & 0xff00) && src_cur < src_end)
1870 if (flags & 1)
1872 /* backwards reference */
1873 if (src_cur + sizeof(WORD) > src_end)
1874 return NULL;
1876 code = *(WORD *)src_cur;
1877 src_cur += sizeof(WORD);
1879 /* find length / displacement bits */
1880 for (displacement_bits = 12; displacement_bits > 4; displacement_bits--)
1881 if ((1 << (displacement_bits - 1)) < dst_cur - dst) break;
1883 length_bits = 16 - displacement_bits;
1884 code_length = (code & ((1 << length_bits) - 1)) + 3;
1885 code_displacement = (code >> length_bits) + 1;
1887 if (dst_cur < dst + code_displacement)
1888 return NULL;
1890 /* copy bytes of chunk - we can't use memcpy() since source and dest can
1891 * be overlapping, and the same bytes can be repeated over and over again */
1892 while (code_length--)
1894 if (dst_cur >= dst_end) return dst_cur;
1895 *dst_cur = *(dst_cur - code_displacement);
1896 dst_cur++;
1899 else
1901 /* uncompressed data */
1902 if (dst_cur >= dst_end) return dst_cur;
1903 *dst_cur++ = *src_cur++;
1905 flags >>= 1;
1909 return dst_cur;
1912 /* decompress data encoded with LZNT1 */
1913 static NTSTATUS lznt1_decompress(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size,
1914 ULONG offset, ULONG *final_size, UCHAR *workspace)
1916 UCHAR *src_cur = src, *src_end = src + src_size;
1917 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1918 ULONG chunk_size, block_size;
1919 WORD chunk_header;
1920 UCHAR *ptr;
1922 if (src_cur + sizeof(WORD) > src_end)
1923 return STATUS_BAD_COMPRESSION_BUFFER;
1925 /* skip over chunks with a distance >= 0x1000 to the destination offset */
1926 while (offset >= 0x1000 && src_cur + sizeof(WORD) <= src_end)
1928 chunk_header = *(WORD *)src_cur;
1929 src_cur += sizeof(WORD);
1930 if (!chunk_header) goto out;
1931 chunk_size = (chunk_header & 0xfff) + 1;
1933 if (src_cur + chunk_size > src_end)
1934 return STATUS_BAD_COMPRESSION_BUFFER;
1936 src_cur += chunk_size;
1937 offset -= 0x1000;
1940 /* handle partially included chunk */
1941 if (offset && src_cur + sizeof(WORD) <= src_end)
1943 chunk_header = *(WORD *)src_cur;
1944 src_cur += sizeof(WORD);
1945 if (!chunk_header) goto out;
1946 chunk_size = (chunk_header & 0xfff) + 1;
1948 if (src_cur + chunk_size > src_end)
1949 return STATUS_BAD_COMPRESSION_BUFFER;
1951 if (dst_cur >= dst_end)
1952 goto out;
1954 if (chunk_header & 0x8000)
1956 /* compressed chunk */
1957 if (!workspace) return STATUS_ACCESS_VIOLATION;
1958 ptr = lznt1_decompress_chunk(workspace, 0x1000, src_cur, chunk_size);
1959 if (!ptr) return STATUS_BAD_COMPRESSION_BUFFER;
1960 if (ptr - workspace > offset)
1962 block_size = min((ptr - workspace) - offset, dst_end - dst_cur);
1963 memcpy(dst_cur, workspace + offset, block_size);
1964 dst_cur += block_size;
1967 else
1969 /* uncompressed chunk */
1970 if (chunk_size > offset)
1972 block_size = min(chunk_size - offset, dst_end - dst_cur);
1973 memcpy(dst_cur, src_cur + offset, block_size);
1974 dst_cur += block_size;
1978 src_cur += chunk_size;
1981 /* handle remaining chunks */
1982 while (src_cur + sizeof(WORD) <= src_end)
1984 chunk_header = *(WORD *)src_cur;
1985 src_cur += sizeof(WORD);
1986 if (!chunk_header) goto out;
1987 chunk_size = (chunk_header & 0xfff) + 1;
1989 if (src_cur + chunk_size > src_end)
1990 return STATUS_BAD_COMPRESSION_BUFFER;
1992 /* fill space with padding when the previous chunk was decompressed
1993 * to less than 4096 bytes. no padding is needed for the last chunk
1994 * or when the next chunk is truncated */
1995 block_size = ((dst_cur - dst) + offset) & 0xfff;
1996 if (block_size)
1998 block_size = 0x1000 - block_size;
1999 if (dst_cur + block_size >= dst_end)
2000 goto out;
2001 memset(dst_cur, 0, block_size);
2002 dst_cur += block_size;
2005 if (dst_cur >= dst_end)
2006 goto out;
2008 if (chunk_header & 0x8000)
2010 /* compressed chunk */
2011 dst_cur = lznt1_decompress_chunk(dst_cur, dst_end - dst_cur, src_cur, chunk_size);
2012 if (!dst_cur) return STATUS_BAD_COMPRESSION_BUFFER;
2014 else
2016 /* uncompressed chunk */
2017 block_size = min(chunk_size, dst_end - dst_cur);
2018 memcpy(dst_cur, src_cur, block_size);
2019 dst_cur += block_size;
2022 src_cur += chunk_size;
2025 out:
2026 if (final_size)
2027 *final_size = dst_cur - dst;
2029 return STATUS_SUCCESS;
2033 /******************************************************************************
2034 * RtlDecompressFragment [NTDLL.@]
2036 NTSTATUS WINAPI RtlDecompressFragment(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2037 PUCHAR compressed, ULONG compressed_size, ULONG offset,
2038 PULONG final_size, PVOID workspace)
2040 TRACE("0x%04x, %p, %u, %p, %u, %u, %p, %p\n", format, uncompressed,
2041 uncompressed_size, compressed, compressed_size, offset, final_size, workspace);
2043 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
2045 case COMPRESSION_FORMAT_LZNT1:
2046 return lznt1_decompress(uncompressed, uncompressed_size, compressed,
2047 compressed_size, offset, final_size, workspace);
2049 case COMPRESSION_FORMAT_NONE:
2050 case COMPRESSION_FORMAT_DEFAULT:
2051 return STATUS_INVALID_PARAMETER;
2053 default:
2054 FIXME("format %u not implemented\n", format);
2055 return STATUS_UNSUPPORTED_COMPRESSION;
2060 /******************************************************************************
2061 * RtlDecompressBuffer [NTDLL.@]
2063 NTSTATUS WINAPI RtlDecompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2064 PUCHAR compressed, ULONG compressed_size, PULONG final_size)
2066 TRACE("0x%04x, %p, %u, %p, %u, %p\n", format, uncompressed,
2067 uncompressed_size, compressed, compressed_size, final_size);
2069 return RtlDecompressFragment(format, uncompressed, uncompressed_size,
2070 compressed, compressed_size, 0, final_size, NULL);
2073 /***********************************************************************
2074 * RtlSetThreadErrorMode [NTDLL.@]
2076 * Set the thread local error mode.
2078 * PARAMS
2079 * mode [I] The new error mode
2080 * oldmode [O] Destination of the old error mode (may be NULL)
2082 * RETURNS
2083 * Success: STATUS_SUCCESS
2084 * Failure: STATUS_INVALID_PARAMETER_1
2086 NTSTATUS WINAPI RtlSetThreadErrorMode( DWORD mode, LPDWORD oldmode )
2088 if (mode & ~0x70)
2089 return STATUS_INVALID_PARAMETER_1;
2091 if (oldmode)
2092 *oldmode = NtCurrentTeb()->HardErrorDisabled;
2094 NtCurrentTeb()->HardErrorDisabled = mode;
2095 return STATUS_SUCCESS;
2098 /***********************************************************************
2099 * RtlGetThreadErrorMode [NTDLL.@]
2101 * Get the thread local error mode.
2103 * PARAMS
2104 * None.
2106 * RETURNS
2107 * The current thread local error mode.
2109 DWORD WINAPI RtlGetThreadErrorMode( void )
2111 return NtCurrentTeb()->HardErrorDisabled;
2114 /******************************************************************************
2115 * RtlGetCurrentTransaction [NTDLL.@]
2117 HANDLE WINAPI RtlGetCurrentTransaction(void)
2119 FIXME("() :stub\n");
2120 return NULL;
2123 /******************************************************************************
2124 * RtlSetCurrentTransaction [NTDLL.@]
2126 BOOL WINAPI RtlSetCurrentTransaction(HANDLE new_transaction)
2128 FIXME("(%p) :stub\n", new_transaction);
2129 return FALSE;
2132 /**********************************************************************
2133 * RtlGetCurrentProcessorNumberEx [NTDLL.@]
2135 void WINAPI RtlGetCurrentProcessorNumberEx(PROCESSOR_NUMBER *processor)
2137 FIXME("(%p) :semi-stub\n", processor);
2138 processor->Group = 0;
2139 processor->Number = NtGetCurrentProcessorNumber();
2140 processor->Reserved = 0;
2143 /***********************************************************************
2144 * RtlIsProcessorFeaturePresent [NTDLL.@]
2146 BOOLEAN WINAPI RtlIsProcessorFeaturePresent( UINT feature )
2148 return feature < PROCESSOR_FEATURE_MAX && user_shared_data->ProcessorFeatures[feature];
2151 /***********************************************************************
2152 * RtlInitializeGenericTableAvl (NTDLL.@)
2154 void WINAPI RtlInitializeGenericTableAvl(PRTL_AVL_TABLE table, PRTL_AVL_COMPARE_ROUTINE compare,
2155 PRTL_AVL_ALLOCATE_ROUTINE allocate, PRTL_AVL_FREE_ROUTINE free, void *context)
2157 FIXME("%p %p %p %p %p: stub\n", table, compare, allocate, free, context);
2160 /***********************************************************************
2161 * RtlInsertElementGenericTableAvl (NTDLL.@)
2163 void WINAPI RtlInsertElementGenericTableAvl(PRTL_AVL_TABLE table, void *buffer, ULONG size, BOOL *element)
2165 FIXME("%p %p %u %p: stub\n", table, buffer, size, element);
2168 /*********************************************************************
2169 * RtlQueryPackageIdentity [NTDLL.@]
2171 NTSTATUS WINAPI RtlQueryPackageIdentity(HANDLE token, WCHAR *fullname, SIZE_T *fullname_size,
2172 WCHAR *appid, SIZE_T *appid_size, BOOLEAN *packaged)
2174 FIXME("(%p, %p, %p, %p, %p, %p): stub\n", token, fullname, fullname_size, appid, appid_size, packaged);
2175 return STATUS_NOT_FOUND;
2178 /*********************************************************************
2179 * RtlQueryProcessPlaceholderCompatibilityMode [NTDLL.@]
2181 char WINAPI RtlQueryProcessPlaceholderCompatibilityMode(void)
2183 FIXME("stub\n");
2184 return PHCM_APPLICATION_DEFAULT;