blob: 82667733a952b06dcf4d21d3d825d15a8da8cfce [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 +010053struct chunk *get_trash_chunk(void);
Willy Tarreaub686afd2017-02-08 11:06:11 +010054struct chunk *alloc_trash_chunk(void);
Christopher Faulet748919a2017-07-26 14:59:46 +020055int init_trash_buffers(void);
56void 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{
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 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
109 chk->len = len;
110 memcpy(chk->str, src, len);
111
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{
120 if (unlikely(chk->len < 0 || chk->len + len >= chk->size))
121 return 0;
122
123 memcpy(chk->str + chk->len, src, len);
124 chk->len += len;
125 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
140 chk->len = len;
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100141 memcpy(chk->str, 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 Tarreau320ec2a2016-02-25 16:15:19 +0100155 if (unlikely(chk->len < 0 || chk->len + len >= chk->size))
Willy Tarreau601360b2016-01-04 20:13:55 +0100156 return 0;
157
158 memcpy(chk->str + chk->len, str, len + 1);
159 chk->len += len;
160 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{
168 if (unlikely(chk->len < 0 || chk->len + nb >= chk->size))
169 return 0;
170
171 memcpy(chk->str + chk->len, str, nb);
172 chk->len += nb;
173 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 Tarreau320ec2a2016-02-25 16:15:19 +0100190 if (chk->len < 0 || chk->len + 1 >= chk->size)
Willy Tarreau601360b2016-01-04 20:13:55 +0100191 return NULL;
192
193 chk->str[chk->len++] = 0;
194 return chk->str + chk->len;
195}
196
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100197static inline void chunk_drop(struct chunk *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200198{
199 chk->str = NULL;
200 chk->len = -1;
201 chk->size = 0;
202}
203
204static inline void chunk_destroy(struct chunk *chk)
205{
206 if (!chk->size)
207 return;
208
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100209 free(chk->str);
210 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 Tarreau320ec2a2016-02-25 16:15:19 +0100222 if (!dst || !src || src->len < 0 || !src->str)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200223 return NULL;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100224
225 if (dst->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200226 free(dst->str);
227 dst->len = src->len;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100228 dst->size = src->len;
229 if (dst->size < src->size || !src->size)
230 dst->size++;
231
232 dst->str = (char *)malloc(dst->size);
David Carlier8ab10432016-03-23 17:50:57 +0000233 if (!dst->str) {
234 dst->len = 0;
235 dst->size = 0;
236 return NULL;
237 }
238
Willy Tarreauc7e42382012-08-24 19:22:53 +0200239 memcpy(dst->str, src->str, dst->len);
Willy Tarreauf9476a52016-01-04 20:36:59 +0100240 if (dst->len < dst->size)
241 dst->str[dst->len] = 0;
242
Willy Tarreauc7e42382012-08-24 19:22:53 +0200243 return dst->str;
244}
245
246#endif /* _TYPES_CHUNK_H */
247
248/*
249 * Local variables:
250 * c-indent-level: 8
251 * c-basic-offset: 8
252 * End:
253 */