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