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