summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ot_buf.c41
-rw-r--r--ot_buf.h27
2 files changed, 68 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
13int 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
32void buf_truncate(ot_buf *buf, size_t len) {
33 if (len < buf->size)
34 buf->size = len;
35}
36
37void buf_reset(ot_buf *buf) {
38 free(buf->data);
39 buf->data = NULL;
40 buf->size = 0;
41}
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 @@
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#ifndef OT_BUF_H__
7#define OT_BUF_H__
8
9#include <stddef.h>
10
11/* A growable byte buffer, holding an HTTP request that did not arrive in a
12 single packet. Requests that arrive whole are served straight from the
13 worker's input buffer and never come near this. */
14typedef struct {
15 char *data;
16 size_t size;
17} ot_buf;
18
19/* Append len bytes from src. Returns 0, or -1 if the memory could not be
20 had, in which case the buffer keeps whatever it already held. */
21int buf_cat(ot_buf *buf, const void *src, size_t len);
22
23void buf_truncate(ot_buf *buf, size_t len);
24
25void buf_reset(ot_buf *buf);
26
27#endif