protozero  1.6.3
Minimalistic protocol buffer decoder and encoder in C++.
varint.hpp
Go to the documentation of this file.
1 #ifndef PROTOZERO_VARINT_HPP
2 #define PROTOZERO_VARINT_HPP
3 
4 /*****************************************************************************
5 
6 protozero - Minimalistic protocol buffer decoder and encoder in C++.
7 
8 This file is from https://github.com/mapbox/protozero where you can find more
9 documentation.
10 
11 *****************************************************************************/
12 
19 #include <protozero/exception.hpp>
20 
21 #include <cstdint>
22 
23 namespace protozero {
24 
28 constexpr const int8_t max_varint_length = sizeof(uint64_t) * 8 / 7 + 1;
29 
30 namespace detail {
31 
32  // from https://github.com/facebook/folly/blob/master/folly/Varint.h
33  inline uint64_t decode_varint_impl(const char** data, const char* end) {
34  const auto begin = reinterpret_cast<const int8_t*>(*data);
35  const auto iend = reinterpret_cast<const int8_t*>(end);
36  const int8_t* p = begin;
37  uint64_t val = 0;
38 
39  if (iend - begin >= max_varint_length) { // fast path
40  do {
41  int64_t b;
42  b = *p++; val = uint64_t((b & 0x7fu) ); if (b >= 0) { break; }
43  b = *p++; val |= uint64_t((b & 0x7fu) << 7u); if (b >= 0) { break; }
44  b = *p++; val |= uint64_t((b & 0x7fu) << 14u); if (b >= 0) { break; }
45  b = *p++; val |= uint64_t((b & 0x7fu) << 21u); if (b >= 0) { break; }
46  b = *p++; val |= uint64_t((b & 0x7fu) << 28u); if (b >= 0) { break; }
47  b = *p++; val |= uint64_t((b & 0x7fu) << 35u); if (b >= 0) { break; }
48  b = *p++; val |= uint64_t((b & 0x7fu) << 42u); if (b >= 0) { break; }
49  b = *p++; val |= uint64_t((b & 0x7fu) << 49u); if (b >= 0) { break; }
50  b = *p++; val |= uint64_t((b & 0x7fu) << 56u); if (b >= 0) { break; }
51  b = *p++; val |= uint64_t((b & 0x01u) << 63u); if (b >= 0) { break; }
53  } while (false);
54  } else {
55  unsigned int shift = 0;
56  while (p != iend && *p < 0) {
57  val |= uint64_t(*p++ & 0x7fu) << shift;
58  shift += 7;
59  }
60  if (p == iend) {
62  }
63  val |= uint64_t(*p++) << shift;
64  }
65 
66  *data = reinterpret_cast<const char*>(p);
67  return val;
68  }
69 
70 } // end namespace detail
71 
89 inline uint64_t decode_varint(const char** data, const char* end) {
90  // If this is a one-byte varint, decode it here.
91  if (end != *data && ((**data & 0x80u) == 0)) {
92  const auto val = static_cast<uint64_t>(**data);
93  ++(*data);
94  return val;
95  }
96  // If this varint is more than one byte, defer to complete implementation.
97  return detail::decode_varint_impl(data, end);
98 }
99 
112 inline void skip_varint(const char** data, const char* end) {
113  const auto begin = reinterpret_cast<const int8_t*>(*data);
114  const auto iend = reinterpret_cast<const int8_t*>(end);
115  const int8_t* p = begin;
116 
117  while (p != iend && *p < 0) {
118  ++p;
119  }
120 
121  if (p >= begin + max_varint_length) {
123  }
124 
125  if (p == iend) {
126  throw end_of_buffer_exception{};
127  }
128 
129  ++p;
130 
131  *data = reinterpret_cast<const char*>(p);
132 }
133 
144 template <typename T>
145 inline int write_varint(T data, uint64_t value) {
146  int n = 1;
147 
148  while (value >= 0x80u) {
149  *data++ = char((value & 0x7fu) | 0x80u);
150  value >>= 7u;
151  ++n;
152  }
153  *data++ = char(value);
154 
155  return n;
156 }
157 
161 inline constexpr uint32_t encode_zigzag32(int32_t value) noexcept {
162  return (static_cast<uint32_t>(value) << 1u) ^ (static_cast<uint32_t>(value >> 31u));
163 }
164 
168 inline constexpr uint64_t encode_zigzag64(int64_t value) noexcept {
169  return (static_cast<uint64_t>(value) << 1u) ^ (static_cast<uint64_t>(value >> 63u));
170 }
171 
175 inline constexpr int32_t decode_zigzag32(uint32_t value) noexcept {
176  return static_cast<int32_t>(value >> 1u) ^ -static_cast<int32_t>(value & 1u);
177 }
178 
182 inline constexpr int64_t decode_zigzag64(uint64_t value) noexcept {
183  return static_cast<int64_t>(value >> 1u) ^ -static_cast<int64_t>(value & 1u);
184 }
185 
186 } // end namespace protozero
187 
188 #endif // PROTOZERO_VARINT_HPP
constexpr uint64_t encode_zigzag64(int64_t value) noexcept
Definition: varint.hpp:168
constexpr int64_t decode_zigzag64(uint64_t value) noexcept
Definition: varint.hpp:182
constexpr const int8_t max_varint_length
Definition: varint.hpp:28
constexpr uint32_t encode_zigzag32(int32_t value) noexcept
Definition: varint.hpp:161
int write_varint(T data, uint64_t value)
Definition: varint.hpp:145
void skip_varint(const char **data, const char *end)
Definition: varint.hpp:112
constexpr int32_t decode_zigzag32(uint32_t value) noexcept
Definition: varint.hpp:175
Contains the exceptions used in the protozero library.
Definition: exception.hpp:41
Definition: exception.hpp:67
uint64_t decode_varint(const char **data, const char *end)
Definition: varint.hpp:89
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:23