/src/PROJ/curl/lib/curlx/dynbuf.c
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 | | #ifdef DEBUGBUILD |
36 | | #define DYNINIT 0xbee51da /* random pattern */ |
37 | | #endif |
38 | | |
39 | | /* |
40 | | * Init a dynbuf struct. |
41 | | */ |
42 | | void curlx_dyn_init(struct dynbuf *s, size_t toobig) |
43 | 0 | { |
44 | 0 | DEBUGASSERT(s); |
45 | 0 | DEBUGASSERT(toobig); |
46 | 0 | DEBUGASSERT(toobig <= MAX_DYNBUF_SIZE); /* catch crazy mistakes */ |
47 | 0 | s->bufr = NULL; |
48 | 0 | s->leng = 0; |
49 | 0 | s->allc = 0; |
50 | 0 | s->toobig = toobig; |
51 | | #ifdef DEBUGBUILD |
52 | | s->init = DYNINIT; |
53 | | #endif |
54 | 0 | } |
55 | | |
56 | | /* |
57 | | * free the buffer and re-init the necessary fields. It does not touch the |
58 | | * 'init' field and thus this buffer can be reused to add data to again. |
59 | | */ |
60 | | void curlx_dyn_free(struct dynbuf *s) |
61 | 0 | { |
62 | 0 | DEBUGASSERT(s); |
63 | 0 | DEBUGASSERT(s->init == DYNINIT); |
64 | 0 | Curl_safefree(s->bufr); |
65 | 0 | s->leng = s->allc = 0; |
66 | 0 | } |
67 | | |
68 | | /* |
69 | | * Store/append an chunk of memory to the dynbuf. |
70 | | */ |
71 | | static CURLcode dyn_nappend(struct dynbuf *s, |
72 | | const unsigned char *mem, size_t len) |
73 | 0 | { |
74 | 0 | size_t indx = s->leng; |
75 | 0 | size_t a = s->allc; |
76 | 0 | size_t fit = len + indx + 1; /* new string + old string + zero byte */ |
77 | | |
78 | | /* try to detect if there is rubbish in the struct */ |
79 | 0 | DEBUGASSERT(s->init == DYNINIT); |
80 | 0 | DEBUGASSERT(s->toobig); |
81 | 0 | DEBUGASSERT(indx < s->toobig); |
82 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
83 | 0 | DEBUGASSERT(a <= s->toobig); |
84 | 0 | DEBUGASSERT(!len || mem); |
85 | |
|
86 | 0 | if(fit > s->toobig) { |
87 | 0 | curlx_dyn_free(s); |
88 | 0 | return CURLE_TOO_LARGE; |
89 | 0 | } |
90 | 0 | else if(!a) { |
91 | 0 | DEBUGASSERT(!indx); |
92 | | /* first invoke */ |
93 | 0 | if(MIN_FIRST_ALLOC > s->toobig) |
94 | 0 | a = s->toobig; |
95 | 0 | else if(fit < MIN_FIRST_ALLOC) |
96 | 0 | a = MIN_FIRST_ALLOC; |
97 | 0 | else |
98 | 0 | a = fit; |
99 | 0 | } |
100 | 0 | else { |
101 | 0 | while(a < fit) |
102 | 0 | a *= 2; |
103 | 0 | if(a > s->toobig) |
104 | | /* no point in allocating a larger buffer than this is allowed to use */ |
105 | 0 | a = s->toobig; |
106 | 0 | } |
107 | | |
108 | 0 | if(a != s->allc) { |
109 | | /* this logic is not using Curl_saferealloc() to make the tool not have to |
110 | | include that as well when it uses this code */ |
111 | 0 | void *p = realloc(s->bufr, a); |
112 | 0 | if(!p) { |
113 | 0 | curlx_dyn_free(s); |
114 | 0 | return CURLE_OUT_OF_MEMORY; |
115 | 0 | } |
116 | 0 | s->bufr = p; |
117 | 0 | s->allc = a; |
118 | 0 | } |
119 | | |
120 | 0 | if(len) |
121 | 0 | memcpy(&s->bufr[indx], mem, len); |
122 | 0 | s->leng = indx + len; |
123 | 0 | s->bufr[s->leng] = 0; |
124 | 0 | return CURLE_OK; |
125 | 0 | } |
126 | | |
127 | | /* |
128 | | * Clears the string, keeps the allocation. This can also be called on a |
129 | | * buffer that already was freed. |
130 | | */ |
131 | | void curlx_dyn_reset(struct dynbuf *s) |
132 | 0 | { |
133 | 0 | DEBUGASSERT(s); |
134 | 0 | DEBUGASSERT(s->init == DYNINIT); |
135 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
136 | 0 | if(s->leng) |
137 | 0 | s->bufr[0] = 0; |
138 | 0 | s->leng = 0; |
139 | 0 | } |
140 | | |
141 | | /* |
142 | | * Specify the size of the tail to keep (number of bytes from the end of the |
143 | | * buffer). The rest will be dropped. |
144 | | */ |
145 | | CURLcode curlx_dyn_tail(struct dynbuf *s, size_t trail) |
146 | 0 | { |
147 | 0 | DEBUGASSERT(s); |
148 | 0 | DEBUGASSERT(s->init == DYNINIT); |
149 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
150 | 0 | if(trail > s->leng) |
151 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
152 | 0 | else if(trail == s->leng) |
153 | 0 | return CURLE_OK; |
154 | 0 | else if(!trail) { |
155 | 0 | curlx_dyn_reset(s); |
156 | 0 | } |
157 | 0 | else { |
158 | 0 | memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail); |
159 | 0 | s->leng = trail; |
160 | 0 | s->bufr[s->leng] = 0; |
161 | 0 | } |
162 | 0 | return CURLE_OK; |
163 | |
|
164 | 0 | } |
165 | | |
166 | | /* |
167 | | * Appends a buffer with length. |
168 | | */ |
169 | | CURLcode curlx_dyn_addn(struct dynbuf *s, const void *mem, size_t len) |
170 | 0 | { |
171 | 0 | DEBUGASSERT(s); |
172 | 0 | DEBUGASSERT(s->init == DYNINIT); |
173 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
174 | 0 | return dyn_nappend(s, mem, len); |
175 | 0 | } |
176 | | |
177 | | /* |
178 | | * Append a null-terminated string at the end. |
179 | | */ |
180 | | CURLcode curlx_dyn_add(struct dynbuf *s, const char *str) |
181 | 0 | { |
182 | 0 | size_t n; |
183 | 0 | DEBUGASSERT(str); |
184 | 0 | DEBUGASSERT(s); |
185 | 0 | DEBUGASSERT(s->init == DYNINIT); |
186 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
187 | 0 | n = strlen(str); |
188 | 0 | return dyn_nappend(s, (const unsigned char *)str, n); |
189 | 0 | } |
190 | | |
191 | | /* |
192 | | * Append a string vprintf()-style |
193 | | */ |
194 | | CURLcode curlx_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap) |
195 | 0 | { |
196 | 0 | #ifdef BUILDING_LIBCURL |
197 | 0 | int rc; |
198 | 0 | DEBUGASSERT(s); |
199 | 0 | DEBUGASSERT(s->init == DYNINIT); |
200 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
201 | 0 | DEBUGASSERT(fmt); |
202 | 0 | rc = curlx_dyn_vprintf(s, fmt, ap); |
203 | |
|
204 | 0 | if(!rc) |
205 | 0 | return CURLE_OK; |
206 | 0 | else if(rc == MERR_TOO_LARGE) |
207 | 0 | return CURLE_TOO_LARGE; |
208 | 0 | return CURLE_OUT_OF_MEMORY; |
209 | | #else |
210 | | char *str; |
211 | | str = curl_mvaprintf(fmt, ap); /* this allocs a new string to append */ |
212 | | |
213 | | if(str) { |
214 | | CURLcode result = dyn_nappend(s, (const unsigned char *)str, strlen(str)); |
215 | | free(str); |
216 | | return result; |
217 | | } |
218 | | /* If we failed, we cleanup the whole buffer and return error */ |
219 | | curlx_dyn_free(s); |
220 | | return CURLE_OUT_OF_MEMORY; |
221 | | #endif |
222 | 0 | } |
223 | | |
224 | | /* |
225 | | * Append a string printf()-style |
226 | | */ |
227 | | CURLcode curlx_dyn_addf(struct dynbuf *s, const char *fmt, ...) |
228 | 0 | { |
229 | 0 | CURLcode result; |
230 | 0 | va_list ap; |
231 | 0 | DEBUGASSERT(s); |
232 | 0 | DEBUGASSERT(s->init == DYNINIT); |
233 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
234 | 0 | va_start(ap, fmt); |
235 | 0 | result = curlx_dyn_vaddf(s, fmt, ap); |
236 | 0 | va_end(ap); |
237 | 0 | return result; |
238 | 0 | } |
239 | | |
240 | | /* |
241 | | * Returns a pointer to the buffer. |
242 | | */ |
243 | | char *curlx_dyn_ptr(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 s->bufr; |
249 | 0 | } |
250 | | |
251 | | char *curlx_dyn_take(struct dynbuf *s, size_t *plen) |
252 | 0 | { |
253 | 0 | char *ptr = s->bufr; |
254 | 0 | DEBUGASSERT(s); |
255 | 0 | DEBUGASSERT(s->init == DYNINIT); |
256 | 0 | *plen = s->leng; |
257 | 0 | s->bufr = NULL; |
258 | 0 | s->leng = 0; |
259 | 0 | s->allc = 0; |
260 | 0 | return ptr; |
261 | 0 | } |
262 | | |
263 | | /* |
264 | | * Returns an unsigned pointer to the buffer. |
265 | | */ |
266 | | unsigned char *curlx_dyn_uptr(const struct dynbuf *s) |
267 | 0 | { |
268 | 0 | DEBUGASSERT(s); |
269 | 0 | DEBUGASSERT(s->init == DYNINIT); |
270 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
271 | 0 | return (unsigned char *)s->bufr; |
272 | 0 | } |
273 | | |
274 | | /* |
275 | | * Returns the length of the buffer. |
276 | | */ |
277 | | size_t curlx_dyn_len(const struct dynbuf *s) |
278 | 0 | { |
279 | 0 | DEBUGASSERT(s); |
280 | 0 | DEBUGASSERT(s->init == DYNINIT); |
281 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
282 | 0 | return s->leng; |
283 | 0 | } |
284 | | |
285 | | /* |
286 | | * Set a new (smaller) length. |
287 | | */ |
288 | | CURLcode curlx_dyn_setlen(struct dynbuf *s, size_t set) |
289 | 0 | { |
290 | 0 | DEBUGASSERT(s); |
291 | 0 | DEBUGASSERT(s->init == DYNINIT); |
292 | 0 | DEBUGASSERT(!s->leng || s->bufr); |
293 | 0 | if(set > s->leng) |
294 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
295 | 0 | s->leng = set; |
296 | 0 | s->bufr[s->leng] = 0; |
297 | 0 | return CURLE_OK; |
298 | 0 | } |