blob: d027569b4300b4aa97cdd0595dc65f95a818ee61 [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
21/*
Willy Tarreau77804732012-10-29 16:14:26 +010022 * Does an snprintf() at the beginning of chunk <chk>, respecting the limit of
23 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
24 * the new chunk size, or < 0 in case of failure.
25 */
26int chunk_printf(struct chunk *chk, const char *fmt, ...)
27{
28 va_list argp;
29 int ret;
30
31 if (!chk->str || !chk->size)
32 return 0;
33
34 va_start(argp, fmt);
35 ret = vsnprintf(chk->str, chk->size, fmt, argp);
36 va_end(argp);
37 chk->len = ret;
38
39 if (ret >= chk->size)
40 ret = -1;
41
42 chk->len = ret;
43 return chk->len;
44}
45
46/*
Willy Tarreauc7e42382012-08-24 19:22:53 +020047 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
48 * at most chk->size chars. If the chk->len is over, nothing is added. Returns
49 * the new chunk size.
50 */
Willy Tarreau77804732012-10-29 16:14:26 +010051int chunk_appendf(struct chunk *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +020052{
53 va_list argp;
54 int ret;
55
56 if (!chk->str || !chk->size)
57 return 0;
58
59 va_start(argp, fmt);
60 ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
61 if (ret >= chk->size - chk->len)
62 /* do not copy anything in case of truncation */
63 chk->str[chk->len] = 0;
64 else
65 chk->len += ret;
66 va_end(argp);
67 return chk->len;
68}
69
70/*
71 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
72 * chk->size chars. Replace non-printable or special chracters with "&#%d;".
73 * If the chk->len is over, nothing is added. Returns the new chunk size.
74 */
75int chunk_htmlencode(struct chunk *dst, struct chunk *src)
76{
77 int i, l;
78 int olen, free;
79 char c;
80
81 olen = dst->len;
82
83 for (i = 0; i < src->len; i++) {
84 free = dst->size - dst->len;
85
86 if (!free) {
87 dst->len = olen;
88 return dst->len;
89 }
90
91 c = src->str[i];
92
93 if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
94 l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
95
96 if (free < l) {
97 dst->len = olen;
98 return dst->len;
99 }
100
101 dst->len += l;
102 } else {
103 dst->str[dst->len] = c;
104 dst->len++;
105 }
106 }
107
108 return dst->len;
109}
110
111/*
112 * Encode chunk <src> into chunk <dst>, respecting the limit of at most
113 * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
114 * If the chk->len is over, nothing is added. Returns the new chunk size.
115 */
116int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc)
117{
118 int i, l;
119 int olen, free;
120 char c;
121
122 olen = dst->len;
123
124 for (i = 0; i < src->len; i++) {
125 free = dst->size - dst->len;
126
127 if (!free) {
128 dst->len = olen;
129 return dst->len;
130 }
131
132 c = src->str[i];
133
134 if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
135 l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
136
137 if (free < l) {
138 dst->len = olen;
139 return dst->len;
140 }
141
142 dst->len += l;
143 } else {
144 dst->str[dst->len] = c;
145 dst->len++;
146 }
147 }
148
149 return dst->len;
150}
151
Willy Tarreauad8f8e82012-10-19 15:18:06 +0200152/* Compares the string in chunk <chk> with the string in <str> which must be
153 * zero-terminated. Return is the same as with strcmp(). Neither is allowed
154 * to be null.
155 */
156int chunk_strcmp(const struct chunk *chk, const char *str)
157{
158 const char *s1 = chk->str;
159 int len = chk->len;
160 int diff = 0;
161
162 do {
163 if (--len < 0)
164 break;
165 diff = (unsigned char)*(s1++) - (unsigned char)*(str++);
166 } while (!diff);
167 return diff;
168}
169
170/* Case-insensitively compares the string in chunk <chk> with the string in
171 * <str> which must be zero-terminated. Return is the same as with strcmp().
172 * Neither is allowed to be null.
173 */
174int chunk_strcasecmp(const struct chunk *chk, const char *str)
175{
176 const char *s1 = chk->str;
177 int len = chk->len;
178 int diff = 0;
179
180 do {
181 if (--len < 0)
182 break;
183 diff = (unsigned char)*s1 - (unsigned char)*str;
184 if (unlikely(diff)) {
185 unsigned int l = (unsigned char)*s1;
186 unsigned int r = (unsigned char)*str;
187
188 l -= 'a';
189 r -= 'a';
190
191 if (likely(l <= (unsigned char)'z' - 'a'))
192 l -= 'a' - 'A';
193 if (likely(r <= (unsigned char)'z' - 'a'))
194 r -= 'a' - 'A';
195 diff = l - r;
196 }
197 s1++; str++;
198 } while (!diff);
199 return diff;
200}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200201
202/*
203 * Local variables:
204 * c-indent-level: 8
205 * c-basic-offset: 8
206 * End:
207 */