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/browser/safe_browsing/safe_browsing_util.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 paths->push_back(std::string(path.begin(), i + 1)); | 410 paths->push_back(std::string(path.begin(), i + 1)); |
411 } | 411 } |
412 | 412 |
413 if (!paths->empty() && paths->back() != path) | 413 if (!paths->empty() && paths->back() != path) |
414 paths->push_back(path); | 414 paths->push_back(path); |
415 | 415 |
416 if (!query.empty()) | 416 if (!query.empty()) |
417 paths->push_back(path + "?" + query); | 417 paths->push_back(path + "?" + query); |
418 } | 418 } |
419 | 419 |
| 420 void GeneratePatternsToCheck(const GURL& url, std::vector<std::string>* urls) { |
| 421 std::vector<std::string> hosts, paths; |
| 422 GenerateHostsToCheck(url, &hosts); |
| 423 GeneratePathsToCheck(url, &paths); |
| 424 for (size_t h = 0; h < hosts.size(); ++h) { |
| 425 for (size_t p = 0; p < paths.size(); ++p) { |
| 426 urls->push_back(hosts[h] + paths[p]); |
| 427 } |
| 428 } |
| 429 } |
| 430 |
420 int GetHashIndex(const SBFullHash& hash, | 431 int GetHashIndex(const SBFullHash& hash, |
421 const std::vector<SBFullHashResult>& full_hashes) { | 432 const std::vector<SBFullHashResult>& full_hashes) { |
422 for (size_t i = 0; i < full_hashes.size(); ++i) { | 433 for (size_t i = 0; i < full_hashes.size(); ++i) { |
423 if (hash == full_hashes[i].hash) | 434 if (hash == full_hashes[i].hash) |
424 return static_cast<int>(i); | 435 return static_cast<int>(i); |
425 } | 436 } |
426 return -1; | 437 return -1; |
427 } | 438 } |
428 | 439 |
429 int GetUrlHashIndex(const GURL& url, | 440 int GetUrlHashIndex(const GURL& url, |
430 const std::vector<SBFullHashResult>& full_hashes) { | 441 const std::vector<SBFullHashResult>& full_hashes) { |
431 if (full_hashes.empty()) | 442 if (full_hashes.empty()) |
432 return -1; | 443 return -1; |
433 | 444 |
434 std::vector<std::string> hosts, paths; | 445 std::vector<std::string> patterns; |
435 GenerateHostsToCheck(url, &hosts); | 446 GeneratePatternsToCheck(url, &patterns); |
436 GeneratePathsToCheck(url, &paths); | |
437 | 447 |
438 for (size_t h = 0; h < hosts.size(); ++h) { | 448 for (size_t i = 0; i < patterns.size(); ++i) { |
439 for (size_t p = 0; p < paths.size(); ++p) { | 449 SBFullHash key; |
440 SBFullHash key; | 450 crypto::SHA256HashString(patterns[i], key.full_hash, sizeof(SBFullHash)); |
441 crypto::SHA256HashString(hosts[h] + paths[p], | 451 int index = GetHashIndex(key, full_hashes); |
442 key.full_hash, | 452 if (index != -1) |
443 sizeof(SBFullHash)); | 453 return index; |
444 int index = GetHashIndex(key, full_hashes); | |
445 if (index != -1) return index; | |
446 } | |
447 } | 454 } |
448 | |
449 return -1; | 455 return -1; |
450 } | 456 } |
451 | 457 |
452 bool IsPhishingList(const std::string& list_name) { | 458 bool IsPhishingList(const std::string& list_name) { |
453 return list_name.compare(kPhishingList) == 0; | 459 return list_name.compare(kPhishingList) == 0; |
454 } | 460 } |
455 | 461 |
456 bool IsMalwareList(const std::string& list_name) { | 462 bool IsMalwareList(const std::string& list_name) { |
457 return list_name.compare(kMalwareList) == 0; | 463 return list_name.compare(kMalwareList) == 0; |
458 } | 464 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 void StringToSBFullHash(const std::string& hash_in, SBFullHash* hash_out) { | 534 void StringToSBFullHash(const std::string& hash_in, SBFullHash* hash_out) { |
529 DCHECK_EQ(static_cast<size_t>(crypto::SHA256_LENGTH), hash_in.size()); | 535 DCHECK_EQ(static_cast<size_t>(crypto::SHA256_LENGTH), hash_in.size()); |
530 memcpy(hash_out->full_hash, hash_in.data(), crypto::SHA256_LENGTH); | 536 memcpy(hash_out->full_hash, hash_in.data(), crypto::SHA256_LENGTH); |
531 } | 537 } |
532 | 538 |
533 std::string SBFullHashToString(const SBFullHash& hash) { | 539 std::string SBFullHashToString(const SBFullHash& hash) { |
534 DCHECK_EQ(static_cast<size_t>(crypto::SHA256_LENGTH), sizeof(hash.full_hash)); | 540 DCHECK_EQ(static_cast<size_t>(crypto::SHA256_LENGTH), sizeof(hash.full_hash)); |
535 return std::string(hash.full_hash, sizeof(hash.full_hash)); | 541 return std::string(hash.full_hash, sizeof(hash.full_hash)); |
536 } | 542 } |
537 } // namespace safe_browsing_util | 543 } // namespace safe_browsing_util |
OLD | NEW |