blob: 648e7b635d1d0856d8777d3e61b78a55fc0e9af5 [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 a subtract */
65static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
66{
67 if (ptr < buf->data)
68 ptr += buf->size;
69 return ptr;
70}
71
72/* Normalizes a pointer after an addition */
73static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
74{
75 if (ptr - buf->size >= buf->data)
76 ptr -= buf->size;
77 return ptr;
78}
79
Willy Tarreauc7e42382012-08-24 19:22:53 +020080/* Normalizes a pointer which is supposed to be relative to the beginning of a
81 * buffer, so that wrapping is correctly handled. The intent is to use this
82 * when increasing a pointer. Note that the wrapping test is only performed
83 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
84 * otherwise an invalid pointer might be returned.
85 */
86static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
87{
88 if (ptr < buf->data)
89 ptr += buf->size;
90 else if (ptr - buf->size >= buf->data)
91 ptr -= buf->size;
92 return ptr;
93}
94
95/* Returns the distance between two pointers, taking into account the ability
96 * to wrap around the buffer's end.
97 */
98static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
99{
100 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200101
102 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200103 return count;
104}
105
106/* returns the amount of pending bytes in the buffer. It is the amount of bytes
107 * that is not scheduled to be sent.
108 */
109static inline int buffer_pending(const struct buffer *buf)
110{
111 return buf->i;
112}
113
Willy Tarreauc7e42382012-08-24 19:22:53 +0200114/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
115static inline int buffer_almost_full(const struct buffer *buf)
116{
Willy Tarreau4428a292014-11-28 20:54:13 +0100117 if (buf == &buf_empty)
118 return 0;
119
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200120 return b_almost_full(buf);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200121}
122
123/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
124 * call this function with remaining data waiting to be sent (o > 0). The
125 * caller must ensure that <n> is smaller than the actual buffer's length.
126 * This is mainly used to remove empty lines at the beginning of a request
127 * or a response.
128 */
129static inline void bi_fast_delete(struct buffer *buf, int n)
130{
131 buf->i -= n;
132 buf->p += n;
133}
134
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200135/* Schedule all remaining buffer data to be sent. ->o is not touched if it
136 * already covers those data. That permits doing a flush even after a forward,
137 * although not recommended.
138 */
139static inline void buffer_flush(struct buffer *buf)
140{
141 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
142 buf->o += buf->i;
143 buf->i = 0;
144}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200145
Willy Tarreauaf819352012-08-27 22:08:00 +0200146/* This function writes the string <str> at position <pos> which must be in
147 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
148 * (l, r, lr) are updated to be valid after the shift. the shift value
149 * (positive or negative) is returned. If there's no space left, the move is
150 * not done. The function does not adjust ->o because it does not make sense
151 * to use it on data scheduled to be sent.
152 */
153static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
154{
155 return buffer_replace2(b, pos, end, str, strlen(str));
156}
157
Willy Tarreau8c89c202012-09-28 16:02:48 +0200158/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
159 * Data are truncated if buffer is full.
160 */
161static inline void bo_putchr(struct buffer *b, char c)
162{
Willy Tarreaueac52592018-06-15 13:59:36 +0200163 if (b_data(b) == b->size)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200164 return;
Willy Tarreau271e2a52018-07-09 10:31:30 +0200165 *b_tail(b) = c;
Willy Tarreaubc59f352018-06-15 13:45:17 +0200166 b->p = b_peek(b, b->o + 1);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200167 b->o++;
168}
169
170/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100171 * Data are truncated if buffer is too short. It returns the number of bytes
172 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200173 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100174static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200175{
Willy Tarreaueac52592018-06-15 13:59:36 +0200176 int cur_len = b_data(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200177 int half;
178
179 if (len > b->size - cur_len)
180 len = (b->size - cur_len);
181 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100182 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200183
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200184 half = b_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200185 if (half > len)
186 half = len;
187
188 memcpy(b->p, blk, half);
Willy Tarreaubc59f352018-06-15 13:45:17 +0200189 b->p = b_peek(b, b->o + half);
190 b->o += half;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200191 if (len > half) {
Christopher Fauletb2b27942018-02-26 10:47:03 +0100192 memcpy(b->p, blk + half, len - half);
Willy Tarreaubc59f352018-06-15 13:45:17 +0200193 b->p = b_peek(b, b->o + len - half);
194 b->o += len - half;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200195 }
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100196 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200197}
198
199/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100200 * Data are truncated if buffer is too short. It returns the number of bytes
201 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200202 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100203static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200204{
205 return bo_putblk(b, str, strlen(str));
206}
207
208/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100209 * Data are truncated if buffer is too short. It returns the number of bytes
210 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200211 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100212static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200213{
214 return bo_putblk(b, chk->str, chk->len);
215}
216
Willy Tarreau145746c2017-10-26 15:26:17 +0200217/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
218 * Data are truncated if buffer is full.
219 */
220static inline void bi_putchr(struct buffer *b, char c)
221{
Willy Tarreaueac52592018-06-15 13:59:36 +0200222 if (b_data(b) == b->size)
Willy Tarreau145746c2017-10-26 15:26:17 +0200223 return;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200224 *b_tail(b) = c;
Willy Tarreau145746c2017-10-26 15:26:17 +0200225 b->i++;
226}
227
228/* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping.
229 * Data are truncated if buffer is too short. It returns the number of bytes
230 * copied.
231 */
232static inline int bi_putblk(struct buffer *b, const char *blk, int len)
233{
Willy Tarreaueac52592018-06-15 13:59:36 +0200234 int cur_len = b_data(b);
Willy Tarreau145746c2017-10-26 15:26:17 +0200235 int half;
236
237 if (len > b->size - cur_len)
238 len = (b->size - cur_len);
239 if (!len)
240 return 0;
241
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200242 half = b_contig_space(b);
Willy Tarreau145746c2017-10-26 15:26:17 +0200243 if (half > len)
244 half = len;
245
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200246 memcpy(b_tail(b), blk, half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200247 if (len > half)
Willy Tarreaubc59f352018-06-15 13:45:17 +0200248 memcpy(b_peek(b, b->o + b->i + half), blk + half, len - half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200249 b->i += len;
250 return len;
251}
252
253/* Tries to copy string <str> into input data at buffer <b>. Supports wrapping.
254 * Data are truncated if buffer is too short. It returns the number of bytes
255 * copied.
256 */
257static inline int bi_putstr(struct buffer *b, const char *str)
258{
259 return bi_putblk(b, str, strlen(str));
260}
261
262/* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping.
263 * Data are truncated if buffer is too short. It returns the number of bytes
264 * copied.
265 */
266static inline int bi_putchk(struct buffer *b, const struct chunk *chk)
267{
268 return bi_putblk(b, chk->str, chk->len);
269}
270
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100271/* Allocates a buffer and replaces *buf with this buffer. If no memory is
272 * available, &buf_wanted is used instead. No control is made to check if *buf
273 * already pointed to another buffer. The allocated buffer is returned, or
274 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100275 */
276static inline struct buffer *b_alloc(struct buffer **buf)
277{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100278 struct buffer *b;
279
280 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100281 b = pool_alloc_dirty(pool_head_buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100282 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100283 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100284 b_reset(b);
285 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100286 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100287 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100288}
289
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100290/* Allocates a buffer and replaces *buf with this buffer. If no memory is
291 * available, &buf_wanted is used instead. No control is made to check if *buf
292 * already pointed to another buffer. The allocated buffer is returned, or
293 * NULL in case no memory is available. The difference with b_alloc() is that
294 * this function only picks from the pool and never calls malloc(), so it can
295 * fail even if some memory is available.
296 */
297static inline struct buffer *b_alloc_fast(struct buffer **buf)
298{
299 struct buffer *b;
300
301 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100302 b = pool_get_first(pool_head_buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100303 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100304 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100305 b_reset(b);
306 *buf = b;
307 }
308 return b;
309}
310
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100311/* Releases buffer *buf (no check of emptiness) */
312static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100313{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100314 pool_free(pool_head_buffer, *buf);
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100315}
316
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100317/* Releases buffer *buf if allocated. */
318static inline void b_drop(struct buffer **buf)
319{
320 if (!(*buf)->size)
321 return;
322 __b_drop(buf);
323}
324
325/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
326static inline void b_free(struct buffer **buf)
327{
328 b_drop(buf);
329 *buf = &buf_empty;
330}
331
Willy Tarreauf4718e82014-12-02 13:54:01 +0100332/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
333 * there are still at least <margin> buffers available in the pool after this
334 * allocation so that we don't leave the pool in a condition where a session or
335 * a response buffer could not be allocated anymore, resulting in a deadlock.
336 * This means that we sometimes need to try to allocate extra entries even if
337 * only one buffer is needed.
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100338 *
339 * We need to lock the pool here to be sure to have <margin> buffers available
340 * after the allocation, regardless how many threads that doing it in the same
341 * time. So, we use internal and lockless memory functions (prefixed with '__').
Willy Tarreauf4718e82014-12-02 13:54:01 +0100342 */
343static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
344{
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100345 struct buffer *b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100346
347 if ((*buf)->size)
348 return *buf;
349
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100350 *buf = &buf_wanted;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100351#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100352 HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100353#endif
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100354
Willy Tarreauf4718e82014-12-02 13:54:01 +0100355 /* fast path */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100356 if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) {
357 b = __pool_get_first(pool_head_buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100358 if (likely(b)) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100359#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100360 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100361#endif
Willy Tarreaubafbe012017-11-24 17:34:44 +0100362 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100363 b_reset(b);
364 *buf = b;
365 return b;
366 }
367 }
Willy Tarreauf4718e82014-12-02 13:54:01 +0100368
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100369 /* slow path, uses malloc() */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100370 b = __pool_refill_alloc(pool_head_buffer, margin);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100371
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100372#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100373 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100374#endif
Willy Tarreauf4718e82014-12-02 13:54:01 +0100375
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100376 if (b) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100377 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100378 b_reset(b);
379 *buf = b;
380 }
381 return b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100382}
383
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100384
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100385/* Offer a buffer currently belonging to target <from> to whoever needs one.
386 * Any pointer is valid for <from>, including NULL. Its purpose is to avoid
387 * passing a buffer to oneself in case of failed allocations (e.g. need two
388 * buffers, get one, fail, release it and wake up self again). In case of
389 * normal buffer release where it is expected that the caller is not waiting
390 * for a buffer, NULL is fine.
391 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100392void __offer_buffer(void *from, unsigned int threshold);
393
394static inline void offer_buffers(void *from, unsigned int threshold)
395{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100396 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +0200397 if (LIST_ISEMPTY(&buffer_wq)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100398 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100399 return;
Emeric Bruna1dd2432017-06-21 15:42:52 +0200400 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100401 __offer_buffer(from, threshold);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100402 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100403}
404
Willy Tarreau6634b632017-09-22 15:02:54 +0200405/*************************************************************************/
406/* functions used to manipulate strings and blocks with wrapping buffers */
407/*************************************************************************/
408
Willy Tarreaubc59f352018-06-15 13:45:17 +0200409/* returns > 0 if the first <n> characters of buffer <b> starting at offset <o>
410 * relative to the buffer's head match <ist>. (empty strings do match). It is
Willy Tarreau6634b632017-09-22 15:02:54 +0200411 * designed to be use with reasonably small strings (ie matches a single byte
412 * per iteration). This function is usable both with input and output data. To
413 * be used like this depending on what to match :
Willy Tarreaubc59f352018-06-15 13:45:17 +0200414 * - input contents : b_isteq(b, b->o, b->i, ist);
415 * - output contents : b_isteq(b, 0, b->o, ist);
Willy Tarreau6634b632017-09-22 15:02:54 +0200416 * Return value :
417 * >0 : the number of matching bytes
418 * =0 : not enough bytes (or matching of empty string)
419 * <0 : non-matching byte found
420 */
421static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
422{
423 struct ist r = ist;
424 const char *p;
Willy Tarreaubc59f352018-06-15 13:45:17 +0200425 const char *end = b_wrap(b);
Willy Tarreau6634b632017-09-22 15:02:54 +0200426
427 if (n < r.len)
428 return 0;
429
Willy Tarreaubc59f352018-06-15 13:45:17 +0200430 p = b_peek(b, o);
Willy Tarreau6634b632017-09-22 15:02:54 +0200431 while (r.len--) {
432 if (*p++ != *r.ptr++)
433 return -1;
434 if (unlikely(p == end))
435 p = b->data;
436 }
437 return ist.len;
438}
439
Willy Tarreaubc59f352018-06-15 13:45:17 +0200440/* "eats" string <ist> from the head of buffer <b>. Wrapping data is explicitly
441 * supported. It matches a single byte per iteration so strings should remain
442 * reasonably small. Returns :
Willy Tarreau6634b632017-09-22 15:02:54 +0200443 * > 0 : number of bytes matched and eaten
444 * = 0 : not enough bytes (or matching an empty string)
445 * < 0 : non-matching byte found
446 */
Willy Tarreaubc59f352018-06-15 13:45:17 +0200447static inline int b_eat(struct buffer *b, const struct ist ist)
Willy Tarreau6634b632017-09-22 15:02:54 +0200448{
Willy Tarreaubc59f352018-06-15 13:45:17 +0200449 int ret = b_isteq(b, 0, b_data(b), ist);
Willy Tarreau6634b632017-09-22 15:02:54 +0200450 if (ret > 0)
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200451 b_del(b, ret);
Willy Tarreau6634b632017-09-22 15:02:54 +0200452 return ret;
453}
454
Willy Tarreaubc59f352018-06-15 13:45:17 +0200455/* injects string <ist> at the tail of input buffer <b> provided that it
Willy Tarreaue5676e72017-09-22 15:47:51 +0200456 * fits. Wrapping is supported. It's designed for small strings as it only
457 * writes a single byte per iteration. Returns the number of characters copied
458 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
459 * will only modify the buffer upon success. In all cases, the contents are
460 * copied prior to reporting an error, so that the destination at least
461 * contains a valid but truncated string.
462 */
463static inline int bi_istput(struct buffer *b, const struct ist ist)
464{
Willy Tarreaubc59f352018-06-15 13:45:17 +0200465 const char *end = b_wrap(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200466 struct ist r = ist;
467 char *p;
468
Willy Tarreaueac52592018-06-15 13:59:36 +0200469 if (r.len > (size_t)b_room(b))
Willy Tarreaue5676e72017-09-22 15:47:51 +0200470 return r.len < b->size ? 0 : -1;
471
Willy Tarreaubc59f352018-06-15 13:45:17 +0200472 p = b_tail(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200473 b->i += r.len;
474 while (r.len--) {
475 *p++ = *r.ptr++;
476 if (unlikely(p == end))
477 p = b->data;
478 }
479 return ist.len;
480}
481
482
Willy Tarreaubc59f352018-06-15 13:45:17 +0200483/* injects string <ist> at the tail of output buffer <b> provided that it
Willy Tarreaue5676e72017-09-22 15:47:51 +0200484 * fits. Input data is assumed not to exist and will silently be overwritten.
485 * Wrapping is supported. It's designed for small strings as it only writes a
486 * single byte per iteration. Returns the number of characters copied (ist.len),
487 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
488 * modify the buffer upon success. In all cases, the contents are copied prior
489 * to reporting an error, so that the destination at least contains a valid
490 * but truncated string.
491 */
492static inline int bo_istput(struct buffer *b, const struct ist ist)
493{
Willy Tarreaubc59f352018-06-15 13:45:17 +0200494 const char *end = b_wrap(b);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200495 struct ist r = ist;
496 char *p;
497
Willy Tarreaueac52592018-06-15 13:59:36 +0200498 if (r.len > (size_t)b_room(b))
Willy Tarreaue5676e72017-09-22 15:47:51 +0200499 return r.len < b->size ? 0 : -1;
500
Willy Tarreaubc59f352018-06-15 13:45:17 +0200501 p = b_tail(b);
502 b->p = b_peek(b, b->o + r.len);
Willy Tarreaue5676e72017-09-22 15:47:51 +0200503 b->o += r.len;
Willy Tarreaue5676e72017-09-22 15:47:51 +0200504 while (r.len--) {
505 *p++ = *r.ptr++;
506 if (unlikely(p == end))
507 p = b->data;
508 }
509 return ist.len;
510}
511
512
Willy Tarreauc7e42382012-08-24 19:22:53 +0200513#endif /* _COMMON_BUFFER_H */
514
515/*
516 * Local variables:
517 * c-indent-level: 8
518 * c-basic-offset: 8
519 * End:
520 */