2020-05-02 23:04:08 +08:00
|
|
|
# dynbuf
|
|
|
|
|
|
|
|
This is the internal module for creating and handling "dynamic buffers". This
|
2021-11-26 15:46:59 +08:00
|
|
|
means buffers that can be appended to, dynamically and grow to adapt.
|
2020-05-02 23:04:08 +08:00
|
|
|
|
|
|
|
There will always be a terminating zero put at the end of the dynamic buffer.
|
|
|
|
|
|
|
|
The `struct dynbuf` is used to hold data for each instance of a dynamic
|
|
|
|
buffer. The members of that struct **MUST NOT** be accessed or modified
|
|
|
|
without using the dedicated dynbuf API.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_init`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
void Curl_dyn_init(struct dynbuf *s, size_t toobig);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
This initializes a struct to use for dynbuf and it cannot fail. The `toobig`
|
|
|
|
value **must** be set to the maximum size we allow this buffer instance to
|
|
|
|
grow to. The functions below will return `CURLE_OUT_OF_MEMORY` when hitting
|
|
|
|
this limit.
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_free`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
void Curl_dyn_free(struct dynbuf *s);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
|
|
|
Free the associated memory and clean up. After a free, the `dynbuf` struct can
|
|
|
|
be re-used to start appending new data to.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_addn`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
|
|
|
Append arbitrary data of a given length to the end of the buffer.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_add`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
CURLcode Curl_dyn_add(struct dynbuf *s, const char *str);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
|
|
|
Append a C string to the end of the buffer.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_addf`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
|
|
|
Append a `printf()`-style string to the end of the buffer.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_vaddf`
|
2020-09-23 15:21:36 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
CURLcode Curl_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap);
|
|
|
|
```
|
2020-09-23 15:21:36 +08:00
|
|
|
|
|
|
|
Append a `vprintf()`-style string to the end of the buffer.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_reset`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
void Curl_dyn_reset(struct dynbuf *s);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
|
|
|
Reset the buffer length, but leave the allocation.
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_tail`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
CURLcode Curl_dyn_tail(struct dynbuf *s, size_t length);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
|
|
|
Keep `length` bytes of the buffer tail (the last `length` bytes of the
|
|
|
|
buffer). The rest of the buffer is dropped. The specified `length` must not be
|
2022-09-01 16:16:24 +08:00
|
|
|
larger than the buffer length. To instead keep the leading part, see
|
|
|
|
`Curl_dyn_setlen()`.
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_ptr`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
char *Curl_dyn_ptr(const struct dynbuf *s);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2022-03-28 15:39:09 +08:00
|
|
|
Returns a `char *` to the buffer if it has a length, otherwise may return
|
|
|
|
NULL. Since the buffer may be reallocated, this pointer should not be trusted
|
|
|
|
or used anymore after the next buffer manipulation call.
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_uptr`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
unsigned char *Curl_dyn_uptr(const struct dynbuf *s);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2022-03-28 15:39:09 +08:00
|
|
|
Returns an `unsigned char *` to the buffer if it has a length, otherwise may
|
|
|
|
return NULL. Since the buffer may be reallocated, this pointer should not be
|
|
|
|
trusted or used anymore after the next buffer manipulation call.
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_len`
|
2020-05-02 23:04:08 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
size_t Curl_dyn_len(const struct dynbuf *s);
|
|
|
|
```
|
2020-05-02 23:04:08 +08:00
|
|
|
|
|
|
|
Returns the length of the buffer in bytes. Does not include the terminating
|
|
|
|
zero byte.
|
2022-09-01 16:16:24 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## `Curl_dyn_setlen`
|
2022-09-01 16:16:24 +08:00
|
|
|
|
|
|
|
```c
|
|
|
|
CURLcode Curl_dyn_setlen(struct dynbuf *s, size_t len);
|
|
|
|
```
|
|
|
|
|
|
|
|
Sets the new shorter length of the buffer in number of bytes. Keeps the
|
|
|
|
leftmost set number of bytes, discards the rest. To instead keep the tail part
|
|
|
|
of the buffer, see `Curl_dyn_tail()`.
|