protozero
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 <cstdint>
20 
21 #include <protozero/exception.hpp>
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 int8_t* begin = reinterpret_cast<const int8_t*>(*data);
35  const int8_t* 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 & 0x7f) ); if (b >= 0) break;
43  b = *p++; val |= uint64_t((b & 0x7f) << 7); if (b >= 0) break;
44  b = *p++; val |= uint64_t((b & 0x7f) << 14); if (b >= 0) break;
45  b = *p++; val |= uint64_t((b & 0x7f) << 21); if (b >= 0) break;
46  b = *p++; val |= uint64_t((b & 0x7f) << 28); if (b >= 0) break;
47  b = *p++; val |= uint64_t((b & 0x7f) << 35); if (b >= 0) break;
48  b = *p++; val |= uint64_t((b & 0x7f) << 42); if (b >= 0) break;
49  b = *p++; val |= uint64_t((b & 0x7f) << 49); if (b >= 0) break;
50  b = *p++; val |= uint64_t((b & 0x7f) << 56); if (b >= 0) break;
51  b = *p++; val |= uint64_t((b & 0x7f) << 63); if (b >= 0) break;
53  } while (false);
54  } else {
55  int shift = 0;
56  while (p != iend && *p < 0) {
57  val |= uint64_t(*p++ & 0x7f) << 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 & 0x80) == 0)) {
92  uint64_t val = 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 int8_t* begin = reinterpret_cast<const int8_t*>(*data);
114  const int8_t* 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 
143 template <typename T>
144 inline int write_varint(T data, uint64_t value) {
145  int n=1;
146 
147  while (value >= 0x80) {
148  *data++ = char((value & 0x7f) | 0x80);
149  value >>= 7;
150  ++n;
151  }
152  *data++ = char(value);
153 
154  return n;
155 }
156 
160 inline uint32_t encode_zigzag32(int32_t value) noexcept {
161  return (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
162 }
163 
167 inline uint64_t encode_zigzag64(int64_t value) noexcept {
168  return (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
169 }
170 
174 inline int32_t decode_zigzag32(uint32_t value) noexcept {
175  return int32_t(value >> 1) ^ -int32_t(value & 1);
176 }
177 
181 inline int64_t decode_zigzag64(uint64_t value) noexcept {
182  return int64_t(value >> 1) ^ -int64_t(value & 1);
183 }
184 
185 } // end namespace protozero
186 
187 #endif // PROTOZERO_VARINT_HPP
uint64_t encode_zigzag64(int64_t value) noexcept
Definition: varint.hpp:167
constexpr const int8_t max_varint_length
Definition: varint.hpp:28
int write_varint(T data, uint64_t value)
Definition: varint.hpp:144
void skip_varint(const char **data, const char *end)
Definition: varint.hpp:112
uint32_t encode_zigzag32(int32_t value) noexcept
Definition: varint.hpp:160
Contains the exceptions used in the protozero library.
Definition: exception.hpp:39
Definition: exception.hpp:61
uint64_t decode_varint(const char **data, const char *end)
Definition: varint.hpp:89
int64_t decode_zigzag64(uint64_t value) noexcept
Definition: varint.hpp:181
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:24
int32_t decode_zigzag32(uint32_t value) noexcept
Definition: varint.hpp:174