blob: b7a5df31774d514bb8e137c6512024683c7c8bfc [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 {
35 char *p; /* buffer's start pointer, separates in and out data */
Willy Tarreau506a29a2018-07-18 10:07:58 +020036 size_t size; /* buffer size in bytes */
37 size_t i; /* number of input bytes pending for analysis in the buffer */
38 size_t o; /* number of out bytes the sender can consume from this buffer */
Willy Tarreau41806d12018-07-11 09:39:05 +020039 char data[0]; /* <size> bytes of stored data */
40};
41
Willy Tarreaubbc68df2018-06-06 14:30:50 +020042
43/***************************************************************************/
44/* Functions used to compute offsets and pointers. Most of them exist in */
45/* both wrapping-safe and unchecked ("__" prefix) variants. Some returning */
46/* a pointer are also provided with an "_ofs" suffix when they return an */
47/* offset relative to the storage area. */
48/***************************************************************************/
49
50/* b_orig() : returns the pointer to the origin of the storage, which is the
51 * location of byte at offset zero. This is mostly used by functions which
52 * handle the wrapping by themselves.
53 */
54static inline char *b_orig(const struct buffer *b)
55{
56 return (char *)b->data;
57}
58
59/* b_size() : returns the size of the buffer. */
60static inline size_t b_size(const struct buffer *b)
61{
62 return b->size;
63}
64
65/* b_wrap() : returns the pointer to the wrapping position of the buffer area,
66 * which is by definition the first byte not part of the buffer.
67 */
68static inline char *b_wrap(const struct buffer *b)
69{
70 return (char *)b->data + b->size;
71}
72
73/* b_data() : returns the number of bytes present in the buffer. */
74static inline size_t b_data(const struct buffer *b)
75{
76 return b->i + b->o;
77}
78
79/* b_room() : returns the amount of room left in the buffer */
80static inline size_t b_room(const struct buffer *b)
81{
82 return b->size - b_data(b);
83}
84
85/* b_full() : returns true if the buffer is full. */
86static inline size_t b_full(const struct buffer *b)
87{
88 return !b_room(b);
89}
90
91
92/* b_stop() : returns the pointer to the byte following the end of the buffer,
93 * which may be out of the buffer if the buffer ends on the last byte of the
94 * area.
95 */
96static inline size_t __b_stop_ofs(const struct buffer *b)
97{
98 return b->p - b->data + b->i;
99}
100
101static inline const char *__b_stop(const struct buffer *b)
102{
103 return b->p + b->i;
104}
105
106static inline size_t b_stop_ofs(const struct buffer *b)
107{
108 size_t stop = __b_stop_ofs(b);
109
110 if (stop > b->size)
111 stop -= b->size;
112 return stop;
113}
114
115static inline const char *b_stop(const struct buffer *b)
116{
117 return b->data + b_stop_ofs(b);
118}
119
120
121/* b_peek() : returns a pointer to the data at position <ofs> relative to the
122 * head of the buffer. Will typically point to input data if called with the
123 * amount of output data. The wrapped versions will only support wrapping once
124 * before the beginning or after the end.
125 */
126static inline size_t __b_peek_ofs(const struct buffer *b, size_t ofs)
127{
128 return b->p - b->data + ofs - b->o;
129}
130
131static inline char *__b_peek(const struct buffer *b, size_t ofs)
132{
133 return b->p - b->o + ofs;
134}
135
136static inline size_t b_peek_ofs(const struct buffer *b, size_t ofs)
137{
138 size_t ret = __b_peek_ofs(b, ofs);
139
140 if (ret >= b->size) {
141 /* wraps either up or down */
142 if ((ssize_t)ret < 0)
143 ret += b->size;
144 else
145 ret -= b->size;
146 }
147
148 return ret;
149}
150
151static inline char *b_peek(const struct buffer *b, size_t ofs)
152{
153 return (char *)b->data + b_peek_ofs(b, ofs);
154}
155
156
157/* b_head() : returns the pointer to the buffer's head, which is the location
158 * of the next byte to be dequeued. Note that for buffers of size zero, the
159 * returned pointer may be outside of the buffer or even invalid.
160 */
161static inline size_t __b_head_ofs(const struct buffer *b)
162{
163 return __b_peek_ofs(b, 0);
164}
165
166static inline char *__b_head(const struct buffer *b)
167{
168 return __b_peek(b, 0);
169}
170
171static inline size_t b_head_ofs(const struct buffer *b)
172{
173 return b_peek_ofs(b, 0);
174}
175
176static inline char *b_head(const struct buffer *b)
177{
178 return b_peek(b, 0);
179}
180
181
182/* b_tail() : returns the pointer to the tail of the buffer, which is the
183 * location of the first byte where it is possible to enqueue new data. Note
184 * that for buffers of size zero, the returned pointer may be outside of the
185 * buffer or even invalid.
186 */
187static inline size_t __b_tail_ofs(const struct buffer *b)
188{
189 return __b_peek_ofs(b, b_data(b));
190}
191
192static inline char *__b_tail(const struct buffer *b)
193{
194 return __b_peek(b, b_data(b));
195}
196
197static inline size_t b_tail_ofs(const struct buffer *b)
198{
199 return b_peek_ofs(b, b_data(b));
200}
201
202static inline char *b_tail(const struct buffer *b)
203{
204 return b_peek(b, b_data(b));
205}
206
207
208/* b_next() : for an absolute pointer <p> or a relative offset <o> pointing to
209 * a valid location within buffer <b>, returns either the absolute pointer or
210 * the relative offset pointing to the next byte, which usually is at (p + 1)
211 * unless p reaches the wrapping point and wrapping is needed.
212 */
213static inline size_t b_next_ofs(const struct buffer *b, size_t o)
214{
215 o++;
216 if (o == b->size)
217 o = 0;
218 return o;
219}
220
221static inline char *b_next(const struct buffer *b, const char *p)
222{
223 p++;
224 if (p == b_wrap(b))
225 p = b_orig(b);
226 return (char *)p;
227}
228
229/* b_dist() : returns the distance between two pointers, taking into account
230 * the ability to wrap around the buffer's end. The operation is not defined if
231 * either of the pointers does not belong to the buffer or if their distance is
232 * greater than the buffer's size.
233 */
234static inline size_t b_dist(const struct buffer *b, const char *from, const char *to)
235{
236 ssize_t dist = to - from;
237
238 dist += dist < 0 ? b_size(b) : 0;
239 return dist;
240}
241
242/* b_almost_full() : returns 1 if the buffer uses at least 3/4 of its capacity,
243 * otherwise zero. Buffers of size zero are considered full.
244 */
245static inline int b_almost_full(const struct buffer *b)
246{
247 return b_data(b) >= b_size(b) * 3 / 4;
248}
249
250/* b_space_wraps() : returns non-zero only if the buffer's free space wraps :
251 * [ |oooo| ] => yes
252 * [ |iiii| ] => yes
253 * [ |oooo|iiii| ] => yes
254 * [oooo| ] => no
255 * [ |oooo] => no
256 * [iiii| ] => no
257 * [ |iiii] => no
258 * [oooo|iiii| ] => no
259 * [ |oooo|iiii] => no
260 * [iiii| |oooo] => no
261 * [oo|iiii| |oo] => no
262 * [iiii| |oo|ii] => no
263 * [oooooooooo|iiiiiiiiiii] => no
264 * [iiiiiiiiiiiii|oooooooo] => no
265 *
266 * So the only case where the buffer does not wrap is when there's data either
267 * at the beginning or at the end of the buffer. Thus we have this :
268 * - if (head <= 0) ==> doesn't wrap
269 * - if (tail >= size) ==> doesn't wrap
270 * - otherwise wraps
271 */
272static inline int b_space_wraps(const struct buffer *b)
273{
274 if ((ssize_t)__b_head_ofs(b) <= 0)
275 return 0;
276 if (__b_tail_ofs(b) >= b_size(b))
277 return 0;
278 return 1;
279}
280
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200281/* b_contig_data() : returns the amount of data that can contiguously be read
282 * at once starting from a relative offset <start> (which allows to easily
283 * pre-compute blocks for memcpy). The start point will typically contain the
284 * amount of past data already returned by a previous call to this function.
285 */
286static inline size_t b_contig_data(const struct buffer *b, size_t start)
287{
288 size_t data = b_wrap(b) - b_peek(b, start);
289 size_t limit = b_data(b) - start;
290
291 if (data > limit)
292 data = limit;
293 return data;
294}
295
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200296/* b_contig_space() : returns the amount of bytes that can be appended to the
297 * buffer at once.
298 */
299static inline size_t b_contig_space(const struct buffer *b)
300{
301 const char *left, *right;
302
303 right = b_head(b);
304 left = right + b_data(b);
305
306 if (left >= b_wrap(b))
307 left -= b_size(b);
308 else
309 right = b_wrap(b);
310
311 return right - left;
312}
313
Willy Tarreau90ed3832018-06-15 14:20:26 +0200314/* b_getblk() : gets one full block of data at once from a buffer, starting
315 * from offset <offset> after the buffer's head, and limited to no more than
316 * <len> bytes. The caller is responsible for ensuring that neither <offset>
317 * nor <offset>+<len> exceed the total number of bytes available in the buffer.
318 * Return values :
319 * >0 : number of bytes read, equal to requested size.
320 * =0 : not enough data available. <blk> is left undefined.
321 * The buffer is left unaffected.
322 */
323static inline size_t b_getblk(const struct buffer *buf, char *blk, size_t len, size_t offset)
324{
325 size_t firstblock;
326
327 if (len + offset > b_data(buf))
328 return 0;
329
330 firstblock = b_wrap(buf) - b_head(buf);
331 if (firstblock > offset) {
332 if (firstblock >= len + offset) {
333 memcpy(blk, b_head(buf) + offset, len);
334 return len;
335 }
336
337 memcpy(blk, b_head(buf) + offset, firstblock - offset);
338 memcpy(blk + firstblock - offset, b_orig(buf), len - firstblock + offset);
339 return len;
340 }
341
342 memcpy(blk, b_orig(buf) + offset - firstblock, len);
343 return len;
344}
345
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200346/* b_getblk_nc() : gets one or two blocks of data at once from a buffer,
347 * starting from offset <ofs> after the beginning of its output, and limited to
348 * no more than <max> bytes. The caller is responsible for ensuring that
349 * neither <ofs> nor <ofs>+<max> exceed the total number of bytes available in
350 * the buffer. Return values :
351 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
352 * =0 : not enough data available. <blk*> are left undefined.
353 * The buffer is left unaffected. Unused buffers are left in an undefined state.
354 */
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200355static 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 +0200356{
357 size_t l1;
358
359 if (!max)
360 return 0;
361
362 *blk1 = b_peek(buf, ofs);
363 l1 = b_wrap(buf) - *blk1;
364 if (l1 < max) {
365 *len1 = l1;
366 *len2 = max - l1;
367 *blk2 = buf->data;
368 return 2;
369 }
370 *len1 = max;
371 return 1;
372}
373
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200374
375/*********************************************/
376/* Functions used to modify the buffer state */
377/*********************************************/
378
379/* b_reset() : resets a buffer. The size is not touched. */
380static inline void b_reset(struct buffer *b)
381{
382 b->o = 0;
383 b->i = 0;
384 b->p = b_orig(b);
385}
Willy Tarreau41806d12018-07-11 09:39:05 +0200386
Olivier Houchard09138ec2018-06-28 19:17:38 +0200387/* b_sub() : decreases the buffer length by <count> */
388static inline void b_sub(struct buffer *b, size_t count)
389{
390 b->i -= count;
391}
392
393/* b_add() : increase the buffer length by <count> */
394static inline void b_add(struct buffer *b, size_t count)
395{
396 b->i += count;
397}
398
399/* bo_add() : increase the buffer output and length by <count>
400 * (LEGACY API)
401 */
402static inline void bo_add(struct buffer *b, size_t count)
403{
404 b->o += count;
405}
406
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200407/* b_set_data() : sets the buffer's length */
408static inline void b_set_data(struct buffer *b, size_t len)
409{
410 if (len >= b->o)
411 b->i = len - b->o;
412 else {
413 b->o = len;
414 b->i = 0;
415 }
416}
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{
424 if (del <= b->o) {
425 b->o -= del;
426 del = 0;
427 }
428 if (del) {
429 b->p = b_peek(b, del);
430 b->i -= del;
431 del = 0;
432 }
433}
434
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200435/* b_realign_if_empty() : realigns a buffer if it's empty */
436static inline void b_realign_if_empty(struct buffer *b)
437{
438 if (!b_data(b))
439 b->p = b->data;
440}
441
Willy Tarreau4cf13002018-06-06 06:53:15 +0200442/* b_slow_realign() : this function realigns a possibly wrapping buffer so that
443 * the part remaining to be parsed is contiguous and starts at the beginning of
444 * the buffer and the already parsed output part ends at the end of the buffer.
445 * This provides the best conditions since it allows the largest inputs to be
446 * processed at once and ensures that once the output data leaves, the whole
447 * buffer is available at once. The number of output bytes supposedly present
448 * at the beginning of the buffer and which need to be moved to the end must be
449 * passed in <output>. A temporary swap area at least as large as b->size must
450 * be provided in <swap>. It's up to the caller to ensure <output> is no larger
451 * than the difference between the whole buffer's length and its input.
452 */
453static inline void b_slow_realign(struct buffer *b, char *swap, size_t output)
454{
455 size_t block1 = output;
456 size_t block2 = 0;
457
458 /* process output data in two steps to cover wrapping */
459 if (block1 > b_size(b) - b_head_ofs(b)) {
460 block2 = b_size(b) - b_head_ofs(b);
461 block1 -= block2;
462 }
463 memcpy(swap + b_size(b) - output, b_head(b), block1);
464 memcpy(swap + b_size(b) - block2, b_orig(b), block2);
465
466 /* process input data in two steps to cover wrapping */
467 block1 = b_data(b) - output;
468 block2 = 0;
469
470 if (block1 > b_tail_ofs(b)) {
471 block2 = b_tail_ofs(b);
472 block1 = block1 - block2;
473 }
474 memcpy(swap, b_peek(b, output), block1);
475 memcpy(swap + block1, b_orig(b), block2);
476
477 /* reinject changes into the buffer */
478 memcpy(b_orig(b), swap, b_data(b) - output);
479 memcpy(b_wrap(b) - output, swap + b_size(b) - output, output);
480
481 b->p = b->data;
482}
Willy Tarreau41806d12018-07-11 09:39:05 +0200483
484#endif /* _COMMON_BUF_H */
485
486/*
487 * Local variables:
488 * c-indent-level: 8
489 * c-basic-offset: 8
490 * End:
491 */