| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/safe_browsing_util.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "chrome/browser/safe_browsing/chunk.pb.h" | 9 #include "chrome/browser/safe_browsing/chunk.pb.h" |
| 10 #include "components/google/core/browser/google_util.h" | 10 #include "components/google/core/browser/google_util.h" |
| 11 | 11 |
| 12 namespace safe_browsing { |
| 13 |
| 12 // SBChunkData ----------------------------------------------------------------- | 14 // SBChunkData ----------------------------------------------------------------- |
| 13 | 15 |
| 14 // TODO(shess): Right now this contains a scoped_ptr<ChunkData> so that the | 16 // TODO(shess): Right now this contains a scoped_ptr<ChunkData> so that the |
| 15 // proto buffer isn't copied all over the place, then these are contained in a | 17 // proto buffer isn't copied all over the place, then these are contained in a |
| 16 // ScopedVector for purposes of passing things around between tasks. This seems | 18 // ScopedVector for purposes of passing things around between tasks. This seems |
| 17 // convoluted. Maybe it would make sense to have an overall container class | 19 // convoluted. Maybe it would make sense to have an overall container class |
| 18 // returning references to a nested per-chunk class? | 20 // returning references to a nested per-chunk class? |
| 19 | 21 |
| 20 SBChunkData::SBChunkData() { | 22 SBChunkData::SBChunkData() { |
| 21 } | 23 } |
| 22 | 24 |
| 23 SBChunkData::SBChunkData(safe_browsing::ChunkData* raw_data) | 25 SBChunkData::SBChunkData(ChunkData* raw_data) |
| 24 : chunk_data_(raw_data) { | 26 : chunk_data_(raw_data) { |
| 25 DCHECK(chunk_data_.get()); | 27 DCHECK(chunk_data_.get()); |
| 26 } | 28 } |
| 27 | 29 |
| 28 SBChunkData::~SBChunkData() { | 30 SBChunkData::~SBChunkData() { |
| 29 } | 31 } |
| 30 | 32 |
| 31 bool SBChunkData::ParseFrom(const unsigned char* data, size_t length) { | 33 bool SBChunkData::ParseFrom(const unsigned char* data, size_t length) { |
| 32 scoped_ptr<safe_browsing::ChunkData> chunk(new safe_browsing::ChunkData()); | 34 scoped_ptr<ChunkData> chunk(new ChunkData()); |
| 33 if (!chunk->ParseFromArray(data, length)) | 35 if (!chunk->ParseFromArray(data, length)) |
| 34 return false; | 36 return false; |
| 35 | 37 |
| 36 if (chunk->chunk_type() != safe_browsing::ChunkData::ADD && | 38 if (chunk->chunk_type() != ChunkData::ADD && |
| 37 chunk->chunk_type() != safe_browsing::ChunkData::SUB) { | 39 chunk->chunk_type() != ChunkData::SUB) { |
| 38 return false; | 40 return false; |
| 39 } | 41 } |
| 40 | 42 |
| 41 size_t hash_size = 0; | 43 size_t hash_size = 0; |
| 42 if (chunk->prefix_type() == safe_browsing::ChunkData::PREFIX_4B) { | 44 if (chunk->prefix_type() == ChunkData::PREFIX_4B) { |
| 43 hash_size = sizeof(SBPrefix); | 45 hash_size = sizeof(SBPrefix); |
| 44 } else if (chunk->prefix_type() == safe_browsing::ChunkData::FULL_32B) { | 46 } else if (chunk->prefix_type() == ChunkData::FULL_32B) { |
| 45 hash_size = sizeof(SBFullHash); | 47 hash_size = sizeof(SBFullHash); |
| 46 } else { | 48 } else { |
| 47 return false; | 49 return false; |
| 48 } | 50 } |
| 49 | 51 |
| 50 const size_t hash_count = chunk->hashes().size() / hash_size; | 52 const size_t hash_count = chunk->hashes().size() / hash_size; |
| 51 if (hash_count * hash_size != chunk->hashes().size()) | 53 if (hash_count * hash_size != chunk->hashes().size()) |
| 52 return false; | 54 return false; |
| 53 | 55 |
| 54 if (chunk->chunk_type() == safe_browsing::ChunkData::SUB && | 56 if (chunk->chunk_type() == ChunkData::SUB && |
| 55 static_cast<size_t>(chunk->add_numbers_size()) != hash_count) { | 57 static_cast<size_t>(chunk->add_numbers_size()) != hash_count) { |
| 56 return false; | 58 return false; |
| 57 } | 59 } |
| 58 | 60 |
| 59 chunk_data_.swap(chunk); | 61 chunk_data_.swap(chunk); |
| 60 return true; | 62 return true; |
| 61 } | 63 } |
| 62 | 64 |
| 63 int SBChunkData::ChunkNumber() const { | 65 int SBChunkData::ChunkNumber() const { |
| 64 return chunk_data_->chunk_number(); | 66 return chunk_data_->chunk_number(); |
| 65 } | 67 } |
| 66 | 68 |
| 67 bool SBChunkData::IsAdd() const { | 69 bool SBChunkData::IsAdd() const { |
| 68 return chunk_data_->chunk_type() == safe_browsing::ChunkData::ADD; | 70 return chunk_data_->chunk_type() == ChunkData::ADD; |
| 69 } | 71 } |
| 70 | 72 |
| 71 bool SBChunkData::IsSub() const { | 73 bool SBChunkData::IsSub() const { |
| 72 return chunk_data_->chunk_type() == safe_browsing::ChunkData::SUB; | 74 return chunk_data_->chunk_type() == ChunkData::SUB; |
| 73 } | 75 } |
| 74 | 76 |
| 75 int SBChunkData::AddChunkNumberAt(size_t i) const { | 77 int SBChunkData::AddChunkNumberAt(size_t i) const { |
| 76 DCHECK(IsSub()); | 78 DCHECK(IsSub()); |
| 77 DCHECK((IsPrefix() && i < PrefixCount()) || | 79 DCHECK((IsPrefix() && i < PrefixCount()) || |
| 78 (IsFullHash() && i < FullHashCount())); | 80 (IsFullHash() && i < FullHashCount())); |
| 79 return chunk_data_->add_numbers(i); | 81 return chunk_data_->add_numbers(i); |
| 80 } | 82 } |
| 81 | 83 |
| 82 bool SBChunkData::IsPrefix() const { | 84 bool SBChunkData::IsPrefix() const { |
| 83 return chunk_data_->prefix_type() == safe_browsing::ChunkData::PREFIX_4B; | 85 return chunk_data_->prefix_type() == ChunkData::PREFIX_4B; |
| 84 } | 86 } |
| 85 | 87 |
| 86 size_t SBChunkData::PrefixCount() const { | 88 size_t SBChunkData::PrefixCount() const { |
| 87 DCHECK(IsPrefix()); | 89 DCHECK(IsPrefix()); |
| 88 return chunk_data_->hashes().size() / sizeof(SBPrefix); | 90 return chunk_data_->hashes().size() / sizeof(SBPrefix); |
| 89 } | 91 } |
| 90 | 92 |
| 91 SBPrefix SBChunkData::PrefixAt(size_t i) const { | 93 SBPrefix SBChunkData::PrefixAt(size_t i) const { |
| 92 DCHECK(IsPrefix()); | 94 DCHECK(IsPrefix()); |
| 93 DCHECK_LT(i, PrefixCount()); | 95 DCHECK_LT(i, PrefixCount()); |
| 94 | 96 |
| 95 SBPrefix prefix; | 97 SBPrefix prefix; |
| 96 memcpy(&prefix, chunk_data_->hashes().data() + i * sizeof(SBPrefix), | 98 memcpy(&prefix, chunk_data_->hashes().data() + i * sizeof(SBPrefix), |
| 97 sizeof(SBPrefix)); | 99 sizeof(SBPrefix)); |
| 98 return prefix; | 100 return prefix; |
| 99 } | 101 } |
| 100 | 102 |
| 101 bool SBChunkData::IsFullHash() const { | 103 bool SBChunkData::IsFullHash() const { |
| 102 return chunk_data_->prefix_type() == safe_browsing::ChunkData::FULL_32B; | 104 return chunk_data_->prefix_type() == ChunkData::FULL_32B; |
| 103 } | 105 } |
| 104 | 106 |
| 105 size_t SBChunkData::FullHashCount() const { | 107 size_t SBChunkData::FullHashCount() const { |
| 106 DCHECK(IsFullHash()); | 108 DCHECK(IsFullHash()); |
| 107 return chunk_data_->hashes().size() / sizeof(SBFullHash); | 109 return chunk_data_->hashes().size() / sizeof(SBFullHash); |
| 108 } | 110 } |
| 109 | 111 |
| 110 SBFullHash SBChunkData::FullHashAt(size_t i) const { | 112 SBFullHash SBChunkData::FullHashAt(size_t i) const { |
| 111 DCHECK(IsFullHash()); | 113 DCHECK(IsFullHash()); |
| 112 DCHECK_LT(i, FullHashCount()); | 114 DCHECK_LT(i, FullHashCount()); |
| 113 | 115 |
| 114 SBFullHash full_hash; | 116 SBFullHash full_hash; |
| 115 memcpy(&full_hash, chunk_data_->hashes().data() + i * sizeof(SBFullHash), | 117 memcpy(&full_hash, chunk_data_->hashes().data() + i * sizeof(SBFullHash), |
| 116 sizeof(SBFullHash)); | 118 sizeof(SBFullHash)); |
| 117 return full_hash; | 119 return full_hash; |
| 118 } | 120 } |
| 119 | 121 |
| 120 // SBListChunkRanges ----------------------------------------------------------- | 122 // SBListChunkRanges ----------------------------------------------------------- |
| 121 | 123 |
| 122 SBListChunkRanges::SBListChunkRanges(const std::string& n) | 124 SBListChunkRanges::SBListChunkRanges(const std::string& n) |
| 123 : name(n) { | 125 : name(n) { |
| 124 } | 126 } |
| 125 | 127 |
| 126 // SBChunkDelete --------------------------------------------------------------- | 128 // SBChunkDelete --------------------------------------------------------------- |
| 127 | 129 |
| 128 SBChunkDelete::SBChunkDelete() : is_sub_del(false) {} | 130 SBChunkDelete::SBChunkDelete() : is_sub_del(false) {} |
| 129 | 131 |
| 130 SBChunkDelete::~SBChunkDelete() {} | 132 SBChunkDelete::~SBChunkDelete() {} |
| 133 |
| 134 } // namespace safe_browsing |
| OLD | NEW |