kernel32: Move ReadConsole and WriteConsole to kernelbase.
[wine.git] / dlls / ntdll / rtl.c
blobca4fea842098a1464d434605491b24505e527974
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);
43 /* CRC polynomial 0xedb88320 */
44 static const DWORD CRC_table[256] =
46 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
47 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
48 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
49 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
50 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
51 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
52 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
53 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
54 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
55 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
56 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
57 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
58 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
59 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
60 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
61 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
62 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
63 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
64 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
65 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
66 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
67 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
68 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
69 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
70 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
71 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
72 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
73 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
74 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
75 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
76 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
77 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
78 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
79 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
80 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
81 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
82 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
83 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
84 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
85 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
86 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
87 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
88 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
91 static const int hex_table[] = {
92 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
93 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
94 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2F */
95 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
96 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4F */
97 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
98 -1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
102 * resource functions
105 /***********************************************************************
106 * RtlInitializeResource (NTDLL.@)
108 * xxxResource() functions implement multiple-reader-single-writer lock.
109 * The code is based on information published in WDJ January 1999 issue.
111 void WINAPI RtlInitializeResource(LPRTL_RWLOCK rwl)
113 if( rwl )
115 rwl->iNumberActive = 0;
116 rwl->uExclusiveWaiters = 0;
117 rwl->uSharedWaiters = 0;
118 rwl->hOwningThreadId = 0;
119 rwl->dwTimeoutBoost = 0; /* no info on this one, default value is 0 */
120 RtlInitializeCriticalSection( &rwl->rtlCS );
121 rwl->rtlCS.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RTL_RWLOCK.rtlCS");
122 NtCreateSemaphore( &rwl->hExclusiveReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
123 NtCreateSemaphore( &rwl->hSharedReleaseSemaphore, SEMAPHORE_ALL_ACCESS, NULL, 0, 65535 );
128 /***********************************************************************
129 * RtlDeleteResource (NTDLL.@)
131 void WINAPI RtlDeleteResource(LPRTL_RWLOCK rwl)
133 if( rwl )
135 RtlEnterCriticalSection( &rwl->rtlCS );
136 if( rwl->iNumberActive || rwl->uExclusiveWaiters || rwl->uSharedWaiters )
137 ERR("Deleting active MRSW lock (%p), expect failure\n", rwl );
138 rwl->hOwningThreadId = 0;
139 rwl->uExclusiveWaiters = rwl->uSharedWaiters = 0;
140 rwl->iNumberActive = 0;
141 NtClose( rwl->hExclusiveReleaseSemaphore );
142 NtClose( rwl->hSharedReleaseSemaphore );
143 RtlLeaveCriticalSection( &rwl->rtlCS );
144 rwl->rtlCS.DebugInfo->Spare[0] = 0;
145 RtlDeleteCriticalSection( &rwl->rtlCS );
150 /***********************************************************************
151 * RtlAcquireResourceExclusive (NTDLL.@)
153 BYTE WINAPI RtlAcquireResourceExclusive(LPRTL_RWLOCK rwl, BYTE fWait)
155 BYTE retVal = 0;
156 if( !rwl ) return 0;
158 start:
159 RtlEnterCriticalSection( &rwl->rtlCS );
160 if( rwl->iNumberActive == 0 ) /* lock is free */
162 rwl->iNumberActive = -1;
163 retVal = 1;
165 else if( rwl->iNumberActive < 0 ) /* exclusive lock in progress */
167 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
169 retVal = 1;
170 rwl->iNumberActive--;
171 goto done;
173 wait:
174 if( fWait )
176 NTSTATUS status;
178 rwl->uExclusiveWaiters++;
180 RtlLeaveCriticalSection( &rwl->rtlCS );
181 status = NtWaitForSingleObject( rwl->hExclusiveReleaseSemaphore, FALSE, NULL );
182 if( HIWORD(status) )
183 goto done;
184 goto start; /* restart the acquisition to avoid deadlocks */
187 else /* one or more shared locks are in progress */
188 if( fWait )
189 goto wait;
191 if( retVal == 1 )
192 rwl->hOwningThreadId = ULongToHandle(GetCurrentThreadId());
193 done:
194 RtlLeaveCriticalSection( &rwl->rtlCS );
195 return retVal;
198 /***********************************************************************
199 * RtlAcquireResourceShared (NTDLL.@)
201 BYTE WINAPI RtlAcquireResourceShared(LPRTL_RWLOCK rwl, BYTE fWait)
203 NTSTATUS status = STATUS_UNSUCCESSFUL;
204 BYTE retVal = 0;
205 if( !rwl ) return 0;
207 start:
208 RtlEnterCriticalSection( &rwl->rtlCS );
209 if( rwl->iNumberActive < 0 )
211 if( rwl->hOwningThreadId == ULongToHandle(GetCurrentThreadId()) )
213 rwl->iNumberActive--;
214 retVal = 1;
215 goto done;
218 if( fWait )
220 rwl->uSharedWaiters++;
221 RtlLeaveCriticalSection( &rwl->rtlCS );
222 status = NtWaitForSingleObject( rwl->hSharedReleaseSemaphore, FALSE, NULL );
223 if( HIWORD(status) )
224 goto done;
225 goto start;
228 else
230 if( status != STATUS_WAIT_0 ) /* otherwise RtlReleaseResource() has already done it */
231 rwl->iNumberActive++;
232 retVal = 1;
234 done:
235 RtlLeaveCriticalSection( &rwl->rtlCS );
236 return retVal;
240 /***********************************************************************
241 * RtlReleaseResource (NTDLL.@)
243 void WINAPI RtlReleaseResource(LPRTL_RWLOCK rwl)
245 RtlEnterCriticalSection( &rwl->rtlCS );
247 if( rwl->iNumberActive > 0 ) /* have one or more readers */
249 if( --rwl->iNumberActive == 0 )
251 if( rwl->uExclusiveWaiters )
253 wake_exclusive:
254 rwl->uExclusiveWaiters--;
255 NtReleaseSemaphore( rwl->hExclusiveReleaseSemaphore, 1, NULL );
259 else
260 if( rwl->iNumberActive < 0 ) /* have a writer, possibly recursive */
262 if( ++rwl->iNumberActive == 0 )
264 rwl->hOwningThreadId = 0;
265 if( rwl->uExclusiveWaiters )
266 goto wake_exclusive;
267 else
268 if( rwl->uSharedWaiters )
270 UINT n = rwl->uSharedWaiters;
271 rwl->iNumberActive = rwl->uSharedWaiters; /* prevent new writers from joining until
272 * all queued readers have done their thing */
273 rwl->uSharedWaiters = 0;
274 NtReleaseSemaphore( rwl->hSharedReleaseSemaphore, n, NULL );
278 RtlLeaveCriticalSection( &rwl->rtlCS );
282 /***********************************************************************
283 * RtlDumpResource (NTDLL.@)
285 void WINAPI RtlDumpResource(LPRTL_RWLOCK rwl)
287 if( rwl )
289 MESSAGE("RtlDumpResource(%p):\n\tactive count = %i\n\twaiting readers = %i\n\twaiting writers = %i\n",
290 rwl, rwl->iNumberActive, rwl->uSharedWaiters, rwl->uExclusiveWaiters );
291 if( rwl->iNumberActive )
292 MESSAGE("\towner thread = %p\n", rwl->hOwningThreadId );
297 * misc functions
300 /******************************************************************************
301 * DbgPrint [NTDLL.@]
303 NTSTATUS WINAPIV DbgPrint(LPCSTR fmt, ...)
305 char buf[512];
306 __ms_va_list args;
308 __ms_va_start(args, fmt);
309 _vsnprintf(buf, sizeof(buf), fmt, args);
310 __ms_va_end(args);
312 MESSAGE("DbgPrint says: %s",buf);
313 /* hmm, raise exception? */
314 return STATUS_SUCCESS;
318 /******************************************************************************
319 * DbgPrintEx [NTDLL.@]
321 NTSTATUS WINAPIV DbgPrintEx(ULONG iComponentId, ULONG Level, LPCSTR fmt, ...)
323 NTSTATUS ret;
324 __ms_va_list args;
326 __ms_va_start(args, fmt);
327 ret = vDbgPrintEx(iComponentId, Level, fmt, args);
328 __ms_va_end(args);
329 return ret;
332 /******************************************************************************
333 * vDbgPrintEx [NTDLL.@]
335 NTSTATUS WINAPI vDbgPrintEx( ULONG id, ULONG level, LPCSTR fmt, __ms_va_list args )
337 return vDbgPrintExWithPrefix( "", id, level, fmt, args );
340 /******************************************************************************
341 * vDbgPrintExWithPrefix [NTDLL.@]
343 NTSTATUS WINAPI vDbgPrintExWithPrefix( LPCSTR prefix, ULONG id, ULONG level, LPCSTR fmt, __ms_va_list args )
345 char buf[1024];
347 _vsnprintf(buf, sizeof(buf), fmt, args);
349 switch (level & DPFLTR_MASK)
351 case DPFLTR_ERROR_LEVEL: ERR("%s%x: %s", prefix, id, buf); break;
352 case DPFLTR_WARNING_LEVEL: WARN("%s%x: %s", prefix, id, buf); break;
353 case DPFLTR_TRACE_LEVEL:
354 case DPFLTR_INFO_LEVEL:
355 default: TRACE("%s%x: %s", prefix, id, buf); break;
357 return STATUS_SUCCESS;
360 /******************************************************************************
361 * RtlAcquirePebLock [NTDLL.@]
363 VOID WINAPI RtlAcquirePebLock(void)
365 RtlEnterCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
368 /******************************************************************************
369 * RtlReleasePebLock [NTDLL.@]
371 VOID WINAPI RtlReleasePebLock(void)
373 RtlLeaveCriticalSection( NtCurrentTeb()->Peb->FastPebLock );
376 /******************************************************************************
377 * RtlNewSecurityObject [NTDLL.@]
379 NTSTATUS WINAPI
380 RtlNewSecurityObject( PSECURITY_DESCRIPTOR ParentDescriptor,
381 PSECURITY_DESCRIPTOR CreatorDescriptor,
382 PSECURITY_DESCRIPTOR *NewDescriptor,
383 BOOLEAN IsDirectoryObject,
384 HANDLE Token,
385 PGENERIC_MAPPING GenericMapping )
387 FIXME("(%p %p %p %d %p %p) stub!\n", ParentDescriptor, CreatorDescriptor,
388 NewDescriptor, IsDirectoryObject, Token, GenericMapping);
389 return STATUS_NOT_IMPLEMENTED;
392 /******************************************************************************
393 * RtlDeleteSecurityObject [NTDLL.@]
395 NTSTATUS WINAPI
396 RtlDeleteSecurityObject( PSECURITY_DESCRIPTOR *ObjectDescriptor )
398 FIXME("(%p) stub!\n", ObjectDescriptor);
399 return STATUS_NOT_IMPLEMENTED;
402 /******************************************************************************
403 * RtlInitializeGenericTable [NTDLL.@]
405 void WINAPI RtlInitializeGenericTable(RTL_GENERIC_TABLE *table, PRTL_GENERIC_COMPARE_ROUTINE compare,
406 PRTL_GENERIC_ALLOCATE_ROUTINE allocate, PRTL_GENERIC_FREE_ROUTINE free,
407 void *context)
409 FIXME("(%p, %p, %p, %p, %p) stub!\n", table, compare, allocate, free, context);
412 /******************************************************************************
413 * RtlEnumerateGenericTableWithoutSplaying [NTDLL.@]
415 void * WINAPI RtlEnumerateGenericTableWithoutSplaying(RTL_GENERIC_TABLE *table, void *previous)
417 static int warn_once;
419 if (!warn_once++)
420 FIXME("(%p, %p) stub!\n", table, previous);
421 return NULL;
424 /******************************************************************************
425 * RtlNumberGenericTableElements [NTDLL.@]
427 ULONG WINAPI RtlNumberGenericTableElements(RTL_GENERIC_TABLE *table)
429 FIXME("(%p) stub!\n", table);
430 return 0;
433 /******************************************************************************
434 * RtlMoveMemory [NTDLL.@]
436 * Move a block of memory that may overlap.
438 * PARAMS
439 * Destination [O] End destination for block
440 * Source [O] Where to start copying from
441 * Length [I] Number of bytes to copy
443 * RETURNS
444 * Nothing.
446 #undef RtlMoveMemory
447 VOID WINAPI RtlMoveMemory( void *Destination, const void *Source, SIZE_T Length )
449 memmove(Destination, Source, Length);
452 /******************************************************************************
453 * RtlFillMemory [NTDLL.@]
455 * Set a block of memory with a value.
457 * PARAMS
458 * Destination [O] Block to fill
459 * Length [I] Number of bytes to fill
460 * Fill [I] Value to set
462 * RETURNS
463 * Nothing.
465 #undef RtlFillMemory
466 VOID WINAPI RtlFillMemory( VOID *Destination, SIZE_T Length, BYTE Fill )
468 memset(Destination, Fill, Length);
471 /******************************************************************************
472 * RtlZeroMemory [NTDLL.@]
474 * Set a block of memory with 0's.
476 * PARAMS
477 * Destination [O] Block to fill
478 * Length [I] Number of bytes to fill
480 * RETURNS
481 * Nothing.
483 #undef RtlZeroMemory
484 VOID WINAPI RtlZeroMemory( VOID *Destination, SIZE_T Length )
486 memset(Destination, 0, Length);
489 /******************************************************************************
490 * RtlCompareMemory [NTDLL.@]
492 * Compare one block of memory with another
494 * PARAMS
495 * Source1 [I] Source block
496 * Source2 [I] Block to compare to Source1
497 * Length [I] Number of bytes to compare
499 * RETURNS
500 * The length of the first byte at which Source1 and Source2 differ, or Length
501 * if they are the same.
503 SIZE_T WINAPI RtlCompareMemory( const VOID *Source1, const VOID *Source2, SIZE_T Length)
505 SIZE_T i;
506 for(i=0; (i<Length) && (((const BYTE*)Source1)[i]==((const BYTE*)Source2)[i]); i++);
507 return i;
510 /******************************************************************************
511 * RtlCompareMemoryUlong [NTDLL.@]
513 * Compare a block of memory with a value, a ULONG at a time
515 * PARAMS
516 * Source1 [I] Source block. This must be ULONG aligned
517 * Length [I] Number of bytes to compare. This should be a multiple of 4
518 * dwVal [I] Value to compare to
520 * RETURNS
521 * The byte position of the first byte at which Source1 is not dwVal.
523 SIZE_T WINAPI RtlCompareMemoryUlong(VOID *Source1, SIZE_T Length, ULONG dwVal)
525 SIZE_T i;
526 for(i = 0; i < Length/sizeof(ULONG) && ((ULONG *)Source1)[i] == dwVal; i++);
527 return i * sizeof(ULONG);
530 /******************************************************************************
531 * RtlCopyMemory [NTDLL.@]
533 #undef RtlCopyMemory
534 void WINAPI RtlCopyMemory(void *dest, const void *src, SIZE_T len)
536 memcpy(dest, src, len);
539 /******************************************************************************
540 * RtlAssert [NTDLL.@]
542 * Fail a debug assertion.
544 * RETURNS
545 * Nothing. This call does not return control to its caller.
547 * NOTES
548 * Not implemented in non-debug versions.
550 void WINAPI RtlAssert(void *assertion, void *filename, ULONG linenumber, char *message)
552 FIXME("(%s, %s, %u, %s): stub\n", debugstr_a((char*)assertion), debugstr_a((char*)filename),
553 linenumber, debugstr_a(message));
556 /*************************************************************************
557 * RtlFillMemoryUlong [NTDLL.@]
559 * Fill memory with a 32 bit (dword) value.
561 * PARAMS
562 * lpDest [I] Bitmap pointer
563 * ulCount [I] Number of dwords to write
564 * ulValue [I] Value to fill with
566 * RETURNS
567 * Nothing.
569 VOID WINAPI RtlFillMemoryUlong(ULONG* lpDest, ULONG ulCount, ULONG ulValue)
571 TRACE("(%p,%d,%d)\n", lpDest, ulCount, ulValue);
573 ulCount /= sizeof(ULONG);
574 while(ulCount--)
575 *lpDest++ = ulValue;
578 /*********************************************************************
579 * RtlComputeCrc32 [NTDLL.@]
581 * Calculate the CRC32 checksum of a block of bytes
583 * PARAMS
584 * dwInitial [I] Initial CRC value
585 * pData [I] Data block
586 * iLen [I] Length of the byte block
588 * RETURNS
589 * The cumulative CRC32 of dwInitial and iLen bytes of the pData block.
591 DWORD WINAPI RtlComputeCrc32(DWORD dwInitial, const BYTE *pData, INT iLen)
593 DWORD crc = ~dwInitial;
595 TRACE("(%d,%p,%d)\n", dwInitial, pData, iLen);
597 while (iLen > 0)
599 crc = CRC_table[(crc ^ *pData) & 0xff] ^ (crc >> 8);
600 pData++;
601 iLen--;
603 return ~crc;
607 /*************************************************************************
608 * RtlUlonglongByteSwap [NTDLL.@]
610 * Swap the bytes of an unsigned long long value.
612 * PARAMS
613 * i [I] Value to swap bytes of
615 * RETURNS
616 * The value with its bytes swapped.
618 ULONGLONG __cdecl RtlUlonglongByteSwap(ULONGLONG i)
620 return ((ULONGLONG)RtlUlongByteSwap(i) << 32) | RtlUlongByteSwap(i>>32);
623 /*************************************************************************
624 * RtlUlongByteSwap [NTDLL.@]
626 * Swap the bytes of an unsigned int value.
628 * NOTES
629 * ix86 version takes argument in %ecx. Other systems use the inline version.
631 #ifdef __i386__
632 __ASM_GLOBAL_FUNC(NTDLL_RtlUlongByteSwap,
633 "movl %ecx,%eax\n\t"
634 "bswap %eax\n\t"
635 "ret")
636 #endif
638 /*************************************************************************
639 * RtlUshortByteSwap [NTDLL.@]
641 * Swap the bytes of an unsigned short value.
643 * NOTES
644 * i386 version takes argument in %cx. Other systems use the inline version.
646 #ifdef __i386__
647 __ASM_GLOBAL_FUNC(NTDLL_RtlUshortByteSwap,
648 "movb %ch,%al\n\t"
649 "movb %cl,%ah\n\t"
650 "ret")
651 #endif
654 /*************************************************************************
655 * RtlUniform [NTDLL.@]
657 * Generates an uniform random number
659 * PARAMS
660 * seed [O] The seed of the Random function
662 * RETURNS
663 * It returns a random number uniformly distributed over [0..MAXLONG-1].
665 * NOTES
666 * Generates an uniform random number using D.H. Lehmer's 1948 algorithm.
667 * In our case the algorithm is:
669 *| result = (*seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
671 *| *seed = result;
673 * DIFFERENCES
674 * The native documentation states that the random number is
675 * uniformly distributed over [0..MAXLONG]. In reality the native
676 * function and our function return a random number uniformly
677 * distributed over [0..MAXLONG-1].
679 ULONG WINAPI RtlUniform (PULONG seed)
681 ULONG result;
684 * Instead of the algorithm stated above, we use the algorithm
685 * below, which is totally equivalent (see the tests), but does
686 * not use a division and therefore is faster.
688 result = *seed * 0xffffffed + 0x7fffffc3;
689 if (result == 0xffffffff || result == 0x7ffffffe) {
690 result = (result + 2) & MAXLONG;
691 } else if (result == 0x7fffffff) {
692 result = 0;
693 } else if ((result & 0x80000000) == 0) {
694 result = result + (~result & 1);
695 } else {
696 result = (result + (result & 1)) & MAXLONG;
697 } /* if */
698 *seed = result;
699 return result;
703 /*************************************************************************
704 * RtlRandom [NTDLL.@]
706 * Generates a random number
708 * PARAMS
709 * seed [O] The seed of the Random function
711 * RETURNS
712 * It returns a random number distributed over [0..MAXLONG-1].
714 ULONG WINAPI RtlRandom (PULONG seed)
716 static ULONG saved_value[128] =
717 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
718 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
719 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
720 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
721 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
722 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
723 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
724 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
725 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
726 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
727 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
728 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
729 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
730 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
731 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
732 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
733 ULONG rand;
734 int pos;
735 ULONG result;
737 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
738 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
739 pos = *seed & 0x7f;
740 result = saved_value[pos];
741 saved_value[pos] = rand;
742 return(result);
746 /*************************************************************************
747 * RtlRandomEx [NTDLL.@]
749 ULONG WINAPI RtlRandomEx( ULONG *seed )
751 WARN( "semi-stub: should use a different algorithm\n" );
752 return RtlRandom( seed );
755 /*************************************************************************
756 * RtlAreAllAccessesGranted [NTDLL.@]
758 * Check if all desired accesses are granted
760 * RETURNS
761 * TRUE: All desired accesses are granted
762 * FALSE: Otherwise
764 BOOLEAN WINAPI RtlAreAllAccessesGranted(
765 ACCESS_MASK GrantedAccess,
766 ACCESS_MASK DesiredAccess)
768 return (GrantedAccess & DesiredAccess) == DesiredAccess;
772 /*************************************************************************
773 * RtlAreAnyAccessesGranted [NTDLL.@]
775 * Check if at least one of the desired accesses is granted
777 * PARAMS
778 * GrantedAccess [I] Access mask of granted accesses
779 * DesiredAccess [I] Access mask of desired accesses
781 * RETURNS
782 * TRUE: At least one of the desired accesses is granted
783 * FALSE: Otherwise
785 BOOLEAN WINAPI RtlAreAnyAccessesGranted(
786 ACCESS_MASK GrantedAccess,
787 ACCESS_MASK DesiredAccess)
789 return (GrantedAccess & DesiredAccess) != 0;
793 /*************************************************************************
794 * RtlMapGenericMask [NTDLL.@]
796 * Determine the nongeneric access rights specified by an access mask
798 * RETURNS
799 * Nothing.
801 void WINAPI RtlMapGenericMask(
802 PACCESS_MASK AccessMask,
803 const GENERIC_MAPPING *GenericMapping)
805 if (*AccessMask & GENERIC_READ) {
806 *AccessMask |= GenericMapping->GenericRead;
807 } /* if */
809 if (*AccessMask & GENERIC_WRITE) {
810 *AccessMask |= GenericMapping->GenericWrite;
811 } /* if */
813 if (*AccessMask & GENERIC_EXECUTE) {
814 *AccessMask |= GenericMapping->GenericExecute;
815 } /* if */
817 if (*AccessMask & GENERIC_ALL) {
818 *AccessMask |= GenericMapping->GenericAll;
819 } /* if */
821 *AccessMask &= 0x0FFFFFFF;
825 /*************************************************************************
826 * RtlCopyLuid [NTDLL.@]
828 * Copy a local unique ID.
830 * PARAMS
831 * LuidDest [O] Destination for the copied Luid
832 * LuidSrc [I] Source Luid to copy to LuidDest
834 * RETURNS
835 * Nothing.
837 void WINAPI RtlCopyLuid (PLUID LuidDest, const LUID *LuidSrc)
839 *LuidDest = *LuidSrc;
843 /*************************************************************************
844 * RtlEqualLuid [NTDLL.@]
846 * Compare two local unique IDs.
848 * PARAMS
849 * Luid1 [I] First Luid to compare to Luid2
850 * Luid2 [I] Second Luid to compare to Luid1
852 * RETURNS
853 * TRUE: The two LUIDs are equal.
854 * FALSE: Otherwise
856 BOOLEAN WINAPI RtlEqualLuid (const LUID *Luid1, const LUID *Luid2)
858 return (Luid1->LowPart == Luid2->LowPart && Luid1->HighPart == Luid2->HighPart);
862 /*************************************************************************
863 * RtlCopyLuidAndAttributesArray [NTDLL.@]
865 * Copy an array of local unique IDs and attributes.
867 * PARAMS
868 * Count [I] Number of Luid/attributes in Src
869 * Src [I] Source Luid/attributes to copy
870 * Dest [O] Destination for copied Luid/attributes
872 * RETURNS
873 * Nothing.
875 * NOTES
876 * Dest must be large enough to hold Src.
878 void WINAPI RtlCopyLuidAndAttributesArray(
879 ULONG Count,
880 const LUID_AND_ATTRIBUTES *Src,
881 PLUID_AND_ATTRIBUTES Dest)
883 ULONG i;
885 for (i = 0; i < Count; i++) Dest[i] = Src[i];
888 static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
890 int base = 10, d;
891 WCHAR c;
892 ULONG cur_value, prev_value = 0;
893 BOOL success = FALSE;
895 if (**str == '.')
897 *str += 1;
898 return FALSE;
901 if ((*str)[0] == '0')
903 if ((*str)[1] == 'x' || (*str)[1] == 'X')
905 *str += 2;
906 if (strict) return FALSE;
907 base = 16;
909 else if ((*str)[1] >= '0' && (*str)[1] <= '9')
911 *str += 1;
912 if (strict) return FALSE;
913 base = 8;
917 for (cur_value = 0; **str; *str += 1)
919 c = **str;
920 if (c >= ARRAY_SIZE(hex_table)) break;
921 d = hex_table[c];
922 if (d == -1 || d >= base) break;
923 cur_value = cur_value * base + d;
924 success = TRUE;
925 if (cur_value < prev_value) return FALSE; /* overflow */
926 prev_value = cur_value;
929 if (success) *value = cur_value;
930 return success;
933 static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
934 const WCHAR **terminator, IN_ADDR *address, USHORT *port)
936 ULONG fields[4];
937 int n = 0;
939 for (;;)
941 if (!parse_ipv4_component(&str, strict, &fields[n]))
942 goto error;
943 n++;
944 if (*str != '.')
945 break;
946 if (n == 4)
947 goto error;
948 str++;
951 if (strict && n < 4)
952 goto error;
954 switch (n)
956 case 4:
957 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFF || fields[3] > 0xFF)
958 goto error;
959 address->S_un.S_un_b.s_b1 = fields[0];
960 address->S_un.S_un_b.s_b2 = fields[1];
961 address->S_un.S_un_b.s_b3 = fields[2];
962 address->S_un.S_un_b.s_b4 = fields[3];
963 break;
964 case 3:
965 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFFFF)
966 goto error;
967 address->S_un.S_un_b.s_b1 = fields[0];
968 address->S_un.S_un_b.s_b2 = fields[1];
969 address->S_un.S_un_b.s_b3 = (fields[2] & 0xFF00) >> 8;
970 address->S_un.S_un_b.s_b4 = (fields[2] & 0x00FF);
971 break;
972 case 2:
973 if (fields[0] > 0xFF || fields[1] > 0xFFFFFF)
974 goto error;
975 address->S_un.S_un_b.s_b1 = fields[0];
976 address->S_un.S_un_b.s_b2 = (fields[1] & 0xFF0000) >> 16;
977 address->S_un.S_un_b.s_b3 = (fields[1] & 0x00FF00) >> 8;
978 address->S_un.S_un_b.s_b4 = (fields[1] & 0x0000FF);
979 break;
980 case 1:
981 address->S_un.S_un_b.s_b1 = (fields[0] & 0xFF000000) >> 24;
982 address->S_un.S_un_b.s_b2 = (fields[0] & 0x00FF0000) >> 16;
983 address->S_un.S_un_b.s_b3 = (fields[0] & 0x0000FF00) >> 8;
984 address->S_un.S_un_b.s_b4 = (fields[0] & 0x000000FF);
985 break;
986 default:
987 goto error;
990 if (terminator) *terminator = str;
992 if (*str == ':')
994 str++;
995 if (!parse_ipv4_component(&str, FALSE, &fields[0]))
996 goto error;
997 if (!fields[0] || fields[0] > 0xFFFF || *str)
998 goto error;
999 if (port)
1001 *port = htons(fields[0]);
1002 if (terminator) *terminator = str;
1006 if (!terminator && *str)
1007 return STATUS_INVALID_PARAMETER;
1008 return STATUS_SUCCESS;
1010 error:
1011 if (terminator) *terminator = str;
1012 return STATUS_INVALID_PARAMETER;
1015 /***********************************************************************
1016 * RtlIpv4StringToAddressExW [NTDLL.@]
1018 NTSTATUS WINAPI RtlIpv4StringToAddressExW(const WCHAR *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1020 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, address, port);
1021 if (!str || !address || !port) return STATUS_INVALID_PARAMETER;
1022 return ipv4_string_to_address(str, strict, NULL, address, port);
1025 /***********************************************************************
1026 * RtlIpv4StringToAddressW [NTDLL.@]
1028 NTSTATUS WINAPI RtlIpv4StringToAddressW(const WCHAR *str, BOOLEAN strict, const WCHAR **terminator, IN_ADDR *address)
1030 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, terminator, address);
1031 return ipv4_string_to_address(str, strict, terminator, address, NULL);
1034 /***********************************************************************
1035 * RtlIpv4StringToAddressExA [NTDLL.@]
1037 NTSTATUS WINAPI RtlIpv4StringToAddressExA(const char *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
1039 WCHAR wstr[32];
1041 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, address, port);
1043 if (!str || !address || !port)
1044 return STATUS_INVALID_PARAMETER;
1046 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1047 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1048 return ipv4_string_to_address(wstr, strict, NULL, address, port);
1051 /***********************************************************************
1052 * RtlIpv4StringToAddressA [NTDLL.@]
1054 NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const char **terminator, IN_ADDR *address)
1056 WCHAR wstr[32];
1057 const WCHAR *wterminator;
1058 NTSTATUS ret;
1060 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, terminator, address);
1062 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1063 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1064 ret = ipv4_string_to_address(wstr, strict, &wterminator, address, NULL);
1065 if (terminator) *terminator = str + (wterminator - wstr);
1066 return ret;
1069 static BOOL parse_ipv6_component(const WCHAR **str, int base, ULONG *value)
1071 WCHAR *terminator;
1072 if (**str >= ARRAY_SIZE(hex_table) || hex_table[**str] == -1) return FALSE;
1073 *value = min(wcstoul(*str, &terminator, base), 0x7FFFFFFF);
1074 if (*terminator == '0') terminator++; /* "0x" but nothing valid after */
1075 else if (terminator == *str) return FALSE;
1076 *str = terminator;
1077 return TRUE;
1080 static NTSTATUS ipv6_string_to_address(const WCHAR *str, BOOL ex,
1081 const WCHAR **terminator, IN6_ADDR *address, ULONG *scope, USHORT *port)
1083 BOOL expecting_port = FALSE, has_0x = FALSE, too_big = FALSE;
1084 int n_bytes = 0, n_ipv4_bytes = 0, gap = -1;
1085 ULONG ip_component, scope_component = 0, port_component = 0;
1086 const WCHAR *prev_str;
1088 if (str[0] == '[')
1090 if (!ex) goto error;
1091 expecting_port = TRUE;
1092 str++;
1095 if (str[0] == ':')
1097 if (str[1] != ':') goto error;
1098 str++;
1099 /* Windows bug: a double colon at the beginning is treated as 4 bytes of zeros instead of 2 */
1100 address->u.Word[0] = 0;
1101 n_bytes = 2;
1104 for (;;)
1106 if (!n_ipv4_bytes && *str == ':')
1108 /* double colon */
1109 if (gap != -1) goto error;
1110 str++;
1111 prev_str = str;
1112 gap = n_bytes;
1113 if (n_bytes == 14 || !parse_ipv6_component(&str, 16, &ip_component)) break;
1114 str = prev_str;
1116 else
1118 prev_str = str;
1121 if (!n_ipv4_bytes && n_bytes <= (gap != -1 ? 10 : 12))
1123 if (parse_ipv6_component(&str, 10, &ip_component) && *str == '.')
1124 n_ipv4_bytes = 1;
1125 str = prev_str;
1128 if (n_ipv4_bytes)
1130 /* IPv4 component */
1131 if (!parse_ipv6_component(&str, 10, &ip_component)) goto error;
1132 if (str - prev_str > 3 || ip_component > 255)
1134 too_big = TRUE;
1136 else
1138 if (*str != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
1139 address->u.Byte[n_bytes] = ip_component;
1140 n_bytes++;
1142 if (n_ipv4_bytes == 4 || *str != '.') break;
1143 n_ipv4_bytes++;
1145 else
1147 /* IPv6 component */
1148 if (!parse_ipv6_component(&str, 16, &ip_component)) goto error;
1149 if (prev_str[0] == '0' && (prev_str[1] == 'x' || prev_str[1] == 'X'))
1151 /* Windows "feature": the last IPv6 component can start with "0x" and be longer than 4 digits */
1152 if (terminator) *terminator = prev_str + 1; /* Windows says that the "x" is the terminator */
1153 if (n_bytes < 14 && gap == -1) return STATUS_INVALID_PARAMETER;
1154 address->u.Word[n_bytes/2] = htons(ip_component);
1155 n_bytes += 2;
1156 has_0x = TRUE;
1157 goto fill_gap;
1159 if (*str != ':' && n_bytes < 14 && gap == -1) goto error;
1160 if (str - prev_str > 4)
1161 too_big = TRUE;
1162 else
1163 address->u.Word[n_bytes/2] = htons(ip_component);
1164 n_bytes += 2;
1165 if (*str != ':' || (gap != -1 && str[1] == ':')) break;
1167 if (n_bytes == (gap != -1 ? 14 : 16)) break;
1168 if (too_big) return STATUS_INVALID_PARAMETER;
1169 str++;
1172 if (terminator) *terminator = str;
1173 if (too_big) return STATUS_INVALID_PARAMETER;
1175 fill_gap:
1176 if (gap == -1)
1178 if (n_bytes < 16) goto error;
1180 else
1182 memmove(address->u.Byte + 16 - (n_bytes - gap), address->u.Byte + gap, n_bytes - gap);
1183 memset(address->u.Byte + gap, 0, 16 - n_bytes);
1186 if (ex)
1188 if (has_0x) goto error;
1190 if (*str == '%')
1192 str++;
1193 if (!parse_ipv4_component(&str, TRUE, &scope_component)) goto error;
1196 if (expecting_port)
1198 if (*str != ']') goto error;
1199 str++;
1200 if (*str == ':')
1202 str++;
1203 if (!parse_ipv4_component(&str, FALSE, &port_component)) goto error;
1204 if (!port_component || port_component > 0xFFFF || *str) goto error;
1205 port_component = htons(port_component);
1210 if (!terminator && *str) return STATUS_INVALID_PARAMETER;
1212 if (scope) *scope = scope_component;
1213 if (port) *port = port_component;
1215 return STATUS_SUCCESS;
1217 error:
1218 if (terminator) *terminator = str;
1219 return STATUS_INVALID_PARAMETER;
1222 /***********************************************************************
1223 * RtlIpv6StringToAddressExW [NTDLL.@]
1225 NTSTATUS NTAPI RtlIpv6StringToAddressExW(const WCHAR *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1227 TRACE("(%s, %p, %p, %p)\n", debugstr_w(str), address, scope, port);
1228 if (!str || !address || !scope || !port) return STATUS_INVALID_PARAMETER;
1229 return ipv6_string_to_address(str, TRUE, NULL, address, scope, port);
1232 /***********************************************************************
1233 * RtlIpv6StringToAddressW [NTDLL.@]
1235 NTSTATUS WINAPI RtlIpv6StringToAddressW(const WCHAR *str, const WCHAR **terminator, IN6_ADDR *address)
1237 TRACE("(%s, %p, %p)\n", debugstr_w(str), terminator, address);
1238 return ipv6_string_to_address(str, FALSE, terminator, address, NULL, NULL);
1241 /***********************************************************************
1242 * RtlIpv6StringToAddressExA [NTDLL.@]
1244 NTSTATUS WINAPI RtlIpv6StringToAddressExA(const char *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
1246 WCHAR wstr[128];
1248 TRACE("(%s, %p, %p, %p)\n", debugstr_a(str), address, scope, port);
1250 if (!str || !address || !scope || !port)
1251 return STATUS_INVALID_PARAMETER;
1253 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1254 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1255 return ipv6_string_to_address(wstr, TRUE, NULL, address, scope, port);
1258 /***********************************************************************
1259 * RtlIpv6StringToAddressA [NTDLL.@]
1261 NTSTATUS WINAPI RtlIpv6StringToAddressA(const char *str, const char **terminator, IN6_ADDR *address)
1263 WCHAR wstr[128];
1264 const WCHAR *wterminator = NULL;
1265 NTSTATUS ret;
1267 TRACE("(%s, %p, %p)\n", debugstr_a(str), terminator, address);
1269 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
1270 wstr[ARRAY_SIZE(wstr) - 1] = 0;
1271 ret = ipv6_string_to_address(wstr, FALSE, &wterminator, address, NULL, NULL);
1272 if (terminator && wterminator) *terminator = str + (wterminator - wstr);
1273 return ret;
1276 /***********************************************************************
1277 * RtlIpv4AddressToStringExW [NTDLL.@]
1279 * Convert the given ipv4 address and optional the port to a string
1281 * PARAMS
1282 * pin [I] PTR to the ip address to convert (network byte order)
1283 * port [I] optional port to convert (network byte order)
1284 * buffer [O] destination buffer for the result
1285 * psize [IO] PTR to available/used size of the destination buffer
1287 * RETURNS
1288 * Success: STATUS_SUCCESS
1289 * Failure: STATUS_INVALID_PARAMETER
1292 NTSTATUS WINAPI RtlIpv4AddressToStringExW(const IN_ADDR *pin, USHORT port, LPWSTR buffer, PULONG psize)
1294 WCHAR tmp_ip[32];
1295 static const WCHAR fmt_ip[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
1296 static const WCHAR fmt_port[] = {':','%','u',0};
1297 ULONG needed;
1299 if (!pin || !buffer || !psize)
1300 return STATUS_INVALID_PARAMETER;
1302 TRACE("(%p:0x%x, %d, %p, %p:%d)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1304 needed = swprintf(tmp_ip, ARRAY_SIZE(tmp_ip), fmt_ip,
1305 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1306 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1308 if (port) needed += swprintf(tmp_ip + needed, ARRAY_SIZE(tmp_ip) - needed, fmt_port, ntohs(port));
1310 if (*psize > needed) {
1311 *psize = needed + 1;
1312 wcscpy(buffer, tmp_ip);
1313 return STATUS_SUCCESS;
1316 *psize = needed + 1;
1317 return STATUS_INVALID_PARAMETER;
1320 /***********************************************************************
1321 * RtlIpv4AddressToStringExA [NTDLL.@]
1323 * Convert the given ipv4 address and optional the port to a string
1325 * See RtlIpv4AddressToStringExW
1327 NTSTATUS WINAPI RtlIpv4AddressToStringExA(const IN_ADDR *pin, USHORT port, LPSTR buffer, PULONG psize)
1329 CHAR tmp_ip[32];
1330 ULONG needed;
1332 if (!pin || !buffer || !psize)
1333 return STATUS_INVALID_PARAMETER;
1335 TRACE("(%p:0x%x, %d, %p, %p:%d)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
1337 needed = sprintf(tmp_ip, "%u.%u.%u.%u",
1338 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
1339 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
1341 if (port) needed += sprintf(tmp_ip + needed, ":%u", ntohs(port));
1343 if (*psize > needed) {
1344 *psize = needed + 1;
1345 strcpy(buffer, tmp_ip);
1346 return STATUS_SUCCESS;
1349 *psize = needed + 1;
1350 return STATUS_INVALID_PARAMETER;
1353 /***********************************************************************
1354 * RtlIpv4AddressToStringW [NTDLL.@]
1356 * Convert the given ipv4 address to a string
1358 * PARAMS
1359 * pin [I] PTR to the ip address to convert (network byte order)
1360 * buffer [O] destination buffer for the result (at least 16 character)
1362 * RETURNS
1363 * PTR to the 0 character at the end of the converted string
1366 WCHAR * WINAPI RtlIpv4AddressToStringW(const IN_ADDR *pin, LPWSTR buffer)
1368 ULONG size = 16;
1370 if (RtlIpv4AddressToStringExW(pin, 0, buffer, &size)) size = 0;
1371 return buffer + size - 1;
1374 /***********************************************************************
1375 * RtlIpv4AddressToStringA [NTDLL.@]
1377 * Convert the given ipv4 address to a string
1379 * See RtlIpv4AddressToStringW
1381 CHAR * WINAPI RtlIpv4AddressToStringA(const IN_ADDR *pin, LPSTR buffer)
1383 ULONG size = 16;
1385 if (RtlIpv4AddressToStringExA(pin, 0, buffer, &size)) size = 0;
1386 return buffer + size - 1;
1389 static BOOL is_ipv4_in_ipv6(const IN6_ADDR *address)
1391 if (address->s6_words[5] == htons(0x5efe) && (address->s6_words[4] & ~htons(0x200)) == 0)
1392 return TRUE;
1393 if (*(UINT64 *)address != 0)
1394 return FALSE;
1395 if (address->s6_words[4] != 0 && address->s6_words[4] != 0xffff)
1396 return FALSE;
1397 if (address->s6_words[4] == 0 && address->s6_words[5] != 0 && address->s6_words[5] != 0xffff)
1398 return FALSE;
1399 if (address->s6_words[4] == 0xffff && address->s6_words[5] != 0)
1400 return FALSE;
1401 if (address->s6_words[6] == 0)
1402 return FALSE;
1403 return TRUE;
1406 /***********************************************************************
1407 * RtlIpv6AddressToStringExA [NTDLL.@]
1409 NTSTATUS WINAPI RtlIpv6AddressToStringExA(const IN6_ADDR *address, ULONG scope, USHORT port, char *str, ULONG *size)
1411 char buffer[64], *p = buffer;
1412 int i, len, gap = -1, gap_len = 1, ipv6_end = 8;
1413 ULONG needed;
1414 NTSTATUS ret;
1416 TRACE("(%p %u %u %p %p)\n", address, scope, port, str, size);
1418 if (!address || !str || !size)
1419 return STATUS_INVALID_PARAMETER;
1421 if (is_ipv4_in_ipv6(address))
1422 ipv6_end = 6;
1424 for (i = 0; i < ipv6_end; i++)
1426 len = 0;
1427 while (!address->s6_words[i] && i < ipv6_end)
1429 i++;
1430 len++;
1432 if (len > gap_len)
1434 gap = i - len;
1435 gap_len = len;
1439 if (port) p += sprintf(p, "[");
1441 i = 0;
1442 while (i < ipv6_end)
1444 if (i == gap)
1446 p += sprintf(p, ":");
1447 i += gap_len;
1448 if (i == ipv6_end) p += sprintf(p, ":");
1449 continue;
1451 if (i > 0) p += sprintf(p, ":");
1452 p += sprintf(p, "%x", ntohs(address->s6_words[i]));
1453 i++;
1456 if (ipv6_end == 6)
1458 if (p[-1] != ':') p += sprintf(p, ":");
1459 p = RtlIpv4AddressToStringA((IN_ADDR *)(address->s6_words + 6), p);
1462 if (scope) p += sprintf(p, "%%%u", scope);
1464 if (port) p += sprintf(p, "]:%u", ntohs(port));
1466 needed = p - buffer + 1;
1468 if (*size >= needed)
1470 strcpy(str, buffer);
1471 ret = STATUS_SUCCESS;
1473 else
1475 ret = STATUS_INVALID_PARAMETER;
1478 *size = needed;
1479 return ret;
1482 /***********************************************************************
1483 * RtlIpv6AddressToStringA [NTDLL.@]
1485 char * WINAPI RtlIpv6AddressToStringA(const IN6_ADDR *address, char *str)
1487 ULONG size = 46;
1488 if (!address || !str) return str - 1;
1489 str[45] = 0; /* this byte is set even though the string is always shorter */
1490 RtlIpv6AddressToStringExA(address, 0, 0, str, &size);
1491 return str + size - 1;
1494 /***********************************************************************
1495 * RtlIpv6AddressToStringExW [NTDLL.@]
1497 NTSTATUS WINAPI RtlIpv6AddressToStringExW(const IN6_ADDR *address, ULONG scope, USHORT port, WCHAR *str, ULONG *size)
1499 char cstr[64];
1500 NTSTATUS ret = RtlIpv6AddressToStringExA(address, scope, port, cstr, size);
1501 if (ret == STATUS_SUCCESS) RtlMultiByteToUnicodeN(str, *size * sizeof(WCHAR), NULL, cstr, *size);
1502 return ret;
1505 /***********************************************************************
1506 * RtlIpv6AddressToStringW [NTDLL.@]
1508 WCHAR * WINAPI RtlIpv6AddressToStringW(const IN6_ADDR *address, WCHAR *str)
1510 ULONG size = 46;
1511 if (!address || !str) return str;
1512 str[45] = 0; /* this word is set even though the string is always shorter */
1513 if (RtlIpv6AddressToStringExW(address, 0, 0, str, &size) != STATUS_SUCCESS)
1514 return str;
1515 return str + size - 1;
1518 /***********************************************************************
1519 * get_pointer_obfuscator (internal)
1521 static DWORD_PTR get_pointer_obfuscator( void )
1523 static DWORD_PTR pointer_obfuscator;
1525 if (!pointer_obfuscator)
1527 ULONG seed = NtGetTickCount();
1528 ULONG_PTR rand;
1530 /* generate a random value for the obfuscator */
1531 rand = RtlUniform( &seed );
1533 /* handle 64bit pointers */
1534 rand ^= (ULONG_PTR)RtlUniform( &seed ) << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1536 /* set the high bits so dereferencing obfuscated pointers will (usually) crash */
1537 rand |= (ULONG_PTR)0xc0000000 << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1539 InterlockedCompareExchangePointer( (void**) &pointer_obfuscator, (void*) rand, NULL );
1542 return pointer_obfuscator;
1545 /*************************************************************************
1546 * RtlEncodePointer [NTDLL.@]
1548 PVOID WINAPI RtlEncodePointer( PVOID ptr )
1550 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1551 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1554 PVOID WINAPI RtlDecodePointer( PVOID ptr )
1556 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1557 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1560 /*************************************************************************
1561 * RtlInitializeSListHead [NTDLL.@]
1563 VOID WINAPI RtlInitializeSListHead(PSLIST_HEADER list)
1565 #ifdef _WIN64
1566 list->s.Alignment = list->s.Region = 0;
1567 list->Header16.HeaderType = 1; /* we use the 16-byte header */
1568 #else
1569 list->Alignment = 0;
1570 #endif
1573 /*************************************************************************
1574 * RtlQueryDepthSList [NTDLL.@]
1576 WORD WINAPI RtlQueryDepthSList(PSLIST_HEADER list)
1578 #ifdef _WIN64
1579 return list->Header16.Depth;
1580 #else
1581 return list->s.Depth;
1582 #endif
1585 /*************************************************************************
1586 * RtlFirstEntrySList [NTDLL.@]
1588 PSLIST_ENTRY WINAPI RtlFirstEntrySList(const SLIST_HEADER* list)
1590 #ifdef _WIN64
1591 return (SLIST_ENTRY *)((ULONG_PTR)list->Header16.NextEntry << 4);
1592 #else
1593 return list->s.Next.Next;
1594 #endif
1597 /*************************************************************************
1598 * RtlInterlockedFlushSList [NTDLL.@]
1600 PSLIST_ENTRY WINAPI RtlInterlockedFlushSList(PSLIST_HEADER list)
1602 SLIST_HEADER old, new;
1604 #ifdef _WIN64
1605 if (!list->Header16.NextEntry) return NULL;
1606 new.s.Alignment = new.s.Region = 0;
1607 new.Header16.HeaderType = 1; /* we use the 16-byte header */
1610 old = *list;
1611 new.Header16.Sequence = old.Header16.Sequence + 1;
1612 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1613 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1614 #else
1615 if (!list->s.Next.Next) return NULL;
1616 new.Alignment = 0;
1619 old = *list;
1620 new.s.Sequence = old.s.Sequence + 1;
1621 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1622 old.Alignment) != old.Alignment);
1623 return old.s.Next.Next;
1624 #endif
1627 /*************************************************************************
1628 * RtlInterlockedPushEntrySList [NTDLL.@]
1630 PSLIST_ENTRY WINAPI RtlInterlockedPushEntrySList(PSLIST_HEADER list, PSLIST_ENTRY entry)
1632 SLIST_HEADER old, new;
1634 #ifdef _WIN64
1635 new.Header16.NextEntry = (ULONG_PTR)entry >> 4;
1638 old = *list;
1639 entry->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1640 new.Header16.Depth = old.Header16.Depth + 1;
1641 new.Header16.Sequence = old.Header16.Sequence + 1;
1642 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1643 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1644 #else
1645 new.s.Next.Next = entry;
1648 old = *list;
1649 entry->Next = old.s.Next.Next;
1650 new.s.Depth = old.s.Depth + 1;
1651 new.s.Sequence = old.s.Sequence + 1;
1652 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1653 old.Alignment) != old.Alignment);
1654 return old.s.Next.Next;
1655 #endif
1658 /*************************************************************************
1659 * RtlInterlockedPopEntrySList [NTDLL.@]
1661 PSLIST_ENTRY WINAPI RtlInterlockedPopEntrySList(PSLIST_HEADER list)
1663 SLIST_HEADER old, new;
1664 PSLIST_ENTRY entry;
1666 #ifdef _WIN64
1669 old = *list;
1670 if (!(entry = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4))) return NULL;
1671 /* entry could be deleted by another thread */
1672 __TRY
1674 new.Header16.NextEntry = (ULONG_PTR)entry->Next >> 4;
1675 new.Header16.Depth = old.Header16.Depth - 1;
1676 new.Header16.Sequence = old.Header16.Sequence + 1;
1678 __EXCEPT_PAGE_FAULT
1681 __ENDTRY
1682 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1683 #else
1686 old = *list;
1687 if (!(entry = old.s.Next.Next)) return NULL;
1688 /* entry could be deleted by another thread */
1689 __TRY
1691 new.s.Next.Next = entry->Next;
1692 new.s.Depth = old.s.Depth - 1;
1693 new.s.Sequence = old.s.Sequence + 1;
1695 __EXCEPT_PAGE_FAULT
1698 __ENDTRY
1699 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1700 old.Alignment) != old.Alignment);
1701 #endif
1702 return entry;
1705 /*************************************************************************
1706 * RtlInterlockedPushListSListEx [NTDLL.@]
1708 PSLIST_ENTRY WINAPI RtlInterlockedPushListSListEx(PSLIST_HEADER list, PSLIST_ENTRY first,
1709 PSLIST_ENTRY last, ULONG count)
1711 SLIST_HEADER old, new;
1713 #ifdef _WIN64
1714 new.Header16.NextEntry = (ULONG_PTR)first >> 4;
1717 old = *list;
1718 new.Header16.Depth = old.Header16.Depth + count;
1719 new.Header16.Sequence = old.Header16.Sequence + 1;
1720 last->Next = (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1721 } while (!InterlockedCompareExchange128((__int64 *)list, new.s.Region, new.s.Alignment, (__int64 *)&old));
1722 return (SLIST_ENTRY *)((ULONG_PTR)old.Header16.NextEntry << 4);
1723 #else
1724 new.s.Next.Next = first;
1727 old = *list;
1728 new.s.Depth = old.s.Depth + count;
1729 new.s.Sequence = old.s.Sequence + 1;
1730 last->Next = old.s.Next.Next;
1731 } while (InterlockedCompareExchange64((__int64 *)&list->Alignment, new.Alignment,
1732 old.Alignment) != old.Alignment);
1733 return old.s.Next.Next;
1734 #endif
1737 /*************************************************************************
1738 * RtlInterlockedPushListSList [NTDLL.@]
1740 DEFINE_FASTCALL_WRAPPER(RtlInterlockedPushListSList, 16)
1741 PSLIST_ENTRY FASTCALL RtlInterlockedPushListSList(PSLIST_HEADER list, PSLIST_ENTRY first,
1742 PSLIST_ENTRY last, ULONG count)
1744 return RtlInterlockedPushListSListEx(list, first, last, count);
1747 /******************************************************************************
1748 * RtlGetCompressionWorkSpaceSize [NTDLL.@]
1750 NTSTATUS WINAPI RtlGetCompressionWorkSpaceSize(USHORT format, PULONG compress_workspace,
1751 PULONG decompress_workspace)
1753 FIXME("0x%04x, %p, %p: semi-stub\n", format, compress_workspace, decompress_workspace);
1755 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1757 case COMPRESSION_FORMAT_LZNT1:
1758 if (compress_workspace)
1760 /* FIXME: The current implementation of RtlCompressBuffer does not use a
1761 * workspace buffer, but Windows applications might expect a nonzero value. */
1762 *compress_workspace = 16;
1764 if (decompress_workspace)
1765 *decompress_workspace = 0x1000;
1766 return STATUS_SUCCESS;
1768 case COMPRESSION_FORMAT_NONE:
1769 case COMPRESSION_FORMAT_DEFAULT:
1770 return STATUS_INVALID_PARAMETER;
1772 default:
1773 FIXME("format %u not implemented\n", format);
1774 return STATUS_UNSUPPORTED_COMPRESSION;
1778 /* compress data using LZNT1, currently only a stub */
1779 static NTSTATUS lznt1_compress(UCHAR *src, ULONG src_size, UCHAR *dst, ULONG dst_size,
1780 ULONG chunk_size, ULONG *final_size, UCHAR *workspace)
1782 UCHAR *src_cur = src, *src_end = src + src_size;
1783 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1784 ULONG block_size;
1786 while (src_cur < src_end)
1788 /* determine size of current chunk */
1789 block_size = min(0x1000, src_end - src_cur);
1790 if (dst_cur + sizeof(WORD) + block_size > dst_end)
1791 return STATUS_BUFFER_TOO_SMALL;
1793 /* write (uncompressed) chunk header */
1794 *(WORD *)dst_cur = 0x3000 | (block_size - 1);
1795 dst_cur += sizeof(WORD);
1797 /* write chunk content */
1798 memcpy(dst_cur, src_cur, block_size);
1799 dst_cur += block_size;
1800 src_cur += block_size;
1803 if (final_size)
1804 *final_size = dst_cur - dst;
1806 return STATUS_SUCCESS;
1809 /******************************************************************************
1810 * RtlCompressBuffer [NTDLL.@]
1812 NTSTATUS WINAPI RtlCompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
1813 PUCHAR compressed, ULONG compressed_size, ULONG chunk_size,
1814 PULONG final_size, PVOID workspace)
1816 FIXME("0x%04x, %p, %u, %p, %u, %u, %p, %p: semi-stub\n", format, uncompressed,
1817 uncompressed_size, compressed, compressed_size, chunk_size, final_size, workspace);
1819 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
1821 case COMPRESSION_FORMAT_LZNT1:
1822 return lznt1_compress(uncompressed, uncompressed_size, compressed,
1823 compressed_size, chunk_size, final_size, workspace);
1825 case COMPRESSION_FORMAT_NONE:
1826 case COMPRESSION_FORMAT_DEFAULT:
1827 return STATUS_INVALID_PARAMETER;
1829 default:
1830 FIXME("format %u not implemented\n", format);
1831 return STATUS_UNSUPPORTED_COMPRESSION;
1835 /* decompress a single LZNT1 chunk */
1836 static UCHAR *lznt1_decompress_chunk(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size)
1838 UCHAR *src_cur = src, *src_end = src + src_size;
1839 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1840 ULONG displacement_bits, length_bits;
1841 ULONG code_displacement, code_length;
1842 WORD flags, code;
1844 while (src_cur < src_end && dst_cur < dst_end)
1846 flags = 0x8000 | *src_cur++;
1847 while ((flags & 0xff00) && src_cur < src_end)
1849 if (flags & 1)
1851 /* backwards reference */
1852 if (src_cur + sizeof(WORD) > src_end)
1853 return NULL;
1855 code = *(WORD *)src_cur;
1856 src_cur += sizeof(WORD);
1858 /* find length / displacement bits */
1859 for (displacement_bits = 12; displacement_bits > 4; displacement_bits--)
1860 if ((1 << (displacement_bits - 1)) < dst_cur - dst) break;
1862 length_bits = 16 - displacement_bits;
1863 code_length = (code & ((1 << length_bits) - 1)) + 3;
1864 code_displacement = (code >> length_bits) + 1;
1866 if (dst_cur < dst + code_displacement)
1867 return NULL;
1869 /* copy bytes of chunk - we can't use memcpy() since source and dest can
1870 * be overlapping, and the same bytes can be repeated over and over again */
1871 while (code_length--)
1873 if (dst_cur >= dst_end) return dst_cur;
1874 *dst_cur = *(dst_cur - code_displacement);
1875 dst_cur++;
1878 else
1880 /* uncompressed data */
1881 if (dst_cur >= dst_end) return dst_cur;
1882 *dst_cur++ = *src_cur++;
1884 flags >>= 1;
1888 return dst_cur;
1891 /* decompress data encoded with LZNT1 */
1892 static NTSTATUS lznt1_decompress(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size,
1893 ULONG offset, ULONG *final_size, UCHAR *workspace)
1895 UCHAR *src_cur = src, *src_end = src + src_size;
1896 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1897 ULONG chunk_size, block_size;
1898 WORD chunk_header;
1899 UCHAR *ptr;
1901 if (src_cur + sizeof(WORD) > src_end)
1902 return STATUS_BAD_COMPRESSION_BUFFER;
1904 /* skip over chunks with a distance >= 0x1000 to the destination offset */
1905 while (offset >= 0x1000 && src_cur + sizeof(WORD) <= src_end)
1907 chunk_header = *(WORD *)src_cur;
1908 src_cur += sizeof(WORD);
1909 if (!chunk_header) goto out;
1910 chunk_size = (chunk_header & 0xfff) + 1;
1912 if (src_cur + chunk_size > src_end)
1913 return STATUS_BAD_COMPRESSION_BUFFER;
1915 src_cur += chunk_size;
1916 offset -= 0x1000;
1919 /* handle partially included chunk */
1920 if (offset && src_cur + sizeof(WORD) <= src_end)
1922 chunk_header = *(WORD *)src_cur;
1923 src_cur += sizeof(WORD);
1924 if (!chunk_header) goto out;
1925 chunk_size = (chunk_header & 0xfff) + 1;
1927 if (src_cur + chunk_size > src_end)
1928 return STATUS_BAD_COMPRESSION_BUFFER;
1930 if (dst_cur >= dst_end)
1931 goto out;
1933 if (chunk_header & 0x8000)
1935 /* compressed chunk */
1936 if (!workspace) return STATUS_ACCESS_VIOLATION;
1937 ptr = lznt1_decompress_chunk(workspace, 0x1000, src_cur, chunk_size);
1938 if (!ptr) return STATUS_BAD_COMPRESSION_BUFFER;
1939 if (ptr - workspace > offset)
1941 block_size = min((ptr - workspace) - offset, dst_end - dst_cur);
1942 memcpy(dst_cur, workspace + offset, block_size);
1943 dst_cur += block_size;
1946 else
1948 /* uncompressed chunk */
1949 if (chunk_size > offset)
1951 block_size = min(chunk_size - offset, dst_end - dst_cur);
1952 memcpy(dst_cur, src_cur + offset, block_size);
1953 dst_cur += block_size;
1957 src_cur += chunk_size;
1960 /* handle remaining chunks */
1961 while (src_cur + sizeof(WORD) <= src_end)
1963 chunk_header = *(WORD *)src_cur;
1964 src_cur += sizeof(WORD);
1965 if (!chunk_header) goto out;
1966 chunk_size = (chunk_header & 0xfff) + 1;
1968 if (src_cur + chunk_size > src_end)
1969 return STATUS_BAD_COMPRESSION_BUFFER;
1971 /* fill space with padding when the previous chunk was decompressed
1972 * to less than 4096 bytes. no padding is needed for the last chunk
1973 * or when the next chunk is truncated */
1974 block_size = ((dst_cur - dst) + offset) & 0xfff;
1975 if (block_size)
1977 block_size = 0x1000 - block_size;
1978 if (dst_cur + block_size >= dst_end)
1979 goto out;
1980 memset(dst_cur, 0, block_size);
1981 dst_cur += block_size;
1984 if (dst_cur >= dst_end)
1985 goto out;
1987 if (chunk_header & 0x8000)
1989 /* compressed chunk */
1990 dst_cur = lznt1_decompress_chunk(dst_cur, dst_end - dst_cur, src_cur, chunk_size);
1991 if (!dst_cur) return STATUS_BAD_COMPRESSION_BUFFER;
1993 else
1995 /* uncompressed chunk */
1996 block_size = min(chunk_size, dst_end - dst_cur);
1997 memcpy(dst_cur, src_cur, block_size);
1998 dst_cur += block_size;
2001 src_cur += chunk_size;
2004 out:
2005 if (final_size)
2006 *final_size = dst_cur - dst;
2008 return STATUS_SUCCESS;
2012 /******************************************************************************
2013 * RtlDecompressFragment [NTDLL.@]
2015 NTSTATUS WINAPI RtlDecompressFragment(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2016 PUCHAR compressed, ULONG compressed_size, ULONG offset,
2017 PULONG final_size, PVOID workspace)
2019 TRACE("0x%04x, %p, %u, %p, %u, %u, %p, %p\n", format, uncompressed,
2020 uncompressed_size, compressed, compressed_size, offset, final_size, workspace);
2022 switch (format & ~COMPRESSION_ENGINE_MAXIMUM)
2024 case COMPRESSION_FORMAT_LZNT1:
2025 return lznt1_decompress(uncompressed, uncompressed_size, compressed,
2026 compressed_size, offset, final_size, workspace);
2028 case COMPRESSION_FORMAT_NONE:
2029 case COMPRESSION_FORMAT_DEFAULT:
2030 return STATUS_INVALID_PARAMETER;
2032 default:
2033 FIXME("format %u not implemented\n", format);
2034 return STATUS_UNSUPPORTED_COMPRESSION;
2039 /******************************************************************************
2040 * RtlDecompressBuffer [NTDLL.@]
2042 NTSTATUS WINAPI RtlDecompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
2043 PUCHAR compressed, ULONG compressed_size, PULONG final_size)
2045 TRACE("0x%04x, %p, %u, %p, %u, %p\n", format, uncompressed,
2046 uncompressed_size, compressed, compressed_size, final_size);
2048 return RtlDecompressFragment(format, uncompressed, uncompressed_size,
2049 compressed, compressed_size, 0, final_size, NULL);
2052 /***********************************************************************
2053 * RtlSetThreadErrorMode [NTDLL.@]
2055 * Set the thread local error mode.
2057 * PARAMS
2058 * mode [I] The new error mode
2059 * oldmode [O] Destination of the old error mode (may be NULL)
2061 * RETURNS
2062 * Success: STATUS_SUCCESS
2063 * Failure: STATUS_INVALID_PARAMETER_1
2065 NTSTATUS WINAPI RtlSetThreadErrorMode( DWORD mode, LPDWORD oldmode )
2067 if (mode & ~0x70)
2068 return STATUS_INVALID_PARAMETER_1;
2070 if (oldmode)
2071 *oldmode = NtCurrentTeb()->HardErrorDisabled;
2073 NtCurrentTeb()->HardErrorDisabled = mode;
2074 return STATUS_SUCCESS;
2077 /***********************************************************************
2078 * RtlGetThreadErrorMode [NTDLL.@]
2080 * Get the thread local error mode.
2082 * PARAMS
2083 * None.
2085 * RETURNS
2086 * The current thread local error mode.
2088 DWORD WINAPI RtlGetThreadErrorMode( void )
2090 return NtCurrentTeb()->HardErrorDisabled;
2093 /******************************************************************************
2094 * RtlGetCurrentTransaction [NTDLL.@]
2096 HANDLE WINAPI RtlGetCurrentTransaction(void)
2098 FIXME("() :stub\n");
2099 return NULL;
2102 /******************************************************************************
2103 * RtlSetCurrentTransaction [NTDLL.@]
2105 BOOL WINAPI RtlSetCurrentTransaction(HANDLE new_transaction)
2107 FIXME("(%p) :stub\n", new_transaction);
2108 return FALSE;
2111 /**********************************************************************
2112 * RtlGetCurrentProcessorNumberEx [NTDLL.@]
2114 void WINAPI RtlGetCurrentProcessorNumberEx(PROCESSOR_NUMBER *processor)
2116 FIXME("(%p) :semi-stub\n", processor);
2117 processor->Group = 0;
2118 processor->Number = NtGetCurrentProcessorNumber();
2119 processor->Reserved = 0;
2122 /***********************************************************************
2123 * RtlInitializeGenericTableAvl (NTDLL.@)
2125 void WINAPI RtlInitializeGenericTableAvl(PRTL_AVL_TABLE table, PRTL_AVL_COMPARE_ROUTINE compare,
2126 PRTL_AVL_ALLOCATE_ROUTINE allocate, PRTL_AVL_FREE_ROUTINE free, void *context)
2128 FIXME("%p %p %p %p %p: stub\n", table, compare, allocate, free, context);
2131 /***********************************************************************
2132 * RtlInsertElementGenericTableAvl (NTDLL.@)
2134 void WINAPI RtlInsertElementGenericTableAvl(PRTL_AVL_TABLE table, void *buffer, ULONG size, BOOL *element)
2136 FIXME("%p %p %u %p: stub\n", table, buffer, size, element);
2139 /*********************************************************************
2140 * RtlQueryPackageIdentity [NTDLL.@]
2142 NTSTATUS WINAPI RtlQueryPackageIdentity(HANDLE token, WCHAR *fullname, SIZE_T *fullname_size,
2143 WCHAR *appid, SIZE_T *appid_size, BOOLEAN *packaged)
2145 FIXME("(%p, %p, %p, %p, %p, %p): stub\n", token, fullname, fullname_size, appid, appid_size, packaged);
2146 return STATUS_NOT_FOUND;