diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-24 12:53:05 +0200 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-24 12:53:05 +0200 |
| commit | abb4f60f8a5166dd446318e0fb4b2df0c4b9ab02 (patch) | |
| tree | 2957ed88c6f8d742a0442ba131efc3d1ed197bec /ot_buf.c | |
| parent | 1b97919d03ab55d4e0be05f4700b5b80ece4e5cc (diff) | |
Add tiny blob wrapper, holding ptr+size, optimized to grow and reset
Diffstat (limited to 'ot_buf.c')
| -rw-r--r-- | ot_buf.c | 41 |
1 files changed, 41 insertions, 0 deletions
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 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <stdlib.h> | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | /* Opentracker */ | ||
| 11 | #include "ot_buf.h" | ||
| 12 | |||
| 13 | int buf_cat(ot_buf *buf, const void *src, size_t len) { | ||
| 14 | char *data; | ||
| 15 | |||
| 16 | if (!len) | ||
| 17 | return 0; | ||
| 18 | |||
| 19 | if (buf->size + len < buf->size) | ||
| 20 | return -1; | ||
| 21 | |||
| 22 | if (!(data = realloc(buf->data, buf->size + len))) | ||
| 23 | return -1; | ||
| 24 | |||
| 25 | buf->data = data; | ||
| 26 | memcpy(data + buf->size, src, len); | ||
| 27 | buf->size += len; | ||
| 28 | |||
| 29 | return 0; | ||
| 30 | } | ||
| 31 | |||
| 32 | void buf_truncate(ot_buf *buf, size_t len) { | ||
| 33 | if (len < buf->size) | ||
| 34 | buf->size = len; | ||
| 35 | } | ||
| 36 | |||
| 37 | void buf_reset(ot_buf *buf) { | ||
| 38 | free(buf->data); | ||
| 39 | buf->data = NULL; | ||
| 40 | buf->size = 0; | ||
| 41 | } | ||
