blob: 9037dd3febfce042529ca8640432f7ddd8977dee [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
Godbacha6547c12014-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 Tarreauc7e42382012-08-24 19:22:53 +0200110/* This function realigns input data in a possibly wrapping buffer so that it
111 * becomes contiguous and starts at the beginning of the buffer area. The
112 * function may only be used when the buffer's output is empty.
113 */
114void buffer_slow_realign(struct buffer *buf)
115{
116 /* two possible cases :
117 * - the buffer is in one contiguous block, we move it in-place
118 * - the buffer is in two blocks, we move it via the swap_buffer
119 */
120 if (buf->i) {
121 int block1 = buf->i;
122 int block2 = 0;
123 if (buf->p + buf->i > buf->data + buf->size) {
124 /* non-contiguous block */
125 block1 = buf->data + buf->size - buf->p;
126 block2 = buf->p + buf->i - (buf->data + buf->size);
127 }
128 if (block2)
129 memcpy(swap_buffer, buf->data, block2);
130 memmove(buf->data, buf->p, block1);
131 if (block2)
132 memcpy(buf->data + block1, swap_buffer, block2);
133 }
134
135 buf->p = buf->data;
136}
137
138
139/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
140 * destination. It does not use any intermediate buffer and does the move in
141 * place, though it will be slower than a simple memmove() on contiguous data,
142 * so it's desirable to use it only on non-contiguous buffers. No pointers are
143 * changed, the caller is responsible for that.
144 */
145void buffer_bounce_realign(struct buffer *buf)
146{
147 int advance, to_move;
148 char *from, *to;
149
150 from = bo_ptr(buf);
151 advance = buf->data + buf->size - from;
152 if (!advance)
153 return;
154
155 to_move = buffer_len(buf);
156 while (to_move) {
157 char last, save;
158
159 last = *from;
160 to = from + advance;
161 if (to >= buf->data + buf->size)
162 to -= buf->size;
163
164 while (1) {
165 save = *to;
166 *to = last;
167 last = save;
168 to_move--;
169 if (!to_move)
170 break;
171
172 /* check if we went back home after rotating a number of bytes */
173 if (to == from)
174 break;
175
176 /* if we ended up in the empty area, let's walk to next place. The
177 * empty area is either between buf->r and from or before from or
178 * after buf->r.
179 */
180 if (from > bi_end(buf)) {
181 if (to >= bi_end(buf) && to < from)
182 break;
183 } else if (from < bi_end(buf)) {
184 if (to < from || to >= bi_end(buf))
185 break;
186 }
187
188 /* we have overwritten a byte of the original set, let's move it */
189 to += advance;
190 if (to >= buf->data + buf->size)
191 to -= buf->size;
192 }
193
194 from++;
195 if (from >= buf->data + buf->size)
196 from -= buf->size;
197 }
198}
199
200
201/*
202 * Dumps part or all of a buffer.
203 */
204void buffer_dump(FILE *o, struct buffer *b, int from, int to)
205{
206 fprintf(o, "Dumping buffer %p\n", b);
William Lallemandbe0efd82012-11-22 18:01:40 +0100207 fprintf(o, " data=%p o=%d i=%d p=%p\n"
208 " relative: p=0x%04x\n",
209 b->data, b->o, b->i, b->p, (unsigned int)(b->p - b->data));
Willy Tarreauc7e42382012-08-24 19:22:53 +0200210
211 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
William Lallemandbe0efd82012-11-22 18:01:40 +0100212 fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
213 /* dump hexa */
214 while (from < to) {
215 int i;
216
217 fprintf(o, " %04x: ", from);
218 for (i = 0; ((from + i) < to) && (i < 16) ; i++) {
219 fprintf(o, "%02x ", (unsigned char)b->data[from + i]);
220 if (((from + i) & 15) == 7)
221 fprintf(o, "- ");
222 }
Godbachc08057c2013-11-14 10:15:20 +0800223 if (to - from < 16) {
Godbachc3916a72013-11-21 10:21:22 +0800224 int j = 0;
225
Godbachc08057c2013-11-14 10:15:20 +0800226 for (j = 0; j < from + 16 - to; j++)
227 fprintf(o, " ");
Godbachc3916a72013-11-21 10:21:22 +0800228 if (j > 8)
229 fprintf(o, " ");
Godbachc08057c2013-11-14 10:15:20 +0800230 }
William Lallemandbe0efd82012-11-22 18:01:40 +0100231 fprintf(o, " ");
232 for (i = 0; (from + i < to) && (i < 16) ; i++) {
Willy Tarreau95898ac2012-11-26 00:57:40 +0100233 fprintf(o, "%c", isprint((int)b->data[from + i]) ? b->data[from + i] : '.') ;
William Lallemandbe0efd82012-11-22 18:01:40 +0100234 if ((((from + i) & 15) == 15) && ((from + i) != to-1))
235 fprintf(o, "\n");
236 }
237 from += i;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200238 }
239 fprintf(o, "\n--\n");
William Lallemandbe0efd82012-11-22 18:01:40 +0100240 fflush(o);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200241}
242
243
244/*
245 * Local variables:
246 * c-indent-level: 8
247 * c-basic-offset: 8
248 * End:
249 */