blob: ae7ae853842d9971c04d70867b481468ee03a6f8 [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 Tarreau4c7e4b72020-05-27 12:58:42 +020028#include <haproxy/api.h>
Willy Tarreau83061a82018-07-13 11:56:34 +020029#include <common/buf.h>
Willy Tarreau20020ae2019-10-29 13:02:15 +010030#include <common/ist.h>
Willy Tarreaub686afd2017-02-08 11:06:11 +010031#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020032
33
Willy Tarreau72d9f332019-03-29 18:13:36 +010034extern struct pool_head *pool_head_trash;
Willy Tarreaub686afd2017-02-08 11:06:11 +010035
Willy Tarreauc7e42382012-08-24 19:22:53 +020036/* function prototypes */
37
Willy Tarreau83061a82018-07-13 11:56:34 +020038int chunk_printf(struct buffer *chk, const char *fmt, ...)
Willy Tarreauc7e42382012-08-24 19:22:53 +020039 __attribute__ ((format(printf, 2, 3)));
40
Willy Tarreau83061a82018-07-13 11:56:34 +020041int chunk_appendf(struct buffer *chk, const char *fmt, ...)
Willy Tarreau77804732012-10-29 16:14:26 +010042 __attribute__ ((format(printf, 2, 3)));
43
Willy Tarreau83061a82018-07-13 11:56:34 +020044int chunk_htmlencode(struct buffer *dst, struct buffer *src);
45int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc);
46int chunk_strcmp(const struct buffer *chk, const char *str);
47int chunk_strcasecmp(const struct buffer *chk, const char *str);
48struct buffer *get_trash_chunk(void);
49struct buffer *alloc_trash_chunk(void);
Christopher Fauletcd7879a2017-10-27 13:53:47 +020050int init_trash_buffers(int first);
Willy Tarreaub686afd2017-02-08 11:06:11 +010051
52/*
53 * free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL.
54 */
Willy Tarreau83061a82018-07-13 11:56:34 +020055static inline void free_trash_chunk(struct buffer *chunk)
Willy Tarreaub686afd2017-02-08 11:06:11 +010056{
Willy Tarreaubafbe012017-11-24 17:34:44 +010057 pool_free(pool_head_trash, chunk);
Willy Tarreaub686afd2017-02-08 11:06:11 +010058}
59
Willy Tarreauc7e42382012-08-24 19:22:53 +020060
Willy Tarreau83061a82018-07-13 11:56:34 +020061static inline void chunk_reset(struct buffer *chk)
Willy Tarreauc26ac9d2012-10-29 13:23:11 +010062{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020063 chk->data = 0;
Willy Tarreauc26ac9d2012-10-29 13:23:11 +010064}
65
Willy Tarreau83061a82018-07-13 11:56:34 +020066static inline void chunk_init(struct buffer *chk, char *str, size_t size)
Willy Tarreauc7e42382012-08-24 19:22:53 +020067{
Willy Tarreau83061a82018-07-13 11:56:34 +020068 chk->area = str;
69 chk->head = 0;
70 chk->data = 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +020071 chk->size = size;
72}
73
74/* report 0 in case of error, 1 if OK. */
Willy Tarreau83061a82018-07-13 11:56:34 +020075static inline int chunk_initlen(struct buffer *chk, char *str, size_t size,
76 int len)
Willy Tarreauc7e42382012-08-24 19:22:53 +020077{
78
Willy Tarreau320ec2a2016-02-25 16:15:19 +010079 if (len < 0 || (size && len > size))
Willy Tarreauc7e42382012-08-24 19:22:53 +020080 return 0;
81
Willy Tarreau83061a82018-07-13 11:56:34 +020082 chk->area = str;
83 chk->head = 0;
84 chk->data = len;
Willy Tarreauc7e42382012-08-24 19:22:53 +020085 chk->size = size;
86
87 return 1;
88}
89
Willy Tarreau70af6332016-01-06 20:45:03 +010090/* this is only for temporary manipulation, the chunk is read-only */
Willy Tarreau83061a82018-07-13 11:56:34 +020091static inline void chunk_initstr(struct buffer *chk, const char *str)
Willy Tarreauc7e42382012-08-24 19:22:53 +020092{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020093 chk->area = (char *)str;
Willy Tarreau83061a82018-07-13 11:56:34 +020094 chk->head = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020095 chk->data = strlen(str);
Willy Tarreauc7e42382012-08-24 19:22:53 +020096 chk->size = 0; /* mark it read-only */
97}
98
Willy Tarreauc1f40b32018-10-08 07:34:25 +020099/* copies chunk <src> into <chk>. Returns 0 in case of failure. */
100static inline int chunk_cpy(struct buffer *chk, const struct buffer *src)
101{
Christopher Faulet48fa0332019-10-14 11:29:48 +0200102 if (unlikely(src->data > chk->size))
Willy Tarreauc1f40b32018-10-08 07:34:25 +0200103 return 0;
104
105 chk->data = src->data;
106 memcpy(chk->area, src->area, src->data);
107 return 1;
108}
109
110/* appends chunk <src> after <chk>. Returns 0 in case of failure. */
111static inline int chunk_cat(struct buffer *chk, const struct buffer *src)
112{
Christopher Faulet48fa0332019-10-14 11:29:48 +0200113 if (unlikely(chk->data + src->data > chk->size))
Willy Tarreauc1f40b32018-10-08 07:34:25 +0200114 return 0;
115
116 memcpy(chk->area + chk->data, src->area, src->data);
117 chk->data += src->data;
118 return 1;
119}
120
Willy Tarreau20020ae2019-10-29 13:02:15 +0100121/* appends ist <src> after <chk>. Returns 0 in case of failure. */
122static inline int chunk_istcat(struct buffer *chk, const struct ist src)
123{
124 if (unlikely(chk->data + src.len > chk->size))
125 return 0;
126
127 memcpy(chk->area + chk->data, src.ptr, src.len);
128 chk->data += src.len;
129 return 1;
130}
131
Willy Tarreau82032f12017-07-27 13:35:34 +0200132/* copies memory area <src> into <chk> for <len> bytes. Returns 0 in
133 * case of failure. No trailing zero is added.
134 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200135static inline int chunk_memcpy(struct buffer *chk, const char *src,
136 size_t len)
Willy Tarreau82032f12017-07-27 13:35:34 +0200137{
Christopher Faulet48fa0332019-10-14 11:29:48 +0200138 if (unlikely(len > chk->size))
Willy Tarreau82032f12017-07-27 13:35:34 +0200139 return 0;
140
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200141 chk->data = len;
142 memcpy(chk->area, src, len);
Willy Tarreau82032f12017-07-27 13:35:34 +0200143
144 return 1;
145}
146
147/* appends memory area <src> after <chk> for <len> bytes. Returns 0 in
148 * case of failure. No trailing zero is added.
149 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200150static inline int chunk_memcat(struct buffer *chk, const char *src,
151 size_t len)
Willy Tarreau82032f12017-07-27 13:35:34 +0200152{
Christopher Faulet48fa0332019-10-14 11:29:48 +0200153 if (unlikely(chk->data + len > chk->size))
Willy Tarreau82032f12017-07-27 13:35:34 +0200154 return 0;
155
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200156 memcpy(chk->area + chk->data, src, len);
157 chk->data += len;
Willy Tarreau82032f12017-07-27 13:35:34 +0200158 return 1;
159}
160
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100161/* copies str into <chk> followed by a trailing zero. Returns 0 in
162 * case of failure.
163 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200164static inline int chunk_strcpy(struct buffer *chk, const char *str)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200165{
166 size_t len;
167
168 len = strlen(str);
169
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100170 if (unlikely(len >= chk->size))
Willy Tarreauc7e42382012-08-24 19:22:53 +0200171 return 0;
172
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200173 chk->data = len;
174 memcpy(chk->area, str, len + 1);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200175
176 return 1;
177}
178
Willy Tarreaud4ad6692020-02-14 11:31:41 +0100179/* copies at most <max> chars from str into <chk> followed by a trailing zero.
180 * Returns 0 in case of failure.
181 */
182static inline int chunk_strncpy(struct buffer *chk, const char *str, size_t max)
183{
184 size_t len;
185
186 len = strlen(str);
187 if (len > max)
188 len = max;
189
190 if (unlikely(len >= chk->size))
191 return 0;
192
193 memcpy(chk->area, str, len);
194 chk->area[len] = 0;
195 chk->data = len;
196 return 1;
197}
198
Willy Tarreau601360b2016-01-04 20:13:55 +0100199/* appends str after <chk> followed by a trailing zero. Returns 0 in
200 * case of failure.
201 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200202static inline int chunk_strcat(struct buffer *chk, const char *str)
Willy Tarreau601360b2016-01-04 20:13:55 +0100203{
204 size_t len;
205
206 len = strlen(str);
207
Willy Tarreaubba81562018-08-22 04:59:48 +0200208 if (unlikely(chk->data + len >= chk->size))
Willy Tarreau601360b2016-01-04 20:13:55 +0100209 return 0;
210
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200211 memcpy(chk->area + chk->data, str, len + 1);
212 chk->data += len;
Willy Tarreau601360b2016-01-04 20:13:55 +0100213 return 1;
214}
215
Baptiste Assmann7819c122016-03-26 14:12:50 +0100216/* appends <nb> characters from str after <chk>.
217 * Returns 0 in case of failure.
218 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200219static inline int chunk_strncat(struct buffer *chk, const char *str, int nb)
Baptiste Assmann7819c122016-03-26 14:12:50 +0100220{
Willy Tarreaubba81562018-08-22 04:59:48 +0200221 if (unlikely(chk->data + nb >= chk->size))
Baptiste Assmann7819c122016-03-26 14:12:50 +0100222 return 0;
223
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200224 memcpy(chk->area + chk->data, str, nb);
225 chk->data += nb;
Baptiste Assmann7819c122016-03-26 14:12:50 +0100226 return 1;
227}
228
Willy Tarreau601360b2016-01-04 20:13:55 +0100229/* Adds a trailing zero to the current chunk and returns the pointer to the
230 * following part. The purpose is to be able to use a chunk as a series of
Joseph Herlantbe7619a2018-11-25 10:49:51 -0800231 * short independent strings with chunk_* functions, which do not need to be
Willy Tarreau601360b2016-01-04 20:13:55 +0100232 * released. Returns NULL if no space is available to ensure that the new
233 * string will have its own trailing zero. For example :
234 * chunk_init(&trash);
235 * pid = chunk_newstr(&trash);
236 * chunk_appendf(&trash, "%d", getpid()));
237 * name = chunk_newstr(&trash);
238 * chunk_appendf(&trash, "%s", gethosname());
239 * printf("hostname=<%s>, pid=<%d>\n", name, pid);
240 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200241static inline char *chunk_newstr(struct buffer *chk)
Willy Tarreau601360b2016-01-04 20:13:55 +0100242{
Willy Tarreaubba81562018-08-22 04:59:48 +0200243 if (chk->data + 1 >= chk->size)
Willy Tarreau601360b2016-01-04 20:13:55 +0100244 return NULL;
245
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200246 chk->area[chk->data++] = 0;
247 return chk->area + chk->data;
Willy Tarreau601360b2016-01-04 20:13:55 +0100248}
249
Willy Tarreau83061a82018-07-13 11:56:34 +0200250static inline void chunk_drop(struct buffer *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200251{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200252 chk->area = NULL;
253 chk->data = -1;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200254 chk->size = 0;
255}
256
Willy Tarreau83061a82018-07-13 11:56:34 +0200257static inline void chunk_destroy(struct buffer *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200258{
259 if (!chk->size)
260 return;
261
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200262 free(chk->area);
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100263 chunk_drop(chk);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200264}
265
266/*
267 * frees the destination chunk if already allocated, allocates a new string,
Willy Tarreauf9476a52016-01-04 20:36:59 +0100268 * and copies the source into it. The new chunk will have extra room for a
269 * trailing zero unless the source chunk was actually full. The pointer to
270 * the destination string is returned, or NULL if the allocation fails or if
271 * any pointer is NULL.
Willy Tarreauc7e42382012-08-24 19:22:53 +0200272 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200273static inline char *chunk_dup(struct buffer *dst, const struct buffer *src)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200274{
Willy Tarreaubba81562018-08-22 04:59:48 +0200275 if (!dst || !src || !src->area)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200276 return NULL;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100277
278 if (dst->size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200279 free(dst->area);
Willy Tarreau83061a82018-07-13 11:56:34 +0200280 dst->head = src->head;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200281 dst->data = src->data;
282 dst->size = src->data;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100283 if (dst->size < src->size || !src->size)
284 dst->size++;
285
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200286 dst->area = (char *)malloc(dst->size);
287 if (!dst->area) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200288 dst->head = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200289 dst->data = 0;
David Carlier8ab10432016-03-23 17:50:57 +0000290 dst->size = 0;
291 return NULL;
292 }
293
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200294 memcpy(dst->area, src->area, dst->data);
295 if (dst->data < dst->size)
296 dst->area[dst->data] = 0;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100297
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200298 return dst->area;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200299}
300
301#endif /* _TYPES_CHUNK_H */
302
303/*
304 * Local variables:
305 * c-indent-level: 8
306 * c-basic-offset: 8
307 * End:
308 */