xref: /aosp_15_r20/external/tink/cc/streamingaead/shared_random_access_stream.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 Google LLC
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 //     http://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 ///////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef TINK_STREAMINGAEAD_SHARED_RANDOM_ACCESS_STREAM_H_
18 #define TINK_STREAMINGAEAD_SHARED_RANDOM_ACCESS_STREAM_H_
19 
20 #include "tink/random_access_stream.h"
21 #include "tink/util/buffer.h"
22 #include "tink/util/status.h"
23 #include "tink/util/statusor.h"
24 
25 namespace crypto {
26 namespace tink {
27 namespace streamingaead {
28 
29 // A RandomAccessStream that wraps another RandomAccessStream
30 // as a non-owned pointer.
31 // The wrapper forwards all calls to the wrapped RandomAccessStream,
32 // which must remain alive as long as as the wrapper is in use.
33 class SharedRandomAccessStream : public crypto::tink::RandomAccessStream {
34  public:
35   // Constructs an RandomAccessStream that wraps 'random_access_stream',
36   // and will forward all the method calls to this wrapped stream.
SharedRandomAccessStream(crypto::tink::RandomAccessStream * random_access_stream)37   explicit SharedRandomAccessStream(
38       crypto::tink::RandomAccessStream* random_access_stream)
39       : random_access_stream_(random_access_stream) {}
40 
41   ~SharedRandomAccessStream() override = default;
42 
PRead(int64_t position,int count,crypto::tink::util::Buffer * dest_buffer)43   crypto::tink::util::Status PRead(
44       int64_t position, int count,
45       crypto::tink::util::Buffer* dest_buffer) override {
46     return random_access_stream_->PRead(position, count, dest_buffer);
47   }
48 
size()49   crypto::tink::util::StatusOr<int64_t> size() override {
50     return random_access_stream_->size();
51   }
52 
53  private:
54   crypto::tink::RandomAccessStream* random_access_stream_;
55 };
56 
57 }  // namespace streamingaead
58 }  // namespace tink
59 }  // namespace crypto
60 
61 #endif  // TINK_STREAMINGAEAD_SHARED_RANDOM_ACCESS_STREAM_H_
62