Electroneum
Loading...
Searching...
No Matches
varint.h
Go to the documentation of this file.
1// Copyrights(c) 2017-2021, The Electroneum Project
2// Copyrights(c) 2014-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31
32#pragma once
33
34#include <limits>
35#include <type_traits>
36#include <utility>
37#include <sstream>
38#include <string>
53
54namespace tools {
55
58 enum {
59 /* \brief Represents the overflow error */
61 /* \brief Represents a non conical represnetation */
63 };
64
67 template<typename OutputIt, typename T>
68 /* Requires T to be both an integral type and unsigned, should be a compile error if it is not */
69 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value, void>::type
70 write_varint(OutputIt &&dest, T i) {
71 /* Make sure that there is one after this */
72 while (i >= 0x80) {
73 *dest = (static_cast<char>(i) & 0x7f) | 0x80;
74 ++dest;
75 i >>= 7; /* I should be in multiples of 7, this should just get the next part */
76 }
77 /* writes the last one to dest */
78 *dest = static_cast<char>(i);
79 dest++; /* Seems kinda pointless... */
80 }
81
84 template<typename T>
85 std::string get_varint_data(const T& v)
86 {
87 std::stringstream ss;
88 write_varint(std::ostreambuf_iterator<char>(ss), v);
89 return ss.str();
90 }
91
93 template<int bits, typename InputIt, typename T>
94 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value && 0 <= bits && bits <= std::numeric_limits<T>::digits, int>::type
95 read_varint(InputIt &&first, InputIt &&last, T &write) {
96 int read = 0;
97 write = 0;
98 for (int shift = 0;; shift += 7) {
99 if (first == last) {
100 return read;
101 }
102 unsigned char byte = *first;
103 ++first;
104 ++read;
105 if (shift + 7 >= bits && byte >= 1 << (bits - shift)) {
106 return EVARINT_OVERFLOW;
107 }
108 if (byte == 0 && shift != 0) {
109 return EVARINT_REPRESENT;
110 }
111
112 write |= static_cast<T>(byte & 0x7f) << shift; /* Does the actually placing into write, stripping the first bit */
113
114 /* If there is no next */
115 if ((byte & 0x80) == 0) {
116 break;
117 }
118 }
119 return read;
120 }
121
125 template<typename InputIt, typename T>
126 int read_varint(InputIt &&first, InputIt &&last, T &i) {
127 return read_varint<std::numeric_limits<T>::digits>(std::forward<InputIt>(first), std::forward<InputIt>(last), i);
128 }
129}
Various Tools.
Definition tools.cpp:31
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&0<=bits &&bits<=std::numeric_limits< T >::digits, int >::type read_varint(InputIt &&first, InputIt &&last, T &write)
reads in the varint that is pointed to by InputIt into write
Definition varint.h:95
std::string get_varint_data(const T &v)
Returns the string that represents the varint.
Definition varint.h:85
@ EVARINT_REPRESENT
Definition varint.h:62
@ EVARINT_OVERFLOW
Definition varint.h:60
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value, void >::type write_varint(OutputIt &&dest, T i)
writes a varint to a stream.
Definition varint.h:70
CXA_THROW_INFO_T void(* dest)(void *))
#define T(x)