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 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 22 | /* trash chunks used for various conversions */ |
| 23 | static struct chunk *trash_chunk; |
| 24 | static struct chunk trash_chunk1; |
| 25 | static struct chunk trash_chunk2; |
| 26 | |
| 27 | /* trash buffers used for various conversions */ |
| 28 | static int trash_size; |
| 29 | static char *trash_buf1; |
| 30 | static char *trash_buf2; |
| 31 | |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 32 | /* the trash pool for reentrant allocations */ |
| 33 | struct pool_head *pool2_trash = NULL; |
| 34 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 35 | /* |
| 36 | * Returns a pre-allocated and initialized trash chunk that can be used for any |
| 37 | * type of conversion. Two chunks and their respective buffers are alternatively |
| 38 | * returned so that it is always possible to iterate data transformations without |
| 39 | * 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] | 40 | * a standard buffer, so they should be enough for everything. For convenience, |
| 41 | * a zero is always emitted at the beginning of the string so that it may be |
| 42 | * used as an empty string as well. |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 43 | */ |
| 44 | struct chunk *get_trash_chunk(void) |
| 45 | { |
| 46 | char *trash_buf; |
| 47 | |
| 48 | if (trash_chunk == &trash_chunk1) { |
| 49 | trash_chunk = &trash_chunk2; |
| 50 | trash_buf = trash_buf2; |
| 51 | } |
| 52 | else { |
| 53 | trash_chunk = &trash_chunk1; |
| 54 | trash_buf = trash_buf1; |
| 55 | } |
Willy Tarreau | 031ad23 | 2013-12-11 17:32:08 +0100 | [diff] [blame] | 56 | *trash_buf = 0; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 57 | chunk_init(trash_chunk, trash_buf, trash_size); |
| 58 | return trash_chunk; |
| 59 | } |
| 60 | |
Willy Tarreau | 2819e99 | 2013-12-13 14:41:10 +0100 | [diff] [blame] | 61 | /* (re)allocates the trash buffers. Returns 0 in case of failure. It is |
| 62 | * possible to call this function multiple times if the trash size changes. |
| 63 | */ |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 64 | int alloc_trash_buffers(int bufsize) |
| 65 | { |
| 66 | trash_size = bufsize; |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 67 | trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize); |
| 68 | trash_buf2 = (char *)my_realloc2(trash_buf2, bufsize); |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 69 | pool2_trash = create_pool("trash", sizeof(struct chunk) + bufsize, MEM_F_EXACT); |
| 70 | return trash_buf1 && trash_buf2 && pool2_trash; |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 73 | /* |
David Carlier | 60deeba | 2015-09-25 11:58:12 +0100 | [diff] [blame] | 74 | * free the trash buffers |
| 75 | */ |
| 76 | void free_trash_buffers(void) |
| 77 | { |
| 78 | free(trash_buf2); |
| 79 | free(trash_buf1); |
| 80 | trash_buf2 = NULL; |
| 81 | trash_buf1 = NULL; |
| 82 | } |
| 83 | |
| 84 | /* |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 85 | * Allocate a trash chunk from the reentrant pool. The buffer starts at the |
| 86 | * end of the chunk. This chunk must be freed using free_trash_chunk(). This |
| 87 | * call may fail and the caller is responsible for checking that the returned |
| 88 | * pointer is not NULL. |
| 89 | */ |
| 90 | struct chunk *alloc_trash_chunk(void) |
| 91 | { |
| 92 | struct chunk *chunk; |
| 93 | |
| 94 | chunk = pool_alloc2(pool2_trash); |
| 95 | if (chunk) { |
| 96 | char *buf = (char *)chunk + sizeof(struct chunk); |
| 97 | *buf = 0; |
| 98 | chunk_init(chunk, buf, pool2_trash->size - sizeof(struct chunk)); |
| 99 | } |
| 100 | return chunk; |
| 101 | } |
| 102 | |
| 103 | /* |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 104 | * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of |
| 105 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 106 | * the new chunk size, or < 0 in case of failure. |
| 107 | */ |
| 108 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
| 109 | { |
| 110 | va_list argp; |
| 111 | int ret; |
| 112 | |
| 113 | if (!chk->str || !chk->size) |
| 114 | return 0; |
| 115 | |
| 116 | va_start(argp, fmt); |
| 117 | ret = vsnprintf(chk->str, chk->size, fmt, argp); |
| 118 | va_end(argp); |
| 119 | chk->len = ret; |
| 120 | |
| 121 | if (ret >= chk->size) |
| 122 | ret = -1; |
| 123 | |
| 124 | chk->len = ret; |
| 125 | return chk->len; |
| 126 | } |
| 127 | |
| 128 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 129 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
| 130 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 131 | * the new chunk size. |
| 132 | */ |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 133 | int chunk_appendf(struct chunk *chk, const char *fmt, ...) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 134 | { |
| 135 | va_list argp; |
| 136 | int ret; |
| 137 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 138 | if (chk->len < 0 || !chk->str || !chk->size) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 139 | return 0; |
| 140 | |
| 141 | va_start(argp, fmt); |
| 142 | ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp); |
| 143 | if (ret >= chk->size - chk->len) |
| 144 | /* do not copy anything in case of truncation */ |
| 145 | chk->str[chk->len] = 0; |
| 146 | else |
| 147 | chk->len += ret; |
| 148 | va_end(argp); |
| 149 | return chk->len; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 154 | * chk->size chars. Replace non-printable or special chracters with "&#%d;". |
| 155 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 156 | */ |
| 157 | int chunk_htmlencode(struct chunk *dst, struct chunk *src) |
| 158 | { |
| 159 | int i, l; |
| 160 | int olen, free; |
| 161 | char c; |
| 162 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 163 | if (dst->len < 0) |
| 164 | return dst->len; |
| 165 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 166 | olen = dst->len; |
| 167 | |
| 168 | for (i = 0; i < src->len; i++) { |
| 169 | free = dst->size - dst->len; |
| 170 | |
| 171 | if (!free) { |
| 172 | dst->len = olen; |
| 173 | return dst->len; |
| 174 | } |
| 175 | |
| 176 | c = src->str[i]; |
| 177 | |
| 178 | if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') { |
| 179 | l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c); |
| 180 | |
| 181 | if (free < l) { |
| 182 | dst->len = olen; |
| 183 | return dst->len; |
| 184 | } |
| 185 | |
| 186 | dst->len += l; |
| 187 | } else { |
| 188 | dst->str[dst->len] = c; |
| 189 | dst->len++; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return dst->len; |
| 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 | */ |
| 201 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) |
| 202 | { |
| 203 | int i, l; |
| 204 | int olen, free; |
| 205 | char c; |
| 206 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 207 | if (dst->len < 0) |
| 208 | return dst->len; |
| 209 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 210 | olen = dst->len; |
| 211 | |
| 212 | for (i = 0; i < src->len; i++) { |
| 213 | free = dst->size - dst->len; |
| 214 | |
| 215 | if (!free) { |
| 216 | dst->len = olen; |
| 217 | return dst->len; |
| 218 | } |
| 219 | |
| 220 | c = src->str[i]; |
| 221 | |
| 222 | if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) { |
| 223 | l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c); |
| 224 | |
| 225 | if (free < l) { |
| 226 | dst->len = olen; |
| 227 | return dst->len; |
| 228 | } |
| 229 | |
| 230 | dst->len += l; |
| 231 | } else { |
| 232 | dst->str[dst->len] = c; |
| 233 | dst->len++; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return dst->len; |
| 238 | } |
| 239 | |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 240 | /* Compares the string in chunk <chk> with the string in <str> which must be |
| 241 | * zero-terminated. Return is the same as with strcmp(). Neither is allowed |
| 242 | * to be null. |
| 243 | */ |
| 244 | int chunk_strcmp(const struct chunk *chk, const char *str) |
| 245 | { |
| 246 | const char *s1 = chk->str; |
| 247 | int len = chk->len; |
| 248 | int diff = 0; |
| 249 | |
| 250 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 251 | if (--len < 0) { |
| 252 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 253 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 254 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 255 | diff = (unsigned char)*(s1++) - (unsigned char)*(str++); |
| 256 | } while (!diff); |
| 257 | return diff; |
| 258 | } |
| 259 | |
| 260 | /* Case-insensitively compares the string in chunk <chk> with the string in |
| 261 | * <str> which must be zero-terminated. Return is the same as with strcmp(). |
| 262 | * Neither is allowed to be null. |
| 263 | */ |
| 264 | int chunk_strcasecmp(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 | if (unlikely(diff)) { |
| 277 | unsigned int l = (unsigned char)*s1; |
| 278 | unsigned int r = (unsigned char)*str; |
| 279 | |
| 280 | l -= 'a'; |
| 281 | r -= 'a'; |
| 282 | |
| 283 | if (likely(l <= (unsigned char)'z' - 'a')) |
| 284 | l -= 'a' - 'A'; |
| 285 | if (likely(r <= (unsigned char)'z' - 'a')) |
| 286 | r -= 'a' - 'A'; |
| 287 | diff = l - r; |
| 288 | } |
| 289 | s1++; str++; |
| 290 | } while (!diff); |
| 291 | return diff; |
| 292 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 293 | |
| 294 | /* |
| 295 | * Local variables: |
| 296 | * c-indent-level: 8 |
| 297 | * c-basic-offset: 8 |
| 298 | * End: |
| 299 | */ |