| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "net/disk_cache/simple/simple_synchronous_entry.h" | 5 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 #include <functional> | 9 #include <functional> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 | 686 |
| 687 int64_t len_from_start = len - (start - offset); | 687 int64_t len_from_start = len - (start - offset); |
| 688 *out_start = start; | 688 *out_start = start; |
| 689 *out_result = static_cast<int>(std::min(avail_so_far, len_from_start)); | 689 *out_result = static_cast<int>(std::min(avail_so_far, len_from_start)); |
| 690 } | 690 } |
| 691 | 691 |
| 692 int SimpleSynchronousEntry::CheckEOFRecord(int index, | 692 int SimpleSynchronousEntry::CheckEOFRecord(int index, |
| 693 const SimpleEntryStat& entry_stat, | 693 const SimpleEntryStat& entry_stat, |
| 694 uint32_t expected_crc32) const { | 694 uint32_t expected_crc32) const { |
| 695 DCHECK(initialized_); | 695 DCHECK(initialized_); |
| 696 |
| 696 uint32_t crc32; | 697 uint32_t crc32; |
| 697 bool has_crc32; | 698 bool has_crc32; |
| 698 bool has_key_sha256; | 699 if (index == 1) { |
| 699 int32_t stream_size; | 700 DCHECK(read_stream1_crc32_); |
| 700 int rv = GetEOFRecordData(index, entry_stat, &has_crc32, &has_key_sha256, | 701 crc32 = stream1_crc32_; |
| 701 &crc32, &stream_size); | 702 has_crc32 = has_stream1_crc32_; |
| 702 if (rv != net::OK) { | 703 } else { |
| 703 Doom(); | 704 DCHECK_EQ(2, index); |
| 704 return rv; | 705 bool has_key_sha256; |
| 706 int32_t stream_size; |
| 707 int rv = GetEOFRecordData(index, entry_stat, &has_crc32, &has_key_sha256, |
| 708 &crc32, &stream_size); |
| 709 if (rv != net::OK) { |
| 710 Doom(); |
| 711 return rv; |
| 712 } |
| 705 } | 713 } |
| 714 |
| 706 if (has_crc32 && crc32 != expected_crc32) { | 715 if (has_crc32 && crc32 != expected_crc32) { |
| 707 DVLOG(1) << "EOF record had bad crc."; | 716 DVLOG(1) << "EOF record had bad crc."; |
| 708 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | 717 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); |
| 709 Doom(); | 718 Doom(); |
| 710 return net::ERR_CACHE_CHECKSUM_MISMATCH; | 719 return net::ERR_CACHE_CHECKSUM_MISMATCH; |
| 711 } | 720 } |
| 712 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); | 721 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); |
| 713 return net::OK; | 722 return net::OK; |
| 714 } | 723 } |
| 715 | 724 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 815 const FilePath& path, | 824 const FilePath& path, |
| 816 const std::string& key, | 825 const std::string& key, |
| 817 const uint64_t entry_hash, | 826 const uint64_t entry_hash, |
| 818 const bool had_index) | 827 const bool had_index) |
| 819 : cache_type_(cache_type), | 828 : cache_type_(cache_type), |
| 820 path_(path), | 829 path_(path), |
| 821 entry_hash_(entry_hash), | 830 entry_hash_(entry_hash), |
| 822 had_index_(had_index), | 831 had_index_(had_index), |
| 823 key_(key), | 832 key_(key), |
| 824 have_open_files_(false), | 833 have_open_files_(false), |
| 825 initialized_(false) { | 834 initialized_(false), |
| 835 read_stream1_crc32_(false) { |
| 826 for (int i = 0; i < kSimpleEntryFileCount; ++i) | 836 for (int i = 0; i < kSimpleEntryFileCount; ++i) |
| 827 empty_file_omitted_[i] = false; | 837 empty_file_omitted_[i] = false; |
| 828 } | 838 } |
| 829 | 839 |
| 830 SimpleSynchronousEntry::~SimpleSynchronousEntry() { | 840 SimpleSynchronousEntry::~SimpleSynchronousEntry() { |
| 831 DCHECK(!(have_open_files_ && initialized_)); | 841 DCHECK(!(have_open_files_ && initialized_)); |
| 832 if (have_open_files_) | 842 if (have_open_files_) |
| 833 CloseFiles(); | 843 CloseFiles(); |
| 834 } | 844 } |
| 835 | 845 |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1225 | 1235 |
| 1226 // Calculate and set the real values for the two streams. | 1236 // Calculate and set the real values for the two streams. |
| 1227 int32_t total_size = out_entry_stat->data_size(1); | 1237 int32_t total_size = out_entry_stat->data_size(1); |
| 1228 if (!has_key_sha256) | 1238 if (!has_key_sha256) |
| 1229 total_size += sizeof(net::SHA256HashValue); | 1239 total_size += sizeof(net::SHA256HashValue); |
| 1230 if (stream_0_size > total_size) | 1240 if (stream_0_size > total_size) |
| 1231 return net::ERR_FAILED; | 1241 return net::ERR_FAILED; |
| 1232 out_entry_stat->set_data_size(0, stream_0_size); | 1242 out_entry_stat->set_data_size(0, stream_0_size); |
| 1233 out_entry_stat->set_data_size(1, total_size - stream_0_size); | 1243 out_entry_stat->set_data_size(1, total_size - stream_0_size); |
| 1234 | 1244 |
| 1235 // Put stream 0 data in memory. | 1245 // Put stream 0 data in memory, as well as sha256 for it and the footer |
| 1246 // of stream 1, so we can get its CRC conveniently in one go |
| 1247 |
| 1236 *stream_0_data = new net::GrowableIOBuffer(); | 1248 *stream_0_data = new net::GrowableIOBuffer(); |
| 1237 (*stream_0_data)->SetCapacity(stream_0_size + sizeof(net::SHA256HashValue)); | 1249 (*stream_0_data) |
| 1238 int file_offset = out_entry_stat->GetOffsetInFile(key_.size(), 0, 0); | 1250 ->SetCapacity(sizeof(SimpleFileEOF) + stream_0_size + |
| 1239 int read_size = stream_0_size; | 1251 sizeof(net::SHA256HashValue)); |
| 1252 int file_offset = out_entry_stat->GetOffsetInFile(key_.size(), 0, 0) - |
| 1253 sizeof(SimpleFileEOF); |
| 1254 if (file_offset < 0) |
| 1255 return net::ERR_FAILED; |
| 1256 |
| 1257 int read_size = stream_0_size + sizeof(SimpleFileEOF); |
| 1240 if (has_key_sha256) | 1258 if (has_key_sha256) |
| 1241 read_size += sizeof(net::SHA256HashValue); | 1259 read_size += sizeof(net::SHA256HashValue); |
| 1242 if (files_[0].Read(file_offset, (*stream_0_data)->data(), read_size) != | 1260 if (files_[0].Read(file_offset, (*stream_0_data)->data(), read_size) != |
| 1243 read_size) | 1261 read_size) |
| 1244 return net::ERR_FAILED; | 1262 return net::ERR_FAILED; |
| 1245 | 1263 |
| 1246 // Check the CRC32. | 1264 // Extract the EOF record for stream 1, and skip over it in the buffer. |
| 1265 SimpleFileEOF stream1_eof; |
| 1266 memcpy(&stream1_eof, (*stream_0_data)->data(), sizeof(SimpleFileEOF)); |
| 1267 (*stream_0_data)->set_offset(sizeof(SimpleFileEOF)); |
| 1268 |
| 1269 // Verify it, exract CRC. |
| 1270 // ### refactor this stuff to not be crude copy and paste |
| 1271 if (stream1_eof.final_magic_number != kSimpleFinalMagicNumber) { |
| 1272 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); |
| 1273 DVLOG(1) << "EOF record had bad magic number."; |
| 1274 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; |
| 1275 } |
| 1276 |
| 1277 if (!base::IsValueInRangeForNumericType<int32_t>(stream1_eof.stream_size)) |
| 1278 return net::ERR_FAILED; |
| 1279 |
| 1280 read_stream1_crc32_ = true; |
| 1281 has_stream1_crc32_ = (stream1_eof.flags & SimpleFileEOF::FLAG_HAS_CRC32) == |
| 1282 SimpleFileEOF::FLAG_HAS_CRC32; |
| 1283 stream1_crc32_ = stream1_eof.data_crc32; |
| 1284 |
| 1285 // Check the CRC32 for stream 0 |
| 1247 uint32_t expected_crc32 = | 1286 uint32_t expected_crc32 = |
| 1248 stream_0_size == 0 | 1287 stream_0_size == 0 |
| 1249 ? crc32(0, Z_NULL, 0) | 1288 ? crc32(0, Z_NULL, 0) |
| 1250 : crc32(crc32(0, Z_NULL, 0), | 1289 : crc32(crc32(0, Z_NULL, 0), |
| 1251 reinterpret_cast<const Bytef*>((*stream_0_data)->data()), | 1290 reinterpret_cast<const Bytef*>((*stream_0_data)->data()), |
| 1252 stream_0_size); | 1291 stream_0_size); |
| 1253 if (has_crc32 && read_crc32 != expected_crc32) { | 1292 if (has_crc32 && read_crc32 != expected_crc32) { |
| 1254 DVLOG(1) << "EOF record had bad crc."; | 1293 DVLOG(1) << "EOF record had bad crc."; |
| 1255 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); | 1294 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); |
| 1256 return net::ERR_FAILED; | 1295 return net::ERR_FAILED; |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1634 range.offset = offset; | 1673 range.offset = offset; |
| 1635 range.length = len; | 1674 range.length = len; |
| 1636 range.data_crc32 = data_crc32; | 1675 range.data_crc32 = data_crc32; |
| 1637 range.file_offset = data_file_offset; | 1676 range.file_offset = data_file_offset; |
| 1638 sparse_ranges_.insert(std::make_pair(offset, range)); | 1677 sparse_ranges_.insert(std::make_pair(offset, range)); |
| 1639 | 1678 |
| 1640 return true; | 1679 return true; |
| 1641 } | 1680 } |
| 1642 | 1681 |
| 1643 } // namespace disk_cache | 1682 } // namespace disk_cache |
| OLD | NEW |