| 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 #include "chrome/browser/safe_browsing/prefix_set.h" | 5 #include "chrome/browser/safe_browsing/prefix_set.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <math.h> | 8 #include <math.h> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/scoped_file.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/md5.h" | 13 #include "base/md5.h" |
| 13 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 // |kMagic| should be reasonably unique, and not match itself across | 18 // |kMagic| should be reasonably unique, and not match itself across |
| 18 // endianness changes. I generated this value with: | 19 // endianness changes. I generated this value with: |
| 19 // md5 -qs chrome/browser/safe_browsing/prefix_set.cc | colrm 9 | 20 // md5 -qs chrome/browser/safe_browsing/prefix_set.cc | colrm 9 |
| 20 static uint32 kMagic = 0x864088dd; | 21 static uint32 kMagic = 0x864088dd; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 159 |
| 159 // static | 160 // static |
| 160 PrefixSet* PrefixSet::LoadFile(const base::FilePath& filter_name) { | 161 PrefixSet* PrefixSet::LoadFile(const base::FilePath& filter_name) { |
| 161 int64 size_64; | 162 int64 size_64; |
| 162 if (!base::GetFileSize(filter_name, &size_64)) | 163 if (!base::GetFileSize(filter_name, &size_64)) |
| 163 return NULL; | 164 return NULL; |
| 164 using base::MD5Digest; | 165 using base::MD5Digest; |
| 165 if (size_64 < static_cast<int64>(sizeof(FileHeader) + sizeof(MD5Digest))) | 166 if (size_64 < static_cast<int64>(sizeof(FileHeader) + sizeof(MD5Digest))) |
| 166 return NULL; | 167 return NULL; |
| 167 | 168 |
| 168 file_util::ScopedFILE file(base::OpenFile(filter_name, "rb")); | 169 base::ScopedFILE file(base::OpenFile(filter_name, "rb")); |
| 169 if (!file.get()) | 170 if (!file.get()) |
| 170 return NULL; | 171 return NULL; |
| 171 | 172 |
| 172 FileHeader header; | 173 FileHeader header; |
| 173 size_t read = fread(&header, sizeof(header), 1, file.get()); | 174 size_t read = fread(&header, sizeof(header), 1, file.get()); |
| 174 if (read != 1) | 175 if (read != 1) |
| 175 return NULL; | 176 return NULL; |
| 176 | 177 |
| 177 // TODO(shess): Version 1 and 2 use the same file structure, with version 1 | 178 // TODO(shess): Version 1 and 2 use the same file structure, with version 1 |
| 178 // data using a signed sort. For M-35, the data is re-sorted before return. | 179 // data using a signed sort. For M-35, the data is re-sorted before return. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 header.index_size = static_cast<uint32>(index_.size()); | 255 header.index_size = static_cast<uint32>(index_.size()); |
| 255 header.deltas_size = static_cast<uint32>(deltas_.size()); | 256 header.deltas_size = static_cast<uint32>(deltas_.size()); |
| 256 | 257 |
| 257 // Sanity check that the 32-bit values never mess things up. | 258 // Sanity check that the 32-bit values never mess things up. |
| 258 if (static_cast<size_t>(header.index_size) != index_.size() || | 259 if (static_cast<size_t>(header.index_size) != index_.size() || |
| 259 static_cast<size_t>(header.deltas_size) != deltas_.size()) { | 260 static_cast<size_t>(header.deltas_size) != deltas_.size()) { |
| 260 NOTREACHED(); | 261 NOTREACHED(); |
| 261 return false; | 262 return false; |
| 262 } | 263 } |
| 263 | 264 |
| 264 file_util::ScopedFILE file(base::OpenFile(filter_name, "wb")); | 265 base::ScopedFILE file(base::OpenFile(filter_name, "wb")); |
| 265 if (!file.get()) | 266 if (!file.get()) |
| 266 return false; | 267 return false; |
| 267 | 268 |
| 268 base::MD5Context context; | 269 base::MD5Context context; |
| 269 base::MD5Init(&context); | 270 base::MD5Init(&context); |
| 270 | 271 |
| 271 // TODO(shess): The I/O code in safe_browsing_store_file.cc would | 272 // TODO(shess): The I/O code in safe_browsing_store_file.cc would |
| 272 // sure be useful about now. | 273 // sure be useful about now. |
| 273 size_t written = fwrite(&header, sizeof(header), 1, file.get()); | 274 size_t written = fwrite(&header, sizeof(header), 1, file.get()); |
| 274 if (written != 1) | 275 if (written != 1) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 if (written != 1) | 309 if (written != 1) |
| 309 return false; | 310 return false; |
| 310 | 311 |
| 311 // TODO(shess): Can this code check that the close was successful? | 312 // TODO(shess): Can this code check that the close was successful? |
| 312 file.reset(); | 313 file.reset(); |
| 313 | 314 |
| 314 return true; | 315 return true; |
| 315 } | 316 } |
| 316 | 317 |
| 317 } // namespace safe_browsing | 318 } // namespace safe_browsing |
| OLD | NEW |