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