cmd: Introduce helpers to handle directory walk.
[wine.git] / dlls / ntdll / rtl.c
blob1288b57f11c94cdb1663a7d6f43cebdc5e1042e9
1 /*
2 * NT basis DLL
4 * This file contains the Rtl* API functions. These should be implementable.
6 * Copyright 1996-1998 Marcus Meissner
7 * Copyright 1999 Alex Korobka
8 * Copyright 2003 Thomas Mertes
9 * Crc32 code Copyright 1986 Gary S. Brown (Public domain)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <stdarg.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "winsock2.h"
31 #include "windef.h"
32 #include "winternl.h"
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35 #include "ntdll_misc.h"
36 #include "in6addr.h"
37 #include "ddk/ntddk.h"
38 #include "ddk/ntifs.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 WINE_DECLARE_DEBUG_CHANNEL(debugstr);
43 #define htons(x) RtlUshortByteSwap(x)
44 #define ntohs(x) RtlUshortByteSwap(x)
46 /* CRC polynomial 0xedb88320 */
47 static const DWORD CRC_table[256] =
49 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
50 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
51 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
52 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
53 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
54 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
55 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
56 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
57 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
58 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
59 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
60 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
61 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
62 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
63 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
64 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
65 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
66 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
67 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
68 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
69 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
70 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
71 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
72 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
73 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
74 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
75 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
76 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
77 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
78 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
79 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
80 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
81 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
82 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
83 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
84 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
85 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
86 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
87 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
88 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
89 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
90 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
91 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
94 static const int hex_table[] = {
95 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
96 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
97 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2F */
98 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
99 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4F */
100 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
101 -1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
105 static LONG WINAPI debug_exception_handler( EXCEPTION_POINTERS *eptr )
107 EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
108 return (rec->ExceptionCode == DBG_PRINTEXCEPTION_C) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
111 /******************************************************************************
112 * DbgPrint [NTDLL.@]
114 NTSTATUS WINAPIV DbgPrint(LPCSTR fmt, ...)
116 NTSTATUS ret;
117 va_list args;
119 va_start(args, fmt);
120 ret = vDbgPrintEx(0, DPFLTR_ERROR_LEVEL, fmt, args);
121 va_end(args);
122 return ret;
126 /******************************************************************************
127 * DbgPrintEx [NTDLL.@]
129 NTSTATUS WINAPIV DbgPrintEx(ULONG iComponentId, ULONG Level, LPCSTR fmt, ...)
131 NTSTATUS ret;
132 va_list args;
134 va_start(args, fmt);
135 ret = vDbgPrintEx(iComponentId, Level, fmt, args);
136 va_end(args);
137 return ret;
140 /******************************************************************************
141 * vDbgPrintEx [NTDLL.@]
143 NTSTATUS WINAPI vDbgPrintEx( ULONG id, ULONG level, LPCSTR fmt, va_list args )
145 return vDbgPrintExWithPrefix( "", id, level, fmt, args );
148 /******************************************************************************
149 * vDbgPrintExWithPrefix [NTDLL.@]
151 NTSTATUS WINAPI vDbgPrintExWithPrefix( LPCSTR prefix, ULONG id, ULONG level, LPCSTR fmt, va_list args )
153 ULONG level_mask = level <= 31 ? (1 << level) : level;
154 SIZE_T len = strlen( prefix );
155 char buf[1024], *end;
157 strcpy( buf, prefix );
158 len += _vsnprintf( buf + len, sizeof(buf) - len, fmt, args );
159 end = buf + len - 1;
161 WARN_(debugstr)(*end == '\n' ? "%08lx:%08lx: %s" : "%08lx:%08lx: %s\n", id, level_mask, buf);
163 if (level_mask & (1 << DPFLTR_ERROR_LEVEL) && NtCurrentTeb()->Peb->BeingDebugged)
165 __TRY
167 EXCEPTION_RECORD record;
168 record.ExceptionCode = DBG_PRINTEXCEPTION_C;
169 record.ExceptionFlags = 0;
170 record.ExceptionRecord = NULL;
171 record.ExceptionAddress = RtlRaiseException;
172 record.NumberParameters = 2;
173 record.ExceptionInformation[1] = (ULONG_PTR)buf;
174 record.ExceptionInformation[0] = strlen( buf ) + 1;
175 RtlRaiseException( &record );
177 __EXCEPT(debug_exception_handler)
180 __ENDTRY
183 return STATUS_SUCCESS;
186 /******************************************************************************
187 * RtlInitializeGenericTable [NTDLL.@]
189 void WINAPI RtlInitializeGenericTable(RTL_GENERIC_TABLE *table, PRTL_GENERIC_COMPARE_ROUTINE compare,
190 PRTL_GENERIC_ALLOCATE_ROUTINE allocate, PRTL_GENERIC_FREE_ROUTINE free,
191 void *context)
193 TRACE("(%p, %p, %p, %p, %p)\n", table, compare, allocate, free, context);
195 table->TableRoot = NULL;
196 table->InsertOrderList.Flink = &table->InsertOrderList;
197 table->InsertOrderList.Blink = &table->InsertOrderList;
198 table->OrderedPointer = &table->InsertOrderList;
199 table->NumberGenericTableElements = 0;
200 table->WhichOrderedElement = 0;
201 table->CompareRoutine = compare;
202 table->AllocateRoutine = allocate;
203 table->FreeRoutine = free;
204 table->TableContext = context;
207 /******************************************************************************
208 * RtlEnumerateGenericTableWithoutSplaying [NTDLL.@]
210 void * WINAPI RtlEnumerateGenericTableWithoutSplaying(RTL_GENERIC_TABLE *table, void *previous)
212 static int warn_once;
214 if (!warn_once++)
215 FIXME("(%p, %p) stub!\n", table, previous);
216 return NULL;
219 /******************************************************************************
220 * RtlNumberGenericTableElements [NTDLL.@]
222 ULONG WINAPI RtlNumberGenericTableElements(RTL_GENERIC_TABLE *table)
224 TRACE("(%p)\n", table);
225 return table->NumberGenericTableElements;
228 /******************************************************************************
229 * RtlGetElementGenericTable [NTDLL.@]
231 void * WINAPI RtlGetElementGenericTable(RTL_GENERIC_TABLE *table, ULONG index)
233 FIXME("(%p, %lu) stub!\n", table, index);
234 return NULL;
237 /******************************************************************************
238 * RtlLookupElementGenericTable [NTDLL.@]
240 void * WINAPI RtlLookupElementGenericTable(RTL_GENERIC_TABLE *table, void *buffer)
242 FIXME("(%p, %p) stub!\n", table, buffer);
243 return NULL;
246 /******************************************************************************
247 * RtlAssert [NTDLL.@]
249 * Fail a debug assertion.
251 * RETURNS
252 * Nothing. This call does not return control to its caller.
254 * NOTES
255 * Not implemented in non-debug versions.
257 void WINAPI RtlAssert(void *assertion, void *filename, ULONG linenumber, char *message)
259 FIXME("(%s, %s, %lu, %s): stub\n", debugstr_a((char*)assertion), debugstr_a((char*)filename),
260 linenumber, debugstr_a(message));
263 /*********************************************************************
264 * RtlComputeCrc32 [NTDLL.@]
266 * Calculate the CRC32 checksum of a block of bytes
268 * PARAMS
269 * dwInitial [I] Initial CRC value
270 * pData [I] Data block
271 * iLen [I] Length of the byte block
273 * RETURNS
274 * The cumulative CRC32 of dwInitial and iLen bytes of the pData block.
276 DWORD WINAPI RtlComputeCrc32(DWORD dwInitial, const BYTE *pData, INT iLen)
278 DWORD crc = ~dwInitial;
280 TRACE("(%lu,%p,%d)\n", dwInitial, pData, iLen);
282 while (iLen > 0)
284 crc = CRC_table[(crc ^ *pData) & 0xff] ^ (crc >> 8);
285 pData++;
286 iLen--;
288 return ~crc;
292 /*************************************************************************
293 * RtlUlonglongByteSwap [NTDLL.@]
295 * Swap the bytes of an unsigned long long value.
297 * PARAMS
298 * i [I] Value to swap bytes of
300 * RETURNS
301 * The value with its bytes swapped.
303 #ifdef __i386__
304 __ASM_FASTCALL_FUNC(RtlUlonglongByteSwap, 8,
305 "movl 4(%esp),%edx\n\t"
306 "bswap %edx\n\t"
307 "movl 8(%esp),%eax\n\t"
308 "bswap %eax\n\t"
309 "ret $8")
310 #endif
312 /*************************************************************************
313 * RtlUlongByteSwap [NTDLL.@]
315 * Swap the bytes of an unsigned int value.
317 * NOTES
318 * ix86 version takes argument in %ecx. Other systems use the inline version.
320 #ifdef __i386__
321 __ASM_FASTCALL_FUNC(RtlUlongByteSwap, 4,
322 "movl %ecx,%eax\n\t"
323 "bswap %eax\n\t"
324 "ret")
325 #endif
327 /*************************************************************************
328 * RtlUshortByteSwap [NTDLL.@]
330 * Swap the bytes of an unsigned short value.
332 * NOTES
333 * i386 version takes argument in %cx. Other systems use the inline version.
335 #ifdef __i386__
336 __ASM_FASTCALL_FUNC(RtlUshortByteSwap, 4,
337 "movb %ch,%al\n\t"
338 "movb %cl,%ah\n\t"
339 "ret")
340 #endif
343 /*************************************************************************
344 * RtlUniform [NTDLL.@]
346 * Generates a uniform random number
348 * PARAMS
349 * seed [O] The seed of the Random function
351 * RETURNS
352 * It returns a random number uniformly distributed over [0..MAXLONG-1].
354 * NOTES
355 * Generates a uniform random number using a linear congruential generator.
357 * DIFFERENCES
358 * The native documentation states that the random number is
359 * uniformly distributed over [0..MAXLONG]. In reality the native
360 * function and our function return a random number uniformly
361 * distributed over [0..MAXLONG-1].
363 ULONG WINAPI RtlUniform( ULONG *seed )
365 /* See the tests for details. */
366 return (*seed = ((ULONGLONG)*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff);
370 /*************************************************************************
371 * RtlRandom [NTDLL.@]
373 * Generates a random number
375 * PARAMS
376 * seed [O] The seed of the Random function
378 * RETURNS
379 * It returns a random number distributed over [0..MAXLONG-1].
381 ULONG WINAPI RtlRandom (PULONG seed)
383 static ULONG saved_value[128] =
384 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
385 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
386 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
387 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
388 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
389 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
390 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
391 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
392 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
393 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
394 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
395 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
396 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
397 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
398 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
399 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
400 ULONG rand;
401 int pos;
402 ULONG result;
404 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
405 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
406 pos = *seed & 0x7f;
407 result = saved_value[pos];
408 saved_value[pos] = rand;
409 return(result);
413 /*************************************************************************
414 * RtlRandomEx [NTDLL.@]
416 ULONG WINAPI RtlRandomEx( ULONG *seed )
418 WARN( "semi-stub: should use a different algorithm\n" );
419 return RtlRandom( seed );
422 static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
424 int base = 10, d;
425 WCHAR c;
426 ULONG cur_value, prev_value = 0;
427 BOOL success = FALSE;
429 if (**str == '.')
431 *str += 1;
432 return FALSE;
435 if ((*str)[0] == '0')
437 if ((*str)[1] == 'x' || (*str)[1] == 'X')
439 *str += 2;
440 if (strict) return FALSE;
441 base = 16;
443 else if ((*str)[1] >= '0' && (*str)[1] <= '9')
445 *str += 1;
446 if (strict) return FALSE;
447 base = 8;
451 for (cur_value = 0; **str; *str += 1)
453 c = **str;
454 if (c >= ARRAY_SIZE(hex_table)) break;
455 d = hex_table[c];
456 if (d == -1 || d >= base) break;
457 cur_value = cur_value * base + d;
458 success = TRUE;
459 if (cur_value < prev_value) return FALSE; /* overflow */
460 prev_value = cur_value;
463 if (success) *value = cur_value;
464 return success;
467 static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
468 const WCHAR **terminator, IN_ADDR *address, USHORT *port)
470 ULONG fields[4];
471 int n = 0;
473 for (;;)
475 if (!parse_ipv4_component(&str, strict, &fields[n]))
476 goto error;
477 n++;
478 if (*str != '.')
479 break;
480 if (n == 4)
481 goto error;
482 str++;
485 if (strict && n < 4)
486 goto error;
488 switch (n)
490 case 4:
491 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFF || fields[3] > 0xFF)
492 goto error;
493 address->S_un.S_un_b.s_b1 = fields[0];
494 address->S_un.S_un_b.s_b2 = fields[1];
495 address->S_un.S_un_b.s_b3 = fields[2];
496 address->S_un.S_un_b.s_b4 = fields[3];
497 break;
498 case 3:
499 if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFFFF)
500 goto error;
501 address->S_un.S_un_b.s_b1 = fields[0];
502 address->S_un.S_un_b.s_b2 = fields[1];
503 address->S_un.S_un_b.s_b3 = (fields[2] & 0xFF00) >> 8;
504 address->S_un.S_un_b.s_b4 = (fields[2] & 0x00FF);
505 break;
506 case 2:
507 if (fields[0] > 0xFF || fields[1] > 0xFFFFFF)
508 goto error;
509 address->S_un.S_un_b.s_b1 = fields[0];
510 address->S_un.S_un_b.s_b2 = (fields[1] & 0xFF0000) >> 16;
511 address->S_un.S_un_b.s_b3 = (fields[1] & 0x00FF00) >> 8;
512 address->S_un.S_un_b.s_b4 = (fields[1] & 0x0000FF);
513 break;
514 case 1:
515 address->S_un.S_un_b.s_b1 = (fields[0] & 0xFF000000) >> 24;
516 address->S_un.S_un_b.s_b2 = (fields[0] & 0x00FF0000) >> 16;
517 address->S_un.S_un_b.s_b3 = (fields[0] & 0x0000FF00) >> 8;
518 address->S_un.S_un_b.s_b4 = (fields[0] & 0x000000FF);
519 break;
520 default:
521 goto error;
524 if (terminator) *terminator = str;
526 if (*str == ':')
528 str++;
529 if (!parse_ipv4_component(&str, FALSE, &fields[0]))
530 goto error;
531 if (!fields[0] || fields[0] > 0xFFFF || *str)
532 goto error;
533 if (port)
535 *port = htons(fields[0]);
536 if (terminator) *terminator = str;
540 if (!terminator && *str)
541 return STATUS_INVALID_PARAMETER;
542 return STATUS_SUCCESS;
544 error:
545 if (terminator) *terminator = str;
546 return STATUS_INVALID_PARAMETER;
549 /***********************************************************************
550 * RtlIpv4StringToAddressExW [NTDLL.@]
552 NTSTATUS WINAPI RtlIpv4StringToAddressExW(const WCHAR *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
554 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, address, port);
555 if (!str || !address || !port) return STATUS_INVALID_PARAMETER;
556 return ipv4_string_to_address(str, strict, NULL, address, port);
559 /***********************************************************************
560 * RtlIpv4StringToAddressW [NTDLL.@]
562 NTSTATUS WINAPI RtlIpv4StringToAddressW(const WCHAR *str, BOOLEAN strict, const WCHAR **terminator, IN_ADDR *address)
564 TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, terminator, address);
565 return ipv4_string_to_address(str, strict, terminator, address, NULL);
568 /***********************************************************************
569 * RtlIpv4StringToAddressExA [NTDLL.@]
571 NTSTATUS WINAPI RtlIpv4StringToAddressExA(const char *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
573 WCHAR wstr[32];
575 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, address, port);
577 if (!str || !address || !port)
578 return STATUS_INVALID_PARAMETER;
580 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
581 wstr[ARRAY_SIZE(wstr) - 1] = 0;
582 return ipv4_string_to_address(wstr, strict, NULL, address, port);
585 /***********************************************************************
586 * RtlIpv4StringToAddressA [NTDLL.@]
588 NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const char **terminator, IN_ADDR *address)
590 WCHAR wstr[32];
591 const WCHAR *wterminator;
592 NTSTATUS ret;
594 TRACE("(%s, %u, %p, %p)\n", debugstr_a(str), strict, terminator, address);
596 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
597 wstr[ARRAY_SIZE(wstr) - 1] = 0;
598 ret = ipv4_string_to_address(wstr, strict, &wterminator, address, NULL);
599 if (terminator) *terminator = str + (wterminator - wstr);
600 return ret;
603 static BOOL parse_ipv6_component(const WCHAR **str, int base, ULONG *value)
605 WCHAR *terminator;
606 if (**str >= ARRAY_SIZE(hex_table) || hex_table[**str] == -1) return FALSE;
607 *value = min(wcstoul(*str, &terminator, base), 0x7FFFFFFF);
608 if (*terminator == '0') terminator++; /* "0x" but nothing valid after */
609 else if (terminator == *str) return FALSE;
610 *str = terminator;
611 return TRUE;
614 static NTSTATUS ipv6_string_to_address(const WCHAR *str, BOOL ex,
615 const WCHAR **terminator, IN6_ADDR *address, ULONG *scope, USHORT *port)
617 BOOL expecting_port = FALSE, has_0x = FALSE, too_big = FALSE;
618 int n_bytes = 0, n_ipv4_bytes = 0, gap = -1;
619 ULONG ip_component, scope_component = 0, port_component = 0;
620 const WCHAR *prev_str;
622 if (str[0] == '[')
624 if (!ex) goto error;
625 expecting_port = TRUE;
626 str++;
629 if (str[0] == ':')
631 if (str[1] != ':') goto error;
632 str++;
633 address->u.Word[0] = 0;
636 for (;;)
638 if (!n_ipv4_bytes && *str == ':')
640 /* double colon */
641 if (gap != -1) goto error;
642 str++;
643 prev_str = str;
644 gap = n_bytes;
645 if (n_bytes == 14 || !parse_ipv6_component(&str, 16, &ip_component)) break;
646 str = prev_str;
648 else
650 prev_str = str;
653 if (!n_ipv4_bytes && n_bytes <= (gap != -1 ? 10 : 12))
655 if (parse_ipv6_component(&str, 10, &ip_component) && *str == '.')
656 n_ipv4_bytes = 1;
657 str = prev_str;
660 if (n_ipv4_bytes)
662 /* IPv4 component */
663 if (!parse_ipv6_component(&str, 10, &ip_component)) goto error;
664 if (str - prev_str > 3 || ip_component > 255)
666 too_big = TRUE;
668 else
670 if (*str != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
671 address->u.Byte[n_bytes] = ip_component;
672 n_bytes++;
674 if (n_ipv4_bytes == 4 || *str != '.') break;
675 n_ipv4_bytes++;
677 else
679 /* IPv6 component */
680 if (!parse_ipv6_component(&str, 16, &ip_component)) goto error;
681 if (prev_str[0] == '0' && (prev_str[1] == 'x' || prev_str[1] == 'X'))
683 /* Windows "feature": the last IPv6 component can start with "0x" and be longer than 4 digits */
684 if (terminator) *terminator = prev_str + 1; /* Windows says that the "x" is the terminator */
685 if (n_bytes < 14 && gap == -1) return STATUS_INVALID_PARAMETER;
686 address->u.Word[n_bytes/2] = htons(ip_component);
687 n_bytes += 2;
688 has_0x = TRUE;
689 goto fill_gap;
691 if (*str != ':' && n_bytes < 14 && gap == -1) goto error;
692 if (str - prev_str > 4)
693 too_big = TRUE;
694 else
695 address->u.Word[n_bytes/2] = htons(ip_component);
696 n_bytes += 2;
697 if (*str != ':' || (gap != -1 && str[1] == ':')) break;
699 if (n_bytes == (gap != -1 ? 14 : 16)) break;
700 if (too_big) return STATUS_INVALID_PARAMETER;
701 str++;
704 if (terminator) *terminator = str;
705 if (too_big) return STATUS_INVALID_PARAMETER;
707 fill_gap:
708 if (gap == -1)
710 if (n_bytes < 16) goto error;
712 else
714 memmove(address->u.Byte + 16 - (n_bytes - gap), address->u.Byte + gap, n_bytes - gap);
715 memset(address->u.Byte + gap, 0, 16 - n_bytes);
718 if (ex)
720 if (has_0x) goto error;
722 if (*str == '%')
724 str++;
725 if (!parse_ipv4_component(&str, TRUE, &scope_component)) goto error;
728 if (expecting_port)
730 if (*str != ']') goto error;
731 str++;
732 if (*str == ':')
734 str++;
735 if (!parse_ipv4_component(&str, FALSE, &port_component)) goto error;
736 if (!port_component || port_component > 0xFFFF || *str) goto error;
737 port_component = htons(port_component);
742 if (!terminator && *str) return STATUS_INVALID_PARAMETER;
744 if (scope) *scope = scope_component;
745 if (port) *port = port_component;
747 return STATUS_SUCCESS;
749 error:
750 if (terminator) *terminator = str;
751 return STATUS_INVALID_PARAMETER;
754 /***********************************************************************
755 * RtlIpv6StringToAddressExW [NTDLL.@]
757 NTSTATUS NTAPI RtlIpv6StringToAddressExW(const WCHAR *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
759 TRACE("(%s, %p, %p, %p)\n", debugstr_w(str), address, scope, port);
760 if (!str || !address || !scope || !port) return STATUS_INVALID_PARAMETER;
761 return ipv6_string_to_address(str, TRUE, NULL, address, scope, port);
764 /***********************************************************************
765 * RtlIpv6StringToAddressW [NTDLL.@]
767 NTSTATUS WINAPI RtlIpv6StringToAddressW(const WCHAR *str, const WCHAR **terminator, IN6_ADDR *address)
769 TRACE("(%s, %p, %p)\n", debugstr_w(str), terminator, address);
770 return ipv6_string_to_address(str, FALSE, terminator, address, NULL, NULL);
773 /***********************************************************************
774 * RtlIpv6StringToAddressExA [NTDLL.@]
776 NTSTATUS WINAPI RtlIpv6StringToAddressExA(const char *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
778 WCHAR wstr[128];
780 TRACE("(%s, %p, %p, %p)\n", debugstr_a(str), address, scope, port);
782 if (!str || !address || !scope || !port)
783 return STATUS_INVALID_PARAMETER;
785 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
786 wstr[ARRAY_SIZE(wstr) - 1] = 0;
787 return ipv6_string_to_address(wstr, TRUE, NULL, address, scope, port);
790 /***********************************************************************
791 * RtlIpv6StringToAddressA [NTDLL.@]
793 NTSTATUS WINAPI RtlIpv6StringToAddressA(const char *str, const char **terminator, IN6_ADDR *address)
795 WCHAR wstr[128];
796 const WCHAR *wterminator = NULL;
797 NTSTATUS ret;
799 TRACE("(%s, %p, %p)\n", debugstr_a(str), terminator, address);
801 RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
802 wstr[ARRAY_SIZE(wstr) - 1] = 0;
803 ret = ipv6_string_to_address(wstr, FALSE, &wterminator, address, NULL, NULL);
804 if (terminator && wterminator) *terminator = str + (wterminator - wstr);
805 return ret;
808 /***********************************************************************
809 * RtlIpv4AddressToStringExW [NTDLL.@]
811 * Convert the given ipv4 address and optional the port to a string
813 * PARAMS
814 * pin [I] PTR to the ip address to convert (network byte order)
815 * port [I] optional port to convert (network byte order)
816 * buffer [O] destination buffer for the result
817 * psize [IO] PTR to available/used size of the destination buffer
819 * RETURNS
820 * Success: STATUS_SUCCESS
821 * Failure: STATUS_INVALID_PARAMETER
824 NTSTATUS WINAPI RtlIpv4AddressToStringExW(const IN_ADDR *pin, USHORT port, LPWSTR buffer, PULONG psize)
826 WCHAR tmp_ip[32];
827 ULONG needed;
829 if (!pin || !buffer || !psize)
830 return STATUS_INVALID_PARAMETER;
832 TRACE("(%p:0x%lx, %d, %p, %p:%ld)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
834 needed = swprintf(tmp_ip, ARRAY_SIZE(tmp_ip), L"%u.%u.%u.%u",
835 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
836 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
838 if (port) needed += swprintf(tmp_ip + needed, ARRAY_SIZE(tmp_ip) - needed, L":%u", ntohs(port));
840 if (*psize > needed) {
841 *psize = needed + 1;
842 wcscpy(buffer, tmp_ip);
843 return STATUS_SUCCESS;
846 *psize = needed + 1;
847 return STATUS_INVALID_PARAMETER;
850 /***********************************************************************
851 * RtlIpv4AddressToStringExA [NTDLL.@]
853 * Convert the given ipv4 address and optional the port to a string
855 * See RtlIpv4AddressToStringExW
857 NTSTATUS WINAPI RtlIpv4AddressToStringExA(const IN_ADDR *pin, USHORT port, LPSTR buffer, PULONG psize)
859 CHAR tmp_ip[32];
860 ULONG needed;
862 if (!pin || !buffer || !psize)
863 return STATUS_INVALID_PARAMETER;
865 TRACE("(%p:0x%lx, %d, %p, %p:%ld)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
867 needed = sprintf(tmp_ip, "%u.%u.%u.%u",
868 pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
869 pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
871 if (port) needed += sprintf(tmp_ip + needed, ":%u", ntohs(port));
873 if (*psize > needed) {
874 *psize = needed + 1;
875 strcpy(buffer, tmp_ip);
876 return STATUS_SUCCESS;
879 *psize = needed + 1;
880 return STATUS_INVALID_PARAMETER;
883 /***********************************************************************
884 * RtlIpv4AddressToStringW [NTDLL.@]
886 * Convert the given ipv4 address to a string
888 * PARAMS
889 * pin [I] PTR to the ip address to convert (network byte order)
890 * buffer [O] destination buffer for the result (at least 16 character)
892 * RETURNS
893 * PTR to the 0 character at the end of the converted string
896 WCHAR * WINAPI RtlIpv4AddressToStringW(const IN_ADDR *pin, LPWSTR buffer)
898 ULONG size = 16;
900 if (RtlIpv4AddressToStringExW(pin, 0, buffer, &size)) size = 0;
901 return buffer + size - 1;
904 /***********************************************************************
905 * RtlIpv4AddressToStringA [NTDLL.@]
907 * Convert the given ipv4 address to a string
909 * See RtlIpv4AddressToStringW
911 CHAR * WINAPI RtlIpv4AddressToStringA(const IN_ADDR *pin, LPSTR buffer)
913 ULONG size = 16;
915 if (RtlIpv4AddressToStringExA(pin, 0, buffer, &size)) size = 0;
916 return buffer + size - 1;
919 static BOOL is_ipv4_in_ipv6(const IN6_ADDR *address)
921 if (address->s6_words[5] == htons(0x5efe) && (address->s6_words[4] & ~htons(0x200)) == 0)
922 return TRUE;
923 if (*(UINT64 *)address != 0)
924 return FALSE;
925 if (address->s6_words[4] != 0 && address->s6_words[4] != 0xffff)
926 return FALSE;
927 if (address->s6_words[4] == 0 && address->s6_words[5] != 0 && address->s6_words[5] != 0xffff)
928 return FALSE;
929 if (address->s6_words[4] == 0xffff && address->s6_words[5] != 0)
930 return FALSE;
931 if (address->s6_words[6] == 0)
932 return FALSE;
933 return TRUE;
936 /***********************************************************************
937 * RtlIpv6AddressToStringExA [NTDLL.@]
939 NTSTATUS WINAPI RtlIpv6AddressToStringExA(const IN6_ADDR *address, ULONG scope, USHORT port, char *str, ULONG *size)
941 char buffer[64], *p = buffer;
942 int i, len, gap = -1, gap_len = 1, ipv6_end = 8;
943 ULONG needed;
944 NTSTATUS ret;
946 TRACE("(%p %lu %u %p %p)\n", address, scope, port, str, size);
948 if (!address || !str || !size)
949 return STATUS_INVALID_PARAMETER;
951 if (is_ipv4_in_ipv6(address))
952 ipv6_end = 6;
954 for (i = 0; i < ipv6_end; i++)
956 len = 0;
957 while (!address->s6_words[i] && i < ipv6_end)
959 i++;
960 len++;
962 if (len > gap_len)
964 gap = i - len;
965 gap_len = len;
969 if (port) p += sprintf(p, "[");
971 i = 0;
972 while (i < ipv6_end)
974 if (i == gap)
976 p += sprintf(p, ":");
977 i += gap_len;
978 if (i == ipv6_end) p += sprintf(p, ":");
979 continue;
981 if (i > 0) p += sprintf(p, ":");
982 p += sprintf(p, "%x", ntohs(address->s6_words[i]));
983 i++;
986 if (ipv6_end == 6)
988 if (p[-1] != ':') p += sprintf(p, ":");
989 p = RtlIpv4AddressToStringA((IN_ADDR *)(address->s6_words + 6), p);
992 if (scope) p += sprintf(p, "%%%lu", scope);
994 if (port) p += sprintf(p, "]:%u", ntohs(port));
996 needed = p - buffer + 1;
998 if (*size >= needed)
1000 strcpy(str, buffer);
1001 ret = STATUS_SUCCESS;
1003 else
1005 ret = STATUS_INVALID_PARAMETER;
1008 *size = needed;
1009 return ret;
1012 /***********************************************************************
1013 * RtlIpv6AddressToStringA [NTDLL.@]
1015 char * WINAPI RtlIpv6AddressToStringA(const IN6_ADDR *address, char *str)
1017 ULONG size = 46;
1018 if (!address || !str) return str - 1;
1019 str[45] = 0; /* this byte is set even though the string is always shorter */
1020 RtlIpv6AddressToStringExA(address, 0, 0, str, &size);
1021 return str + size - 1;
1024 /***********************************************************************
1025 * RtlIpv6AddressToStringExW [NTDLL.@]
1027 NTSTATUS WINAPI RtlIpv6AddressToStringExW(const IN6_ADDR *address, ULONG scope, USHORT port, WCHAR *str, ULONG *size)
1029 char cstr[64];
1030 NTSTATUS ret = RtlIpv6AddressToStringExA(address, scope, port, cstr, size);
1031 if (ret == STATUS_SUCCESS) RtlMultiByteToUnicodeN(str, *size * sizeof(WCHAR), NULL, cstr, *size);
1032 return ret;
1035 /***********************************************************************
1036 * RtlIpv6AddressToStringW [NTDLL.@]
1038 WCHAR * WINAPI RtlIpv6AddressToStringW(const IN6_ADDR *address, WCHAR *str)
1040 ULONG size = 46;
1041 if (!address || !str) return str;
1042 str[45] = 0; /* this word is set even though the string is always shorter */
1043 if (RtlIpv6AddressToStringExW(address, 0, 0, str, &size) != STATUS_SUCCESS)
1044 return str;
1045 return str + size - 1;
1048 /***********************************************************************
1049 * get_pointer_obfuscator (internal)
1051 static DWORD_PTR get_pointer_obfuscator( void )
1053 static DWORD_PTR pointer_obfuscator;
1055 if (!pointer_obfuscator)
1057 ULONG seed = NtGetTickCount();
1058 ULONG_PTR rand;
1060 /* generate a random value for the obfuscator */
1061 rand = RtlUniform( &seed );
1063 /* handle 64bit pointers */
1064 rand ^= (ULONG_PTR)RtlUniform( &seed ) << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1066 /* set the high bits so dereferencing obfuscated pointers will (usually) crash */
1067 rand |= (ULONG_PTR)0xc0000000 << ((sizeof (DWORD_PTR) - sizeof (ULONG))*8);
1069 InterlockedCompareExchangePointer( (void**) &pointer_obfuscator, (void*) rand, NULL );
1072 return pointer_obfuscator;
1075 /*************************************************************************
1076 * RtlEncodePointer [NTDLL.@]
1078 PVOID WINAPI RtlEncodePointer( PVOID ptr )
1080 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1081 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1084 PVOID WINAPI RtlDecodePointer( PVOID ptr )
1086 DWORD_PTR ptrval = (DWORD_PTR) ptr;
1087 return (PVOID)(ptrval ^ get_pointer_obfuscator());
1090 /******************************************************************************
1091 * RtlGetCompressionWorkSpaceSize [NTDLL.@]
1093 NTSTATUS WINAPI RtlGetCompressionWorkSpaceSize(USHORT format, PULONG compress_workspace,
1094 PULONG decompress_workspace)
1096 FIXME("0x%04x, %p, %p: semi-stub\n", format, compress_workspace, decompress_workspace);
1098 switch (format & COMPRESSION_FORMAT_MASK)
1100 case COMPRESSION_FORMAT_LZNT1:
1101 if (compress_workspace)
1103 /* FIXME: The current implementation of RtlCompressBuffer does not use a
1104 * workspace buffer, but Windows applications might expect a nonzero value. */
1105 *compress_workspace = 16;
1107 if (decompress_workspace)
1108 *decompress_workspace = 0x1000;
1109 return STATUS_SUCCESS;
1111 case COMPRESSION_FORMAT_NONE:
1112 case COMPRESSION_FORMAT_DEFAULT:
1113 return STATUS_INVALID_PARAMETER;
1115 default:
1116 FIXME("format %u not implemented\n", format);
1117 return STATUS_UNSUPPORTED_COMPRESSION;
1121 /* compress data using LZNT1, currently only a stub */
1122 static NTSTATUS lznt1_compress(UCHAR *src, ULONG src_size, UCHAR *dst, ULONG dst_size,
1123 ULONG chunk_size, ULONG *final_size, UCHAR *workspace)
1125 UCHAR *src_cur = src, *src_end = src + src_size;
1126 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1127 ULONG block_size;
1129 while (src_cur < src_end)
1131 /* determine size of current chunk */
1132 block_size = min(0x1000, src_end - src_cur);
1133 if (dst_cur + sizeof(WORD) + block_size > dst_end)
1134 return STATUS_BUFFER_TOO_SMALL;
1136 /* write (uncompressed) chunk header */
1137 *(WORD *)dst_cur = 0x3000 | (block_size - 1);
1138 dst_cur += sizeof(WORD);
1140 /* write chunk content */
1141 memcpy(dst_cur, src_cur, block_size);
1142 dst_cur += block_size;
1143 src_cur += block_size;
1146 if (final_size)
1147 *final_size = dst_cur - dst;
1149 return STATUS_SUCCESS;
1152 /******************************************************************************
1153 * RtlCompressBuffer [NTDLL.@]
1155 NTSTATUS WINAPI RtlCompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
1156 PUCHAR compressed, ULONG compressed_size, ULONG chunk_size,
1157 PULONG final_size, PVOID workspace)
1159 FIXME("0x%04x, %p, %lu, %p, %lu, %lu, %p, %p: semi-stub\n", format, uncompressed,
1160 uncompressed_size, compressed, compressed_size, chunk_size, final_size, workspace);
1162 switch (format & COMPRESSION_FORMAT_MASK)
1164 case COMPRESSION_FORMAT_LZNT1:
1165 return lznt1_compress(uncompressed, uncompressed_size, compressed,
1166 compressed_size, chunk_size, final_size, workspace);
1168 case COMPRESSION_FORMAT_NONE:
1169 case COMPRESSION_FORMAT_DEFAULT:
1170 return STATUS_INVALID_PARAMETER;
1172 default:
1173 FIXME("format %u not implemented\n", format);
1174 return STATUS_UNSUPPORTED_COMPRESSION;
1178 /* decompress a single LZNT1 chunk */
1179 static UCHAR *lznt1_decompress_chunk(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size)
1181 UCHAR *src_cur = src, *src_end = src + src_size;
1182 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1183 ULONG displacement_bits, length_bits;
1184 ULONG code_displacement, code_length;
1185 WORD flags, code;
1187 while (src_cur < src_end && dst_cur < dst_end)
1189 flags = 0x8000 | *src_cur++;
1190 while ((flags & 0xff00) && src_cur < src_end)
1192 if (flags & 1)
1194 /* backwards reference */
1195 if (src_cur + sizeof(WORD) > src_end)
1196 return NULL;
1198 code = *(WORD *)src_cur;
1199 src_cur += sizeof(WORD);
1201 /* find length / displacement bits */
1202 for (displacement_bits = 12; displacement_bits > 4; displacement_bits--)
1203 if ((1 << (displacement_bits - 1)) < dst_cur - dst) break;
1205 length_bits = 16 - displacement_bits;
1206 code_length = (code & ((1 << length_bits) - 1)) + 3;
1207 code_displacement = (code >> length_bits) + 1;
1209 if (dst_cur < dst + code_displacement)
1210 return NULL;
1212 /* copy bytes of chunk - we can't use memcpy() since source and dest can
1213 * be overlapping, and the same bytes can be repeated over and over again */
1214 while (code_length--)
1216 if (dst_cur >= dst_end) return dst_cur;
1217 *dst_cur = *(dst_cur - code_displacement);
1218 dst_cur++;
1221 else
1223 /* uncompressed data */
1224 if (dst_cur >= dst_end) return dst_cur;
1225 *dst_cur++ = *src_cur++;
1227 flags >>= 1;
1231 return dst_cur;
1234 /* decompress data encoded with LZNT1 */
1235 static NTSTATUS lznt1_decompress(UCHAR *dst, ULONG dst_size, UCHAR *src, ULONG src_size,
1236 ULONG offset, ULONG *final_size, UCHAR *workspace)
1238 UCHAR *src_cur = src, *src_end = src + src_size;
1239 UCHAR *dst_cur = dst, *dst_end = dst + dst_size;
1240 ULONG chunk_size, block_size;
1241 WORD chunk_header;
1242 UCHAR *ptr;
1244 if (src_cur + sizeof(WORD) > src_end)
1245 return STATUS_BAD_COMPRESSION_BUFFER;
1247 /* skip over chunks with a distance >= 0x1000 to the destination offset */
1248 while (offset >= 0x1000 && src_cur + sizeof(WORD) <= src_end)
1250 chunk_header = *(WORD *)src_cur;
1251 src_cur += sizeof(WORD);
1252 if (!chunk_header) goto out;
1253 chunk_size = (chunk_header & 0xfff) + 1;
1255 if (src_cur + chunk_size > src_end)
1256 return STATUS_BAD_COMPRESSION_BUFFER;
1258 src_cur += chunk_size;
1259 offset -= 0x1000;
1262 /* handle partially included chunk */
1263 if (offset && src_cur + sizeof(WORD) <= src_end)
1265 chunk_header = *(WORD *)src_cur;
1266 src_cur += sizeof(WORD);
1267 if (!chunk_header) goto out;
1268 chunk_size = (chunk_header & 0xfff) + 1;
1270 if (src_cur + chunk_size > src_end)
1271 return STATUS_BAD_COMPRESSION_BUFFER;
1273 if (dst_cur >= dst_end)
1274 goto out;
1276 if (chunk_header & 0x8000)
1278 /* compressed chunk */
1279 if (!workspace) return STATUS_ACCESS_VIOLATION;
1280 ptr = lznt1_decompress_chunk(workspace, 0x1000, src_cur, chunk_size);
1281 if (!ptr) return STATUS_BAD_COMPRESSION_BUFFER;
1282 if (ptr - workspace > offset)
1284 block_size = min((ptr - workspace) - offset, dst_end - dst_cur);
1285 memcpy(dst_cur, workspace + offset, block_size);
1286 dst_cur += block_size;
1289 else
1291 /* uncompressed chunk */
1292 if (chunk_size > offset)
1294 block_size = min(chunk_size - offset, dst_end - dst_cur);
1295 memcpy(dst_cur, src_cur + offset, block_size);
1296 dst_cur += block_size;
1300 src_cur += chunk_size;
1303 /* handle remaining chunks */
1304 while (src_cur + sizeof(WORD) <= src_end)
1306 chunk_header = *(WORD *)src_cur;
1307 src_cur += sizeof(WORD);
1308 if (!chunk_header) goto out;
1309 chunk_size = (chunk_header & 0xfff) + 1;
1311 if (src_cur + chunk_size > src_end)
1312 return STATUS_BAD_COMPRESSION_BUFFER;
1314 /* fill space with padding when the previous chunk was decompressed
1315 * to less than 4096 bytes. no padding is needed for the last chunk
1316 * or when the next chunk is truncated */
1317 block_size = ((dst_cur - dst) + offset) & 0xfff;
1318 if (block_size)
1320 block_size = 0x1000 - block_size;
1321 if (dst_cur + block_size >= dst_end)
1322 goto out;
1323 memset(dst_cur, 0, block_size);
1324 dst_cur += block_size;
1327 if (dst_cur >= dst_end)
1328 goto out;
1330 if (chunk_header & 0x8000)
1332 /* compressed chunk */
1333 dst_cur = lznt1_decompress_chunk(dst_cur, dst_end - dst_cur, src_cur, chunk_size);
1334 if (!dst_cur) return STATUS_BAD_COMPRESSION_BUFFER;
1336 else
1338 /* uncompressed chunk */
1339 block_size = min(chunk_size, dst_end - dst_cur);
1340 memcpy(dst_cur, src_cur, block_size);
1341 dst_cur += block_size;
1344 src_cur += chunk_size;
1347 out:
1348 if (final_size)
1349 *final_size = dst_cur - dst;
1351 return STATUS_SUCCESS;
1355 /******************************************************************************
1356 * RtlDecompressFragment [NTDLL.@]
1358 NTSTATUS WINAPI RtlDecompressFragment(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
1359 PUCHAR compressed, ULONG compressed_size, ULONG offset,
1360 PULONG final_size, PVOID workspace)
1362 TRACE("0x%04x, %p, %lu, %p, %lu, %lu, %p, %p\n", format, uncompressed,
1363 uncompressed_size, compressed, compressed_size, offset, final_size, workspace);
1365 switch (format & COMPRESSION_FORMAT_MASK)
1367 case COMPRESSION_FORMAT_LZNT1:
1368 return lznt1_decompress(uncompressed, uncompressed_size, compressed,
1369 compressed_size, offset, final_size, workspace);
1371 case COMPRESSION_FORMAT_NONE:
1372 case COMPRESSION_FORMAT_DEFAULT:
1373 return STATUS_INVALID_PARAMETER;
1375 default:
1376 FIXME("format %u not implemented\n", format);
1377 return STATUS_UNSUPPORTED_COMPRESSION;
1382 /******************************************************************************
1383 * RtlDecompressBuffer [NTDLL.@]
1385 NTSTATUS WINAPI RtlDecompressBuffer(USHORT format, PUCHAR uncompressed, ULONG uncompressed_size,
1386 PUCHAR compressed, ULONG compressed_size, PULONG final_size)
1388 TRACE("0x%04x, %p, %lu, %p, %lu, %p\n", format, uncompressed,
1389 uncompressed_size, compressed, compressed_size, final_size);
1391 return RtlDecompressFragment(format, uncompressed, uncompressed_size,
1392 compressed, compressed_size, 0, final_size, NULL);
1395 /******************************************************************************
1396 * RtlGetCurrentTransaction [NTDLL.@]
1398 HANDLE WINAPI RtlGetCurrentTransaction(void)
1400 FIXME("() :stub\n");
1401 return NULL;
1404 /******************************************************************************
1405 * RtlSetCurrentTransaction [NTDLL.@]
1407 BOOL WINAPI RtlSetCurrentTransaction(HANDLE new_transaction)
1409 FIXME("(%p) :stub\n", new_transaction);
1410 return FALSE;
1413 /**********************************************************************
1414 * RtlGetCurrentProcessorNumberEx [NTDLL.@]
1416 void WINAPI RtlGetCurrentProcessorNumberEx(PROCESSOR_NUMBER *processor)
1418 FIXME("(%p) :semi-stub\n", processor);
1419 processor->Group = 0;
1420 processor->Number = NtGetCurrentProcessorNumber();
1421 processor->Reserved = 0;
1424 /***********************************************************************
1425 * RtlIsProcessorFeaturePresent [NTDLL.@]
1427 BOOLEAN WINAPI RtlIsProcessorFeaturePresent( UINT feature )
1429 #ifdef __aarch64__
1430 static const ULONGLONG arm64_features =
1431 (1ull << PF_COMPARE_EXCHANGE_DOUBLE) |
1432 (1ull << PF_NX_ENABLED) |
1433 (1ull << PF_ARM_VFP_32_REGISTERS_AVAILABLE) |
1434 (1ull << PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) |
1435 (1ull << PF_SECOND_LEVEL_ADDRESS_TRANSLATION) |
1436 (1ull << PF_FASTFAIL_AVAILABLE) |
1437 (1ull << PF_ARM_DIVIDE_INSTRUCTION_AVAILABLE) |
1438 (1ull << PF_ARM_64BIT_LOADSTORE_ATOMIC) |
1439 (1ull << PF_ARM_EXTERNAL_CACHE_AVAILABLE) |
1440 (1ull << PF_ARM_FMAC_INSTRUCTIONS_AVAILABLE) |
1441 (1ull << PF_ARM_V8_INSTRUCTIONS_AVAILABLE) |
1442 (1ull << PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) |
1443 (1ull << PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) |
1444 (1ull << PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE) |
1445 (1ull << PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE) |
1446 (1ull << PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE) |
1447 (1ull << PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE);
1449 return (feature < PROCESSOR_FEATURE_MAX && (arm64_features & (1ull << feature)) &&
1450 user_shared_data->ProcessorFeatures[feature]);
1451 #elif defined _WIN64
1452 return feature < PROCESSOR_FEATURE_MAX && user_shared_data->ProcessorFeatures[feature];
1453 #else
1454 return NtWow64IsProcessorFeaturePresent( feature );
1455 #endif
1458 /***********************************************************************
1459 * RtlInitializeGenericTableAvl (NTDLL.@)
1461 void WINAPI RtlInitializeGenericTableAvl(PRTL_AVL_TABLE table, PRTL_AVL_COMPARE_ROUTINE compare,
1462 PRTL_AVL_ALLOCATE_ROUTINE allocate, PRTL_AVL_FREE_ROUTINE free, void *context)
1464 FIXME("%p %p %p %p %p: stub\n", table, compare, allocate, free, context);
1467 /***********************************************************************
1468 * RtlInsertElementGenericTableAvl (NTDLL.@)
1470 void WINAPI RtlInsertElementGenericTableAvl(PRTL_AVL_TABLE table, void *buffer, ULONG size, BOOL *element)
1472 FIXME("%p %p %lu %p: stub\n", table, buffer, size, element);
1475 /******************************************************************************
1476 * RtlLookupElementGenericTableAvl (NTDLL.@)
1478 void * WINAPI RtlLookupElementGenericTableAvl(PRTL_AVL_TABLE table, void *buffer)
1480 FIXME("(%p, %p) stub!\n", table, buffer);
1481 return NULL;
1484 /*********************************************************************
1485 * RtlQueryPackageIdentity [NTDLL.@]
1487 NTSTATUS WINAPI RtlQueryPackageIdentity(HANDLE token, WCHAR *fullname, SIZE_T *fullname_size,
1488 WCHAR *appid, SIZE_T *appid_size, BOOLEAN *packaged)
1490 FIXME("(%p, %p, %p, %p, %p, %p): stub\n", token, fullname, fullname_size, appid, appid_size, packaged);
1491 return STATUS_NOT_FOUND;
1494 /*********************************************************************
1495 * RtlQueryProcessPlaceholderCompatibilityMode [NTDLL.@]
1497 char WINAPI RtlQueryProcessPlaceholderCompatibilityMode(void)
1499 FIXME("stub\n");
1500 return PHCM_APPLICATION_DEFAULT;
1503 /*********************************************************************
1504 * RtlGetDeviceFamilyInfoEnum [NTDLL.@]
1506 void WINAPI RtlGetDeviceFamilyInfoEnum(ULONGLONG *version, DWORD *family, DWORD *form)
1508 FIXME("%p %p %p: stub\n", version, family, form);
1510 if (version) *version = 0;
1511 if (family) *family = DEVICEFAMILYINFOENUM_DESKTOP;
1512 if (form) *form = DEVICEFAMILYDEVICEFORM_UNKNOWN;