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 | |
| 114 | if (!chk->str || !chk->size) |
| 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 | |
| 139 | olen = dst->len; |
| 140 | |
| 141 | for (i = 0; i < src->len; i++) { |
| 142 | free = dst->size - dst->len; |
| 143 | |
| 144 | if (!free) { |
| 145 | dst->len = olen; |
| 146 | return dst->len; |
| 147 | } |
| 148 | |
| 149 | c = src->str[i]; |
| 150 | |
| 151 | if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') { |
| 152 | l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c); |
| 153 | |
| 154 | if (free < l) { |
| 155 | dst->len = olen; |
| 156 | return dst->len; |
| 157 | } |
| 158 | |
| 159 | dst->len += l; |
| 160 | } else { |
| 161 | dst->str[dst->len] = c; |
| 162 | dst->len++; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return dst->len; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 171 | * chk->size chars. Replace non-printable or char passed in qc with "<%02X>". |
| 172 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 173 | */ |
| 174 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) |
| 175 | { |
| 176 | int i, l; |
| 177 | int olen, free; |
| 178 | char c; |
| 179 | |
| 180 | olen = dst->len; |
| 181 | |
| 182 | for (i = 0; i < src->len; i++) { |
| 183 | free = dst->size - dst->len; |
| 184 | |
| 185 | if (!free) { |
| 186 | dst->len = olen; |
| 187 | return dst->len; |
| 188 | } |
| 189 | |
| 190 | c = src->str[i]; |
| 191 | |
| 192 | if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) { |
| 193 | l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c); |
| 194 | |
| 195 | if (free < l) { |
| 196 | dst->len = olen; |
| 197 | return dst->len; |
| 198 | } |
| 199 | |
| 200 | dst->len += l; |
| 201 | } else { |
| 202 | dst->str[dst->len] = c; |
| 203 | dst->len++; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return dst->len; |
| 208 | } |
| 209 | |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 210 | /* Compares the string in chunk <chk> with the string in <str> which must be |
| 211 | * zero-terminated. Return is the same as with strcmp(). Neither is allowed |
| 212 | * to be null. |
| 213 | */ |
| 214 | int chunk_strcmp(const struct chunk *chk, const char *str) |
| 215 | { |
| 216 | const char *s1 = chk->str; |
| 217 | int len = chk->len; |
| 218 | int diff = 0; |
| 219 | |
| 220 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 221 | if (--len < 0) { |
| 222 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 223 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 224 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 225 | diff = (unsigned char)*(s1++) - (unsigned char)*(str++); |
| 226 | } while (!diff); |
| 227 | return diff; |
| 228 | } |
| 229 | |
| 230 | /* Case-insensitively compares the string in chunk <chk> with the string in |
| 231 | * <str> which must be zero-terminated. Return is the same as with strcmp(). |
| 232 | * Neither is allowed to be null. |
| 233 | */ |
| 234 | int chunk_strcasecmp(const struct chunk *chk, const char *str) |
| 235 | { |
| 236 | const char *s1 = chk->str; |
| 237 | int len = chk->len; |
| 238 | int diff = 0; |
| 239 | |
| 240 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 241 | if (--len < 0) { |
| 242 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 243 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 244 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 245 | diff = (unsigned char)*s1 - (unsigned char)*str; |
| 246 | if (unlikely(diff)) { |
| 247 | unsigned int l = (unsigned char)*s1; |
| 248 | unsigned int r = (unsigned char)*str; |
| 249 | |
| 250 | l -= 'a'; |
| 251 | r -= 'a'; |
| 252 | |
| 253 | if (likely(l <= (unsigned char)'z' - 'a')) |
| 254 | l -= 'a' - 'A'; |
| 255 | if (likely(r <= (unsigned char)'z' - 'a')) |
| 256 | r -= 'a' - 'A'; |
| 257 | diff = l - r; |
| 258 | } |
| 259 | s1++; str++; |
| 260 | } while (!diff); |
| 261 | return diff; |
| 262 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 263 | |
| 264 | /* |
| 265 | * Local variables: |
| 266 | * c-indent-level: 8 |
| 267 | * c-basic-offset: 8 |
| 268 | * End: |
| 269 | */ |