aboutsummaryrefslogtreecommitdiff
path: root/cc/input_stream.h
blob: cd832b05233af22d827a899fb50d82af56a8adb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef TINK_INPUT_STREAM_H_
#define TINK_INPUT_STREAM_H_

#include "tink/util/status.h"
#include "tink/util/statusor.h"

namespace crypto {
namespace tink {

// Abstract interface similar to an input stream, and to
// Protocol Buffers' google::protobuf::io::ZeroCopyInputStream.
class InputStream {
 public:
  InputStream() = default;
  virtual ~InputStream() = default;

  // Obtains a chunk of data from the stream.
  //
  // Preconditions:
  // * "data" is not NULL.
  //
  // Postconditions:
  // * If the returned status is not OK, there is no more data to return
  //   (status equal to OUT_OF_RANGE) or an error occurred
  //   (status not in {OK, OUT_OF_RANGE}.  All errors are permanent.
  // * Otherwise, the returned value is the number of bytes read and "data"
  //   points to a buffer containing these bytes.
  // * Ownership of this buffer remains with the stream, and the buffer
  //   remains valid only until some other non-const method of the stream
  //   is called or the stream is destroyed.
  // * It is legal for the returned buffer to have zero size, as long as
  //   repeatedly calling Next() eventually yields a buffer with non-zero
  //   size or a non-OK status.
  virtual crypto::tink::util::StatusOr<int> Next(const void** data) = 0;

  // Backs up a number of bytes, so that the next call to Next() returns
  // data again that was already returned by the last call to Next().
  // This is useful when writing procedures that are only supposed to read
  // up to a certain point in the input, then return.  If Next() returns a
  // buffer that goes beyond what you wanted to read, you can use BackUp()
  // to return to the point where you intended to finish.
  //
  // Preconditions:
  // * The last call to Next() must have returned status OK.
  //   If there was no Next()-call yet, or the last one failed,
  //   BackUp()-call is ignored.
  // * count must be less than or equal to the size of the last buffer
  //   returned by Next().  Non-positive count is ignored (no action on this
  //   stream), and count larger than the size of the last buffer is treated
  //   as equal to the size of the last buffer.
  //
  // Postconditions:
  // * The last "count" bytes of the last buffer returned by Next() will be
  //   pushed back into the stream.  Subsequent calls to Next() will return
  //   the same data again before producing new data.
  // * Repeated calls to BackUp() accumulate:
  //      BackUp(a);
  //      Backup(b);
  //   is equivalent to
  //      Backup(c);
  //   with c = max(0, a) + max(0, b).
  // * The actual result of BackUp()-call can be verified via Position().
  virtual void BackUp(int count) = 0;

  // Returns the current byte position in the stream.
  //
  // Preconditions:
  // * The last call to Next() ended with status in {OK, OUT_OF_RANGE}.
  //
  // Postconditions:
  // * Position equal to i means that the next call to Next() will
  //   return a data buffer starting at (i+1)st byte of the stream (if any).
  // * If the last call to Next() reached end of stream (status OUT_OF_RANGE),
  //   then returned value is the total number of bytes in the stream.
  // * If the last call to Next() ended with a failure, -1 is returned;
  virtual int64_t Position() const = 0;
};

}  // namespace tink
}  // namespace crypto

#endif  // TINK_INPUT_STREAM_H_