Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/common/buffer.h |
| 3 | * Buffer management definitions, macros and inline functions. |
| 4 | * |
| 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _COMMON_BUFFER_H |
| 23 | #define _COMMON_BUFFER_H |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 29 | #include <common/chunk.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/config.h> |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 31 | #include <common/ist.h> |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 32 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 33 | |
| 34 | |
| 35 | struct buffer { |
| 36 | char *p; /* buffer's start pointer, separates in and out data */ |
| 37 | unsigned int size; /* buffer size in bytes */ |
| 38 | unsigned int i; /* number of input bytes pending for analysis in the buffer */ |
| 39 | unsigned int o; /* number of out bytes the sender can consume from this buffer */ |
| 40 | char data[0]; /* <size> bytes */ |
| 41 | }; |
| 42 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 43 | /* an element of the <buffer_wq> list. It represents an object that need to |
| 44 | * acquire a buffer to continue its process. */ |
| 45 | struct buffer_wait { |
| 46 | void *target; /* The waiting object that should be woken up */ |
| 47 | int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */ |
| 48 | struct list list; /* Next element in the <buffer_wq> list */ |
| 49 | }; |
| 50 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 51 | extern struct pool_head *pool_head_buffer; |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 52 | extern struct buffer buf_empty; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 53 | extern struct buffer buf_wanted; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 54 | extern struct list buffer_wq; |
Willy Tarreau | 53bae85 | 2017-11-26 11:00:37 +0100 | [diff] [blame] | 55 | __decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 56 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 57 | int init_buffer(); |
Christopher Faulet | ad405f1 | 2017-08-29 15:30:11 +0200 | [diff] [blame] | 58 | void deinit_buffer(); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 59 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 60 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 61 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
| 62 | void buffer_slow_realign(struct buffer *buf); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 63 | |
| 64 | /*****************************************************************/ |
| 65 | /* These functions are used to compute various buffer area sizes */ |
| 66 | /*****************************************************************/ |
| 67 | |
| 68 | /* Returns an absolute pointer for a position relative to the current buffer's |
| 69 | * pointer. It is written so that it is optimal when <ofs> is a const. It is |
| 70 | * written as a macro instead of an inline function so that the compiler knows |
| 71 | * when it can optimize out the sign test on <ofs> when passed an unsigned int. |
Willy Tarreau | ce39bfb | 2012-09-22 18:36:29 +0200 | [diff] [blame] | 72 | * Note that callers MUST cast <ofs> to int if they expect negative values. |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 73 | */ |
| 74 | #define b_ptr(b, ofs) \ |
| 75 | ({ \ |
| 76 | char *__ret = (b)->p + (ofs); \ |
| 77 | if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \ |
| 78 | __ret -= (b)->size; \ |
| 79 | else if ((ofs) < 0 && __ret < (b)->data) \ |
| 80 | __ret += (b)->size; \ |
| 81 | __ret; \ |
| 82 | }) |
| 83 | |
Willy Tarreau | 26488ad | 2017-09-19 21:14:08 +0200 | [diff] [blame] | 84 | /* Returns the pointer to the buffer's end (data+size) */ |
| 85 | static inline const char *b_end(const struct buffer *b) |
| 86 | { |
| 87 | return b->data + b->size; |
| 88 | } |
| 89 | |
| 90 | /* Returns the distance between <p> and the buffer's end (data+size) */ |
| 91 | static inline unsigned int b_to_end(const struct buffer *b) |
| 92 | { |
| 93 | return b->data + b->size - b->p; |
| 94 | } |
| 95 | |
Willy Tarreau | 4a6425d | 2017-09-19 14:18:46 +0200 | [diff] [blame] | 96 | /* Skips <del> bytes in a one-way buffer <b> : <p> advances by <del>, <i> |
| 97 | * shrinks by <del> as well, and <o> is left untouched (supposed to be zero). |
| 98 | * The caller is responsible for ensuring that <del> is always smaller than or |
| 99 | * equal to b->i. |
| 100 | */ |
Willy Tarreau | 7f564d2 | 2017-10-18 08:32:12 +0200 | [diff] [blame] | 101 | static inline void bi_del(struct buffer *b, unsigned int del) |
Willy Tarreau | 4a6425d | 2017-09-19 14:18:46 +0200 | [diff] [blame] | 102 | { |
| 103 | b->i -= del; |
| 104 | b->p = b_ptr(b, del); |
| 105 | } |
| 106 | |
Willy Tarreau | 7f564d2 | 2017-10-18 08:32:12 +0200 | [diff] [blame] | 107 | /* Skips <del> bytes from the output of buffer <b> by simply shrinking <o>. |
| 108 | * The caller is responsible for ensuring that <del> is always smaller than or |
| 109 | * equal to b->o. |
| 110 | */ |
| 111 | static inline void bo_del(struct buffer *b, unsigned int del) |
| 112 | { |
| 113 | b->o -= del; |
| 114 | } |
| 115 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 116 | /* Advances the buffer by <adv> bytes, which means that the buffer |
| 117 | * pointer advances, and that as many bytes from in are transferred |
| 118 | * to out. The caller is responsible for ensuring that adv is always |
| 119 | * smaller than or equal to b->i. |
| 120 | */ |
| 121 | static inline void b_adv(struct buffer *b, unsigned int adv) |
| 122 | { |
| 123 | b->i -= adv; |
| 124 | b->o += adv; |
| 125 | b->p = b_ptr(b, adv); |
| 126 | } |
| 127 | |
| 128 | /* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes |
| 129 | * backwards, and that as many bytes from out are moved to in. The caller is |
| 130 | * responsible for ensuring that adv is always smaller than or equal to b->o. |
| 131 | */ |
| 132 | static inline void b_rew(struct buffer *b, unsigned int adv) |
| 133 | { |
| 134 | b->i += adv; |
| 135 | b->o -= adv; |
| 136 | b->p = b_ptr(b, (int)-adv); |
| 137 | } |
| 138 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 139 | /* Returns the start of the input data in a buffer */ |
| 140 | static inline char *bi_ptr(const struct buffer *b) |
| 141 | { |
| 142 | return b->p; |
| 143 | } |
| 144 | |
| 145 | /* Returns the end of the input data in a buffer (pointer to next |
| 146 | * insertion point). |
| 147 | */ |
| 148 | static inline char *bi_end(const struct buffer *b) |
| 149 | { |
| 150 | char *ret = b->p + b->i; |
| 151 | |
| 152 | if (ret >= b->data + b->size) |
| 153 | ret -= b->size; |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | /* Returns the amount of input data that can contiguously be read at once */ |
| 158 | static inline int bi_contig_data(const struct buffer *b) |
| 159 | { |
| 160 | int data = b->data + b->size - b->p; |
| 161 | |
| 162 | if (data > b->i) |
| 163 | data = b->i; |
| 164 | return data; |
| 165 | } |
| 166 | |
| 167 | /* Returns the start of the output data in a buffer */ |
| 168 | static inline char *bo_ptr(const struct buffer *b) |
| 169 | { |
| 170 | char *ret = b->p - b->o; |
| 171 | |
| 172 | if (ret < b->data) |
| 173 | ret += b->size; |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | /* Returns the end of the output data in a buffer */ |
| 178 | static inline char *bo_end(const struct buffer *b) |
| 179 | { |
| 180 | return b->p; |
| 181 | } |
| 182 | |
| 183 | /* Returns the amount of output data that can contiguously be read at once */ |
| 184 | static inline int bo_contig_data(const struct buffer *b) |
| 185 | { |
| 186 | char *beg = b->p - b->o; |
| 187 | |
| 188 | if (beg < b->data) |
| 189 | return b->data - beg; |
| 190 | return b->o; |
| 191 | } |
| 192 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 193 | /* Return the amount of bytes that can be written into the input area at once |
| 194 | * including reserved space which may be overwritten (this is the caller |
| 195 | * responsibility to know if the reserved space is protected or not). |
| 196 | */ |
| 197 | static inline int bi_contig_space(const struct buffer *b) |
| 198 | { |
| 199 | const char *left, *right; |
| 200 | |
Christopher Faulet | a36b311 | 2017-06-13 22:00:22 +0200 | [diff] [blame] | 201 | left = b->p + b->i; |
| 202 | right = b->p - b->o; |
| 203 | if (left >= b->data + b->size) |
| 204 | left -= b->size; |
| 205 | else { |
| 206 | if (right < b->data) |
| 207 | right += b->size; |
| 208 | else |
| 209 | right = b->data + b->size; |
| 210 | } |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 211 | return (right - left); |
| 212 | } |
| 213 | |
| 214 | /* Return the amount of bytes that can be written into the output area at once |
| 215 | * including reserved space which may be overwritten (this is the caller |
| 216 | * responsibility to know if the reserved space is protected or not). Input data |
| 217 | * are assumed to not exist. |
| 218 | */ |
| 219 | static inline int bo_contig_space(const struct buffer *b) |
| 220 | { |
| 221 | const char *left, *right; |
| 222 | |
Christopher Faulet | a36b311 | 2017-06-13 22:00:22 +0200 | [diff] [blame] | 223 | left = b->p; |
| 224 | right = b->p - b->o; |
| 225 | if (right < b->data) |
| 226 | right += b->size; |
| 227 | else |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 228 | right = b->data + b->size; |
| 229 | |
| 230 | return (right - left); |
| 231 | } |
| 232 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 233 | /* Return the buffer's length in bytes by summing the input and the output */ |
| 234 | static inline int buffer_len(const struct buffer *buf) |
| 235 | { |
| 236 | return buf->i + buf->o; |
| 237 | } |
| 238 | |
| 239 | /* Return non-zero only if the buffer is not empty */ |
| 240 | static inline int buffer_not_empty(const struct buffer *buf) |
| 241 | { |
| 242 | return buf->i | buf->o; |
| 243 | } |
| 244 | |
| 245 | /* Return non-zero only if the buffer is empty */ |
| 246 | static inline int buffer_empty(const struct buffer *buf) |
| 247 | { |
| 248 | return !buffer_not_empty(buf); |
| 249 | } |
| 250 | |
Willy Tarreau | 5b9834f | 2017-10-16 14:01:18 +0200 | [diff] [blame] | 251 | /* Return non-zero only if the buffer's free space wraps : |
| 252 | * [ |oooo| ] => yes |
| 253 | * [ |iiii| ] => yes |
| 254 | * [ |oooo|iiii| ] => yes |
| 255 | * [oooo| ] => no |
| 256 | * [ |oooo] => no |
| 257 | * [iiii| ] => no |
| 258 | * [ |iiii] => no |
| 259 | * [oooo|iiii| ] => no |
| 260 | * [ |oooo|iiii] => no |
| 261 | * [iiii| |oooo] => no |
| 262 | * [oo|iiii| |oo] => no |
| 263 | * [iiii| |oo|ii] => no |
| 264 | * [oooooooooo|iiiiiiiiiii] => no |
| 265 | * [iiiiiiiiiiiii|oooooooo] => no |
| 266 | * |
| 267 | * So the only case where the buffer does not wrap is when there's data either |
| 268 | * at the beginning or at the end of the buffer. Thus we have this : |
| 269 | * - if (p+i >= size) ==> doesn't wrap |
| 270 | * - if (p-data <= o) ==> doesn't wrap |
| 271 | * - otherwise wraps |
| 272 | */ |
| 273 | static inline int buffer_space_wraps(const struct buffer *buf) |
| 274 | { |
| 275 | if (buf->p + buf->i >= buf->data + buf->size) |
| 276 | return 0; |
| 277 | if (buf->p <= buf->data + buf->o) |
| 278 | return 0; |
| 279 | return 1; |
| 280 | } |
| 281 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 282 | /* Returns non-zero if the buffer's INPUT is considered full, which means that |
| 283 | * it holds at least as much INPUT data as (size - reserve). This also means |
| 284 | * that data that are scheduled for output are considered as potential free |
| 285 | * space, and that the reserved space is always considered as not usable. This |
| 286 | * information alone cannot be used as a general purpose free space indicator. |
| 287 | * However it accurately indicates that too many data were fed in the buffer |
Willy Tarreau | 3889fff | 2015-01-13 20:20:10 +0100 | [diff] [blame] | 288 | * for an analyzer for instance. See the channel_may_recv() function for a more |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 289 | * generic function taking everything into account. |
| 290 | */ |
| 291 | static inline int buffer_full(const struct buffer *b, unsigned int reserve) |
| 292 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 293 | if (b == &buf_empty) |
| 294 | return 0; |
| 295 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 296 | return (b->i + reserve >= b->size); |
| 297 | } |
| 298 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 299 | /* Normalizes a pointer after a subtract */ |
| 300 | static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr) |
| 301 | { |
| 302 | if (ptr < buf->data) |
| 303 | ptr += buf->size; |
| 304 | return ptr; |
| 305 | } |
| 306 | |
| 307 | /* Normalizes a pointer after an addition */ |
| 308 | static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr) |
| 309 | { |
| 310 | if (ptr - buf->size >= buf->data) |
| 311 | ptr -= buf->size; |
| 312 | return ptr; |
| 313 | } |
| 314 | |
| 315 | /* Return the maximum amount of bytes that can be written into the buffer, |
| 316 | * including reserved space which may be overwritten. |
| 317 | */ |
| 318 | static inline int buffer_total_space(const struct buffer *buf) |
| 319 | { |
| 320 | return buf->size - buffer_len(buf); |
| 321 | } |
| 322 | |
Thierry FOURNIER | d2b597a | 2015-03-07 14:38:50 +0100 | [diff] [blame] | 323 | /* Returns the amount of byte that can be written starting from <p> into the |
| 324 | * input buffer at once, including reserved space which may be overwritten. |
| 325 | * This is used by Lua to insert data in the input side just before the other |
| 326 | * data using buffer_replace(). The goal is to transfer these new data in the |
| 327 | * output buffer. |
| 328 | */ |
| 329 | static inline int bi_space_for_replace(const struct buffer *buf) |
| 330 | { |
| 331 | const char *end; |
| 332 | |
| 333 | /* If the input side data overflows, we cannot insert data contiguously. */ |
| 334 | if (buf->p + buf->i >= buf->data + buf->size) |
| 335 | return 0; |
| 336 | |
| 337 | /* Check the last byte used in the buffer, it may be a byte of the output |
| 338 | * side if the buffer wraps, or its the end of the buffer. |
| 339 | */ |
| 340 | end = buffer_wrap_sub(buf, buf->p - buf->o); |
| 341 | if (end <= buf->p) |
| 342 | end = buf->data + buf->size; |
| 343 | |
| 344 | /* Compute the amount of bytes which can be written. */ |
| 345 | return end - (buf->p + buf->i); |
| 346 | } |
| 347 | |
| 348 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 349 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 350 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 351 | * when increasing a pointer. Note that the wrapping test is only performed |
| 352 | * once, so the original pointer must be between ->data-size and ->data+2*size-1, |
| 353 | * otherwise an invalid pointer might be returned. |
| 354 | */ |
| 355 | static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr) |
| 356 | { |
| 357 | if (ptr < buf->data) |
| 358 | ptr += buf->size; |
| 359 | else if (ptr - buf->size >= buf->data) |
| 360 | ptr -= buf->size; |
| 361 | return ptr; |
| 362 | } |
| 363 | |
| 364 | /* Returns the distance between two pointers, taking into account the ability |
| 365 | * to wrap around the buffer's end. |
| 366 | */ |
| 367 | static inline int buffer_count(const struct buffer *buf, const char *from, const char *to) |
| 368 | { |
| 369 | int count = to - from; |
Willy Tarreau | bf43927 | 2013-04-02 01:25:57 +0200 | [diff] [blame] | 370 | |
| 371 | count += count < 0 ? buf->size : 0; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 372 | return count; |
| 373 | } |
| 374 | |
| 375 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 376 | * that is not scheduled to be sent. |
| 377 | */ |
| 378 | static inline int buffer_pending(const struct buffer *buf) |
| 379 | { |
| 380 | return buf->i; |
| 381 | } |
| 382 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 383 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 384 | static inline int buffer_almost_full(const struct buffer *buf) |
| 385 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 386 | if (buf == &buf_empty) |
| 387 | return 0; |
| 388 | |
| 389 | if (!buf->size || buffer_total_space(buf) < buf->size / 4) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 390 | return 1; |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to |
| 395 | * call this function with remaining data waiting to be sent (o > 0). The |
| 396 | * caller must ensure that <n> is smaller than the actual buffer's length. |
| 397 | * This is mainly used to remove empty lines at the beginning of a request |
| 398 | * or a response. |
| 399 | */ |
| 400 | static inline void bi_fast_delete(struct buffer *buf, int n) |
| 401 | { |
| 402 | buf->i -= n; |
| 403 | buf->p += n; |
| 404 | } |
| 405 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 406 | /* Tries to realign the given buffer. */ |
| 407 | static inline void buffer_realign(struct buffer *buf) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 408 | { |
| 409 | if (!(buf->i | buf->o)) { |
| 410 | /* let's realign the buffer to optimize I/O */ |
| 411 | buf->p = buf->data; |
| 412 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 415 | /* Schedule all remaining buffer data to be sent. ->o is not touched if it |
| 416 | * already covers those data. That permits doing a flush even after a forward, |
| 417 | * although not recommended. |
| 418 | */ |
| 419 | static inline void buffer_flush(struct buffer *buf) |
| 420 | { |
| 421 | buf->p = buffer_wrap_add(buf, buf->p + buf->i); |
| 422 | buf->o += buf->i; |
| 423 | buf->i = 0; |
| 424 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 425 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 426 | /* This function writes the string <str> at position <pos> which must be in |
| 427 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 428 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 429 | * (positive or negative) is returned. If there's no space left, the move is |
| 430 | * not done. The function does not adjust ->o because it does not make sense |
| 431 | * to use it on data scheduled to be sent. |
| 432 | */ |
| 433 | static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
| 434 | { |
| 435 | return buffer_replace2(b, pos, end, str, strlen(str)); |
| 436 | } |
| 437 | |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 438 | /* Tries to write char <c> into output data at buffer <b>. Supports wrapping. |
| 439 | * Data are truncated if buffer is full. |
| 440 | */ |
| 441 | static inline void bo_putchr(struct buffer *b, char c) |
| 442 | { |
| 443 | if (buffer_len(b) == b->size) |
| 444 | return; |
| 445 | *b->p = c; |
| 446 | b->p = b_ptr(b, 1); |
| 447 | b->o++; |
| 448 | } |
| 449 | |
| 450 | /* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping. |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 451 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 452 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 453 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 454 | static inline int bo_putblk(struct buffer *b, const char *blk, int len) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 455 | { |
| 456 | int cur_len = buffer_len(b); |
| 457 | int half; |
| 458 | |
| 459 | if (len > b->size - cur_len) |
| 460 | len = (b->size - cur_len); |
| 461 | if (!len) |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 462 | return 0; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 463 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 464 | half = bo_contig_space(b); |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 465 | if (half > len) |
| 466 | half = len; |
| 467 | |
| 468 | memcpy(b->p, blk, half); |
| 469 | b->p = b_ptr(b, half); |
| 470 | if (len > half) { |
| 471 | memcpy(b->p, blk, len - half); |
| 472 | b->p = b_ptr(b, half); |
| 473 | } |
| 474 | b->o += len; |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 475 | return len; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /* Tries to copy string <str> into output data at buffer <b>. Supports wrapping. |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 479 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 480 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 481 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 482 | static inline int bo_putstr(struct buffer *b, const char *str) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 483 | { |
| 484 | return bo_putblk(b, str, strlen(str)); |
| 485 | } |
| 486 | |
| 487 | /* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping. |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 488 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 489 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 490 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 491 | static inline int bo_putchk(struct buffer *b, const struct chunk *chk) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 492 | { |
| 493 | return bo_putblk(b, chk->str, chk->len); |
| 494 | } |
| 495 | |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 496 | /* Gets one full block of data at once from a buffer's output, optionally |
| 497 | * starting at a specific offset. Return values : |
| 498 | * >0 : number of bytes read, equal to requested size. |
| 499 | * =0 : not enough data available. <blk> is left undefined. |
| 500 | * The buffer is left unaffected. |
| 501 | */ |
| 502 | static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset) |
| 503 | { |
| 504 | int firstblock; |
| 505 | |
| 506 | if (len + offset > buf->o) |
| 507 | return 0; |
| 508 | |
| 509 | firstblock = buf->data + buf->size - bo_ptr(buf); |
| 510 | if (firstblock > offset) { |
| 511 | if (firstblock >= len + offset) { |
| 512 | memcpy(blk, bo_ptr(buf) + offset, len); |
| 513 | return len; |
| 514 | } |
| 515 | |
| 516 | memcpy(blk, bo_ptr(buf) + offset, firstblock - offset); |
| 517 | memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset); |
| 518 | return len; |
| 519 | } |
| 520 | |
| 521 | memcpy(blk, buf->data + offset - firstblock, len); |
| 522 | return len; |
| 523 | } |
| 524 | |
| 525 | /* Gets one or two blocks of data at once from a buffer's output. |
| 526 | * Return values : |
| 527 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 528 | * =0 : not enough data available. <blk*> are left undefined. |
| 529 | * The buffer is left unaffected. Unused buffers are left in an undefined state. |
| 530 | */ |
| 531 | static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2) |
| 532 | { |
| 533 | if (unlikely(buf->o == 0)) |
| 534 | return 0; |
| 535 | |
Willy Tarreau | 0621da5 | 2017-10-20 18:21:49 +0200 | [diff] [blame] | 536 | if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) { |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 537 | *blk1 = buf->p - buf->o + buf->size; |
| 538 | *len1 = buf->data + buf->size - *blk1; |
| 539 | *blk2 = buf->data; |
| 540 | *len2 = buf->p - buf->data; |
| 541 | return 2; |
| 542 | } |
| 543 | |
Willy Tarreau | 4b75fff | 2017-11-02 17:16:07 +0100 | [diff] [blame] | 544 | *blk1 = bo_ptr(buf); |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 545 | *len1 = buf->o; |
| 546 | return 1; |
| 547 | } |
| 548 | |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 549 | /* Tries to write char <c> into input data at buffer <b>. Supports wrapping. |
| 550 | * Data are truncated if buffer is full. |
| 551 | */ |
| 552 | static inline void bi_putchr(struct buffer *b, char c) |
| 553 | { |
| 554 | if (buffer_len(b) == b->size) |
| 555 | return; |
| 556 | *bi_end(b) = c; |
| 557 | b->i++; |
| 558 | } |
| 559 | |
| 560 | /* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping. |
| 561 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 562 | * copied. |
| 563 | */ |
| 564 | static inline int bi_putblk(struct buffer *b, const char *blk, int len) |
| 565 | { |
| 566 | int cur_len = buffer_len(b); |
| 567 | int half; |
| 568 | |
| 569 | if (len > b->size - cur_len) |
| 570 | len = (b->size - cur_len); |
| 571 | if (!len) |
| 572 | return 0; |
| 573 | |
| 574 | half = bi_contig_space(b); |
| 575 | if (half > len) |
| 576 | half = len; |
| 577 | |
| 578 | memcpy(bi_end(b), blk, half); |
| 579 | if (len > half) |
| 580 | memcpy(b_ptr(b, b->i + half), blk, len - half); |
| 581 | b->i += len; |
| 582 | return len; |
| 583 | } |
| 584 | |
| 585 | /* Tries to copy string <str> into input data at buffer <b>. Supports wrapping. |
| 586 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 587 | * copied. |
| 588 | */ |
| 589 | static inline int bi_putstr(struct buffer *b, const char *str) |
| 590 | { |
| 591 | return bi_putblk(b, str, strlen(str)); |
| 592 | } |
| 593 | |
| 594 | /* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping. |
| 595 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 596 | * copied. |
| 597 | */ |
| 598 | static inline int bi_putchk(struct buffer *b, const struct chunk *chk) |
| 599 | { |
| 600 | return bi_putblk(b, chk->str, chk->len); |
| 601 | } |
| 602 | |
| 603 | /* Gets one full block of data at once from a buffer's input. Return values : |
| 604 | * >0 : number of bytes read, equal to requested size. |
| 605 | * =0 : not enough data available. <blk> is left undefined. |
| 606 | * The buffer is left unaffected. |
| 607 | */ |
| 608 | static inline int bi_getblk(const struct buffer *buf, char *blk, int len) |
| 609 | { |
| 610 | int firstblock; |
| 611 | |
| 612 | if (len > buf->i) |
| 613 | return 0; |
| 614 | |
| 615 | firstblock = bi_contig_data(buf); |
| 616 | if (firstblock > len) |
| 617 | firstblock = len; |
| 618 | |
| 619 | memcpy(blk, bi_ptr(buf), firstblock); |
| 620 | if (len > firstblock) |
| 621 | memcpy(blk + firstblock, buf->data, len - firstblock); |
| 622 | return len; |
| 623 | } |
| 624 | |
| 625 | /* Gets one or two blocks of data at once from a buffer's input. |
| 626 | * Return values : |
| 627 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 628 | * =0 : not enough data available. <blk*> are left undefined. |
| 629 | * The buffer is left unaffected. Unused buffers are left in an undefined state. |
| 630 | */ |
| 631 | static inline int bi_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2) |
| 632 | { |
| 633 | if (unlikely(buf->i == 0)) |
| 634 | return 0; |
| 635 | |
| 636 | if (unlikely(buf->p + buf->i > buf->data + buf->size)) { |
| 637 | *blk1 = buf->p; |
| 638 | *len1 = buf->data + buf->size - buf->p; |
| 639 | *blk2 = buf->data; |
| 640 | *len2 = buf->i - *len1; |
| 641 | return 2; |
| 642 | } |
| 643 | |
| 644 | *blk1 = buf->p; |
| 645 | *len1 = buf->i; |
| 646 | return 1; |
| 647 | } |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 648 | |
Willy Tarreau | 474cf54 | 2014-11-24 10:54:47 +0100 | [diff] [blame] | 649 | /* Resets a buffer. The size is not touched. */ |
| 650 | static inline void b_reset(struct buffer *buf) |
| 651 | { |
| 652 | buf->o = 0; |
| 653 | buf->i = 0; |
| 654 | buf->p = buf->data; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 655 | |
Willy Tarreau | 474cf54 | 2014-11-24 10:54:47 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 658 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 659 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 660 | * already pointed to another buffer. The allocated buffer is returned, or |
| 661 | * NULL in case no memory is available. |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 662 | */ |
| 663 | static inline struct buffer *b_alloc(struct buffer **buf) |
| 664 | { |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 665 | struct buffer *b; |
| 666 | |
| 667 | *buf = &buf_wanted; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 668 | b = pool_alloc_dirty(pool_head_buffer); |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 669 | if (likely(b)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 670 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 671 | b_reset(b); |
| 672 | *buf = b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 673 | } |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 674 | return b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 677 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 678 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 679 | * already pointed to another buffer. The allocated buffer is returned, or |
| 680 | * NULL in case no memory is available. The difference with b_alloc() is that |
| 681 | * this function only picks from the pool and never calls malloc(), so it can |
| 682 | * fail even if some memory is available. |
| 683 | */ |
| 684 | static inline struct buffer *b_alloc_fast(struct buffer **buf) |
| 685 | { |
| 686 | struct buffer *b; |
| 687 | |
| 688 | *buf = &buf_wanted; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 689 | b = pool_get_first(pool_head_buffer); |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 690 | if (likely(b)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 691 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 692 | b_reset(b); |
| 693 | *buf = b; |
| 694 | } |
| 695 | return b; |
| 696 | } |
| 697 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 698 | /* Releases buffer *buf (no check of emptiness) */ |
| 699 | static inline void __b_drop(struct buffer **buf) |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 700 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 701 | pool_free(pool_head_buffer, *buf); |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 704 | /* Releases buffer *buf if allocated. */ |
| 705 | static inline void b_drop(struct buffer **buf) |
| 706 | { |
| 707 | if (!(*buf)->size) |
| 708 | return; |
| 709 | __b_drop(buf); |
| 710 | } |
| 711 | |
| 712 | /* Releases buffer *buf if allocated, and replaces it with &buf_empty. */ |
| 713 | static inline void b_free(struct buffer **buf) |
| 714 | { |
| 715 | b_drop(buf); |
| 716 | *buf = &buf_empty; |
| 717 | } |
| 718 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 719 | /* Ensures that <buf> is allocated. If an allocation is needed, it ensures that |
| 720 | * there are still at least <margin> buffers available in the pool after this |
| 721 | * allocation so that we don't leave the pool in a condition where a session or |
| 722 | * a response buffer could not be allocated anymore, resulting in a deadlock. |
| 723 | * This means that we sometimes need to try to allocate extra entries even if |
| 724 | * only one buffer is needed. |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 725 | * |
| 726 | * We need to lock the pool here to be sure to have <margin> buffers available |
| 727 | * after the allocation, regardless how many threads that doing it in the same |
| 728 | * time. So, we use internal and lockless memory functions (prefixed with '__'). |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 729 | */ |
| 730 | static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin) |
| 731 | { |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 732 | struct buffer *b; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 733 | |
| 734 | if ((*buf)->size) |
| 735 | return *buf; |
| 736 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 737 | *buf = &buf_wanted; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 738 | HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 739 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 740 | /* fast path */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 741 | if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) { |
| 742 | b = __pool_get_first(pool_head_buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 743 | if (likely(b)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 744 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
| 745 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 746 | b_reset(b); |
| 747 | *buf = b; |
| 748 | return b; |
| 749 | } |
| 750 | } |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 751 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 752 | /* slow path, uses malloc() */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 753 | b = __pool_refill_alloc(pool_head_buffer, margin); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 754 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 755 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 756 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 757 | if (b) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 758 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 759 | b_reset(b); |
| 760 | *buf = b; |
| 761 | } |
| 762 | return b; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 765 | |
| 766 | void __offer_buffer(void *from, unsigned int threshold); |
| 767 | |
| 768 | static inline void offer_buffers(void *from, unsigned int threshold) |
| 769 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 770 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 771 | if (LIST_ISEMPTY(&buffer_wq)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 772 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 773 | return; |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 774 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 775 | __offer_buffer(from, threshold); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 776 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 779 | /*************************************************************************/ |
| 780 | /* functions used to manipulate strings and blocks with wrapping buffers */ |
| 781 | /*************************************************************************/ |
| 782 | |
| 783 | /* returns > 0 if the first <n> characters of buffer <b> starting at |
| 784 | * offset <o> relative to b->p match <ist>. (empty strings do match). It is |
| 785 | * designed to be use with reasonably small strings (ie matches a single byte |
| 786 | * per iteration). This function is usable both with input and output data. To |
| 787 | * be used like this depending on what to match : |
| 788 | * - input contents : b_isteq(b, 0, b->i, ist); |
| 789 | * - output contents : b_isteq(b, -b->o, b->o, ist); |
| 790 | * Return value : |
| 791 | * >0 : the number of matching bytes |
| 792 | * =0 : not enough bytes (or matching of empty string) |
| 793 | * <0 : non-matching byte found |
| 794 | */ |
| 795 | static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist) |
| 796 | { |
| 797 | struct ist r = ist; |
| 798 | const char *p; |
| 799 | const char *end = b->data + b->size; |
| 800 | |
| 801 | if (n < r.len) |
| 802 | return 0; |
| 803 | |
| 804 | p = b_ptr(b, o); |
| 805 | while (r.len--) { |
| 806 | if (*p++ != *r.ptr++) |
| 807 | return -1; |
| 808 | if (unlikely(p == end)) |
| 809 | p = b->data; |
| 810 | } |
| 811 | return ist.len; |
| 812 | } |
| 813 | |
| 814 | /* "eats" string <ist> from the input region of buffer <b>. Wrapping data is |
| 815 | * explicitly supported. It matches a single byte per iteration so strings |
| 816 | * should remain reasonably small. Returns : |
| 817 | * > 0 : number of bytes matched and eaten |
| 818 | * = 0 : not enough bytes (or matching an empty string) |
| 819 | * < 0 : non-matching byte found |
| 820 | */ |
| 821 | static inline int bi_eat(struct buffer *b, const struct ist ist) |
| 822 | { |
| 823 | int ret = b_isteq(b, 0, b->i, ist); |
| 824 | if (ret > 0) |
| 825 | bi_del(b, ret); |
| 826 | return ret; |
| 827 | } |
| 828 | |
Willy Tarreau | e5676e7 | 2017-09-22 15:47:51 +0200 | [diff] [blame] | 829 | /* injects string <ist> into the input region of buffer <b> provided that it |
| 830 | * fits. Wrapping is supported. It's designed for small strings as it only |
| 831 | * writes a single byte per iteration. Returns the number of characters copied |
| 832 | * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It |
| 833 | * will only modify the buffer upon success. In all cases, the contents are |
| 834 | * copied prior to reporting an error, so that the destination at least |
| 835 | * contains a valid but truncated string. |
| 836 | */ |
| 837 | static inline int bi_istput(struct buffer *b, const struct ist ist) |
| 838 | { |
| 839 | const char *end = b->data + b->size; |
| 840 | struct ist r = ist; |
| 841 | char *p; |
| 842 | |
| 843 | if (r.len > (size_t)(b->size - b->i - b->o)) |
| 844 | return r.len < b->size ? 0 : -1; |
| 845 | |
| 846 | p = b_ptr(b, b->i); |
| 847 | b->i += r.len; |
| 848 | while (r.len--) { |
| 849 | *p++ = *r.ptr++; |
| 850 | if (unlikely(p == end)) |
| 851 | p = b->data; |
| 852 | } |
| 853 | return ist.len; |
| 854 | } |
| 855 | |
| 856 | |
| 857 | /* injects string <ist> into the output region of buffer <b> provided that it |
| 858 | * fits. Input data is assumed not to exist and will silently be overwritten. |
| 859 | * Wrapping is supported. It's designed for small strings as it only writes a |
| 860 | * single byte per iteration. Returns the number of characters copied (ist.len), |
| 861 | * 0 if it temporarily does not fit or -1 if it will never fit. It will only |
| 862 | * modify the buffer upon success. In all cases, the contents are copied prior |
| 863 | * to reporting an error, so that the destination at least contains a valid |
| 864 | * but truncated string. |
| 865 | */ |
| 866 | static inline int bo_istput(struct buffer *b, const struct ist ist) |
| 867 | { |
| 868 | const char *end = b->data + b->size; |
| 869 | struct ist r = ist; |
| 870 | char *p; |
| 871 | |
| 872 | if (r.len > (size_t)(b->size - b->o)) |
| 873 | return r.len < b->size ? 0 : -1; |
| 874 | |
| 875 | p = b->p; |
| 876 | b->o += r.len; |
| 877 | b->p = b_ptr(b, r.len); |
| 878 | while (r.len--) { |
| 879 | *p++ = *r.ptr++; |
| 880 | if (unlikely(p == end)) |
| 881 | p = b->data; |
| 882 | } |
| 883 | return ist.len; |
| 884 | } |
| 885 | |
| 886 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 887 | #endif /* _COMMON_BUFFER_H */ |
| 888 | |
| 889 | /* |
| 890 | * Local variables: |
| 891 | * c-indent-level: 8 |
| 892 | * c-basic-offset: 8 |
| 893 | * End: |
| 894 | */ |