2018-01-25 16:24:33 +00:00
|
|
|
#include <llarp/buffer.h>
|
2018-02-01 17:06:49 +00:00
|
|
|
#include <cstring>
|
2018-01-19 16:51:27 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2018-01-29 14:27:24 +00:00
|
|
|
size_t llarp_buffer_size_left(llarp_buffer_t *buff) {
|
|
|
|
std::ptrdiff_t diff = buff->cur - buff->base;
|
|
|
|
if (diff < 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (diff > buff->sz)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return buff->sz - diff;
|
|
|
|
}
|
2018-02-01 17:06:49 +00:00
|
|
|
|
2018-02-01 17:07:01 +00:00
|
|
|
bool llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz) {
|
2018-02-01 17:06:49 +00:00
|
|
|
size_t left = llarp_buffer_size_left(buff);
|
2018-02-01 17:07:01 +00:00
|
|
|
if (sz > left) {
|
2018-02-01 17:06:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::memcpy(buff->cur, data, sz);
|
|
|
|
buff->cur += sz;
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
}
|