blob: 4624e046ba084416de728a50b7bf2ef825234089 [file] [log] [blame]
Elad Alonff435412018-10-26 17:09:411/*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 17:11:0011#ifndef LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_
12#define LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_
Elad Alonff435412018-10-26 17:09:4113
Yves Gerey3e707812018-11-28 15:47:4914#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3316
Elad Alonff435412018-10-26 17:09:4117#include <string>
Björn Tereliusb37180f2021-03-31 11:56:5718#include <utility>
Elad Alonff435412018-10-26 17:09:4119
20#include "absl/strings/string_view.h"
Danil Chapovalov707e3d12021-09-07 13:19:1621#include "rtc_base/bitstream_reader.h"
Elad Alonff435412018-10-26 17:09:4122
23namespace webrtc {
24
25extern const size_t kMaxVarIntLengthBytes;
26
27// Encode a given uint64_t as a varint. From least to most significant,
28// each batch of seven bits are put into the lower bits of a byte, and the last
29// remaining bit in that byte (the highest one) marks whether additional bytes
Björn Tereliusb37180f2021-03-31 11:56:5730// follow (which happens if and only if there are other bits in `input` which
Elad Alonff435412018-10-26 17:09:4131// are non-zero).
32// Notes: If input == 0, one byte is used. If input is uint64_t::max, exactly
33// kMaxVarIntLengthBytes are used.
34std::string EncodeVarInt(uint64_t input);
35
36// Inverse of EncodeVarInt().
Björn Tereliusb37180f2021-03-31 11:56:5737// Returns true and the remaining (unread) slice of the input if decoding
38// succeeds. Returns false otherwise and `output` is not modified.
39std::pair<bool, absl::string_view> DecodeVarInt(absl::string_view input,
40 uint64_t* output);
Elad Alonff435412018-10-26 17:09:4141
Danil Chapovalov707e3d12021-09-07 13:19:1642// Same as other version, but uses a BitstreamReader for input.
43// If decoding is successful returns the decoded varint.
44// If not successful, `input` reader is set into the failure state, return value
45// is unspecified.
46uint64_t DecodeVarInt(BitstreamReader& input);
Elad Alonff435412018-10-26 17:09:4147
48} // namespace webrtc
49
Steve Anton10542f22019-01-11 17:11:0050#endif // LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_