~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to common/w32reg.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* w32reg.c -  MS-Windows Registry access
 
2
 *      Copyright (C) 1999, 2002 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
6
 * GnuPG is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * GnuPG is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#ifdef HAVE_W32_SYSTEM
 
23
 /* This module is only used in this environment */
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#include <stdarg.h>
 
29
#include <windows.h>
 
30
 
 
31
#include "util.h"
 
32
#include "sysutils.h"
 
33
 
 
34
static HKEY
 
35
get_root_key(const char *root)
 
36
{
 
37
    HKEY root_key;
 
38
        
 
39
    if( !root )
 
40
        root_key = HKEY_CURRENT_USER;
 
41
    else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
 
42
        root_key = HKEY_CLASSES_ROOT;
 
43
    else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
 
44
        root_key = HKEY_CURRENT_USER;
 
45
    else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
 
46
        root_key = HKEY_LOCAL_MACHINE;
 
47
    else if( !strcmp( root, "HKEY_USERS" ) )
 
48
        root_key = HKEY_USERS;
 
49
    else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
 
50
        root_key = HKEY_PERFORMANCE_DATA;
 
51
    else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
 
52
        root_key = HKEY_CURRENT_CONFIG;
 
53
    else
 
54
        return NULL;
 
55
        
 
56
    return root_key;
 
57
}
 
58
 
 
59
 
 
60
/****************
 
61
 * Return a string from the Win32 Registry or NULL in case of
 
62
 * error.  Caller must release the return value.   A NULL for root
 
63
 * is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn.
 
64
 * NOTE: The value is allocated with a plain malloc() - use free() and not
 
65
 * the usual m_free()!!!
 
66
 */
 
67
char *
 
68
read_w32_registry_string( const char *root, const char *dir, const char *name )
 
69
{
 
70
    HKEY root_key, key_handle;
 
71
    DWORD n1, nbytes, type;
 
72
    char *result = NULL;
 
73
 
 
74
    if ( !(root_key = get_root_key(root) ) )
 
75
        return NULL;
 
76
 
 
77
    if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
 
78
      {
 
79
        if (root)
 
80
          return NULL; /* no need for a RegClose, so return direct */
 
81
        /* It seems to be common practise to fall back to HLM. */
 
82
        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
 
83
          return NULL; /* still no need for a RegClose, so return direct */
 
84
      }
 
85
 
 
86
    nbytes = 1;
 
87
    if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) )
 
88
        goto leave;
 
89
    result = malloc( (n1=nbytes+1) );
 
90
    if( !result )
 
91
        goto leave;
 
92
    if( RegQueryValueEx( key_handle, name, 0, &type, result, &n1 ) ) {
 
93
        free(result); result = NULL;
 
94
        goto leave;
 
95
    }
 
96
    result[nbytes] = 0; /* make sure it is really a string  */
 
97
    if (type == REG_EXPAND_SZ && strchr (result, '%')) {
 
98
        char *tmp;
 
99
        
 
100
        n1 += 1000;
 
101
        tmp = malloc (n1+1);
 
102
        if (!tmp)
 
103
            goto leave;
 
104
        nbytes = ExpandEnvironmentStrings (result, tmp, n1);
 
105
        if (nbytes && nbytes > n1) {
 
106
            free (tmp);
 
107
            n1 = nbytes;
 
108
            tmp = malloc (n1 + 1);
 
109
            if (!tmp)
 
110
                goto leave;
 
111
            nbytes = ExpandEnvironmentStrings (result, tmp, n1);
 
112
            if (nbytes && nbytes > n1) {
 
113
                free (tmp); /* oops - truncated, better don't expand at all */
 
114
                goto leave;
 
115
            }
 
116
            tmp[nbytes] = 0;
 
117
            free (result);
 
118
            result = tmp;
 
119
        }
 
120
        else if (nbytes) { /* okay, reduce the length */
 
121
            tmp[nbytes] = 0;
 
122
            free (result);
 
123
            result = malloc (strlen (tmp)+1);
 
124
            if (!result)
 
125
                result = tmp;
 
126
            else {
 
127
                strcpy (result, tmp);
 
128
                free (tmp);
 
129
            }
 
130
        }
 
131
        else {  /* error - don't expand */
 
132
            free (tmp);
 
133
        }
 
134
    }
 
135
 
 
136
  leave:
 
137
    RegCloseKey( key_handle );
 
138
    return result;
 
139
}
 
140
 
 
141
 
 
142
int
 
143
write_w32_registry_string(const char *root, const char *dir, 
 
144
                          const char *name, const char *value)
 
145
{
 
146
    HKEY root_key, reg_key;
 
147
        
 
148
    if ( !(root_key = get_root_key(root) ) )
 
149
        return -1;
 
150
 
 
151
    if ( RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key ) 
 
152
         != ERROR_SUCCESS )
 
153
        return -1;
 
154
        
 
155
    if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value, 
 
156
                        strlen( value ) ) != ERROR_SUCCESS ) {
 
157
        if ( RegCreateKey( root_key, name, &reg_key ) != ERROR_SUCCESS ) {
 
158
            RegCloseKey(reg_key);
 
159
            return -1;
 
160
        }
 
161
        if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
 
162
                            strlen( value ) ) != ERROR_SUCCESS ) {
 
163
            RegCloseKey(reg_key);
 
164
            return -1;
 
165
        }
 
166
    }
 
167
 
 
168
    RegCloseKey( reg_key );
 
169
        
 
170
    return 0;
 
171
}
 
172
 
 
173
#endif /*HAVE_W32_SYSTEM*/