blob: d39de7112a1380c43d853dd1b6a3c9d0872eae34 [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 Tarreau506a29a2018-07-18 10:07:58 +020033
Willy Tarreau41806d12018-07-11 09:39:05 +020034/* Structure defining a buffer's head */
35struct buffer {
Willy Tarreau506a29a2018-07-18 10:07:58 +020036 size_t size; /* buffer size in bytes */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020037 char *area; /* points to <size> bytes */
38 size_t data; /* amount of data after head including wrapping */
39 size_t head; /* start offset of remaining data relative to area */
Willy Tarreau41806d12018-07-11 09:39:05 +020040};
41
Willy Tarreauc9fa0482018-07-10 17:43:27 +020042/* A buffer may be in 3 different states :
43 * - unallocated : size == 0, area == 0 (b_is_null() is true)
44 * - waiting : size == 0, area != 0
45 * - allocated : size > 0, area > 0
46 */
47
48/* initializers for certain buffer states. It is important that the NULL buffer
49 * remains the one with all fields initialized to zero so that a calloc() or a
50 * memset() on a struct automatically sets a NULL buffer.
51 */
52#define BUF_NULL ((struct buffer){ })
53#define BUF_WANTED ((struct buffer){ .area = (char *)1 })
54
Willy Tarreaubbc68df2018-06-06 14:30:50 +020055
56/***************************************************************************/
57/* Functions used to compute offsets and pointers. Most of them exist in */
58/* both wrapping-safe and unchecked ("__" prefix) variants. Some returning */
59/* a pointer are also provided with an "_ofs" suffix when they return an */
60/* offset relative to the storage area. */
61/***************************************************************************/
62
Willy Tarreauc9fa0482018-07-10 17:43:27 +020063/* b_is_null() : returns true if (and only if) the buffer is not yet allocated
64 * and thus points to a NULL area.
65 */
66static inline int b_is_null(const struct buffer *buf)
67{
68 return buf->area == NULL;
69}
70
Willy Tarreaubbc68df2018-06-06 14:30:50 +020071/* b_orig() : returns the pointer to the origin of the storage, which is the
72 * location of byte at offset zero. This is mostly used by functions which
73 * handle the wrapping by themselves.
74 */
75static inline char *b_orig(const struct buffer *b)
76{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020077 return b->area;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020078}
79
80/* b_size() : returns the size of the buffer. */
81static inline size_t b_size(const struct buffer *b)
82{
83 return b->size;
84}
85
86/* b_wrap() : returns the pointer to the wrapping position of the buffer area,
87 * which is by definition the first byte not part of the buffer.
88 */
89static inline char *b_wrap(const struct buffer *b)
90{
Willy Tarreauc9fa0482018-07-10 17:43:27 +020091 return b->area + b->size;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020092}
93
94/* b_data() : returns the number of bytes present in the buffer. */
95static inline size_t b_data(const struct buffer *b)
96{
Willy Tarreaubd1dba82018-07-10 10:43:27 +020097 return b->data;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020098}
99
100/* b_room() : returns the amount of room left in the buffer */
101static inline size_t b_room(const struct buffer *b)
102{
103 return b->size - b_data(b);
104}
105
106/* b_full() : returns true if the buffer is full. */
107static inline size_t b_full(const struct buffer *b)
108{
109 return !b_room(b);
110}
111
112
113/* b_stop() : returns the pointer to the byte following the end of the buffer,
114 * which may be out of the buffer if the buffer ends on the last byte of the
115 * area.
116 */
117static inline size_t __b_stop_ofs(const struct buffer *b)
118{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200119 return b->head + b->data;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200120}
121
122static inline const char *__b_stop(const struct buffer *b)
123{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200124 return b_orig(b) + __b_stop_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200125}
126
127static inline size_t b_stop_ofs(const struct buffer *b)
128{
129 size_t stop = __b_stop_ofs(b);
130
131 if (stop > b->size)
132 stop -= b->size;
133 return stop;
134}
135
136static inline const char *b_stop(const struct buffer *b)
137{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200138 return b_orig(b) + b_stop_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200139}
140
141
142/* b_peek() : returns a pointer to the data at position <ofs> relative to the
143 * head of the buffer. Will typically point to input data if called with the
144 * amount of output data. The wrapped versions will only support wrapping once
145 * before the beginning or after the end.
146 */
147static inline size_t __b_peek_ofs(const struct buffer *b, size_t ofs)
148{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200149 return b->head + ofs;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200150}
151
152static inline char *__b_peek(const struct buffer *b, size_t ofs)
153{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200154 return b_orig(b) + __b_peek_ofs(b, ofs);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200155}
156
157static inline size_t b_peek_ofs(const struct buffer *b, size_t ofs)
158{
159 size_t ret = __b_peek_ofs(b, ofs);
160
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200161 if (ret >= b->size)
162 ret -= b->size;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200163
164 return ret;
165}
166
167static inline char *b_peek(const struct buffer *b, size_t ofs)
168{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200169 return b_orig(b) + b_peek_ofs(b, ofs);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200170}
171
172
173/* b_head() : returns the pointer to the buffer's head, which is the location
174 * of the next byte to be dequeued. Note that for buffers of size zero, the
175 * returned pointer may be outside of the buffer or even invalid.
176 */
177static inline size_t __b_head_ofs(const struct buffer *b)
178{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200179 return b->head;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200180}
181
182static inline char *__b_head(const struct buffer *b)
183{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200184 return b_orig(b) + __b_head_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200185}
186
187static inline size_t b_head_ofs(const struct buffer *b)
188{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200189 return __b_head_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200190}
191
192static inline char *b_head(const struct buffer *b)
193{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200194 return __b_head(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200195}
196
197
198/* b_tail() : returns the pointer to the tail of the buffer, which is the
199 * location of the first byte where it is possible to enqueue new data. Note
200 * that for buffers of size zero, the returned pointer may be outside of the
201 * buffer or even invalid.
202 */
203static inline size_t __b_tail_ofs(const struct buffer *b)
204{
205 return __b_peek_ofs(b, b_data(b));
206}
207
208static inline char *__b_tail(const struct buffer *b)
209{
210 return __b_peek(b, b_data(b));
211}
212
213static inline size_t b_tail_ofs(const struct buffer *b)
214{
215 return b_peek_ofs(b, b_data(b));
216}
217
218static inline char *b_tail(const struct buffer *b)
219{
220 return b_peek(b, b_data(b));
221}
222
223
224/* b_next() : for an absolute pointer <p> or a relative offset <o> pointing to
225 * a valid location within buffer <b>, returns either the absolute pointer or
226 * the relative offset pointing to the next byte, which usually is at (p + 1)
227 * unless p reaches the wrapping point and wrapping is needed.
228 */
229static inline size_t b_next_ofs(const struct buffer *b, size_t o)
230{
231 o++;
232 if (o == b->size)
233 o = 0;
234 return o;
235}
236
237static inline char *b_next(const struct buffer *b, const char *p)
238{
239 p++;
240 if (p == b_wrap(b))
241 p = b_orig(b);
242 return (char *)p;
243}
244
245/* b_dist() : returns the distance between two pointers, taking into account
246 * the ability to wrap around the buffer's end. The operation is not defined if
247 * either of the pointers does not belong to the buffer or if their distance is
248 * greater than the buffer's size.
249 */
250static inline size_t b_dist(const struct buffer *b, const char *from, const char *to)
251{
252 ssize_t dist = to - from;
253
254 dist += dist < 0 ? b_size(b) : 0;
255 return dist;
256}
257
258/* b_almost_full() : returns 1 if the buffer uses at least 3/4 of its capacity,
259 * otherwise zero. Buffers of size zero are considered full.
260 */
261static inline int b_almost_full(const struct buffer *b)
262{
263 return b_data(b) >= b_size(b) * 3 / 4;
264}
265
266/* b_space_wraps() : returns non-zero only if the buffer's free space wraps :
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200267 * [ |xxxx| ] => yes
268 * [xxxx| ] => no
269 * [ |xxxx] => no
270 * [xxxx| |xxxx] => no
271 * [xxxxxxxxxx|xxxxxxxxxxx] => no
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200272 *
273 * So the only case where the buffer does not wrap is when there's data either
274 * at the beginning or at the end of the buffer. Thus we have this :
275 * - if (head <= 0) ==> doesn't wrap
276 * - if (tail >= size) ==> doesn't wrap
277 * - otherwise wraps
278 */
279static inline int b_space_wraps(const struct buffer *b)
280{
281 if ((ssize_t)__b_head_ofs(b) <= 0)
282 return 0;
283 if (__b_tail_ofs(b) >= b_size(b))
284 return 0;
285 return 1;
286}
287
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200288/* b_contig_data() : returns the amount of data that can contiguously be read
289 * at once starting from a relative offset <start> (which allows to easily
290 * pre-compute blocks for memcpy). The start point will typically contain the
291 * amount of past data already returned by a previous call to this function.
292 */
293static inline size_t b_contig_data(const struct buffer *b, size_t start)
294{
295 size_t data = b_wrap(b) - b_peek(b, start);
296 size_t limit = b_data(b) - start;
297
298 if (data > limit)
299 data = limit;
300 return data;
301}
302
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200303/* b_contig_space() : returns the amount of bytes that can be appended to the
Willy Tarreauab322d42018-07-20 16:07:42 +0200304 * buffer at once. We have 8 possible cases :
305 *
306 * [____________________] return size
307 * [______|_____________] return size - tail_ofs
308 * [XXXXXX|_____________] return size - tail_ofs
309 * [___|XXXXXX|_________] return size - tail_ofs
310 * [______________XXXXXX] return head_ofs
311 * [XXXX|___________|XXX] return head_ofs - tail_ofs
312 * [XXXXXXXXXX|XXXXXXXXX] return 0
313 * [XXXXXXXXXXXXXXXXXXXX] return 0
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200314 */
315static inline size_t b_contig_space(const struct buffer *b)
316{
Willy Tarreauab322d42018-07-20 16:07:42 +0200317 size_t left, right;
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200318
Willy Tarreauab322d42018-07-20 16:07:42 +0200319 right = b_head_ofs(b);
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200320 left = right + b_data(b);
321
Willy Tarreauab322d42018-07-20 16:07:42 +0200322 left = b_size(b) - left;
323 if ((ssize_t)left <= 0)
324 left += right;
325 return left;
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200326}
327
Willy Tarreau90ed3832018-06-15 14:20:26 +0200328/* b_getblk() : gets one full block of data at once from a buffer, starting
329 * from offset <offset> after the buffer's head, and limited to no more than
330 * <len> bytes. The caller is responsible for ensuring that neither <offset>
331 * nor <offset>+<len> exceed the total number of bytes available in the buffer.
332 * Return values :
333 * >0 : number of bytes read, equal to requested size.
334 * =0 : not enough data available. <blk> is left undefined.
335 * The buffer is left unaffected.
336 */
337static inline size_t b_getblk(const struct buffer *buf, char *blk, size_t len, size_t offset)
338{
339 size_t firstblock;
340
341 if (len + offset > b_data(buf))
342 return 0;
343
344 firstblock = b_wrap(buf) - b_head(buf);
345 if (firstblock > offset) {
346 if (firstblock >= len + offset) {
347 memcpy(blk, b_head(buf) + offset, len);
348 return len;
349 }
350
351 memcpy(blk, b_head(buf) + offset, firstblock - offset);
352 memcpy(blk + firstblock - offset, b_orig(buf), len - firstblock + offset);
353 return len;
354 }
355
356 memcpy(blk, b_orig(buf) + offset - firstblock, len);
357 return len;
358}
359
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200360/* b_getblk_nc() : gets one or two blocks of data at once from a buffer,
361 * starting from offset <ofs> after the beginning of its output, and limited to
362 * no more than <max> bytes. The caller is responsible for ensuring that
363 * neither <ofs> nor <ofs>+<max> exceed the total number of bytes available in
364 * the buffer. Return values :
365 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
366 * =0 : not enough data available. <blk*> are left undefined.
367 * The buffer is left unaffected. Unused buffers are left in an undefined state.
368 */
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200369static 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 +0200370{
371 size_t l1;
372
373 if (!max)
374 return 0;
375
376 *blk1 = b_peek(buf, ofs);
377 l1 = b_wrap(buf) - *blk1;
378 if (l1 < max) {
379 *len1 = l1;
380 *len2 = max - l1;
Willy Tarreau591d4452018-06-15 17:21:00 +0200381 *blk2 = b_orig(buf);
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200382 return 2;
383 }
384 *len1 = max;
385 return 1;
386}
387
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200388
389/*********************************************/
390/* Functions used to modify the buffer state */
391/*********************************************/
392
393/* b_reset() : resets a buffer. The size is not touched. */
394static inline void b_reset(struct buffer *b)
395{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200396 b->head = 0;
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200397 b->data = 0;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200398}
Willy Tarreau41806d12018-07-11 09:39:05 +0200399
Olivier Houchard09138ec2018-06-28 19:17:38 +0200400/* b_sub() : decreases the buffer length by <count> */
401static inline void b_sub(struct buffer *b, size_t count)
402{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200403 b->data -= count;
Olivier Houchard09138ec2018-06-28 19:17:38 +0200404}
405
406/* b_add() : increase the buffer length by <count> */
407static inline void b_add(struct buffer *b, size_t count)
408{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200409 b->data += count;
Olivier Houchard09138ec2018-06-28 19:17:38 +0200410}
411
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200412/* b_set_data() : sets the buffer's length */
413static inline void b_set_data(struct buffer *b, size_t len)
414{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200415 b->data = len;
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200416}
417
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200418/* b_del() : skips <del> bytes in a buffer <b>. Covers both the output and the
419 * input parts so it's up to the caller to know where it plays and that <del>
420 * is always smaller than the amount of data in the buffer.
421 */
422static inline void b_del(struct buffer *b, size_t del)
423{
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200424 b->data -= del;
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200425 b->head += del;
426 if (b->head >= b->size)
427 b->head -= b->size;
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200428}
429
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200430/* b_realign_if_empty() : realigns a buffer if it's empty */
431static inline void b_realign_if_empty(struct buffer *b)
432{
433 if (!b_data(b))
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200434 b->head = 0;
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200435}
436
Willy Tarreau4cf13002018-06-06 06:53:15 +0200437/* b_slow_realign() : this function realigns a possibly wrapping buffer so that
438 * the part remaining to be parsed is contiguous and starts at the beginning of
439 * the buffer and the already parsed output part ends at the end of the buffer.
440 * This provides the best conditions since it allows the largest inputs to be
441 * processed at once and ensures that once the output data leaves, the whole
442 * buffer is available at once. The number of output bytes supposedly present
443 * at the beginning of the buffer and which need to be moved to the end must be
444 * passed in <output>. A temporary swap area at least as large as b->size must
445 * be provided in <swap>. It's up to the caller to ensure <output> is no larger
446 * than the difference between the whole buffer's length and its input.
447 */
448static inline void b_slow_realign(struct buffer *b, char *swap, size_t output)
449{
450 size_t block1 = output;
451 size_t block2 = 0;
452
453 /* process output data in two steps to cover wrapping */
454 if (block1 > b_size(b) - b_head_ofs(b)) {
455 block2 = b_size(b) - b_head_ofs(b);
456 block1 -= block2;
457 }
458 memcpy(swap + b_size(b) - output, b_head(b), block1);
459 memcpy(swap + b_size(b) - block2, b_orig(b), block2);
460
461 /* process input data in two steps to cover wrapping */
462 block1 = b_data(b) - output;
463 block2 = 0;
464
465 if (block1 > b_tail_ofs(b)) {
466 block2 = b_tail_ofs(b);
467 block1 = block1 - block2;
468 }
469 memcpy(swap, b_peek(b, output), block1);
470 memcpy(swap + block1, b_orig(b), block2);
471
472 /* reinject changes into the buffer */
473 memcpy(b_orig(b), swap, b_data(b) - output);
474 memcpy(b_wrap(b) - output, swap + b_size(b) - output, output);
475
Christopher Fauletad4e1a42018-08-06 15:43:12 +0200476 b->head = (output ? b_size(b) - output : 0);
Willy Tarreau4cf13002018-06-06 06:53:15 +0200477}
Willy Tarreau41806d12018-07-11 09:39:05 +0200478
Willy Tarreau55372f62018-07-10 10:04:02 +0200479/* b_putchar() : tries to append char <c> at the end of buffer <b>. Supports
480 * wrapping. Data are truncated if buffer is full.
481 */
482static inline void b_putchr(struct buffer *b, char c)
483{
484 if (b_full(b))
485 return;
486 *b_tail(b) = c;
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200487 b->data++;
Willy Tarreau55372f62018-07-10 10:04:02 +0200488}
489
Willy Tarreauf7d04472018-07-20 16:20:34 +0200490/* __b_putblk() : tries to append <len> bytes from block <blk> to the end of
491 * buffer <b> without checking for free space (it's up to the caller to do it).
492 * Supports wrapping. It must not be called with len == 0.
493 */
494static inline void __b_putblk(struct buffer *b, const char *blk, size_t len)
495{
496 size_t half = b_contig_space(b);
497
Willy Tarreauec3750c2018-09-05 19:00:20 +0200498 if (half > len)
499 half = len;
500
Willy Tarreauf7d04472018-07-20 16:20:34 +0200501 memcpy(b_tail(b), blk, half);
502
503 if (len > half)
504 memcpy(b_peek(b, b_data(b) + half), blk + half, len - half);
505 b->data += len;
506}
507
Willy Tarreau55372f62018-07-10 10:04:02 +0200508/* b_putblk() : tries to append block <blk> at the end of buffer <b>. Supports
509 * wrapping. Data are truncated if buffer is too short. It returns the number
510 * of bytes copied.
511 */
512static inline size_t b_putblk(struct buffer *b, const char *blk, size_t len)
513{
Willy Tarreau55372f62018-07-10 10:04:02 +0200514 if (len > b_room(b))
515 len = b_room(b);
Willy Tarreauf7d04472018-07-20 16:20:34 +0200516 if (len)
517 __b_putblk(b, blk, len);
Willy Tarreau55372f62018-07-10 10:04:02 +0200518 return len;
519}
520
Willy Tarreauf1488882018-07-20 16:24:39 +0200521/* b_xfer() : transfers at most <count> bytes from buffer <src> to buffer <dst>
522 * and returns the number of bytes copied. The bytes are removed from <src> and
523 * added to <dst>. The caller is responsible for ensuring that <count> is not
Willy Tarreau7999bfb2018-07-20 18:58:51 +0200524 * larger than b_room(dst). Whenever possible (if the destination is empty and
525 * at least as much as the source was requested), the buffers are simply
526 * swapped instead of copied.
Willy Tarreauf1488882018-07-20 16:24:39 +0200527 */
528static inline size_t b_xfer(struct buffer *dst, struct buffer *src, size_t count)
529{
530 size_t ret, block1, block2;
531
532 ret = 0;
533 if (!count)
534 goto leave;
535
536 ret = b_data(src);
537 if (!ret)
538 goto leave;
539
540 if (ret > count)
541 ret = count;
Willy Tarreau7999bfb2018-07-20 18:58:51 +0200542 else if (!b_data(dst)) {
543 /* zero copy is possible by just swapping buffers */
544 struct buffer tmp = *dst;
545 *dst = *src;
546 *src = tmp;
547 goto leave;
548 }
Willy Tarreauf1488882018-07-20 16:24:39 +0200549
550 block1 = b_contig_data(src, 0);
551 if (block1 > ret)
552 block1 = ret;
553 block2 = ret - block1;
554
555 if (block1)
556 __b_putblk(dst, b_head(src), block1);
557
558 if (block2)
559 __b_putblk(dst, b_peek(src, block1), block2);
560
561 b_del(src, ret);
562 leave:
563 return ret;
564}
565
Willy Tarreaue3128022018-07-12 15:55:34 +0200566/* b_rep_blk() : writes the block <blk> at position <pos> which must be in
567 * buffer <b>, and moves the part between <end> and the buffer's tail just
568 * after the end of the copy of <blk>. This effectively replaces the part
569 * located between <pos> and <end> with a copy of <blk> of length <len>. The
570 * buffer's length is automatically updated. This is used to replace a block
571 * with another one inside a buffer. The shift value (positive or negative) is
572 * returned. If there's no space left, the move is not done. If <len> is null,
573 * the <blk> pointer is allowed to be null, in order to erase a block.
574 */
575static inline int b_rep_blk(struct buffer *b, char *pos, char *end, const char *blk, size_t len)
576{
577 int delta;
578
579 delta = len - (end - pos);
580
Olivier Houchard363c7452018-09-26 15:09:58 +0200581 if (__b_tail(b) + delta > b_wrap(b))
Willy Tarreaue3128022018-07-12 15:55:34 +0200582 return 0; /* no space left */
583
584 if (b_data(b) &&
585 b_tail(b) + delta > b_head(b) &&
586 b_head(b) >= b_tail(b))
587 return 0; /* no space left before wrapping data */
588
589 /* first, protect the end of the buffer */
590 memmove(end + delta, end, b_tail(b) - end);
591
592 /* now, copy blk over pos */
593 if (len)
594 memcpy(pos, blk, len);
595
596 b_add(b, delta);
597 b_realign_if_empty(b);
598
599 return delta;
600}
601
Willy Tarreau55372f62018-07-10 10:04:02 +0200602
Willy Tarreau41806d12018-07-11 09:39:05 +0200603#endif /* _COMMON_BUF_H */
604
605/*
606 * Local variables:
607 * c-indent-level: 8
608 * c-basic-offset: 8
609 * End:
610 */