1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
3a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0
4a8e1175bSopenharmony_ci *
5a8e1175bSopenharmony_ci *  Licensed under the Apache License, Version 2.0 (the "License"); you may
6a8e1175bSopenharmony_ci *  not use this file except in compliance with the License.
7a8e1175bSopenharmony_ci *  You may obtain a copy of the License at
8a8e1175bSopenharmony_ci *
9a8e1175bSopenharmony_ci *  http://www.apache.org/licenses/LICENSE-2.0
10a8e1175bSopenharmony_ci *
11a8e1175bSopenharmony_ci *  Unless required by applicable law or agreed to in writing, software
12a8e1175bSopenharmony_ci *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13a8e1175bSopenharmony_ci *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14a8e1175bSopenharmony_ci *  See the License for the specific language governing permissions and
15a8e1175bSopenharmony_ci *  limitations under the License.
16a8e1175bSopenharmony_ci *
17a8e1175bSopenharmony_ci *  This file is part of mbed TLS (https://tls.mbed.org)
18a8e1175bSopenharmony_ci */
19a8e1175bSopenharmony_ci
20a8e1175bSopenharmony_ci/**
21a8e1175bSopenharmony_ci * \file mps_reader.h
22a8e1175bSopenharmony_ci *
23a8e1175bSopenharmony_ci * \brief This file defines reader objects, which together with their
24a8e1175bSopenharmony_ci *        sibling writer objects form the basis for the communication
25a8e1175bSopenharmony_ci *        between the various layers of the Mbed TLS messaging stack,
26a8e1175bSopenharmony_ci *        as well as the communication between the messaging stack and
27a8e1175bSopenharmony_ci *        the (D)TLS handshake protocol implementation.
28a8e1175bSopenharmony_ci *
29a8e1175bSopenharmony_ci * Readers provide a means of transferring incoming data from
30a8e1175bSopenharmony_ci * a 'producer' providing it in chunks of arbitrary size, to
31a8e1175bSopenharmony_ci * a 'consumer' which fetches and processes it in chunks of
32a8e1175bSopenharmony_ci * again arbitrary, and potentially different, size.
33a8e1175bSopenharmony_ci *
34a8e1175bSopenharmony_ci * Readers can thus be seen as datagram-to-stream converters,
35a8e1175bSopenharmony_ci * and they abstract away the following two tasks from the user:
36a8e1175bSopenharmony_ci * 1. The pointer arithmetic of stepping through a producer-
37a8e1175bSopenharmony_ci *    provided chunk in smaller chunks.
38a8e1175bSopenharmony_ci * 2. The merging of incoming data chunks in case the
39a8e1175bSopenharmony_ci *    consumer requests data in larger chunks than what the
40a8e1175bSopenharmony_ci *    producer provides.
41a8e1175bSopenharmony_ci *
42a8e1175bSopenharmony_ci * The basic abstract flow of operation is the following:
43a8e1175bSopenharmony_ci * - Initially, the reader is in 'producing mode'.
44a8e1175bSopenharmony_ci * - The producer hands an incoming data buffer to the reader,
45a8e1175bSopenharmony_ci *   moving it from 'producing' to 'consuming' mode.
46a8e1175bSopenharmony_ci * - The consumer subsequently fetches and processes the buffer
47a8e1175bSopenharmony_ci *   content. Once that's done -- or partially done and a consumer's
48a8e1175bSopenharmony_ci *   request can't be fulfilled -- the producer revokes the reader's
49a8e1175bSopenharmony_ci *   access to the incoming data buffer, putting the reader back to
50a8e1175bSopenharmony_ci *   producing mode.
51a8e1175bSopenharmony_ci * - The producer subsequently gathers more incoming data and hands
52a8e1175bSopenharmony_ci *   it to the reader until it switches back to consuming mode
53a8e1175bSopenharmony_ci *   if enough data is available for the last consumer request to
54a8e1175bSopenharmony_ci *   be satisfiable.
55a8e1175bSopenharmony_ci * - Repeat the above.
56a8e1175bSopenharmony_ci *
57a8e1175bSopenharmony_ci * The abstract states of the reader from the producer's and
58a8e1175bSopenharmony_ci * consumer's perspective are as follows:
59a8e1175bSopenharmony_ci *
60a8e1175bSopenharmony_ci * - From the perspective of the consumer, the state of the
61a8e1175bSopenharmony_ci *   reader consists of the following:
62a8e1175bSopenharmony_ci *   - A byte stream representing (concatenation of) the data
63a8e1175bSopenharmony_ci *     received through calls to mbedtls_mps_reader_get(),
64a8e1175bSopenharmony_ci *   - A marker within that byte stream indicating which data
65a8e1175bSopenharmony_ci *     can be considered processed, and hence need not be retained,
66a8e1175bSopenharmony_ci *     when the reader is passed back to the producer via
67a8e1175bSopenharmony_ci *     mbedtls_mps_reader_reclaim().
68a8e1175bSopenharmony_ci *     The marker is set via mbedtls_mps_reader_commit()
69a8e1175bSopenharmony_ci *     which places it at the end of the current byte stream.
70a8e1175bSopenharmony_ci *   The consumer need not be aware of the distinction between consumer
71a8e1175bSopenharmony_ci *   and producer mode, because it only interfaces with the reader
72a8e1175bSopenharmony_ci *   when the latter is in consuming mode.
73a8e1175bSopenharmony_ci *
74a8e1175bSopenharmony_ci * - From the perspective of the producer, the reader's state is one of:
75a8e1175bSopenharmony_ci *   - Attached: The reader is in consuming mode.
76a8e1175bSopenharmony_ci *   - Unset: No incoming data buffer is currently managed by the reader,
77a8e1175bSopenharmony_ci *            and all previously handed incoming data buffers have been
78a8e1175bSopenharmony_ci *            fully processed. More data needs to be fed into the reader
79a8e1175bSopenharmony_ci *            via mbedtls_mps_reader_feed().
80a8e1175bSopenharmony_ci *
81a8e1175bSopenharmony_ci *   - Accumulating: No incoming data buffer is currently managed by the
82a8e1175bSopenharmony_ci *                   reader, but some data from the previous incoming data
83a8e1175bSopenharmony_ci *                   buffer hasn't been processed yet and is internally
84a8e1175bSopenharmony_ci *                   held back.
85a8e1175bSopenharmony_ci *   The Attached state belongs to consuming mode, while the Unset and
86a8e1175bSopenharmony_ci *   Accumulating states belong to producing mode.
87a8e1175bSopenharmony_ci *
88a8e1175bSopenharmony_ci * Transitioning from the Unset or Accumulating state to Attached is
89a8e1175bSopenharmony_ci * done via successful calls to mbedtls_mps_reader_feed(), while
90a8e1175bSopenharmony_ci * transitioning from Attached to either Unset or Accumulating (depending
91a8e1175bSopenharmony_ci * on what has been processed) is done via mbedtls_mps_reader_reclaim().
92a8e1175bSopenharmony_ci *
93a8e1175bSopenharmony_ci * The following diagram depicts the producer-state progression:
94a8e1175bSopenharmony_ci *
95a8e1175bSopenharmony_ci *        +------------------+             reclaim
96a8e1175bSopenharmony_ci *        |      Unset       +<-------------------------------------+       get
97a8e1175bSopenharmony_ci *        +--------|---------+                                      |   +------+
98a8e1175bSopenharmony_ci *                 |                                                |   |      |
99a8e1175bSopenharmony_ci *                 |                                                |   |      |
100a8e1175bSopenharmony_ci *                 |                feed                  +---------+---+--+   |
101a8e1175bSopenharmony_ci *                 +-------------------------------------->                <---+
102a8e1175bSopenharmony_ci *                                                        |    Attached    |
103a8e1175bSopenharmony_ci *                 +-------------------------------------->                <---+
104a8e1175bSopenharmony_ci *                 |     feed, enough data available      +---------+---+--+   |
105a8e1175bSopenharmony_ci *                 |     to serve previous consumer request         |   |      |
106a8e1175bSopenharmony_ci *                 |                                                |   |      |
107a8e1175bSopenharmony_ci *        +--------+---------+                                      |   +------+
108a8e1175bSopenharmony_ci *   +---->   Accumulating   |<-------------------------------------+    commit
109a8e1175bSopenharmony_ci *   |    +---+--------------+      reclaim, previous read request
110a8e1175bSopenharmony_ci *   |        |                        couldn't be fulfilled
111a8e1175bSopenharmony_ci *   |        |
112a8e1175bSopenharmony_ci *   +--------+
113a8e1175bSopenharmony_ci *     feed, need more data to serve
114a8e1175bSopenharmony_ci *     previous consumer request
115a8e1175bSopenharmony_ci *                                         |
116a8e1175bSopenharmony_ci *                                         |
117a8e1175bSopenharmony_ci *               producing mode            |           consuming mode
118a8e1175bSopenharmony_ci *                                         |
119a8e1175bSopenharmony_ci *
120a8e1175bSopenharmony_ci */
121a8e1175bSopenharmony_ci
122a8e1175bSopenharmony_ci#ifndef MBEDTLS_READER_H
123a8e1175bSopenharmony_ci#define MBEDTLS_READER_H
124a8e1175bSopenharmony_ci
125a8e1175bSopenharmony_ci#include <stdio.h>
126a8e1175bSopenharmony_ci
127a8e1175bSopenharmony_ci#include "mps_common.h"
128a8e1175bSopenharmony_ci#include "mps_error.h"
129a8e1175bSopenharmony_ci
130a8e1175bSopenharmony_cistruct mbedtls_mps_reader;
131a8e1175bSopenharmony_citypedef struct mbedtls_mps_reader mbedtls_mps_reader;
132a8e1175bSopenharmony_ci
133a8e1175bSopenharmony_ci/*
134a8e1175bSopenharmony_ci * Structure definitions
135a8e1175bSopenharmony_ci */
136a8e1175bSopenharmony_ci
137a8e1175bSopenharmony_cistruct mbedtls_mps_reader {
138a8e1175bSopenharmony_ci    unsigned char *frag;  /*!< The fragment of incoming data managed by
139a8e1175bSopenharmony_ci                           *   the reader; it is provided to the reader
140a8e1175bSopenharmony_ci                           *   through mbedtls_mps_reader_feed(). The reader
141a8e1175bSopenharmony_ci                           *   does not own the fragment and does not
142a8e1175bSopenharmony_ci                           *   perform any allocation operations on it,
143a8e1175bSopenharmony_ci                           *   but does have read and write access to it.
144a8e1175bSopenharmony_ci                           *
145a8e1175bSopenharmony_ci                           *   The reader is in consuming mode if
146a8e1175bSopenharmony_ci                           *   and only if \c frag is not \c NULL.          */
147a8e1175bSopenharmony_ci    mbedtls_mps_stored_size_t frag_len;
148a8e1175bSopenharmony_ci    /*!< The length of the current fragment.
149a8e1175bSopenharmony_ci     *   Must be 0 if \c frag == \c NULL.             */
150a8e1175bSopenharmony_ci    mbedtls_mps_stored_size_t commit;
151a8e1175bSopenharmony_ci    /*!< The offset of the last commit, relative
152a8e1175bSopenharmony_ci     *   to the first byte in the fragment, if
153a8e1175bSopenharmony_ci     *   no accumulator is present. If an accumulator
154a8e1175bSopenharmony_ci     *   is present, it is viewed as a prefix to the
155a8e1175bSopenharmony_ci     *   current fragment, and this variable contains
156a8e1175bSopenharmony_ci     *   an offset from the beginning of the accumulator.
157a8e1175bSopenharmony_ci     *
158a8e1175bSopenharmony_ci     *   This is only used when the reader is in
159a8e1175bSopenharmony_ci     *   consuming mode, i.e. \c frag != \c NULL;
160a8e1175bSopenharmony_ci     *   otherwise, its value is \c 0.                */
161a8e1175bSopenharmony_ci    mbedtls_mps_stored_size_t end;
162a8e1175bSopenharmony_ci    /*!< The offset of the end of the last chunk
163a8e1175bSopenharmony_ci     *   passed to the user through a call to
164a8e1175bSopenharmony_ci     *   mbedtls_mps_reader_get(), relative to the first
165a8e1175bSopenharmony_ci     *   byte in the fragment, if no accumulator is
166a8e1175bSopenharmony_ci     *   present. If an accumulator is present, it is
167a8e1175bSopenharmony_ci     *   viewed as a prefix to the current fragment, and
168a8e1175bSopenharmony_ci     *   this variable contains an offset from the
169a8e1175bSopenharmony_ci     *   beginning of the accumulator.
170a8e1175bSopenharmony_ci     *
171a8e1175bSopenharmony_ci     *   This is only used when the reader is in
172a8e1175bSopenharmony_ci     *   consuming mode, i.e. \c frag != \c NULL;
173a8e1175bSopenharmony_ci     *   otherwise, its value is \c 0.                */
174a8e1175bSopenharmony_ci    mbedtls_mps_stored_size_t pending;
175a8e1175bSopenharmony_ci    /*!< The amount of incoming data missing on the
176a8e1175bSopenharmony_ci     *   last call to mbedtls_mps_reader_get().
177a8e1175bSopenharmony_ci     *   In particular, it is \c 0 if the last call
178a8e1175bSopenharmony_ci     *   was successful.
179a8e1175bSopenharmony_ci     *   If a reader is reclaimed after an
180a8e1175bSopenharmony_ci     *   unsuccessful call to mbedtls_mps_reader_get(),
181a8e1175bSopenharmony_ci     *   this variable is used to have the reader
182a8e1175bSopenharmony_ci     *   remember how much data should be accumulated
183a8e1175bSopenharmony_ci     *   so that the call to mbedtls_mps_reader_get()
184a8e1175bSopenharmony_ci     *   succeeds next time.
185a8e1175bSopenharmony_ci     *   This is only used when the reader is in
186a8e1175bSopenharmony_ci     *   consuming mode, i.e. \c frag != \c NULL;
187a8e1175bSopenharmony_ci     *   otherwise, its value is \c 0.                */
188a8e1175bSopenharmony_ci
189a8e1175bSopenharmony_ci    /* The accumulator is only needed if we need to be able to pause
190a8e1175bSopenharmony_ci     * the reader. A few bytes could be saved by moving this to a
191a8e1175bSopenharmony_ci     * separate struct and using a pointer here. */
192a8e1175bSopenharmony_ci
193a8e1175bSopenharmony_ci    unsigned char *acc;   /*!< The accumulator is used to gather incoming
194a8e1175bSopenharmony_ci                           *   data if a read-request via mbedtls_mps_reader_get()
195a8e1175bSopenharmony_ci                           *   cannot be served from the current fragment.   */
196a8e1175bSopenharmony_ci    mbedtls_mps_stored_size_t acc_len;
197a8e1175bSopenharmony_ci    /*!< The total size of the accumulator.           */
198a8e1175bSopenharmony_ci    mbedtls_mps_stored_size_t acc_available;
199a8e1175bSopenharmony_ci    /*!< The number of bytes currently gathered in
200a8e1175bSopenharmony_ci     *   the accumulator. This is both used in
201a8e1175bSopenharmony_ci     *   producing and in consuming mode:
202a8e1175bSopenharmony_ci     *   While producing, it is increased until
203a8e1175bSopenharmony_ci     *   it reaches the value of \c acc_remaining below.
204a8e1175bSopenharmony_ci     *   While consuming, it is used to judge if a
205a8e1175bSopenharmony_ci     *   get request can be served from the
206a8e1175bSopenharmony_ci     *   accumulator or not.
207a8e1175bSopenharmony_ci     *   Must not be larger than \c acc_len.           */
208a8e1175bSopenharmony_ci    union {
209a8e1175bSopenharmony_ci        mbedtls_mps_stored_size_t acc_remaining;
210a8e1175bSopenharmony_ci        /*!< This indicates the amount of data still
211a8e1175bSopenharmony_ci         *   to be gathered in the accumulator. It is
212a8e1175bSopenharmony_ci         *   only used in producing mode.
213a8e1175bSopenharmony_ci         *   Must be at most acc_len - acc_available.  */
214a8e1175bSopenharmony_ci        mbedtls_mps_stored_size_t frag_offset;
215a8e1175bSopenharmony_ci        /*!< If an accumulator is present and in use, this
216a8e1175bSopenharmony_ci         *   field indicates the offset of the current
217a8e1175bSopenharmony_ci         *   fragment from the beginning of the
218a8e1175bSopenharmony_ci         *   accumulator. If no accumulator is present
219a8e1175bSopenharmony_ci         *   or the accumulator is not in use, this is \c 0.
220a8e1175bSopenharmony_ci         *   It is only used in consuming mode.
221a8e1175bSopenharmony_ci         *   Must not be larger than \c acc_available. */
222a8e1175bSopenharmony_ci    } acc_share;
223a8e1175bSopenharmony_ci};
224a8e1175bSopenharmony_ci
225a8e1175bSopenharmony_ci/*
226a8e1175bSopenharmony_ci * API organization:
227a8e1175bSopenharmony_ci * A reader object is usually prepared and maintained
228a8e1175bSopenharmony_ci * by some lower layer and passed for usage to an upper
229a8e1175bSopenharmony_ci * layer, and the API naturally splits according to which
230a8e1175bSopenharmony_ci * layer is supposed to use the respective functions.
231a8e1175bSopenharmony_ci */
232a8e1175bSopenharmony_ci
233a8e1175bSopenharmony_ci/*
234a8e1175bSopenharmony_ci * Maintenance API (Lower layer)
235a8e1175bSopenharmony_ci */
236a8e1175bSopenharmony_ci
237a8e1175bSopenharmony_ci/**
238a8e1175bSopenharmony_ci * \brief           Initialize a reader object
239a8e1175bSopenharmony_ci *
240a8e1175bSopenharmony_ci * \param reader    The reader to be initialized.
241a8e1175bSopenharmony_ci * \param acc       The buffer to be used as a temporary accumulator
242a8e1175bSopenharmony_ci *                  in case get requests through mbedtls_mps_reader_get()
243a8e1175bSopenharmony_ci *                  exceed the buffer provided by mbedtls_mps_reader_feed().
244a8e1175bSopenharmony_ci *                  This buffer is owned by the caller and exclusive use
245a8e1175bSopenharmony_ci *                  for reading and writing is given to the reader for the
246a8e1175bSopenharmony_ci *                  duration of the reader's lifetime. It is thus the caller's
247a8e1175bSopenharmony_ci *                  responsibility to maintain (and not touch) the buffer for
248a8e1175bSopenharmony_ci *                  the lifetime of the reader, and to properly zeroize and
249a8e1175bSopenharmony_ci *                  free the memory after the reader has been destroyed.
250a8e1175bSopenharmony_ci * \param acc_len   The size in Bytes of \p acc.
251a8e1175bSopenharmony_ci *
252a8e1175bSopenharmony_ci * \return          \c 0 on success.
253a8e1175bSopenharmony_ci * \return          A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
254a8e1175bSopenharmony_ci */
255a8e1175bSopenharmony_ciint mbedtls_mps_reader_init(mbedtls_mps_reader *reader,
256a8e1175bSopenharmony_ci                            unsigned char *acc,
257a8e1175bSopenharmony_ci                            mbedtls_mps_size_t acc_len);
258a8e1175bSopenharmony_ci
259a8e1175bSopenharmony_ci/**
260a8e1175bSopenharmony_ci * \brief           Free a reader object
261a8e1175bSopenharmony_ci *
262a8e1175bSopenharmony_ci * \param reader    The reader to be freed.
263a8e1175bSopenharmony_ci *
264a8e1175bSopenharmony_ci * \return          \c 0 on success.
265a8e1175bSopenharmony_ci * \return          A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
266a8e1175bSopenharmony_ci */
267a8e1175bSopenharmony_ciint mbedtls_mps_reader_free(mbedtls_mps_reader *reader);
268a8e1175bSopenharmony_ci
269a8e1175bSopenharmony_ci/**
270a8e1175bSopenharmony_ci * \brief           Pass chunk of data for the reader to manage.
271a8e1175bSopenharmony_ci *
272a8e1175bSopenharmony_ci * \param reader    The reader context to use. The reader must be
273a8e1175bSopenharmony_ci *                  in producing mode.
274a8e1175bSopenharmony_ci * \param buf       The buffer to be managed by the reader.
275a8e1175bSopenharmony_ci * \param buflen    The size in Bytes of \p buffer.
276a8e1175bSopenharmony_ci *
277a8e1175bSopenharmony_ci * \return          \c 0 on success. In this case, the reader will be
278a8e1175bSopenharmony_ci *                  moved to consuming mode and obtains read access
279a8e1175bSopenharmony_ci *                  of \p buf until mbedtls_mps_reader_reclaim()
280a8e1175bSopenharmony_ci *                  is called. It is the responsibility of the caller
281a8e1175bSopenharmony_ci *                  to ensure that the \p buf persists and is not changed
282a8e1175bSopenharmony_ci *                  between successful calls to mbedtls_mps_reader_feed()
283a8e1175bSopenharmony_ci *                  and mbedtls_mps_reader_reclaim().
284a8e1175bSopenharmony_ci * \return          \c MBEDTLS_ERR_MPS_READER_NEED_MORE if more input data is
285a8e1175bSopenharmony_ci *                  required to fulfill a previous request to mbedtls_mps_reader_get().
286a8e1175bSopenharmony_ci *                  In this case, the reader remains in producing mode and
287a8e1175bSopenharmony_ci *                  takes no ownership of the provided buffer (an internal copy
288a8e1175bSopenharmony_ci *                  is made instead).
289a8e1175bSopenharmony_ci * \return          Another negative \c MBEDTLS_ERR_READER_XXX error code on
290a8e1175bSopenharmony_ci *                  different kinds of failures.
291a8e1175bSopenharmony_ci */
292a8e1175bSopenharmony_ciint mbedtls_mps_reader_feed(mbedtls_mps_reader *reader,
293a8e1175bSopenharmony_ci                            unsigned char *buf,
294a8e1175bSopenharmony_ci                            mbedtls_mps_size_t buflen);
295a8e1175bSopenharmony_ci
296a8e1175bSopenharmony_ci/**
297a8e1175bSopenharmony_ci * \brief           Reclaim reader's access to the current input buffer.
298a8e1175bSopenharmony_ci *
299a8e1175bSopenharmony_ci * \param reader    The reader context to use. The reader must be
300a8e1175bSopenharmony_ci *                  in consuming mode.
301a8e1175bSopenharmony_ci * \param paused    If not \c NULL, the integer at address \p paused will be
302a8e1175bSopenharmony_ci *                  modified to indicate whether the reader has been paused
303a8e1175bSopenharmony_ci *                  (value \c 1) or not (value \c 0). Pausing happens if there
304a8e1175bSopenharmony_ci *                  is uncommitted data and a previous request to
305a8e1175bSopenharmony_ci *                  mbedtls_mps_reader_get() has exceeded the bounds of the
306a8e1175bSopenharmony_ci *                  input buffer.
307a8e1175bSopenharmony_ci *
308a8e1175bSopenharmony_ci * \return          \c 0 on success.
309a8e1175bSopenharmony_ci * \return          A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
310a8e1175bSopenharmony_ci */
311a8e1175bSopenharmony_ciint mbedtls_mps_reader_reclaim(mbedtls_mps_reader *reader,
312a8e1175bSopenharmony_ci                               int *paused);
313a8e1175bSopenharmony_ci
314a8e1175bSopenharmony_ci/*
315a8e1175bSopenharmony_ci * Usage API (Upper layer)
316a8e1175bSopenharmony_ci */
317a8e1175bSopenharmony_ci
318a8e1175bSopenharmony_ci/**
319a8e1175bSopenharmony_ci * \brief           Request data from the reader.
320a8e1175bSopenharmony_ci *
321a8e1175bSopenharmony_ci * \param reader    The reader context to use. The reader must
322a8e1175bSopenharmony_ci *                  be in consuming mode.
323a8e1175bSopenharmony_ci * \param desired   The desired amount of data to be read, in Bytes.
324a8e1175bSopenharmony_ci * \param buffer    The address to store the buffer pointer in.
325a8e1175bSopenharmony_ci *                  This must not be \c NULL.
326a8e1175bSopenharmony_ci * \param buflen    The address to store the actual buffer
327a8e1175bSopenharmony_ci *                  length in, or \c NULL.
328a8e1175bSopenharmony_ci *
329a8e1175bSopenharmony_ci * \return          \c 0 on success. In this case, \c *buf holds the
330a8e1175bSopenharmony_ci *                  address of a buffer of size \c *buflen
331a8e1175bSopenharmony_ci *                  (if \c buflen != \c NULL) or \c desired
332a8e1175bSopenharmony_ci *                  (if \c buflen == \c NULL). The user has read access
333a8e1175bSopenharmony_ci *                  to the buffer and guarantee of stability of the data
334a8e1175bSopenharmony_ci *                  until the next call to mbedtls_mps_reader_reclaim().
335a8e1175bSopenharmony_ci * \return          #MBEDTLS_ERR_MPS_READER_OUT_OF_DATA if there is not enough
336a8e1175bSopenharmony_ci *                  data available to serve the get request. In this case, the
337a8e1175bSopenharmony_ci *                  reader remains intact and in consuming mode, and the consumer
338a8e1175bSopenharmony_ci *                  should retry the call after a successful cycle of
339a8e1175bSopenharmony_ci *                  mbedtls_mps_reader_reclaim() and mbedtls_mps_reader_feed().
340a8e1175bSopenharmony_ci *                  If, after such a cycle, the consumer requests a different
341a8e1175bSopenharmony_ci *                  amount of data, the result is implementation-defined;
342a8e1175bSopenharmony_ci *                  progress is guaranteed only if the same amount of data
343a8e1175bSopenharmony_ci *                  is requested after a mbedtls_mps_reader_reclaim() and
344a8e1175bSopenharmony_ci *                  mbedtls_mps_reader_feed() cycle.
345a8e1175bSopenharmony_ci * \return          Another negative \c MBEDTLS_ERR_READER_XXX error
346a8e1175bSopenharmony_ci *                  code for different kinds of failure.
347a8e1175bSopenharmony_ci *
348a8e1175bSopenharmony_ci * \note            Passing \c NULL as \p buflen is a convenient way to
349a8e1175bSopenharmony_ci *                  indicate that fragmentation is not tolerated.
350a8e1175bSopenharmony_ci *                  It's functionally equivalent to passing a valid
351a8e1175bSopenharmony_ci *                  address as buflen and checking \c *buflen == \c desired
352a8e1175bSopenharmony_ci *                  afterwards.
353a8e1175bSopenharmony_ci */
354a8e1175bSopenharmony_ciint mbedtls_mps_reader_get(mbedtls_mps_reader *reader,
355a8e1175bSopenharmony_ci                           mbedtls_mps_size_t desired,
356a8e1175bSopenharmony_ci                           unsigned char **buffer,
357a8e1175bSopenharmony_ci                           mbedtls_mps_size_t *buflen);
358a8e1175bSopenharmony_ci
359a8e1175bSopenharmony_ci/**
360a8e1175bSopenharmony_ci * \brief         Mark data obtained from mbedtls_mps_reader_get() as processed.
361a8e1175bSopenharmony_ci *
362a8e1175bSopenharmony_ci *                This call indicates that all data received from prior calls to
363a8e1175bSopenharmony_ci *                mbedtls_mps_reader_get() has been or will have been
364a8e1175bSopenharmony_ci *                processed when mbedtls_mps_reader_reclaim() is called,
365a8e1175bSopenharmony_ci *                and thus need not be backed up.
366a8e1175bSopenharmony_ci *
367a8e1175bSopenharmony_ci *                This function has no user observable effect until
368a8e1175bSopenharmony_ci *                mbedtls_mps_reader_reclaim() is called. In particular,
369a8e1175bSopenharmony_ci *                buffers received from mbedtls_mps_reader_get() remain
370a8e1175bSopenharmony_ci *                valid until mbedtls_mps_reader_reclaim() is called.
371a8e1175bSopenharmony_ci *
372a8e1175bSopenharmony_ci * \param reader  The reader context to use.
373a8e1175bSopenharmony_ci *
374a8e1175bSopenharmony_ci * \return        \c 0 on success.
375a8e1175bSopenharmony_ci * \return        A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
376a8e1175bSopenharmony_ci *
377a8e1175bSopenharmony_ci */
378a8e1175bSopenharmony_ciint mbedtls_mps_reader_commit(mbedtls_mps_reader *reader);
379a8e1175bSopenharmony_ci
380a8e1175bSopenharmony_ci#endif /* MBEDTLS_READER_H */
381