1 // Copyright 2018 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_INPUT_STREAM_H_ 18 #define TINK_INPUT_STREAM_H_ 19 20 #include "tink/util/status.h" 21 #include "tink/util/statusor.h" 22 23 namespace crypto { 24 namespace tink { 25 26 // Abstract interface similar to an input stream, and to 27 // Protocol Buffers' google::protobuf::io::ZeroCopyInputStream. 28 class InputStream { 29 public: 30 InputStream() = default; 31 virtual ~InputStream() = default; 32 33 // Obtains a chunk of data from the stream. 34 // 35 // Preconditions: 36 // * "data" is not NULL. 37 // 38 // Postconditions: 39 // * If the returned status is not OK, there is no more data to return 40 // (status equal to OUT_OF_RANGE) or an error occurred 41 // (status not in {OK, OUT_OF_RANGE}. All errors are permanent. 42 // * Otherwise, the returned value is the number of bytes read and "data" 43 // points to a buffer containing these bytes. 44 // * Ownership of this buffer remains with the stream, and the buffer 45 // remains valid only until some other non-const method of the stream 46 // is called or the stream is destroyed. 47 // * It is legal for the returned buffer to have zero size, as long as 48 // repeatedly calling Next() eventually yields a buffer with non-zero 49 // size or a non-OK status. 50 virtual crypto::tink::util::StatusOr<int> Next(const void** data) = 0; 51 52 // Backs up a number of bytes, so that the next call to Next() returns 53 // data again that was already returned by the last call to Next(). 54 // This is useful when writing procedures that are only supposed to read 55 // up to a certain point in the input, then return. If Next() returns a 56 // buffer that goes beyond what you wanted to read, you can use BackUp() 57 // to return to the point where you intended to finish. 58 // 59 // Preconditions: 60 // * The last call to Next() must have returned status OK. 61 // If there was no Next()-call yet, or the last one failed, 62 // BackUp()-call is ignored. 63 // * count must be less than or equal to the size of the last buffer 64 // returned by Next(). Non-positive count is ignored (no action on this 65 // stream), and count larger than the size of the last buffer is treated 66 // as equal to the size of the last buffer. 67 // 68 // Postconditions: 69 // * The last "count" bytes of the last buffer returned by Next() will be 70 // pushed back into the stream. Subsequent calls to Next() will return 71 // the same data again before producing new data. 72 // * Repeated calls to BackUp() accumulate: 73 // BackUp(a); 74 // Backup(b); 75 // is equivalent to 76 // Backup(c); 77 // with c = max(0, a) + max(0, b). 78 // * The actual result of BackUp()-call can be verified via Position(). 79 virtual void BackUp(int count) = 0; 80 81 // Returns the current byte position in the stream. 82 // 83 // Preconditions: 84 // * The last call to Next() ended with status in {OK, OUT_OF_RANGE}. 85 // 86 // Postconditions: 87 // * Position equal to i means that the next call to Next() will 88 // return a data buffer starting at (i+1)st byte of the stream (if any). 89 // * If the last call to Next() reached end of stream (status OUT_OF_RANGE), 90 // then returned value is the total number of bytes in the stream. 91 // * If the last call to Next() ended with a failure, -1 is returned; 92 virtual int64_t Position() const = 0; 93 }; 94 95 } // namespace tink 96 } // namespace crypto 97 98 #endif // TINK_INPUT_STREAM_H_ 99