Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | #include "dynbuf.h" |
27 | | #include "curl_printf.h" |
28 | | #ifdef BUILDING_LIBCURL |
29 | | #include "curl_memory.h" |
30 | | #endif |
31 | | #include "memdebug.h" |
32 | | |
33 | 0 | #define MIN_FIRST_ALLOC 32 |
34 | | |
35 | 1.19k | #define DYNINIT 0xbee51da /* random pattern */ |
36 | | |
37 | | /* |
38 | | * Init a dynbuf struct. |
39 | | */ |
40 | | void Curl_dyn_init(struct dynbuf *s, size_t toobig) |
41 | 1.19k | { |
42 | 1.19k | DEBUGASSERT(s); |
43 | 1.19k | DEBUGASSERT(toobig); |
44 | 1.19k | s->bufr = NULL; |
45 | 1.19k | s->leng = 0; |
46 | 1.19k | s->allc = 0; |
47 | 1.19k | s->toobig = toobig; |
48 | 1.19k | #ifdef DEBUGBUILD |
49 | 1.19k | s->init = DYNINIT; |
50 | 1.19k | #endif |
51 | 1.19k | } |
52 | | |
53 | | /* |
54 | | * free the buffer and re-init the necessary fields. It doesn't touch the |
55 | | * 'init' field and thus this buffer can be reused to add data to again. |
56 | | */ |
57 | | void Curl_dyn_free(struct dynbuf *s) |
58 | 1.19k | { |
59 | 1.19k | DEBUGASSERT(s); |
60 | 1.19k | Curl_safefree(s->bufr); |
61 | 1.19k | s->leng = s->allc = 0; |
62 | 1.19k | } |
63 | | |
64 | | /* |
65 | | * Store/append an chunk of memory to the dynbuf. |
66 | | */ |
67 | | static CURLcode dyn_nappend(struct dynbuf *s, |
68 | | const unsigned char *mem, size_t len) |
69 | 0 | { |
70 | 0 | size_t indx = s->leng; |
71 | 0 | size_t a = s->allc; |
72 | 0 | size_t fit = len + indx + 1; /* new string + old string + zero byte */ |
73 | | |
74 | | /* try to detect if there's rubbish in the struct */ |
75 | 0 | DEBUGASSERT(s->init == DYNINIT); |
76 | 0 | DEBUGASSERT(s->toobig); |
77 | 0 | DEBUGASSERT(indx < s->toobig); |
78 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
79 | 0 | DEBUGASSERT(a <= s->toobig); |
80 | | |
81 | 0 | if(fit > s->toobig) { |
82 | 0 | Curl_dyn_free(s); |
83 | 0 | return CURLE_OUT_OF_MEMORY; |
84 | 0 | } |
85 | 0 | else if(!a) { |
86 | 0 | DEBUGASSERT(!indx); |
87 | | /* first invoke */ |
88 | 0 | if(MIN_FIRST_ALLOC > s->toobig) |
89 | 0 | a = s->toobig; |
90 | 0 | else if(fit < MIN_FIRST_ALLOC) |
91 | 0 | a = MIN_FIRST_ALLOC; |
92 | 0 | else |
93 | 0 | a = fit; |
94 | 0 | } |
95 | 0 | else { |
96 | 0 | while(a < fit) |
97 | 0 | a *= 2; |
98 | 0 | if(a > s->toobig) |
99 | | /* no point in allocating a larger buffer than this is allowed to use */ |
100 | 0 | a = s->toobig; |
101 | 0 | } |
102 | | |
103 | 0 | if(a != s->allc) { |
104 | | /* this logic is not using Curl_saferealloc() to make the tool not have to |
105 | | include that as well when it uses this code */ |
106 | 0 | void *p = realloc(s->bufr, a); |
107 | 0 | if(!p) { |
108 | 0 | Curl_dyn_free(s); |
109 | 0 | return CURLE_OUT_OF_MEMORY; |
110 | 0 | } |
111 | 0 | s->bufr = p; |
112 | 0 | s->allc = a; |
113 | 0 | } |
114 | | |
115 | 0 | if(len) |
116 | 0 | memcpy(&s->bufr[indx], mem, len); |
117 | 0 | s->leng = indx + len; |
118 | 0 | s->bufr[s->leng] = 0; |
119 | 0 | return CURLE_OK; |
120 | 0 | } |
121 | | |
122 | | /* |
123 | | * Clears the string, keeps the allocation. This can also be called on a |
124 | | * buffer that already was freed. |
125 | | */ |
126 | | void Curl_dyn_reset(struct dynbuf *s) |
127 | 0 | { |
128 | 0 | DEBUGASSERT(s); |
129 | 0 | DEBUGASSERT(s->init == DYNINIT); |
130 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
131 | 0 | if(s->leng) |
132 | 0 | s->bufr[0] = 0; |
133 | 0 | s->leng = 0; |
134 | 0 | } |
135 | | |
136 | | /* |
137 | | * Specify the size of the tail to keep (number of bytes from the end of the |
138 | | * buffer). The rest will be dropped. |
139 | | */ |
140 | | CURLcode Curl_dyn_tail(struct dynbuf *s, size_t trail) |
141 | 0 | { |
142 | 0 | DEBUGASSERT(s); |
143 | 0 | DEBUGASSERT(s->init == DYNINIT); |
144 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
145 | 0 | if(trail > s->leng) |
146 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
147 | 0 | else if(trail == s->leng) |
148 | 0 | return CURLE_OK; |
149 | 0 | else if(!trail) { |
150 | 0 | Curl_dyn_reset(s); |
151 | 0 | } |
152 | 0 | else { |
153 | 0 | memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail); |
154 | 0 | s->leng = trail; |
155 | 0 | s->bufr[s->leng] = 0; |
156 | 0 | } |
157 | 0 | return CURLE_OK; |
158 | |
|
159 | 0 | } |
160 | | |
161 | | /* |
162 | | * Appends a buffer with length. |
163 | | */ |
164 | | CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len) |
165 | 0 | { |
166 | 0 | DEBUGASSERT(s); |
167 | 0 | DEBUGASSERT(s->init == DYNINIT); |
168 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
169 | 0 | return dyn_nappend(s, mem, len); |
170 | 0 | } |
171 | | |
172 | | /* |
173 | | * Append a null-terminated string at the end. |
174 | | */ |
175 | | CURLcode Curl_dyn_add(struct dynbuf *s, const char *str) |
176 | 0 | { |
177 | 0 | size_t n = strlen(str); |
178 | 0 | DEBUGASSERT(s); |
179 | 0 | DEBUGASSERT(s->init == DYNINIT); |
180 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
181 | 0 | return dyn_nappend(s, (unsigned char *)str, n); |
182 | 0 | } |
183 | | |
184 | | /* |
185 | | * Append a string vprintf()-style |
186 | | */ |
187 | | CURLcode Curl_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap) |
188 | 0 | { |
189 | 0 | #ifdef BUILDING_LIBCURL |
190 | 0 | int rc; |
191 | 0 | DEBUGASSERT(s); |
192 | 0 | DEBUGASSERT(s->init == DYNINIT); |
193 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
194 | 0 | rc = Curl_dyn_vprintf(s, fmt, ap); |
195 | |
|
196 | 0 | if(!rc) |
197 | 0 | return CURLE_OK; |
198 | | #else |
199 | | char *str; |
200 | | str = vaprintf(fmt, ap); /* this allocs a new string to append */ |
201 | | |
202 | | if(str) { |
203 | | CURLcode result = dyn_nappend(s, (unsigned char *)str, strlen(str)); |
204 | | free(str); |
205 | | return result; |
206 | | } |
207 | | /* If we failed, we cleanup the whole buffer and return error */ |
208 | | Curl_dyn_free(s); |
209 | | #endif |
210 | 0 | return CURLE_OUT_OF_MEMORY; |
211 | 0 | } |
212 | | |
213 | | /* |
214 | | * Append a string printf()-style |
215 | | */ |
216 | | CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...) |
217 | 0 | { |
218 | 0 | CURLcode result; |
219 | 0 | va_list ap; |
220 | 0 | DEBUGASSERT(s); |
221 | 0 | DEBUGASSERT(s->init == DYNINIT); |
222 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
223 | 0 | va_start(ap, fmt); |
224 | 0 | result = Curl_dyn_vaddf(s, fmt, ap); |
225 | 0 | va_end(ap); |
226 | 0 | return result; |
227 | 0 | } |
228 | | |
229 | | /* |
230 | | * Returns a pointer to the buffer. |
231 | | */ |
232 | | char *Curl_dyn_ptr(const struct dynbuf *s) |
233 | 0 | { |
234 | 0 | DEBUGASSERT(s); |
235 | 0 | DEBUGASSERT(s->init == DYNINIT); |
236 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
237 | 0 | return s->bufr; |
238 | 0 | } |
239 | | |
240 | | /* |
241 | | * Returns an unsigned pointer to the buffer. |
242 | | */ |
243 | | unsigned char *Curl_dyn_uptr(const struct dynbuf *s) |
244 | 0 | { |
245 | 0 | DEBUGASSERT(s); |
246 | 0 | DEBUGASSERT(s->init == DYNINIT); |
247 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
248 | 0 | return (unsigned char *)s->bufr; |
249 | 0 | } |
250 | | |
251 | | /* |
252 | | * Returns the length of the buffer. |
253 | | */ |
254 | | size_t Curl_dyn_len(const struct dynbuf *s) |
255 | 0 | { |
256 | 0 | DEBUGASSERT(s); |
257 | 0 | DEBUGASSERT(s->init == DYNINIT); |
258 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
259 | 0 | return s->leng; |
260 | 0 | } |
261 | | |
262 | | /* |
263 | | * Set a new (smaller) length. |
264 | | */ |
265 | | CURLcode Curl_dyn_setlen(struct dynbuf *s, size_t set) |
266 | 0 | { |
267 | 0 | DEBUGASSERT(s); |
268 | 0 | DEBUGASSERT(s->init == DYNINIT); |
269 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
270 | 0 | if(set > s->leng) |
271 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
272 | 0 | s->leng = set; |
273 | 0 | s->bufr[s->leng] = 0; |
274 | 0 | return CURLE_OK; |
275 | 0 | } |