blob: 88d40ff184d3f0d0377a0786b918f99b0f565833 [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 */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100199 max = buffer_contig_space_with_res(buf, buf->size - 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 Tarreau19ae56b2011-11-28 10:36:13 +0100319/* This function writes the string <str> at position <pos> which must be in
320 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
321 * (l, r, lr) are updated to be valid after the shift. the shift value
322 * (positive or negative) is returned. If there's no space left, the move is
323 * not done. The function does not adjust ->send_max nor BF_OUT_EMPTY because
324 * it does not make sense to use it on data scheduled to be sent. The string
325 * length is taken from parameter <len>. If <len> is null, the <str> pointer
326 * is allowed to be null.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100328int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329{
330 int delta;
331
332 delta = len - (end - pos);
333
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200334 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335 return 0; /* no space left */
336
Willy Tarreaubbfa7932010-01-25 01:49:57 +0100337 if (delta + b->r > b->w && b->w >= b->r && b->l)
338 return 0; /* no space left before wrapping data */
339
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100341 memmove(end + delta, end, b->r - end);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200342
343 /* now, copy str over pos */
344 if (len)
345 memcpy(pos, str, len);
346
347 /* we only move data after the displaced zone */
348 if (b->r > pos) b->r += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349 if (b->lr > pos) b->lr += delta;
350 b->l += delta;
351
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200352 b->flags &= ~BF_FULL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200353 if (b->l == 0)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200354 b->r = b->w = b->lr = b->data;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100355 if (b->l >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200356 b->flags |= BF_FULL;
357
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358 return delta;
359}
360
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100362 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
363 * argument informs about the length of string <str> so that we don't have to
364 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
365 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreauf890dc92008-12-13 21:12:26 +0100366 * some circumstances. The send limit is *not* adjusted.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100367 *
368 * The number of bytes added is returned on success. 0 is returned on failure.
369 */
370int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
371{
372 int delta;
373
374 delta = len + 2;
375
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200376 if (delta + b->r >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100377 return 0; /* no space left */
378
379 /* first, protect the end of the buffer */
Willy Tarreau019fd5b2009-12-28 18:37:54 +0100380 memmove(pos + delta, pos, b->r - pos);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100381
382 /* now, copy str over pos */
383 if (len && str) {
384 memcpy(pos, str, len);
385 pos[len] = '\r';
386 pos[len + 1] = '\n';
387 }
388
389 /* we only move data after the displaced zone */
390 if (b->r > pos) b->r += delta;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100391 if (b->lr > pos) b->lr += delta;
392 b->l += delta;
393
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200394 b->flags &= ~BF_FULL;
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100395 if (b->l >= buffer_max_len(b))
Willy Tarreaue393fe22008-08-16 22:18:07 +0200396 b->flags |= BF_FULL;
397
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100398 return delta;
399}
400
401
Willy Tarreaub97f1992010-02-25 23:54:31 +0100402/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
403 * destination. It does not use any intermediate buffer and does the move in
404 * place, though it will be slower than a simple memmove() on contiguous data,
405 * so it's desirable to use it only on non-contiguous buffers. No pointers are
406 * changed, the caller is responsible for that.
407 */
408void buffer_bounce_realign(struct buffer *buf)
409{
410 int advance, to_move;
411 char *from, *to;
412
413 advance = buf->data + buf->size - buf->w;
414 if (!advance)
415 return;
416
417 from = buf->w;
418 to_move = buf->l;
419 while (to_move) {
420 char last, save;
421
422 last = *from;
423 to = from + advance;
424 if (to >= buf->data + buf->size)
425 to -= buf->size;
426
427 while (1) {
428 save = *to;
429 *to = last;
430 last = save;
431 to_move--;
432 if (!to_move)
433 break;
434
435 /* check if we went back home after rotating a number of bytes */
436 if (to == from)
437 break;
438
439 /* if we ended up in the empty area, let's walk to next place. The
440 * empty area is either between buf->r and from or before from or
441 * after buf->r.
442 */
443 if (from > buf->r) {
444 if (to >= buf->r && to < from)
445 break;
446 } else if (from < buf->r) {
447 if (to < from || to >= buf->r)
448 break;
449 }
450
451 /* we have overwritten a byte of the original set, let's move it */
452 to += advance;
453 if (to >= buf->data + buf->size)
454 to -= buf->size;
455 }
456
457 from++;
458 if (from >= buf->data + buf->size)
459 from -= buf->size;
460 }
461}
462
463
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100464/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100465 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200466 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100467 * the new chunk size.
468 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200469int chunk_printf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100470{
471 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200472 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100473
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200474 if (!chk->str || !chk->size)
475 return 0;
476
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100477 va_start(argp, fmt);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200478 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
479 if (ret >= chk->size - chk->len)
Willy Tarreaudceaa082007-07-25 14:38:45 +0200480 /* do not copy anything in case of truncation */
481 chk->str[chk->len] = 0;
482 else
483 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100484 va_end(argp);
485 return chk->len;
486}
487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100488/*
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200489 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
490 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
491 * If the chk->len is over, nothing is added. Returns the new chunk size.
492 */
493int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
494
495 int i, l;
496 int olen, free;
497 char c;
498
499 olen = dst->len;
500
501 for (i = 0; i < src->len; i++) {
502 free = dst->size - dst->len;
503
504 if (!free) {
505 dst->len = olen;
506 return dst->len;
507 }
508
509 c = src->str[i];
510
Willy Tarreau88e05812010-03-03 00:16:00 +0100511 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200512 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
513
514 if (free < l) {
515 dst->len = olen;
516 return dst->len;
517 }
518
519 dst->len += l;
520 } else {
521 dst->str[dst->len] = c;
522 dst->len++;
523 }
524 }
525
526 return dst->len;
527}
528
529/*
530 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
531 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
532 * If the chk->len is over, nothing is added. Returns the new chunk size.
533 */
534int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
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 == qc) {
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200552 l = snprintf(dst->str + dst->len, free, "<%02X>", (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/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100570 * Dumps part or all of a buffer.
571 */
572void buffer_dump(FILE *o, struct buffer *b, int from, int to)
573{
574 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100575 fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n",
576 b->data, b->l, b->r, b->w, b->lr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100577
578 if (!to || to > b->l)
579 to = b->l;
580
581 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
582 for (; from < to; from++) {
583 if ((from & 15) == 0)
584 fprintf(o, " %04x: ", from);
585 fprintf(o, "%02x ", b->data[from]);
586 if ((from & 15) == 7)
587 fprintf(o, "- ");
588 else if (((from & 15) == 15) && (from != to-1))
589 fprintf(o, "\n");
590 }
591 fprintf(o, "\n--\n");
592}
593
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100594
595/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200596 * Local variables:
597 * c-indent-level: 8
598 * c-basic-offset: 8
599 * End:
600 */