blob: f84ab10510e04954104a1b7787af7af5d6bd15c4 [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
36* a standard buffer, so they should be enough for everything.
37*/
38struct chunk *get_trash_chunk(void)
39{
40 char *trash_buf;
41
42 if (trash_chunk == &trash_chunk1) {
43 trash_chunk = &trash_chunk2;
44 trash_buf = trash_buf2;
45 }
46 else {
47 trash_chunk = &trash_chunk1;
48 trash_buf = trash_buf1;
49 }
50 chunk_init(trash_chunk, trash_buf, trash_size);
51 return trash_chunk;
52}
53
Willy Tarreau2819e992013-12-13 14:41:10 +010054/* (re)allocates the trash buffers. Returns 0 in case of failure. It is
55 * possible to call this function multiple times if the trash size changes.
56 */
Willy Tarreau47ca5452012-12-23 20:22:19 +010057int alloc_trash_buffers(int bufsize)
58{
59 trash_size = bufsize;
Willy Tarreau2819e992013-12-13 14:41:10 +010060 trash_buf1 = (char *)realloc(trash_buf1, bufsize);
61 trash_buf2 = (char *)realloc(trash_buf2, bufsize);
Willy Tarreau47ca5452012-12-23 20:22:19 +010062 return trash_buf1 && trash_buf2;
63}
64
Willy Tarreauc7e42382012-08-24 19:22:53 +020065/*
Willy Tarreau77804732012-10-29 16:14:26 +010066 * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of
67 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
68 * the new chunk size, or < 0 in case of failure.
69 */
70int chunk_printf(struct chunk *chk, const char *fmt, ...)
71{
72 va_list argp;
73 int ret;
74
75 if (!chk->str || !chk->size)
76 return 0;
77
78 va_start(argp, fmt);
79 ret = vsnprintf(chk->str, chk->size, fmt, argp);
80 va_end(argp);
81 chk->len = ret;
82
83 if (ret >= chk->size)
84 ret = -1;
85
86 chk->len = ret;
87 return chk->len;
88}
89
90/*
Willy Tarreauc7e42382012-08-24 19:22:53 +020091 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
92 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
93 * the new chunk size.
94 */
Willy Tarreau77804732012-10-29 16:14:26 +010095int chunk_appendf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +020096{
97 va_list argp;
98 int ret;
99
100 if (!chk->str || !chk->size)
101 return 0;
102
103 va_start(argp, fmt);
104 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
105 if (ret >= chk->size - chk->len)
106 /* do not copy anything in case of truncation */
107 chk->str[chk->len] = 0;
108 else
109 chk->len += ret;
110 va_end(argp);
111 return chk->len;
112}
113
114/*
115 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
116 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
117 * If the chk->len is over, nothing is added. Returns the new chunk size.
118 */
119int chunk_htmlencode(struct chunk *dst, struct chunk *src)
120{
121 int i, l;
122 int olen, free;
123 char c;
124
125 olen = dst->len;
126
127 for (i = 0; i < src->len; i++) {
128 free = dst->size - dst->len;
129
130 if (!free) {
131 dst->len = olen;
132 return dst->len;
133 }
134
135 c = src->str[i];
136
137 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
138 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
139
140 if (free < l) {
141 dst->len = olen;
142 return dst->len;
143 }
144
145 dst->len += l;
146 } else {
147 dst->str[dst->len] = c;
148 dst->len++;
149 }
150 }
151
152 return dst->len;
153}
154
155/*
156 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
157 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
158 * If the chk->len is over, nothing is added. Returns the new chunk size.
159 */
160int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc)
161{
162 int i, l;
163 int olen, free;
164 char c;
165
166 olen = dst->len;
167
168 for (i = 0; i < src->len; i++) {
169 free = dst->size - dst->len;
170
171 if (!free) {
172 dst->len = olen;
173 return dst->len;
174 }
175
176 c = src->str[i];
177
178 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
179 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
180
181 if (free < l) {
182 dst->len = olen;
183 return dst->len;
184 }
185
186 dst->len += l;
187 } else {
188 dst->str[dst->len] = c;
189 dst->len++;
190 }
191 }
192
193 return dst->len;
194}
195
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200196/* Compares the string in chunk <chk> with the string in <str> which must be
197 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
198 * to be null.
199 */
200int chunk_strcmp(const struct chunk *chk, const char *str)
201{
202 const char *s1 = chk->str;
203 int len = chk->len;
204 int diff = 0;
205
206 do {
207 if (--len < 0)
208 break;
209 diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
210 } while (!diff);
211 return diff;
212}
213
214/* Case-insensitively compares the string in chunk <chk> with the string in
215 * <str> which must be zero-terminated. Return is the same as with strcmp().
216 * Neither is allowed to be null.
217 */
218int chunk_strcasecmp(const struct chunk *chk, const char *str)
219{
220 const char *s1 = chk->str;
221 int len = chk->len;
222 int diff = 0;
223
224 do {
225 if (--len < 0)
226 break;
227 diff = (unsigned char)*s1 - (unsigned char)*str;
228 if (unlikely(diff)) {
229 unsigned int l = (unsigned char)*s1;
230 unsigned int r = (unsigned char)*str;
231
232 l -= 'a';
233 r -= 'a';
234
235 if (likely(l <= (unsigned char)'z' - 'a'))
236 l -= 'a' - 'A';
237 if (likely(r <= (unsigned char)'z' - 'a'))
238 r -= 'a' - 'A';
239 diff = l - r;
240 }
241 s1++; str++;
242 } while (!diff);
243 return diff;
244}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200245
246/*
247 * Local variables:
248 * c-indent-level: 8
249 * c-basic-offset: 8
250 * End:
251 */