9 void buffer_init(Buffer
*buf
) {
10 memset(buf
, 0, sizeof *buf
);
13 bool buffer_grow(Buffer
*buf
, size_t size
) {
16 if (buf
->size
< size
) {
19 char *data
= realloc(buf
->data
, size
);
28 void buffer_truncate(Buffer
*buf
) {
32 void buffer_release(Buffer
*buf
) {
39 bool buffer_put(Buffer
*buf
, const void *data
, size_t len
) {
40 if (!buffer_grow(buf
, len
))
42 memmove(buf
->data
, data
, len
);
47 bool buffer_put0(Buffer
*buf
, const char *data
) {
48 return buffer_put(buf
, data
, strlen(data
)+1);
51 bool buffer_insert(Buffer
*buf
, size_t pos
, const void *data
, size_t len
) {
54 if (!buffer_grow(buf
, buf
->len
+ len
))
56 memmove(buf
->data
+ pos
+ len
, buf
->data
+ pos
, buf
->len
- pos
);
57 memcpy(buf
->data
+ pos
, data
, len
);
62 bool buffer_insert0(Buffer
*buf
, size_t pos
, const char *data
) {
64 return buffer_prepend0(buf
, data
);
66 return buffer_append0(buf
, data
);
67 return buffer_insert(buf
, pos
, data
, strlen(data
));
70 bool buffer_append(Buffer
*buf
, const void *data
, size_t len
) {
71 return buffer_insert(buf
, buf
->len
, data
, len
);
74 bool buffer_append0(Buffer
*buf
, const char *data
) {
75 if (buf
->len
> 0 && buf
->data
[buf
->len
-1] == '\0')
77 return buffer_append(buf
, data
, strlen(data
)) && buffer_append(buf
, "\0", 1);
80 bool buffer_prepend(Buffer
*buf
, const void *data
, size_t len
) {
81 return buffer_insert(buf
, 0, data
, len
);
84 bool buffer_prepend0(Buffer
*buf
, const char *data
) {
85 return buffer_prepend(buf
, data
, strlen(data
) + (buf
->len
== 0));