blob: 383e15736f18629781442435eef4b1762fef6349 [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);
Christopher Faulet748919a2017-07-26 14:59:46 +020050void deinit_trash_buffers(void);
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 Tarreau82032f12017-07-27 13:35:34 +020099/* copies memory area <src> into <chk> for <len> bytes. Returns 0 in
100 * case of failure. No trailing zero is added.
101 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200102static inline int chunk_memcpy(struct buffer *chk, const char *src,
103 size_t len)
Willy Tarreau82032f12017-07-27 13:35:34 +0200104{
105 if (unlikely(len >= chk->size))
106 return 0;
107
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200108 chk->data = len;
109 memcpy(chk->area, src, len);
Willy Tarreau82032f12017-07-27 13:35:34 +0200110
111 return 1;
112}
113
114/* appends memory area <src> after <chk> for <len> bytes. Returns 0 in
115 * case of failure. No trailing zero is added.
116 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200117static inline int chunk_memcat(struct buffer *chk, const char *src,
118 size_t len)
Willy Tarreau82032f12017-07-27 13:35:34 +0200119{
Willy Tarreaubba81562018-08-22 04:59:48 +0200120 if (unlikely(chk->data + len >= chk->size))
Willy Tarreau82032f12017-07-27 13:35:34 +0200121 return 0;
122
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200123 memcpy(chk->area + chk->data, src, len);
124 chk->data += len;
Willy Tarreau82032f12017-07-27 13:35:34 +0200125 return 1;
126}
127
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100128/* copies str into <chk> followed by a trailing zero. Returns 0 in
129 * case of failure.
130 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200131static inline int chunk_strcpy(struct buffer *chk, const char *str)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200132{
133 size_t len;
134
135 len = strlen(str);
136
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100137 if (unlikely(len >= chk->size))
Willy Tarreauc7e42382012-08-24 19:22:53 +0200138 return 0;
139
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200140 chk->data = len;
141 memcpy(chk->area, str, len + 1);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200142
143 return 1;
144}
145
Willy Tarreau601360b2016-01-04 20:13:55 +0100146/* appends str after <chk> followed by a trailing zero. Returns 0 in
147 * case of failure.
148 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200149static inline int chunk_strcat(struct buffer *chk, const char *str)
Willy Tarreau601360b2016-01-04 20:13:55 +0100150{
151 size_t len;
152
153 len = strlen(str);
154
Willy Tarreaubba81562018-08-22 04:59:48 +0200155 if (unlikely(chk->data + len >= chk->size))
Willy Tarreau601360b2016-01-04 20:13:55 +0100156 return 0;
157
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200158 memcpy(chk->area + chk->data, str, len + 1);
159 chk->data += len;
Willy Tarreau601360b2016-01-04 20:13:55 +0100160 return 1;
161}
162
Baptiste Assmann7819c122016-03-26 14:12:50 +0100163/* appends <nb> characters from str after <chk>.
164 * Returns 0 in case of failure.
165 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200166static inline int chunk_strncat(struct buffer *chk, const char *str, int nb)
Baptiste Assmann7819c122016-03-26 14:12:50 +0100167{
Willy Tarreaubba81562018-08-22 04:59:48 +0200168 if (unlikely(chk->data + nb >= chk->size))
Baptiste Assmann7819c122016-03-26 14:12:50 +0100169 return 0;
170
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200171 memcpy(chk->area + chk->data, str, nb);
172 chk->data += nb;
Baptiste Assmann7819c122016-03-26 14:12:50 +0100173 return 1;
174}
175
Willy Tarreau601360b2016-01-04 20:13:55 +0100176/* Adds a trailing zero to the current chunk and returns the pointer to the
177 * following part. The purpose is to be able to use a chunk as a series of
178 * short independant strings with chunk_* functions, which do not need to be
179 * released. Returns NULL if no space is available to ensure that the new
180 * string will have its own trailing zero. For example :
181 * chunk_init(&trash);
182 * pid = chunk_newstr(&trash);
183 * chunk_appendf(&trash, "%d", getpid()));
184 * name = chunk_newstr(&trash);
185 * chunk_appendf(&trash, "%s", gethosname());
186 * printf("hostname=<%s>, pid=<%d>\n", name, pid);
187 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200188static inline char *chunk_newstr(struct buffer *chk)
Willy Tarreau601360b2016-01-04 20:13:55 +0100189{
Willy Tarreaubba81562018-08-22 04:59:48 +0200190 if (chk->data + 1 >= chk->size)
Willy Tarreau601360b2016-01-04 20:13:55 +0100191 return NULL;
192
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200193 chk->area[chk->data++] = 0;
194 return chk->area + chk->data;
Willy Tarreau601360b2016-01-04 20:13:55 +0100195}
196
Willy Tarreau83061a82018-07-13 11:56:34 +0200197static inline void chunk_drop(struct buffer *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200198{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200199 chk->area = NULL;
200 chk->data = -1;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200201 chk->size = 0;
202}
203
Willy Tarreau83061a82018-07-13 11:56:34 +0200204static inline void chunk_destroy(struct buffer *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200205{
206 if (!chk->size)
207 return;
208
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200209 free(chk->area);
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100210 chunk_drop(chk);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200211}
212
213/*
214 * frees the destination chunk if already allocated, allocates a new string,
Willy Tarreauf9476a52016-01-04 20:36:59 +0100215 * and copies the source into it. The new chunk will have extra room for a
216 * trailing zero unless the source chunk was actually full. The pointer to
217 * the destination string is returned, or NULL if the allocation fails or if
218 * any pointer is NULL.
Willy Tarreauc7e42382012-08-24 19:22:53 +0200219 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200220static inline char *chunk_dup(struct buffer *dst, const struct buffer *src)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200221{
Willy Tarreaubba81562018-08-22 04:59:48 +0200222 if (!dst || !src || !src->area)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200223 return NULL;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100224
225 if (dst->size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200226 free(dst->area);
Willy Tarreau83061a82018-07-13 11:56:34 +0200227 dst->head = src->head;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200228 dst->data = src->data;
229 dst->size = src->data;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100230 if (dst->size < src->size || !src->size)
231 dst->size++;
232
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200233 dst->area = (char *)malloc(dst->size);
234 if (!dst->area) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200235 dst->head = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200236 dst->data = 0;
David Carlier8ab10432016-03-23 17:50:57 +0000237 dst->size = 0;
238 return NULL;
239 }
240
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200241 memcpy(dst->area, src->area, dst->data);
242 if (dst->data < dst->size)
243 dst->area[dst->data] = 0;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100244
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200245 return dst->area;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200246}
247
248#endif /* _TYPES_CHUNK_H */
249
250/*
251 * Local variables:
252 * c-indent-level: 8
253 * c-basic-offset: 8
254 * End:
255 */