blob: 8dfa857966b41a80090c0cd625a38e559a1cff7b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Buffer management functions.
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
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 Tarreauc0dde7a2007-01-01 21:38:07 +010013#include <stdarg.h>
14#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <string.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018#include <proto/buffers.h>
19
20void **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 */
26int 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 Tarreau35d66b02007-01-02 00:28:21 +010038 buf->total += len;
Willy Tarreaubaaee002006-06-26 02:48:02 +020039 if (buf->r == buf->data + BUFSIZE)
40 buf->r = buf->data;
41 return 0;
42}
43
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010044/* 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 */
48int 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 Tarreau35d66b02007-01-02 00:28:21 +010063 buf->total += chunk->len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010064 if (buf->r == buf->data + BUFSIZE)
65 buf->r = buf->data;
66 chunk->len = 0;
67 return 0;
68}
69
Willy Tarreaubaaee002006-06-26 02:48:02 +020070/*
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 */
78int buffer_replace(struct buffer *b, char *pos, char *end, char *str)
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;
98 if (b->h > pos) b->h += delta;
99 if (b->lr > pos) b->lr += delta;
100 b->l += delta;
101
102 return delta;
103}
104
105/*
106 * same except that the string length is given, which allows str to be NULL if
107 * len is 0.
108 */
109int buffer_replace2(struct buffer *b, char *pos, char *end, char *str, int len)
110{
111 int delta;
112
113 delta = len - (end - pos);
114
115 if (delta + b->r >= b->data + BUFSIZE)
116 return 0; /* no space left */
117
118 if (b->data + b->l < end) {
119 /* The data has been stolen, we could have crashed.
120 * Maybe we should abort() ? */
121 return 0;
122 }
123
124 /* first, protect the end of the buffer */
125 memmove(end + delta, end, b->data + b->l - end);
126
127 /* now, copy str over pos */
128 if (len)
129 memcpy(pos, str, len);
130
131 /* we only move data after the displaced zone */
132 if (b->r > pos) b->r += delta;
133 if (b->w > pos) b->w += delta;
134 if (b->h > pos) b->h += delta;
135 if (b->lr > pos) b->lr += delta;
136 b->l += delta;
137
138 return delta;
139}
140
141
142/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100143 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
144 * at most <size> chars. If the size is over, nothing is added. Returns
145 * the new chunk size.
146 */
147int chunk_printf(struct chunk *chk, int size, const char *fmt, ...)
148{
149 va_list argp;
150
151 va_start(argp, fmt);
152 chk->len += vsnprintf(chk->str + chk->len, size - chk->len, fmt, argp);
153 va_end(argp);
154 return chk->len;
155}
156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100157/*
158 * Dumps part or all of a buffer.
159 */
160void buffer_dump(FILE *o, struct buffer *b, int from, int to)
161{
162 fprintf(o, "Dumping buffer %p\n", b);
163 fprintf(o, " data=%p l=%d r=%p w=%p h=%p lr=%p\n",
164 b->data, b->l, b->r, b->w, b->h, b->lr);
165
166 if (!to || to > b->l)
167 to = b->l;
168
169 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
170 for (; from < to; from++) {
171 if ((from & 15) == 0)
172 fprintf(o, " %04x: ", from);
173 fprintf(o, "%02x ", b->data[from]);
174 if ((from & 15) == 7)
175 fprintf(o, "- ");
176 else if (((from & 15) == 15) && (from != to-1))
177 fprintf(o, "\n");
178 }
179 fprintf(o, "\n--\n");
180}
181
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100182
183/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184 * Local variables:
185 * c-indent-level: 8
186 * c-basic-offset: 8
187 * End:
188 */