From: Willy Tarreau Date: Fri, 22 Sep 2017 13:47:51 +0000 (+0200) Subject: MINOR: buffer: add two functions to inject data into buffers X-Git-Tag: v1.8-dev3~31 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e5676e71034a26e338552702e7100b99806b1538;p=thirdparty%2Fhaproxy.git MINOR: buffer: add two functions to inject data into buffers bi_istput() injects the ist string into the input region of the buffer, it will be used to feed small data chunks into the conn_stream. bo_istput() does the same into the output region of the buffer, it will be used to send data via the transport layer and assumes there's no input data. --- diff --git a/include/common/buffer.h b/include/common/buffer.h index 8050b1a36f..339de377b3 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -618,6 +618,64 @@ static inline int bi_eat(struct buffer *b, const struct ist ist) return ret; } +/* injects string into the input region of buffer provided that it + * fits. Wrapping is supported. It's designed for small strings as it only + * writes a single byte per iteration. Returns the number of characters copied + * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It + * will only modify the buffer upon success. In all cases, the contents are + * copied prior to reporting an error, so that the destination at least + * contains a valid but truncated string. + */ +static inline int bi_istput(struct buffer *b, const struct ist ist) +{ + const char *end = b->data + b->size; + struct ist r = ist; + char *p; + + if (r.len > (size_t)(b->size - b->i - b->o)) + return r.len < b->size ? 0 : -1; + + p = b_ptr(b, b->i); + b->i += r.len; + while (r.len--) { + *p++ = *r.ptr++; + if (unlikely(p == end)) + p = b->data; + } + return ist.len; +} + + +/* injects string into the output region of buffer provided that it + * fits. Input data is assumed not to exist and will silently be overwritten. + * Wrapping is supported. It's designed for small strings as it only writes a + * single byte per iteration. Returns the number of characters copied (ist.len), + * 0 if it temporarily does not fit or -1 if it will never fit. It will only + * modify the buffer upon success. In all cases, the contents are copied prior + * to reporting an error, so that the destination at least contains a valid + * but truncated string. + */ +static inline int bo_istput(struct buffer *b, const struct ist ist) +{ + const char *end = b->data + b->size; + struct ist r = ist; + char *p; + + if (r.len > (size_t)(b->size - b->o)) + return r.len < b->size ? 0 : -1; + + p = b->p; + b->o += r.len; + b->p = b_ptr(b, r.len); + while (r.len--) { + *p++ = *r.ptr++; + if (unlikely(p == end)) + p = b->data; + } + return ist.len; +} + + #endif /* _COMMON_BUFFER_H */ /*