Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Buffer management functions. |
| 3 | * |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 13 | #include <stdarg.h> |
| 14 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 15 | #include <string.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 16 | |
| 17 | #include <common/config.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 18 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | #include <proto/buffers.h> |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 20 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 22 | struct pool_head *pool2_buffer; |
| 23 | |
| 24 | |
| 25 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 26 | int init_buffer() |
| 27 | { |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 28 | pool2_buffer = create_pool("buffer", sizeof(struct buffer) + global.tune.bufsize, MEM_F_SHARED); |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 29 | return pool2_buffer != NULL; |
| 30 | } |
| 31 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 33 | /* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 34 | * success, -2 if the message is larger than the buffer size, or the number of |
| 35 | * bytes available otherwise. The send limit is automatically adjusted with the |
| 36 | * amount of data written. FIXME-20060521: handle unaligned data. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | */ |
| 38 | int buffer_write(struct buffer *buf, const char *msg, int len) |
| 39 | { |
| 40 | int max; |
| 41 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 42 | if (len == 0) |
| 43 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 45 | if (len > buf->size) { |
| 46 | /* we can't write this chunk and will never be able to, because |
| 47 | * it is larger than the buffer. This must be reported as an |
| 48 | * error. Then we return -2 so that writers that don't care can |
| 49 | * ignore it and go on, and others can check for this value. |
| 50 | */ |
| 51 | return -2; |
| 52 | } |
| 53 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 54 | max = buffer_realign(buf); |
| 55 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | if (len > max) |
| 57 | return max; |
| 58 | |
| 59 | memcpy(buf->r, msg, len); |
| 60 | buf->l += len; |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 61 | buf->send_max += len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | buf->r += len; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 63 | buf->total += len; |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 64 | if (buf->r == buf->data + buf->size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | buf->r = buf->data; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 66 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 67 | buf->flags &= ~(BF_OUT_EMPTY|BF_FULL); |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 68 | if (buf->l >= buf->max_len) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 69 | buf->flags |= BF_FULL; |
| 70 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 71 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 74 | /* Try to write string <str> into buffer <buf> after length controls. This |
| 75 | * is the equivalent of buffer_write() except that to_forward and send_max |
| 76 | * are updated and that max_len is respected. Returns -1 in case of success, |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 77 | * -2 if it is larger than the buffer size, or the number of bytes available |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 78 | * otherwise. The send limit is automatically adjusted with the amount of data |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 79 | * written. |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 80 | */ |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 81 | int buffer_feed(struct buffer *buf, const char *str, int len) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 82 | { |
| 83 | int max; |
| 84 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 85 | if (len == 0) |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 86 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 87 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 88 | if (len > buf->max_len) { |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 89 | /* we can't write this chunk and will never be able to, because |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 90 | * it is larger than the buffer's current max size. |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 91 | */ |
| 92 | return -2; |
| 93 | } |
| 94 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 95 | max = buffer_contig_space(buf); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 96 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 97 | if (len > max) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 98 | return max; |
| 99 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 100 | memcpy(buf->r, str, len); |
| 101 | buf->l += len; |
| 102 | buf->r += len; |
| 103 | buf->total += len; |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 104 | if (buf->to_forward) { |
| 105 | unsigned long fwd = len; |
| 106 | if (buf->to_forward != BUF_INFINITE_FORWARD) { |
| 107 | if (fwd > buf->to_forward) |
| 108 | fwd = buf->to_forward; |
| 109 | buf->to_forward -= fwd; |
| 110 | } |
| 111 | buf->send_max += fwd; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 112 | buf->flags &= ~BF_OUT_EMPTY; |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 115 | if (buf->r == buf->data + buf->size) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 116 | buf->r = buf->data; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 117 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 118 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 119 | if (buf->l >= buf->max_len) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 120 | buf->flags |= BF_FULL; |
| 121 | |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 122 | /* notify that some data was read from the SI into the buffer */ |
| 123 | buf->flags |= BF_READ_PARTIAL; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 124 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 127 | /* Get one text line out of a buffer from a stream interface. |
| 128 | * Return values : |
| 129 | * >0 : number of bytes read. Includes the \n if present before len or end. |
| 130 | * =0 : no '\n' before end found. <buf> is undefined. |
| 131 | * <0 : no more bytes readable + shutdown set. |
| 132 | * The buffer status is not changed. The caller must call buffer_skip() to |
| 133 | * update it. The '\n' is waited for as long as neither the buffer nor the |
| 134 | * output are full. If either of them is full, the string may be returned |
| 135 | * as is, without the '\n'. |
| 136 | */ |
| 137 | int buffer_si_peekline(struct buffer *buf, char *str, int len) |
| 138 | { |
| 139 | int ret, max; |
| 140 | char *p; |
| 141 | |
| 142 | ret = 0; |
| 143 | max = len; |
| 144 | if (!buf->send_max) { |
| 145 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 146 | ret = -1; |
| 147 | goto out; |
| 148 | } |
| 149 | |
| 150 | p = buf->w; |
| 151 | |
| 152 | if (max > buf->send_max) { |
| 153 | max = buf->send_max; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 154 | str[max-1] = 0; |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 155 | } |
| 156 | while (max) { |
| 157 | *str++ = *p; |
| 158 | ret++; |
| 159 | max--; |
| 160 | |
| 161 | if (*p == '\n') |
| 162 | break; |
| 163 | p++; |
| 164 | if (p == buf->data + buf->size) |
| 165 | p = buf->data; |
| 166 | } |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 167 | if (ret > 0 && ret < len && ret < buf->send_max && |
| 168 | *(str-1) != '\n' && |
| 169 | !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 170 | ret = 0; |
| 171 | out: |
| 172 | if (max) |
| 173 | *str = 0; |
| 174 | return ret; |
| 175 | } |
| 176 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 177 | /* |
| 178 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 179 | * and moves <end> just after the end of <str>. |
| 180 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 181 | * the shift value (positive or negative) is returned. |
| 182 | * If there's no space left, the move is not done. |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 183 | * The function does not adjust ->send_max nor BF_OUT_EMPTY because it does not |
| 184 | * make sense to use it on data scheduled to be sent. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 185 | * |
| 186 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 187 | int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 188 | { |
| 189 | int delta; |
| 190 | int len; |
| 191 | |
| 192 | len = strlen(str); |
| 193 | delta = len - (end - pos); |
| 194 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 195 | if (delta + b->r >= b->data + b->size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 196 | return 0; /* no space left */ |
| 197 | |
| 198 | /* first, protect the end of the buffer */ |
| 199 | memmove(end + delta, end, b->data + b->l - end); |
| 200 | |
| 201 | /* now, copy str over pos */ |
| 202 | memcpy(pos, str,len); |
| 203 | |
| 204 | /* we only move data after the displaced zone */ |
| 205 | if (b->r > pos) b->r += delta; |
| 206 | if (b->w > pos) b->w += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 207 | if (b->lr > pos) b->lr += delta; |
| 208 | b->l += delta; |
| 209 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 210 | b->flags &= ~BF_FULL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 211 | if (b->l == 0) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 212 | b->r = b->w = b->lr = b->data; |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 213 | if (b->l >= b->max_len) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 214 | b->flags |= BF_FULL; |
| 215 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 216 | return delta; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * same except that the string length is given, which allows str to be NULL if |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 221 | * len is 0. The send limit is *not* adjusted. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 222 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 223 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | { |
| 225 | int delta; |
| 226 | |
| 227 | delta = len - (end - pos); |
| 228 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 229 | if (delta + b->r >= b->data + b->size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 230 | return 0; /* no space left */ |
| 231 | |
| 232 | if (b->data + b->l < end) { |
| 233 | /* The data has been stolen, we could have crashed. |
| 234 | * Maybe we should abort() ? */ |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* first, protect the end of the buffer */ |
| 239 | memmove(end + delta, end, b->data + b->l - end); |
| 240 | |
| 241 | /* now, copy str over pos */ |
| 242 | if (len) |
| 243 | memcpy(pos, str, len); |
| 244 | |
| 245 | /* we only move data after the displaced zone */ |
| 246 | if (b->r > pos) b->r += delta; |
| 247 | if (b->w > pos) b->w += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 248 | if (b->lr > pos) b->lr += delta; |
| 249 | b->l += delta; |
| 250 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 251 | b->flags &= ~BF_FULL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 252 | if (b->l == 0) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 253 | b->r = b->w = b->lr = b->data; |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 254 | if (b->l >= b->max_len) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 255 | b->flags |= BF_FULL; |
| 256 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 257 | return delta; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 262 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 263 | * argument informs about the length of string <str> so that we don't have to |
| 264 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 265 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 266 | * some circumstances. The send limit is *not* adjusted. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 267 | * |
| 268 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 269 | */ |
| 270 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 271 | { |
| 272 | int delta; |
| 273 | |
| 274 | delta = len + 2; |
| 275 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 276 | if (delta + b->r >= b->data + b->size) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 277 | return 0; /* no space left */ |
| 278 | |
| 279 | /* first, protect the end of the buffer */ |
| 280 | memmove(pos + delta, pos, b->data + b->l - pos); |
| 281 | |
| 282 | /* now, copy str over pos */ |
| 283 | if (len && str) { |
| 284 | memcpy(pos, str, len); |
| 285 | pos[len] = '\r'; |
| 286 | pos[len + 1] = '\n'; |
| 287 | } |
| 288 | |
| 289 | /* we only move data after the displaced zone */ |
| 290 | if (b->r > pos) b->r += delta; |
| 291 | if (b->w > pos) b->w += delta; |
| 292 | if (b->lr > pos) b->lr += delta; |
| 293 | b->l += delta; |
| 294 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 295 | b->flags &= ~BF_FULL; |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 296 | if (b->l >= b->max_len) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 297 | b->flags |= BF_FULL; |
| 298 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 299 | return delta; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /* |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 304 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 305 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 306 | * the new chunk size. |
| 307 | */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 308 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 309 | { |
| 310 | va_list argp; |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 311 | int ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 312 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 313 | if (!chk->str || !chk->size) |
| 314 | return 0; |
| 315 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 316 | va_start(argp, fmt); |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 317 | ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp); |
| 318 | if (ret >= chk->size - chk->len) |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 319 | /* do not copy anything in case of truncation */ |
| 320 | chk->str[chk->len] = 0; |
| 321 | else |
| 322 | chk->len += ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 323 | va_end(argp); |
| 324 | return chk->len; |
| 325 | } |
| 326 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 327 | /* |
| 328 | * Dumps part or all of a buffer. |
| 329 | */ |
| 330 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 331 | { |
| 332 | fprintf(o, "Dumping buffer %p\n", b); |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 333 | fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n", |
| 334 | b->data, b->l, b->r, b->w, b->lr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 335 | |
| 336 | if (!to || to > b->l) |
| 337 | to = b->l; |
| 338 | |
| 339 | fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); |
| 340 | for (; from < to; from++) { |
| 341 | if ((from & 15) == 0) |
| 342 | fprintf(o, " %04x: ", from); |
| 343 | fprintf(o, "%02x ", b->data[from]); |
| 344 | if ((from & 15) == 7) |
| 345 | fprintf(o, "- "); |
| 346 | else if (((from & 15) == 15) && (from != to-1)) |
| 347 | fprintf(o, "\n"); |
| 348 | } |
| 349 | fprintf(o, "\n--\n"); |
| 350 | } |
| 351 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 352 | |
| 353 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 354 | * Local variables: |
| 355 | * c-indent-level: 8 |
| 356 | * c-basic-offset: 8 |
| 357 | * End: |
| 358 | */ |