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