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