blob: b9c1d2066f09e835e06e8604592abdd17c78127b [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 {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020034 char *area; /* points to <size> bytes */
35 size_t size; /* buffer size in bytes */
36 size_t data; /* amount of data after head including wrapping */
Willy Tarreauc7e42382012-08-24 19:22:53 +020037};
38
Willy Tarreaubafbe012017-11-24 17:34:44 +010039struct pool_head *pool_head_trash;
Willy Tarreaub686afd2017-02-08 11:06:11 +010040
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 +010053struct chunk *get_trash_chunk(void);
Willy Tarreaub686afd2017-02-08 11:06:11 +010054struct chunk *alloc_trash_chunk(void);
Christopher Fauletcd7879a2017-10-27 13:53:47 +020055int init_trash_buffers(int first);
Christopher Faulet748919a2017-07-26 14:59:46 +020056void deinit_trash_buffers(void);
Willy Tarreaub686afd2017-02-08 11:06:11 +010057
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{
Willy Tarreaubafbe012017-11-24 17:34:44 +010063 pool_free(pool_head_trash, chunk);
Willy Tarreaub686afd2017-02-08 11:06:11 +010064}
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{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020069 chk->data = 0;
Willy Tarreauc26ac9d2012-10-29 13:23:11 +010070}
71
Willy Tarreauc7e42382012-08-24 19:22:53 +020072static inline void chunk_init(struct chunk *chk, char *str, size_t size)
73{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020074 chk->area = str;
75 chk->data = 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +020076 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
Willy Tarreau843b7cb2018-07-13 10:54:26 +020086 chk->area = str;
87 chk->data = len;
Willy Tarreauc7e42382012-08-24 19:22:53 +020088 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 Tarreau843b7cb2018-07-13 10:54:26 +020096 chk->area = (char *)str;
97 chk->data = strlen(str);
Willy Tarreauc7e42382012-08-24 19:22:53 +020098 chk->size = 0; /* mark it read-only */
99}
100
Willy Tarreau82032f12017-07-27 13:35:34 +0200101/* copies memory area <src> into <chk> for <len> bytes. Returns 0 in
102 * case of failure. No trailing zero is added.
103 */
104static inline int chunk_memcpy(struct chunk *chk, const char *src, size_t len)
105{
106 if (unlikely(len >= chk->size))
107 return 0;
108
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200109 chk->data = len;
110 memcpy(chk->area, src, len);
Willy Tarreau82032f12017-07-27 13:35:34 +0200111
112 return 1;
113}
114
115/* appends memory area <src> after <chk> for <len> bytes. Returns 0 in
116 * case of failure. No trailing zero is added.
117 */
118static inline int chunk_memcat(struct chunk *chk, const char *src, size_t len)
119{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200120 if (unlikely(chk->data < 0 || 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 Tarreauc7e42382012-08-24 19:22:53 +0200131static inline int chunk_strcpy(struct chunk *chk, const char *str)
132{
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 */
149static inline int chunk_strcat(struct chunk *chk, const char *str)
150{
151 size_t len;
152
153 len = strlen(str);
154
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200155 if (unlikely(chk->data < 0 || 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 */
166static inline int chunk_strncat(struct chunk *chk, const char *str, int nb)
167{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200168 if (unlikely(chk->data < 0 || 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 */
188static inline char *chunk_newstr(struct chunk *chk)
189{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200190 if (chk->data < 0 || 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 Tarreauc26ac9d2012-10-29 13:23:11 +0100197static inline void chunk_drop(struct chunk *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
204static inline void chunk_destroy(struct chunk *chk)
205{
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 */
220static inline char *chunk_dup(struct chunk *dst, const struct chunk *src)
221{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200222 if (!dst || !src || src->data < 0 || !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);
227 dst->data = src->data;
228 dst->size = src->data;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100229 if (dst->size < src->size || !src->size)
230 dst->size++;
231
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200232 dst->area = (char *)malloc(dst->size);
233 if (!dst->area) {
234 dst->data = 0;
David Carlier8ab10432016-03-23 17:50:57 +0000235 dst->size = 0;
236 return NULL;
237 }
238
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200239 memcpy(dst->area, src->area, dst->data);
240 if (dst->data < dst->size)
241 dst->area[dst->data] = 0;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100242
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200243 return dst->area;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200244}
245
246#endif /* _TYPES_CHUNK_H */
247
248/*
249 * Local variables:
250 * c-indent-level: 8
251 * c-basic-offset: 8
252 * End:
253 */