xref: /aosp_15_r20/external/tink/cc/streamingaead/buffered_input_stream.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 Google Inc.
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_BUFFERED_INPUT_STREAM_H_
18 #define TINK_STREAMINGAEAD_BUFFERED_INPUT_STREAM_H_
19 
20 #include <memory>
21 #include <vector>
22 
23 #include "tink/input_stream.h"
24 #include "tink/util/status.h"
25 #include "tink/util/statusor.h"
26 
27 namespace crypto {
28 namespace tink {
29 namespace streamingaead {
30 
31 // An InputStream that initially buffers all the read bytes, and offers
32 // rewind-functionality, until explicitly instructed to disable
33 // rewinding (and stop buffering).
34 class BufferedInputStream : public crypto::tink::InputStream {
35  public:
36   // Constructs an InputStream that will read from 'input_stream',
37   // buffering all the read bytes in memory, and offering rewinding
38   // to the beginning of the stream (as long as rewinding is enabled).
39   explicit BufferedInputStream(
40       std::unique_ptr<crypto::tink::InputStream> input_stream);
41 
42   ~BufferedInputStream() override;
43 
44   crypto::tink::util::StatusOr<int> Next(const void** data) override;
45 
46   void BackUp(int count) override;
47 
48   int64_t Position() const override;
49 
50   // Rewinds this stream to the beginning (if rewinding is still enabled).
51   crypto::tink::util::Status Rewind();
52 
53   // Disables rewinding.
54   void DisableRewinding();
55 
56  private:
57   std::unique_ptr<crypto::tink::InputStream> input_stream_;
58   bool direct_access_;      // true iff we don't buffer any data any more
59 
60   // The fields below are valid and in use iff direct_access_ is false.
61   // Once direct_access_ becomes true, all the calls to this stream's methods
62   // are directly relayed to methods of input_stream_.
63   crypto::tink::util::Status status_;
64   std::vector<uint8_t> buffer_;
65   bool after_rewind_;       // true iff no Next has been called after rewind
66   bool rewinding_enabled_;  // true iff this stream can be rewound
67   int64_t position_;     // current position in the stream (from the beginning)
68 
69   // Counters that describe the state of the data in buffer_.
70   int count_in_buffer_;  // # of bytes available in buffer_
71   int count_backedup_;   // # of bytes available in buffer_ that were backed up
72   int buffer_offset_;    // offset at which the returned bytes start in buffer_
73 };
74 
75 }  // namespace streamingaead
76 }  // namespace tink
77 }  // namespace crypto
78 
79 #endif  // TINK_STREAMINGAEAD_BUFFERED_INPUT_STREAM_H_
80