blob: 41d36248c917207e1e1edc24f0ccb89b9e08b682 [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 Tarreau89fa7062012-03-02 16:13:16 +010049 buf->p = buffer_wrap_add(buf, buf->p + bytes);
Willy Tarreau2e046c62012-03-01 16:08:30 +010050 buf->o += bytes;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010051 buf->i -= bytes;
Willy Tarreau0bc34932011-03-28 16:25:58 +020052 buf->flags &= ~BF_OUT_EMPTY;
53 return bytes;
54 }
55
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010056 forwarded = buf->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +010057 buf->p = buffer_wrap_add(buf, buf->p + forwarded);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010058 buf->o += forwarded;
59 buf->i = 0;
60
Willy Tarreau2e046c62012-03-01 16:08:30 +010061 if (buf->o)
Willy Tarreau0bc34932011-03-28 16:25:58 +020062 buf->flags &= ~BF_OUT_EMPTY;
63
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010064 if (buf->o < buffer_max_len(buf))
Willy Tarreau0bc34932011-03-28 16:25:58 +020065 buf->flags &= ~BF_FULL;
66 else
67 buf->flags |= BF_FULL;
68
Willy Tarreau0bc34932011-03-28 16:25:58 +020069 /* Note: the case below is the only case where we may return
70 * a byte count that does not fit into a 32-bit number.
71 */
72 if (likely(buf->to_forward == BUF_INFINITE_FORWARD))
73 return bytes;
74
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010075 if (likely(bytes == BUF_INFINITE_FORWARD)) {
76 buf->to_forward = bytes;
77 return bytes;
78 }
79
80 new_forward = buf->to_forward + bytes - forwarded;
81 bytes = forwarded; /* at least those bytes were scheduled */
Willy Tarreau0bc34932011-03-28 16:25:58 +020082
83 if (new_forward <= buf->to_forward) {
84 /* integer overflow detected, let's assume no more than 2G at once */
85 new_forward = MID_RANGE(new_forward);
86 }
87
88 if (new_forward > buf->to_forward) {
89 bytes += new_forward - buf->to_forward;
90 buf->to_forward = new_forward;
91 }
92 return bytes;
93}
Willy Tarreaubaaee002006-06-26 02:48:02 +020094
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020095/* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of
Willy Tarreau078e2942009-08-18 07:19:39 +020096 * success, -2 if the message is larger than the buffer size, or the number of
97 * bytes available otherwise. The send limit is automatically adjusted with the
98 * amount of data written. FIXME-20060521: handle unaligned data.
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 */
100int buffer_write(struct buffer *buf, const char *msg, int len)
101{
102 int max;
103
Willy Tarreauaeac3192009-08-31 08:09:57 +0200104 if (len == 0)
105 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106
Willy Tarreau078e2942009-08-18 07:19:39 +0200107 if (len > buf->size) {
108 /* we can't write this chunk and will never be able to, because
109 * it is larger than the buffer. This must be reported as an
110 * error. Then we return -2 so that writers that don't care can
111 * ignore it and go on, and others can check for this value.
112 */
113 return -2;
114 }
115
Willy Tarreauaeac3192009-08-31 08:09:57 +0200116 max = buffer_realign(buf);
117
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118 if (len > max)
119 return max;
120
121 memcpy(buf->r, msg, len);
Willy Tarreau2e046c62012-03-01 16:08:30 +0100122 buf->o += len;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100123 buf->p = buffer_wrap_add(buf, buf->p + len);
124 buf->r = buffer_wrap_add(buf, buf->r + len);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100125 buf->total += len;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200126
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200127 buf->flags &= ~(BF_OUT_EMPTY|BF_FULL);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100128 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200129 buf->flags |= BF_FULL;
130
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200131 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132}
133
Willy Tarreau74b08c92010-09-08 17:04:31 +0200134/* Tries to copy character <c> into buffer <buf> after length controls. The
Willy Tarreau2e046c62012-03-01 16:08:30 +0100135 * ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200136 * closed, -2 is returned. If there is not enough room left in the buffer, -1
137 * is returned. Otherwise the number of bytes copied is returned (1). Buffer
138 * flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
139 * transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100140 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200141int buffer_put_char(struct buffer *buf, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100142{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200143 if (unlikely(buffer_input_closed(buf)))
144 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100145
Willy Tarreau74b08c92010-09-08 17:04:31 +0200146 if (buf->flags & BF_FULL)
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200147 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100148
Willy Tarreau74b08c92010-09-08 17:04:31 +0200149 *buf->r = c;
150
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100151 buf->i++;
152 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200153 buf->flags |= BF_FULL;
154 buf->flags |= BF_READ_PARTIAL;
155
156 buf->r++;
157 if (buf->r - buf->data == buf->size)
158 buf->r -= buf->size;
159
160 if (buf->to_forward >= 1) {
161 if (buf->to_forward != BUF_INFINITE_FORWARD)
162 buf->to_forward--;
Willy Tarreau2e046c62012-03-01 16:08:30 +0100163 buf->o++;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100164 buf->i--;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200165 buf->flags &= ~BF_OUT_EMPTY;
166 }
167
168 buf->total++;
169 return 1;
170}
171
172/* Tries to copy block <blk> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100173 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200174 * closed, -2 is returned. If the block is too large for this buffer, -3 is
175 * returned. If there is not enough room left in the buffer, -1 is returned.
176 * Otherwise the number of bytes copied is returned (0 being a valid number).
177 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
178 * transferred.
179 */
180int buffer_put_block(struct buffer *buf, const char *blk, int len)
181{
182 int max;
183
184 if (unlikely(buffer_input_closed(buf)))
185 return -2;
186
Willy Tarreau591fedc2010-08-10 15:28:21 +0200187 max = buffer_max_len(buf);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100188 if (unlikely(len > max - buffer_len(buf))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200189 /* we can't write this chunk right now because the buffer is
190 * almost full or because the block is too large. Return the
191 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200192 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200193 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200194 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200195
Willy Tarreau74b08c92010-09-08 17:04:31 +0200196 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200197 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100198
Willy Tarreau74b08c92010-09-08 17:04:31 +0200199 if (unlikely(len == 0))
200 return 0;
201
Willy Tarreau591fedc2010-08-10 15:28:21 +0200202 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100203 max = buffer_contig_space_with_res(buf, buf->size - max);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200204 memcpy(buf->r, blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200205 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200206 memcpy(buf->data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100207
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100208 buf->i += len;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200209 buf->r += len;
210 buf->total += len;
Willy Tarreau31971e52009-09-20 12:07:52 +0200211 if (buf->to_forward) {
212 unsigned long fwd = len;
213 if (buf->to_forward != BUF_INFINITE_FORWARD) {
214 if (fwd > buf->to_forward)
215 fwd = buf->to_forward;
216 buf->to_forward -= fwd;
217 }
Willy Tarreau2e046c62012-03-01 16:08:30 +0100218 buf->o += fwd;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100219 buf->i -= fwd;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100220 buf->p = buffer_wrap_add(buf, buf->p + fwd);
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200221 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200222 }
223
Willy Tarreau591fedc2010-08-10 15:28:21 +0200224 if (buf->r >= buf->data + buf->size)
225 buf->r -= buf->size;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200226
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200227 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100228 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200229 buf->flags |= BF_FULL;
230
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200231 /* notify that some data was read from the SI into the buffer */
232 buf->flags |= BF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200233 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100234}
235
Willy Tarreau74b08c92010-09-08 17:04:31 +0200236/* Gets one text line out of a buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200237 * Return values :
238 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200239 * =0 : no '\n' before end found. <str> is left undefined.
240 * <0 : no more bytes readable because output is shut.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200241 * The buffer status is not changed. The caller must call buffer_skip() to
242 * update it. The '\n' is waited for as long as neither the buffer nor the
243 * output are full. If either of them is full, the string may be returned
244 * as is, without the '\n'.
245 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200246int buffer_get_line(struct buffer *buf, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200247{
248 int ret, max;
249 char *p;
250
251 ret = 0;
252 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200253
254 /* closed or empty + imminent close = -1; empty = 0 */
255 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200256 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
257 ret = -1;
258 goto out;
259 }
260
Willy Tarreau89fa7062012-03-02 16:13:16 +0100261 p = buffer_wrap_sub(buf, buf->p - buf->o);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200262
Willy Tarreau2e046c62012-03-01 16:08:30 +0100263 if (max > buf->o) {
264 max = buf->o;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200265 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200266 }
267 while (max) {
268 *str++ = *p;
269 ret++;
270 max--;
271
272 if (*p == '\n')
273 break;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100274 p = buffer_wrap_add(buf, p + 1);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200275 }
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
Willy Tarreau89fa7062012-03-02 16:13:16 +0100307 firstblock = buf->data + buf->size - buffer_wrap_sub(buf, buf->p - buf->o);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200308 if (firstblock > offset) {
309 if (firstblock >= len + offset) {
Willy Tarreau89fa7062012-03-02 16:13:16 +0100310 memcpy(blk, buffer_wrap_sub(buf, buf->p - buf->o) + offset, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200311 return len;
312 }
313
Willy Tarreau89fa7062012-03-02 16:13:16 +0100314 memcpy(blk, buffer_wrap_sub(buf, buf->p - buf->o) + offset, firstblock - offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200315 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 Tarreau89fa7062012-03-02 16:13:16 +0100341 if (delta + b->r > buffer_wrap_sub(b, b->p - b->o) &&
342 buffer_wrap_sub(b, b->p - b->o) >= b->r && buffer_not_empty(b))
Willy Tarreaubbfa7932010-01-25 01:49:57 +0100343 return 0; /* no space left before wrapping data */
344
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100346 memmove(end + delta, end, b->r - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200347
348 /* now, copy str over pos */
349 if (len)
350 memcpy(pos, str, len);
351
352 /* we only move data after the displaced zone */
353 if (b->r > pos) b->r += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354 if (b->lr > pos) b->lr += delta;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100355 b->i += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200357 b->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100358 if (buffer_len(b) == 0)
Willy Tarreau89fa7062012-03-02 16:13:16 +0100359 b->r = b->p = b->lr = b->data;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100360 if (buffer_len(b) >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200361 b->flags |= BF_FULL;
362
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363 return delta;
364}
365
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100367 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
368 * argument informs about the length of string <str> so that we don't have to
369 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
370 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreauf890dc92008-12-13 21:12:26 +0100371 * some circumstances. The send limit is *not* adjusted.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100372 *
373 * The number of bytes added is returned on success. 0 is returned on failure.
374 */
375int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
376{
377 int delta;
378
379 delta = len + 2;
380
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200381 if (delta + b->r >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100382 return 0; /* no space left */
383
384 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100385 memmove(pos + delta, pos, b->r - pos);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100386
387 /* now, copy str over pos */
388 if (len && str) {
389 memcpy(pos, str, len);
390 pos[len] = '\r';
391 pos[len + 1] = '\n';
392 }
393
394 /* we only move data after the displaced zone */
395 if (b->r > pos) b->r += delta;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100396 if (b->lr > pos) b->lr += delta;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100397 b->i += delta;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100398
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200399 b->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100400 if (buffer_len(b) >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200401 b->flags |= BF_FULL;
402
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100403 return delta;
404}
405
406
Willy Tarreaub97f1992010-02-25 23:54:31 +0100407/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
408 * destination. It does not use any intermediate buffer and does the move in
409 * place, though it will be slower than a simple memmove() on contiguous data,
410 * so it's desirable to use it only on non-contiguous buffers. No pointers are
411 * changed, the caller is responsible for that.
412 */
413void buffer_bounce_realign(struct buffer *buf)
414{
415 int advance, to_move;
416 char *from, *to;
417
Willy Tarreau89fa7062012-03-02 16:13:16 +0100418 from = buffer_wrap_sub(buf, buf->p - buf->o);
419 advance = buf->data + buf->size - from;
Willy Tarreaub97f1992010-02-25 23:54:31 +0100420 if (!advance)
421 return;
422
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100423 to_move = buffer_len(buf);
Willy Tarreaub97f1992010-02-25 23:54:31 +0100424 while (to_move) {
425 char last, save;
426
427 last = *from;
428 to = from + advance;
429 if (to >= buf->data + buf->size)
430 to -= buf->size;
431
432 while (1) {
433 save = *to;
434 *to = last;
435 last = save;
436 to_move--;
437 if (!to_move)
438 break;
439
440 /* check if we went back home after rotating a number of bytes */
441 if (to == from)
442 break;
443
444 /* if we ended up in the empty area, let's walk to next place. The
445 * empty area is either between buf->r and from or before from or
446 * after buf->r.
447 */
448 if (from > buf->r) {
449 if (to >= buf->r && to < from)
450 break;
451 } else if (from < buf->r) {
452 if (to < from || to >= buf->r)
453 break;
454 }
455
456 /* we have overwritten a byte of the original set, let's move it */
457 to += advance;
458 if (to >= buf->data + buf->size)
459 to -= buf->size;
460 }
461
462 from++;
463 if (from >= buf->data + buf->size)
464 from -= buf->size;
465 }
466}
467
468
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100469/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100470 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200471 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100472 * the new chunk size.
473 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200474int chunk_printf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100475{
476 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200477 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100478
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200479 if (!chk->str || !chk->size)
480 return 0;
481
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100482 va_start(argp, fmt);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200483 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
484 if (ret >= chk->size - chk->len)
Willy Tarreaudceaa082007-07-25 14:38:45 +0200485 /* do not copy anything in case of truncation */
486 chk->str[chk->len] = 0;
487 else
488 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100489 va_end(argp);
490 return chk->len;
491}
492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100493/*
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200494 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
495 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
496 * If the chk->len is over, nothing is added. Returns the new chunk size.
497 */
498int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
499
500 int i, l;
501 int olen, free;
502 char c;
503
504 olen = dst->len;
505
506 for (i = 0; i < src->len; i++) {
507 free = dst->size - dst->len;
508
509 if (!free) {
510 dst->len = olen;
511 return dst->len;
512 }
513
514 c = src->str[i];
515
Willy Tarreau88e05812010-03-03 00:16:00 +0100516 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200517 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
518
519 if (free < l) {
520 dst->len = olen;
521 return dst->len;
522 }
523
524 dst->len += l;
525 } else {
526 dst->str[dst->len] = c;
527 dst->len++;
528 }
529 }
530
531 return dst->len;
532}
533
534/*
535 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
536 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
537 * If the chk->len is over, nothing is added. Returns the new chunk size.
538 */
539int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
540 int i, l;
541 int olen, free;
542 char c;
543
544 olen = dst->len;
545
546 for (i = 0; i < src->len; i++) {
547 free = dst->size - dst->len;
548
549 if (!free) {
550 dst->len = olen;
551 return dst->len;
552 }
553
554 c = src->str[i];
555
Willy Tarreau88e05812010-03-03 00:16:00 +0100556 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200557 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
558
559 if (free < l) {
560 dst->len = olen;
561 return dst->len;
562 }
563
564 dst->len += l;
565 } else {
566 dst->str[dst->len] = c;
567 dst->len++;
568 }
569 }
570
571 return dst->len;
572}
573
574/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100575 * Dumps part or all of a buffer.
576 */
577void buffer_dump(FILE *o, struct buffer *b, int from, int to)
578{
579 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreau89fa7062012-03-02 16:13:16 +0100580 fprintf(o, " data=%p o=%d i=%d r=%p p=%p lr=%p\n",
581 b->data, b->o, b->i, b->r, b->p, b->lr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100582
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100583 if (!to || to > buffer_len(b))
584 to = buffer_len(b);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100585
586 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
587 for (; from < to; from++) {
588 if ((from & 15) == 0)
589 fprintf(o, " %04x: ", from);
590 fprintf(o, "%02x ", b->data[from]);
591 if ((from & 15) == 7)
592 fprintf(o, "- ");
593 else if (((from & 15) == 15) && (from != to-1))
594 fprintf(o, "\n");
595 }
596 fprintf(o, "\n--\n");
597}
598
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100599
600/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601 * Local variables:
602 * c-indent-level: 8
603 * c-basic-offset: 8
604 * End:
605 */