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