From: Christopher Faulet Date: Thu, 4 Feb 2021 09:54:37 +0000 (+0100) Subject: MINOR: buf: Add function to realign a buffer with a specific head position X-Git-Tag: v2.5-dev1~236 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4fc51a73e669fdd479213682ef714c755657a06f;p=thirdparty%2Fhaproxy.git MINOR: buf: Add function to realign a buffer with a specific head position b_slow_realign() function may be used to realign a buffer with a given amount of output data, eventually 0. In such case, the head is set to 0. This function is not designed to be used with input only buffers, like those used in the muxes. It is the purpose of b_slow_realign_ofs() function. It does almost the same, realign a buffer. But it do so by setting the buffer head to a specific offset. --- diff --git a/include/haproxy/buf.h b/include/haproxy/buf.h index a0e1cb8113..f66f5c6870 100644 --- a/include/haproxy/buf.h +++ b/include/haproxy/buf.h @@ -469,6 +469,37 @@ static inline void b_slow_realign(struct buffer *b, char *swap, size_t output) b->head = (output ? b_size(b) - output : 0); } +/* b_slow_realign_ofs() : this function realigns a possibly wrapping buffer + * setting its new head at . Depending of the value, the resulting + * buffer may also wrap. A temporary swap area at least as large as b->size must + * be provided in . It's up to the caller to ensuze is not larger + * than b->size. + */ +static inline void b_slow_realign_ofs(struct buffer *b, char *swap, size_t ofs) +{ + size_t block1 = b_data(b); + size_t block2 = 0; + + if (__b_tail_ofs(b) >= b_size(b)) { + block2 = b_tail_ofs(b); + block1 -= block2; + } + memcpy(swap, b_head(b), block1); + memcpy(swap + block1, b_orig(b), block2); + + block1 = b_data(b); + block2 = 0; + if (block1 > b_size(b) - ofs) { + block1 = b_size(b) - ofs; + block2 = b_data(b) - block1; + } + memcpy(b_orig(b) + ofs, swap, block1); + memcpy(b_orig(b), swap + block1, block2); + + b->head = ofs; +} + + /* b_putchar() : tries to append char at the end of buffer . Supports * wrapping. Data are truncated if buffer is full. */