| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "components/policy/core/common/preg_parser_win.h" | 5 #include "components/policy/core/common/preg_parser_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <functional> |
| 10 #include <iterator> | 11 #include <iterator> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 15 #include "base/files/memory_mapped_file.h" | 16 #include "base/files/memory_mapped_file.h" |
| 16 #include "base/i18n/case_conversion.h" | 17 #include "base/i18n/case_conversion.h" |
| 17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 18 #include "base/stl_util.h" | |
| 19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/sys_byteorder.h" | 23 #include "base/sys_byteorder.h" |
| 24 #include "base/values.h" | 24 #include "base/values.h" |
| 25 #include "components/policy/core/common/policy_load_status.h" | 25 #include "components/policy/core/common/policy_load_status.h" |
| 26 #include "components/policy/core/common/registry_dict_win.h" | 26 #include "components/policy/core/common/registry_dict_win.h" |
| 27 | 27 |
| 28 namespace policy { | 28 namespace policy { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 99 |
| 100 return current == L'\0'; | 100 return current == L'\0'; |
| 101 } | 101 } |
| 102 | 102 |
| 103 std::string DecodePRegStringValue(const std::vector<uint8>& data) { | 103 std::string DecodePRegStringValue(const std::vector<uint8>& data) { |
| 104 size_t len = data.size() / sizeof(base::char16); | 104 size_t len = data.size() / sizeof(base::char16); |
| 105 if (len <= 0) | 105 if (len <= 0) |
| 106 return std::string(); | 106 return std::string(); |
| 107 | 107 |
| 108 const base::char16* chars = | 108 const base::char16* chars = |
| 109 reinterpret_cast<const base::char16*>(vector_as_array(&data)); | 109 reinterpret_cast<const base::char16*>(data.data()); |
| 110 base::string16 result; | 110 base::string16 result; |
| 111 std::transform(chars, chars + len - 1, std::back_inserter(result), | 111 std::transform(chars, chars + len - 1, std::back_inserter(result), |
| 112 std::ptr_fun(base::ByteSwapToLE16)); | 112 std::ptr_fun(base::ByteSwapToLE16)); |
| 113 return base::UTF16ToUTF8(result); | 113 return base::UTF16ToUTF8(result); |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Decodes a value from a PReg file given as a uint8 vector. | 116 // Decodes a value from a PReg file given as a uint8 vector. |
| 117 bool DecodePRegValue(uint32 type, | 117 bool DecodePRegValue(uint32 type, |
| 118 const std::vector<uint8>& data, | 118 const std::vector<uint8>& data, |
| 119 scoped_ptr<base::Value>* value) { | 119 scoped_ptr<base::Value>* value) { |
| 120 switch (type) { | 120 switch (type) { |
| 121 case REG_SZ: | 121 case REG_SZ: |
| 122 case REG_EXPAND_SZ: | 122 case REG_EXPAND_SZ: |
| 123 value->reset(new base::StringValue(DecodePRegStringValue(data))); | 123 value->reset(new base::StringValue(DecodePRegStringValue(data))); |
| 124 return true; | 124 return true; |
| 125 case REG_DWORD_LITTLE_ENDIAN: | 125 case REG_DWORD_LITTLE_ENDIAN: |
| 126 case REG_DWORD_BIG_ENDIAN: | 126 case REG_DWORD_BIG_ENDIAN: |
| 127 if (data.size() == sizeof(uint32)) { | 127 if (data.size() == sizeof(uint32)) { |
| 128 uint32 val = *reinterpret_cast<const uint32*>(vector_as_array(&data)); | 128 uint32 val = *reinterpret_cast<const uint32*>(data.data()); |
| 129 if (type == REG_DWORD_BIG_ENDIAN) | 129 if (type == REG_DWORD_BIG_ENDIAN) |
| 130 val = base::NetToHost32(val); | 130 val = base::NetToHost32(val); |
| 131 else | 131 else |
| 132 val = base::ByteSwapToLE32(val); | 132 val = base::ByteSwapToLE32(val); |
| 133 value->reset(new base::FundamentalValue(static_cast<int>(val))); | 133 value->reset(new base::FundamentalValue(static_cast<int>(val))); |
| 134 return true; | 134 return true; |
| 135 } else { | 135 } else { |
| 136 LOG(ERROR) << "Bad data size " << data.size(); | 136 LOG(ERROR) << "Bad data size " << data.size(); |
| 137 } | 137 } |
| 138 break; | 138 break; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 if (current == kDelimSemicolon) { | 283 if (current == kDelimSemicolon) { |
| 284 if (!ReadField32(&cursor, end, &size)) | 284 if (!ReadField32(&cursor, end, &size)) |
| 285 break; | 285 break; |
| 286 current = NextChar(&cursor, end); | 286 current = NextChar(&cursor, end); |
| 287 } | 287 } |
| 288 | 288 |
| 289 if (current == kDelimSemicolon) { | 289 if (current == kDelimSemicolon) { |
| 290 if (size > kMaxPRegFileSize) | 290 if (size > kMaxPRegFileSize) |
| 291 break; | 291 break; |
| 292 data.resize(size); | 292 data.resize(size); |
| 293 if (!ReadFieldBinary(&cursor, end, size, vector_as_array(&data))) | 293 if (!ReadFieldBinary(&cursor, end, size, data.data())) |
| 294 break; | 294 break; |
| 295 current = NextChar(&cursor, end); | 295 current = NextChar(&cursor, end); |
| 296 } | 296 } |
| 297 | 297 |
| 298 if (current != kDelimBracketClose) | 298 if (current != kDelimBracketClose) |
| 299 break; | 299 break; |
| 300 | 300 |
| 301 // Process the record if it is within the |root| subtree. | 301 // Process the record if it is within the |root| subtree. |
| 302 if (base::StartsWith(base::i18n::ToLower(key_name), | 302 if (base::StartsWith(base::i18n::ToLower(key_name), |
| 303 base::i18n::ToLower(root), | 303 base::i18n::ToLower(root), |
| 304 base::CompareCase::SENSITIVE)) | 304 base::CompareCase::SENSITIVE)) |
| 305 HandleRecord(key_name.substr(root.size()), value, type, data, dict); | 305 HandleRecord(key_name.substr(root.size()), value, type, data, dict); |
| 306 } | 306 } |
| 307 | 307 |
| 308 LOG(ERROR) << "Error parsing " << file_path.value() << " at offset " | 308 LOG(ERROR) << "Error parsing " << file_path.value() << " at offset " |
| 309 << reinterpret_cast<const uint8*>(cursor - 1) - mapped_file.data(); | 309 << reinterpret_cast<const uint8*>(cursor - 1) - mapped_file.data(); |
| 310 status->Add(POLICY_LOAD_STATUS_PARSE_ERROR); | 310 status->Add(POLICY_LOAD_STATUS_PARSE_ERROR); |
| 311 return false; | 311 return false; |
| 312 } | 312 } |
| 313 | 313 |
| 314 } // namespace preg_parser | 314 } // namespace preg_parser |
| 315 } // namespace policy | 315 } // namespace policy |
| OLD | NEW |