1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2020, 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.haxx.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 ***************************************************************************/
23 #include "curl_setup.h"
25 #include "curl_printf.h"
26 #ifdef BUILDING_LIBCURL
27 #include "curl_memory.h"
31 #define MIN_FIRST_ALLOC 32
33 #define DYNINIT 0xbee51da /* random pattern */
36 * Init a dynbuf struct.
38 void Curl_dyn_init(struct dynbuf *s, size_t toobig)
52 * free the buffer and re-init the necessary fields. It doesn't touch the
53 * 'init' field and thus this buffer can be reused to add data to again.
55 void Curl_dyn_free(struct dynbuf *s)
58 Curl_safefree(s->bufr);
59 s->leng = s->allc = 0;
63 * Store/append an chunk of memory to the dynbuf.
65 static CURLcode dyn_nappend(struct dynbuf *s,
66 const unsigned char *mem, size_t len)
68 size_t indx = s->leng;
70 size_t fit = len + indx + 1; /* new string + old string + zero byte */
72 /* try to detect if there's rubbish in the struct */
73 DEBUGASSERT(s->init == DYNINIT);
74 DEBUGASSERT(s->toobig);
75 DEBUGASSERT(indx < s->toobig);
76 DEBUGASSERT(!s->leng || s->bufr);
80 return CURLE_OUT_OF_MEMORY;
85 if(fit < MIN_FIRST_ALLOC)
96 /* this logic is not using Curl_saferealloc() to make the tool not have to
97 include that as well when it uses this code */
98 void *p = realloc(s->bufr, a);
100 Curl_safefree(s->bufr);
101 s->leng = s->allc = 0;
102 return CURLE_OUT_OF_MEMORY;
109 memcpy(&s->bufr[indx], mem, len);
110 s->leng = indx + len;
111 s->bufr[s->leng] = 0;
116 * Clears the string, keeps the allocation. This can also be called on a
117 * buffer that already was freed.
119 void Curl_dyn_reset(struct dynbuf *s)
122 DEBUGASSERT(s->init == DYNINIT);
123 DEBUGASSERT(!s->leng || s->bufr);
131 * Specify the size of the tail to keep (number of bytes from the end of the
132 * buffer). The rest will be dropped.
134 CURLcode Curl_dyn_tail(struct dynbuf *s, size_t trail)
137 DEBUGASSERT(s->init == DYNINIT);
138 DEBUGASSERT(!s->leng || s->bufr);
140 return CURLE_BAD_FUNCTION_ARGUMENT;
141 else if(trail == s->leng)
147 memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail);
149 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);