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 */ |
| 35 | struct pool_head *pool2_trash = NULL; |
| 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 | { |
| 71 | trash_size = bufsize; |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 72 | trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize); |
| 73 | trash_buf2 = (char *)my_realloc2(trash_buf2, bufsize); |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 74 | pool2_trash = create_pool("trash", sizeof(struct chunk) + bufsize, MEM_F_EXACT); |
| 75 | return trash_buf1 && trash_buf2 && pool2_trash; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 78 | /* Initialize the trash buffers. It returns 0 if an error occurred. */ |
| 79 | int init_trash_buffers() |
| 80 | { |
Christopher Faulet | 6adad11 | 2017-04-21 16:47:03 +0200 | [diff] [blame] | 81 | if (global.nbthread > 1 && tid == (unsigned int)(-1)) { |
| 82 | hap_register_per_thread_init(init_trash_buffers); |
| 83 | hap_register_per_thread_deinit(deinit_trash_buffers); |
| 84 | } |
| 85 | |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 86 | chunk_init(&trash, my_realloc2(trash.str, global.tune.bufsize), global.tune.bufsize); |
| 87 | if (!trash.str || !alloc_trash_buffers(global.tune.bufsize)) |
| 88 | return 0; |
| 89 | return 1; |
| 90 | } |
| 91 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 92 | /* |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 93 | * free the trash buffers |
| 94 | */ |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 95 | void deinit_trash_buffers(void) |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 96 | { |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 97 | chunk_destroy(&trash); |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 98 | free(trash_buf2); |
| 99 | free(trash_buf1); |
| 100 | trash_buf2 = NULL; |
| 101 | trash_buf1 = NULL; |
| 102 | } |
| 103 | |
| 104 | /* |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 105 | * Allocate a trash chunk from the reentrant pool. The buffer starts at the |
| 106 | * end of the chunk. This chunk must be freed using free_trash_chunk(). This |
| 107 | * call may fail and the caller is responsible for checking that the returned |
| 108 | * pointer is not NULL. |
| 109 | */ |
| 110 | struct chunk *alloc_trash_chunk(void) |
| 111 | { |
| 112 | struct chunk *chunk; |
| 113 | |
| 114 | chunk = pool_alloc2(pool2_trash); |
| 115 | if (chunk) { |
| 116 | char *buf = (char *)chunk + sizeof(struct chunk); |
| 117 | *buf = 0; |
| 118 | chunk_init(chunk, buf, pool2_trash->size - sizeof(struct chunk)); |
| 119 | } |
| 120 | return chunk; |
| 121 | } |
| 122 | |
| 123 | /* |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 124 | * 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 | */ |
| 128 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
| 129 | { |
| 130 | va_list argp; |
| 131 | int ret; |
| 132 | |
| 133 | if (!chk->str || !chk->size) |
| 134 | return 0; |
| 135 | |
| 136 | va_start(argp, fmt); |
| 137 | ret = vsnprintf(chk->str, chk->size, fmt, argp); |
| 138 | va_end(argp); |
| 139 | chk->len = ret; |
| 140 | |
| 141 | if (ret >= chk->size) |
| 142 | ret = -1; |
| 143 | |
| 144 | chk->len = ret; |
| 145 | return chk->len; |
| 146 | } |
| 147 | |
| 148 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 149 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
| 150 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 151 | * the new chunk size. |
| 152 | */ |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 153 | int chunk_appendf(struct chunk *chk, const char *fmt, ...) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 154 | { |
| 155 | va_list argp; |
| 156 | int ret; |
| 157 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 158 | if (chk->len < 0 || !chk->str || !chk->size) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 159 | return 0; |
| 160 | |
| 161 | va_start(argp, fmt); |
| 162 | ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp); |
| 163 | if (ret >= chk->size - chk->len) |
| 164 | /* do not copy anything in case of truncation */ |
| 165 | chk->str[chk->len] = 0; |
| 166 | else |
| 167 | chk->len += ret; |
| 168 | va_end(argp); |
| 169 | return chk->len; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 174 | * chk->size chars. Replace non-printable or special chracters with "&#%d;". |
| 175 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 176 | */ |
| 177 | int chunk_htmlencode(struct chunk *dst, struct chunk *src) |
| 178 | { |
| 179 | int i, l; |
| 180 | int olen, free; |
| 181 | char c; |
| 182 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 183 | if (dst->len < 0) |
| 184 | return dst->len; |
| 185 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 186 | olen = dst->len; |
| 187 | |
| 188 | for (i = 0; i < src->len; i++) { |
| 189 | free = dst->size - dst->len; |
| 190 | |
| 191 | if (!free) { |
| 192 | dst->len = olen; |
| 193 | return dst->len; |
| 194 | } |
| 195 | |
| 196 | c = src->str[i]; |
| 197 | |
| 198 | if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') { |
| 199 | l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c); |
| 200 | |
| 201 | if (free < l) { |
| 202 | dst->len = olen; |
| 203 | return dst->len; |
| 204 | } |
| 205 | |
| 206 | dst->len += l; |
| 207 | } else { |
| 208 | dst->str[dst->len] = c; |
| 209 | dst->len++; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return dst->len; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 218 | * chk->size chars. Replace non-printable or char passed in qc with "<%02X>". |
| 219 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 220 | */ |
| 221 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) |
| 222 | { |
| 223 | int i, l; |
| 224 | int olen, free; |
| 225 | char c; |
| 226 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 227 | if (dst->len < 0) |
| 228 | return dst->len; |
| 229 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 230 | olen = dst->len; |
| 231 | |
| 232 | for (i = 0; i < src->len; i++) { |
| 233 | free = dst->size - dst->len; |
| 234 | |
| 235 | if (!free) { |
| 236 | dst->len = olen; |
| 237 | return dst->len; |
| 238 | } |
| 239 | |
| 240 | c = src->str[i]; |
| 241 | |
| 242 | if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) { |
| 243 | l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c); |
| 244 | |
| 245 | if (free < l) { |
| 246 | dst->len = olen; |
| 247 | return dst->len; |
| 248 | } |
| 249 | |
| 250 | dst->len += l; |
| 251 | } else { |
| 252 | dst->str[dst->len] = c; |
| 253 | dst->len++; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return dst->len; |
| 258 | } |
| 259 | |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 260 | /* 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 | */ |
| 264 | int chunk_strcmp(const struct chunk *chk, const char *str) |
| 265 | { |
| 266 | const char *s1 = chk->str; |
| 267 | int len = chk->len; |
| 268 | int diff = 0; |
| 269 | |
| 270 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 271 | if (--len < 0) { |
| 272 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 273 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 274 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 275 | 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 | */ |
| 284 | int chunk_strcasecmp(const struct chunk *chk, const char *str) |
| 285 | { |
| 286 | const char *s1 = chk->str; |
| 287 | int len = chk->len; |
| 288 | int diff = 0; |
| 289 | |
| 290 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 291 | if (--len < 0) { |
| 292 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 293 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 294 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 295 | 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 Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * Local variables: |
| 316 | * c-indent-level: 8 |
| 317 | * c-basic-offset: 8 |
| 318 | * End: |
| 319 | */ |