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