]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: chunks: add chunk_memcpy() and chunk_memcat()
authorWilly Tarreau <w@1wt.eu>
Thu, 27 Jul 2017 11:35:34 +0000 (13:35 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 18 Aug 2017 11:26:20 +0000 (13:26 +0200)
These two functions respectively copy a memory area onto the chunk, and
append the contents of a memory area over a chunk. They are convenient
to prepare binary output data to be sent and will be used for HTTP/2.

include/common/chunk.h

index 8c067f7dafa321c5d452daa29235fbabbc77af7e..1f89e9bf21152f15d58f591d16102c32ddaa9471 100644 (file)
@@ -98,6 +98,33 @@ static inline void chunk_initstr(struct chunk *chk, const char *str)
        chk->size = 0;                  /* mark it read-only */
 }
 
+/* copies memory area <src> into <chk> for <len> bytes. Returns 0 in
+ * case of failure. No trailing zero is added.
+ */
+static inline int chunk_memcpy(struct chunk *chk, const char *src, size_t len)
+{
+       if (unlikely(len >= chk->size))
+               return 0;
+
+       chk->len  = len;
+       memcpy(chk->str, src, len);
+
+       return 1;
+}
+
+/* appends memory area <src> after <chk> for <len> bytes. Returns 0 in
+ * case of failure. No trailing zero is added.
+ */
+static inline int chunk_memcat(struct chunk *chk, const char *src, size_t len)
+{
+       if (unlikely(chk->len < 0 || chk->len + len >= chk->size))
+               return 0;
+
+       memcpy(chk->str + chk->len, src, len);
+       chk->len += len;
+       return 1;
+}
+
 /* copies str into <chk> followed by a trailing zero. Returns 0 in
  * case of failure.
  */