Willy Tarreau | 41806d1 | 2018-07-11 09:39:05 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | a1bd1fa | 2019-03-29 17:26:33 +0100 | [diff] [blame] | 31 | #include <inttypes.h> |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 32 | #include <string.h> |
Willy Tarreau | a7280a1 | 2018-11-26 19:41:40 +0100 | [diff] [blame] | 33 | #include <unistd.h> |
Willy Tarreau | 506a29a | 2018-07-18 10:07:58 +0200 | [diff] [blame] | 34 | |
Willy Tarreau | 41806d1 | 2018-07-11 09:39:05 +0200 | [diff] [blame] | 35 | /* Structure defining a buffer's head */ |
| 36 | struct buffer { |
Willy Tarreau | 506a29a | 2018-07-18 10:07:58 +0200 | [diff] [blame] | 37 | size_t size; /* buffer size in bytes */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 38 | 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 Tarreau | 41806d1 | 2018-07-11 09:39:05 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 43 | /* A buffer may be in 3 different states : |
| 44 | * - unallocated : size == 0, area == 0 (b_is_null() is true) |
Olivier Houchard | 203d735 | 2019-01-29 19:10:02 +0100 | [diff] [blame] | 45 | * - waiting : size == 0, area != 0 (b_is_null() is true) |
| 46 | * - allocated : size > 0, area > 0 (b_is_null() is false) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 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 Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 56 | |
| 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 Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 64 | /* b_is_null() : returns true if (and only if) the buffer is not yet allocated |
Olivier Houchard | 203d735 | 2019-01-29 19:10:02 +0100 | [diff] [blame] | 65 | * and thus has an empty size. Its pointer may then be anything, including NULL |
| 66 | * (unallocated) or an invalid pointer such as (char*)1 (allocation pending). |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 67 | */ |
| 68 | static inline int b_is_null(const struct buffer *buf) |
| 69 | { |
Olivier Houchard | 203d735 | 2019-01-29 19:10:02 +0100 | [diff] [blame] | 70 | return buf->size == 0; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 73 | /* b_orig() : returns the pointer to the origin of the storage, which is the |
| 74 | * location of byte at offset zero. This is mostly used by functions which |
| 75 | * handle the wrapping by themselves. |
| 76 | */ |
| 77 | static inline char *b_orig(const struct buffer *b) |
| 78 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 79 | return b->area; |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* b_size() : returns the size of the buffer. */ |
| 83 | static inline size_t b_size(const struct buffer *b) |
| 84 | { |
| 85 | return b->size; |
| 86 | } |
| 87 | |
| 88 | /* b_wrap() : returns the pointer to the wrapping position of the buffer area, |
| 89 | * which is by definition the first byte not part of the buffer. |
| 90 | */ |
| 91 | static inline char *b_wrap(const struct buffer *b) |
| 92 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 93 | return b->area + b->size; |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* b_data() : returns the number of bytes present in the buffer. */ |
| 97 | static inline size_t b_data(const struct buffer *b) |
| 98 | { |
Willy Tarreau | bd1dba8 | 2018-07-10 10:43:27 +0200 | [diff] [blame] | 99 | return b->data; |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* b_room() : returns the amount of room left in the buffer */ |
| 103 | static inline size_t b_room(const struct buffer *b) |
| 104 | { |
| 105 | return b->size - b_data(b); |
| 106 | } |
| 107 | |
| 108 | /* b_full() : returns true if the buffer is full. */ |
| 109 | static inline size_t b_full(const struct buffer *b) |
| 110 | { |
| 111 | return !b_room(b); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /* b_stop() : returns the pointer to the byte following the end of the buffer, |
| 116 | * which may be out of the buffer if the buffer ends on the last byte of the |
| 117 | * area. |
| 118 | */ |
| 119 | static inline size_t __b_stop_ofs(const struct buffer *b) |
| 120 | { |
Willy Tarreau | bd1dba8 | 2018-07-10 10:43:27 +0200 | [diff] [blame] | 121 | return b->head + b->data; |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static inline const char *__b_stop(const struct buffer *b) |
| 125 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 126 | return b_orig(b) + __b_stop_ofs(b); |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static inline size_t b_stop_ofs(const struct buffer *b) |
| 130 | { |
| 131 | size_t stop = __b_stop_ofs(b); |
| 132 | |
| 133 | if (stop > b->size) |
| 134 | stop -= b->size; |
| 135 | return stop; |
| 136 | } |
| 137 | |
| 138 | static inline const char *b_stop(const struct buffer *b) |
| 139 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 140 | return b_orig(b) + b_stop_ofs(b); |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | /* b_peek() : returns a pointer to the data at position <ofs> relative to the |
| 145 | * head of the buffer. Will typically point to input data if called with the |
| 146 | * amount of output data. The wrapped versions will only support wrapping once |
| 147 | * before the beginning or after the end. |
| 148 | */ |
| 149 | static inline size_t __b_peek_ofs(const struct buffer *b, size_t ofs) |
| 150 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 151 | return b->head + ofs; |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static inline char *__b_peek(const struct buffer *b, size_t ofs) |
| 155 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 156 | return b_orig(b) + __b_peek_ofs(b, ofs); |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static inline size_t b_peek_ofs(const struct buffer *b, size_t ofs) |
| 160 | { |
| 161 | size_t ret = __b_peek_ofs(b, ofs); |
| 162 | |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 163 | if (ret >= b->size) |
| 164 | ret -= b->size; |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static inline char *b_peek(const struct buffer *b, size_t ofs) |
| 170 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 171 | return b_orig(b) + b_peek_ofs(b, ofs); |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
| 175 | /* b_head() : returns the pointer to the buffer's head, which is the location |
| 176 | * of the next byte to be dequeued. Note that for buffers of size zero, the |
| 177 | * returned pointer may be outside of the buffer or even invalid. |
| 178 | */ |
| 179 | static inline size_t __b_head_ofs(const struct buffer *b) |
| 180 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 181 | return b->head; |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static inline char *__b_head(const struct buffer *b) |
| 185 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 186 | return b_orig(b) + __b_head_ofs(b); |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static inline size_t b_head_ofs(const struct buffer *b) |
| 190 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 191 | return __b_head_ofs(b); |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static inline char *b_head(const struct buffer *b) |
| 195 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 196 | return __b_head(b); |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
| 200 | /* b_tail() : returns the pointer to the tail of the buffer, which is the |
| 201 | * location of the first byte where it is possible to enqueue new data. Note |
| 202 | * that for buffers of size zero, the returned pointer may be outside of the |
| 203 | * buffer or even invalid. |
| 204 | */ |
| 205 | static inline size_t __b_tail_ofs(const struct buffer *b) |
| 206 | { |
| 207 | return __b_peek_ofs(b, b_data(b)); |
| 208 | } |
| 209 | |
| 210 | static inline char *__b_tail(const struct buffer *b) |
| 211 | { |
| 212 | return __b_peek(b, b_data(b)); |
| 213 | } |
| 214 | |
| 215 | static inline size_t b_tail_ofs(const struct buffer *b) |
| 216 | { |
| 217 | return b_peek_ofs(b, b_data(b)); |
| 218 | } |
| 219 | |
| 220 | static inline char *b_tail(const struct buffer *b) |
| 221 | { |
| 222 | return b_peek(b, b_data(b)); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /* b_next() : for an absolute pointer <p> or a relative offset <o> pointing to |
| 227 | * a valid location within buffer <b>, returns either the absolute pointer or |
| 228 | * the relative offset pointing to the next byte, which usually is at (p + 1) |
| 229 | * unless p reaches the wrapping point and wrapping is needed. |
| 230 | */ |
| 231 | static inline size_t b_next_ofs(const struct buffer *b, size_t o) |
| 232 | { |
| 233 | o++; |
| 234 | if (o == b->size) |
| 235 | o = 0; |
| 236 | return o; |
| 237 | } |
| 238 | |
| 239 | static inline char *b_next(const struct buffer *b, const char *p) |
| 240 | { |
| 241 | p++; |
| 242 | if (p == b_wrap(b)) |
| 243 | p = b_orig(b); |
| 244 | return (char *)p; |
| 245 | } |
| 246 | |
| 247 | /* b_dist() : returns the distance between two pointers, taking into account |
| 248 | * the ability to wrap around the buffer's end. The operation is not defined if |
| 249 | * either of the pointers does not belong to the buffer or if their distance is |
| 250 | * greater than the buffer's size. |
| 251 | */ |
| 252 | static inline size_t b_dist(const struct buffer *b, const char *from, const char *to) |
| 253 | { |
| 254 | ssize_t dist = to - from; |
| 255 | |
| 256 | dist += dist < 0 ? b_size(b) : 0; |
| 257 | return dist; |
| 258 | } |
| 259 | |
| 260 | /* b_almost_full() : returns 1 if the buffer uses at least 3/4 of its capacity, |
| 261 | * otherwise zero. Buffers of size zero are considered full. |
| 262 | */ |
| 263 | static inline int b_almost_full(const struct buffer *b) |
| 264 | { |
| 265 | return b_data(b) >= b_size(b) * 3 / 4; |
| 266 | } |
| 267 | |
| 268 | /* b_space_wraps() : returns non-zero only if the buffer's free space wraps : |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 269 | * [ |xxxx| ] => yes |
| 270 | * [xxxx| ] => no |
| 271 | * [ |xxxx] => no |
| 272 | * [xxxx| |xxxx] => no |
| 273 | * [xxxxxxxxxx|xxxxxxxxxxx] => no |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 274 | * |
| 275 | * So the only case where the buffer does not wrap is when there's data either |
| 276 | * at the beginning or at the end of the buffer. Thus we have this : |
| 277 | * - if (head <= 0) ==> doesn't wrap |
| 278 | * - if (tail >= size) ==> doesn't wrap |
| 279 | * - otherwise wraps |
| 280 | */ |
| 281 | static inline int b_space_wraps(const struct buffer *b) |
| 282 | { |
| 283 | if ((ssize_t)__b_head_ofs(b) <= 0) |
| 284 | return 0; |
| 285 | if (__b_tail_ofs(b) >= b_size(b)) |
| 286 | return 0; |
| 287 | return 1; |
| 288 | } |
| 289 | |
Willy Tarreau | 7194d3c | 2018-06-06 16:55:45 +0200 | [diff] [blame] | 290 | /* b_contig_data() : returns the amount of data that can contiguously be read |
| 291 | * at once starting from a relative offset <start> (which allows to easily |
| 292 | * pre-compute blocks for memcpy). The start point will typically contain the |
| 293 | * amount of past data already returned by a previous call to this function. |
| 294 | */ |
| 295 | static inline size_t b_contig_data(const struct buffer *b, size_t start) |
| 296 | { |
| 297 | size_t data = b_wrap(b) - b_peek(b, start); |
| 298 | size_t limit = b_data(b) - start; |
| 299 | |
| 300 | if (data > limit) |
| 301 | data = limit; |
| 302 | return data; |
| 303 | } |
| 304 | |
Willy Tarreau | e4d5a03 | 2018-06-07 18:58:07 +0200 | [diff] [blame] | 305 | /* b_contig_space() : returns the amount of bytes that can be appended to the |
Willy Tarreau | ab322d4 | 2018-07-20 16:07:42 +0200 | [diff] [blame] | 306 | * buffer at once. We have 8 possible cases : |
| 307 | * |
| 308 | * [____________________] return size |
| 309 | * [______|_____________] return size - tail_ofs |
| 310 | * [XXXXXX|_____________] return size - tail_ofs |
| 311 | * [___|XXXXXX|_________] return size - tail_ofs |
| 312 | * [______________XXXXXX] return head_ofs |
| 313 | * [XXXX|___________|XXX] return head_ofs - tail_ofs |
| 314 | * [XXXXXXXXXX|XXXXXXXXX] return 0 |
| 315 | * [XXXXXXXXXXXXXXXXXXXX] return 0 |
Willy Tarreau | e4d5a03 | 2018-06-07 18:58:07 +0200 | [diff] [blame] | 316 | */ |
| 317 | static inline size_t b_contig_space(const struct buffer *b) |
| 318 | { |
Willy Tarreau | ab322d4 | 2018-07-20 16:07:42 +0200 | [diff] [blame] | 319 | size_t left, right; |
Willy Tarreau | e4d5a03 | 2018-06-07 18:58:07 +0200 | [diff] [blame] | 320 | |
Willy Tarreau | ab322d4 | 2018-07-20 16:07:42 +0200 | [diff] [blame] | 321 | right = b_head_ofs(b); |
Willy Tarreau | e4d5a03 | 2018-06-07 18:58:07 +0200 | [diff] [blame] | 322 | left = right + b_data(b); |
| 323 | |
Willy Tarreau | ab322d4 | 2018-07-20 16:07:42 +0200 | [diff] [blame] | 324 | left = b_size(b) - left; |
| 325 | if ((ssize_t)left <= 0) |
| 326 | left += right; |
| 327 | return left; |
Willy Tarreau | e4d5a03 | 2018-06-07 18:58:07 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Willy Tarreau | 90ed383 | 2018-06-15 14:20:26 +0200 | [diff] [blame] | 330 | /* b_getblk() : gets one full block of data at once from a buffer, starting |
| 331 | * from offset <offset> after the buffer's head, and limited to no more than |
| 332 | * <len> bytes. The caller is responsible for ensuring that neither <offset> |
| 333 | * nor <offset>+<len> exceed the total number of bytes available in the buffer. |
| 334 | * Return values : |
| 335 | * >0 : number of bytes read, equal to requested size. |
| 336 | * =0 : not enough data available. <blk> is left undefined. |
| 337 | * The buffer is left unaffected. |
| 338 | */ |
| 339 | static inline size_t b_getblk(const struct buffer *buf, char *blk, size_t len, size_t offset) |
| 340 | { |
| 341 | size_t firstblock; |
| 342 | |
| 343 | if (len + offset > b_data(buf)) |
| 344 | return 0; |
| 345 | |
| 346 | firstblock = b_wrap(buf) - b_head(buf); |
| 347 | if (firstblock > offset) { |
| 348 | if (firstblock >= len + offset) { |
| 349 | memcpy(blk, b_head(buf) + offset, len); |
| 350 | return len; |
| 351 | } |
| 352 | |
| 353 | memcpy(blk, b_head(buf) + offset, firstblock - offset); |
| 354 | memcpy(blk + firstblock - offset, b_orig(buf), len - firstblock + offset); |
| 355 | return len; |
| 356 | } |
| 357 | |
| 358 | memcpy(blk, b_orig(buf) + offset - firstblock, len); |
| 359 | return len; |
| 360 | } |
| 361 | |
Willy Tarreau | a1f78fb | 2018-06-14 14:38:11 +0200 | [diff] [blame] | 362 | /* b_getblk_nc() : gets one or two blocks of data at once from a buffer, |
| 363 | * starting from offset <ofs> after the beginning of its output, and limited to |
| 364 | * no more than <max> bytes. The caller is responsible for ensuring that |
| 365 | * neither <ofs> nor <ofs>+<max> exceed the total number of bytes available in |
| 366 | * the buffer. Return values : |
| 367 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 368 | * =0 : not enough data available. <blk*> are left undefined. |
| 369 | * The buffer is left unaffected. Unused buffers are left in an undefined state. |
| 370 | */ |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 371 | static 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 Tarreau | a1f78fb | 2018-06-14 14:38:11 +0200 | [diff] [blame] | 372 | { |
| 373 | size_t l1; |
| 374 | |
| 375 | if (!max) |
| 376 | return 0; |
| 377 | |
| 378 | *blk1 = b_peek(buf, ofs); |
| 379 | l1 = b_wrap(buf) - *blk1; |
| 380 | if (l1 < max) { |
| 381 | *len1 = l1; |
| 382 | *len2 = max - l1; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 383 | *blk2 = b_orig(buf); |
Willy Tarreau | a1f78fb | 2018-06-14 14:38:11 +0200 | [diff] [blame] | 384 | return 2; |
| 385 | } |
| 386 | *len1 = max; |
| 387 | return 1; |
| 388 | } |
| 389 | |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 390 | |
| 391 | /*********************************************/ |
| 392 | /* Functions used to modify the buffer state */ |
| 393 | /*********************************************/ |
| 394 | |
| 395 | /* b_reset() : resets a buffer. The size is not touched. */ |
| 396 | static inline void b_reset(struct buffer *b) |
| 397 | { |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 398 | b->head = 0; |
Willy Tarreau | bd1dba8 | 2018-07-10 10:43:27 +0200 | [diff] [blame] | 399 | b->data = 0; |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 400 | } |
Willy Tarreau | 41806d1 | 2018-07-11 09:39:05 +0200 | [diff] [blame] | 401 | |
Olivier Houchard | 09138ec | 2018-06-28 19:17:38 +0200 | [diff] [blame] | 402 | /* b_sub() : decreases the buffer length by <count> */ |
| 403 | static inline void b_sub(struct buffer *b, size_t count) |
| 404 | { |
Willy Tarreau | bd1dba8 | 2018-07-10 10:43:27 +0200 | [diff] [blame] | 405 | b->data -= count; |
Olivier Houchard | 09138ec | 2018-06-28 19:17:38 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* b_add() : increase the buffer length by <count> */ |
| 409 | static inline void b_add(struct buffer *b, size_t count) |
| 410 | { |
Willy Tarreau | bd1dba8 | 2018-07-10 10:43:27 +0200 | [diff] [blame] | 411 | b->data += count; |
Olivier Houchard | 09138ec | 2018-06-28 19:17:38 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Olivier Houchard | a04e40d | 2018-06-28 19:10:25 +0200 | [diff] [blame] | 414 | /* b_set_data() : sets the buffer's length */ |
| 415 | static inline void b_set_data(struct buffer *b, size_t len) |
| 416 | { |
Willy Tarreau | bd1dba8 | 2018-07-10 10:43:27 +0200 | [diff] [blame] | 417 | b->data = len; |
Olivier Houchard | a04e40d | 2018-06-28 19:10:25 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 420 | /* b_del() : skips <del> bytes in a buffer <b>. Covers both the output and the |
| 421 | * input parts so it's up to the caller to know where it plays and that <del> |
| 422 | * is always smaller than the amount of data in the buffer. |
| 423 | */ |
| 424 | static inline void b_del(struct buffer *b, size_t del) |
| 425 | { |
Willy Tarreau | bd1dba8 | 2018-07-10 10:43:27 +0200 | [diff] [blame] | 426 | b->data -= del; |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 427 | b->head += del; |
| 428 | if (b->head >= b->size) |
| 429 | b->head -= b->size; |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Willy Tarreau | f17f19f | 2018-06-15 17:50:15 +0200 | [diff] [blame] | 432 | /* b_realign_if_empty() : realigns a buffer if it's empty */ |
| 433 | static inline void b_realign_if_empty(struct buffer *b) |
| 434 | { |
| 435 | if (!b_data(b)) |
Willy Tarreau | d54a8ce | 2018-06-29 18:42:02 +0200 | [diff] [blame] | 436 | b->head = 0; |
Willy Tarreau | f17f19f | 2018-06-15 17:50:15 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Willy Tarreau | 4cf1300 | 2018-06-06 06:53:15 +0200 | [diff] [blame] | 439 | /* b_slow_realign() : this function realigns a possibly wrapping buffer so that |
| 440 | * the part remaining to be parsed is contiguous and starts at the beginning of |
| 441 | * the buffer and the already parsed output part ends at the end of the buffer. |
| 442 | * This provides the best conditions since it allows the largest inputs to be |
| 443 | * processed at once and ensures that once the output data leaves, the whole |
| 444 | * buffer is available at once. The number of output bytes supposedly present |
| 445 | * at the beginning of the buffer and which need to be moved to the end must be |
| 446 | * passed in <output>. A temporary swap area at least as large as b->size must |
| 447 | * be provided in <swap>. It's up to the caller to ensure <output> is no larger |
| 448 | * than the difference between the whole buffer's length and its input. |
| 449 | */ |
| 450 | static inline void b_slow_realign(struct buffer *b, char *swap, size_t output) |
| 451 | { |
| 452 | size_t block1 = output; |
| 453 | size_t block2 = 0; |
| 454 | |
| 455 | /* process output data in two steps to cover wrapping */ |
| 456 | if (block1 > b_size(b) - b_head_ofs(b)) { |
| 457 | block2 = b_size(b) - b_head_ofs(b); |
| 458 | block1 -= block2; |
| 459 | } |
| 460 | memcpy(swap + b_size(b) - output, b_head(b), block1); |
| 461 | memcpy(swap + b_size(b) - block2, b_orig(b), block2); |
| 462 | |
| 463 | /* process input data in two steps to cover wrapping */ |
| 464 | block1 = b_data(b) - output; |
| 465 | block2 = 0; |
| 466 | |
| 467 | if (block1 > b_tail_ofs(b)) { |
| 468 | block2 = b_tail_ofs(b); |
| 469 | block1 = block1 - block2; |
| 470 | } |
| 471 | memcpy(swap, b_peek(b, output), block1); |
| 472 | memcpy(swap + block1, b_orig(b), block2); |
| 473 | |
| 474 | /* reinject changes into the buffer */ |
| 475 | memcpy(b_orig(b), swap, b_data(b) - output); |
| 476 | memcpy(b_wrap(b) - output, swap + b_size(b) - output, output); |
| 477 | |
Christopher Faulet | ad4e1a4 | 2018-08-06 15:43:12 +0200 | [diff] [blame] | 478 | b->head = (output ? b_size(b) - output : 0); |
Willy Tarreau | 4cf1300 | 2018-06-06 06:53:15 +0200 | [diff] [blame] | 479 | } |
Willy Tarreau | 41806d1 | 2018-07-11 09:39:05 +0200 | [diff] [blame] | 480 | |
Willy Tarreau | 55372f6 | 2018-07-10 10:04:02 +0200 | [diff] [blame] | 481 | /* b_putchar() : tries to append char <c> at the end of buffer <b>. Supports |
| 482 | * wrapping. Data are truncated if buffer is full. |
| 483 | */ |
| 484 | static inline void b_putchr(struct buffer *b, char c) |
| 485 | { |
| 486 | if (b_full(b)) |
| 487 | return; |
| 488 | *b_tail(b) = c; |
Willy Tarreau | bd1dba8 | 2018-07-10 10:43:27 +0200 | [diff] [blame] | 489 | b->data++; |
Willy Tarreau | 55372f6 | 2018-07-10 10:04:02 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Willy Tarreau | f7d0447 | 2018-07-20 16:20:34 +0200 | [diff] [blame] | 492 | /* __b_putblk() : tries to append <len> bytes from block <blk> to the end of |
| 493 | * buffer <b> without checking for free space (it's up to the caller to do it). |
| 494 | * Supports wrapping. It must not be called with len == 0. |
| 495 | */ |
| 496 | static inline void __b_putblk(struct buffer *b, const char *blk, size_t len) |
| 497 | { |
| 498 | size_t half = b_contig_space(b); |
| 499 | |
Willy Tarreau | ec3750c | 2018-09-05 19:00:20 +0200 | [diff] [blame] | 500 | if (half > len) |
| 501 | half = len; |
| 502 | |
Willy Tarreau | f7d0447 | 2018-07-20 16:20:34 +0200 | [diff] [blame] | 503 | memcpy(b_tail(b), blk, half); |
| 504 | |
| 505 | if (len > half) |
| 506 | memcpy(b_peek(b, b_data(b) + half), blk + half, len - half); |
| 507 | b->data += len; |
| 508 | } |
| 509 | |
Willy Tarreau | 55372f6 | 2018-07-10 10:04:02 +0200 | [diff] [blame] | 510 | /* b_putblk() : tries to append block <blk> at the end of buffer <b>. Supports |
| 511 | * wrapping. Data are truncated if buffer is too short. It returns the number |
| 512 | * of bytes copied. |
| 513 | */ |
| 514 | static inline size_t b_putblk(struct buffer *b, const char *blk, size_t len) |
| 515 | { |
Willy Tarreau | 55372f6 | 2018-07-10 10:04:02 +0200 | [diff] [blame] | 516 | if (len > b_room(b)) |
| 517 | len = b_room(b); |
Willy Tarreau | f7d0447 | 2018-07-20 16:20:34 +0200 | [diff] [blame] | 518 | if (len) |
| 519 | __b_putblk(b, blk, len); |
Willy Tarreau | 55372f6 | 2018-07-10 10:04:02 +0200 | [diff] [blame] | 520 | return len; |
| 521 | } |
| 522 | |
Willy Tarreau | f148888 | 2018-07-20 16:24:39 +0200 | [diff] [blame] | 523 | /* b_xfer() : transfers at most <count> bytes from buffer <src> to buffer <dst> |
| 524 | * and returns the number of bytes copied. The bytes are removed from <src> and |
| 525 | * added to <dst>. The caller is responsible for ensuring that <count> is not |
Willy Tarreau | 7999bfb | 2018-07-20 18:58:51 +0200 | [diff] [blame] | 526 | * larger than b_room(dst). Whenever possible (if the destination is empty and |
| 527 | * at least as much as the source was requested), the buffers are simply |
| 528 | * swapped instead of copied. |
Willy Tarreau | f148888 | 2018-07-20 16:24:39 +0200 | [diff] [blame] | 529 | */ |
| 530 | static inline size_t b_xfer(struct buffer *dst, struct buffer *src, size_t count) |
| 531 | { |
| 532 | size_t ret, block1, block2; |
| 533 | |
| 534 | ret = 0; |
| 535 | if (!count) |
| 536 | goto leave; |
| 537 | |
| 538 | ret = b_data(src); |
| 539 | if (!ret) |
| 540 | goto leave; |
| 541 | |
| 542 | if (ret > count) |
| 543 | ret = count; |
Willy Tarreau | 7999bfb | 2018-07-20 18:58:51 +0200 | [diff] [blame] | 544 | else if (!b_data(dst)) { |
| 545 | /* zero copy is possible by just swapping buffers */ |
| 546 | struct buffer tmp = *dst; |
| 547 | *dst = *src; |
| 548 | *src = tmp; |
| 549 | goto leave; |
| 550 | } |
Willy Tarreau | f148888 | 2018-07-20 16:24:39 +0200 | [diff] [blame] | 551 | |
| 552 | block1 = b_contig_data(src, 0); |
| 553 | if (block1 > ret) |
| 554 | block1 = ret; |
| 555 | block2 = ret - block1; |
| 556 | |
| 557 | if (block1) |
| 558 | __b_putblk(dst, b_head(src), block1); |
| 559 | |
| 560 | if (block2) |
| 561 | __b_putblk(dst, b_peek(src, block1), block2); |
| 562 | |
| 563 | b_del(src, ret); |
| 564 | leave: |
| 565 | return ret; |
| 566 | } |
| 567 | |
Willy Tarreau | f48919a | 2018-12-22 19:19:50 +0100 | [diff] [blame] | 568 | /* Moves <len> bytes from absolute position <src> of buffer <b> by <shift> |
| 569 | * bytes, while supporting wrapping of both the source and the destination. |
| 570 | * The position is relative to the buffer's origin and may overlap with the |
| 571 | * target position. The <shift>'s absolute value must be strictly lower than |
| 572 | * the buffer's size. The main purpose is to aggregate data block during |
| 573 | * parsing while removing unused delimiters. The buffer's length is not |
| 574 | * modified, and the caller must take care of size adjustments and holes by |
| 575 | * itself. |
| 576 | */ |
| 577 | static inline void b_move(const struct buffer *b, size_t src, size_t len, ssize_t shift) |
| 578 | { |
| 579 | char *orig = b_orig(b); |
| 580 | size_t size = b_size(b); |
| 581 | size_t dst = src + size + shift; |
| 582 | size_t cnt; |
| 583 | |
| 584 | if (dst >= size) |
| 585 | dst -= size; |
| 586 | |
| 587 | if (shift < 0) { |
| 588 | /* copy from left to right */ |
| 589 | for (; (cnt = len); len -= cnt) { |
| 590 | if (cnt > size - src) |
| 591 | cnt = size - src; |
| 592 | if (cnt > size - dst) |
| 593 | cnt = size - dst; |
| 594 | |
| 595 | memmove(orig + dst, orig + src, cnt); |
| 596 | dst += cnt; |
| 597 | src += cnt; |
| 598 | if (dst >= size) |
| 599 | dst -= size; |
| 600 | if (src >= size) |
| 601 | src -= size; |
| 602 | } |
| 603 | } |
| 604 | else if (shift > 0) { |
| 605 | /* copy from right to left */ |
| 606 | for (; (cnt = len); len -= cnt) { |
| 607 | size_t src_end = src + len; |
| 608 | size_t dst_end = dst + len; |
| 609 | |
| 610 | if (dst_end > size) |
| 611 | dst_end -= size; |
| 612 | if (src_end > size) |
| 613 | src_end -= size; |
| 614 | |
| 615 | if (cnt > dst_end) |
| 616 | cnt = dst_end; |
| 617 | if (cnt > src_end) |
| 618 | cnt = src_end; |
| 619 | |
| 620 | memmove(orig + dst_end - cnt, orig + src_end - cnt, cnt); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
Willy Tarreau | e312802 | 2018-07-12 15:55:34 +0200 | [diff] [blame] | 625 | /* b_rep_blk() : writes the block <blk> at position <pos> which must be in |
| 626 | * buffer <b>, and moves the part between <end> and the buffer's tail just |
| 627 | * after the end of the copy of <blk>. This effectively replaces the part |
| 628 | * located between <pos> and <end> with a copy of <blk> of length <len>. The |
| 629 | * buffer's length is automatically updated. This is used to replace a block |
| 630 | * with another one inside a buffer. The shift value (positive or negative) is |
| 631 | * returned. If there's no space left, the move is not done. If <len> is null, |
| 632 | * the <blk> pointer is allowed to be null, in order to erase a block. |
| 633 | */ |
| 634 | static inline int b_rep_blk(struct buffer *b, char *pos, char *end, const char *blk, size_t len) |
| 635 | { |
| 636 | int delta; |
| 637 | |
| 638 | delta = len - (end - pos); |
| 639 | |
Olivier Houchard | 363c745 | 2018-09-26 15:09:58 +0200 | [diff] [blame] | 640 | if (__b_tail(b) + delta > b_wrap(b)) |
Willy Tarreau | e312802 | 2018-07-12 15:55:34 +0200 | [diff] [blame] | 641 | return 0; /* no space left */ |
| 642 | |
| 643 | if (b_data(b) && |
| 644 | b_tail(b) + delta > b_head(b) && |
| 645 | b_head(b) >= b_tail(b)) |
| 646 | return 0; /* no space left before wrapping data */ |
| 647 | |
| 648 | /* first, protect the end of the buffer */ |
| 649 | memmove(end + delta, end, b_tail(b) - end); |
| 650 | |
| 651 | /* now, copy blk over pos */ |
| 652 | if (len) |
| 653 | memcpy(pos, blk, len); |
| 654 | |
| 655 | b_add(b, delta); |
| 656 | b_realign_if_empty(b); |
| 657 | |
| 658 | return delta; |
| 659 | } |
| 660 | |
Willy Tarreau | 55372f6 | 2018-07-10 10:04:02 +0200 | [diff] [blame] | 661 | |
Willy Tarreau | 41806d1 | 2018-07-11 09:39:05 +0200 | [diff] [blame] | 662 | #endif /* _COMMON_BUF_H */ |
| 663 | |
| 664 | /* |
| 665 | * Local variables: |
| 666 | * c-indent-level: 8 |
| 667 | * c-basic-offset: 8 |
| 668 | * End: |
| 669 | */ |