blob: e2511076ba57381611e965d9345aff6431361df8 [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>
20
Willy Tarreau47ca5452012-12-23 20:22:19 +010021/* trash chunks used for various conversions */
22static struct chunk *trash_chunk;
23static struct chunk trash_chunk1;
24static struct chunk trash_chunk2;
25
26/* trash buffers used for various conversions */
27static int trash_size;
28static char *trash_buf1;
29static 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 Tarreau031ad232013-12-11 17:32:08 +010036* 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 Tarreau47ca5452012-12-23 20:22:19 +010039*/
40struct 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 Tarreau031ad232013-12-11 17:32:08 +010052 *trash_buf = 0;
Willy Tarreau47ca5452012-12-23 20:22:19 +010053 chunk_init(trash_chunk, trash_buf, trash_size);
54 return trash_chunk;
55}
56
Willy Tarreau2819e992013-12-13 14:41:10 +010057/* (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 Tarreau47ca5452012-12-23 20:22:19 +010060int alloc_trash_buffers(int bufsize)
61{
62 trash_size = bufsize;
Willy Tarreau2819e992013-12-13 14:41:10 +010063 trash_buf1 = (char *)realloc(trash_buf1, bufsize);
64 trash_buf2 = (char *)realloc(trash_buf2, bufsize);
Willy Tarreau47ca5452012-12-23 20:22:19 +010065 return trash_buf1 && trash_buf2;
66}
67
Willy Tarreauc7e42382012-08-24 19:22:53 +020068/*
David Carlier60deeba2015-09-25 11:58:12 +010069 * free the trash buffers
70 */
71void 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 Tarreau77804732012-10-29 16:14:26 +010080 * 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 */
84int 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 Tarreauc7e42382012-08-24 19:22:53 +0200105 * 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 Tarreau77804732012-10-29 16:14:26 +0100109int chunk_appendf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200110{
111 va_list argp;
112 int ret;
113
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100114 if (chk->len < 0 || !chk->str || !chk->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200115 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 */
133int chunk_htmlencode(struct chunk *dst, struct chunk *src)
134{
135 int i, l;
136 int olen, free;
137 char c;
138
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100139 if (dst->len < 0)
140 return dst->len;
141
Willy Tarreauc7e42382012-08-24 19:22:53 +0200142 olen = dst->len;
143
144 for (i = 0; i < src->len; i++) {
145 free = dst->size - dst->len;
146
147 if (!free) {
148 dst->len = olen;
149 return dst->len;
150 }
151
152 c = src->str[i];
153
154 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
155 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
156
157 if (free < l) {
158 dst->len = olen;
159 return dst->len;
160 }
161
162 dst->len += l;
163 } else {
164 dst->str[dst->len] = c;
165 dst->len++;
166 }
167 }
168
169 return dst->len;
170}
171
172/*
173 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
174 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
175 * If the chk->len is over, nothing is added. Returns the new chunk size.
176 */
177int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc)
178{
179 int i, l;
180 int olen, free;
181 char c;
182
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100183 if (dst->len < 0)
184 return dst->len;
185
Willy Tarreauc7e42382012-08-24 19:22:53 +0200186 olen = dst->len;
187
188 for (i = 0; i < src->len; i++) {
189 free = dst->size - dst->len;
190
191 if (!free) {
192 dst->len = olen;
193 return dst->len;
194 }
195
196 c = src->str[i];
197
198 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
199 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
200
201 if (free < l) {
202 dst->len = olen;
203 return dst->len;
204 }
205
206 dst->len += l;
207 } else {
208 dst->str[dst->len] = c;
209 dst->len++;
210 }
211 }
212
213 return dst->len;
214}
215
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200216/* Compares the string in chunk <chk> with the string in <str> which must be
217 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
218 * to be null.
219 */
220int chunk_strcmp(const struct chunk *chk, const char *str)
221{
222 const char *s1 = chk->str;
223 int len = chk->len;
224 int diff = 0;
225
226 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200227 if (--len < 0) {
228 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200229 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200230 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200231 diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
232 } while (!diff);
233 return diff;
234}
235
236/* Case-insensitively compares the string in chunk <chk> with the string in
237 * <str> which must be zero-terminated. Return is the same as with strcmp().
238 * Neither is allowed to be null.
239 */
240int chunk_strcasecmp(const struct chunk *chk, const char *str)
241{
242 const char *s1 = chk->str;
243 int len = chk->len;
244 int diff = 0;
245
246 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200247 if (--len < 0) {
248 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200249 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200250 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200251 diff = (unsigned char)*s1 - (unsigned char)*str;
252 if (unlikely(diff)) {
253 unsigned int l = (unsigned char)*s1;
254 unsigned int r = (unsigned char)*str;
255
256 l -= 'a';
257 r -= 'a';
258
259 if (likely(l <= (unsigned char)'z' - 'a'))
260 l -= 'a' - 'A';
261 if (likely(r <= (unsigned char)'z' - 'a'))
262 r -= 'a' - 'A';
263 diff = l - r;
264 }
265 s1++; str++;
266 } while (!diff);
267 return diff;
268}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200269
270/*
271 * Local variables:
272 * c-indent-level: 8
273 * c-basic-offset: 8
274 * End:
275 */