#include "k5-platform.h"
#include "k5-buf.h"
#include <assert.h>
static inline char *
endptr(struct k5buf *buf)
{
return (char *)buf->data + buf->len;
}
static inline void
set_error(struct k5buf *buf)
{
buf->buftype = K5BUF_ERROR;
buf->data = NULL;
buf->space = buf->len = 0;
}
static int
ensure_space(struct k5buf *buf, size_t len)
{
size_t new_space;
char *new_data;
if (buf->buftype == K5BUF_ERROR)
return 0;
if (buf->space - buf->len >= len)
return 1;
if (buf->buftype == K5BUF_FIXED)
goto error_exit;
assert(buf->buftype == K5BUF_DYNAMIC || buf->buftype == K5BUF_DYNAMIC_ZAP);
new_space = buf->space * 2;
while (new_space - buf->len < len) {
if (new_space > SIZE_MAX / 2)
goto error_exit;
new_space *= 2;
}
if (buf->buftype == K5BUF_DYNAMIC_ZAP) {
new_data = malloc(new_space);
if (new_data == NULL)
goto error_exit;
memcpy(new_data, buf->data, buf->len);
zap(buf->data, buf->len);
free(buf->data);
} else {
new_data = realloc(buf->data, new_space);
if (new_data == NULL)
goto error_exit;
}
buf->data = new_data;
buf->space = new_space;
return 1;
error_exit:
if (buf->buftype == K5BUF_DYNAMIC_ZAP)
zap(buf->data, buf->len);
if (buf->buftype == K5BUF_DYNAMIC_ZAP || buf->buftype == K5BUF_DYNAMIC)
free(buf->data);
set_error(buf);
return 0;
}
void
k5_buf_init_fixed(struct k5buf *buf, void *data, size_t space)
{
assert(space > 0);
buf->buftype = K5BUF_FIXED;
buf->data = data;
buf->space = space;
buf->len = 0;
}
void
k5_buf_init_dynamic(struct k5buf *buf)
{
buf->buftype = K5BUF_DYNAMIC;
buf->space = 128;
buf->data = malloc(buf->space);
if (buf->data == NULL) {
set_error(buf);
return;
}
buf->len = 0;
}
void
k5_buf_init_dynamic_zap(struct k5buf *buf)
{
k5_buf_init_dynamic(buf);
if (buf->buftype == K5BUF_DYNAMIC)
buf->buftype = K5BUF_DYNAMIC_ZAP;
}
void
k5_buf_add(struct k5buf *buf, const char *data)
{
k5_buf_add_len(buf, data, strlen(data));
}
void
k5_buf_add_len(struct k5buf *buf, const void *data, size_t len)
{
if (!ensure_space(buf, len))
return;
if (len > 0)
memcpy(endptr(buf), data, len);
buf->len += len;
}
void
k5_buf_add_vfmt(struct k5buf *buf, const char *fmt, va_list ap)
{
va_list apcopy;
int r;
size_t remaining;
char *tmp;
if (buf->buftype == K5BUF_ERROR)
return;
remaining = buf->space - buf->len;
if (buf->buftype == K5BUF_FIXED) {
r = vsnprintf(endptr(buf), remaining, fmt, ap);
if (SNPRINTF_OVERFLOW(r, remaining))
set_error(buf);
else
buf->len += (unsigned int) r;
return;
}
assert(buf->buftype == K5BUF_DYNAMIC || buf->buftype == K5BUF_DYNAMIC_ZAP);
va_copy(apcopy, ap);
r = vsnprintf(endptr(buf), remaining, fmt, apcopy);
va_end(apcopy);
if (!SNPRINTF_OVERFLOW(r, remaining)) {
buf->len += (unsigned int) r;
return;
}
if (r >= 0) {
if (!ensure_space(buf, r + 1))
return;
remaining = buf->space - buf->len;
r = vsnprintf(endptr(buf), remaining, fmt, ap);
if (SNPRINTF_OVERFLOW(r, remaining))
k5_buf_free(buf);
else
buf->len += (unsigned int) r;
return;
}
r = vasprintf(&tmp, fmt, ap);
if (r < 0) {
k5_buf_free(buf);
return;
}
if (ensure_space(buf, r)) {
memcpy(endptr(buf), tmp, r);
buf->len += r;
}
if (buf->buftype == K5BUF_DYNAMIC_ZAP)
zap(tmp, strlen(tmp));
free(tmp);
}
void
k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
k5_buf_add_vfmt(buf, fmt, ap);
va_end(ap);
}
char *
k5_buf_cstring(struct k5buf *buf)
{
if (!ensure_space(buf, 1))
return NULL;
*endptr(buf) = '\0';
return buf->data;
}
void *
k5_buf_get_space(struct k5buf *buf, size_t len)
{
if (!ensure_space(buf, len))
return NULL;
buf->len += len;
return endptr(buf) - len;
}
void
k5_buf_truncate(struct k5buf *buf, size_t len)
{
if (buf->buftype == K5BUF_ERROR)
return;
assert(len <= buf->len);
buf->len = len;
}
int
k5_buf_status(struct k5buf *buf)
{
return (buf->buftype == K5BUF_ERROR) ? ENOMEM : 0;
}
void
k5_buf_free(struct k5buf *buf)
{
if (buf->buftype == K5BUF_ERROR)
return;
assert(buf->buftype == K5BUF_DYNAMIC || buf->buftype == K5BUF_DYNAMIC_ZAP);
if (buf->buftype == K5BUF_DYNAMIC_ZAP)
zap(buf->data, buf->len);
free(buf->data);
set_error(buf);
}