blob: abc039d12f0e3003c09e9a22cd8fd495de5acaf3 [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020019#include <haproxy/chunk.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020020#include <haproxy/global.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/tools.h>
Christopher Faulet748919a2017-07-26 14:59:46 +020022
Willy Tarreau47ca5452012-12-23 20:22:19 +010023/* trash chunks used for various conversions */
Willy Tarreau83061a82018-07-13 11:56:34 +020024static THREAD_LOCAL struct buffer *trash_chunk;
25static THREAD_LOCAL struct buffer trash_chunk1;
26static THREAD_LOCAL struct buffer trash_chunk2;
Willy Tarreau47ca5452012-12-23 20:22:19 +010027
28/* trash buffers used for various conversions */
Willy Tarreauff882702021-04-10 17:23:00 +020029static int trash_size __read_mostly;
Christopher Faulet6adad112017-04-21 16:47:03 +020030static THREAD_LOCAL char *trash_buf1;
31static THREAD_LOCAL char *trash_buf2;
Willy Tarreau47ca5452012-12-23 20:22:19 +010032
Willy Tarreaub686afd2017-02-08 11:06:11 +010033/* the trash pool for reentrant allocations */
Willy Tarreauff882702021-04-10 17:23:00 +020034struct pool_head *pool_head_trash __read_mostly = NULL;
Willy Tarreaub686afd2017-02-08 11:06:11 +010035
Christopher Faulet748919a2017-07-26 14:59:46 +020036/* this is used to drain data, and as a temporary buffer for sprintf()... */
Willy Tarreau83061a82018-07-13 11:56:34 +020037THREAD_LOCAL struct buffer trash = { };
Christopher Faulet748919a2017-07-26 14:59:46 +020038
Willy Tarreau47ca5452012-12-23 20:22:19 +010039/*
40* Returns a pre-allocated and initialized trash chunk that can be used for any
41* type of conversion. Two chunks and their respective buffers are alternatively
42* returned so that it is always possible to iterate data transformations without
43* losing the data being transformed. The blocks are initialized to the size of
Willy Tarreau031ad232013-12-11 17:32:08 +010044* a standard buffer, so they should be enough for everything. For convenience,
45* a zero is always emitted at the beginning of the string so that it may be
46* used as an empty string as well.
Willy Tarreau47ca5452012-12-23 20:22:19 +010047*/
Willy Tarreau83061a82018-07-13 11:56:34 +020048struct buffer *get_trash_chunk(void)
Willy Tarreau47ca5452012-12-23 20:22:19 +010049{
50 char *trash_buf;
51
52 if (trash_chunk == &trash_chunk1) {
53 trash_chunk = &trash_chunk2;
54 trash_buf = trash_buf2;
55 }
56 else {
57 trash_chunk = &trash_chunk1;
58 trash_buf = trash_buf1;
59 }
Willy Tarreau031ad232013-12-11 17:32:08 +010060 *trash_buf = 0;
Willy Tarreau47ca5452012-12-23 20:22:19 +010061 chunk_init(trash_chunk, trash_buf, trash_size);
62 return trash_chunk;
63}
64
Willy Tarreau2819e992013-12-13 14:41:10 +010065/* (re)allocates the trash buffers. Returns 0 in case of failure. It is
66 * possible to call this function multiple times if the trash size changes.
67 */
Christopher Faulet748919a2017-07-26 14:59:46 +020068static int alloc_trash_buffers(int bufsize)
Willy Tarreau47ca5452012-12-23 20:22:19 +010069{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020070 chunk_init(&trash, my_realloc2(trash.area, bufsize), bufsize);
Willy Tarreaue1861612019-06-07 15:33:06 +020071 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 Tarreau843b7cb2018-07-13 10:54:26 +020074 return trash.area && trash_buf1 && trash_buf2;
Willy Tarreau47ca5452012-12-23 20:22:19 +010075}
76
Willy Tarreau082b6282019-05-22 14:42:12 +020077static int alloc_trash_buffers_per_thread()
Christopher Fauletcd7879a2017-10-27 13:53:47 +020078{
79 return alloc_trash_buffers(global.tune.bufsize);
80}
81
Willy Tarreau082b6282019-05-22 14:42:12 +020082static void free_trash_buffers_per_thread()
Christopher Fauletcd7879a2017-10-27 13:53:47 +020083{
84 chunk_destroy(&trash);
85 free(trash_buf2);
86 free(trash_buf1);
87 trash_buf2 = NULL;
88 trash_buf1 = NULL;
89}
90
Christopher Faulet748919a2017-07-26 14:59:46 +020091/* Initialize the trash buffers. It returns 0 if an error occurred. */
Christopher Fauletcd7879a2017-10-27 13:53:47 +020092int init_trash_buffers(int first)
Christopher Faulet748919a2017-07-26 14:59:46 +020093{
Willy Tarreaubafbe012017-11-24 17:34:44 +010094 pool_destroy(pool_head_trash);
Willy Tarreau83061a82018-07-13 11:56:34 +020095 pool_head_trash = create_pool("trash",
96 sizeof(struct buffer) + global.tune.bufsize,
97 MEM_F_EXACT);
Willy Tarreaubafbe012017-11-24 17:34:44 +010098 if (!pool_head_trash || !alloc_trash_buffers(global.tune.bufsize))
Christopher Faulet748919a2017-07-26 14:59:46 +020099 return 0;
100 return 1;
101}
102
Willy Tarreauc7e42382012-08-24 19:22:53 +0200103/*
Willy Tarreaub686afd2017-02-08 11:06:11 +0100104 * Allocate a trash chunk from the reentrant pool. The buffer starts at the
105 * end of the chunk. This chunk must be freed using free_trash_chunk(). This
106 * call may fail and the caller is responsible for checking that the returned
107 * pointer is not NULL.
108 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200109struct buffer *alloc_trash_chunk(void)
Willy Tarreaub686afd2017-02-08 11:06:11 +0100110{
Willy Tarreau83061a82018-07-13 11:56:34 +0200111 struct buffer *chunk;
Willy Tarreaub686afd2017-02-08 11:06:11 +0100112
Willy Tarreaubafbe012017-11-24 17:34:44 +0100113 chunk = pool_alloc(pool_head_trash);
Willy Tarreaub686afd2017-02-08 11:06:11 +0100114 if (chunk) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200115 char *buf = (char *)chunk + sizeof(struct buffer);
Willy Tarreaub686afd2017-02-08 11:06:11 +0100116 *buf = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200117 chunk_init(chunk, buf,
118 pool_head_trash->size - sizeof(struct buffer));
Willy Tarreaub686afd2017-02-08 11:06:11 +0100119 }
120 return chunk;
121}
122
123/*
Willy Tarreau77804732012-10-29 16:14:26 +0100124 * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of
125 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
126 * the new chunk size, or < 0 in case of failure.
127 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200128int chunk_printf(struct buffer *chk, const char *fmt, ...)
Willy Tarreau77804732012-10-29 16:14:26 +0100129{
130 va_list argp;
131 int ret;
132
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200133 if (!chk->area || !chk->size)
Willy Tarreau77804732012-10-29 16:14:26 +0100134 return 0;
135
136 va_start(argp, fmt);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200137 ret = vsnprintf(chk->area, chk->size, fmt, argp);
Willy Tarreau77804732012-10-29 16:14:26 +0100138 va_end(argp);
Willy Tarreau77804732012-10-29 16:14:26 +0100139
140 if (ret >= chk->size)
Willy Tarreau5f6333c2018-08-22 05:14:37 +0200141 return -1;
Willy Tarreau77804732012-10-29 16:14:26 +0100142
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200143 chk->data = ret;
144 return chk->data;
Willy Tarreau77804732012-10-29 16:14:26 +0100145}
146
147/*
Willy Tarreauc7e42382012-08-24 19:22:53 +0200148 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
149 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
150 * the new chunk size.
151 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200152int chunk_appendf(struct buffer *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200153{
154 va_list argp;
Dragan Dosen92565202023-07-27 20:30:42 +0200155 size_t room;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200156 int ret;
157
Willy Tarreaubba81562018-08-22 04:59:48 +0200158 if (!chk->area || !chk->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200159 return 0;
160
Dragan Dosen92565202023-07-27 20:30:42 +0200161 room = chk->size - chk->data;
162 if (!room)
163 return chk->data;
164
Willy Tarreauc7e42382012-08-24 19:22:53 +0200165 va_start(argp, fmt);
Dragan Dosen92565202023-07-27 20:30:42 +0200166 ret = vsnprintf(chk->area + chk->data, room, fmt, argp);
167 if (ret >= room)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200168 /* do not copy anything in case of truncation */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200169 chk->area[chk->data] = 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200170 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200171 chk->data += ret;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200172 va_end(argp);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200173 return chk->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200174}
175
176/*
177 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500178 * chk->size chars. Replace non-printable or special characters with "&#%d;".
Willy Tarreauc7e42382012-08-24 19:22:53 +0200179 * If the chk->len is over, nothing is added. Returns the new chunk size.
180 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200181int chunk_htmlencode(struct buffer *dst, struct buffer *src)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200182{
183 int i, l;
184 int olen, free;
185 char c;
186
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200187 olen = dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200188
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200189 for (i = 0; i < src->data; i++) {
190 free = dst->size - dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200191
192 if (!free) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200193 dst->data = olen;
194 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200195 }
196
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200197 c = src->area[i];
Willy Tarreauc7e42382012-08-24 19:22:53 +0200198
Willy Tarreau90807112020-02-25 08:16:33 +0100199 if (!isascii((unsigned char)c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200200 l = snprintf(dst->area + dst->data, free, "&#%u;",
201 (unsigned char)c);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200202
203 if (free < l) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200204 dst->data = olen;
205 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200206 }
207
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200208 dst->data += l;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200209 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200210 dst->area[dst->data] = c;
211 dst->data++;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200212 }
213 }
214
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200215 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200216}
217
218/*
219 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
220 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
221 * If the chk->len is over, nothing is added. Returns the new chunk size.
222 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200223int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200224{
225 int i, l;
226 int olen, free;
227 char c;
228
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200229 olen = dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200230
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200231 for (i = 0; i < src->data; i++) {
232 free = dst->size - dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200233
234 if (!free) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200235 dst->data = olen;
236 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200237 }
238
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200239 c = src->area[i];
Willy Tarreauc7e42382012-08-24 19:22:53 +0200240
Willy Tarreau90807112020-02-25 08:16:33 +0100241 if (!isascii((unsigned char)c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200242 l = snprintf(dst->area + dst->data, free, "<%02X>",
243 (unsigned char)c);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200244
245 if (free < l) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200246 dst->data = olen;
247 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200248 }
249
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200250 dst->data += l;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200251 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200252 dst->area[dst->data] = c;
253 dst->data++;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200254 }
255 }
256
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200257 return dst->data;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200258}
259
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200260/* Compares the string in chunk <chk> with the string in <str> which must be
261 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
262 * to be null.
263 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200264int chunk_strcmp(const struct buffer *chk, const char *str)
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200265{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200266 const char *s1 = chk->area;
267 int len = chk->data;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200268 int diff = 0;
269
270 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200271 if (--len < 0) {
272 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200273 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200274 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200275 diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
276 } while (!diff);
277 return diff;
278}
279
280/* Case-insensitively compares the string in chunk <chk> with the string in
281 * <str> which must be zero-terminated. Return is the same as with strcmp().
282 * Neither is allowed to be null.
283 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200284int chunk_strcasecmp(const struct buffer *chk, const char *str)
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200285{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200286 const char *s1 = chk->area;
287 int len = chk->data;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200288 int diff = 0;
289
290 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200291 if (--len < 0) {
292 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200293 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200294 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200295 diff = (unsigned char)*s1 - (unsigned char)*str;
296 if (unlikely(diff)) {
297 unsigned int l = (unsigned char)*s1;
298 unsigned int r = (unsigned char)*str;
299
300 l -= 'a';
301 r -= 'a';
302
303 if (likely(l <= (unsigned char)'z' - 'a'))
304 l -= 'a' - 'A';
305 if (likely(r <= (unsigned char)'z' - 'a'))
306 r -= 'a' - 'A';
307 diff = l - r;
308 }
309 s1++; str++;
310 } while (!diff);
311 return diff;
312}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200313
Willy Tarreau082b6282019-05-22 14:42:12 +0200314REGISTER_PER_THREAD_ALLOC(alloc_trash_buffers_per_thread);
315REGISTER_PER_THREAD_FREE(free_trash_buffers_per_thread);
Willy Tarreau172f5ce2018-11-26 11:21:50 +0100316
Willy Tarreauc7e42382012-08-24 19:22:53 +0200317/*
318 * Local variables:
319 * c-indent-level: 8
320 * c-basic-offset: 8
321 * End:
322 */