Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 28 | #include <common/buf.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 29 | #include <common/config.h> |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 30 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 31 | |
| 32 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 33 | struct pool_head *pool_head_trash; |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 34 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 35 | /* function prototypes */ |
| 36 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 37 | int chunk_printf(struct buffer *chk, const char *fmt, ...) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 38 | __attribute__ ((format(printf, 2, 3))); |
| 39 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 40 | int chunk_appendf(struct buffer *chk, const char *fmt, ...) |
Willy Tarreau | 7780473 | 2012-10-29 16:14:26 +0100 | [diff] [blame] | 41 | __attribute__ ((format(printf, 2, 3))); |
| 42 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 43 | int chunk_htmlencode(struct buffer *dst, struct buffer *src); |
| 44 | int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc); |
| 45 | int chunk_strcmp(const struct buffer *chk, const char *str); |
| 46 | int chunk_strcasecmp(const struct buffer *chk, const char *str); |
| 47 | struct buffer *get_trash_chunk(void); |
| 48 | struct buffer *alloc_trash_chunk(void); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 49 | int init_trash_buffers(int first); |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL. |
| 53 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 54 | static inline void free_trash_chunk(struct buffer *chunk) |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 55 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 56 | pool_free(pool_head_trash, chunk); |
Willy Tarreau | b686afd | 2017-02-08 11:06:11 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 59 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 60 | static inline void chunk_reset(struct buffer *chk) |
Willy Tarreau | c26ac9d | 2012-10-29 13:23:11 +0100 | [diff] [blame] | 61 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 62 | chk->data = 0; |
Willy Tarreau | c26ac9d | 2012-10-29 13:23:11 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 65 | static inline void chunk_init(struct buffer *chk, char *str, size_t size) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 66 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 67 | chk->area = str; |
| 68 | chk->head = 0; |
| 69 | chk->data = 0; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 70 | chk->size = size; |
| 71 | } |
| 72 | |
| 73 | /* report 0 in case of error, 1 if OK. */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 74 | static inline int chunk_initlen(struct buffer *chk, char *str, size_t size, |
| 75 | int len) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 76 | { |
| 77 | |
Willy Tarreau | 320ec2a | 2016-02-25 16:15:19 +0100 | [diff] [blame] | 78 | if (len < 0 || (size && len > size)) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 79 | return 0; |
| 80 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 81 | chk->area = str; |
| 82 | chk->head = 0; |
| 83 | chk->data = len; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 84 | chk->size = size; |
| 85 | |
| 86 | return 1; |
| 87 | } |
| 88 | |
Willy Tarreau | 70af633 | 2016-01-06 20:45:03 +0100 | [diff] [blame] | 89 | /* this is only for temporary manipulation, the chunk is read-only */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 90 | static inline void chunk_initstr(struct buffer *chk, const char *str) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 91 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 92 | chk->area = (char *)str; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 93 | chk->head = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 94 | chk->data = strlen(str); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 95 | chk->size = 0; /* mark it read-only */ |
| 96 | } |
| 97 | |
Willy Tarreau | c1f40b3 | 2018-10-08 07:34:25 +0200 | [diff] [blame] | 98 | /* copies chunk <src> into <chk>. Returns 0 in case of failure. */ |
| 99 | static inline int chunk_cpy(struct buffer *chk, const struct buffer *src) |
| 100 | { |
| 101 | if (unlikely(src->data >= chk->size)) |
| 102 | return 0; |
| 103 | |
| 104 | chk->data = src->data; |
| 105 | memcpy(chk->area, src->area, src->data); |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | /* appends chunk <src> after <chk>. Returns 0 in case of failure. */ |
| 110 | static inline int chunk_cat(struct buffer *chk, const struct buffer *src) |
| 111 | { |
| 112 | if (unlikely(chk->data + src->data >= chk->size)) |
| 113 | return 0; |
| 114 | |
| 115 | memcpy(chk->area + chk->data, src->area, src->data); |
| 116 | chk->data += src->data; |
| 117 | return 1; |
| 118 | } |
| 119 | |
Willy Tarreau | 82032f1 | 2017-07-27 13:35:34 +0200 | [diff] [blame] | 120 | /* copies memory area <src> into <chk> for <len> bytes. Returns 0 in |
| 121 | * case of failure. No trailing zero is added. |
| 122 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 123 | static inline int chunk_memcpy(struct buffer *chk, const char *src, |
| 124 | size_t len) |
Willy Tarreau | 82032f1 | 2017-07-27 13:35:34 +0200 | [diff] [blame] | 125 | { |
| 126 | if (unlikely(len >= chk->size)) |
| 127 | return 0; |
| 128 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 129 | chk->data = len; |
| 130 | memcpy(chk->area, src, len); |
Willy Tarreau | 82032f1 | 2017-07-27 13:35:34 +0200 | [diff] [blame] | 131 | |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | /* appends memory area <src> after <chk> for <len> bytes. Returns 0 in |
| 136 | * case of failure. No trailing zero is added. |
| 137 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 138 | static inline int chunk_memcat(struct buffer *chk, const char *src, |
| 139 | size_t len) |
Willy Tarreau | 82032f1 | 2017-07-27 13:35:34 +0200 | [diff] [blame] | 140 | { |
Willy Tarreau | bba8156 | 2018-08-22 04:59:48 +0200 | [diff] [blame] | 141 | if (unlikely(chk->data + len >= chk->size)) |
Willy Tarreau | 82032f1 | 2017-07-27 13:35:34 +0200 | [diff] [blame] | 142 | return 0; |
| 143 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 144 | memcpy(chk->area + chk->data, src, len); |
| 145 | chk->data += len; |
Willy Tarreau | 82032f1 | 2017-07-27 13:35:34 +0200 | [diff] [blame] | 146 | return 1; |
| 147 | } |
| 148 | |
Willy Tarreau | 0b6044f | 2016-01-04 20:21:33 +0100 | [diff] [blame] | 149 | /* copies str into <chk> followed by a trailing zero. Returns 0 in |
| 150 | * case of failure. |
| 151 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 152 | static inline int chunk_strcpy(struct buffer *chk, const char *str) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 153 | { |
| 154 | size_t len; |
| 155 | |
| 156 | len = strlen(str); |
| 157 | |
Willy Tarreau | 0b6044f | 2016-01-04 20:21:33 +0100 | [diff] [blame] | 158 | if (unlikely(len >= chk->size)) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 159 | return 0; |
| 160 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 161 | chk->data = len; |
| 162 | memcpy(chk->area, str, len + 1); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 163 | |
| 164 | return 1; |
| 165 | } |
| 166 | |
Willy Tarreau | 601360b | 2016-01-04 20:13:55 +0100 | [diff] [blame] | 167 | /* appends str after <chk> followed by a trailing zero. Returns 0 in |
| 168 | * case of failure. |
| 169 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 170 | static inline int chunk_strcat(struct buffer *chk, const char *str) |
Willy Tarreau | 601360b | 2016-01-04 20:13:55 +0100 | [diff] [blame] | 171 | { |
| 172 | size_t len; |
| 173 | |
| 174 | len = strlen(str); |
| 175 | |
Willy Tarreau | bba8156 | 2018-08-22 04:59:48 +0200 | [diff] [blame] | 176 | if (unlikely(chk->data + len >= chk->size)) |
Willy Tarreau | 601360b | 2016-01-04 20:13:55 +0100 | [diff] [blame] | 177 | return 0; |
| 178 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 179 | memcpy(chk->area + chk->data, str, len + 1); |
| 180 | chk->data += len; |
Willy Tarreau | 601360b | 2016-01-04 20:13:55 +0100 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | |
Baptiste Assmann | 7819c12 | 2016-03-26 14:12:50 +0100 | [diff] [blame] | 184 | /* appends <nb> characters from str after <chk>. |
| 185 | * Returns 0 in case of failure. |
| 186 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 187 | static inline int chunk_strncat(struct buffer *chk, const char *str, int nb) |
Baptiste Assmann | 7819c12 | 2016-03-26 14:12:50 +0100 | [diff] [blame] | 188 | { |
Willy Tarreau | bba8156 | 2018-08-22 04:59:48 +0200 | [diff] [blame] | 189 | if (unlikely(chk->data + nb >= chk->size)) |
Baptiste Assmann | 7819c12 | 2016-03-26 14:12:50 +0100 | [diff] [blame] | 190 | return 0; |
| 191 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 192 | memcpy(chk->area + chk->data, str, nb); |
| 193 | chk->data += nb; |
Baptiste Assmann | 7819c12 | 2016-03-26 14:12:50 +0100 | [diff] [blame] | 194 | return 1; |
| 195 | } |
| 196 | |
Willy Tarreau | 601360b | 2016-01-04 20:13:55 +0100 | [diff] [blame] | 197 | /* Adds a trailing zero to the current chunk and returns the pointer to the |
| 198 | * following part. The purpose is to be able to use a chunk as a series of |
| 199 | * short independant strings with chunk_* functions, which do not need to be |
| 200 | * released. Returns NULL if no space is available to ensure that the new |
| 201 | * string will have its own trailing zero. For example : |
| 202 | * chunk_init(&trash); |
| 203 | * pid = chunk_newstr(&trash); |
| 204 | * chunk_appendf(&trash, "%d", getpid())); |
| 205 | * name = chunk_newstr(&trash); |
| 206 | * chunk_appendf(&trash, "%s", gethosname()); |
| 207 | * printf("hostname=<%s>, pid=<%d>\n", name, pid); |
| 208 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 209 | static inline char *chunk_newstr(struct buffer *chk) |
Willy Tarreau | 601360b | 2016-01-04 20:13:55 +0100 | [diff] [blame] | 210 | { |
Willy Tarreau | bba8156 | 2018-08-22 04:59:48 +0200 | [diff] [blame] | 211 | if (chk->data + 1 >= chk->size) |
Willy Tarreau | 601360b | 2016-01-04 20:13:55 +0100 | [diff] [blame] | 212 | return NULL; |
| 213 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 214 | chk->area[chk->data++] = 0; |
| 215 | return chk->area + chk->data; |
Willy Tarreau | 601360b | 2016-01-04 20:13:55 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 218 | static inline void chunk_drop(struct buffer *chk) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 219 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 220 | chk->area = NULL; |
| 221 | chk->data = -1; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 222 | chk->size = 0; |
| 223 | } |
| 224 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 225 | static inline void chunk_destroy(struct buffer *chk) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 226 | { |
| 227 | if (!chk->size) |
| 228 | return; |
| 229 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 230 | free(chk->area); |
Willy Tarreau | c26ac9d | 2012-10-29 13:23:11 +0100 | [diff] [blame] | 231 | chunk_drop(chk); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
| 235 | * frees the destination chunk if already allocated, allocates a new string, |
Willy Tarreau | f9476a5 | 2016-01-04 20:36:59 +0100 | [diff] [blame] | 236 | * and copies the source into it. The new chunk will have extra room for a |
| 237 | * trailing zero unless the source chunk was actually full. The pointer to |
| 238 | * the destination string is returned, or NULL if the allocation fails or if |
| 239 | * any pointer is NULL. |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 240 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 241 | static inline char *chunk_dup(struct buffer *dst, const struct buffer *src) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 242 | { |
Willy Tarreau | bba8156 | 2018-08-22 04:59:48 +0200 | [diff] [blame] | 243 | if (!dst || !src || !src->area) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 244 | return NULL; |
Willy Tarreau | f9476a5 | 2016-01-04 20:36:59 +0100 | [diff] [blame] | 245 | |
| 246 | if (dst->size) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 247 | free(dst->area); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 248 | dst->head = src->head; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 249 | dst->data = src->data; |
| 250 | dst->size = src->data; |
Willy Tarreau | f9476a5 | 2016-01-04 20:36:59 +0100 | [diff] [blame] | 251 | if (dst->size < src->size || !src->size) |
| 252 | dst->size++; |
| 253 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 254 | dst->area = (char *)malloc(dst->size); |
| 255 | if (!dst->area) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 256 | dst->head = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 257 | dst->data = 0; |
David Carlier | 8ab1043 | 2016-03-23 17:50:57 +0000 | [diff] [blame] | 258 | dst->size = 0; |
| 259 | return NULL; |
| 260 | } |
| 261 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 262 | memcpy(dst->area, src->area, dst->data); |
| 263 | if (dst->data < dst->size) |
| 264 | dst->area[dst->data] = 0; |
Willy Tarreau | f9476a5 | 2016-01-04 20:36:59 +0100 | [diff] [blame] | 265 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 266 | return dst->area; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | #endif /* _TYPES_CHUNK_H */ |
| 270 | |
| 271 | /* |
| 272 | * Local variables: |
| 273 | * c-indent-level: 8 |
| 274 | * c-basic-offset: 8 |
| 275 | * End: |
| 276 | */ |