blob: a4cbb7d2cf17b3cc9a76149abfb5658963b8e9fe [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
127
128/*
129 * Local variables:
130 * c-indent-level: 8
131 * c-basic-offset: 8
132 * End:
133 */