Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Buffer management functions. |
| 3 | * |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 14 | #include <stdarg.h> |
| 15 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <string.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 17 | |
| 18 | #include <common/config.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 19 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | #include <proto/buffers.h> |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 21 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 23 | struct pool_head *pool2_buffer; |
| 24 | |
| 25 | |
| 26 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 27 | int init_buffer() |
| 28 | { |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 29 | pool2_buffer = create_pool("buffer", sizeof(struct buffer) + global.tune.bufsize, MEM_F_SHARED); |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 30 | return pool2_buffer != NULL; |
| 31 | } |
| 32 | |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 33 | /* Schedule up to <bytes> more bytes to be forwarded by the buffer without notifying |
| 34 | * the task. Any pending data in the buffer is scheduled to be sent as well, |
| 35 | * in the limit of the number of bytes to forward. This must be the only method |
| 36 | * to use to schedule bytes to be sent. If the requested number is too large, it |
| 37 | * is automatically adjusted. The number of bytes taken into account is returned. |
| 38 | * Directly touching ->to_forward will cause lockups when send_max goes down to |
| 39 | * zero if nobody is ready to push the remaining data. |
| 40 | */ |
| 41 | unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes) |
| 42 | { |
| 43 | unsigned int data_left; |
| 44 | unsigned int new_forward; |
| 45 | |
| 46 | if (!bytes) |
| 47 | return 0; |
| 48 | data_left = buf->l - buf->send_max; |
| 49 | if (bytes <= (unsigned long long)data_left) { |
| 50 | buf->send_max += bytes; |
| 51 | buf->flags &= ~BF_OUT_EMPTY; |
| 52 | return bytes; |
| 53 | } |
| 54 | |
| 55 | buf->send_max += data_left; |
| 56 | if (buf->send_max) |
| 57 | buf->flags &= ~BF_OUT_EMPTY; |
| 58 | |
| 59 | if (buf->l < buffer_max_len(buf)) |
| 60 | buf->flags &= ~BF_FULL; |
| 61 | else |
| 62 | buf->flags |= BF_FULL; |
| 63 | |
| 64 | if (likely(bytes == BUF_INFINITE_FORWARD)) { |
| 65 | buf->to_forward = bytes; |
| 66 | return bytes; |
| 67 | } |
| 68 | |
| 69 | /* Note: the case below is the only case where we may return |
| 70 | * a byte count that does not fit into a 32-bit number. |
| 71 | */ |
| 72 | if (likely(buf->to_forward == BUF_INFINITE_FORWARD)) |
| 73 | return bytes; |
| 74 | |
| 75 | new_forward = buf->to_forward + bytes - data_left; |
| 76 | bytes = data_left; /* at least those bytes were scheduled */ |
| 77 | |
| 78 | if (new_forward <= buf->to_forward) { |
| 79 | /* integer overflow detected, let's assume no more than 2G at once */ |
| 80 | new_forward = MID_RANGE(new_forward); |
| 81 | } |
| 82 | |
| 83 | if (new_forward > buf->to_forward) { |
| 84 | bytes += new_forward - buf->to_forward; |
| 85 | buf->to_forward = new_forward; |
| 86 | } |
| 87 | return bytes; |
| 88 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 89 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 90 | /* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 91 | * success, -2 if the message is larger than the buffer size, or the number of |
| 92 | * bytes available otherwise. The send limit is automatically adjusted with the |
| 93 | * amount of data written. FIXME-20060521: handle unaligned data. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | */ |
| 95 | int buffer_write(struct buffer *buf, const char *msg, int len) |
| 96 | { |
| 97 | int max; |
| 98 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 99 | if (len == 0) |
| 100 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 102 | if (len > buf->size) { |
| 103 | /* we can't write this chunk and will never be able to, because |
| 104 | * it is larger than the buffer. This must be reported as an |
| 105 | * error. Then we return -2 so that writers that don't care can |
| 106 | * ignore it and go on, and others can check for this value. |
| 107 | */ |
| 108 | return -2; |
| 109 | } |
| 110 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 111 | max = buffer_realign(buf); |
| 112 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | if (len > max) |
| 114 | return max; |
| 115 | |
| 116 | memcpy(buf->r, msg, len); |
| 117 | buf->l += len; |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 118 | buf->send_max += len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | buf->r += len; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 120 | buf->total += len; |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 121 | if (buf->r == buf->data + buf->size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | buf->r = buf->data; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 123 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 124 | buf->flags &= ~(BF_OUT_EMPTY|BF_FULL); |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 125 | if (buf->l >= buffer_max_len(buf)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 126 | buf->flags |= BF_FULL; |
| 127 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 128 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 131 | /* Tries to copy character <c> into buffer <buf> after length controls. The |
| 132 | * send_max and to_forward pointers are updated. If the buffer's input is |
| 133 | * closed, -2 is returned. If there is not enough room left in the buffer, -1 |
| 134 | * is returned. Otherwise the number of bytes copied is returned (1). Buffer |
| 135 | * flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 136 | * transferred. |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 137 | */ |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 138 | int buffer_put_char(struct buffer *buf, char c) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 139 | { |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 140 | if (unlikely(buffer_input_closed(buf))) |
| 141 | return -2; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 142 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 143 | if (buf->flags & BF_FULL) |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 144 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 145 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 146 | *buf->r = c; |
| 147 | |
| 148 | buf->l++; |
| 149 | if (buf->l >= buffer_max_len(buf)) |
| 150 | buf->flags |= BF_FULL; |
| 151 | buf->flags |= BF_READ_PARTIAL; |
| 152 | |
| 153 | buf->r++; |
| 154 | if (buf->r - buf->data == buf->size) |
| 155 | buf->r -= buf->size; |
| 156 | |
| 157 | if (buf->to_forward >= 1) { |
| 158 | if (buf->to_forward != BUF_INFINITE_FORWARD) |
| 159 | buf->to_forward--; |
| 160 | buf->send_max++; |
| 161 | buf->flags &= ~BF_OUT_EMPTY; |
| 162 | } |
| 163 | |
| 164 | buf->total++; |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | /* Tries to copy block <blk> at once into buffer <buf> after length controls. |
| 169 | * The send_max and to_forward pointers are updated. If the buffer's input is |
| 170 | * closed, -2 is returned. If the block is too large for this buffer, -3 is |
| 171 | * returned. If there is not enough room left in the buffer, -1 is returned. |
| 172 | * Otherwise the number of bytes copied is returned (0 being a valid number). |
| 173 | * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 174 | * transferred. |
| 175 | */ |
| 176 | int buffer_put_block(struct buffer *buf, const char *blk, int len) |
| 177 | { |
| 178 | int max; |
| 179 | |
| 180 | if (unlikely(buffer_input_closed(buf))) |
| 181 | return -2; |
| 182 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 183 | max = buffer_max_len(buf); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 184 | if (unlikely(len > max - buf->l)) { |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 185 | /* we can't write this chunk right now because the buffer is |
| 186 | * almost full or because the block is too large. Return the |
| 187 | * available space or -2 if impossible. |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 188 | */ |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 189 | if (len > max) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 190 | return -3; |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 191 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 192 | return -1; |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 193 | } |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 194 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 195 | if (unlikely(len == 0)) |
| 196 | return 0; |
| 197 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 198 | /* OK so the data fits in the buffer in one or two blocks */ |
| 199 | max = buffer_contig_space_with_len(buf, max); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 200 | memcpy(buf->r, blk, MIN(len, max)); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 201 | if (len > max) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 202 | memcpy(buf->data, blk + max, len - max); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 203 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 204 | buf->l += len; |
| 205 | buf->r += len; |
| 206 | buf->total += len; |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 207 | if (buf->to_forward) { |
| 208 | unsigned long fwd = len; |
| 209 | if (buf->to_forward != BUF_INFINITE_FORWARD) { |
| 210 | if (fwd > buf->to_forward) |
| 211 | fwd = buf->to_forward; |
| 212 | buf->to_forward -= fwd; |
| 213 | } |
| 214 | buf->send_max += fwd; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 215 | buf->flags &= ~BF_OUT_EMPTY; |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 218 | if (buf->r >= buf->data + buf->size) |
| 219 | buf->r -= buf->size; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 220 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 221 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 222 | if (buf->l >= buffer_max_len(buf)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 223 | buf->flags |= BF_FULL; |
| 224 | |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 225 | /* notify that some data was read from the SI into the buffer */ |
| 226 | buf->flags |= BF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 227 | return len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 230 | /* Gets one text line out of a buffer from a stream interface. |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 231 | * Return values : |
| 232 | * >0 : number of bytes read. Includes the \n if present before len or end. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 233 | * =0 : no '\n' before end found. <str> is left undefined. |
| 234 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 235 | * The buffer status is not changed. The caller must call buffer_skip() to |
| 236 | * update it. The '\n' is waited for as long as neither the buffer nor the |
| 237 | * output are full. If either of them is full, the string may be returned |
| 238 | * as is, without the '\n'. |
| 239 | */ |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 240 | int buffer_get_line(struct buffer *buf, char *str, int len) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 241 | { |
| 242 | int ret, max; |
| 243 | char *p; |
| 244 | |
| 245 | ret = 0; |
| 246 | max = len; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 247 | |
| 248 | /* closed or empty + imminent close = -1; empty = 0 */ |
| 249 | if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) { |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 250 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 251 | ret = -1; |
| 252 | goto out; |
| 253 | } |
| 254 | |
| 255 | p = buf->w; |
| 256 | |
| 257 | if (max > buf->send_max) { |
| 258 | max = buf->send_max; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 259 | str[max-1] = 0; |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 260 | } |
| 261 | while (max) { |
| 262 | *str++ = *p; |
| 263 | ret++; |
| 264 | max--; |
| 265 | |
| 266 | if (*p == '\n') |
| 267 | break; |
| 268 | p++; |
| 269 | if (p == buf->data + buf->size) |
| 270 | p = buf->data; |
| 271 | } |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 272 | if (ret > 0 && ret < len && ret < buf->send_max && |
| 273 | *(str-1) != '\n' && |
| 274 | !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 275 | ret = 0; |
| 276 | out: |
| 277 | if (max) |
| 278 | *str = 0; |
| 279 | return ret; |
| 280 | } |
| 281 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 282 | /* Gets one full block of data at once from a buffer, optionally from a |
| 283 | * specific offset. Return values : |
| 284 | * >0 : number of bytes read, equal to requested size. |
| 285 | * =0 : not enough data available. <blk> is left undefined. |
| 286 | * <0 : no more bytes readable because output is shut. |
| 287 | * The buffer status is not changed. The caller must call buffer_skip() to |
| 288 | * update it. |
| 289 | */ |
| 290 | int buffer_get_block(struct buffer *buf, char *blk, int len, int offset) |
| 291 | { |
| 292 | int firstblock; |
| 293 | |
| 294 | if (buf->flags & BF_SHUTW) |
| 295 | return -1; |
| 296 | |
| 297 | if (len + offset > buf->send_max) { |
| 298 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 299 | return -1; |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | firstblock = buf->data + buf->size - buf->w; |
| 304 | if (firstblock > offset) { |
| 305 | if (firstblock >= len + offset) { |
| 306 | memcpy(blk, buf->w + offset, len); |
| 307 | return len; |
| 308 | } |
| 309 | |
| 310 | memcpy(blk, buf->w + offset, firstblock - offset); |
| 311 | memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset); |
| 312 | return len; |
| 313 | } |
| 314 | |
| 315 | memcpy(blk, buf->data + offset - firstblock, len); |
| 316 | return len; |
| 317 | } |
| 318 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 319 | /* |
| 320 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 321 | * and moves <end> just after the end of <str>. |
Willy Tarreau | bbfa793 | 2010-01-25 01:49:57 +0100 | [diff] [blame] | 322 | * <b>'s parameters (l, r, lr) are recomputed to be valid after the shift. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 323 | * the shift value (positive or negative) is returned. |
| 324 | * If there's no space left, the move is not done. |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 325 | * The function does not adjust ->send_max nor BF_OUT_EMPTY because it does not |
| 326 | * make sense to use it on data scheduled to be sent. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 327 | * |
| 328 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 329 | int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 330 | { |
| 331 | int delta; |
| 332 | int len; |
| 333 | |
| 334 | len = strlen(str); |
| 335 | delta = len - (end - pos); |
| 336 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 337 | if (delta + b->r >= b->data + b->size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 338 | return 0; /* no space left */ |
| 339 | |
Willy Tarreau | bbfa793 | 2010-01-25 01:49:57 +0100 | [diff] [blame] | 340 | if (delta + b->r > b->w && b->w >= b->r && b->l) |
| 341 | return 0; /* no space left before wrapping data */ |
| 342 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 343 | /* first, protect the end of the buffer */ |
Willy Tarreau | 019fd5b | 2009-12-28 18:37:54 +0100 | [diff] [blame] | 344 | memmove(end + delta, end, b->r - end); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 345 | |
| 346 | /* now, copy str over pos */ |
| 347 | memcpy(pos, str,len); |
| 348 | |
| 349 | /* we only move data after the displaced zone */ |
| 350 | if (b->r > pos) b->r += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 351 | if (b->lr > pos) b->lr += delta; |
| 352 | b->l += delta; |
| 353 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 354 | b->flags &= ~BF_FULL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 355 | if (b->l == 0) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 356 | b->r = b->w = b->lr = b->data; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 357 | if (b->l >= buffer_max_len(b)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 358 | b->flags |= BF_FULL; |
| 359 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 360 | return delta; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * same except that the string length is given, which allows str to be NULL if |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 365 | * len is 0. The send limit is *not* adjusted. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 366 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 367 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 368 | { |
| 369 | int delta; |
| 370 | |
| 371 | delta = len - (end - pos); |
| 372 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 373 | if (delta + b->r >= b->data + b->size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 374 | return 0; /* no space left */ |
| 375 | |
Willy Tarreau | bbfa793 | 2010-01-25 01:49:57 +0100 | [diff] [blame] | 376 | if (delta + b->r > b->w && b->w >= b->r && b->l) |
| 377 | return 0; /* no space left before wrapping data */ |
| 378 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 379 | /* first, protect the end of the buffer */ |
Willy Tarreau | 019fd5b | 2009-12-28 18:37:54 +0100 | [diff] [blame] | 380 | memmove(end + delta, end, b->r - end); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 381 | |
| 382 | /* now, copy str over pos */ |
| 383 | if (len) |
| 384 | memcpy(pos, str, len); |
| 385 | |
| 386 | /* we only move data after the displaced zone */ |
| 387 | if (b->r > pos) b->r += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 388 | if (b->lr > pos) b->lr += delta; |
| 389 | b->l += delta; |
| 390 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 391 | b->flags &= ~BF_FULL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 392 | if (b->l == 0) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 393 | b->r = b->w = b->lr = b->data; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 394 | if (b->l >= buffer_max_len(b)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 395 | b->flags |= BF_FULL; |
| 396 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 397 | return delta; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 402 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 403 | * argument informs about the length of string <str> so that we don't have to |
| 404 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 405 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 406 | * some circumstances. The send limit is *not* adjusted. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 407 | * |
| 408 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 409 | */ |
| 410 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 411 | { |
| 412 | int delta; |
| 413 | |
| 414 | delta = len + 2; |
| 415 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 416 | if (delta + b->r >= b->data + b->size) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 417 | return 0; /* no space left */ |
| 418 | |
| 419 | /* first, protect the end of the buffer */ |
Willy Tarreau | 019fd5b | 2009-12-28 18:37:54 +0100 | [diff] [blame] | 420 | memmove(pos + delta, pos, b->r - pos); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 421 | |
| 422 | /* now, copy str over pos */ |
| 423 | if (len && str) { |
| 424 | memcpy(pos, str, len); |
| 425 | pos[len] = '\r'; |
| 426 | pos[len + 1] = '\n'; |
| 427 | } |
| 428 | |
| 429 | /* we only move data after the displaced zone */ |
| 430 | if (b->r > pos) b->r += delta; |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 431 | if (b->lr > pos) b->lr += delta; |
| 432 | b->l += delta; |
| 433 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 434 | b->flags &= ~BF_FULL; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 435 | if (b->l >= buffer_max_len(b)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 436 | b->flags |= BF_FULL; |
| 437 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 438 | return delta; |
| 439 | } |
| 440 | |
| 441 | |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 442 | /* Realigns a possibly non-contiguous buffer by bouncing bytes from source to |
| 443 | * destination. It does not use any intermediate buffer and does the move in |
| 444 | * place, though it will be slower than a simple memmove() on contiguous data, |
| 445 | * so it's desirable to use it only on non-contiguous buffers. No pointers are |
| 446 | * changed, the caller is responsible for that. |
| 447 | */ |
| 448 | void buffer_bounce_realign(struct buffer *buf) |
| 449 | { |
| 450 | int advance, to_move; |
| 451 | char *from, *to; |
| 452 | |
| 453 | advance = buf->data + buf->size - buf->w; |
| 454 | if (!advance) |
| 455 | return; |
| 456 | |
| 457 | from = buf->w; |
| 458 | to_move = buf->l; |
| 459 | while (to_move) { |
| 460 | char last, save; |
| 461 | |
| 462 | last = *from; |
| 463 | to = from + advance; |
| 464 | if (to >= buf->data + buf->size) |
| 465 | to -= buf->size; |
| 466 | |
| 467 | while (1) { |
| 468 | save = *to; |
| 469 | *to = last; |
| 470 | last = save; |
| 471 | to_move--; |
| 472 | if (!to_move) |
| 473 | break; |
| 474 | |
| 475 | /* check if we went back home after rotating a number of bytes */ |
| 476 | if (to == from) |
| 477 | break; |
| 478 | |
| 479 | /* if we ended up in the empty area, let's walk to next place. The |
| 480 | * empty area is either between buf->r and from or before from or |
| 481 | * after buf->r. |
| 482 | */ |
| 483 | if (from > buf->r) { |
| 484 | if (to >= buf->r && to < from) |
| 485 | break; |
| 486 | } else if (from < buf->r) { |
| 487 | if (to < from || to >= buf->r) |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | /* we have overwritten a byte of the original set, let's move it */ |
| 492 | to += advance; |
| 493 | if (to >= buf->data + buf->size) |
| 494 | to -= buf->size; |
| 495 | } |
| 496 | |
| 497 | from++; |
| 498 | if (from >= buf->data + buf->size) |
| 499 | from -= buf->size; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 504 | /* |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 505 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 506 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 507 | * the new chunk size. |
| 508 | */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 509 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 510 | { |
| 511 | va_list argp; |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 512 | int ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 513 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 514 | if (!chk->str || !chk->size) |
| 515 | return 0; |
| 516 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 517 | va_start(argp, fmt); |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 518 | ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp); |
| 519 | if (ret >= chk->size - chk->len) |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 520 | /* do not copy anything in case of truncation */ |
| 521 | chk->str[chk->len] = 0; |
| 522 | else |
| 523 | chk->len += ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 524 | va_end(argp); |
| 525 | return chk->len; |
| 526 | } |
| 527 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 528 | /* |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 529 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 530 | * chk->size chars. Replace non-printable or special chracters with "&#%d;". |
| 531 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 532 | */ |
| 533 | int chunk_htmlencode(struct chunk *dst, struct chunk *src) { |
| 534 | |
| 535 | int i, l; |
| 536 | int olen, free; |
| 537 | char c; |
| 538 | |
| 539 | olen = dst->len; |
| 540 | |
| 541 | for (i = 0; i < src->len; i++) { |
| 542 | free = dst->size - dst->len; |
| 543 | |
| 544 | if (!free) { |
| 545 | dst->len = olen; |
| 546 | return dst->len; |
| 547 | } |
| 548 | |
| 549 | c = src->str[i]; |
| 550 | |
Willy Tarreau | 88e0581 | 2010-03-03 00:16:00 +0100 | [diff] [blame] | 551 | if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') { |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 552 | l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c); |
| 553 | |
| 554 | if (free < l) { |
| 555 | dst->len = olen; |
| 556 | return dst->len; |
| 557 | } |
| 558 | |
| 559 | dst->len += l; |
| 560 | } else { |
| 561 | dst->str[dst->len] = c; |
| 562 | dst->len++; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | return dst->len; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 571 | * chk->size chars. Replace non-printable or char passed in qc with "<%02X>". |
| 572 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 573 | */ |
| 574 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) { |
| 575 | int i, l; |
| 576 | int olen, free; |
| 577 | char c; |
| 578 | |
| 579 | olen = dst->len; |
| 580 | |
| 581 | for (i = 0; i < src->len; i++) { |
| 582 | free = dst->size - dst->len; |
| 583 | |
| 584 | if (!free) { |
| 585 | dst->len = olen; |
| 586 | return dst->len; |
| 587 | } |
| 588 | |
| 589 | c = src->str[i]; |
| 590 | |
Willy Tarreau | 88e0581 | 2010-03-03 00:16:00 +0100 | [diff] [blame] | 591 | if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) { |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 592 | l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c); |
| 593 | |
| 594 | if (free < l) { |
| 595 | dst->len = olen; |
| 596 | return dst->len; |
| 597 | } |
| 598 | |
| 599 | dst->len += l; |
| 600 | } else { |
| 601 | dst->str[dst->len] = c; |
| 602 | dst->len++; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | return dst->len; |
| 607 | } |
| 608 | |
| 609 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 610 | * Dumps part or all of a buffer. |
| 611 | */ |
| 612 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 613 | { |
| 614 | fprintf(o, "Dumping buffer %p\n", b); |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 615 | fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n", |
| 616 | b->data, b->l, b->r, b->w, b->lr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 617 | |
| 618 | if (!to || to > b->l) |
| 619 | to = b->l; |
| 620 | |
| 621 | fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); |
| 622 | for (; from < to; from++) { |
| 623 | if ((from & 15) == 0) |
| 624 | fprintf(o, " %04x: ", from); |
| 625 | fprintf(o, "%02x ", b->data[from]); |
| 626 | if ((from & 15) == 7) |
| 627 | fprintf(o, "- "); |
| 628 | else if (((from & 15) == 15) && (from != to-1)) |
| 629 | fprintf(o, "\n"); |
| 630 | } |
| 631 | fprintf(o, "\n--\n"); |
| 632 | } |
| 633 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 634 | |
| 635 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 636 | * Local variables: |
| 637 | * c-indent-level: 8 |
| 638 | * c-basic-offset: 8 |
| 639 | * End: |
| 640 | */ |