blob: cc670bf38c91ebf6ec28a58e2fcb7cdaec592513 [file] [log] [blame]
Willy Tarreau41806d12018-07-11 09:39:05 +02001/*
2 * include/common/buf.h
3 * Simple buffer handling.
4 *
5 * Copyright (C) 2000-2018 Willy Tarreau - w@1wt.eu
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef _COMMON_BUF_H
29#define _COMMON_BUF_H
30
Willy Tarreau506a29a2018-07-18 10:07:58 +020031#include <stdint.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020032#include <string.h>
Willy Tarreaua7280a12018-11-26 19:41:40 +010033#include <unistd.h>
Willy Tarreau506a29a2018-07-18 10:07:58 +020034
Willy Tarreau41806d12018-07-11 09:39:05 +020035/* Structure defining a buffer's head */
36struct buffer {
Willy Tarreau506a29a2018-07-18 10:07:58 +020037 size_t size; /* buffer size in bytes */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020038 char *area; /* points to <size> bytes */
39 size_t data; /* amount of data after head including wrapping */
40 size_t head; /* start offset of remaining data relative to area */
Willy Tarreau41806d12018-07-11 09:39:05 +020041};
42
Willy Tarreauc9fa0482018-07-10 17:43:27 +020043/* A buffer may be in 3 different states :
44 * - unallocated : size == 0, area == 0 (b_is_null() is true)
45 * - waiting : size == 0, area != 0
46 * - allocated : size > 0, area > 0
47 */
48
49/* initializers for certain buffer states. It is important that the NULL buffer
50 * remains the one with all fields initialized to zero so that a calloc() or a
51 * memset() on a struct automatically sets a NULL buffer.
52 */
53#define BUF_NULL ((struct buffer){ })
54#define BUF_WANTED ((struct buffer){ .area = (char *)1 })
55
Willy Tarreaubbc68df2018-06-06 14:30:50 +020056
57/***************************************************************************/
58/* Functions used to compute offsets and pointers. Most of them exist in */
59/* both wrapping-safe and unchecked ("__" prefix) variants. Some returning */
60/* a pointer are also provided with an "_ofs" suffix when they return an */
61/* offset relative to the storage area. */
62/***************************************************************************/
63
Willy Tarreauc9fa0482018-07-10 17:43:27 +020064/* b_is_null() : returns true if (and only if) the buffer is not yet allocated
65 * and thus points to a NULL area.
66 */
67static inline int b_is_null(const struct buffer *buf)
68{
69 return buf->area == NULL;
70}
71
Willy Tarreaubbc68df2018-06-06 14:30:50 +020072/* b_orig() : returns the pointer to the origin of the storage, which is the
73 * location of byte at offset zero. This is mostly used by functions which
74 * handle the wrapping by themselves.
75 */
76static inline char *b_orig(const struct buffer *b)
77{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020078 return b->area;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020079}
80
81/* b_size() : returns the size of the buffer. */
82static inline size_t b_size(const struct buffer *b)
83{
84 return b->size;
85}
86
87/* b_wrap() : returns the pointer to the wrapping position of the buffer area,
88 * which is by definition the first byte not part of the buffer.
89 */
90static inline char *b_wrap(const struct buffer *b)
91{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020092 return b->area + b->size;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020093}
94
95/* b_data() : returns the number of bytes present in the buffer. */
96static inline size_t b_data(const struct buffer *b)
97{
Willy Tarreaubd1dba82018-07-10 10:43:27 +020098 return b->data;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020099}
100
101/* b_room() : returns the amount of room left in the buffer */
102static inline size_t b_room(const struct buffer *b)
103{
104 return b->size - b_data(b);
105}
106
107/* b_full() : returns true if the buffer is full. */
108static inline size_t b_full(const struct buffer *b)
109{
110 return !b_room(b);
111}
112
113
114/* b_stop() : returns the pointer to the byte following the end of the buffer,
115 * which may be out of the buffer if the buffer ends on the last byte of the
116 * area.
117 */
118static inline size_t __b_stop_ofs(const struct buffer *b)
119{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200120 return b->head + b->data;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200121}
122
123static inline const char *__b_stop(const struct buffer *b)
124{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200125 return b_orig(b) + __b_stop_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200126}
127
128static inline size_t b_stop_ofs(const struct buffer *b)
129{
130 size_t stop = __b_stop_ofs(b);
131
132 if (stop > b->size)
133 stop -= b->size;
134 return stop;
135}
136
137static inline const char *b_stop(const struct buffer *b)
138{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200139 return b_orig(b) + b_stop_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200140}
141
142
143/* b_peek() : returns a pointer to the data at position <ofs> relative to the
144 * head of the buffer. Will typically point to input data if called with the
145 * amount of output data. The wrapped versions will only support wrapping once
146 * before the beginning or after the end.
147 */
148static inline size_t __b_peek_ofs(const struct buffer *b, size_t ofs)
149{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200150 return b->head + ofs;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200151}
152
153static inline char *__b_peek(const struct buffer *b, size_t ofs)
154{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200155 return b_orig(b) + __b_peek_ofs(b, ofs);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200156}
157
158static inline size_t b_peek_ofs(const struct buffer *b, size_t ofs)
159{
160 size_t ret = __b_peek_ofs(b, ofs);
161
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200162 if (ret >= b->size)
163 ret -= b->size;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200164
165 return ret;
166}
167
168static inline char *b_peek(const struct buffer *b, size_t ofs)
169{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200170 return b_orig(b) + b_peek_ofs(b, ofs);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200171}
172
173
174/* b_head() : returns the pointer to the buffer's head, which is the location
175 * of the next byte to be dequeued. Note that for buffers of size zero, the
176 * returned pointer may be outside of the buffer or even invalid.
177 */
178static inline size_t __b_head_ofs(const struct buffer *b)
179{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200180 return b->head;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200181}
182
183static inline char *__b_head(const struct buffer *b)
184{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200185 return b_orig(b) + __b_head_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200186}
187
188static inline size_t b_head_ofs(const struct buffer *b)
189{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200190 return __b_head_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200191}
192
193static inline char *b_head(const struct buffer *b)
194{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200195 return __b_head(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200196}
197
198
199/* b_tail() : returns the pointer to the tail of the buffer, which is the
200 * location of the first byte where it is possible to enqueue new data. Note
201 * that for buffers of size zero, the returned pointer may be outside of the
202 * buffer or even invalid.
203 */
204static inline size_t __b_tail_ofs(const struct buffer *b)
205{
206 return __b_peek_ofs(b, b_data(b));
207}
208
209static inline char *__b_tail(const struct buffer *b)
210{
211 return __b_peek(b, b_data(b));
212}
213
214static inline size_t b_tail_ofs(const struct buffer *b)
215{
216 return b_peek_ofs(b, b_data(b));
217}
218
219static inline char *b_tail(const struct buffer *b)
220{
221 return b_peek(b, b_data(b));
222}
223
224
225/* b_next() : for an absolute pointer <p> or a relative offset <o> pointing to
226 * a valid location within buffer <b>, returns either the absolute pointer or
227 * the relative offset pointing to the next byte, which usually is at (p + 1)
228 * unless p reaches the wrapping point and wrapping is needed.
229 */
230static inline size_t b_next_ofs(const struct buffer *b, size_t o)
231{
232 o++;
233 if (o == b->size)
234 o = 0;
235 return o;
236}
237
238static inline char *b_next(const struct buffer *b, const char *p)
239{
240 p++;
241 if (p == b_wrap(b))
242 p = b_orig(b);
243 return (char *)p;
244}
245
246/* b_dist() : returns the distance between two pointers, taking into account
247 * the ability to wrap around the buffer's end. The operation is not defined if
248 * either of the pointers does not belong to the buffer or if their distance is
249 * greater than the buffer's size.
250 */
251static inline size_t b_dist(const struct buffer *b, const char *from, const char *to)
252{
253 ssize_t dist = to - from;
254
255 dist += dist < 0 ? b_size(b) : 0;
256 return dist;
257}
258
259/* b_almost_full() : returns 1 if the buffer uses at least 3/4 of its capacity,
260 * otherwise zero. Buffers of size zero are considered full.
261 */
262static inline int b_almost_full(const struct buffer *b)
263{
264 return b_data(b) >= b_size(b) * 3 / 4;
265}
266
267/* b_space_wraps() : returns non-zero only if the buffer's free space wraps :
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200268 * [ |xxxx| ] => yes
269 * [xxxx| ] => no
270 * [ |xxxx] => no
271 * [xxxx| |xxxx] => no
272 * [xxxxxxxxxx|xxxxxxxxxxx] => no
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200273 *
274 * So the only case where the buffer does not wrap is when there's data either
275 * at the beginning or at the end of the buffer. Thus we have this :
276 * - if (head <= 0) ==> doesn't wrap
277 * - if (tail >= size) ==> doesn't wrap
278 * - otherwise wraps
279 */
280static inline int b_space_wraps(const struct buffer *b)
281{
282 if ((ssize_t)__b_head_ofs(b) <= 0)
283 return 0;
284 if (__b_tail_ofs(b) >= b_size(b))
285 return 0;
286 return 1;
287}
288
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200289/* b_contig_data() : returns the amount of data that can contiguously be read
290 * at once starting from a relative offset <start> (which allows to easily
291 * pre-compute blocks for memcpy). The start point will typically contain the
292 * amount of past data already returned by a previous call to this function.
293 */
294static inline size_t b_contig_data(const struct buffer *b, size_t start)
295{
296 size_t data = b_wrap(b) - b_peek(b, start);
297 size_t limit = b_data(b) - start;
298
299 if (data > limit)
300 data = limit;
301 return data;
302}
303
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200304/* b_contig_space() : returns the amount of bytes that can be appended to the
Willy Tarreauab322d42018-07-20 16:07:42 +0200305 * buffer at once. We have 8 possible cases :
306 *
307 * [____________________] return size
308 * [______|_____________] return size - tail_ofs
309 * [XXXXXX|_____________] return size - tail_ofs
310 * [___|XXXXXX|_________] return size - tail_ofs
311 * [______________XXXXXX] return head_ofs
312 * [XXXX|___________|XXX] return head_ofs - tail_ofs
313 * [XXXXXXXXXX|XXXXXXXXX] return 0
314 * [XXXXXXXXXXXXXXXXXXXX] return 0
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200315 */
316static inline size_t b_contig_space(const struct buffer *b)
317{
Willy Tarreauab322d42018-07-20 16:07:42 +0200318 size_t left, right;
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200319
Willy Tarreauab322d42018-07-20 16:07:42 +0200320 right = b_head_ofs(b);
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200321 left = right + b_data(b);
322
Willy Tarreauab322d42018-07-20 16:07:42 +0200323 left = b_size(b) - left;
324 if ((ssize_t)left <= 0)
325 left += right;
326 return left;
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200327}
328
Willy Tarreau90ed3832018-06-15 14:20:26 +0200329/* b_getblk() : gets one full block of data at once from a buffer, starting
330 * from offset <offset> after the buffer's head, and limited to no more than
331 * <len> bytes. The caller is responsible for ensuring that neither <offset>
332 * nor <offset>+<len> exceed the total number of bytes available in the buffer.
333 * Return values :
334 * >0 : number of bytes read, equal to requested size.
335 * =0 : not enough data available. <blk> is left undefined.
336 * The buffer is left unaffected.
337 */
338static inline size_t b_getblk(const struct buffer *buf, char *blk, size_t len, size_t offset)
339{
340 size_t firstblock;
341
342 if (len + offset > b_data(buf))
343 return 0;
344
345 firstblock = b_wrap(buf) - b_head(buf);
346 if (firstblock > offset) {
347 if (firstblock >= len + offset) {
348 memcpy(blk, b_head(buf) + offset, len);
349 return len;
350 }
351
352 memcpy(blk, b_head(buf) + offset, firstblock - offset);
353 memcpy(blk + firstblock - offset, b_orig(buf), len - firstblock + offset);
354 return len;
355 }
356
357 memcpy(blk, b_orig(buf) + offset - firstblock, len);
358 return len;
359}
360
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200361/* b_getblk_nc() : gets one or two blocks of data at once from a buffer,
362 * starting from offset <ofs> after the beginning of its output, and limited to
363 * no more than <max> bytes. The caller is responsible for ensuring that
364 * neither <ofs> nor <ofs>+<max> exceed the total number of bytes available in
365 * the buffer. Return values :
366 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
367 * =0 : not enough data available. <blk*> are left undefined.
368 * The buffer is left unaffected. Unused buffers are left in an undefined state.
369 */
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200370static inline size_t b_getblk_nc(const struct buffer *buf, const char **blk1, size_t *len1, const char **blk2, size_t *len2, size_t ofs, size_t max)
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200371{
372 size_t l1;
373
374 if (!max)
375 return 0;
376
377 *blk1 = b_peek(buf, ofs);
378 l1 = b_wrap(buf) - *blk1;
379 if (l1 < max) {
380 *len1 = l1;
381 *len2 = max - l1;
Willy Tarreau591d4452018-06-15 17:21:00 +0200382 *blk2 = b_orig(buf);
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200383 return 2;
384 }
385 *len1 = max;
386 return 1;
387}
388
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200389
390/*********************************************/
391/* Functions used to modify the buffer state */
392/*********************************************/
393
394/* b_reset() : resets a buffer. The size is not touched. */
395static inline void b_reset(struct buffer *b)
396{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200397 b->head = 0;
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200398 b->data = 0;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200399}
Willy Tarreau41806d12018-07-11 09:39:05 +0200400
Olivier Houchard09138ec2018-06-28 19:17:38 +0200401/* b_sub() : decreases the buffer length by <count> */
402static inline void b_sub(struct buffer *b, size_t count)
403{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200404 b->data -= count;
Olivier Houchard09138ec2018-06-28 19:17:38 +0200405}
406
407/* b_add() : increase the buffer length by <count> */
408static inline void b_add(struct buffer *b, size_t count)
409{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200410 b->data += count;
Olivier Houchard09138ec2018-06-28 19:17:38 +0200411}
412
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200413/* b_set_data() : sets the buffer's length */
414static inline void b_set_data(struct buffer *b, size_t len)
415{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200416 b->data = len;
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200417}
418
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200419/* b_del() : skips <del> bytes in a buffer <b>. Covers both the output and the
420 * input parts so it's up to the caller to know where it plays and that <del>
421 * is always smaller than the amount of data in the buffer.
422 */
423static inline void b_del(struct buffer *b, size_t del)
424{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200425 b->data -= del;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200426 b->head += del;
427 if (b->head >= b->size)
428 b->head -= b->size;
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200429}
430
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200431/* b_realign_if_empty() : realigns a buffer if it's empty */
432static inline void b_realign_if_empty(struct buffer *b)
433{
434 if (!b_data(b))
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200435 b->head = 0;
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200436}
437
Willy Tarreau4cf13002018-06-06 06:53:15 +0200438/* b_slow_realign() : this function realigns a possibly wrapping buffer so that
439 * the part remaining to be parsed is contiguous and starts at the beginning of
440 * the buffer and the already parsed output part ends at the end of the buffer.
441 * This provides the best conditions since it allows the largest inputs to be
442 * processed at once and ensures that once the output data leaves, the whole
443 * buffer is available at once. The number of output bytes supposedly present
444 * at the beginning of the buffer and which need to be moved to the end must be
445 * passed in <output>. A temporary swap area at least as large as b->size must
446 * be provided in <swap>. It's up to the caller to ensure <output> is no larger
447 * than the difference between the whole buffer's length and its input.
448 */
449static inline void b_slow_realign(struct buffer *b, char *swap, size_t output)
450{
451 size_t block1 = output;
452 size_t block2 = 0;
453
454 /* process output data in two steps to cover wrapping */
455 if (block1 > b_size(b) - b_head_ofs(b)) {
456 block2 = b_size(b) - b_head_ofs(b);
457 block1 -= block2;
458 }
459 memcpy(swap + b_size(b) - output, b_head(b), block1);
460 memcpy(swap + b_size(b) - block2, b_orig(b), block2);
461
462 /* process input data in two steps to cover wrapping */
463 block1 = b_data(b) - output;
464 block2 = 0;
465
466 if (block1 > b_tail_ofs(b)) {
467 block2 = b_tail_ofs(b);
468 block1 = block1 - block2;
469 }
470 memcpy(swap, b_peek(b, output), block1);
471 memcpy(swap + block1, b_orig(b), block2);
472
473 /* reinject changes into the buffer */
474 memcpy(b_orig(b), swap, b_data(b) - output);
475 memcpy(b_wrap(b) - output, swap + b_size(b) - output, output);
476
Christopher Fauletad4e1a42018-08-06 15:43:12 +0200477 b->head = (output ? b_size(b) - output : 0);
Willy Tarreau4cf13002018-06-06 06:53:15 +0200478}
Willy Tarreau41806d12018-07-11 09:39:05 +0200479
Willy Tarreau55372f62018-07-10 10:04:02 +0200480/* b_putchar() : tries to append char <c> at the end of buffer <b>. Supports
481 * wrapping. Data are truncated if buffer is full.
482 */
483static inline void b_putchr(struct buffer *b, char c)
484{
485 if (b_full(b))
486 return;
487 *b_tail(b) = c;
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200488 b->data++;
Willy Tarreau55372f62018-07-10 10:04:02 +0200489}
490
Willy Tarreauf7d04472018-07-20 16:20:34 +0200491/* __b_putblk() : tries to append <len> bytes from block <blk> to the end of
492 * buffer <b> without checking for free space (it's up to the caller to do it).
493 * Supports wrapping. It must not be called with len == 0.
494 */
495static inline void __b_putblk(struct buffer *b, const char *blk, size_t len)
496{
497 size_t half = b_contig_space(b);
498
Willy Tarreauec3750c2018-09-05 19:00:20 +0200499 if (half > len)
500 half = len;
501
Willy Tarreauf7d04472018-07-20 16:20:34 +0200502 memcpy(b_tail(b), blk, half);
503
504 if (len > half)
505 memcpy(b_peek(b, b_data(b) + half), blk + half, len - half);
506 b->data += len;
507}
508
Willy Tarreau55372f62018-07-10 10:04:02 +0200509/* b_putblk() : tries to append block <blk> at the end of buffer <b>. Supports
510 * wrapping. Data are truncated if buffer is too short. It returns the number
511 * of bytes copied.
512 */
513static inline size_t b_putblk(struct buffer *b, const char *blk, size_t len)
514{
Willy Tarreau55372f62018-07-10 10:04:02 +0200515 if (len > b_room(b))
516 len = b_room(b);
Willy Tarreauf7d04472018-07-20 16:20:34 +0200517 if (len)
518 __b_putblk(b, blk, len);
Willy Tarreau55372f62018-07-10 10:04:02 +0200519 return len;
520}
521
Willy Tarreauf1488882018-07-20 16:24:39 +0200522/* b_xfer() : transfers at most <count> bytes from buffer <src> to buffer <dst>
523 * and returns the number of bytes copied. The bytes are removed from <src> and
524 * added to <dst>. The caller is responsible for ensuring that <count> is not
Willy Tarreau7999bfb2018-07-20 18:58:51 +0200525 * larger than b_room(dst). Whenever possible (if the destination is empty and
526 * at least as much as the source was requested), the buffers are simply
527 * swapped instead of copied.
Willy Tarreauf1488882018-07-20 16:24:39 +0200528 */
529static inline size_t b_xfer(struct buffer *dst, struct buffer *src, size_t count)
530{
531 size_t ret, block1, block2;
532
533 ret = 0;
534 if (!count)
535 goto leave;
536
537 ret = b_data(src);
538 if (!ret)
539 goto leave;
540
541 if (ret > count)
542 ret = count;
Willy Tarreau7999bfb2018-07-20 18:58:51 +0200543 else if (!b_data(dst)) {
544 /* zero copy is possible by just swapping buffers */
545 struct buffer tmp = *dst;
546 *dst = *src;
547 *src = tmp;
548 goto leave;
549 }
Willy Tarreauf1488882018-07-20 16:24:39 +0200550
551 block1 = b_contig_data(src, 0);
552 if (block1 > ret)
553 block1 = ret;
554 block2 = ret - block1;
555
556 if (block1)
557 __b_putblk(dst, b_head(src), block1);
558
559 if (block2)
560 __b_putblk(dst, b_peek(src, block1), block2);
561
562 b_del(src, ret);
563 leave:
564 return ret;
565}
566
Willy Tarreaue3128022018-07-12 15:55:34 +0200567/* b_rep_blk() : writes the block <blk> at position <pos> which must be in
568 * buffer <b>, and moves the part between <end> and the buffer's tail just
569 * after the end of the copy of <blk>. This effectively replaces the part
570 * located between <pos> and <end> with a copy of <blk> of length <len>. The
571 * buffer's length is automatically updated. This is used to replace a block
572 * with another one inside a buffer. The shift value (positive or negative) is
573 * returned. If there's no space left, the move is not done. If <len> is null,
574 * the <blk> pointer is allowed to be null, in order to erase a block.
575 */
576static inline int b_rep_blk(struct buffer *b, char *pos, char *end, const char *blk, size_t len)
577{
578 int delta;
579
580 delta = len - (end - pos);
581
Olivier Houchard363c7452018-09-26 15:09:58 +0200582 if (__b_tail(b) + delta > b_wrap(b))
Willy Tarreaue3128022018-07-12 15:55:34 +0200583 return 0; /* no space left */
584
585 if (b_data(b) &&
586 b_tail(b) + delta > b_head(b) &&
587 b_head(b) >= b_tail(b))
588 return 0; /* no space left before wrapping data */
589
590 /* first, protect the end of the buffer */
591 memmove(end + delta, end, b_tail(b) - end);
592
593 /* now, copy blk over pos */
594 if (len)
595 memcpy(pos, blk, len);
596
597 b_add(b, delta);
598 b_realign_if_empty(b);
599
600 return delta;
601}
602
Willy Tarreau55372f62018-07-10 10:04:02 +0200603
Willy Tarreau41806d12018-07-11 09:39:05 +0200604#endif /* _COMMON_BUF_H */
605
606/*
607 * Local variables:
608 * c-indent-level: 8
609 * c-basic-offset: 8
610 * End:
611 */