xref: /aosp_15_r20/external/cronet/net/disk_cache/simple/simple_util.h (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 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_UTIL_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_UTIL_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <string_view>
12 
13 #include "base/containers/span.h"
14 #include "net/base/net_export.h"
15 #include "net/disk_cache/simple/simple_file_tracker.h"
16 
17 namespace base {
18 class FilePath;
19 }
20 
21 namespace disk_cache::simple_util {
22 
23 NET_EXPORT_PRIVATE std::string ConvertEntryHashKeyToHexString(
24     uint64_t hash_key);
25 
26 // |key| is the regular cache key, such as an URL.
27 // Returns the Hex ascii representation of the uint64_t hash_key.
28 NET_EXPORT_PRIVATE std::string GetEntryHashKeyAsHexString(
29     const std::string& key);
30 
31 // |key| is the regular HTTP Cache key, which is a URL.
32 // Returns the hash of the key as uint64_t.
33 NET_EXPORT_PRIVATE uint64_t GetEntryHashKey(const std::string& key);
34 
35 // Parses the |hash_key| string into a uint64_t buffer.
36 // |hash_key| string must be of the form: FFFFFFFFFFFFFFFF .
37 NET_EXPORT_PRIVATE bool GetEntryHashKeyFromHexString(std::string_view hash_key,
38                                                      uint64_t* hash_key_out);
39 
40 // Given a |key| for a (potential) entry in the simple backend and the |index|
41 // of a stream on that entry, returns the filename in which that stream would be
42 // stored.
43 NET_EXPORT_PRIVATE std::string GetFilenameFromKeyAndFileIndex(
44     const std::string& key,
45     int file_index);
46 
47 // Same as |GetFilenameFromKeyAndIndex| above, but using a numeric key.
48 NET_EXPORT_PRIVATE std::string GetFilenameFromEntryFileKeyAndFileIndex(
49     const SimpleFileTracker::EntryFileKey& key,
50     int file_index);
51 
52 // Given a |key| for an entry, returns the name of the sparse data file.
53 NET_EXPORT_PRIVATE std::string GetSparseFilenameFromEntryFileKey(
54     const SimpleFileTracker::EntryFileKey& key);
55 
56 // Given the size of a key, the size in bytes of the header at the beginning
57 // of a simple cache file.
58 size_t GetHeaderSize(size_t key_length);
59 
60 // Given the size of a file holding a stream in the simple backend and the key
61 // to an entry, returns the number of bytes in the stream.
62 NET_EXPORT_PRIVATE int32_t GetDataSizeFromFileSize(size_t key_length,
63                                                    int64_t file_size);
64 
65 // Given the size of a stream in the simple backend and the key to an entry,
66 // returns the number of bytes in the file.
67 NET_EXPORT_PRIVATE int64_t GetFileSizeFromDataSize(size_t key_length,
68                                                    int32_t data_size);
69 
70 // Given the stream index, returns the number of the file the stream is stored
71 // in.
72 NET_EXPORT_PRIVATE int GetFileIndexFromStreamIndex(int stream_index);
73 
74 // Deletes a file, insuring POSIX semantics. Provided that all open handles to
75 // this file were opened with File::FLAG_WIN_SHARE_DELETE, it is possible to
76 // delete an open file and continue to use that file. After deleting an open
77 // file, it is possible to immediately create a new file with the same name.
78 NET_EXPORT_PRIVATE bool SimpleCacheDeleteFile(const base::FilePath& path);
79 
80 // Prefer span form for new code.
81 uint32_t Crc32(base::span<const uint8_t> data);
82 uint32_t Crc32(const char* data, int length);
83 
84 uint32_t IncrementalCrc32(uint32_t previous_crc, const char* data, int length);
85 
86 }  // namespace disk_cache::simple_util
87 
88 #endif  // NET_DISK_CACHE_SIMPLE_SIMPLE_UTIL_H_
89