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 | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 50 | |
Willy Tarreau | c26ac9d | 2012-10-29 13:23:11 +0100 | [diff] [blame] | 51 | static inline void chunk_reset(struct chunk *chk) |
| 52 | { |
| 53 | chk->len = 0; |
| 54 | } |
| 55 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 56 | static inline void chunk_init(struct chunk *chk, char *str, size_t size) |
| 57 | { |
| 58 | chk->str = str; |
| 59 | chk->len = 0; |
| 60 | chk->size = size; |
| 61 | } |
| 62 | |
| 63 | /* report 0 in case of error, 1 if OK. */ |
| 64 | static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) |
| 65 | { |
| 66 | |
| 67 | if (size && len > size) |
| 68 | return 0; |
| 69 | |
| 70 | chk->str = str; |
| 71 | chk->len = len; |
| 72 | chk->size = size; |
| 73 | |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | static inline void chunk_initstr(struct chunk *chk, char *str) |
| 78 | { |
| 79 | chk->str = str; |
| 80 | chk->len = strlen(str); |
| 81 | chk->size = 0; /* mark it read-only */ |
| 82 | } |
| 83 | |
| 84 | static inline int chunk_strcpy(struct chunk *chk, const char *str) |
| 85 | { |
| 86 | size_t len; |
| 87 | |
| 88 | len = strlen(str); |
| 89 | |
| 90 | if (unlikely(len > chk->size)) |
| 91 | return 0; |
| 92 | |
| 93 | chk->len = len; |
| 94 | memcpy(chk->str, str, len); |
| 95 | |
| 96 | return 1; |
| 97 | } |
| 98 | |
Willy Tarreau | c26ac9d | 2012-10-29 13:23:11 +0100 | [diff] [blame] | 99 | static inline void chunk_drop(struct chunk *chk) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 100 | { |
| 101 | chk->str = NULL; |
| 102 | chk->len = -1; |
| 103 | chk->size = 0; |
| 104 | } |
| 105 | |
| 106 | static inline void chunk_destroy(struct chunk *chk) |
| 107 | { |
| 108 | if (!chk->size) |
| 109 | return; |
| 110 | |
Willy Tarreau | c26ac9d | 2012-10-29 13:23:11 +0100 | [diff] [blame] | 111 | free(chk->str); |
| 112 | chunk_drop(chk); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * frees the destination chunk if already allocated, allocates a new string, |
| 117 | * and copies the source into it. The pointer to the destination string is |
| 118 | * returned, or NULL if the allocation fails or if any pointer is NULL.. |
| 119 | */ |
| 120 | static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) |
| 121 | { |
| 122 | if (!dst || !src || !src->str) |
| 123 | return NULL; |
| 124 | if (dst->str) |
| 125 | free(dst->str); |
| 126 | dst->len = src->len; |
| 127 | dst->str = (char *)malloc(dst->len); |
| 128 | memcpy(dst->str, src->str, dst->len); |
| 129 | return dst->str; |
| 130 | } |
| 131 | |
| 132 | #endif /* _TYPES_CHUNK_H */ |
| 133 | |
| 134 | /* |
| 135 | * Local variables: |
| 136 | * c-indent-level: 8 |
| 137 | * c-basic-offset: 8 |
| 138 | * End: |
| 139 | */ |