ntdll: Fix a few memory leaks in tests.
[wine] / dlls / ntdll / reg.c
1 /*
2  * Registry functions
3  *
4  * Copyright (C) 1999 Juergen Schmied
5  * Copyright (C) 2000 Alexandre Julliard
6  * Copyright 2005 Ivan Leo Puoti, Laurent Pinchart
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  * NOTES:
23  *      HKEY_LOCAL_MACHINE      \\REGISTRY\\MACHINE
24  *      HKEY_USERS              \\REGISTRY\\USER
25  *      HKEY_CURRENT_CONFIG     \\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\HARDWARE PROFILES\\CURRENT
26   *     HKEY_CLASSES            \\REGISTRY\\MACHINE\\SOFTWARE\\CLASSES
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "wine/library.h"
39 #include "ntdll_misc.h"
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(reg);
44
45 /* maximum length of a key/value name in bytes (without terminating null) */
46 #define MAX_NAME_LENGTH ((MAX_PATH-1) * sizeof(WCHAR))
47
48 /******************************************************************************
49  * NtCreateKey [NTDLL.@]
50  * ZwCreateKey [NTDLL.@]
51  */
52 NTSTATUS WINAPI NtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
53                              ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
54                              PULONG dispos )
55 {
56     NTSTATUS ret;
57
58     if (!retkey || !attr) return STATUS_ACCESS_VIOLATION;
59     if (attr->Length > sizeof(OBJECT_ATTRIBUTES)) return STATUS_INVALID_PARAMETER;
60     if (attr->ObjectName->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
61
62     TRACE( "(%p,%s,%s,%x,%x,%p)\n", attr->RootDirectory, debugstr_us(attr->ObjectName),
63            debugstr_us(class), options, access, retkey );
64
65     SERVER_START_REQ( create_key )
66     {
67         req->parent     = wine_server_obj_handle( attr->RootDirectory );
68         req->access     = access;
69         req->attributes = attr->Attributes;
70         req->options    = options;
71         req->namelen    = attr->ObjectName->Length;
72         wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
73         if (class) wine_server_add_data( req, class->Buffer, class->Length );
74         if (!(ret = wine_server_call( req )))
75         {
76             *retkey = wine_server_ptr_handle( reply->hkey );
77             if (dispos) *dispos = reply->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
78         }
79     }
80     SERVER_END_REQ;
81     TRACE("<- %p\n", *retkey);
82     return ret;
83 }
84
85 /******************************************************************************
86  *  RtlpNtCreateKey [NTDLL.@]
87  *
88  *  See NtCreateKey.
89  */
90 NTSTATUS WINAPI RtlpNtCreateKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
91                                  ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
92                                  PULONG dispos )
93 {
94     OBJECT_ATTRIBUTES oa;
95
96     if (attr)
97     {
98         oa = *attr;
99         oa.Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
100         attr = &oa;
101     }
102
103     return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
104 }
105
106 /******************************************************************************
107  * NtOpenKey [NTDLL.@]
108  * ZwOpenKey [NTDLL.@]
109  *
110  *   OUT        HANDLE                  retkey (returns 0 when failure)
111  *   IN         ACCESS_MASK             access
112  *   IN         POBJECT_ATTRIBUTES      attr
113  */
114 NTSTATUS WINAPI NtOpenKey( PHANDLE retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
115 {
116     NTSTATUS ret;
117     DWORD len = attr->ObjectName->Length;
118
119     TRACE( "(%p,%s,%x,%p)\n", attr->RootDirectory,
120            debugstr_us(attr->ObjectName), access, retkey );
121
122     if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
123     if (!retkey) return STATUS_INVALID_PARAMETER;
124
125     SERVER_START_REQ( open_key )
126     {
127         req->parent     = wine_server_obj_handle( attr->RootDirectory );
128         req->access     = access;
129         req->attributes = attr->Attributes;
130         wine_server_add_data( req, attr->ObjectName->Buffer, len );
131         ret = wine_server_call( req );
132         *retkey = wine_server_ptr_handle( reply->hkey );
133     }
134     SERVER_END_REQ;
135     TRACE("<- %p\n", *retkey);
136     return ret;
137 }
138
139 /******************************************************************************
140  * RtlpNtOpenKey [NTDLL.@]
141  *
142  * See NtOpenKey.
143  */
144 NTSTATUS WINAPI RtlpNtOpenKey( PHANDLE retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
145 {
146     if (attr)
147         attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
148     return NtOpenKey(retkey, access, attr);
149 }
150
151 /******************************************************************************
152  * NtDeleteKey [NTDLL.@]
153  * ZwDeleteKey [NTDLL.@]
154  */
155 NTSTATUS WINAPI NtDeleteKey( HANDLE hkey )
156 {
157     NTSTATUS ret;
158
159     TRACE( "(%p)\n", hkey );
160
161     SERVER_START_REQ( delete_key )
162     {
163         req->hkey = wine_server_obj_handle( hkey );
164         ret = wine_server_call( req );
165     }
166     SERVER_END_REQ;
167     return ret;
168 }
169
170 /******************************************************************************
171  * RtlpNtMakeTemporaryKey [NTDLL.@]
172  *
173  *  See NtDeleteKey.
174  */
175 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HANDLE hkey )
176 {
177     return NtDeleteKey(hkey);
178 }
179
180 /******************************************************************************
181  * NtDeleteValueKey [NTDLL.@]
182  * ZwDeleteValueKey [NTDLL.@]
183  */
184 NTSTATUS WINAPI NtDeleteValueKey( HANDLE hkey, const UNICODE_STRING *name )
185 {
186     NTSTATUS ret;
187
188     TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
189     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
190
191     SERVER_START_REQ( delete_key_value )
192     {
193         req->hkey = wine_server_obj_handle( hkey );
194         wine_server_add_data( req, name->Buffer, name->Length );
195         ret = wine_server_call( req );
196     }
197     SERVER_END_REQ;
198     return ret;
199 }
200
201
202 /******************************************************************************
203  *     enumerate_key
204  *
205  * Implementation of NtQueryKey and NtEnumerateKey
206  */
207 static NTSTATUS enumerate_key( HANDLE handle, int index, KEY_INFORMATION_CLASS info_class,
208                                void *info, DWORD length, DWORD *result_len )
209
210 {
211     NTSTATUS ret;
212     void *data_ptr;
213     size_t fixed_size;
214
215     switch(info_class)
216     {
217     case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
218     case KeyFullInformation:  data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
219     case KeyNodeInformation:  data_ptr = ((KEY_NODE_INFORMATION *)info)->Name;  break;
220     case KeyNameInformation:  data_ptr = ((KEY_NAME_INFORMATION *)info)->Name;  break;
221     default:
222         FIXME( "Information class %d not implemented\n", info_class );
223         return STATUS_INVALID_PARAMETER;
224     }
225     fixed_size = (char *)data_ptr - (char *)info;
226
227     SERVER_START_REQ( enum_key )
228     {
229         req->hkey       = wine_server_obj_handle( handle );
230         req->index      = index;
231         req->info_class = info_class;
232         if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
233         if (!(ret = wine_server_call( req )))
234         {
235             switch(info_class)
236             {
237             case KeyBasicInformation:
238                 {
239                     KEY_BASIC_INFORMATION keyinfo;
240                     fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
241                     keyinfo.LastWriteTime.QuadPart = reply->modif;
242                     keyinfo.TitleIndex = 0;
243                     keyinfo.NameLength = reply->namelen;
244                     memcpy( info, &keyinfo, min( length, fixed_size ) );
245                 }
246                 break;
247             case KeyFullInformation:
248                 {
249                     KEY_FULL_INFORMATION keyinfo;
250                     fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
251                     keyinfo.LastWriteTime.QuadPart = reply->modif;
252                     keyinfo.TitleIndex = 0;
253                     keyinfo.ClassLength = wine_server_reply_size(reply);
254                     keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
255                     keyinfo.SubKeys = reply->subkeys;
256                     keyinfo.MaxNameLen = reply->max_subkey;
257                     keyinfo.MaxClassLen = reply->max_class;
258                     keyinfo.Values = reply->values;
259                     keyinfo.MaxValueNameLen = reply->max_value;
260                     keyinfo.MaxValueDataLen = reply->max_data;
261                     memcpy( info, &keyinfo, min( length, fixed_size ) );
262                 }
263                 break;
264             case KeyNodeInformation:
265                 {
266                     KEY_NODE_INFORMATION keyinfo;
267                     fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
268                     keyinfo.LastWriteTime.QuadPart = reply->modif;
269                     keyinfo.TitleIndex = 0;
270                     if (reply->namelen < wine_server_reply_size(reply))
271                     {
272                         keyinfo.ClassLength = wine_server_reply_size(reply) - reply->namelen;
273                         keyinfo.ClassOffset = fixed_size + reply->namelen;
274                     }
275                     else
276                     {
277                         keyinfo.ClassLength = 0;
278                         keyinfo.ClassOffset = -1;
279                     }
280                     keyinfo.NameLength = reply->namelen;
281                     memcpy( info, &keyinfo, min( length, fixed_size ) );
282                 }
283                 break;
284             case KeyNameInformation:
285                 {
286                     KEY_NAME_INFORMATION keyinfo;
287                     fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
288                     keyinfo.NameLength = reply->namelen;
289                     memcpy( info, &keyinfo, min( length, fixed_size ) );
290                 }
291                 break;
292             }
293             *result_len = fixed_size + reply->total;
294             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
295         }
296     }
297     SERVER_END_REQ;
298     return ret;
299 }
300
301
302
303 /******************************************************************************
304  * NtEnumerateKey [NTDLL.@]
305  * ZwEnumerateKey [NTDLL.@]
306  *
307  * NOTES
308  *  the name copied into the buffer is NOT 0-terminated
309  */
310 NTSTATUS WINAPI NtEnumerateKey( HANDLE handle, ULONG index, KEY_INFORMATION_CLASS info_class,
311                                 void *info, DWORD length, DWORD *result_len )
312 {
313     /* -1 means query key, so avoid it here */
314     if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
315     return enumerate_key( handle, index, info_class, info, length, result_len );
316 }
317
318
319 /******************************************************************************
320  * RtlpNtEnumerateSubKey [NTDLL.@]
321  *
322  */
323 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HANDLE handle, UNICODE_STRING *out, ULONG index )
324 {
325   KEY_BASIC_INFORMATION *info;
326   DWORD dwLen, dwResultLen;
327   NTSTATUS ret;
328
329   if (out->Length)
330   {
331     dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
332     info = RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
333     if (!info)
334       return STATUS_NO_MEMORY;
335   }
336   else
337   {
338     dwLen = 0;
339     info = NULL;
340   }
341
342   ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
343   dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
344
345   if (ret == STATUS_BUFFER_OVERFLOW)
346     out->Length = dwResultLen;
347   else if (!ret)
348   {
349     if (out->Length < info->NameLength)
350     {
351       out->Length = dwResultLen;
352       ret = STATUS_BUFFER_OVERFLOW;
353     }
354     else
355     {
356       out->Length = info->NameLength;
357       memcpy(out->Buffer, info->Name, info->NameLength);
358     }
359   }
360
361   RtlFreeHeap( GetProcessHeap(), 0, info );
362   return ret;
363 }
364
365 /******************************************************************************
366  * NtQueryKey [NTDLL.@]
367  * ZwQueryKey [NTDLL.@]
368  */
369 NTSTATUS WINAPI NtQueryKey( HANDLE handle, KEY_INFORMATION_CLASS info_class,
370                             void *info, DWORD length, DWORD *result_len )
371 {
372     return enumerate_key( handle, -1, info_class, info, length, result_len );
373 }
374
375
376 /* fill the key value info structure for a specific info class */
377 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
378                                  DWORD length, int type, int name_len, int data_len )
379 {
380     switch(info_class)
381     {
382     case KeyValueBasicInformation:
383         {
384             KEY_VALUE_BASIC_INFORMATION keyinfo;
385             keyinfo.TitleIndex = 0;
386             keyinfo.Type       = type;
387             keyinfo.NameLength = name_len;
388             length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
389             memcpy( info, &keyinfo, length );
390             break;
391         }
392     case KeyValueFullInformation:
393         {
394             KEY_VALUE_FULL_INFORMATION keyinfo;
395             keyinfo.TitleIndex = 0;
396             keyinfo.Type       = type;
397             keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
398             keyinfo.DataLength = data_len;
399             keyinfo.NameLength = name_len;
400             length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
401             memcpy( info, &keyinfo, length );
402             break;
403         }
404     case KeyValuePartialInformation:
405         {
406             KEY_VALUE_PARTIAL_INFORMATION keyinfo;
407             keyinfo.TitleIndex = 0;
408             keyinfo.Type       = type;
409             keyinfo.DataLength = data_len;
410             length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
411             memcpy( info, &keyinfo, length );
412             break;
413         }
414     default:
415         break;
416     }
417 }
418
419
420 /******************************************************************************
421  *  NtEnumerateValueKey [NTDLL.@]
422  *  ZwEnumerateValueKey [NTDLL.@]
423  */
424 NTSTATUS WINAPI NtEnumerateValueKey( HANDLE handle, ULONG index,
425                                      KEY_VALUE_INFORMATION_CLASS info_class,
426                                      void *info, DWORD length, DWORD *result_len )
427 {
428     NTSTATUS ret;
429     void *ptr;
430     size_t fixed_size;
431
432     TRACE( "(%p,%u,%d,%p,%d)\n", handle, index, info_class, info, length );
433
434     /* compute the length we want to retrieve */
435     switch(info_class)
436     {
437     case KeyValueBasicInformation:   ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
438     case KeyValueFullInformation:    ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
439     case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
440     default:
441         FIXME( "Information class %d not implemented\n", info_class );
442         return STATUS_INVALID_PARAMETER;
443     }
444     fixed_size = (char *)ptr - (char *)info;
445
446     SERVER_START_REQ( enum_key_value )
447     {
448         req->hkey       = wine_server_obj_handle( handle );
449         req->index      = index;
450         req->info_class = info_class;
451         if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
452         if (!(ret = wine_server_call( req )))
453         {
454             copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
455                                  wine_server_reply_size(reply) - reply->namelen );
456             *result_len = fixed_size + reply->total;
457             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
458         }
459     }
460     SERVER_END_REQ;
461     return ret;
462 }
463
464
465 /******************************************************************************
466  * NtQueryValueKey [NTDLL.@]
467  * ZwQueryValueKey [NTDLL.@]
468  *
469  * NOTES
470  *  the name in the KeyValueInformation is never set
471  */
472 NTSTATUS WINAPI NtQueryValueKey( HANDLE handle, const UNICODE_STRING *name,
473                                  KEY_VALUE_INFORMATION_CLASS info_class,
474                                  void *info, DWORD length, DWORD *result_len )
475 {
476     NTSTATUS ret;
477     UCHAR *data_ptr;
478     unsigned int fixed_size = 0;
479
480     TRACE( "(%p,%s,%d,%p,%d)\n", handle, debugstr_us(name), info_class, info, length );
481
482     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
483
484     /* compute the length we want to retrieve */
485     switch(info_class)
486     {
487     case KeyValueBasicInformation:
488     {
489         KEY_VALUE_BASIC_INFORMATION *basic_info = info;
490         if (FIELD_OFFSET(KEY_VALUE_BASIC_INFORMATION, Name) < length)
491         {
492             memcpy(basic_info->Name, name->Buffer,
493                    min(length - FIELD_OFFSET(KEY_VALUE_BASIC_INFORMATION, Name), name->Length));
494         }
495         fixed_size = FIELD_OFFSET(KEY_VALUE_BASIC_INFORMATION, Name) + name->Length;
496         data_ptr = NULL;
497         break;
498     }
499     case KeyValueFullInformation:
500     {
501         KEY_VALUE_FULL_INFORMATION *full_info = info;
502         if (FIELD_OFFSET(KEY_VALUE_FULL_INFORMATION, Name) < length)
503         {
504             memcpy(full_info->Name, name->Buffer,
505                    min(length - FIELD_OFFSET(KEY_VALUE_FULL_INFORMATION, Name), name->Length));
506         }
507         data_ptr = (UCHAR *)full_info->Name + name->Length;
508         fixed_size = (char *)data_ptr - (char *)info;
509         break;
510     }
511     case KeyValuePartialInformation:
512         data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
513         fixed_size = (char *)data_ptr - (char *)info;
514         break;
515     default:
516         FIXME( "Information class %d not implemented\n", info_class );
517         return STATUS_INVALID_PARAMETER;
518     }
519
520     SERVER_START_REQ( get_key_value )
521     {
522         req->hkey = wine_server_obj_handle( handle );
523         wine_server_add_data( req, name->Buffer, name->Length );
524         if (length > fixed_size && data_ptr) wine_server_set_reply( req, data_ptr, length - fixed_size );
525         if (!(ret = wine_server_call( req )))
526         {
527             copy_key_value_info( info_class, info, length, reply->type,
528                                  name->Length, reply->total );
529             *result_len = fixed_size + (info_class == KeyValueBasicInformation ? 0 : reply->total);
530             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
531         }
532     }
533     SERVER_END_REQ;
534     return ret;
535 }
536
537 /******************************************************************************
538  * RtlpNtQueryValueKey [NTDLL.@]
539  *
540  */
541 NTSTATUS WINAPI RtlpNtQueryValueKey( HANDLE handle, ULONG *result_type, PBYTE dest,
542                                      DWORD *result_len, void *unknown )
543 {
544     KEY_VALUE_PARTIAL_INFORMATION *info;
545     UNICODE_STRING name;
546     NTSTATUS ret;
547     DWORD dwResultLen;
548     DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + (result_len ? *result_len : 0);
549
550     info = RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
551     if (!info)
552       return STATUS_NO_MEMORY;
553
554     name.Length = 0;
555     ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
556
557     if (!ret || ret == STATUS_BUFFER_OVERFLOW)
558     {
559         if (result_len)
560             *result_len = info->DataLength;
561
562         if (result_type)
563             *result_type = info->Type;
564
565         if (ret != STATUS_BUFFER_OVERFLOW)
566             memcpy( dest, info->Data, info->DataLength );
567     }
568
569     RtlFreeHeap( GetProcessHeap(), 0, info );
570     return ret;
571 }
572
573 /******************************************************************************
574  *  NtFlushKey  [NTDLL.@]
575  *  ZwFlushKey  [NTDLL.@]
576  */
577 NTSTATUS WINAPI NtFlushKey(HANDLE key)
578 {
579     NTSTATUS ret;
580
581     TRACE("key=%p\n", key);
582
583     SERVER_START_REQ( flush_key )
584     {
585         req->hkey = wine_server_obj_handle( key );
586         ret = wine_server_call( req );
587     }
588     SERVER_END_REQ;
589     
590     return ret;
591 }
592
593 /******************************************************************************
594  *  NtLoadKey   [NTDLL.@]
595  *  ZwLoadKey   [NTDLL.@]
596  */
597 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, OBJECT_ATTRIBUTES *file )
598 {
599     NTSTATUS ret;
600     HANDLE hive;
601     IO_STATUS_BLOCK io;
602
603     TRACE("(%p,%p)\n", attr, file);
604
605     ret = NtCreateFile(&hive, GENERIC_READ, file, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
606                        FILE_OPEN, 0, NULL, 0);
607     if (ret) return ret;
608
609     SERVER_START_REQ( load_registry )
610     {
611         req->hkey = wine_server_obj_handle( attr->RootDirectory );
612         req->file = wine_server_obj_handle( hive );
613         wine_server_add_data(req, attr->ObjectName->Buffer, attr->ObjectName->Length);
614         ret = wine_server_call( req );
615     }
616     SERVER_END_REQ;
617
618     NtClose(hive);
619    
620     return ret;
621 }
622
623 /******************************************************************************
624  *  NtNotifyChangeKey   [NTDLL.@]
625  *  ZwNotifyChangeKey   [NTDLL.@]
626  */
627 NTSTATUS WINAPI NtNotifyChangeKey(
628         IN HANDLE KeyHandle,
629         IN HANDLE Event,
630         IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
631         IN PVOID ApcContext OPTIONAL,
632         OUT PIO_STATUS_BLOCK IoStatusBlock,
633         IN ULONG CompletionFilter,
634         IN BOOLEAN Asynchronous,
635         OUT PVOID ChangeBuffer,
636         IN ULONG Length,
637         IN BOOLEAN WatchSubtree)
638 {
639     NTSTATUS ret;
640
641     TRACE("(%p,%p,%p,%p,%p,0x%08x, 0x%08x,%p,0x%08x,0x%08x)\n",
642         KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
643         Asynchronous, ChangeBuffer, Length, WatchSubtree);
644
645     if (ApcRoutine || ApcContext || ChangeBuffer || Length)
646         FIXME("Unimplemented optional parameter\n");
647
648     if (!Asynchronous)
649     {
650         OBJECT_ATTRIBUTES attr;
651         InitializeObjectAttributes( &attr, NULL, 0, NULL, NULL );
652         ret = NtCreateEvent( &Event, EVENT_ALL_ACCESS, &attr, FALSE, FALSE );
653         if (ret != STATUS_SUCCESS)
654             return ret;
655     }
656
657     SERVER_START_REQ( set_registry_notification )
658     {
659         req->hkey    = wine_server_obj_handle( KeyHandle );
660         req->event   = wine_server_obj_handle( Event );
661         req->subtree = WatchSubtree;
662         req->filter  = CompletionFilter;
663         ret = wine_server_call( req );
664     }
665     SERVER_END_REQ;
666  
667     if (!Asynchronous)
668     {
669         if (ret == STATUS_SUCCESS)
670             NtWaitForSingleObject( Event, FALSE, NULL );
671         NtClose( Event );
672     }
673
674     return STATUS_SUCCESS;
675 }
676
677 /******************************************************************************
678  * NtQueryMultipleValueKey [NTDLL]
679  * ZwQueryMultipleValueKey
680  */
681
682 NTSTATUS WINAPI NtQueryMultipleValueKey(
683         HANDLE KeyHandle,
684         PKEY_MULTIPLE_VALUE_INFORMATION ListOfValuesToQuery,
685         ULONG NumberOfItems,
686         PVOID MultipleValueInformation,
687         ULONG Length,
688         PULONG  ReturnLength)
689 {
690         FIXME("(%p,%p,0x%08x,%p,0x%08x,%p) stub!\n",
691         KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
692         Length,ReturnLength);
693         return STATUS_SUCCESS;
694 }
695
696 /******************************************************************************
697  * NtReplaceKey [NTDLL.@]
698  * ZwReplaceKey [NTDLL.@]
699  */
700 NTSTATUS WINAPI NtReplaceKey(
701         IN POBJECT_ATTRIBUTES ObjectAttributes,
702         IN HANDLE Key,
703         IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
704 {
705     FIXME("(%s,%p,%s),stub!\n", debugstr_ObjectAttributes(ObjectAttributes), Key,
706           debugstr_ObjectAttributes(ReplacedObjectAttributes) );
707     return STATUS_SUCCESS;
708 }
709 /******************************************************************************
710  * NtRestoreKey [NTDLL.@]
711  * ZwRestoreKey [NTDLL.@]
712  */
713 NTSTATUS WINAPI NtRestoreKey(
714         HANDLE KeyHandle,
715         HANDLE FileHandle,
716         ULONG RestoreFlags)
717 {
718         FIXME("(%p,%p,0x%08x) stub\n",
719         KeyHandle, FileHandle, RestoreFlags);
720         return STATUS_SUCCESS;
721 }
722 /******************************************************************************
723  * NtSaveKey [NTDLL.@]
724  * ZwSaveKey [NTDLL.@]
725  */
726 NTSTATUS WINAPI NtSaveKey(IN HANDLE KeyHandle, IN HANDLE FileHandle)
727 {
728     NTSTATUS ret;
729
730     TRACE("(%p,%p)\n", KeyHandle, FileHandle);
731
732     SERVER_START_REQ( save_registry )
733     {
734         req->hkey = wine_server_obj_handle( KeyHandle );
735         req->file = wine_server_obj_handle( FileHandle );
736         ret = wine_server_call( req );
737     }
738     SERVER_END_REQ;
739
740     return ret;
741 }
742 /******************************************************************************
743  * NtSetInformationKey [NTDLL.@]
744  * ZwSetInformationKey [NTDLL.@]
745  */
746 NTSTATUS WINAPI NtSetInformationKey(
747         IN HANDLE KeyHandle,
748         IN const int KeyInformationClass,
749         IN PVOID KeyInformation,
750         IN ULONG KeyInformationLength)
751 {
752         FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
753         KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
754         return STATUS_SUCCESS;
755 }
756
757
758 /******************************************************************************
759  * NtSetValueKey [NTDLL.@]
760  * ZwSetValueKey [NTDLL.@]
761  *
762  * NOTES
763  *   win95 does not care about count for REG_SZ and finds out the len by itself (js)
764  *   NT does definitely care (aj)
765  */
766 NTSTATUS WINAPI NtSetValueKey( HANDLE hkey, const UNICODE_STRING *name, ULONG TitleIndex,
767                                ULONG type, const void *data, ULONG count )
768 {
769     NTSTATUS ret;
770
771     TRACE( "(%p,%s,%d,%p,%d)\n", hkey, debugstr_us(name), type, data, count );
772
773     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
774
775     SERVER_START_REQ( set_key_value )
776     {
777         req->hkey    = wine_server_obj_handle( hkey );
778         req->type    = type;
779         req->namelen = name->Length;
780         wine_server_add_data( req, name->Buffer, name->Length );
781         wine_server_add_data( req, data, count );
782         ret = wine_server_call( req );
783     }
784     SERVER_END_REQ;
785     return ret;
786 }
787
788 /******************************************************************************
789  * RtlpNtSetValueKey [NTDLL.@]
790  *
791  */
792 NTSTATUS WINAPI RtlpNtSetValueKey( HANDLE hkey, ULONG type, const void *data,
793                                    ULONG count )
794 {
795     UNICODE_STRING name;
796
797     name.Length = 0;
798     return NtSetValueKey( hkey, &name, 0, type, data, count );
799 }
800
801 /******************************************************************************
802  * NtUnloadKey [NTDLL.@]
803  * ZwUnloadKey [NTDLL.@]
804  */
805 NTSTATUS WINAPI NtUnloadKey(IN POBJECT_ATTRIBUTES attr)
806 {
807     NTSTATUS ret;
808
809     TRACE("(%p)\n", attr);
810
811     SERVER_START_REQ( unload_registry )
812     {
813         req->hkey = wine_server_obj_handle( attr->RootDirectory );
814         ret = wine_server_call(req);
815     }
816     SERVER_END_REQ;
817
818     return ret;
819 }
820
821 /******************************************************************************
822  *  RtlFormatCurrentUserKeyPath         [NTDLL.@]
823  *
824  */
825 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
826 {
827     static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
828     HANDLE token;
829     NTSTATUS status;
830
831     status = NtOpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &token);
832     if (status == STATUS_NO_TOKEN)
833         status = NtOpenProcessToken(GetCurrentProcess(), TOKEN_READ, &token);
834     if (status == STATUS_SUCCESS)
835     {
836         char buffer[sizeof(TOKEN_USER) + sizeof(SID) + sizeof(DWORD)*SID_MAX_SUB_AUTHORITIES];
837         DWORD len = sizeof(buffer);
838
839         status = NtQueryInformationToken(token, TokenUser, buffer, len, &len);
840         if (status == STATUS_SUCCESS)
841         {
842             KeyPath->MaximumLength = 0;
843             status = RtlConvertSidToUnicodeString(KeyPath, ((TOKEN_USER *)buffer)->User.Sid, FALSE);
844             if (status == STATUS_BUFFER_OVERFLOW)
845             {
846                 PWCHAR buf = RtlAllocateHeap(GetProcessHeap(), 0,
847                                              sizeof(pathW) + KeyPath->Length + sizeof(WCHAR));
848                 if (buf)
849                 {
850                     memcpy(buf, pathW, sizeof(pathW));
851                     KeyPath->MaximumLength = KeyPath->Length + sizeof(WCHAR);
852                     KeyPath->Buffer = (PWCHAR)((LPBYTE)buf + sizeof(pathW));
853                     status = RtlConvertSidToUnicodeString(KeyPath,
854                                                           ((TOKEN_USER *)buffer)->User.Sid, FALSE);
855                     KeyPath->Buffer = buf;
856                     KeyPath->Length += sizeof(pathW);
857                     KeyPath->MaximumLength += sizeof(pathW);
858                 }
859                 else
860                     status = STATUS_NO_MEMORY;
861             }
862         }
863         NtClose(token);
864     }
865     return status;
866 }
867
868 /******************************************************************************
869  *  RtlOpenCurrentUser          [NTDLL.@]
870  *
871  * NOTES
872  *  If we return just HKEY_CURRENT_USER the advapi tries to find a remote
873  *  registry (odd handle) and fails.
874  */
875 NTSTATUS WINAPI RtlOpenCurrentUser(
876         IN ACCESS_MASK DesiredAccess, /* [in] */
877         OUT PHANDLE KeyHandle)        /* [out] handle of HKEY_CURRENT_USER */
878 {
879         OBJECT_ATTRIBUTES ObjectAttributes;
880         UNICODE_STRING ObjectName;
881         NTSTATUS ret;
882
883         TRACE("(0x%08x, %p)\n",DesiredAccess, KeyHandle);
884
885         if ((ret = RtlFormatCurrentUserKeyPath(&ObjectName))) return ret;
886         InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
887         ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
888         RtlFreeUnicodeString(&ObjectName);
889         return ret;
890 }
891
892
893 static NTSTATUS RTL_ReportRegistryValue(PKEY_VALUE_FULL_INFORMATION pInfo,
894                                         PRTL_QUERY_REGISTRY_TABLE pQuery, PVOID pContext, PVOID pEnvironment)
895 {
896     PUNICODE_STRING str;
897     UNICODE_STRING src, dst;
898     LONG *bin;
899     ULONG offset;
900     PWSTR wstr;
901     DWORD res;
902     NTSTATUS status = STATUS_SUCCESS;
903     ULONG len;
904     LPWSTR String;
905     ULONG count = 0;
906
907     if (pInfo == NULL)
908     {
909         if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
910             return STATUS_INVALID_PARAMETER;
911         else
912         {
913             status = pQuery->QueryRoutine(pQuery->Name, pQuery->DefaultType, pQuery->DefaultData,
914                                           pQuery->DefaultLength, pContext, pQuery->EntryContext);
915         }
916         return status;
917     }
918     len = pInfo->DataLength;
919
920     if (pQuery->Flags & RTL_QUERY_REGISTRY_DIRECT)
921     {
922         str = pQuery->EntryContext;
923
924         switch(pInfo->Type)
925         {
926         case REG_EXPAND_SZ:
927             if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
928             {
929                 RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
930                 res = 0;
931                 dst.MaximumLength = 0;
932                 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
933                 dst.Length = 0;
934                 dst.MaximumLength = res;
935                 dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
936                 RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
937                 status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
938                                      dst.Length, pContext, pQuery->EntryContext);
939                 RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
940             }
941
942         case REG_SZ:
943         case REG_LINK:
944             if (str->Buffer == NULL)
945                 RtlCreateUnicodeString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
946             else
947                 RtlAppendUnicodeToString(str, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
948             break;
949
950         case REG_MULTI_SZ:
951             if (!(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND))
952                 return STATUS_INVALID_PARAMETER;
953
954             if (str->Buffer == NULL)
955             {
956                 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
957                 str->MaximumLength = len;
958             }
959             len = min(len, str->MaximumLength);
960             memcpy(str->Buffer, ((CHAR*)pInfo) + pInfo->DataOffset, len);
961             str->Length = len;
962             break;
963
964         default:
965             bin = pQuery->EntryContext;
966             if (pInfo->DataLength <= sizeof(ULONG))
967                 memcpy(bin, ((CHAR*)pInfo) + pInfo->DataOffset,
968                     pInfo->DataLength);
969             else
970             {
971                 if (bin[0] <= sizeof(ULONG))
972                 {
973                     memcpy(&bin[1], ((CHAR*)pInfo) + pInfo->DataOffset,
974                     min(-bin[0], pInfo->DataLength));
975                 }
976                 else
977                 {
978                    len = min(bin[0], pInfo->DataLength);
979                     bin[1] = len;
980                     bin[2] = pInfo->Type;
981                     memcpy(&bin[3], ((CHAR*)pInfo) + pInfo->DataOffset, len);
982                 }
983            }
984            break;
985         }
986     }
987     else
988     {
989         if((pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND) ||
990            (pInfo->Type != REG_EXPAND_SZ && pInfo->Type != REG_MULTI_SZ))
991         {
992             status = pQuery->QueryRoutine(pInfo->Name, pInfo->Type,
993                 ((CHAR*)pInfo) + pInfo->DataOffset, pInfo->DataLength,
994                 pContext, pQuery->EntryContext);
995         }
996         else if (pInfo->Type == REG_EXPAND_SZ)
997         {
998             RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
999             res = 0;
1000             dst.MaximumLength = 0;
1001             RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1002             dst.Length = 0;
1003             dst.MaximumLength = res;
1004             dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
1005             RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1006             status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
1007                                           dst.Length, pContext, pQuery->EntryContext);
1008             RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
1009         }
1010         else /* REG_MULTI_SZ */
1011         {
1012             if(pQuery->Flags & RTL_QUERY_REGISTRY_NOEXPAND)
1013             {
1014                 for (offset = 0; offset <= pInfo->DataLength; offset += len + sizeof(WCHAR))
1015                     {
1016                     wstr = (WCHAR*)(((CHAR*)pInfo) + offset);
1017                     len = strlenW(wstr) * sizeof(WCHAR);
1018                     status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, wstr, len,
1019                         pContext, pQuery->EntryContext);
1020                     if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1021                         return status;
1022                     }
1023             }
1024             else
1025             {
1026                 while(count<=pInfo->DataLength)
1027                 {
1028                     String = (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset)+count;
1029                     count+=strlenW(String)+1;
1030                     RtlInitUnicodeString(&src, (WCHAR*)(((CHAR*)pInfo) + pInfo->DataOffset));
1031                     res = 0;
1032                     dst.MaximumLength = 0;
1033                     RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1034                     dst.Length = 0;
1035                     dst.MaximumLength = res;
1036                     dst.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, res * sizeof(WCHAR));
1037                     RtlExpandEnvironmentStrings_U(pEnvironment, &src, &dst, &res);
1038                     status = pQuery->QueryRoutine(pQuery->Name, pInfo->Type, dst.Buffer,
1039                                                   dst.Length, pContext, pQuery->EntryContext);
1040                     RtlFreeHeap(GetProcessHeap(), 0, dst.Buffer);
1041                     if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1042                         return status;
1043                 }
1044             }
1045         }
1046     }
1047     return status;
1048 }
1049
1050
1051 static NTSTATUS RTL_GetKeyHandle(ULONG RelativeTo, PCWSTR Path, PHANDLE handle)
1052 {
1053     UNICODE_STRING KeyString;
1054     OBJECT_ATTRIBUTES regkey;
1055     PCWSTR base;
1056     INT len;
1057     NTSTATUS status;
1058
1059     static const WCHAR empty[] = {0};
1060     static const WCHAR control[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e',
1061     '\\','S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1062     'C','o','n','t','r','o','l','\\',0};
1063
1064     static const WCHAR devicemap[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1065     'H','a','r','d','w','a','r','e','\\','D','e','v','i','c','e','M','a','p','\\',0};
1066
1067     static const WCHAR services[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1068     'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1069     'S','e','r','v','i','c','e','s','\\',0};
1070
1071     static const WCHAR user[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',
1072     'C','u','r','r','e','n','t','U','s','e','r','\\',0};
1073
1074     static const WCHAR windows_nt[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1075     'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1076     'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',0};
1077
1078     switch (RelativeTo & 0xff)
1079     {
1080     case RTL_REGISTRY_ABSOLUTE:
1081         base = empty;
1082         break;
1083
1084     case RTL_REGISTRY_CONTROL:
1085         base = control;
1086         break;
1087
1088     case RTL_REGISTRY_DEVICEMAP:
1089         base = devicemap;
1090         break;
1091
1092     case RTL_REGISTRY_SERVICES:
1093         base = services;
1094         break;
1095
1096     case RTL_REGISTRY_USER:
1097         base = user;
1098         break;
1099
1100     case RTL_REGISTRY_WINDOWS_NT:
1101         base = windows_nt;
1102         break;
1103
1104     default:
1105         return STATUS_INVALID_PARAMETER;
1106     }
1107
1108     len = (strlenW(base) + strlenW(Path) + 1) * sizeof(WCHAR);
1109     KeyString.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, len);
1110     if (KeyString.Buffer == NULL)
1111         return STATUS_NO_MEMORY;
1112
1113     strcpyW(KeyString.Buffer, base);
1114     strcatW(KeyString.Buffer, Path);
1115     KeyString.Length = len - sizeof(WCHAR);
1116     KeyString.MaximumLength = len;
1117     InitializeObjectAttributes(&regkey, &KeyString, OBJ_CASE_INSENSITIVE, NULL, NULL);
1118     status = NtOpenKey(handle, KEY_ALL_ACCESS, &regkey);
1119     RtlFreeHeap(GetProcessHeap(), 0, KeyString.Buffer);
1120     return status;
1121 }
1122
1123 /*************************************************************************
1124  * RtlQueryRegistryValues   [NTDLL.@]
1125  *
1126  * Query multiple registry values with a signle call.
1127  *
1128  * PARAMS
1129  *  RelativeTo  [I] Registry path that Path refers to
1130  *  Path        [I] Path to key
1131  *  QueryTable  [I] Table of key values to query
1132  *  Context     [I] Parameter to pass to the application defined QueryRoutine function
1133  *  Environment [I] Optional parameter to use when performing expansion
1134  *
1135  * RETURNS
1136  *  STATUS_SUCCESS or an appropriate NTSTATUS error code.
1137  */
1138 NTSTATUS WINAPI RtlQueryRegistryValues(IN ULONG RelativeTo, IN PCWSTR Path,
1139                                        IN PRTL_QUERY_REGISTRY_TABLE QueryTable, IN PVOID Context,
1140                                        IN PVOID Environment OPTIONAL)
1141 {
1142     UNICODE_STRING Value;
1143     HANDLE handle, topkey;
1144     PKEY_VALUE_FULL_INFORMATION pInfo = NULL;
1145     ULONG len, buflen = 0;
1146     NTSTATUS status=STATUS_SUCCESS, ret = STATUS_SUCCESS;
1147     INT i;
1148
1149     TRACE("(%d, %s, %p, %p, %p)\n", RelativeTo, debugstr_w(Path), QueryTable, Context, Environment);
1150
1151     if(Path == NULL)
1152         return STATUS_INVALID_PARAMETER;
1153
1154     /* get a valid handle */
1155     if (RelativeTo & RTL_REGISTRY_HANDLE)
1156         topkey = handle = (HANDLE)Path;
1157     else
1158     {
1159         status = RTL_GetKeyHandle(RelativeTo, Path, &topkey);
1160         handle = topkey;
1161     }
1162     if(status != STATUS_SUCCESS)
1163         return status;
1164
1165     /* Process query table entries */
1166     for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable)
1167     {
1168         if (QueryTable->Flags &
1169             (RTL_QUERY_REGISTRY_SUBKEY | RTL_QUERY_REGISTRY_TOPKEY))
1170         {
1171             /* topkey must be kept open just in case we will reuse it later */
1172             if (handle != topkey)
1173                 NtClose(handle);
1174
1175             if (QueryTable->Flags & RTL_QUERY_REGISTRY_SUBKEY)
1176             {
1177                 handle = 0;
1178                 status = RTL_GetKeyHandle(PtrToUlong(QueryTable->Name), Path, &handle);
1179                 if(status != STATUS_SUCCESS)
1180                 {
1181                     ret = status;
1182                     goto out;
1183                 }
1184             }
1185             else
1186                 handle = topkey;
1187         }
1188
1189         if (QueryTable->Flags & RTL_QUERY_REGISTRY_NOVALUE)
1190         {
1191             QueryTable->QueryRoutine(QueryTable->Name, REG_NONE, NULL, 0,
1192                 Context, QueryTable->EntryContext);
1193             continue;
1194         }
1195
1196         if (!handle)
1197         {
1198             if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1199             {
1200                 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1201                 goto out;
1202             }
1203             continue;
1204         }
1205
1206         if (QueryTable->Name == NULL)
1207         {
1208             if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT)
1209             {
1210                 ret = STATUS_INVALID_PARAMETER;
1211                 goto out;
1212             }
1213
1214             /* Report all subkeys */
1215             for (i = 0;; ++i)
1216             {
1217                 status = NtEnumerateValueKey(handle, i,
1218                     KeyValueFullInformation, pInfo, buflen, &len);
1219                 if (status == STATUS_NO_MORE_ENTRIES)
1220                     break;
1221                 if (status == STATUS_BUFFER_OVERFLOW ||
1222                     status == STATUS_BUFFER_TOO_SMALL)
1223                 {
1224                     buflen = len;
1225                     RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1226                     pInfo = RtlAllocateHeap(GetProcessHeap(), 0, buflen);
1227                     NtEnumerateValueKey(handle, i, KeyValueFullInformation,
1228                         pInfo, buflen, &len);
1229                 }
1230
1231                 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1232                 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1233                 {
1234                     ret = status;
1235                     goto out;
1236                 }
1237                 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1238                 {
1239                     RtlInitUnicodeString(&Value, pInfo->Name);
1240                     NtDeleteValueKey(handle, &Value);
1241                 }
1242             }
1243
1244             if (i == 0  && (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED))
1245             {
1246                 ret = STATUS_OBJECT_NAME_NOT_FOUND;
1247                 goto out;
1248             }
1249         }
1250         else
1251         {
1252             RtlInitUnicodeString(&Value, QueryTable->Name);
1253             status = NtQueryValueKey(handle, &Value, KeyValueFullInformation,
1254                 pInfo, buflen, &len);
1255             if (status == STATUS_BUFFER_OVERFLOW ||
1256                 status == STATUS_BUFFER_TOO_SMALL)
1257             {
1258                 buflen = len;
1259                 RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1260                 pInfo = RtlAllocateHeap(GetProcessHeap(), 0, buflen);
1261                 status = NtQueryValueKey(handle, &Value,
1262                     KeyValueFullInformation, pInfo, buflen, &len);
1263             }
1264             if (status != STATUS_SUCCESS)
1265             {
1266                 if (QueryTable->Flags & RTL_QUERY_REGISTRY_REQUIRED)
1267                 {
1268                     ret = STATUS_OBJECT_NAME_NOT_FOUND;
1269                     goto out;
1270                 }
1271                 status = RTL_ReportRegistryValue(NULL, QueryTable, Context, Environment);
1272                 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1273                 {
1274                     ret = status;
1275                     goto out;
1276                 }
1277             }
1278             else
1279             {
1280                 status = RTL_ReportRegistryValue(pInfo, QueryTable, Context, Environment);
1281                 if(status != STATUS_SUCCESS && status != STATUS_BUFFER_TOO_SMALL)
1282                 {
1283                     ret = status;
1284                     goto out;
1285                 }
1286                 if (QueryTable->Flags & RTL_QUERY_REGISTRY_DELETE)
1287                     NtDeleteValueKey(handle, &Value);
1288             }
1289         }
1290     }
1291
1292 out:
1293     RtlFreeHeap(GetProcessHeap(), 0, pInfo);
1294     if (handle != topkey)
1295         NtClose(handle);
1296     NtClose(topkey);
1297     return ret;
1298 }
1299
1300 /*************************************************************************
1301  * RtlCheckRegistryKey   [NTDLL.@]
1302  *
1303  * Query multiple registry values with a signle call.
1304  *
1305  * PARAMS
1306  *  RelativeTo [I] Registry path that Path refers to
1307  *  Path       [I] Path to key
1308  *
1309  * RETURNS
1310  *  STATUS_SUCCESS if the specified key exists, or an NTSTATUS error code.
1311  */
1312 NTSTATUS WINAPI RtlCheckRegistryKey(IN ULONG RelativeTo, IN PWSTR Path)
1313 {
1314     HANDLE handle;
1315     NTSTATUS status;
1316
1317     TRACE("(%d, %s)\n", RelativeTo, debugstr_w(Path));
1318
1319     if((!RelativeTo) && Path == NULL)
1320         return STATUS_OBJECT_PATH_SYNTAX_BAD;
1321     if(RelativeTo & RTL_REGISTRY_HANDLE)
1322         return STATUS_SUCCESS;
1323
1324     status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1325     if (handle) NtClose(handle);
1326     if (status == STATUS_INVALID_HANDLE) status = STATUS_OBJECT_NAME_NOT_FOUND;
1327     return status;
1328 }
1329
1330 /*************************************************************************
1331  * RtlDeleteRegistryValue   [NTDLL.@]
1332  *
1333  * Query multiple registry values with a signle call.
1334  *
1335  * PARAMS
1336  *  RelativeTo [I] Registry path that Path refers to
1337  *  Path       [I] Path to key
1338  *  ValueName  [I] Name of the value to delete
1339  *
1340  * RETURNS
1341  *  STATUS_SUCCESS if the specified key is successfully deleted, or an NTSTATUS error code.
1342  */
1343 NTSTATUS WINAPI RtlDeleteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName)
1344 {
1345     NTSTATUS status;
1346     HANDLE handle;
1347     UNICODE_STRING Value;
1348
1349     TRACE("(%d, %s, %s)\n", RelativeTo, debugstr_w(Path), debugstr_w(ValueName));
1350
1351     RtlInitUnicodeString(&Value, ValueName);
1352     if(RelativeTo == RTL_REGISTRY_HANDLE)
1353     {
1354         return NtDeleteValueKey((HANDLE)Path, &Value);
1355     }
1356     status = RTL_GetKeyHandle(RelativeTo, Path, &handle);
1357     if (status) return status;
1358     status = NtDeleteValueKey(handle, &Value);
1359     NtClose(handle);
1360     return status;
1361 }
1362
1363 /*************************************************************************
1364  * RtlWriteRegistryValue   [NTDLL.@]
1365  *
1366  * Sets the registry value with provided data.
1367  *
1368  * PARAMS
1369  *  RelativeTo [I] Registry path that path parameter refers to
1370  *  path       [I] Path to the key (or handle - see RTL_GetKeyHandle)
1371  *  name       [I] Name of the registry value to set
1372  *  type       [I] Type of the registry key to set
1373  *  data       [I] Pointer to the user data to be set
1374  *  length     [I] Length of the user data pointed by data
1375  *
1376  * RETURNS
1377  *  STATUS_SUCCESS if the specified key is successfully set,
1378  *  or an NTSTATUS error code.
1379  */
1380 NTSTATUS WINAPI RtlWriteRegistryValue( ULONG RelativeTo, PCWSTR path, PCWSTR name,
1381                                        ULONG type, PVOID data, ULONG length )
1382 {
1383     HANDLE hkey;
1384     NTSTATUS status;
1385     UNICODE_STRING str;
1386
1387     TRACE( "(%d, %s, %s) -> %d: %p [%d]\n", RelativeTo, debugstr_w(path), debugstr_w(name),
1388            type, data, length );
1389
1390     RtlInitUnicodeString( &str, name );
1391
1392     if (RelativeTo == RTL_REGISTRY_HANDLE)
1393         return NtSetValueKey( (HANDLE)path, &str, 0, type, data, length );
1394
1395     status = RTL_GetKeyHandle( RelativeTo, path, &hkey );
1396     if (status != STATUS_SUCCESS) return status;
1397
1398     status = NtSetValueKey( hkey, &str, 0, type, data, length );
1399     NtClose( hkey );
1400
1401     return status;
1402 }