blob: 415859b4fdadca1c26134a4b0f24358480ed32d8 [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
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 Lallemandbe0efd82012-11-22 18:01:40 +010013#include <ctype.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
18#include <common/buffer.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020019#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020020
21#include <types/global.h>
22
Willy Tarreau9b28e032012-10-12 23:49:43 +020023struct pool_head *pool2_buffer;
24
25
26/* perform minimal intializations, report 0 in case of error, 1 if OK. */
27int 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 Tarreauaf819352012-08-27 22:08:00 +020033/* 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 */
43int 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 FOURNIERb9290282015-03-10 01:55:01 +010049 if (bi_end(b) + delta > b->data + b->size)
Willy Tarreauaf819352012-08-27 22:08:00 +020050 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 Tarreau5fb38032012-12-16 19:39:09 +010066 if (buffer_empty(b))
Willy Tarreauaf819352012-08-27 22:08:00 +020067 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 */
82int 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
Godbache31de832014-10-31 13:16:37 +080091 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 Tarreauaf819352012-08-27 22:08:00 +020096 /* 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 Tarreau7ec76552015-07-02 12:50:23 +0200110/* 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 Tarreauc7e42382012-08-24 19:22:53 +0200115 */
116void buffer_slow_realign(struct buffer *buf)
117{
Willy Tarreau7ec76552015-07-02 12:50:23 +0200118 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 Tarreauc7e42382012-08-24 19:22:53 +0200136 }
Willy Tarreau7ec76552015-07-02 12:50:23 +0200137 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 Tarreauc7e42382012-08-24 19:22:53 +0200143
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 */
154void 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 */
213void buffer_dump(FILE *o, struct buffer *b, int from, int to)
214{
215 fprintf(o, "Dumping buffer %p\n", b);
William Lallemandbe0efd82012-11-22 18:01:40 +0100216 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 Tarreauc7e42382012-08-24 19:22:53 +0200219
220 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
William Lallemandbe0efd82012-11-22 18:01:40 +0100221 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 }
Godbachc08057c2013-11-14 10:15:20 +0800232 if (to - from < 16) {
Godbachc3916a72013-11-21 10:21:22 +0800233 int j = 0;
234
Godbachc08057c2013-11-14 10:15:20 +0800235 for (j = 0; j < from + 16 - to; j++)
236 fprintf(o, " ");
Godbachc3916a72013-11-21 10:21:22 +0800237 if (j > 8)
238 fprintf(o, " ");
Godbachc08057c2013-11-14 10:15:20 +0800239 }
William Lallemandbe0efd82012-11-22 18:01:40 +0100240 fprintf(o, " ");
241 for (i = 0; (from + i < to) && (i < 16) ; i++) {
Willy Tarreau95898ac2012-11-26 00:57:40 +0100242 fprintf(o, "%c", isprint((int)b->data[from + i]) ? b->data[from + i] : '.') ;
William Lallemandbe0efd82012-11-22 18:01:40 +0100243 if ((((from + i) & 15) == 15) && ((from + i) != to-1))
244 fprintf(o, "\n");
245 }
246 from += i;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200247 }
248 fprintf(o, "\n--\n");
William Lallemandbe0efd82012-11-22 18:01:40 +0100249 fflush(o);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200250}
251
252
253/*
254 * Local variables:
255 * c-indent-level: 8
256 * c-basic-offset: 8
257 * End:
258 */