blob: effac3df59e48fb54ead9b215b0dd0a2c00f8572 [file] [log] [blame]
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#pragma once
#include <cstdint>
#include <limits>
#include "pw_status/status_with_size.h"
#include "pw_stream/stream.h"
namespace pw {
namespace varint {
/// @brief Decodes a variable-length integer (varint) from the current position
/// of a `pw::stream`. Reads a maximum of 10 bytes or `max_size`, whichever is
/// smaller.
///
/// @param reader The `pw::stream` to read from.
///
/// @param output The integer to read into. If reading into a signed integer,
/// the value is
/// [ZigZag](https://protobuf.dev/programming-guides/encoding/#signed-ints)-decoded.
///
/// @param max_size The maximum number of bytes to read. The upper limit is 10
/// bytes.
///
/// @returns The number of bytes read from the stream if successful. The value
/// is placed in `output`. Returns `OutOfRange` if the varint does not fit into
/// `output`. Also returns `OutOfRange` if the input is exhausted before the
/// number terminates.
StatusWithSize Read(stream::Reader& reader,
int64_t* output,
size_t max_size = std::numeric_limits<size_t>::max());
StatusWithSize Read(stream::Reader& reader,
uint64_t* output,
size_t max_size = std::numeric_limits<size_t>::max());
} // namespace varint
} // namespace pw