607e5fcb5d031144e78a45116c368bf7450d509f
[reactos.git] / reactos / drivers / filesystems / udfs / Include / regtools.cpp
1 ////////////////////////////////////////////////////////////////////
2 // Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
3 // All rights reserved
4 // This file was released under the GPLv2 on June 2015.
5 ////////////////////////////////////////////////////////////////////
6
7 #include "regtools.h"
8
9 #ifndef WIN_32_MODE
10
11 NTSTATUS
12 RegTGetKeyHandle(
13 IN HKEY hRootKey,
14 IN PWCHAR KeyName,
15 OUT HKEY* hKey
16 )
17 {
18 OBJECT_ATTRIBUTES ObjectAttributes;
19 UNICODE_STRING NameString;
20 NTSTATUS status;
21
22 //KdPrint(("RegTGetKeyHandle: h=%x, %S\n", hRootKey, KeyName));
23
24 NameString.Buffer = KeyName;
25 NameString.Length = wcslen(KeyName)*sizeof(WCHAR);
26 NameString.MaximumLength = NameString.Length + sizeof(WCHAR);
27
28 InitializeObjectAttributes(
29 &ObjectAttributes,
30 &NameString,
31 OBJ_CASE_INSENSITIVE,
32 hRootKey,
33 NULL
34 );
35
36 status = ZwOpenKey(
37 hKey,
38 KEY_WRITE | KEY_READ,
39 &ObjectAttributes
40 );
41
42 if(!NT_SUCCESS(status)) {
43 //KdPrint((" status %x\n", status));
44 *hKey = NULL;
45 }
46
47 return status;
48 } // end RegTGetKeyHandle()
49
50 VOID
51 RegTCloseKeyHandle(
52 IN HKEY hKey
53 )
54 {
55 ZwClose(hKey);
56 } // end RegTCloseKeyHandle()
57
58 #else //WIN_32_MODE
59
60 NTSTATUS
61 RegTGetKeyHandle(
62 IN HKEY hRootKey,
63 IN PWCHAR KeyName,
64 OUT HKEY* hKey
65 )
66 {
67 LONG status;
68
69 if(!hRootKey)
70 hRootKey = HKEY_LOCAL_MACHINE;
71
72 status = RegOpenKeyExW(
73 hRootKey,
74 KeyName,
75 0,
76 KEY_WRITE | KEY_READ,
77 hKey
78 );
79
80 if(status != ERROR_SUCCESS) {
81 *hKey = NULL;
82 }
83
84 return status;
85 } // end RegTGetKeyHandle()
86
87 VOID
88 RegTCloseKeyHandle(
89 IN HKEY hKey
90 )
91 {
92 if(!hKey) {
93 return;
94 }
95 RegCloseKey(hKey);
96 } // end RegTCloseKeyHandle()
97
98 #endif //WIN_32_MODE
99
100 BOOLEAN
101 RegTGetDwordValue(
102 IN HKEY hRootKey,
103 IN PWSTR RegistryPath,
104 IN PWSTR Name,
105 IN PULONG pUlong
106 )
107 {
108 #ifndef WIN_32_MODE
109 UNICODE_STRING NameString;
110 PKEY_VALUE_PARTIAL_INFORMATION ValInfo;
111 #endif //WIN_32_MODE
112 ULONG len;
113 NTSTATUS status;
114 HKEY hKey;
115 BOOLEAN retval = FALSE;
116 BOOLEAN free_h = FALSE;
117
118 #ifdef WIN_32_MODE
119 if(!hRootKey)
120 hRootKey = HKEY_LOCAL_MACHINE;
121 #endif //WIN_32_MODE
122
123 if(RegistryPath && RegistryPath[0]) {
124 status = RegTGetKeyHandle(hRootKey, RegistryPath, &hKey);
125 #ifdef WIN_32_MODE
126 if(status != ERROR_SUCCESS)
127 #else //WIN_32_MODE
128 if(!NT_SUCCESS(status))
129 #endif //WIN_32_MODE
130 return FALSE;
131 free_h = TRUE;
132 } else {
133 hKey = hRootKey;
134 }
135 if(!hKey)
136 return FALSE;
137
138 #ifndef WIN_32_MODE
139 /*
140 KdPrint(("h=%x|%S, %S (%x)\n",
141 hRootKey, RegistryPath, Name, *pUlong));
142 */
143 len = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG) + 0x20;
144 ValInfo = (PKEY_VALUE_PARTIAL_INFORMATION)
145 MyAllocatePool__(NonPagedPool, len);
146 if(!ValInfo) {
147 if(free_h) {
148 RegTCloseKeyHandle(hKey);
149 }
150 return FALSE;
151 }
152
153 NameString.Buffer = Name;
154 NameString.Length = wcslen(Name)*sizeof(WCHAR);
155 NameString.MaximumLength = NameString.Length + sizeof(WCHAR);
156
157 status = ZwQueryValueKey(hKey,
158 &NameString,
159 KeyValuePartialInformation,
160 ValInfo,
161 len,
162 &len);
163 if(NT_SUCCESS(status) &&
164 ValInfo->DataLength == sizeof(ULONG)) {
165 RtlCopyMemory(pUlong, ValInfo->Data, sizeof(ULONG));
166 retval = TRUE;
167 //KdPrint((" -> %x\n",*pUlong));
168 } else {
169 //KdPrint((" err %x\n",status));
170 }
171
172 MyFreePool__(ValInfo);
173 #else //WIN_32_MODE
174 len = sizeof(ULONG);
175 if (ERROR_SUCCESS == RegQueryValueExW(
176 hKey, // handle of key to query
177 Name, // address of name of value to query
178 0, // reserved
179 NULL, // address of buffer for value type
180 (BYTE *)pUlong, // address of data buffer
181 &len // address of data buffer size
182 ) && len == sizeof(ULONG)) {
183 retval = TRUE;
184 }
185 #endif //WIN_32_MODE
186 if(free_h) {
187 RegTCloseKeyHandle(hKey);
188 }
189 return retval;
190 } // end RegTGetDwordValue()
191
192 BOOLEAN
193 RegTGetStringValue(
194 IN HKEY hRootKey,
195 IN PWSTR RegistryPath,
196 IN PWSTR Name,
197 IN PWCHAR pStr,
198 IN ULONG MaxLen
199 )
200 {
201 #ifndef WIN_32_MODE
202 UNICODE_STRING NameString;
203 PKEY_VALUE_PARTIAL_INFORMATION ValInfo;
204 #endif //USER_MODE
205 ULONG len;
206 NTSTATUS status;
207 HKEY hKey;
208 BOOLEAN retval = FALSE;
209 BOOLEAN free_h = FALSE;
210
211 #ifdef WIN_32_MODE
212 if(!hRootKey)
213 hRootKey = HKEY_LOCAL_MACHINE;
214 #endif //WIN_32_MODE
215
216 if(RegistryPath && RegistryPath[0]) {
217 status = RegTGetKeyHandle(hRootKey, RegistryPath, &hKey);
218 #ifdef WIN_32_MODE
219 if(status != ERROR_SUCCESS)
220 #else //WIN_32_MODE
221 if(!NT_SUCCESS(status))
222 #endif //WIN_32_MODE
223 return FALSE;
224 free_h = TRUE;
225 } else {
226 hKey = hRootKey;
227 }
228 if(!hKey)
229 return FALSE;
230
231 pStr[0] = 0;
232 #ifndef WIN_32_MODE
233 len = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + MaxLen + 0x20;
234 ValInfo = (PKEY_VALUE_PARTIAL_INFORMATION)
235 MyAllocatePool__(NonPagedPool, len);
236 if(!ValInfo) {
237 if(free_h) {
238 RegTCloseKeyHandle(hKey);
239 }
240 return FALSE;
241 }
242
243 NameString.Buffer = Name;
244 NameString.Length = wcslen(Name)*sizeof(WCHAR);
245 NameString.MaximumLength = NameString.Length + sizeof(WCHAR);
246
247 status = ZwQueryValueKey(hKey,
248 &NameString,
249 KeyValuePartialInformation,
250 ValInfo,
251 len,
252 &len);
253 if(NT_SUCCESS(status) &&
254 ValInfo->DataLength) {
255 RtlCopyMemory(pStr, ValInfo->Data, min(ValInfo->DataLength, MaxLen) );
256 if(pStr[(ValInfo->DataLength)/sizeof(WCHAR)-1]) {
257 pStr[(ValInfo->DataLength)/sizeof(WCHAR)-1] = 0;
258 }
259 retval = TRUE;
260 }
261
262 MyFreePool__(ValInfo);
263 #else //WIN_32_MODE
264 len = MaxLen;
265 if (ERROR_SUCCESS == RegQueryValueExW(
266 hKey, // handle of key to query
267 Name, // address of name of value to query
268 0, // reserved
269 NULL, // address of buffer for value type
270 (BYTE *)pStr, // address of data buffer
271 &len // address of data buffer size
272 ) && len) {
273 if(pStr[len-1]) {
274 pStr[len-1] = 0;
275 }
276 retval = TRUE;
277 }
278 #endif //WIN_32_MODE
279
280 if(free_h) {
281 RegTCloseKeyHandle(hKey);
282 }
283 return retval;
284 } // end RegTGetStringValue()
285