Chromium Code Reviews| 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 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 return true; | 124 return true; |
| 125 } | 125 } |
| 126 | 126 |
| 127 private: | 127 private: |
| 128 const char* data_; | 128 const char* data_; |
| 129 size_t length_; | 129 size_t length_; |
| 130 | 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(BufferReader); | 131 DISALLOW_COPY_AND_ASSIGN(BufferReader); |
| 132 }; | 132 }; |
| 133 | 133 |
| 134 bool ParseGetHashMetadata(size_t hash_count, BufferReader* reader) { | |
| 135 for (size_t i = 0; i < hash_count; ++i) { | |
| 136 base::StringPiece line; | |
| 137 if (!reader->GetLine(&line)) | |
| 138 return false; | |
| 139 | |
| 140 size_t meta_data_len; | |
| 141 if (!base::StringToSizeT(line, &meta_data_len)) | |
| 142 return false; | |
| 143 | |
| 144 const void* meta_data; | |
| 145 if (!reader->RefData(&meta_data, meta_data_len)) | |
| 146 return false; | |
| 147 } | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 134 } // namespace | 151 } // namespace |
| 135 | 152 |
| 136 namespace safe_browsing { | 153 namespace safe_browsing { |
| 137 | 154 |
| 138 // BODY = CACHELIFETIME LF HASHENTRY* EOF | 155 // BODY = CACHELIFETIME LF HASHENTRY* EOF |
| 139 // CACHELIFETIME = DIGIT+ | 156 // CACHELIFETIME = DIGIT+ |
| 140 // HASHENTRY = LISTNAME ":" HASHSIZE ":" NUMRESPONSES [":m"] LF | 157 // HASHENTRY = LISTNAME ":" HASHSIZE ":" NUMRESPONSES [":m"] LF |
| 141 // HASHDATA (METADATALEN LF METADATA)* | 158 // HASHDATA (METADATALEN LF METADATA)* |
| 142 // HASHSIZE = DIGIT+ # Length of each full hash | 159 // HASHSIZE = DIGIT+ # Length of each full hash |
| 143 // NUMRESPONSES = DIGIT+ # Number of full hashes in HASHDATA | 160 // NUMRESPONSES = DIGIT+ # Number of full hashes in HASHDATA |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 size_t hash_count; | 216 size_t hash_count; |
| 200 if (!base::StringToSizeT(hash_count_string, &hash_count)) | 217 if (!base::StringToSizeT(hash_count_string, &hash_count)) |
| 201 return false; | 218 return false; |
| 202 | 219 |
| 203 if (hash_len * hash_count > reader.length()) | 220 if (hash_len * hash_count > reader.length()) |
| 204 return false; | 221 return false; |
| 205 | 222 |
| 206 // Ignore hash results from lists we don't recognize. | 223 // Ignore hash results from lists we don't recognize. |
| 207 if (full_hash.list_id < 0) { | 224 if (full_hash.list_id < 0) { |
| 208 reader.Advance(hash_len * hash_count); | 225 reader.Advance(hash_len * hash_count); |
| 226 if (has_metadata && !ParseGetHashMetadata(hash_count, &reader)) | |
|
noé
2014/09/18 20:41:07
nit: can you add a TODO here to send the metadata
mattm
2014/09/18 20:50:32
Done.
I need to pull that out of my old api 2.3
| |
| 227 return false; | |
| 209 continue; | 228 continue; |
| 210 } | 229 } |
| 211 | 230 |
| 212 for (size_t i = 0; i < hash_count; ++i) { | 231 for (size_t i = 0; i < hash_count; ++i) { |
| 213 if (!reader.GetData(&full_hash.hash, hash_len)) | 232 if (!reader.GetData(&full_hash.hash, hash_len)) |
| 214 return false; | 233 return false; |
| 215 full_hashes->push_back(full_hash); | 234 full_hashes->push_back(full_hash); |
| 216 } | 235 } |
| 217 | 236 |
| 218 // Discard the metadata for now. | 237 // Discard the metadata for now. |
| 219 if (has_metadata) { | 238 if (has_metadata && !ParseGetHashMetadata(hash_count, &reader)) |
| 220 for (size_t i = 0; i < hash_count; ++i) { | 239 return false; |
| 221 base::StringPiece line; | |
| 222 if (!reader.GetLine(&line)) | |
| 223 return false; | |
| 224 | |
| 225 size_t meta_data_len; | |
| 226 if (!base::StringToSizeT(line, &meta_data_len)) | |
| 227 return false; | |
| 228 | |
| 229 const void* meta_data; | |
| 230 if (!reader.RefData(&meta_data, meta_data_len)) | |
| 231 return false; | |
| 232 } | |
| 233 } | |
| 234 } | 240 } |
| 235 | 241 |
| 236 return reader.empty(); | 242 return reader.empty(); |
| 237 } | 243 } |
| 238 | 244 |
| 239 // BODY = HEADER LF PREFIXES EOF | 245 // BODY = HEADER LF PREFIXES EOF |
| 240 // HEADER = PREFIXSIZE ":" LENGTH | 246 // HEADER = PREFIXSIZE ":" LENGTH |
| 241 // PREFIXSIZE = DIGIT+ # Size of each prefix in bytes | 247 // PREFIXSIZE = DIGIT+ # Size of each prefix in bytes |
| 242 // LENGTH = DIGIT+ # Size of PREFIXES in bytes | 248 // LENGTH = DIGIT+ # Size of PREFIXES in bytes |
| 243 std::string FormatGetHash(const std::vector<SBPrefix>& prefixes) { | 249 std::string FormatGetHash(const std::vector<SBPrefix>& prefixes) { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 if (!list.adds.empty() && !list.subs.empty()) | 381 if (!list.adds.empty() && !list.subs.empty()) |
| 376 formatted_results.append(":"); | 382 formatted_results.append(":"); |
| 377 if (!list.subs.empty()) | 383 if (!list.subs.empty()) |
| 378 formatted_results.append("s:").append(list.subs); | 384 formatted_results.append("s:").append(list.subs); |
| 379 formatted_results.append("\n"); | 385 formatted_results.append("\n"); |
| 380 | 386 |
| 381 return formatted_results; | 387 return formatted_results; |
| 382 } | 388 } |
| 383 | 389 |
| 384 } // namespace safe_browsing | 390 } // namespace safe_browsing |
| OLD | NEW |