blob: 02a66b36662763a9bc3e7206bbe977af47d0a81e [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 Tarreaud54a8ce2018-06-29 18:42:02 +020035 size_t head; /* start offset of remaining data relative to data */
36 size_t len; /* length of data after head */
Willy Tarreau506a29a2018-07-18 10:07:58 +020037 size_t size; /* buffer size in bytes */
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020038 size_t output; /* TEMPORARY: part of <len> which is to be forwarded */
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{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020076 return b->len;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020077}
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{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +020098 return b->head + b->len;
Willy Tarreaubbc68df2018-06-06 14:30:50 +020099}
100
101static inline const char *__b_stop(const struct buffer *b)
102{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200103 return b_orig(b) + __b_stop_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200104}
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{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200117 return b_orig(b) + b_stop_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200118}
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{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200128 return b->head + ofs;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200129}
130
131static inline char *__b_peek(const struct buffer *b, size_t ofs)
132{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200133 return b_orig(b) + __b_peek_ofs(b, ofs);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200134}
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
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200140 if (ret >= b->size)
141 ret -= b->size;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200142
143 return ret;
144}
145
146static inline char *b_peek(const struct buffer *b, size_t ofs)
147{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200148 return b_orig(b) + b_peek_ofs(b, ofs);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200149}
150
151
152/* b_head() : returns the pointer to the buffer's head, which is the location
153 * of the next byte to be dequeued. Note that for buffers of size zero, the
154 * returned pointer may be outside of the buffer or even invalid.
155 */
156static inline size_t __b_head_ofs(const struct buffer *b)
157{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200158 return b->head;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200159}
160
161static inline char *__b_head(const struct buffer *b)
162{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200163 return b_orig(b) + __b_head_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200164}
165
166static inline size_t b_head_ofs(const struct buffer *b)
167{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200168 return __b_head_ofs(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200169}
170
171static inline char *b_head(const struct buffer *b)
172{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200173 return __b_head(b);
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200174}
175
176
177/* b_tail() : returns the pointer to the tail of the buffer, which is the
178 * location of the first byte where it is possible to enqueue new data. Note
179 * that for buffers of size zero, the returned pointer may be outside of the
180 * buffer or even invalid.
181 */
182static inline size_t __b_tail_ofs(const struct buffer *b)
183{
184 return __b_peek_ofs(b, b_data(b));
185}
186
187static inline char *__b_tail(const struct buffer *b)
188{
189 return __b_peek(b, b_data(b));
190}
191
192static inline size_t b_tail_ofs(const struct buffer *b)
193{
194 return b_peek_ofs(b, b_data(b));
195}
196
197static inline char *b_tail(const struct buffer *b)
198{
199 return b_peek(b, b_data(b));
200}
201
202
203/* b_next() : for an absolute pointer <p> or a relative offset <o> pointing to
204 * a valid location within buffer <b>, returns either the absolute pointer or
205 * the relative offset pointing to the next byte, which usually is at (p + 1)
206 * unless p reaches the wrapping point and wrapping is needed.
207 */
208static inline size_t b_next_ofs(const struct buffer *b, size_t o)
209{
210 o++;
211 if (o == b->size)
212 o = 0;
213 return o;
214}
215
216static inline char *b_next(const struct buffer *b, const char *p)
217{
218 p++;
219 if (p == b_wrap(b))
220 p = b_orig(b);
221 return (char *)p;
222}
223
224/* b_dist() : returns the distance between two pointers, taking into account
225 * the ability to wrap around the buffer's end. The operation is not defined if
226 * either of the pointers does not belong to the buffer or if their distance is
227 * greater than the buffer's size.
228 */
229static inline size_t b_dist(const struct buffer *b, const char *from, const char *to)
230{
231 ssize_t dist = to - from;
232
233 dist += dist < 0 ? b_size(b) : 0;
234 return dist;
235}
236
237/* b_almost_full() : returns 1 if the buffer uses at least 3/4 of its capacity,
238 * otherwise zero. Buffers of size zero are considered full.
239 */
240static inline int b_almost_full(const struct buffer *b)
241{
242 return b_data(b) >= b_size(b) * 3 / 4;
243}
244
245/* b_space_wraps() : returns non-zero only if the buffer's free space wraps :
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200246 * [ |xxxx| ] => yes
247 * [xxxx| ] => no
248 * [ |xxxx] => no
249 * [xxxx| |xxxx] => no
250 * [xxxxxxxxxx|xxxxxxxxxxx] => no
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200251 *
252 * So the only case where the buffer does not wrap is when there's data either
253 * at the beginning or at the end of the buffer. Thus we have this :
254 * - if (head <= 0) ==> doesn't wrap
255 * - if (tail >= size) ==> doesn't wrap
256 * - otherwise wraps
257 */
258static inline int b_space_wraps(const struct buffer *b)
259{
260 if ((ssize_t)__b_head_ofs(b) <= 0)
261 return 0;
262 if (__b_tail_ofs(b) >= b_size(b))
263 return 0;
264 return 1;
265}
266
Willy Tarreau7194d3c2018-06-06 16:55:45 +0200267/* b_contig_data() : returns the amount of data that can contiguously be read
268 * at once starting from a relative offset <start> (which allows to easily
269 * pre-compute blocks for memcpy). The start point will typically contain the
270 * amount of past data already returned by a previous call to this function.
271 */
272static inline size_t b_contig_data(const struct buffer *b, size_t start)
273{
274 size_t data = b_wrap(b) - b_peek(b, start);
275 size_t limit = b_data(b) - start;
276
277 if (data > limit)
278 data = limit;
279 return data;
280}
281
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200282/* b_contig_space() : returns the amount of bytes that can be appended to the
283 * buffer at once.
284 */
285static inline size_t b_contig_space(const struct buffer *b)
286{
287 const char *left, *right;
288
289 right = b_head(b);
290 left = right + b_data(b);
291
292 if (left >= b_wrap(b))
293 left -= b_size(b);
294 else
295 right = b_wrap(b);
296
297 return right - left;
298}
299
Willy Tarreau90ed3832018-06-15 14:20:26 +0200300/* b_getblk() : gets one full block of data at once from a buffer, starting
301 * from offset <offset> after the buffer's head, and limited to no more than
302 * <len> bytes. The caller is responsible for ensuring that neither <offset>
303 * nor <offset>+<len> exceed the total number of bytes available in the buffer.
304 * Return values :
305 * >0 : number of bytes read, equal to requested size.
306 * =0 : not enough data available. <blk> is left undefined.
307 * The buffer is left unaffected.
308 */
309static inline size_t b_getblk(const struct buffer *buf, char *blk, size_t len, size_t offset)
310{
311 size_t firstblock;
312
313 if (len + offset > b_data(buf))
314 return 0;
315
316 firstblock = b_wrap(buf) - b_head(buf);
317 if (firstblock > offset) {
318 if (firstblock >= len + offset) {
319 memcpy(blk, b_head(buf) + offset, len);
320 return len;
321 }
322
323 memcpy(blk, b_head(buf) + offset, firstblock - offset);
324 memcpy(blk + firstblock - offset, b_orig(buf), len - firstblock + offset);
325 return len;
326 }
327
328 memcpy(blk, b_orig(buf) + offset - firstblock, len);
329 return len;
330}
331
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200332/* b_getblk_nc() : gets one or two blocks of data at once from a buffer,
333 * starting from offset <ofs> after the beginning of its output, and limited to
334 * no more than <max> bytes. The caller is responsible for ensuring that
335 * neither <ofs> nor <ofs>+<max> exceed the total number of bytes available in
336 * the buffer. Return values :
337 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
338 * =0 : not enough data available. <blk*> are left undefined.
339 * The buffer is left unaffected. Unused buffers are left in an undefined state.
340 */
Willy Tarreau55f3ce12018-07-18 11:49:27 +0200341static 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 +0200342{
343 size_t l1;
344
345 if (!max)
346 return 0;
347
348 *blk1 = b_peek(buf, ofs);
349 l1 = b_wrap(buf) - *blk1;
350 if (l1 < max) {
351 *len1 = l1;
352 *len2 = max - l1;
Willy Tarreau591d4452018-06-15 17:21:00 +0200353 *blk2 = b_orig(buf);
Willy Tarreaua1f78fb2018-06-14 14:38:11 +0200354 return 2;
355 }
356 *len1 = max;
357 return 1;
358}
359
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200360
361/*********************************************/
362/* Functions used to modify the buffer state */
363/*********************************************/
364
365/* b_reset() : resets a buffer. The size is not touched. */
366static inline void b_reset(struct buffer *b)
367{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200368 b->head = 0;
369 b->len = 0;
370 b->output = 0;
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200371}
Willy Tarreau41806d12018-07-11 09:39:05 +0200372
Olivier Houchard09138ec2018-06-28 19:17:38 +0200373/* b_sub() : decreases the buffer length by <count> */
374static inline void b_sub(struct buffer *b, size_t count)
375{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200376 b->len -= count;
Olivier Houchard09138ec2018-06-28 19:17:38 +0200377}
378
379/* b_add() : increase the buffer length by <count> */
380static inline void b_add(struct buffer *b, size_t count)
381{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200382 b->len += count;
Olivier Houchard09138ec2018-06-28 19:17:38 +0200383}
384
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200385/* b_set_data() : sets the buffer's length */
386static inline void b_set_data(struct buffer *b, size_t len)
387{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200388 if (len < b->output)
389 b->output = len;
390 b->len = len;
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200391}
392
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200393/* b_del() : skips <del> bytes in a buffer <b>. Covers both the output and the
394 * input parts so it's up to the caller to know where it plays and that <del>
395 * is always smaller than the amount of data in the buffer.
396 */
397static inline void b_del(struct buffer *b, size_t del)
398{
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200399 if (del >= b->output)
400 b->output = 0;
401 else
402 b->output -= del;
403 b->len -= del;
404 b->head += del;
405 if (b->head >= b->size)
406 b->head -= b->size;
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200407}
408
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200409/* b_realign_if_empty() : realigns a buffer if it's empty */
410static inline void b_realign_if_empty(struct buffer *b)
411{
412 if (!b_data(b))
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200413 b->head = 0;
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200414}
415
Willy Tarreau4cf13002018-06-06 06:53:15 +0200416/* b_slow_realign() : this function realigns a possibly wrapping buffer so that
417 * the part remaining to be parsed is contiguous and starts at the beginning of
418 * the buffer and the already parsed output part ends at the end of the buffer.
419 * This provides the best conditions since it allows the largest inputs to be
420 * processed at once and ensures that once the output data leaves, the whole
421 * buffer is available at once. The number of output bytes supposedly present
422 * at the beginning of the buffer and which need to be moved to the end must be
423 * passed in <output>. A temporary swap area at least as large as b->size must
424 * be provided in <swap>. It's up to the caller to ensure <output> is no larger
425 * than the difference between the whole buffer's length and its input.
426 */
427static inline void b_slow_realign(struct buffer *b, char *swap, size_t output)
428{
429 size_t block1 = output;
430 size_t block2 = 0;
431
432 /* process output data in two steps to cover wrapping */
433 if (block1 > b_size(b) - b_head_ofs(b)) {
434 block2 = b_size(b) - b_head_ofs(b);
435 block1 -= block2;
436 }
437 memcpy(swap + b_size(b) - output, b_head(b), block1);
438 memcpy(swap + b_size(b) - block2, b_orig(b), block2);
439
440 /* process input data in two steps to cover wrapping */
441 block1 = b_data(b) - output;
442 block2 = 0;
443
444 if (block1 > b_tail_ofs(b)) {
445 block2 = b_tail_ofs(b);
446 block1 = block1 - block2;
447 }
448 memcpy(swap, b_peek(b, output), block1);
449 memcpy(swap + block1, b_orig(b), block2);
450
451 /* reinject changes into the buffer */
452 memcpy(b_orig(b), swap, b_data(b) - output);
453 memcpy(b_wrap(b) - output, swap + b_size(b) - output, output);
454
Willy Tarreaud54a8ce2018-06-29 18:42:02 +0200455 b->head = b_size(b) - output;
Willy Tarreau4cf13002018-06-06 06:53:15 +0200456}
Willy Tarreau41806d12018-07-11 09:39:05 +0200457
458#endif /* _COMMON_BUF_H */
459
460/*
461 * Local variables:
462 * c-indent-level: 8
463 * c-basic-offset: 8
464 * End:
465 */