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