master
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"
26#include "curlx/dynbuf.h"
27#include "curl_printf.h"
29#define MIN_FIRST_ALLOC 32
31#ifdef DEBUGBUILD
32#define DYNINIT 0xbee51da /* random pattern */
33#endif
35/*
36 * Init a dynbuf struct.
37 */
38void curlx_dyn_init(struct dynbuf *s, size_t toobig)
39{
40 DEBUGASSERT(s);
41 DEBUGASSERT(toobig);
42 DEBUGASSERT(toobig <= MAX_DYNBUF_SIZE); /* catch crazy mistakes */
43 s->bufr = NULL;
44 s->leng = 0;
45 s->allc = 0;
46 s->toobig = toobig;
47#ifdef DEBUGBUILD
48 s->init = DYNINIT;
49#endif
50}
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 */
56void curlx_dyn_free(struct dynbuf *s)
57{
58 DEBUGASSERT(s);
59 DEBUGASSERT(s->init == DYNINIT);
60 curlx_safefree(s->bufr);
61 s->leng = s->allc = 0;
62}
64/*
65 * Store/append an chunk of memory to the dynbuf.
66 */
67static CURLcode dyn_nappend(struct dynbuf *s,
68 const unsigned char *mem, size_t len)
69{
70 size_t idx = s->leng;
71 size_t a = s->allc;
72 size_t fit = len + idx + 1; /* new string + old string + zero byte */
74 /* try to detect if there is rubbish in the struct */
75 DEBUGASSERT(s->init == DYNINIT);
76 DEBUGASSERT(s->toobig);
77 DEBUGASSERT(idx < s->toobig);
78 DEBUGASSERT(!s->leng || s->bufr);
79 DEBUGASSERT(a <= s->toobig);
80 DEBUGASSERT(!len || mem);
82 if(fit > s->toobig) {
83 curlx_dyn_free(s);
84 return CURLE_TOO_LARGE;
85 }
86 else if(!a) {
87 DEBUGASSERT(!idx);
88 /* first invoke */
89 if(MIN_FIRST_ALLOC > s->toobig)
90 a = s->toobig;
91 else if(fit < MIN_FIRST_ALLOC)
92 a = MIN_FIRST_ALLOC;
93 else
94 a = fit;
95 }
96 else {
97 while(a < fit)
98 a *= 2;
99 if(a > s->toobig)
100 /* no point in allocating a larger buffer than this is allowed to use */
101 a = s->toobig;
102 }
104 if(a != s->allc) {
105 void *p = curlx_realloc(s->bufr, a);
106 if(!p) {
107 curlx_dyn_free(s);
108 return CURLE_OUT_OF_MEMORY;
109 }
110 s->bufr = p;
111 s->allc = a;
112 }
114 if(len)
115 memcpy(&s->bufr[idx], mem, len);
116 s->leng = idx + len;
117 s->bufr[s->leng] = 0;
118 return CURLE_OK;
119}
121/*
122 * Clears the string, keeps the allocation. This can also be called on a
123 * buffer that already was freed.
124 */
125void curlx_dyn_reset(struct dynbuf *s)
126{
127 DEBUGASSERT(s);
128 DEBUGASSERT(s->init == DYNINIT);
129 DEBUGASSERT(!s->leng || s->bufr);
130 if(s->leng)
131 s->bufr[0] = 0;
132 s->leng = 0;
133}
135/*
136 * Specify the size of the tail to keep (number of bytes from the end of the
137 * buffer). The rest is dropped.
138 */
139CURLcode curlx_dyn_tail(struct dynbuf *s, size_t trail)
140{
141 DEBUGASSERT(s);
142 DEBUGASSERT(s->init == DYNINIT);
143 DEBUGASSERT(!s->leng || s->bufr);
144 if(trail > s->leng)
145 return CURLE_BAD_FUNCTION_ARGUMENT;
146 else if(trail == s->leng)
147 return CURLE_OK;
148 else if(!trail) {
149 curlx_dyn_reset(s);
150 }
151 else {
152 memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail);
153 s->leng = trail;
154 s->bufr[s->leng] = 0;
155 }
156 return CURLE_OK;
157}
159/*
160 * Appends a buffer with length.
161 */
162CURLcode curlx_dyn_addn(struct dynbuf *s, const void *mem, size_t len)
163{
164 DEBUGASSERT(s);
165 DEBUGASSERT(s->init == DYNINIT);
166 DEBUGASSERT(!s->leng || s->bufr);
167 return dyn_nappend(s, mem, len);
168}
170/*
171 * Append a null-terminated string at the end.
172 */
173CURLcode curlx_dyn_add(struct dynbuf *s, const char *str)
174{
175 size_t n;
176 DEBUGASSERT(str);
177 DEBUGASSERT(s);
178 DEBUGASSERT(s->init == DYNINIT);
179 DEBUGASSERT(!s->leng || s->bufr);
180 n = strlen(str);
181 return dyn_nappend(s, (const unsigned char *)str, n);
182}
184/*
185 * Append a string vprintf()-style
186 */
187CURLcode curlx_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 DEBUGASSERT(fmt);
195 rc = curlx_dyn_vprintf(s, fmt, ap);
197 if(!rc)
198 return CURLE_OK;
199 else if(rc == MERR_TOO_LARGE)
200 return CURLE_TOO_LARGE;
201 return CURLE_OUT_OF_MEMORY;
202#else
203 char *str;
204 str = curl_mvaprintf(fmt, ap); /* this allocs a new string to append */
206 if(str) {
207 CURLcode result = dyn_nappend(s, (const unsigned char *)str, strlen(str));
208 curl_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}
217/*
218 * Append a string printf()-style
219 */
220CURLcode curlx_dyn_addf(struct dynbuf *s, const char *fmt, ...)
221{
222 CURLcode result;
223 va_list ap;
224 DEBUGASSERT(s);
225 DEBUGASSERT(s->init == DYNINIT);
226 DEBUGASSERT(!s->leng || s->bufr);
227 DEBUGASSERT(strcmp(fmt, "%s")); /* use curlx_dyn_add instead */
228 va_start(ap, fmt);
229 result = curlx_dyn_vaddf(s, fmt, ap);
230 va_end(ap);
231 return result;
232}
234/*
235 * Returns a pointer to the buffer.
236 */
237char *curlx_dyn_ptr(const struct dynbuf *s)
238{
239 DEBUGASSERT(s);
240 DEBUGASSERT(s->init == DYNINIT);
241 DEBUGASSERT(!s->leng || s->bufr);
242 return s->bufr;
243}
245char *curlx_dyn_take(struct dynbuf *s, size_t *plen)
246{
247 char *ptr = s->bufr;
248 DEBUGASSERT(s);
249 DEBUGASSERT(s->init == DYNINIT);
250 *plen = s->leng;
251 s->bufr = NULL;
252 s->leng = 0;
253 s->allc = 0;
254 return ptr;
255}
257/*
258 * Returns an unsigned pointer to the buffer.
259 */
260unsigned char *curlx_dyn_uptr(const struct dynbuf *s)
261{
262 DEBUGASSERT(s);
263 DEBUGASSERT(s->init == DYNINIT);
264 DEBUGASSERT(!s->leng || s->bufr);
265 return (unsigned char *)s->bufr;
266}
268/*
269 * Returns the length of the buffer.
270 */
271size_t curlx_dyn_len(const struct dynbuf *s)
272{
273 DEBUGASSERT(s);
274 DEBUGASSERT(s->init == DYNINIT);
275 DEBUGASSERT(!s->leng || s->bufr);
276 return s->leng;
277}
279/*
280 * Set a new (smaller) length.
281 */
282CURLcode curlx_dyn_setlen(struct dynbuf *s, size_t set)
283{
284 DEBUGASSERT(s);
285 DEBUGASSERT(s->init == DYNINIT);
286 DEBUGASSERT(!s->leng || s->bufr);
287 if(set > s->leng)
288 return CURLE_BAD_FUNCTION_ARGUMENT;
289 s->leng = set;
290 s->bufr[s->leng] = 0;
291 return CURLE_OK;
292}
Diagnostics4 git calls · 111 ms
CallStatusSizeTime0111 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=106 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.2 KiB106 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2290 git.response.cache=MISS git.response.duration_ms=106 git.fetch.wants=1e5ea5177daf4bf4b16442525c2f1b55270114bb git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2290 git.pkt.payload.max=2269 git.sideband.band1.bytes=2268 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2f16f18dbfda9
- Request
- b32d958e…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format