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>
This commit is contained in:
William Lallemand 2024-08-08 17:05:45 +02:00 committed by Christopher Faulet
parent 1038e1517a
commit dcfe6118b2
2 changed files with 31 additions and 0 deletions

View File

@ -44,6 +44,7 @@ int ci_putblk(struct channel *chn, const char *str, int len);
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);

View File

@ -548,6 +548,36 @@ int ci_getline_nc(const struct channel *chn,
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