blob: 7becb4863dbf078b5c9e62c4d1c392eb75b659fa [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 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +020099int bo_inject(struct buffer *buf, const char *msg, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100{
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 Tarreau9dab5fc2012-05-07 11:56:55 +0200126 if (bi_full(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 Tarreau9dab5fc2012-05-07 11:56:55 +0200139int bi_putchr(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++;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200150 if (bi_full(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 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200174int bi_putblk(struct buffer *buf, const char *blk, int len)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200175{
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 Tarreau9dab5fc2012-05-07 11:56:55 +0200215 if (bi_full(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 Tarreau9dab5fc2012-05-07 11:56:55 +0200228 * The buffer status is not changed. The caller must call bo_skip() to
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200229 * 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 Tarreau9dab5fc2012-05-07 11:56:55 +0200233int bo_getline(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.
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200278 * The buffer status is not changed. The caller must call bo_skip() to
Willy Tarreau74b08c92010-09-08 17:04:31 +0200279 * update it.
280 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200281int bo_getblk(struct buffer *buf, char *blk, int len, int offset)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200282{
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 Tarreau9dab5fc2012-05-07 11:56:55 +0200346 if (bi_full(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 Tarreau9dab5fc2012-05-07 11:56:55 +0200384 if (bi_full(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 Tarreaua7fe8e52012-05-08 20:40:09 +0200391/* This function realigns input data in a possibly wrapping buffer so that it
392 * becomes contiguous and starts at the beginning of the buffer area. The
393 * function may only be used when the buffer's output is empty.
394 */
395void buffer_slow_realign(struct buffer *buf)
396{
397 /* two possible cases :
398 * - the buffer is in one contiguous block, we move it in-place
399 * - the buffer is in two blocks, we move it via the swap_buffer
400 */
401 if (buf->i) {
402 int block1 = buf->i;
403 int block2 = 0;
404 if (buf->p + buf->i > buf->data + buf->size) {
405 /* non-contiguous block */
406 block1 = buf->data + buf->size - buf->p;
407 block2 = buf->p + buf->i - (buf->data + buf->size);
408 }
409 if (block2)
410 memcpy(swap_buffer, buf->data, block2);
411 memmove(buf->data, buf->p, block1);
412 if (block2)
413 memcpy(buf->data + block1, swap_buffer, block2);
414 }
415
416 buf->p = buf->data;
417}
418
Willy Tarreaub97f1992010-02-25 23:54:31 +0100419/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
420 * destination. It does not use any intermediate buffer and does the move in
421 * place, though it will be slower than a simple memmove() on contiguous data,
422 * so it's desirable to use it only on non-contiguous buffers. No pointers are
423 * changed, the caller is responsible for that.
424 */
425void buffer_bounce_realign(struct buffer *buf)
426{
427 int advance, to_move;
428 char *from, *to;
429
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200430 from = bo_ptr(buf);
Willy Tarreau89fa7062012-03-02 16:13:16 +0100431 advance = buf->data + buf->size - from;
Willy Tarreaub97f1992010-02-25 23:54:31 +0100432 if (!advance)
433 return;
434
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100435 to_move = buffer_len(buf);
Willy Tarreaub97f1992010-02-25 23:54:31 +0100436 while (to_move) {
437 char last, save;
438
439 last = *from;
440 to = from + advance;
441 if (to >= buf->data + buf->size)
442 to -= buf->size;
443
444 while (1) {
445 save = *to;
446 *to = last;
447 last = save;
448 to_move--;
449 if (!to_move)
450 break;
451
452 /* check if we went back home after rotating a number of bytes */
453 if (to == from)
454 break;
455
456 /* if we ended up in the empty area, let's walk to next place. The
457 * empty area is either between buf->r and from or before from or
458 * after buf->r.
459 */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200460 if (from > bi_end(buf)) {
461 if (to >= bi_end(buf) && to < from)
Willy Tarreaub97f1992010-02-25 23:54:31 +0100462 break;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200463 } else if (from < bi_end(buf)) {
464 if (to < from || to >= bi_end(buf))
Willy Tarreaub97f1992010-02-25 23:54:31 +0100465 break;
466 }
467
468 /* we have overwritten a byte of the original set, let's move it */
469 to += advance;
470 if (to >= buf->data + buf->size)
471 to -= buf->size;
472 }
473
474 from++;
475 if (from >= buf->data + buf->size)
476 from -= buf->size;
477 }
478}
479
480
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100481/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100482 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200483 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100484 * the new chunk size.
485 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200486int chunk_printf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100487{
488 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200489 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100490
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200491 if (!chk->str || !chk->size)
492 return 0;
493
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100494 va_start(argp, fmt);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200495 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
496 if (ret >= chk->size - chk->len)
Willy Tarreaudceaa082007-07-25 14:38:45 +0200497 /* do not copy anything in case of truncation */
498 chk->str[chk->len] = 0;
499 else
500 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100501 va_end(argp);
502 return chk->len;
503}
504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100505/*
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200506 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
507 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
508 * If the chk->len is over, nothing is added. Returns the new chunk size.
509 */
510int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
511
512 int i, l;
513 int olen, free;
514 char c;
515
516 olen = dst->len;
517
518 for (i = 0; i < src->len; i++) {
519 free = dst->size - dst->len;
520
521 if (!free) {
522 dst->len = olen;
523 return dst->len;
524 }
525
526 c = src->str[i];
527
Willy Tarreau88e05812010-03-03 00:16:00 +0100528 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200529 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
530
531 if (free < l) {
532 dst->len = olen;
533 return dst->len;
534 }
535
536 dst->len += l;
537 } else {
538 dst->str[dst->len] = c;
539 dst->len++;
540 }
541 }
542
543 return dst->len;
544}
545
546/*
547 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
548 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
549 * If the chk->len is over, nothing is added. Returns the new chunk size.
550 */
551int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
552 int i, l;
553 int olen, free;
554 char c;
555
556 olen = dst->len;
557
558 for (i = 0; i < src->len; i++) {
559 free = dst->size - dst->len;
560
561 if (!free) {
562 dst->len = olen;
563 return dst->len;
564 }
565
566 c = src->str[i];
567
Willy Tarreau88e05812010-03-03 00:16:00 +0100568 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200569 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
570
571 if (free < l) {
572 dst->len = olen;
573 return dst->len;
574 }
575
576 dst->len += l;
577 } else {
578 dst->str[dst->len] = c;
579 dst->len++;
580 }
581 }
582
583 return dst->len;
584}
585
586/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100587 * Dumps part or all of a buffer.
588 */
589void buffer_dump(FILE *o, struct buffer *b, int from, int to)
590{
591 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreaua458b672012-03-05 11:17:50 +0100592 fprintf(o, " data=%p o=%d i=%d p=%p\n",
593 b->data, b->o, b->i, b->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100594
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100595 if (!to || to > buffer_len(b))
596 to = buffer_len(b);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100597
598 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
599 for (; from < to; from++) {
600 if ((from & 15) == 0)
601 fprintf(o, " %04x: ", from);
602 fprintf(o, "%02x ", b->data[from]);
603 if ((from & 15) == 7)
604 fprintf(o, "- ");
605 else if (((from & 15) == 15) && (from != to-1))
606 fprintf(o, "\n");
607 }
608 fprintf(o, "\n--\n");
609}
610
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100611
612/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613 * Local variables:
614 * c-indent-level: 8
615 * c-basic-offset: 8
616 * End:
617 */