Index: components/safe_browsing_db/util.cc |
diff --git a/chrome/browser/safe_browsing/safe_browsing_util.cc b/components/safe_browsing_db/util.cc |
similarity index 69% |
copy from chrome/browser/safe_browsing/safe_browsing_util.cc |
copy to components/safe_browsing_db/util.cc |
index 3a5ba42464e87b273c6ef8195a6e932f39d60a6b..8d95b9c849f023118976d63f79e53d5cf542edcf 100644 |
--- a/chrome/browser/safe_browsing/safe_browsing_util.cc |
+++ b/components/safe_browsing_db/util.cc |
@@ -1,155 +1,21 @@ |
-// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "chrome/browser/safe_browsing/safe_browsing_util.h" |
+#include "components/safe_browsing_db/util.h" |
-#include "base/logging.h" |
#include "base/strings/string_util.h" |
-#include "base/strings/stringprintf.h" |
-#include "chrome/browser/safe_browsing/chunk.pb.h" |
-#include "components/google/core/browser/google_util.h" |
#include "crypto/sha2.h" |
#include "net/base/escape.h" |
#include "url/gurl.h" |
#include "url/url_util.h" |
-// SBCachedFullHashResult ------------------------------------------------------ |
- |
-SBCachedFullHashResult::SBCachedFullHashResult() {} |
- |
-SBCachedFullHashResult::SBCachedFullHashResult( |
- const base::Time& in_expire_after) |
- : expire_after(in_expire_after) {} |
- |
-SBCachedFullHashResult::~SBCachedFullHashResult() {} |
- |
-// SBChunkData ----------------------------------------------------------------- |
- |
-// TODO(shess): Right now this contains a scoped_ptr<ChunkData> so that the |
-// proto buffer isn't copied all over the place, then these are contained in a |
-// ScopedVector for purposes of passing things around between tasks. This seems |
-// convoluted. Maybe it would make sense to have an overall container class |
-// returning references to a nested per-chunk class? |
- |
-SBChunkData::SBChunkData() { |
-} |
- |
-SBChunkData::SBChunkData(safe_browsing::ChunkData* raw_data) |
- : chunk_data_(raw_data) { |
- DCHECK(chunk_data_.get()); |
-} |
- |
-SBChunkData::~SBChunkData() { |
-} |
- |
-bool SBChunkData::ParseFrom(const unsigned char* data, size_t length) { |
- scoped_ptr<safe_browsing::ChunkData> chunk(new safe_browsing::ChunkData()); |
- if (!chunk->ParseFromArray(data, length)) |
- return false; |
- |
- if (chunk->chunk_type() != safe_browsing::ChunkData::ADD && |
- chunk->chunk_type() != safe_browsing::ChunkData::SUB) { |
- return false; |
- } |
- |
- size_t hash_size = 0; |
- if (chunk->prefix_type() == safe_browsing::ChunkData::PREFIX_4B) { |
- hash_size = sizeof(SBPrefix); |
- } else if (chunk->prefix_type() == safe_browsing::ChunkData::FULL_32B) { |
- hash_size = sizeof(SBFullHash); |
- } else { |
- return false; |
- } |
- |
- const size_t hash_count = chunk->hashes().size() / hash_size; |
- if (hash_count * hash_size != chunk->hashes().size()) |
- return false; |
- |
- if (chunk->chunk_type() == safe_browsing::ChunkData::SUB && |
- static_cast<size_t>(chunk->add_numbers_size()) != hash_count) { |
- return false; |
- } |
- |
- chunk_data_.swap(chunk); |
- return true; |
-} |
- |
-int SBChunkData::ChunkNumber() const { |
- return chunk_data_->chunk_number(); |
-} |
- |
-bool SBChunkData::IsAdd() const { |
- return chunk_data_->chunk_type() == safe_browsing::ChunkData::ADD; |
-} |
- |
-bool SBChunkData::IsSub() const { |
- return chunk_data_->chunk_type() == safe_browsing::ChunkData::SUB; |
-} |
- |
-int SBChunkData::AddChunkNumberAt(size_t i) const { |
- DCHECK(IsSub()); |
- DCHECK((IsPrefix() && i < PrefixCount()) || |
- (IsFullHash() && i < FullHashCount())); |
- return chunk_data_->add_numbers(i); |
-} |
- |
-bool SBChunkData::IsPrefix() const { |
- return chunk_data_->prefix_type() == safe_browsing::ChunkData::PREFIX_4B; |
-} |
- |
-size_t SBChunkData::PrefixCount() const { |
- DCHECK(IsPrefix()); |
- return chunk_data_->hashes().size() / sizeof(SBPrefix); |
-} |
- |
-SBPrefix SBChunkData::PrefixAt(size_t i) const { |
- DCHECK(IsPrefix()); |
- DCHECK_LT(i, PrefixCount()); |
- |
- SBPrefix prefix; |
- memcpy(&prefix, chunk_data_->hashes().data() + i * sizeof(SBPrefix), |
- sizeof(SBPrefix)); |
- return prefix; |
-} |
- |
-bool SBChunkData::IsFullHash() const { |
- return chunk_data_->prefix_type() == safe_browsing::ChunkData::FULL_32B; |
-} |
- |
-size_t SBChunkData::FullHashCount() const { |
- DCHECK(IsFullHash()); |
- return chunk_data_->hashes().size() / sizeof(SBFullHash); |
-} |
- |
-SBFullHash SBChunkData::FullHashAt(size_t i) const { |
- DCHECK(IsFullHash()); |
- DCHECK_LT(i, FullHashCount()); |
- |
- SBFullHash full_hash; |
- memcpy(&full_hash, chunk_data_->hashes().data() + i * sizeof(SBFullHash), |
- sizeof(SBFullHash)); |
- return full_hash; |
-} |
- |
-// SBListChunkRanges ----------------------------------------------------------- |
- |
-SBListChunkRanges::SBListChunkRanges(const std::string& n) |
- : name(n) { |
-} |
- |
-// SBChunkDelete --------------------------------------------------------------- |
- |
-SBChunkDelete::SBChunkDelete() : is_sub_del(false) {} |
- |
-SBChunkDelete::~SBChunkDelete() {} |
- |
// Utility functions ----------------------------------------------------------- |
namespace { |
bool IsKnownList(const std::string& name) { |
- for (size_t i = 0; i < arraysize(safe_browsing_util::kAllLists); ++i) { |
- if (!strcmp(safe_browsing_util::kAllLists[i], name.c_str())) { |
+ for (size_t i = 0; i < arraysize(safe_browsing::kAllLists); ++i) { |
+ if (!strcmp(safe_browsing::kAllLists[i], name.c_str())) { |
return true; |
} |
} |
@@ -157,7 +23,19 @@ bool IsKnownList(const std::string& name) { |
} |
} // namespace |
-namespace safe_browsing_util { |
+ |
+// SBCachedFullHashResult ------------------------------------------------------ |
+ |
+SBCachedFullHashResult::SBCachedFullHashResult() {} |
+ |
+SBCachedFullHashResult::SBCachedFullHashResult( |
+ const base::Time& in_expire_after) |
+ : expire_after(in_expire_after) {} |
+ |
+SBCachedFullHashResult::~SBCachedFullHashResult() {} |
+ |
+ |
+namespace safe_browsing { |
// Listnames that browser can process. |
const char kMalwareList[] = "goog-malware-shavar"; |
@@ -184,23 +62,23 @@ const char* kAllLists[9] = { |
ListType GetListId(const base::StringPiece& name) { |
ListType id; |
- if (name == safe_browsing_util::kMalwareList) { |
+ if (name == kMalwareList) { |
id = MALWARE; |
- } else if (name == safe_browsing_util::kPhishingList) { |
+ } else if (name == kPhishingList) { |
id = PHISH; |
- } else if (name == safe_browsing_util::kBinUrlList) { |
+ } else if (name == kBinUrlList) { |
id = BINURL; |
- } else if (name == safe_browsing_util::kCsdWhiteList) { |
+ } else if (name == kCsdWhiteList) { |
id = CSDWHITELIST; |
- } else if (name == safe_browsing_util::kDownloadWhiteList) { |
+ } else if (name == kDownloadWhiteList) { |
id = DOWNLOADWHITELIST; |
- } else if (name == safe_browsing_util::kExtensionBlacklist) { |
+ } else if (name == kExtensionBlacklist) { |
id = EXTENSIONBLACKLIST; |
- } else if (name == safe_browsing_util::kIPBlacklist) { |
+ } else if (name == kIPBlacklist) { |
id = IPBLACKLIST; |
- } else if (name == safe_browsing_util::kUnwantedUrlList) { |
+ } else if (name == kUnwantedUrlList) { |
id = UNWANTEDURL; |
- } else if (name == safe_browsing_util::kInclusionWhitelist) { |
+ } else if (name == kInclusionWhitelist) { |
id = INCLUSIONWHITELIST; |
} else { |
id = INVALID; |
@@ -211,31 +89,31 @@ ListType GetListId(const base::StringPiece& name) { |
bool GetListName(ListType list_id, std::string* list) { |
switch (list_id) { |
case MALWARE: |
- *list = safe_browsing_util::kMalwareList; |
+ *list = kMalwareList; |
break; |
case PHISH: |
- *list = safe_browsing_util::kPhishingList; |
+ *list = kPhishingList; |
break; |
case BINURL: |
- *list = safe_browsing_util::kBinUrlList; |
+ *list = kBinUrlList; |
break; |
case CSDWHITELIST: |
- *list = safe_browsing_util::kCsdWhiteList; |
+ *list = kCsdWhiteList; |
break; |
case DOWNLOADWHITELIST: |
- *list = safe_browsing_util::kDownloadWhiteList; |
+ *list = kDownloadWhiteList; |
break; |
case EXTENSIONBLACKLIST: |
- *list = safe_browsing_util::kExtensionBlacklist; |
+ *list = kExtensionBlacklist; |
break; |
case IPBLACKLIST: |
- *list = safe_browsing_util::kIPBlacklist; |
+ *list = kIPBlacklist; |
break; |
case UNWANTEDURL: |
- *list = safe_browsing_util::kUnwantedUrlList; |
+ *list = kUnwantedUrlList; |
break; |
case INCLUSIONWHITELIST: |
- *list = safe_browsing_util::kInclusionWhitelist; |
+ *list = kInclusionWhitelist; |
break; |
default: |
return false; |
@@ -244,6 +122,26 @@ bool GetListName(ListType list_id, std::string* list) { |
return true; |
} |
+ |
+SBFullHash SBFullHashForString(const base::StringPiece& str) { |
+ SBFullHash h; |
+ crypto::SHA256HashString(str, &h.full_hash, sizeof(h.full_hash)); |
+ return h; |
+} |
+ |
+SBFullHash StringToSBFullHash(const std::string& hash_in) { |
+ DCHECK_EQ(crypto::kSHA256Length, hash_in.size()); |
+ SBFullHash hash_out; |
+ memcpy(hash_out.full_hash, hash_in.data(), crypto::kSHA256Length); |
+ return hash_out; |
+} |
+ |
+std::string SBFullHashToString(const SBFullHash& hash) { |
+ DCHECK_EQ(crypto::kSHA256Length, sizeof(hash.full_hash)); |
+ return std::string(hash.full_hash, sizeof(hash.full_hash)); |
+} |
+ |
+ |
std::string Unescape(const std::string& url) { |
std::string unescaped_str(url); |
std::string old_unescaped_str; |
@@ -470,16 +368,4 @@ void GeneratePatternsToCheck(const GURL& url, std::vector<std::string>* urls) { |
} |
} |
-SBFullHash StringToSBFullHash(const std::string& hash_in) { |
- DCHECK_EQ(crypto::kSHA256Length, hash_in.size()); |
- SBFullHash hash_out; |
- memcpy(hash_out.full_hash, hash_in.data(), crypto::kSHA256Length); |
- return hash_out; |
-} |
- |
-std::string SBFullHashToString(const SBFullHash& hash) { |
- DCHECK_EQ(crypto::kSHA256Length, sizeof(hash.full_hash)); |
- return std::string(hash.full_hash, sizeof(hash.full_hash)); |
-} |
- |
-} // namespace safe_browsing_util |
+} // namespace safe_browsing |