blob: e43969c54cc2c02d0ca2b15ea9dfe852d5fc2290 [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
107/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
108 * call this function with remaining data waiting to be sent (o > 0). The
109 * caller must ensure that <n> is smaller than the actual buffer's length.
110 * This is mainly used to remove empty lines at the beginning of a request
111 * or a response.
112 */
113static inline void bi_fast_delete(struct buffer *buf, int n)
114{
115 buf->i -= n;
116 buf->p += n;
117}
118
Willy Tarreauaf819352012-08-27 22:08:00 +0200119/* This function writes the string <str> at position <pos> which must be in
120 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
121 * (l, r, lr) are updated to be valid after the shift. the shift value
122 * (positive or negative) is returned. If there's no space left, the move is
123 * not done. The function does not adjust ->o because it does not make sense
124 * to use it on data scheduled to be sent.
125 */
126static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
127{
128 return buffer_replace2(b, pos, end, str, strlen(str));
129}
130
Willy Tarreau8c89c202012-09-28 16:02:48 +0200131/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
132 * Data are truncated if buffer is full.
133 */
134static inline void bo_putchr(struct buffer *b, char c)
135{
Willy Tarreaueac52592018-06-15 13:59:36 +0200136 if (b_data(b) == b->size)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200137 return;
Willy Tarreau271e2a52018-07-09 10:31:30 +0200138 *b_tail(b) = c;
Willy Tarreaubc59f352018-06-15 13:45:17 +0200139 b->p = b_peek(b, b->o + 1);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200140 b->o++;
141}
142
143/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100144 * Data are truncated if buffer is too short. It returns the number of bytes
145 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200146 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100147static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200148{
Willy Tarreaueac52592018-06-15 13:59:36 +0200149 int cur_len = b_data(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200150 int half;
151
152 if (len > b->size - cur_len)
153 len = (b->size - cur_len);
154 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100155 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200156
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200157 half = b_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200158 if (half > len)
159 half = len;
160
161 memcpy(b->p, blk, half);
Willy Tarreaubc59f352018-06-15 13:45:17 +0200162 b->p = b_peek(b, b->o + half);
163 b->o += half;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200164 if (len > half) {
Christopher Fauletb2b27942018-02-26 10:47:03 +0100165 memcpy(b->p, blk + half, len - half);
Willy Tarreaubc59f352018-06-15 13:45:17 +0200166 b->p = b_peek(b, b->o + len - half);
167 b->o += len - half;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200168 }
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100169 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200170}
171
172/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100173 * Data are truncated if buffer is too short. It returns the number of bytes
174 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200175 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100176static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200177{
178 return bo_putblk(b, str, strlen(str));
179}
180
181/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100182 * Data are truncated if buffer is too short. It returns the number of bytes
183 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200184 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100185static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200186{
187 return bo_putblk(b, chk->str, chk->len);
188}
189
Willy Tarreau145746c2017-10-26 15:26:17 +0200190/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
191 * Data are truncated if buffer is full.
192 */
193static inline void bi_putchr(struct buffer *b, char c)
194{
Willy Tarreaueac52592018-06-15 13:59:36 +0200195 if (b_data(b) == b->size)
Willy Tarreau145746c2017-10-26 15:26:17 +0200196 return;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200197 *b_tail(b) = c;
Willy Tarreau145746c2017-10-26 15:26:17 +0200198 b->i++;
199}
200
201/* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping.
202 * Data are truncated if buffer is too short. It returns the number of bytes
203 * copied.
204 */
205static inline int bi_putblk(struct buffer *b, const char *blk, int len)
206{
Willy Tarreaueac52592018-06-15 13:59:36 +0200207 int cur_len = b_data(b);
Willy Tarreau145746c2017-10-26 15:26:17 +0200208 int half;
209
210 if (len > b->size - cur_len)
211 len = (b->size - cur_len);
212 if (!len)
213 return 0;
214
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200215 half = b_contig_space(b);
Willy Tarreau145746c2017-10-26 15:26:17 +0200216 if (half > len)
217 half = len;
218
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200219 memcpy(b_tail(b), blk, half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200220 if (len > half)
Willy Tarreaubc59f352018-06-15 13:45:17 +0200221 memcpy(b_peek(b, b->o + b->i + half), blk + half, len - half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200222 b->i += len;
223 return len;
224}
225
226/* Tries to copy string <str> into input data at buffer <b>. Supports wrapping.
227 * Data are truncated if buffer is too short. It returns the number of bytes
228 * copied.
229 */
230static inline int bi_putstr(struct buffer *b, const char *str)
231{
232 return bi_putblk(b, str, strlen(str));
233}
234
235/* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping.
236 * Data are truncated if buffer is too short. It returns the number of bytes
237 * copied.
238 */
239static inline int bi_putchk(struct buffer *b, const struct chunk *chk)
240{
241 return bi_putblk(b, chk->str, chk->len);
242}
243
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100244/* Allocates a buffer and replaces *buf with this buffer. If no memory is
245 * available, &buf_wanted is used instead. No control is made to check if *buf
246 * already pointed to another buffer. The allocated buffer is returned, or
247 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100248 */
249static inline struct buffer *b_alloc(struct buffer **buf)
250{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100251 struct buffer *b;
252
253 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100254 b = pool_alloc_dirty(pool_head_buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100255 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100256 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100257 b_reset(b);
258 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100259 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100260 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100261}
262
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100263/* Allocates a buffer and replaces *buf with this buffer. If no memory is
264 * available, &buf_wanted is used instead. No control is made to check if *buf
265 * already pointed to another buffer. The allocated buffer is returned, or
266 * NULL in case no memory is available. The difference with b_alloc() is that
267 * this function only picks from the pool and never calls malloc(), so it can
268 * fail even if some memory is available.
269 */
270static inline struct buffer *b_alloc_fast(struct buffer **buf)
271{
272 struct buffer *b;
273
274 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100275 b = pool_get_first(pool_head_buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100276 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100277 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100278 b_reset(b);
279 *buf = b;
280 }
281 return b;
282}
283
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100284/* Releases buffer *buf (no check of emptiness) */
285static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100286{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100287 pool_free(pool_head_buffer, *buf);
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100288}
289
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100290/* Releases buffer *buf if allocated. */
291static inline void b_drop(struct buffer **buf)
292{
293 if (!(*buf)->size)
294 return;
295 __b_drop(buf);
296}
297
298/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
299static inline void b_free(struct buffer **buf)
300{
301 b_drop(buf);
302 *buf = &buf_empty;
303}
304
Willy Tarreauf4718e82014-12-02 13:54:01 +0100305/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
306 * there are still at least <margin> buffers available in the pool after this
307 * allocation so that we don't leave the pool in a condition where a session or
308 * a response buffer could not be allocated anymore, resulting in a deadlock.
309 * This means that we sometimes need to try to allocate extra entries even if
310 * only one buffer is needed.
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100311 *
312 * We need to lock the pool here to be sure to have <margin> buffers available
313 * after the allocation, regardless how many threads that doing it in the same
314 * time. So, we use internal and lockless memory functions (prefixed with '__').
Willy Tarreauf4718e82014-12-02 13:54:01 +0100315 */
316static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
317{
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100318 struct buffer *b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100319
320 if ((*buf)->size)
321 return *buf;
322
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100323 *buf = &buf_wanted;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100324#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100325 HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100326#endif
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100327
Willy Tarreauf4718e82014-12-02 13:54:01 +0100328 /* fast path */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100329 if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) {
330 b = __pool_get_first(pool_head_buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100331 if (likely(b)) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100332#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100333 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100334#endif
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 return b;
339 }
340 }
Willy Tarreauf4718e82014-12-02 13:54:01 +0100341
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100342 /* slow path, uses malloc() */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100343 b = __pool_refill_alloc(pool_head_buffer, margin);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100344
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100345#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100346 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100347#endif
Willy Tarreauf4718e82014-12-02 13:54:01 +0100348
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100349 if (b) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100350 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100351 b_reset(b);
352 *buf = b;
353 }
354 return b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100355}
356
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100357
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100358/* Offer a buffer currently belonging to target <from> to whoever needs one.
359 * Any pointer is valid for <from>, including NULL. Its purpose is to avoid
360 * passing a buffer to oneself in case of failed allocations (e.g. need two
361 * buffers, get one, fail, release it and wake up self again). In case of
362 * normal buffer release where it is expected that the caller is not waiting
363 * for a buffer, NULL is fine.
364 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100365void __offer_buffer(void *from, unsigned int threshold);
366
367static inline void offer_buffers(void *from, unsigned int threshold)
368{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100369 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +0200370 if (LIST_ISEMPTY(&buffer_wq)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100371 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100372 return;
Emeric Bruna1dd2432017-06-21 15:42:52 +0200373 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100374 __offer_buffer(from, threshold);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100375 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100376}
377
Willy Tarreau6634b632017-09-22 15:02:54 +0200378/*************************************************************************/
379/* functions used to manipulate strings and blocks with wrapping buffers */
380/*************************************************************************/
381
Willy Tarreaubc59f352018-06-15 13:45:17 +0200382/* returns > 0 if the first <n> characters of buffer <b> starting at offset <o>
383 * relative to the buffer's head match <ist>. (empty strings do match). It is
Willy Tarreau6634b632017-09-22 15:02:54 +0200384 * designed to be use with reasonably small strings (ie matches a single byte
385 * per iteration). This function is usable both with input and output data. To
386 * be used like this depending on what to match :
Willy Tarreaubc59f352018-06-15 13:45:17 +0200387 * - input contents : b_isteq(b, b->o, b->i, ist);
388 * - output contents : b_isteq(b, 0, b->o, ist);
Willy Tarreau6634b632017-09-22 15:02:54 +0200389 * Return value :
390 * >0 : the number of matching bytes
391 * =0 : not enough bytes (or matching of empty string)
392 * <0 : non-matching byte found
393 */
394static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
395{
396 struct ist r = ist;
397 const char *p;
Willy Tarreaubc59f352018-06-15 13:45:17 +0200398 const char *end = b_wrap(b);
Willy Tarreau6634b632017-09-22 15:02:54 +0200399
400 if (n < r.len)
401 return 0;
402
Willy Tarreaubc59f352018-06-15 13:45:17 +0200403 p = b_peek(b, o);
Willy Tarreau6634b632017-09-22 15:02:54 +0200404 while (r.len--) {
405 if (*p++ != *r.ptr++)
406 return -1;
407 if (unlikely(p == end))
Willy Tarreau591d4452018-06-15 17:21:00 +0200408 p = b_orig(b);
Willy Tarreau6634b632017-09-22 15:02:54 +0200409 }
410 return ist.len;
411}
412
Willy Tarreaubc59f352018-06-15 13:45:17 +0200413/* "eats" string <ist> from the head of buffer <b>. Wrapping data is explicitly
414 * supported. It matches a single byte per iteration so strings should remain
415 * reasonably small. Returns :
Willy Tarreau6634b632017-09-22 15:02:54 +0200416 * > 0 : number of bytes matched and eaten
417 * = 0 : not enough bytes (or matching an empty string)
418 * < 0 : non-matching byte found
419 */
Willy Tarreaubc59f352018-06-15 13:45:17 +0200420static inline int b_eat(struct buffer *b, const struct ist ist)
Willy Tarreau6634b632017-09-22 15:02:54 +0200421{
Willy Tarreaubc59f352018-06-15 13:45:17 +0200422 int ret = b_isteq(b, 0, b_data(b), ist);
Willy Tarreau6634b632017-09-22 15:02:54 +0200423 if (ret > 0)
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200424 b_del(b, ret);
Willy Tarreau6634b632017-09-22 15:02:54 +0200425 return ret;
426}
427
Willy Tarreaubc59f352018-06-15 13:45:17 +0200428/* injects string <ist> at the tail of input buffer <b> provided that it
Willy Tarreaue5676e72017-09-22 15:47:51 +0200429 * fits. Wrapping is supported. It's designed for small strings as it only
430 * writes a single byte per iteration. Returns the number of characters copied
431 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
432 * will only modify the buffer upon success. In all cases, the contents are
433 * copied prior to reporting an error, so that the destination at least
434 * contains a valid but truncated string.
435 */
436static inline int bi_istput(struct buffer *b, const struct ist ist)
437{
Willy Tarreaubc59f352018-06-15 13:45:17 +0200438 const char *end = b_wrap(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200439 struct ist r = ist;
440 char *p;
441
Willy Tarreaueac52592018-06-15 13:59:36 +0200442 if (r.len > (size_t)b_room(b))
Willy Tarreaue5676e72017-09-22 15:47:51 +0200443 return r.len < b->size ? 0 : -1;
444
Willy Tarreaubc59f352018-06-15 13:45:17 +0200445 p = b_tail(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200446 b->i += r.len;
447 while (r.len--) {
448 *p++ = *r.ptr++;
449 if (unlikely(p == end))
Willy Tarreau591d4452018-06-15 17:21:00 +0200450 p = b_orig(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200451 }
452 return ist.len;
453}
454
455
Willy Tarreaubc59f352018-06-15 13:45:17 +0200456/* injects string <ist> at the tail of output buffer <b> provided that it
Willy Tarreaue5676e72017-09-22 15:47:51 +0200457 * fits. Input data is assumed not to exist and will silently be overwritten.
458 * Wrapping is supported. It's designed for small strings as it only writes a
459 * single byte per iteration. Returns the number of characters copied (ist.len),
460 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
461 * modify the buffer upon success. In all cases, the contents are copied prior
462 * to reporting an error, so that the destination at least contains a valid
463 * but truncated string.
464 */
465static inline int bo_istput(struct buffer *b, const struct ist ist)
466{
Willy Tarreaubc59f352018-06-15 13:45:17 +0200467 const char *end = b_wrap(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200468 struct ist r = ist;
469 char *p;
470
Willy Tarreaueac52592018-06-15 13:59:36 +0200471 if (r.len > (size_t)b_room(b))
Willy Tarreaue5676e72017-09-22 15:47:51 +0200472 return r.len < b->size ? 0 : -1;
473
Willy Tarreaubc59f352018-06-15 13:45:17 +0200474 p = b_tail(b);
475 b->p = b_peek(b, b->o + r.len);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200476 b->o += r.len;
Willy Tarreaue5676e72017-09-22 15:47:51 +0200477 while (r.len--) {
478 *p++ = *r.ptr++;
479 if (unlikely(p == end))
Willy Tarreau591d4452018-06-15 17:21:00 +0200480 p = b_orig(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200481 }
482 return ist.len;
483}
484
485
Willy Tarreauc7e42382012-08-24 19:22:53 +0200486#endif /* _COMMON_BUFFER_H */
487
488/*
489 * Local variables:
490 * c-indent-level: 8
491 * c-basic-offset: 8
492 * End:
493 */