blob: fbba6d53676b9b1d22bd515fe70d8f9b55780596 [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/*
Willy Tarreau77804732012-10-29 16:14:26 +010069 * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of
70 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
71 * the new chunk size, or < 0 in case of failure.
72 */
73int chunk_printf(struct chunk *chk, const char *fmt, ...)
74{
75 va_list argp;
76 int ret;
77
78 if (!chk->str || !chk->size)
79 return 0;
80
81 va_start(argp, fmt);
82 ret = vsnprintf(chk->str, chk->size, fmt, argp);
83 va_end(argp);
84 chk->len = ret;
85
86 if (ret >= chk->size)
87 ret = -1;
88
89 chk->len = ret;
90 return chk->len;
91}
92
93/*
Willy Tarreauc7e42382012-08-24 19:22:53 +020094 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
95 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
96 * the new chunk size.
97 */
Willy Tarreau77804732012-10-29 16:14:26 +010098int chunk_appendf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +020099{
100 va_list argp;
101 int ret;
102
Willy Tarreauce0162e2016-02-25 16:15:19 +0100103 if (chk->len < 0 || !chk->str || !chk->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200104 return 0;
105
106 va_start(argp, fmt);
107 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
108 if (ret >= chk->size - chk->len)
109 /* do not copy anything in case of truncation */
110 chk->str[chk->len] = 0;
111 else
112 chk->len += ret;
113 va_end(argp);
114 return chk->len;
115}
116
117/*
118 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
119 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
120 * If the chk->len is over, nothing is added. Returns the new chunk size.
121 */
122int chunk_htmlencode(struct chunk *dst, struct chunk *src)
123{
124 int i, l;
125 int olen, free;
126 char c;
127
Willy Tarreauce0162e2016-02-25 16:15:19 +0100128 if (dst->len < 0)
129 return dst->len;
130
Willy Tarreauc7e42382012-08-24 19:22:53 +0200131 olen = dst->len;
132
133 for (i = 0; i < src->len; i++) {
134 free = dst->size - dst->len;
135
136 if (!free) {
137 dst->len = olen;
138 return dst->len;
139 }
140
141 c = src->str[i];
142
143 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
144 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
145
146 if (free < l) {
147 dst->len = olen;
148 return dst->len;
149 }
150
151 dst->len += l;
152 } else {
153 dst->str[dst->len] = c;
154 dst->len++;
155 }
156 }
157
158 return dst->len;
159}
160
161/*
162 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
163 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
164 * If the chk->len is over, nothing is added. Returns the new chunk size.
165 */
166int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc)
167{
168 int i, l;
169 int olen, free;
170 char c;
171
Willy Tarreauce0162e2016-02-25 16:15:19 +0100172 if (dst->len < 0)
173 return dst->len;
174
Willy Tarreauc7e42382012-08-24 19:22:53 +0200175 olen = dst->len;
176
177 for (i = 0; i < src->len; i++) {
178 free = dst->size - dst->len;
179
180 if (!free) {
181 dst->len = olen;
182 return dst->len;
183 }
184
185 c = src->str[i];
186
187 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
188 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
189
190 if (free < l) {
191 dst->len = olen;
192 return dst->len;
193 }
194
195 dst->len += l;
196 } else {
197 dst->str[dst->len] = c;
198 dst->len++;
199 }
200 }
201
202 return dst->len;
203}
204
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200205/* Compares the string in chunk <chk> with the string in <str> which must be
206 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
207 * to be null.
208 */
209int chunk_strcmp(const struct chunk *chk, const char *str)
210{
211 const char *s1 = chk->str;
212 int len = chk->len;
213 int diff = 0;
214
215 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200216 if (--len < 0) {
217 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200218 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200219 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200220 diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
221 } while (!diff);
222 return diff;
223}
224
225/* Case-insensitively compares the string in chunk <chk> with the string in
226 * <str> which must be zero-terminated. Return is the same as with strcmp().
227 * Neither is allowed to be null.
228 */
229int chunk_strcasecmp(const struct chunk *chk, const char *str)
230{
231 const char *s1 = chk->str;
232 int len = chk->len;
233 int diff = 0;
234
235 do {
Emeric Brun78bd4032014-05-09 17:11:07 +0200236 if (--len < 0) {
237 diff = (unsigned char)0 - (unsigned char)*str;
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200238 break;
Emeric Brun78bd4032014-05-09 17:11:07 +0200239 }
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200240 diff = (unsigned char)*s1 - (unsigned char)*str;
241 if (unlikely(diff)) {
242 unsigned int l = (unsigned char)*s1;
243 unsigned int r = (unsigned char)*str;
244
245 l -= 'a';
246 r -= 'a';
247
248 if (likely(l <= (unsigned char)'z' - 'a'))
249 l -= 'a' - 'A';
250 if (likely(r <= (unsigned char)'z' - 'a'))
251 r -= 'a' - 'A';
252 diff = l - r;
253 }
254 s1++; str++;
255 } while (!diff);
256 return diff;
257}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200258
259/*
260 * Local variables:
261 * c-indent-level: 8
262 * c-basic-offset: 8
263 * End:
264 */