blob: 6eeb64f548615b0f0e6b02e44376ba34eaeb74b2 [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 Tarreau743a2d32012-05-31 16:37:11 +0200157 b_adv(buf, 1);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200158 }
159
160 buf->total++;
161 return 1;
162}
163
164/* Tries to copy block <blk> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100165 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200166 * closed, -2 is returned. If the block is too large for this buffer, -3 is
167 * returned. If there is not enough room left in the buffer, -1 is returned.
168 * Otherwise the number of bytes copied is returned (0 being a valid number).
169 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
170 * transferred.
171 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200172int bi_putblk(struct buffer *buf, const char *blk, int len)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200173{
174 int max;
175
176 if (unlikely(buffer_input_closed(buf)))
177 return -2;
178
Willy Tarreau591fedc2010-08-10 15:28:21 +0200179 max = buffer_max_len(buf);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100180 if (unlikely(len > max - buffer_len(buf))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200181 /* we can't write this chunk right now because the buffer is
182 * almost full or because the block is too large. Return the
183 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200184 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200185 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200186 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200187
Willy Tarreau74b08c92010-09-08 17:04:31 +0200188 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200189 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100190
Willy Tarreau74b08c92010-09-08 17:04:31 +0200191 if (unlikely(len == 0))
192 return 0;
193
Willy Tarreau591fedc2010-08-10 15:28:21 +0200194 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100195 max = buffer_contig_space_with_res(buf, buf->size - max);
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200196 memcpy(bi_end(buf), blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200197 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200198 memcpy(buf->data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100199
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100200 buf->i += len;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200201 buf->total += len;
Willy Tarreau31971e52009-09-20 12:07:52 +0200202 if (buf->to_forward) {
203 unsigned long fwd = len;
204 if (buf->to_forward != BUF_INFINITE_FORWARD) {
205 if (fwd > buf->to_forward)
206 fwd = buf->to_forward;
207 buf->to_forward -= fwd;
208 }
Willy Tarreau328582c2012-05-05 23:32:27 +0200209 b_adv(buf, fwd);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200210 }
211
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200212 buf->flags &= ~BF_FULL;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200213 if (bi_full(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200214 buf->flags |= BF_FULL;
215
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200216 /* notify that some data was read from the SI into the buffer */
217 buf->flags |= BF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200218 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100219}
220
Willy Tarreau74b08c92010-09-08 17:04:31 +0200221/* Gets one text line out of a buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200222 * Return values :
223 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200224 * =0 : no '\n' before end found. <str> is left undefined.
225 * <0 : no more bytes readable because output is shut.
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200226 * The buffer status is not changed. The caller must call bo_skip() to
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200227 * update it. The '\n' is waited for as long as neither the buffer nor the
228 * output are full. If either of them is full, the string may be returned
229 * as is, without the '\n'.
230 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200231int bo_getline(struct buffer *buf, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200232{
233 int ret, max;
234 char *p;
235
236 ret = 0;
237 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200238
239 /* closed or empty + imminent close = -1; empty = 0 */
240 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200241 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
242 ret = -1;
243 goto out;
244 }
245
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200246 p = bo_ptr(buf);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200247
Willy Tarreau2e046c62012-03-01 16:08:30 +0100248 if (max > buf->o) {
249 max = buf->o;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200250 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200251 }
252 while (max) {
253 *str++ = *p;
254 ret++;
255 max--;
256
257 if (*p == '\n')
258 break;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100259 p = buffer_wrap_add(buf, p + 1);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200260 }
Willy Tarreau2e046c62012-03-01 16:08:30 +0100261 if (ret > 0 && ret < len && ret < buf->o &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200262 *(str-1) != '\n' &&
263 !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200264 ret = 0;
265 out:
266 if (max)
267 *str = 0;
268 return ret;
269}
270
Willy Tarreau74b08c92010-09-08 17:04:31 +0200271/* Gets one full block of data at once from a buffer, optionally from a
272 * specific offset. Return values :
273 * >0 : number of bytes read, equal to requested size.
274 * =0 : not enough data available. <blk> is left undefined.
275 * <0 : no more bytes readable because output is shut.
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200276 * The buffer status is not changed. The caller must call bo_skip() to
Willy Tarreau74b08c92010-09-08 17:04:31 +0200277 * update it.
278 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200279int bo_getblk(struct buffer *buf, char *blk, int len, int offset)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200280{
281 int firstblock;
282
283 if (buf->flags & BF_SHUTW)
284 return -1;
285
Willy Tarreau2e046c62012-03-01 16:08:30 +0100286 if (len + offset > buf->o) {
Willy Tarreau74b08c92010-09-08 17:04:31 +0200287 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
288 return -1;
289 return 0;
290 }
291
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200292 firstblock = buf->data + buf->size - bo_ptr(buf);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200293 if (firstblock > offset) {
294 if (firstblock >= len + offset) {
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200295 memcpy(blk, bo_ptr(buf) + offset, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200296 return len;
297 }
298
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200299 memcpy(blk, bo_ptr(buf) + offset, firstblock - offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200300 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
301 return len;
302 }
303
304 memcpy(blk, buf->data + offset - firstblock, len);
305 return len;
306}
307
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100308/* This function writes the string <str> at position <pos> which must be in
309 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
Willy Tarreaua458b672012-03-05 11:17:50 +0100310 * <l> and <r> are updated to be valid after the shift. The shift value
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100311 * (positive or negative) is returned. If there's no space left, the move is
Willy Tarreaua458b672012-03-05 11:17:50 +0100312 * not done. The function does not adjust ->o nor BF_OUT_EMPTY because it
313 * does not make sense to use it on data scheduled to be sent. For the same
314 * reason, it does not make sense to call this function on unparsed data, so
315 * <orig> is not updated. The string length is taken from parameter <len>. If
316 * <len> is null, the <str> pointer is allowed to be null.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100318int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319{
320 int delta;
321
322 delta = len - (end - pos);
323
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200324 if (bi_end(b) + delta >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325 return 0; /* no space left */
326
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100327 if (buffer_not_empty(b) &&
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200328 bi_end(b) + delta > bo_ptr(b) &&
329 bo_ptr(b) >= bi_end(b))
Willy Tarreaubbfa7932010-01-25 01:49:57 +0100330 return 0; /* no space left before wrapping data */
331
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332 /* first, protect the end of the buffer */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200333 memmove(end + delta, end, bi_end(b) - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334
335 /* now, copy str over pos */
336 if (len)
337 memcpy(pos, str, len);
338
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100339 b->i += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200341 b->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100342 if (buffer_len(b) == 0)
Willy Tarreaua458b672012-03-05 11:17:50 +0100343 b->p = b->data;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200344 if (bi_full(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200345 b->flags |= BF_FULL;
346
Willy Tarreaubaaee002006-06-26 02:48:02 +0200347 return delta;
348}
349
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100351 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
352 * argument informs about the length of string <str> so that we don't have to
353 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
354 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreaua458b672012-03-05 11:17:50 +0100355 * some circumstances. The send limit is *not* adjusted. Same comments as above
356 * for the valid use cases.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100357 *
358 * The number of bytes added is returned on success. 0 is returned on failure.
359 */
360int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
361{
362 int delta;
363
364 delta = len + 2;
365
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200366 if (bi_end(b) + delta >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100367 return 0; /* no space left */
368
369 /* first, protect the end of the buffer */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200370 memmove(pos + delta, pos, bi_end(b) - pos);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100371
372 /* now, copy str over pos */
373 if (len && str) {
374 memcpy(pos, str, len);
375 pos[len] = '\r';
376 pos[len + 1] = '\n';
377 }
378
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100379 b->i += delta;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100380
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200381 b->flags &= ~BF_FULL;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200382 if (bi_full(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200383 b->flags |= BF_FULL;
384
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100385 return delta;
386}
387
388
Willy Tarreaua7fe8e52012-05-08 20:40:09 +0200389/* This function realigns input data in a possibly wrapping buffer so that it
390 * becomes contiguous and starts at the beginning of the buffer area. The
391 * function may only be used when the buffer's output is empty.
392 */
393void buffer_slow_realign(struct buffer *buf)
394{
395 /* two possible cases :
396 * - the buffer is in one contiguous block, we move it in-place
397 * - the buffer is in two blocks, we move it via the swap_buffer
398 */
399 if (buf->i) {
400 int block1 = buf->i;
401 int block2 = 0;
402 if (buf->p + buf->i > buf->data + buf->size) {
403 /* non-contiguous block */
404 block1 = buf->data + buf->size - buf->p;
405 block2 = buf->p + buf->i - (buf->data + buf->size);
406 }
407 if (block2)
408 memcpy(swap_buffer, buf->data, block2);
409 memmove(buf->data, buf->p, block1);
410 if (block2)
411 memcpy(buf->data + block1, swap_buffer, block2);
412 }
413
414 buf->p = buf->data;
415}
416
Willy Tarreaub97f1992010-02-25 23:54:31 +0100417/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
418 * destination. It does not use any intermediate buffer and does the move in
419 * place, though it will be slower than a simple memmove() on contiguous data,
420 * so it's desirable to use it only on non-contiguous buffers. No pointers are
421 * changed, the caller is responsible for that.
422 */
423void buffer_bounce_realign(struct buffer *buf)
424{
425 int advance, to_move;
426 char *from, *to;
427
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200428 from = bo_ptr(buf);
Willy Tarreau89fa7062012-03-02 16:13:16 +0100429 advance = buf->data + buf->size - from;
Willy Tarreaub97f1992010-02-25 23:54:31 +0100430 if (!advance)
431 return;
432
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100433 to_move = buffer_len(buf);
Willy Tarreaub97f1992010-02-25 23:54:31 +0100434 while (to_move) {
435 char last, save;
436
437 last = *from;
438 to = from + advance;
439 if (to >= buf->data + buf->size)
440 to -= buf->size;
441
442 while (1) {
443 save = *to;
444 *to = last;
445 last = save;
446 to_move--;
447 if (!to_move)
448 break;
449
450 /* check if we went back home after rotating a number of bytes */
451 if (to == from)
452 break;
453
454 /* if we ended up in the empty area, let's walk to next place. The
455 * empty area is either between buf->r and from or before from or
456 * after buf->r.
457 */
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200458 if (from > bi_end(buf)) {
459 if (to >= bi_end(buf) && to < from)
Willy Tarreaub97f1992010-02-25 23:54:31 +0100460 break;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +0200461 } else if (from < bi_end(buf)) {
462 if (to < from || to >= bi_end(buf))
Willy Tarreaub97f1992010-02-25 23:54:31 +0100463 break;
464 }
465
466 /* we have overwritten a byte of the original set, let's move it */
467 to += advance;
468 if (to >= buf->data + buf->size)
469 to -= buf->size;
470 }
471
472 from++;
473 if (from >= buf->data + buf->size)
474 from -= buf->size;
475 }
476}
477
478
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100479/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100480 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200481 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100482 * the new chunk size.
483 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200484int chunk_printf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100485{
486 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200487 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100488
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200489 if (!chk->str || !chk->size)
490 return 0;
491
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100492 va_start(argp, fmt);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200493 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
494 if (ret >= chk->size - chk->len)
Willy Tarreaudceaa082007-07-25 14:38:45 +0200495 /* do not copy anything in case of truncation */
496 chk->str[chk->len] = 0;
497 else
498 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100499 va_end(argp);
500 return chk->len;
501}
502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100503/*
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200504 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
505 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
506 * If the chk->len is over, nothing is added. Returns the new chunk size.
507 */
508int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
509
510 int i, l;
511 int olen, free;
512 char c;
513
514 olen = dst->len;
515
516 for (i = 0; i < src->len; i++) {
517 free = dst->size - dst->len;
518
519 if (!free) {
520 dst->len = olen;
521 return dst->len;
522 }
523
524 c = src->str[i];
525
Willy Tarreau88e05812010-03-03 00:16:00 +0100526 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200527 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
528
529 if (free < l) {
530 dst->len = olen;
531 return dst->len;
532 }
533
534 dst->len += l;
535 } else {
536 dst->str[dst->len] = c;
537 dst->len++;
538 }
539 }
540
541 return dst->len;
542}
543
544/*
545 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
546 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
547 * If the chk->len is over, nothing is added. Returns the new chunk size.
548 */
549int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
550 int i, l;
551 int olen, free;
552 char c;
553
554 olen = dst->len;
555
556 for (i = 0; i < src->len; i++) {
557 free = dst->size - dst->len;
558
559 if (!free) {
560 dst->len = olen;
561 return dst->len;
562 }
563
564 c = src->str[i];
565
Willy Tarreau88e05812010-03-03 00:16:00 +0100566 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200567 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
568
569 if (free < l) {
570 dst->len = olen;
571 return dst->len;
572 }
573
574 dst->len += l;
575 } else {
576 dst->str[dst->len] = c;
577 dst->len++;
578 }
579 }
580
581 return dst->len;
582}
583
584/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100585 * Dumps part or all of a buffer.
586 */
587void buffer_dump(FILE *o, struct buffer *b, int from, int to)
588{
589 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreaua458b672012-03-05 11:17:50 +0100590 fprintf(o, " data=%p o=%d i=%d p=%p\n",
591 b->data, b->o, b->i, b->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100592
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100593 if (!to || to > buffer_len(b))
594 to = buffer_len(b);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100595
596 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
597 for (; from < to; from++) {
598 if ((from & 15) == 0)
599 fprintf(o, " %04x: ", from);
600 fprintf(o, "%02x ", b->data[from]);
601 if ((from & 15) == 7)
602 fprintf(o, "- ");
603 else if (((from & 15) == 15) && (from != to-1))
604 fprintf(o, "\n");
605 }
606 fprintf(o, "\n--\n");
607}
608
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100609
610/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611 * Local variables:
612 * c-indent-level: 8
613 * c-basic-offset: 8
614 * End:
615 */