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