Fixed the displaying of the FOURCC codes in _dump_pixelformat.
[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
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <unistd.h>
18
19 #include "winbase.h"
20 #include "winreg.h"
21 #include "winerror.h"
22 #include "wine/winbase16.h"
23 #include "wine/unicode.h"
24 #include "heap.h"
25 #include "server.h"
26 #include "debugtools.h"
27
28 DEFAULT_DEBUG_CHANNEL(reg);
29
30
31 /* Ansi->Unicode conversion without string delimiters */
32 static LPWSTR memcpyAtoW( LPWSTR dst, LPCSTR src, INT n )
33 {
34     LPWSTR p = dst;
35     while (n-- > 0) *p++ = (WCHAR)*src++;
36     return dst;
37 }
38
39 /* Unicode->Ansi conversion without string delimiters */
40 static LPSTR memcpyWtoA( LPSTR dst, LPCWSTR src, INT n )
41 {
42     LPSTR p = dst;
43     while (n-- > 0) *p++ = (CHAR)*src++;
44     return dst;
45 }
46
47 /* check if value type needs string conversion (Ansi<->Unicode) */
48 static inline int is_string( DWORD type )
49 {
50     return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
51 }
52
53 /* copy a key name into the request buffer */
54 static inline DWORD copy_nameW( LPWSTR dest, LPCWSTR name )
55 {
56     if (name)
57     {
58         if (strlenW(name) > MAX_PATH) return ERROR_MORE_DATA;
59         strcpyW( dest, name );
60     }
61     else dest[0] = 0;
62     return ERROR_SUCCESS;
63 }
64
65 /* copy a key name into the request buffer */
66 static inline DWORD copy_nameAtoW( LPWSTR dest, LPCSTR name )
67 {
68     if (name)
69     {
70         if (strlen(name) > MAX_PATH) return ERROR_MORE_DATA;
71         lstrcpyAtoW( dest, name );
72     }
73     else dest[0] = 0;
74     return ERROR_SUCCESS;
75 }
76
77 /* do a server call without setting the last error code */
78 static inline int reg_server_call( enum request req )
79 {
80     unsigned int res = server_call_noerr( req );
81     if (res) res = RtlNtStatusToDosError(res);
82     return res;
83 }
84
85 /******************************************************************************
86  *           RegCreateKeyExW   [ADVAPI32.131]
87  *
88  * PARAMS
89  *    hkey       [I] Handle of an open key
90  *    name       [I] Address of subkey name
91  *    reserved   [I] Reserved - must be 0
92  *    class      [I] Address of class string
93  *    options    [I] Special options flag
94  *    access     [I] Desired security access
95  *    sa         [I] Address of key security structure
96  *    retkey     [O] Address of buffer for opened handle
97  *    dispos     [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
98  *
99  * NOTES
100  *  in case of failing retkey remains untouched
101  */
102 DWORD WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
103                               DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa, 
104                               LPHKEY retkey, LPDWORD dispos )
105 {
106     DWORD ret;
107     struct create_key_request *req = get_req_buffer();
108
109     TRACE( "(0x%x,%s,%ld,%s,%lx,%lx,%p,%p,%p)\n", hkey, debugstr_w(name), reserved,
110            debugstr_w(class), options, access, sa, retkey, dispos );
111
112     if (reserved) return ERROR_INVALID_PARAMETER;
113     if (!(access & KEY_ALL_ACCESS) || (access & ~KEY_ALL_ACCESS)) return ERROR_ACCESS_DENIED;
114
115     req->parent  = hkey;
116     req->access  = access;
117     req->options = options;
118     req->modif   = time(NULL);
119     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
120     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
121     lstrcpynW( req->class, class ? class : (LPWSTR)"\0\0",
122                server_remaining(req->class) / sizeof(WCHAR) );
123     if ((ret = reg_server_call( REQ_CREATE_KEY )) == ERROR_SUCCESS)
124     {
125         *retkey = req->hkey;
126         if (dispos) *dispos = req->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
127     }
128     return ret;
129 }
130
131
132 /******************************************************************************
133  *           RegCreateKeyExA   [ADVAPI32.130]
134  */
135 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
136                               DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa, 
137                               LPHKEY retkey, LPDWORD dispos )
138 {
139     DWORD ret;
140     struct create_key_request *req = get_req_buffer();
141
142     TRACE( "(0x%x,%s,%ld,%s,%lx,%lx,%p,%p,%p)\n", hkey, debugstr_a(name), reserved,
143            debugstr_a(class), options, access, sa, retkey, dispos );
144
145     if (reserved) return ERROR_INVALID_PARAMETER;
146     if (!(access & KEY_ALL_ACCESS) || (access & ~KEY_ALL_ACCESS)) return ERROR_ACCESS_DENIED;
147
148     req->parent  = hkey;
149     req->access  = access;
150     req->options = options;
151     req->modif   = time(NULL);
152     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
153     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
154     lstrcpynAtoW( req->class, class ? class : "",
155                   server_remaining(req->class) / sizeof(WCHAR) );
156     if ((ret = reg_server_call( REQ_CREATE_KEY )) == ERROR_SUCCESS)
157     {
158         *retkey = req->hkey;
159         if (dispos) *dispos = req->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
160     }
161     return ret;
162 }
163
164
165 /******************************************************************************
166  *           RegCreateKeyW   [ADVAPI32.132]
167  */
168 DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR name, LPHKEY retkey )
169 {
170     /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
171     /* but at least my version of NT (4.0 SP5) doesn't do this.  -- AJ */
172     return RegCreateKeyExW( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
173                             KEY_ALL_ACCESS, NULL, retkey, NULL );
174 }
175
176
177 /******************************************************************************
178  *           RegCreateKeyA   [ADVAPI32.129]
179  */
180 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
181 {
182     return RegCreateKeyExA( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
183                             KEY_ALL_ACCESS, NULL, retkey, NULL );
184 }
185
186
187
188 /******************************************************************************
189  *           RegOpenKeyExW   [ADVAPI32.150]
190  *
191  * Opens the specified key
192  *
193  * Unlike RegCreateKeyEx, this does not create the key if it does not exist.
194  *
195  * PARAMS
196  *    hkey       [I] Handle of open key
197  *    name       [I] Name of subkey to open
198  *    reserved   [I] Reserved - must be zero
199  *    access     [I] Security access mask
200  *    retkey     [O] Handle to open key
201  *
202  * RETURNS
203  *    Success: ERROR_SUCCESS
204  *    Failure: Error code
205  *
206  * NOTES
207  *  in case of failing is retkey = 0
208  */
209 DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
210 {
211     DWORD ret;
212     struct open_key_request *req = get_req_buffer();
213
214     TRACE( "(0x%x,%s,%ld,%lx,%p)\n", hkey, debugstr_w(name), reserved, access, retkey );
215
216     if (!retkey) return ERROR_INVALID_PARAMETER;
217     *retkey = 0;
218
219     req->parent = hkey;
220     req->access = access;
221     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
222     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
223     if ((ret = reg_server_call( REQ_OPEN_KEY )) == ERROR_SUCCESS) *retkey = req->hkey;
224     return ret;
225 }
226
227
228 /******************************************************************************
229  *           RegOpenKeyExA   [ADVAPI32.149]
230  */
231 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
232 {
233     DWORD ret;
234     struct open_key_request *req = get_req_buffer();
235
236     TRACE( "(0x%x,%s,%ld,%lx,%p)\n", hkey, debugstr_a(name), reserved, access, retkey );
237
238     if (!retkey) return ERROR_INVALID_PARAMETER;
239     *retkey = 0;
240
241     req->parent = hkey;
242     req->access = access;
243     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
244     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
245     if ((ret = reg_server_call( REQ_OPEN_KEY )) == ERROR_SUCCESS) *retkey = req->hkey;
246     return ret;
247 }
248
249
250 /******************************************************************************
251  *           RegOpenKeyW   [ADVAPI32.151]
252  *
253  * PARAMS
254  *    hkey    [I] Handle of open key
255  *    name    [I] Address of name of subkey to open
256  *    retkey  [O] Handle to open key
257  *
258  * RETURNS
259  *    Success: ERROR_SUCCESS
260  *    Failure: Error code
261  *
262  * NOTES
263  *  in case of failing is retkey = 0
264  */
265 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, LPHKEY retkey )
266 {
267     return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
268 }
269
270
271 /******************************************************************************
272  *           RegOpenKeyA   [ADVAPI32.148]
273  */
274 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
275 {
276     return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
277 }
278
279
280
281 /******************************************************************************
282  *           RegEnumKeyExW   [ADVAPI32.139]
283  *
284  * PARAMS
285  *    hkey         [I] Handle to key to enumerate
286  *    index        [I] Index of subkey to enumerate
287  *    name         [O] Buffer for subkey name
288  *    name_len     [O] Size of subkey buffer
289  *    reserved     [I] Reserved
290  *    class        [O] Buffer for class string
291  *    class_len    [O] Size of class buffer
292  *    ft           [O] Time key last written to
293  */
294 DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
295                             LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
296 {
297     DWORD ret, len, cls_len;
298     struct enum_key_request *req = get_req_buffer();
299
300     TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
301            name_len ? *name_len : -1, reserved, class, class_len, ft );
302
303     if (reserved) return ERROR_INVALID_PARAMETER;
304
305     req->hkey = hkey;
306     req->index = index;
307     if ((ret = reg_server_call( REQ_ENUM_KEY )) != ERROR_SUCCESS) return ret;
308
309     len = strlenW( req->name ) + 1;
310     cls_len = strlenW( req->class ) + 1;
311     if (len > *name_len) return ERROR_MORE_DATA;
312     if (class_len && (cls_len > *class_len)) return ERROR_MORE_DATA;
313
314     memcpy( name, req->name, len * sizeof(WCHAR) );
315     *name_len = len - 1;
316     if (class_len)
317     {
318         if (class) memcpy( class, req->class, cls_len * sizeof(WCHAR) );
319         *class_len = cls_len - 1;
320     }
321     if (ft) RtlSecondsSince1970ToTime( req->modif, ft );
322     return ERROR_SUCCESS;
323 }
324
325
326 /******************************************************************************
327  *           RegEnumKeyExA   [ADVAPI32.138]
328  */
329 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
330                             LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
331 {
332     DWORD ret, len, cls_len;
333     struct enum_key_request *req = get_req_buffer();
334
335     TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
336            name_len ? *name_len : -1, reserved, class, class_len, ft );
337
338     if (reserved) return ERROR_INVALID_PARAMETER;
339
340     req->hkey = hkey;
341     req->index = index;
342     if ((ret = reg_server_call( REQ_ENUM_KEY )) != ERROR_SUCCESS) return ret;
343
344     len = strlenW( req->name ) + 1;
345     cls_len = strlenW( req->class ) + 1;
346     if (len > *name_len) return ERROR_MORE_DATA;
347     if (class_len && (cls_len > *class_len)) return ERROR_MORE_DATA;
348
349     memcpyWtoA( name, req->name, len );
350     *name_len = len - 1;
351     if (class_len)
352     {
353         if (class) memcpyWtoA( class, req->class, cls_len );
354         *class_len = cls_len - 1;
355     }
356     if (ft) RtlSecondsSince1970ToTime( req->modif, ft );
357     return ERROR_SUCCESS;
358 }
359
360
361 /******************************************************************************
362  *           RegEnumKeyW   [ADVAPI32.140]
363  */
364 DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
365 {
366     return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
367 }
368
369
370 /******************************************************************************
371  *           RegEnumKeyA   [ADVAPI32.137]
372  */
373 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
374 {
375     return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
376 }
377
378
379 /******************************************************************************
380  *           RegQueryInfoKeyW   [ADVAPI32.153]
381  *
382  * PARAMS
383  *    hkey       [I] Handle to key to query
384  *    class      [O] Buffer for class string
385  *    class_len  [O] Size of class string buffer
386  *    reserved   [I] Reserved
387  *    subkeys    [O] Buffer for number of subkeys
388  *    max_subkey [O] Buffer for longest subkey name length
389  *    max_class  [O] Buffer for longest class string length
390  *    values     [O] Buffer for number of value entries
391  *    max_value  [O] Buffer for longest value name length
392  *    max_data   [O] Buffer for longest value data length
393  *    security   [O] Buffer for security descriptor length
394  *    modif      [O] Modification time
395  *
396  * - win95 allows class to be valid and class_len to be NULL 
397  * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
398  * - both allow class to be NULL and class_len to be NULL 
399  * (it's hard to test validity, so test !NULL instead)
400  */
401 DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
402                                LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
403                                LPDWORD values, LPDWORD max_value, LPDWORD max_data,
404                                LPDWORD security, FILETIME *modif )
405 {
406     DWORD ret;
407     struct query_key_info_request *req = get_req_buffer();
408
409
410     TRACE( "(0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
411            reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
412
413     if (class && !class_len && !(GetVersion() & 0x80000000 /*NT*/))
414         return ERROR_INVALID_PARAMETER;
415
416     req->hkey = hkey;
417     if ((ret = reg_server_call( REQ_QUERY_KEY_INFO )) != ERROR_SUCCESS) return ret;
418
419     if (class)
420     {
421         if (class_len && (strlenW(req->class) + 1 > *class_len))
422         {
423             *class_len = strlenW(req->class);
424             return ERROR_MORE_DATA;
425         }
426         strcpyW( class, req->class );
427     }
428     if (class_len) *class_len = strlenW( req->class );
429     if (subkeys) *subkeys = req->subkeys;
430     if (max_subkey) *max_subkey = req->max_subkey;
431     if (max_class) *max_class = req->max_class;
432     if (values) *values = req->values;
433     if (max_value) *max_value = req->max_value;
434     if (max_data) *max_data = req->max_data;
435     if (modif) RtlSecondsSince1970ToTime( req->modif, modif );
436     return ERROR_SUCCESS;
437 }
438
439
440 /******************************************************************************
441  *           RegQueryInfoKeyA   [ADVAPI32.152]
442  */
443 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
444                                LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
445                                LPDWORD values, LPDWORD max_value, LPDWORD max_data,
446                                LPDWORD security, FILETIME *modif )
447 {
448     DWORD ret;
449     struct query_key_info_request *req = get_req_buffer();
450
451
452     TRACE( "(0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
453            reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
454
455     if (class && !class_len && !(GetVersion() & 0x80000000 /*NT*/))
456         return ERROR_INVALID_PARAMETER;
457
458     req->hkey = hkey;
459     if ((ret = reg_server_call( REQ_QUERY_KEY_INFO )) != ERROR_SUCCESS) return ret;
460
461     if (class)
462     {
463         if (class_len && (strlenW(req->class) + 1 > *class_len))
464         {
465             *class_len = strlenW(req->class);
466             return ERROR_MORE_DATA;
467         }
468         lstrcpyWtoA( class, req->class );
469     }
470     if (class_len) *class_len = strlenW( req->class );
471     if (subkeys) *subkeys = req->subkeys;
472     if (max_subkey) *max_subkey = req->max_subkey;
473     if (max_class) *max_class = req->max_class;
474     if (values) *values = req->values;
475     if (max_value) *max_value = req->max_value;
476     if (max_data) *max_data = req->max_data;
477     if (modif) RtlSecondsSince1970ToTime( req->modif, modif );
478     return ERROR_SUCCESS;
479 }
480
481
482 /******************************************************************************
483  *           RegCloseKey   [ADVAPI32.126]
484  *
485  * Releases the handle of the specified key
486  *
487  * PARAMS
488  *    hkey [I] Handle of key to close
489  *
490  * RETURNS
491  *    Success: ERROR_SUCCESS
492  *    Failure: Error code
493  */
494 DWORD WINAPI RegCloseKey( HKEY hkey )
495 {
496     struct close_key_request *req = get_req_buffer();
497     TRACE( "(0x%x)\n", hkey );
498     req->hkey = hkey;
499     return reg_server_call( REQ_CLOSE_KEY );
500 }
501
502
503 /******************************************************************************
504  *           RegDeleteKeyW   [ADVAPI32.134]
505  *
506  * PARAMS
507  *    hkey   [I] Handle to open key
508  *    name   [I] Name of subkey to delete
509  *
510  * RETURNS
511  *    Success: ERROR_SUCCESS
512  *    Failure: Error code
513  */
514 DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
515 {
516     DWORD ret;
517     struct delete_key_request *req = get_req_buffer();
518
519     TRACE( "(0x%x,%s)\n", hkey, debugstr_w(name) );
520
521     req->hkey = hkey;
522     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
523     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
524     return reg_server_call( REQ_DELETE_KEY );
525 }
526
527
528 /******************************************************************************
529  *           RegDeleteKeyA   [ADVAPI32.133]
530  */
531 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
532 {
533     DWORD ret;
534     struct delete_key_request *req = get_req_buffer();
535
536     TRACE( "(0x%x,%s)\n", hkey, debugstr_a(name) );
537
538     req->hkey = hkey;
539     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
540     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
541     return reg_server_call( REQ_DELETE_KEY );
542 }
543
544
545
546 /******************************************************************************
547  *           RegSetValueExW   [ADVAPI32.170]
548  *
549  * Sets the data and type of a value under a register key
550  *
551  * PARAMS
552  *    hkey       [I] Handle of key to set value for
553  *    name       [I] Name of value to set
554  *    reserved   [I] Reserved - must be zero
555  *    type       [I] Flag for value type
556  *    data       [I] Address of value data
557  *    count      [I] Size of value data
558  *
559  * RETURNS
560  *    Success: ERROR_SUCCESS
561  *    Failure: Error code
562  *
563  * NOTES
564  *   win95 does not care about count for REG_SZ and finds out the len by itself (js) 
565  *   NT does definitely care (aj)
566  */
567 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
568                              DWORD type, CONST BYTE *data, DWORD count )
569 {
570     DWORD ret;
571     struct set_key_value_request *req = get_req_buffer();
572     unsigned int max, pos;
573
574     TRACE( "(0x%x,%s,%ld,%ld,%p,%ld)\n", hkey, debugstr_w(name), reserved, type, data, count );
575
576     if (reserved) return ERROR_INVALID_PARAMETER;
577
578     if (count && type == REG_SZ)
579     {
580         LPCWSTR str = (LPCWSTR)data;
581         /* if user forgot to count terminating null, add it (yes NT does this) */
582         if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
583             count += sizeof(WCHAR);
584     }
585
586     req->hkey = hkey;
587     req->type = type;
588     req->total = count;
589     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
590
591     max = server_remaining( req->data );
592     pos = 0;
593     while (pos < count)
594     {
595         unsigned int len = count - pos;
596         if (len > max) len = max;
597         req->offset = pos;
598         req->len    = len;
599         memcpy( req->data, data + pos, len );
600         if ((ret = reg_server_call( REQ_SET_KEY_VALUE )) != ERROR_SUCCESS) break;
601         pos += len;
602     }
603     return ret;
604 }
605
606
607 /******************************************************************************
608  *           RegSetValueExA   [ADVAPI32.169]
609  */
610 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
611                              CONST BYTE *data, DWORD count )
612 {
613     DWORD ret;
614     struct set_key_value_request *req = get_req_buffer();
615     unsigned int max, pos;
616
617     TRACE( "(0x%x,%s,%ld,%ld,%p,%ld)\n", hkey, debugstr_a(name), reserved, type, data, count );
618
619     if (reserved) return ERROR_INVALID_PARAMETER;
620
621     if (count && type == REG_SZ)
622     {
623         /* if user forgot to count terminating null, add it (yes NT does this) */
624         if (data[count-1] && !data[count]) count++;
625     }
626     if (is_string( type )) /* need to convert to Unicode */
627         count *= sizeof(WCHAR);
628
629     req->hkey = hkey;
630     req->type = type;
631     req->total = count;
632     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
633
634     max = server_remaining( req->data );
635     pos = 0;
636     while (pos < count)
637     {
638         unsigned int len = count - pos;
639         if (len > max) len = max;
640         req->offset = pos;
641         req->len    = len;
642
643         if (is_string( type ))
644             memcpyAtoW( (LPWSTR)req->data, data + pos/sizeof(WCHAR), len/sizeof(WCHAR) );
645         else
646             memcpy( req->data, data + pos, len );
647         if ((ret = reg_server_call( REQ_SET_KEY_VALUE )) != ERROR_SUCCESS) break;
648         pos += len;
649     }
650     return ret;
651 }
652
653
654 /******************************************************************************
655  *           RegSetValueW   [ADVAPI32.171]
656  */
657 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
658 {
659     HKEY subkey = hkey;
660     DWORD ret;
661
662     TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
663
664     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
665
666     if (name && name[0])  /* need to create the subkey */
667     {
668         if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
669     }
670
671     ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (LPBYTE)data,
672                           (strlenW( data ) + 1) * sizeof(WCHAR) );
673     if (subkey != hkey) RegCloseKey( subkey );
674     return ret;
675 }
676
677
678 /******************************************************************************
679  *           RegSetValueA   [ADVAPI32.168]
680  */
681 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
682 {
683     HKEY subkey = hkey;
684     DWORD ret;
685
686     TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
687
688     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
689
690     if (name && name[0])  /* need to create the subkey */
691     {
692         if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
693     }
694     ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
695     if (subkey != hkey) RegCloseKey( subkey );
696     return ret;
697 }
698
699
700
701 /******************************************************************************
702  *           RegQueryValueExW   [ADVAPI32.158]
703  *
704  * Retrieves type and data for a specified name associated with an open key
705  *
706  * PARAMS
707  *    hkey      [I]   Handle of key to query
708  *    name      [I]   Name of value to query
709  *    reserved  [I]   Reserved - must be NULL
710  *    type      [O]   Address of buffer for value type.  If NULL, the type
711  *                        is not required.
712  *    data      [O]   Address of data buffer.  If NULL, the actual data is
713  *                        not required.
714  *    count     [I/O] Address of data buffer size
715  *
716  * RETURNS 
717  *    ERROR_SUCCESS:   Success
718  *    ERROR_MORE_DATA: !!! if the specified buffer is not big enough to hold the data
719  *                     buffer is left untouched. The MS-documentation is wrong (js) !!!
720  */
721 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
722                                LPBYTE data, LPDWORD count )
723 {
724     DWORD ret;
725     struct get_key_value_request *req = get_req_buffer();
726
727     TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
728           hkey, debugstr_w(name), reserved, type, data, count, count ? *count : 0 );
729
730     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
731
732     req->hkey = hkey;
733     req->offset = 0;
734     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
735     if ((ret = reg_server_call( REQ_GET_KEY_VALUE )) != ERROR_SUCCESS) return ret;
736
737     if (data)
738     {
739         if (*count < req->len) ret = ERROR_MORE_DATA;
740         else
741         {
742             /* copy the data */
743             unsigned int max = server_remaining( req->data );
744             unsigned int pos = 0;
745             while (pos < req->len)
746             {
747                 unsigned int len = min( req->len - pos, max );
748                 memcpy( data + pos, req->data, len );
749                 if ((pos += len) >= req->len) break;
750                 req->offset = pos;
751                 if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
752                 if ((ret = reg_server_call( REQ_GET_KEY_VALUE )) != ERROR_SUCCESS) return ret;
753             }
754         }
755         /* if the type is REG_SZ and data is not 0-terminated
756          * and there is enough space in the buffer NT appends a \0 */
757         if (req->len && is_string(req->type) &&
758             (req->len < *count) && ((WCHAR *)data)[req->len-1]) ((WCHAR *)data)[req->len] = 0;
759     }
760     if (type) *type = req->type;
761     if (count) *count = req->len;
762     return ret;
763 }
764
765
766 /******************************************************************************
767  *           RegQueryValueExA   [ADVAPI32.157]
768  *
769  * NOTES:
770  * the documentation is wrong: if the buffer is to small it remains untouched 
771  */
772 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
773                                LPBYTE data, LPDWORD count )
774 {
775     DWORD ret, total_len;
776     struct get_key_value_request *req = get_req_buffer();
777
778     TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
779           hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
780
781     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
782
783     req->hkey = hkey;
784     req->offset = 0;
785     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
786     if ((ret = reg_server_call( REQ_GET_KEY_VALUE )) != ERROR_SUCCESS) return ret;
787
788     total_len = is_string( req->type ) ? req->len/sizeof(WCHAR) : req->len;
789
790     if (data)
791     {
792         if (*count < total_len) ret = ERROR_MORE_DATA;
793         else
794         {
795             /* copy the data */
796             unsigned int max = server_remaining( req->data );
797             unsigned int pos = 0;
798             while (pos < req->len)
799             {
800                 unsigned int len = min( req->len - pos, max );
801                 if (is_string( req->type ))
802                     memcpyWtoA( data + pos/sizeof(WCHAR), (WCHAR *)req->data, len/sizeof(WCHAR) );
803                 else
804                     memcpy( data + pos, req->data, len );
805                 if ((pos += len) >= req->len) break;
806                 req->offset = pos;
807                 if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
808                 if ((ret = reg_server_call( REQ_GET_KEY_VALUE )) != ERROR_SUCCESS) return ret;
809             }
810         }
811         /* if the type is REG_SZ and data is not 0-terminated
812          * and there is enough space in the buffer NT appends a \0 */
813         if (total_len && is_string(req->type) && (total_len < *count) && data[total_len-1])
814             data[total_len] = 0;
815     }
816
817     if (count) *count = total_len;
818     if (type) *type = req->type;
819     return ret;
820 }
821
822
823 /******************************************************************************
824  *           RegQueryValueW   [ADVAPI32.159]
825  */
826 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
827 {
828     DWORD ret;
829     HKEY subkey = hkey;
830
831     TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
832
833     if (name && name[0])
834     {
835         if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
836     }
837     ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
838     if (subkey != hkey) RegCloseKey( subkey );
839     if (ret == ERROR_FILE_NOT_FOUND)
840     {
841         /* return empty string if default value not found */
842         if (data) *data = 0;
843         if (count) *count = 1;
844         ret = ERROR_SUCCESS;
845     }
846     return ret;
847 }
848
849
850 /******************************************************************************
851  *           RegQueryValueA   [ADVAPI32.156]
852  */
853 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
854 {
855     DWORD ret;
856     HKEY subkey = hkey;
857
858     TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
859
860     if (name && name[0])
861     {
862         if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
863     }
864     ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
865     if (subkey != hkey) RegCloseKey( subkey );
866     if (ret == ERROR_FILE_NOT_FOUND)
867     {
868         /* return empty string if default value not found */
869         if (data) *data = 0;
870         if (count) *count = 1;
871         ret = ERROR_SUCCESS;
872     }
873     return ret;
874 }
875
876
877 /******************************************************************************
878  *           RegEnumValueW   [ADVAPI32.142]
879  *
880  * PARAMS
881  *    hkey       [I] Handle to key to query
882  *    index      [I] Index of value to query
883  *    value      [O] Value string
884  *    val_count  [I/O] Size of value buffer (in wchars)
885  *    reserved   [I] Reserved
886  *    type       [O] Type code
887  *    data       [O] Value data
888  *    count      [I/O] Size of data buffer (in bytes)
889  */
890
891 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
892                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
893 {
894     DWORD ret, len;
895     struct enum_key_value_request *req = get_req_buffer();
896
897     TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
898           hkey, index, value, val_count, reserved, type, data, count );
899
900     /* NT only checks count, not val_count */
901     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
902
903     req->hkey = hkey;
904     req->index = index;
905     req->offset = 0;
906     if ((ret = reg_server_call( REQ_ENUM_KEY_VALUE )) != ERROR_SUCCESS) return ret;
907
908     len = strlenW( req->name ) + 1;
909     if (len > *val_count) return ERROR_MORE_DATA;
910     memcpy( value, req->name, len * sizeof(WCHAR) );
911     *val_count = len - 1;
912
913     if (data)
914     {
915         if (*count < req->len) ret = ERROR_MORE_DATA;
916         else
917         {
918             /* copy the data */
919             unsigned int max = server_remaining( req->data );
920             unsigned int pos = 0;
921             while (pos < req->len)
922             {
923                 unsigned int len = min( req->len - pos, max );
924                 memcpy( data + pos, req->data, len );
925                 if ((pos += len) >= req->len) break;
926                 req->offset = pos;
927                 if ((ret = reg_server_call( REQ_ENUM_KEY_VALUE )) != ERROR_SUCCESS) return ret;
928             }
929         }
930         /* if the type is REG_SZ and data is not 0-terminated
931          * and there is enough space in the buffer NT appends a \0 */
932         if (req->len && is_string(req->type) &&
933             (req->len < *count) && ((WCHAR *)data)[req->len-1]) ((WCHAR *)data)[req->len] = 0;
934     }
935     if (type) *type = req->type;
936     if (count) *count = req->len;
937     return ret;
938 }
939
940
941 /******************************************************************************
942  *           RegEnumValueA   [ADVAPI32.141]
943  */
944 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
945                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
946 {
947     DWORD ret, len, total_len;
948     struct enum_key_value_request *req = get_req_buffer();
949
950     TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
951           hkey, index, value, val_count, reserved, type, data, count );
952
953     /* NT only checks count, not val_count */
954     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
955
956     req->hkey = hkey;
957     req->index = index;
958     req->offset = 0;
959     if ((ret = reg_server_call( REQ_ENUM_KEY_VALUE )) != ERROR_SUCCESS) return ret;
960
961     len = strlenW( req->name ) + 1;
962     if (len > *val_count) return ERROR_MORE_DATA;
963     memcpyWtoA( value, req->name, len );
964     *val_count = len - 1;
965
966     total_len = is_string( req->type ) ? req->len/sizeof(WCHAR) : req->len;
967
968     if (data)
969     {
970         if (*count < total_len) ret = ERROR_MORE_DATA;
971         else
972         {
973             /* copy the data */
974             unsigned int max = server_remaining( req->data );
975             unsigned int pos = 0;
976             while (pos < req->len)
977             {
978                 unsigned int len = min( req->len - pos, max );
979                 if (is_string( req->type ))
980                     memcpyWtoA( data + pos/sizeof(WCHAR), (WCHAR *)req->data, len/sizeof(WCHAR) );
981                 else
982                     memcpy( data + pos, req->data, len );
983                 if ((pos += len) >= req->len) break;
984                 req->offset = pos;
985                 if ((ret = reg_server_call( REQ_ENUM_KEY_VALUE )) != ERROR_SUCCESS) return ret;
986             }
987         }
988         /* if the type is REG_SZ and data is not 0-terminated
989          * and there is enough space in the buffer NT appends a \0 */
990         if (total_len && is_string(req->type) && (total_len < *count) && data[total_len-1])
991             data[total_len] = 0;
992     }
993
994     if (count) *count = total_len;
995     if (type) *type = req->type;
996     return ret;
997 }
998
999
1000
1001 /******************************************************************************
1002  *           RegDeleteValueW   [ADVAPI32.136]
1003  *
1004  * PARAMS
1005  *    hkey   [I] handle to key
1006  *    name   [I] name of value to delete
1007  *
1008  * RETURNS
1009  *    error status
1010  */
1011 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1012 {
1013     DWORD ret;
1014     struct delete_key_value_request *req = get_req_buffer();
1015
1016     TRACE( "(0x%x,%s)\n", hkey, debugstr_w(name) );
1017
1018     req->hkey = hkey;
1019     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
1020     return reg_server_call( REQ_DELETE_KEY_VALUE );
1021 }
1022
1023
1024 /******************************************************************************
1025  *           RegDeleteValueA   [ADVAPI32.135]
1026  */
1027 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1028 {
1029     DWORD ret;
1030     struct delete_key_value_request *req = get_req_buffer();
1031
1032     TRACE( "(0x%x,%s)\n", hkey, debugstr_a(name) );
1033
1034     req->hkey = hkey;
1035     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
1036     return reg_server_call( REQ_DELETE_KEY_VALUE );
1037 }
1038
1039
1040 /******************************************************************************
1041  *           RegLoadKeyW   [ADVAPI32.185]
1042  *
1043  * PARAMS
1044  *    hkey      [I] Handle of open key
1045  *    subkey    [I] Address of name of subkey
1046  *    filename  [I] Address of filename for registry information
1047  */
1048 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1049 {
1050     struct load_registry_request *req = get_req_buffer();
1051     HANDLE file;
1052     DWORD ret, err = GetLastError();
1053
1054     TRACE( "(%x,%s,%s)\n", hkey, debugstr_w(subkey), debugstr_w(filename) );
1055
1056     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1057     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1058
1059     if ((file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1060                              FILE_ATTRIBUTE_NORMAL, -1 )) == INVALID_HANDLE_VALUE)
1061     {
1062         ret = GetLastError();
1063         goto done;
1064     }
1065     req->hkey  = hkey;
1066     req->file  = file;
1067     if ((ret = copy_nameW( req->name, subkey )) != ERROR_SUCCESS) goto done;
1068     ret = reg_server_call( REQ_LOAD_REGISTRY );
1069     CloseHandle( file );
1070
1071  done:
1072     SetLastError( err );  /* restore the last error code */
1073     return ret;
1074 }
1075
1076
1077 /******************************************************************************
1078  *           RegLoadKeyA   [ADVAPI32.184]
1079  */
1080 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1081 {
1082     struct load_registry_request *req = get_req_buffer();
1083     HANDLE file;
1084     DWORD ret, err = GetLastError();
1085
1086     TRACE( "(%x,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
1087
1088     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1089     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1090
1091     if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1092                              FILE_ATTRIBUTE_NORMAL, -1 )) == INVALID_HANDLE_VALUE)
1093     {
1094         ret = GetLastError();
1095         goto done;
1096     }
1097     req->hkey  = hkey;
1098     req->file  = file;
1099     if ((ret = copy_nameAtoW( req->name, subkey )) != ERROR_SUCCESS) goto done;
1100     ret = reg_server_call( REQ_LOAD_REGISTRY );
1101     CloseHandle( file );
1102
1103  done:
1104     SetLastError( err );  /* restore the last error code */
1105     return ret;
1106 }
1107
1108
1109 /******************************************************************************
1110  *           RegSaveKeyA   [ADVAPI32.165]
1111  *
1112  * PARAMS
1113  *    hkey   [I] Handle of key where save begins
1114  *    lpFile [I] Address of filename to save to
1115  *    sa     [I] Address of security structure
1116  */
1117 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1118 {
1119     struct save_registry_request *req = get_req_buffer();
1120     char buffer[1024];
1121     int count = 0;
1122     LPSTR name;
1123     DWORD ret, err;
1124     HFILE handle;
1125
1126     TRACE( "(%x,%s,%p)\n", hkey, debugstr_a(file), sa );
1127
1128     if (!file || !*file) return ERROR_INVALID_PARAMETER;
1129
1130     err = GetLastError();
1131     GetFullPathNameA( file, sizeof(buffer), buffer, &name );
1132     for (;;)
1133     {
1134         sprintf( name, "reg%04x.tmp", count++ );
1135         handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
1136                             CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
1137         if (handle != INVALID_HANDLE_VALUE) break;
1138         if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1139
1140         /* Something gone haywire ? Please report if this happens abnormally */
1141         if (count >= 100)
1142             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);
1143     }
1144
1145     req->hkey = hkey;
1146     req->file = handle;
1147     ret = reg_server_call( REQ_SAVE_REGISTRY );
1148     CloseHandle( handle );
1149     if (!ret)
1150     {
1151         if (!MoveFileExA( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1152         {
1153             ERR( "Failed to move %s to %s\n", buffer, file );
1154             ret = GetLastError();
1155         }
1156     }
1157     if (ret) DeleteFileA( buffer );
1158
1159 done:
1160     SetLastError( err );  /* restore last error code */
1161     return ret;
1162 }
1163
1164
1165 /******************************************************************************
1166  *           RegSaveKeyW   [ADVAPI32.166]
1167  */
1168 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1169 {
1170     LPSTR fileA = HEAP_strdupWtoA( GetProcessHeap(), 0, file );
1171     DWORD ret = RegSaveKeyA( hkey, fileA, sa );
1172     if (fileA) HeapFree( GetProcessHeap(), 0, fileA );
1173     return ret;
1174 }