1 /*
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 
11 #ifndef LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_
12 #define LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <string>
18 
19 #include "absl/strings/string_view.h"
20 #include "rtc_base/bit_buffer.h"
21 
22 namespace webrtc {
23 
24 extern const size_t kMaxVarIntLengthBytes;
25 
26 // Encode a given uint64_t as a varint. From least to most significant,
27 // each batch of seven bits are put into the lower bits of a byte, and the last
28 // remaining bit in that byte (the highest one) marks whether additional bytes
29 // follow (which happens if and only if there are other bits in |input| which
30 // are non-zero).
31 // Notes: If input == 0, one byte is used. If input is uint64_t::max, exactly
32 // kMaxVarIntLengthBytes are used.
33 std::string EncodeVarInt(uint64_t input);
34 
35 // Inverse of EncodeVarInt().
36 // If decoding is successful, a non-zero number is returned, indicating the
37 // number of bytes read from |input|, and the decoded varint is written
38 // into |output|.
39 // If not successful, 0 is returned, and |output| is not modified.
40 size_t DecodeVarInt(absl::string_view input, uint64_t* output);
41 
42 // Same as other version, but uses a rtc::BitBuffer for input.
43 // Some bits may be consumed even if a varint fails to be read.
44 size_t DecodeVarInt(rtc::BitBuffer* input, uint64_t* output);
45 
46 }  // namespace webrtc
47 
48 #endif  // LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_
49