blob: 8225d96f18bcb53681e7f32ed58c81468f8de354 [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
70 if (size && len > size)
71 return 0;
72
73 chk->str = str;
74 chk->len = len;
75 chk->size = size;
76
77 return 1;
78}
79
80static inline void chunk_initstr(struct chunk *chk, char *str)
81{
82 chk->str = str;
83 chk->len = strlen(str);
84 chk->size = 0; /* mark it read-only */
85}
86
87static inline int chunk_strcpy(struct chunk *chk, const char *str)
88{
89 size_t len;
90
91 len = strlen(str);
92
93 if (unlikely(len > chk->size))
94 return 0;
95
96 chk->len = len;
97 memcpy(chk->str, str, len);
98
99 return 1;
100}
101
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100102static inline void chunk_drop(struct chunk *chk)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200103{
104 chk->str = NULL;
105 chk->len = -1;
106 chk->size = 0;
107}
108
109static inline void chunk_destroy(struct chunk *chk)
110{
111 if (!chk->size)
112 return;
113
Willy Tarreauc26ac9d2012-10-29 13:23:11 +0100114 free(chk->str);
115 chunk_drop(chk);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200116}
117
118/*
119 * frees the destination chunk if already allocated, allocates a new string,
120 * and copies the source into it. The pointer to the destination string is
121 * returned, or NULL if the allocation fails or if any pointer is NULL..
122 */
123static inline char *chunk_dup(struct chunk *dst, const struct chunk *src)
124{
125 if (!dst || !src || !src->str)
126 return NULL;
127 if (dst->str)
128 free(dst->str);
129 dst->len = src->len;
130 dst->str = (char *)malloc(dst->len);
131 memcpy(dst->str, src->str, dst->len);
132 return dst->str;
133}
134
135#endif /* _TYPES_CHUNK_H */
136
137/*
138 * Local variables:
139 * c-indent-level: 8
140 * c-basic-offset: 8
141 * End:
142 */