/src/connectedhomeip/third_party/fuzztest/common/blob_file.h
Line | Count | Source |
1 | | // Copyright 2022 The Centipede Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // Blob is a sequence of bytes, a BlobFile is a sequence of blobs. |
16 | | // BlobFileReader reads blobs from a BlobFile. |
17 | | // BlobFileWriter writes blobs to a BlobFile. |
18 | | // Only one active BlobFileWriter is allowed for a given file. |
19 | | // Multiple BlobFileReader objects can open the same file, concurrently |
20 | | // with up to one BlobFileWriter. |
21 | | // BlobFileReader/BlobFileWriter should have some level of protection against |
22 | | // failed operations (may vary depending on the implementation). |
23 | | // Different implementations of BlobFileReader/BlobFileWriter don't have to |
24 | | // be file-format compatible. |
25 | | |
26 | | #ifndef FUZZTEST_COMMON_BLOB_FILE_H_ |
27 | | #define FUZZTEST_COMMON_BLOB_FILE_H_ |
28 | | |
29 | | #include <memory> |
30 | | #include <string_view> |
31 | | #include <vector> |
32 | | |
33 | | #include "absl/base/nullability.h" |
34 | | #include "absl/status/status.h" |
35 | | #include "./common/defs.h" |
36 | | |
37 | | namespace centipede { |
38 | | |
39 | | // Reads blobs from a BlobFile. See the top file comment. |
40 | | class BlobFileReader { |
41 | | public: |
42 | 0 | BlobFileReader() = default; |
43 | | // Implementations must take care to call their Close() in the dtor, unless |
44 | | // the client has already explicitly called it. |
45 | 0 | virtual ~BlobFileReader() = default; |
46 | | |
47 | | // Not copyable or movable. |
48 | | BlobFileReader(const BlobFileReader &) = delete; |
49 | | BlobFileReader &operator=(const BlobFileReader &) = delete; |
50 | | BlobFileReader(BlobFileReader &&) = delete; |
51 | | BlobFileReader &operator=(BlobFileReader &&) = delete; |
52 | | |
53 | | // Opens the file `path`. |
54 | | virtual absl::Status Open(std::string_view path) = 0; |
55 | | |
56 | | // Reads one `blob` from an open file. |
57 | | // Implementations must ensure that the memory wrapped by `blob` remains valid |
58 | | // until the next Read() or Close() call. |
59 | | // Returns absl::OutOfRangeError when there are no more blobs to read. |
60 | | virtual absl::Status Read(ByteSpan &blob) = 0; |
61 | | |
62 | | // Closes the previously opened file, if any. |
63 | | virtual absl::Status Close() = 0; |
64 | | }; |
65 | | |
66 | | // Writes blobs to a BlobFile. See the top file comment. |
67 | | class BlobFileWriter { |
68 | | public: |
69 | 0 | BlobFileWriter() = default; |
70 | | // Implementations must take care to call their Close() in the dtor, unless |
71 | | // the client has already explicitly called it. |
72 | 0 | virtual ~BlobFileWriter() = default; |
73 | | |
74 | | // Not copyable or movable. |
75 | | BlobFileWriter(const BlobFileWriter &) = delete; |
76 | | BlobFileWriter &operator=(const BlobFileWriter &) = delete; |
77 | | BlobFileWriter(BlobFileWriter &&) = delete; |
78 | | BlobFileWriter &operator=(BlobFileWriter &&) = delete; |
79 | | |
80 | | // Opens the file `path` with mode `mode` (which must be either "w" or "a" at |
81 | | // the moment). Implementations must ensure that this is called only once. |
82 | | virtual absl::Status Open(std::string_view path, std::string_view mode) = 0; |
83 | | |
84 | | // Writes `blob` to this file. Implementations must ensure that the file has |
85 | | // been opened. |
86 | | virtual absl::Status Write(ByteSpan blob) = 0; |
87 | | |
88 | | // Same as above, but for `ByteArray`. |
89 | 0 | absl::Status Write(const ByteArray &bytes) { return Write(ByteSpan{bytes}); } |
90 | | |
91 | | // Closes the file, which was previously opened and never closed. |
92 | | virtual absl::Status Close() = 0; |
93 | | }; |
94 | | |
95 | | // Creates a new object of a default implementation of BlobFileReader. |
96 | | // The current default implementation supports reading files in the bespoke |
97 | | // legacy or Riegeli (https://github.com/google/riegeli) format. |
98 | | std::unique_ptr<BlobFileReader> DefaultBlobFileReaderFactory(); |
99 | | |
100 | | // Creates a new object of a default implementation of BlobFileWriter. |
101 | | // If `riegeli` is `true`, the implementation uses Riegeli |
102 | | // (https://github.com/google/riegeli). |
103 | | std::unique_ptr<BlobFileWriter> DefaultBlobFileWriterFactory( |
104 | | #ifdef CENTIPEDE_DISABLE_RIEGELI |
105 | | bool riegeli = false |
106 | | #else |
107 | | bool riegeli = true |
108 | | #endif // CENTIPEDE_DISABLE_RIEGELI |
109 | | ); |
110 | | |
111 | | // Adds a prefix and a postfix to `data` such that the result can be |
112 | | // appended to another such packed data and then the operation can be reversed. |
113 | | // The purpose is to allow appending blobs of data to a (possibly remote) file |
114 | | // such that when reading this file we can separate the blobs. |
115 | | // NOTE: The now-default blob file format (Riegeli) doesn't need this, but some |
116 | | // external clients continue to use plain blob files and are unlikely to switch |
117 | | // (e.g. Chromium). |
118 | | ByteArray PackBytesForAppendFile(ByteSpan blob); |
119 | | // Unpacks `packed_data` into `unpacked` and `hashes`. |
120 | | // `packed_data` is multiple data packed by PackBytesForAppendFile() |
121 | | // and merged together. |
122 | | // `unpacked` or `hashes` can be nullptr. |
123 | | void UnpackBytesFromAppendFile( |
124 | | const ByteArray &packed_data, |
125 | | absl::Nullable<std::vector<ByteArray> *> unpacked, |
126 | | absl::Nullable<std::vector<std::string> *> hashes = nullptr); |
127 | | |
128 | | } // namespace centipede |
129 | | |
130 | | #endif // FUZZTEST_COMMON_BLOB_FILE_H_ |