2018-12-12 00:33:54 +00:00
|
|
|
#ifndef LLARP_BUFFER_HPP
|
|
|
|
#define LLARP_BUFFER_HPP
|
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
#include <util/common.hpp>
|
|
|
|
#include <util/mem.h>
|
|
|
|
#include <util/types.hpp>
|
2018-12-12 00:33:54 +00:00
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
#include <cassert>
|
2019-02-17 12:13:34 +00:00
|
|
|
#include <iterator>
|
2019-07-30 23:42:13 +00:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2019-02-02 23:12:42 +00:00
|
|
|
#include <utility>
|
2019-03-07 15:17:29 +00:00
|
|
|
#include <algorithm>
|
2019-02-02 23:12:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buffer.h
|
|
|
|
*
|
|
|
|
* generic memory buffer
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
llarp_buffer_t represents a region of memory that is ONLY
|
|
|
|
valid in the current scope.
|
|
|
|
|
|
|
|
make sure to follow the rules:
|
|
|
|
|
|
|
|
ALWAYS copy the contents of the buffer if that data is to be used outside the
|
|
|
|
current scope.
|
|
|
|
|
|
|
|
ALWAYS pass a llarp_buffer_t * if you plan on modifying the data associated
|
|
|
|
with the buffer
|
|
|
|
|
|
|
|
ALWAYS pass a llarp_buffer_t * if you plan on advancing the stream position
|
|
|
|
|
2019-02-03 01:44:09 +00:00
|
|
|
ALWAYS pass a const llarp_buffer_t & if you are doing a read only operation
|
|
|
|
that does not modify the buffer
|
2019-02-02 23:12:42 +00:00
|
|
|
|
2019-02-03 01:44:09 +00:00
|
|
|
ALWAYS pass a const llarp_buffer_t & if you don't want to advance the stream
|
|
|
|
position
|
2019-02-02 23:12:42 +00:00
|
|
|
|
|
|
|
ALWAYS bail out of the current operation if you run out of space in a buffer
|
|
|
|
|
|
|
|
ALWAYS assume the pointers in the buffer are stack allocated memory
|
|
|
|
(yes even if you know they are not)
|
|
|
|
|
|
|
|
NEVER malloc() the pointers in the buffer when using it
|
|
|
|
|
|
|
|
NEVER realloc() the pointers in the buffer when using it
|
|
|
|
|
|
|
|
NEVER free() the pointers in the buffer when using it
|
|
|
|
|
|
|
|
NEVER use llarp_buffer_t ** (double pointers)
|
|
|
|
|
|
|
|
NEVER use llarp_buffer_t ** (double pointers)
|
|
|
|
|
|
|
|
ABSOLUTELY NEVER USE DOUBLE POINTERS.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-02-03 00:48:10 +00:00
|
|
|
struct ManagedBuffer;
|
2019-02-02 23:12:42 +00:00
|
|
|
|
|
|
|
struct llarp_buffer_t
|
2018-12-12 00:33:54 +00:00
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
/// starting memory address
|
2020-04-07 18:38:56 +00:00
|
|
|
byte_t* base{nullptr};
|
2019-02-02 23:12:42 +00:00
|
|
|
/// memory address of stream position
|
2020-04-07 18:38:56 +00:00
|
|
|
byte_t* cur{nullptr};
|
2019-02-02 23:12:42 +00:00
|
|
|
/// max size of buffer
|
2019-07-30 23:42:13 +00:00
|
|
|
size_t sz{0};
|
2019-02-02 23:12:42 +00:00
|
|
|
|
|
|
|
byte_t operator[](size_t x)
|
|
|
|
{
|
|
|
|
return *(this->base + x);
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
llarp_buffer_t() = default;
|
2019-02-02 23:12:42 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_buffer_t(byte_t* b, byte_t* c, size_t s) : base(b), cur(c), sz(s)
|
2019-02-02 23:12:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_buffer_t(const ManagedBuffer&) = delete;
|
|
|
|
llarp_buffer_t(ManagedBuffer&&) = delete;
|
2019-02-02 23:12:42 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename T>
|
|
|
|
llarp_buffer_t(T* buf, size_t _sz) : base(reinterpret_cast<byte_t*>(buf)), cur(base), sz(_sz)
|
2018-12-12 00:33:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename T>
|
|
|
|
llarp_buffer_t(const T* buf, size_t _sz)
|
|
|
|
: base(reinterpret_cast<byte_t*>(const_cast<T*>(buf))), cur(base), sz(_sz)
|
2018-12-12 00:33:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** initialize llarp_buffer_t from container */
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename T>
|
|
|
|
llarp_buffer_t(T& t) : base(t.data()), cur(t.data()), sz(t.size())
|
2018-12-12 00:33:54 +00:00
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
// use data over the first element to "enforce" the container used has
|
|
|
|
// contiguous memory. (Note this isn't required by the standard, but a
|
|
|
|
// reasonable test on most standard library implementations).
|
2018-12-12 00:33:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename T>
|
|
|
|
llarp_buffer_t(const T& t) : llarp_buffer_t(t.data(), t.size())
|
2019-02-02 23:12:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-28 00:19:25 +00:00
|
|
|
// clang-format off
|
|
|
|
byte_t * begin() { return base; }
|
|
|
|
byte_t * begin() const { return base; }
|
|
|
|
byte_t * end() { return base + sz; }
|
|
|
|
byte_t * end() const { return base + sz; }
|
|
|
|
// clang-format on
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
size_t
|
|
|
|
size_left() const;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename OutputIt>
|
2019-03-07 15:17:29 +00:00
|
|
|
bool
|
|
|
|
read_into(OutputIt begin, OutputIt end);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename InputIt>
|
2019-02-17 12:13:34 +00:00
|
|
|
bool
|
|
|
|
write(InputIt begin, InputIt end);
|
|
|
|
|
2019-02-18 23:50:04 +00:00
|
|
|
#ifndef _WIN32
|
2019-02-17 12:13:34 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
writef(const char* fmt, ...) __attribute__((format(printf, 2, 3)));
|
2019-04-22 12:25:25 +00:00
|
|
|
|
2019-04-19 18:24:33 +00:00
|
|
|
#elif defined(__MINGW64__) || defined(__MINGW32__)
|
2019-02-18 23:58:12 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
writef(const char* fmt, ...) __attribute__((__format__(__MINGW_PRINTF_FORMAT, 2, 3)));
|
2019-04-19 18:24:33 +00:00
|
|
|
#else
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
writef(const char* fmt, ...);
|
2019-02-18 23:50:04 +00:00
|
|
|
#endif
|
2019-02-17 12:13:34 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
put_uint16(uint16_t i);
|
|
|
|
bool
|
|
|
|
put_uint32(uint32_t i);
|
|
|
|
|
2019-03-07 15:17:29 +00:00
|
|
|
bool
|
|
|
|
put_uint64(uint64_t i);
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
read_uint16(uint16_t& i);
|
2019-02-17 12:13:34 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
read_uint32(uint32_t& i);
|
2019-02-17 12:13:34 +00:00
|
|
|
|
2019-03-07 15:17:29 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
read_uint64(uint64_t& i);
|
2019-03-07 15:17:29 +00:00
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
size_t
|
2020-04-07 18:38:56 +00:00
|
|
|
read_until(char delim, byte_t* result, size_t resultlen);
|
2019-02-17 12:13:34 +00:00
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
private:
|
2019-02-03 00:48:10 +00:00
|
|
|
friend struct ManagedBuffer;
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_buffer_t(const llarp_buffer_t&) = default;
|
|
|
|
llarp_buffer_t(llarp_buffer_t&&) = default;
|
2019-02-02 23:12:42 +00:00
|
|
|
};
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
operator==(const llarp_buffer_t& buff, const char* data);
|
2019-02-17 12:13:34 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename OutputIt>
|
2019-03-07 15:17:29 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_t::read_into(OutputIt begin, OutputIt end)
|
|
|
|
{
|
|
|
|
auto dist = std::distance(begin, end);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (static_cast<decltype(dist)>(size_left()) >= dist)
|
2019-03-07 15:17:29 +00:00
|
|
|
{
|
|
|
|
std::copy_n(cur, dist, begin);
|
|
|
|
cur += dist;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename InputIt>
|
2019-02-17 12:13:34 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_t::write(InputIt begin, InputIt end)
|
|
|
|
{
|
|
|
|
auto dist = std::distance(begin, end);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (static_cast<decltype(dist)>(size_left()) >= dist)
|
2019-02-17 12:13:34 +00:00
|
|
|
{
|
|
|
|
cur = std::copy(begin, end, cur);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-03 01:44:09 +00:00
|
|
|
/**
|
|
|
|
Provide a copyable/moveable wrapper around `llarp_buffer_t`.
|
|
|
|
*/
|
2019-02-03 00:48:10 +00:00
|
|
|
struct ManagedBuffer
|
2019-02-02 23:12:42 +00:00
|
|
|
{
|
|
|
|
llarp_buffer_t underlying;
|
|
|
|
|
2019-02-03 01:44:09 +00:00
|
|
|
ManagedBuffer() = delete;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
explicit ManagedBuffer(const llarp_buffer_t& b) : underlying(b)
|
2018-12-12 00:33:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
ManagedBuffer(ManagedBuffer&&) = default;
|
|
|
|
ManagedBuffer(const ManagedBuffer&) = default;
|
2019-02-03 01:44:09 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
operator const llarp_buffer_t&() const
|
2019-02-03 01:44:09 +00:00
|
|
|
{
|
|
|
|
return underlying;
|
|
|
|
}
|
2019-02-02 23:12:42 +00:00
|
|
|
};
|
|
|
|
|
2018-12-12 00:33:54 +00:00
|
|
|
#endif
|