/src/suricata7/libhtp/htp/bstr_builder.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "bstr.h" |
40 | | #include "htp_list.h" |
41 | | |
42 | 0 | htp_status_t bstr_builder_appendn(bstr_builder_t *bb, bstr *b) { |
43 | 0 | return htp_list_push(bb->pieces, b); |
44 | 0 | } |
45 | | |
46 | 0 | htp_status_t bstr_builder_append_c(bstr_builder_t *bb, const char *cstr) { |
47 | 0 | bstr *b = bstr_dup_c(cstr); |
48 | 0 | if (b == NULL) return HTP_ERROR; |
49 | 0 | return htp_list_push(bb->pieces, b); |
50 | 0 | } |
51 | | |
52 | 0 | htp_status_t bstr_builder_append_mem(bstr_builder_t *bb, const void *data, size_t len) { |
53 | 0 | bstr *b = bstr_dup_mem(data, len); |
54 | 0 | if (b == NULL) return HTP_ERROR; |
55 | 0 | return htp_list_push(bb->pieces, b); |
56 | 0 | } |
57 | | |
58 | 0 | void bstr_builder_clear(bstr_builder_t *bb) { |
59 | | // Do nothing if the list is empty |
60 | 0 | if (htp_list_size(bb->pieces) == 0) return; |
61 | | |
62 | 0 | for (size_t i = 0, n = htp_list_size(bb->pieces); i < n; i++) { |
63 | 0 | bstr *b = htp_list_get(bb->pieces, i); |
64 | 0 | bstr_free(b); |
65 | 0 | } |
66 | |
|
67 | 0 | htp_list_clear(bb->pieces); |
68 | 0 | } |
69 | | |
70 | 0 | bstr_builder_t *bstr_builder_create(void) { |
71 | 0 | bstr_builder_t *bb = calloc(1, sizeof (bstr_builder_t)); |
72 | 0 | if (bb == NULL) return NULL; |
73 | | |
74 | 0 | bb->pieces = htp_list_create(BSTR_BUILDER_DEFAULT_SIZE); |
75 | 0 | if (bb->pieces == NULL) { |
76 | 0 | free(bb); |
77 | 0 | return NULL; |
78 | 0 | } |
79 | | |
80 | 0 | return bb; |
81 | 0 | } |
82 | | |
83 | 0 | void bstr_builder_destroy(bstr_builder_t *bb) { |
84 | 0 | if (bb == NULL) return; |
85 | | |
86 | | // Destroy any pieces we might have |
87 | 0 | for (size_t i = 0, n = htp_list_size(bb->pieces); i < n; i++) { |
88 | 0 | bstr *b = htp_list_get(bb->pieces, i); |
89 | 0 | bstr_free(b); |
90 | 0 | } |
91 | |
|
92 | 0 | htp_list_destroy(bb->pieces); |
93 | |
|
94 | 0 | free(bb); |
95 | 0 | } |
96 | | |
97 | 0 | size_t bstr_builder_size(const bstr_builder_t *bb) { |
98 | 0 | return htp_list_size(bb->pieces); |
99 | 0 | } |
100 | | |
101 | 0 | bstr *bstr_builder_to_str(const bstr_builder_t *bb) { |
102 | 0 | size_t len = 0; |
103 | | |
104 | | // Determine the size of the string |
105 | 0 | for (size_t i = 0, n = htp_list_size(bb->pieces); i < n; i++) { |
106 | 0 | bstr *b = htp_list_get(bb->pieces, i); |
107 | 0 | len += bstr_len(b); |
108 | 0 | } |
109 | | |
110 | | // Allocate string |
111 | 0 | bstr *bnew = bstr_alloc(len); |
112 | 0 | if (bnew == NULL) return NULL; |
113 | | |
114 | | // Determine the size of the string |
115 | 0 | for (size_t i = 0, n = htp_list_size(bb->pieces); i < n; i++) { |
116 | 0 | bstr *b = htp_list_get(bb->pieces, i); |
117 | 0 | bstr_add_noex(bnew, b); |
118 | 0 | } |
119 | |
|
120 | 0 | return bnew; |
121 | 0 | } |