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

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: s/ContainsFullHash/GetMatchingHashPrefix 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 DCHECK_EQ(32u, full_hash.size());
430 for (const auto& pair : hash_prefix_map_) {
431 const PrefixSize& prefix_size = pair.first;
432 const HashPrefixes& hash_prefixes = pair.second;
433 const HashPrefix& hash_prefix = full_hash.substr(0, prefix_size);
Nathan Parker 2016/07/19 23:01:31 This is a reference to a temporary... I'm not sure
vakh (use Gerrit instead) 2016/07/20 00:24:51 Done.
434 if (HashPrefixMatches(hash_prefix, hash_prefixes.begin(),
435 hash_prefixes.end())) {
436 return hash_prefix;
437 }
438 }
439 return HashPrefix();
440 }
441
442 // static
443 bool V4Store::HashPrefixMatches(const HashPrefix& hash_prefix,
444 const HashPrefixes::const_iterator& begin,
445 const HashPrefixes::const_iterator& end) {
446 if (begin == end) {
447 return false;
448 }
449 size_t distance = std::distance(begin, end);
450 const PrefixSize& prefix_size = hash_prefix.length();
Nathan Parker 2016/07/19 23:01:31 no need for reference, since it's just as cheap (c
vakh (use Gerrit instead) 2016/07/20 00:24:52 Done.
451 DCHECK_EQ(0u, distance % prefix_size);
452 size_t mid_prefix_index = ((distance / prefix_size) / 2) * prefix_size;
453 HashPrefixes::const_iterator mid = begin + mid_prefix_index;
454 HashPrefix mid_prefix = HashPrefix(mid, mid + prefix_size);
Nathan Parker 2016/07/19 23:01:31 This is making a copy, which I suppose would copy
vakh (use Gerrit instead) 2016/07/20 00:24:52 It copies exactly prefix_size bytes.
455 if (mid_prefix == hash_prefix) {
456 return true;
457 }
458 if (hash_prefix < mid_prefix) {
Nathan Parker 2016/07/19 23:01:31 You can avoid iterating over both strings twice (i
vakh (use Gerrit instead) 2016/07/20 00:24:52 Done.
459 return HashPrefixMatches(hash_prefix, begin, mid);
460 } else {
461 return HashPrefixMatches(hash_prefix, mid + prefix_size, end);
462 }
463 }
464
428 } // namespace safe_browsing 465 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698