2019-02-02 23:12:42 +00:00
|
|
|
#include <util/buffer.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/endian.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-04-04 15:19:11 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
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;
|
|
|
|
if(diff > sz)
|
|
|
|
{
|
2018-05-10 23:32:46 +00:00
|
|
|
return 0;
|
2019-02-17 12:13:34 +00:00
|
|
|
}
|
2018-05-10 23:32:46 +00:00
|
|
|
else
|
2019-02-17 12:13:34 +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-19 18:24:33 +00:00
|
|
|
size_t sz = size_left();
|
2018-04-05 14:43:16 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2019-02-17 12:13:34 +00:00
|
|
|
written = vsnprintf(reinterpret_cast< char* >(cur), sz, fmt, args);
|
2018-04-05 14:43:16 +00:00
|
|
|
va_end(args);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(written <= 0)
|
|
|
|
return false;
|
|
|
|
if(sz < written)
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
if(size_left() < sizeof(uint16_t))
|
|
|
|
return false;
|
|
|
|
htobe16buf(cur, i);
|
|
|
|
cur += sizeof(uint16_t);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:17:29 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_t::put_uint64(uint64_t i)
|
|
|
|
{
|
|
|
|
if(size_left() < sizeof(uint64_t))
|
|
|
|
return false;
|
|
|
|
htobe64buf(cur, i);
|
|
|
|
cur += sizeof(uint64_t);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_t::put_uint32(uint32_t i)
|
|
|
|
{
|
|
|
|
if(size_left() < sizeof(uint32_t))
|
|
|
|
return false;
|
|
|
|
htobe32buf(cur, i);
|
|
|
|
cur += sizeof(uint32_t);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_buffer_t::read_uint16(uint16_t& i)
|
|
|
|
{
|
|
|
|
if(size_left() < sizeof(uint16_t))
|
|
|
|
return false;
|
|
|
|
i = bufbe16toh(cur);
|
|
|
|
cur += sizeof(uint16_t);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_buffer_t::read_uint32(uint32_t& i)
|
|
|
|
{
|
|
|
|
if(size_left() < sizeof(uint32_t))
|
|
|
|
return false;
|
|
|
|
i = bufbe32toh(cur);
|
|
|
|
cur += sizeof(uint32_t);
|
|
|
|
return true;
|
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)
|
|
|
|
{
|
|
|
|
if(size_left() < sizeof(uint64_t))
|
|
|
|
return false;
|
|
|
|
i = bufbe64toh(cur);
|
|
|
|
cur += sizeof(uint64_t);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
size_t
|
2019-02-17 12:13:34 +00:00
|
|
|
llarp_buffer_t::read_until(char delim, byte_t* result, size_t resultsize)
|
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
|
2019-02-17 12:13:34 +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
|
|
|
}
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
if(size_left())
|
2018-05-13 18:07:36 +00:00
|
|
|
return read;
|
2018-05-22 15:54:19 +00:00
|
|
|
else
|
2018-05-13 18:07:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
2019-02-17 12:13:34 +00:00
|
|
|
operator==(const llarp_buffer_t& buff, const char* str)
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
ManagedBuffer copy{buff};
|
2019-02-02 23:12:42 +00:00
|
|
|
while(*str
|
|
|
|
&& copy.underlying.cur != (copy.underlying.base + copy.underlying.sz))
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
2019-02-02 23:12:42 +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;
|
|
|
|
}
|