xref: /aosp_15_r20/external/webrtc/logging/rtc_event_log/encoder/var_int.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #include <utility>
19 
20 #include "absl/strings/string_view.h"
21 #include "rtc_base/bitstream_reader.h"
22 
23 namespace webrtc {
24 
25 extern 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
30 // follow (which happens if and only if there are other bits in `input` which
31 // are non-zero).
32 // Notes: If input == 0, one byte is used. If input is uint64_t::max, exactly
33 // kMaxVarIntLengthBytes are used.
34 std::string EncodeVarInt(uint64_t input);
35 
36 // Inverse of EncodeVarInt().
37 // Returns true and the remaining (unread) slice of the input if decoding
38 // succeeds. Returns false otherwise and `output` is not modified.
39 std::pair<bool, absl::string_view> DecodeVarInt(absl::string_view input,
40                                                 uint64_t* output);
41 
42 // 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.
46 uint64_t DecodeVarInt(BitstreamReader& input);
47 
48 }  // namespace webrtc
49 
50 #endif  // LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_
51