Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / windows / leashdll / registry.c
1 #include <windows.h>
2 #include "leash-int.h"
3
4 static
5 LONG
6 write_registry_setting_ex(
7     HKEY hRoot,
8     char* setting,
9     DWORD type,
10     void* buffer,
11     size_t size
12     )
13 {
14     HKEY hKey = 0;
15     LONG rc = 0;
16
17     if (rc = RegCreateKeyEx(hRoot, LEASH_SETTINGS_REGISTRY_KEY_NAME, 0, 0, 0,
18                             KEY_ALL_ACCESS, 0, &hKey, 0))
19         goto cleanup;
20
21     rc = RegSetValueEx(hKey, setting, 0, type, (LPBYTE)buffer, size);
22  cleanup:
23     if (hKey)
24         RegCloseKey(hKey);
25     return rc;
26 }
27
28 LONG
29 write_registry_setting(
30     char* setting,
31     DWORD type,
32     void* buffer,
33     size_t size
34     )
35 {
36     return write_registry_setting_ex(HKEY_CURRENT_USER,
37                                      setting,
38                                      type,
39                                      buffer,
40                                      size);
41 }
42
43 static
44 LONG
45 read_registry_setting_ex(
46     HKEY hRoot,
47     char* setting,
48     void* buffer,
49     size_t size
50     )
51 {
52     HKEY hKey = 0;
53     LONG rc = 0;
54     DWORD dwType;
55     DWORD dwCount;
56
57     if (rc = RegOpenKeyEx(hRoot,
58                           LEASH_SETTINGS_REGISTRY_KEY_NAME,
59                           0, KEY_QUERY_VALUE, &hKey))
60         goto cleanup;
61
62     memset(buffer, 0, size);
63     dwCount = size;
64     rc = RegQueryValueEx(hKey, setting, NULL, &dwType, (LPBYTE)buffer,
65                          &dwCount);
66  cleanup:
67     if (hKey)
68         RegCloseKey(hKey);
69     return rc;
70 }
71
72 LONG
73 read_registry_setting_user(
74     char* setting,
75     void* buffer,
76     size_t size
77     )
78 {
79     return read_registry_setting_ex(HKEY_CURRENT_USER, setting, buffer, size);
80 }
81
82 static
83 LONG
84 read_registry_setting_machine(
85     char* setting,
86     void* buffer,
87     size_t size
88     )
89 {
90     return read_registry_setting_ex(HKEY_LOCAL_MACHINE, setting, buffer, size);
91 }
92
93 LONG
94 read_registry_setting(
95     char* setting,
96     void* buffer,
97     size_t size
98     )
99 {
100     LONG rc;
101     rc = read_registry_setting_user(setting, buffer, size);
102     if (!rc) return rc;
103     rc = read_registry_setting_machine(setting, buffer, size);
104     return rc;
105 }