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