xref: /aosp_15_r20/external/cronet/net/disk_cache/simple/simple_util.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
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_util.h"
6 
7 #include <string.h>
8 
9 #include <limits>
10 #include <string_view>
11 
12 #include "base/check_op.h"
13 #include "base/files/file_util.h"
14 #include "base/format_macros.h"
15 #include "base/hash/sha1.h"
16 #include "base/numerics/safe_conversions.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/threading/thread_restrictions.h"
20 #include "base/time/time.h"
21 #include "net/disk_cache/simple/simple_entry_format.h"
22 #include "third_party/zlib/zlib.h"
23 
24 namespace {
25 
26 // Size of the uint64_t hash_key number in Hex format in a string.
27 const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof(uint64_t);
28 
29 }  // namespace
30 
31 namespace disk_cache::simple_util {
32 
ConvertEntryHashKeyToHexString(uint64_t hash_key)33 std::string ConvertEntryHashKeyToHexString(uint64_t hash_key) {
34   const std::string hash_key_str = base::StringPrintf("%016" PRIx64, hash_key);
35   DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size());
36   return hash_key_str;
37 }
38 
GetEntryHashKeyAsHexString(const std::string & key)39 std::string GetEntryHashKeyAsHexString(const std::string& key) {
40   std::string hash_key_str =
41       ConvertEntryHashKeyToHexString(GetEntryHashKey(key));
42   DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size());
43   return hash_key_str;
44 }
45 
GetEntryHashKeyFromHexString(std::string_view hash_key,uint64_t * hash_key_out)46 bool GetEntryHashKeyFromHexString(std::string_view hash_key,
47                                   uint64_t* hash_key_out) {
48   if (hash_key.size() != kEntryHashKeyAsHexStringSize) {
49     return false;
50   }
51   return base::HexStringToUInt64(hash_key, hash_key_out);
52 }
53 
GetEntryHashKey(const std::string & key)54 uint64_t GetEntryHashKey(const std::string& key) {
55   unsigned char sha_hash[base::kSHA1Length];
56 
57   base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(key.data()),
58                       key.size(), sha_hash);
59   uint64_t as_uint64;
60   memcpy(&as_uint64, sha_hash, sizeof(as_uint64));
61   return as_uint64;
62 }
63 
GetFilenameFromEntryFileKeyAndFileIndex(const SimpleFileTracker::EntryFileKey & key,int file_index)64 std::string GetFilenameFromEntryFileKeyAndFileIndex(
65     const SimpleFileTracker::EntryFileKey& key,
66     int file_index) {
67   if (key.doom_generation == 0)
68     return base::StringPrintf("%016" PRIx64 "_%1d", key.entry_hash, file_index);
69   else
70     return base::StringPrintf("todelete_%016" PRIx64 "_%1d_%" PRIu64,
71                               key.entry_hash, file_index, key.doom_generation);
72 }
73 
GetSparseFilenameFromEntryFileKey(const SimpleFileTracker::EntryFileKey & key)74 std::string GetSparseFilenameFromEntryFileKey(
75     const SimpleFileTracker::EntryFileKey& key) {
76   if (key.doom_generation == 0)
77     return base::StringPrintf("%016" PRIx64 "_s", key.entry_hash);
78   else
79     return base::StringPrintf("todelete_%016" PRIx64 "_s_%" PRIu64,
80                               key.entry_hash, key.doom_generation);
81 }
82 
GetFilenameFromKeyAndFileIndex(const std::string & key,int file_index)83 std::string GetFilenameFromKeyAndFileIndex(const std::string& key,
84                                            int file_index) {
85   return GetEntryHashKeyAsHexString(key) +
86          base::StringPrintf("_%1d", file_index);
87 }
88 
GetHeaderSize(size_t key_length)89 size_t GetHeaderSize(size_t key_length) {
90   return sizeof(SimpleFileHeader) + key_length;
91 }
92 
GetDataSizeFromFileSize(size_t key_length,int64_t file_size)93 int32_t GetDataSizeFromFileSize(size_t key_length, int64_t file_size) {
94   int64_t data_size =
95       file_size - key_length - sizeof(SimpleFileHeader) - sizeof(SimpleFileEOF);
96   return base::checked_cast<int32_t>(data_size);
97 }
98 
GetFileSizeFromDataSize(size_t key_length,int32_t data_size)99 int64_t GetFileSizeFromDataSize(size_t key_length, int32_t data_size) {
100   return data_size + key_length + sizeof(SimpleFileHeader) +
101          sizeof(SimpleFileEOF);
102 }
103 
GetFileIndexFromStreamIndex(int stream_index)104 int GetFileIndexFromStreamIndex(int stream_index) {
105   return (stream_index == 2) ? 1 : 0;
106 }
107 
Crc32(base::span<const uint8_t> data)108 uint32_t Crc32(base::span<const uint8_t> data) {
109   auto chars = base::as_chars(data);
110   return Crc32(chars.data(), base::checked_cast<int>(data.size()));
111 }
112 
Crc32(const char * data,int length)113 uint32_t Crc32(const char* data, int length) {
114   uint32_t empty_crc = crc32(0, Z_NULL, 0);
115   if (length == 0)
116     return empty_crc;
117   return crc32(empty_crc, reinterpret_cast<const Bytef*>(data), length);
118 }
119 
IncrementalCrc32(uint32_t previous_crc,const char * data,int length)120 uint32_t IncrementalCrc32(uint32_t previous_crc, const char* data, int length) {
121   return crc32(previous_crc, reinterpret_cast<const Bytef*>(data), length);
122 }
123 
124 }  // namespace disk_cache::simple_util
125