#include "buffer.h"
#include "xalloc.h"
+void buffer_compact(buffer_t *buffer) {
+ if(buffer->offset / 7 > buffer->len / 8) {
+ memmove(buffer->data, buffer->data + buffer->offset, buffer->len - buffer->offset);
+ buffer->len -= buffer->offset;
+ buffer->offset = 0;
+ }
+}
+
// Make sure we can add size bytes to the buffer, and return a pointer to the start of those bytes.
char *buffer_prepare(buffer_t *buffer, int size) {
}
}
+ char *start = buffer->data + buffer->len;
+
buffer->len += size;
- return buffer->data + buffer->offset;
+
+ return start;
}
// Copy data into the buffer.
if(buffer->offset >= buffer->len) {
buffer->offset = 0;
buffer->len = 0;
- } else {
- buffer->offset += size;
}
return start;
if(!newline)
return NULL;
- int len = newline + 1 - buffer->data + buffer->offset;
+ int len = newline + 1 - (buffer->data + buffer->offset);
*newline = 0;
return buffer_consume(buffer, len);
}
int offset;
} buffer_t;
+extern void buffer_compact(buffer_t *buffer);
extern char *buffer_prepare(buffer_t *buffer, int size);
extern char *buffer_add(buffer_t *buffer, const char *data, int size);
extern char *buffer_readline(buffer_t *buffer);
/* Add our data to buffer */
if(c->status.encryptout) {
- char outbuf[length];
size_t outlen = length;
- if(!cipher_encrypt(&c->outcipher, outbuf, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
+ if(!cipher_encrypt(&c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
logger(LOG_ERR, "Error while encrypting metadata to %s (%s)",
c->name, c->hostname);
return false;
}
} while(inlen);
+ buffer_compact(&c->inbuf);
+
return true;
}