/src/yara/libyara/simple_str.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 2022. The YARA Authors. All Rights Reserved. |
3 | | |
4 | | Redistribution and use in source and binary forms, with or without modification, |
5 | | are permitted provided that the following conditions are met: |
6 | | |
7 | | 1. Redistributions of source code must retain the above copyright notice, this |
8 | | list of conditions and the following disclaimer. |
9 | | |
10 | | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | this list of conditions and the following disclaimer in the documentation and/or |
12 | | other materials provided with the distribution. |
13 | | |
14 | | 3. Neither the name of the copyright holder nor the names of its contributors |
15 | | may be used to endorse or promote products derived from this software without |
16 | | specific prior written permission. |
17 | | |
18 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
19 | | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
22 | | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
25 | | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | |
30 | | #include <stdarg.h> |
31 | | #include <string.h> |
32 | | #include <yara/mem.h> |
33 | | #include <yara/simple_str.h> |
34 | | #include <yara/types.h> |
35 | | |
36 | | static bool sstr_vappendf(SIMPLE_STR* ss, const char* fmt, va_list va) |
37 | 0 | { |
38 | | // Create copy because list will get consumed when getting the final length |
39 | 0 | va_list va2; |
40 | 0 | va_copy(va2, va); |
41 | |
|
42 | 0 | int size = vsnprintf(NULL, 0, fmt, va2); |
43 | |
|
44 | 0 | va_end(va2); |
45 | |
|
46 | 0 | if (size < 0) |
47 | 0 | return false; |
48 | | |
49 | 0 | if (ss->cap < ss->len + size + 1) |
50 | 0 | { |
51 | 0 | uint32_t new_size = (ss->len + size) * 2 + 64; |
52 | 0 | char* tmp = yr_realloc(ss->str, new_size); |
53 | |
|
54 | 0 | if (!tmp) |
55 | 0 | return false; |
56 | | |
57 | 0 | ss->str = tmp; |
58 | 0 | ss->cap = new_size; |
59 | 0 | } |
60 | | |
61 | 0 | ss->len += vsnprintf(ss->str + ss->len, ss->cap, fmt, va); |
62 | |
|
63 | 0 | return true; |
64 | 0 | } |
65 | | |
66 | | SIMPLE_STR* sstr_new(const char* s) |
67 | 0 | { |
68 | 0 | SIMPLE_STR* ss = yr_calloc(1, sizeof(SIMPLE_STR)); |
69 | 0 | if (!ss) |
70 | 0 | return NULL; |
71 | | |
72 | 0 | if (s) |
73 | 0 | { |
74 | 0 | uint32_t slen = strlen(s); |
75 | 0 | ss->str = yr_malloc(slen + 1); |
76 | 0 | if (!ss->str) |
77 | 0 | { |
78 | 0 | yr_free(ss); |
79 | 0 | return NULL; |
80 | 0 | } |
81 | 0 | ss->len = slen; |
82 | 0 | ss->cap = slen; |
83 | 0 | memcpy(ss->str, s, slen + 1); |
84 | 0 | } |
85 | | |
86 | 0 | return ss; |
87 | 0 | } |
88 | | |
89 | | SIMPLE_STR* sstr_newf(const char* fmt, ...) |
90 | 0 | { |
91 | 0 | SIMPLE_STR* ss = sstr_new(NULL); |
92 | 0 | if (!ss) |
93 | 0 | return NULL; |
94 | | |
95 | 0 | va_list va; |
96 | 0 | va_start(va, fmt); |
97 | 0 | bool ret = sstr_vappendf(ss, fmt, va); |
98 | 0 | va_end(va); |
99 | |
|
100 | 0 | if (ret) |
101 | 0 | return ss; |
102 | | |
103 | 0 | sstr_free(ss); |
104 | |
|
105 | 0 | return NULL; |
106 | 0 | } |
107 | | |
108 | | void sstr_free(SIMPLE_STR* ss) |
109 | 0 | { |
110 | 0 | if (ss) |
111 | 0 | { |
112 | 0 | yr_free(ss->str); |
113 | 0 | yr_free(ss); |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | bool sstr_appendf(SIMPLE_STR* ss, const char* fmt, ...) |
118 | 0 | { |
119 | 0 | va_list vlist; |
120 | 0 | va_start(vlist, fmt); |
121 | 0 | bool ret = sstr_vappendf(ss, fmt, vlist); |
122 | 0 | va_end(vlist); |
123 | |
|
124 | 0 | return ret; |
125 | 0 | } |
126 | | |
127 | | char* sstr_move(SIMPLE_STR* ss) |
128 | 0 | { |
129 | 0 | char* ret = ss->str; |
130 | 0 | ss->str = NULL; |
131 | 0 | ss->len = 0; |
132 | 0 | ss->cap = 0; |
133 | |
|
134 | 0 | return ret; |
135 | 0 | } |