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 */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 25 | static THREAD_LOCAL struct buffer *trash_chunk; |
| 26 | static THREAD_LOCAL struct buffer trash_chunk1; |
| 27 | static THREAD_LOCAL struct buffer 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()... */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 38 | THREAD_LOCAL struct buffer trash = { }; |
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 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 49 | struct buffer *get_trash_chunk(void) |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 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 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 71 | chunk_init(&trash, my_realloc2(trash.area, 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); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 75 | return trash.area && 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); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 100 | pool_head_trash = create_pool("trash", |
| 101 | sizeof(struct buffer) + global.tune.bufsize, |
| 102 | MEM_F_EXACT); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 103 | if (!pool_head_trash || !alloc_trash_buffers(global.tune.bufsize)) |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 104 | return 0; |
| 105 | return 1; |
| 106 | } |
| 107 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 108 | /* |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 109 | * free the trash buffers |
| 110 | */ |
Christopher Faulet | 748919a | 2017-07-26 14:59:46 +0200 | [diff] [blame] | 111 | void deinit_trash_buffers(void) |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 112 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 113 | pool_destroy(pool_head_trash); |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 117 | * 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 Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 122 | struct buffer *alloc_trash_chunk(void) |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 123 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 124 | struct buffer *chunk; |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 125 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 126 | chunk = pool_alloc(pool_head_trash); |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 127 | if (chunk) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 128 | char *buf = (char *)chunk + sizeof(struct buffer); |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 129 | *buf = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 130 | chunk_init(chunk, buf, |
| 131 | pool_head_trash->size - sizeof(struct buffer)); |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 132 | } |
| 133 | return chunk; |
| 134 | } |
| 135 | |
| 136 | /* |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 137 | * 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 Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 141 | int chunk_printf(struct buffer *chk, const char *fmt, ...) |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 142 | { |
| 143 | va_list argp; |
| 144 | int ret; |
| 145 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 146 | if (!chk->area || !chk->size) |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 147 | return 0; |
| 148 | |
| 149 | va_start(argp, fmt); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 150 | ret = vsnprintf(chk->area, chk->size, fmt, argp); |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 151 | va_end(argp); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 152 | chk->data = ret; |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 153 | |
| 154 | if (ret >= chk->size) |
| 155 | ret = -1; |
| 156 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 157 | chk->data = ret; |
| 158 | return chk->data; |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 162 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
| 163 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 164 | * the new chunk size. |
| 165 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 166 | int chunk_appendf(struct buffer *chk, const char *fmt, ...) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 167 | { |
| 168 | va_list argp; |
| 169 | int ret; |
| 170 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 171 | if (chk->data < 0 || !chk->area || !chk->size) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 172 | return 0; |
| 173 | |
| 174 | va_start(argp, fmt); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 175 | ret = vsnprintf(chk->area + chk->data, chk->size - chk->data, fmt, |
| 176 | argp); |
| 177 | if (ret >= chk->size - chk->data) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 178 | /* do not copy anything in case of truncation */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 179 | chk->area[chk->data] = 0; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 180 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 181 | chk->data += ret; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 182 | va_end(argp); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 183 | return chk->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 188 | * chk->size chars. Replace non-printable or special chracters with "&#%d;". |
| 189 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 190 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 191 | int chunk_htmlencode(struct buffer *dst, struct buffer *src) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 192 | { |
| 193 | int i, l; |
| 194 | int olen, free; |
| 195 | char c; |
| 196 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 197 | if (dst->data < 0) |
| 198 | return dst->data; |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 199 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 200 | olen = dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 201 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 202 | for (i = 0; i < src->data; i++) { |
| 203 | free = dst->size - dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 204 | |
| 205 | if (!free) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 206 | dst->data = olen; |
| 207 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 210 | c = src->area[i]; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 211 | |
| 212 | if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 213 | l = snprintf(dst->area + dst->data, free, "&#%u;", |
| 214 | (unsigned char)c); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 215 | |
| 216 | if (free < l) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 217 | dst->data = olen; |
| 218 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 221 | dst->data += l; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 222 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 223 | dst->area[dst->data] = c; |
| 224 | dst->data++; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 228 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 233 | * chk->size chars. Replace non-printable or char passed in qc with "<%02X>". |
| 234 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 235 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 236 | int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 237 | { |
| 238 | int i, l; |
| 239 | int olen, free; |
| 240 | char c; |
| 241 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 242 | if (dst->data < 0) |
| 243 | return dst->data; |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 245 | olen = dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 246 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 247 | for (i = 0; i < src->data; i++) { |
| 248 | free = dst->size - dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 249 | |
| 250 | if (!free) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 251 | dst->data = olen; |
| 252 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 255 | c = src->area[i]; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 256 | |
| 257 | if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 258 | l = snprintf(dst->area + dst->data, free, "<%02X>", |
| 259 | (unsigned char)c); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 260 | |
| 261 | if (free < l) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 262 | dst->data = olen; |
| 263 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 266 | dst->data += l; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 267 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 268 | dst->area[dst->data] = c; |
| 269 | dst->data++; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 273 | return dst->data; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 276 | /* Compares the string in chunk <chk> with the string in <str> which must be |
| 277 | * zero-terminated. Return is the same as with strcmp(). Neither is allowed |
| 278 | * to be null. |
| 279 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 280 | int chunk_strcmp(const struct buffer *chk, const char *str) |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 281 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 282 | const char *s1 = chk->area; |
| 283 | int len = chk->data; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 284 | int diff = 0; |
| 285 | |
| 286 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 287 | if (--len < 0) { |
| 288 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 289 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 290 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 291 | diff = (unsigned char)*(s1++) - (unsigned char)*(str++); |
| 292 | } while (!diff); |
| 293 | return diff; |
| 294 | } |
| 295 | |
| 296 | /* Case-insensitively compares the string in chunk <chk> with the string in |
| 297 | * <str> which must be zero-terminated. Return is the same as with strcmp(). |
| 298 | * Neither is allowed to be null. |
| 299 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 300 | int chunk_strcasecmp(const struct buffer *chk, const char *str) |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 301 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 302 | const char *s1 = chk->area; |
| 303 | int len = chk->data; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 304 | int diff = 0; |
| 305 | |
| 306 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 307 | if (--len < 0) { |
| 308 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 309 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 310 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 311 | diff = (unsigned char)*s1 - (unsigned char)*str; |
| 312 | if (unlikely(diff)) { |
| 313 | unsigned int l = (unsigned char)*s1; |
| 314 | unsigned int r = (unsigned char)*str; |
| 315 | |
| 316 | l -= 'a'; |
| 317 | r -= 'a'; |
| 318 | |
| 319 | if (likely(l <= (unsigned char)'z' - 'a')) |
| 320 | l -= 'a' - 'A'; |
| 321 | if (likely(r <= (unsigned char)'z' - 'a')) |
| 322 | r -= 'a' - 'A'; |
| 323 | diff = l - r; |
| 324 | } |
| 325 | s1++; str++; |
| 326 | } while (!diff); |
| 327 | return diff; |
| 328 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Local variables: |
| 332 | * c-indent-level: 8 |
| 333 | * c-basic-offset: 8 |
| 334 | * End: |
| 335 | */ |