blob: d7d8501d638101a49a130335e34fd0e2933e4270 [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
103 if (!chk->str || !chk->size)
104 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
128 olen = dst->len;
129
130 for (i = 0; i < src->len; i++) {
131 free = dst->size - dst->len;
132
133 if (!free) {
134 dst->len = olen;
135 return dst->len;
136 }
137
138 c = src->str[i];
139
140 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
141 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
142
143 if (free < l) {
144 dst->len = olen;
145 return dst->len;
146 }
147
148 dst->len += l;
149 } else {
150 dst->str[dst->len] = c;
151 dst->len++;
152 }
153 }
154
155 return dst->len;
156}
157
158/*
159 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
160 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
161 * If the chk->len is over, nothing is added. Returns the new chunk size.
162 */
163int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc)
164{
165 int i, l;
166 int olen, free;
167 char c;
168
169 olen = dst->len;
170
171 for (i = 0; i < src->len; i++) {
172 free = dst->size - dst->len;
173
174 if (!free) {
175 dst->len = olen;
176 return dst->len;
177 }
178
179 c = src->str[i];
180
181 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
182 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
183
184 if (free < l) {
185 dst->len = olen;
186 return dst->len;
187 }
188
189 dst->len += l;
190 } else {
191 dst->str[dst->len] = c;
192 dst->len++;
193 }
194 }
195
196 return dst->len;
197}
198
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200199/* Compares the string in chunk <chk> with the string in <str> which must be
200 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
201 * to be null.
202 */
203int chunk_strcmp(const struct chunk *chk, const char *str)
204{
205 const char *s1 = chk->str;
206 int len = chk->len;
207 int diff = 0;
208
209 do {
210 if (--len < 0)
211 break;
212 diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
213 } while (!diff);
214 return diff;
215}
216
217/* Case-insensitively compares the string in chunk <chk> with the string in
218 * <str> which must be zero-terminated. Return is the same as with strcmp().
219 * Neither is allowed to be null.
220 */
221int chunk_strcasecmp(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 {
228 if (--len < 0)
229 break;
230 diff = (unsigned char)*s1 - (unsigned char)*str;
231 if (unlikely(diff)) {
232 unsigned int l = (unsigned char)*s1;
233 unsigned int r = (unsigned char)*str;
234
235 l -= 'a';
236 r -= 'a';
237
238 if (likely(l <= (unsigned char)'z' - 'a'))
239 l -= 'a' - 'A';
240 if (likely(r <= (unsigned char)'z' - 'a'))
241 r -= 'a' - 'A';
242 diff = l - r;
243 }
244 s1++; str++;
245 } while (!diff);
246 return diff;
247}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200248
249/*
250 * Local variables:
251 * c-indent-level: 8
252 * c-basic-offset: 8
253 * End:
254 */