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 */ |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 199 | max = buffer_contig_space_with_res(buf, buf->size - 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 | 19ae56b | 2011-11-28 10:36:13 +0100 | [diff] [blame] | 319 | /* This function writes the string <str> at position <pos> which must be in |
| 320 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 321 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 322 | * (positive or negative) is returned. If there's no space left, the move is |
| 323 | * not done. The function does not adjust ->send_max nor BF_OUT_EMPTY because |
| 324 | * it does not make sense to use it on data scheduled to be sent. The string |
| 325 | * length is taken from parameter <len>. If <len> is null, the <str> pointer |
| 326 | * is allowed to be null. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 327 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 328 | 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] | 329 | { |
| 330 | int delta; |
| 331 | |
| 332 | delta = len - (end - pos); |
| 333 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 334 | if (delta + b->r >= b->data + b->size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 335 | return 0; /* no space left */ |
| 336 | |
Willy Tarreau | bbfa793 | 2010-01-25 01:49:57 +0100 | [diff] [blame] | 337 | if (delta + b->r > b->w && b->w >= b->r && b->l) |
| 338 | return 0; /* no space left before wrapping data */ |
| 339 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 340 | /* first, protect the end of the buffer */ |
Willy Tarreau | 019fd5b | 2009-12-28 18:37:54 +0100 | [diff] [blame] | 341 | memmove(end + delta, end, b->r - end); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 342 | |
| 343 | /* now, copy str over pos */ |
| 344 | if (len) |
| 345 | memcpy(pos, str, len); |
| 346 | |
| 347 | /* we only move data after the displaced zone */ |
| 348 | if (b->r > pos) b->r += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 349 | if (b->lr > pos) b->lr += delta; |
| 350 | b->l += delta; |
| 351 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 352 | b->flags &= ~BF_FULL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 353 | if (b->l == 0) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 354 | b->r = b->w = b->lr = b->data; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 355 | if (b->l >= buffer_max_len(b)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 356 | b->flags |= BF_FULL; |
| 357 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 358 | return delta; |
| 359 | } |
| 360 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 361 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 362 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 363 | * argument informs about the length of string <str> so that we don't have to |
| 364 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 365 | * 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] | 366 | * some circumstances. The send limit is *not* adjusted. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 367 | * |
| 368 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 369 | */ |
| 370 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 371 | { |
| 372 | int delta; |
| 373 | |
| 374 | delta = len + 2; |
| 375 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 376 | if (delta + b->r >= b->data + b->size) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 377 | return 0; /* no space left */ |
| 378 | |
| 379 | /* first, protect the end of the buffer */ |
Willy Tarreau | 019fd5b | 2009-12-28 18:37:54 +0100 | [diff] [blame] | 380 | memmove(pos + delta, pos, b->r - pos); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 381 | |
| 382 | /* now, copy str over pos */ |
| 383 | if (len && str) { |
| 384 | memcpy(pos, str, len); |
| 385 | pos[len] = '\r'; |
| 386 | pos[len + 1] = '\n'; |
| 387 | } |
| 388 | |
| 389 | /* we only move data after the displaced zone */ |
| 390 | if (b->r > pos) b->r += delta; |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 391 | if (b->lr > pos) b->lr += delta; |
| 392 | b->l += delta; |
| 393 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 394 | b->flags &= ~BF_FULL; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 395 | if (b->l >= buffer_max_len(b)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 396 | b->flags |= BF_FULL; |
| 397 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 398 | return delta; |
| 399 | } |
| 400 | |
| 401 | |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 402 | /* Realigns a possibly non-contiguous buffer by bouncing bytes from source to |
| 403 | * destination. It does not use any intermediate buffer and does the move in |
| 404 | * place, though it will be slower than a simple memmove() on contiguous data, |
| 405 | * so it's desirable to use it only on non-contiguous buffers. No pointers are |
| 406 | * changed, the caller is responsible for that. |
| 407 | */ |
| 408 | void buffer_bounce_realign(struct buffer *buf) |
| 409 | { |
| 410 | int advance, to_move; |
| 411 | char *from, *to; |
| 412 | |
| 413 | advance = buf->data + buf->size - buf->w; |
| 414 | if (!advance) |
| 415 | return; |
| 416 | |
| 417 | from = buf->w; |
| 418 | to_move = buf->l; |
| 419 | while (to_move) { |
| 420 | char last, save; |
| 421 | |
| 422 | last = *from; |
| 423 | to = from + advance; |
| 424 | if (to >= buf->data + buf->size) |
| 425 | to -= buf->size; |
| 426 | |
| 427 | while (1) { |
| 428 | save = *to; |
| 429 | *to = last; |
| 430 | last = save; |
| 431 | to_move--; |
| 432 | if (!to_move) |
| 433 | break; |
| 434 | |
| 435 | /* check if we went back home after rotating a number of bytes */ |
| 436 | if (to == from) |
| 437 | break; |
| 438 | |
| 439 | /* if we ended up in the empty area, let's walk to next place. The |
| 440 | * empty area is either between buf->r and from or before from or |
| 441 | * after buf->r. |
| 442 | */ |
| 443 | if (from > buf->r) { |
| 444 | if (to >= buf->r && to < from) |
| 445 | break; |
| 446 | } else if (from < buf->r) { |
| 447 | if (to < from || to >= buf->r) |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | /* we have overwritten a byte of the original set, let's move it */ |
| 452 | to += advance; |
| 453 | if (to >= buf->data + buf->size) |
| 454 | to -= buf->size; |
| 455 | } |
| 456 | |
| 457 | from++; |
| 458 | if (from >= buf->data + buf->size) |
| 459 | from -= buf->size; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 464 | /* |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 465 | * 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] | 466 | * 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] | 467 | * the new chunk size. |
| 468 | */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 469 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 470 | { |
| 471 | va_list argp; |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 472 | int ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 473 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 474 | if (!chk->str || !chk->size) |
| 475 | return 0; |
| 476 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 477 | va_start(argp, fmt); |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 478 | ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp); |
| 479 | if (ret >= chk->size - chk->len) |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 480 | /* do not copy anything in case of truncation */ |
| 481 | chk->str[chk->len] = 0; |
| 482 | else |
| 483 | chk->len += ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 484 | va_end(argp); |
| 485 | return chk->len; |
| 486 | } |
| 487 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 488 | /* |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 489 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 490 | * chk->size chars. Replace non-printable or special chracters with "&#%d;". |
| 491 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 492 | */ |
| 493 | int chunk_htmlencode(struct chunk *dst, struct chunk *src) { |
| 494 | |
| 495 | int i, l; |
| 496 | int olen, free; |
| 497 | char c; |
| 498 | |
| 499 | olen = dst->len; |
| 500 | |
| 501 | for (i = 0; i < src->len; i++) { |
| 502 | free = dst->size - dst->len; |
| 503 | |
| 504 | if (!free) { |
| 505 | dst->len = olen; |
| 506 | return dst->len; |
| 507 | } |
| 508 | |
| 509 | c = src->str[i]; |
| 510 | |
Willy Tarreau | 88e0581 | 2010-03-03 00:16:00 +0100 | [diff] [blame] | 511 | 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] | 512 | l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c); |
| 513 | |
| 514 | if (free < l) { |
| 515 | dst->len = olen; |
| 516 | return dst->len; |
| 517 | } |
| 518 | |
| 519 | dst->len += l; |
| 520 | } else { |
| 521 | dst->str[dst->len] = c; |
| 522 | dst->len++; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | return dst->len; |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 531 | * chk->size chars. Replace non-printable or char passed in qc with "<%02X>". |
| 532 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 533 | */ |
| 534 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) { |
| 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 == qc) { |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 552 | l = snprintf(dst->str + dst->len, free, "<%02X>", (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 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 570 | * Dumps part or all of a buffer. |
| 571 | */ |
| 572 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 573 | { |
| 574 | fprintf(o, "Dumping buffer %p\n", b); |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 575 | fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n", |
| 576 | b->data, b->l, b->r, b->w, b->lr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 577 | |
| 578 | if (!to || to > b->l) |
| 579 | to = b->l; |
| 580 | |
| 581 | fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); |
| 582 | for (; from < to; from++) { |
| 583 | if ((from & 15) == 0) |
| 584 | fprintf(o, " %04x: ", from); |
| 585 | fprintf(o, "%02x ", b->data[from]); |
| 586 | if ((from & 15) == 7) |
| 587 | fprintf(o, "- "); |
| 588 | else if (((from & 15) == 15) && (from != to-1)) |
| 589 | fprintf(o, "\n"); |
| 590 | } |
| 591 | fprintf(o, "\n--\n"); |
| 592 | } |
| 593 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 594 | |
| 595 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 596 | * Local variables: |
| 597 | * c-indent-level: 8 |
| 598 | * c-basic-offset: 8 |
| 599 | * End: |
| 600 | */ |