From 4750be2da326297830691c54adbab0a5dea14802 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Fri, 23 Feb 2018 12:54:43 +0200 Subject: wip --- src/util/membuf.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/util/membuf.c (limited to 'src/util/membuf.c') diff --git a/src/util/membuf.c b/src/util/membuf.c new file mode 100644 index 0000000..0602679 --- /dev/null +++ b/src/util/membuf.c @@ -0,0 +1,31 @@ +#include "membuf.h" + +#include +#include +#include +#include + +static void +membuf_bounds_check(const struct membuf *buf, const size_t nmemb) +{ + assert(buf); + + if (buf->mem.len < nmemb || buf->written > buf->mem.len - nmemb) + errx(EXIT_FAILURE, "%s: %zu bytes exceeds the maximum storage size of %zu bytes", __func__, buf->written + nmemb, buf->mem.len); +} + +void +membuf_terminate(struct membuf *buf, const void *data, const size_t data_sz) +{ + assert(data || !data_sz); + membuf_bounds_check(buf, data_sz); + memcpy((char*)buf->mem.data + buf->written, data, data_sz); +} + +void +membuf_append(struct membuf *buf, const void *data, const size_t data_sz) +{ + membuf_terminate(buf, data, data_sz); + buf->written += data_sz; + assert(buf->written <= buf->mem.len); +} -- cgit v1.2.3