blob: 8c067f7dafa321c5d452daa29235fbabbc77af7e [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
2 * include/common/chunk.h
3 * Chunk management definitions, macros and inline functions.
4 *
5 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _TYPES_CHUNK_H
23#define _TYPES_CHUNK_H
24
25#include <stdlib.h>
26#include <string.h>
27
28#include <common/config.h>
Willy Tarreaub686afd2017-02-08 11:06:11 +010029#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030
31
32/* describes a chunk of string */
33struct chunk {
34 char *str; /* beginning of the string itself. Might not be 0-terminated */
35 int size; /* total size of the buffer, 0 if the *str is read-only */
36 int len; /* current size of the string from first to last char. <0 = uninit. */
37};
38
Willy Tarreaub686afd2017-02-08 11:06:11 +010039struct pool_head *pool2_trash;
40
Willy Tarreauc7e42382012-08-24 19:22:53 +020041/* function prototypes */
42
43int chunk_printf(struct chunk *chk, const char *fmt, ...)
44 __attribute__ ((format(printf, 2, 3)));
45
Willy Tarreau77804732012-10-29 16:14:26 +010046int chunk_appendf(struct chunk *chk, const char *fmt, ...)
47 __attribute__ ((format(printf, 2, 3)));
48
Willy Tarreauc7e42382012-08-24 19:22:53 +020049int chunk_htmlencode(struct chunk *dst, struct chunk *src);
50int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
Willy Tarreauad8f8e82012-10-19 15:18:06 +020051int chunk_strcmp(const struct chunk *chk, const char *str);
52int chunk_strcasecmp(const struct chunk *chk, const char *str);
Willy Tarreau47ca5452012-12-23 20:22:19 +010053int alloc_trash_buffers(int bufsize);
David Carlier60deeba2015-09-25 11:58:12 +010054void free_trash_buffers(void);
Willy Tarreau47ca5452012-12-23 20:22:19 +010055struct chunk *get_trash_chunk(void);
Willy Tarreaub686afd2017-02-08 11:06:11 +010056struct chunk *alloc_trash_chunk(void);
57
58/*
59 * free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL.
60 */
61static inline void free_trash_chunk(struct chunk *chunk)
62{
63 pool_free2(pool2_trash, chunk);
64}
65
Willy Tarreauc7e42382012-08-24 19:22:53 +020066
Willy Tarreauc26ac9d2012-10-29 13:23:11 +010067static inline void chunk_reset(struct chunk *chk)
68{
69 chk->len = 0;
70}
71
Willy Tarreauc7e42382012-08-24 19:22:53 +020072static inline void chunk_init(struct chunk *chk, char *str, size_t size)
73{
74 chk->str = str;
75 chk->len = 0;
76 chk->size = size;
77}
78
79/* report 0 in case of error, 1 if OK. */
80static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len)
81{
82
Willy Tarreau320ec2a2016-02-25 16:15:19 +010083 if (len < 0 || (size && len > size))
Willy Tarreauc7e42382012-08-24 19:22:53 +020084 return 0;
85
86 chk->str = str;
87 chk->len = len;
88 chk->size = size;
89
90 return 1;
91}
92
Willy Tarreau70af6332016-01-06 20:45:03 +010093/* this is only for temporary manipulation, the chunk is read-only */
94static inline void chunk_initstr(struct chunk *chk, const char *str)
Willy Tarreauc7e42382012-08-24 19:22:53 +020095{
Willy Tarreau70af6332016-01-06 20:45:03 +010096 chk->str = (char *)str;
Willy Tarreauc7e42382012-08-24 19:22:53 +020097 chk->len = strlen(str);
98 chk->size = 0; /* mark it read-only */
99}
100
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100101/* copies str into <chk> followed by a trailing zero. Returns 0 in
102 * case of failure.
103 */
Willy Tarreauc7e42382012-08-24 19:22:53 +0200104static inline int chunk_strcpy(struct chunk *chk, const char *str)
105{
106 size_t len;
107
108 len = strlen(str);
109
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100110 if (unlikely(len >= chk->size))
Willy Tarreauc7e42382012-08-24 19:22:53 +0200111 return 0;
112
113 chk->len = len;
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100114 memcpy(chk->str, str, len + 1);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200115
116 return 1;
117}
118
Willy Tarreau601360b2016-01-04 20:13:55 +0100119/* appends str after <chk> followed by a trailing zero. Returns 0 in
120 * case of failure.
121 */
122static inline int chunk_strcat(struct chunk *chk, const char *str)
123{
124 size_t len;
125
126 len = strlen(str);
127
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100128 if (unlikely(chk->len < 0 || chk->len + len >= chk->size))
Willy Tarreau601360b2016-01-04 20:13:55 +0100129 return 0;
130
131 memcpy(chk->str + chk->len, str, len + 1);
132 chk->len += len;
133 return 1;
134}
135
Baptiste Assmann7819c122016-03-26 14:12:50 +0100136/* appends <nb> characters from str after <chk>.
137 * Returns 0 in case of failure.
138 */
139static inline int chunk_strncat(struct chunk *chk, const char *str, int nb)
140{
141 if (unlikely(chk->len < 0 || chk->len + nb >= chk->size))
142 return 0;
143
144 memcpy(chk->str + chk->len, str, nb);
145 chk->len += nb;
146 return 1;
147}
148
Willy Tarreau601360b2016-01-04 20:13:55 +0100149/* Adds a trailing zero to the current chunk and returns the pointer to the
150 * following part. The purpose is to be able to use a chunk as a series of
151 * short independant strings with chunk_* functions, which do not need to be
152 * released. Returns NULL if no space is available to ensure that the new
153 * string will have its own trailing zero. For example :
154 * chunk_init(&trash);
155 * pid = chunk_newstr(&trash);
156 * chunk_appendf(&trash, "%d", getpid()));
157 * name = chunk_newstr(&trash);
158 * chunk_appendf(&trash, "%s", gethosname());
159 * printf("hostname=<%s>, pid=<%d>\n", name, pid);
160 */
161static inline char *chunk_newstr(struct chunk *chk)
162{
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100163 if (chk->len < 0 || chk->len + 1 >= chk->size)
Willy Tarreau601360b2016-01-04 20:13:55 +0100164 return NULL;
165
166 chk->str[chk->len++] = 0;
167 return chk->str + chk->len;
168}
169
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100170static inline void chunk_drop(struct chunk *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200171{
172 chk->str = NULL;
173 chk->len = -1;
174 chk->size = 0;
175}
176
177static inline void chunk_destroy(struct chunk *chk)
178{
179 if (!chk->size)
180 return;
181
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100182 free(chk->str);
183 chunk_drop(chk);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200184}
185
186/*
187 * frees the destination chunk if already allocated, allocates a new string,
Willy Tarreauf9476a52016-01-04 20:36:59 +0100188 * and copies the source into it. The new chunk will have extra room for a
189 * trailing zero unless the source chunk was actually full. The pointer to
190 * the destination string is returned, or NULL if the allocation fails or if
191 * any pointer is NULL.
Willy Tarreauc7e42382012-08-24 19:22:53 +0200192 */
193static inline char *chunk_dup(struct chunk *dst, const struct chunk *src)
194{
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100195 if (!dst || !src || src->len < 0 || !src->str)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200196 return NULL;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100197
198 if (dst->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200199 free(dst->str);
200 dst->len = src->len;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100201 dst->size = src->len;
202 if (dst->size < src->size || !src->size)
203 dst->size++;
204
205 dst->str = (char *)malloc(dst->size);
David Carlier8ab10432016-03-23 17:50:57 +0000206 if (!dst->str) {
207 dst->len = 0;
208 dst->size = 0;
209 return NULL;
210 }
211
Willy Tarreauc7e42382012-08-24 19:22:53 +0200212 memcpy(dst->str, src->str, dst->len);
Willy Tarreauf9476a52016-01-04 20:36:59 +0100213 if (dst->len < dst->size)
214 dst->str[dst->len] = 0;
215
Willy Tarreauc7e42382012-08-24 19:22:53 +0200216 return dst->str;
217}
218
219#endif /* _TYPES_CHUNK_H */
220
221/*
222 * Local variables:
223 * c-indent-level: 8
224 * c-basic-offset: 8
225 * End:
226 */