MINOR: channel: implement ci_insert() function

ci_insert() is a function which allows to insert a string <str> of size
<len> at <pos> of the input buffer. This is the equivalent of
ci_insert_line2() but without inserting '\r\n'

(cherry picked from commit b2a8e8731da82b8bbd9dfff6d5a0d71f25a5ee49)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit dcfe6118b280c2f21d2a2fd6395fea1088d796f5)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit a9750c27bf3355def3cd5ebaa1df157b6a0dcd5a)
Signed-off-by: Willy Tarreau <w@1wt.eu>
diff --git a/include/haproxy/channel.h b/include/haproxy/channel.h
index 174f1b7..b8e1841 100644
--- a/include/haproxy/channel.h
+++ b/include/haproxy/channel.h
@@ -44,6 +44,7 @@
 int ci_putchr(struct channel *chn, char c);
 int ci_getline_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
 int ci_getblk_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
+int ci_insert(struct channel *c, int pos, const char *str, int len);
 int ci_insert_line2(struct channel *c, int pos, const char *str, int len);
 int co_inject(struct channel *chn, const char *msg, int len);
 int co_getchar(const struct channel *chn, char *c);
diff --git a/src/channel.c b/src/channel.c
index 2812e9b..08bfe96 100644
--- a/src/channel.c
+++ b/src/channel.c
@@ -548,6 +548,36 @@
 	return 0;
 }
 
+/* Inserts <str> at position <pos> relative to channel <c>'s  * input head. The
+ * <len> argument informs about the length of string <str> so that we don't have
+ * to measure it. <str> must be a valid pointer.
+ *
+ * The number of bytes added is returned on success. 0 is returned on failure.
+ */
+int ci_insert(struct channel *c, int pos, const char *str, int len)
+{
+	struct buffer *b = &c->buf;
+	char *dst = c_ptr(c, pos);
+
+	if (__b_tail(b) + len >= b_wrap(b))
+		return 0;  /* no space left */
+
+	if (b_data(b) &&
+	    b_tail(b) + len > b_head(b) &&
+	    b_head(b) >= b_tail(b))
+		return 0;  /* no space left before wrapping data */
+
+	/* first, protect the end of the buffer */
+	memmove(dst + len, dst, b_tail(b) - dst);
+
+	/* now, copy str over dst */
+	memcpy(dst, str, len);
+
+	b_add(b, len);
+	return len;
+}
+
+
 /* Inserts <str> followed by "\r\n" at position <pos> relative to channel <c>'s
  * input head. The <len> argument informs about the length of string <str> so
  * that we don't have to measure it. <str> must be a valid pointer and must not