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