blob: 91a0cbbc7a437a033eb56961809b7475a3f9b789 [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 Tarreau363a5bb2012-03-02 20:14:45 +010099 * Note: this function appends data to the buffer's output and possibly overwrites
100 * any pending input data which are assumed not to exist.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 */
102int buffer_write(struct buffer *buf, const char *msg, int len)
103{
104 int max;
105
Willy Tarreauaeac3192009-08-31 08:09:57 +0200106 if (len == 0)
107 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108
Willy Tarreau078e2942009-08-18 07:19:39 +0200109 if (len > buf->size) {
110 /* we can't write this chunk and will never be able to, because
111 * it is larger than the buffer. This must be reported as an
112 * error. Then we return -2 so that writers that don't care can
113 * ignore it and go on, and others can check for this value.
114 */
115 return -2;
116 }
117
Willy Tarreauaeac3192009-08-31 08:09:57 +0200118 max = buffer_realign(buf);
119
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120 if (len > max)
121 return max;
122
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100123 memcpy(buf->p, msg, len);
Willy Tarreau2e046c62012-03-01 16:08:30 +0100124 buf->o += len;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100125 buf->p = buffer_wrap_add(buf, buf->p + len);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100126 buf->total += len;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200127
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200128 buf->flags &= ~(BF_OUT_EMPTY|BF_FULL);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100129 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200130 buf->flags |= BF_FULL;
131
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200132 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133}
134
Willy Tarreau74b08c92010-09-08 17:04:31 +0200135/* Tries to copy character <c> into buffer <buf> after length controls. The
Willy Tarreau2e046c62012-03-01 16:08:30 +0100136 * ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200137 * closed, -2 is returned. If there is not enough room left in the buffer, -1
138 * is returned. Otherwise the number of bytes copied is returned (1). Buffer
139 * flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
140 * transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100141 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200142int buffer_put_char(struct buffer *buf, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100143{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200144 if (unlikely(buffer_input_closed(buf)))
145 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100146
Willy Tarreau74b08c92010-09-08 17:04:31 +0200147 if (buf->flags & BF_FULL)
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200148 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100149
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100150 *buffer_wrap_add(buf, buf->p + buf->i) = c;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200151
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100152 buf->i++;
153 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200154 buf->flags |= BF_FULL;
155 buf->flags |= BF_READ_PARTIAL;
156
Willy Tarreau74b08c92010-09-08 17:04:31 +0200157 if (buf->to_forward >= 1) {
158 if (buf->to_forward != BUF_INFINITE_FORWARD)
159 buf->to_forward--;
Willy Tarreau2e046c62012-03-01 16:08:30 +0100160 buf->o++;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100161 buf->i--;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200162 buf->flags &= ~BF_OUT_EMPTY;
163 }
164
165 buf->total++;
166 return 1;
167}
168
169/* Tries to copy block <blk> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100170 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200171 * closed, -2 is returned. If the block is too large for this buffer, -3 is
172 * returned. If there is not enough room left in the buffer, -1 is returned.
173 * Otherwise the number of bytes copied is returned (0 being a valid number).
174 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
175 * transferred.
176 */
177int buffer_put_block(struct buffer *buf, const char *blk, int len)
178{
179 int max;
180
181 if (unlikely(buffer_input_closed(buf)))
182 return -2;
183
Willy Tarreau591fedc2010-08-10 15:28:21 +0200184 max = buffer_max_len(buf);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100185 if (unlikely(len > max - buffer_len(buf))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200186 /* we can't write this chunk right now because the buffer is
187 * almost full or because the block is too large. Return the
188 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200189 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200190 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200191 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200192
Willy Tarreau74b08c92010-09-08 17:04:31 +0200193 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200194 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100195
Willy Tarreau74b08c92010-09-08 17:04:31 +0200196 if (unlikely(len == 0))
197 return 0;
198
Willy Tarreau591fedc2010-08-10 15:28:21 +0200199 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100200 max = buffer_contig_space_with_res(buf, buf->size - max);
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100201 memcpy(buffer_wrap_add(buf, buf->p + buf->i), blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200202 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200203 memcpy(buf->data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100204
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100205 buf->i += len;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200206 buf->total += len;
Willy Tarreau31971e52009-09-20 12:07:52 +0200207 if (buf->to_forward) {
208 unsigned long fwd = len;
209 if (buf->to_forward != BUF_INFINITE_FORWARD) {
210 if (fwd > buf->to_forward)
211 fwd = buf->to_forward;
212 buf->to_forward -= fwd;
213 }
Willy Tarreau2e046c62012-03-01 16:08:30 +0100214 buf->o += fwd;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100215 buf->i -= fwd;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100216 buf->p = buffer_wrap_add(buf, buf->p + fwd);
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200217 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200218 }
219
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200220 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100221 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200222 buf->flags |= BF_FULL;
223
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200224 /* notify that some data was read from the SI into the buffer */
225 buf->flags |= BF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200226 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100227}
228
Willy Tarreau74b08c92010-09-08 17:04:31 +0200229/* Gets one text line out of a buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200230 * Return values :
231 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200232 * =0 : no '\n' before end found. <str> is left undefined.
233 * <0 : no more bytes readable because output is shut.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200234 * The buffer status is not changed. The caller must call buffer_skip() to
235 * update it. The '\n' is waited for as long as neither the buffer nor the
236 * output are full. If either of them is full, the string may be returned
237 * as is, without the '\n'.
238 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200239int buffer_get_line(struct buffer *buf, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200240{
241 int ret, max;
242 char *p;
243
244 ret = 0;
245 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200246
247 /* closed or empty + imminent close = -1; empty = 0 */
248 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200249 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
250 ret = -1;
251 goto out;
252 }
253
Willy Tarreau89fa7062012-03-02 16:13:16 +0100254 p = buffer_wrap_sub(buf, buf->p - buf->o);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200255
Willy Tarreau2e046c62012-03-01 16:08:30 +0100256 if (max > buf->o) {
257 max = buf->o;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200258 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200259 }
260 while (max) {
261 *str++ = *p;
262 ret++;
263 max--;
264
265 if (*p == '\n')
266 break;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100267 p = buffer_wrap_add(buf, p + 1);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200268 }
Willy Tarreau2e046c62012-03-01 16:08:30 +0100269 if (ret > 0 && ret < len && ret < buf->o &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200270 *(str-1) != '\n' &&
271 !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200272 ret = 0;
273 out:
274 if (max)
275 *str = 0;
276 return ret;
277}
278
Willy Tarreau74b08c92010-09-08 17:04:31 +0200279/* Gets one full block of data at once from a buffer, optionally from a
280 * specific offset. Return values :
281 * >0 : number of bytes read, equal to requested size.
282 * =0 : not enough data available. <blk> is left undefined.
283 * <0 : no more bytes readable because output is shut.
284 * The buffer status is not changed. The caller must call buffer_skip() to
285 * update it.
286 */
287int buffer_get_block(struct buffer *buf, char *blk, int len, int offset)
288{
289 int firstblock;
290
291 if (buf->flags & BF_SHUTW)
292 return -1;
293
Willy Tarreau2e046c62012-03-01 16:08:30 +0100294 if (len + offset > buf->o) {
Willy Tarreau74b08c92010-09-08 17:04:31 +0200295 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
296 return -1;
297 return 0;
298 }
299
Willy Tarreau89fa7062012-03-02 16:13:16 +0100300 firstblock = buf->data + buf->size - buffer_wrap_sub(buf, buf->p - buf->o);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200301 if (firstblock > offset) {
302 if (firstblock >= len + offset) {
Willy Tarreau89fa7062012-03-02 16:13:16 +0100303 memcpy(blk, buffer_wrap_sub(buf, buf->p - buf->o) + offset, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200304 return len;
305 }
306
Willy Tarreau89fa7062012-03-02 16:13:16 +0100307 memcpy(blk, buffer_wrap_sub(buf, buf->p - buf->o) + offset, firstblock - offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200308 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
309 return len;
310 }
311
312 memcpy(blk, buf->data + offset - firstblock, len);
313 return len;
314}
315
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100316/* This function writes the string <str> at position <pos> which must be in
317 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
318 * (l, r, lr) are updated to be valid after the shift. the shift value
319 * (positive or negative) is returned. If there's no space left, the move is
Willy Tarreau2e046c62012-03-01 16:08:30 +0100320 * not done. The function does not adjust ->o nor BF_OUT_EMPTY because
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100321 * it does not make sense to use it on data scheduled to be sent. The string
322 * length is taken from parameter <len>. If <len> is null, the <str> pointer
323 * is allowed to be null.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100325int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326{
327 int delta;
328
329 delta = len - (end - pos);
330
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100331 if (delta + buffer_wrap_add(b, b->p + b->i) >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332 return 0; /* no space left */
333
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100334 if (buffer_not_empty(b) &&
335 delta + buffer_wrap_add(b, b->p + b->i) > buffer_wrap_sub(b, b->p - b->o) &&
336 buffer_wrap_sub(b, b->p - b->o) >= buffer_wrap_add(b, b->p + b->i))
Willy Tarreaubbfa7932010-01-25 01:49:57 +0100337 return 0; /* no space left before wrapping data */
338
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 /* first, protect the end of the buffer */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100340 memmove(end + delta, end, buffer_wrap_add(b, b->p + b->i) - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341
342 /* now, copy str over pos */
343 if (len)
344 memcpy(pos, str, len);
345
346 /* we only move data after the displaced zone */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200347 if (b->lr > pos) b->lr += delta;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100348 b->i += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200350 b->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100351 if (buffer_len(b) == 0)
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100352 b->p = b->lr = b->data;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100353 if (buffer_len(b) >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200354 b->flags |= BF_FULL;
355
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356 return delta;
357}
358
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100360 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
361 * argument informs about the length of string <str> so that we don't have to
362 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
363 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreauf890dc92008-12-13 21:12:26 +0100364 * some circumstances. The send limit is *not* adjusted.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100365 *
366 * The number of bytes added is returned on success. 0 is returned on failure.
367 */
368int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
369{
370 int delta;
371
372 delta = len + 2;
373
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100374 if (delta + buffer_wrap_add(b, b->p + b->i) >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100375 return 0; /* no space left */
376
377 /* first, protect the end of the buffer */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100378 memmove(pos + delta, pos, buffer_wrap_add(b, b->p + b->i) - pos);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100379
380 /* now, copy str over pos */
381 if (len && str) {
382 memcpy(pos, str, len);
383 pos[len] = '\r';
384 pos[len + 1] = '\n';
385 }
386
387 /* we only move data after the displaced zone */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100388 if (b->lr > pos) b->lr += delta;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100389 b->i += delta;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100390
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200391 b->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100392 if (buffer_len(b) >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200393 b->flags |= BF_FULL;
394
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100395 return delta;
396}
397
398
Willy Tarreaub97f1992010-02-25 23:54:31 +0100399/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
400 * destination. It does not use any intermediate buffer and does the move in
401 * place, though it will be slower than a simple memmove() on contiguous data,
402 * so it's desirable to use it only on non-contiguous buffers. No pointers are
403 * changed, the caller is responsible for that.
404 */
405void buffer_bounce_realign(struct buffer *buf)
406{
407 int advance, to_move;
408 char *from, *to;
409
Willy Tarreau89fa7062012-03-02 16:13:16 +0100410 from = buffer_wrap_sub(buf, buf->p - buf->o);
411 advance = buf->data + buf->size - from;
Willy Tarreaub97f1992010-02-25 23:54:31 +0100412 if (!advance)
413 return;
414
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100415 to_move = buffer_len(buf);
Willy Tarreaub97f1992010-02-25 23:54:31 +0100416 while (to_move) {
417 char last, save;
418
419 last = *from;
420 to = from + advance;
421 if (to >= buf->data + buf->size)
422 to -= buf->size;
423
424 while (1) {
425 save = *to;
426 *to = last;
427 last = save;
428 to_move--;
429 if (!to_move)
430 break;
431
432 /* check if we went back home after rotating a number of bytes */
433 if (to == from)
434 break;
435
436 /* if we ended up in the empty area, let's walk to next place. The
437 * empty area is either between buf->r and from or before from or
438 * after buf->r.
439 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100440 if (from > buffer_wrap_add(buf, buf->p + buf->i)) {
441 if (to >= buffer_wrap_add(buf, buf->p + buf->i) && to < from)
Willy Tarreaub97f1992010-02-25 23:54:31 +0100442 break;
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100443 } else if (from < buffer_wrap_add(buf, buf->p + buf->i)) {
444 if (to < from || to >= buffer_wrap_add(buf, buf->p + buf->i))
Willy Tarreaub97f1992010-02-25 23:54:31 +0100445 break;
446 }
447
448 /* we have overwritten a byte of the original set, let's move it */
449 to += advance;
450 if (to >= buf->data + buf->size)
451 to -= buf->size;
452 }
453
454 from++;
455 if (from >= buf->data + buf->size)
456 from -= buf->size;
457 }
458}
459
460
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100461/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100462 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200463 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100464 * the new chunk size.
465 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200466int chunk_printf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100467{
468 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200469 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100470
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200471 if (!chk->str || !chk->size)
472 return 0;
473
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100474 va_start(argp, fmt);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200475 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
476 if (ret >= chk->size - chk->len)
Willy Tarreaudceaa082007-07-25 14:38:45 +0200477 /* do not copy anything in case of truncation */
478 chk->str[chk->len] = 0;
479 else
480 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100481 va_end(argp);
482 return chk->len;
483}
484
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100485/*
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200486 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
487 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
488 * If the chk->len is over, nothing is added. Returns the new chunk size.
489 */
490int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
491
492 int i, l;
493 int olen, free;
494 char c;
495
496 olen = dst->len;
497
498 for (i = 0; i < src->len; i++) {
499 free = dst->size - dst->len;
500
501 if (!free) {
502 dst->len = olen;
503 return dst->len;
504 }
505
506 c = src->str[i];
507
Willy Tarreau88e05812010-03-03 00:16:00 +0100508 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200509 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
510
511 if (free < l) {
512 dst->len = olen;
513 return dst->len;
514 }
515
516 dst->len += l;
517 } else {
518 dst->str[dst->len] = c;
519 dst->len++;
520 }
521 }
522
523 return dst->len;
524}
525
526/*
527 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
528 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
529 * If the chk->len is over, nothing is added. Returns the new chunk size.
530 */
531int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
532 int i, l;
533 int olen, free;
534 char c;
535
536 olen = dst->len;
537
538 for (i = 0; i < src->len; i++) {
539 free = dst->size - dst->len;
540
541 if (!free) {
542 dst->len = olen;
543 return dst->len;
544 }
545
546 c = src->str[i];
547
Willy Tarreau88e05812010-03-03 00:16:00 +0100548 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200549 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
550
551 if (free < l) {
552 dst->len = olen;
553 return dst->len;
554 }
555
556 dst->len += l;
557 } else {
558 dst->str[dst->len] = c;
559 dst->len++;
560 }
561 }
562
563 return dst->len;
564}
565
566/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100567 * Dumps part or all of a buffer.
568 */
569void buffer_dump(FILE *o, struct buffer *b, int from, int to)
570{
571 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100572 fprintf(o, " data=%p o=%d i=%d p=%p lr=%p\n",
573 b->data, b->o, b->i, b->p, b->lr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100574
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100575 if (!to || to > buffer_len(b))
576 to = buffer_len(b);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100577
578 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
579 for (; from < to; from++) {
580 if ((from & 15) == 0)
581 fprintf(o, " %04x: ", from);
582 fprintf(o, "%02x ", b->data[from]);
583 if ((from & 15) == 7)
584 fprintf(o, "- ");
585 else if (((from & 15) == 15) && (from != to-1))
586 fprintf(o, "\n");
587 }
588 fprintf(o, "\n--\n");
589}
590
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100591
592/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593 * Local variables:
594 * c-indent-level: 8
595 * c-basic-offset: 8
596 * End:
597 */