Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables

Buffer is a dynamic array, that can be resized if needed. More...

Data Structures

struct  sol_buffer
 A sol_buffer is a dynamic array, that can be resized if needed. More...
 

Macros

#define SOL_BUFFER_CAN_RESIZE(buf)   (!(buf->flags & (SOL_BUFFER_FLAGS_FIXED_CAPACITY | SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED)))
 Convenience flag to check if the buffer is resizable. More...
 
#define SOL_BUFFER_DECLARE_STATIC(name_, size_)
 A helper macro to create a static allocated buffer with a fixed capacity. More...
 
#define SOL_BUFFER_INIT_CONST(data_, size_)   SOL_BUFFER_C_CAST { .data = data_, .capacity = size_, .used = size_, .flags = SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED }
 Helper macro to initialize an buffer with const data. More...
 
#define SOL_BUFFER_INIT_DATA(data_, size_)   SOL_BUFFER_C_CAST { .data = data_, .capacity = size_, .used = size_, .flags = SOL_BUFFER_FLAGS_DEFAULT }
 Helper macro to initialize an buffer with the given data. More...
 
#define SOL_BUFFER_INIT_EMPTY   SOL_BUFFER_C_CAST { .data = NULL, .capacity = 0, .used = 0, .flags = SOL_BUFFER_FLAGS_DEFAULT }
 Helper macro to initialize an empty buffer. More...
 
#define SOL_BUFFER_INIT_FLAGS(data_, size_, flags_)   SOL_BUFFER_C_CAST { .data = data_, .capacity = size_, .used = 0, .flags = (enum sol_buffer_flags)(flags_) }
 Helper macro to initialize an buffer with the given data and flags. More...
 
#define SOL_BUFFER_NEEDS_NUL_BYTE(buf)   (!((buf)->flags & SOL_BUFFER_FLAGS_NO_NUL_BYTE))
 Convenience flag to check for flags not containing SOL_BUFFER_FLAGS_NO_NUL_BYTE, that is, buffers that needs the trailing nul byte terminator. More...
 

Typedefs

typedef struct sol_buffer sol_buffer
 A sol_buffer is a dynamic array, that can be resized if needed. More...
 

Enumerations

enum  sol_buffer_flags {
  SOL_BUFFER_FLAGS_DEFAULT = 0, SOL_BUFFER_FLAGS_FIXED_CAPACITY = (1 << 0), SOL_BUFFER_FLAGS_NO_FREE = (1 << 1), SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED = (SOL_BUFFER_FLAGS_FIXED_CAPACITY | SOL_BUFFER_FLAGS_NO_FREE),
  SOL_BUFFER_FLAGS_NO_NUL_BYTE = (1 << 2), SOL_BUFFER_FLAGS_CLEAR_MEMORY = (1 << 3)
}
 Flags used to set sol_buffer capabilities. More...
 
enum  sol_decode_case { SOL_DECODE_UPPERCASE, SOL_DECODE_LOWERCASE, SOL_DECODE_BOTH }
 Case of a string to be decoded. More...
 

Functions

int sol_buffer_append_as_base16 (struct sol_buffer *buf, const struct sol_str_slice slice, bool uppercase)
 Append the 'slice' at the end of 'buf' encoded as base16 (hexadecimal). More...
 
