blob: d7bfb967e49ad0643b89c75a565ff3d497a04496 [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
2 * include/common/buffer.h
3 * Buffer management definitions, macros and inline functions.
4 *
5 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _COMMON_BUFFER_H
23#define _COMMON_BUFFER_H
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
Willy Tarreau41806d12018-07-11 09:39:05 +020029#include <common/buf.h>
Willy Tarreau8c89c202012-09-28 16:02:48 +020030#include <common/chunk.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020031#include <common/config.h>
Willy Tarreau6634b632017-09-22 15:02:54 +020032#include <common/ist.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020033#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020034
35
Christopher Fauleta73e59b2016-12-09 17:30:18 +010036/* an element of the <buffer_wq> list. It represents an object that need to
37 * acquire a buffer to continue its process. */
38struct buffer_wait {
39 void *target; /* The waiting object that should be woken up */
40 int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */
41 struct list list; /* Next element in the <buffer_wq> list */
42};
43
Willy Tarreaubafbe012017-11-24 17:34:44 +010044extern struct pool_head *pool_head_buffer;
Willy Tarreau2a4b5432014-11-24 11:39:34 +010045extern struct buffer buf_empty;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010046extern struct buffer buf_wanted;
Christopher Fauleta73e59b2016-12-09 17:30:18 +010047extern struct list buffer_wq;
Willy Tarreau53bae852017-11-26 11:00:37 +010048__decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock);
Willy Tarreauc7e42382012-08-24 19:22:53 +020049
Willy Tarreau9b28e032012-10-12 23:49:43 +020050int init_buffer();
Christopher Fauletad405f12017-08-29 15:30:11 +020051void deinit_buffer();
Willy Tarreauaf819352012-08-27 22:08:00 +020052int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
53int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020054void buffer_dump(FILE *o, struct buffer *b, int from, int to);
Willy Tarreauc7e42382012-08-24 19:22:53 +020055
56/*****************************************************************/
57/* These functions are used to compute various buffer area sizes */
58/*****************************************************************/
59
Willy Tarreauc7e42382012-08-24 19:22:53 +020060
Willy Tarreaueac52592018-06-15 13:59:36 +020061
62/***** FIXME: OLD API BELOW *****/
Willy Tarreauc7e42382012-08-24 19:22:53 +020063
Willy Tarreauc7e42382012-08-24 19:22:53 +020064/* Normalizes a pointer after an addition */
65static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
66{
Willy Tarreau591d4452018-06-15 17:21:00 +020067 if (ptr - buf->size >= b_orig(buf))
Willy Tarreauc7e42382012-08-24 19:22:53 +020068 ptr -= buf->size;
69 return ptr;
70}
71
Willy Tarreauc7e42382012-08-24 19:22:53 +020072/* Normalizes a pointer which is supposed to be relative to the beginning of a
73 * buffer, so that wrapping is correctly handled. The intent is to use this
74 * when increasing a pointer. Note that the wrapping test is only performed
75 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
76 * otherwise an invalid pointer might be returned.
77 */
78static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
79{
Willy Tarreau591d4452018-06-15 17:21:00 +020080 if (ptr < b_orig(buf))
Willy Tarreauc7e42382012-08-24 19:22:53 +020081 ptr += buf->size;
Willy Tarreau591d4452018-06-15 17:21:00 +020082 else if (ptr - buf->size >= b_orig(buf))
Willy Tarreauc7e42382012-08-24 19:22:53 +020083 ptr -= buf->size;
84 return ptr;
85}
86
87/* Returns the distance between two pointers, taking into account the ability
88 * to wrap around the buffer's end.
89 */
90static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
91{
92 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +020093
94 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +020095 return count;
96}
97
Willy Tarreauc7e42382012-08-24 19:22:53 +020098/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
99static inline int buffer_almost_full(const struct buffer *buf)
100{
Willy Tarreau4428a292014-11-28 20:54:13 +0100101 if (buf == &buf_empty)
102 return 0;
103
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200104 return b_almost_full(buf);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200105}
106
Willy Tarreauaf819352012-08-27 22:08:00 +0200107/* This function writes the string <str> at position <pos> which must be in
108 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
109 * (l, r, lr) are updated to be valid after the shift. the shift value
110 * (positive or negative) is returned. If there's no space left, the move is
111 * not done. The function does not adjust ->o because it does not make sense
112 * to use it on data scheduled to be sent.
113 */
114static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
115{
116 return buffer_replace2(b, pos, end, str, strlen(str));
117}
118
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200119/* Tries to append char <c> at the end of buffer <b>. Supports wrapping. Data
120 * are truncated if buffer is full.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200121 */
122static inline void bo_putchr(struct buffer *b, char c)
123{
Willy Tarreaueac52592018-06-15 13:59:36 +0200124 if (b_data(b) == b->size)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200125 return;
Willy Tarreau271e2a52018-07-09 10:31:30 +0200126 *b_tail(b) = c;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200127 b->len++;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200128}
129
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200130/* Tries to append block <blk> at the end of buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100131 * Data are truncated if buffer is too short. It returns the number of bytes
132 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200133 */
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200134static inline unsigned int bo_putblk(struct buffer *b, const char *blk, unsigned int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200135{
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200136 unsigned int half;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200137
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200138 if (len > b_room(b))
139 len = b_room(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200140 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100141 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200142
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200143 half = b_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200144 if (half > len)
145 half = len;
146
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200147 memcpy(b_tail(b), blk, half);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200148 b->len += half;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200149 if (len > half) {
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200150 memcpy(b_tail(b), blk + half, len - half);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200151 b->len += len - half;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200152 }
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100153 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200154}
155
156/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100157 * Data are truncated if buffer is too short. It returns the number of bytes
158 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200159 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100160static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200161{
162 return bo_putblk(b, str, strlen(str));
163}
164
165/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100166 * Data are truncated if buffer is too short. It returns the number of bytes
167 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200168 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100169static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200170{
171 return bo_putblk(b, chk->str, chk->len);
172}
173
Willy Tarreau145746c2017-10-26 15:26:17 +0200174/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
175 * Data are truncated if buffer is full.
176 */
177static inline void bi_putchr(struct buffer *b, char c)
178{
Willy Tarreaueac52592018-06-15 13:59:36 +0200179 if (b_data(b) == b->size)
Willy Tarreau145746c2017-10-26 15:26:17 +0200180 return;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200181 *b_tail(b) = c;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200182 b->len++;
Willy Tarreau145746c2017-10-26 15:26:17 +0200183}
184
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200185/* Tries to append block <blk> at the end of buffer <b>. Supports wrapping.
Willy Tarreau145746c2017-10-26 15:26:17 +0200186 * Data are truncated if buffer is too short. It returns the number of bytes
187 * copied.
188 */
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200189static inline unsigned int bi_putblk(struct buffer *b, const char *blk, unsigned int len)
Willy Tarreau145746c2017-10-26 15:26:17 +0200190{
Willy Tarreau145746c2017-10-26 15:26:17 +0200191 int half;
192
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200193 if (len > b_room(b))
194 len = b_room(b);
Willy Tarreau145746c2017-10-26 15:26:17 +0200195 if (!len)
196 return 0;
197
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200198 half = b_contig_space(b);
Willy Tarreau145746c2017-10-26 15:26:17 +0200199 if (half > len)
200 half = len;
201
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200202 memcpy(b_tail(b), blk, half);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200203 b->len += half;
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200204 if (len > half) {
205 memcpy(b_tail(b), blk + half, len - half);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200206 b->len += len - half;
Willy Tarreau523cc5d2018-07-09 10:32:29 +0200207 }
Willy Tarreau145746c2017-10-26 15:26:17 +0200208 return len;
209}
210
211/* Tries to copy string <str> into input data at buffer <b>. Supports wrapping.
212 * Data are truncated if buffer is too short. It returns the number of bytes
213 * copied.
214 */
215static inline int bi_putstr(struct buffer *b, const char *str)
216{
217 return bi_putblk(b, str, strlen(str));
218}
219
220/* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping.
221 * Data are truncated if buffer is too short. It returns the number of bytes
222 * copied.
223 */
224static inline int bi_putchk(struct buffer *b, const struct chunk *chk)
225{
226 return bi_putblk(b, chk->str, chk->len);
227}
228
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100229/* Allocates a buffer and replaces *buf with this buffer. If no memory is
230 * available, &buf_wanted is used instead. No control is made to check if *buf
231 * already pointed to another buffer. The allocated buffer is returned, or
232 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100233 */
234static inline struct buffer *b_alloc(struct buffer **buf)
235{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100236 struct buffer *b;
237
238 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100239 b = pool_alloc_dirty(pool_head_buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100240 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100241 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100242 b_reset(b);
243 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100244 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100245 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100246}
247
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100248/* Allocates a buffer and replaces *buf with this buffer. If no memory is
249 * available, &buf_wanted is used instead. No control is made to check if *buf
250 * already pointed to another buffer. The allocated buffer is returned, or
251 * NULL in case no memory is available. The difference with b_alloc() is that
252 * this function only picks from the pool and never calls malloc(), so it can
253 * fail even if some memory is available.
254 */
255static inline struct buffer *b_alloc_fast(struct buffer **buf)
256{
257 struct buffer *b;
258
259 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100260 b = pool_get_first(pool_head_buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100261 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100262 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100263 b_reset(b);
264 *buf = b;
265 }
266 return b;
267}
268
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100269/* Releases buffer *buf (no check of emptiness) */
270static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100271{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100272 pool_free(pool_head_buffer, *buf);
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100273}
274
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100275/* Releases buffer *buf if allocated. */
276static inline void b_drop(struct buffer **buf)
277{
278 if (!(*buf)->size)
279 return;
280 __b_drop(buf);
281}
282
283/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
284static inline void b_free(struct buffer **buf)
285{
286 b_drop(buf);
287 *buf = &buf_empty;
288}
289
Willy Tarreauf4718e82014-12-02 13:54:01 +0100290/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
291 * there are still at least <margin> buffers available in the pool after this
292 * allocation so that we don't leave the pool in a condition where a session or
293 * a response buffer could not be allocated anymore, resulting in a deadlock.
294 * This means that we sometimes need to try to allocate extra entries even if
295 * only one buffer is needed.
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100296 *
297 * We need to lock the pool here to be sure to have <margin> buffers available
298 * after the allocation, regardless how many threads that doing it in the same
299 * time. So, we use internal and lockless memory functions (prefixed with '__').
Willy Tarreauf4718e82014-12-02 13:54:01 +0100300 */
301static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
302{
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100303 struct buffer *b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100304
305 if ((*buf)->size)
306 return *buf;
307
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100308 *buf = &buf_wanted;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100309#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100310 HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100311#endif
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100312
Willy Tarreauf4718e82014-12-02 13:54:01 +0100313 /* fast path */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100314 if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) {
315 b = __pool_get_first(pool_head_buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100316 if (likely(b)) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100317#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100318 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100319#endif
Willy Tarreaubafbe012017-11-24 17:34:44 +0100320 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100321 b_reset(b);
322 *buf = b;
323 return b;
324 }
325 }
Willy Tarreauf4718e82014-12-02 13:54:01 +0100326
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100327 /* slow path, uses malloc() */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100328 b = __pool_refill_alloc(pool_head_buffer, margin);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100329
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100330#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100331 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100332#endif
Willy Tarreauf4718e82014-12-02 13:54:01 +0100333
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100334 if (b) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100335 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100336 b_reset(b);
337 *buf = b;
338 }
339 return b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100340}
341
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100342
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100343/* Offer a buffer currently belonging to target <from> to whoever needs one.
344 * Any pointer is valid for <from>, including NULL. Its purpose is to avoid
345 * passing a buffer to oneself in case of failed allocations (e.g. need two
346 * buffers, get one, fail, release it and wake up self again). In case of
347 * normal buffer release where it is expected that the caller is not waiting
348 * for a buffer, NULL is fine.
349 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100350void __offer_buffer(void *from, unsigned int threshold);
351
352static inline void offer_buffers(void *from, unsigned int threshold)
353{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100354 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +0200355 if (LIST_ISEMPTY(&buffer_wq)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100356 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100357 return;
Emeric Bruna1dd2432017-06-21 15:42:52 +0200358 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100359 __offer_buffer(from, threshold);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100360 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100361}
362
Willy Tarreau6634b632017-09-22 15:02:54 +0200363/*************************************************************************/
364/* functions used to manipulate strings and blocks with wrapping buffers */
365/*************************************************************************/
366
Willy Tarreaubc59f352018-06-15 13:45:17 +0200367/* returns > 0 if the first <n> characters of buffer <b> starting at offset <o>
368 * relative to the buffer's head match <ist>. (empty strings do match). It is
Willy Tarreau6634b632017-09-22 15:02:54 +0200369 * designed to be use with reasonably small strings (ie matches a single byte
370 * per iteration). This function is usable both with input and output data. To
371 * be used like this depending on what to match :
Willy Tarreaubc59f352018-06-15 13:45:17 +0200372 * - input contents : b_isteq(b, b->o, b->i, ist);
373 * - output contents : b_isteq(b, 0, b->o, ist);
Willy Tarreau6634b632017-09-22 15:02:54 +0200374 * Return value :
375 * >0 : the number of matching bytes
376 * =0 : not enough bytes (or matching of empty string)
377 * <0 : non-matching byte found
378 */
379static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
380{
381 struct ist r = ist;
382 const char *p;
Willy Tarreaubc59f352018-06-15 13:45:17 +0200383 const char *end = b_wrap(b);
Willy Tarreau6634b632017-09-22 15:02:54 +0200384
385 if (n < r.len)
386 return 0;
387
Willy Tarreaubc59f352018-06-15 13:45:17 +0200388 p = b_peek(b, o);
Willy Tarreau6634b632017-09-22 15:02:54 +0200389 while (r.len--) {
390 if (*p++ != *r.ptr++)
391 return -1;
392 if (unlikely(p == end))
Willy Tarreau591d4452018-06-15 17:21:00 +0200393 p = b_orig(b);
Willy Tarreau6634b632017-09-22 15:02:54 +0200394 }
395 return ist.len;
396}
397
Willy Tarreaubc59f352018-06-15 13:45:17 +0200398/* "eats" string <ist> from the head of buffer <b>. Wrapping data is explicitly
399 * supported. It matches a single byte per iteration so strings should remain
400 * reasonably small. Returns :
Willy Tarreau6634b632017-09-22 15:02:54 +0200401 * > 0 : number of bytes matched and eaten
402 * = 0 : not enough bytes (or matching an empty string)
403 * < 0 : non-matching byte found
404 */
Willy Tarreaubc59f352018-06-15 13:45:17 +0200405static inline int b_eat(struct buffer *b, const struct ist ist)
Willy Tarreau6634b632017-09-22 15:02:54 +0200406{
Willy Tarreaubc59f352018-06-15 13:45:17 +0200407 int ret = b_isteq(b, 0, b_data(b), ist);
Willy Tarreau6634b632017-09-22 15:02:54 +0200408 if (ret > 0)
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200409 b_del(b, ret);
Willy Tarreau6634b632017-09-22 15:02:54 +0200410 return ret;
411}
412
Willy Tarreaubc59f352018-06-15 13:45:17 +0200413/* injects string <ist> at the tail of input buffer <b> provided that it
Willy Tarreaue5676e72017-09-22 15:47:51 +0200414 * fits. Wrapping is supported. It's designed for small strings as it only
415 * writes a single byte per iteration. Returns the number of characters copied
416 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
417 * will only modify the buffer upon success. In all cases, the contents are
418 * copied prior to reporting an error, so that the destination at least
419 * contains a valid but truncated string.
420 */
421static inline int bi_istput(struct buffer *b, const struct ist ist)
422{
Willy Tarreaubc59f352018-06-15 13:45:17 +0200423 const char *end = b_wrap(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200424 struct ist r = ist;
425 char *p;
426
Willy Tarreaueac52592018-06-15 13:59:36 +0200427 if (r.len > (size_t)b_room(b))
Willy Tarreaue5676e72017-09-22 15:47:51 +0200428 return r.len < b->size ? 0 : -1;
429
Willy Tarreaubc59f352018-06-15 13:45:17 +0200430 p = b_tail(b);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200431 b->len += r.len;
Willy Tarreaue5676e72017-09-22 15:47:51 +0200432 while (r.len--) {
433 *p++ = *r.ptr++;
434 if (unlikely(p == end))
Willy Tarreau591d4452018-06-15 17:21:00 +0200435 p = b_orig(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200436 }
437 return ist.len;
438}
439
440
Willy Tarreaubc59f352018-06-15 13:45:17 +0200441/* injects string <ist> at the tail of output buffer <b> provided that it
Willy Tarreaue5676e72017-09-22 15:47:51 +0200442 * fits. Input data is assumed not to exist and will silently be overwritten.
443 * Wrapping is supported. It's designed for small strings as it only writes a
444 * single byte per iteration. Returns the number of characters copied (ist.len),
445 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
446 * modify the buffer upon success. In all cases, the contents are copied prior
447 * to reporting an error, so that the destination at least contains a valid
448 * but truncated string.
449 */
450static inline int bo_istput(struct buffer *b, const struct ist ist)
451{
Willy Tarreaubc59f352018-06-15 13:45:17 +0200452 const char *end = b_wrap(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200453 struct ist r = ist;
454 char *p;
455
Willy Tarreaueac52592018-06-15 13:59:36 +0200456 if (r.len > (size_t)b_room(b))
Willy Tarreaue5676e72017-09-22 15:47:51 +0200457 return r.len < b->size ? 0 : -1;
458
Willy Tarreaubc59f352018-06-15 13:45:17 +0200459 p = b_tail(b);
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200460 b->len += r.len;
Willy Tarreaue5676e72017-09-22 15:47:51 +0200461 while (r.len--) {
462 *p++ = *r.ptr++;
463 if (unlikely(p == end))
Willy Tarreau591d4452018-06-15 17:21:00 +0200464 p = b_orig(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200465 }
466 return ist.len;
467}
468
469
Willy Tarreauc7e42382012-08-24 19:22:53 +0200470#endif /* _COMMON_BUFFER_H */
471
472/*
473 * Local variables:
474 * c-indent-level: 8
475 * c-basic-offset: 8
476 * End:
477 */