1<!-- 2Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 4SPDX-License-Identifier: curl 5--> 6 7# dynbuf 8 9This is the internal module for creating and handling "dynamic buffers". This 10means buffers that can be appended to, dynamically and grow to adapt. 11 12There is always a terminating zero put at the end of the dynamic buffer. 13 14The `struct dynbuf` is used to hold data for each instance of a dynamic 15buffer. The members of that struct **MUST NOT** be accessed or modified 16without using the dedicated dynbuf API. 17 18## `Curl_dyn_init` 19 20```c 21void Curl_dyn_init(struct dynbuf *s, size_t toobig); 22``` 23 24This initializes a struct to use for dynbuf and it cannot fail. The `toobig` 25value **must** be set to the maximum size we allow this buffer instance to 26grow to. The functions below return `CURLE_OUT_OF_MEMORY` when hitting this 27limit. 28 29## `Curl_dyn_free` 30 31```c 32void Curl_dyn_free(struct dynbuf *s); 33``` 34 35Free the associated memory and clean up. After a free, the `dynbuf` struct can 36be reused to start appending new data to. 37 38## `Curl_dyn_addn` 39 40```c 41CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len); 42``` 43 44Append arbitrary data of a given length to the end of the buffer. 45 46If this function fails it calls `Curl_dyn_free` on `dynbuf`. 47 48## `Curl_dyn_add` 49 50```c 51CURLcode Curl_dyn_add(struct dynbuf *s, const char *str); 52``` 53 54Append a C string to the end of the buffer. 55 56If this function fails it calls `Curl_dyn_free` on `dynbuf`. 57 58## `Curl_dyn_addf` 59 60```c 61CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...); 62``` 63 64Append a `printf()`-style string to the end of the buffer. 65 66If this function fails it calls `Curl_dyn_free` on `dynbuf`. 67 68## `Curl_dyn_vaddf` 69 70```c 71CURLcode Curl_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap); 72``` 73 74Append a `vprintf()`-style string to the end of the buffer. 75 76If this function fails it calls `Curl_dyn_free` on `dynbuf`. 77 78## `Curl_dyn_reset` 79 80```c 81void Curl_dyn_reset(struct dynbuf *s); 82``` 83 84Reset the buffer length, but leave the allocation. 85 86## `Curl_dyn_tail` 87 88```c 89CURLcode Curl_dyn_tail(struct dynbuf *s, size_t length); 90``` 91 92Keep `length` bytes of the buffer tail (the last `length` bytes of the 93buffer). The rest of the buffer is dropped. The specified `length` must not be 94larger than the buffer length. To instead keep the leading part, see 95`Curl_dyn_setlen()`. 96 97## `Curl_dyn_ptr` 98 99```c 100char *Curl_dyn_ptr(const struct dynbuf *s); 101``` 102 103Returns a `char *` to the buffer if it has a length, otherwise may return 104NULL. Since the buffer may be reallocated, this pointer should not be trusted 105or used anymore after the next buffer manipulation call. 106 107## `Curl_dyn_uptr` 108 109```c 110unsigned char *Curl_dyn_uptr(const struct dynbuf *s); 111``` 112 113Returns an `unsigned char *` to the buffer if it has a length, otherwise may 114return NULL. Since the buffer may be reallocated, this pointer should not be 115trusted or used anymore after the next buffer manipulation call. 116 117## `Curl_dyn_len` 118 119```c 120size_t Curl_dyn_len(const struct dynbuf *s); 121``` 122 123Returns the length of the buffer in bytes. Does not include the terminating 124zero byte. 125 126## `Curl_dyn_setlen` 127 128```c 129CURLcode Curl_dyn_setlen(struct dynbuf *s, size_t len); 130``` 131 132Sets the new shorter length of the buffer in number of bytes. Keeps the 133leftmost set number of bytes, discards the rest. To instead keep the tail part 134of the buffer, see `Curl_dyn_tail()`. 135 136## `Curl_dyn_take` 137 138```c 139char *Curl_dyn_take(struct dynbuf *s, size_t *plen); 140``` 141 142Transfers ownership of the internal buffer to the caller. The dynbuf 143resets to its initial state. The returned pointer may be `NULL` if the 144dynbuf never allocated memory. The returned length is the amount of 145data written to the buffer. The actual allocated memory might be larger. 146