blob: aac5282fb15046b0b1730896740a76220499fe0f [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>
29
30
31/* describes a chunk of string */
32struct chunk {
33 char *str; /* beginning of the string itself. Might not be 0-terminated */
34 int size; /* total size of the buffer, 0 if the *str is read-only */
35 int len; /* current size of the string from first to last char. <0 = uninit. */
36};
37
38/* function prototypes */
39
40int chunk_printf(struct chunk *chk, const char *fmt, ...)
41 __attribute__ ((format(printf, 2, 3)));
42
Willy Tarreau77804732012-10-29 16:14:26 +010043int chunk_appendf(struct chunk *chk, const char *fmt, ...)
44 __attribute__ ((format(printf, 2, 3)));
45
Willy Tarreauc7e42382012-08-24 19:22:53 +020046int chunk_htmlencode(struct chunk *dst, struct chunk *src);
47int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
Willy Tarreauad8f8e82012-10-19 15:18:06 +020048int chunk_strcmp(const struct chunk *chk, const char *str);
49int chunk_strcasecmp(const struct chunk *chk, const char *str);
Willy Tarreau47ca5452012-12-23 20:22:19 +010050int alloc_trash_buffers(int bufsize);
David Carlier60deeba2015-09-25 11:58:12 +010051void free_trash_buffers(void);
Willy Tarreau47ca5452012-12-23 20:22:19 +010052struct chunk *get_trash_chunk(void);
Willy Tarreauc7e42382012-08-24 19:22:53 +020053
Willy Tarreauc26ac9d2012-10-29 13:23:11 +010054static inline void chunk_reset(struct chunk *chk)
55{
56 chk->len = 0;
57}
58
Willy Tarreauc7e42382012-08-24 19:22:53 +020059static inline void chunk_init(struct chunk *chk, char *str, size_t size)
60{
61 chk->str = str;
62 chk->len = 0;
63 chk->size = size;
64}
65
66/* report 0 in case of error, 1 if OK. */
67static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len)
68{
69
Willy Tarreau320ec2a2016-02-25 16:15:19 +010070 if (len < 0 || (size && len > size))
Willy Tarreauc7e42382012-08-24 19:22:53 +020071 return 0;
72
73 chk->str = str;
74 chk->len = len;
75 chk->size = size;
76
77 return 1;
78}
79
Willy Tarreau70af6332016-01-06 20:45:03 +010080/* this is only for temporary manipulation, the chunk is read-only */
81static inline void chunk_initstr(struct chunk *chk, const char *str)
Willy Tarreauc7e42382012-08-24 19:22:53 +020082{
Willy Tarreau70af6332016-01-06 20:45:03 +010083 chk->str = (char *)str;
Willy Tarreauc7e42382012-08-24 19:22:53 +020084 chk->len = strlen(str);
85 chk->size = 0; /* mark it read-only */
86}
87
Willy Tarreau0b6044f2016-01-04 20:21:33 +010088/* copies str into <chk> followed by a trailing zero. Returns 0 in
89 * case of failure.
90 */
Willy Tarreauc7e42382012-08-24 19:22:53 +020091static inline int chunk_strcpy(struct chunk *chk, const char *str)
92{
93 size_t len;
94
95 len = strlen(str);
96
Willy Tarreau0b6044f2016-01-04 20:21:33 +010097 if (unlikely(len >= chk->size))
Willy Tarreauc7e42382012-08-24 19:22:53 +020098 return 0;
99
100 chk->len = len;
Willy Tarreau0b6044f2016-01-04 20:21:33 +0100101 memcpy(chk->str, str, len + 1);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200102
103 return 1;
104}
105
Willy Tarreau601360b2016-01-04 20:13:55 +0100106/* appends str after <chk> followed by a trailing zero. Returns 0 in
107 * case of failure.
108 */
109static inline int chunk_strcat(struct chunk *chk, const char *str)
110{
111 size_t len;
112
113 len = strlen(str);
114
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100115 if (unlikely(chk->len < 0 || chk->len + len >= chk->size))
Willy Tarreau601360b2016-01-04 20:13:55 +0100116 return 0;
117
118 memcpy(chk->str + chk->len, str, len + 1);
119 chk->len += len;
120 return 1;
121}
122
123/* Adds a trailing zero to the current chunk and returns the pointer to the
124 * following part. The purpose is to be able to use a chunk as a series of
125 * short independant strings with chunk_* functions, which do not need to be
126 * released. Returns NULL if no space is available to ensure that the new
127 * string will have its own trailing zero. For example :
128 * chunk_init(&trash);
129 * pid = chunk_newstr(&trash);
130 * chunk_appendf(&trash, "%d", getpid()));
131 * name = chunk_newstr(&trash);
132 * chunk_appendf(&trash, "%s", gethosname());
133 * printf("hostname=<%s>, pid=<%d>\n", name, pid);
134 */
135static inline char *chunk_newstr(struct chunk *chk)
136{
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100137 if (chk->len < 0 || chk->len + 1 >= chk->size)
Willy Tarreau601360b2016-01-04 20:13:55 +0100138 return NULL;
139
140 chk->str[chk->len++] = 0;
141 return chk->str + chk->len;
142}
143
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100144static inline void chunk_drop(struct chunk *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200145{
146 chk->str = NULL;
147 chk->len = -1;
148 chk->size = 0;
149}
150
151static inline void chunk_destroy(struct chunk *chk)
152{
153 if (!chk->size)
154 return;
155
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100156 free(chk->str);
157 chunk_drop(chk);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200158}
159
160/*
161 * frees the destination chunk if already allocated, allocates a new string,
Willy Tarreauf9476a52016-01-04 20:36:59 +0100162 * and copies the source into it. The new chunk will have extra room for a
163 * trailing zero unless the source chunk was actually full. The pointer to
164 * the destination string is returned, or NULL if the allocation fails or if
165 * any pointer is NULL.
Willy Tarreauc7e42382012-08-24 19:22:53 +0200166 */
167static inline char *chunk_dup(struct chunk *dst, const struct chunk *src)
168{
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100169 if (!dst || !src || src->len < 0 || !src->str)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200170 return NULL;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100171
172 if (dst->size)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200173 free(dst->str);
174 dst->len = src->len;
Willy Tarreauf9476a52016-01-04 20:36:59 +0100175 dst->size = src->len;
176 if (dst->size < src->size || !src->size)
177 dst->size++;
178
179 dst->str = (char *)malloc(dst->size);
David Carlier8ab10432016-03-23 17:50:57 +0000180 if (!dst->str) {
181 dst->len = 0;
182 dst->size = 0;
183 return NULL;
184 }
185
Willy Tarreauc7e42382012-08-24 19:22:53 +0200186 memcpy(dst->str, src->str, dst->len);
Willy Tarreauf9476a52016-01-04 20:36:59 +0100187 if (dst->len < dst->size)
188 dst->str[dst->len] = 0;
189
Willy Tarreauc7e42382012-08-24 19:22:53 +0200190 return dst->str;
191}
192
193#endif /* _TYPES_CHUNK_H */
194
195/*
196 * Local variables:
197 * c-indent-level: 8
198 * c-basic-offset: 8
199 * End:
200 */