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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 written = fwrite(&digest, sizeof(digest), 1, file.get()); | 258 written = fwrite(&digest, sizeof(digest), 1, file.get()); |
259 if (written != 1) | 259 if (written != 1) |
260 return false; | 260 return false; |
261 | 261 |
262 // TODO(shess): Can this code check that the close was successful? | 262 // TODO(shess): Can this code check that the close was successful? |
263 file.reset(); | 263 file.reset(); |
264 | 264 |
265 return true; | 265 return true; |
266 } | 266 } |
267 | 267 |
| 268 size_t PrefixSet::IndexBinFor(size_t target_index) const { |
| 269 // The items in |index_| have the logical index of each previous |
| 270 // item in |index_| plus the count of deltas between the items. |
| 271 // Since the indices into |deltas_| are absolute, the logical index |
| 272 // is then the sum of the two indices. |
| 273 size_t lo = 0; |
| 274 size_t hi = index_.size(); |
| 275 |
| 276 // Binary search because linear search was too slow (really, the |
| 277 // unit test sucked). Inline because the elements can't be compared |
| 278 // in isolation (their position matters). |
| 279 while (hi - lo > 1) { |
| 280 const size_t i = (lo + hi) / 2; |
| 281 |
| 282 if (target_index < i + index_[i].second) { |
| 283 DCHECK_LT(i, hi); // Always making progress. |
| 284 hi = i; |
| 285 } else { |
| 286 DCHECK_GT(i, lo); // Always making progress. |
| 287 lo = i; |
| 288 } |
| 289 } |
| 290 return lo; |
| 291 } |
| 292 |
| 293 size_t PrefixSet::GetSize() const { |
| 294 return index_.size() + deltas_.size(); |
| 295 } |
| 296 |
| 297 bool PrefixSet::IsDeltaAt(size_t target_index) const { |
| 298 CHECK_LT(target_index, GetSize()); |
| 299 |
| 300 const size_t i = IndexBinFor(target_index); |
| 301 return target_index > i + index_[i].second; |
| 302 } |
| 303 |
| 304 uint16 PrefixSet::DeltaAt(size_t target_index) const { |
| 305 CHECK_LT(target_index, GetSize()); |
| 306 |
| 307 // Find the |index_| entry which contains |target_index|. |
| 308 const size_t i = IndexBinFor(target_index); |
| 309 |
| 310 // Exactly on the |index_| entry means no delta. |
| 311 CHECK_GT(target_index, i + index_[i].second); |
| 312 |
| 313 // -i backs out the |index_| entries, -1 gets the delta that lead to |
| 314 // the value at |target_index|. |
| 315 CHECK_LT(target_index - i - 1, deltas_.size()); |
| 316 return deltas_[target_index - i - 1]; |
| 317 } |
| 318 |
268 } // namespace safe_browsing | 319 } // namespace safe_browsing |
OLD | NEW |