blob: b0b4e64e1556cb6877a2ab6815d9d04e5715953e [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>
32
Willy Tarreau41806d12018-07-11 09:39:05 +020033/* Structure defining a buffer's head */
34struct buffer {
Willy Tarreau892f1db2018-07-09 10:55:37 +020035 size_t head; /* start offset of remaining data relative to area */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020036 size_t len; /* length of data after head */
Willy Tarreau506a29a2018-07-18 10:07:58 +020037 size_t size; /* buffer size in bytes */
Willy Tarreau892f1db2018-07-09 10:55:37 +020038 char area[0]; /* <size> bytes of stored data */
Willy Tarreau41806d12018-07-11 09:39:05 +020039};
40
Willy Tarreaubbc68df2018-06-06 14:30:50 +020041
42/***************************************************************************/
43/* Functions used to compute offsets and pointers. Most of them exist in */
44/* both wrapping-safe and unchecked ("__" prefix) variants. Some returning */
45/* a pointer are also provided with an "_ofs" suffix when they return an */
46/* offset relative to the storage area. */
47/***************************************************************************/
48
49/* b_orig() : returns the pointer to the origin of the storage, which is the
50 * location of byte at offset zero. This is mostly used by functions which
51 * handle the wrapping by themselves.
52 */
53static inline char *b_orig(const struct buffer *b)
54{
Willy Tarreau892f1db2018-07-09 10:55:37 +020055 return (char *)b->area;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020056}
57
58/* b_size() : returns the size of the buffer. */
59static inline size_t b_size(const struct buffer *b)
60{
61 return b->size;
62}
63
64/* b_wrap() : returns the pointer to the wrapping position of the buffer area,
65 * which is by definition the first byte not part of the buffer.
66 */
67static inline char *b_wrap(const struct buffer *b)
68{
Willy Tarreau892f1db2018-07-09 10:55:37 +020069 return (char *)b->area + b->size;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020070}
71
72/* b_data() : returns the number of bytes present in the buffer. */
73static inline size_t b_data(const struct buffer *b)
74{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020075 return b->len;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020076}
77
78/* b_room() : returns the amount of room left in the buffer */
79static inline size_t b_room(const struct buffer *b)
80{
81 return b->size - b_data(b);
82}
83
84/* b_full() : returns true if the buffer is full. */
85static inline size_t b_full(const struct buffer *b)
86{
87 return !b_room(b);
88}
89
90
91/* b_stop() : returns the pointer to the byte following the end of the buffer,
92 * which may be out of the buffer if the buffer ends on the last byte of the
93 * area.
94 */
95static inline size_t __b_stop_ofs(const struct buffer *b)
96{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020097 return b->head + b->len;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020098}
99
100static inline const char *__b_stop(const struct buffer *b)
101{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200102 return b_orig(b) + __b_stop_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200103}
104
105static inline size_t b_stop_ofs(const struct buffer *b)
106{
107 size_t stop = __b_stop_ofs(b);
108
109 if (stop > b->size)
110 stop -= b->size;
111 return stop;
112}
113
114static inline const char *b_stop(const struct buffer *b)
115{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200116 return b_orig(b) + b_stop_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200117}
118
119
120/* b_peek() : returns a pointer to the data at position <ofs> relative to the
121 * head of the buffer. Will typically point to input data if called with the
122 * amount of output data. The wrapped versions will only support wrapping once
123 * before the beginning or after the end.
124 */
125static inline size_t __b_peek_ofs(const struct buffer *b, size_t ofs)
126{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200127 return b->head + ofs;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200128}
129
130static inline char *__b_peek(const struct buffer *b, size_t ofs)
131{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200132 return b_orig(b) + __b_peek_ofs(b, ofs);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200133}
134
135static inline size_t b_peek_ofs(const struct buffer *b, size_t ofs)
136{
137 size_t ret = __b_peek_ofs(b, ofs);
138
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200139 if (ret >= b->size)
140 ret -= b->size;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200141
142 return ret;
143}
144
145static inline char *b_peek(const struct buffer *b, size_t ofs)
146{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200147 return b_orig(b) + b_peek_ofs(b, ofs);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200148}
149
150
151/* b_head() : returns the pointer to the buffer's head, which is the location
152 * of the next byte to be dequeued. Note that for buffers of size zero, the
153 * returned pointer may be outside of the buffer or even invalid.
154 */
155static inline size_t __b_head_ofs(const struct buffer *b)
156{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200157 return b->head;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200158}
159
160static inline char *__b_head(const struct buffer *b)
161{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200162 return b_orig(b) + __b_head_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200163}
164
165static inline size_t b_head_ofs(const struct buffer *b)
166{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200167 return __b_head_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200168}
169
170static inline char *b_head(const struct buffer *b)
171{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200172 return __b_head(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200173}
174
175
176/* b_tail() : returns the pointer to the tail of the buffer, which is the
177 * location of the first byte where it is possible to enqueue new data. Note
178 * that for buffers of size zero, the returned pointer may be outside of the
179 * buffer or even invalid.
180 */
181static inline size_t __b_tail_ofs(const struct buffer *b)
182{
183 return __b_peek_ofs(b, b_data(b));
184}
185
186static inline char *__b_tail(const struct buffer *b)
187{
188 return __b_peek(b, b_data(b));
189}
190
191static inline size_t b_tail_ofs(const struct buffer *b)
192{
193 return b_peek_ofs(b, b_data(b));
194}
195
196static inline char *b_tail(const struct buffer *b)
197{
198 return b_peek(b, b_data(b));
199}
200
201
202/* b_next() : for an absolute pointer <p> or a relative offset <o> pointing to
203 * a valid location within buffer <b>, returns either the absolute pointer or
204 * the relative offset pointing to the next byte, which usually is at (p + 1)
205 * unless p reaches the wrapping point and wrapping is needed.
206 */
207static inline size_t b_next_ofs(const struct buffer *b, size_t o)
208{
209 o++;
210 if (o == b->size)
211 o = 0;
212 return o;
213}
214
215static inline char *b_next(const struct buffer *b, const char *p)
216{
217 p++;
218 if (p == b_wrap(b))
219 p = b_orig(b);
220 return (char *)p;
221}
222
223/* b_dist() : returns the distance between two pointers, taking into account
224 * the ability to wrap around the buffer's end. The operation is not defined if
225 * either of the pointers does not belong to the buffer or if their distance is
226 * greater than the buffer's size.
227 */
228static inline size_t b_dist(const struct buffer *b, const char *from, const char *to)
229{
230 ssize_t dist = to - from;
231
232 dist += dist < 0 ? b_size(b) : 0;
233 return dist;
234}
235
236/* b_almost_full() : returns 1 if the buffer uses at least 3/4 of its capacity,
237 * otherwise zero. Buffers of size zero are considered full.
238 */
239static inline int b_almost_full(const struct buffer *b)
240{
241 return b_data(b) >= b_size(b) * 3 / 4;
242}
243
244/* b_space_wraps() : returns non-zero only if the buffer's free space wraps :
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200245 * [ |xxxx| ] => yes
246 * [xxxx| ] => no
247 * [ |xxxx] => no
248 * [xxxx| |xxxx] => no
249 * [xxxxxxxxxx|xxxxxxxxxxx] => no
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200250 *
251 * So the only case where the buffer does not wrap is when there's data either
252 * at the beginning or at the end of the buffer. Thus we have this :
253 * - if (head <= 0) ==> doesn't wrap
254 * - if (tail >= size) ==> doesn't wrap
255 * - otherwise wraps
256 */
257static inline int b_space_wraps(const struct buffer *b)
258{
259 if ((ssize_t)__b_head_ofs(b) <= 0)
260 return 0;
261 if (__b_tail_ofs(b) >= b_size(b))
262 return 0;
263 return 1;
264}
265
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200266/* b_contig_data() : returns the amount of data that can contiguously be read
267 * at once starting from a relative offset <start> (which allows to easily
268 * pre-compute blocks for memcpy). The start point will typically contain the
269 * amount of past data already returned by a previous call to this function.
270 */
271static inline size_t b_contig_data(const struct buffer *b, size_t start)
272{
273 size_t data = b_wrap(b) - b_peek(b, start);
274 size_t limit = b_data(b) - start;
275
276 if (data > limit)
277 data = limit;
278 return data;
279}
280
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200281/* b_contig_space() : returns the amount of bytes that can be appended to the
282 * buffer at once.
283 */
284static inline size_t b_contig_space(const struct buffer *b)
285{
286 const char *left, *right;
287
288 right = b_head(b);
289 left = right + b_data(b);
290
291 if (left >= b_wrap(b))
292 left -= b_size(b);
293 else
294 right = b_wrap(b);
295
296 return right - left;
297}
298
Willy Tarreau90ed3832018-06-15 14:20:26 +0200299/* b_getblk() : gets one full block of data at once from a buffer, starting
300 * from offset <offset> after the buffer's head, and limited to no more than
301 * <len> bytes. The caller is responsible for ensuring that neither <offset>
302 * nor <offset>+<len> exceed the total number of bytes available in the buffer.
303 * Return values :
304 * >0 : number of bytes read, equal to requested size.
305 * =0 : not enough data available. <blk> is left undefined.
306 * The buffer is left unaffected.
307 */
308static inline size_t b_getblk(const struct buffer *buf, char *blk, size_t len, size_t offset)
309{
310 size_t firstblock;
311
312 if (len + offset > b_data(buf))
313 return 0;
314
315 firstblock = b_wrap(buf) - b_head(buf);
316 if (firstblock > offset) {
317 if (firstblock >= len + offset) {
318 memcpy(blk, b_head(buf) + offset, len);
319 return len;
320 }
321
322 memcpy(blk, b_head(buf) + offset, firstblock - offset);
323 memcpy(blk + firstblock - offset, b_orig(buf), len - firstblock + offset);
324 return len;
325 }
326
327 memcpy(blk, b_orig(buf) + offset - firstblock, len);
328 return len;
329}
330
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200331/* b_getblk_nc() : gets one or two blocks of data at once from a buffer,
332 * starting from offset <ofs> after the beginning of its output, and limited to
333 * no more than <max> bytes. The caller is responsible for ensuring that
334 * neither <ofs> nor <ofs>+<max> exceed the total number of bytes available in
335 * the buffer. Return values :
336 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
337 * =0 : not enough data available. <blk*> are left undefined.
338 * The buffer is left unaffected. Unused buffers are left in an undefined state.
339 */
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200340static 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 +0200341{
342 size_t l1;
343
344 if (!max)
345 return 0;
346
347 *blk1 = b_peek(buf, ofs);
348 l1 = b_wrap(buf) - *blk1;
349 if (l1 < max) {
350 *len1 = l1;
351 *len2 = max - l1;
Willy Tarreau591d4452018-06-15 17:21:00 +0200352 *blk2 = b_orig(buf);
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200353 return 2;
354 }
355 *len1 = max;
356 return 1;
357}
358
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200359
360/*********************************************/
361/* Functions used to modify the buffer state */
362/*********************************************/
363
364/* b_reset() : resets a buffer. The size is not touched. */
365static inline void b_reset(struct buffer *b)
366{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200367 b->head = 0;
368 b->len = 0;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200369}
Willy Tarreau41806d12018-07-11 09:39:05 +0200370
Olivier Houchard09138ec2018-06-28 19:17:38 +0200371/* b_sub() : decreases the buffer length by <count> */
372static inline void b_sub(struct buffer *b, size_t count)
373{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200374 b->len -= count;
Olivier Houchard09138ec2018-06-28 19:17:38 +0200375}
376
377/* b_add() : increase the buffer length by <count> */
378static inline void b_add(struct buffer *b, size_t count)
379{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200380 b->len += count;
Olivier Houchard09138ec2018-06-28 19:17:38 +0200381}
382
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200383/* b_set_data() : sets the buffer's length */
384static inline void b_set_data(struct buffer *b, size_t len)
385{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200386 b->len = len;
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200387}
388
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200389/* b_del() : skips <del> bytes in a buffer <b>. Covers both the output and the
390 * input parts so it's up to the caller to know where it plays and that <del>
391 * is always smaller than the amount of data in the buffer.
392 */
393static inline void b_del(struct buffer *b, size_t del)
394{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200395 b->len -= del;
396 b->head += del;
397 if (b->head >= b->size)
398 b->head -= b->size;
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200399}
400
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200401/* b_realign_if_empty() : realigns a buffer if it's empty */
402static inline void b_realign_if_empty(struct buffer *b)
403{
404 if (!b_data(b))
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200405 b->head = 0;
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200406}
407
Willy Tarreau4cf13002018-06-06 06:53:15 +0200408/* b_slow_realign() : this function realigns a possibly wrapping buffer so that
409 * the part remaining to be parsed is contiguous and starts at the beginning of
410 * the buffer and the already parsed output part ends at the end of the buffer.
411 * This provides the best conditions since it allows the largest inputs to be
412 * processed at once and ensures that once the output data leaves, the whole
413 * buffer is available at once. The number of output bytes supposedly present
414 * at the beginning of the buffer and which need to be moved to the end must be
415 * passed in <output>. A temporary swap area at least as large as b->size must
416 * be provided in <swap>. It's up to the caller to ensure <output> is no larger
417 * than the difference between the whole buffer's length and its input.
418 */
419static inline void b_slow_realign(struct buffer *b, char *swap, size_t output)
420{
421 size_t block1 = output;
422 size_t block2 = 0;
423
424 /* process output data in two steps to cover wrapping */
425 if (block1 > b_size(b) - b_head_ofs(b)) {
426 block2 = b_size(b) - b_head_ofs(b);
427 block1 -= block2;
428 }
429 memcpy(swap + b_size(b) - output, b_head(b), block1);
430 memcpy(swap + b_size(b) - block2, b_orig(b), block2);
431
432 /* process input data in two steps to cover wrapping */
433 block1 = b_data(b) - output;
434 block2 = 0;
435
436 if (block1 > b_tail_ofs(b)) {
437 block2 = b_tail_ofs(b);
438 block1 = block1 - block2;
439 }
440 memcpy(swap, b_peek(b, output), block1);
441 memcpy(swap + block1, b_orig(b), block2);
442
443 /* reinject changes into the buffer */
444 memcpy(b_orig(b), swap, b_data(b) - output);
445 memcpy(b_wrap(b) - output, swap + b_size(b) - output, output);
446
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200447 b->head = b_size(b) - output;
Willy Tarreau4cf13002018-06-06 06:53:15 +0200448}
Willy Tarreau41806d12018-07-11 09:39:05 +0200449
Willy Tarreau55372f62018-07-10 10:04:02 +0200450/* b_putchar() : tries to append char <c> at the end of buffer <b>. Supports
451 * wrapping. Data are truncated if buffer is full.
452 */
453static inline void b_putchr(struct buffer *b, char c)
454{
455 if (b_full(b))
456 return;
457 *b_tail(b) = c;
458 b->len++;
459}
460
461/* b_putblk() : tries to append block <blk> at the end of buffer <b>. Supports
462 * wrapping. Data are truncated if buffer is too short. It returns the number
463 * of bytes copied.
464 */
465static inline size_t b_putblk(struct buffer *b, const char *blk, size_t len)
466{
467 size_t half;
468
469 if (len > b_room(b))
470 len = b_room(b);
471 if (!len)
472 return 0;
473
474 half = b_contig_space(b);
475 if (half > len)
476 half = len;
477
478 memcpy(b_tail(b), blk, half);
479 b->len += half;
480 if (len > half) {
481 memcpy(b_tail(b), blk + half, len - half);
482 b->len += len - half;
483 }
484 return len;
485}
486
Willy Tarreaue3128022018-07-12 15:55:34 +0200487/* b_rep_blk() : writes the block <blk> at position <pos> which must be in
488 * buffer <b>, and moves the part between <end> and the buffer's tail just
489 * after the end of the copy of <blk>. This effectively replaces the part
490 * located between <pos> and <end> with a copy of <blk> of length <len>. The
491 * buffer's length is automatically updated. This is used to replace a block
492 * with another one inside a buffer. The shift value (positive or negative) is
493 * returned. If there's no space left, the move is not done. If <len> is null,
494 * the <blk> pointer is allowed to be null, in order to erase a block.
495 */
496static inline int b_rep_blk(struct buffer *b, char *pos, char *end, const char *blk, size_t len)
497{
498 int delta;
499
500 delta = len - (end - pos);
501
502 if (b_tail(b) + delta > b_wrap(b))
503 return 0; /* no space left */
504
505 if (b_data(b) &&
506 b_tail(b) + delta > b_head(b) &&
507 b_head(b) >= b_tail(b))
508 return 0; /* no space left before wrapping data */
509
510 /* first, protect the end of the buffer */
511 memmove(end + delta, end, b_tail(b) - end);
512
513 /* now, copy blk over pos */
514 if (len)
515 memcpy(pos, blk, len);
516
517 b_add(b, delta);
518 b_realign_if_empty(b);
519
520 return delta;
521}
522
Willy Tarreau55372f62018-07-10 10:04:02 +0200523
Willy Tarreau41806d12018-07-11 09:39:05 +0200524#endif /* _COMMON_BUF_H */
525
526/*
527 * Local variables:
528 * c-indent-level: 8
529 * c-basic-offset: 8
530 * End:
531 */