var_int.h 1.7 KB

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