1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2020 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #include "curl_printf.h"
28 #ifdef BUILDING_LIBCURL
29 #include "curl_memory.h"
33 #define MIN_FIRST_ALLOC 32
35 #define DYNINIT 0xbee51da /* random pattern */
38 * Init a dynbuf struct.
40 void Curl_dyn_init(struct dynbuf *s, size_t toobig)
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.
57 void Curl_dyn_free(struct dynbuf *s)
60 Curl_safefree(s->bufr);
61 s->leng = s->allc = 0;
65 * Store/append an chunk of memory to the dynbuf.
67 static CURLcode dyn_nappend(struct dynbuf *s,
68 const unsigned char *mem, size_t len)
70 size_t indx = s->leng;
72 size_t fit = len + indx + 1; /* new string + old string + zero byte */
74 /* try to detect if there's rubbish in the struct */
75 DEBUGASSERT(s->init == DYNINIT);
76 DEBUGASSERT(s->toobig);
77 DEBUGASSERT(indx < s->toobig);
78 DEBUGASSERT(!s->leng || s->bufr);
82 return CURLE_OUT_OF_MEMORY;
87 if(fit < MIN_FIRST_ALLOC)
98 /* this logic is not using Curl_saferealloc() to make the tool not have to
99 include that as well when it uses this code */
100 void *p = realloc(s->bufr, a);
102 Curl_safefree(s->bufr);
103 s->leng = s->allc = 0;
104 return CURLE_OUT_OF_MEMORY;
111 memcpy(&s->bufr[indx], mem, len);
112 s->leng = indx + len;
113 s->bufr[s->leng] = 0;
118 * Clears the string, keeps the allocation. This can also be called on a
119 * buffer that already was freed.
121 void Curl_dyn_reset(struct dynbuf *s)
124 DEBUGASSERT(s->init == DYNINIT);
125 DEBUGASSERT(!s->leng || s->bufr);
132 * Specify the size of the tail to keep (number of bytes from the end of the
133 * buffer). The rest will be dropped.
135 CURLcode Curl_dyn_tail(struct dynbuf *s, size_t trail)
138 DEBUGASSERT(s->init == DYNINIT);
139 DEBUGASSERT(!s->leng || s->bufr);
141 return CURLE_BAD_FUNCTION_ARGUMENT;
142 else if(trail == s->leng)
148 memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail);
150 s->bufr[s->leng] = 0;
157 * Appends a buffer with length.
159 CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len)
162 DEBUGASSERT(s->init == DYNINIT);
163 DEBUGASSERT(!s->leng || s->bufr);
164 return dyn_nappend(s, mem, len);
168 * Append a null-terminated string at the end.
170 CURLcode Curl_dyn_add(struct dynbuf *s, const char *str)
172 size_t n = strlen(str);
174 DEBUGASSERT(s->init == DYNINIT);
175 DEBUGASSERT(!s->leng || s->bufr);
176 return dyn_nappend(s, (unsigned char *)str, n);
180 * Append a string vprintf()-style
182 CURLcode Curl_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap)
184 #ifdef BUILDING_LIBCURL
187 DEBUGASSERT(s->init == DYNINIT);
188 DEBUGASSERT(!s->leng || s->bufr);
189 rc = Curl_dyn_vprintf(s, fmt, ap);
195 str = vaprintf(fmt, ap); /* this allocs a new string to append */
198 CURLcode result = dyn_nappend(s, (unsigned char *)str, strlen(str));
202 /* If we failed, we cleanup the whole buffer and return error */
205 return CURLE_OUT_OF_MEMORY;
209 * Append a string printf()-style
211 CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...)
216 DEBUGASSERT(s->init == DYNINIT);
217 DEBUGASSERT(!s->leng || s->bufr);
219 result = Curl_dyn_vaddf(s, fmt, ap);
225 * Returns a pointer to the buffer.
227 char *Curl_dyn_ptr(const struct dynbuf *s)
230 DEBUGASSERT(s->init == DYNINIT);
231 DEBUGASSERT(!s->leng || s->bufr);
236 * Returns an unsigned pointer to the buffer.
238 unsigned char *Curl_dyn_uptr(const struct dynbuf *s)
241 DEBUGASSERT(s->init == DYNINIT);
242 DEBUGASSERT(!s->leng || s->bufr);
243 return (unsigned char *)s->bufr;
247 * Returns the length of the buffer.
249 size_t Curl_dyn_len(const struct dynbuf *s)
252 DEBUGASSERT(s->init == DYNINIT);
253 DEBUGASSERT(!s->leng || s->bufr);
258 * Set a new (smaller) length.
260 CURLcode Curl_dyn_setlen(struct dynbuf *s, size_t set)
263 DEBUGASSERT(s->init == DYNINIT);
264 DEBUGASSERT(!s->leng || s->bufr);
266 return CURLE_BAD_FUNCTION_ARGUMENT;
268 s->bufr[s->leng] = 0;