2021-03-09 22:24:35 +00:00
|
|
|
#include "buffer.hpp"
|
2022-04-28 15:09:51 +00:00
|
|
|
#include <oxenc/endian.h>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
size_t
|
2019-02-17 12:13:34 +00:00
|
|
|
llarp_buffer_t::size_left() const
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
size_t diff = cur - base;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (diff > sz)
|
2019-02-17 12:13:34 +00:00
|
|
|
{
|
2018-05-10 23:32:46 +00:00
|
|
|
return 0;
|
2019-02-17 12:13:34 +00:00
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
return sz - diff;
|
2018-05-10 23:32:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
2019-02-17 12:13:34 +00:00
|
|
|
llarp_buffer_t::writef(const char* fmt, ...)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-04-05 14:43:16 +00:00
|
|
|
int written;
|
2019-04-25 23:21:19 +00:00
|
|
|
size_t toWrite = size_left();
|
2018-04-05 14:43:16 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2020-04-07 18:38:56 +00:00
|
|
|
written = vsnprintf(reinterpret_cast<char*>(cur), toWrite, fmt, args);
|
2018-04-05 14:43:16 +00:00
|
|
|
va_end(args);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (written <= 0)
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (toWrite < static_cast<size_t>(written))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
cur += written;
|
2018-04-05 14:43:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2022-04-28 15:09:51 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
template <typename UInt>
|
|
|
|
bool
|
|
|
|
put(llarp_buffer_t& buf, UInt i)
|
|
|
|
{
|
|
|
|
if (buf.size_left() < sizeof(UInt))
|
|
|
|
return false;
|
|
|
|
oxenc::write_host_as_big(i, buf.cur);
|
|
|
|
buf.cur += sizeof(UInt);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename UInt>
|
|
|
|
bool
|
|
|
|
read(llarp_buffer_t& buf, UInt& i)
|
|
|
|
{
|
|
|
|
if (buf.size_left() < sizeof(UInt))
|
|
|
|
return false;
|
|
|
|
i = oxenc::load_big_to_host<UInt>(buf.cur);
|
|
|
|
buf.cur += sizeof(UInt);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
2019-02-17 12:13:34 +00:00
|
|
|
llarp_buffer_t::put_uint16(uint16_t i)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2022-04-28 15:09:51 +00:00
|
|
|
return put(*this, i);
|
2019-02-17 12:13:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:17:29 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_t::put_uint64(uint64_t i)
|
|
|
|
{
|
2022-04-28 15:09:51 +00:00
|
|
|
return put(*this, i);
|
2019-03-07 15:17:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_t::put_uint32(uint32_t i)
|
|
|
|
{
|
2022-04-28 15:09:51 +00:00
|
|
|
return put(*this, i);
|
2019-02-17 12:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_buffer_t::read_uint16(uint16_t& i)
|
|
|
|
{
|
2022-04-28 15:09:51 +00:00
|
|
|
return read(*this, i);
|
2019-02-17 12:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_buffer_t::read_uint32(uint32_t& i)
|
|
|
|
{
|
2022-04-28 15:09:51 +00:00
|
|
|
return read(*this, i);
|
2018-05-10 23:32:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:17:29 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_t::read_uint64(uint64_t& i)
|
|
|
|
{
|
2022-04-28 15:09:51 +00:00
|
|
|
return read(*this, i);
|
2019-03-07 15:17:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
size_t
|
2021-02-26 03:40:42 +00:00
|
|
|
llarp_buffer_t::read_until(char c_delim, byte_t* result, size_t resultsize)
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
2021-02-26 03:40:42 +00:00
|
|
|
const auto delim = static_cast<byte_t>(c_delim);
|
2018-05-13 18:07:36 +00:00
|
|
|
size_t read = 0;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2019-01-31 07:53:43 +00:00
|
|
|
// do the bound check first, to avoid over running
|
2020-04-07 18:38:56 +00:00
|
|
|
while ((cur != base + sz) && *cur != delim && resultsize)
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
*result = *cur;
|
|
|
|
cur++;
|
2018-05-22 15:54:19 +00:00
|
|
|
result++;
|
|
|
|
resultsize--;
|
|
|
|
read++;
|
2018-05-13 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (size_left())
|
2018-05-13 18:07:36 +00:00
|
|
|
return read;
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-05-13 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 10:51:44 +00:00
|
|
|
std::vector<byte_t>
|
|
|
|
llarp_buffer_t::copy() const
|
|
|
|
{
|
|
|
|
std::vector<byte_t> copy;
|
|
|
|
copy.resize(sz);
|
|
|
|
std::copy_n(base, sz, copy.data());
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
2021-02-26 03:40:42 +00:00
|
|
|
operator==(const llarp_buffer_t& buff, const char* c_str)
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
2021-02-26 03:40:42 +00:00
|
|
|
const auto* str = reinterpret_cast<const byte_t*>(c_str);
|
2019-02-17 12:13:34 +00:00
|
|
|
ManagedBuffer copy{buff};
|
2020-04-07 18:38:56 +00:00
|
|
|
while (*str && copy.underlying.cur != (copy.underlying.base + copy.underlying.sz))
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (*copy.underlying.cur != *str)
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2019-02-02 23:12:42 +00:00
|
|
|
copy.underlying.cur++;
|
2018-05-13 18:07:36 +00:00
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return *str == 0;
|
|
|
|
}
|
2021-03-02 02:06:20 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
OwnedBuffer
|
|
|
|
OwnedBuffer::copy_from(const llarp_buffer_t& b)
|
|
|
|
{
|
|
|
|
auto buf = std::make_unique<byte_t[]>(b.sz);
|
|
|
|
std::copy(b.begin(), b.end(), buf.get());
|
|
|
|
return {std::move(buf), b.sz};
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnedBuffer
|
|
|
|
OwnedBuffer::copy_used(const llarp_buffer_t& b)
|
|
|
|
{
|
2021-03-07 15:59:48 +00:00
|
|
|
const size_t sz = b.cur - b.base;
|
|
|
|
auto buf = std::make_unique<byte_t[]>(sz);
|
2021-03-02 02:06:20 +00:00
|
|
|
std::copy(b.base, b.cur, buf.get());
|
2021-03-07 15:59:48 +00:00
|
|
|
return {std::move(buf), sz};
|
2021-03-02 02:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|