2000-07-25 17:10:52 +02:00
/*
* Registry management
*
* Copyright ( C ) 1999 Alexandre Julliard
*
* Based on misc / registry . c code
* Copyright ( C ) 1996 Marcus Meissner
* Copyright ( C ) 1998 Matthew Becker
* Copyright ( C ) 1999 Sylvain St - Germain
*
* This file is concerned about handle management and interaction with the Wine server .
* Registry file I / O is in misc / registry . c .
2002-03-10 00:29:33 +01:00
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
2000-07-25 17:10:52 +02:00
*/
2002-08-17 02:43:16 +02:00
# include "config.h"
2000-07-25 17:10:52 +02:00
# include <stdlib.h>
# include <stdio.h>
2002-08-17 02:43:16 +02:00
# ifdef HAVE_UNISTD_H
# include <unistd.h>
# endif
2000-07-25 17:10:52 +02:00
# include "winbase.h"
# include "winreg.h"
# include "winerror.h"
# include "wine/winbase16.h"
2000-08-14 16:41:19 +02:00
# include "wine/unicode.h"
2001-07-19 02:39:09 +02:00
# include "wine/server.h"
2002-03-10 00:29:33 +01:00
# include "wine/debug.h"
2000-07-25 17:10:52 +02:00
2002-03-10 00:29:33 +01:00
WINE_DEFAULT_DEBUG_CHANNEL ( reg ) ;
2000-07-25 17:10:52 +02:00
/* check if value type needs string conversion (Ansi<->Unicode) */
static inline int is_string ( DWORD type )
{
return ( type = = REG_SZ ) | | ( type = = REG_EXPAND_SZ ) | | ( type = = REG_MULTI_SZ ) ;
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegCreateKeyExA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegCreateKeyExA ( HKEY hkey , LPCSTR name , DWORD reserved , LPSTR class ,
2002-06-01 01:06:46 +02:00
DWORD options , REGSAM access , SECURITY_ATTRIBUTES * sa ,
2000-07-25 17:10:52 +02:00
LPHKEY retkey , LPDWORD dispos )
{
2000-10-01 03:44:50 +02:00
OBJECT_ATTRIBUTES attr ;
UNICODE_STRING nameW , classW ;
ANSI_STRING nameA , classA ;
NTSTATUS status ;
2000-07-25 17:10:52 +02:00
if ( reserved ) return ERROR_INVALID_PARAMETER ;
if ( ! ( access & KEY_ALL_ACCESS ) | | ( access & ~ KEY_ALL_ACCESS ) ) return ERROR_ACCESS_DENIED ;
2000-10-01 03:44:50 +02:00
attr . Length = sizeof ( attr ) ;
attr . RootDirectory = hkey ;
attr . ObjectName = & nameW ;
attr . Attributes = 0 ;
attr . SecurityDescriptor = NULL ;
attr . SecurityQualityOfService = NULL ;
RtlInitAnsiString ( & nameA , name ) ;
RtlInitAnsiString ( & classA , class ) ;
/* FIXME: should use Unicode buffer in TEB */
if ( ! ( status = RtlAnsiStringToUnicodeString ( & nameW , & nameA , TRUE ) ) )
2000-07-25 17:10:52 +02:00
{
2000-10-01 03:44:50 +02:00
if ( ! ( status = RtlAnsiStringToUnicodeString ( & classW , & classA , TRUE ) ) )
{
status = NtCreateKey ( retkey , access , & attr , 0 , & classW , options , dispos ) ;
RtlFreeUnicodeString ( & classW ) ;
}
RtlFreeUnicodeString ( & nameW ) ;
2000-07-25 17:10:52 +02:00
}
2000-10-01 03:44:50 +02:00
return RtlNtStatusToDosError ( status ) ;
2000-07-25 17:10:52 +02:00
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegCreateKeyA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegCreateKeyA ( HKEY hkey , LPCSTR name , LPHKEY retkey )
{
return RegCreateKeyExA ( hkey , name , 0 , NULL , REG_OPTION_NON_VOLATILE ,
KEY_ALL_ACCESS , NULL , retkey , NULL ) ;
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegOpenKeyExA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegOpenKeyExA ( HKEY hkey , LPCSTR name , DWORD reserved , REGSAM access , LPHKEY retkey )
{
2000-10-01 03:44:50 +02:00
OBJECT_ATTRIBUTES attr ;
UNICODE_STRING nameW ;
STRING nameA ;
NTSTATUS status ;
attr . Length = sizeof ( attr ) ;
attr . RootDirectory = hkey ;
attr . ObjectName = & nameW ;
attr . Attributes = 0 ;
attr . SecurityDescriptor = NULL ;
attr . SecurityQualityOfService = NULL ;
RtlInitAnsiString ( & nameA , name ) ;
/* FIXME: should use Unicode buffer in TEB */
if ( ! ( status = RtlAnsiStringToUnicodeString ( & nameW , & nameA , TRUE ) ) )
{
status = NtOpenKey ( retkey , access , & attr ) ;
RtlFreeUnicodeString ( & nameW ) ;
}
return RtlNtStatusToDosError ( status ) ;
2000-07-25 17:10:52 +02:00
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegOpenKeyA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegOpenKeyA ( HKEY hkey , LPCSTR name , LPHKEY retkey )
{
return RegOpenKeyExA ( hkey , name , 0 , KEY_ALL_ACCESS , retkey ) ;
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegEnumKeyExA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegEnumKeyExA ( HKEY hkey , DWORD index , LPSTR name , LPDWORD name_len ,
LPDWORD reserved , LPSTR class , LPDWORD class_len , FILETIME * ft )
{
2000-10-02 05:46:58 +02:00
NTSTATUS status ;
char buffer [ 256 ] , * buf_ptr = buffer ;
KEY_NODE_INFORMATION * info = ( KEY_NODE_INFORMATION * ) buffer ;
DWORD total_size ;
2000-07-25 17:10:52 +02:00
TRACE ( " (0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p) \n " , hkey , index , name , name_len ,
name_len ? * name_len : - 1 , reserved , class , class_len , ft ) ;
if ( reserved ) return ERROR_INVALID_PARAMETER ;
2000-10-02 05:46:58 +02:00
status = NtEnumerateKey ( hkey , index , KeyNodeInformation ,
buffer , sizeof ( buffer ) , & total_size ) ;
2000-07-25 17:10:52 +02:00
2000-10-02 05:46:58 +02:00
while ( status = = STATUS_BUFFER_OVERFLOW )
{
/* retry with a dynamically allocated buffer */
if ( buf_ptr ! = buffer ) HeapFree ( GetProcessHeap ( ) , 0 , buf_ptr ) ;
if ( ! ( buf_ptr = HeapAlloc ( GetProcessHeap ( ) , 0 , total_size ) ) )
return ERROR_NOT_ENOUGH_MEMORY ;
info = ( KEY_NODE_INFORMATION * ) buf_ptr ;
status = NtEnumerateKey ( hkey , index , KeyNodeInformation ,
buf_ptr , total_size , & total_size ) ;
}
2000-07-25 17:10:52 +02:00
2000-10-02 05:46:58 +02:00
if ( ! status )
2000-07-25 17:10:52 +02:00
{
2001-12-05 23:18:48 +01:00
DWORD len , cls_len ;
2000-10-02 05:46:58 +02:00
2001-12-05 23:18:48 +01:00
RtlUnicodeToMultiByteSize ( & len , info - > Name , info - > NameLength ) ;
RtlUnicodeToMultiByteSize ( & cls_len , ( WCHAR * ) ( buf_ptr + info - > ClassOffset ) ,
info - > ClassLength ) ;
2001-11-23 19:44:43 +01:00
if ( ft ) * ft = * ( FILETIME * ) & info - > LastWriteTime ;
2000-10-02 05:46:58 +02:00
if ( len > = * name_len | | ( class_len & & ( cls_len > = * class_len ) ) )
status = STATUS_BUFFER_OVERFLOW ;
else
{
* name_len = len ;
2001-12-05 23:18:48 +01:00
RtlUnicodeToMultiByteN ( name , len , NULL , info - > Name , info - > NameLength ) ;
2000-10-02 05:46:58 +02:00
name [ len ] = 0 ;
if ( class_len )
{
* class_len = cls_len ;
if ( class )
{
2001-12-05 23:18:48 +01:00
RtlUnicodeToMultiByteN ( class , cls_len , NULL ,
( WCHAR * ) ( buf_ptr + info - > ClassOffset ) ,
info - > ClassLength ) ;
2000-10-02 05:46:58 +02:00
class [ cls_len ] = 0 ;
}
}
}
2000-07-25 17:10:52 +02:00
}
2000-10-02 05:46:58 +02:00
if ( buf_ptr ! = buffer ) HeapFree ( GetProcessHeap ( ) , 0 , buf_ptr ) ;
return RtlNtStatusToDosError ( status ) ;
2000-07-25 17:10:52 +02:00
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegEnumKeyA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegEnumKeyA ( HKEY hkey , DWORD index , LPSTR name , DWORD name_len )
{
return RegEnumKeyExA ( hkey , index , name , & name_len , NULL , NULL , NULL , NULL ) ;
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegQueryInfoKeyA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegQueryInfoKeyA ( HKEY hkey , LPSTR class , LPDWORD class_len , LPDWORD reserved ,
LPDWORD subkeys , LPDWORD max_subkey , LPDWORD max_class ,
LPDWORD values , LPDWORD max_value , LPDWORD max_data ,
LPDWORD security , FILETIME * modif )
{
2000-10-02 05:46:58 +02:00
NTSTATUS status ;
char buffer [ 256 ] , * buf_ptr = buffer ;
KEY_FULL_INFORMATION * info = ( KEY_FULL_INFORMATION * ) buffer ;
2001-12-05 23:18:48 +01:00
DWORD total_size , len ;
2000-07-25 17:10:52 +02:00
TRACE ( " (0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p) \n " , hkey , class , class_len ? * class_len : 0 ,
reserved , subkeys , max_subkey , values , max_value , max_data , security , modif ) ;
2000-08-14 16:41:19 +02:00
if ( class & & ! class_len & & ! ( GetVersion ( ) & 0x80000000 /*NT*/ ) )
2000-07-25 17:10:52 +02:00
return ERROR_INVALID_PARAMETER ;
2000-10-02 05:46:58 +02:00
status = NtQueryKey ( hkey , KeyFullInformation , buffer , sizeof ( buffer ) , & total_size ) ;
2001-12-05 23:18:48 +01:00
if ( status & & status ! = STATUS_BUFFER_OVERFLOW ) goto done ;
2000-07-25 17:10:52 +02:00
2000-10-02 05:46:58 +02:00
if ( class | | class_len )
2000-07-25 17:10:52 +02:00
{
2000-10-02 05:46:58 +02:00
/* retry with a dynamically allocated buffer */
while ( status = = STATUS_BUFFER_OVERFLOW )
{
if ( buf_ptr ! = buffer ) HeapFree ( GetProcessHeap ( ) , 0 , buf_ptr ) ;
if ( ! ( buf_ptr = HeapAlloc ( GetProcessHeap ( ) , 0 , total_size ) ) )
return ERROR_NOT_ENOUGH_MEMORY ;
info = ( KEY_FULL_INFORMATION * ) buf_ptr ;
status = NtQueryKey ( hkey , KeyFullInformation , buf_ptr , total_size , & total_size ) ;
}
2001-12-05 23:18:48 +01:00
if ( status ) goto done ;
RtlUnicodeToMultiByteSize ( & len , ( WCHAR * ) ( buf_ptr + info - > ClassOffset ) , info - > ClassLength ) ;
if ( class_len )
2000-07-25 17:10:52 +02:00
{
2001-12-05 23:18:48 +01:00
if ( len + 1 > * class_len ) status = STATUS_BUFFER_OVERFLOW ;
* class_len = len ;
}
if ( class & & ! status )
{
RtlUnicodeToMultiByteN ( class , len , NULL , ( WCHAR * ) ( buf_ptr + info - > ClassOffset ) ,
info - > ClassLength ) ;
class [ len ] = 0 ;
2000-07-25 17:10:52 +02:00
}
}
2001-12-05 23:18:48 +01:00
else status = STATUS_SUCCESS ;
2000-10-02 05:46:58 +02:00
2001-12-05 23:18:48 +01:00
if ( subkeys ) * subkeys = info - > SubKeys ;
if ( max_subkey ) * max_subkey = info - > MaxNameLen ;
if ( max_class ) * max_class = info - > MaxClassLen ;
if ( values ) * values = info - > Values ;
if ( max_value ) * max_value = info - > MaxValueNameLen ;
if ( max_data ) * max_data = info - > MaxValueDataLen ;
if ( modif ) * modif = * ( FILETIME * ) & info - > LastWriteTime ;
2000-10-02 05:46:58 +02:00
2001-12-05 23:18:48 +01:00
done :
2000-10-02 05:46:58 +02:00
if ( buf_ptr ! = buffer ) HeapFree ( GetProcessHeap ( ) , 0 , buf_ptr ) ;
return RtlNtStatusToDosError ( status ) ;
2000-07-25 17:10:52 +02:00
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegCloseKey [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*
* Releases the handle of the specified key
*
* PARAMS
* hkey [ I ] Handle of key to close
*
* RETURNS
* Success : ERROR_SUCCESS
* Failure : Error code
*/
DWORD WINAPI RegCloseKey ( HKEY hkey )
{
2002-07-31 21:26:03 +02:00
if ( ! hkey | | hkey > = ( HKEY ) 0x80000000 ) return ERROR_SUCCESS ;
2000-10-01 03:44:50 +02:00
return RtlNtStatusToDosError ( NtClose ( hkey ) ) ;
2000-07-25 17:10:52 +02:00
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegDeleteKeyA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegDeleteKeyA ( HKEY hkey , LPCSTR name )
{
DWORD ret ;
2000-10-01 03:44:50 +02:00
HKEY tmp ;
2000-07-25 17:10:52 +02:00
2000-10-01 03:44:50 +02:00
if ( ! name | | ! * name ) return NtDeleteKey ( hkey ) ;
if ( ! ( ret = RegOpenKeyExA ( hkey , name , 0 , 0 , & tmp ) ) )
{
ret = RtlNtStatusToDosError ( NtDeleteKey ( tmp ) ) ;
RegCloseKey ( tmp ) ;
}
return ret ;
2000-07-25 17:10:52 +02:00
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegSetValueExA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegSetValueExA ( HKEY hkey , LPCSTR name , DWORD reserved , DWORD type ,
2000-10-01 03:44:50 +02:00
CONST BYTE * data , DWORD count )
2000-07-25 17:10:52 +02:00
{
2000-10-01 03:44:50 +02:00
UNICODE_STRING nameW ;
ANSI_STRING nameA ;
WCHAR * dataW = NULL ;
NTSTATUS status ;
2000-07-25 17:10:52 +02:00
2000-10-01 03:44:50 +02:00
if ( count & & is_string ( type ) )
2000-07-25 17:10:52 +02:00
{
/* if user forgot to count terminating null, add it (yes NT does this) */
if ( data [ count - 1 ] & & ! data [ count ] ) count + + ;
}
2000-10-01 03:44:50 +02:00
if ( is_string ( type ) ) /* need to convert to Unicode */
2000-07-25 17:10:52 +02:00
{
2001-12-05 23:18:48 +01:00
DWORD lenW ;
RtlMultiByteToUnicodeSize ( & lenW , data , count ) ;
if ( ! ( dataW = HeapAlloc ( GetProcessHeap ( ) , 0 , lenW ) ) ) return ERROR_OUTOFMEMORY ;
RtlMultiByteToUnicodeN ( dataW , lenW , NULL , data , count ) ;
count = lenW ;
2000-10-01 03:44:50 +02:00
data = ( BYTE * ) dataW ;
}
2000-07-25 17:10:52 +02:00
2000-10-01 03:44:50 +02:00
RtlInitAnsiString ( & nameA , name ) ;
/* FIXME: should use Unicode buffer in TEB */
if ( ! ( status = RtlAnsiStringToUnicodeString ( & nameW , & nameA , TRUE ) ) )
{
status = NtSetValueKey ( hkey , & nameW , 0 , type , data , count ) ;
RtlFreeUnicodeString ( & nameW ) ;
2000-07-25 17:10:52 +02:00
}
2000-10-01 03:44:50 +02:00
if ( dataW ) HeapFree ( GetProcessHeap ( ) , 0 , dataW ) ;
return RtlNtStatusToDosError ( status ) ;
2000-07-25 17:10:52 +02:00
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegSetValueA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegSetValueA ( HKEY hkey , LPCSTR name , DWORD type , LPCSTR data , DWORD count )
{
HKEY subkey = hkey ;
DWORD ret ;
TRACE ( " (0x%x,%s,%ld,%s,%ld) \n " , hkey , debugstr_a ( name ) , type , debugstr_a ( data ) , count ) ;
if ( type ! = REG_SZ ) return ERROR_INVALID_PARAMETER ;
if ( name & & name [ 0 ] ) /* need to create the subkey */
{
if ( ( ret = RegCreateKeyA ( hkey , name , & subkey ) ) ! = ERROR_SUCCESS ) return ret ;
}
ret = RegSetValueExA ( subkey , NULL , 0 , REG_SZ , ( LPBYTE ) data , strlen ( data ) + 1 ) ;
if ( subkey ! = hkey ) RegCloseKey ( subkey ) ;
return ret ;
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegQueryValueExA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*
* NOTES :
2002-06-01 01:06:46 +02:00
* the documentation is wrong : if the buffer is too small it remains untouched
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegQueryValueExA ( HKEY hkey , LPCSTR name , LPDWORD reserved , LPDWORD type ,
2000-10-01 03:44:50 +02:00
LPBYTE data , LPDWORD count )
2000-07-25 17:10:52 +02:00
{
2000-10-01 03:44:50 +02:00
NTSTATUS status ;
ANSI_STRING nameA ;
UNICODE_STRING nameW ;
DWORD total_size ;
2000-10-02 05:46:58 +02:00
char buffer [ 256 ] , * buf_ptr = buffer ;
2000-10-01 03:44:50 +02:00
KEY_VALUE_PARTIAL_INFORMATION * info = ( KEY_VALUE_PARTIAL_INFORMATION * ) buffer ;
2002-07-05 23:30:38 +02:00
static const int info_size = offsetof ( KEY_VALUE_PARTIAL_INFORMATION , Data ) ;
2000-07-25 17:10:52 +02:00
TRACE ( " (0x%x,%s,%p,%p,%p,%p=%ld) \n " ,
hkey , debugstr_a ( name ) , reserved , type , data , count , count ? * count : 0 ) ;
if ( ( data & & ! count ) | | reserved ) return ERROR_INVALID_PARAMETER ;
2000-10-01 03:44:50 +02:00
RtlInitAnsiString ( & nameA , name ) ;
/* FIXME: should use Unicode buffer in TEB */
2000-10-02 05:46:58 +02:00
if ( ( status = RtlAnsiStringToUnicodeString ( & nameW , & nameA , TRUE ) ) )
return RtlNtStatusToDosError ( status ) ;
2000-07-25 17:10:52 +02:00
2000-10-01 03:44:50 +02:00
status = NtQueryValueKey ( hkey , & nameW , KeyValuePartialInformation ,
buffer , sizeof ( buffer ) , & total_size ) ;
if ( status & & status ! = STATUS_BUFFER_OVERFLOW ) goto done ;
2000-07-25 17:10:52 +02:00
2000-10-01 03:44:50 +02:00
/* we need to fetch the contents for a string type even if not requested,
* because we need to compute the length of the ASCII string . */
if ( data | | is_string ( info - > Type ) )
2000-07-25 17:10:52 +02:00
{
2000-10-01 03:44:50 +02:00
/* retry with a dynamically allocated buffer */
while ( status = = STATUS_BUFFER_OVERFLOW )
2000-07-25 17:10:52 +02:00
{
2000-10-01 03:44:50 +02:00
if ( buf_ptr ! = buffer ) HeapFree ( GetProcessHeap ( ) , 0 , buf_ptr ) ;
if ( ! ( buf_ptr = HeapAlloc ( GetProcessHeap ( ) , 0 , total_size ) ) )
2000-10-02 05:46:58 +02:00
{
2000-10-01 03:44:50 +02:00
status = STATUS_NO_MEMORY ;
2000-10-02 05:46:58 +02:00
goto done ;
}
info = ( KEY_VALUE_PARTIAL_INFORMATION * ) buf_ptr ;
status = NtQueryValueKey ( hkey , & nameW , KeyValuePartialInformation ,
buf_ptr , total_size , & total_size ) ;
2000-10-01 03:44:50 +02:00
}
if ( ! status )
{
if ( is_string ( info - > Type ) )
2000-07-25 17:10:52 +02:00
{
2000-10-01 03:44:50 +02:00
DWORD len = WideCharToMultiByte ( CP_ACP , 0 , ( WCHAR * ) ( buf_ptr + info_size ) ,
( total_size - info_size ) / sizeof ( WCHAR ) ,
NULL , 0 , NULL , NULL ) ;
if ( data & & len )
{
if ( len > * count ) status = STATUS_BUFFER_OVERFLOW ;
else
{
WideCharToMultiByte ( CP_ACP , 0 , ( WCHAR * ) ( buf_ptr + info_size ) ,
( total_size - info_size ) / sizeof ( WCHAR ) ,
data , len , NULL , NULL ) ;
/* if the type is REG_SZ and data is not 0-terminated
* and there is enough space in the buffer NT appends a \ 0 */
if ( len < * count & & data [ len - 1 ] ) data [ len ] = 0 ;
}
}
total_size = len + info_size ;
}
2000-10-13 19:04:14 +02:00
else if ( data )
{
if ( total_size - info_size > * count ) status = STATUS_BUFFER_OVERFLOW ;
else memcpy ( data , buf_ptr + info_size , total_size - info_size ) ;
}
2000-07-25 17:10:52 +02:00
}
2000-10-02 05:46:58 +02:00
else if ( status ! = STATUS_BUFFER_OVERFLOW ) goto done ;
2000-07-25 17:10:52 +02:00
}
2000-10-01 03:44:50 +02:00
if ( type ) * type = info - > Type ;
if ( count ) * count = total_size - info_size ;
done :
2000-10-02 05:46:58 +02:00
if ( buf_ptr ! = buffer ) HeapFree ( GetProcessHeap ( ) , 0 , buf_ptr ) ;
RtlFreeUnicodeString ( & nameW ) ;
2000-10-01 03:44:50 +02:00
return RtlNtStatusToDosError ( status ) ;
2000-07-25 17:10:52 +02:00
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegQueryValueA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegQueryValueA ( HKEY hkey , LPCSTR name , LPSTR data , LPLONG count )
{
DWORD ret ;
HKEY subkey = hkey ;
TRACE ( " (%x,%s,%p,%ld) \n " , hkey , debugstr_a ( name ) , data , count ? * count : 0 ) ;
if ( name & & name [ 0 ] )
{
if ( ( ret = RegOpenKeyA ( hkey , name , & subkey ) ) ! = ERROR_SUCCESS ) return ret ;
}
ret = RegQueryValueExA ( subkey , NULL , NULL , NULL , ( LPBYTE ) data , count ) ;
if ( subkey ! = hkey ) RegCloseKey ( subkey ) ;
if ( ret = = ERROR_FILE_NOT_FOUND )
{
/* return empty string if default value not found */
if ( data ) * data = 0 ;
if ( count ) * count = 1 ;
ret = ERROR_SUCCESS ;
}
return ret ;
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegEnumValueA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
2000-12-15 21:57:00 +01:00
DWORD WINAPI RegEnumValueA ( HKEY hkey , DWORD index , LPSTR value , LPDWORD val_count ,
2000-07-25 17:10:52 +02:00
LPDWORD reserved , LPDWORD type , LPBYTE data , LPDWORD count )
{
2000-12-15 21:57:00 +01:00
NTSTATUS status ;
DWORD total_size ;
char buffer [ 256 ] , * buf_ptr = buffer ;
KEY_VALUE_FULL_INFORMATION * info = ( KEY_VALUE_FULL_INFORMATION * ) buffer ;
2002-07-05 23:30:38 +02:00
static const int info_size = offsetof ( KEY_VALUE_FULL_INFORMATION , Name ) ;
2000-07-25 17:10:52 +02:00
TRACE ( " (%x,%ld,%p,%p,%p,%p,%p,%p) \n " ,
hkey , index , value , val_count , reserved , type , data , count ) ;
/* NT only checks count, not val_count */
if ( ( data & & ! count ) | | reserved ) return ERROR_INVALID_PARAMETER ;
2000-12-15 21:57:00 +01:00
total_size = info_size + ( MAX_PATH + 1 ) * sizeof ( WCHAR ) ;
if ( data ) total_size + = * count ;
total_size = min ( sizeof ( buffer ) , total_size ) ;
2000-07-25 17:10:52 +02:00
2000-12-15 21:57:00 +01:00
status = NtEnumerateValueKey ( hkey , index , KeyValueFullInformation ,
buffer , total_size , & total_size ) ;
if ( status & & status ! = STATUS_BUFFER_OVERFLOW ) goto done ;
2000-07-25 17:10:52 +02:00
2000-12-15 21:57:00 +01:00
/* we need to fetch the contents for a string type even if not requested,
* because we need to compute the length of the ASCII string . */
if ( value | | data | | is_string ( info - > Type ) )
2000-07-25 17:10:52 +02:00
{
2000-12-15 21:57:00 +01:00
/* retry with a dynamically allocated buffer */
while ( status = = STATUS_BUFFER_OVERFLOW )
2000-07-25 17:10:52 +02:00
{
2000-12-15 21:57:00 +01:00
if ( buf_ptr ! = buffer ) HeapFree ( GetProcessHeap ( ) , 0 , buf_ptr ) ;
if ( ! ( buf_ptr = HeapAlloc ( GetProcessHeap ( ) , 0 , total_size ) ) )
return ERROR_NOT_ENOUGH_MEMORY ;
info = ( KEY_VALUE_FULL_INFORMATION * ) buf_ptr ;
status = NtEnumerateValueKey ( hkey , index , KeyValueFullInformation ,
buf_ptr , total_size , & total_size ) ;
2000-07-25 17:10:52 +02:00
}
2000-12-15 21:57:00 +01:00
if ( status ) goto done ;
2000-07-25 17:10:52 +02:00
2000-12-15 21:57:00 +01:00
if ( is_string ( info - > Type ) )
2000-07-25 17:10:52 +02:00
{
2001-12-05 23:18:48 +01:00
DWORD len ;
RtlUnicodeToMultiByteSize ( & len , ( WCHAR * ) ( buf_ptr + info - > DataOffset ) ,
total_size - info - > DataOffset ) ;
2000-12-15 21:57:00 +01:00
if ( data & & len )
2000-07-25 17:10:52 +02:00
{
2002-07-10 05:27:35 +02:00
if ( len > * count ) status = STATUS_BUFFER_OVERFLOW ;
else
2000-12-15 21:57:00 +01:00
{
2002-07-10 05:27:35 +02:00
RtlUnicodeToMultiByteN ( data , len , NULL , ( WCHAR * ) ( buf_ptr + info - > DataOffset ) ,
total_size - info - > DataOffset ) ;
/* if the type is REG_SZ and data is not 0-terminated
* and there is enough space in the buffer NT appends a \ 0 */
if ( len < * count & & data [ len - 1 ] ) data [ len ] = 0 ;
2000-12-15 21:57:00 +01:00
}
2000-07-25 17:10:52 +02:00
}
2000-12-15 21:57:00 +01:00
info - > DataLength = len ;
}
else if ( data )
{
if ( total_size - info - > DataOffset > * count ) status = STATUS_BUFFER_OVERFLOW ;
else memcpy ( data , buf_ptr + info - > DataOffset , total_size - info - > DataOffset ) ;
2000-07-25 17:10:52 +02:00
}
2002-07-10 05:27:35 +02:00
if ( value & & ! status )
{
DWORD len ;
RtlUnicodeToMultiByteSize ( & len , info - > Name , info - > NameLength ) ;
if ( len > = * val_count )
{
status = STATUS_BUFFER_OVERFLOW ;
if ( * val_count )
{
len = * val_count - 1 ;
RtlUnicodeToMultiByteN ( value , len , NULL , info - > Name , info - > NameLength ) ;
value [ len ] = 0 ;
}
}
else
{
RtlUnicodeToMultiByteN ( value , len , NULL , info - > Name , info - > NameLength ) ;
value [ len ] = 0 ;
* val_count = len ;
}
}
2000-07-25 17:10:52 +02:00
}
2001-12-05 23:18:48 +01:00
else status = STATUS_SUCCESS ;
2000-07-25 17:10:52 +02:00
2000-12-15 21:57:00 +01:00
if ( type ) * type = info - > Type ;
if ( count ) * count = info - > DataLength ;
2000-07-25 17:10:52 +02:00
2000-12-15 21:57:00 +01:00
done :
if ( buf_ptr ! = buffer ) HeapFree ( GetProcessHeap ( ) , 0 , buf_ptr ) ;
return RtlNtStatusToDosError ( status ) ;
2000-07-25 17:10:52 +02:00
}
2000-12-15 21:57:00 +01:00
2000-07-25 17:10:52 +02:00
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegDeleteValueA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
DWORD WINAPI RegDeleteValueA ( HKEY hkey , LPCSTR name )
{
2000-10-01 03:44:50 +02:00
UNICODE_STRING nameW ;
STRING nameA ;
NTSTATUS status ;
2000-07-25 17:10:52 +02:00
2000-10-01 03:44:50 +02:00
RtlInitAnsiString ( & nameA , name ) ;
/* FIXME: should use Unicode buffer in TEB */
if ( ! ( status = RtlAnsiStringToUnicodeString ( & nameW , & nameA , TRUE ) ) )
{
status = NtDeleteValueKey ( hkey , & nameW ) ;
RtlFreeUnicodeString ( & nameW ) ;
}
return RtlNtStatusToDosError ( status ) ;
2000-07-25 17:10:52 +02:00
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegLoadKeyA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*/
LONG WINAPI RegLoadKeyA ( HKEY hkey , LPCSTR subkey , LPCSTR filename )
{
HANDLE file ;
2001-11-30 19:46:42 +01:00
WCHAR buffer [ MAX_PATH ] ;
2000-10-15 02:40:25 +02:00
DWORD ret , len , err = GetLastError ( ) ;
2000-07-25 17:10:52 +02:00
TRACE ( " (%x,%s,%s) \n " , hkey , debugstr_a ( subkey ) , debugstr_a ( filename ) ) ;
if ( ! filename | | ! * filename ) return ERROR_INVALID_PARAMETER ;
if ( ! subkey | | ! * subkey ) return ERROR_INVALID_PARAMETER ;
2001-11-30 19:46:42 +01:00
if ( ! ( len = MultiByteToWideChar ( CP_ACP , 0 , subkey , strlen ( subkey ) , buffer , MAX_PATH ) ) )
return ERROR_INVALID_PARAMETER ;
2000-10-15 02:40:25 +02:00
2000-07-25 17:10:52 +02:00
if ( ( file = CreateFileA ( filename , GENERIC_READ , 0 , NULL , OPEN_EXISTING ,
2001-01-06 02:29:18 +01:00
FILE_ATTRIBUTE_NORMAL , 0 ) ) = = INVALID_HANDLE_VALUE )
2000-07-25 17:10:52 +02:00
{
ret = GetLastError ( ) ;
goto done ;
}
2000-10-15 02:40:25 +02:00
2001-11-30 19:46:42 +01:00
SERVER_START_REQ ( load_registry )
2000-10-15 02:40:25 +02:00
{
req - > hkey = hkey ;
req - > file = file ;
2001-11-30 19:46:42 +01:00
wine_server_add_data ( req , buffer , len * sizeof ( WCHAR ) ) ;
ret = RtlNtStatusToDosError ( wine_server_call ( req ) ) ;
2000-10-15 02:40:25 +02:00
}
2001-11-30 19:46:42 +01:00
SERVER_END_REQ ;
2000-07-25 17:10:52 +02:00
CloseHandle ( file ) ;
done :
SetLastError ( err ) ; /* restore the last error code */
return ret ;
}
/******************************************************************************
2001-06-13 22:13:18 +02:00
* RegSaveKeyA [ ADVAPI32 . @ ]
2000-07-25 17:10:52 +02:00
*
* PARAMS
* hkey [ I ] Handle of key where save begins
* lpFile [ I ] Address of filename to save to
* sa [ I ] Address of security structure
*/
LONG WINAPI RegSaveKeyA ( HKEY hkey , LPCSTR file , LPSECURITY_ATTRIBUTES sa )
{
char buffer [ 1024 ] ;
int count = 0 ;
LPSTR name ;
DWORD ret , err ;
2001-01-06 02:29:18 +01:00
HANDLE handle ;
2000-07-25 17:10:52 +02:00
TRACE ( " (%x,%s,%p) \n " , hkey , debugstr_a ( file ) , sa ) ;
if ( ! file | | ! * file ) return ERROR_INVALID_PARAMETER ;
err = GetLastError ( ) ;
GetFullPathNameA ( file , sizeof ( buffer ) , buffer , & name ) ;
for ( ; ; )
{
sprintf ( name , " reg%04x.tmp " , count + + ) ;
handle = CreateFileA ( buffer , GENERIC_WRITE , 0 , NULL ,
2001-01-06 02:29:18 +01:00
CREATE_NEW , FILE_ATTRIBUTE_NORMAL , 0 ) ;
2000-07-25 17:10:52 +02:00
if ( handle ! = INVALID_HANDLE_VALUE ) break ;
if ( ( ret = GetLastError ( ) ) ! = ERROR_ALREADY_EXISTS ) goto done ;
/* Something gone haywire ? Please report if this happens abnormally */
if ( count > = 100 )
MESSAGE ( " Wow, we are already fiddling with a temp file %s with an ordinal as high as %d ! \n You might want to delete all corresponding temp files in that directory. \n " , buffer , count ) ;
}
2001-02-27 03:09:16 +01:00
SERVER_START_REQ ( save_registry )
2000-10-15 02:40:25 +02:00
{
req - > hkey = hkey ;
req - > file = handle ;
2001-11-30 19:46:42 +01:00
ret = RtlNtStatusToDosError ( wine_server_call ( req ) ) ;
2000-10-15 02:40:25 +02:00
}
SERVER_END_REQ ;
2000-07-25 17:10:52 +02:00
CloseHandle ( handle ) ;
if ( ! ret )
{
if ( ! MoveFileExA ( buffer , file , MOVEFILE_REPLACE_EXISTING ) )
{
ERR ( " Failed to move %s to %s \n " , buffer , file ) ;
ret = GetLastError ( ) ;
}
}
if ( ret ) DeleteFileA ( buffer ) ;
done :
SetLastError ( err ) ; /* restore last error code */
return ret ;
}