xref: /aosp_15_r20/external/cronet/base/win/registry.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/win/registry.h"
6 
7 #include <ntstatus.h>
8 #include <stddef.h>
9 
10 #include <algorithm>
11 #include <iterator>
12 #include <memory>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include "base/check_op.h"
18 #include "base/functional/callback.h"
19 #include "base/notreached.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/string_util_win.h"
22 #include "base/threading/thread_restrictions.h"
23 #include "base/win/object_watcher.h"
24 #include "base/win/scoped_handle.h"
25 #include "base/win/shlwapi.h"
26 
27 extern "C" NTSTATUS WINAPI NtDeleteKey(IN HANDLE KeyHandle);
28 
29 namespace base::win {
30 
31 namespace {
32 
33 // RegEnumValue() reports the number of characters from the name that were
34 // written to the buffer, not how many there are. This constant is the maximum
35 // name size, such that a buffer with this size should read any name.
36 constexpr DWORD MAX_REGISTRY_NAME_SIZE = 16384;
37 
38 // Registry values are read as BYTE* but can have wchar_t* data whose last
39 // wchar_t is truncated. This function converts the reported |byte_size| to
40 // a size in wchar_t that can store a truncated wchar_t if necessary.
to_wchar_size(DWORD byte_size)41 inline DWORD to_wchar_size(DWORD byte_size) {
42   return (byte_size + sizeof(wchar_t) - 1) / sizeof(wchar_t);
43 }
44 
45 // Mask to pull WOW64 access flags out of REGSAM access.
46 constexpr REGSAM kWow64AccessMask = KEY_WOW64_32KEY | KEY_WOW64_64KEY;
47 
48 constexpr DWORD kInvalidIterValue = static_cast<DWORD>(-1);
49 
50 }  // namespace
51 
52 // Watches for modifications to a key.
53 class RegKey::Watcher : public ObjectWatcher::Delegate {
54  public:
55   Watcher() = default;
56 
57   Watcher(const Watcher&) = delete;
58   Watcher& operator=(const Watcher&) = delete;
59 
60   ~Watcher() override = default;
61 
62   bool StartWatching(HKEY key, ChangeCallback callback);
63 
64   // ObjectWatcher::Delegate:
OnObjectSignaled(HANDLE object)65   void OnObjectSignaled(HANDLE object) override {
66     DCHECK(watch_event_.is_valid());
67     DCHECK_EQ(watch_event_.get(), object);
68     std::move(callback_).Run();
69   }
70 
71  private:
72   ScopedHandle watch_event_;
73   ObjectWatcher object_watcher_;
74   ChangeCallback callback_;
75 };
76 
StartWatching(HKEY key,ChangeCallback callback)77 bool RegKey::Watcher::StartWatching(HKEY key, ChangeCallback callback) {
78   DCHECK(key);
79   DCHECK(callback_.is_null());
80 
81   if (!watch_event_.is_valid()) {
82     watch_event_.Set(CreateEvent(nullptr, TRUE, FALSE, nullptr));
83   }
84 
85   if (!watch_event_.is_valid()) {
86     return false;
87   }
88 
89   DWORD filter = REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_ATTRIBUTES |
90                  REG_NOTIFY_CHANGE_LAST_SET | REG_NOTIFY_CHANGE_SECURITY |
91                  REG_NOTIFY_THREAD_AGNOSTIC;
92   // Watch the registry key for a change of value.
93   LONG result =
94       RegNotifyChangeKeyValue(key, /*bWatchSubtree=*/TRUE, filter,
95                               watch_event_.get(), /*fAsynchronous=*/TRUE);
96   if (result != ERROR_SUCCESS) {
97     watch_event_.Close();
98     return false;
99   }
100 
101   callback_ = std::move(callback);
102   return object_watcher_.StartWatchingOnce(watch_event_.get(), this);
103 }
104 
105 // RegKey ----------------------------------------------------------------------
106 
107 RegKey::RegKey() = default;
108 
RegKey(HKEY key)109 RegKey::RegKey(HKEY key) : key_(key) {}
110 
RegKey(HKEY rootkey,const wchar_t * subkey,REGSAM access)111 RegKey::RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
112   if (rootkey) {
113     if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK)) {
114       (void)Create(rootkey, subkey, access);
115     } else {
116       (void)Open(rootkey, subkey, access);
117     }
118   } else {
119     DCHECK(!subkey);
120     wow64access_ = access & kWow64AccessMask;
121   }
122 }
123 
RegKey(RegKey && other)124 RegKey::RegKey(RegKey&& other) noexcept
125     : key_(other.key_),
126       wow64access_(other.wow64access_),
127       key_watcher_(std::move(other.key_watcher_)) {
128   other.key_ = nullptr;
129   other.wow64access_ = 0;
130 }
131 
operator =(RegKey && other)132 RegKey& RegKey::operator=(RegKey&& other) {
133   Close();
134   std::swap(key_, other.key_);
135   std::swap(wow64access_, other.wow64access_);
136   key_watcher_ = std::move(other.key_watcher_);
137   return *this;
138 }
139 
~RegKey()140 RegKey::~RegKey() {
141   Close();
142 }
143 
Create(HKEY rootkey,const wchar_t * subkey,REGSAM access)144 LONG RegKey::Create(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
145   DWORD disposition_value;
146   return CreateWithDisposition(rootkey, subkey, &disposition_value, access);
147 }
148 
CreateWithDisposition(HKEY rootkey,const wchar_t * subkey,DWORD * disposition,REGSAM access)149 LONG RegKey::CreateWithDisposition(HKEY rootkey,
150                                    const wchar_t* subkey,
151                                    DWORD* disposition,
152                                    REGSAM access) {
153   DCHECK(rootkey && subkey && access && disposition);
154   HKEY subhkey = nullptr;
155   LONG result =
156       RegCreateKeyEx(rootkey, subkey, 0, nullptr, REG_OPTION_NON_VOLATILE,
157                      access, nullptr, &subhkey, disposition);
158   if (result == ERROR_SUCCESS) {
159     Close();
160     key_ = subhkey;
161     wow64access_ = access & kWow64AccessMask;
162   }
163 
164   return result;
165 }
166 
CreateKey(const wchar_t * name,REGSAM access)167 LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) {
168   DCHECK(name && access);
169 
170   if (!Valid()) {
171     // The parent key has not been opened or created.
172     return ERROR_INVALID_HANDLE;
173   }
174 
175   // After the application has accessed an alternate registry view using one
176   // of the [KEY_WOW64_32KEY / KEY_WOW64_64KEY] flags, all subsequent
177   // operations (create, delete, or open) on child registry keys must
178   // explicitly use the same flag. Otherwise, there can be unexpected
179   // behavior.
180   // http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx.
181   if ((access & kWow64AccessMask) != wow64access_) {
182     NOTREACHED();
183     return ERROR_INVALID_PARAMETER;
184   }
185   HKEY subkey = nullptr;
186   LONG result = RegCreateKeyEx(key_, name, 0, nullptr, REG_OPTION_NON_VOLATILE,
187                                access, nullptr, &subkey, nullptr);
188   if (result == ERROR_SUCCESS) {
189     Close();
190     key_ = subkey;
191     wow64access_ = access & kWow64AccessMask;
192   }
193 
194   return result;
195 }
196 
Open(HKEY rootkey,const wchar_t * subkey,REGSAM access)197 LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
198   return Open(rootkey, subkey, /*options=*/0, access);
199 }
200 
OpenKey(const wchar_t * relative_key_name,REGSAM access)201 LONG RegKey::OpenKey(const wchar_t* relative_key_name, REGSAM access) {
202   DCHECK(relative_key_name && access);
203 
204   if (!Valid()) {
205     // The parent key has not been opened or created.
206     return ERROR_INVALID_HANDLE;
207   }
208 
209   // After the application has accessed an alternate registry view using one
210   // of the [KEY_WOW64_32KEY / KEY_WOW64_64KEY] flags, all subsequent
211   // operations (create, delete, or open) on child registry keys must
212   // explicitly use the same flag. Otherwise, there can be unexpected
213   // behavior.
214   // http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx.
215   if ((access & kWow64AccessMask) != wow64access_) {
216     NOTREACHED();
217     return ERROR_INVALID_PARAMETER;
218   }
219   HKEY subkey = nullptr;
220   LONG result = RegOpenKeyEx(key_, relative_key_name, 0, access, &subkey);
221 
222   // We have to close the current opened key before replacing it with the new
223   // one.
224   if (result == ERROR_SUCCESS) {
225     Close();
226     key_ = subkey;
227     wow64access_ = access & kWow64AccessMask;
228   }
229   return result;
230 }
231 
Close()232 void RegKey::Close() {
233   if (key_) {
234     ::RegCloseKey(key_);
235     key_ = nullptr;
236     wow64access_ = 0;
237   }
238 }
239 
240 // TODO(wfh): Remove this and other unsafe methods. See http://crbug.com/375400
Set(HKEY key)241 void RegKey::Set(HKEY key) {
242   if (key_ != key) {
243     Close();
244     key_ = key;
245   }
246 }
247 
Take()248 HKEY RegKey::Take() {
249   DCHECK_EQ(wow64access_, 0u);
250   HKEY key = key_;
251   key_ = nullptr;
252   return key;
253 }
254 
HasValue(const wchar_t * name) const255 bool RegKey::HasValue(const wchar_t* name) const {
256   return RegQueryValueEx(key_, name, nullptr, nullptr, nullptr, nullptr) ==
257          ERROR_SUCCESS;
258 }
259 
GetValueCount() const260 base::expected<DWORD, LONG> RegKey::GetValueCount() const {
261   DWORD count = 0;
262   LONG result =
263       RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr, nullptr,
264                       nullptr, &count, nullptr, nullptr, nullptr, nullptr);
265   if (result == ERROR_SUCCESS) {
266     return base::ok(count);
267   }
268   return base::unexpected(result);
269 }
270 
GetValueNameAt(DWORD index,std::wstring * name) const271 LONG RegKey::GetValueNameAt(DWORD index, std::wstring* name) const {
272   wchar_t buf[256];
273   DWORD bufsize = std::size(buf);
274   LONG r = ::RegEnumValue(key_, index, buf, &bufsize, nullptr, nullptr, nullptr,
275                           nullptr);
276   if (r == ERROR_SUCCESS) {
277     name->assign(buf, bufsize);
278   }
279 
280   return r;
281 }
282 
DeleteKey(const wchar_t * name,RecursiveDelete recursive)283 LONG RegKey::DeleteKey(const wchar_t* name, RecursiveDelete recursive) {
284   DCHECK(name);
285 
286   if (!Valid()) {
287     return ERROR_INVALID_HANDLE;
288   }
289 
290   // Verify the key exists before attempting delete to replicate previous
291   // behavior.
292   RegKey target_key;
293   LONG result = target_key.Open(key_, name, REG_OPTION_OPEN_LINK,
294                                 wow64access_ | KEY_QUERY_VALUE | DELETE);
295   if (result != ERROR_SUCCESS) {
296     return result;
297   }
298 
299   if (recursive.value()) {
300     target_key.Close();
301     return RegDelRecurse(key_, name, wow64access_);
302   }
303 
304   // Next, try to delete the key if it is a symbolic link.
305   if (auto deleted_link = target_key.DeleteIfLink(); deleted_link.has_value()) {
306     return deleted_link.value();
307   }
308 
309   // It's not a symbolic link, so try to delete it without recursing.
310   return ::RegDeleteKeyEx(target_key.key_, L"", wow64access_, 0);
311 }
312 
DeleteValue(const wchar_t * value_name)313 LONG RegKey::DeleteValue(const wchar_t* value_name) {
314   // `RegDeleteValue()` will return an error if `key_` is invalid.
315   LONG result = RegDeleteValue(key_, value_name);
316   return result;
317 }
318 
ReadValueDW(const wchar_t * name,DWORD * out_value) const319 LONG RegKey::ReadValueDW(const wchar_t* name, DWORD* out_value) const {
320   DCHECK(out_value);
321   DWORD type = REG_DWORD;
322   DWORD size = sizeof(DWORD);
323   DWORD local_value = 0;
324   LONG result = ReadValue(name, &local_value, &size, &type);
325   if (result == ERROR_SUCCESS) {
326     if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD)) {
327       *out_value = local_value;
328     } else {
329       result = ERROR_CANTREAD;
330     }
331   }
332 
333   return result;
334 }
335 
ReadInt64(const wchar_t * name,int64_t * out_value) const336 LONG RegKey::ReadInt64(const wchar_t* name, int64_t* out_value) const {
337   DCHECK(out_value);
338   DWORD type = REG_QWORD;
339   int64_t local_value = 0;
340   DWORD size = sizeof(local_value);
341   LONG result = ReadValue(name, &local_value, &size, &type);
342   if (result == ERROR_SUCCESS) {
343     if ((type == REG_QWORD || type == REG_BINARY) &&
344         size == sizeof(local_value)) {
345       *out_value = local_value;
346     } else {
347       result = ERROR_CANTREAD;
348     }
349   }
350 
351   return result;
352 }
353 
ReadValue(const wchar_t * name,std::wstring * out_value) const354 LONG RegKey::ReadValue(const wchar_t* name, std::wstring* out_value) const {
355   DCHECK(out_value);
356   const size_t kMaxStringLength = 1024;  // This is after expansion.
357   // Use the one of the other forms of ReadValue if 1024 is too small for you.
358   wchar_t raw_value[kMaxStringLength];
359   DWORD type = REG_SZ, size = sizeof(raw_value);
360   LONG result = ReadValue(name, raw_value, &size, &type);
361   if (result == ERROR_SUCCESS) {
362     if (type == REG_SZ) {
363       *out_value = raw_value;
364     } else if (type == REG_EXPAND_SZ) {
365       wchar_t expanded[kMaxStringLength];
366       size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength);
367       // Success: returns the number of wchar_t's copied
368       // Fail: buffer too small, returns the size required
369       // Fail: other, returns 0
370       if (size == 0 || size > kMaxStringLength) {
371         result = ERROR_MORE_DATA;
372       } else {
373         *out_value = expanded;
374       }
375     } else {
376       // Not a string. Oops.
377       result = ERROR_CANTREAD;
378     }
379   }
380 
381   return result;
382 }
383 
ReadValue(const wchar_t * name,void * data,DWORD * dsize,DWORD * dtype) const384 LONG RegKey::ReadValue(const wchar_t* name,
385                        void* data,
386                        DWORD* dsize,
387                        DWORD* dtype) const {
388   LONG result = RegQueryValueEx(key_, name, nullptr, dtype,
389                                 reinterpret_cast<LPBYTE>(data), dsize);
390   return result;
391 }
392 
ReadValues(const wchar_t * name,std::vector<std::wstring> * values)393 LONG RegKey::ReadValues(const wchar_t* name,
394                         std::vector<std::wstring>* values) {
395   values->clear();
396 
397   DWORD type = REG_MULTI_SZ;
398   DWORD size = 0;
399   LONG result = ReadValue(name, nullptr, &size, &type);
400   if (result != ERROR_SUCCESS || size == 0) {
401     return result;
402   }
403 
404   if (type != REG_MULTI_SZ) {
405     return ERROR_CANTREAD;
406   }
407 
408   std::vector<wchar_t> buffer(size / sizeof(wchar_t));
409   result = ReadValue(name, buffer.data(), &size, nullptr);
410   if (result != ERROR_SUCCESS || size == 0) {
411     return result;
412   }
413 
414   // Parse the double-null-terminated list of strings.
415   // Note: This code is paranoid to not read outside of |buf|, in the case where
416   // it may not be properly terminated.
417   auto entry = buffer.cbegin();
418   auto buffer_end = buffer.cend();
419   while (entry < buffer_end && *entry != '\0') {
420     auto entry_end = std::find(entry, buffer_end, '\0');
421     values->emplace_back(entry, entry_end);
422     entry = entry_end + 1;
423   }
424   return 0;
425 }
426 
WriteValue(const wchar_t * name,DWORD in_value)427 LONG RegKey::WriteValue(const wchar_t* name, DWORD in_value) {
428   return WriteValue(name, &in_value, static_cast<DWORD>(sizeof(in_value)),
429                     REG_DWORD);
430 }
431 
WriteValue(const wchar_t * name,const wchar_t * in_value)432 LONG RegKey::WriteValue(const wchar_t* name, const wchar_t* in_value) {
433   return WriteValue(
434       name, in_value,
435       static_cast<DWORD>(sizeof(*in_value) *
436                          (std::char_traits<wchar_t>::length(in_value) + 1)),
437       REG_SZ);
438 }
439 
WriteValue(const wchar_t * name,const void * data,DWORD dsize,DWORD dtype)440 LONG RegKey::WriteValue(const wchar_t* name,
441                         const void* data,
442                         DWORD dsize,
443                         DWORD dtype) {
444   DCHECK(data || !dsize);
445 
446   LONG result =
447       RegSetValueEx(key_, name, 0, dtype,
448                     reinterpret_cast<LPBYTE>(const_cast<void*>(data)), dsize);
449   return result;
450 }
451 
StartWatching(ChangeCallback callback)452 bool RegKey::StartWatching(ChangeCallback callback) {
453   if (!key_watcher_) {
454     key_watcher_ = std::make_unique<Watcher>();
455   }
456 
457   if (!key_watcher_->StartWatching(key_, std::move(callback))) {
458     return false;
459   }
460 
461   return true;
462 }
463 
Open(HKEY rootkey,const wchar_t * subkey,DWORD options,REGSAM access)464 LONG RegKey::Open(HKEY rootkey,
465                   const wchar_t* subkey,
466                   DWORD options,
467                   REGSAM access) {
468   DCHECK(options == 0 || options == REG_OPTION_OPEN_LINK) << options;
469   DCHECK(rootkey && subkey && access);
470   HKEY subhkey = nullptr;
471 
472   LONG result = RegOpenKeyEx(rootkey, subkey, options, access, &subhkey);
473   if (result == ERROR_SUCCESS) {
474     Close();
475     key_ = subhkey;
476     wow64access_ = access & kWow64AccessMask;
477   }
478 
479   return result;
480 }
481 
IsLink() const482 expected<bool, LONG> RegKey::IsLink() const {
483   DWORD value_type = 0;
484   LONG result = ::RegQueryValueEx(key_, L"SymbolicLinkValue",
485                                   /*lpReserved=*/nullptr, &value_type,
486                                   /*lpData=*/nullptr, /*lpcbData=*/nullptr);
487   if (result == ERROR_FILE_NOT_FOUND) {
488     return ok(false);
489   }
490   if (result == ERROR_SUCCESS) {
491     return ok(value_type == REG_LINK);
492   }
493   return unexpected(result);
494 }
495 
DeleteIfLink()496 std::optional<LONG> RegKey::DeleteIfLink() {
497   if (auto is_link = IsLink(); !is_link.has_value()) {
498     return is_link.error();  // Failed to determine if a link.
499   } else if (is_link.value() == false) {
500     return std::nullopt;  // Not a link.
501   }
502 
503   const NTSTATUS delete_result = ::NtDeleteKey(key_);
504   if (delete_result == STATUS_SUCCESS) {
505     return ERROR_SUCCESS;
506   }
507   using RtlNtStatusToDosErrorFunction = ULONG(WINAPI*)(NTSTATUS);
508   static const RtlNtStatusToDosErrorFunction rtl_nt_status_to_dos_error =
509       reinterpret_cast<RtlNtStatusToDosErrorFunction>(::GetProcAddress(
510           ::GetModuleHandle(L"ntdll.dll"), "RtlNtStatusToDosError"));
511   // The most common cause of failure is the presence of subkeys, which is
512   // reported as `STATUS_CANNOT_DELETE` and maps to `ERROR_ACCESS_DENIED`.
513   return rtl_nt_status_to_dos_error
514              ? static_cast<LONG>(rtl_nt_status_to_dos_error(delete_result))
515              : ERROR_ACCESS_DENIED;
516 }
517 
518 // static
RegDelRecurse(HKEY root_key,const wchar_t * name,REGSAM access)519 LONG RegKey::RegDelRecurse(HKEY root_key, const wchar_t* name, REGSAM access) {
520   // First, open the key; taking care not to traverse symbolic links.
521   RegKey target_key;
522   LONG result = target_key.Open(
523       root_key, name, REG_OPTION_OPEN_LINK,
524       access | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE | DELETE);
525   if (result == ERROR_FILE_NOT_FOUND) {  // The key doesn't exist.
526     return ERROR_SUCCESS;
527   }
528   if (result != ERROR_SUCCESS) {
529     return result;
530   }
531 
532   // Next, try to delete the key if it is a symbolic link.
533   if (auto deleted_link = target_key.DeleteIfLink(); deleted_link.has_value()) {
534     return deleted_link.value();
535   }
536 
537   // It's not a symbolic link, so try to delete it without recursing.
538   result = ::RegDeleteKeyEx(target_key.key_, L"", access, 0);
539   if (result == ERROR_SUCCESS) {
540     return result;
541   }
542 
543   // Enumerate the keys.
544   const DWORD kMaxKeyNameLength = 256;  // Includes string terminator.
545   auto subkey_buffer = std::make_unique<wchar_t[]>(kMaxKeyNameLength);
546   while (true) {
547     DWORD key_size = kMaxKeyNameLength;
548     if (::RegEnumKeyEx(target_key.key_, 0, &subkey_buffer[0], &key_size,
549                        nullptr, nullptr, nullptr, nullptr) != ERROR_SUCCESS) {
550       break;
551     }
552     CHECK_LT(key_size, kMaxKeyNameLength);
553     CHECK_EQ(subkey_buffer[key_size], L'\0');
554     if (RegDelRecurse(target_key.key_, &subkey_buffer[0], access) !=
555         ERROR_SUCCESS) {
556       break;
557     }
558   }
559 
560   // Try again to delete the key.
561   return ::RegDeleteKeyEx(target_key.key_, L"", access, 0);
562 }
563 
564 // RegistryValueIterator ------------------------------------------------------
565 
RegistryValueIterator(HKEY root_key,const wchar_t * folder_key,REGSAM wow64access)566 RegistryValueIterator::RegistryValueIterator(HKEY root_key,
567                                              const wchar_t* folder_key,
568                                              REGSAM wow64access)
569     : name_(MAX_PATH, '\0'), value_(MAX_PATH, '\0') {
570   Initialize(root_key, folder_key, wow64access);
571 }
572 
RegistryValueIterator(HKEY root_key,const wchar_t * folder_key)573 RegistryValueIterator::RegistryValueIterator(HKEY root_key,
574                                              const wchar_t* folder_key)
575     : name_(MAX_PATH, '\0'), value_(MAX_PATH, '\0') {
576   Initialize(root_key, folder_key, 0);
577 }
578 
Initialize(HKEY root_key,const wchar_t * folder_key,REGSAM wow64access)579 void RegistryValueIterator::Initialize(HKEY root_key,
580                                        const wchar_t* folder_key,
581                                        REGSAM wow64access) {
582   DCHECK_EQ(wow64access & ~kWow64AccessMask, static_cast<REGSAM>(0));
583   LONG result =
584       RegOpenKeyEx(root_key, folder_key, 0, KEY_READ | wow64access, &key_);
585   if (result != ERROR_SUCCESS) {
586     key_ = nullptr;
587   } else {
588     DWORD count = 0;
589     result =
590         ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr, nullptr,
591                           nullptr, &count, nullptr, nullptr, nullptr, nullptr);
592 
593     if (result != ERROR_SUCCESS) {
594       ::RegCloseKey(key_);
595       key_ = nullptr;
596     } else {
597       index_ = count - 1;
598     }
599   }
600 
601   Read();
602 }
603 
~RegistryValueIterator()604 RegistryValueIterator::~RegistryValueIterator() {
605   if (key_)
606     ::RegCloseKey(key_);
607 }
608 
ValueCount() const609 DWORD RegistryValueIterator::ValueCount() const {
610   DWORD count = 0;
611   LONG result =
612       ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr, nullptr,
613                         nullptr, &count, nullptr, nullptr, nullptr, nullptr);
614   if (result != ERROR_SUCCESS)
615     return 0;
616 
617   return count;
618 }
619 
Valid() const620 bool RegistryValueIterator::Valid() const {
621   return key_ != nullptr && index_ != kInvalidIterValue;
622 }
623 
operator ++()624 void RegistryValueIterator::operator++() {
625   if (index_ != kInvalidIterValue)
626     --index_;
627   Read();
628 }
629 
Read()630 bool RegistryValueIterator::Read() {
631   if (Valid()) {
632     DWORD capacity = static_cast<DWORD>(name_.capacity());
633     DWORD name_size = capacity;
634     // |value_size_| is in bytes. Reserve the last character for a NUL.
635     value_size_ = static_cast<DWORD>((value_.size() - 1) * sizeof(wchar_t));
636     LONG result = ::RegEnumValue(
637         key_, index_, WriteInto(&name_, name_size), &name_size, nullptr, &type_,
638         reinterpret_cast<BYTE*>(value_.data()), &value_size_);
639 
640     if (result == ERROR_MORE_DATA) {
641       // Registry key names are limited to 255 characters and fit within
642       // MAX_PATH (which is 260) but registry value names can use up to 16,383
643       // characters and the value itself is not limited
644       // (from http://msdn.microsoft.com/en-us/library/windows/desktop/
645       // ms724872(v=vs.85).aspx).
646       // Resize the buffers and retry if their size caused the failure.
647       DWORD value_size_in_wchars = to_wchar_size(value_size_);
648       if (value_size_in_wchars + 1 > value_.size())
649         value_.resize(value_size_in_wchars + 1, '\0');
650       value_size_ = static_cast<DWORD>((value_.size() - 1) * sizeof(wchar_t));
651       name_size = name_size == capacity ? MAX_REGISTRY_NAME_SIZE : capacity;
652       result = ::RegEnumValue(
653           key_, index_, WriteInto(&name_, name_size), &name_size, nullptr,
654           &type_, reinterpret_cast<BYTE*>(value_.data()), &value_size_);
655     }
656 
657     if (result == ERROR_SUCCESS) {
658       DCHECK_LT(to_wchar_size(value_size_), value_.size());
659       value_[to_wchar_size(value_size_)] = '\0';
660       return true;
661     }
662   }
663 
664   name_[0] = '\0';
665   value_[0] = '\0';
666   value_size_ = 0;
667   return false;
668 }
669 
670 // RegistryKeyIterator --------------------------------------------------------
671 
RegistryKeyIterator(HKEY root_key,const wchar_t * folder_key)672 RegistryKeyIterator::RegistryKeyIterator(HKEY root_key,
673                                          const wchar_t* folder_key) {
674   Initialize(root_key, folder_key, 0);
675 }
676 
RegistryKeyIterator(HKEY root_key,const wchar_t * folder_key,REGSAM wow64access)677 RegistryKeyIterator::RegistryKeyIterator(HKEY root_key,
678                                          const wchar_t* folder_key,
679                                          REGSAM wow64access) {
680   Initialize(root_key, folder_key, wow64access);
681 }
682 
~RegistryKeyIterator()683 RegistryKeyIterator::~RegistryKeyIterator() {
684   if (key_)
685     ::RegCloseKey(key_);
686 }
687 
SubkeyCount() const688 DWORD RegistryKeyIterator::SubkeyCount() const {
689   DWORD count = 0;
690   LONG result =
691       ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, &count, nullptr,
692                         nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
693   if (result != ERROR_SUCCESS)
694     return 0;
695 
696   return count;
697 }
698 
Valid() const699 bool RegistryKeyIterator::Valid() const {
700   return key_ != nullptr && index_ != kInvalidIterValue;
701 }
702 
operator ++()703 void RegistryKeyIterator::operator++() {
704   if (index_ != kInvalidIterValue)
705     --index_;
706   Read();
707 }
708 
Read()709 bool RegistryKeyIterator::Read() {
710   if (Valid()) {
711     DWORD ncount = static_cast<DWORD>(std::size(name_));
712     FILETIME written;
713     LONG r = ::RegEnumKeyEx(key_, index_, name_, &ncount, nullptr, nullptr,
714                             nullptr, &written);
715     if (ERROR_SUCCESS == r)
716       return true;
717   }
718 
719   name_[0] = '\0';
720   return false;
721 }
722 
Initialize(HKEY root_key,const wchar_t * folder_key,REGSAM wow64access)723 void RegistryKeyIterator::Initialize(HKEY root_key,
724                                      const wchar_t* folder_key,
725                                      REGSAM wow64access) {
726   DCHECK_EQ(wow64access & ~kWow64AccessMask, static_cast<REGSAM>(0));
727   LONG result =
728       RegOpenKeyEx(root_key, folder_key, 0, KEY_READ | wow64access, &key_);
729   if (result != ERROR_SUCCESS) {
730     key_ = nullptr;
731   } else {
732     DWORD count = 0;
733     result =
734         ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, &count, nullptr,
735                           nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
736 
737     if (result != ERROR_SUCCESS) {
738       ::RegCloseKey(key_);
739       key_ = nullptr;
740     } else {
741       index_ = count - 1;
742     }
743   }
744 
745   Read();
746 }
747 
748 }  // namespace base::win
749