blob: 60fb3fce521ee0f9a9edcf9cce7baad90b7b4587 [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
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 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
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 Tarreauc7e42382012-08-24 19:22:53 +0200105/* 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 */
109void 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 */
140void 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 */
199void buffer_dump(FILE *o, struct buffer *b, int from, int to)
200{
201 fprintf(o, "Dumping buffer %p\n", b);
William Lallemandbe0efd82012-11-22 18:01:40 +0100202 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 Tarreauc7e42382012-08-24 19:22:53 +0200205
206 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
William Lallemandbe0efd82012-11-22 18:01:40 +0100207 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 Tarreau95898ac2012-11-26 00:57:40 +0100220 fprintf(o, "%c", isprint((int)b->data[from + i]) ? b->data[from + i] : '.') ;
William Lallemandbe0efd82012-11-22 18:01:40 +0100221 if ((((from + i) & 15) == 15) && ((from + i) != to-1))
222 fprintf(o, "\n");
223 }
224 from += i;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200225 }
226 fprintf(o, "\n--\n");
William Lallemandbe0efd82012-11-22 18:01:40 +0100227 fflush(o);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200228}
229
230
231/*
232 * Local variables:
233 * c-indent-level: 8
234 * c-basic-offset: 8
235 * End:
236 */