From b2a8e8731da82b8bbd9dfff6d5a0d71f25a5ee49 Mon Sep 17 00:00:00 2001 From: William Lallemand Date: Thu, 8 Aug 2024 17:05:45 +0200 Subject: [PATCH] MINOR: channel: implement ci_insert() function ci_insert() is a function which allows to insert a string of size at of the input buffer. This is the equivalent of ci_insert_line2() but without inserting '\r\n' --- include/haproxy/channel.h | 1 + src/channel.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/haproxy/channel.h b/include/haproxy/channel.h index 22949e145..d2d45e552 100644 --- a/include/haproxy/channel.h +++ b/include/haproxy/channel.h @@ -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); diff --git a/src/channel.c b/src/channel.c index 0b6389dd5..47d5dde6e 100644 --- a/src/channel.c +++ b/src/channel.c @@ -548,6 +548,36 @@ int ci_getline_nc(const struct channel *chn, return 0; } +/* Inserts at position relative to channel 's * input head. The + * argument informs about the length of string so that we don't have + * to measure it. 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 followed by "\r\n" at position relative to channel 's * input head. The argument informs about the length of string so * that we don't have to measure it. must be a valid pointer and must not