blob: 7e90ee6e2e8388d1864d177c14934cbda11c306f [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 */
Willy Tarreau83061a82018-07-13 11:56:34 +020025static THREAD_LOCAL struct buffer *trash_chunk;
26static THREAD_LOCAL struct buffer trash_chunk1;
27static THREAD_LOCAL struct buffer trash_chunk2;
Willy Tarreau47ca5452012-12-23 20:22:19 +010028
29/* trash buffers used for various conversions */
30static int trash_size;
Christopher Faulet6adad112017-04-21 16:47:03 +020031static THREAD_LOCAL char *trash_buf1;
32static THREAD_LOCAL char *trash_buf2;
Willy Tarreau47ca5452012-12-23 20:22:19 +010033
Willy Tarreaub686afd2017-02-08 11:06:11 +010034/* the trash pool for reentrant allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +010035struct pool_head *pool_head_trash = NULL;
Willy Tarreaub686afd2017-02-08 11:06:11 +010036
Christopher Faulet748919a2017-07-26 14:59:46 +020037/* this is used to drain data, and as a temporary buffer for sprintf()... */
Willy Tarreau83061a82018-07-13 11:56:34 +020038THREAD_LOCAL struct buffer trash = { };
Christopher Faulet748919a2017-07-26 14:59:46 +020039
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*/
Willy Tarreau83061a82018-07-13 11:56:34 +020049struct buffer *get_trash_chunk(void)
Willy Tarreau47ca5452012-12-23 20:22:19 +010050{
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{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020071 chunk_init(&trash, my_realloc2(trash.area, bufsize), bufsize);
Willy Tarreau47ca5452012-12-23 20:22:19 +010072 trash_size = bufsize;
Hubert Verstraete831962e2016-06-28 22:44:26 +020073 trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize);
74 trash_buf2 = (char *)my_realloc2(trash_buf2, bufsize);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020075 return trash.area && trash_buf1 && trash_buf2;
Willy Tarreau47ca5452012-12-23 20:22:19 +010076}
77
Christopher Fauletcd7879a2017-10-27 13:53:47 +020078static int init_trash_buffers_per_thread()
79{
80 return alloc_trash_buffers(global.tune.bufsize);
81}
82
83static void deinit_trash_buffers_per_thread()
84{
85 chunk_destroy(&trash);
86 free(trash_buf2);
87 free(trash_buf1);
88 trash_buf2 = NULL;
89 trash_buf1 = NULL;
90}
91
Christopher Faulet748919a2017-07-26 14:59:46 +020092/* Initialize the trash buffers. It returns 0 if an error occurred. */
Christopher Fauletcd7879a2017-10-27 13:53:47 +020093int init_trash_buffers(int first)
Christopher Faulet748919a2017-07-26 14:59:46 +020094{
Christopher Fauletcd7879a2017-10-27 13:53:47 +020095 if (!first) {
96 hap_register_per_thread_init(init_trash_buffers_per_thread);
97 hap_register_per_thread_deinit(deinit_trash_buffers_per_thread);
Christopher Faulet6adad112017-04-21 16:47:03 +020098 }
Willy Tarreaubafbe012017-11-24 17:34:44 +010099 pool_destroy(pool_head_trash);
Willy Tarreau83061a82018-07-13 11:56:34 +0200100 pool_head_trash = create_pool("trash",
101 sizeof(struct buffer) + global.tune.bufsize,
102 MEM_F_EXACT);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100103 if (!pool_head_trash || !alloc_trash_buffers(global.tune.bufsize))
Christopher Faulet748919a2017-07-26 14:59:46 +0200104 return 0;
105 return 1;
106}
107
Willy Tarreauc7e42382012-08-24 19:22:53 +0200108/*
David Carlier60deeba2015-09-25 11:58:12 +0100109 * free the trash buffers
110 */
Christopher Faulet748919a2017-07-26 14:59:46 +0200111void deinit_trash_buffers(void)
David Carlier60deeba2015-09-25 11:58:12 +0100112{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100113 pool_destroy(pool_head_trash);
David Carlier60deeba2015-09-25 11:58:12 +0100114}
115
116/*
Willy Tarreaub686afd2017-02-08 11:06:11 +0100117 * Allocate a trash chunk from the reentrant pool. The buffer starts at the
118 * end of the chunk. This chunk must be freed using free_trash_chunk(). This
119 * call may fail and the caller is responsible for checking that the returned
120 * pointer is not NULL.
121 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200122struct buffer *alloc_trash_chunk(void)
Willy Tarreaub686afd2017-02-08 11:06:11 +0100123{
Willy Tarreau83061a82018-07-13 11:56:34 +0200124 struct buffer *chunk;
Willy Tarreaub686afd2017-02-08 11:06:11 +0100125
Willy Tarreaubafbe012017-11-24 17:34:44 +0100126 chunk = pool_alloc(pool_head_trash);
Willy Tarreaub686afd2017-02-08 11:06:11 +0100127 if (chunk) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200128 char *buf = (char *)chunk + sizeof(struct buffer);
Willy Tarreaub686afd2017-02-08 11:06:11 +0100129 *buf = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200130 chunk_init(chunk, buf,
131 pool_head_trash->size - sizeof(struct buffer));
Willy Tarreaub686afd2017-02-08 11:06:11 +0100132 }
133 return chunk;
134}
135
136/*
Willy Tarreau77804732012-10-29 16:14:26 +0100137 * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of
138 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
139 * the new chunk size, or < 0 in case of failure.
140 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200141int chunk_printf(struct buffer *chk, const char *fmt, ...)
Willy Tarreau77804732012-10-29 16:14:26 +0100142{
143 va_list argp;
144 int ret;
145
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200146 if (!chk->area || !chk->size)
Willy Tarreau77804732012-10-29 16:14:26 +0100147 return 0;
148
149 va_start(argp, fmt);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200150 ret = vsnprintf(chk->area, chk->size, fmt, argp);
Willy Tarreau77804732012-10-29 16:14:26 +0100151 va_end(argp);
Willy Tarreau77804732012-10-29 16:14:26 +0100152
153 if (ret >= chk->size)
Willy Tarreau5f6333c2018-08-22 05:14:37 +0200154 return -1;
Willy Tarreau77804732012-10-29 16:14:26 +0100155
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200156 chk->data = ret;
157 return chk->data;
Willy Tarreau77804732012-10-29 16:14:26 +0100158}
159
160/*
Willy Tarreauc7e42382012-08-24 19:22:53 +0200161 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
162 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
163 * the new chunk size.
164 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200165int chunk_appendf(struct buffer *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200166{
167 va_list argp;
168 int ret;
169
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200170 if (chk->data < 0 || !chk->area || !chk->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200171 return 0;
172
173 va_start(argp, fmt);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200174 ret = vsnprintf(chk->area + chk->data, chk->size - chk->data, fmt,
175 argp);
176 if (ret >= chk->size - chk->data)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200177 /* do not copy anything in case of truncation */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200178 chk->area[chk->data] = 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200179 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200180 chk->data += ret;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200181 va_end(argp);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200182 return chk->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200183}
184
185/*
186 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
187 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
188 * If the chk->len is over, nothing is added. Returns the new chunk size.
189 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200190int chunk_htmlencode(struct buffer *dst, struct buffer *src)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200191{
192 int i, l;
193 int olen, free;
194 char c;
195
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200196 if (dst->data < 0)
197 return dst->data;
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100198
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200199 olen = dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200200
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200201 for (i = 0; i < src->data; i++) {
202 free = dst->size - dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200203
204 if (!free) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200205 dst->data = olen;
206 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200207 }
208
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200209 c = src->area[i];
Willy Tarreauc7e42382012-08-24 19:22:53 +0200210
211 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200212 l = snprintf(dst->area + dst->data, free, "&#%u;",
213 (unsigned char)c);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200214
215 if (free < l) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200216 dst->data = olen;
217 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200218 }
219
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200220 dst->data += l;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200221 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200222 dst->area[dst->data] = c;
223 dst->data++;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200224 }
225 }
226
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200227 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200228}
229
230/*
231 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
232 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
233 * If the chk->len is over, nothing is added. Returns the new chunk size.
234 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200235int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200236{
237 int i, l;
238 int olen, free;
239 char c;
240
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200241 if (dst->data < 0)
242 return dst->data;
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100243
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200244 olen = dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200245
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200246 for (i = 0; i < src->data; i++) {
247 free = dst->size - dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200248
249 if (!free) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200250 dst->data = olen;
251 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200252 }
253
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200254 c = src->area[i];
Willy Tarreauc7e42382012-08-24 19:22:53 +0200255
256 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200257 l = snprintf(dst->area + dst->data, free, "<%02X>",
258 (unsigned char)c);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200259
260 if (free < l) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200261 dst->data = olen;
262 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200263 }
264
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200265 dst->data += l;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200266 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200267 dst->area[dst->data] = c;
268 dst->data++;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200269 }
270 }
271
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200272 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200273}
274
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200275/* Compares the string in chunk <chk> with the string in <str> which must be
276 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
277 * to be null.
278 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200279int chunk_strcmp(const struct buffer *chk, const char *str)
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200280{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200281 const char *s1 = chk->area;
282 int len = chk->data;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200283 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 } while (!diff);
292 return diff;
293}
294
295/* Case-insensitively compares the string in chunk <chk> with the string in
296 * <str> which must be zero-terminated. Return is the same as with strcmp().
297 * Neither is allowed to be null.
298 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200299int chunk_strcasecmp(const struct buffer *chk, const char *str)
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200300{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200301 const char *s1 = chk->area;
302 int len = chk->data;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200303 int diff = 0;
304
305 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200306 if (--len < 0) {
307 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200308 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200309 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200310 diff = (unsigned char)*s1 - (unsigned char)*str;
311 if (unlikely(diff)) {
312 unsigned int l = (unsigned char)*s1;
313 unsigned int r = (unsigned char)*str;
314
315 l -= 'a';
316 r -= 'a';
317
318 if (likely(l <= (unsigned char)'z' - 'a'))
319 l -= 'a' - 'A';
320 if (likely(r <= (unsigned char)'z' - 'a'))
321 r -= 'a' - 'A';
322 diff = l - r;
323 }
324 s1++; str++;
325 } while (!diff);
326 return diff;
327}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200328
329/*
330 * Local variables:
331 * c-indent-level: 8
332 * c-basic-offset: 8
333 * End:
334 */