blob: 60902127c64c4eee688f2f15527c0fdaf6556070 [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
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 Verstraete831962e2016-06-28 22:44:26 +020020#include <common/standard.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020021
Willy Tarreau47ca5452012-12-23 20:22:19 +010022/* trash chunks used for various conversions */
23static struct chunk *trash_chunk;
24static struct chunk trash_chunk1;
25static struct chunk trash_chunk2;
26
27/* trash buffers used for various conversions */
28static int trash_size;
29static char *trash_buf1;
30static char *trash_buf2;
31
Willy Tarreaub686afd2017-02-08 11:06:11 +010032/* the trash pool for reentrant allocations */
33struct pool_head *pool2_trash = NULL;
34
Willy Tarreau47ca5452012-12-23 20:22:19 +010035/*
36* Returns a pre-allocated and initialized trash chunk that can be used for any
37* type of conversion. Two chunks and their respective buffers are alternatively
38* returned so that it is always possible to iterate data transformations without
39* losing the data being transformed. The blocks are initialized to the size of
Willy Tarreau031ad232013-12-11 17:32:08 +010040* a standard buffer, so they should be enough for everything. For convenience,
41* a zero is always emitted at the beginning of the string so that it may be
42* used as an empty string as well.
Willy Tarreau47ca5452012-12-23 20:22:19 +010043*/
44struct chunk *get_trash_chunk(void)
45{
46 char *trash_buf;
47
48 if (trash_chunk == &trash_chunk1) {
49 trash_chunk = &trash_chunk2;
50 trash_buf = trash_buf2;
51 }
52 else {
53 trash_chunk = &trash_chunk1;
54 trash_buf = trash_buf1;
55 }
Willy Tarreau031ad232013-12-11 17:32:08 +010056 *trash_buf = 0;
Willy Tarreau47ca5452012-12-23 20:22:19 +010057 chunk_init(trash_chunk, trash_buf, trash_size);
58 return trash_chunk;
59}
60
Willy Tarreau2819e992013-12-13 14:41:10 +010061/* (re)allocates the trash buffers. Returns 0 in case of failure. It is
62 * possible to call this function multiple times if the trash size changes.
63 */
Willy Tarreau47ca5452012-12-23 20:22:19 +010064int alloc_trash_buffers(int bufsize)
65{
66 trash_size = bufsize;
Hubert Verstraete831962e2016-06-28 22:44:26 +020067 trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize);
68 trash_buf2 = (char *)my_realloc2(trash_buf2, bufsize);
Willy Tarreaub686afd2017-02-08 11:06:11 +010069 pool2_trash = create_pool("trash", sizeof(struct chunk) + bufsize, MEM_F_EXACT);
70 return trash_buf1 && trash_buf2 && pool2_trash;
Willy Tarreau47ca5452012-12-23 20:22:19 +010071}
72
Willy Tarreauc7e42382012-08-24 19:22:53 +020073/*
David Carlier60deeba2015-09-25 11:58:12 +010074 * free the trash buffers
75 */
76void free_trash_buffers(void)
77{
78 free(trash_buf2);
79 free(trash_buf1);
80 trash_buf2 = NULL;
81 trash_buf1 = NULL;
82}
83
84/*
Willy Tarreaub686afd2017-02-08 11:06:11 +010085 * Allocate a trash chunk from the reentrant pool. The buffer starts at the
86 * end of the chunk. This chunk must be freed using free_trash_chunk(). This
87 * call may fail and the caller is responsible for checking that the returned
88 * pointer is not NULL.
89 */
90struct chunk *alloc_trash_chunk(void)
91{
92 struct chunk *chunk;
93
94 chunk = pool_alloc2(pool2_trash);
95 if (chunk) {
96 char *buf = (char *)chunk + sizeof(struct chunk);
97 *buf = 0;
98 chunk_init(chunk, buf, pool2_trash->size - sizeof(struct chunk));
99 }
100 return chunk;
101}
102
103/*
Willy Tarreau77804732012-10-29 16:14:26 +0100104 * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of
105 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
106 * the new chunk size, or < 0 in case of failure.
107 */
108int chunk_printf(struct chunk *chk, const char *fmt, ...)
109{
110 va_list argp;
111 int ret;
112
113 if (!chk->str || !chk->size)
114 return 0;
115
116 va_start(argp, fmt);
117 ret = vsnprintf(chk->str, chk->size, fmt, argp);
118 va_end(argp);
119 chk->len = ret;
120
121 if (ret >= chk->size)
122 ret = -1;
123
124 chk->len = ret;
125 return chk->len;
126}
127
128/*
Willy Tarreauc7e42382012-08-24 19:22:53 +0200129 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
130 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
131 * the new chunk size.
132 */
Willy Tarreau77804732012-10-29 16:14:26 +0100133int chunk_appendf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200134{
135 va_list argp;
136 int ret;
137
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100138 if (chk->len < 0 || !chk->str || !chk->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200139 return 0;
140
141 va_start(argp, fmt);
142 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
143 if (ret >= chk->size - chk->len)
144 /* do not copy anything in case of truncation */
145 chk->str[chk->len] = 0;
146 else
147 chk->len += ret;
148 va_end(argp);
149 return chk->len;
150}
151
152/*
153 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
154 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
155 * If the chk->len is over, nothing is added. Returns the new chunk size.
156 */
157int chunk_htmlencode(struct chunk *dst, struct chunk *src)
158{
159 int i, l;
160 int olen, free;
161 char c;
162
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100163 if (dst->len < 0)
164 return dst->len;
165
Willy Tarreauc7e42382012-08-24 19:22:53 +0200166 olen = dst->len;
167
168 for (i = 0; i < src->len; i++) {
169 free = dst->size - dst->len;
170
171 if (!free) {
172 dst->len = olen;
173 return dst->len;
174 }
175
176 c = src->str[i];
177
178 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
179 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
180
181 if (free < l) {
182 dst->len = olen;
183 return dst->len;
184 }
185
186 dst->len += l;
187 } else {
188 dst->str[dst->len] = c;
189 dst->len++;
190 }
191 }
192
193 return dst->len;
194}
195
196/*
197 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
198 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
199 * If the chk->len is over, nothing is added. Returns the new chunk size.
200 */
201int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc)
202{
203 int i, l;
204 int olen, free;
205 char c;
206
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100207 if (dst->len < 0)
208 return dst->len;
209
Willy Tarreauc7e42382012-08-24 19:22:53 +0200210 olen = dst->len;
211
212 for (i = 0; i < src->len; i++) {
213 free = dst->size - dst->len;
214
215 if (!free) {
216 dst->len = olen;
217 return dst->len;
218 }
219
220 c = src->str[i];
221
222 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
223 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
224
225 if (free < l) {
226 dst->len = olen;
227 return dst->len;
228 }
229
230 dst->len += l;
231 } else {
232 dst->str[dst->len] = c;
233 dst->len++;
234 }
235 }
236
237 return dst->len;
238}
239
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200240/* Compares the string in chunk <chk> with the string in <str> which must be
241 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
242 * to be null.
243 */
244int chunk_strcmp(const struct chunk *chk, const char *str)
245{
246 const char *s1 = chk->str;
247 int len = chk->len;
248 int diff = 0;
249
250 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200251 if (--len < 0) {
252 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200253 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200254 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200255 diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
256 } while (!diff);
257 return diff;
258}
259
260/* Case-insensitively compares the string in chunk <chk> with the string in
261 * <str> which must be zero-terminated. Return is the same as with strcmp().
262 * Neither is allowed to be null.
263 */
264int chunk_strcasecmp(const struct chunk *chk, const char *str)
265{
266 const char *s1 = chk->str;
267 int len = chk->len;
268 int diff = 0;
269
270 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200271 if (--len < 0) {
272 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200273 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200274 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200275 diff = (unsigned char)*s1 - (unsigned char)*str;
276 if (unlikely(diff)) {
277 unsigned int l = (unsigned char)*s1;
278 unsigned int r = (unsigned char)*str;
279
280 l -= 'a';
281 r -= 'a';
282
283 if (likely(l <= (unsigned char)'z' - 'a'))
284 l -= 'a' - 'A';
285 if (likely(r <= (unsigned char)'z' - 'a'))
286 r -= 'a' - 'A';
287 diff = l - r;
288 }
289 s1++; str++;
290 } while (!diff);
291 return diff;
292}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200293
294/*
295 * Local variables:
296 * c-indent-level: 8
297 * c-basic-offset: 8
298 * End:
299 */