blob: 74e3a1069c06abeb659970b0713c4a17f4f0086b [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 Tarreau0bc34932011-03-28 16:25:58 +020045
46 if (!bytes)
47 return 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010048 if (bytes <= (unsigned long long)buf->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +010049 buf->o += bytes;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010050 buf->i -= bytes;
Willy Tarreau0bc34932011-03-28 16:25:58 +020051 buf->flags &= ~BF_OUT_EMPTY;
52 return bytes;
53 }
54
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010055 forwarded = buf->i;
56 buf->o += forwarded;
57 buf->i = 0;
58
Willy Tarreau2e046c62012-03-01 16:08:30 +010059 if (buf->o)
Willy Tarreau0bc34932011-03-28 16:25:58 +020060 buf->flags &= ~BF_OUT_EMPTY;
61
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010062 if (buf->o < buffer_max_len(buf))
Willy Tarreau0bc34932011-03-28 16:25:58 +020063 buf->flags &= ~BF_FULL;
64 else
65 buf->flags |= BF_FULL;
66
Willy Tarreau0bc34932011-03-28 16:25:58 +020067 /* Note: the case below is the only case where we may return
68 * a byte count that does not fit into a 32-bit number.
69 */
70 if (likely(buf->to_forward == BUF_INFINITE_FORWARD))
71 return bytes;
72
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010073 if (likely(bytes == BUF_INFINITE_FORWARD)) {
74 buf->to_forward = bytes;
75 return bytes;
76 }
77
78 new_forward = buf->to_forward + bytes - forwarded;
79 bytes = forwarded; /* at least those bytes were scheduled */
Willy Tarreau0bc34932011-03-28 16:25:58 +020080
81 if (new_forward <= buf->to_forward) {
82 /* integer overflow detected, let's assume no more than 2G at once */
83 new_forward = MID_RANGE(new_forward);
84 }
85
86 if (new_forward > buf->to_forward) {
87 bytes += new_forward - buf->to_forward;
88 buf->to_forward = new_forward;
89 }
90 return bytes;
91}
Willy Tarreaubaaee002006-06-26 02:48:02 +020092
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020093/* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of
Willy Tarreau078e2942009-08-18 07:19:39 +020094 * success, -2 if the message is larger than the buffer size, or the number of
95 * bytes available otherwise. The send limit is automatically adjusted with the
96 * amount of data written. FIXME-20060521: handle unaligned data.
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 */
98int buffer_write(struct buffer *buf, const char *msg, int len)
99{
100 int max;
101
Willy Tarreauaeac3192009-08-31 08:09:57 +0200102 if (len == 0)
103 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104
Willy Tarreau078e2942009-08-18 07:19:39 +0200105 if (len > buf->size) {
106 /* we can't write this chunk and will never be able to, because
107 * it is larger than the buffer. This must be reported as an
108 * error. Then we return -2 so that writers that don't care can
109 * ignore it and go on, and others can check for this value.
110 */
111 return -2;
112 }
113
Willy Tarreauaeac3192009-08-31 08:09:57 +0200114 max = buffer_realign(buf);
115
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 if (len > max)
117 return max;
118
119 memcpy(buf->r, msg, len);
Willy Tarreau2e046c62012-03-01 16:08:30 +0100120 buf->o += len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121 buf->r += len;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100122 buf->total += len;
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200123 if (buf->r == buf->data + buf->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 buf->r = buf->data;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200125
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200126 buf->flags &= ~(BF_OUT_EMPTY|BF_FULL);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100127 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200128 buf->flags |= BF_FULL;
129
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200130 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131}
132
Willy Tarreau74b08c92010-09-08 17:04:31 +0200133/* Tries to copy character <c> into buffer <buf> after length controls. The
Willy Tarreau2e046c62012-03-01 16:08:30 +0100134 * ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200135 * closed, -2 is returned. If there is not enough room left in the buffer, -1
136 * is returned. Otherwise the number of bytes copied is returned (1). Buffer
137 * flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
138 * transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100139 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200140int buffer_put_char(struct buffer *buf, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100141{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200142 if (unlikely(buffer_input_closed(buf)))
143 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100144
Willy Tarreau74b08c92010-09-08 17:04:31 +0200145 if (buf->flags & BF_FULL)
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200146 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100147
Willy Tarreau74b08c92010-09-08 17:04:31 +0200148 *buf->r = c;
149
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100150 buf->i++;
151 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200152 buf->flags |= BF_FULL;
153 buf->flags |= BF_READ_PARTIAL;
154
155 buf->r++;
156 if (buf->r - buf->data == buf->size)
157 buf->r -= buf->size;
158
159 if (buf->to_forward >= 1) {
160 if (buf->to_forward != BUF_INFINITE_FORWARD)
161 buf->to_forward--;
Willy Tarreau2e046c62012-03-01 16:08:30 +0100162 buf->o++;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100163 buf->i--;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200164 buf->flags &= ~BF_OUT_EMPTY;
165 }
166
167 buf->total++;
168 return 1;
169}
170
171/* Tries to copy block <blk> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100172 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200173 * closed, -2 is returned. If the block is too large for this buffer, -3 is
174 * returned. If there is not enough room left in the buffer, -1 is returned.
175 * Otherwise the number of bytes copied is returned (0 being a valid number).
176 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
177 * transferred.
178 */
179int buffer_put_block(struct buffer *buf, const char *blk, int len)
180{
181 int max;
182
183 if (unlikely(buffer_input_closed(buf)))
184 return -2;
185
Willy Tarreau591fedc2010-08-10 15:28:21 +0200186 max = buffer_max_len(buf);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100187 if (unlikely(len > max - buffer_len(buf))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200188 /* we can't write this chunk right now because the buffer is
189 * almost full or because the block is too large. Return the
190 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200191 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200192 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200193 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200194
Willy Tarreau74b08c92010-09-08 17:04:31 +0200195 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200196 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100197
Willy Tarreau74b08c92010-09-08 17:04:31 +0200198 if (unlikely(len == 0))
199 return 0;
200
Willy Tarreau591fedc2010-08-10 15:28:21 +0200201 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100202 max = buffer_contig_space_with_res(buf, buf->size - max);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200203 memcpy(buf->r, blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200204 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200205 memcpy(buf->data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100206
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100207 buf->i += len;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200208 buf->r += len;
209 buf->total += len;
Willy Tarreau31971e52009-09-20 12:07:52 +0200210 if (buf->to_forward) {
211 unsigned long fwd = len;
212 if (buf->to_forward != BUF_INFINITE_FORWARD) {
213 if (fwd > buf->to_forward)
214 fwd = buf->to_forward;
215 buf->to_forward -= fwd;
216 }
Willy Tarreau2e046c62012-03-01 16:08:30 +0100217 buf->o += fwd;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100218 buf->i -= fwd;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200219 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200220 }
221
Willy Tarreau591fedc2010-08-10 15:28:21 +0200222 if (buf->r >= buf->data + buf->size)
223 buf->r -= buf->size;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200224
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200225 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100226 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200227 buf->flags |= BF_FULL;
228
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200229 /* notify that some data was read from the SI into the buffer */
230 buf->flags |= BF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200231 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100232}
233
Willy Tarreau74b08c92010-09-08 17:04:31 +0200234/* Gets one text line out of a buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200235 * Return values :
236 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200237 * =0 : no '\n' before end found. <str> is left undefined.
238 * <0 : no more bytes readable because output is shut.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200239 * The buffer status is not changed. The caller must call buffer_skip() to
240 * update it. The '\n' is waited for as long as neither the buffer nor the
241 * output are full. If either of them is full, the string may be returned
242 * as is, without the '\n'.
243 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200244int buffer_get_line(struct buffer *buf, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200245{
246 int ret, max;
247 char *p;
248
249 ret = 0;
250 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200251
252 /* closed or empty + imminent close = -1; empty = 0 */
253 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200254 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
255 ret = -1;
256 goto out;
257 }
258
259 p = buf->w;
260
Willy Tarreau2e046c62012-03-01 16:08:30 +0100261 if (max > buf->o) {
262 max = buf->o;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200263 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200264 }
265 while (max) {
266 *str++ = *p;
267 ret++;
268 max--;
269
270 if (*p == '\n')
271 break;
272 p++;
273 if (p == buf->data + buf->size)
274 p = buf->data;
275 }
Willy Tarreau2e046c62012-03-01 16:08:30 +0100276 if (ret > 0 && ret < len && ret < buf->o &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200277 *(str-1) != '\n' &&
278 !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200279 ret = 0;
280 out:
281 if (max)
282 *str = 0;
283 return ret;
284}
285
Willy Tarreau74b08c92010-09-08 17:04:31 +0200286/* Gets one full block of data at once from a buffer, optionally from a
287 * specific offset. Return values :
288 * >0 : number of bytes read, equal to requested size.
289 * =0 : not enough data available. <blk> is left undefined.
290 * <0 : no more bytes readable because output is shut.
291 * The buffer status is not changed. The caller must call buffer_skip() to
292 * update it.
293 */
294int buffer_get_block(struct buffer *buf, char *blk, int len, int offset)
295{
296 int firstblock;
297
298 if (buf->flags & BF_SHUTW)
299 return -1;
300
Willy Tarreau2e046c62012-03-01 16:08:30 +0100301 if (len + offset > buf->o) {
Willy Tarreau74b08c92010-09-08 17:04:31 +0200302 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
303 return -1;
304 return 0;
305 }
306
307 firstblock = buf->data + buf->size - buf->w;
308 if (firstblock > offset) {
309 if (firstblock >= len + offset) {
310 memcpy(blk, buf->w + offset, len);
311 return len;
312 }
313
314 memcpy(blk, buf->w + offset, firstblock - offset);
315 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
316 return len;
317 }
318
319 memcpy(blk, buf->data + offset - firstblock, len);
320 return len;
321}
322
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100323/* This function writes the string <str> at position <pos> which must be in
324 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
325 * (l, r, lr) are updated to be valid after the shift. the shift value
326 * (positive or negative) is returned. If there's no space left, the move is
Willy Tarreau2e046c62012-03-01 16:08:30 +0100327 * not done. The function does not adjust ->o nor BF_OUT_EMPTY because
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100328 * it does not make sense to use it on data scheduled to be sent. The string
329 * length is taken from parameter <len>. If <len> is null, the <str> pointer
330 * is allowed to be null.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200331 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100332int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333{
334 int delta;
335
336 delta = len - (end - pos);
337
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200338 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 return 0; /* no space left */
340
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100341 if (delta + b->r > b->w && b->w >= b->r && buffer_not_empty(b))
Willy Tarreaubbfa7932010-01-25 01:49:57 +0100342 return 0; /* no space left before wrapping data */
343
Willy Tarreaubaaee002006-06-26 02:48:02 +0200344 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100345 memmove(end + delta, end, b->r - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346
347 /* now, copy str over pos */
348 if (len)
349 memcpy(pos, str, len);
350
351 /* we only move data after the displaced zone */
352 if (b->r > pos) b->r += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353 if (b->lr > pos) b->lr += delta;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100354 b->i += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200356 b->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100357 if (buffer_len(b) == 0)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200358 b->r = b->w = b->lr = b->data;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100359 if (buffer_len(b) >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200360 b->flags |= BF_FULL;
361
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362 return delta;
363}
364
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100366 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
367 * argument informs about the length of string <str> so that we don't have to
368 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
369 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreauf890dc92008-12-13 21:12:26 +0100370 * some circumstances. The send limit is *not* adjusted.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100371 *
372 * The number of bytes added is returned on success. 0 is returned on failure.
373 */
374int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
375{
376 int delta;
377
378 delta = len + 2;
379
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200380 if (delta + b->r >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100381 return 0; /* no space left */
382
383 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100384 memmove(pos + delta, pos, b->r - pos);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100385
386 /* now, copy str over pos */
387 if (len && str) {
388 memcpy(pos, str, len);
389 pos[len] = '\r';
390 pos[len + 1] = '\n';
391 }
392
393 /* we only move data after the displaced zone */
394 if (b->r > pos) b->r += delta;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100395 if (b->lr > pos) b->lr += delta;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100396 b->i += delta;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100397
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200398 b->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100399 if (buffer_len(b) >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200400 b->flags |= BF_FULL;
401
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402 return delta;
403}
404
405
Willy Tarreaub97f1992010-02-25 23:54:31 +0100406/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
407 * destination. It does not use any intermediate buffer and does the move in
408 * place, though it will be slower than a simple memmove() on contiguous data,
409 * so it's desirable to use it only on non-contiguous buffers. No pointers are
410 * changed, the caller is responsible for that.
411 */
412void buffer_bounce_realign(struct buffer *buf)
413{
414 int advance, to_move;
415 char *from, *to;
416
417 advance = buf->data + buf->size - buf->w;
418 if (!advance)
419 return;
420
421 from = buf->w;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100422 to_move = buffer_len(buf);
Willy Tarreaub97f1992010-02-25 23:54:31 +0100423 while (to_move) {
424 char last, save;
425
426 last = *from;
427 to = from + advance;
428 if (to >= buf->data + buf->size)
429 to -= buf->size;
430
431 while (1) {
432 save = *to;
433 *to = last;
434 last = save;
435 to_move--;
436 if (!to_move)
437 break;
438
439 /* check if we went back home after rotating a number of bytes */
440 if (to == from)
441 break;
442
443 /* if we ended up in the empty area, let's walk to next place. The
444 * empty area is either between buf->r and from or before from or
445 * after buf->r.
446 */
447 if (from > buf->r) {
448 if (to >= buf->r && to < from)
449 break;
450 } else if (from < buf->r) {
451 if (to < from || to >= buf->r)
452 break;
453 }
454
455 /* we have overwritten a byte of the original set, let's move it */
456 to += advance;
457 if (to >= buf->data + buf->size)
458 to -= buf->size;
459 }
460
461 from++;
462 if (from >= buf->data + buf->size)
463 from -= buf->size;
464 }
465}
466
467
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100468/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100469 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200470 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100471 * the new chunk size.
472 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200473int chunk_printf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100474{
475 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200476 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100477
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200478 if (!chk->str || !chk->size)
479 return 0;
480
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100481 va_start(argp, fmt);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200482 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
483 if (ret >= chk->size - chk->len)
Willy Tarreaudceaa082007-07-25 14:38:45 +0200484 /* do not copy anything in case of truncation */
485 chk->str[chk->len] = 0;
486 else
487 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100488 va_end(argp);
489 return chk->len;
490}
491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100492/*
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200493 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
494 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
495 * If the chk->len is over, nothing is added. Returns the new chunk size.
496 */
497int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
498
499 int i, l;
500 int olen, free;
501 char c;
502
503 olen = dst->len;
504
505 for (i = 0; i < src->len; i++) {
506 free = dst->size - dst->len;
507
508 if (!free) {
509 dst->len = olen;
510 return dst->len;
511 }
512
513 c = src->str[i];
514
Willy Tarreau88e05812010-03-03 00:16:00 +0100515 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200516 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
517
518 if (free < l) {
519 dst->len = olen;
520 return dst->len;
521 }
522
523 dst->len += l;
524 } else {
525 dst->str[dst->len] = c;
526 dst->len++;
527 }
528 }
529
530 return dst->len;
531}
532
533/*
534 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
535 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
536 * If the chk->len is over, nothing is added. Returns the new chunk size.
537 */
538int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
539 int i, l;
540 int olen, free;
541 char c;
542
543 olen = dst->len;
544
545 for (i = 0; i < src->len; i++) {
546 free = dst->size - dst->len;
547
548 if (!free) {
549 dst->len = olen;
550 return dst->len;
551 }
552
553 c = src->str[i];
554
Willy Tarreau88e05812010-03-03 00:16:00 +0100555 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200556 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
557
558 if (free < l) {
559 dst->len = olen;
560 return dst->len;
561 }
562
563 dst->len += l;
564 } else {
565 dst->str[dst->len] = c;
566 dst->len++;
567 }
568 }
569
570 return dst->len;
571}
572
573/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100574 * Dumps part or all of a buffer.
575 */
576void buffer_dump(FILE *o, struct buffer *b, int from, int to)
577{
578 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100579 fprintf(o, " data=%p o=%d i=%d r=%p w=%p lr=%p\n",
580 b->data, b->o, b->i, b->r, b->w, b->lr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100581
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100582 if (!to || to > buffer_len(b))
583 to = buffer_len(b);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100584
585 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
586 for (; from < to; from++) {
587 if ((from & 15) == 0)
588 fprintf(o, " %04x: ", from);
589 fprintf(o, "%02x ", b->data[from]);
590 if ((from & 15) == 7)
591 fprintf(o, "- ");
592 else if (((from & 15) == 15) && (from != to-1))
593 fprintf(o, "\n");
594 }
595 fprintf(o, "\n--\n");
596}
597
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100598
599/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200600 * Local variables:
601 * c-indent-level: 8
602 * c-basic-offset: 8
603 * End:
604 */