xref: /aosp_15_r20/external/cronet/net/disk_cache/simple/simple_entry_format.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_ENTRY_FORMAT_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_FORMAT_H_
7 
8 #include <stdint.h>
9 
10 #include "net/base/net_export.h"
11 
12 namespace disk_cache {
13 
14 const uint64_t kSimpleInitialMagicNumber = UINT64_C(0xfcfb6d1ba7725c30);
15 const uint64_t kSimpleFinalMagicNumber = UINT64_C(0xf4fa6f45970d41d8);
16 const uint64_t kSimpleSparseRangeMagicNumber = UINT64_C(0xeb97bf016553676b);
17 
18 // A file containing stream 0 and stream 1 in the Simple cache consists of:
19 //   - a SimpleFileHeader.
20 //   - the key.
21 //   - the data from stream 1.
22 //   - a SimpleFileEOF record for stream 1.
23 //   - the data from stream 0.
24 //   - (optionally) the SHA256 of the key.
25 //   - a SimpleFileEOF record for stream 0.
26 //
27 // Because stream 0 data (typically HTTP headers) is on the critical path of
28 // requests, on open, the cache reads the end of the record and does not
29 // read the SimpleFileHeader. If the key can be validated with a SHA256, then
30 // the stream 0 data can be returned to the caller without reading the
31 // SimpleFileHeader. If the key SHA256 is not present, then the cache must
32 // read the SimpleFileHeader to confirm key equality.
33 
34 // A file containing stream 2 in the Simple cache consists of:
35 //   - a SimpleFileHeader.
36 //   - the key.
37 //   - the data.
38 //   - at the end, a SimpleFileEOF record.
39 
40 // This is the number of files we can use for representing normal/dense streams.
41 static const int kSimpleEntryNormalFileCount = 2;
42 static const int kSimpleEntryStreamCount = 3;
43 
44 // Total # of files name we can potentially use; this includes both normal
45 // API and sparse streams.
46 static const int kSimpleEntryTotalFileCount = kSimpleEntryNormalFileCount + 1;
47 
48 // Note that stream 0/stream 1 files rely on the footer to verify the entry,
49 // so if the format changes, it's insufficient to change the version here;
50 // likely the EOF magic should be updated as well.
51 struct NET_EXPORT_PRIVATE SimpleFileHeader {
52   SimpleFileHeader();
53 
54   uint64_t initial_magic_number;
55   uint32_t version;
56   uint32_t key_length;
57   uint32_t key_hash;
58 };
59 
60 struct NET_EXPORT_PRIVATE SimpleFileEOF {
61   enum Flags {
62     FLAG_HAS_CRC32 = (1U << 0),
63     FLAG_HAS_KEY_SHA256 = (1U << 1),  // Preceding the record if present.
64   };
65 
66   SimpleFileEOF();
67 
68   uint64_t final_magic_number;
69   uint32_t flags;
70   uint32_t data_crc32;
71   // |stream_size| is only used in the EOF record for stream 0.
72   uint32_t stream_size;
73 };
74 
75 struct SimpleFileSparseRangeHeader {
76   SimpleFileSparseRangeHeader();
77 
78   uint64_t sparse_range_magic_number;
79   int64_t offset;
80   int64_t length;
81   uint32_t data_crc32;
82 };
83 
84 }  // namespace disk_cache
85 
86 #endif  // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_FORMAT_H_
87