int sol_buffer_append_as_base64 (struct sol_buffer *buf, const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
 Append the 'slice' at the end of 'buf' encoded as base64 using the given map. More...
 
int sol_buffer_append_buffer (struct sol_buffer *dst, const struct sol_buffer *src)
 Appends the contents of the buffer from in the end of buf, reallocating if necessary. More...
 
int sol_buffer_append_bytes (struct sol_buffer *buf, const uint8_t *bytes, size_t size)
 Appends the bytes array to the end of buf, reallocating if necessary. More...
 
int sol_buffer_append_char (struct sol_buffer *buf, const char c)
 Appends character c into the end of buf, reallocating if necessary. More...
 
int sol_buffer_append_from_base16 (struct sol_buffer *buf, const struct sol_str_slice slice, enum sol_decode_case decode_case)
 Append the 'slice' at the end of 'buf' decoded from base16 (hexadecimal). More...
 
int sol_buffer_append_from_base64 (struct sol_buffer *buf, const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
 Append the 'slice' at the end of 'buf' decoded from base64 using the given map. More...
 
int static int sol_buffer_append_printf (struct sol_buffer *buf, const char *fmt,...) SOL_ATTR_PRINTF(2
 Append the formatted string in the end of the buffer (including trailing '\0'). More...
 
int sol_buffer_append_slice (struct sol_buffer *buf, const struct sol_str_slice slice)
 Appends slice into the end of buf, reallocating if necessary. More...
 
int sol_buffer_append_vprintf (struct sol_buffer *buf, const char *fmt, va_list args) SOL_ATTR_PRINTF(2
 Append the formatted string in the end of the buffer (including trailing '\0'). More...
 
static void * sol_buffer_at (const struct sol_buffer *buf, size_t pos)
 Returns a pointer to the data at position pos in the buffer buf. More...
 
static void * sol_buffer_at_end (const struct sol_buffer *buf)
 Returns a pointer to the end of the used portion of the buffer. More...
 
struct sol_buffersol_buffer_copy (const struct sol_buffer *buf)
 Allocate a new sol_buffer and a new data block and copy the contents of the provided sol_buffer. More...
 
int sol_buffer_ensure (struct sol_buffer *buf, size_t min_size)
 Ensures that buf has at least min_size. More...
 
int sol_buffer_ensure_nul_byte (struct sol_buffer *buf)
 Ensures that buffer has a terminating NULL byte. More...
 
int sol_buffer_expand (struct sol_buffer *buf, size_t bytes)
 Increment the buffer capacity to fit the bytes. More...
 
void sol_buffer_fini (struct sol_buffer *buf)
 Finalizes the buffer. More...
 
static void sol_buffer_free (struct sol_buffer *buf)
 Delete the buffer. More...
 
static struct sol_str_slice sol_buffer_get_slice (const struct sol_buffer *buf)
 Creates a string slice from the buffer's valid data. More...
 
static struct sol_str_slice sol_buffer_get_slice_at (const struct sol_buffer *buf, size_t pos)
 Creates a string slice from the buffer's data starting at position pos. More...
 
static void sol_buffer_init (struct sol_buffer *buf)
 Initializes a sol_buffer structure. More...
 
static void sol_buffer_init_flags (struct sol_buffer *buf, void *data, size_t data_size, enum sol_buffer_flags flags)
 Initializes a sol_buffer structure with the given data and flags. More...
 
int sol_buffer_insert_as_base16 (struct sol_buffer *buf, size_t pos, const struct sol_str_slice slice, bool uppercase)
 Insert the 'slice' into 'buf' at position 'pos' encoded as base16 (hexadecimal). More...
 
int sol_buffer_insert_as_base64 (struct sol_buffer *buf, size_t pos, const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
 Insert the 'slice' into 'buf' at position 'pos' encoded as base64 using the given map. More...
 
static int sol_buffer_insert_buffer (struct sol_buffer *dst, size_t pos, const struct sol_buffer *src)
 Insert the dst into src at position pos, reallocating if necessary. More...
 
int sol_buffer_insert_bytes (struct sol_buffer *buf, size_t pos, const uint8_t *bytes, size_t size)
 Insert the bytes array into buf at position pos, reallocating if necessary. More...
 
int sol_buffer_insert_char (struct sol_buffer *buf, size_t pos, const char c)
 Insert character c into buf at position pos, reallocating if necessary. More...
 
int sol_buffer_insert_from_base16 (struct sol_buffer *buf, size_t pos, const struct sol_str_slice slice, enum sol_decode_case decode_case)
 Insert the 'slice' into 'buf' at position 'pos' decoded from base16 (hexadecimal). More...
 
int sol_buffer_insert_from_base64 (struct sol_buffer *buf, size_t pos, const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
 Insert the 'slice' into 'buf' at position 'pos' decoded from base64 using the given map. More...
 
int static int sol_buffer_insert_printf (struct sol_buffer *buf, size_t pos, const char *fmt,...) SOL_ATTR_PRINTF(3
 Insert the formatted string in the given position in the buffer. More...
 
int sol_buffer_insert_slice (struct sol_buffer *buf, size_t pos, const struct sol_str_slice slice)
 Insert the slice into buf at position pos, reallocating if necessary. More...
 
int sol_buffer_insert_vprintf (struct sol_buffer *buf, size_t pos, const char *fmt, va_list args) SOL_ATTR_PRINTF(3
 Insert the formatted string in the given position in the buffer. More...
 
static struct sol_buffersol_buffer_new (void)
 Creates a new buffer. More...
 
int sol_buffer_remove_data (struct sol_buffer *buf, size_t offset, size_t size)
 Removes part of data inside the buffer rearranging the memory properly. More...
 
static void sol_buffer_reset (struct sol_buffer *buf)
 Reset the buffer content. More...
 
int sol_buffer_resize (struct sol_buffer *buf, size_t new_size)
 Resize the buffer to the given size. More...
 
static int sol_buffer_set_buffer (struct sol_buffer *dst, const struct sol_buffer *src)
 Copy src into dst, ensuring that will fit. More...
 
static int sol_buffer_set_buffer_at (struct sol_buffer *dst, size_t pos, const struct sol_buffer *src)
 Copy src into dst at position pos, ensuring that will fit. More...
 
int sol_buffer_set_char_at (struct sol_buffer *buf, size_t pos, char c)
 Set character c into buf at position pos, reallocating if necessary. More...
 
int sol_buffer_set_slice (struct sol_buffer *buf, const struct sol_str_slice slice)
 Copy slice into buf, ensuring that will fit. More...
 
int sol_buffer_set_slice_at (struct sol_buffer *buf, size_t pos, const struct sol_str_slice slice)
 Set the string slice slice into buf at position pos, reallocating if necessary. More...
 
void * sol_buffer_steal (struct sol_buffer *buf, size_t *size)
 'Steals' sol_buffer internal buffer and resets sol_buffer. More...
 
void * sol_buffer_steal_or_copy (struct sol_buffer *buf, size_t *size)
 'Steals' buf internal buffer and resets it. More...
 
struct sol_blobsol_buffer_to_blob (struct sol_buffer *buf)
 Convert a buffer to a struct sol_blob. More...
 
static int sol_buffer_trim (struct sol_buffer *buf)
 Frees memory that is not in being used by the buffer. More...
 

Variables

const char SOL_BASE64_MAP [66]
 The default base 64 map to use. More...
 

Detailed Description

Buffer is a dynamic array, that can be resized if needed.

See also Arena if you are allocating multiple pieces of data that will be deallocated twice.

See Also
Arena

Macro Definition Documentation

#define SOL_BUFFER_CAN_RESIZE (   buf)    (!(buf->flags & (SOL_BUFFER_FLAGS_FIXED_CAPACITY | SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED)))

Convenience flag to check if the buffer is resizable.

#define SOL_BUFFER_DECLARE_STATIC (   name_,
  size_ 
)
Value:
uint8_t name_ ## storage[(size_)] = { 0 }; \
struct sol_buffer name_ = SOL_BUFFER_INIT_FLAGS(name_ ## storage, (size_), SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED)
Buffers where the buf->data is not owned by sol_buffer, that is, it can't be resized and free() should...
Definition: sol-buffer.h:88
#define SOL_BUFFER_INIT_FLAGS(data_, size_, flags_)
Helper macro to initialize an buffer with the given data and flags.
Definition: sol-buffer.h:164
A sol_buffer is a dynamic array, that can be resized if needed.
Definition: sol-buffer.h:130

A helper macro to create a static allocated buffer with a fixed capacity.

This macro will expand into the following code:

// SOL_BUFFER_DECLARE_STATIC(buf, 1024);
uint8_t buf_storage[1024] = { 0 };
Parameters
name_The name of the struct sol_buffer variable
size_The capacity of the buffer
Examples:
/src/samples/bluetooth/browse.c, /src/samples/bluetooth/connect-paired.c, /src/samples/coap/iotivity-test-client.c, /src/samples/coap/lwm2m-client.c, /src/samples/coap/lwm2m-server.c, /src/samples/coap/oic-client.c, /src/samples/coap/simple-client.c, /src/samples/coap/simple-server.c, /src/samples/http/server-sse.c, and /src/samples/network/network-status.c.

Referenced by _on_network_event(), create_location_obj(), create_security_obj(), create_server_obj(), found_device(), found_resource(), found_resource_print(), got_get_response(), light_resource_to_rep(), location_changed_cb(), on_connect(), on_disconnect(), print_response(), reply_cb(), request_cb(), resource_notify(), write_security_tlv(), and write_server_tlv().

#define SOL_BUFFER_INIT_CONST (   data_,
  size_ 
)    SOL_BUFFER_C_CAST { .data = data_, .capacity = size_, .used = size_, .flags = SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED }

Helper macro to initialize an buffer with const data.

Also set the appropriated flag.

Parameters
data_Buffer initial data
size_Initial data size
Examples:
/src/samples/http/server-https.c, /src/samples/iio+network/iio-gyroscope-console-and-mqtt-publish.c, /src/samples/mqtt/mqtt-publish.c, and /src/samples/network/echo-client.c.

Referenced by iio_gyroscope_reader_cb(), main(), on_can_write(), and request_cb().

#define SOL_BUFFER_INIT_DATA (   data_,
  size_ 
)    SOL_BUFFER_C_CAST { .data = data_, .capacity = size_, .used = size_, .flags = SOL_BUFFER_FLAGS_DEFAULT }

Helper macro to initialize an buffer with the given data.

Parameters
data_Buffer initial data
size_Initial data size
#define SOL_BUFFER_INIT_EMPTY   SOL_BUFFER_C_CAST { .data = NULL, .capacity = 0, .used = 0, .flags = SOL_BUFFER_FLAGS_DEFAULT }
#define SOL_BUFFER_INIT_FLAGS (   data_,
  size_,
  flags_ 
)    SOL_BUFFER_C_CAST { .data = data_, .capacity = size_, .used = 0, .flags = (enum sol_buffer_flags)(flags_) }

Helper macro to initialize an buffer with the given data and flags.

Parameters
data_Buffer initial data
size_Initial data size
flags_Buffer flags
#define SOL_BUFFER_NEEDS_NUL_BYTE (   buf)    (!((buf)->flags & SOL_BUFFER_FLAGS_NO_NUL_BYTE))

Convenience flag to check for flags not containing SOL_BUFFER_FLAGS_NO_NUL_BYTE, that is, buffers that needs the trailing nul byte terminator.

Typedef Documentation

typedef struct sol_buffer sol_buffer

A sol_buffer is a dynamic array, that can be resized if needed.

It grows exponentially but also supports setting a specific size.

Useful to reduce the noise of handling realloc/size-variable manually.

See also sol_arena if you are allocating multiple pieces of data that will be deallocated twice.

Enumeration Type Documentation

Flags used to set sol_buffer capabilities.

Enumerator
SOL_BUFFER_FLAGS_DEFAULT 

Default flags: buffer may be resized and memory will be free'd at the end.

SOL_BUFFER_FLAGS_FIXED_CAPACITY 

Fixed capacity buffers won't be resized, sol_buffer_resize() will fail with -EPERM.

SOL_BUFFER_FLAGS_NO_FREE 

No free buffers won't call free(buf->data) at sol_buffer_fini().

SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED 

Buffers where the buf->data is not owned by sol_buffer, that is, it can't be resized and free() should not be called at it at sol_buffer_fini().

SOL_BUFFER_FLAGS_NO_NUL_BYTE 

Do not reserve space for the NUL byte.

SOL_BUFFER_FLAGS_CLEAR_MEMORY 

Securely clear buffer data before finishing.

Prefer using this flag combined with SOL_BUFFER_FLAGS_FIXED_CAPACITY, because of resizing overhead: every time buffer is resized, new memory is allocated, old memory is copied to new destination and old memory is cleared.

Case of a string to be decoded.

Used by functions sol_buffer_append_from_base16 or sol_buffer_insert_from_base16.

Enumerator
SOL_DECODE_UPPERCASE 
SOL_DECODE_LOWERCASE 
SOL_DECODE_BOTH 

Function Documentation

int sol_buffer_append_as_base16 ( struct sol_buffer buf,
const struct sol_str_slice  slice,
bool  uppercase 
)

Append the 'slice' at the end of 'buf' encoded as base16 (hexadecimal).

See https://en.wikipedia.org/wiki/Base16

Parameters
bufthe already-initialized buffer to append the encoded slice.
slicethe byte string to encode, may contain null bytes (\0), it will be encoded up the slice.len.
uppercaseif true, uppercase letters ABCDEF are used, otherwise lowercase abcdef are used instead.
Returns
0 on success, -errno on failure.
See Also
sol_buffer_insert_as_base16()
sol_buffer_insert_from_base16()
sol_buffer_append_from_base16()
Examples:
/src/samples/crypto/message-digest.c, and /src/samples/crypto/sha256sum.c.

Referenced by on_digest_ready(), and store_digest().

int sol_buffer_append_as_base64 ( struct sol_buffer buf,
const struct sol_str_slice  slice,
const char  base64_map[SOL_STATIC_ARRAY_SIZE(65)] 
)

Append the 'slice' at the end of 'buf' encoded as base64 using the given map.

See https://en.wikipedia.org/wiki/Base64

Parameters
bufthe already-initialized buffer to append the encoded slice.
slicethe byte string to encode, may contain null bytes (\0), it will be encoded up the slice.len.
base64_mapthe map to use, the default is available as SOL_BASE64_MAP. Note that the last char in the map (position 64) is used as the padding char.
Returns
0 on success, -errno on failure.
See Also
sol_buffer_insert_as_base64()
sol_buffer_insert_from_base64()
sol_buffer_append_from_base64()
int sol_buffer_append_buffer ( struct sol_buffer dst,
const struct sol_buffer src 
)

Appends the contents of the buffer from in the end of buf, reallocating if necessary.

Parameters
dstDestination buffer
srcThe buffer from where the data will be copied
Returns
0 on success, error code (always negative) otherwise
int sol_buffer_append_bytes ( struct sol_buffer buf,
const uint8_t *  bytes,
size_t  size 
)

Appends the bytes array to the end of buf, reallocating if necessary.

Parameters
bufDestination buffer
bytesBytes to be inserted
sizeNumber of bytes to insert
Returns
0 on success, error code (always negative) otherwise
int sol_buffer_append_char ( struct sol_buffer buf,
const char  c 
)

Appends character c into the end of buf, reallocating if necessary.

Parameters
bufDestination buffer
cCharacter to be appended
Returns
0 on success, error code (always negative) otherwise
int sol_buffer_append_from_base16 ( struct sol_buffer buf,
const struct sol_str_slice  slice,
enum sol_decode_case  decode_case 
)

Append the 'slice' at the end of 'buf' decoded from base16 (hexadecimal).

See https://en.wikipedia.org/wiki/Base16

Parameters
bufthe already-initialized buffer to append the decoded slice.
slicethe slice to decode, it must be a set of 0-9 or letters A-F (if uppercase) or a-f, otherwise decode fails.
decode_caseif SOL_DECODE_UPPERCASE, uppercase letters ABCDEF are used, if SOL_DECODE_LOWERCASE, lowercase abcdef are used instead. If SOL_DECODE_BOTH both, lowercase and uppercase, letters can be used.
Returns
0 on success, -errno on failure.
See Also
sol_buffer_insert_as_base16()
sol_buffer_append_as_base16()
sol_buffer_insert_from_base16()
int sol_buffer_append_from_base64 ( struct sol_buffer buf,
const struct sol_str_slice  slice,
const char  base64_map[SOL_STATIC_ARRAY_SIZE(65)] 
)

Append the 'slice' at the end of 'buf' decoded from base64 using the given map.

See https://en.wikipedia.org/wiki/Base64

Parameters
bufthe already-initialized buffer to append the decoded slice.
slicethe slice to decode, it must be composed solely of the base64_map characters or it will fail.
base64_mapthe map to use, the default is available as SOL_BASE64_MAP. Note that the last char in the map (position 64) is used as the padding char.
Returns
0 on success, -errno on failure.
See Also
sol_buffer_insert_as_base64()
sol_buffer_append_as_base64()
sol_buffer_insert_from_base64()
int static int static int sol_buffer_append_printf ( struct sol_buffer buf,
const char *  fmt,
  ... 
)
inlinestatic

Append the formatted string in the end of the buffer (including trailing '\0').

Parameters
bufThe buffer
fmtA standard 'printf()' format string
...The arguments to 'printf()'
Returns
0 on success, error code (always negative) otherwise
Examples:
/src/samples/coap/iotivity-test-client.c, /src/samples/coap/simple-server.c, and /src/samples/http/server-sse.c.

References sol_buffer_append_vprintf().

Referenced by dump_byte_string(), light_resource_to_rep(), and request_cb().

int sol_buffer_append_slice ( struct sol_buffer buf,
const struct sol_str_slice  slice 
)

Appends slice into the end of buf, reallocating if necessary.

Parameters
bufDestination buffer
sliceString slice to be appended
Returns
0 on success, error code (always negative) otherwise
Examples:
/src/samples/common/uart.c.

Referenced by producer_make_data(), and sol_json_serialize_null().

int sol_buffer_append_vprintf ( struct sol_buffer buf,
const char *  fmt,
va_list  args 
)

Append the formatted string in the end of the buffer (including trailing '\0').

Similar to sol_buffer_insert_printf, but receives va_list instead.

Parameters
bufThe buffer
fmtA standard 'printf()' format string
argsva_list to 'vprintf()'
Returns
0 on success, error code (always negative) otherwise

Referenced by sol_buffer_append_printf().

static void* sol_buffer_at ( const struct sol_buffer buf,
size_t  pos 
)
inlinestatic

Returns a pointer to the data at position pos in the buffer buf.

Parameters
bufThe buffer
posPosition of the data
Returns
Pointer to the data at the given position
Examples:
/src/samples/coap/simple-client.c, and /src/samples/coap/simple-server.c.

References sol_buffer::data, and sol_buffer::used.

Referenced by light_method_put(), reply_cb(), and sol_buffer_get_slice_at().

static void* sol_buffer_at_end ( const struct sol_buffer buf)
inlinestatic

Returns a pointer to the end of the used portion of the buffer.

Parameters
bufThe buffer
Returns
Pointer to the end of the buffer
Examples:
/src/samples/design_patterns/stream_sample.c.

References sol_buffer::data, and sol_buffer::used.

Referenced by _can_read().

struct sol_buffer* sol_buffer_copy ( const struct sol_buffer buf)

Allocate a new sol_buffer and a new data block and copy the contents of the provided sol_buffer.

After this call, user is responsible for calling fini on the buffer and freeing it afterwards. For it's memory to be freed properly, the flag SOL_BUFFER_FLAGS_NO_FREE will always be unset, despite the original buffer.

Parameters
bufBuffer to be copied
Returns
A copy of buf, NULL on error.
int sol_buffer_ensure ( struct sol_buffer buf,
size_t  min_size 
)

Ensures that buf has at least min_size.

If buffer has null-bytes (ie: null terminated strings), then the resized amount will include that null byte automatically. See SOL_BUFFER_FLAGS_NO_NUL_BYTE.

It may allocate more than requested to avoid subsequent reallocs, the internal heuristic rounds up to next power-of-2.

Parameters
bufThe buffer
min_sizeMinimum size
Returns
0 on success, error code (always negative) otherwise
int sol_buffer_ensure_nul_byte ( struct sol_buffer buf)

Ensures that buffer has a terminating NULL byte.

If flag SOL_BUFFER_FLAGS_NO_NUL_BYTE is not set.

Returns
a negative number in case it was not possible to ensure a terminating NULL byte - if flag SOL_BUFFER_FLAGS_NO_NUL_BYTE is set for instance, or if it could not resize the buffer
int sol_buffer_expand ( struct sol_buffer buf,
size_t  bytes 
)

Increment the buffer capacity to fit the bytes.

This function will increase the buffer capacity in order to be able to fit bytes.

If buffer has null-bytes (ie: null terminated strings), then the resized amount will include that null byte automatically. See SOL_BUFFER_FLAGS_NO_NUL_BYTE.

Parameters
bufThe buffer
bytesThe number of bytes that the buffer must fit.
Returns
0 on success, error code (always negative) otherwise
Note
Internally this function uses sol_buffer_ensure()
Examples:
/src/samples/design_patterns/stream_sample.c.

Referenced by _can_read().

void sol_buffer_fini ( struct sol_buffer buf)
static void sol_buffer_free ( struct sol_buffer buf)
inlinestatic

Delete the buffer.

Buffer is finalized and all memory is freed.

Parameters
bufBuffer to be deleted

References sol_buffer_fini().

static struct sol_str_slice sol_buffer_get_slice ( const struct sol_buffer buf)
static
static struct sol_str_slice sol_buffer_get_slice_at ( const struct sol_buffer buf,
size_t  pos 
)
static

Creates a string slice from the buffer's data starting at position pos.

Parameters
bufThe buffer
posStart position
Returns
String slice of the data

References sol_buffer_at(), and SOL_STR_SLICE_STR.

static void sol_buffer_init ( struct sol_buffer buf)
inlinestatic
static void sol_buffer_init_flags ( struct sol_buffer buf,
void *  data,
size_t  data_size,
enum sol_buffer_flags  flags 
)
inlinestatic

Initializes a sol_buffer structure with the given data and flags.

Parameters
bufPointer to buffer
dataPointer to buffer initial data
data_sizeSize of the initial data
flagsBuffer flags
Examples:
/src/samples/bluetooth/heartbeat.c, and /src/samples/design_patterns/stream_sample.c.

References sol_buffer::capacity, sol_buffer::data, sol_buffer::flags, SOL_BUFFER_FLAGS_MEMORY_NOT_OWNED, and sol_buffer::used.

Referenced by hrs_measurement_read(), and my_stream_api_new().

int sol_buffer_insert_as_base16 ( struct sol_buffer buf,
size_t  pos,
const struct sol_str_slice  slice,
bool  uppercase 
)

Insert the 'slice' into 'buf' at position 'pos' encoded as base16 (hexadecimal).

Parameters
bufthe already-initialized buffer to append the encoded slice.
posthe position in bytes from 0 up to buf->used. If pos == buf->end, then the behavior is the same as sol_buffer_append_as_base16().
slicethe byte string to encode, may contain null bytes (\0), it will be encoded up the slice.len.
uppercaseif true, uppercase letters ABCDEF are used, otherwise lowercase abcdef are used instead.
Returns
0 on success, -errno on failure.
See Also
sol_buffer_append_as_base16()
sol_buffer_insert_from_base16()
sol_buffer_append_from_base16()
int sol_buffer_insert_as_base64 ( struct sol_buffer buf,
size_t  pos,
const struct sol_str_slice  slice,
const char  base64_map[SOL_STATIC_ARRAY_SIZE(65)] 
)

Insert the 'slice' into 'buf' at position 'pos' encoded as base64 using the given map.

Parameters
bufthe already-initialized buffer to append the encoded slice.
posthe position in bytes from 0 up to buf->used. If pos == buf->end, then the behavior is the same as sol_buffer_append_as_base64().
slicethe byte string to encode, may contain null bytes (\0), it will be encoded up the slice.len.
base64_mapthe map to use, the default is available as SOL_BASE64_MAP. Note that the last char in the map (position 64) is used as the padding char.
Returns
0 on success, -errno on failure.
See Also
sol_buffer_append_as_base64()
sol_buffer_insert_from_base64()
sol_buffer_append_from_base64()
static int sol_buffer_insert_buffer ( struct sol_buffer dst,
size_t  pos,
const struct sol_buffer src 
)
inlinestatic

Insert the dst into src at position pos, reallocating if necessary.

If pos == src->end, then the behavior is the same as sol_buffer_append_buffer

Parameters
dstDestination buffer
posStart position
srcSource buffer
Returns
0 on success, error code (always negative) otherwise

References sol_buffer_get_slice(), and sol_buffer_insert_slice().

int sol_buffer_insert_bytes ( struct sol_buffer buf,
size_t  pos,
const uint8_t *  bytes,
size_t  size 
)

Insert the bytes array into buf at position pos, reallocating if necessary.

If pos == buf->end, then the behavior is the same as sol_buffer_append_bytes.

Parameters
bufDestination buffer
posStart position
bytesBytes to be inserted
sizeNumber of bytes to insert
Returns
0 on success, error code (always negative) otherwise
int sol_buffer_insert_char ( struct sol_buffer buf,
size_t  pos,
const char  c 
)

Insert character c into buf at position pos, reallocating if necessary.

If pos == buf->end, then the behavior is the same as sol_buffer_append_char and a trailing '\0' is guaranteed.

Parameters
bufDestination buffer
posStart position
cCharacter to be inserted
Returns
0 on success, error code (always negative) otherwise
int sol_buffer_insert_from_base16 ( struct sol_buffer buf,
size_t  pos,
const struct sol_str_slice  slice,
enum sol_decode_case  decode_case 
)

Insert the 'slice' into 'buf' at position 'pos' decoded from base16 (hexadecimal).

Parameters
bufthe already-initialized buffer to append the decoded slice.
posthe position in bytes from 0 up to buf->used. If pos == buf->end, then the behavior is the same as sol_buffer_append_from_base16().
slicethe slice to decode, it must be a set of 0-9 or letters A-F (if uppercase) or a-f, otherwise decode fails.
decode_caseif SOL_DECODE_UPPERCASE, uppercase letters ABCDEF are used, if SOL_DECODE_LOWERCASE, lowercase abcdef are used instead. If SOL_DECODE_BOTH both, lowercase and uppercase, letters can be used.
Returns
0 on success, -errno on failure.
See Also
sol_buffer_insert_as_base16()
sol_buffer_append_as_base16()
sol_buffer_append_from_base16()
int sol_buffer_insert_from_base64 ( struct sol_buffer buf,
size_t  pos,
const struct sol_str_slice  slice,
const char  base64_map[SOL_STATIC_ARRAY_SIZE(65)] 
)

Insert the 'slice' into 'buf' at position 'pos' decoded from base64 using the given map.

Parameters
bufthe already-initialized buffer to append the decoded slice.
posthe position in bytes from 0 up to buf->used. If pos == buf->end, then the behavior is the same as sol_buffer_append_from_base64().
slicethe slice to decode, it must be composed solely of the base64_map characters or it will fail.
base64_mapthe map to use, the default is available as SOL_BASE64_MAP. Note that the last char in the map (position 64) is used as the padding char.
Returns
0 on success, -errno on failure.
See Also
sol_buffer_insert_as_base64()
sol_buffer_append_as_base64()
sol_buffer_append_from_base64()
int static int static int sol_buffer_insert_printf ( struct sol_buffer buf,
size_t  pos,
const char *  fmt,
  ... 
)
inlinestatic

Insert the formatted string in the given position in the buffer.

Parameters
bufThe buffer
posPosition to start write the string
fmtA standard 'printf()' format string
...The arguments to 'printf()'
Returns
0 on success, error code (always negative) otherwise

References sol_buffer_insert_vprintf().

int sol_buffer_insert_slice ( struct sol_buffer buf,
size_t  pos,
const struct sol_str_slice  slice 
)

Insert the slice into buf at position pos, reallocating if necessary.

If pos == buf->end, then the behavior is the same as sol_buffer_append_slice

Parameters
bufDestination buffer
posStart position
sliceSource slice
Returns
0 on success, error code (always negative) otherwise

Referenced by sol_buffer_insert_buffer().

int sol_buffer_insert_vprintf ( struct sol_buffer buf,
size_t  pos,
const char *  fmt,
va_list  args 
)

Insert the formatted string in the given position in the buffer.

Similar to sol_buffer_insert_printf, but receives va_list instead.

If position == buf->pos, then the behavior is the same as sol_buffer_append_vprintf.

Parameters
bufThe buffer
posPosition to start write the string
fmtA standard 'printf()' format string
argsva_list to 'vprintf()'
Returns
0 on success, error code (always negative) otherwise

Referenced by sol_buffer_insert_printf().

static struct sol_buffer* sol_buffer_new ( void  )
static

Creates a new buffer.

Returns
Pointer to the new buffer

References sol_buffer_init().

int sol_buffer_remove_data ( struct sol_buffer buf,
size_t  offset,
size_t  size 
)

Removes part of data inside the buffer rearranging the memory properly.

It's removed up to the buffer's size in case of size greater than used data.

Parameters
bufThe buffer (already-initialized)
offsetPosition (from begin of the buffer) where
sizeAmount of data (in bytes) that should be removed size bytes will be removed
Returns
0 on success, error code (always negative) otherwise
Note
the buffer keeps its capacity after this function, it means, the data is not released. If that is wanted, one should call sol_buffer_trim
Examples:
/src/samples/design_patterns/stream_sample.c.

Referenced by _inform_user().

static void sol_buffer_reset ( struct sol_buffer buf)
inlinestatic

Reset the buffer content.

All allocated memory is kept.

Parameters
bufThe buffer

References sol_buffer::used.

int sol_buffer_resize ( struct sol_buffer buf,
size_t  new_size 
)

Resize the buffer to the given size.

The new size will be exactly of the given new_size, no null-byte is automatically handled and if used member is larger than the new size, then it's limited to that amount (clamp).

Parameters
bufThe buffer
new_sizeNew size
Returns
0 on success, error code (always negative) otherwise

Referenced by sol_buffer_trim().

static int sol_buffer_set_buffer ( struct sol_buffer dst,
const struct sol_buffer src 
)
inlinestatic

Copy src into dst, ensuring that will fit.

If data exists, then it won't be moved/shiffted, instead it will be overriden.

Parameters
dstThe buffer's destiny
srcThe source data
Returns
0 on success, error code (always negative) otherwise

References sol_buffer_get_slice(), and sol_buffer_set_slice().

static int sol_buffer_set_buffer_at ( struct sol_buffer dst,
size_t  pos,
const struct sol_buffer src 
)
inlinestatic

Copy src into dst at position pos, ensuring that will fit.

The memory regions of src and dst may overlap.

Parameters
dstThe buffer's destiny
posStart position
srcThe source data
Returns
0 on success, error code (always negative) otherwise

References sol_buffer_get_slice(), and sol_buffer_set_slice_at().

int sol_buffer_set_char_at ( struct sol_buffer buf,
size_t  pos,
char  c 
)

Set character c into buf at position pos, reallocating if necessary.

If pos == buf->end, then the behavior is the same as sol_buffer_insert_char

Parameters
bufDestination buffer
posStart position
cCharacter to be set
Returns
0 on success, error code (always negative) otherwise
int sol_buffer_set_slice ( struct sol_buffer buf,
const struct sol_str_slice  slice 
)

Copy slice into buf, ensuring that will fit.

Also includes an extra NULL byte so the buffer data can be used as a C string.

If data exists, then it won't be moved/shiffted, instead it will be overriden. Example:

  • buffer "abcd", slice "XY", result: "XYcd";
  • buffer "XY", slice "abcd", result: "abcd".
Parameters
bufThe buffer
sliceString slice
Returns
0 on success, error code (always negative) otherwise
Examples:
/src/samples/http/server.c.

Referenced by sol_buffer_set_buffer(), and startup_server().

int sol_buffer_set_slice_at ( struct sol_buffer buf,
size_t  pos,
const struct sol_str_slice  slice 
)

Set the string slice slice into buf at position pos, reallocating if necessary.

The memory regions of slice and buf may overlap.

If pos == buf->end, then the behavior is the same as sol_buffer_append_slice.

If data exists after pos, then it won't be moved/shiffted, instead it will be overriden. Example:

  • buffer "abcd", slice "XY", pos: 4, result: "abcdXY";
  • buffer "abcd", slice "XY", pos: 3, result: "abcXY";
  • buffer "abcd", slice "XY", pos: 2, result: "abXY";
  • buffer "abcd", slice "XY", pos: 1, result: "aXYd";
  • buffer "abcd", slice "XY", pos: 0, result: "XYcd";
Parameters
bufDestination buffer
posStart position
sliceString slice to be set
Returns
0 on success, error code (always negative) otherwise

Referenced by sol_buffer_set_buffer_at().

void* sol_buffer_steal ( struct sol_buffer buf,
size_t *  size 
)

'Steals' sol_buffer internal buffer and resets sol_buffer.

After this call, user is responsible for the memory returned.

Parameters
bufbuffer to have it's internal buffer stolen
sizeif not NULL, will store memory returned size
Returns
buffer internal buffer. It's caller responsibility now to free this memory
Note
If buffer was allocated with sol_buffer_new(), it still needs to be freed by calling sol_buffer_free();
If the buffer flags are set to SOL_BUFFER_FLAGS_NO_FREE, this function will return NULL.
Examples:
/src/samples/coap/simple-server.c, and /src/samples/common/uart.c.

Referenced by light_resource_to_rep(), producer_make_data(), sol_efivars_read_string(), sol_fs_read_string(), and sol_memmap_read_string().

void* sol_buffer_steal_or_copy ( struct sol_buffer buf,
size_t *  size 
)

'Steals' buf internal buffer and resets it.

If the SOL_BUFFER_FLAGS_NO_FREE was set, it will return a copy of the buffer's data.

After this call, user is responsible for the memory returned.

Parameters
bufbuffer to have it's internal buffer stolen or copied
sizeif not NULL, will store memory returned size
Returns
buffer internal buffer. It's caller responsibility now to free this memory
Note
If buffer was allocated with sol_buffer_new(), it still needs to be freed by calling sol_buffer_free();
See Also
sol_buffer_steal
struct sol_blob* sol_buffer_to_blob ( struct sol_buffer buf)

Convert a buffer to a struct sol_blob.

The buffer will be stolen by the created blob.

Parameters
bufThe buf to be transformed in a blob
Returns
a blob or NULL on error
Examples:
/src/samples/coap/lwm2m-client.c, and /src/samples/http/server-sse.c.

Referenced by create_security_obj(), create_server_obj(), on_stdin(), write_security_tlv(), and write_server_tlv().

static int sol_buffer_trim ( struct sol_buffer buf)
inlinestatic

Frees memory that is not in being used by the buffer.

If buffer has null-bytes (ie: null terminated strings), then the resized amount will include that null byte automatically. See SOL_BUFFER_FLAGS_NO_NUL_BYTE.

Parameters
bufThe buffer
Returns
0 on success, error code (always negative) otherwise

References sol_buffer::capacity, sol_buffer::flags, SOL_BUFFER_FLAGS_NO_NUL_BYTE, sol_buffer_resize(), and sol_buffer::used.

Variable Documentation

const char SOL_BASE64_MAP[66]

The default base 64 map to use.

The last byte (position 64) is the padding character. This is a NUL terminated string.