Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: net/disk_cache/simple/simple_util.cc

Issue 14263005: Refactor our SimpleIndex file format and serialization to use Pickle instead of the previously bugg… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
9 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/sha1.h"
10 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
11 #include "net/disk_cache/simple/simple_disk_format.h" 13 #include "base/strings/string_number_conversions.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
12 22
13 namespace disk_cache { 23 namespace disk_cache {
14 24
15 namespace simple_util { 25 namespace simple_util {
16 26
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
17 std::string GetFilenameFromKeyAndIndex(const std::string& key, int index) { 55 std::string GetFilenameFromKeyAndIndex(const std::string& key, int index) {
18 return disk_cache::GetEntryHashKeyAsHexString(key) + 56 return GetEntryHashKeyAsHexString(key) + base::StringPrintf("_%1d", index);
19 base::StringPrintf("_%1d", index);
20 } 57 }
21 58
22 int32 GetDataSizeFromKeyAndFileSize(const std::string& key, int64 file_size) { 59 int32 GetDataSizeFromKeyAndFileSize(const std::string& key, int64 file_size) {
23 int64 data_size = file_size - key.size() - 60 int64 data_size = file_size - key.size() -
24 sizeof(disk_cache::SimpleFileHeader); 61 sizeof(disk_cache::SimpleFileHeader);
25 DCHECK_GE(implicit_cast<int64>(std::numeric_limits<int32>::max()), data_size); 62 DCHECK_GE(implicit_cast<int64>(std::numeric_limits<int32>::max()), data_size);
26 return data_size; 63 return data_size;
27 } 64 }
28 65
29 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) { 66 int64 GetFileSizeFromKeyAndDataSize(const std::string& key, int32 data_size) {
30 return data_size + key.size() + sizeof(disk_cache::SimpleFileHeader); 67 return data_size + key.size() + sizeof(disk_cache::SimpleFileHeader);
31 } 68 }
32 69
33 int64 GetFileOffsetFromKeyAndDataOffset(const std::string& key, 70 int64 GetFileOffsetFromKeyAndDataOffset(const std::string& key,
34 int data_offset) { 71 int data_offset) {
35 const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key.size(); 72 const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key.size();
36 return headers_size + data_offset; 73 return headers_size + data_offset;
37 } 74 }
38 75
39 } // namespace simple_backend 76 } // namespace simple_backend
40 77
41 } // namespace disk_cache 78 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698