From: Willy Tarreau Date: Mon, 8 Oct 2018 05:34:25 +0000 (+0200) Subject: MINOR: chunk: add chunk_cpy() and chunk_cat() X-Git-Tag: v1.9-dev4~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c1f40b38a6a27f2c5977cab098297d2f01108a54;p=thirdparty%2Fhaproxy.git MINOR: chunk: add chunk_cpy() and chunk_cat() Sometimes we need to concatenate constant chunks to existing ones, but no function currently exists to do this easily, hence these two new ones. --- diff --git a/include/common/chunk.h b/include/common/chunk.h index 383e15736f..ffcb93c8ff 100644 --- a/include/common/chunk.h +++ b/include/common/chunk.h @@ -96,6 +96,28 @@ static inline void chunk_initstr(struct buffer *chk, const char *str) chk->size = 0; /* mark it read-only */ } +/* copies chunk into . Returns 0 in case of failure. */ +static inline int chunk_cpy(struct buffer *chk, const struct buffer *src) +{ + if (unlikely(src->data >= chk->size)) + return 0; + + chk->data = src->data; + memcpy(chk->area, src->area, src->data); + return 1; +} + +/* appends chunk after . Returns 0 in case of failure. */ +static inline int chunk_cat(struct buffer *chk, const struct buffer *src) +{ + if (unlikely(chk->data + src->data >= chk->size)) + return 0; + + memcpy(chk->area + chk->data, src->area, src->data); + chk->data += src->data; + return 1; +} + /* copies memory area into for bytes. Returns 0 in * case of failure. No trailing zero is added. */