• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   }
184   HKEY subkey = nullptr;
185   LONG result = RegCreateKeyEx(key_, name, 0, nullptr, REG_OPTION_NON_VOLATILE,
186                                access, nullptr, &subkey, nullptr);
187   if (result == ERROR_SUCCESS) {
188     Close();
189     key_ = subkey;
190     wow64access_ = access & kWow64AccessMask;
191   }
192 
193   return result;
194 }
195 
Open(HKEY rootkey,const wchar_t * subkey,REGSAM access)196 LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
197   return Open(rootkey, subkey, /*options=*/0, access);
198 }
199 
OpenKey(const wchar_t * relative_key_name,REGSAM access)200 LONG RegKey::OpenKey(const wchar_t* relative_key_name, REGSAM access) {
201   DCHECK(relative_key_name && access);
202 
203   if (!Valid()) {
204     // The parent key has not been opened or created.
205     return ERROR_INVALID_HANDLE;
206   }
207 
208   // After the application has accessed an alternate registry view using one
209   // of the [KEY_WOW64_32KEY / KEY_WOW64_64KEY] flags, all subsequent
210   // operations (create, delete, or open) on child registry keys must
211   // explicitly use the same flag. Otherwise, there can be unexpected
212   // behavior.
213   // http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx.
214   if ((access & kWow64AccessMask) != wow64access_) {
215     NOTREACHED();
216   }
217   HKEY subkey = nullptr;
218   LONG result = RegOpenKeyEx(key_, relative_key_name, 0, access, &subkey);
219 
220   // We have to close the current opened key before replacing it with the new
221   // one.
222   if (result == ERROR_SUCCESS) {
223     Close();
224     key_ = subkey;
225     wow64access_ = access & kWow64AccessMask;
226   }
227   return result;
228 }
229 
Close()230 void RegKey::Close() {
231   if (key_) {
232     ::RegCloseKey(key_);
233     key_ = nullptr;
234     wow64access_ = 0;
235   }
236 }
237 
238 // TODO(wfh): Remove this and other unsafe methods. See http://crbug.com/375400
Set(HKEY key)239 void RegKey::Set(HKEY key) {
240   if (key_ != key) {
241     Close();
242     key_ = key;
243   }
244 }
245 
Take()246 HKEY RegKey::Take() {
247   DCHECK_EQ(wow64access_, 0u);
248   HKEY key = key_;
249   key_ = nullptr;
250   return key;
251 }
252 
HasValue(const wchar_t * name) const253 bool RegKey::HasValue(const wchar_t* name) const {
254   return RegQueryValueEx(key_, name, nullptr, nullptr, nullptr, nullptr) ==
255          ERROR_SUCCESS;
256 }
257 
GetValueCount() const258 base::expected<DWORD, LONG> RegKey::GetValueCount() const {
259   DWORD count = 0;
260   LONG result =
261       RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr, nullptr,
262                       nullptr, &count, nullptr, nullptr, nullptr, nullptr);
263   if (result == ERROR_SUCCESS) {
264     return base::ok(count);
265   }
266   return base::unexpected(result);
267 }
268 
GetValueNameAt(DWORD index,std::wstring * name) const269 LONG RegKey::GetValueNameAt(DWORD index, std::wstring* name) const {
270   wchar_t buf[256];
271   DWORD bufsize = std::size(buf);
272   LONG r = ::RegEnumValue(key_, index, buf, &bufsize, nullptr, nullptr, nullptr,
273                           nullptr);
274   if (r == ERROR_SUCCESS) {
275     name->assign(buf, bufsize);
276   }
277 
278   return r;
279 }
280 
DeleteKey(const wchar_t * name,RecursiveDelete recursive)281 LONG RegKey::DeleteKey(const wchar_t* name, RecursiveDelete recursive) {
282   DCHECK(name);
283 
284   if (!Valid()) {
285     return ERROR_INVALID_HANDLE;
286   }
287 
288   // Verify the key exists before attempting delete to replicate previous
289   // behavior.
290   RegKey target_key;
291   LONG result = target_key.Open(key_, name, REG_OPTION_OPEN_LINK,
292                                 wow64access_ | KEY_QUERY_VALUE | DELETE);
293   if (result != ERROR_SUCCESS) {
294     return result;
295   }
296 
297   if (recursive.value()) {
298     target_key.Close();
299     return RegDelRecurse(key_, name, wow64access_);
300   }
301 
302   // Next, try to delete the key if it is a symbolic link.
303   if (auto deleted_link = target_key.DeleteIfLink(); deleted_link.has_value()) {
304     return deleted_link.value();
305   }
306 
307   // It's not a symbolic link, so try to delete it without recursing.
308   return ::RegDeleteKeyEx(target_key.key_, L"", wow64access_, 0);
309 }
310 
DeleteValue(const wchar_t * value_name)311 LONG RegKey::DeleteValue(const wchar_t* value_name) {
312   // `RegDeleteValue()` will return an error if `key_` is invalid.
313   LONG result = RegDeleteValue(key_, value_name);
314   return result;
315 }
316 
ReadValueDW(const wchar_t * name,DWORD * out_value) const317 LONG RegKey::ReadValueDW(const wchar_t* name, DWORD* out_value) const {
318   DCHECK(out_value);
319   DWORD type = REG_DWORD;
320   DWORD size = sizeof(DWORD);
321   DWORD local_value = 0;
322   LONG result = ReadValue(name, &local_value, &size, &type);
323   if (result == ERROR_SUCCESS) {
324     if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD)) {
325       *out_value = local_value;
326     } else {
327       result = ERROR_CANTREAD;
328     }
329   }
330 
331   return result;
332 }
333 
ReadInt64(const wchar_t * name,int64_t * out_value) const334 LONG RegKey::ReadInt64(const wchar_t* name, int64_t* out_value) const {
335   DCHECK(out_value);
336   DWORD type = REG_QWORD;
337   int64_t local_value = 0;
338   DWORD size = sizeof(local_value);
339   LONG result = ReadValue(name, &local_value, &size, &type);
340   if (result == ERROR_SUCCESS) {
341     if ((type == REG_QWORD || type == REG_BINARY) &&
342         size == sizeof(local_value)) {
343       *out_value = local_value;
344     } else {
345       result = ERROR_CANTREAD;
346     }
347   }
348 
349   return result;
350 }
351 
ReadValue(const wchar_t * name,std::wstring * out_value) const352 LONG RegKey::ReadValue(const wchar_t* name, std::wstring* out_value) const {
353   DCHECK(out_value);
354   const size_t kMaxStringLength = 1024;  // This is after expansion.
355   // Use the one of the other forms of ReadValue if 1024 is too small for you.
356   wchar_t raw_value[kMaxStringLength];
357   DWORD type = REG_SZ, size = sizeof(raw_value);
358   LONG result = ReadValue(name, raw_value, &size, &type);
359   if (result == ERROR_SUCCESS) {
360     if (type == REG_SZ) {
361       *out_value = raw_value;
362     } else if (type == REG_EXPAND_SZ) {
363       wchar_t expanded[kMaxStringLength];
364       size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength);
365       // Success: returns the number of wchar_t's copied
366       // Fail: buffer too small, returns the size required
367       // Fail: other, returns 0
368       if (size == 0 || size > kMaxStringLength) {
369         result = ERROR_MORE_DATA;
370       } else {
371         *out_value = expanded;
372       }
373     } else {
374       // Not a string. Oops.
375       result = ERROR_CANTREAD;
376     }
377   }
378 
379   return result;
380 }
381 
ReadValue(const wchar_t * name,void * data,DWORD * dsize,DWORD * dtype) const382 LONG RegKey::ReadValue(const wchar_t* name,
383                        void* data,
384                        DWORD* dsize,
385                        DWORD* dtype) const {
386   LONG result = RegQueryValueEx(key_, name, nullptr, dtype,
387                                 reinterpret_cast<LPBYTE>(data), dsize);
388   return result;
389 }
390 
ReadValues(const wchar_t * name,std::vector<std::wstring> * values)391 LONG RegKey::ReadValues(const wchar_t* name,
392                         std::vector<std::wstring>* values) {
393   values->clear();
394 
395   DWORD type = REG_MULTI_SZ;
396   DWORD size = 0;
397   LONG result = ReadValue(name, nullptr, &size, &type);
398   if (result != ERROR_SUCCESS || size == 0) {
399     return result;
400   }
401 
402   if (type != REG_MULTI_SZ) {
403     return ERROR_CANTREAD;
404   }
405 
406   std::vector<wchar_t> buffer(size / sizeof(wchar_t));
407   result = ReadValue(name, buffer.data(), &size, nullptr);
408   if (result != ERROR_SUCCESS || size == 0) {
409     return result;
410   }
411 
412   // Parse the double-null-terminated list of strings.
413   // Note: This code is paranoid to not read outside of |buf|, in the case where
414   // it may not be properly terminated.
415   auto entry = buffer.cbegin();
416   auto buffer_end = buffer.cend();
417   while (entry < buffer_end && *entry != '\0') {
418     auto entry_end = std::find(entry, buffer_end, '\0');
419     values->emplace_back(entry, entry_end);
420     entry = entry_end + 1;
421   }
422   return 0;
423 }
424 
WriteValue(const wchar_t * name,DWORD in_value)425 LONG RegKey::WriteValue(const wchar_t* name, DWORD in_value) {
426   return WriteValue(name, &in_value, static_cast<DWORD>(sizeof(in_value)),
427                     REG_DWORD);
428 }
429 
WriteValue(const wchar_t * name,const wchar_t * in_value)430 LONG RegKey::WriteValue(const wchar_t* name, const wchar_t* in_value) {
431   return WriteValue(
432       name, in_value,
433       static_cast<DWORD>(sizeof(*in_value) *
434                          (std::char_traits<wchar_t>::length(in_value) + 1)),
435       REG_SZ);
436 }
437 
WriteValue(const wchar_t * name,const void * data,DWORD dsize,DWORD dtype)438 LONG RegKey::WriteValue(const wchar_t* name,
439                         const void* data,
440                         DWORD dsize,
441                         DWORD dtype) {
442   DCHECK(data || !dsize);
443 
444   LONG result =
445       RegSetValueEx(key_, name, 0, dtype,
446                     reinterpret_cast<LPBYTE>(const_cast<void*>(data)), dsize);
447   return result;
448 }
449 
StartWatching(ChangeCallback callback)450 bool RegKey::StartWatching(ChangeCallback callback) {
451   if (!key_watcher_) {
452     key_watcher_ = std::make_unique<Watcher>();
453   }
454 
455   if (!key_watcher_->StartWatching(key_, std::move(callback))) {
456     return false;
457   }
458 
459   return true;
460 }
461 
Open(HKEY rootkey,const wchar_t * subkey,DWORD options,REGSAM access)462 LONG RegKey::Open(HKEY rootkey,
463                   const wchar_t* subkey,
464                   DWORD options,
465                   REGSAM access) {
466   DCHECK(options == 0 || options == REG_OPTION_OPEN_LINK) << options;
467   DCHECK(rootkey && subkey && access);
468   HKEY subhkey = nullptr;
469 
470   LONG result = RegOpenKeyEx(rootkey, subkey, options, access, &subhkey);
471   if (result == ERROR_SUCCESS) {
472     Close();
473     key_ = subhkey;
474     wow64access_ = access & kWow64AccessMask;
475   }
476 
477   return result;
478 }
479 
IsLink() const480 expected<bool, LONG> RegKey::IsLink() const {
481   DWORD value_type = 0;
482   LONG result = ::RegQueryValueEx(key_, L"SymbolicLinkValue",
483                                   /*lpReserved=*/nullptr, &value_type,
484                                   /*lpData=*/nullptr, /*lpcbData=*/nullptr);
485   if (result == ERROR_FILE_NOT_FOUND) {
486     return ok(false);
487   }
488   if (result == ERROR_SUCCESS) {
489     return ok(value_type == REG_LINK);
490   }
491   return unexpected(result);
492 }
493 
DeleteIfLink()494 std::optional<LONG> RegKey::DeleteIfLink() {
495   if (auto is_link = IsLink(); !is_link.has_value()) {
496     return is_link.error();  // Failed to determine if a link.
497   } else if (is_link.value() == false) {
498     return std::nullopt;  // Not a link.
499   }
500 
501   const NTSTATUS delete_result = ::NtDeleteKey(key_);
502   if (delete_result == STATUS_SUCCESS) {
503     return ERROR_SUCCESS;
504   }
505   using RtlNtStatusToDosErrorFunction = ULONG(WINAPI*)(NTSTATUS);
506   static const RtlNtStatusToDosErrorFunction rtl_nt_status_to_dos_error =
507       reinterpret_cast<RtlNtStatusToDosErrorFunction>(::GetProcAddress(
508           ::GetModuleHandle(L"ntdll.dll"), "RtlNtStatusToDosError"));
509   // The most common cause of failure is the presence of subkeys, which is
510   // reported as `STATUS_CANNOT_DELETE` and maps to `ERROR_ACCESS_DENIED`.
511   return rtl_nt_status_to_dos_error
512              ? static_cast<LONG>(rtl_nt_status_to_dos_error(delete_result))
513              : ERROR_ACCESS_DENIED;
514 }
515 
516 // static
RegDelRecurse(HKEY root_key,const wchar_t * name,REGSAM access)517 LONG RegKey::RegDelRecurse(HKEY root_key, const wchar_t* name, REGSAM access) {
518   // First, open the key; taking care not to traverse symbolic links.
519   RegKey target_key;
520   LONG result = target_key.Open(
521       root_key, name, REG_OPTION_OPEN_LINK,
522       access | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE | DELETE);
523   if (result == ERROR_FILE_NOT_FOUND) {  // The key doesn't exist.
524     return ERROR_SUCCESS;
525   }
526   if (result != ERROR_SUCCESS) {
527     return result;
528   }
529 
530   // Next, try to delete the key if it is a symbolic link.
531   if (auto deleted_link = target_key.DeleteIfLink(); deleted_link.has_value()) {
532     return deleted_link.value();
533   }
534 
535   // It's not a symbolic link, so try to delete it without recursing.
536   result = ::RegDeleteKeyEx(target_key.key_, L"", access, 0);
537   if (result == ERROR_SUCCESS) {
538     return result;
539   }
540 
541   // Enumerate the keys.
542   const DWORD kMaxKeyNameLength = 256;  // Includes string terminator.
543   auto subkey_buffer = std::make_unique<wchar_t[]>(kMaxKeyNameLength);
544   while (true) {
545     DWORD key_size = kMaxKeyNameLength;
546     if (::RegEnumKeyEx(target_key.key_, 0, &subkey_buffer[0], &key_size,
547                        nullptr, nullptr, nullptr, nullptr) != ERROR_SUCCESS) {
548       break;
549     }
550     CHECK_LT(key_size, kMaxKeyNameLength);
551     CHECK_EQ(subkey_buffer[key_size], L'\0');
552     if (RegDelRecurse(target_key.key_, &subkey_buffer[0], access) !=
553         ERROR_SUCCESS) {
554       break;
555     }
556   }
557 
558   // Try again to delete the key.
559   return ::RegDeleteKeyEx(target_key.key_, L"", access, 0);
560 }
561 
562 // RegistryValueIterator ------------------------------------------------------
563 
RegistryValueIterator(HKEY root_key,const wchar_t * folder_key,REGSAM wow64access)564 RegistryValueIterator::RegistryValueIterator(HKEY root_key,
565                                              const wchar_t* folder_key,
566                                              REGSAM wow64access)
567     : name_(MAX_PATH, '\0'), value_(MAX_PATH, '\0') {
568   Initialize(root_key, folder_key, wow64access);
569 }
570 
RegistryValueIterator(HKEY root_key,const wchar_t * folder_key)571 RegistryValueIterator::RegistryValueIterator(HKEY root_key,
572                                              const wchar_t* folder_key)
573     : name_(MAX_PATH, '\0'), value_(MAX_PATH, '\0') {
574   Initialize(root_key, folder_key, 0);
575 }
576 
Initialize(HKEY root_key,const wchar_t * folder_key,REGSAM wow64access)577 void RegistryValueIterator::Initialize(HKEY root_key,
578                                        const wchar_t* folder_key,
579                                        REGSAM wow64access) {
580   DCHECK_EQ(wow64access & ~kWow64AccessMask, static_cast<REGSAM>(0));
581   LONG result =
582       RegOpenKeyEx(root_key, folder_key, 0, KEY_READ | wow64access, &key_);
583   if (result != ERROR_SUCCESS) {
584     key_ = nullptr;
585   } else {
586     DWORD count = 0;
587     result =
588         ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr, nullptr,
589                           nullptr, &count, nullptr, nullptr, nullptr, nullptr);
590 
591     if (result != ERROR_SUCCESS) {
592       ::RegCloseKey(key_);
593       key_ = nullptr;
594     } else {
595       index_ = count - 1;
596     }
597   }
598 
599   Read();
600 }
601 
~RegistryValueIterator()602 RegistryValueIterator::~RegistryValueIterator() {
603   if (key_)
604     ::RegCloseKey(key_);
605 }
606 
ValueCount() const607 DWORD RegistryValueIterator::ValueCount() const {
608   DWORD count = 0;
609   LONG result =
610       ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr, nullptr,
611                         nullptr, &count, nullptr, nullptr, nullptr, nullptr);
612   if (result != ERROR_SUCCESS)
613     return 0;
614 
615   return count;
616 }
617 
Valid() const618 bool RegistryValueIterator::Valid() const {
619   return key_ != nullptr && index_ != kInvalidIterValue;
620 }
621 
operator ++()622 void RegistryValueIterator::operator++() {
623   if (index_ != kInvalidIterValue)
624     --index_;
625   Read();
626 }
627 
Read()628 bool RegistryValueIterator::Read() {
629   if (Valid()) {
630     DWORD capacity = static_cast<DWORD>(name_.capacity());
631     DWORD name_size = capacity;
632     // |value_size_| is in bytes. Reserve the last character for a NUL.
633     value_size_ = static_cast<DWORD>((value_.size() - 1) * sizeof(wchar_t));
634     LONG result = ::RegEnumValue(
635         key_, index_, WriteInto(&name_, name_size), &name_size, nullptr, &type_,
636         reinterpret_cast<BYTE*>(value_.data()), &value_size_);
637 
638     if (result == ERROR_MORE_DATA) {
639       // Registry key names are limited to 255 characters and fit within
640       // MAX_PATH (which is 260) but registry value names can use up to 16,383
641       // characters and the value itself is not limited
642       // (from http://msdn.microsoft.com/en-us/library/windows/desktop/
643       // ms724872(v=vs.85).aspx).
644       // Resize the buffers and retry if their size caused the failure.
645       DWORD value_size_in_wchars = to_wchar_size(value_size_);
646       if (value_size_in_wchars + 1 > value_.size())
647         value_.resize(value_size_in_wchars + 1, '\0');
648       value_size_ = static_cast<DWORD>((value_.size() - 1) * sizeof(wchar_t));
649       name_size = name_size == capacity ? MAX_REGISTRY_NAME_SIZE : capacity;
650       result = ::RegEnumValue(
651           key_, index_, WriteInto(&name_, name_size), &name_size, nullptr,
652           &type_, reinterpret_cast<BYTE*>(value_.data()), &value_size_);
653     }
654 
655     if (result == ERROR_SUCCESS) {
656       DCHECK_LT(to_wchar_size(value_size_), value_.size());
657       value_[to_wchar_size(value_size_)] = '\0';
658       return true;
659     }
660   }
661 
662   name_[0] = '\0';
663   value_[0] = '\0';
664   value_size_ = 0;
665   return false;
666 }
667 
668 // RegistryKeyIterator --------------------------------------------------------
669 
RegistryKeyIterator(HKEY root_key,const wchar_t * folder_key)670 RegistryKeyIterator::RegistryKeyIterator(HKEY root_key,
671                                          const wchar_t* folder_key) {
672   Initialize(root_key, folder_key, 0);
673 }
674 
RegistryKeyIterator(HKEY root_key,const wchar_t * folder_key,REGSAM wow64access)675 RegistryKeyIterator::RegistryKeyIterator(HKEY root_key,
676                                          const wchar_t* folder_key,
677                                          REGSAM wow64access) {
678   Initialize(root_key, folder_key, wow64access);
679 }
680 
~RegistryKeyIterator()681 RegistryKeyIterator::~RegistryKeyIterator() {
682   if (key_)
683     ::RegCloseKey(key_);
684 }
685 
SubkeyCount() const686 DWORD RegistryKeyIterator::SubkeyCount() const {
687   DWORD count = 0;
688   LONG result =
689       ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, &count, nullptr,
690                         nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
691   if (result != ERROR_SUCCESS)
692     return 0;
693 
694   return count;
695 }
696 
Valid() const697 bool RegistryKeyIterator::Valid() const {
698   return key_ != nullptr && index_ != kInvalidIterValue;
699 }
700 
operator ++()701 void RegistryKeyIterator::operator++() {
702   if (index_ != kInvalidIterValue)
703     --index_;
704   Read();
705 }
706 
Read()707 bool RegistryKeyIterator::Read() {
708   if (Valid()) {
709     DWORD ncount = static_cast<DWORD>(std::size(name_));
710     FILETIME written;
711     LONG r = ::RegEnumKeyEx(key_, index_, name_, &ncount, nullptr, nullptr,
712                             nullptr, &written);
713     if (ERROR_SUCCESS == r)
714       return true;
715   }
716 
717   name_[0] = '\0';
718   return false;
719 }
720 
Initialize(HKEY root_key,const wchar_t * folder_key,REGSAM wow64access)721 void RegistryKeyIterator::Initialize(HKEY root_key,
722                                      const wchar_t* folder_key,
723                                      REGSAM wow64access) {
724   DCHECK_EQ(wow64access & ~kWow64AccessMask, static_cast<REGSAM>(0));
725   LONG result =
726       RegOpenKeyEx(root_key, folder_key, 0, KEY_READ | wow64access, &key_);
727   if (result != ERROR_SUCCESS) {
728     key_ = nullptr;
729   } else {
730     DWORD count = 0;
731     result =
732         ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, &count, nullptr,
733                           nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
734 
735     if (result != ERROR_SUCCESS) {
736       ::RegCloseKey(key_);
737       key_ = nullptr;
738     } else {
739       index_ = count - 1;
740     }
741   }
742 
743   Read();
744 }
745 
746 }  // namespace base::win
747