Furrent
Functions
util.cpp File Reference
#include "download/util.hpp"
#include <limits>
#include <stdexcept>
Include dependency graph for util.cpp:

Functions

std::array< uint8_t, 4 > encode_integer (int64_t n)
 
int64_t decode_integer (const std::array< uint8_t, 4 > &buf)
 

Function Documentation

◆ decode_integer()

int64_t decode_integer ( const std::array< uint8_t, 4 > &  buf)

Decodes an integer from a byte array. We return an int64_t as output because that's the integer type used throughout Furrent but the Torrent protocol works with 32bits unsigned integers so that's what will be decoded. Because any uint32_t can fit inside an int64_t, it's a safe cast to do.

◆ encode_integer()

std::array<uint8_t, 4> encode_integer ( int64_t  n)

Takes an integer and encodes to a byte array. We take int64_t as input because that's the integer type used throughout Furrent but the Torrent protocol works with 32bits unsigned integer so we expect n to be greater or equal than 0 and to fit in 32bits.