blob: 7134fead3503620bf3213cc52620798c2f374674 [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
32/*
33* Returns a pre-allocated and initialized trash chunk that can be used for any
34* type of conversion. Two chunks and their respective buffers are alternatively
35* returned so that it is always possible to iterate data transformations without
36* losing the data being transformed. The blocks are initialized to the size of
Willy Tarreau031ad232013-12-11 17:32:08 +010037* a standard buffer, so they should be enough for everything. For convenience,
38* a zero is always emitted at the beginning of the string so that it may be
39* used as an empty string as well.
Willy Tarreau47ca5452012-12-23 20:22:19 +010040*/
41struct chunk *get_trash_chunk(void)
42{
43 char *trash_buf;
44
45 if (trash_chunk == &trash_chunk1) {
46 trash_chunk = &trash_chunk2;
47 trash_buf = trash_buf2;
48 }
49 else {
50 trash_chunk = &trash_chunk1;
51 trash_buf = trash_buf1;
52 }
Willy Tarreau031ad232013-12-11 17:32:08 +010053 *trash_buf = 0;
Willy Tarreau47ca5452012-12-23 20:22:19 +010054 chunk_init(trash_chunk, trash_buf, trash_size);
55 return trash_chunk;
56}
57
Willy Tarreau2819e992013-12-13 14:41:10 +010058/* (re)allocates the trash buffers. Returns 0 in case of failure. It is
59 * possible to call this function multiple times if the trash size changes.
60 */
Willy Tarreau47ca5452012-12-23 20:22:19 +010061int alloc_trash_buffers(int bufsize)
62{
63 trash_size = bufsize;
Hubert Verstraete831962e2016-06-28 22:44:26 +020064 trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize);
65 trash_buf2 = (char *)my_realloc2(trash_buf2, bufsize);
Willy Tarreau47ca5452012-12-23 20:22:19 +010066 return trash_buf1 && trash_buf2;
67}
68
Willy Tarreauc7e42382012-08-24 19:22:53 +020069/*
David Carlier60deeba2015-09-25 11:58:12 +010070 * free the trash buffers
71 */
72void free_trash_buffers(void)
73{
74 free(trash_buf2);
75 free(trash_buf1);
76 trash_buf2 = NULL;
77 trash_buf1 = NULL;
78}
79
80/*
Willy Tarreau77804732012-10-29 16:14:26 +010081 * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of
82 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
83 * the new chunk size, or < 0 in case of failure.
84 */
85int chunk_printf(struct chunk *chk, const char *fmt, ...)
86{
87 va_list argp;
88 int ret;
89
90 if (!chk->str || !chk->size)
91 return 0;
92
93 va_start(argp, fmt);
94 ret = vsnprintf(chk->str, chk->size, fmt, argp);
95 va_end(argp);
96 chk->len = ret;
97
98 if (ret >= chk->size)
99 ret = -1;
100
101 chk->len = ret;
102 return chk->len;
103}
104
105/*
Willy Tarreauc7e42382012-08-24 19:22:53 +0200106 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
107 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
108 * the new chunk size.
109 */
Willy Tarreau77804732012-10-29 16:14:26 +0100110int chunk_appendf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200111{
112 va_list argp;
113 int ret;
114
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100115 if (chk->len < 0 || !chk->str || !chk->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200116 return 0;
117
118 va_start(argp, fmt);
119 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
120 if (ret >= chk->size - chk->len)
121 /* do not copy anything in case of truncation */
122 chk->str[chk->len] = 0;
123 else
124 chk->len += ret;
125 va_end(argp);
126 return chk->len;
127}
128
129/*
130 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
131 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
132 * If the chk->len is over, nothing is added. Returns the new chunk size.
133 */
134int chunk_htmlencode(struct chunk *dst, struct chunk *src)
135{
136 int i, l;
137 int olen, free;
138 char c;
139
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100140 if (dst->len < 0)
141 return dst->len;
142
Willy Tarreauc7e42382012-08-24 19:22:53 +0200143 olen = dst->len;
144
145 for (i = 0; i < src->len; i++) {
146 free = dst->size - dst->len;
147
148 if (!free) {
149 dst->len = olen;
150 return dst->len;
151 }
152
153 c = src->str[i];
154
155 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
156 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
157
158 if (free < l) {
159 dst->len = olen;
160 return dst->len;
161 }
162
163 dst->len += l;
164 } else {
165 dst->str[dst->len] = c;
166 dst->len++;
167 }
168 }
169
170 return dst->len;
171}
172
173/*
174 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
175 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
176 * If the chk->len is over, nothing is added. Returns the new chunk size.
177 */
178int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc)
179{
180 int i, l;
181 int olen, free;
182 char c;
183
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100184 if (dst->len < 0)
185 return dst->len;
186
Willy Tarreauc7e42382012-08-24 19:22:53 +0200187 olen = dst->len;
188
189 for (i = 0; i < src->len; i++) {
190 free = dst->size - dst->len;
191
192 if (!free) {
193 dst->len = olen;
194 return dst->len;
195 }
196
197 c = src->str[i];
198
199 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
200 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
201
202 if (free < l) {
203 dst->len = olen;
204 return dst->len;
205 }
206
207 dst->len += l;
208 } else {
209 dst->str[dst->len] = c;
210 dst->len++;
211 }
212 }
213
214 return dst->len;
215}
216
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200217/* Compares the string in chunk <chk> with the string in <str> which must be
218 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
219 * to be null.
220 */
221int chunk_strcmp(const struct chunk *chk, const char *str)
222{
223 const char *s1 = chk->str;
224 int len = chk->len;
225 int diff = 0;
226
227 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200228 if (--len < 0) {
229 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200230 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200231 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200232 diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
233 } while (!diff);
234 return diff;
235}
236
237/* Case-insensitively compares the string in chunk <chk> with the string in
238 * <str> which must be zero-terminated. Return is the same as with strcmp().
239 * Neither is allowed to be null.
240 */
241int chunk_strcasecmp(const struct chunk *chk, const char *str)
242{
243 const char *s1 = chk->str;
244 int len = chk->len;
245 int diff = 0;
246
247 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200248 if (--len < 0) {
249 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200250 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200251 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200252 diff = (unsigned char)*s1 - (unsigned char)*str;
253 if (unlikely(diff)) {
254 unsigned int l = (unsigned char)*s1;
255 unsigned int r = (unsigned char)*str;
256
257 l -= 'a';
258 r -= 'a';
259
260 if (likely(l <= (unsigned char)'z' - 'a'))
261 l -= 'a' - 'A';
262 if (likely(r <= (unsigned char)'z' - 'a'))
263 r -= 'a' - 'A';
264 diff = l - r;
265 }
266 s1++; str++;
267 } while (!diff);
268 return diff;
269}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200270
271/*
272 * Local variables:
273 * c-indent-level: 8
274 * c-basic-offset: 8
275 * End:
276 */