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