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 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 18 | #include <haproxy/api.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 19 | #include <haproxy/chunk.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 20 | #include <haproxy/global.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 21 | #include <haproxy/tools.h> |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 23 | /* trash chunks used for various conversions */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 24 | static THREAD_LOCAL struct buffer *trash_chunk; |
| 25 | static THREAD_LOCAL struct buffer trash_chunk1; |
| 26 | static THREAD_LOCAL struct buffer trash_chunk2; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 27 | |
| 28 | /* trash buffers used for various conversions */ |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 29 | static int trash_size __read_mostly; |
Christopher Faulet | 6adad11 | 2017-04-21 16:47:03 +0200 | [diff] [blame] | 30 | static THREAD_LOCAL char *trash_buf1; |
| 31 | static THREAD_LOCAL char *trash_buf2; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 32 | |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 33 | /* the trash pool for reentrant allocations */ |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 34 | struct pool_head *pool_head_trash __read_mostly = NULL; |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 35 | |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 36 | /* this is used to drain data, and as a temporary buffer for sprintf()... */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 37 | THREAD_LOCAL struct buffer trash = { }; |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 39 | /* |
| 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 Tarreau | 031ad23 | 2013-12-11 17:32:08 +0100 | [diff] [blame] | 44 | * 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 Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 47 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 48 | struct buffer *get_trash_chunk(void) |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 49 | { |
| 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 Tarreau | 031ad23 | 2013-12-11 17:32:08 +0100 | [diff] [blame] | 60 | *trash_buf = 0; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 61 | chunk_init(trash_chunk, trash_buf, trash_size); |
| 62 | return trash_chunk; |
| 63 | } |
| 64 | |
Willy Tarreau | 2819e99 | 2013-12-13 14:41:10 +0100 | [diff] [blame] | 65 | /* (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 Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 68 | static int alloc_trash_buffers(int bufsize) |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 69 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 70 | chunk_init(&trash, my_realloc2(trash.area, bufsize), bufsize); |
Willy Tarreau | e186161 | 2019-06-07 15:33:06 +0200 | [diff] [blame] | 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 | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 74 | return trash.area && trash_buf1 && trash_buf2; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 77 | static int alloc_trash_buffers_per_thread() |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 78 | { |
| 79 | return alloc_trash_buffers(global.tune.bufsize); |
| 80 | } |
| 81 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 82 | static void free_trash_buffers_per_thread() |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 83 | { |
| 84 | chunk_destroy(&trash); |
Willy Tarreau | d941146 | 2022-04-27 17:55:41 +0200 | [diff] [blame] | 85 | ha_free(&trash_buf2); |
| 86 | ha_free(&trash_buf1); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 89 | /* Initialize the trash buffers. It returns 0 if an error occurred. */ |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 90 | int init_trash_buffers(int first) |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 91 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 92 | pool_destroy(pool_head_trash); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 93 | pool_head_trash = create_pool("trash", |
| 94 | sizeof(struct buffer) + global.tune.bufsize, |
| 95 | MEM_F_EXACT); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 96 | if (!pool_head_trash || !alloc_trash_buffers(global.tune.bufsize)) |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 97 | return 0; |
| 98 | return 1; |
| 99 | } |
| 100 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 101 | /* |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 102 | * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of |
| 103 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 104 | * the new chunk size, or < 0 in case of failure. |
| 105 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 106 | int chunk_printf(struct buffer *chk, const char *fmt, ...) |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 107 | { |
| 108 | va_list argp; |
| 109 | int ret; |
| 110 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 111 | if (!chk->area || !chk->size) |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 112 | return 0; |
| 113 | |
| 114 | va_start(argp, fmt); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 115 | ret = vsnprintf(chk->area, chk->size, fmt, argp); |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 116 | va_end(argp); |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 117 | |
| 118 | if (ret >= chk->size) |
Willy Tarreau | 5f6333c | 2018-08-22 05:14:37 +0200 | [diff] [blame] | 119 | return -1; |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 120 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 121 | chk->data = ret; |
| 122 | return chk->data; |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 126 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
| 127 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 128 | * the new chunk size. |
| 129 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 130 | int chunk_appendf(struct buffer *chk, const char *fmt, ...) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 131 | { |
| 132 | va_list argp; |
Dragan Dosen | aafbc3a | 2023-07-27 20:30:42 +0200 | [diff] [blame] | 133 | size_t room; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 134 | int ret; |
| 135 | |
Willy Tarreau | bba8156 | 2018-08-22 04:59:48 +0200 | [diff] [blame] | 136 | if (!chk->area || !chk->size) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 137 | return 0; |
| 138 | |
Dragan Dosen | aafbc3a | 2023-07-27 20:30:42 +0200 | [diff] [blame] | 139 | room = chk->size - chk->data; |
| 140 | if (!room) |
| 141 | return chk->data; |
| 142 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 143 | va_start(argp, fmt); |
Dragan Dosen | aafbc3a | 2023-07-27 20:30:42 +0200 | [diff] [blame] | 144 | ret = vsnprintf(chk->area + chk->data, room, fmt, argp); |
| 145 | if (ret >= room) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 146 | /* do not copy anything in case of truncation */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 147 | chk->area[chk->data] = 0; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 148 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 149 | chk->data += ret; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 150 | va_end(argp); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 151 | return chk->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 156 | * chk->size chars. Replace non-printable or special characters with "&#%d;". |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 157 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 158 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 159 | int chunk_htmlencode(struct buffer *dst, struct buffer *src) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 160 | { |
| 161 | int i, l; |
| 162 | int olen, free; |
| 163 | char c; |
| 164 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 165 | olen = dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 166 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 167 | for (i = 0; i < src->data; i++) { |
| 168 | free = dst->size - dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 169 | |
| 170 | if (!free) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 171 | dst->data = olen; |
| 172 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 175 | c = src->area[i]; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 176 | |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 177 | if (!isascii((unsigned char)c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 178 | l = snprintf(dst->area + dst->data, free, "&#%u;", |
| 179 | (unsigned char)c); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 180 | |
| 181 | if (free < l) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 182 | dst->data = olen; |
| 183 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 186 | dst->data += l; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 187 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 188 | dst->area[dst->data] = c; |
| 189 | dst->data++; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 193 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 198 | * chk->size chars. Replace non-printable or char passed in qc with "<%02X>". |
| 199 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 200 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 201 | int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 202 | { |
| 203 | int i, l; |
| 204 | int olen, free; |
| 205 | char c; |
| 206 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 207 | olen = dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 209 | for (i = 0; i < src->data; i++) { |
| 210 | free = dst->size - dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 211 | |
| 212 | if (!free) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 213 | dst->data = olen; |
| 214 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 217 | c = src->area[i]; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 218 | |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 219 | if (!isascii((unsigned char)c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 220 | l = snprintf(dst->area + dst->data, free, "<%02X>", |
| 221 | (unsigned char)c); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 222 | |
| 223 | if (free < l) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 224 | dst->data = olen; |
| 225 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 228 | dst->data += l; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 229 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 230 | dst->area[dst->data] = c; |
| 231 | dst->data++; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 235 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 238 | /* Compares the string in chunk <chk> with the string in <str> which must be |
| 239 | * zero-terminated. Return is the same as with strcmp(). Neither is allowed |
| 240 | * to be null. |
| 241 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 242 | int chunk_strcmp(const struct buffer *chk, const char *str) |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 243 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 244 | const char *s1 = chk->area; |
| 245 | int len = chk->data; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 246 | int diff = 0; |
| 247 | |
| 248 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 249 | if (--len < 0) { |
| 250 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 251 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 252 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 253 | diff = (unsigned char)*(s1++) - (unsigned char)*(str++); |
| 254 | } while (!diff); |
| 255 | return diff; |
| 256 | } |
| 257 | |
| 258 | /* Case-insensitively compares the string in chunk <chk> with the string in |
| 259 | * <str> which must be zero-terminated. Return is the same as with strcmp(). |
| 260 | * Neither is allowed to be null. |
| 261 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 262 | int chunk_strcasecmp(const struct buffer *chk, const char *str) |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 263 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 264 | const char *s1 = chk->area; |
| 265 | int len = chk->data; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 266 | int diff = 0; |
| 267 | |
| 268 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 269 | if (--len < 0) { |
| 270 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 271 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 272 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 273 | diff = (unsigned char)*s1 - (unsigned char)*str; |
| 274 | if (unlikely(diff)) { |
| 275 | unsigned int l = (unsigned char)*s1; |
| 276 | unsigned int r = (unsigned char)*str; |
| 277 | |
| 278 | l -= 'a'; |
| 279 | r -= 'a'; |
| 280 | |
| 281 | if (likely(l <= (unsigned char)'z' - 'a')) |
| 282 | l -= 'a' - 'A'; |
| 283 | if (likely(r <= (unsigned char)'z' - 'a')) |
| 284 | r -= 'a' - 'A'; |
| 285 | diff = l - r; |
| 286 | } |
| 287 | s1++; str++; |
| 288 | } while (!diff); |
| 289 | return diff; |
| 290 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 291 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 292 | REGISTER_PER_THREAD_ALLOC(alloc_trash_buffers_per_thread); |
| 293 | REGISTER_PER_THREAD_FREE(free_trash_buffers_per_thread); |
Willy Tarreau | d941146 | 2022-04-27 17:55:41 +0200 | [diff] [blame] | 294 | REGISTER_POST_DEINIT(free_trash_buffers_per_thread); |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 295 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 296 | /* |
| 297 | * Local variables: |
| 298 | * c-indent-level: 8 |
| 299 | * c-basic-offset: 8 |
| 300 | * End: |
| 301 | */ |