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