2018-01-25 16:24:33 +00:00
|
|
|
#include <llarp/buffer.h>
|
2018-04-04 15:19:11 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2018-01-19 16:51:27 +00:00
|
|
|
|
|
|
|
extern "C" {
|
2018-05-10 23:32:46 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
size_t
|
|
|
|
llarp_buffer_size_left(llarp_buffer_t* buff)
|
|
|
|
{
|
2018-05-10 23:32:46 +00:00
|
|
|
size_t diff = buff->cur - buff->base;
|
2018-05-22 15:54:19 +00:00
|
|
|
if(diff > buff->sz)
|
2018-05-10 23:32:46 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return buff->sz - diff;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +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-22 15:54:19 +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);
|
2018-05-22 15:54:19 +00:00
|
|
|
written = vsnprintf((char*)buff->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;
|
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
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_write(llarp_buffer_t* buff, const void* data, size_t sz)
|
|
|
|
{
|
2018-05-10 23:32:46 +00:00
|
|
|
size_t left = llarp_buffer_size_left(buff);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(left >= sz)
|
|
|
|
{
|
2018-05-10 23:32:46 +00:00
|
|
|
memcpy(buff->cur, data, sz);
|
|
|
|
buff->cur += sz;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_readfile(llarp_buffer_t* buff, FILE* f, llarp_alloc* mem)
|
|
|
|
{
|
2018-04-30 16:14:20 +00:00
|
|
|
ssize_t len;
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
len = ftell(f);
|
|
|
|
rewind(f);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(len > 0)
|
|
|
|
{
|
|
|
|
buff->base = static_cast< uint8_t* >(mem->alloc(mem, len, 8));
|
|
|
|
buff->cur = buff->base;
|
|
|
|
buff->sz = len;
|
2018-05-16 15:53:28 +00:00
|
|
|
ssize_t sz = fread(buff->base, len, 1, f);
|
2018-04-08 12:18:16 +00:00
|
|
|
rewind(f);
|
2018-04-30 16:14:20 +00:00
|
|
|
return sz == len;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-13 18:07:36 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
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;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
while(*buff->cur != delim && resultsize
|
|
|
|
&& (buff->cur != buff->base + buff->sz))
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
|
|
|
*result = *buff->cur;
|
2018-05-22 15:54:19 +00:00
|
|
|
buff->cur++;
|
|
|
|
result++;
|
|
|
|
resultsize--;
|
|
|
|
read++;
|
2018-05-13 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(llarp_buffer_size_left(buff))
|
|
|
|
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
|
|
|
|
llarp_buffer_eq(llarp_buffer_t buf, const char* str)
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
|
|
|
while(*str && buf.cur != (buf.base + buf.sz))
|
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
if(*buf.cur != *str)
|
|
|
|
return false;
|
|
|
|
buf.cur++;
|
2018-05-13 18:07:36 +00:00
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return *str == 0;
|
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
}
|