From abb4f60f8a5166dd446318e0fb4b2df0c4b9ab02 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 12:53:05 +0200 Subject: Add tiny blob wrapper, holding ptr+size, optimized to grow and reset --- ot_buf.c | 41 +++++++++++++++++++++++++++++++++++++++++ ot_buf.h | 27 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 ot_buf.c create mode 100644 ot_buf.h diff --git a/ot_buf.c b/ot_buf.c new file mode 100644 index 0000000..eb00edf --- /dev/null +++ b/ot_buf.c @@ -0,0 +1,41 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. + + $id$ */ + +/* System */ +#include +#include + +/* Opentracker */ +#include "ot_buf.h" + +int buf_cat(ot_buf *buf, const void *src, size_t len) { + char *data; + + if (!len) + return 0; + + if (buf->size + len < buf->size) + return -1; + + if (!(data = realloc(buf->data, buf->size + len))) + return -1; + + buf->data = data; + memcpy(data + buf->size, src, len); + buf->size += len; + + return 0; +} + +void buf_truncate(ot_buf *buf, size_t len) { + if (len < buf->size) + buf->size = len; +} + +void buf_reset(ot_buf *buf) { + free(buf->data); + buf->data = NULL; + buf->size = 0; +} diff --git a/ot_buf.h b/ot_buf.h new file mode 100644 index 0000000..219a069 --- /dev/null +++ b/ot_buf.h @@ -0,0 +1,27 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. + + $id$ */ + +#ifndef OT_BUF_H__ +#define OT_BUF_H__ + +#include + +/* A growable byte buffer, holding an HTTP request that did not arrive in a + single packet. Requests that arrive whole are served straight from the + worker's input buffer and never come near this. */ +typedef struct { + char *data; + size_t size; +} ot_buf; + +/* Append len bytes from src. Returns 0, or -1 if the memory could not be + had, in which case the buffer keeps whatever it already held. */ +int buf_cat(ot_buf *buf, const void *src, size_t len); + +void buf_truncate(ot_buf *buf, size_t len); + +void buf_reset(ot_buf *buf); + +#endif -- cgit v1.3