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