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