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