blob: e99535a438293a6ef2d443ec81431814d3b6c730 [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
2 * Chunk management functions.
3 *
4 * Copyright 2000-2012 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
13#include <ctype.h>
14#include <stdarg.h>
15#include <stdio.h>
16#include <string.h>
17
18#include <common/config.h>
19#include <common/chunk.h>
Hubert Verstraete831962e2016-06-28 22:44:26 +020020#include <common/standard.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020021
Christopher Faulet748919a2017-07-26 14:59:46 +020022#include <types/global.h>
23
Willy Tarreau47ca5452012-12-23 20:22:19 +010024/* trash chunks used for various conversions */
25static struct chunk *trash_chunk;
26static struct chunk trash_chunk1;
27static struct chunk trash_chunk2;
28
29/* trash buffers used for various conversions */
30static int trash_size;
31static char *trash_buf1;
32static char *trash_buf2;
33
Willy Tarreaub686afd2017-02-08 11:06:11 +010034/* the trash pool for reentrant allocations */
35struct pool_head *pool2_trash = NULL;
36
Christopher Faulet748919a2017-07-26 14:59:46 +020037/* this is used to drain data, and as a temporary buffer for sprintf()... */
38struct chunk trash = { .str = NULL };
39
Willy Tarreau47ca5452012-12-23 20:22:19 +010040/*
41* Returns a pre-allocated and initialized trash chunk that can be used for any
42* type of conversion. Two chunks and their respective buffers are alternatively
43* returned so that it is always possible to iterate data transformations without
44* losing the data being transformed. The blocks are initialized to the size of
Willy Tarreau031ad232013-12-11 17:32:08 +010045* a standard buffer, so they should be enough for everything. For convenience,
46* a zero is always emitted at the beginning of the string so that it may be
47* used as an empty string as well.
Willy Tarreau47ca5452012-12-23 20:22:19 +010048*/
49struct chunk *get_trash_chunk(void)
50{
51 char *trash_buf;
52
53 if (trash_chunk == &trash_chunk1) {
54 trash_chunk = &trash_chunk2;
55 trash_buf = trash_buf2;
56 }
57 else {
58 trash_chunk = &trash_chunk1;
59 trash_buf = trash_buf1;
60 }
Willy Tarreau031ad232013-12-11 17:32:08 +010061 *trash_buf = 0;
Willy Tarreau47ca5452012-12-23 20:22:19 +010062 chunk_init(trash_chunk, trash_buf, trash_size);
63 return trash_chunk;
64}
65
Willy Tarreau2819e992013-12-13 14:41:10 +010066/* (re)allocates the trash buffers. Returns 0 in case of failure. It is
67 * possible to call this function multiple times if the trash size changes.
68 */
Christopher Faulet748919a2017-07-26 14:59:46 +020069static int alloc_trash_buffers(int bufsize)
Willy Tarreau47ca5452012-12-23 20:22:19 +010070{
71 trash_size = bufsize;
Hubert Verstraete831962e2016-06-28 22:44:26 +020072 trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize);
73 trash_buf2 = (char *)my_realloc2(trash_buf2, bufsize);
Willy Tarreaub686afd2017-02-08 11:06:11 +010074 pool2_trash = create_pool("trash", sizeof(struct chunk) + bufsize, MEM_F_EXACT);
75 return trash_buf1 && trash_buf2 && pool2_trash;
Willy Tarreau47ca5452012-12-23 20:22:19 +010076}
77
Christopher Faulet748919a2017-07-26 14:59:46 +020078/* Initialize the trash buffers. It returns 0 if an error occurred. */
79int init_trash_buffers()
80{
81 chunk_init(&trash, my_realloc2(trash.str, global.tune.bufsize), global.tune.bufsize);
82 if (!trash.str || !alloc_trash_buffers(global.tune.bufsize))
83 return 0;
84 return 1;
85}
86
Willy Tarreauc7e42382012-08-24 19:22:53 +020087/*
David Carlier60deeba2015-09-25 11:58:12 +010088 * free the trash buffers
89 */
Christopher Faulet748919a2017-07-26 14:59:46 +020090void deinit_trash_buffers(void)
David Carlier60deeba2015-09-25 11:58:12 +010091{
Christopher Faulet748919a2017-07-26 14:59:46 +020092 chunk_destroy(&trash);
David Carlier60deeba2015-09-25 11:58:12 +010093 free(trash_buf2);
94 free(trash_buf1);
95 trash_buf2 = NULL;
96 trash_buf1 = NULL;
97}
98
99/*
Willy Tarreaub686afd2017-02-08 11:06:11 +0100100 * Allocate a trash chunk from the reentrant pool. The buffer starts at the
101 * end of the chunk. This chunk must be freed using free_trash_chunk(). This
102 * call may fail and the caller is responsible for checking that the returned
103 * pointer is not NULL.
104 */
105struct chunk *alloc_trash_chunk(void)
106{
107 struct chunk *chunk;
108
109 chunk = pool_alloc2(pool2_trash);
110 if (chunk) {
111 char *buf = (char *)chunk + sizeof(struct chunk);
112 *buf = 0;
113 chunk_init(chunk, buf, pool2_trash->size - sizeof(struct chunk));
114 }
115 return chunk;
116}
117
118/*
Willy Tarreau77804732012-10-29 16:14:26 +0100119 * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of
120 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
121 * the new chunk size, or < 0 in case of failure.
122 */
123int chunk_printf(struct chunk *chk, const char *fmt, ...)
124{
125 va_list argp;
126 int ret;
127
128 if (!chk->str || !chk->size)
129 return 0;
130
131 va_start(argp, fmt);
132 ret = vsnprintf(chk->str, chk->size, fmt, argp);
133 va_end(argp);
134 chk->len = ret;
135
136 if (ret >= chk->size)
137 ret = -1;
138
139 chk->len = ret;
140 return chk->len;
141}
142
143/*
Willy Tarreauc7e42382012-08-24 19:22:53 +0200144 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
145 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
146 * the new chunk size.
147 */
Willy Tarreau77804732012-10-29 16:14:26 +0100148int chunk_appendf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200149{
150 va_list argp;
151 int ret;
152
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100153 if (chk->len < 0 || !chk->str || !chk->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200154 return 0;
155
156 va_start(argp, fmt);
157 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
158 if (ret >= chk->size - chk->len)
159 /* do not copy anything in case of truncation */
160 chk->str[chk->len] = 0;
161 else
162 chk->len += ret;
163 va_end(argp);
164 return chk->len;
165}
166
167/*
168 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
169 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
170 * If the chk->len is over, nothing is added. Returns the new chunk size.
171 */
172int chunk_htmlencode(struct chunk *dst, struct chunk *src)
173{
174 int i, l;
175 int olen, free;
176 char c;
177
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100178 if (dst->len < 0)
179 return dst->len;
180
Willy Tarreauc7e42382012-08-24 19:22:53 +0200181 olen = dst->len;
182
183 for (i = 0; i < src->len; i++) {
184 free = dst->size - dst->len;
185
186 if (!free) {
187 dst->len = olen;
188 return dst->len;
189 }
190
191 c = src->str[i];
192
193 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
194 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
195
196 if (free < l) {
197 dst->len = olen;
198 return dst->len;
199 }
200
201 dst->len += l;
202 } else {
203 dst->str[dst->len] = c;
204 dst->len++;
205 }
206 }
207
208 return dst->len;
209}
210
211/*
212 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
213 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
214 * If the chk->len is over, nothing is added. Returns the new chunk size.
215 */
216int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc)
217{
218 int i, l;
219 int olen, free;
220 char c;
221
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100222 if (dst->len < 0)
223 return dst->len;
224
Willy Tarreauc7e42382012-08-24 19:22:53 +0200225 olen = dst->len;
226
227 for (i = 0; i < src->len; i++) {
228 free = dst->size - dst->len;
229
230 if (!free) {
231 dst->len = olen;
232 return dst->len;
233 }
234
235 c = src->str[i];
236
237 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
238 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
239
240 if (free < l) {
241 dst->len = olen;
242 return dst->len;
243 }
244
245 dst->len += l;
246 } else {
247 dst->str[dst->len] = c;
248 dst->len++;
249 }
250 }
251
252 return dst->len;
253}
254
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200255/* Compares the string in chunk <chk> with the string in <str> which must be
256 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
257 * to be null.
258 */
259int chunk_strcmp(const struct chunk *chk, const char *str)
260{
261 const char *s1 = chk->str;
262 int len = chk->len;
263 int diff = 0;
264
265 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200266 if (--len < 0) {
267 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200268 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200269 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200270 diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
271 } while (!diff);
272 return diff;
273}
274
275/* Case-insensitively compares the string in chunk <chk> with the string in
276 * <str> which must be zero-terminated. Return is the same as with strcmp().
277 * Neither is allowed to be null.
278 */
279int chunk_strcasecmp(const struct chunk *chk, const char *str)
280{
281 const char *s1 = chk->str;
282 int len = chk->len;
283 int diff = 0;
284
285 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200286 if (--len < 0) {
287 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200288 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200289 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200290 diff = (unsigned char)*s1 - (unsigned char)*str;
291 if (unlikely(diff)) {
292 unsigned int l = (unsigned char)*s1;
293 unsigned int r = (unsigned char)*str;
294
295 l -= 'a';
296 r -= 'a';
297
298 if (likely(l <= (unsigned char)'z' - 'a'))
299 l -= 'a' - 'A';
300 if (likely(r <= (unsigned char)'z' - 'a'))
301 r -= 'a' - 'A';
302 diff = l - r;
303 }
304 s1++; str++;
305 } while (!diff);
306 return diff;
307}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200308
309/*
310 * Local variables:
311 * c-indent-level: 8
312 * c-basic-offset: 8
313 * End:
314 */