[email protected] | ce82c057 | 2012-04-16 21:22:19 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | // |
5 | // Utilities for the SafeBrowsing code. | ||||
6 | |||||
[email protected] | 5da98afc | 2008-09-20 11:42:49 | [diff] [blame] | 7 | #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ |
8 | #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 10 | #include <stddef.h> |
11 | |||||
[email protected] | 1eb867e8 | 2009-12-01 23:50:10 | [diff] [blame] | 12 | #include <cstring> |
dcheng | 7bacc0e | 2016-04-11 20:10:54 | [diff] [blame] | 13 | #include <memory> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include <string> |
15 | #include <vector> | ||||
16 | |||||
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 17 | #include "base/macros.h" |
[email protected] | 2898118f | 2014-03-13 10:13:49 | [diff] [blame] | 18 | #include "base/strings/string_piece.h" |
[email protected] | 9c60c966 | 2014-06-28 05:36:44 | [diff] [blame] | 19 | #include "base/time/time.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include "chrome/browser/safe_browsing/chunk_range.h" |
Tim Volodine | e4593847 | 2017-09-21 10:08:22 | [diff] [blame] | 21 | #include "components/safe_browsing/db/util.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 22 | |
[email protected] | d2b383a | 2014-06-11 07:11:59 | [diff] [blame] | 23 | namespace safe_browsing { |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 24 | |
[email protected] | d2b383a | 2014-06-11 07:11:59 | [diff] [blame] | 25 | class ChunkData; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | |
[email protected] | ce82c057 | 2012-04-16 21:22:19 | [diff] [blame] | 27 | // Container for holding a chunk URL and the list it belongs to. |
[email protected] | 961354e | 2009-12-01 21:09:54 | [diff] [blame] | 28 | struct ChunkUrl { |
29 | std::string url; | ||||
[email protected] | 961354e | 2009-12-01 21:09:54 | [diff] [blame] | 30 | std::string list_name; |
31 | }; | ||||
32 | |||||
[email protected] | d2b383a | 2014-06-11 07:11:59 | [diff] [blame] | 33 | // Data for an individual chunk sent from the server. |
34 | class SBChunkData { | ||||
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 35 | public: |
[email protected] | d2b383a | 2014-06-11 07:11:59 | [diff] [blame] | 36 | SBChunkData(); |
37 | ~SBChunkData(); | ||||
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 38 | |
[email protected] | d2b383a | 2014-06-11 07:11:59 | [diff] [blame] | 39 | // Create with manufactured data, for testing only. |
40 | // TODO(shess): Right now the test code calling this is in an anonymous | ||||
41 | // namespace. Figure out how to shift this into private:. | ||||
dcheng | 7bacc0e | 2016-04-11 20:10:54 | [diff] [blame] | 42 | explicit SBChunkData(std::unique_ptr<ChunkData> data); |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 43 | |
[email protected] | d2b383a | 2014-06-11 07:11:59 | [diff] [blame] | 44 | // Read serialized ChunkData, returning true if the parse suceeded. |
45 | bool ParseFrom(const unsigned char* data, size_t length); | ||||
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 46 | |
[email protected] | d2b383a | 2014-06-11 07:11:59 | [diff] [blame] | 47 | // Access the chunk data. |AddChunkNumberAt()| can only be called if |
48 | // |IsSub()| returns true. |Prefix*()| and |FullHash*()| can only be called | ||||
49 | // if the corrosponding |Is*()| returned true. | ||||
50 | int ChunkNumber() const; | ||||
51 | bool IsAdd() const; | ||||
52 | bool IsSub() const; | ||||
53 | int AddChunkNumberAt(size_t i) const; | ||||
54 | bool IsPrefix() const; | ||||
55 | size_t PrefixCount() const; | ||||
56 | SBPrefix PrefixAt(size_t i) const; | ||||
57 | bool IsFullHash() const; | ||||
58 | size_t FullHashCount() const; | ||||
59 | SBFullHash FullHashAt(size_t i) const; | ||||
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 60 | |
61 | private: | ||||
[email protected] | d2b383a | 2014-06-11 07:11:59 | [diff] [blame] | 62 | // Protocol buffer sent from server. |
dcheng | 7bacc0e | 2016-04-11 20:10:54 | [diff] [blame] | 63 | std::unique_ptr<ChunkData> chunk_data_; |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 64 | |
[email protected] | d2b383a | 2014-06-11 07:11:59 | [diff] [blame] | 65 | DISALLOW_COPY_AND_ASSIGN(SBChunkData); |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 66 | }; |
67 | |||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | // Contains information about a list in the database. |
69 | struct SBListChunkRanges { | ||||
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 70 | explicit SBListChunkRanges(const std::string& n); |
71 | |||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | std::string name; // The list name. |
73 | std::string adds; // The ranges for add chunks. | ||||
74 | std::string subs; // The ranges for sub chunks. | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | }; |
76 | |||||
77 | // Container for deleting chunks from the database. | ||||
78 | struct SBChunkDelete { | ||||
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 79 | SBChunkDelete(); |
vmpstr | b8aacbe | 2016-02-26 02:00:48 | [diff] [blame] | 80 | SBChunkDelete(const SBChunkDelete& other); |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 81 | ~SBChunkDelete(); |
82 | |||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | std::string list_name; |
84 | bool is_sub_del; | ||||
85 | std::vector<ChunkRange> chunk_del; | ||||
86 | }; | ||||
87 | |||||
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 88 | } // namespace safe_browsing |
89 | |||||
[email protected] | 5da98afc | 2008-09-20 11:42:49 | [diff] [blame] | 90 | #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ |