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 <haproxy/api.h>
19 #include <haproxy/chunk.h>
20 #include <haproxy/global.h>
21 #include <haproxy/tools.h>
22 
23 /* trash chunks used for various conversions */
24 static THREAD_LOCAL struct buffer *trash_chunk;
25 static THREAD_LOCAL struct buffer trash_chunk1;
26 static THREAD_LOCAL struct buffer trash_chunk2;
27 
28 /* trash buffers used for various conversions */
29 static int trash_size;
30 static THREAD_LOCAL char *trash_buf1;
31 static THREAD_LOCAL char *trash_buf2;
32 
33 /* the trash pool for reentrant allocations */
34 struct pool_head *pool_head_trash = NULL;
35 
36 /* this is used to drain data, and as a temporary buffer for sprintf()... */
37 THREAD_LOCAL struct buffer trash = { };
38 
39 /*
40 * Returns a pre-allocated and initialized trash chunk that can be used for any
41 * type of conversion. Two chunks and their respective buffers are alternatively
42 * returned so that it is always possible to iterate data transformations without
43 * losing the data being transformed. The blocks are initialized to the size of
44 * a standard buffer, so they should be enough for everything. For convenience,
45 * a zero is always emitted at the beginning of the string so that it may be
46 * used as an empty string as well.
47 */
get_trash_chunk(void)48 struct buffer *get_trash_chunk(void)
49 {
50 	char *trash_buf;
51 
52 	if (trash_chunk == &trash_chunk1) {
53 		trash_chunk = &trash_chunk2;
54 		trash_buf = trash_buf2;
55 	}
56 	else {
57 		trash_chunk = &trash_chunk1;
58 		trash_buf = trash_buf1;
59 	}
60 	*trash_buf = 0;
61 	chunk_init(trash_chunk, trash_buf, trash_size);
62 	return trash_chunk;
63 }
64 
65 /* (re)allocates the trash buffers. Returns 0 in case of failure. It is
66  * possible to call this function multiple times if the trash size changes.
67  */
alloc_trash_buffers(int bufsize)68 static int alloc_trash_buffers(int bufsize)
69 {
70 	chunk_init(&trash, my_realloc2(trash.area, bufsize), bufsize);
71 	trash_size = bufsize;
72 	trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize);
73 	trash_buf2 = (char *)my_realloc2(trash_buf2, bufsize);
74 	return trash.area && trash_buf1 && trash_buf2;
75 }
76 
alloc_trash_buffers_per_thread()77 static int alloc_trash_buffers_per_thread()
78 {
79 	return alloc_trash_buffers(global.tune.bufsize);
80 }
81 
free_trash_buffers_per_thread()82 static void free_trash_buffers_per_thread()
83 {
84 	chunk_destroy(&trash);
85 	free(trash_buf2);
86 	free(trash_buf1);
87 	trash_buf2 = NULL;
88 	trash_buf1 = NULL;
89 }
90 
91 /* Initialize the trash buffers. It returns 0 if an error occurred. */
init_trash_buffers(int first)92 int init_trash_buffers(int first)
93 {
94 	pool_destroy(pool_head_trash);
95 	pool_head_trash = create_pool("trash",
96 				      sizeof(struct buffer) + global.tune.bufsize,
97 				      MEM_F_EXACT);
98 	if (!pool_head_trash || !alloc_trash_buffers(global.tune.bufsize))
99 		return 0;
100 	return 1;
101 }
102 
103 /*
104  * Allocate a trash chunk from the reentrant pool. The buffer starts at the
105  * end of the chunk. This chunk must be freed using free_trash_chunk(). This
106  * call may fail and the caller is responsible for checking that the returned
107  * pointer is not NULL.
108  */
alloc_trash_chunk(void)109 struct buffer *alloc_trash_chunk(void)
110 {
111 	struct buffer *chunk;
112 
113 	chunk = pool_alloc(pool_head_trash);
114 	if (chunk) {
115 		char *buf = (char *)chunk + sizeof(struct buffer);
116 		*buf = 0;
117 		chunk_init(chunk, buf,
118 			   pool_head_trash->size - sizeof(struct buffer));
119 	}
120 	return chunk;
121 }
122 
123 /*
124  * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of
125  * at most chk->size chars. If the chk->len is over, nothing is added. Returns
126  * the new chunk size, or < 0 in case of failure.
127  */
chunk_printf(struct buffer * chk,const char * fmt,...)128 int chunk_printf(struct buffer *chk, const char *fmt, ...)
129 {
130 	va_list argp;
131 	int ret;
132 
133 	if (!chk->area || !chk->size)
134 		return 0;
135 
136 	va_start(argp, fmt);
137 	ret = vsnprintf(chk->area, chk->size, fmt, argp);
138 	va_end(argp);
139 
140 	if (ret >= chk->size)
141 		return -1;
142 
143 	chk->data = ret;
144 	return chk->data;
145 }
146 
147 /*
148  * Does an snprintf() at the end of chunk <chk>, respecting the limit of
149  * at most chk->size chars. If the chk->len is over, nothing is added. Returns
150  * the new chunk size.
151  */
chunk_appendf(struct buffer * chk,const char * fmt,...)152 int chunk_appendf(struct buffer *chk, const char *fmt, ...)
153 {
154 	va_list argp;
155 	int ret;
156 
157 	if (!chk->area || !chk->size)
158 		return 0;
159 
160 	va_start(argp, fmt);
161 	ret = vsnprintf(chk->area + chk->data, chk->size - chk->data, fmt,
162 			argp);
163 	if (ret >= chk->size - chk->data)
164 		/* do not copy anything in case of truncation */
165 		chk->area[chk->data] = 0;
166 	else
167 		chk->data += ret;
168 	va_end(argp);
169 	return chk->data;
170 }
171 
172 /*
173  * Encode chunk <src> into chunk <dst>, respecting the limit of at most
174  * chk->size chars. Replace non-printable or special characters with "&#%d;".
175  * If the chk->len is over, nothing is added. Returns the new chunk size.
176  */
chunk_htmlencode(struct buffer * dst,struct buffer * src)177 int chunk_htmlencode(struct buffer *dst, struct buffer *src)
178 {
179 	int i, l;
180 	int olen, free;
181 	char c;
182 
183 	olen = dst->data;
184 
185 	for (i = 0; i < src->data; i++) {
186 		free = dst->size - dst->data;
187 
188 		if (!free) {
189 			dst->data = olen;
190 			return dst->data;
191 		}
192 
193 		c = src->area[i];
194 
195 		if (!isascii((unsigned char)c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
196 			l = snprintf(dst->area + dst->data, free, "&#%u;",
197 				     (unsigned char)c);
198 
199 			if (free < l) {
200 				dst->data = olen;
201 				return dst->data;
202 			}
203 
204 			dst->data += l;
205 		} else {
206 			dst->area[dst->data] = c;
207 			dst->data++;
208 		}
209 	}
210 
211 	return dst->data;
212 }
213 
214 /*
215  * Encode chunk <src> into chunk <dst>, respecting the limit of at most
216  * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
217  * If the chk->len is over, nothing is added. Returns the new chunk size.
218  */
chunk_asciiencode(struct buffer * dst,struct buffer * src,char qc)219 int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc)
220 {
221 	int i, l;
222 	int olen, free;
223 	char c;
224 
225 	olen = dst->data;
226 
227 	for (i = 0; i < src->data; i++) {
228 		free = dst->size - dst->data;
229 
230 		if (!free) {
231 			dst->data = olen;
232 			return dst->data;
233 		}
234 
235 		c = src->area[i];
236 
237 		if (!isascii((unsigned char)c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
238 			l = snprintf(dst->area + dst->data, free, "<%02X>",
239 				     (unsigned char)c);
240 
241 			if (free < l) {
242 				dst->data = olen;
243 				return dst->data;
244 			}
245 
246 			dst->data += l;
247 		} else {
248 			dst->area[dst->data] = c;
249 			dst->data++;
250 		}
251 	}
252 
253 	return dst->data;
254 }
255 
256 /* Compares the string in chunk <chk> with the string in <str> which must be
257  * zero-terminated. Return is the same as with strcmp(). Neither is allowed
258  * to be null.
259  */
chunk_strcmp(const struct buffer * chk,const char * str)260 int chunk_strcmp(const struct buffer *chk, const char *str)
261 {
262 	const char *s1 = chk->area;
263 	int len = chk->data;
264 	int diff = 0;
265 
266 	do {
267 		if (--len < 0) {
268 			diff = (unsigned char)0 - (unsigned char)*str;
269 			break;
270 		}
271 		diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
272 	} while (!diff);
273 	return diff;
274 }
275 
276 /* Case-insensitively compares the string in chunk <chk> with the string in
277  * <str> which must be zero-terminated. Return is the same as with strcmp().
278  * Neither is allowed to be null.
279  */
chunk_strcasecmp(const struct buffer * chk,const char * str)280 int chunk_strcasecmp(const struct buffer *chk, const char *str)
281 {
282 	const char *s1 = chk->area;
283 	int len = chk->data;
284 	int diff = 0;
285 
286 	do {
287 		if (--len < 0) {
288 			diff = (unsigned char)0 - (unsigned char)*str;
289 			break;
290 		}
291 		diff = (unsigned char)*s1 - (unsigned char)*str;
292 		if (unlikely(diff)) {
293 			unsigned int l = (unsigned char)*s1;
294 			unsigned int r = (unsigned char)*str;
295 
296 			l -= 'a';
297 			r -= 'a';
298 
299 			if (likely(l <= (unsigned char)'z' - 'a'))
300 				l -= 'a' - 'A';
301 			if (likely(r <= (unsigned char)'z' - 'a'))
302 				r -= 'a' - 'A';
303 			diff = l - r;
304 		}
305 		s1++; str++;
306 	} while (!diff);
307 	return diff;
308 }
309 
310 REGISTER_PER_THREAD_ALLOC(alloc_trash_buffers_per_thread);
311 REGISTER_PER_THREAD_FREE(free_trash_buffers_per_thread);
312 
313 /*
314  * Local variables:
315  *  c-indent-level: 8
316  *  c-basic-offset: 8
317  * End:
318  */
319