Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Buffer management functions. |
| 3 | * |
| 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
| 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 | |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include <common/config.h> |
| 18 | #include <common/buffer.h> |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 19 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 20 | |
| 21 | #include <types/global.h> |
| 22 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +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 | { |
| 29 | pool2_buffer = create_pool("buffer", sizeof (struct buffer) + global.tune.bufsize, MEM_F_SHARED); |
| 30 | return pool2_buffer != NULL; |
| 31 | } |
| 32 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 33 | /* This function writes the string <str> at position <pos> which must be in |
| 34 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 35 | * <l> and <r> are updated to be valid after the shift. The shift value |
| 36 | * (positive or negative) is returned. If there's no space left, the move is |
| 37 | * not done. The function does not adjust ->o because it does not make sense to |
| 38 | * use it on data scheduled to be sent. For the same reason, it does not make |
| 39 | * sense to call this function on unparsed data, so <orig> is not updated. The |
| 40 | * string length is taken from parameter <len>. If <len> is null, the <str> |
| 41 | * pointer is allowed to be null. |
| 42 | */ |
| 43 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len) |
| 44 | { |
| 45 | int delta; |
| 46 | |
| 47 | delta = len - (end - pos); |
| 48 | |
Thierry FOURNIER | b929028 | 2015-03-10 01:55:01 +0100 | [diff] [blame] | 49 | if (bi_end(b) + delta > b->data + b->size) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 50 | return 0; /* no space left */ |
| 51 | |
| 52 | if (buffer_not_empty(b) && |
| 53 | bi_end(b) + delta > bo_ptr(b) && |
| 54 | bo_ptr(b) >= bi_end(b)) |
| 55 | return 0; /* no space left before wrapping data */ |
| 56 | |
| 57 | /* first, protect the end of the buffer */ |
| 58 | memmove(end + delta, end, bi_end(b) - end); |
| 59 | |
| 60 | /* now, copy str over pos */ |
| 61 | if (len) |
| 62 | memcpy(pos, str, len); |
| 63 | |
| 64 | b->i += delta; |
| 65 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 66 | if (buffer_empty(b)) |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 67 | b->p = b->data; |
| 68 | |
| 69 | return delta; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 74 | * argument informs about the length of string <str> so that we don't have to |
| 75 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 76 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
| 77 | * some circumstances. The send limit is *not* adjusted. Same comments as above |
| 78 | * for the valid use cases. |
| 79 | * |
| 80 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 81 | */ |
| 82 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 83 | { |
| 84 | int delta; |
| 85 | |
| 86 | delta = len + 2; |
| 87 | |
| 88 | if (bi_end(b) + delta >= b->data + b->size) |
| 89 | return 0; /* no space left */ |
| 90 | |
Godbach | e31de83 | 2014-10-31 13:16:37 +0800 | [diff] [blame] | 91 | if (buffer_not_empty(b) && |
| 92 | bi_end(b) + delta > bo_ptr(b) && |
| 93 | bo_ptr(b) >= bi_end(b)) |
| 94 | return 0; /* no space left before wrapping data */ |
| 95 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 96 | /* first, protect the end of the buffer */ |
| 97 | memmove(pos + delta, pos, bi_end(b) - pos); |
| 98 | |
| 99 | /* now, copy str over pos */ |
| 100 | if (len && str) { |
| 101 | memcpy(pos, str, len); |
| 102 | pos[len] = '\r'; |
| 103 | pos[len + 1] = '\n'; |
| 104 | } |
| 105 | |
| 106 | b->i += delta; |
| 107 | return delta; |
| 108 | } |
| 109 | |
Willy Tarreau | 7ec7655 | 2015-07-02 12:50:23 +0200 | [diff] [blame^] | 110 | /* This function realigns a possibly wrapping buffer so that the input part is |
| 111 | * contiguous and starts at the beginning of the buffer and the output part |
| 112 | * ends at the end of the buffer. This provides the best conditions since it |
| 113 | * allows the largest inputs to be processed at once and ensures that once the |
| 114 | * output data leaves, the whole buffer is available at once. |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 115 | */ |
| 116 | void buffer_slow_realign(struct buffer *buf) |
| 117 | { |
Willy Tarreau | 7ec7655 | 2015-07-02 12:50:23 +0200 | [diff] [blame^] | 118 | int block1 = buf->o; |
| 119 | int block2 = 0; |
| 120 | |
| 121 | /* process output data in two steps to cover wrapping */ |
| 122 | if (block1 > buf->p - buf->data) { |
| 123 | block2 = buf->p - buf->data; |
| 124 | block1 -= block2; |
| 125 | } |
| 126 | memcpy(swap_buffer + buf->size - buf->o, bo_ptr(buf), block1); |
| 127 | memcpy(swap_buffer + buf->size - block2, buf->data, block2); |
| 128 | |
| 129 | /* process input data in two steps to cover wrapping */ |
| 130 | block1 = buf->i; |
| 131 | block2 = 0; |
| 132 | |
| 133 | if (block1 > buf->data + buf->size - buf->p) { |
| 134 | block1 = buf->data + buf->size - buf->p; |
| 135 | block2 = buf->i - block1; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 136 | } |
Willy Tarreau | 7ec7655 | 2015-07-02 12:50:23 +0200 | [diff] [blame^] | 137 | memcpy(swap_buffer, bi_ptr(buf), block1); |
| 138 | memcpy(swap_buffer + block1, buf->data, block2); |
| 139 | |
| 140 | /* reinject changes into the buffer */ |
| 141 | memcpy(buf->data, swap_buffer, buf->i); |
| 142 | memcpy(buf->data + buf->size - buf->o, swap_buffer + buf->size - buf->o, buf->o); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 143 | |
| 144 | buf->p = buf->data; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /* Realigns a possibly non-contiguous buffer by bouncing bytes from source to |
| 149 | * destination. It does not use any intermediate buffer and does the move in |
| 150 | * place, though it will be slower than a simple memmove() on contiguous data, |
| 151 | * so it's desirable to use it only on non-contiguous buffers. No pointers are |
| 152 | * changed, the caller is responsible for that. |
| 153 | */ |
| 154 | void buffer_bounce_realign(struct buffer *buf) |
| 155 | { |
| 156 | int advance, to_move; |
| 157 | char *from, *to; |
| 158 | |
| 159 | from = bo_ptr(buf); |
| 160 | advance = buf->data + buf->size - from; |
| 161 | if (!advance) |
| 162 | return; |
| 163 | |
| 164 | to_move = buffer_len(buf); |
| 165 | while (to_move) { |
| 166 | char last, save; |
| 167 | |
| 168 | last = *from; |
| 169 | to = from + advance; |
| 170 | if (to >= buf->data + buf->size) |
| 171 | to -= buf->size; |
| 172 | |
| 173 | while (1) { |
| 174 | save = *to; |
| 175 | *to = last; |
| 176 | last = save; |
| 177 | to_move--; |
| 178 | if (!to_move) |
| 179 | break; |
| 180 | |
| 181 | /* check if we went back home after rotating a number of bytes */ |
| 182 | if (to == from) |
| 183 | break; |
| 184 | |
| 185 | /* if we ended up in the empty area, let's walk to next place. The |
| 186 | * empty area is either between buf->r and from or before from or |
| 187 | * after buf->r. |
| 188 | */ |
| 189 | if (from > bi_end(buf)) { |
| 190 | if (to >= bi_end(buf) && to < from) |
| 191 | break; |
| 192 | } else if (from < bi_end(buf)) { |
| 193 | if (to < from || to >= bi_end(buf)) |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | /* we have overwritten a byte of the original set, let's move it */ |
| 198 | to += advance; |
| 199 | if (to >= buf->data + buf->size) |
| 200 | to -= buf->size; |
| 201 | } |
| 202 | |
| 203 | from++; |
| 204 | if (from >= buf->data + buf->size) |
| 205 | from -= buf->size; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /* |
| 211 | * Dumps part or all of a buffer. |
| 212 | */ |
| 213 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 214 | { |
| 215 | fprintf(o, "Dumping buffer %p\n", b); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 216 | fprintf(o, " data=%p o=%d i=%d p=%p\n" |
| 217 | " relative: p=0x%04x\n", |
| 218 | b->data, b->o, b->i, b->p, (unsigned int)(b->p - b->data)); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 219 | |
| 220 | fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 221 | fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
| 222 | /* dump hexa */ |
| 223 | while (from < to) { |
| 224 | int i; |
| 225 | |
| 226 | fprintf(o, " %04x: ", from); |
| 227 | for (i = 0; ((from + i) < to) && (i < 16) ; i++) { |
| 228 | fprintf(o, "%02x ", (unsigned char)b->data[from + i]); |
| 229 | if (((from + i) & 15) == 7) |
| 230 | fprintf(o, "- "); |
| 231 | } |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 232 | if (to - from < 16) { |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 233 | int j = 0; |
| 234 | |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 235 | for (j = 0; j < from + 16 - to; j++) |
| 236 | fprintf(o, " "); |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 237 | if (j > 8) |
| 238 | fprintf(o, " "); |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 239 | } |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 240 | fprintf(o, " "); |
| 241 | for (i = 0; (from + i < to) && (i < 16) ; i++) { |
Willy Tarreau | 95898ac | 2012-11-26 00:57:40 +0100 | [diff] [blame] | 242 | fprintf(o, "%c", isprint((int)b->data[from + i]) ? b->data[from + i] : '.') ; |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 243 | if ((((from + i) & 15) == 15) && ((from + i) != to-1)) |
| 244 | fprintf(o, "\n"); |
| 245 | } |
| 246 | from += i; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 247 | } |
| 248 | fprintf(o, "\n--\n"); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 249 | fflush(o); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | /* |
| 254 | * Local variables: |
| 255 | * c-indent-level: 8 |
| 256 | * c-basic-offset: 8 |
| 257 | * End: |
| 258 | */ |