blob: 2bd24a8c549b888dbcf7f6b4e1f9d67425c3be16 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Buffer management functions.
3 *
Willy Tarreaub97f1992010-02-25 23:54:31 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +020013#include <ctype.h>
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010014#include <stdarg.h>
15#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <string.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020017
18#include <common/config.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <proto/buffers.h>
Willy Tarreau27a674e2009-08-17 07:23:33 +020021#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022
Willy Tarreau7341d942007-05-13 19:56:02 +020023struct pool_head *pool2_buffer;
24
25
26/* perform minimal intializations, report 0 in case of error, 1 if OK. */
27int init_buffer()
28{
Willy Tarreau27a674e2009-08-17 07:23:33 +020029 pool2_buffer = create_pool("buffer", sizeof(struct buffer) + global.tune.bufsize, MEM_F_SHARED);
Willy Tarreau7341d942007-05-13 19:56:02 +020030 return pool2_buffer != NULL;
31}
32
Willy Tarreau0bc34932011-03-28 16:25:58 +020033/* Schedule up to <bytes> more bytes to be forwarded by the buffer without notifying
34 * the task. Any pending data in the buffer is scheduled to be sent as well,
35 * in the limit of the number of bytes to forward. This must be the only method
36 * to use to schedule bytes to be sent. If the requested number is too large, it
37 * is automatically adjusted. The number of bytes taken into account is returned.
Willy Tarreau2e046c62012-03-01 16:08:30 +010038 * Directly touching ->to_forward will cause lockups when ->o goes down to
Willy Tarreau0bc34932011-03-28 16:25:58 +020039 * zero if nobody is ready to push the remaining data.
40 */
41unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes)
42{
Willy Tarreau0bc34932011-03-28 16:25:58 +020043 unsigned int new_forward;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010044 unsigned int forwarded;
Willy Tarreau328582c2012-05-05 23:32:27 +020045 unsigned int bytes32;
Willy Tarreau0bc34932011-03-28 16:25:58 +020046
Willy Tarreau328582c2012-05-05 23:32:27 +020047 bytes32 = bytes;
48
49 /* hint: avoid comparisons on long long for the fast case, since if the
50 * length does not fit in an unsigned it, it will never be forwarded at
51 * once anyway.
52 */
53 if (bytes <= ~0U) {
54 if (bytes32 <= buf->i) {
55 /* OK this amount of bytes might be forwarded at once */
56 if (!bytes32)
57 return 0;
58 b_adv(buf, bytes32);
59 return bytes;
60 }
Willy Tarreau0bc34932011-03-28 16:25:58 +020061 }
62
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010063 forwarded = buf->i;
Willy Tarreau328582c2012-05-05 23:32:27 +020064 b_adv(buf, buf->i);
Willy Tarreau0bc34932011-03-28 16:25:58 +020065
Willy Tarreau0bc34932011-03-28 16:25:58 +020066 /* Note: the case below is the only case where we may return
67 * a byte count that does not fit into a 32-bit number.
68 */
69 if (likely(buf->to_forward == BUF_INFINITE_FORWARD))
70 return bytes;
71
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010072 if (likely(bytes == BUF_INFINITE_FORWARD)) {
73 buf->to_forward = bytes;
74 return bytes;
75 }
76
77 new_forward = buf->to_forward + bytes - forwarded;
78 bytes = forwarded; /* at least those bytes were scheduled */
Willy Tarreau0bc34932011-03-28 16:25:58 +020079
80 if (new_forward <= buf->to_forward) {
81 /* integer overflow detected, let's assume no more than 2G at once */
82 new_forward = MID_RANGE(new_forward);
83 }
84
85 if (new_forward > buf->to_forward) {
86 bytes += new_forward - buf->to_forward;
87 buf->to_forward = new_forward;
88 }
89 return bytes;
90}
Willy Tarreaubaaee002006-06-26 02:48:02 +020091
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020092/* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of
Willy Tarreau078e2942009-08-18 07:19:39 +020093 * success, -2 if the message is larger than the buffer size, or the number of
94 * bytes available otherwise. The send limit is automatically adjusted with the
95 * amount of data written. FIXME-20060521: handle unaligned data.
Willy Tarreau363a5bb2012-03-02 20:14:45 +010096 * Note: this function appends data to the buffer's output and possibly overwrites
97 * any pending input data which are assumed not to exist.
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 */
99int buffer_write(struct buffer *buf, const char *msg, int len)
100{
101 int max;
102
Willy Tarreauaeac3192009-08-31 08:09:57 +0200103 if (len == 0)
104 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105
Willy Tarreau078e2942009-08-18 07:19:39 +0200106 if (len > buf->size) {
107 /* we can't write this chunk and will never be able to, because
108 * it is larger than the buffer. This must be reported as an
109 * error. Then we return -2 so that writers that don't care can
110 * ignore it and go on, and others can check for this value.
111 */
112 return -2;
113 }
114
Willy Tarreauaeac3192009-08-31 08:09:57 +0200115 max = buffer_realign(buf);
116
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 if (len > max)
118 return max;
119
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100120 memcpy(buf->p, msg, len);
Willy Tarreau2e046c62012-03-01 16:08:30 +0100121 buf->o += len;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200122 buf->p = b_ptr(buf, len);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100123 buf->total += len;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200124
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200125 buf->flags &= ~(BF_OUT_EMPTY|BF_FULL);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100126 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200127 buf->flags |= BF_FULL;
128
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200129 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130}
131
Willy Tarreau74b08c92010-09-08 17:04:31 +0200132/* Tries to copy character <c> into buffer <buf> after length controls. The
Willy Tarreau2e046c62012-03-01 16:08:30 +0100133 * ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200134 * closed, -2 is returned. If there is not enough room left in the buffer, -1
135 * is returned. Otherwise the number of bytes copied is returned (1). Buffer
136 * flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
137 * transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100138 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200139int buffer_put_char(struct buffer *buf, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100140{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200141 if (unlikely(buffer_input_closed(buf)))
142 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100143
Willy Tarreau74b08c92010-09-08 17:04:31 +0200144 if (buf->flags & BF_FULL)
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200145 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100146
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200147 *bi_end(buf) = c;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200148
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100149 buf->i++;
150 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200151 buf->flags |= BF_FULL;
152 buf->flags |= BF_READ_PARTIAL;
153
Willy Tarreau74b08c92010-09-08 17:04:31 +0200154 if (buf->to_forward >= 1) {
155 if (buf->to_forward != BUF_INFINITE_FORWARD)
156 buf->to_forward--;
Willy Tarreau2e046c62012-03-01 16:08:30 +0100157 buf->o++;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100158 buf->i--;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200159 buf->flags &= ~BF_OUT_EMPTY;
160 }
161
162 buf->total++;
163 return 1;
164}
165
166/* Tries to copy block <blk> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100167 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200168 * closed, -2 is returned. If the block is too large for this buffer, -3 is
169 * returned. If there is not enough room left in the buffer, -1 is returned.
170 * Otherwise the number of bytes copied is returned (0 being a valid number).
171 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
172 * transferred.
173 */
174int buffer_put_block(struct buffer *buf, const char *blk, int len)
175{
176 int max;
177
178 if (unlikely(buffer_input_closed(buf)))
179 return -2;
180
Willy Tarreau591fedc2010-08-10 15:28:21 +0200181 max = buffer_max_len(buf);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100182 if (unlikely(len > max - buffer_len(buf))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200183 /* we can't write this chunk right now because the buffer is
184 * almost full or because the block is too large. Return the
185 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200186 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200187 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200188 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200189
Willy Tarreau74b08c92010-09-08 17:04:31 +0200190 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200191 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100192
Willy Tarreau74b08c92010-09-08 17:04:31 +0200193 if (unlikely(len == 0))
194 return 0;
195
Willy Tarreau591fedc2010-08-10 15:28:21 +0200196 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100197 max = buffer_contig_space_with_res(buf, buf->size - max);
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200198 memcpy(bi_end(buf), blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200199 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200200 memcpy(buf->data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100201
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100202 buf->i += len;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200203 buf->total += len;
Willy Tarreau31971e52009-09-20 12:07:52 +0200204 if (buf->to_forward) {
205 unsigned long fwd = len;
206 if (buf->to_forward != BUF_INFINITE_FORWARD) {
207 if (fwd > buf->to_forward)
208 fwd = buf->to_forward;
209 buf->to_forward -= fwd;
210 }
Willy Tarreau328582c2012-05-05 23:32:27 +0200211 b_adv(buf, fwd);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200212 }
213
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200214 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100215 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200216 buf->flags |= BF_FULL;
217
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200218 /* notify that some data was read from the SI into the buffer */
219 buf->flags |= BF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200220 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100221}
222
Willy Tarreau74b08c92010-09-08 17:04:31 +0200223/* Gets one text line out of a buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200224 * Return values :
225 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200226 * =0 : no '\n' before end found. <str> is left undefined.
227 * <0 : no more bytes readable because output is shut.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200228 * The buffer status is not changed. The caller must call buffer_skip() to
229 * update it. The '\n' is waited for as long as neither the buffer nor the
230 * output are full. If either of them is full, the string may be returned
231 * as is, without the '\n'.
232 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200233int buffer_get_line(struct buffer *buf, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200234{
235 int ret, max;
236 char *p;
237
238 ret = 0;
239 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200240
241 /* closed or empty + imminent close = -1; empty = 0 */
242 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200243 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
244 ret = -1;
245 goto out;
246 }
247
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200248 p = bo_ptr(buf);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200249
Willy Tarreau2e046c62012-03-01 16:08:30 +0100250 if (max > buf->o) {
251 max = buf->o;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200252 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200253 }
254 while (max) {
255 *str++ = *p;
256 ret++;
257 max--;
258
259 if (*p == '\n')
260 break;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100261 p = buffer_wrap_add(buf, p + 1);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200262 }
Willy Tarreau2e046c62012-03-01 16:08:30 +0100263 if (ret > 0 && ret < len && ret < buf->o &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200264 *(str-1) != '\n' &&
265 !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200266 ret = 0;
267 out:
268 if (max)
269 *str = 0;
270 return ret;
271}
272
Willy Tarreau74b08c92010-09-08 17:04:31 +0200273/* Gets one full block of data at once from a buffer, optionally from a
274 * specific offset. Return values :
275 * >0 : number of bytes read, equal to requested size.
276 * =0 : not enough data available. <blk> is left undefined.
277 * <0 : no more bytes readable because output is shut.
278 * The buffer status is not changed. The caller must call buffer_skip() to
279 * update it.
280 */
281int buffer_get_block(struct buffer *buf, char *blk, int len, int offset)
282{
283 int firstblock;
284
285 if (buf->flags & BF_SHUTW)
286 return -1;
287
Willy Tarreau2e046c62012-03-01 16:08:30 +0100288 if (len + offset > buf->o) {
Willy Tarreau74b08c92010-09-08 17:04:31 +0200289 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
290 return -1;
291 return 0;
292 }
293
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200294 firstblock = buf->data + buf->size - bo_ptr(buf);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200295 if (firstblock > offset) {
296 if (firstblock >= len + offset) {
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200297 memcpy(blk, bo_ptr(buf) + offset, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200298 return len;
299 }
300
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200301 memcpy(blk, bo_ptr(buf) + offset, firstblock - offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200302 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
303 return len;
304 }
305
306 memcpy(blk, buf->data + offset - firstblock, len);
307 return len;
308}
309
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100310/* This function writes the string <str> at position <pos> which must be in
311 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
Willy Tarreaua458b672012-03-05 11:17:50 +0100312 * <l> and <r> are updated to be valid after the shift. The shift value
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100313 * (positive or negative) is returned. If there's no space left, the move is
Willy Tarreaua458b672012-03-05 11:17:50 +0100314 * not done. The function does not adjust ->o nor BF_OUT_EMPTY because it
315 * does not make sense to use it on data scheduled to be sent. For the same
316 * reason, it does not make sense to call this function on unparsed data, so
317 * <orig> is not updated. The string length is taken from parameter <len>. If
318 * <len> is null, the <str> pointer is allowed to be null.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100320int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321{
322 int delta;
323
324 delta = len - (end - pos);
325
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200326 if (bi_end(b) + delta >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 return 0; /* no space left */
328
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100329 if (buffer_not_empty(b) &&
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200330 bi_end(b) + delta > bo_ptr(b) &&
331 bo_ptr(b) >= bi_end(b))
Willy Tarreaubbfa7932010-01-25 01:49:57 +0100332 return 0; /* no space left before wrapping data */
333
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 /* first, protect the end of the buffer */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200335 memmove(end + delta, end, bi_end(b) - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336
337 /* now, copy str over pos */
338 if (len)
339 memcpy(pos, str, len);
340
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100341 b->i += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200342
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200343 b->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100344 if (buffer_len(b) == 0)
Willy Tarreaua458b672012-03-05 11:17:50 +0100345 b->p = b->data;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100346 if (buffer_len(b) >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200347 b->flags |= BF_FULL;
348
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349 return delta;
350}
351
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100353 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
354 * argument informs about the length of string <str> so that we don't have to
355 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
356 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreaua458b672012-03-05 11:17:50 +0100357 * some circumstances. The send limit is *not* adjusted. Same comments as above
358 * for the valid use cases.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100359 *
360 * The number of bytes added is returned on success. 0 is returned on failure.
361 */
362int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
363{
364 int delta;
365
366 delta = len + 2;
367
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200368 if (bi_end(b) + delta >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100369 return 0; /* no space left */
370
371 /* first, protect the end of the buffer */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200372 memmove(pos + delta, pos, bi_end(b) - pos);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100373
374 /* now, copy str over pos */
375 if (len && str) {
376 memcpy(pos, str, len);
377 pos[len] = '\r';
378 pos[len + 1] = '\n';
379 }
380
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100381 b->i += delta;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100382
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200383 b->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100384 if (buffer_len(b) >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200385 b->flags |= BF_FULL;
386
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100387 return delta;
388}
389
390
Willy Tarreaub97f1992010-02-25 23:54:31 +0100391/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
392 * destination. It does not use any intermediate buffer and does the move in
393 * place, though it will be slower than a simple memmove() on contiguous data,
394 * so it's desirable to use it only on non-contiguous buffers. No pointers are
395 * changed, the caller is responsible for that.
396 */
397void buffer_bounce_realign(struct buffer *buf)
398{
399 int advance, to_move;
400 char *from, *to;
401
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200402 from = bo_ptr(buf);
Willy Tarreau89fa7062012-03-02 16:13:16 +0100403 advance = buf->data + buf->size - from;
Willy Tarreaub97f1992010-02-25 23:54:31 +0100404 if (!advance)
405 return;
406
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100407 to_move = buffer_len(buf);
Willy Tarreaub97f1992010-02-25 23:54:31 +0100408 while (to_move) {
409 char last, save;
410
411 last = *from;
412 to = from + advance;
413 if (to >= buf->data + buf->size)
414 to -= buf->size;
415
416 while (1) {
417 save = *to;
418 *to = last;
419 last = save;
420 to_move--;
421 if (!to_move)
422 break;
423
424 /* check if we went back home after rotating a number of bytes */
425 if (to == from)
426 break;
427
428 /* if we ended up in the empty area, let's walk to next place. The
429 * empty area is either between buf->r and from or before from or
430 * after buf->r.
431 */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200432 if (from > bi_end(buf)) {
433 if (to >= bi_end(buf) && to < from)
Willy Tarreaub97f1992010-02-25 23:54:31 +0100434 break;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200435 } else if (from < bi_end(buf)) {
436 if (to < from || to >= bi_end(buf))
Willy Tarreaub97f1992010-02-25 23:54:31 +0100437 break;
438 }
439
440 /* we have overwritten a byte of the original set, let's move it */
441 to += advance;
442 if (to >= buf->data + buf->size)
443 to -= buf->size;
444 }
445
446 from++;
447 if (from >= buf->data + buf->size)
448 from -= buf->size;
449 }
450}
451
452
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100453/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100454 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200455 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100456 * the new chunk size.
457 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200458int chunk_printf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100459{
460 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200461 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100462
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200463 if (!chk->str || !chk->size)
464 return 0;
465
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100466 va_start(argp, fmt);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200467 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
468 if (ret >= chk->size - chk->len)
Willy Tarreaudceaa082007-07-25 14:38:45 +0200469 /* do not copy anything in case of truncation */
470 chk->str[chk->len] = 0;
471 else
472 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100473 va_end(argp);
474 return chk->len;
475}
476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100477/*
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200478 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
479 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
480 * If the chk->len is over, nothing is added. Returns the new chunk size.
481 */
482int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
483
484 int i, l;
485 int olen, free;
486 char c;
487
488 olen = dst->len;
489
490 for (i = 0; i < src->len; i++) {
491 free = dst->size - dst->len;
492
493 if (!free) {
494 dst->len = olen;
495 return dst->len;
496 }
497
498 c = src->str[i];
499
Willy Tarreau88e05812010-03-03 00:16:00 +0100500 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200501 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
502
503 if (free < l) {
504 dst->len = olen;
505 return dst->len;
506 }
507
508 dst->len += l;
509 } else {
510 dst->str[dst->len] = c;
511 dst->len++;
512 }
513 }
514
515 return dst->len;
516}
517
518/*
519 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
520 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
521 * If the chk->len is over, nothing is added. Returns the new chunk size.
522 */
523int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
524 int i, l;
525 int olen, free;
526 char c;
527
528 olen = dst->len;
529
530 for (i = 0; i < src->len; i++) {
531 free = dst->size - dst->len;
532
533 if (!free) {
534 dst->len = olen;
535 return dst->len;
536 }
537
538 c = src->str[i];
539
Willy Tarreau88e05812010-03-03 00:16:00 +0100540 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200541 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
542
543 if (free < l) {
544 dst->len = olen;
545 return dst->len;
546 }
547
548 dst->len += l;
549 } else {
550 dst->str[dst->len] = c;
551 dst->len++;
552 }
553 }
554
555 return dst->len;
556}
557
558/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100559 * Dumps part or all of a buffer.
560 */
561void buffer_dump(FILE *o, struct buffer *b, int from, int to)
562{
563 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreaua458b672012-03-05 11:17:50 +0100564 fprintf(o, " data=%p o=%d i=%d p=%p\n",
565 b->data, b->o, b->i, b->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100566
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100567 if (!to || to > buffer_len(b))
568 to = buffer_len(b);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100569
570 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
571 for (; from < to; from++) {
572 if ((from & 15) == 0)
573 fprintf(o, " %04x: ", from);
574 fprintf(o, "%02x ", b->data[from]);
575 if ((from & 15) == 7)
576 fprintf(o, "- ");
577 else if (((from & 15) == 15) && (from != to-1))
578 fprintf(o, "\n");
579 }
580 fprintf(o, "\n--\n");
581}
582
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100583
584/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200585 * Local variables:
586 * c-indent-level: 8
587 * c-basic-offset: 8
588 * End:
589 */