Monero
Loading...
Searching...
No Matches
varint.h File Reference

provides the implementation of varint's More...

#include <limits>
#include <type_traits>
#include <utility>
#include <sstream>
#include <string>
Include dependency graph for varint.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  tools
 Various Tools.

Enumerations

enum  { tools::EVARINT_OVERFLOW = -1 , tools::EVARINT_REPRESENT = -2 }
 Error codes for varint. More...

Functions

template<typename OutputIt, typename T>
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value, void >::type tools::write_varint (OutputIt &&dest, T i)
 writes a varint to a stream.
template<typename T>
std::string tools::get_varint_data (const T &v)
 Returns the string that represents the varint.
template<int bits, typename InputIt, typename T>
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&0<=bits &&bits<=std::numeric_limits< T >::digits, int >::type tools::read_varint (InputIt &&first, InputIt &&last, T &write)
 reads in the varint that is pointed to by InputIt into write
template<typename InputIt, typename T>
int tools::read_varint (InputIt &&first, InputIt &&last, T &i)
 Wrapper around the other read_varint, Sets template parameters for you.

Detailed Description

provides the implementation of varint's

The representation of varints is rather odd. The first bit of each octet is significant, it represents wheter there is another part waiting to be read. For example 0x8002 would return 0x200, even though 0x02 does not have its msb set. The actual way they are read is as follows: Strip the msb of each byte, then from left to right, read in what remains, placing it in reverse, into the buffer. Thus, the following bit stream: 0xff02 would return 0x027f. 0xff turns into 0x7f, is placed on the beginning of the buffer, then 0x02 is unchanged, since its msb is not set, and placed at the end of the buffer.