xref: /aosp_15_r20/external/pigweed/pw_varint/public/pw_varint/stream.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include <cstdint>
17 #include <limits>
18 
19 #include "pw_status/status_with_size.h"
20 #include "pw_stream/stream.h"
21 
22 namespace pw {
23 namespace varint {
24 
25 /// @brief Decodes a variable-length integer (varint) from the current position
26 /// of a `pw::stream`. Reads a maximum of 10 bytes or `max_size`, whichever is
27 /// smaller.
28 ///
29 /// This method always returns the number of bytes read, even on error.
30 ///
31 /// @param reader The `pw::stream` to read from.
32 ///
33 /// @param output The integer to read into. If reading into a signed integer,
34 /// the value is
35 /// [ZigZag](https://protobuf.dev/programming-guides/encoding/#signed-ints)-decoded.
36 ///
37 /// @param max_size The maximum number of bytes to read. The upper limit is 10
38 /// bytes.
39 ///
40 /// @returns @rst
41 ///
42 /// .. pw-status-codes::
43 ///
44 ///    OK: The decoded value is placed in ``output``.
45 ///
46 ///    OUT_OF_RANGE: No input is available, e.g. the stream is closed.
47 ///
48 ///    DATA_LOSS: The decoded value is too large for ``output`` or is
49 ///    incomplete, e.g. the input was exhausted after a partial varint was
50 ///    read.
51 ///
52 /// @endrst
53 StatusWithSize Read(stream::Reader& reader,
54                     int64_t* output,
55                     size_t max_size = std::numeric_limits<size_t>::max());
56 StatusWithSize Read(stream::Reader& reader,
57                     uint64_t* output,
58                     size_t max_size = std::numeric_limits<size_t>::max());
59 
60 }  // namespace varint
61 }  // namespace pw
62