blob: 44b3f7d3bd26cb97bbd3fa35ad02ff5046f6d423 [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.
38 * Directly touching ->to_forward will cause lockups when send_max goes down to
39 * zero if nobody is ready to push the remaining data.
40 */
41unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes)
42{
43 unsigned int data_left;
44 unsigned int new_forward;
45
46 if (!bytes)
47 return 0;
48 data_left = buf->l - buf->send_max;
49 if (bytes <= (unsigned long long)data_left) {
50 buf->send_max += bytes;
51 buf->flags &= ~BF_OUT_EMPTY;
52 return bytes;
53 }
54
55 buf->send_max += data_left;
56 if (buf->send_max)
57 buf->flags &= ~BF_OUT_EMPTY;
58
59 if (buf->l < buffer_max_len(buf))
60 buf->flags &= ~BF_FULL;
61 else
62 buf->flags |= BF_FULL;
63
64 if (likely(bytes == BUF_INFINITE_FORWARD)) {
65 buf->to_forward = bytes;
66 return bytes;
67 }
68
69 /* 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
75 new_forward = buf->to_forward + bytes - data_left;
76 bytes = data_left; /* at least those bytes were scheduled */
77
78 if (new_forward <= buf->to_forward) {
79 /* integer overflow detected, let's assume no more than 2G at once */
80 new_forward = MID_RANGE(new_forward);
81 }
82
83 if (new_forward > buf->to_forward) {
84 bytes += new_forward - buf->to_forward;
85 buf->to_forward = new_forward;
86 }
87 return bytes;
88}
Willy Tarreaubaaee002006-06-26 02:48:02 +020089
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020090/* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of
Willy Tarreau078e2942009-08-18 07:19:39 +020091 * success, -2 if the message is larger than the buffer size, or the number of
92 * bytes available otherwise. The send limit is automatically adjusted with the
93 * amount of data written. FIXME-20060521: handle unaligned data.
Willy Tarreaubaaee002006-06-26 02:48:02 +020094 */
95int buffer_write(struct buffer *buf, const char *msg, int len)
96{
97 int max;
98
Willy Tarreauaeac3192009-08-31 08:09:57 +020099 if (len == 0)
100 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101
Willy Tarreau078e2942009-08-18 07:19:39 +0200102 if (len > buf->size) {
103 /* we can't write this chunk and will never be able to, because
104 * it is larger than the buffer. This must be reported as an
105 * error. Then we return -2 so that writers that don't care can
106 * ignore it and go on, and others can check for this value.
107 */
108 return -2;
109 }
110
Willy Tarreauaeac3192009-08-31 08:09:57 +0200111 max = buffer_realign(buf);
112
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 if (len > max)
114 return max;
115
116 memcpy(buf->r, msg, len);
117 buf->l += len;
Willy Tarreauf890dc92008-12-13 21:12:26 +0100118 buf->send_max += len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 buf->r += len;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100120 buf->total += len;
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200121 if (buf->r == buf->data + buf->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 buf->r = buf->data;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200123
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200124 buf->flags &= ~(BF_OUT_EMPTY|BF_FULL);
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100125 if (buf->l >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200126 buf->flags |= BF_FULL;
127
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200128 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129}
130
Willy Tarreau74b08c92010-09-08 17:04:31 +0200131/* Tries to copy character <c> into buffer <buf> after length controls. The
132 * send_max and to_forward pointers are updated. If the buffer's input is
133 * closed, -2 is returned. If there is not enough room left in the buffer, -1
134 * is returned. Otherwise the number of bytes copied is returned (1). Buffer
135 * flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
136 * transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100137 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200138int buffer_put_char(struct buffer *buf, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100139{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200140 if (unlikely(buffer_input_closed(buf)))
141 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100142
Willy Tarreau74b08c92010-09-08 17:04:31 +0200143 if (buf->flags & BF_FULL)
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200144 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100145
Willy Tarreau74b08c92010-09-08 17:04:31 +0200146 *buf->r = c;
147
148 buf->l++;
149 if (buf->l >= buffer_max_len(buf))
150 buf->flags |= BF_FULL;
151 buf->flags |= BF_READ_PARTIAL;
152
153 buf->r++;
154 if (buf->r - buf->data == buf->size)
155 buf->r -= buf->size;
156
157 if (buf->to_forward >= 1) {
158 if (buf->to_forward != BUF_INFINITE_FORWARD)
159 buf->to_forward--;
160 buf->send_max++;
161 buf->flags &= ~BF_OUT_EMPTY;
162 }
163
164 buf->total++;
165 return 1;
166}
167
168/* Tries to copy block <blk> at once into buffer <buf> after length controls.
169 * The send_max and to_forward pointers are updated. If the buffer's input is
170 * closed, -2 is returned. If the block is too large for this buffer, -3 is
171 * returned. If there is not enough room left in the buffer, -1 is returned.
172 * Otherwise the number of bytes copied is returned (0 being a valid number).
173 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
174 * transferred.
175 */
176int buffer_put_block(struct buffer *buf, const char *blk, int len)
177{
178 int max;
179
180 if (unlikely(buffer_input_closed(buf)))
181 return -2;
182
Willy Tarreau591fedc2010-08-10 15:28:21 +0200183 max = buffer_max_len(buf);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200184 if (unlikely(len > max - buf->l)) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200185 /* we can't write this chunk right now because the buffer is
186 * almost full or because the block is too large. Return the
187 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200188 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200189 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200190 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200191
Willy Tarreau74b08c92010-09-08 17:04:31 +0200192 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200193 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100194
Willy Tarreau74b08c92010-09-08 17:04:31 +0200195 if (unlikely(len == 0))
196 return 0;
197
Willy Tarreau591fedc2010-08-10 15:28:21 +0200198 /* OK so the data fits in the buffer in one or two blocks */
199 max = buffer_contig_space_with_len(buf, max);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200200 memcpy(buf->r, blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200201 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200202 memcpy(buf->data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100203
Willy Tarreauaeac3192009-08-31 08:09:57 +0200204 buf->l += len;
205 buf->r += len;
206 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 }
214 buf->send_max += fwd;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200215 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200216 }
217
Willy Tarreau591fedc2010-08-10 15:28:21 +0200218 if (buf->r >= buf->data + buf->size)
219 buf->r -= buf->size;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200220
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200221 buf->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100222 if (buf->l >= buffer_max_len(buf))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200223 buf->flags |= BF_FULL;
224
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200225 /* notify that some data was read from the SI into the buffer */
226 buf->flags |= BF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200227 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100228}
229
Willy Tarreau74b08c92010-09-08 17:04:31 +0200230/* Gets one text line out of a buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200231 * Return values :
232 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200233 * =0 : no '\n' before end found. <str> is left undefined.
234 * <0 : no more bytes readable because output is shut.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200235 * The buffer status is not changed. The caller must call buffer_skip() to
236 * update it. The '\n' is waited for as long as neither the buffer nor the
237 * output are full. If either of them is full, the string may be returned
238 * as is, without the '\n'.
239 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200240int buffer_get_line(struct buffer *buf, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200241{
242 int ret, max;
243 char *p;
244
245 ret = 0;
246 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200247
248 /* closed or empty + imminent close = -1; empty = 0 */
249 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200250 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
251 ret = -1;
252 goto out;
253 }
254
255 p = buf->w;
256
257 if (max > buf->send_max) {
258 max = buf->send_max;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200259 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200260 }
261 while (max) {
262 *str++ = *p;
263 ret++;
264 max--;
265
266 if (*p == '\n')
267 break;
268 p++;
269 if (p == buf->data + buf->size)
270 p = buf->data;
271 }
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200272 if (ret > 0 && ret < len && ret < buf->send_max &&
273 *(str-1) != '\n' &&
274 !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200275 ret = 0;
276 out:
277 if (max)
278 *str = 0;
279 return ret;
280}
281
Willy Tarreau74b08c92010-09-08 17:04:31 +0200282/* Gets one full block of data at once from a buffer, optionally from a
283 * specific offset. Return values :
284 * >0 : number of bytes read, equal to requested size.
285 * =0 : not enough data available. <blk> is left undefined.
286 * <0 : no more bytes readable because output is shut.
287 * The buffer status is not changed. The caller must call buffer_skip() to
288 * update it.
289 */
290int buffer_get_block(struct buffer *buf, char *blk, int len, int offset)
291{
292 int firstblock;
293
294 if (buf->flags & BF_SHUTW)
295 return -1;
296
297 if (len + offset > buf->send_max) {
298 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
299 return -1;
300 return 0;
301 }
302
303 firstblock = buf->data + buf->size - buf->w;
304 if (firstblock > offset) {
305 if (firstblock >= len + offset) {
306 memcpy(blk, buf->w + offset, len);
307 return len;
308 }
309
310 memcpy(blk, buf->w + offset, firstblock - offset);
311 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
312 return len;
313 }
314
315 memcpy(blk, buf->data + offset - firstblock, len);
316 return len;
317}
318
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319/*
320 * this function writes the string <str> at position <pos> which must be in buffer <b>,
321 * and moves <end> just after the end of <str>.
Willy Tarreaubbfa7932010-01-25 01:49:57 +0100322 * <b>'s parameters (l, r, lr) are recomputed to be valid after the shift.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323 * the shift value (positive or negative) is returned.
324 * If there's no space left, the move is not done.
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200325 * The function does not adjust ->send_max nor BF_OUT_EMPTY because it does not
326 * make sense to use it on data scheduled to be sent.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 *
328 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100329int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330{
331 int delta;
332 int len;
333
334 len = strlen(str);
335 delta = len - (end - pos);
336
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200337 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200338 return 0; /* no space left */
339
Willy Tarreaubbfa7932010-01-25 01:49:57 +0100340 if (delta + b->r > b->w && b->w >= b->r && b->l)
341 return 0; /* no space left before wrapping data */
342
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100344 memmove(end + delta, end, b->r - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345
346 /* now, copy str over pos */
347 memcpy(pos, str,len);
348
349 /* we only move data after the displaced zone */
350 if (b->r > pos) b->r += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351 if (b->lr > pos) b->lr += delta;
352 b->l += delta;
353
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200354 b->flags &= ~BF_FULL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200355 if (b->l == 0)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200356 b->r = b->w = b->lr = b->data;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100357 if (b->l >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200358 b->flags |= BF_FULL;
359
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360 return delta;
361}
362
363/*
364 * same except that the string length is given, which allows str to be NULL if
Willy Tarreauf890dc92008-12-13 21:12:26 +0100365 * len is 0. The send limit is *not* adjusted.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100367int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368{
369 int delta;
370
371 delta = len - (end - pos);
372
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200373 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374 return 0; /* no space left */
375
Willy Tarreaubbfa7932010-01-25 01:49:57 +0100376 if (delta + b->r > b->w && b->w >= b->r && b->l)
377 return 0; /* no space left before wrapping data */
378
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100380 memmove(end + delta, end, b->r - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381
382 /* now, copy str over pos */
383 if (len)
384 memcpy(pos, str, len);
385
386 /* we only move data after the displaced zone */
387 if (b->r > pos) b->r += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388 if (b->lr > pos) b->lr += delta;
389 b->l += delta;
390
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200391 b->flags &= ~BF_FULL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200392 if (b->l == 0)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200393 b->r = b->w = b->lr = b->data;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100394 if (b->l >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200395 b->flags |= BF_FULL;
396
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397 return delta;
398}
399
400
401/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
403 * argument informs about the length of string <str> so that we don't have to
404 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
405 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreauf890dc92008-12-13 21:12:26 +0100406 * some circumstances. The send limit is *not* adjusted.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100407 *
408 * The number of bytes added is returned on success. 0 is returned on failure.
409 */
410int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
411{
412 int delta;
413
414 delta = len + 2;
415
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200416 if (delta + b->r >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100417 return 0; /* no space left */
418
419 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100420 memmove(pos + delta, pos, b->r - pos);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100421
422 /* now, copy str over pos */
423 if (len && str) {
424 memcpy(pos, str, len);
425 pos[len] = '\r';
426 pos[len + 1] = '\n';
427 }
428
429 /* we only move data after the displaced zone */
430 if (b->r > pos) b->r += delta;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100431 if (b->lr > pos) b->lr += delta;
432 b->l += delta;
433
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200434 b->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100435 if (b->l >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200436 b->flags |= BF_FULL;
437
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100438 return delta;
439}
440
441
Willy Tarreaub97f1992010-02-25 23:54:31 +0100442/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
443 * destination. It does not use any intermediate buffer and does the move in
444 * place, though it will be slower than a simple memmove() on contiguous data,
445 * so it's desirable to use it only on non-contiguous buffers. No pointers are
446 * changed, the caller is responsible for that.
447 */
448void buffer_bounce_realign(struct buffer *buf)
449{
450 int advance, to_move;
451 char *from, *to;
452
453 advance = buf->data + buf->size - buf->w;
454 if (!advance)
455 return;
456
457 from = buf->w;
458 to_move = buf->l;
459 while (to_move) {
460 char last, save;
461
462 last = *from;
463 to = from + advance;
464 if (to >= buf->data + buf->size)
465 to -= buf->size;
466
467 while (1) {
468 save = *to;
469 *to = last;
470 last = save;
471 to_move--;
472 if (!to_move)
473 break;
474
475 /* check if we went back home after rotating a number of bytes */
476 if (to == from)
477 break;
478
479 /* if we ended up in the empty area, let's walk to next place. The
480 * empty area is either between buf->r and from or before from or
481 * after buf->r.
482 */
483 if (from > buf->r) {
484 if (to >= buf->r && to < from)
485 break;
486 } else if (from < buf->r) {
487 if (to < from || to >= buf->r)
488 break;
489 }
490
491 /* we have overwritten a byte of the original set, let's move it */
492 to += advance;
493 if (to >= buf->data + buf->size)
494 to -= buf->size;
495 }
496
497 from++;
498 if (from >= buf->data + buf->size)
499 from -= buf->size;
500 }
501}
502
503
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100504/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100505 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200506 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100507 * the new chunk size.
508 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200509int chunk_printf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100510{
511 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200512 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100513
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200514 if (!chk->str || !chk->size)
515 return 0;
516
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100517 va_start(argp, fmt);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200518 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
519 if (ret >= chk->size - chk->len)
Willy Tarreaudceaa082007-07-25 14:38:45 +0200520 /* do not copy anything in case of truncation */
521 chk->str[chk->len] = 0;
522 else
523 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100524 va_end(argp);
525 return chk->len;
526}
527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100528/*
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200529 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
530 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
531 * If the chk->len is over, nothing is added. Returns the new chunk size.
532 */
533int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
534
535 int i, l;
536 int olen, free;
537 char c;
538
539 olen = dst->len;
540
541 for (i = 0; i < src->len; i++) {
542 free = dst->size - dst->len;
543
544 if (!free) {
545 dst->len = olen;
546 return dst->len;
547 }
548
549 c = src->str[i];
550
Willy Tarreau88e05812010-03-03 00:16:00 +0100551 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200552 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
553
554 if (free < l) {
555 dst->len = olen;
556 return dst->len;
557 }
558
559 dst->len += l;
560 } else {
561 dst->str[dst->len] = c;
562 dst->len++;
563 }
564 }
565
566 return dst->len;
567}
568
569/*
570 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
571 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
572 * If the chk->len is over, nothing is added. Returns the new chunk size.
573 */
574int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
575 int i, l;
576 int olen, free;
577 char c;
578
579 olen = dst->len;
580
581 for (i = 0; i < src->len; i++) {
582 free = dst->size - dst->len;
583
584 if (!free) {
585 dst->len = olen;
586 return dst->len;
587 }
588
589 c = src->str[i];
590
Willy Tarreau88e05812010-03-03 00:16:00 +0100591 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200592 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
593
594 if (free < l) {
595 dst->len = olen;
596 return dst->len;
597 }
598
599 dst->len += l;
600 } else {
601 dst->str[dst->len] = c;
602 dst->len++;
603 }
604 }
605
606 return dst->len;
607}
608
609/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100610 * Dumps part or all of a buffer.
611 */
612void buffer_dump(FILE *o, struct buffer *b, int from, int to)
613{
614 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100615 fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n",
616 b->data, b->l, b->r, b->w, b->lr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100617
618 if (!to || to > b->l)
619 to = b->l;
620
621 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
622 for (; from < to; from++) {
623 if ((from & 15) == 0)
624 fprintf(o, " %04x: ", from);
625 fprintf(o, "%02x ", b->data[from]);
626 if ((from & 15) == 7)
627 fprintf(o, "- ");
628 else if (((from & 15) == 15) && (from != to-1))
629 fprintf(o, "\n");
630 }
631 fprintf(o, "\n--\n");
632}
633
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100634
635/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 * Local variables:
637 * c-indent-level: 8
638 * c-basic-offset: 8
639 * End:
640 */