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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 if (full_hashes) { | 152 if (full_hashes) { |
151 (*full_hashes)[full_hashes->size() - hash_count + i].metadata.assign( | 153 (*full_hashes)[full_hashes->size() - hash_count + i].metadata.assign( |
152 reinterpret_cast<const char*>(meta_data), meta_data_len); | 154 reinterpret_cast<const char*>(meta_data), meta_data_len); |
153 } | 155 } |
154 } | 156 } |
155 return true; | 157 return true; |
156 } | 158 } |
157 | 159 |
158 } // namespace | 160 } // namespace |
159 | 161 |
160 namespace safe_browsing { | |
161 | |
162 // BODY = CACHELIFETIME LF HASHENTRY* EOF | 162 // BODY = CACHELIFETIME LF HASHENTRY* EOF |
163 // CACHELIFETIME = DIGIT+ | 163 // CACHELIFETIME = DIGIT+ |
164 // HASHENTRY = LISTNAME ":" HASHSIZE ":" NUMRESPONSES [":m"] LF | 164 // HASHENTRY = LISTNAME ":" HASHSIZE ":" NUMRESPONSES [":m"] LF |
165 // HASHDATA (METADATALEN LF METADATA)* | 165 // HASHDATA (METADATALEN LF METADATA)* |
166 // HASHSIZE = DIGIT+ # Length of each full hash | 166 // HASHSIZE = DIGIT+ # Length of each full hash |
167 // NUMRESPONSES = DIGIT+ # Number of full hashes in HASHDATA | 167 // NUMRESPONSES = DIGIT+ # Number of full hashes in HASHDATA |
168 // HASHDATA = <HASHSIZE*NUMRESPONSES number of unsigned bytes> | 168 // HASHDATA = <HASHSIZE*NUMRESPONSES number of unsigned bytes> |
169 // METADATALEN = DIGIT+ | 169 // METADATALEN = DIGIT+ |
170 // METADATA = <METADATALEN number of unsigned bytes> | 170 // METADATA = <METADATALEN number of unsigned bytes> |
171 bool ParseGetHash(const char* chunk_data, | 171 bool ParseGetHash(const char* chunk_data, |
(...skipping 20 matching lines...) Expand all Loading... |
192 | 192 |
193 *cache_lifetime = base::TimeDelta::FromSeconds(cache_lifetime_seconds); | 193 *cache_lifetime = base::TimeDelta::FromSeconds(cache_lifetime_seconds); |
194 } | 194 } |
195 | 195 |
196 while (!reader.empty()) { | 196 while (!reader.empty()) { |
197 std::vector<base::StringPiece> cmd_parts; | 197 std::vector<base::StringPiece> cmd_parts; |
198 if (!reader.GetPieces(3, &cmd_parts)) | 198 if (!reader.GetPieces(3, &cmd_parts)) |
199 return false; | 199 return false; |
200 | 200 |
201 SBFullHashResult full_hash; | 201 SBFullHashResult full_hash; |
202 full_hash.list_id = safe_browsing::GetListId(cmd_parts[0]); | 202 full_hash.list_id = GetListId(cmd_parts[0]); |
203 | 203 |
204 size_t hash_len; | 204 size_t hash_len; |
205 if (!base::StringToSizeT(cmd_parts[1], &hash_len)) | 205 if (!base::StringToSizeT(cmd_parts[1], &hash_len)) |
206 return false; | 206 return false; |
207 | 207 |
208 // TODO(shess): Is this possible? If not, why the length present? | 208 // TODO(shess): Is this possible? If not, why the length present? |
209 if (hash_len != sizeof(SBFullHash)) | 209 if (hash_len != sizeof(SBFullHash)) |
210 return false; | 210 return false; |
211 | 211 |
212 // Metadata is indicated by an optional ":m" at the end of the line. | 212 // 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()) | 387 if (!list.adds.empty() && !list.subs.empty()) |
388 formatted_results.append(":"); | 388 formatted_results.append(":"); |
389 if (!list.subs.empty()) | 389 if (!list.subs.empty()) |
390 formatted_results.append("s:").append(list.subs); | 390 formatted_results.append("s:").append(list.subs); |
391 formatted_results.append("\n"); | 391 formatted_results.append("\n"); |
392 | 392 |
393 return formatted_results; | 393 return formatted_results; |
394 } | 394 } |
395 | 395 |
396 } // namespace safe_browsing | 396 } // namespace safe_browsing |
OLD | NEW |