unbound  0.1
Functions
sbuffer.c File Reference

This file contains the definition of sldns_buffer, and functions to manipulate those. More...

#include "config.h"
#include "sldns/sbuffer.h"
#include <stdarg.h>

Functions

sldns_buffersldns_buffer_new (size_t capacity)
 creates a new buffer with the specified capacity. More...
 
void sldns_buffer_new_frm_data (sldns_buffer *buffer, void *data, size_t size)
 creates a buffer with the specified data. More...
 
void sldns_buffer_init_frm_data (sldns_buffer *buffer, void *data, size_t size)
 Setup a buffer with the data pointed to. More...
 
void sldns_buffer_init_vfixed_frm_data (sldns_buffer *buffer, void *data, size_t size)
 Setup a buffer with the data pointed to. More...
 
int sldns_buffer_set_capacity (sldns_buffer *buffer, size_t capacity)
 changes the buffer's capacity. More...
 
int sldns_buffer_reserve (sldns_buffer *buffer, size_t amount)
 ensures BUFFER can contain at least AMOUNT more bytes. More...
 
int sldns_buffer_printf (sldns_buffer *buffer, const char *format,...)
 
void sldns_buffer_free (sldns_buffer *buffer)
 frees the buffer. More...
 
void * sldns_buffer_export (sldns_buffer *buffer)
 Makes the buffer fixed and returns a pointer to the data. More...
 
void sldns_buffer_copy (sldns_buffer *result, sldns_buffer *from)
 Copy contents of the from buffer to the result buffer and then flips the result buffer. More...
 

Detailed Description

This file contains the definition of sldns_buffer, and functions to manipulate those.

Function Documentation

◆ sldns_buffer_new()

sldns_buffer* sldns_buffer_new ( size_t  capacity)

◆ sldns_buffer_new_frm_data()

void sldns_buffer_new_frm_data ( sldns_buffer buffer,
void *  data,
size_t  size 
)

creates a buffer with the specified data.

The data IS copied and MEMORY allocations are done. The buffer is not fixed and can be resized using buffer_reserve().

Parameters
[in]bufferpointer to the buffer to put the data in
[in]datathe data to encapsulate in the buffer
[in]sizethe size of the data

References sldns_buffer::_capacity, sldns_buffer::_data, sldns_buffer::_fixed, sldns_buffer::_limit, sldns_buffer::_position, sldns_buffer::_status_err, and sldns_buffer::_vfixed.

Referenced by hex_buffer2wire().

◆ sldns_buffer_init_frm_data()

void sldns_buffer_init_frm_data ( sldns_buffer buffer,
void *  data,
size_t  size 
)

Setup a buffer with the data pointed to.

No data copied, no memory allocs. The buffer is fixed.

Parameters
[in]bufferpointer to the buffer to put the data in
[in]datathe data to encapsulate in the buffer
[in]sizethe size of the data

References sldns_buffer::_capacity, sldns_buffer::_data, sldns_buffer::_fixed, sldns_buffer::_limit, and sldns_buffer::_vfixed.

Referenced by sldns_str2wire_ipseckey_buf(), sldns_str2wire_nsec_buf(), and sldns_str2wire_wks_buf().

◆ sldns_buffer_init_vfixed_frm_data()

void sldns_buffer_init_vfixed_frm_data ( sldns_buffer buffer,
void *  data,
size_t  size 
)

Setup a buffer with the data pointed to.

No data copied, no memory allocs. The buffer is "virtually" fixed. Writes beyond size (the capacity) will only update position, but no data will be written beyond capacity. This allows to determine how big the buffer should have been to contain all the written data, by looking at the position with sldns_buffer_position(), similarly to the return value of POSIX's snprintf.

Parameters
[in]bufferpointer to the buffer to put the data in
[in]datathe data to encapsulate in the buffer
[in]sizethe size of the data

References sldns_buffer::_capacity, sldns_buffer::_data, sldns_buffer::_fixed, sldns_buffer::_limit, and sldns_buffer::_vfixed.

◆ sldns_buffer_set_capacity()

int sldns_buffer_set_capacity ( sldns_buffer buffer,
size_t  capacity 
)

changes the buffer's capacity.

The data is reallocated so any pointers to the data may become invalid. The buffer's limit is set to the buffer's new capacity.

Parameters
[in]bufferthe buffer
[in]capacitythe capacity to use
Returns
whether this failed or succeeded

◆ sldns_buffer_reserve()

int sldns_buffer_reserve ( sldns_buffer buffer,
size_t  amount 
)

ensures BUFFER can contain at least AMOUNT more bytes.

The buffer's capacity is increased if necessary using buffer_set_capacity().

The buffer's limit is always set to the (possibly increased) capacity.

Parameters
[in]bufferthe buffer
[in]amountamount to use
Returns
whether this failed or succeeded

◆ sldns_buffer_free()

void sldns_buffer_free ( sldns_buffer buffer)

◆ sldns_buffer_export()

void* sldns_buffer_export ( sldns_buffer buffer)

Makes the buffer fixed and returns a pointer to the data.

The caller is responsible for free'ing the result.

Parameters
[in]*bufferthe buffer to be exported
Returns
void

References sldns_buffer::_data, and sldns_buffer::_fixed.

◆ sldns_buffer_copy()

void sldns_buffer_copy ( sldns_buffer result,
sldns_buffer from 
)

Copy contents of the from buffer to the result buffer and then flips the result buffer.

Data will be silently truncated if the result buffer is too small.

Parameters
[out]*resultresulting buffer which is copied to.
[in]*fromwhat to copy to result.

References sldns_buffer_begin(), sldns_buffer_capacity(), sldns_buffer_clear(), sldns_buffer_flip(), sldns_buffer_limit(), and sldns_buffer_write().

Referenced by entry_to_buf(), and mesh_make_new_space().