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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | #include <proto/buffers.h> |
| 19 | |
| 20 | void **pool_buffer = NULL; |
| 21 | |
| 22 | /* writes <len> bytes from message <msg> to buffer <buf>. Returns 0 in case of |
| 23 | * success, or the number of bytes available otherwise. |
| 24 | * FIXME-20060521: handle unaligned data. |
| 25 | */ |
| 26 | int buffer_write(struct buffer *buf, const char *msg, int len) |
| 27 | { |
| 28 | int max; |
| 29 | |
| 30 | max = buffer_realign(buf); |
| 31 | |
| 32 | if (len > max) |
| 33 | return max; |
| 34 | |
| 35 | memcpy(buf->r, msg, len); |
| 36 | buf->l += len; |
| 37 | buf->r += len; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 38 | buf->total += len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | if (buf->r == buf->data + BUFSIZE) |
| 40 | buf->r = buf->data; |
| 41 | return 0; |
| 42 | } |
| 43 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 44 | /* writes the chunk <chunk> to buffer <buf>. Returns 0 in case of |
| 45 | * success, or the number of bytes available otherwise. If the chunk |
| 46 | * has been written, its size is automatically reset to zero. |
| 47 | */ |
| 48 | int buffer_write_chunk(struct buffer *buf, struct chunk *chunk) |
| 49 | { |
| 50 | int max; |
| 51 | |
| 52 | if (chunk->len == 0) |
| 53 | return 0; |
| 54 | |
| 55 | max = buffer_realign(buf); |
| 56 | |
| 57 | if (chunk->len > max) |
| 58 | return max; |
| 59 | |
| 60 | memcpy(buf->r, chunk->str, chunk->len); |
| 61 | buf->l += chunk->len; |
| 62 | buf->r += chunk->len; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 63 | buf->total += chunk->len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 64 | if (buf->r == buf->data + BUFSIZE) |
| 65 | buf->r = buf->data; |
| 66 | chunk->len = 0; |
| 67 | return 0; |
| 68 | } |
| 69 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | /* |
| 71 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 72 | * and moves <end> just after the end of <str>. |
| 73 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 74 | * the shift value (positive or negative) is returned. |
| 75 | * If there's no space left, the move is not done. |
| 76 | * |
| 77 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 78 | 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] | 79 | { |
| 80 | int delta; |
| 81 | int len; |
| 82 | |
| 83 | len = strlen(str); |
| 84 | delta = len - (end - pos); |
| 85 | |
| 86 | if (delta + b->r >= b->data + BUFSIZE) |
| 87 | return 0; /* no space left */ |
| 88 | |
| 89 | /* first, protect the end of the buffer */ |
| 90 | memmove(end + delta, end, b->data + b->l - end); |
| 91 | |
| 92 | /* now, copy str over pos */ |
| 93 | memcpy(pos, str,len); |
| 94 | |
| 95 | /* we only move data after the displaced zone */ |
| 96 | if (b->r > pos) b->r += delta; |
| 97 | if (b->w > pos) b->w += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | if (b->lr > pos) b->lr += delta; |
| 99 | b->l += delta; |
| 100 | |
| 101 | return delta; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * same except that the string length is given, which allows str to be NULL if |
| 106 | * len is 0. |
| 107 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 108 | 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] | 109 | { |
| 110 | int delta; |
| 111 | |
| 112 | delta = len - (end - pos); |
| 113 | |
| 114 | if (delta + b->r >= b->data + BUFSIZE) |
| 115 | return 0; /* no space left */ |
| 116 | |
| 117 | if (b->data + b->l < end) { |
| 118 | /* The data has been stolen, we could have crashed. |
| 119 | * Maybe we should abort() ? */ |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /* first, protect the end of the buffer */ |
| 124 | memmove(end + delta, end, b->data + b->l - end); |
| 125 | |
| 126 | /* now, copy str over pos */ |
| 127 | if (len) |
| 128 | memcpy(pos, str, len); |
| 129 | |
| 130 | /* we only move data after the displaced zone */ |
| 131 | if (b->r > pos) b->r += delta; |
| 132 | if (b->w > pos) b->w += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 133 | if (b->lr > pos) b->lr += delta; |
| 134 | b->l += delta; |
| 135 | |
| 136 | return delta; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 141 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 142 | * argument informs about the length of string <str> so that we don't have to |
| 143 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 144 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
| 145 | * some circumstances. |
| 146 | * |
| 147 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 148 | */ |
| 149 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 150 | { |
| 151 | int delta; |
| 152 | |
| 153 | delta = len + 2; |
| 154 | |
| 155 | if (delta + b->r >= b->data + BUFSIZE) |
| 156 | return 0; /* no space left */ |
| 157 | |
| 158 | /* first, protect the end of the buffer */ |
| 159 | memmove(pos + delta, pos, b->data + b->l - pos); |
| 160 | |
| 161 | /* now, copy str over pos */ |
| 162 | if (len && str) { |
| 163 | memcpy(pos, str, len); |
| 164 | pos[len] = '\r'; |
| 165 | pos[len + 1] = '\n'; |
| 166 | } |
| 167 | |
| 168 | /* we only move data after the displaced zone */ |
| 169 | if (b->r > pos) b->r += delta; |
| 170 | if (b->w > pos) b->w += delta; |
| 171 | if (b->lr > pos) b->lr += delta; |
| 172 | b->l += delta; |
| 173 | |
| 174 | return delta; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /* |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 179 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
| 180 | * at most <size> chars. If the size is over, nothing is added. Returns |
| 181 | * the new chunk size. |
| 182 | */ |
| 183 | int chunk_printf(struct chunk *chk, int size, const char *fmt, ...) |
| 184 | { |
| 185 | va_list argp; |
| 186 | |
| 187 | va_start(argp, fmt); |
| 188 | chk->len += vsnprintf(chk->str + chk->len, size - chk->len, fmt, argp); |
| 189 | va_end(argp); |
| 190 | return chk->len; |
| 191 | } |
| 192 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 193 | /* |
| 194 | * Dumps part or all of a buffer. |
| 195 | */ |
| 196 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 197 | { |
| 198 | fprintf(o, "Dumping buffer %p\n", b); |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 199 | fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n", |
| 200 | b->data, b->l, b->r, b->w, b->lr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 201 | |
| 202 | if (!to || to > b->l) |
| 203 | to = b->l; |
| 204 | |
| 205 | fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); |
| 206 | for (; from < to; from++) { |
| 207 | if ((from & 15) == 0) |
| 208 | fprintf(o, " %04x: ", from); |
| 209 | fprintf(o, "%02x ", b->data[from]); |
| 210 | if ((from & 15) == 7) |
| 211 | fprintf(o, "- "); |
| 212 | else if (((from & 15) == 15) && (from != to-1)) |
| 213 | fprintf(o, "\n"); |
| 214 | } |
| 215 | fprintf(o, "\n--\n"); |
| 216 | } |
| 217 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 218 | |
| 219 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 220 | * Local variables: |
| 221 | * c-indent-level: 8 |
| 222 | * c-basic-offset: 8 |
| 223 | * End: |
| 224 | */ |