Use the Unix codepage to convert the user name to Unicode.
[wine] / dlls / ntdll / reg.c
1 /*
2  * Registry functions
3  *
4  * Copyright (C) 1999 Juergen Schmied
5  * Copyright (C) 2000 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * NOTES:
22  *      HKEY_LOCAL_MACHINE      \\REGISTRY\\MACHINE
23  *      HKEY_USERS              \\REGISTRY\\USER
24  *      HKEY_CURRENT_CONFIG     \\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\HARDWARE PROFILES\\CURRENT
25   *     HKEY_CLASSES            \\REGISTRY\\MACHINE\\SOFTWARE\\CLASSES
26  */
27
28 #include "config.h"
29 #include "wine/port.h"
30
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "winerror.h"
36 #include "wine/unicode.h"
37 #include "wine/library.h"
38 #include "wine/server.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winreg.h"
42 #include "winternl.h"
43 #include "ntdll_misc.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(reg);
47
48 /* maximum length of a key/value name in bytes (without terminating null) */
49 #define MAX_NAME_LENGTH ((MAX_PATH-1) * sizeof(WCHAR))
50
51
52 /******************************************************************************
53  * NtCreateKey [NTDLL.@]
54  * ZwCreateKey [NTDLL.@]
55  */
56 NTSTATUS WINAPI NtCreateKey( PHKEY retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
57                              ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
58                              PULONG dispos )
59 {
60     NTSTATUS ret;
61
62     TRACE( "(%p,%s,%s,%lx,%lx,%p)\n", attr->RootDirectory, debugstr_us(attr->ObjectName),
63            debugstr_us(class), options, access, retkey );
64
65     if (attr->ObjectName->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
66     if (!retkey) return STATUS_INVALID_PARAMETER;
67
68     SERVER_START_REQ( create_key )
69     {
70         req->parent  = attr->RootDirectory;
71         req->access  = access;
72         req->options = options;
73         req->modif   = 0;
74         req->namelen = attr->ObjectName->Length;
75         wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
76         if (class) wine_server_add_data( req, class->Buffer, class->Length );
77         if (!(ret = wine_server_call( req )))
78         {
79             *retkey = reply->hkey;
80             if (dispos) *dispos = reply->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
81         }
82     }
83     SERVER_END_REQ;
84     TRACE("<- %p\n", *retkey);
85     return ret;
86 }
87
88 /******************************************************************************
89  *  RtlpNtCreateKey [NTDLL.@]
90  *
91  *  See NtCreateKey.
92  */
93 NTSTATUS WINAPI RtlpNtCreateKey( PHKEY retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
94                                  ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
95                                  PULONG dispos )
96 {
97     if (attr)
98         attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
99
100     return NtCreateKey(retkey, access, attr, 0, NULL, 0, dispos);
101 }
102
103 /******************************************************************************
104  * NtOpenKey [NTDLL.@]
105  * ZwOpenKey [NTDLL.@]
106  *
107  *   OUT        PHKEY                   retkey (returns 0 when failure)
108  *   IN         ACCESS_MASK             access
109  *   IN         POBJECT_ATTRIBUTES      attr
110  */
111 NTSTATUS WINAPI NtOpenKey( PHKEY retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
112 {
113     NTSTATUS ret;
114     DWORD len = attr->ObjectName->Length;
115
116     TRACE( "(%p,%s,%lx,%p)\n", attr->RootDirectory,
117            debugstr_us(attr->ObjectName), access, retkey );
118
119     if (len > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
120     if (!retkey) return STATUS_INVALID_PARAMETER;
121
122     SERVER_START_REQ( open_key )
123     {
124         req->parent = attr->RootDirectory;
125         req->access = access;
126         wine_server_add_data( req, attr->ObjectName->Buffer, len );
127         ret = wine_server_call( req );
128         *retkey = reply->hkey;
129     }
130     SERVER_END_REQ;
131     TRACE("<- %p\n", *retkey);
132     return ret;
133 }
134
135 /******************************************************************************
136  * RtlpNtOpenKey [NTDLL.@]
137  *
138  * See NtOpenKey.
139  */
140 NTSTATUS WINAPI RtlpNtOpenKey( PHKEY retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
141 {
142     if (attr)
143         attr->Attributes &= ~(OBJ_PERMANENT|OBJ_EXCLUSIVE);
144     return NtOpenKey(retkey, access, attr);
145 }
146
147 /******************************************************************************
148  * NtDeleteKey [NTDLL.@]
149  * ZwDeleteKey [NTDLL.@]
150  */
151 NTSTATUS WINAPI NtDeleteKey( HKEY hkey )
152 {
153     NTSTATUS ret;
154
155     TRACE( "(%p)\n", hkey );
156
157     SERVER_START_REQ( delete_key )
158     {
159         req->hkey = hkey;
160         ret = wine_server_call( req );
161     }
162     SERVER_END_REQ;
163     return ret;
164 }
165
166 /******************************************************************************
167  * RtlpNtMakeTemporaryKey [NTDLL.@]
168  *
169  *  See NtDeleteKey.
170  */
171 NTSTATUS WINAPI RtlpNtMakeTemporaryKey( HKEY hkey )
172 {
173     return NtDeleteKey(hkey);
174 }
175
176 /******************************************************************************
177  * NtDeleteValueKey [NTDLL.@]
178  * ZwDeleteValueKey [NTDLL.@]
179  */
180 NTSTATUS WINAPI NtDeleteValueKey( HKEY hkey, const UNICODE_STRING *name )
181 {
182     NTSTATUS ret;
183
184     TRACE( "(%p,%s)\n", hkey, debugstr_us(name) );
185     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
186
187     SERVER_START_REQ( delete_key_value )
188     {
189         req->hkey = hkey;
190         wine_server_add_data( req, name->Buffer, name->Length );
191         ret = wine_server_call( req );
192     }
193     SERVER_END_REQ;
194     return ret;
195 }
196
197
198 /******************************************************************************
199  *     enumerate_key
200  *
201  * Implementation of NtQueryKey and NtEnumerateKey
202  */
203 static NTSTATUS enumerate_key( HKEY handle, int index, KEY_INFORMATION_CLASS info_class,
204                                void *info, DWORD length, DWORD *result_len )
205
206 {
207     NTSTATUS ret;
208     void *data_ptr;
209     size_t fixed_size;
210
211     switch(info_class)
212     {
213     case KeyBasicInformation: data_ptr = ((KEY_BASIC_INFORMATION *)info)->Name; break;
214     case KeyFullInformation:  data_ptr = ((KEY_FULL_INFORMATION *)info)->Class; break;
215     case KeyNodeInformation:  data_ptr = ((KEY_NODE_INFORMATION *)info)->Name;  break;
216     default:
217         FIXME( "Information class %d not implemented\n", info_class );
218         return STATUS_INVALID_PARAMETER;
219     }
220     fixed_size = (char *)data_ptr - (char *)info;
221
222     SERVER_START_REQ( enum_key )
223     {
224         req->hkey       = handle;
225         req->index      = index;
226         req->info_class = info_class;
227         if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
228         if (!(ret = wine_server_call( req )))
229         {
230             LARGE_INTEGER modif;
231
232             RtlSecondsSince1970ToTime( reply->modif, &modif );
233
234             switch(info_class)
235             {
236             case KeyBasicInformation:
237                 {
238                     KEY_BASIC_INFORMATION keyinfo;
239                     fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
240                     keyinfo.LastWriteTime = modif;
241                     keyinfo.TitleIndex = 0;
242                     keyinfo.NameLength = reply->namelen;
243                     memcpy( info, &keyinfo, min( length, fixed_size ) );
244                 }
245                 break;
246             case KeyFullInformation:
247                 {
248                     KEY_FULL_INFORMATION keyinfo;
249                     fixed_size = (char *)keyinfo.Class - (char *)&keyinfo;
250                     keyinfo.LastWriteTime = modif;
251                     keyinfo.TitleIndex = 0;
252                     keyinfo.ClassLength = wine_server_reply_size(reply);
253                     keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size : -1;
254                     keyinfo.SubKeys = reply->subkeys;
255                     keyinfo.MaxNameLen = reply->max_subkey;
256                     keyinfo.MaxClassLen = reply->max_class;
257                     keyinfo.Values = reply->values;
258                     keyinfo.MaxValueNameLen = reply->max_value;
259                     keyinfo.MaxValueDataLen = reply->max_data;
260                     memcpy( info, &keyinfo, min( length, fixed_size ) );
261                 }
262                 break;
263             case KeyNodeInformation:
264                 {
265                     KEY_NODE_INFORMATION keyinfo;
266                     fixed_size = (char *)keyinfo.Name - (char *)&keyinfo;
267                     keyinfo.LastWriteTime = modif;
268                     keyinfo.TitleIndex = 0;
269                     keyinfo.ClassLength = max( 0, wine_server_reply_size(reply) - reply->namelen );
270                     keyinfo.ClassOffset = keyinfo.ClassLength ? fixed_size + reply->namelen : -1;
271                     keyinfo.NameLength = reply->namelen;
272                     memcpy( info, &keyinfo, min( length, fixed_size ) );
273                 }
274                 break;
275             }
276             *result_len = fixed_size + reply->total;
277             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
278         }
279     }
280     SERVER_END_REQ;
281     return ret;
282 }
283
284
285
286 /******************************************************************************
287  * NtEnumerateKey [NTDLL.@]
288  * ZwEnumerateKey [NTDLL.@]
289  *
290  * NOTES
291  *  the name copied into the buffer is NOT 0-terminated
292  */
293 NTSTATUS WINAPI NtEnumerateKey( HKEY handle, ULONG index, KEY_INFORMATION_CLASS info_class,
294                                 void *info, DWORD length, DWORD *result_len )
295 {
296     /* -1 means query key, so avoid it here */
297     if (index == (ULONG)-1) return STATUS_NO_MORE_ENTRIES;
298     return enumerate_key( handle, index, info_class, info, length, result_len );
299 }
300
301
302 /******************************************************************************
303  * RtlpNtEnumerateSubKey [NTDLL.@]
304  *
305  */
306 NTSTATUS WINAPI RtlpNtEnumerateSubKey( HKEY handle, UNICODE_STRING *out, ULONG index )
307 {
308   KEY_BASIC_INFORMATION *info;
309   DWORD dwLen, dwResultLen;
310   NTSTATUS ret;
311
312   if (out->Length)
313   {
314     dwLen = out->Length + sizeof(KEY_BASIC_INFORMATION);
315     info = (KEY_BASIC_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
316     if (!info)
317       return STATUS_NO_MEMORY;
318   }
319   else
320   {
321     dwLen = 0;
322     info = NULL;
323   }
324
325   ret = NtEnumerateKey( handle, index, KeyBasicInformation, info, dwLen, &dwResultLen );
326   dwResultLen -= sizeof(KEY_BASIC_INFORMATION);
327
328   if (ret == STATUS_BUFFER_OVERFLOW)
329     out->Length = dwResultLen;
330   else if (!ret)
331   {
332     if (out->Length < info->NameLength)
333     {
334       out->Length = dwResultLen;
335       ret = STATUS_BUFFER_OVERFLOW;
336     }
337     else
338     {
339       out->Length = info->NameLength;
340       memcpy(out->Buffer, info->Name, info->NameLength);
341     }
342   }
343
344   if (info)
345     RtlFreeHeap( GetProcessHeap(), 0, info );
346   return ret;
347 }
348
349 /******************************************************************************
350  * NtQueryKey [NTDLL.@]
351  * ZwQueryKey [NTDLL.@]
352  */
353 NTSTATUS WINAPI NtQueryKey( HKEY handle, KEY_INFORMATION_CLASS info_class,
354                             void *info, DWORD length, DWORD *result_len )
355 {
356     return enumerate_key( handle, -1, info_class, info, length, result_len );
357 }
358
359
360 /* fill the key value info structure for a specific info class */
361 static void copy_key_value_info( KEY_VALUE_INFORMATION_CLASS info_class, void *info,
362                                  DWORD length, int type, int name_len, int data_len )
363 {
364     switch(info_class)
365     {
366     case KeyValueBasicInformation:
367         {
368             KEY_VALUE_BASIC_INFORMATION keyinfo;
369             keyinfo.TitleIndex = 0;
370             keyinfo.Type       = type;
371             keyinfo.NameLength = name_len;
372             length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
373             memcpy( info, &keyinfo, length );
374             break;
375         }
376     case KeyValueFullInformation:
377         {
378             KEY_VALUE_FULL_INFORMATION keyinfo;
379             keyinfo.TitleIndex = 0;
380             keyinfo.Type       = type;
381             keyinfo.DataOffset = (char *)keyinfo.Name - (char *)&keyinfo + name_len;
382             keyinfo.DataLength = data_len;
383             keyinfo.NameLength = name_len;
384             length = min( length, (char *)keyinfo.Name - (char *)&keyinfo );
385             memcpy( info, &keyinfo, length );
386             break;
387         }
388     case KeyValuePartialInformation:
389         {
390             KEY_VALUE_PARTIAL_INFORMATION keyinfo;
391             keyinfo.TitleIndex = 0;
392             keyinfo.Type       = type;
393             keyinfo.DataLength = data_len;
394             length = min( length, (char *)keyinfo.Data - (char *)&keyinfo );
395             memcpy( info, &keyinfo, length );
396             break;
397         }
398     default:
399         break;
400     }
401 }
402
403
404 /******************************************************************************
405  *  NtEnumerateValueKey [NTDLL.@]
406  *  ZwEnumerateValueKey [NTDLL.@]
407  */
408 NTSTATUS WINAPI NtEnumerateValueKey( HKEY handle, ULONG index,
409                                      KEY_VALUE_INFORMATION_CLASS info_class,
410                                      void *info, DWORD length, DWORD *result_len )
411 {
412     NTSTATUS ret;
413     void *ptr;
414     size_t fixed_size;
415
416     TRACE( "(%p,%lu,%d,%p,%ld)\n", handle, index, info_class, info, length );
417
418     /* compute the length we want to retrieve */
419     switch(info_class)
420     {
421     case KeyValueBasicInformation:   ptr = ((KEY_VALUE_BASIC_INFORMATION *)info)->Name; break;
422     case KeyValueFullInformation:    ptr = ((KEY_VALUE_FULL_INFORMATION *)info)->Name; break;
423     case KeyValuePartialInformation: ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data; break;
424     default:
425         FIXME( "Information class %d not implemented\n", info_class );
426         return STATUS_INVALID_PARAMETER;
427     }
428     fixed_size = (char *)ptr - (char *)info;
429
430     SERVER_START_REQ( enum_key_value )
431     {
432         req->hkey       = handle;
433         req->index      = index;
434         req->info_class = info_class;
435         if (length > fixed_size) wine_server_set_reply( req, ptr, length - fixed_size );
436         if (!(ret = wine_server_call( req )))
437         {
438             copy_key_value_info( info_class, info, length, reply->type, reply->namelen,
439                                  wine_server_reply_size(reply) - reply->namelen );
440             *result_len = fixed_size + reply->total;
441             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
442         }
443     }
444     SERVER_END_REQ;
445     return ret;
446 }
447
448
449 /******************************************************************************
450  * NtQueryValueKey [NTDLL.@]
451  * ZwQueryValueKey [NTDLL.@]
452  *
453  * NOTES
454  *  the name in the KeyValueInformation is never set
455  */
456 NTSTATUS WINAPI NtQueryValueKey( HKEY handle, const UNICODE_STRING *name,
457                                  KEY_VALUE_INFORMATION_CLASS info_class,
458                                  void *info, DWORD length, DWORD *result_len )
459 {
460     NTSTATUS ret;
461     UCHAR *data_ptr;
462     int fixed_size = 0;
463
464     TRACE( "(%p,%s,%d,%p,%ld)\n", handle, debugstr_us(name), info_class, info, length );
465
466     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
467
468     /* compute the length we want to retrieve */
469     switch(info_class)
470     {
471     case KeyValueBasicInformation:
472         fixed_size = (char *)((KEY_VALUE_BASIC_INFORMATION *)info)->Name - (char *)info;
473         data_ptr = NULL;
474         break;
475     case KeyValueFullInformation:
476         data_ptr = (UCHAR *)((KEY_VALUE_FULL_INFORMATION *)info)->Name;
477         fixed_size = (char *)data_ptr - (char *)info;
478         break;
479     case KeyValuePartialInformation:
480         data_ptr = ((KEY_VALUE_PARTIAL_INFORMATION *)info)->Data;
481         fixed_size = (char *)data_ptr - (char *)info;
482         break;
483     default:
484         FIXME( "Information class %d not implemented\n", info_class );
485         return STATUS_INVALID_PARAMETER;
486     }
487
488     SERVER_START_REQ( get_key_value )
489     {
490         req->hkey = handle;
491         wine_server_add_data( req, name->Buffer, name->Length );
492         if (length > fixed_size) wine_server_set_reply( req, data_ptr, length - fixed_size );
493         if (!(ret = wine_server_call( req )))
494         {
495             copy_key_value_info( info_class, info, length, reply->type,
496                                  0, wine_server_reply_size(reply) );
497             *result_len = fixed_size + reply->total;
498             if (length < *result_len) ret = STATUS_BUFFER_OVERFLOW;
499         }
500     }
501     SERVER_END_REQ;
502     return ret;
503 }
504
505 /******************************************************************************
506  * RtlpNtQueryValueKey [NTDLL.@]
507  *
508  */
509 NTSTATUS WINAPI RtlpNtQueryValueKey( HKEY handle, ULONG *result_type, PBYTE dest,
510                                      DWORD *result_len )
511 {
512     KEY_VALUE_PARTIAL_INFORMATION *info;
513     UNICODE_STRING name;
514     NTSTATUS ret;
515     DWORD dwResultLen;
516     DWORD dwLen = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + result_len ? *result_len : 0;
517
518     info = (KEY_VALUE_PARTIAL_INFORMATION*)RtlAllocateHeap( GetProcessHeap(), 0, dwLen );
519     if (!info)
520       return STATUS_NO_MEMORY;
521
522     name.Length = 0;
523     ret = NtQueryValueKey( handle, &name, KeyValuePartialInformation, info, dwLen, &dwResultLen );
524
525     if (!ret || ret == STATUS_BUFFER_OVERFLOW)
526     {
527         if (result_len)
528             *result_len = info->DataLength;
529
530         if (result_type)
531             *result_type = info->Type;
532
533         if (ret != STATUS_BUFFER_OVERFLOW)
534             memcpy( dest, info->Data, info->DataLength );
535     }
536
537     RtlFreeHeap( GetProcessHeap(), 0, info );
538     return ret;
539 }
540
541 /******************************************************************************
542  *  NtFlushKey  [NTDLL.@]
543  *  ZwFlushKey  [NTDLL.@]
544  */
545 NTSTATUS WINAPI NtFlushKey(HKEY key)
546 {
547     NTSTATUS ret;
548
549     TRACE("key=%p\n", key);
550
551     SERVER_START_REQ( flush_key )
552     {
553         req->hkey = key;
554         ret = wine_server_call( req );
555     }
556     SERVER_END_REQ;
557     
558     return ret;
559 }
560
561 /******************************************************************************
562  *  NtLoadKey   [NTDLL.@]
563  *  ZwLoadKey   [NTDLL.@]
564  */
565 NTSTATUS WINAPI NtLoadKey( const OBJECT_ATTRIBUTES *attr, const OBJECT_ATTRIBUTES *file )
566 {
567     FIXME("stub!\n");
568     dump_ObjectAttributes(attr);
569     dump_ObjectAttributes(file);
570     return STATUS_SUCCESS;
571 }
572
573 /******************************************************************************
574  *  NtNotifyChangeKey   [NTDLL.@]
575  *  ZwNotifyChangeKey   [NTDLL.@]
576  */
577 NTSTATUS WINAPI NtNotifyChangeKey(
578         IN HKEY KeyHandle,
579         IN HANDLE Event,
580         IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
581         IN PVOID ApcContext OPTIONAL,
582         OUT PIO_STATUS_BLOCK IoStatusBlock,
583         IN ULONG CompletionFilter,
584         IN BOOLEAN Asynchroneous,
585         OUT PVOID ChangeBuffer,
586         IN ULONG Length,
587         IN BOOLEAN WatchSubtree)
588 {
589         FIXME("(%p,%p,%p,%p,%p,0x%08lx, 0x%08x,%p,0x%08lx,0x%08x) stub!\n",
590         KeyHandle, Event, ApcRoutine, ApcContext, IoStatusBlock, CompletionFilter,
591         Asynchroneous, ChangeBuffer, Length, WatchSubtree);
592         return STATUS_SUCCESS;
593 }
594
595 /******************************************************************************
596  * NtQueryMultipleValueKey [NTDLL]
597  * ZwQueryMultipleValueKey
598  */
599
600 NTSTATUS WINAPI NtQueryMultipleValueKey(
601         HKEY KeyHandle,
602         PVALENTW ListOfValuesToQuery,
603         ULONG NumberOfItems,
604         PVOID MultipleValueInformation,
605         ULONG Length,
606         PULONG  ReturnLength)
607 {
608         FIXME("(%p,%p,0x%08lx,%p,0x%08lx,%p) stub!\n",
609         KeyHandle, ListOfValuesToQuery, NumberOfItems, MultipleValueInformation,
610         Length,ReturnLength);
611         return STATUS_SUCCESS;
612 }
613
614 /******************************************************************************
615  * NtReplaceKey [NTDLL.@]
616  * ZwReplaceKey [NTDLL.@]
617  */
618 NTSTATUS WINAPI NtReplaceKey(
619         IN POBJECT_ATTRIBUTES ObjectAttributes,
620         IN HKEY Key,
621         IN POBJECT_ATTRIBUTES ReplacedObjectAttributes)
622 {
623         FIXME("(%p),stub!\n", Key);
624         dump_ObjectAttributes(ObjectAttributes);
625         dump_ObjectAttributes(ReplacedObjectAttributes);
626         return STATUS_SUCCESS;
627 }
628 /******************************************************************************
629  * NtRestoreKey [NTDLL.@]
630  * ZwRestoreKey [NTDLL.@]
631  */
632 NTSTATUS WINAPI NtRestoreKey(
633         HKEY KeyHandle,
634         HANDLE FileHandle,
635         ULONG RestoreFlags)
636 {
637         FIXME("(%p,%p,0x%08lx) stub\n",
638         KeyHandle, FileHandle, RestoreFlags);
639         return STATUS_SUCCESS;
640 }
641 /******************************************************************************
642  * NtSaveKey [NTDLL.@]
643  * ZwSaveKey [NTDLL.@]
644  */
645 NTSTATUS WINAPI NtSaveKey(
646         IN HKEY KeyHandle,
647         IN HANDLE FileHandle)
648 {
649         FIXME("(%p,%p) stub\n",
650         KeyHandle, FileHandle);
651         return STATUS_SUCCESS;
652 }
653 /******************************************************************************
654  * NtSetInformationKey [NTDLL.@]
655  * ZwSetInformationKey [NTDLL.@]
656  */
657 NTSTATUS WINAPI NtSetInformationKey(
658         IN HKEY KeyHandle,
659         IN const int KeyInformationClass,
660         IN PVOID KeyInformation,
661         IN ULONG KeyInformationLength)
662 {
663         FIXME("(%p,0x%08x,%p,0x%08lx) stub\n",
664         KeyHandle, KeyInformationClass, KeyInformation, KeyInformationLength);
665         return STATUS_SUCCESS;
666 }
667
668
669 /******************************************************************************
670  * NtSetValueKey [NTDLL.@]
671  * ZwSetValueKey [NTDLL.@]
672  *
673  * NOTES
674  *   win95 does not care about count for REG_SZ and finds out the len by itself (js)
675  *   NT does definitely care (aj)
676  */
677 NTSTATUS WINAPI NtSetValueKey( HKEY hkey, const UNICODE_STRING *name, ULONG TitleIndex,
678                                ULONG type, const void *data, ULONG count )
679 {
680     NTSTATUS ret;
681
682     TRACE( "(%p,%s,%ld,%p,%ld)\n", hkey, debugstr_us(name), type, data, count );
683
684     if (name->Length > MAX_NAME_LENGTH) return STATUS_BUFFER_OVERFLOW;
685
686     SERVER_START_REQ( set_key_value )
687     {
688         req->hkey    = hkey;
689         req->type    = type;
690         req->namelen = name->Length;
691         wine_server_add_data( req, name->Buffer, name->Length );
692         wine_server_add_data( req, data, count );
693         ret = wine_server_call( req );
694     }
695     SERVER_END_REQ;
696     return ret;
697 }
698
699 /******************************************************************************
700  * RtlpNtSetValueKey [NTDLL.@]
701  *
702  */
703 NTSTATUS WINAPI RtlpNtSetValueKey( HKEY hkey, ULONG type, const void *data,
704                                    ULONG count )
705 {
706     UNICODE_STRING name;
707
708     name.Length = 0;
709     return NtSetValueKey( hkey, &name, 0, type, data, count );
710 }
711
712 /******************************************************************************
713  * NtUnloadKey [NTDLL.@]
714  * ZwUnloadKey [NTDLL.@]
715  */
716 NTSTATUS WINAPI NtUnloadKey(
717         IN HKEY KeyHandle)
718 {
719         FIXME("(%p) stub\n",
720         KeyHandle);
721         return STATUS_SUCCESS;
722 }
723
724 /******************************************************************************
725  *  RtlFormatCurrentUserKeyPath         [NTDLL.@]
726  *
727  * NOTE: under NT the user name part of the path is an SID.
728  */
729 NTSTATUS WINAPI RtlFormatCurrentUserKeyPath( IN OUT PUNICODE_STRING KeyPath)
730 {
731     static const WCHAR pathW[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\'};
732     const char *user = wine_get_user_name();
733     int len = ntdll_umbstowcs( 0, user, strlen(user)+1, NULL, 0 );
734
735     KeyPath->MaximumLength = sizeof(pathW) + len * sizeof(WCHAR);
736     KeyPath->Length = KeyPath->MaximumLength - sizeof(WCHAR);
737     if (!(KeyPath->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, KeyPath->MaximumLength )))
738         return STATUS_NO_MEMORY;
739     memcpy( KeyPath->Buffer, pathW, sizeof(pathW) );
740     ntdll_umbstowcs( 0, user, strlen(user)+1, KeyPath->Buffer + sizeof(pathW)/sizeof(WCHAR), len );
741     return STATUS_SUCCESS;
742 }
743
744 /******************************************************************************
745  *  RtlOpenCurrentUser          [NTDLL.@]
746  *
747  * if we return just HKEY_CURRENT_USER the advapi tries to find a remote
748  * registry (odd handle) and fails
749  *
750  */
751 DWORD WINAPI RtlOpenCurrentUser(
752         IN ACCESS_MASK DesiredAccess, /* [in] */
753         OUT PHKEY KeyHandle)          /* [out] handle of HKEY_CURRENT_USER */
754 {
755         OBJECT_ATTRIBUTES ObjectAttributes;
756         UNICODE_STRING ObjectName;
757         NTSTATUS ret;
758
759         TRACE("(0x%08lx, %p) stub\n",DesiredAccess, KeyHandle);
760
761         RtlFormatCurrentUserKeyPath(&ObjectName);
762         InitializeObjectAttributes(&ObjectAttributes,&ObjectName,OBJ_CASE_INSENSITIVE,0, NULL);
763         ret = NtCreateKey(KeyHandle, DesiredAccess, &ObjectAttributes, 0, NULL, 0, NULL);
764         RtlFreeUnicodeString(&ObjectName);
765         return ret;
766 }