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