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 { | 22 namespace safe_browsing { |
23 | 23 |
24 // Helper class for scanning a buffer. | 24 // Helper class for scanning a buffer. |
Nathan Parker
2015/11/05 22:00:52
If this is used only in this file, you could put i
vakh (old account. dont use)
2015/11/07 01:22:56
Done.
| |
25 class BufferReader { | 25 class BufferReader { |
26 public: | 26 public: |
27 BufferReader(const char* data, size_t length) | 27 BufferReader(const char* data, size_t length) |
28 : data_(data), | 28 : data_(data), |
29 length_(length) { | 29 length_(length) { |
30 } | 30 } |
31 | 31 |
32 // Return info about remaining buffer data. | 32 // Return info about remaining buffer data. |
33 size_t length() const { | 33 size_t length() const { |
34 return length_; | 34 return length_; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 return false; | 148 return false; |
149 | 149 |
150 if (full_hashes) { | 150 if (full_hashes) { |
151 (*full_hashes)[full_hashes->size() - hash_count + i].metadata.assign( | 151 (*full_hashes)[full_hashes->size() - hash_count + i].metadata.assign( |
152 reinterpret_cast<const char*>(meta_data), meta_data_len); | 152 reinterpret_cast<const char*>(meta_data), meta_data_len); |
153 } | 153 } |
154 } | 154 } |
155 return true; | 155 return true; |
156 } | 156 } |
157 | 157 |
158 } // namespace | |
159 | |
160 namespace safe_browsing { | |
161 | |
162 // BODY = CACHELIFETIME LF HASHENTRY* EOF | 158 // BODY = CACHELIFETIME LF HASHENTRY* EOF |
163 // CACHELIFETIME = DIGIT+ | 159 // CACHELIFETIME = DIGIT+ |
164 // HASHENTRY = LISTNAME ":" HASHSIZE ":" NUMRESPONSES [":m"] LF | 160 // HASHENTRY = LISTNAME ":" HASHSIZE ":" NUMRESPONSES [":m"] LF |
165 // HASHDATA (METADATALEN LF METADATA)* | 161 // HASHDATA (METADATALEN LF METADATA)* |
166 // HASHSIZE = DIGIT+ # Length of each full hash | 162 // HASHSIZE = DIGIT+ # Length of each full hash |
167 // NUMRESPONSES = DIGIT+ # Number of full hashes in HASHDATA | 163 // NUMRESPONSES = DIGIT+ # Number of full hashes in HASHDATA |
168 // HASHDATA = <HASHSIZE*NUMRESPONSES number of unsigned bytes> | 164 // HASHDATA = <HASHSIZE*NUMRESPONSES number of unsigned bytes> |
169 // METADATALEN = DIGIT+ | 165 // METADATALEN = DIGIT+ |
170 // METADATA = <METADATALEN number of unsigned bytes> | 166 // METADATA = <METADATALEN number of unsigned bytes> |
171 bool ParseGetHash(const char* chunk_data, | 167 bool ParseGetHash(const char* chunk_data, |
(...skipping 20 matching lines...) Expand all Loading... | |
192 | 188 |
193 *cache_lifetime = base::TimeDelta::FromSeconds(cache_lifetime_seconds); | 189 *cache_lifetime = base::TimeDelta::FromSeconds(cache_lifetime_seconds); |
194 } | 190 } |
195 | 191 |
196 while (!reader.empty()) { | 192 while (!reader.empty()) { |
197 std::vector<base::StringPiece> cmd_parts; | 193 std::vector<base::StringPiece> cmd_parts; |
198 if (!reader.GetPieces(3, &cmd_parts)) | 194 if (!reader.GetPieces(3, &cmd_parts)) |
199 return false; | 195 return false; |
200 | 196 |
201 SBFullHashResult full_hash; | 197 SBFullHashResult full_hash; |
202 full_hash.list_id = safe_browsing::GetListId(cmd_parts[0]); | 198 full_hash.list_id = GetListId(cmd_parts[0]); |
203 | 199 |
204 size_t hash_len; | 200 size_t hash_len; |
205 if (!base::StringToSizeT(cmd_parts[1], &hash_len)) | 201 if (!base::StringToSizeT(cmd_parts[1], &hash_len)) |
206 return false; | 202 return false; |
207 | 203 |
208 // TODO(shess): Is this possible? If not, why the length present? | 204 // TODO(shess): Is this possible? If not, why the length present? |
209 if (hash_len != sizeof(SBFullHash)) | 205 if (hash_len != sizeof(SBFullHash)) |
210 return false; | 206 return false; |
211 | 207 |
212 // Metadata is indicated by an optional ":m" at the end of the line. | 208 // 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()) | 383 if (!list.adds.empty() && !list.subs.empty()) |
388 formatted_results.append(":"); | 384 formatted_results.append(":"); |
389 if (!list.subs.empty()) | 385 if (!list.subs.empty()) |
390 formatted_results.append("s:").append(list.subs); | 386 formatted_results.append("s:").append(list.subs); |
391 formatted_results.append("\n"); | 387 formatted_results.append("\n"); |
392 | 388 |
393 return formatted_results; | 389 return formatted_results; |
394 } | 390 } |
395 | 391 |
396 } // namespace safe_browsing | 392 } // namespace safe_browsing |
OLD | NEW |