| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/safe_browsing_db/prefix_set.h" | 5 #include "components/safe_browsing_db/prefix_set.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/md5.h" | 15 #include "base/md5.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
| 18 #include "base/trace_event/trace_event.h" |
| 18 | 19 |
| 19 namespace safe_browsing { | 20 namespace safe_browsing { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // |kMagic| should be reasonably unique, and not match itself across | 24 // |kMagic| should be reasonably unique, and not match itself across |
| 24 // endianness changes. I generated this value with: | 25 // endianness changes. I generated this value with: |
| 25 // md5 -qs chrome/browser/safe_browsing/prefix_set.cc | colrm 9 | 26 // md5 -qs chrome/browser/safe_browsing/prefix_set.cc | colrm 9 |
| 26 static uint32_t kMagic = 0x864088dd; | 27 static uint32_t kMagic = 0x864088dd; |
| 27 | 28 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 std::vector<SBFullHash>* full_hashes) { | 96 std::vector<SBFullHash>* full_hashes) { |
| 96 DCHECK(index && deltas && full_hashes); | 97 DCHECK(index && deltas && full_hashes); |
| 97 index_.swap(*index); | 98 index_.swap(*index); |
| 98 deltas_.swap(*deltas); | 99 deltas_.swap(*deltas); |
| 99 full_hashes_.swap(*full_hashes); | 100 full_hashes_.swap(*full_hashes); |
| 100 } | 101 } |
| 101 | 102 |
| 102 PrefixSet::~PrefixSet() {} | 103 PrefixSet::~PrefixSet() {} |
| 103 | 104 |
| 104 bool PrefixSet::PrefixExists(SBPrefix prefix) const { | 105 bool PrefixSet::PrefixExists(SBPrefix prefix) const { |
| 106 TRACE_EVENT0("toplevel", "PrefixSet::PrefixExists"); |
| 105 if (index_.empty()) | 107 if (index_.empty()) |
| 106 return false; | 108 return false; |
| 107 | 109 |
| 108 // Find the first position after |prefix| in |index_|. | 110 // Find the first position after |prefix| in |index_|. |
| 109 IndexVector::const_iterator iter = | 111 IndexVector::const_iterator iter = |
| 110 std::upper_bound(index_.begin(), index_.end(), | 112 std::upper_bound(index_.begin(), index_.end(), |
| 111 IndexPair(prefix, 0), PrefixLess); | 113 IndexPair(prefix, 0), PrefixLess); |
| 112 | 114 |
| 113 // |prefix| comes before anything that's in the set. | 115 // |prefix| comes before anything that's in the set. |
| 114 if (iter == index_.begin()) | 116 if (iter == index_.begin()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 127 | 129 |
| 128 // Scan forward accumulating deltas while a match is possible. | 130 // Scan forward accumulating deltas while a match is possible. |
| 129 for (size_t di = iter->second; di < bound && current < prefix; ++di) { | 131 for (size_t di = iter->second; di < bound && current < prefix; ++di) { |
| 130 current += deltas_[di]; | 132 current += deltas_[di]; |
| 131 } | 133 } |
| 132 | 134 |
| 133 return current == prefix; | 135 return current == prefix; |
| 134 } | 136 } |
| 135 | 137 |
| 136 bool PrefixSet::Exists(const SBFullHash& hash) const { | 138 bool PrefixSet::Exists(const SBFullHash& hash) const { |
| 139 TRACE_EVENT0("toplevel", "PrefixSet::Exists"); |
| 140 |
| 137 if (std::binary_search(full_hashes_.begin(), full_hashes_.end(), | 141 if (std::binary_search(full_hashes_.begin(), full_hashes_.end(), |
| 138 hash, SBFullHashLess)) { | 142 hash, SBFullHashLess)) { |
| 139 return true; | 143 return true; |
| 140 } | 144 } |
| 145 TRACE_EVENT0("toplevel", "PrefixSet::Exists::B"); |
| 141 return PrefixExists(hash.prefix); | 146 return PrefixExists(hash.prefix); |
| 142 } | 147 } |
| 143 | 148 |
| 144 void PrefixSet::GetPrefixes(std::vector<SBPrefix>* prefixes) const { | 149 void PrefixSet::GetPrefixes(std::vector<SBPrefix>* prefixes) const { |
| 145 prefixes->reserve(index_.size() + deltas_.size()); | 150 prefixes->reserve(index_.size() + deltas_.size()); |
| 146 | 151 |
| 147 for (size_t ii = 0; ii < index_.size(); ++ii) { | 152 for (size_t ii = 0; ii < index_.size(); ++ii) { |
| 148 // The deltas for this |index_| entry run to the next index entry, | 153 // The deltas for this |index_| entry run to the next index entry, |
| 149 // or the end of the deltas. | 154 // or the end of the deltas. |
| 150 const size_t deltas_end = | 155 const size_t deltas_end = |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 } | 449 } |
| 445 buffer_.push_back(prefix); | 450 buffer_.push_back(prefix); |
| 446 | 451 |
| 447 // Flush buffer when a run can be constructed. +1 for the index item, and +1 | 452 // Flush buffer when a run can be constructed. +1 for the index item, and +1 |
| 448 // to leave at least one item in the buffer for dropping duplicates. | 453 // to leave at least one item in the buffer for dropping duplicates. |
| 449 if (buffer_.size() > PrefixSet::kMaxRun + 2) | 454 if (buffer_.size() > PrefixSet::kMaxRun + 2) |
| 450 EmitRun(); | 455 EmitRun(); |
| 451 } | 456 } |
| 452 | 457 |
| 453 } // namespace safe_browsing | 458 } // namespace safe_browsing |
| OLD | NEW |