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_disk_format.h" | 5 #include "net/disk_cache/simple/simple_disk_format.h" |
6 | 6 |
7 #include "base/hash.h" | 7 #include "base/hash.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/sha1.h" | 9 #include "base/sha1.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 | 12 |
13 namespace disk_cache { | 13 namespace disk_cache { |
14 | 14 |
15 std::string GetEntryHashForKey(const std::string& key) { | 15 std::string GetEntryHashForKey(const std::string& key) { |
16 const std::string sha_hash = base::SHA1HashString(key); | 16 const std::string sha_hash = base::SHA1HashString(key); |
17 const std::string key_hash = base::StringPrintf( | 17 const std::string key_hash = base::StringPrintf( |
18 "%02x%02x%02x%02x%02x", | 18 "%02x%02x%02x%02x%02x", |
19 implicit_cast<unsigned char>(sha_hash[0]), | 19 implicit_cast<unsigned char>(sha_hash[0]), |
20 implicit_cast<unsigned char>(sha_hash[1]), | 20 implicit_cast<unsigned char>(sha_hash[1]), |
21 implicit_cast<unsigned char>(sha_hash[2]), | 21 implicit_cast<unsigned char>(sha_hash[2]), |
22 implicit_cast<unsigned char>(sha_hash[3]), | 22 implicit_cast<unsigned char>(sha_hash[3]), |
23 implicit_cast<unsigned char>(sha_hash[4])); | 23 implicit_cast<unsigned char>(sha_hash[4])); |
24 DCHECK_EQ(static_cast<size_t>(kEntryHashKeySize), key_hash.size()); | 24 DCHECK_EQ(static_cast<size_t>(kEntryHashKeySize), key_hash.size()); |
25 return key_hash; | 25 return key_hash; |
26 } | 26 } |
27 | 27 |
28 namespace SimpleIndexFile { | 28 namespace SimpleIndexFile { |
29 | 29 |
30 EntryMetadata::EntryMetadata() : | 30 Footer::Footer() { |
31 last_used_time(0), | 31 // Make hashing repeatable: leave no padding bytes untouched. |
32 entry_size(0) { | 32 memset(this, 0, sizeof(*this)); |
| 33 } |
| 34 |
| 35 Header::Header() { |
| 36 // Make hashing repeatable: leave no padding bytes untouched. |
| 37 memset(this, 0, sizeof(*this)); |
| 38 } |
| 39 |
| 40 EntryMetadata::EntryMetadata() { |
| 41 // Make hashing repeatable: leave no padding bytes untouched. |
| 42 memset(this, 0, sizeof(*this)); |
33 } | 43 } |
34 | 44 |
35 EntryMetadata::EntryMetadata(const std::string& hash_key_p, | 45 EntryMetadata::EntryMetadata(const std::string& hash_key_p, |
36 base::Time last_used_time_p, | 46 base::Time last_used_time_p, |
37 uint64 entry_size_p) : | 47 uint64 entry_size_p) { |
38 last_used_time(last_used_time_p.ToInternalValue()), | 48 // Make hashing repeatable: leave no padding bytes untouched. |
39 entry_size(entry_size_p) { | 49 memset(this, 0, sizeof(*this)); |
| 50 |
| 51 // Proceed with field initializations. |
| 52 entry_size = entry_size_p; |
| 53 last_used_time = last_used_time_p.ToInternalValue(); |
40 DCHECK_EQ(kEntryHashKeySize, implicit_cast<int>(hash_key_p.size())); | 54 DCHECK_EQ(kEntryHashKeySize, implicit_cast<int>(hash_key_p.size())); |
41 hash_key_p.copy(hash_key, kEntryHashKeySize); | 55 hash_key_p.copy(hash_key, kEntryHashKeySize); |
42 } | 56 } |
43 | 57 |
44 std::string EntryMetadata::GetHashKey() const { | 58 std::string EntryMetadata::GetHashKey() const { |
45 return std::string(hash_key, kEntryHashKeySize); | 59 return std::string(hash_key, kEntryHashKeySize); |
46 } | 60 } |
47 | 61 |
48 base::Time EntryMetadata::GetLastUsedTime() const { | 62 base::Time EntryMetadata::GetLastUsedTime() const { |
49 return base::Time::FromInternalValue(last_used_time); | 63 return base::Time::FromInternalValue(last_used_time); |
(...skipping 17 matching lines...) Expand all Loading... |
67 void EntryMetadata::DeSerialize(const char* in_buffer, | 81 void EntryMetadata::DeSerialize(const char* in_buffer, |
68 EntryMetadata* out_entry_metadata) { | 82 EntryMetadata* out_entry_metadata) { |
69 DCHECK(in_buffer); | 83 DCHECK(in_buffer); |
70 DCHECK(out_entry_metadata); | 84 DCHECK(out_entry_metadata); |
71 memcpy(out_entry_metadata, in_buffer, kEntryMetadataSize); | 85 memcpy(out_entry_metadata, in_buffer, kEntryMetadataSize); |
72 } | 86 } |
73 | 87 |
74 } // namespace SimpleIndexFile | 88 } // namespace SimpleIndexFile |
75 | 89 |
76 } // namespace disk_cache | 90 } // namespace disk_cache |
OLD | NEW |