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