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/format_macros.h" | 7 #include <cstring> |
8 #include "base/hash.h" | |
9 #include "base/logging.h" | |
10 #include "base/sha1.h" | |
11 #include "base/stringprintf.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/time.h" | |
14 | 8 |
15 namespace disk_cache { | 9 namespace disk_cache { |
16 | 10 |
17 SimpleFileHeader::SimpleFileHeader() { | 11 SimpleFileHeader::SimpleFileHeader() { |
18 // Make hashing repeatable: leave no padding bytes untouched. | 12 // Make hashing repeatable: leave no padding bytes untouched. |
19 memset(this, 0, sizeof(*this)); | 13 std::memset(this, 0, sizeof(*this)); |
20 } | 14 } |
21 | 15 |
22 std::string ConvertEntryHashKeyToHexString(uint64 hash_key) { | |
23 const std::string hash_key_str = base::StringPrintf("%016" PRIx64, hash_key); | |
24 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); | |
25 return hash_key_str; | |
26 } | |
27 | |
28 std::string GetEntryHashKeyAsHexString(const std::string& key) { | |
29 std::string hash_key_str = | |
30 ConvertEntryHashKeyToHexString(GetEntryHashKey(key)); | |
31 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); | |
32 return hash_key_str; | |
33 } | |
34 | |
35 bool GetEntryHashKeyFromHexString(const std::string& hash_key, | |
36 uint64* hash_key_out) { | |
37 if (hash_key.size() != kEntryHashKeyAsHexStringSize) { | |
38 return false; | |
39 } | |
40 return base::HexStringToUInt64(hash_key, hash_key_out); | |
41 } | |
42 | |
43 uint64 GetEntryHashKey(const std::string& key) { | |
44 const std::string sha_hash = base::SHA1HashString(key); | |
45 uint64 hash_key = 0; | |
46 sha_hash.copy(reinterpret_cast<char*>(&hash_key), sizeof(hash_key)); | |
47 return hash_key; | |
48 } | |
49 | |
50 namespace SimpleIndexFile { | |
51 | |
52 Footer::Footer() { | |
53 // Make hashing repeatable: leave no padding bytes untouched. | |
54 memset(this, 0, sizeof(*this)); | |
55 } | |
56 | |
57 Header::Header() { | |
58 // Make hashing repeatable: leave no padding bytes untouched. | |
59 memset(this, 0, sizeof(*this)); | |
60 } | |
61 | |
62 EntryMetadata::EntryMetadata() { | |
63 // Make hashing repeatable: leave no padding bytes untouched. | |
64 memset(this, 0, sizeof(*this)); | |
65 } | |
66 | |
67 EntryMetadata::EntryMetadata(uint64 hash_key_p, | |
68 base::Time last_used_time_p, | |
69 uint64 entry_size_p) { | |
70 // Make hashing repeatable: leave no padding bytes untouched. | |
71 memset(this, 0, sizeof(*this)); | |
72 | |
73 // Proceed with field initializations. | |
74 hash_key = hash_key_p; | |
75 entry_size = entry_size_p; | |
76 last_used_time = last_used_time_p.ToInternalValue(); | |
77 } | |
78 | |
79 uint64 EntryMetadata::GetHashKey() const { | |
80 return hash_key; | |
81 } | |
82 | |
83 base::Time EntryMetadata::GetLastUsedTime() const { | |
84 return base::Time::FromInternalValue(last_used_time); | |
85 } | |
86 | |
87 void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time_p) { | |
88 last_used_time = last_used_time_p.ToInternalValue(); | |
89 } | |
90 | |
91 // static | |
92 void EntryMetadata::Serialize(const EntryMetadata& in_entry_metadata, | |
93 std::string* out_buffer) { | |
94 DCHECK(out_buffer); | |
95 // TODO(felipeg): We may choose to, instead, serialize each struct member | |
96 // separately. | |
97 out_buffer->append(reinterpret_cast<const char*>(&in_entry_metadata), | |
98 kEntryMetadataSize); | |
99 } | |
100 | |
101 // static | |
102 void EntryMetadata::DeSerialize(const char* in_buffer, | |
103 EntryMetadata* out_entry_metadata) { | |
104 DCHECK(in_buffer); | |
105 DCHECK(out_entry_metadata); | |
106 memcpy(out_entry_metadata, in_buffer, kEntryMetadataSize); | |
107 } | |
108 | |
109 // static | |
110 void EntryMetadata::Merge(const EntryMetadata& from, | |
111 EntryMetadata* to) { | |
112 if (to->last_used_time == 0) | |
113 to->last_used_time = from.last_used_time; | |
114 if (to->entry_size == 0) | |
115 to->entry_size = from.entry_size; | |
116 } | |
117 | |
118 } // namespace SimpleIndexFile | |
119 | |
120 } // namespace disk_cache | 16 } // namespace disk_cache |
OLD | NEW |