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 // Parse the data returned from the SafeBrowsing v2.1 protocol response. | 5 // Parse the data returned from the SafeBrowsing v2.1 protocol response. |
6 | 6 |
7 // TODOv3(shess): Review these changes carefully. | 7 // TODOv3(shess): Review these changes carefully. |
8 | 8 |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 | 10 |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/sys_byteorder.h" | 16 #include "base/sys_byteorder.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 #include "chrome/browser/safe_browsing/protocol_parser.h" | 19 #include "chrome/browser/safe_browsing/protocol_parser.h" |
20 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
21 | 21 |
| 22 namespace safe_browsing { |
| 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 // Helper class for scanning a buffer. | 26 // Helper class for scanning a buffer. |
25 class BufferReader { | 27 class BufferReader { |
26 public: | 28 public: |
27 BufferReader(const char* data, size_t length) | 29 BufferReader(const char* data, size_t length) |
28 : data_(data), | 30 : data_(data), |
29 length_(length) { | 31 length_(length) { |
30 } | 32 } |
31 | 33 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 return true; | 126 return true; |
125 } | 127 } |
126 | 128 |
127 private: | 129 private: |
128 const char* data_; | 130 const char* data_; |
129 size_t length_; | 131 size_t length_; |
130 | 132 |
131 DISALLOW_COPY_AND_ASSIGN(BufferReader); | 133 DISALLOW_COPY_AND_ASSIGN(BufferReader); |
132 }; | 134 }; |
133 | 135 |
134 bool ParseGetHashMetadata(size_t hash_count, | 136 bool ParseGetHashMetadata( |
135 BufferReader* reader, | 137 size_t hash_count, |
136 std::vector<SBFullHashResult>* full_hashes) { | 138 BufferReader* reader, |
| 139 std::vector<SBFullHashResult>* full_hashes) { |
137 for (size_t i = 0; i < hash_count; ++i) { | 140 for (size_t i = 0; i < hash_count; ++i) { |
138 base::StringPiece line; | 141 base::StringPiece line; |
139 if (!reader->GetLine(&line)) | 142 if (!reader->GetLine(&line)) |
140 return false; | 143 return false; |
141 | 144 |
142 size_t meta_data_len; | 145 size_t meta_data_len; |
143 if (!base::StringToSizeT(line, &meta_data_len)) | 146 if (!base::StringToSizeT(line, &meta_data_len)) |
144 return false; | 147 return false; |
145 | 148 |
146 const void* meta_data; | 149 const void* meta_data; |
147 if (!reader->RefData(&meta_data, meta_data_len)) | 150 if (!reader->RefData(&meta_data, meta_data_len)) |
148 return false; | 151 return false; |
149 | 152 |
150 if (full_hashes) { | 153 if (full_hashes) { |
151 (*full_hashes)[full_hashes->size() - hash_count + i].metadata.assign( | 154 (*full_hashes)[full_hashes->size() - hash_count + i].metadata.assign( |
152 reinterpret_cast<const char*>(meta_data), meta_data_len); | 155 reinterpret_cast<const char*>(meta_data), meta_data_len); |
153 } | 156 } |
154 } | 157 } |
155 return true; | 158 return true; |
156 } | 159 } |
157 | 160 |
158 } // namespace | 161 } // namespace |
159 | 162 |
160 namespace safe_browsing { | |
161 | |
162 // BODY = CACHELIFETIME LF HASHENTRY* EOF | 163 // BODY = CACHELIFETIME LF HASHENTRY* EOF |
163 // CACHELIFETIME = DIGIT+ | 164 // CACHELIFETIME = DIGIT+ |
164 // HASHENTRY = LISTNAME ":" HASHSIZE ":" NUMRESPONSES [":m"] LF | 165 // HASHENTRY = LISTNAME ":" HASHSIZE ":" NUMRESPONSES [":m"] LF |
165 // HASHDATA (METADATALEN LF METADATA)* | 166 // HASHDATA (METADATALEN LF METADATA)* |
166 // HASHSIZE = DIGIT+ # Length of each full hash | 167 // HASHSIZE = DIGIT+ # Length of each full hash |
167 // NUMRESPONSES = DIGIT+ # Number of full hashes in HASHDATA | 168 // NUMRESPONSES = DIGIT+ # Number of full hashes in HASHDATA |
168 // HASHDATA = <HASHSIZE*NUMRESPONSES number of unsigned bytes> | 169 // HASHDATA = <HASHSIZE*NUMRESPONSES number of unsigned bytes> |
169 // METADATALEN = DIGIT+ | 170 // METADATALEN = DIGIT+ |
170 // METADATA = <METADATALEN number of unsigned bytes> | 171 // METADATA = <METADATALEN number of unsigned bytes> |
171 bool ParseGetHash(const char* chunk_data, | 172 bool ParseGetHash(const char* chunk_data, |
(...skipping 20 matching lines...) Expand all Loading... |
192 | 193 |
193 *cache_lifetime = base::TimeDelta::FromSeconds(cache_lifetime_seconds); | 194 *cache_lifetime = base::TimeDelta::FromSeconds(cache_lifetime_seconds); |
194 } | 195 } |
195 | 196 |
196 while (!reader.empty()) { | 197 while (!reader.empty()) { |
197 std::vector<base::StringPiece> cmd_parts; | 198 std::vector<base::StringPiece> cmd_parts; |
198 if (!reader.GetPieces(3, &cmd_parts)) | 199 if (!reader.GetPieces(3, &cmd_parts)) |
199 return false; | 200 return false; |
200 | 201 |
201 SBFullHashResult full_hash; | 202 SBFullHashResult full_hash; |
202 full_hash.list_id = safe_browsing::GetListId(cmd_parts[0]); | 203 full_hash.list_id = GetListId(cmd_parts[0]); |
203 | 204 |
204 size_t hash_len; | 205 size_t hash_len; |
205 if (!base::StringToSizeT(cmd_parts[1], &hash_len)) | 206 if (!base::StringToSizeT(cmd_parts[1], &hash_len)) |
206 return false; | 207 return false; |
207 | 208 |
208 // TODO(shess): Is this possible? If not, why the length present? | 209 // TODO(shess): Is this possible? If not, why the length present? |
209 if (hash_len != sizeof(SBFullHash)) | 210 if (hash_len != sizeof(SBFullHash)) |
210 return false; | 211 return false; |
211 | 212 |
212 // Metadata is indicated by an optional ":m" at the end of the line. | 213 // Metadata is indicated by an optional ":m" at the end of the line. |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 if (!list.adds.empty() && !list.subs.empty()) | 388 if (!list.adds.empty() && !list.subs.empty()) |
388 formatted_results.append(":"); | 389 formatted_results.append(":"); |
389 if (!list.subs.empty()) | 390 if (!list.subs.empty()) |
390 formatted_results.append("s:").append(list.subs); | 391 formatted_results.append("s:").append(list.subs); |
391 formatted_results.append("\n"); | 392 formatted_results.append("\n"); |
392 | 393 |
393 return formatted_results; | 394 return formatted_results; |
394 } | 395 } |
395 | 396 |
396 } // namespace safe_browsing | 397 } // namespace safe_browsing |
OLD | NEW |