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 | #define MIN_FIRST_ALLOC 32 |
34 | |
35 | #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 | { |
42 | DEBUGASSERT(s); |
43 | DEBUGASSERT(toobig); |
44 | s->bufr = NULL; |
45 | s->leng = 0; |
46 | s->allc = 0; |
47 | s->toobig = toobig; |
48 | #ifdef DEBUGBUILD |
49 | s->init = DYNINIT; |
50 | #endif |
51 | } |
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 | { |
59 | DEBUGASSERT(s); |
60 | Curl_safefree(s->bufr); |
61 | s->leng = s->allc = 0; |
62 | } |
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 | { |
70 | size_t indx = s->leng; |
71 | size_t a = s->allc; |
72 | 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 | DEBUGASSERT(s->init == DYNINIT); |
76 | DEBUGASSERT(s->toobig); |
77 | DEBUGASSERT(indx < s->toobig); |
78 | DEBUGASSERT(!s->leng || s->bufr); |
79 | DEBUGASSERT(a <= s->toobig); |
80 | |
81 | if(fit > s->toobig) { |
82 | Curl_dyn_free(s); |
83 | return CURLE_OUT_OF_MEMORY; |
84 | } |
85 | else if(!a) { |
86 | DEBUGASSERT(!indx); |
87 | /* first invoke */ |
88 | if(MIN_FIRST_ALLOC > s->toobig) |
89 | a = s->toobig; |
90 | else if(fit < MIN_FIRST_ALLOC) |
91 | a = MIN_FIRST_ALLOC; |
92 | else |
93 | a = fit; |
94 | } |
95 | else { |
96 | while(a < fit) |
97 | a *= 2; |
98 | if(a > s->toobig) |
99 | /* no point in allocating a larger buffer than this is allowed to use */ |
100 | a = s->toobig; |
101 | } |
102 | |
103 | 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 | void *p = realloc(s->bufr, a); |
107 | if(!p) { |
108 | Curl_dyn_free(s); |
109 | return CURLE_OUT_OF_MEMORY; |
110 | } |
111 | s->bufr = p; |
112 | s->allc = a; |
113 | } |
114 | |
115 | if(len) |
116 | memcpy(dest: &s->bufr[indx], src: mem, n: len); |
117 | s->leng = indx + len; |
118 | s->bufr[s->leng] = 0; |
119 | return CURLE_OK; |
120 | } |
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 | { |
128 | DEBUGASSERT(s); |
129 | DEBUGASSERT(s->init == DYNINIT); |
130 | DEBUGASSERT(!s->leng || s->bufr); |
131 | if(s->leng) |
132 | s->bufr[0] = 0; |
133 | s->leng = 0; |
134 | } |
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 | { |
142 | DEBUGASSERT(s); |
143 | DEBUGASSERT(s->init == DYNINIT); |
144 | DEBUGASSERT(!s->leng || s->bufr); |
145 | if(trail > s->leng) |
146 | return CURLE_BAD_FUNCTION_ARGUMENT; |
147 | else if(trail == s->leng) |
148 | return CURLE_OK; |
149 | else if(!trail) { |
150 | Curl_dyn_reset(s); |
151 | } |
152 | else { |
153 | memmove(dest: &s->bufr[0], src: &s->bufr[s->leng - trail], n: trail); |
154 | s->leng = trail; |
155 | s->bufr[s->leng] = 0; |
156 | } |
157 | return CURLE_OK; |
158 | |
159 | } |
160 | |
161 | /* |
162 | * Appends a buffer with length. |
163 | */ |
164 | CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len) |
165 | { |
166 | DEBUGASSERT(s); |
167 | DEBUGASSERT(s->init == DYNINIT); |
168 | DEBUGASSERT(!s->leng || s->bufr); |
169 | return dyn_nappend(s, mem, len); |
170 | } |
171 | |
172 | /* |
173 | * Append a null-terminated string at the end. |
174 | */ |
175 | CURLcode Curl_dyn_add(struct dynbuf *s, const char *str) |
176 | { |
177 | size_t n = strlen(s: str); |
178 | DEBUGASSERT(s); |
179 | DEBUGASSERT(s->init == DYNINIT); |
180 | DEBUGASSERT(!s->leng || s->bufr); |
181 | return dyn_nappend(s, mem: (unsigned char *)str, len: n); |
182 | } |
183 | |
184 | /* |
185 | * Append a string vprintf()-style |
186 | */ |
187 | CURLcode Curl_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap) |
188 | { |
189 | #ifdef BUILDING_LIBCURL |
190 | int rc; |
191 | DEBUGASSERT(s); |
192 | DEBUGASSERT(s->init == DYNINIT); |
193 | DEBUGASSERT(!s->leng || s->bufr); |
194 | rc = Curl_dyn_vprintf(dyn: s, format: fmt, ap_save: ap); |
195 | |
196 | if(!rc) |
197 | 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 | return CURLE_OUT_OF_MEMORY; |
211 | } |
212 | |
213 | /* |
214 | * Append a string printf()-style |
215 | */ |
216 | CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...) |
217 | { |
218 | CURLcode result; |
219 | va_list ap; |
220 | DEBUGASSERT(s); |
221 | DEBUGASSERT(s->init == DYNINIT); |
222 | DEBUGASSERT(!s->leng || s->bufr); |
223 | va_start(ap, fmt); |
224 | result = Curl_dyn_vaddf(s, fmt, ap); |
225 | va_end(ap); |
226 | return result; |
227 | } |
228 | |
229 | /* |
230 | * Returns a pointer to the buffer. |
231 | */ |
232 | char *Curl_dyn_ptr(const struct dynbuf *s) |
233 | { |
234 | DEBUGASSERT(s); |
235 | DEBUGASSERT(s->init == DYNINIT); |
236 | DEBUGASSERT(!s->leng || s->bufr); |
237 | return s->bufr; |
238 | } |
239 | |
240 | /* |
241 | * Returns an unsigned pointer to the buffer. |
242 | */ |
243 | unsigned char *Curl_dyn_uptr(const struct dynbuf *s) |
244 | { |
245 | DEBUGASSERT(s); |
246 | DEBUGASSERT(s->init == DYNINIT); |
247 | DEBUGASSERT(!s->leng || s->bufr); |
248 | return (unsigned char *)s->bufr; |
249 | } |
250 | |
251 | /* |
252 | * Returns the length of the buffer. |
253 | */ |
254 | size_t Curl_dyn_len(const struct dynbuf *s) |
255 | { |
256 | DEBUGASSERT(s); |
257 | DEBUGASSERT(s->init == DYNINIT); |
258 | DEBUGASSERT(!s->leng || s->bufr); |
259 | return s->leng; |
260 | } |
261 | |
262 | /* |
263 | * Set a new (smaller) length. |
264 | */ |
265 | CURLcode Curl_dyn_setlen(struct dynbuf *s, size_t set) |
266 | { |
267 | DEBUGASSERT(s); |
268 | DEBUGASSERT(s->init == DYNINIT); |
269 | DEBUGASSERT(!s->leng || s->bufr); |
270 | if(set > s->leng) |
271 | return CURLE_BAD_FUNCTION_ARGUMENT; |
272 | s->leng = set; |
273 | s->bufr[s->leng] = 0; |
274 | return CURLE_OK; |
275 | } |
276 | |