Implemented RegQueryMultipleValues.
[wine] / dlls / advapi32 / registry.c
1 /*
2  * Registry management
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * Based on misc/registry.c code
7  * Copyright (C) 1996 Marcus Meissner
8  * Copyright (C) 1998 Matthew Becker
9  * Copyright (C) 1999 Sylvain St-Germain
10  *
11  * This file is concerned about handle management and interaction with the Wine server.
12  * Registry file I/O is in misc/registry.c.
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <unistd.h>
32
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "winerror.h"
36 #include "wine/unicode.h"
37 #include "heap.h"
38 #include "wine/server.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(reg);
42
43
44 /* check if value type needs string conversion (Ansi<->Unicode) */
45 inline static int is_string( DWORD type )
46 {
47     return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
48 }
49
50 /* check if current version is NT or Win95 */
51 inline static int is_version_nt(void)
52 {
53     return !(GetVersion() & 0x80000000);
54 }
55
56 /* allowed bits for access mask */
57 #define KEY_ACCESS_MASK (KEY_ALL_ACCESS | MAXIMUM_ALLOWED)
58
59 /******************************************************************************
60  *           RegCreateKeyExW   [ADVAPI32.@]
61  *
62  * PARAMS
63  *    hkey       [I] Handle of an open key
64  *    name       [I] Address of subkey name
65  *    reserved   [I] Reserved - must be 0
66  *    class      [I] Address of class string
67  *    options    [I] Special options flag
68  *    access     [I] Desired security access
69  *    sa         [I] Address of key security structure
70  *    retkey     [O] Address of buffer for opened handle
71  *    dispos     [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
72  *
73  * NOTES
74  *  in case of failing retkey remains untouched
75  *
76  * FIXME MAXIMUM_ALLOWED in access mask not supported by server
77  */
78 DWORD WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
79                               DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
80                               LPHKEY retkey, LPDWORD dispos )
81 {
82     OBJECT_ATTRIBUTES attr;
83     UNICODE_STRING nameW, classW;
84
85     if (reserved) return ERROR_INVALID_PARAMETER;
86     if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
87
88     attr.Length = sizeof(attr);
89     attr.RootDirectory = hkey;
90     attr.ObjectName = &nameW;
91     attr.Attributes = 0;
92     attr.SecurityDescriptor = NULL;
93     attr.SecurityQualityOfService = NULL;
94     RtlInitUnicodeString( &nameW, name );
95     RtlInitUnicodeString( &classW, class );
96
97     return RtlNtStatusToDosError( NtCreateKey( retkey, access, &attr, 0,
98                                                &classW, options, dispos ) );
99 }
100
101
102 /******************************************************************************
103  *           RegCreateKeyExA   [ADVAPI32.@]
104  *
105  * FIXME MAXIMUM_ALLOWED in access mask not supported by server
106  */
107 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
108                               DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
109                               LPHKEY retkey, LPDWORD dispos )
110 {
111     OBJECT_ATTRIBUTES attr;
112     UNICODE_STRING classW;
113     ANSI_STRING nameA, classA;
114     NTSTATUS status;
115
116     if (reserved) return ERROR_INVALID_PARAMETER;
117     if (!is_version_nt()) access = KEY_ALL_ACCESS;  /* Win95 ignores the access mask */
118     else if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
119
120     attr.Length = sizeof(attr);
121     attr.RootDirectory = hkey;
122     attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
123     attr.Attributes = 0;
124     attr.SecurityDescriptor = NULL;
125     attr.SecurityQualityOfService = NULL;
126     RtlInitAnsiString( &nameA, name );
127     RtlInitAnsiString( &classA, class );
128
129     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
130                                                  &nameA, FALSE )))
131     {
132         if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
133         {
134             status = NtCreateKey( retkey, access, &attr, 0, &classW, options, dispos );
135             RtlFreeUnicodeString( &classW );
136         }
137     }
138     return RtlNtStatusToDosError( status );
139 }
140
141
142 /******************************************************************************
143  *           RegCreateKeyW   [ADVAPI32.@]
144  */
145 DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR name, LPHKEY retkey )
146 {
147     /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
148     /* but at least my version of NT (4.0 SP5) doesn't do this.  -- AJ */
149     return RegCreateKeyExW( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
150                             KEY_ALL_ACCESS, NULL, retkey, NULL );
151 }
152
153
154 /******************************************************************************
155  *           RegCreateKeyA   [ADVAPI32.@]
156  */
157 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
158 {
159     return RegCreateKeyExA( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
160                             KEY_ALL_ACCESS, NULL, retkey, NULL );
161 }
162
163
164
165 /******************************************************************************
166  *           RegOpenKeyExW   [ADVAPI32.@]
167  *
168  * Opens the specified key
169  *
170  * Unlike RegCreateKeyEx, this does not create the key if it does not exist.
171  *
172  * PARAMS
173  *    hkey       [I] Handle of open key
174  *    name       [I] Name of subkey to open
175  *    reserved   [I] Reserved - must be zero
176  *    access     [I] Security access mask
177  *    retkey     [O] Handle to open key
178  *
179  * RETURNS
180  *    Success: ERROR_SUCCESS
181  *    Failure: Error code
182  *
183  * NOTES
184  *  in case of failing is retkey = 0
185  */
186 DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
187 {
188     OBJECT_ATTRIBUTES attr;
189     UNICODE_STRING nameW;
190
191     attr.Length = sizeof(attr);
192     attr.RootDirectory = hkey;
193     attr.ObjectName = &nameW;
194     attr.Attributes = 0;
195     attr.SecurityDescriptor = NULL;
196     attr.SecurityQualityOfService = NULL;
197     RtlInitUnicodeString( &nameW, name );
198     return RtlNtStatusToDosError( NtOpenKey( retkey, access, &attr ) );
199 }
200
201
202 /******************************************************************************
203  *           RegOpenKeyExA   [ADVAPI32.@]
204  */
205 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
206 {
207     OBJECT_ATTRIBUTES attr;
208     STRING nameA;
209     NTSTATUS status;
210
211     if (!is_version_nt()) access = KEY_ALL_ACCESS;  /* Win95 ignores the access mask */
212
213     attr.Length = sizeof(attr);
214     attr.RootDirectory = hkey;
215     attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
216     attr.Attributes = 0;
217     attr.SecurityDescriptor = NULL;
218     attr.SecurityQualityOfService = NULL;
219
220     RtlInitAnsiString( &nameA, name );
221     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
222                                                  &nameA, FALSE )))
223     {
224         status = NtOpenKey( retkey, access, &attr );
225     }
226     return RtlNtStatusToDosError( status );
227 }
228
229
230 /******************************************************************************
231  *           RegOpenKeyW   [ADVAPI32.@]
232  *
233  * PARAMS
234  *    hkey    [I] Handle of open key
235  *    name    [I] Address of name of subkey to open
236  *    retkey  [O] Handle to open key
237  *
238  * RETURNS
239  *    Success: ERROR_SUCCESS
240  *    Failure: Error code
241  *
242  * NOTES
243  *  in case of failing is retkey = 0
244  */
245 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, LPHKEY retkey )
246 {
247     return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
248 }
249
250
251 /******************************************************************************
252  *           RegOpenKeyA   [ADVAPI32.@]
253  */
254 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
255 {
256     return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
257 }
258
259
260 /******************************************************************************
261  *           RegOpenCurrentUser   [ADVAPI32.@]
262  * FIXME: This function is supposed to retrieve a handle to the
263  * HKEY_CURRENT_USER for the user the current thread is impersonating.
264  * Since Wine does not currently allow threads to impersonate other users,
265  * this stub should work fine.
266  */
267 DWORD WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
268 {
269     return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
270 }
271
272
273
274 /******************************************************************************
275  *           RegEnumKeyExW   [ADVAPI32.@]
276  *
277  * PARAMS
278  *    hkey         [I] Handle to key to enumerate
279  *    index        [I] Index of subkey to enumerate
280  *    name         [O] Buffer for subkey name
281  *    name_len     [O] Size of subkey buffer
282  *    reserved     [I] Reserved
283  *    class        [O] Buffer for class string
284  *    class_len    [O] Size of class buffer
285  *    ft           [O] Time key last written to
286  */
287 DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
288                             LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
289 {
290     NTSTATUS status;
291     char buffer[256], *buf_ptr = buffer;
292     KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
293     DWORD total_size;
294
295     TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
296            name_len ? *name_len : -1, reserved, class, class_len, ft );
297
298     if (reserved) return ERROR_INVALID_PARAMETER;
299
300     if (!hkey) return ERROR_INVALID_HANDLE;
301
302     status = NtEnumerateKey( hkey, index, KeyNodeInformation,
303                              buffer, sizeof(buffer), &total_size );
304
305     while (status == STATUS_BUFFER_OVERFLOW)
306     {
307         /* retry with a dynamically allocated buffer */
308         if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
309         if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
310             return ERROR_NOT_ENOUGH_MEMORY;
311         info = (KEY_NODE_INFORMATION *)buf_ptr;
312         status = NtEnumerateKey( hkey, index, KeyNodeInformation,
313                                  buf_ptr, total_size, &total_size );
314     }
315
316     if (!status)
317     {
318         DWORD len = info->NameLength / sizeof(WCHAR);
319         DWORD cls_len = info->ClassLength / sizeof(WCHAR);
320
321         if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
322
323         if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
324             status = STATUS_BUFFER_OVERFLOW;
325         else
326         {
327             *name_len = len;
328             memcpy( name, info->Name, info->NameLength );
329             name[len] = 0;
330             if (class_len)
331             {
332                 *class_len = cls_len;
333                 if (class)
334                 {
335                     memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
336                     class[cls_len] = 0;
337                 }
338             }
339         }
340     }
341
342     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
343     return RtlNtStatusToDosError( status );
344 }
345
346
347 /******************************************************************************
348  *           RegEnumKeyExA   [ADVAPI32.@]
349  */
350 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
351                             LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
352 {
353     NTSTATUS status;
354     char buffer[256], *buf_ptr = buffer;
355     KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
356     DWORD total_size;
357
358     TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
359            name_len ? *name_len : -1, reserved, class, class_len, ft );
360
361     if (reserved) return ERROR_INVALID_PARAMETER;
362
363     if (!hkey) return ERROR_INVALID_HANDLE;
364
365     status = NtEnumerateKey( hkey, index, KeyNodeInformation,
366                              buffer, sizeof(buffer), &total_size );
367
368     while (status == STATUS_BUFFER_OVERFLOW)
369     {
370         /* retry with a dynamically allocated buffer */
371         if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
372         if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
373             return ERROR_NOT_ENOUGH_MEMORY;
374         info = (KEY_NODE_INFORMATION *)buf_ptr;
375         status = NtEnumerateKey( hkey, index, KeyNodeInformation,
376                                  buf_ptr, total_size, &total_size );
377     }
378
379     if (!status)
380     {
381         DWORD len, cls_len;
382
383         RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
384         RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
385                                    info->ClassLength );
386         if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
387
388         if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
389             status = STATUS_BUFFER_OVERFLOW;
390         else
391         {
392             *name_len = len;
393             RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
394             name[len] = 0;
395             if (class_len)
396             {
397                 *class_len = cls_len;
398                 if (class)
399                 {
400                     RtlUnicodeToMultiByteN( class, cls_len, NULL,
401                                             (WCHAR *)(buf_ptr + info->ClassOffset),
402                                             info->ClassLength );
403                     class[cls_len] = 0;
404                 }
405             }
406         }
407     }
408
409     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
410     return RtlNtStatusToDosError( status );
411 }
412
413
414 /******************************************************************************
415  *           RegEnumKeyW   [ADVAPI32.@]
416  */
417 DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
418 {
419     return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
420 }
421
422
423 /******************************************************************************
424  *           RegEnumKeyA   [ADVAPI32.@]
425  */
426 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
427 {
428     return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
429 }
430
431
432 /******************************************************************************
433  *           RegQueryInfoKeyW   [ADVAPI32.@]
434  *
435  * PARAMS
436  *    hkey       [I] Handle to key to query
437  *    class      [O] Buffer for class string
438  *    class_len  [O] Size of class string buffer
439  *    reserved   [I] Reserved
440  *    subkeys    [O] Buffer for number of subkeys
441  *    max_subkey [O] Buffer for longest subkey name length
442  *    max_class  [O] Buffer for longest class string length
443  *    values     [O] Buffer for number of value entries
444  *    max_value  [O] Buffer for longest value name length
445  *    max_data   [O] Buffer for longest value data length
446  *    security   [O] Buffer for security descriptor length
447  *    modif      [O] Modification time
448  *
449  * - win95 allows class to be valid and class_len to be NULL
450  * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
451  * - both allow class to be NULL and class_len to be NULL
452  * (it's hard to test validity, so test !NULL instead)
453  */
454 DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
455                                LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
456                                LPDWORD values, LPDWORD max_value, LPDWORD max_data,
457                                LPDWORD security, FILETIME *modif )
458 {
459     NTSTATUS status;
460     char buffer[256], *buf_ptr = buffer;
461     KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
462     DWORD total_size;
463
464     TRACE( "(0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
465            reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
466
467     if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
468
469     status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
470     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
471
472     if (class)
473     {
474         /* retry with a dynamically allocated buffer */
475         while (status == STATUS_BUFFER_OVERFLOW)
476         {
477             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
478             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
479                 return ERROR_NOT_ENOUGH_MEMORY;
480             info = (KEY_FULL_INFORMATION *)buf_ptr;
481             status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
482         }
483
484         if (status) goto done;
485
486         if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
487         {
488             status = STATUS_BUFFER_OVERFLOW;
489         }
490         else
491         {
492             memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
493             class[info->ClassLength/sizeof(WCHAR)] = 0;
494         }
495     }
496     else status = STATUS_SUCCESS;
497
498     if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
499     if (subkeys) *subkeys = info->SubKeys;
500     if (max_subkey) *max_subkey = info->MaxNameLen;
501     if (max_class) *max_class = info->MaxClassLen;
502     if (values) *values = info->Values;
503     if (max_value) *max_value = info->MaxValueNameLen;
504     if (max_data) *max_data = info->MaxValueDataLen;
505     if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
506
507  done:
508     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
509     return RtlNtStatusToDosError( status );
510 }
511
512
513 /******************************************************************************
514  *           RegQueryMultipleValuesA   [ADVAPI32.@]
515  */
516 DWORD WINAPI RegQueryMultipleValuesA(HKEY hkey, PVALENTA val_list, DWORD num_vals,
517                                      LPSTR lpValueBuf, LPDWORD ldwTotsize)
518 {
519     int i;
520     DWORD maxBytes = *ldwTotsize;
521     HRESULT status;
522     LPSTR bufptr = lpValueBuf;
523     *ldwTotsize = 0;
524
525     TRACE("(%x,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
526
527     for(i=0; i < num_vals; ++i)
528     {
529
530         val_list[i].ve_valuelen=0;
531         status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
532         if(status != ERROR_SUCCESS)
533         {
534             return status;
535         }
536
537         if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
538         {
539             status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
540                                       bufptr, &val_list[i].ve_valuelen);
541             if(status != ERROR_SUCCESS)
542             {
543                 return status;
544             }
545
546             val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
547
548             bufptr += val_list[i].ve_valuelen;
549         }
550
551         *ldwTotsize += val_list[i].ve_valuelen;
552     }
553     return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
554 }
555
556
557 /******************************************************************************
558  *           RegQueryMultipleValuesW   [ADVAPI32.@]
559  */
560 DWORD WINAPI RegQueryMultipleValuesW(HKEY hkey, PVALENTW val_list, DWORD num_vals,
561                                      LPWSTR lpValueBuf, LPDWORD ldwTotsize)
562 {
563     int i;
564     DWORD maxBytes = *ldwTotsize;
565     HRESULT status;
566     LPSTR bufptr = (LPSTR)lpValueBuf;
567     *ldwTotsize = 0;
568
569     TRACE("(%x,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
570
571     for(i=0; i < num_vals; ++i)
572     {
573         val_list[i].ve_valuelen=0;
574         status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
575         if(status != ERROR_SUCCESS)
576         {
577             return status;
578         }
579
580         if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
581         {
582             status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
583                                       bufptr, &val_list[i].ve_valuelen);
584             if(status != ERROR_SUCCESS)
585             {
586                 return status;
587             }
588
589             val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
590
591             bufptr += val_list[i].ve_valuelen;
592         }
593
594         *ldwTotsize += val_list[i].ve_valuelen;
595     }
596     return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
597 }
598
599 /******************************************************************************
600  *           RegQueryInfoKeyA   [ADVAPI32.@]
601  */
602 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
603                                LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
604                                LPDWORD values, LPDWORD max_value, LPDWORD max_data,
605                                LPDWORD security, FILETIME *modif )
606 {
607     NTSTATUS status;
608     char buffer[256], *buf_ptr = buffer;
609     KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
610     DWORD total_size, len;
611
612     TRACE( "(0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
613            reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
614
615     if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
616
617     status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
618     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
619
620     if (class || class_len)
621     {
622         /* retry with a dynamically allocated buffer */
623         while (status == STATUS_BUFFER_OVERFLOW)
624         {
625             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
626             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
627                 return ERROR_NOT_ENOUGH_MEMORY;
628             info = (KEY_FULL_INFORMATION *)buf_ptr;
629             status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
630         }
631
632         if (status) goto done;
633
634         RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
635         if (class_len)
636         {
637             if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
638             *class_len = len;
639         }
640         if (class && !status)
641         {
642             RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
643                                     info->ClassLength );
644             class[len] = 0;
645         }
646     }
647     else status = STATUS_SUCCESS;
648
649     if (subkeys) *subkeys = info->SubKeys;
650     if (max_subkey) *max_subkey = info->MaxNameLen;
651     if (max_class) *max_class = info->MaxClassLen;
652     if (values) *values = info->Values;
653     if (max_value) *max_value = info->MaxValueNameLen;
654     if (max_data) *max_data = info->MaxValueDataLen;
655     if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
656
657  done:
658     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
659     return RtlNtStatusToDosError( status );
660 }
661
662
663 /******************************************************************************
664  *           RegCloseKey   [ADVAPI32.@]
665  *
666  * Releases the handle of the specified key
667  *
668  * PARAMS
669  *    hkey [I] Handle of key to close
670  *
671  * RETURNS
672  *    Success: ERROR_SUCCESS
673  *    Failure: Error code
674  */
675 DWORD WINAPI RegCloseKey( HKEY hkey )
676 {
677     if (!hkey || hkey >= 0x80000000) return ERROR_SUCCESS;
678     return RtlNtStatusToDosError( NtClose( hkey ) );
679 }
680
681
682 /******************************************************************************
683  *           RegDeleteKeyW   [ADVAPI32.@]
684  *
685  * PARAMS
686  *    hkey   [I] Handle to open key
687  *    name   [I] Name of subkey to delete
688  *
689  * RETURNS
690  *    Success: ERROR_SUCCESS
691  *    Failure: Error code
692  */
693 DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
694 {
695     DWORD ret;
696     HKEY tmp;
697
698     if (!name || !*name)
699     {
700         ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
701     }
702     else if (!(ret = RegOpenKeyExW( hkey, name, 0, 0, &tmp )))
703     {
704         if (!is_version_nt()) /* win95 does recursive key deletes */
705         {
706             WCHAR name[MAX_PATH];
707
708             while(!RegEnumKeyW(tmp, 0, name, sizeof name))
709             {
710                 if(RegDeleteKeyW(tmp, name))  /* recurse */
711                     break;
712             }
713         }
714         ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
715         RegCloseKey( tmp );
716     }
717     TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
718     return ret;
719 }
720
721
722 /******************************************************************************
723  *           RegDeleteKeyA   [ADVAPI32.@]
724  */
725 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
726 {
727     DWORD ret;
728     HKEY tmp;
729
730     if (!name || !*name)
731     {
732         ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
733     }
734     else if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, &tmp )))
735     {
736         if (!is_version_nt()) /* win95 does recursive key deletes */
737         {
738             CHAR name[MAX_PATH];
739
740             while(!RegEnumKeyA(tmp, 0, name, sizeof name))
741             {
742                 if(RegDeleteKeyA(tmp, name))  /* recurse */
743                     break;
744             }
745         }
746         ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
747         RegCloseKey( tmp );
748     }
749     TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
750     return ret;
751 }
752
753
754
755 /******************************************************************************
756  *           RegSetValueExW   [ADVAPI32.@]
757  *
758  * Sets the data and type of a value under a register key
759  *
760  * PARAMS
761  *    hkey       [I] Handle of key to set value for
762  *    name       [I] Name of value to set
763  *    reserved   [I] Reserved - must be zero
764  *    type       [I] Flag for value type
765  *    data       [I] Address of value data
766  *    count      [I] Size of value data
767  *
768  * RETURNS
769  *    Success: ERROR_SUCCESS
770  *    Failure: Error code
771  *
772  * NOTES
773  *   win95 does not care about count for REG_SZ and finds out the len by itself (js)
774  *   NT does definitely care (aj)
775  */
776 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
777                              DWORD type, CONST BYTE *data, DWORD count )
778 {
779     UNICODE_STRING nameW;
780
781     if (!is_version_nt())  /* win95 */
782     {
783         if (type == REG_SZ) count = (strlenW( (WCHAR *)data ) + 1) * sizeof(WCHAR);
784     }
785     else if (count && is_string(type))
786     {
787         LPCWSTR str = (LPCWSTR)data;
788         /* if user forgot to count terminating null, add it (yes NT does this) */
789         if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
790             count += sizeof(WCHAR);
791     }
792
793     RtlInitUnicodeString( &nameW, name );
794     return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
795 }
796
797
798 /******************************************************************************
799  *           RegSetValueExA   [ADVAPI32.@]
800  */
801 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
802                              CONST BYTE *data, DWORD count )
803 {
804     ANSI_STRING nameA;
805     WCHAR *dataW = NULL;
806     NTSTATUS status;
807
808     if (!is_version_nt())  /* win95 */
809     {
810         if (type == REG_SZ) count = strlen(data) + 1;
811     }
812     else if (count && is_string(type))
813     {
814         /* if user forgot to count terminating null, add it (yes NT does this) */
815         if (data[count-1] && !data[count]) count++;
816     }
817
818     if (is_string( type )) /* need to convert to Unicode */
819     {
820         DWORD lenW;
821         RtlMultiByteToUnicodeSize( &lenW, data, count );
822         if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
823         RtlMultiByteToUnicodeN( dataW, lenW, NULL, data, count );
824         count = lenW;
825         data = (BYTE *)dataW;
826     }
827
828     RtlInitAnsiString( &nameA, name );
829     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
830                                                  &nameA, FALSE )))
831     {
832         status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
833     }
834     if (dataW) HeapFree( GetProcessHeap(), 0, dataW );
835     return RtlNtStatusToDosError( status );
836 }
837
838
839 /******************************************************************************
840  *           RegSetValueW   [ADVAPI32.@]
841  */
842 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
843 {
844     HKEY subkey = hkey;
845     DWORD ret;
846
847     TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
848
849     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
850
851     if (name && name[0])  /* need to create the subkey */
852     {
853         if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
854     }
855
856     ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (LPBYTE)data,
857                           (strlenW( data ) + 1) * sizeof(WCHAR) );
858     if (subkey != hkey) RegCloseKey( subkey );
859     return ret;
860 }
861
862
863 /******************************************************************************
864  *           RegSetValueA   [ADVAPI32.@]
865  */
866 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
867 {
868     HKEY subkey = hkey;
869     DWORD ret;
870
871     TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
872
873     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
874
875     if (name && name[0])  /* need to create the subkey */
876     {
877         if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
878     }
879     ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
880     if (subkey != hkey) RegCloseKey( subkey );
881     return ret;
882 }
883
884
885
886 /******************************************************************************
887  *           RegQueryValueExW   [ADVAPI32.@]
888  *
889  * Retrieves type and data for a specified name associated with an open key
890  *
891  * PARAMS
892  *    hkey      [I]   Handle of key to query
893  *    name      [I]   Name of value to query
894  *    reserved  [I]   Reserved - must be NULL
895  *    type      [O]   Address of buffer for value type.  If NULL, the type
896  *                        is not required.
897  *    data      [O]   Address of data buffer.  If NULL, the actual data is
898  *                        not required.
899  *    count     [I/O] Address of data buffer size
900  *
901  * RETURNS
902  *    ERROR_SUCCESS:   Success
903  *    ERROR_MORE_DATA: !!! if the specified buffer is not big enough to hold the data
904  *                     buffer is left untouched. The MS-documentation is wrong (js) !!!
905  */
906 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
907                                LPBYTE data, LPDWORD count )
908 {
909     NTSTATUS status;
910     UNICODE_STRING name_str;
911     DWORD total_size;
912     char buffer[256], *buf_ptr = buffer;
913     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
914     static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
915
916     TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
917           hkey, debugstr_w(name), reserved, type, data, count, count ? *count : 0 );
918
919     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
920
921     RtlInitUnicodeString( &name_str, name );
922
923     if (data) total_size = min( sizeof(buffer), *count + info_size );
924     else total_size = info_size;
925
926     status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
927                               buffer, total_size, &total_size );
928     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
929
930     if (data)
931     {
932         /* retry with a dynamically allocated buffer */
933         while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
934         {
935             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
936             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
937                 return ERROR_NOT_ENOUGH_MEMORY;
938             info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
939             status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
940                                       buf_ptr, total_size, &total_size );
941         }
942
943         if (!status)
944         {
945             memcpy( data, buf_ptr + info_size, total_size - info_size );
946             /* if the type is REG_SZ and data is not 0-terminated
947              * and there is enough space in the buffer NT appends a \0 */
948             if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
949             {
950                 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
951                 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
952             }
953         }
954         else if (status != STATUS_BUFFER_OVERFLOW) goto done;
955     }
956     else status = STATUS_SUCCESS;
957
958     if (type) *type = info->Type;
959     if (count) *count = total_size - info_size;
960
961  done:
962     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
963     return RtlNtStatusToDosError(status);
964 }
965
966
967 /******************************************************************************
968  *           RegQueryValueExA   [ADVAPI32.@]
969  *
970  * NOTES:
971  * the documentation is wrong: if the buffer is too small it remains untouched
972  */
973 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
974                                LPBYTE data, LPDWORD count )
975 {
976     NTSTATUS status;
977     ANSI_STRING nameA;
978     DWORD total_size;
979     char buffer[256], *buf_ptr = buffer;
980     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
981     static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
982
983     TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
984           hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
985
986     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
987
988     RtlInitAnsiString( &nameA, name );
989     if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
990                                                 &nameA, FALSE )))
991         return RtlNtStatusToDosError(status);
992
993     status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
994                               KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
995     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
996
997     /* we need to fetch the contents for a string type even if not requested,
998      * because we need to compute the length of the ASCII string. */
999     if (data || is_string(info->Type))
1000     {
1001         /* retry with a dynamically allocated buffer */
1002         while (status == STATUS_BUFFER_OVERFLOW)
1003         {
1004             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1005             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1006             {
1007                 status = STATUS_NO_MEMORY;
1008                 goto done;
1009             }
1010             info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1011             status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1012                                     KeyValuePartialInformation, buf_ptr, total_size, &total_size );
1013         }
1014
1015         if (status) goto done;
1016
1017         if (is_string(info->Type))
1018         {
1019             DWORD len;
1020
1021             RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1022                                        total_size - info_size );
1023             if (data && len)
1024             {
1025                 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1026                 else
1027                 {
1028                     RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1029                                             total_size - info_size );
1030                     /* if the type is REG_SZ and data is not 0-terminated
1031                      * and there is enough space in the buffer NT appends a \0 */
1032                     if (len < *count && data[len-1]) data[len] = 0;
1033                 }
1034             }
1035             total_size = len + info_size;
1036         }
1037         else if (data)
1038         {
1039             if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1040             else memcpy( data, buf_ptr + info_size, total_size - info_size );
1041         }
1042     }
1043     else status = STATUS_SUCCESS;
1044
1045     if (type) *type = info->Type;
1046     if (count) *count = total_size - info_size;
1047
1048  done:
1049     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1050     return RtlNtStatusToDosError(status);
1051 }
1052
1053
1054 /******************************************************************************
1055  *           RegQueryValueW   [ADVAPI32.@]
1056  */
1057 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1058 {
1059     DWORD ret;
1060     HKEY subkey = hkey;
1061
1062     TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
1063
1064     if (name && name[0])
1065     {
1066         if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1067     }
1068     ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1069     if (subkey != hkey) RegCloseKey( subkey );
1070     if (ret == ERROR_FILE_NOT_FOUND)
1071     {
1072         /* return empty string if default value not found */
1073         if (data) *data = 0;
1074         if (count) *count = 1;
1075         ret = ERROR_SUCCESS;
1076     }
1077     return ret;
1078 }
1079
1080
1081 /******************************************************************************
1082  *           RegQueryValueA   [ADVAPI32.@]
1083  */
1084 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1085 {
1086     DWORD ret;
1087     HKEY subkey = hkey;
1088
1089     TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1090
1091     if (name && name[0])
1092     {
1093         if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1094     }
1095     ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1096     if (subkey != hkey) RegCloseKey( subkey );
1097     if (ret == ERROR_FILE_NOT_FOUND)
1098     {
1099         /* return empty string if default value not found */
1100         if (data) *data = 0;
1101         if (count) *count = 1;
1102         ret = ERROR_SUCCESS;
1103     }
1104     return ret;
1105 }
1106
1107
1108 /******************************************************************************
1109  *           RegEnumValueW   [ADVAPI32.@]
1110  *
1111  * PARAMS
1112  *    hkey       [I] Handle to key to query
1113  *    index      [I] Index of value to query
1114  *    value      [O] Value string
1115  *    val_count  [I/O] Size of value buffer (in wchars)
1116  *    reserved   [I] Reserved
1117  *    type       [O] Type code
1118  *    data       [O] Value data
1119  *    count      [I/O] Size of data buffer (in bytes)
1120  */
1121
1122 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1123                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1124 {
1125     NTSTATUS status;
1126     DWORD total_size;
1127     char buffer[256], *buf_ptr = buffer;
1128     KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1129     static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1130
1131     TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
1132           hkey, index, value, val_count, reserved, type, data, count );
1133
1134     /* NT only checks count, not val_count */
1135     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1136
1137     total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1138     if (data) total_size += *count;
1139     total_size = min( sizeof(buffer), total_size );
1140
1141     status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1142                                   buffer, total_size, &total_size );
1143     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1144
1145     if (value || data)
1146     {
1147         /* retry with a dynamically allocated buffer */
1148         while (status == STATUS_BUFFER_OVERFLOW)
1149         {
1150             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1151             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1152                 return ERROR_NOT_ENOUGH_MEMORY;
1153             info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1154             status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1155                                           buf_ptr, total_size, &total_size );
1156         }
1157
1158         if (status) goto done;
1159
1160         if (value)
1161         {
1162             if (info->NameLength/sizeof(WCHAR) >= *val_count)
1163             {
1164                 status = STATUS_BUFFER_OVERFLOW;
1165                 goto overflow;
1166             }
1167             memcpy( value, info->Name, info->NameLength );
1168             *val_count = info->NameLength / sizeof(WCHAR);
1169             value[*val_count] = 0;
1170         }
1171
1172         if (data)
1173         {
1174             if (total_size - info->DataOffset > *count)
1175             {
1176                 status = STATUS_BUFFER_OVERFLOW;
1177                 goto overflow;
1178             }
1179             memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1180             if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1181             {
1182                 /* if the type is REG_SZ and data is not 0-terminated
1183                  * and there is enough space in the buffer NT appends a \0 */
1184                 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1185                 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1186             }
1187         }
1188     }
1189     else status = STATUS_SUCCESS;
1190
1191  overflow:
1192     if (type) *type = info->Type;
1193     if (count) *count = info->DataLength;
1194
1195  done:
1196     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1197     return RtlNtStatusToDosError(status);
1198 }
1199
1200
1201 /******************************************************************************
1202  *           RegEnumValueA   [ADVAPI32.@]
1203  */
1204 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1205                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1206 {
1207     NTSTATUS status;
1208     DWORD total_size;
1209     char buffer[256], *buf_ptr = buffer;
1210     KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1211     static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1212
1213     TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
1214           hkey, index, value, val_count, reserved, type, data, count );
1215
1216     /* NT only checks count, not val_count */
1217     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1218
1219     total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1220     if (data) total_size += *count;
1221     total_size = min( sizeof(buffer), total_size );
1222
1223     status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1224                                   buffer, total_size, &total_size );
1225     if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1226
1227     /* we need to fetch the contents for a string type even if not requested,
1228      * because we need to compute the length of the ASCII string. */
1229     if (value || data || is_string(info->Type))
1230     {
1231         /* retry with a dynamically allocated buffer */
1232         while (status == STATUS_BUFFER_OVERFLOW)
1233         {
1234             if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1235             if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1236                 return ERROR_NOT_ENOUGH_MEMORY;
1237             info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1238             status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1239                                           buf_ptr, total_size, &total_size );
1240         }
1241
1242         if (status) goto done;
1243
1244         if (is_string(info->Type))
1245         {
1246             DWORD len;
1247             RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1248                                        total_size - info->DataOffset );
1249             if (data && len)
1250             {
1251                 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1252                 else
1253                 {
1254                     RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1255                                             total_size - info->DataOffset );
1256                     /* if the type is REG_SZ and data is not 0-terminated
1257                      * and there is enough space in the buffer NT appends a \0 */
1258                     if (len < *count && data[len-1]) data[len] = 0;
1259                 }
1260             }
1261             info->DataLength = len;
1262         }
1263         else if (data)
1264         {
1265             if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1266             else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1267         }
1268
1269         if (value && !status)
1270         {
1271             DWORD len;
1272
1273             RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1274             if (len >= *val_count)
1275             {
1276                 status = STATUS_BUFFER_OVERFLOW;
1277                 if (*val_count)
1278                 {
1279                     len = *val_count - 1;
1280                     RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1281                     value[len] = 0;
1282                 }
1283             }
1284             else
1285             {
1286                 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1287                 value[len] = 0;
1288                 *val_count = len;
1289             }
1290         }
1291     }
1292     else status = STATUS_SUCCESS;
1293
1294     if (type) *type = info->Type;
1295     if (count) *count = info->DataLength;
1296
1297  done:
1298     if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1299     return RtlNtStatusToDosError(status);
1300 }
1301
1302
1303
1304 /******************************************************************************
1305  *           RegDeleteValueW   [ADVAPI32.@]
1306  *
1307  * PARAMS
1308  *    hkey   [I] handle to key
1309  *    name   [I] name of value to delete
1310  *
1311  * RETURNS
1312  *    error status
1313  */
1314 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1315 {
1316     UNICODE_STRING nameW;
1317     RtlInitUnicodeString( &nameW, name );
1318     return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1319 }
1320
1321
1322 /******************************************************************************
1323  *           RegDeleteValueA   [ADVAPI32.@]
1324  */
1325 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1326 {
1327     STRING nameA;
1328     NTSTATUS status;
1329
1330     RtlInitAnsiString( &nameA, name );
1331     if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1332                                                  &nameA, FALSE )))
1333     {
1334         status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1335     }
1336     return RtlNtStatusToDosError( status );
1337 }
1338
1339
1340 /******************************************************************************
1341  *           RegLoadKeyW   [ADVAPI32.@]
1342  *
1343  * PARAMS
1344  *    hkey      [I] Handle of open key
1345  *    subkey    [I] Address of name of subkey
1346  *    filename  [I] Address of filename for registry information
1347  */
1348 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1349 {
1350     HANDLE file;
1351     DWORD ret, len, err = GetLastError();
1352
1353     TRACE( "(%x,%s,%s)\n", hkey, debugstr_w(subkey), debugstr_w(filename) );
1354
1355     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1356     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1357
1358     len = strlenW( subkey ) * sizeof(WCHAR);
1359     if (len > MAX_PATH*sizeof(WCHAR)) return ERROR_INVALID_PARAMETER;
1360
1361     if ((file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1362                              FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1363     {
1364         ret = GetLastError();
1365         goto done;
1366     }
1367
1368     SERVER_START_REQ( load_registry )
1369     {
1370         req->hkey  = hkey;
1371         req->file  = file;
1372         wine_server_add_data( req, subkey, len );
1373         ret = RtlNtStatusToDosError( wine_server_call(req) );
1374     }
1375     SERVER_END_REQ;
1376     CloseHandle( file );
1377
1378  done:
1379     SetLastError( err );  /* restore the last error code */
1380     return ret;
1381 }
1382
1383
1384 /******************************************************************************
1385  *           RegLoadKeyA   [ADVAPI32.@]
1386  */
1387 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1388 {
1389     WCHAR buffer[MAX_PATH];
1390     HANDLE file;
1391     DWORD ret, len, err = GetLastError();
1392
1393     TRACE( "(%x,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
1394
1395     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1396     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1397
1398     if (!(len = MultiByteToWideChar( CP_ACP, 0, subkey, strlen(subkey), buffer, MAX_PATH )))
1399         return ERROR_INVALID_PARAMETER;
1400
1401     if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1402                              FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1403     {
1404         ret = GetLastError();
1405         goto done;
1406     }
1407
1408     SERVER_START_REQ( load_registry )
1409     {
1410         req->hkey  = hkey;
1411         req->file  = file;
1412         wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
1413         ret = RtlNtStatusToDosError( wine_server_call(req) );
1414     }
1415     SERVER_END_REQ;
1416     CloseHandle( file );
1417
1418  done:
1419     SetLastError( err );  /* restore the last error code */
1420     return ret;
1421 }
1422
1423
1424 /******************************************************************************
1425  *           RegSaveKeyA   [ADVAPI32.@]
1426  *
1427  * PARAMS
1428  *    hkey   [I] Handle of key where save begins
1429  *    lpFile [I] Address of filename to save to
1430  *    sa     [I] Address of security structure
1431  */
1432 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1433 {
1434     char buffer[1024];
1435     int count = 0;
1436     LPSTR name;
1437     DWORD ret, err;
1438     HFILE handle;
1439
1440     TRACE( "(%x,%s,%p)\n", hkey, debugstr_a(file), sa );
1441
1442     if (!file || !*file) return ERROR_INVALID_PARAMETER;
1443
1444     err = GetLastError();
1445     GetFullPathNameA( file, sizeof(buffer), buffer, &name );
1446     for (;;)
1447     {
1448         sprintf( name, "reg%04x.tmp", count++ );
1449         handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
1450                             CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1451         if (handle != INVALID_HANDLE_VALUE) break;
1452         if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1453
1454         /* Something gone haywire ? Please report if this happens abnormally */
1455         if (count >= 100)
1456             MESSAGE("Wow, we are already fiddling with a temp file %s with an ordinal as high as %d !\nYou might want to delete all corresponding temp files in that directory.\n", buffer, count);
1457     }
1458
1459     SERVER_START_REQ( save_registry )
1460     {
1461         req->hkey = hkey;
1462         req->file = handle;
1463         ret = RtlNtStatusToDosError( wine_server_call( req ) );
1464     }
1465     SERVER_END_REQ;
1466
1467     CloseHandle( handle );
1468     if (!ret)
1469     {
1470         if (!MoveFileExA( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1471         {
1472             ERR( "Failed to move %s to %s\n", buffer, file );
1473             ret = GetLastError();
1474         }
1475     }
1476     if (ret) DeleteFileA( buffer );
1477
1478 done:
1479     SetLastError( err );  /* restore last error code */
1480     return ret;
1481 }
1482
1483
1484 /******************************************************************************
1485  *           RegSaveKeyW   [ADVAPI32.@]
1486  */
1487 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1488 {
1489     LPSTR fileA = HEAP_strdupWtoA( GetProcessHeap(), 0, file );
1490     DWORD ret = RegSaveKeyA( hkey, fileA, sa );
1491     if (fileA) HeapFree( GetProcessHeap(), 0, fileA );
1492     return ret;
1493 }
1494
1495
1496 /******************************************************************************
1497  * RegRestoreKeyW [ADVAPI32.@]
1498  *
1499  * PARAMS
1500  *    hkey    [I] Handle of key where restore begins
1501  *    lpFile  [I] Address of filename containing saved tree
1502  *    dwFlags [I] Optional flags
1503  */
1504 LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
1505 {
1506     TRACE("(%x,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
1507
1508     /* It seems to do this check before the hkey check */
1509     if (!lpFile || !*lpFile)
1510         return ERROR_INVALID_PARAMETER;
1511
1512     FIXME("(%x,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
1513
1514     /* Check for file existence */
1515
1516     return ERROR_SUCCESS;
1517 }
1518
1519
1520 /******************************************************************************
1521  * RegRestoreKeyA [ADVAPI32.@]
1522  */
1523 LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
1524 {
1525     LPWSTR lpFileW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpFile );
1526     LONG ret = RegRestoreKeyW( hkey, lpFileW, dwFlags );
1527     HeapFree( GetProcessHeap(), 0, lpFileW );
1528     return ret;
1529 }
1530
1531
1532 /******************************************************************************
1533  * RegUnLoadKeyW [ADVAPI32.@]
1534  *
1535  * PARAMS
1536  *    hkey     [I] Handle of open key
1537  *    lpSubKey [I] Address of name of subkey to unload
1538  */
1539 LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
1540 {
1541     FIXME("(%x,%s): stub\n",hkey, debugstr_w(lpSubKey));
1542     return ERROR_SUCCESS;
1543 }
1544
1545
1546 /******************************************************************************
1547  * RegUnLoadKeyA [ADVAPI32.@]
1548  */
1549 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
1550 {
1551     LPWSTR lpSubKeyW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpSubKey );
1552     LONG ret = RegUnLoadKeyW( hkey, lpSubKeyW );
1553     if(lpSubKeyW) HeapFree( GetProcessHeap(), 0, lpSubKeyW);
1554     return ret;
1555 }
1556
1557
1558 /******************************************************************************
1559  * RegReplaceKeyW [ADVAPI32.@]
1560  *
1561  * PARAMS
1562  *    hkey      [I] Handle of open key
1563  *    lpSubKey  [I] Address of name of subkey
1564  *    lpNewFile [I] Address of filename for file with new data
1565  *    lpOldFile [I] Address of filename for backup file
1566  */
1567 LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
1568                               LPCWSTR lpOldFile )
1569 {
1570     FIXME("(%x,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
1571           debugstr_w(lpNewFile),debugstr_w(lpOldFile));
1572     return ERROR_SUCCESS;
1573 }
1574
1575
1576 /******************************************************************************
1577  * RegReplaceKeyA [ADVAPI32.@]
1578  */
1579 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
1580                               LPCSTR lpOldFile )
1581 {
1582     LPWSTR lpSubKeyW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpSubKey );
1583     LPWSTR lpNewFileW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpNewFile );
1584     LPWSTR lpOldFileW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpOldFile );
1585     LONG ret = RegReplaceKeyW( hkey, lpSubKeyW, lpNewFileW, lpOldFileW );
1586     HeapFree( GetProcessHeap(), 0, lpOldFileW );
1587     HeapFree( GetProcessHeap(), 0, lpNewFileW );
1588     HeapFree( GetProcessHeap(), 0, lpSubKeyW );
1589     return ret;
1590 }
1591
1592
1593 /******************************************************************************
1594  * RegSetKeySecurity [ADVAPI32.@]
1595  *
1596  * PARAMS
1597  *    hkey          [I] Open handle of key to set
1598  *    SecurityInfo  [I] Descriptor contents
1599  *    pSecurityDesc [I] Address of descriptor for key
1600  */
1601 LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
1602                                PSECURITY_DESCRIPTOR pSecurityDesc )
1603 {
1604     TRACE("(%x,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
1605
1606     /* It seems to perform this check before the hkey check */
1607     if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
1608         (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
1609         (SecurityInfo & DACL_SECURITY_INFORMATION) ||
1610         (SecurityInfo & SACL_SECURITY_INFORMATION)) {
1611         /* Param OK */
1612     } else
1613         return ERROR_INVALID_PARAMETER;
1614
1615     if (!pSecurityDesc)
1616         return ERROR_INVALID_PARAMETER;
1617
1618     FIXME(":(%x,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
1619
1620     return ERROR_SUCCESS;
1621 }
1622
1623
1624 /******************************************************************************
1625  * RegGetKeySecurity [ADVAPI32.@]
1626  * Retrieves a copy of security descriptor protecting the registry key
1627  *
1628  * PARAMS
1629  *    hkey                   [I]   Open handle of key to set
1630  *    SecurityInformation    [I]   Descriptor contents
1631  *    pSecurityDescriptor    [O]   Address of descriptor for key
1632  *    lpcbSecurityDescriptor [I/O] Address of size of buffer and description
1633  *
1634  * RETURNS
1635  *    Success: ERROR_SUCCESS
1636  *    Failure: Error code
1637  */
1638 LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
1639                                PSECURITY_DESCRIPTOR pSecurityDescriptor,
1640                                LPDWORD lpcbSecurityDescriptor )
1641 {
1642     TRACE("(%x,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
1643           lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1644
1645     /* FIXME: Check for valid SecurityInformation values */
1646
1647     if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
1648         return ERROR_INSUFFICIENT_BUFFER;
1649
1650     FIXME("(%x,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
1651           pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1652
1653     /* Do not leave security descriptor filled with garbage */
1654     RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1655
1656     return ERROR_SUCCESS;
1657 }
1658
1659
1660 /******************************************************************************
1661  * RegFlushKey [ADVAPI32.@]
1662  * Immediately writes key to registry.
1663  * Only returns after data has been written to disk.
1664  *
1665  * FIXME: does it really wait until data is written ?
1666  *
1667  * PARAMS
1668  *    hkey [I] Handle of key to write
1669  *
1670  * RETURNS
1671  *    Success: ERROR_SUCCESS
1672  *    Failure: Error code
1673  */
1674 DWORD WINAPI RegFlushKey( HKEY hkey )
1675 {
1676     FIXME( "(%x): stub\n", hkey );
1677     return ERROR_SUCCESS;
1678 }
1679
1680
1681 /******************************************************************************
1682  * RegConnectRegistryW [ADVAPI32.@]
1683  *
1684  * PARAMS
1685  *    lpMachineName [I] Address of name of remote computer
1686  *    hHey          [I] Predefined registry handle
1687  *    phkResult     [I] Address of buffer for remote registry handle
1688  */
1689 LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
1690                                    LPHKEY phkResult )
1691 {
1692     TRACE("(%s,%x,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
1693
1694     if (!lpMachineName || !*lpMachineName) {
1695         /* Use the local machine name */
1696         return RegOpenKeyA( hKey, "", phkResult );
1697     }
1698
1699     FIXME("Cannot connect to %s\n",debugstr_w(lpMachineName));
1700     return ERROR_BAD_NETPATH;
1701 }
1702
1703
1704 /******************************************************************************
1705  * RegConnectRegistryA [ADVAPI32.@]
1706  */
1707 LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, LPHKEY reskey )
1708 {
1709     LPWSTR machineW = HEAP_strdupAtoW( GetProcessHeap(), 0, machine );
1710     DWORD ret = RegConnectRegistryW( machineW, hkey, reskey );
1711     HeapFree( GetProcessHeap(), 0, machineW );
1712     return ret;
1713 }
1714
1715
1716 /******************************************************************************
1717  * RegNotifyChangeKeyValue [ADVAPI32.@]
1718  *
1719  * PARAMS
1720  *    hkey            [I] Handle of key to watch
1721  *    fWatchSubTree   [I] Flag for subkey notification
1722  *    fdwNotifyFilter [I] Changes to be reported
1723  *    hEvent          [I] Handle of signaled event
1724  *    fAsync          [I] Flag for asynchronous reporting
1725  */
1726 LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
1727                                      DWORD fdwNotifyFilter, HANDLE hEvent,
1728                                      BOOL fAsync )
1729 {
1730     FIXME("(%x,%i,%ld,%x,%i): stub\n",hkey,fWatchSubTree,fdwNotifyFilter,
1731           hEvent,fAsync);
1732     return ERROR_SUCCESS;
1733 }