1 /***************************************************************************
2  * Copyright (c) 2009-2010 Open Information Security Foundation
3  * Copyright (c) 2010-2013 Qualys, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12 
13  * - Redistributions in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in the
15  *   documentation and/or other materials provided with the distribution.
16 
17  * - Neither the name of the Qualys, Inc. nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  ***************************************************************************/
33 
34 /**
35  * @file
36  * @author Ivan Ristic <ivanr@webkreator.com>
37  */
38 
39 #ifndef _BSTR_BUILDER_H
40 #define	_BSTR_BUILDER_H
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 typedef struct bstr_builder_t bstr_builder_t;
47 
48 #include "htp_list.h"
49 
50 struct bstr_builder_t {
51     htp_list_t *pieces;
52 };
53 
54 #define BSTR_BUILDER_DEFAULT_SIZE 16
55 
56 /**
57  * Adds one new string to the builder. This function will adopt the
58  * string and destroy it when the builder itself is destroyed.
59  *
60  * @param[in] bb
61  * @param[in] b
62  * @return HTP_OK on success, HTP_ERROR on failure.
63  */
64 htp_status_t bstr_builder_appendn(bstr_builder_t *bb, bstr *b);
65 
66 /**
67  * Adds one new piece, in the form of a NUL-terminated string, to
68  * the builder. This function will make a copy of the provided string.
69  *
70  * @param[in] bb
71  * @param[in] cstr
72  * @return HTP_OK on success, HTP_ERROR on failure.
73  */
74 htp_status_t bstr_builder_append_c(bstr_builder_t *bb, const char *cstr);
75 
76 /**
77  * Adds one new piece, defined with the supplied pointer and
78  * length, to the builder. This function will make a copy of the
79  * provided data region.
80  *
81  * @param[in] bb
82  * @param[in] data
83  * @param[in] len
84  * @return @return HTP_OK on success, HTP_ERROR on failure.
85  */
86 htp_status_t bstr_builder_append_mem(bstr_builder_t *bb, const void *data, size_t len);
87 
88 /**
89  * Clears this string builder, destroying all existing pieces. You may
90  * want to clear a builder once you've either read all the pieces and
91  * done something with them, or after you've converted the builder into
92  * a single string.
93  *
94  * @param[in] bb
95  */
96 void bstr_builder_clear(bstr_builder_t *bb);
97 
98 /**
99  * Creates a new string builder.
100  *
101  * @return New string builder, or NULL on error.
102  */
103 bstr_builder_t *bstr_builder_create(void);
104 
105 /**
106  * Destroys an existing string builder, also destroying all
107  * the pieces stored within.
108  *
109  * @param[in] bb
110  */
111 void bstr_builder_destroy(bstr_builder_t *bb);
112 
113 /**
114  * Returns the size (the number of pieces) currently in a string builder.
115  *
116  * @param[in] bb
117  * @return size
118  */
119 size_t bstr_builder_size(const bstr_builder_t *bb);
120 
121 /**
122  * Creates a single string out of all the pieces held in a
123  * string builder. This method will not destroy any of the pieces.
124  *
125  * @param[in] bb
126  * @return New string, or NULL on error.
127  */
128 bstr *bstr_builder_to_str(const bstr_builder_t *bb);
129 
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif	/* _BSTR_BUILDER_H */
136 
137