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