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