Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: components/safe_browsing_db/v4_store.cc

Issue 2164523002: PVer4: V4Store: Check whether a hash prefix exists for a full hash (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comment about what happens if there are more than one hash prefixes that match a given full hash Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "base/base64.h" 5 #include "base/base64.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/metrics/sparse_histogram.h" 9 #include "base/metrics/sparse_histogram.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 HashPrefix* smallest_hash_prefix) { 206 HashPrefix* smallest_hash_prefix) {
207 HashPrefix current_hash_prefix; 207 HashPrefix current_hash_prefix;
208 bool has_unmerged = false; 208 bool has_unmerged = false;
209 209
210 for (const auto& iterator_pair : iterator_map) { 210 for (const auto& iterator_pair : iterator_map) {
211 PrefixSize prefix_size = iterator_pair.first; 211 PrefixSize prefix_size = iterator_pair.first;
212 HashPrefixes::const_iterator start = iterator_pair.second; 212 HashPrefixes::const_iterator start = iterator_pair.second;
213 const HashPrefixes& hash_prefixes = hash_prefix_map.at(prefix_size); 213 const HashPrefixes& hash_prefixes = hash_prefix_map.at(prefix_size);
214 if (prefix_size <= 214 if (prefix_size <=
215 static_cast<PrefixSize>(std::distance(start, hash_prefixes.end()))) { 215 static_cast<PrefixSize>(std::distance(start, hash_prefixes.end()))) {
216 current_hash_prefix = std::string(start, start + prefix_size); 216 current_hash_prefix = HashPrefix(start, start + prefix_size);
217 if (!has_unmerged || *smallest_hash_prefix > current_hash_prefix) { 217 if (!has_unmerged || *smallest_hash_prefix > current_hash_prefix) {
218 has_unmerged = true; 218 has_unmerged = true;
219 smallest_hash_prefix->swap(current_hash_prefix); 219 smallest_hash_prefix->swap(current_hash_prefix);
220 } 220 }
221 } 221 }
222 } 222 }
223 return has_unmerged; 223 return has_unmerged;
224 } 224 }
225 225
226 // static 226 // static
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 DCHECK_EQ(file_format_string.size(), written); 418 DCHECK_EQ(file_format_string.size(), written);
419 419
420 if (!base::Move(new_filename, store_path_)) { 420 if (!base::Move(new_filename, store_path_)) {
421 DVLOG(1) << "store_path_: " << store_path_.value(); 421 DVLOG(1) << "store_path_: " << store_path_.value();
422 return UNABLE_TO_RENAME_FAILURE; 422 return UNABLE_TO_RENAME_FAILURE;
423 } 423 }
424 424
425 return WRITE_SUCCESS; 425 return WRITE_SUCCESS;
426 } 426 }
427 427
428 HashPrefix V4Store::GetMatchingHashPrefix(const FullHash& full_hash) {
429 // It should never be the case that more than one hash prefixes match a given
430 // full hash. However, if that happens, this method returns any one of them.
431 // It does not guarantee which one of those will be returned.
432 DCHECK_EQ(32u, full_hash.size());
433 for (const auto& pair : hash_prefix_map_) {
434 const PrefixSize& prefix_size = pair.first;
435 const HashPrefixes& hash_prefixes = pair.second;
436 HashPrefix hash_prefix = full_hash.substr(0, prefix_size);
437 if (HashPrefixMatches(hash_prefix, hash_prefixes.begin(),
438 hash_prefixes.end())) {
439 return hash_prefix;
440 }
441 }
442 return HashPrefix();
443 }
444
445 // static
446 bool V4Store::HashPrefixMatches(const HashPrefix& hash_prefix,
447 const HashPrefixes::const_iterator& begin,
448 const HashPrefixes::const_iterator& end) {
449 if (begin == end) {
450 return false;
451 }
452 size_t distance = std::distance(begin, end);
453 const PrefixSize prefix_size = hash_prefix.length();
454 DCHECK_EQ(0u, distance % prefix_size);
455 size_t mid_prefix_index = ((distance / prefix_size) / 2) * prefix_size;
456 HashPrefixes::const_iterator mid = begin + mid_prefix_index;
457 HashPrefix mid_prefix = HashPrefix(mid, mid + prefix_size);
458 int result = hash_prefix.compare(mid_prefix);
459 if (result == 0) {
460 return true;
461 } else if (result < 0) {
462 return HashPrefixMatches(hash_prefix, begin, mid);
463 } else {
464 return HashPrefixMatches(hash_prefix, mid + prefix_size, end);
465 }
466 }
467
428 } // namespace safe_browsing 468 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_store.h ('k') | components/safe_browsing_db/v4_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698