diff --git a/http.c b/http.c index 579ca722..5bd5e158 100644 --- a/http.c +++ b/http.c @@ -92,6 +92,30 @@ void __attribute__ ((format (printf, 2, 3))) } } +void buf_append_bytes(struct oc_text_buf *buf, const void *bytes, int len) +{ + int new_buf_len; + + if (!buf || buf->error) + return; + + new_buf_len = (buf->pos + len + BUF_CHUNK_SIZE - 1) & ~(BUF_CHUNK_SIZE-1); + if (new_buf_len > MAX_BUF_LEN) { + buf->error = -E2BIG; + return; + } + if (buf->buf_len < new_buf_len) { + realloc_inplace(buf->data, new_buf_len); + if (!buf->data) { + buf->error =- -ENOMEM; + return; + } + buf->buf_len = new_buf_len; + } + memcpy(buf->data + buf->pos, bytes, len); + buf->pos += len; +} + int buf_error(struct oc_text_buf *buf) { return buf ? buf->error : -ENOMEM; diff --git a/openconnect-internal.h b/openconnect-internal.h index 7a0f3468..812522fc 100644 --- a/openconnect-internal.h +++ b/openconnect-internal.h @@ -568,6 +568,7 @@ int prepare_stoken(struct openconnect_info *vpninfo); struct oc_text_buf *buf_alloc(void); void __attribute__ ((format (printf, 2, 3))) buf_append(struct oc_text_buf *buf, const char *fmt, ...); +void buf_append_bytes(struct oc_text_buf *buf, const void *bytes, int len); int buf_error(struct oc_text_buf *buf); int buf_free(struct oc_text_buf *buf); char *openconnect_create_useragent(const char *base);