Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 20 | #include <common/standard.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 21 | |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 22 | #include <types/global.h> |
| 23 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 24 | /* trash chunks used for various conversions */ |
Christopher Faulet | 6adad11 | 2017-04-21 16:47:03 +0200 | [diff] [blame] | 25 | static THREAD_LOCAL struct chunk *trash_chunk; |
| 26 | static THREAD_LOCAL struct chunk trash_chunk1; |
| 27 | static THREAD_LOCAL struct chunk trash_chunk2; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 28 | |
| 29 | /* trash buffers used for various conversions */ |
| 30 | static int trash_size; |
Christopher Faulet | 6adad11 | 2017-04-21 16:47:03 +0200 | [diff] [blame] | 31 | static THREAD_LOCAL char *trash_buf1; |
| 32 | static THREAD_LOCAL char *trash_buf2; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 33 | |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 34 | /* the trash pool for reentrant allocations */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 35 | struct pool_head *pool_head_trash = NULL; |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 36 | |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 37 | /* this is used to drain data, and as a temporary buffer for sprintf()... */ |
Christopher Faulet | 6adad11 | 2017-04-21 16:47:03 +0200 | [diff] [blame] | 38 | THREAD_LOCAL struct chunk trash = { .str = NULL }; |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 40 | /* |
| 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 Tarreau | 031ad23 | 2013-12-11 17:32:08 +0100 | [diff] [blame] | 45 | * 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 Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 48 | */ |
| 49 | struct 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 Tarreau | 031ad23 | 2013-12-11 17:32:08 +0100 | [diff] [blame] | 61 | *trash_buf = 0; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 62 | chunk_init(trash_chunk, trash_buf, trash_size); |
| 63 | return trash_chunk; |
| 64 | } |
| 65 | |
Willy Tarreau | 2819e99 | 2013-12-13 14:41:10 +0100 | [diff] [blame] | 66 | /* (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 Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 69 | static int alloc_trash_buffers(int bufsize) |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 70 | { |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 71 | chunk_init(&trash, my_realloc2(trash.str, bufsize), bufsize); |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 72 | trash_size = bufsize; |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 73 | trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize); |
| 74 | trash_buf2 = (char *)my_realloc2(trash_buf2, bufsize); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 75 | return trash.str && trash_buf1 && trash_buf2; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 78 | static int init_trash_buffers_per_thread() |
| 79 | { |
| 80 | return alloc_trash_buffers(global.tune.bufsize); |
| 81 | } |
| 82 | |
| 83 | static 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 Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 92 | /* Initialize the trash buffers. It returns 0 if an error occurred. */ |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 93 | int init_trash_buffers(int first) |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 94 | { |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 95 | 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 Faulet | 6adad11 | 2017-04-21 16:47:03 +0200 | [diff] [blame] | 98 | } |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 99 | pool_destroy(pool_head_trash); |
| 100 | pool_head_trash = create_pool("trash", sizeof(struct chunk) + global.tune.bufsize, MEM_F_EXACT); |
| 101 | if (!pool_head_trash || !alloc_trash_buffers(global.tune.bufsize)) |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | return 1; |
| 104 | } |
| 105 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 106 | /* |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 107 | * free the trash buffers |
| 108 | */ |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 109 | void deinit_trash_buffers(void) |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 110 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 111 | pool_destroy(pool_head_trash); |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 115 | * Allocate a trash chunk from the reentrant pool. The buffer starts at the |
| 116 | * end of the chunk. This chunk must be freed using free_trash_chunk(). This |
| 117 | * call may fail and the caller is responsible for checking that the returned |
| 118 | * pointer is not NULL. |
| 119 | */ |
| 120 | struct chunk *alloc_trash_chunk(void) |
| 121 | { |
| 122 | struct chunk *chunk; |
| 123 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 124 | chunk = pool_alloc(pool_head_trash); |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 125 | if (chunk) { |
| 126 | char *buf = (char *)chunk + sizeof(struct chunk); |
| 127 | *buf = 0; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 128 | chunk_init(chunk, buf, pool_head_trash->size - sizeof(struct chunk)); |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 129 | } |
| 130 | return chunk; |
| 131 | } |
| 132 | |
| 133 | /* |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 134 | * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of |
| 135 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 136 | * the new chunk size, or < 0 in case of failure. |
| 137 | */ |
| 138 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
| 139 | { |
| 140 | va_list argp; |
| 141 | int ret; |
| 142 | |
| 143 | if (!chk->str || !chk->size) |
| 144 | return 0; |
| 145 | |
| 146 | va_start(argp, fmt); |
| 147 | ret = vsnprintf(chk->str, chk->size, fmt, argp); |
| 148 | va_end(argp); |
| 149 | chk->len = ret; |
| 150 | |
| 151 | if (ret >= chk->size) |
| 152 | ret = -1; |
| 153 | |
| 154 | chk->len = ret; |
| 155 | return chk->len; |
| 156 | } |
| 157 | |
| 158 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 159 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
| 160 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 161 | * the new chunk size. |
| 162 | */ |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 163 | int chunk_appendf(struct chunk *chk, const char *fmt, ...) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 164 | { |
| 165 | va_list argp; |
| 166 | int ret; |
| 167 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 168 | if (chk->len < 0 || !chk->str || !chk->size) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 169 | return 0; |
| 170 | |
| 171 | va_start(argp, fmt); |
| 172 | ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp); |
| 173 | if (ret >= chk->size - chk->len) |
| 174 | /* do not copy anything in case of truncation */ |
| 175 | chk->str[chk->len] = 0; |
| 176 | else |
| 177 | chk->len += ret; |
| 178 | va_end(argp); |
| 179 | return chk->len; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 184 | * chk->size chars. Replace non-printable or special chracters with "&#%d;". |
| 185 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 186 | */ |
| 187 | int chunk_htmlencode(struct chunk *dst, struct chunk *src) |
| 188 | { |
| 189 | int i, l; |
| 190 | int olen, free; |
| 191 | char c; |
| 192 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 193 | if (dst->len < 0) |
| 194 | return dst->len; |
| 195 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 196 | olen = dst->len; |
| 197 | |
| 198 | for (i = 0; i < src->len; i++) { |
| 199 | free = dst->size - dst->len; |
| 200 | |
| 201 | if (!free) { |
| 202 | dst->len = olen; |
| 203 | return dst->len; |
| 204 | } |
| 205 | |
| 206 | c = src->str[i]; |
| 207 | |
| 208 | if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') { |
| 209 | l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c); |
| 210 | |
| 211 | if (free < l) { |
| 212 | dst->len = olen; |
| 213 | return dst->len; |
| 214 | } |
| 215 | |
| 216 | dst->len += l; |
| 217 | } else { |
| 218 | dst->str[dst->len] = c; |
| 219 | dst->len++; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return dst->len; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 228 | * chk->size chars. Replace non-printable or char passed in qc with "<%02X>". |
| 229 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 230 | */ |
| 231 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) |
| 232 | { |
| 233 | int i, l; |
| 234 | int olen, free; |
| 235 | char c; |
| 236 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 237 | if (dst->len < 0) |
| 238 | return dst->len; |
| 239 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 240 | olen = dst->len; |
| 241 | |
| 242 | for (i = 0; i < src->len; i++) { |
| 243 | free = dst->size - dst->len; |
| 244 | |
| 245 | if (!free) { |
| 246 | dst->len = olen; |
| 247 | return dst->len; |
| 248 | } |
| 249 | |
| 250 | c = src->str[i]; |
| 251 | |
| 252 | if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) { |
| 253 | l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c); |
| 254 | |
| 255 | if (free < l) { |
| 256 | dst->len = olen; |
| 257 | return dst->len; |
| 258 | } |
| 259 | |
| 260 | dst->len += l; |
| 261 | } else { |
| 262 | dst->str[dst->len] = c; |
| 263 | dst->len++; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return dst->len; |
| 268 | } |
| 269 | |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 270 | /* Compares the string in chunk <chk> with the string in <str> which must be |
| 271 | * zero-terminated. Return is the same as with strcmp(). Neither is allowed |
| 272 | * to be null. |
| 273 | */ |
| 274 | int chunk_strcmp(const struct chunk *chk, const char *str) |
| 275 | { |
| 276 | const char *s1 = chk->str; |
| 277 | int len = chk->len; |
| 278 | int diff = 0; |
| 279 | |
| 280 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 281 | if (--len < 0) { |
| 282 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 283 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 284 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 285 | diff = (unsigned char)*(s1++) - (unsigned char)*(str++); |
| 286 | } while (!diff); |
| 287 | return diff; |
| 288 | } |
| 289 | |
| 290 | /* Case-insensitively compares the string in chunk <chk> with the string in |
| 291 | * <str> which must be zero-terminated. Return is the same as with strcmp(). |
| 292 | * Neither is allowed to be null. |
| 293 | */ |
| 294 | int chunk_strcasecmp(const struct chunk *chk, const char *str) |
| 295 | { |
| 296 | const char *s1 = chk->str; |
| 297 | int len = chk->len; |
| 298 | int diff = 0; |
| 299 | |
| 300 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 301 | if (--len < 0) { |
| 302 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 303 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 304 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 305 | diff = (unsigned char)*s1 - (unsigned char)*str; |
| 306 | if (unlikely(diff)) { |
| 307 | unsigned int l = (unsigned char)*s1; |
| 308 | unsigned int r = (unsigned char)*str; |
| 309 | |
| 310 | l -= 'a'; |
| 311 | r -= 'a'; |
| 312 | |
| 313 | if (likely(l <= (unsigned char)'z' - 'a')) |
| 314 | l -= 'a' - 'A'; |
| 315 | if (likely(r <= (unsigned char)'z' - 'a')) |
| 316 | r -= 'a' - 'A'; |
| 317 | diff = l - r; |
| 318 | } |
| 319 | s1++; str++; |
| 320 | } while (!diff); |
| 321 | return diff; |
| 322 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * Local variables: |
| 326 | * c-indent-level: 8 |
| 327 | * c-basic-offset: 8 |
| 328 | * End: |
| 329 | */ |