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 | /* |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 69 | * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of |
| 70 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 71 | * the new chunk size, or < 0 in case of failure. |
| 72 | */ |
| 73 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
| 74 | { |
| 75 | va_list argp; |
| 76 | int ret; |
| 77 | |
| 78 | if (!chk->str || !chk->size) |
| 79 | return 0; |
| 80 | |
| 81 | va_start(argp, fmt); |
| 82 | ret = vsnprintf(chk->str, chk->size, fmt, argp); |
| 83 | va_end(argp); |
| 84 | chk->len = ret; |
| 85 | |
| 86 | if (ret >= chk->size) |
| 87 | ret = -1; |
| 88 | |
| 89 | chk->len = ret; |
| 90 | return chk->len; |
| 91 | } |
| 92 | |
| 93 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 94 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
| 95 | * at most chk->size chars. If the chk->len is over, nothing is added. Returns |
| 96 | * the new chunk size. |
| 97 | */ |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 98 | int chunk_appendf(struct chunk *chk, const char *fmt, ...) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 99 | { |
| 100 | va_list argp; |
| 101 | int ret; |
| 102 | |
| 103 | if (!chk->str || !chk->size) |
| 104 | return 0; |
| 105 | |
| 106 | va_start(argp, fmt); |
| 107 | ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp); |
| 108 | if (ret >= chk->size - chk->len) |
| 109 | /* do not copy anything in case of truncation */ |
| 110 | chk->str[chk->len] = 0; |
| 111 | else |
| 112 | chk->len += ret; |
| 113 | va_end(argp); |
| 114 | return chk->len; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 119 | * chk->size chars. Replace non-printable or special chracters with "&#%d;". |
| 120 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 121 | */ |
| 122 | int chunk_htmlencode(struct chunk *dst, struct chunk *src) |
| 123 | { |
| 124 | int i, l; |
| 125 | int olen, free; |
| 126 | char c; |
| 127 | |
| 128 | olen = dst->len; |
| 129 | |
| 130 | for (i = 0; i < src->len; i++) { |
| 131 | free = dst->size - dst->len; |
| 132 | |
| 133 | if (!free) { |
| 134 | dst->len = olen; |
| 135 | return dst->len; |
| 136 | } |
| 137 | |
| 138 | c = src->str[i]; |
| 139 | |
| 140 | if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') { |
| 141 | l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c); |
| 142 | |
| 143 | if (free < l) { |
| 144 | dst->len = olen; |
| 145 | return dst->len; |
| 146 | } |
| 147 | |
| 148 | dst->len += l; |
| 149 | } else { |
| 150 | dst->str[dst->len] = c; |
| 151 | dst->len++; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return dst->len; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Encode chunk <src> into chunk <dst>, respecting the limit of at most |
| 160 | * chk->size chars. Replace non-printable or char passed in qc with "<%02X>". |
| 161 | * If the chk->len is over, nothing is added. Returns the new chunk size. |
| 162 | */ |
| 163 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) |
| 164 | { |
| 165 | int i, l; |
| 166 | int olen, free; |
| 167 | char c; |
| 168 | |
| 169 | olen = dst->len; |
| 170 | |
| 171 | for (i = 0; i < src->len; i++) { |
| 172 | free = dst->size - dst->len; |
| 173 | |
| 174 | if (!free) { |
| 175 | dst->len = olen; |
| 176 | return dst->len; |
| 177 | } |
| 178 | |
| 179 | c = src->str[i]; |
| 180 | |
| 181 | if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) { |
| 182 | l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c); |
| 183 | |
| 184 | if (free < l) { |
| 185 | dst->len = olen; |
| 186 | return dst->len; |
| 187 | } |
| 188 | |
| 189 | dst->len += l; |
| 190 | } else { |
| 191 | dst->str[dst->len] = c; |
| 192 | dst->len++; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return dst->len; |
| 197 | } |
| 198 | |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 199 | /* Compares the string in chunk <chk> with the string in <str> which must be |
| 200 | * zero-terminated. Return is the same as with strcmp(). Neither is allowed |
| 201 | * to be null. |
| 202 | */ |
| 203 | int chunk_strcmp(const struct chunk *chk, const char *str) |
| 204 | { |
| 205 | const char *s1 = chk->str; |
| 206 | int len = chk->len; |
| 207 | int diff = 0; |
| 208 | |
| 209 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 210 | if (--len < 0) { |
| 211 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 212 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 213 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 214 | diff = (unsigned char)*(s1++) - (unsigned char)*(str++); |
| 215 | } while (!diff); |
| 216 | return diff; |
| 217 | } |
| 218 | |
| 219 | /* Case-insensitively compares the string in chunk <chk> with the string in |
| 220 | * <str> which must be zero-terminated. Return is the same as with strcmp(). |
| 221 | * Neither is allowed to be null. |
| 222 | */ |
| 223 | int chunk_strcasecmp(const struct chunk *chk, const char *str) |
| 224 | { |
| 225 | const char *s1 = chk->str; |
| 226 | int len = chk->len; |
| 227 | int diff = 0; |
| 228 | |
| 229 | do { |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 230 | if (--len < 0) { |
| 231 | diff = (unsigned char)0 - (unsigned char)*str; |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 232 | break; |
Emeric Brun | 78bd403 | 2014-05-09 17:11:07 +0200 | [diff] [blame] | 233 | } |
Willy Tarreau | ad8f8e8 | 2012-10-19 15:18:06 +0200 | [diff] [blame] | 234 | diff = (unsigned char)*s1 - (unsigned char)*str; |
| 235 | if (unlikely(diff)) { |
| 236 | unsigned int l = (unsigned char)*s1; |
| 237 | unsigned int r = (unsigned char)*str; |
| 238 | |
| 239 | l -= 'a'; |
| 240 | r -= 'a'; |
| 241 | |
| 242 | if (likely(l <= (unsigned char)'z' - 'a')) |
| 243 | l -= 'a' - 'A'; |
| 244 | if (likely(r <= (unsigned char)'z' - 'a')) |
| 245 | r -= 'a' - 'A'; |
| 246 | diff = l - r; |
| 247 | } |
| 248 | s1++; str++; |
| 249 | } while (!diff); |
| 250 | return diff; |
| 251 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Local variables: |
| 255 | * c-indent-level: 8 |
| 256 | * c-basic-offset: 8 |
| 257 | * End: |
| 258 | */ |