xref: /aosp_15_r20/external/tink/cc/random_access_stream.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1*e7b1675dSTing-Kang Chang // Copyright 2019 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_RANDOM_ACCESS_STREAM_H_
18*e7b1675dSTing-Kang Chang #define TINK_RANDOM_ACCESS_STREAM_H_
19*e7b1675dSTing-Kang Chang 
20*e7b1675dSTing-Kang Chang #include "tink/util/buffer.h"
21*e7b1675dSTing-Kang Chang #include "tink/util/status.h"
22*e7b1675dSTing-Kang Chang #include "tink/util/statusor.h"
23*e7b1675dSTing-Kang Chang 
24*e7b1675dSTing-Kang Chang namespace crypto {
25*e7b1675dSTing-Kang Chang namespace tink {
26*e7b1675dSTing-Kang Chang 
27*e7b1675dSTing-Kang Chang // Abstract interface for streams that provide random access for reading,
28*e7b1675dSTing-Kang Chang // like regular files.
29*e7b1675dSTing-Kang Chang class RandomAccessStream {
30*e7b1675dSTing-Kang Chang  public:
31*e7b1675dSTing-Kang Chang   RandomAccessStream() = default;
32*e7b1675dSTing-Kang Chang   virtual ~RandomAccessStream() = default;
33*e7b1675dSTing-Kang Chang 
34*e7b1675dSTing-Kang Chang   // Reads up to 'count' bytes starting at 'position' and writes them
35*e7b1675dSTing-Kang Chang   // to 'dest_buffer'.  'position' must be within the size of the stream,
36*e7b1675dSTing-Kang Chang   // 'count' must be positive, 'dest_buffer' must be non-NULL and its
37*e7b1675dSTing-Kang Chang   // allocated size must be not smaller than 'count'.
38*e7b1675dSTing-Kang Chang   //
39*e7b1675dSTing-Kang Chang   // Return values:
40*e7b1675dSTing-Kang Chang   //  OK: if exactly 'count' bytes were read and written to 'dest_buffer',
41*e7b1675dSTing-Kang Chang   //      or if fewer than 'count' (potentially 0) bytes were read,
42*e7b1675dSTing-Kang Chang   //      but the lack of bytes is not permanent, and retrying may succeed;
43*e7b1675dSTing-Kang Chang   //      in this case 'dest_buffer' contains the read bytes, and the size
44*e7b1675dSTing-Kang Chang   //      of the buffer has been changed to the number of bytes read.
45*e7b1675dSTing-Kang Chang   //  OUT_OF_RANGE: if the end of stream has been reached;
46*e7b1675dSTing-Kang Chang   //      in this case 'dest_buffer' contains the bytes read till the end
47*e7b1675dSTing-Kang Chang   //      of the stream (if any).  This status is returned also when
48*e7b1675dSTing-Kang Chang   //      'position' is larger than the current size of the stream.
49*e7b1675dSTing-Kang Chang   //  INVALID_ARGUMENT: if some of the arguments are not valid.
50*e7b1675dSTing-Kang Chang   //  other: if some other error occurred.
51*e7b1675dSTing-Kang Chang   virtual crypto::tink::util::Status PRead(
52*e7b1675dSTing-Kang Chang       int64_t position,
53*e7b1675dSTing-Kang Chang       int count,
54*e7b1675dSTing-Kang Chang       crypto::tink::util::Buffer* dest_buffer) = 0;
55*e7b1675dSTing-Kang Chang 
56*e7b1675dSTing-Kang Chang   // Returns the size of this stream in bytes, if available.
57*e7b1675dSTing-Kang Chang   // If the size is not available, returns a non-Ok status.
58*e7b1675dSTing-Kang Chang   // The returned value is the "logical" size of a stream, i.e. of
59*e7b1675dSTing-Kang Chang   // a sequence of bytes), stating how many bytes are there in the sequence.
60*e7b1675dSTing-Kang Chang   // For a successful PRead-operation the starting position should be
61*e7b1675dSTing-Kang Chang   // in the range 0..size()-1 (otherwise PRead may return a non-Ok status).
62*e7b1675dSTing-Kang Chang   virtual crypto::tink::util::StatusOr<int64_t> size() = 0;
63*e7b1675dSTing-Kang Chang };
64*e7b1675dSTing-Kang Chang 
65*e7b1675dSTing-Kang Chang }  // namespace tink
66*e7b1675dSTing-Kang Chang }  // namespace crypto
67*e7b1675dSTing-Kang Chang 
68*e7b1675dSTing-Kang Chang #endif  // TINK_RANDOM_ACCESS_STREAM_H_
69