OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/installer/util/registry_key_backup.h" | 5 #include "chrome/installer/util/registry_key_backup.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <map> |
| 9 #include <utility> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/win/registry.h" | 13 #include "base/win/registry.h" |
13 | 14 |
14 using base::win::RegKey; | 15 using base::win::RegKey; |
15 | 16 |
16 namespace { | 17 namespace { |
| 18 |
17 const REGSAM kKeyReadNoNotify = (KEY_READ) & ~(KEY_NOTIFY); | 19 const REGSAM kKeyReadNoNotify = (KEY_READ) & ~(KEY_NOTIFY); |
18 } // namespace | |
19 | |
20 // A container for a registry key, its values, and its subkeys. | |
21 class RegistryKeyBackup::KeyData { | |
22 public: | |
23 KeyData(); | |
24 ~KeyData(); | |
25 | |
26 // Initializes this object by reading the values and subkeys of |key|. | |
27 // Security descriptors are not backed up. Returns true if the operation was | |
28 // successful; false otherwise, in which case the state of this object is not | |
29 // modified. | |
30 bool Initialize(const RegKey& key); | |
31 | |
32 // Writes the contents of this object to |key|, which must have been opened | |
33 // with at least REG_SET_VALUE and KEY_CREATE_SUB_KEY access rights. Returns | |
34 // true if the operation was successful; false otherwise, in which case the | |
35 // contents of |key| may have been modified. | |
36 bool WriteTo(RegKey* key) const; | |
37 | |
38 private: | |
39 class ValueData; | |
40 | |
41 // The values of this key. | |
42 scoped_array<ValueData> values_; | |
43 // The names of this key's sub-keys (the data for subkey_names_[i] is in | |
44 // subkeys_[i]). | |
45 scoped_array<std::wstring> subkey_names_; | |
46 // The key data of this key's sub-keys. | |
47 scoped_array<KeyData> subkeys_; | |
48 // The number of values for this key. | |
49 DWORD num_values_; | |
50 // The number of subkeys for this key. | |
51 DWORD num_subkeys_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(KeyData); | |
54 }; | |
55 | 20 |
56 // A container for a registry value. | 21 // A container for a registry value. |
57 class RegistryKeyBackup::KeyData::ValueData { | 22 class ValueData { |
58 public: | 23 public: |
59 ValueData(); | 24 ValueData(); |
60 ~ValueData(); | 25 ~ValueData(); |
61 | 26 |
62 // Initializes this object with a name (the first |name_size| characters in | 27 // Initializes this object with a name (the first |name_size| characters in |
63 // |name_buffer|, |type|, and data (the first |data_size| bytes in |data|). | 28 // |name_buffer|, |type|, and data (the first |data_size| bytes in |data|). |
64 void Initialize(const wchar_t* name_buffer, DWORD name_size, | 29 void Initialize(const wchar_t* name_buffer, DWORD name_size, |
65 DWORD type, const uint8* data, DWORD data_size); | 30 DWORD type, const uint8* data, DWORD data_size); |
66 | 31 |
67 // The possibly empty name of this value. | 32 // The possibly empty name of this value. |
(...skipping 14 matching lines...) Expand all Loading... |
82 | 47 |
83 private: | 48 private: |
84 // This value's name, or the empty string if this is the default (unnamed) | 49 // This value's name, or the empty string if this is the default (unnamed) |
85 // value. | 50 // value. |
86 std::wstring name_; | 51 std::wstring name_; |
87 // This value's data. | 52 // This value's data. |
88 std::vector<uint8> data_; | 53 std::vector<uint8> data_; |
89 // This value's type (e.g., REG_DWORD, REG_SZ, REG_QWORD, etc). | 54 // This value's type (e.g., REG_DWORD, REG_SZ, REG_QWORD, etc). |
90 DWORD type_; | 55 DWORD type_; |
91 | 56 |
92 DISALLOW_COPY_AND_ASSIGN(ValueData); | 57 // Copy constructible and assignable for use in STL containers. |
93 }; | 58 }; |
94 | 59 |
95 RegistryKeyBackup::KeyData::ValueData::ValueData() | 60 } // namespace |
96 : type_(REG_NONE) { | 61 |
| 62 // A container for a registry key, its values, and its subkeys. |
| 63 class RegistryKeyBackup::KeyData { |
| 64 public: |
| 65 KeyData(); |
| 66 ~KeyData(); |
| 67 |
| 68 // Initializes this object by reading the values and subkeys of |key|. |
| 69 // Security descriptors are not backed up. Returns true if the operation was |
| 70 // successful; false otherwise, in which case the state of this object is not |
| 71 // modified. |
| 72 bool Initialize(const RegKey& key); |
| 73 |
| 74 // Writes the contents of this object to |key|, which must have been opened |
| 75 // with at least REG_SET_VALUE and KEY_CREATE_SUB_KEY access rights. Returns |
| 76 // true if the operation was successful; false otherwise, in which case the |
| 77 // contents of |key| may have been modified. |
| 78 bool WriteTo(RegKey* key) const; |
| 79 |
| 80 private: |
| 81 // The values of this key. |
| 82 std::vector<ValueData> values_; |
| 83 // Map of subkey names to the corresponding KeyData. |
| 84 std::map<std::wstring, KeyData> subkeys_; |
| 85 |
| 86 // Copy constructible and assignable for use in STL containers. |
| 87 }; |
| 88 |
| 89 ValueData::ValueData() : type_(REG_NONE) { |
97 } | 90 } |
98 | 91 |
99 RegistryKeyBackup::KeyData::ValueData::~ValueData() | 92 ValueData::~ValueData() { |
100 { | |
101 } | 93 } |
102 | 94 |
103 void RegistryKeyBackup::KeyData::ValueData::Initialize( | 95 void ValueData::Initialize( |
104 const wchar_t* name_buffer, | 96 const wchar_t* name_buffer, |
105 DWORD name_size, | 97 DWORD name_size, |
106 DWORD type, | 98 DWORD type, |
107 const uint8* data, | 99 const uint8* data, |
108 DWORD data_size) { | 100 DWORD data_size) { |
109 name_.assign(name_buffer, name_size); | 101 name_.assign(name_buffer, name_size); |
110 type_ = type; | 102 type_ = type; |
111 data_.assign(data, data + data_size); | 103 data_.assign(data, data + data_size); |
112 } | 104 } |
113 | 105 |
114 RegistryKeyBackup::KeyData::KeyData() | 106 RegistryKeyBackup::KeyData::KeyData() { |
115 : num_values_(0), | |
116 num_subkeys_(0) { | |
117 } | 107 } |
118 | 108 |
119 RegistryKeyBackup::KeyData::~KeyData() | 109 RegistryKeyBackup::KeyData::~KeyData() { |
120 { | |
121 } | 110 } |
122 | 111 |
123 bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { | 112 bool RegistryKeyBackup::KeyData::Initialize(const RegKey& key) { |
124 scoped_array<ValueData> values; | 113 std::vector<ValueData> values; |
125 scoped_array<std::wstring> subkey_names; | 114 std::map<std::wstring, KeyData> subkeys; |
126 scoped_array<KeyData> subkeys; | |
127 | 115 |
128 DWORD num_subkeys = 0; | 116 DWORD num_subkeys = 0; |
129 DWORD max_subkey_name_len = 0; | 117 DWORD max_subkey_name_len = 0; |
130 DWORD num_values = 0; | 118 DWORD num_values = 0; |
131 DWORD max_value_name_len = 0; | 119 DWORD max_value_name_len = 0; |
132 DWORD max_value_len = 0; | 120 DWORD max_value_len = 0; |
133 LONG result = RegQueryInfoKey(key.Handle(), NULL, NULL, NULL, | 121 LONG result = RegQueryInfoKey(key.Handle(), NULL, NULL, NULL, |
134 &num_subkeys, &max_subkey_name_len, NULL, | 122 &num_subkeys, &max_subkey_name_len, NULL, |
135 &num_values, &max_value_name_len, | 123 &num_values, &max_value_name_len, |
136 &max_value_len, NULL, NULL); | 124 &max_value_len, NULL, NULL); |
137 if (result != ERROR_SUCCESS) { | 125 if (result != ERROR_SUCCESS) { |
138 LOG(ERROR) << "Failed getting info of key to backup, result: " << result; | 126 LOG(ERROR) << "Failed getting info of key to backup, result: " << result; |
139 return false; | 127 return false; |
140 } | 128 } |
141 if (max_subkey_name_len >= std::numeric_limits<DWORD>::max() - 1 || | |
142 max_value_name_len >= std::numeric_limits<DWORD>::max() - 1) { | |
143 LOG(ERROR) | |
144 << "Failed backing up key; subkeys and/or names are out of range."; | |
145 return false; | |
146 } | |
147 DWORD max_name_len = std::max(max_subkey_name_len, max_value_name_len) + 1; | 129 DWORD max_name_len = std::max(max_subkey_name_len, max_value_name_len) + 1; |
148 scoped_array<wchar_t> name_buffer(new wchar_t[max_name_len]); | 130 std::vector<wchar_t> name_buffer(max_name_len); |
149 | 131 |
150 // Backup the values. | 132 // Backup the values. |
151 if (num_values != 0) { | 133 if (num_values != 0) { |
152 values.reset(new ValueData[num_values]); | 134 values.reserve(num_values); |
153 scoped_array<uint8> value_buffer(new uint8[max_value_len]); | 135 std::vector<uint8> value_buffer(max_value_len != 0 ? max_value_len : 1); |
154 DWORD name_size = 0; | 136 DWORD name_size = 0; |
155 DWORD value_type = REG_NONE; | 137 DWORD value_type = REG_NONE; |
156 DWORD value_size = 0; | 138 DWORD value_size = 0; |
157 | 139 |
158 for (DWORD i = 0; i < num_values; ) { | 140 for (DWORD i = 0; i < num_values; ) { |
159 name_size = max_name_len; | 141 name_size = static_cast<DWORD>(name_buffer.size()); |
160 value_size = max_value_len; | 142 value_size = static_cast<DWORD>(value_buffer.size()); |
161 result = RegEnumValue(key.Handle(), i, name_buffer.get(), &name_size, | 143 result = RegEnumValue(key.Handle(), i, &name_buffer[0], &name_size, |
162 NULL, &value_type, value_buffer.get(), &value_size); | 144 NULL, &value_type, &value_buffer[0], &value_size); |
163 switch (result) { | 145 switch (result) { |
164 case ERROR_NO_MORE_ITEMS: | 146 case ERROR_NO_MORE_ITEMS: |
165 num_values = i; | 147 num_values = i; |
166 break; | 148 break; |
167 case ERROR_SUCCESS: | 149 case ERROR_SUCCESS: |
168 values[i].Initialize(name_buffer.get(), name_size, value_type, | 150 values.push_back(ValueData()); |
169 value_buffer.get(), value_size); | 151 values.back().Initialize(&name_buffer[0], name_size, value_type, |
| 152 &value_buffer[0], value_size); |
170 ++i; | 153 ++i; |
171 break; | 154 break; |
172 case ERROR_MORE_DATA: | 155 case ERROR_MORE_DATA: |
173 if (value_size > max_value_len) { | 156 if (value_size > value_buffer.size()) |
174 max_value_len = value_size; | 157 value_buffer.resize(value_size); |
175 value_buffer.reset(); // Release to heap before new allocation. | 158 // |name_size| does not include space for the terminating NULL. |
176 value_buffer.reset(new uint8[max_value_len]); | 159 if (name_size + 1 > name_buffer.size()) |
177 } else { | 160 name_buffer.resize(name_size + 1); |
178 DCHECK_LT(max_name_len - 1, name_size); | |
179 if (name_size >= std::numeric_limits<DWORD>::max() - 1) { | |
180 LOG(ERROR) << "Failed backing up key; value name out of range."; | |
181 return false; | |
182 } | |
183 max_name_len = name_size + 1; | |
184 name_buffer.reset(); // Release to heap before new allocation. | |
185 name_buffer.reset(new wchar_t[max_name_len]); | |
186 } | |
187 break; | 161 break; |
188 default: | 162 default: |
189 LOG(ERROR) << "Failed backing up value " << i << ", result: " | 163 LOG(ERROR) << "Failed backing up value " << i << ", result: " |
190 << result; | 164 << result; |
191 return false; | 165 return false; |
192 } | 166 } |
193 } | 167 } |
194 DLOG_IF(WARNING, RegEnumValue(key.Handle(), num_values, name_buffer.get(), | 168 DLOG_IF(WARNING, RegEnumValue(key.Handle(), num_values, &name_buffer[0], |
195 &name_size, NULL, &value_type, NULL, | 169 &name_size, NULL, &value_type, NULL, |
196 NULL) != ERROR_NO_MORE_ITEMS) | 170 NULL) != ERROR_NO_MORE_ITEMS) |
197 << "Concurrent modifications to registry key during backup operation."; | 171 << "Concurrent modifications to registry key during backup operation."; |
198 } | 172 } |
199 | 173 |
200 // Backup the subkeys. | 174 // Backup the subkeys. |
201 if (num_subkeys != 0) { | 175 if (num_subkeys != 0) { |
202 subkey_names.reset(new std::wstring[num_subkeys]); | |
203 subkeys.reset(new KeyData[num_subkeys]); | |
204 DWORD name_size = 0; | 176 DWORD name_size = 0; |
205 | 177 |
206 // Get the names of them. | 178 // Get the names of them. |
207 for (DWORD i = 0; i < num_subkeys; ) { | 179 for (DWORD i = 0; i < num_subkeys; ) { |
208 name_size = max_name_len; | 180 name_size = static_cast<DWORD>(name_buffer.size()); |
209 result = RegEnumKeyEx(key.Handle(), i, name_buffer.get(), &name_size, | 181 result = RegEnumKeyEx(key.Handle(), i, &name_buffer[0], &name_size, |
210 NULL, NULL, NULL, NULL); | 182 NULL, NULL, NULL, NULL); |
211 switch (result) { | 183 switch (result) { |
212 case ERROR_NO_MORE_ITEMS: | 184 case ERROR_NO_MORE_ITEMS: |
213 num_subkeys = i; | 185 num_subkeys = i; |
214 break; | 186 break; |
215 case ERROR_SUCCESS: | 187 case ERROR_SUCCESS: |
216 subkey_names[i].assign(name_buffer.get(), name_size); | 188 subkeys.insert(std::make_pair(&name_buffer[0], KeyData())); |
217 ++i; | 189 ++i; |
218 break; | 190 break; |
219 case ERROR_MORE_DATA: | 191 case ERROR_MORE_DATA: |
220 if (name_size >= std::numeric_limits<DWORD>::max() - 1) { | 192 name_buffer.resize(name_size + 1); |
221 LOG(ERROR) << "Failed backing up key; subkey name out of range."; | |
222 return false; | |
223 } | |
224 max_name_len = name_size + 1; | |
225 name_buffer.reset(new wchar_t[max_name_len]); | |
226 break; | 193 break; |
227 default: | 194 default: |
228 LOG(ERROR) << "Failed getting name of subkey " << i | 195 LOG(ERROR) << "Failed getting name of subkey " << i |
229 << " for backup, result: " << result; | 196 << " for backup, result: " << result; |
230 return false; | 197 return false; |
231 } | 198 } |
232 } | 199 } |
233 DLOG_IF(WARNING, | 200 DLOG_IF(WARNING, |
234 RegEnumKeyEx(key.Handle(), num_subkeys, NULL, &name_size, NULL, | 201 RegEnumKeyEx(key.Handle(), num_subkeys, NULL, &name_size, NULL, |
235 NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS) | 202 NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS) |
236 << "Concurrent modifications to registry key during backup operation."; | 203 << "Concurrent modifications to registry key during backup operation."; |
237 | 204 |
238 // Get their values. | 205 // Get their values. |
239 RegKey subkey; | 206 RegKey subkey; |
240 for (DWORD i = 0; i < num_subkeys; ++i) { | 207 for (std::map<std::wstring, KeyData>::iterator it = subkeys.begin(); |
241 result = subkey.Open(key.Handle(), subkey_names[i].c_str(), | 208 it != subkeys.end(); ++it) { |
242 kKeyReadNoNotify); | 209 result = subkey.Open(key.Handle(), it->first.c_str(), kKeyReadNoNotify); |
243 if (result != ERROR_SUCCESS) { | 210 if (result != ERROR_SUCCESS) { |
244 LOG(ERROR) << "Failed opening subkey \"" << subkey_names[i] | 211 LOG(ERROR) << "Failed opening subkey \"" << it->first |
245 << "\" for backup, result: " << result; | 212 << "\" for backup, result: " << result; |
246 return false; | 213 return false; |
247 } | 214 } |
248 if (!subkeys[i].Initialize(subkey)) { | 215 if (!it->second.Initialize(subkey)) { |
249 LOG(ERROR) << "Failed backing up subkey \"" << subkey_names[i] << "\""; | 216 LOG(ERROR) << "Failed backing up subkey \"" << it->first << "\""; |
250 return false; | 217 return false; |
251 } | 218 } |
252 } | 219 } |
253 } | 220 } |
254 | 221 |
255 values_.swap(values); | 222 values_.swap(values); |
256 subkey_names_.swap(subkey_names); | |
257 subkeys_.swap(subkeys); | 223 subkeys_.swap(subkeys); |
258 num_values_ = num_values; | |
259 num_subkeys_ = num_subkeys; | |
260 | 224 |
261 return true; | 225 return true; |
262 } | 226 } |
263 | 227 |
264 bool RegistryKeyBackup::KeyData::WriteTo(RegKey* key) const { | 228 bool RegistryKeyBackup::KeyData::WriteTo(RegKey* key) const { |
265 DCHECK(key); | 229 DCHECK(key); |
266 | 230 |
267 LONG result = ERROR_SUCCESS; | 231 LONG result = ERROR_SUCCESS; |
268 | 232 |
269 // Write the values. | 233 // Write the values. |
270 for (DWORD i = 0; i < num_values_; ++i) { | 234 for (std::vector<ValueData>::const_iterator it = values_.begin(); |
271 const ValueData& value = values_[i]; | 235 it != values_.end(); ++it) { |
| 236 const ValueData& value = *it; |
272 result = RegSetValueEx(key->Handle(), value.name(), 0, value.type(), | 237 result = RegSetValueEx(key->Handle(), value.name(), 0, value.type(), |
273 value.data(), value.data_len()); | 238 value.data(), value.data_len()); |
274 if (result != ERROR_SUCCESS) { | 239 if (result != ERROR_SUCCESS) { |
275 LOG(ERROR) << "Failed writing value \"" << value.name_str() | 240 LOG(ERROR) << "Failed writing value \"" << value.name_str() |
276 << "\", result: " << result; | 241 << "\", result: " << result; |
277 return false; | 242 return false; |
278 } | 243 } |
279 } | 244 } |
280 | 245 |
281 // Write the subkeys. | 246 // Write the subkeys. |
282 RegKey subkey; | 247 RegKey subkey; |
283 for (DWORD i = 0; i < num_subkeys_; ++i) { | 248 for (std::map<std::wstring, KeyData>::const_iterator it = subkeys_.begin(); |
284 const std::wstring& name = subkey_names_[i]; | 249 it != subkeys_.end(); ++it) { |
| 250 const std::wstring& name = it->first; |
285 | 251 |
286 result = subkey.Create(key->Handle(), name.c_str(), KEY_WRITE); | 252 result = subkey.Create(key->Handle(), name.c_str(), KEY_WRITE); |
287 if (result != ERROR_SUCCESS) { | 253 if (result != ERROR_SUCCESS) { |
288 LOG(ERROR) << "Failed creating subkey \"" << name << "\", result: " | 254 LOG(ERROR) << "Failed creating subkey \"" << name << "\", result: " |
289 << result; | 255 << result; |
290 return false; | 256 return false; |
291 } | 257 } |
292 if (!subkeys_[i].WriteTo(&subkey)) { | 258 if (!it->second.WriteTo(&subkey)) { |
293 LOG(ERROR) << "Failed writing subkey \"" << name << "\", result: " | 259 LOG(ERROR) << "Failed writing subkey \"" << name << "\", result: " |
294 << result; | 260 << result; |
295 return false; | 261 return false; |
296 } | 262 } |
297 } | 263 } |
298 | 264 |
299 return true; | 265 return true; |
300 } | 266 } |
301 | 267 |
302 RegistryKeyBackup::RegistryKeyBackup() { | 268 RegistryKeyBackup::RegistryKeyBackup() { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 } else { | 309 } else { |
344 success = key_data_->WriteTo(&dest_key); | 310 success = key_data_->WriteTo(&dest_key); |
345 LOG_IF(ERROR, !success) << "Failed to write key data."; | 311 LOG_IF(ERROR, !success) << "Failed to write key data."; |
346 } | 312 } |
347 } else { | 313 } else { |
348 success = true; | 314 success = true; |
349 } | 315 } |
350 | 316 |
351 return success; | 317 return success; |
352 } | 318 } |
OLD | NEW |