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