blob: ebd194a2d3cf6a7b8bc5f5e67e4d43c34b4e867d [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
Willy Tarreau83061a82018-07-13 11:56:34 +020028#include <common/buf.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/config.h>
Willy Tarreaub686afd2017-02-08 11:06:11 +010030#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020031
32
Willy Tarreaubafbe012017-11-24 17:34:44 +010033struct pool_head *pool_head_trash;
Willy Tarreaub686afd2017-02-08 11:06:11 +010034
Willy Tarreauc7e42382012-08-24 19:22:53 +020035/* function prototypes */
36
Willy Tarreau83061a82018-07-13 11:56:34 +020037int chunk_printf(struct buffer *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +020038 __attribute__ ((format(printf, 2, 3)));
39
Willy Tarreau83061a82018-07-13 11:56:34 +020040int chunk_appendf(struct buffer *chk, const char *fmt, ...)
Willy Tarreau77804732012-10-29 16:14:26 +010041 __attribute__ ((format(printf, 2, 3)));
42
Willy Tarreau83061a82018-07-13 11:56:34 +020043int chunk_htmlencode(struct buffer *dst, struct buffer *src);
44int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc);
45int chunk_strcmp(const struct buffer *chk, const char *str);
46int chunk_strcasecmp(const struct buffer *chk, const char *str);
47struct buffer *get_trash_chunk(void);
48struct buffer *alloc_trash_chunk(void);
Christopher Fauletcd7879a2017-10-27 13:53:47 +020049int init_trash_buffers(int first);
Willy Tarreaub686afd2017-02-08 11:06:11 +010050
51/*
52 * free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL.
53 */
Willy Tarreau83061a82018-07-13 11:56:34 +020054static inline void free_trash_chunk(struct buffer *chunk)
Willy Tarreaub686afd2017-02-08 11:06:11 +010055{
Willy Tarreaubafbe012017-11-24 17:34:44 +010056 pool_free(pool_head_trash, chunk);
Willy Tarreaub686afd2017-02-08 11:06:11 +010057}
58
Willy Tarreauc7e42382012-08-24 19:22:53 +020059
Willy Tarreau83061a82018-07-13 11:56:34 +020060static inline void chunk_reset(struct buffer *chk)
Willy Tarreauc26ac9d2012-10-29 13:23:11 +010061{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020062 chk->data = 0;
Willy Tarreauc26ac9d2012-10-29 13:23:11 +010063}
64
Willy Tarreau83061a82018-07-13 11:56:34 +020065static inline void chunk_init(struct buffer *chk, char *str, size_t size)
Willy Tarreauc7e42382012-08-24 19:22:53 +020066{
Willy Tarreau83061a82018-07-13 11:56:34 +020067 chk->area = str;
68 chk->head = 0;
69 chk->data = 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +020070 chk->size = size;
71}
72
73/* report 0 in case of error, 1 if OK. */
Willy Tarreau83061a82018-07-13 11:56:34 +020074static inline int chunk_initlen(struct buffer *chk, char *str, size_t size,
75 int len)
Willy Tarreauc7e42382012-08-24 19:22:53 +020076{
77
Willy Tarreau320ec2a2016-02-25 16:15:19 +010078 if (len < 0 || (size && len > size))
Willy Tarreauc7e42382012-08-24 19:22:53 +020079 return 0;
80
Willy Tarreau83061a82018-07-13 11:56:34 +020081 chk->area = str;
82 chk->head = 0;
83 chk->data = len;
Willy Tarreauc7e42382012-08-24 19:22:53 +020084 chk->size = size;
85
86 return 1;
87}
88
Willy Tarreau70af6332016-01-06 20:45:03 +010089/* this is only for temporary manipulation, the chunk is read-only */
Willy Tarreau83061a82018-07-13 11:56:34 +020090static inline void chunk_initstr(struct buffer *chk, const char *str)
Willy Tarreauc7e42382012-08-24 19:22:53 +020091{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020092 chk->area = (char *)str;
Willy Tarreau83061a82018-07-13 11:56:34 +020093 chk->head = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020094 chk->data = strlen(str);
Willy Tarreauc7e42382012-08-24 19:22:53 +020095 chk->size = 0; /* mark it read-only */
96}
97
Willy Tarreauc1f40b32018-10-08 07:34:25 +020098/* copies chunk <src> into <chk>. Returns 0 in case of failure. */
99static inline int chunk_cpy(struct buffer *chk, const struct buffer *src)
100{
101 if (unlikely(src->data >= chk->size))
102 return 0;
103
104 chk->data = src->data;
105 memcpy(chk->area, src->area, src->data);
106 return 1;
107}
108
109/* appends chunk <src> after <chk>. Returns 0 in case of failure. */
110static inline int chunk_cat(struct buffer *chk, const struct buffer *src)
111{
112 if (unlikely(chk->data + src->data >= chk->size))
113 return 0;
114
115 memcpy(chk->area + chk->data, src->area, src->data);
116 chk->data += src->data;
117 return 1;
118}
119
Willy Tarreau82032f12017-07-27 13:35:34 +0200120/* copies memory area <src> into <chk> for <len> bytes. Returns 0 in
121 * case of failure. No trailing zero is added.
122 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200123static inline int chunk_memcpy(struct buffer *chk, const char *src,
124 size_t len)
Willy Tarreau82032f12017-07-27 13:35:34 +0200125{
126 if (unlikely(len >= chk->size))
127 return 0;
128
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200129 chk->data = len;
130 memcpy(chk->area, src, len);
Willy Tarreau82032f12017-07-27 13:35:34 +0200131
132 return 1;
133}
134
135/* appends memory area <src> after <chk> for <len> bytes. Returns 0 in
136 * case of failure. No trailing zero is added.
137 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200138static inline int chunk_memcat(struct buffer *chk, const char *src,
139 size_t len)
Willy Tarreau82032f12017-07-27 13:35:34 +0200140{
Willy Tarreaubba81562018-08-22 04:59:48 +0200141 if (unlikely(chk->data + len >= chk->size))
Willy Tarreau82032f12017-07-27 13:35:34 +0200142 return 0;
143
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200144 memcpy(chk->area + chk->data, src, len);
145 chk->data += len;
Willy Tarreau82032f12017-07-27 13:35:34 +0200146 return 1;
147}
148
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100149/* copies str into <chk> followed by a trailing zero. Returns 0 in
150 * case of failure.
151 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200152static inline int chunk_strcpy(struct buffer *chk, const char *str)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200153{
154 size_t len;
155
156 len = strlen(str);
157
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100158 if (unlikely(len >= chk->size))
Willy Tarreauc7e42382012-08-24 19:22:53 +0200159 return 0;
160
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200161 chk->data = len;
162 memcpy(chk->area, str, len + 1);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200163
164 return 1;
165}
166
Willy Tarreau601360b2016-01-04 20:13:55 +0100167/* appends str after <chk> followed by a trailing zero. Returns 0 in
168 * case of failure.
169 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200170static inline int chunk_strcat(struct buffer *chk, const char *str)
Willy Tarreau601360b2016-01-04 20:13:55 +0100171{
172 size_t len;
173
174 len = strlen(str);
175
Willy Tarreaubba81562018-08-22 04:59:48 +0200176 if (unlikely(chk->data + len >= chk->size))
Willy Tarreau601360b2016-01-04 20:13:55 +0100177 return 0;
178
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200179 memcpy(chk->area + chk->data, str, len + 1);
180 chk->data += len;
Willy Tarreau601360b2016-01-04 20:13:55 +0100181 return 1;
182}
183
Baptiste Assmann7819c122016-03-26 14:12:50 +0100184/* appends <nb> characters from str after <chk>.
185 * Returns 0 in case of failure.
186 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200187static inline int chunk_strncat(struct buffer *chk, const char *str, int nb)
Baptiste Assmann7819c122016-03-26 14:12:50 +0100188{
Willy Tarreaubba81562018-08-22 04:59:48 +0200189 if (unlikely(chk->data + nb >= chk->size))
Baptiste Assmann7819c122016-03-26 14:12:50 +0100190 return 0;
191
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200192 memcpy(chk->area + chk->data, str, nb);
193 chk->data += nb;
Baptiste Assmann7819c122016-03-26 14:12:50 +0100194 return 1;
195}
196
Willy Tarreau601360b2016-01-04 20:13:55 +0100197/* Adds a trailing zero to the current chunk and returns the pointer to the
198 * following part. The purpose is to be able to use a chunk as a series of
199 * short independant strings with chunk_* functions, which do not need to be
200 * released. Returns NULL if no space is available to ensure that the new
201 * string will have its own trailing zero. For example :
202 * chunk_init(&trash);
203 * pid = chunk_newstr(&trash);
204 * chunk_appendf(&trash, "%d", getpid()));
205 * name = chunk_newstr(&trash);
206 * chunk_appendf(&trash, "%s", gethosname());
207 * printf("hostname=<%s>, pid=<%d>\n", name, pid);
208 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200209static inline char *chunk_newstr(struct buffer *chk)
Willy Tarreau601360b2016-01-04 20:13:55 +0100210{
Willy Tarreaubba81562018-08-22 04:59:48 +0200211 if (chk->data + 1 >= chk->size)
Willy Tarreau601360b2016-01-04 20:13:55 +0100212 return NULL;
213
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200214 chk->area[chk->data++] = 0;
215 return chk->area + chk->data;
Willy Tarreau601360b2016-01-04 20:13:55 +0100216}
217
Willy Tarreau83061a82018-07-13 11:56:34 +0200218static inline void chunk_drop(struct buffer *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200219{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200220 chk->area = NULL;
221 chk->data = -1;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200222 chk->size = 0;
223}
224
Willy Tarreau83061a82018-07-13 11:56:34 +0200225static inline void chunk_destroy(struct buffer *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200226{
227 if (!chk->size)
228 return;
229
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200230 free(chk->area);
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100231 chunk_drop(chk);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200232}
233
234/*
235 * frees the destination chunk if already allocated, allocates a new string,
Willy Tarreauf9476a52016-01-04 20:36:59 +0100236 * and copies the source into it. The new chunk will have extra room for a
237 * trailing zero unless the source chunk was actually full. The pointer to
238 * the destination string is returned, or NULL if the allocation fails or if
239 * any pointer is NULL.
Willy Tarreauc7e42382012-08-24 19:22:53 +0200240 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200241static inline char *chunk_dup(struct buffer *dst, const struct buffer *src)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200242{
Willy Tarreaubba81562018-08-22 04:59:48 +0200243 if (!dst || !src || !src->area)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200244 return NULL;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100245
246 if (dst->size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200247 free(dst->area);
Willy Tarreau83061a82018-07-13 11:56:34 +0200248 dst->head = src->head;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200249 dst->data = src->data;
250 dst->size = src->data;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100251 if (dst->size < src->size || !src->size)
252 dst->size++;
253
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200254 dst->area = (char *)malloc(dst->size);
255 if (!dst->area) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200256 dst->head = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200257 dst->data = 0;
David Carlier8ab10432016-03-23 17:50:57 +0000258 dst->size = 0;
259 return NULL;
260 }
261
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200262 memcpy(dst->area, src->area, dst->data);
263 if (dst->data < dst->size)
264 dst->area[dst->data] = 0;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100265
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200266 return dst->area;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200267}
268
269#endif /* _TYPES_CHUNK_H */
270
271/*
272 * Local variables:
273 * c-indent-level: 8
274 * c-basic-offset: 8
275 * End:
276 */