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