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 | |
| 49 | if (bi_end(b) + delta >= b->data + b->size) |
| 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 | |
| 91 | /* first, protect the end of the buffer */ |
| 92 | memmove(pos + delta, pos, bi_end(b) - pos); |
| 93 | |
| 94 | /* now, copy str over pos */ |
| 95 | if (len && str) { |
| 96 | memcpy(pos, str, len); |
| 97 | pos[len] = '\r'; |
| 98 | pos[len + 1] = '\n'; |
| 99 | } |
| 100 | |
| 101 | b->i += delta; |
| 102 | return delta; |
| 103 | } |
| 104 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 105 | /* This function realigns input data in a possibly wrapping buffer so that it |
| 106 | * becomes contiguous and starts at the beginning of the buffer area. The |
| 107 | * function may only be used when the buffer's output is empty. |
| 108 | */ |
| 109 | void buffer_slow_realign(struct buffer *buf) |
| 110 | { |
| 111 | /* two possible cases : |
| 112 | * - the buffer is in one contiguous block, we move it in-place |
| 113 | * - the buffer is in two blocks, we move it via the swap_buffer |
| 114 | */ |
| 115 | if (buf->i) { |
| 116 | int block1 = buf->i; |
| 117 | int block2 = 0; |
| 118 | if (buf->p + buf->i > buf->data + buf->size) { |
| 119 | /* non-contiguous block */ |
| 120 | block1 = buf->data + buf->size - buf->p; |
| 121 | block2 = buf->p + buf->i - (buf->data + buf->size); |
| 122 | } |
| 123 | if (block2) |
| 124 | memcpy(swap_buffer, buf->data, block2); |
| 125 | memmove(buf->data, buf->p, block1); |
| 126 | if (block2) |
| 127 | memcpy(buf->data + block1, swap_buffer, block2); |
| 128 | } |
| 129 | |
| 130 | buf->p = buf->data; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* Realigns a possibly non-contiguous buffer by bouncing bytes from source to |
| 135 | * destination. It does not use any intermediate buffer and does the move in |
| 136 | * place, though it will be slower than a simple memmove() on contiguous data, |
| 137 | * so it's desirable to use it only on non-contiguous buffers. No pointers are |
| 138 | * changed, the caller is responsible for that. |
| 139 | */ |
| 140 | void buffer_bounce_realign(struct buffer *buf) |
| 141 | { |
| 142 | int advance, to_move; |
| 143 | char *from, *to; |
| 144 | |
| 145 | from = bo_ptr(buf); |
| 146 | advance = buf->data + buf->size - from; |
| 147 | if (!advance) |
| 148 | return; |
| 149 | |
| 150 | to_move = buffer_len(buf); |
| 151 | while (to_move) { |
| 152 | char last, save; |
| 153 | |
| 154 | last = *from; |
| 155 | to = from + advance; |
| 156 | if (to >= buf->data + buf->size) |
| 157 | to -= buf->size; |
| 158 | |
| 159 | while (1) { |
| 160 | save = *to; |
| 161 | *to = last; |
| 162 | last = save; |
| 163 | to_move--; |
| 164 | if (!to_move) |
| 165 | break; |
| 166 | |
| 167 | /* check if we went back home after rotating a number of bytes */ |
| 168 | if (to == from) |
| 169 | break; |
| 170 | |
| 171 | /* if we ended up in the empty area, let's walk to next place. The |
| 172 | * empty area is either between buf->r and from or before from or |
| 173 | * after buf->r. |
| 174 | */ |
| 175 | if (from > bi_end(buf)) { |
| 176 | if (to >= bi_end(buf) && to < from) |
| 177 | break; |
| 178 | } else if (from < bi_end(buf)) { |
| 179 | if (to < from || to >= bi_end(buf)) |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | /* we have overwritten a byte of the original set, let's move it */ |
| 184 | to += advance; |
| 185 | if (to >= buf->data + buf->size) |
| 186 | to -= buf->size; |
| 187 | } |
| 188 | |
| 189 | from++; |
| 190 | if (from >= buf->data + buf->size) |
| 191 | from -= buf->size; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* |
| 197 | * Dumps part or all of a buffer. |
| 198 | */ |
| 199 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 200 | { |
| 201 | fprintf(o, "Dumping buffer %p\n", b); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 202 | fprintf(o, " data=%p o=%d i=%d p=%p\n" |
| 203 | " relative: p=0x%04x\n", |
| 204 | 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] | 205 | |
| 206 | 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] | 207 | fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
| 208 | /* dump hexa */ |
| 209 | while (from < to) { |
| 210 | int i; |
| 211 | |
| 212 | fprintf(o, " %04x: ", from); |
| 213 | for (i = 0; ((from + i) < to) && (i < 16) ; i++) { |
| 214 | fprintf(o, "%02x ", (unsigned char)b->data[from + i]); |
| 215 | if (((from + i) & 15) == 7) |
| 216 | fprintf(o, "- "); |
| 217 | } |
| 218 | fprintf(o, " "); |
| 219 | for (i = 0; (from + i < to) && (i < 16) ; i++) { |
Willy Tarreau | 95898ac | 2012-11-26 00:57:40 +0100 | [diff] [blame] | 220 | 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] | 221 | if ((((from + i) & 15) == 15) && ((from + i) != to-1)) |
| 222 | fprintf(o, "\n"); |
| 223 | } |
| 224 | from += i; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 225 | } |
| 226 | fprintf(o, "\n--\n"); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 227 | fflush(o); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | /* |
| 232 | * Local variables: |
| 233 | * c-indent-level: 8 |
| 234 | * c-basic-offset: 8 |
| 235 | * End: |
| 236 | */ |