]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: chunk: add chunk_cpy() and chunk_cat()
authorWilly Tarreau <w@1wt.eu>
Mon, 8 Oct 2018 05:34:25 +0000 (07:34 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 12 Oct 2018 14:58:01 +0000 (16:58 +0200)
Sometimes we need to concatenate constant chunks to existing ones, but
no function currently exists to do this easily, hence these two new ones.

include/common/chunk.h

index 383e15736f18629781442435eef4b1762fef6349..ffcb93c8ff50003d9c5471c01da4a03efc19760d 100644 (file)
@@ -96,6 +96,28 @@ static inline void chunk_initstr(struct buffer *chk, const char *str)
        chk->size = 0;                  /* mark it read-only */
 }
 
+/* copies chunk <src> into <chk>. 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 <src> after <chk>. 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 <src> into <chk> for <len> bytes. Returns 0 in
  * case of failure. No trailing zero is added.
  */