Remove unnecessary white line
[folly.git] / folly / Varint.h
index 4664cb95d4f5c4e2aa33efdb95f3398199ff1b53..c28d7f724fa2ddace0307b5b71d3dd8accbd1a9c 100644 (file)
 #pragma once
 
 #include <type_traits>
+
 #include <folly/Conv.h>
+#include <folly/Expected.h>
+#include <folly/Likely.h>
 #include <folly/Range.h>
 
 namespace folly {
@@ -55,10 +58,24 @@ size_t encodeVarint(uint64_t val, uint8_t* buf);
 
 /**
  * Decode a value from a given buffer, advances data past the returned value.
+ * Throws on error.
  */
 template <class T>
 uint64_t decodeVarint(Range<T*>& data);
 
+enum class DecodeVarintError {
+  TooManyBytes = 0,
+  TooFewBytes = 1,
+};
+
+/**
+ * A variant of decodeVarint() that does not throw on error. Useful in contexts
+ * where only part of a serialized varint may be attempted to be decoded, e.g.,
+ * when a serialized varint arrives on the boundary of a network packet.
+ */
+template <class T>
+Expected<uint64_t, DecodeVarintError> tryDecodeVarint(Range<T*>& data);
+
 /**
  * ZigZag encoding that maps signed integers with a small absolute value
  * to unsigned integers with a small (positive) values. Without this,
@@ -93,6 +110,18 @@ inline size_t encodeVarint(uint64_t val, uint8_t* buf) {
 
 template <class T>
 inline uint64_t decodeVarint(Range<T*>& data) {
+  auto expected = tryDecodeVarint(data);
+  if (!expected) {
+    throw std::invalid_argument(
+        expected.error() == DecodeVarintError::TooManyBytes
+            ? "Invalid varint value: too many bytes."
+            : "Invalid varint value: too few bytes.");
+  }
+  return *expected;
+}
+
+template <class T>
+inline Expected<uint64_t, DecodeVarintError> tryDecodeVarint(Range<T*>& data) {
   static_assert(
       std::is_same<typename std::remove_cv<T>::type, char>::value ||
           std::is_same<typename std::remove_cv<T>::type, unsigned char>::value,
@@ -107,17 +136,57 @@ inline uint64_t decodeVarint(Range<T*>& data) {
   if (LIKELY(size_t(end - begin) >= kMaxVarintLength64)) {  // fast path
     int64_t b;
     do {
-      b = *p++; val  = (b & 0x7f)      ; if (b >= 0) break;
-      b = *p++; val |= (b & 0x7f) <<  7; if (b >= 0) break;
-      b = *p++; val |= (b & 0x7f) << 14; if (b >= 0) break;
-      b = *p++; val |= (b & 0x7f) << 21; if (b >= 0) break;
-      b = *p++; val |= (b & 0x7f) << 28; if (b >= 0) break;
-      b = *p++; val |= (b & 0x7f) << 35; if (b >= 0) break;
-      b = *p++; val |= (b & 0x7f) << 42; if (b >= 0) break;
-      b = *p++; val |= (b & 0x7f) << 49; if (b >= 0) break;
-      b = *p++; val |= (b & 0x7f) << 56; if (b >= 0) break;
-      b = *p++; val |= (b & 0x7f) << 63; if (b >= 0) break;
-      throw std::invalid_argument("Invalid varint value. Too big.");
+      b = *p++;
+      val = (b & 0x7f);
+      if (b >= 0) {
+        break;
+      }
+      b = *p++;
+      val |= (b & 0x7f) << 7;
+      if (b >= 0) {
+        break;
+      }
+      b = *p++;
+      val |= (b & 0x7f) << 14;
+      if (b >= 0) {
+        break;
+      }
+      b = *p++;
+      val |= (b & 0x7f) << 21;
+      if (b >= 0) {
+        break;
+      }
+      b = *p++;
+      val |= (b & 0x7f) << 28;
+      if (b >= 0) {
+        break;
+      }
+      b = *p++;
+      val |= (b & 0x7f) << 35;
+      if (b >= 0) {
+        break;
+      }
+      b = *p++;
+      val |= (b & 0x7f) << 42;
+      if (b >= 0) {
+        break;
+      }
+      b = *p++;
+      val |= (b & 0x7f) << 49;
+      if (b >= 0) {
+        break;
+      }
+      b = *p++;
+      val |= (b & 0x7f) << 56;
+      if (b >= 0) {
+        break;
+      }
+      b = *p++;
+      val |= (b & 0x01) << 63;
+      if (b >= 0) {
+        break;
+      }
+      return makeUnexpected(DecodeVarintError::TooManyBytes);
     } while (false);
   } else {
     int shift = 0;
@@ -126,9 +195,7 @@ inline uint64_t decodeVarint(Range<T*>& data) {
       shift += 7;
     }
     if (p == end) {
-      throw std::invalid_argument("Invalid varint value. Too small: " +
-                                  folly::to<std::string>(end - begin) +
-                                  " bytes");
+      return makeUnexpected(DecodeVarintError::TooFewBytes);
     }
     val |= static_cast<uint64_t>(*p++) << shift;
   }
@@ -137,4 +204,4 @@ inline uint64_t decodeVarint(Range<T*>& data) {
   return val;
 }
 
-}  // namespaces
+} // namespace folly