Elad Alon | ff43541 | 2018-10-26 17:09:41 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_ |
| 12 | #define LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_ |
Elad Alon | ff43541 | 2018-10-26 17:09:41 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 16 | |
Elad Alon | ff43541 | 2018-10-26 17:09:41 | [diff] [blame] | 17 | #include <string> |
Björn Terelius | b37180f | 2021-03-31 11:56:57 | [diff] [blame] | 18 | #include <utility> |
Elad Alon | ff43541 | 2018-10-26 17:09:41 | [diff] [blame] | 19 | |
| 20 | #include "absl/strings/string_view.h" |
Danil Chapovalov | 707e3d1 | 2021-09-07 13:19:16 | [diff] [blame] | 21 | #include "rtc_base/bitstream_reader.h" |
Elad Alon | ff43541 | 2018-10-26 17:09:41 | [diff] [blame] | 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 |
Björn Terelius | b37180f | 2021-03-31 11:56:57 | [diff] [blame] | 30 | // follow (which happens if and only if there are other bits in `input` which |
Elad Alon | ff43541 | 2018-10-26 17:09:41 | [diff] [blame] | 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(). |
Björn Terelius | b37180f | 2021-03-31 11:56:57 | [diff] [blame] | 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); |
Elad Alon | ff43541 | 2018-10-26 17:09:41 | [diff] [blame] | 41 | |
Danil Chapovalov | 707e3d1 | 2021-09-07 13:19:16 | [diff] [blame] | 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); |
Elad Alon | ff43541 | 2018-10-26 17:09:41 | [diff] [blame] | 47 | |
| 48 | } // namespace webrtc |
| 49 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 50 | #endif // LOGGING_RTC_EVENT_LOG_ENCODER_VAR_INT_H_ |