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