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