lokinet/llarp/util/buffer.cpp

124 lines
2.4 KiB
C++
Raw Normal View History

2019-02-02 23:12:42 +00:00
#include <util/buffer.hpp>
#include <util/endian.hpp>
2018-04-04 15:19:11 +00:00
#include <stdarg.h>
#include <stdio.h>
2018-01-19 16:51:27 +00:00
size_t
llarp_buffer_size_left(const llarp_buffer_t& buff)
{
2018-05-28 14:26:16 +00:00
size_t diff = buff.cur - buff.base;
if(diff > buff.sz)
2018-05-10 23:32:46 +00:00
return 0;
else
2018-05-28 14:26:16 +00:00
return buff.sz - diff;
2018-05-10 23:32:46 +00:00
}
bool
llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...)
{
2018-04-05 14:43:16 +00:00
int written;
2018-05-28 14:26:16 +00:00
ssize_t sz = llarp_buffer_size_left(*buff);
2018-04-05 14:43:16 +00:00
va_list args;
va_start(args, fmt);
written = vsnprintf((char*)buff->cur, sz, fmt, args);
2018-04-05 14:43:16 +00:00
va_end(args);
if(written <= 0)
return false;
if(sz < written)
return false;
2018-05-20 17:45:47 +00:00
buff->cur += written;
2018-04-05 14:43:16 +00:00
return true;
}
2018-04-08 12:18:16 +00:00
bool
llarp_buffer_write(llarp_buffer_t* buff, const void* data, size_t sz)
{
2018-05-28 14:26:16 +00:00
size_t left = llarp_buffer_size_left(*buff);
if(left >= sz)
{
2018-05-10 23:32:46 +00:00
memcpy(buff->cur, data, sz);
buff->cur += sz;
return true;
}
return false;
}
size_t
llarp_buffer_read_until(llarp_buffer_t* buff, char delim, byte_t* result,
size_t resultsize)
2018-05-13 18:07:36 +00:00
{
size_t read = 0;
2019-01-31 07:53:43 +00:00
// do the bound check first, to avoid over running
while((buff->cur != buff->base + buff->sz) && *buff->cur != delim
&& resultsize)
2018-05-13 18:07:36 +00:00
{
*result = *buff->cur;
buff->cur++;
result++;
resultsize--;
read++;
2018-05-13 18:07:36 +00:00
}
2018-05-28 14:26:16 +00:00
if(llarp_buffer_size_left(*buff))
2018-05-13 18:07:36 +00:00
return read;
else
2018-05-13 18:07:36 +00:00
return 0;
}
bool
llarp_buffer_eq(const llarp_buffer_t& buf, const char* str)
2018-05-13 18:07:36 +00:00
{
2019-02-03 00:48:10 +00:00
ManagedBuffer copy{buf};
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)
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;
}
2018-08-01 22:09:12 +00:00
bool
llarp_buffer_put_uint16(llarp_buffer_t* buf, uint16_t i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint16_t))
return false;
htobe16buf(buf->cur, i);
buf->cur += sizeof(uint16_t);
return true;
}
bool
llarp_buffer_put_uint32(llarp_buffer_t* buf, uint32_t i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint32_t))
return false;
htobe32buf(buf->cur, i);
buf->cur += sizeof(uint32_t);
return true;
}
2018-09-04 12:55:20 +00:00
2018-08-01 22:09:12 +00:00
bool
llarp_buffer_read_uint16(llarp_buffer_t* buf, uint16_t* i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint16_t))
return false;
*i = bufbe16toh(buf->cur);
buf->cur += sizeof(uint16_t);
return true;
}
2018-09-04 12:55:20 +00:00
bool
llarp_buffer_read_uint32(llarp_buffer_t* buf, uint32_t* i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint32_t))
return false;
*i = bufbe32toh(buf->cur);
buf->cur += sizeof(uint32_t);
return true;
}