| 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/prefix_set.h" | 5 #include "chrome/browser/safe_browsing/prefix_set.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <math.h> | 8 #include <math.h> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 return a.first < b.first; | 35 return a.first < b.first; |
| 36 } | 36 } |
| 37 | 37 |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 namespace safe_browsing { | 40 namespace safe_browsing { |
| 41 | 41 |
| 42 PrefixSet::PrefixSet(const std::vector<SBPrefix>& sorted_prefixes) | 42 PrefixSet::PrefixSet(const std::vector<SBPrefix>& sorted_prefixes) |
| 43 : checksum_(0) { | 43 : checksum_(0) { |
| 44 if (sorted_prefixes.size()) { | 44 if (sorted_prefixes.size()) { |
| 45 // Estimate the resulting vector sizes. There will be strictly |
| 46 // more than |min_runs| entries in |index_|, but there generally |
| 47 // aren't many forced breaks. |
| 48 const size_t min_runs = sorted_prefixes.size() / kMaxRun; |
| 49 index_.reserve(min_runs); |
| 50 deltas_.reserve(sorted_prefixes.size() - min_runs); |
| 51 |
| 45 // Lead with the first prefix. | 52 // Lead with the first prefix. |
| 46 SBPrefix prev_prefix = sorted_prefixes[0]; | 53 SBPrefix prev_prefix = sorted_prefixes[0]; |
| 47 size_t run_length = 0; | 54 size_t run_length = 0; |
| 48 index_.push_back(std::make_pair(prev_prefix, deltas_.size())); | 55 index_.push_back(std::make_pair(prev_prefix, deltas_.size())); |
| 49 | 56 |
| 50 // Used to build a checksum from the data used to construct the | 57 // Used to build a checksum from the data used to construct the |
| 51 // structures. Since the data is a bunch of uniform hashes, it | 58 // structures. Since the data is a bunch of uniform hashes, it |
| 52 // seems reasonable to just xor most of it in, rather than trying | 59 // seems reasonable to just xor most of it in, rather than trying |
| 53 // to use a more complicated algorithm. | 60 // to use a more complicated algorithm. |
| 54 uint32 checksum = static_cast<uint32>(sorted_prefixes[0]); | 61 uint32 checksum = static_cast<uint32>(sorted_prefixes[0]); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 | 141 |
| 135 // Scan forward accumulating deltas while a match is possible. | 142 // Scan forward accumulating deltas while a match is possible. |
| 136 for (size_t di = iter->second; di < bound && current < prefix; ++di) { | 143 for (size_t di = iter->second; di < bound && current < prefix; ++di) { |
| 137 current += deltas_[di]; | 144 current += deltas_[di]; |
| 138 } | 145 } |
| 139 | 146 |
| 140 return current == prefix; | 147 return current == prefix; |
| 141 } | 148 } |
| 142 | 149 |
| 143 void PrefixSet::GetPrefixes(std::vector<SBPrefix>* prefixes) const { | 150 void PrefixSet::GetPrefixes(std::vector<SBPrefix>* prefixes) const { |
| 151 prefixes->reserve(index_.size() + deltas_.size()); |
| 152 |
| 144 for (size_t ii = 0; ii < index_.size(); ++ii) { | 153 for (size_t ii = 0; ii < index_.size(); ++ii) { |
| 145 // The deltas for this |index_| entry run to the next index entry, | 154 // The deltas for this |index_| entry run to the next index entry, |
| 146 // or the end of the deltas. | 155 // or the end of the deltas. |
| 147 const size_t deltas_end = | 156 const size_t deltas_end = |
| 148 (ii + 1 < index_.size()) ? index_[ii + 1].second : deltas_.size(); | 157 (ii + 1 < index_.size()) ? index_[ii + 1].second : deltas_.size(); |
| 149 | 158 |
| 150 SBPrefix current = index_[ii].first; | 159 SBPrefix current = index_[ii].first; |
| 151 prefixes->push_back(current); | 160 prefixes->push_back(current); |
| 152 for (size_t di = index_[ii].second; di < deltas_end; ++di) { | 161 for (size_t di = index_[ii].second; di < deltas_end; ++di) { |
| 153 current += deltas_[di]; | 162 current += deltas_[di]; |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 } | 360 } |
| 352 | 361 |
| 353 for (size_t di = 0; di < deltas_.size(); ++di) { | 362 for (size_t di = 0; di < deltas_.size(); ++di) { |
| 354 checksum ^= static_cast<uint32>(deltas_[di]); | 363 checksum ^= static_cast<uint32>(deltas_[di]); |
| 355 } | 364 } |
| 356 | 365 |
| 357 return checksum == checksum_; | 366 return checksum == checksum_; |
| 358 } | 367 } |
| 359 | 368 |
| 360 } // namespace safe_browsing | 369 } // namespace safe_browsing |
| OLD | NEW |