| 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_util.h" | 5 #include "net/disk_cache/simple/simple_util.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/format_macros.h" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/sha1.h" | |
| 12 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 13 #include "base/strings/string_number_conversions.h" | 11 #include "net/disk_cache/simple/simple_disk_format.h" |
| 14 #include "net/disk_cache/simple/simple_entry_format.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Size of the uint64 hash_key number in Hex format in a string. | |
| 19 const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof(uint64); | |
| 20 | |
| 21 } // namespace | |
| 22 | 12 |
| 23 namespace disk_cache { | 13 namespace disk_cache { |
| 24 | 14 |
| 25 namespace simple_util { | 15 namespace simple_util { |
| 26 | 16 |
| 27 std::string ConvertEntryHashKeyToHexString(uint64 hash_key) { | |
| 28 const std::string hash_key_str = base::StringPrintf("%016" PRIx64, hash_key); | |
| 29 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); | |
| 30 return hash_key_str; | |
| 31 } | |
| 32 | |
| 33 std::string GetEntryHashKeyAsHexString(const std::string& key) { | |
| 34 std::string hash_key_str = | |
| 35 ConvertEntryHashKeyToHexString(GetEntryHashKey(key)); | |
| 36 DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size()); | |
| 37 return hash_key_str; | |
| 38 } | |
| 39 | |
| 40 bool GetEntryHashKeyFromHexString(const std::string& hash_key, | |
| 41 uint64* hash_key_out) { | |
| 42 if (hash_key.size() != kEntryHashKeyAsHexStringSize) { | |
| 43 return false; | |
| 44 } | |
| 45 return base::HexStringToUInt64(hash_key, hash_key_out); | |
| 46 } | |
| 47 | |
| 48 uint64 GetEntryHashKey(const std::string& key) { | |
| 49 const std::string sha_hash = base::SHA1HashString(key); | |
| 50 uint64 hash_key = 0; | |
| 51 sha_hash.copy(reinterpret_cast<char*>(&hash_key), sizeof(hash_key)); | |
| 52 return hash_key; | |
| 53 } | |
| 54 | |
| 55 std::string GetFilenameFromKeyAndIndex(const std::string& key, int index) { | 17 std::string GetFilenameFromKeyAndIndex(const std::string& key, int index) { |
| 56 return GetEntryHashKeyAsHexString(key) + base::StringPrintf("_%1d", index); | 18 return disk_cache::GetEntryHashKeyAsHexString(key) + |
| 19 base::StringPrintf("_%1d", index); |
| 57 } | 20 } |
| 58 | 21 |
| 59 int32 GetDataSizeFromKeyAndFileSize(const std::string& key, int64 file_size) { | 22 int32 GetDataSizeFromKeyAndFileSize(const std::string& key, int64 file_size) { |
| 60 int64 data_size = file_size - key.size() - | 23 int64 data_size = file_size - key.size() - |
| 61 sizeof(disk_cache::SimpleFileHeader); | 24 sizeof(disk_cache::SimpleFileHeader); |
| 62 DCHECK_GE(implicit_cast<int64>(std::numeric_limits<int32>::max()), data_size); | 25 DCHECK_GE(implicit_cast<int64>(std::numeric_limits<int32>::max()), data_size); |
| 63 return data_size; | 26 return data_size; |
| 64 } | 27 } |
| 65 | 28 |
| 66 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) { | 29 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) { |
| 67 return data_size + key.size() + sizeof(disk_cache::SimpleFileHeader); | 30 return data_size + key.size() + sizeof(disk_cache::SimpleFileHeader); |
| 68 } | 31 } |
| 69 | 32 |
| 70 int64 GetFileOffsetFromKeyAndDataOffset(const std::string& key, | 33 int64 GetFileOffsetFromKeyAndDataOffset(const std::string& key, |
| 71 int data_offset) { | 34 int data_offset) { |
| 72 const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key.size(); | 35 const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key.size(); |
| 73 return headers_size + data_offset; | 36 return headers_size + data_offset; |
| 74 } | 37 } |
| 75 | 38 |
| 76 } // namespace simple_backend | 39 } // namespace simple_backend |
| 77 | 40 |
| 78 } // namespace disk_cache | 41 } // namespace disk_cache |
| OLD | NEW |