The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Macros | Typedefs
dbuff.h File Reference

A generic data buffer structure for encoding and decoding. More...

#include <errno.h>
#include <freeradius-devel/missing.h>
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/nbo.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
+ Include dependency graph for dbuff.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  fr_dbuff_thread_local_t
 Structure to encapsulate a thread local dbuff information. More...
 

Macros

#define FR_DBUFF_ADV_PARENT_CURRENT   0x01
 Advance current position of parent. More...
 
#define FR_DBUFF_ADV_PARENT_END   0x02
 Advance end pointer of parent. More...
 
#define FR_DBUFF_RETURN(_func, ...)
 Generic wrapper macro to return if there's insufficient memory to satisfy the request on the dbuff. More...
 

Typedefs

typedef size_t(* fr_dbuff_extend_t) (fr_dbuff_t *dbuff, size_t req_extension)
 dbuff extension callback More...
 
typedef struct fr_dbuff_marker_s fr_dbuff_marker_t
 A position marker associated with a dbuff. More...
 
typedef struct fr_dbuff_s fr_dbuff_t
 A dbuff. More...
 

Initialisers

static int _dbuff_thread_local_free (void *dbtl)
 
size_t _fr_dbuff_extend_fd (fr_dbuff_t *dbuff, size_t extension)
 Refresh the buffer with more data from the file. More...
 
size_t _fr_dbuff_extend_talloc (fr_dbuff_t *dbuff, size_t extension)
 Reallocate the current buffer. More...
 
#define FR_DBUFF(_dbuff_or_marker)   _FR_DBUFF(_dbuff_or_marker, fr_dbuff_current(_dbuff_or_marker), 0x00)
 Create a new dbuff pointing to the same underlying buffer. More...
 
#define FR_DBUFF_ABS(_dbuff_or_marker)   _FR_DBUFF(_dbuff_or_marker, fr_dbuff_start(_dbuff_or_marker), 0x00)
 Create a new dbuff pointing to the same underlying buffer. More...
 
#define FR_DBUFF_BIND_CURRENT(_dbuff_or_marker)   _FR_DBUFF(_dbuff_or_marker, fr_dbuff_current(_dbuff_or_marker), FR_DBUFF_ADV_PARENT_CURRENT)
 Create a new dbuff pointing to the same underlying buffer. More...
 
#define FR_DBUFF_BIND_CURRENT_ABS(_dbuff_or_marker)   FR_DBUFF_ABS(_dbuff_or_marker, fr_dbuff_start(_dbuff_or_marker), FR_DBUFF_ADV_PARENT_CURRENT)
 Create a new dbuff pointing to the same underlying buffer. More...
 
#define FR_DBUFF_BIND_END_ABS(_dbuff_or_marker)   _FR_DBUFF(_dbuff_or_marker, fr_dbuff_start(_dbuff_or_marker), FR_DBUFF_ADV_PARENT_END)
 Create a new dbuff pointing to the same underlying buffer. More...
 
static void fr_dbuff_free_talloc (fr_dbuff_t *dbuff)
 Free the talloc buffer associated with a dbuff. More...
 
#define fr_dbuff_init(_out, _start, _len_or_end)
 Initialise an dbuff for encoding or decoding. More...
 
static fr_dbuff_tfr_dbuff_init_fd (fr_dbuff_t *dbuff, fr_dbuff_uctx_fd_t *fctx, uint8_t *buff, size_t len, int fd, size_t max)
 Initialise a special dbuff which automatically reads in more data as the buffer is exhausted. More...
 
static fr_dbuff_tfr_dbuff_init_talloc (TALLOC_CTX *ctx, fr_dbuff_t *dbuff, fr_dbuff_uctx_talloc_t *tctx, size_t init, size_t max)
 Initialise a special dbuff which automatically extends as additional data is written. More...
 
#define FR_DBUFF_MAX(_dbuff_or_marker, _max)   _FR_DBUFF_MAX(_dbuff_or_marker, _max, 0x00)
 Limit the maximum number of bytes available in the dbuff when passing it to another function. More...
 
#define FR_DBUFF_MAX_BIND_CURRENT(_dbuff_or_marker, _max)   _FR_DBUFF_MAX(_dbuff_or_marker, _max, FR_DBUFF_ADV_PARENT_CURRENT)
 Limit the maximum number of bytes available in the dbuff when passing it to another function. More...
 
int fr_dbuff_reset_talloc (fr_dbuff_t *dbuff)
 Reset a talloced buffer to its initial length, clearing any data stored. More...
 
#define FR_DBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max)
 Create a function local and thread local extensible dbuff. More...
 
#define FR_DBUFF_TMP(_start, _len_or_end)
 Creates a compound literal to pass into functions which accept a dbuff. More...
 
int fr_dbuff_trim_talloc (fr_dbuff_t *dbuff, size_t len)
 Trim a talloced dbuff to the minimum length required to represent the contained string. More...
 

Extension requests

These functions/macros may be used to request that the underlying buffer is either extended to accommodate more data, or that data is shifted out of the buffer, and that the buffer is refilled.

#define fr_dbuff_extend(_dbuff)   fr_dbuff_extend_lowat(NULL, _dbuff, 1)
 Extend if no space remains. More...
 
#define fr_dbuff_extend_lowat(_status, _dbuff_or_marker, _lowat)
 Extend if we're below _lowat. More...
 
#define FR_DBUFF_EXTEND_LOWAT_OR_RETURN(_dbuff_or_marker, _lowat)
 Extend if we're below _lowat and return if we can't extend above _lowat. More...
 
enum  fr_dbuff_extend_status_t {
  FR_DBUFF_NOT_EXTENDABLE = 0x00 ,
  FR_DBUFF_EXTENDABLE = FR_DBUFF_FLAG_EXTENDABLE ,
  FR_DBUFF_EXTENDABLE_EXTENDED = FR_DBUFF_FLAG_EXTENDABLE | FR_DBUFF_FLAG_EXTENDED ,
  FR_DBUFF_EXTENDED = FR_DBUFF_FLAG_EXTENDED
}
 Whether the buffer is currently extendable and whether it was extended. More...
 
#define FR_DBUFF_FLAG_EXTENDABLE   0x01
 Flag indicating a dbuff is extendable. More...
 
#define FR_DBUFF_FLAG_EXTENDED   0x02
 Flag indicating that during the last extend call the dbuff was extended. More...
 
#define fr_dbuff_is_extendable(_status)   ((_status) & FR_DBUFF_FLAG_EXTENDABLE)
 Check if a dbuff can be extended again. More...
 
#define fr_dbuff_was_extended(_status)   ((_status) & FR_DBUFF_FLAG_EXTENDED)
 Check if the dbuff was extended during the last extend call. More...
 

Length checks

These macros return the amount of data used/remaining relative to the dbuff or marker's 'start', 'current', and 'end' pointers.

In the majority of cases these macros should not be used and the extension request functions should be used instead. The only exception to this is if the caller is certain the fr_dbuff_t is not extensible.

#define fr_dbuff_ahead(_dbuff_or_marker)
 How many bytes the dbuff or marker is ahead of its parent. More...
 
#define fr_dbuff_behind(_dbuff_or_marker)
 How many bytes the dbuff or marker is behind its parent. More...
 
#define fr_dbuff_len(_dbuff_or_marker)    ((size_t)(fr_dbuff_end(_dbuff_or_marker) - fr_dbuff_start(_dbuff_or_marker)))
 The length of the underlying buffer. More...
 
#define fr_dbuff_remaining(_dbuff_or_marker)
 Return the number of bytes remaining between the dbuff or marker and the end of the buffer. More...
 
#define FR_DBUFF_REMAINING_RETURN(_dbuff_or_marker, _len)    if ((_len) > fr_dbuff_remaining(_dbuff_or_marker)) return -((_len) - fr_dbuff_remaining(_dbuff_or_marker))
 Check if _len bytes are available in the dbuff and if not return the number of bytes we'd need. More...
 
#define fr_dbuff_used(_dbuff_or_marker)
 Return the number of bytes remaining between the start of the dbuff or marker and the current position. More...
 

Accessors

Caching the pointers returned by the accessors is strongly discouraged.

Cached pointers can become invalidated if the fr_dbuff_t is extended, as the extensions callback may use realloc or memmove on the underlying buffer.

fr_dbuff_t dbuff;
fr_dbuff_uctx_talloc_t tctx;
fr_dbuff_init_talloc(NULL, &dbuff, &tctx, 512, SIZE_MAX);
p = fr_dbuff_current(&dbuff); // Cache the start pointer
fr_dbuff_extend_lowat(&dbuff, 1024); // Extension call triggers realloc
printf("%s", p); // Should print an empty string but may
// SEGV as p may now be invalid.
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition: dbuff.h:906
#define fr_dbuff_extend_lowat(_status, _dbuff_or_marker, _lowat)
Extend if we're below _lowat.
Definition: dbuff.h:655
static fr_dbuff_t * fr_dbuff_init_talloc(TALLOC_CTX *ctx, fr_dbuff_t *dbuff, fr_dbuff_uctx_talloc_t *tctx, size_t init, size_t max)
Initialise a special dbuff which automatically extends as additional data is written.
Definition: dbuff.h:406
unsigned char uint8_t
Definition: merged_model.c:30

If offsets of a fr_dbuff_t need to be accessed, markers should be used. If a dbuff is extended all markers associated with it will be updated so that the content they point to remains constant.

fr_dbuff_t dbuff;
fr_dbuff_uctx_talloc_t tctx;
fr_dbuff_init_talloc(NULL, &dbuff, &tctx, 512, SIZE_MAX);
fr_dbuff_marker(&m, &dbuff);
fr_dbuff_extend_lowat(&dbuff, 1024); // Extension call triggers realloc
printf("%s", fr_dbuff_current(&m)); // Marker was updated when the dbuff
// was extended. All is well.
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition: dbuff.h:81
static uint8_t * fr_dbuff_marker(fr_dbuff_marker_t *m, fr_dbuff_t *dbuff)
Initialises a new marker pointing to the 'current' position of the dbuff.
Definition: dbuff.h:1187

Using offsets of the pointers returned by accessor functions is also strongly discouraged as it invalidates many of the protections dbuffs give.

fr_dbuff_t dbuff;
fr_dbuff_init(&dbuff, buff, sizeof(buff));
fr_dbuff_current(&dbuff)[2] = 0x00; // Write to invalid memory
#define fr_dbuff_init(_out, _start, _len_or_end)
Initialise an dbuff for encoding or decoding.
Definition: dbuff.h:354
static char buff[sizeof("18446744073709551615")+3]
Definition: size_tests.c:41
#define fr_dbuff_buff(_dbuff_or_marker)
 Return the underlying buffer in a dbuff or one of marker. More...
 
#define fr_dbuff_current(_dbuff_or_marker)
 Return the 'current' position of a dbuff or marker. More...
 
#define fr_dbuff_end(_dbuff_or_marker)
 Return the current 'end' position of a dbuff or marker. More...
 
#define fr_dbuff_ptr(_dbuff_or_marker)
 Return a pointer to the dbuff. More...
 
#define fr_dbuff_ptr_const(_dbuff_or_marker)
 Return a const pointer to the dbuff. More...
 
#define fr_dbuff_start(_dbuff_or_marker)
 Return the 'start' position of a dbuff or marker. More...
 

Position modification (recursive)

Modify the 'current' position pointer of a dbuff or marker.

#define fr_dbuff_advance(_dbuff_or_marker, _len)
 Advance 'current' position in dbuff or marker by _len bytes. More...
 
#define fr_dbuff_advance_extend(_dbuff_or_marker, _len)
 Advance current'position in dbuff or marker by _len bytes (extending if necessary) More...
 
#define FR_DBUFF_ADVANCE_RETURN(_dbuff_or_marker, _len)   FR_DBUFF_RETURN(fr_dbuff_advance, _dbuff_or_marker, _len)
 Advance the 'current' position in dbuff or marker by _len bytes returning if _len is out of range. More...
 
#define FR_DBUFF_BIND_EXTEND_RETURN(_dbuff_or_marker, _len)   FR_DBUFF_RETURN(fr_dbuff_advance_extend, _dbuff_or_marker, _len)
 
#define fr_dbuff_set(_dst, _src)
 Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer, or a length value. More...
 
#define fr_dbuff_set_end(_dst, _end)
 Set a new 'end' position in a dbuff or marker. More...
 
#define FR_DBUFF_SET_RETURN(_dst, _src)   FR_DBUFF_RETURN(fr_dbuff_set, _dst, _src)
 Set the 'current' position in a dbuff or marker returning if _src is out of range. More...
 
#define fr_dbuff_set_to_end(_dbuff_or_marker)    fr_dbuff_set(_dbuff_or_marker, fr_dbuff_end(_dbuff_or_marker))
 Reset the 'current' position of the dbuff or marker to the 'end' of the buffer. More...
 
#define fr_dbuff_set_to_start(_dbuff_or_marker)    fr_dbuff_set(_dbuff_or_marker, fr_dbuff_start(_dbuff_or_marker))
 Reset the 'current' position of the dbuff or marker to the 'start' of the buffer. More...
 

"in" functions (copy data into a dbuff)

#define fr_dbuff_in(_dbuff_or_marker, _in)
 Copy data from a fixed sized C type into a dbuff or marker. More...
 
#define fr_dbuff_in_bytes(_dbuff_or_marker, ...)    fr_dbuff_in_memcpy(_dbuff_or_marker, ((uint8_t []){ __VA_ARGS__ }), sizeof((uint8_t []){ __VA_ARGS__ }))
 Copy a byte sequence into a dbuff or marker. More...
 
#define fr_dbuff_in_bytes_partial(_dbuff, ...)    fr_dbuff_in_memcpy_partial(_dbuff, ((uint8_t []){ __VA_ARGS__ }), sizeof((uint8_t []){ __VA_ARGS__ }))
 Copy a partial byte sequence into a dbuff. More...
 
#define FR_DBUFF_IN_BYTES_RETURN(_dbuff_or_marker, ...)    FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, ((uint8_t []){ __VA_ARGS__ }), sizeof((uint8_t []){ __VA_ARGS__ }))
 Copy a byte sequence into a dbuff or marker returning if there's insufficient space. More...
 
#define fr_dbuff_in_memcpy(_dbuff_or_marker, _in, _inlen)
 Copy exactly _inlen bytes into a dbuff or marker. More...
 
#define fr_dbuff_in_memcpy_partial(_out, _in, _inlen)
 Copy at most _inlen bytes into the dbuff. More...
 
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)   FR_DBUFF_RETURN(fr_dbuff_in_memcpy, _dbuff_or_marker, _in, _inlen)
 Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space. More...
 
#define FR_DBUFF_IN_RETURN(_dbuff_or_marker, _in)   FR_DBUFF_RETURN(fr_dbuff_in, _dbuff_or_marker, _in)
 Copy data from a fixed sized C type into a dbuff returning if there is insufficient space. More...
 
#define fr_dbuff_in_uint64v(_dbuff_or_marker, _num)    _fr_dbuff_in_uint64v(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), _num)
 Copy an integer value into a dbuff or marker using our internal variable length encoding. More...
 
#define FR_DBUFF_IN_UINT64V(_dbuff_or_marker, _num)   FR_DBUFF_RETURN(fr_dbuff_in_uint64v, _dbuff_or_marker, _num)
 Copy an integer value into a dbuff or marker using our internal variable length encoding returning if there is insufficient space. More...
 
#define fr_dbuff_memset(_dbuff_or_marker, _c, _inlen)    _fr_dbuff_memset(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), _c, _inlen)
 Set _inlen bytes of a dbuff or marker to _c. More...
 
#define FR_DBUFF_MEMSET_RETURN(_dbuff_or_marker, _c, _inlen)   FR_DBUFF_RETURN(fr_dbuff_memset, _dbuff_or_marker, _c, _inlen)
 Set _inlen bytes of a dbuff or marker to _c returning if there is insufficient space. More...
 

"move" functions (copy data between dbuffs and markers)

#define fr_dbuff_move(_out, _in, _len)
 Copy in as many bytes as possible from one dbuff or marker to another. More...
 

"out" functions (copy data out of a dbuff)

#define fr_dbuff_out(_out, _dbuff_or_marker)
 Copy data from a dbuff or marker to a fixed sized C type. More...
 
#define fr_dbuff_out_int64v(_num, _dbuff_or_marker, _len)    _fr_dbuff_out_int64v(_num, _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), _len)
 Read bytes from a dbuff or marker and interpret them as a network order signed integer. More...
 
#define FR_DBUFF_OUT_INT64V_RETURN(_num, _dbuff_or_marker, _len)   FR_DBUFF_RETURN(fr_dbuff_out_int64v, _num, _dbuff_or_marker, _len)
 Read bytes from a dbuff or marker and interpret them as a network order unsigned integer. More...
 
#define fr_dbuff_out_memcpy(_out, _dbuff_or_marker, _outlen)
 Copy exactly _outlen bytes from the dbuff. More...
 
#define FR_DBUFF_OUT_MEMCPY_RETURN(_out, _dbuff_or_marker, _outlen)   FR_DBUFF_RETURN(fr_dbuff_out_memcpy, _out, _dbuff_or_marker, _outlen)
 Copy outlen bytes from the dbuff returning if there's insufficient data in the dbuff. More...
 
#define FR_DBUFF_OUT_RETURN(_out, _dbuff_or_marker)   FR_DBUFF_RETURN(fr_dbuff_out, _out, _dbuff_or_marker)
 Copy data from a dbuff or marker to a fixed sized C type returning if there is insufficient data. More...
 
#define fr_dbuff_out_uint64v(_num, _dbuff_or_marker, _len)    _fr_dbuff_out_uint64v(_num, _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), _len)
 Read bytes from a dbuff or marker and interpret them as a network order unsigned integer. More...
 
#define FR_DBUFF_OUT_UINT64V_RETURN(_num, _dbuff_or_marker, _len)   FR_DBUFF_RETURN(fr_dbuff_out_uint64v, _num, _dbuff_or_marker, _len)
 Read bytes from a dbuff or marker and interpret them as a network order unsigned integer. More...
 

Extension callback helpers

These public functions are intended to be called by extension callbacks to fixup dbuffs after the underlying buffer or its contents has been altered.

size_t fr_dbuff_shift (fr_dbuff_t *dbuff, size_t shift)
 Shift the contents of the dbuff, returning the number of bytes we managed to shift. More...
 
void fr_dbuff_update (fr_dbuff_t *dbuff, uint8_t *new_buff, size_t new_len)
 Update all markers and pointers in the set of dbuffs to point to new_buff. More...
 

Marker management

Markers serve two purposes:

  • Markers allow the caller to track content in a dbuff as the dbuff is extended. If the caller referred to content using a pointer into the underlying buffer, that pointer may be invalidated if the buffer were extended.
  • Markers prevent content being shifted out of the buffer during an extension.

Most operations that can be performed on an fr_dbuff_t can also be performed on a fr_dbuff_marker_t.

It is recommended that markers be created against a stack-frame-local dbuff so that they are automatically released when the framed is popped.

See also
fr_dbuff_marker_t
static uint8_tfr_dbuff_marker (fr_dbuff_marker_t *m, fr_dbuff_t *dbuff)
 Initialises a new marker pointing to the 'current' position of the dbuff. More...
 
static void fr_dbuff_marker_release (fr_dbuff_marker_t *m)
 Releases the specified marker and any markers added before it. More...
 
static size_t fr_dbuff_marker_release_ahead (fr_dbuff_marker_t *m)
 Trims the linked list back to the specified pointer and return how many bytes marker was ahead of p. More...
 
static size_t fr_dbuff_marker_release_behind (fr_dbuff_marker_t *m)
 Trims the linked list back to the specified pointer and return how many bytes marker was behind p. More...
 

Detailed Description

A generic data buffer structure for encoding and decoding.

Because doing manual length checks is error prone and a waste of everyone's time.

Definition in file dbuff.h.


Data Structure Documentation

◆ fr_dbuff_thread_local_t

struct fr_dbuff_thread_local_t

Structure to encapsulate a thread local dbuff information.

Definition at line 535 of file dbuff.h.

+ Collaboration diagram for fr_dbuff_thread_local_t:
Data Fields
fr_dbuff_t dbuff Thread local dbuff.
fr_dbuff_uctx_talloc_t tctx Thread local tctx.

Macro Definition Documentation

◆ FR_DBUFF

#define FR_DBUFF (   _dbuff_or_marker)    _FR_DBUFF(_dbuff_or_marker, fr_dbuff_current(_dbuff_or_marker), 0x00)

Create a new dbuff pointing to the same underlying buffer.

  • Parent will NOT be advanced by operations on its child.
  • Child will have its start pointer set to the p pointer of the parent.
Parameters
[in]_dbuff_or_markerto make an ephemeral copy of.

Definition at line 222 of file dbuff.h.

◆ FR_DBUFF_ABS

#define FR_DBUFF_ABS (   _dbuff_or_marker)    _FR_DBUFF(_dbuff_or_marker, fr_dbuff_start(_dbuff_or_marker), 0x00)

Create a new dbuff pointing to the same underlying buffer.

  • Parent will NOT be advanced by operations on its child.
  • Child will have its start pointer set to the start pointer of the parent.
Parameters
[in]_dbuff_or_markerto make an ephemeral copy of.

Definition at line 231 of file dbuff.h.

◆ FR_DBUFF_ADV_PARENT_CURRENT

#define FR_DBUFF_ADV_PARENT_CURRENT   0x01

Advance current position of parent.

Useful for nested encoders/decoders.

Definition at line 125 of file dbuff.h.

◆ FR_DBUFF_ADV_PARENT_END

#define FR_DBUFF_ADV_PARENT_END   0x02

Advance end pointer of parent.

Useful for producer/consumer

Definition at line 127 of file dbuff.h.

◆ fr_dbuff_advance

#define fr_dbuff_advance (   _dbuff_or_marker,
  _len 
)
Value:
fr_dbuff_set(_dbuff_or_marker, \
(fr_dbuff_current(_dbuff_or_marker) + \
(_Generic((_len), \
unsigned char : (size_t)(_len), \
unsigned short : (size_t)(_len), \
unsigned int : (size_t)(_len), \
unsigned long : (size_t)(_len), \
unsigned long long : (size_t)(_len), \
int : (size_t)(_len) \
))))
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition: dbuff.h:999

Advance 'current' position in dbuff or marker by _len bytes.

Parameters
[in]_dbuff_or_markerto advance.
[in]_lenHow much to advance dbuff by. Must be a positive integer.
Returns
  • 0 not advanced.
  • >0 the number of bytes the dbuff or marker was advanced by.
  • <0 the number of bytes required to complete the advancement

Definition at line 1067 of file dbuff.h.

◆ fr_dbuff_advance_extend

#define fr_dbuff_advance_extend (   _dbuff_or_marker,
  _len 
)
Value:
_fr_dbuff_advance(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), \
(_Generic((_len), \
unsigned char : (size_t)(_len), \
unsigned short : (size_t)(_len), \
unsigned int : (size_t)(_len), \
unsigned long : (size_t)(_len), \
unsigned long long : (size_t)(_len), \
int : (size_t)(_len) \
)))
#define fr_dbuff_ptr(_dbuff_or_marker)
Return a pointer to the dbuff.
Definition: dbuff.h:853

Advance current'position in dbuff or marker by _len bytes (extending if necessary)

Parameters
[in]_dbuff_or_markerto advance.
[in]_lenHow much to advance dbuff by. Must be a positive integer.
Returns
  • 0 not advanced.
  • >0 the number of bytes the dbuff or marker was advanced by.
  • <0 the number of bytes we'd need to complete the advance.

Definition at line 1134 of file dbuff.h.

◆ FR_DBUFF_ADVANCE_RETURN

#define FR_DBUFF_ADVANCE_RETURN (   _dbuff_or_marker,
  _len 
)    FR_DBUFF_RETURN(fr_dbuff_advance, _dbuff_or_marker, _len)

Advance the 'current' position in dbuff or marker by _len bytes returning if _len is out of range.

Parameters
[in]_dbuff_or_markerto advance.
[in]_lenHow much to advance dbuff by. Must be a positive integer.
Returns
  • 0 not advanced.
  • >0 the number of bytes the dbuff or marker was advanced by.
  • <0 the number of bytes required to complete the advancement

Definition at line 1083 of file dbuff.h.

◆ fr_dbuff_ahead

#define fr_dbuff_ahead (   _dbuff_or_marker)
Value:
(fr_dbuff_current((_dbuff_or_marker)->parent) > fr_dbuff_current(_dbuff_or_marker) ? \
0 : fr_dbuff_current(_dbuff_or_marker) - fr_dbuff_current((_dbuff_or_marker)->parent))
static fr_slen_t parent
Definition: pair.h:844

How many bytes the dbuff or marker is ahead of its parent.

Returns
  • 0 the dbuff or marker is behind its parent.
  • >0 the number of bytes the marker is ahead of its parent.

Definition at line 791 of file dbuff.h.

◆ fr_dbuff_behind

#define fr_dbuff_behind (   _dbuff_or_marker)
Value:
(fr_dbuff_current(_dbuff_or_marker) > fr_dbuff_current((_dbuff_or_marker)->parent) ? \
0 : fr_dbuff_current((_dbuff_or_marker)->parent) - fr_dbuff_current(_dbuff_or_marker))

How many bytes the dbuff or marker is behind its parent.

Parameters
[in]_dbuff_or_marker
Returns
  • 0 the dbuff or marker is ahead of its parent.
  • >0 the number of bytes the marker is behind its parent.

Definition at line 781 of file dbuff.h.

◆ FR_DBUFF_BIND_CURRENT

#define FR_DBUFF_BIND_CURRENT (   _dbuff_or_marker)    _FR_DBUFF(_dbuff_or_marker, fr_dbuff_current(_dbuff_or_marker), FR_DBUFF_ADV_PARENT_CURRENT)

Create a new dbuff pointing to the same underlying buffer.

  • Parent p pointer will be advanced with child's p pointer.
  • Child will have its start pointer set to the p pointer of the parent.
Parameters
[in]_dbuff_or_markerto make an ephemeral copy of.

Definition at line 240 of file dbuff.h.

◆ FR_DBUFF_BIND_CURRENT_ABS

#define FR_DBUFF_BIND_CURRENT_ABS (   _dbuff_or_marker)    FR_DBUFF_ABS(_dbuff_or_marker, fr_dbuff_start(_dbuff_or_marker), FR_DBUFF_ADV_PARENT_CURRENT)

Create a new dbuff pointing to the same underlying buffer.

  • Parent p pointer will be advanced with child's p pointer.
  • Child will have its start pointer set to the start pointer of the parent.
Parameters
[in]_dbuff_or_markerto make an ephemeral copy of.

Definition at line 249 of file dbuff.h.

◆ FR_DBUFF_BIND_END_ABS

#define FR_DBUFF_BIND_END_ABS (   _dbuff_or_marker)    _FR_DBUFF(_dbuff_or_marker, fr_dbuff_start(_dbuff_or_marker), FR_DBUFF_ADV_PARENT_END)

Create a new dbuff pointing to the same underlying buffer.

This is used to create the producer of a producer/consumer pairs of dbuffs.

  • Parent end pointer will be advanced with child's p pointer.
  • Child will have its start pointer set to the start pointer of the parent.
Parameters
[in]_dbuff_or_markerto make an ephemeral copy of.

Definition at line 260 of file dbuff.h.

◆ FR_DBUFF_BIND_EXTEND_RETURN

#define FR_DBUFF_BIND_EXTEND_RETURN (   _dbuff_or_marker,
  _len 
)    FR_DBUFF_RETURN(fr_dbuff_advance_extend, _dbuff_or_marker, _len)

Definition at line 1145 of file dbuff.h.

◆ fr_dbuff_buff

#define fr_dbuff_buff (   _dbuff_or_marker)
Value:
_Generic((_dbuff_or_marker), \
fr_dbuff_t * : (((fr_dbuff_t const *)(_dbuff_or_marker))->buff), \
fr_dbuff_t const * : (((fr_dbuff_t const *)(_dbuff_or_marker))->buff), \
fr_dbuff_marker_t * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->parent->buff), \
fr_dbuff_marker_t const * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->parent->buff) \
)

Return the underlying buffer in a dbuff or one of marker.

Parameters
[in]_dbuff_or_markerto return the buffer for.
Returns
A pointer to the start of the buffer.

Definition at line 877 of file dbuff.h.

◆ fr_dbuff_current

#define fr_dbuff_current (   _dbuff_or_marker)
Value:
(_Generic((_dbuff_or_marker), \
fr_dbuff_t * : (((fr_dbuff_t const *)(_dbuff_or_marker))->p), \
fr_dbuff_t const * : (((fr_dbuff_t const *)(_dbuff_or_marker))->p), \
fr_dbuff_marker_t * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->p), \
fr_dbuff_marker_t const * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->p) \
))

Return the 'current' position of a dbuff or marker.

Parameters
[in]_dbuff_or_markerto return the current position of.
Returns
A pointer to the current position of the buffer or marker.

Definition at line 906 of file dbuff.h.

◆ fr_dbuff_end

#define fr_dbuff_end (   _dbuff_or_marker)
Value:
(_Generic((_dbuff_or_marker), \
fr_dbuff_t * : (((fr_dbuff_t const *)(_dbuff_or_marker))->end), \
fr_dbuff_t const * : (((fr_dbuff_t const *)(_dbuff_or_marker))->end), \
fr_dbuff_marker_t * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->parent->end), \
fr_dbuff_marker_t const * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->parent->end) \
))

Return the current 'end' position of a dbuff or marker.

Parameters
[in]_dbuff_or_markerto return the end position of.
Returns
A pointer to the end position of the buffer or marker.

Definition at line 933 of file dbuff.h.

◆ fr_dbuff_extend

#define fr_dbuff_extend (   _dbuff)    fr_dbuff_extend_lowat(NULL, _dbuff, 1)

Extend if no space remains.

Parameters
[in]_dbuffto extend.
Returns
  • 0 if there are no bytes left in the buffer and we couldn't extend.
  • >0 the number of bytes in the buffer after extending.

Definition at line 700 of file dbuff.h.

◆ fr_dbuff_extend_lowat

#define fr_dbuff_extend_lowat (   _status,
  _dbuff_or_marker,
  _lowat 
)
Value:
_fr_dbuff_extend_lowat(_status, \
fr_dbuff_ptr(_dbuff_or_marker), \
fr_dbuff_remaining(_dbuff_or_marker), _lowat)
#define fr_dbuff_remaining(_dbuff_or_marker)
Return the number of bytes remaining between the dbuff or marker and the end of the buffer.
Definition: dbuff.h:738

Extend if we're below _lowat.

Parameters
[out]_statusMay be NULL. If fr_dbuff_extend_lowat is used in a copy loop, the caller should pass a pointer to a fr_dbuff_extend_status_t. The initial value of the fr_dbuff_extend_status_t variable should be FR_DBUFF_EXTENDABLE, and will be updated to indicate whether the dbuff is extensible, whether it was extended, and whether it may be extended again. This information is used the loop condition to prevent spurious extension calls.
[in]_dbuff_or_markerto extend.
[in]_lowatIf bytes remaining are below the amount, extend.
Returns
  • 0 if there are no bytes left in the buffer and we couldn't extend.
  • >0 the number of bytes in the buffer after extending.

Definition at line 655 of file dbuff.h.

◆ FR_DBUFF_EXTEND_LOWAT_OR_RETURN

#define FR_DBUFF_EXTEND_LOWAT_OR_RETURN (   _dbuff_or_marker,
  _lowat 
)
Value:
do { \
size_t _remaining = fr_dbuff_extend_lowat(NULL, _dbuff_or_marker, _lowat); \
if (_remaining < _lowat) return -(_lowat - _remaining); \
} while (0)

Extend if we're below _lowat and return if we can't extend above _lowat.

Parameters
[in]_dbuff_or_markerto extend.
[in]_lowatIf bytes remaining are below the amount, extend.
Returns
  • 0 if there are no bytes left in the buffer and we couldn't extend.
  • >0 the number of bytes in the buffer after extending.

Definition at line 668 of file dbuff.h.

◆ FR_DBUFF_FLAG_EXTENDABLE

#define FR_DBUFF_FLAG_EXTENDABLE   0x01

Flag indicating a dbuff is extendable.

Definition at line 577 of file dbuff.h.

◆ FR_DBUFF_FLAG_EXTENDED

#define FR_DBUFF_FLAG_EXTENDED   0x02

Flag indicating that during the last extend call the dbuff was extended.

Definition at line 581 of file dbuff.h.

◆ fr_dbuff_in

#define fr_dbuff_in (   _dbuff_or_marker,
  _in 
)
Value:
_Generic((_in), \
int8_t : fr_dbuff_in_bytes(_dbuff_or_marker, (int8_t)_in), \
int16_t : _fr_dbuff_in_int16(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), (int16_t)_in), \
int32_t : _fr_dbuff_in_int32(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), (int32_t)_in), \
int64_t : _fr_dbuff_in_int64(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), (int64_t)_in), \
uint8_t : fr_dbuff_in_bytes(_dbuff_or_marker, (uint8_t)_in), \
uint16_t : _fr_dbuff_in_uint16(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), (uint16_t)_in), \
uint32_t : _fr_dbuff_in_uint32(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), (uint32_t)_in), \
uint64_t : _fr_dbuff_in_uint64(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), (uint64_t)_in), \
float : _fr_dbuff_in_float(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), (float)_in), \
double : _fr_dbuff_in_double(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), (double)_in) \
)
#define fr_dbuff_in_bytes(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker.
Definition: dbuff.h:1460
unsigned short uint16_t
Definition: merged_model.c:31
unsigned int uint32_t
Definition: merged_model.c:33

Copy data from a fixed sized C type into a dbuff or marker.

Parameters
[out]_dbuff_or_markerto write to. Integer types will be automatically converted to big endian byte order.
[in]_inValue to copy.
Returns
  • <0 the number of bytes we would have needed to complete the conversion.
  • >0 the number of bytes _dbuff_or_marker was advanced by.

Definition at line 1562 of file dbuff.h.

◆ fr_dbuff_in_bytes

#define fr_dbuff_in_bytes (   _dbuff_or_marker,
  ... 
)     fr_dbuff_in_memcpy(_dbuff_or_marker, ((uint8_t []){ __VA_ARGS__ }), sizeof((uint8_t []){ __VA_ARGS__ }))

Copy a byte sequence into a dbuff or marker.

Copy exactly _inlen bytes into a dbuff or marker.

Parameters
[in]_dbuff_or_markerto copy byte sequence into.
[in]...bytes to copy.

Definition at line 1460 of file dbuff.h.

◆ fr_dbuff_in_bytes_partial

#define fr_dbuff_in_bytes_partial (   _dbuff,
  ... 
)     fr_dbuff_in_memcpy_partial(_dbuff, ((uint8_t []){ __VA_ARGS__ }), sizeof((uint8_t []){ __VA_ARGS__ }))

Copy a partial byte sequence into a dbuff.

Copy at most _inlen bytes into the dbuff.

Parameters
[in]_dbuffto copy byte sequence into.
[in]...bytes to copy.

Definition at line 1450 of file dbuff.h.

◆ FR_DBUFF_IN_BYTES_RETURN

#define FR_DBUFF_IN_BYTES_RETURN (   _dbuff_or_marker,
  ... 
)     FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, ((uint8_t []){ __VA_ARGS__ }), sizeof((uint8_t []){ __VA_ARGS__ }))

Copy a byte sequence into a dbuff or marker returning if there's insufficient space.

Copy exactly _inlen bytes into a dbuff or marker.

Parameters
[in]_dbuff_or_markerto copy byte sequence into.
[in]...bytes to copy.

Definition at line 1467 of file dbuff.h.

◆ fr_dbuff_in_memcpy

#define fr_dbuff_in_memcpy (   _dbuff_or_marker,
  _in,
  _inlen 
)
Value:
_Generic((_in), \
uint8_t * : _fr_dbuff_in_memcpy(_fr_dbuff_current_ptr(_dbuff_or_marker), \
fr_dbuff_ptr(_dbuff_or_marker), \
(uint8_t const *)(_in), \
_inlen), \
uint8_t const * : _fr_dbuff_in_memcpy(_fr_dbuff_current_ptr(_dbuff_or_marker), \
fr_dbuff_ptr(_dbuff_or_marker), \
(uint8_t const *)(_in), \
_inlen), \
char * : _fr_dbuff_in_memcpy(_fr_dbuff_current_ptr(_dbuff_or_marker), \
fr_dbuff_ptr(_dbuff_or_marker), \
(uint8_t const *)(_in), \
(size_t)(_inlen) == SIZE_MAX ? strlen((char const *)(_in)) : (_inlen)), \
char const * : _fr_dbuff_in_memcpy(_fr_dbuff_current_ptr(_dbuff_or_marker), \
fr_dbuff_ptr(_dbuff_or_marker), \
(uint8_t const *)(_in), \
(size_t)(_inlen) == SIZE_MAX ? strlen((char const *)(_in)) : (_inlen)), \
fr_dbuff_t * : _fr_dbuff_in_memcpy_dbuff(_fr_dbuff_current_ptr(_dbuff_or_marker), \
fr_dbuff_ptr(_dbuff_or_marker), \
&((fr_dbuff_t const *)(_in))->p, \
((fr_dbuff_t const *)(_in)), \
_inlen), \
fr_dbuff_marker_t * : _fr_dbuff_in_memcpy_dbuff(_fr_dbuff_current_ptr(_dbuff_or_marker), \
fr_dbuff_ptr(_dbuff_or_marker), \
&((fr_dbuff_marker_t const *)(_in))->p, \
((fr_dbuff_marker_t const *)(_in))->parent, _inlen) \
)

Copy exactly _inlen bytes into a dbuff or marker.

If _in is a dbuff and _inlen is greater than the number of bytes available in that dbuff, the copy operation will fail.

Note
_in will not be advanced. If this is required fr_dbuff_move should be used.
Parameters
[in]_dbuff_or_markerto copy data to.
[in]_indata to copy in to the dbuff or marker.
[in]_inlenHow much data we need to copy. If _in is a char * or dbuff * and SIZE_MAX is passed, then _inlen will be substituted for the length of the data in the dbuff.
Returns
  • 0 no data copied.
  • >0 the number of bytes copied to the dbuff.
  • <0 the number of bytes we would have needed to complete the copy operation.

Definition at line 1345 of file dbuff.h.

◆ fr_dbuff_in_memcpy_partial

#define fr_dbuff_in_memcpy_partial (   _out,
  _in,
  _inlen 
)
Value:
_Generic((_in), \
uint8_t * : _fr_dbuff_in_memcpy_partial(_fr_dbuff_current_ptr(_out), _out, (uint8_t const *)(_in), _inlen), \
uint8_t const * : _fr_dbuff_in_memcpy_partial(_fr_dbuff_current_ptr(_out), _out, (uint8_t const *)(_in), _inlen), \
char * : _fr_dbuff_in_memcpy_partial(_fr_dbuff_current_ptr(_out), _out, (uint8_t const *)(_in), _inlen == SIZE_MAX ? strlen((char const *)(_in)) : _inlen), \
char const * : _fr_dbuff_in_memcpy_partial(_fr_dbuff_current_ptr(_out), _out, (uint8_t const *)(_in), _inlen == SIZE_MAX ? strlen((char const *)(_in)) : _inlen), \
fr_dbuff_t * : _fr_dbuff_in_memcpy_partial_dbuff(_fr_dbuff_current_ptr(_out), fr_dbuff_ptr(_out), &((fr_dbuff_t const *)(_in))->p, ((fr_dbuff_t const *)(_in)), _inlen), \
fr_dbuff_marker_t * : _fr_dbuff_in_memcpy_partial_dbuff(_fr_dbuff_current_ptr(_out), fr_dbuff_ptr(_out), &((fr_dbuff_marker_t const *)(_in))->p, ((fr_dbuff_marker_t const *)(_in))->parent, _inlen) \
)

Copy at most _inlen bytes into the dbuff.

Use this variant when writing data to a streaming buffer where partial writes will be tracked.

If _in is a fr_dbuff_t and _inlen is greater than the number of bytes available in that dbuff, the copy operation will truncated.

Note
_in will not be advanced. If this is required fr_dbuff_move should be used.
Parameters
[in]_outto copy data to.
[in]_inData to copy to dbuff.
[in]_inlenHow much data we need to copy. If _in is a char * or dbuff * and SIZE_MAX is passed, then _inlen will be substituted for the length of the buffer.
Returns
  • 0 no data copied.
  • >0 the number of bytes copied to the dbuff.

Definition at line 1433 of file dbuff.h.

◆ FR_DBUFF_IN_MEMCPY_RETURN

#define FR_DBUFF_IN_MEMCPY_RETURN (   _dbuff_or_marker,
  _in,
  _inlen 
)    FR_DBUFF_RETURN(fr_dbuff_in_memcpy, _dbuff_or_marker, _in, _inlen)

Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.

If _in is a dbuff and _inlen is greater than the number of bytes available in that dbuff, the copy operation will fail.

Note
_in will not be advanced. If this is required fr_dbuff_move should be used.
Parameters
[in]_dbuff_or_markerto copy data to.
[in]_indata to copy in to the dbuff or marker.
[in]_inlenHow much data we need to copy. If _in is a char * or dbuff * and SIZE_MAX is passed, then _inlen will be substituted for the length of the data in the dbuff.
Returns
  • 0 no data copied.
  • >0 the number of bytes copied to the dbuff.
  • <0 the number of bytes we would have needed to complete the copy operation.

Definition at line 1377 of file dbuff.h.

◆ FR_DBUFF_IN_RETURN

#define FR_DBUFF_IN_RETURN (   _dbuff_or_marker,
  _in 
)    FR_DBUFF_RETURN(fr_dbuff_in, _dbuff_or_marker, _in)

Copy data from a fixed sized C type into a dbuff returning if there is insufficient space.

Parameters
[out]_dbuff_or_markerto write to. Integer types will be automatically converted to big endian byte order.
[in]_inValue to copy.
Returns
  • <0 the number of bytes we would have needed to complete the conversion.
  • >0 the number of bytes _dbuff_or_marker was advanced by.

Definition at line 1580 of file dbuff.h.

◆ fr_dbuff_in_uint64v

#define fr_dbuff_in_uint64v (   _dbuff_or_marker,
  _num 
)     _fr_dbuff_in_uint64v(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), _num)

Copy an integer value into a dbuff or marker using our internal variable length encoding.

Parameters
[out]_dbuff_or_markerto copy integer value to.
[in]_numto copy.
Returns
  • <0 the number of bytes we would have needed to encode the integer value.
  • >0 the number of bytes used to represent the integer value.

Definition at line 1604 of file dbuff.h.

◆ FR_DBUFF_IN_UINT64V

#define FR_DBUFF_IN_UINT64V (   _dbuff_or_marker,
  _num 
)    FR_DBUFF_RETURN(fr_dbuff_in_uint64v, _dbuff_or_marker, _num)

Copy an integer value into a dbuff or marker using our internal variable length encoding returning if there is insufficient space.

Parameters
[out]_dbuff_or_markerto copy integer value to.
[in]_numto copy.
Returns
  • <0 the number of bytes we would have needed to encode the integer value.
  • >0 the number of bytes used to represent the integer value.

Definition at line 1611 of file dbuff.h.

◆ fr_dbuff_init

#define fr_dbuff_init (   _out,
  _start,
  _len_or_end 
)
Value:
_fr_dbuff_init(_out, \
(uint8_t const *)(_start), \
_Generic((_len_or_end), \
size_t : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
long : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
int : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
unsigned int : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
uint8_t * : (uint8_t const *)(_len_or_end), \
uint8_t const * : (uint8_t const *)(_len_or_end), \
char * : (uint8_t const *)(_len_or_end), \
char const * : (uint8_t const *)(_len_or_end) \
), \
_Generic((_start), \
uint8_t * : false, \
uint8_t const * : true, \
char * : false, \
char const * : true \
))

Initialise an dbuff for encoding or decoding.

Parameters
[out]_outPointer to buffer to parse
[in]_startStart of the buffer to parse.
[in]_len_or_endEither an end pointer or the length of the buffer we're decoding.

Definition at line 354 of file dbuff.h.

◆ fr_dbuff_is_extendable

#define fr_dbuff_is_extendable (   _status)    ((_status) & FR_DBUFF_FLAG_EXTENDABLE)

Check if a dbuff can be extended again.

Definition at line 605 of file dbuff.h.

◆ fr_dbuff_len

#define fr_dbuff_len (   _dbuff_or_marker)     ((size_t)(fr_dbuff_end(_dbuff_or_marker) - fr_dbuff_start(_dbuff_or_marker)))

The length of the underlying buffer.

Parameters
[in]_dbuff_or_markerto return the length of.
Returns
The length of the underlying buffer.

Definition at line 771 of file dbuff.h.

◆ FR_DBUFF_MAX

#define FR_DBUFF_MAX (   _dbuff_or_marker,
  _max 
)    _FR_DBUFF_MAX(_dbuff_or_marker, _max, 0x00)

Limit the maximum number of bytes available in the dbuff when passing it to another function.

if (my_child_encoder(&tlv, vp) < 0) return -1;
return fr_dbuff_advance(dbuff, fr_dbuff_used(tlv))
#define fr_dbuff_advance(_dbuff_or_marker, _len)
Advance 'current' position in dbuff or marker by _len bytes.
Definition: dbuff.h:1067
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition: dbuff.h:762
#define FR_DBUFF_MAX(_dbuff_or_marker, _max)
Limit the maximum number of bytes available in the dbuff when passing it to another function.
Definition: dbuff.h:301
#define UINT8_MAX
Definition: merged_model.c:32
fr_pair_t * vp
Note
Do not use to re-initialise the contents of _dbuff, i.e. to permanently shrink the exiting dbuff. The parent pointer will loop.
Do not modify the "child" dbuff directly. Use the functions supplied as part of this API.
Parameters
[in]_dbuff_or_markerto reserve bytes in.
[in]_maxThe maximum number of bytes the caller is allowed to write to.

Definition at line 301 of file dbuff.h.

◆ FR_DBUFF_MAX_BIND_CURRENT

#define FR_DBUFF_MAX_BIND_CURRENT (   _dbuff_or_marker,
  _max 
)    _FR_DBUFF_MAX(_dbuff_or_marker, _max, FR_DBUFF_ADV_PARENT_CURRENT)

Limit the maximum number of bytes available in the dbuff when passing it to another function.

my_child_encoder(&FR_DBUFF_MAX_BIND_CURRENT(dbuff, 253), vp);
#define FR_DBUFF_MAX_BIND_CURRENT(_dbuff_or_marker, _max)
Limit the maximum number of bytes available in the dbuff when passing it to another function.
Definition: dbuff.h:318
Note
Do not use to re-initialise the contents of _dbuff, i.e. to permanently shrink the exiting dbuff. The parent pointer will loop.
Do not modify the "child" dbuff directly. Use the functions supplied as part of this API.
Parameters
[in]_dbuff_or_markerto reserve bytes in.
[in]_maxThe maximum number of bytes the caller is allowed to write to.

Definition at line 318 of file dbuff.h.

◆ fr_dbuff_memset

#define fr_dbuff_memset (   _dbuff_or_marker,
  _c,
  _inlen 
)     _fr_dbuff_memset(_fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), _c, _inlen)

Set _inlen bytes of a dbuff or marker to _c.

Parameters
[in]_dbuff_or_markerto copy data to. Will be advanced by _inlen bytes.
[in]_cValue to set.
[in]_inlenHow much data we need to copy.
Returns
  • 0 no data set.
  • >0 the number of bytes set in the dbuff.
  • <0 the number of bytes required.

Definition at line 1496 of file dbuff.h.

◆ FR_DBUFF_MEMSET_RETURN

#define FR_DBUFF_MEMSET_RETURN (   _dbuff_or_marker,
  _c,
  _inlen 
)    FR_DBUFF_RETURN(fr_dbuff_memset, _dbuff_or_marker, _c, _inlen)

Set _inlen bytes of a dbuff or marker to _c returning if there is insufficient space.

Parameters
[in]_dbuff_or_markerto copy data to. Will be advanced by _inlen bytes.
[in]_cValue to set.
[in]_inlenHow much data we need to copy.
Returns
  • 0 no data set.
  • >0 the number of bytes set in the dbuff.
  • <0 the number of bytes required.

Definition at line 1503 of file dbuff.h.

◆ fr_dbuff_move

#define fr_dbuff_move (   _out,
  _in,
  _len 
)
Value:
_Generic((_out), \
fr_dbuff_t * : \
_Generic((_in), \
fr_dbuff_t * : _fr_dbuff_move_dbuff_to_dbuff((fr_dbuff_t *)_out, \
(fr_dbuff_t *)_in, \
_len), \
fr_dbuff_marker_t * : _fr_dbuff_move_dbuff_marker_to_dbuff((fr_dbuff_t *)_out, \
_len) \
), \
_Generic((_in), \
fr_dbuff_t * : _fr_dbuff_move_dbuff_to_dbuff_marker((fr_dbuff_marker_t *)_out, \
(fr_dbuff_t *)_in, \
_len), \
fr_dbuff_marker_t * : _fr_dbuff_move_dbuff_marker_to_dbuff_marker((fr_dbuff_marker_t *)_out, \
_len) \
) \
)

Copy in as many bytes as possible from one dbuff or marker to another.

Warning
Advances both _in and _out by _len, this may not be what you want. If you only want _out to be advanced use fr_dbuff_in_memcpy(_out, _in, _len). If you only want _in to be advanced use fr_dbuff_out_memcpy(_out, _in, _len).
Parameters
[in]_outto copy into.
[in]_into copy from.
[in]_lenThe maximum length to copy.
Returns
Number of bytes to copy.

Definition at line 1648 of file dbuff.h.

◆ fr_dbuff_out

#define fr_dbuff_out (   _out,
  _dbuff_or_marker 
)
Value:
_Generic((_out), \
uint8_t * : _fr_dbuff_out_memcpy((uint8_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), 1), \
uint16_t * : _fr_dbuff_out_uint16((uint16_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker)), \
uint32_t * : _fr_dbuff_out_uint32((uint32_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker)), \
uint64_t * : _fr_dbuff_out_uint64((uint64_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker)), \
int8_t * : _fr_dbuff_out_memcpy((uint8_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), 1), \
int16_t * : _fr_dbuff_out_int16((int16_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker)), \
int32_t * : _fr_dbuff_out_int32((int32_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker)), \
int64_t * : _fr_dbuff_out_int64((int64_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker)), \
float * : _fr_dbuff_out_uint32((uint32_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker)), \
double * : _fr_dbuff_out_uint64((uint64_t *)(_out), _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker)) \
)

Copy data from a dbuff or marker to a fixed sized C type.

Parameters
[out]_outWhere to write the data. If out is an integer type a byteswap will be performed if native endianness is not big endian.
[in]_dbuff_or_markerto copy data from. Will be advanced by the number of bytes consumed, i.e. if out is a uin16_t *, _dbuff_or_marker will be advanced by two bytes.
Returns
  • <0 the number of bytes we would have needed to complete the conversion.
  • >0 the number of bytes _in was advanced by.

Definition at line 1779 of file dbuff.h.

◆ fr_dbuff_out_int64v

#define fr_dbuff_out_int64v (   _num,
  _dbuff_or_marker,
  _len 
)     _fr_dbuff_out_int64v(_num, _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), _len)

Read bytes from a dbuff or marker and interpret them as a network order signed integer.

Parameters
[in]_numpoints to an int64_t to store the integer in
[in]_dbuff_or_markerdata to copy bytes from
[in]_lennumber of bytes to read (must be positive and less than eight)
Returns
  • 0 no data read.
  • >0 the number of bytes read.
  • <0 the number of bytes we would have needed to complete the read operation.

Definition at line 1867 of file dbuff.h.

◆ FR_DBUFF_OUT_INT64V_RETURN

#define FR_DBUFF_OUT_INT64V_RETURN (   _num,
  _dbuff_or_marker,
  _len 
)    FR_DBUFF_RETURN(fr_dbuff_out_int64v, _num, _dbuff_or_marker, _len)

Read bytes from a dbuff or marker and interpret them as a network order unsigned integer.

Parameters
[in]_numpoints to an int64_t to store the integer in
[in]_dbuff_or_markerdata to copy bytes from
[in]_lennumber of bytes to read (must be positive and less than eight)
Returns
  • 0 no data read.
  • >0 the number of bytes read.
  • <0 the number of bytes we would have needed to complete the read operation.

Definition at line 1874 of file dbuff.h.

◆ fr_dbuff_out_memcpy

#define fr_dbuff_out_memcpy (   _out,
  _dbuff_or_marker,
  _outlen 
)
Value:
_Generic((_out), \
uint8_t * : _fr_dbuff_out_memcpy((uint8_t *)(_out), \
_fr_dbuff_current_ptr(_dbuff_or_marker), \
fr_dbuff_ptr(_dbuff_or_marker), \
_outlen), \
fr_dbuff_t * : _fr_dbuff_out_memcpy_dbuff(_fr_dbuff_current_ptr((fr_dbuff_t *)_out), \
fr_dbuff_ptr((fr_dbuff_t *)(_out)), \
_fr_dbuff_current_ptr(_dbuff_or_marker), \
fr_dbuff_ptr(_dbuff_or_marker), _outlen), \
fr_dbuff_marker_t * : _fr_dbuff_out_memcpy_dbuff(_fr_dbuff_current_ptr((fr_dbuff_marker_t *)_out), \
_fr_dbuff_current_ptr(_dbuff_or_marker), \
fr_dbuff_ptr(_dbuff_or_marker), _outlen) \
)

Copy exactly _outlen bytes from the dbuff.

If _out is a dbuff and _outlen is greater than the number of bytes available in that dbuff, the copy operation will fail.

Note
_out will not be advanced. If this is required fr_dbuff_move should be used.
Parameters
[in]_outeither a buffer, or another dbuff/marker to copy data to.
[in]_dbuff_or_markerto copy data from.
[in]_outlenHow much data we need to copy. If _out is fr_dbuff_t * and SIZE_MAX is passed, then _inlen will be substituted for the length of the buffer.
Returns
  • 0 no data copied.
  • >0 the number of bytes copied.
  • <0 the number of bytes we would have needed to complete the copy operation.

Definition at line 1724 of file dbuff.h.

◆ FR_DBUFF_OUT_MEMCPY_RETURN

#define FR_DBUFF_OUT_MEMCPY_RETURN (   _out,
  _dbuff_or_marker,
  _outlen 
)    FR_DBUFF_RETURN(fr_dbuff_out_memcpy, _out, _dbuff_or_marker, _outlen)

Copy outlen bytes from the dbuff returning if there's insufficient data in the dbuff.

If _out is a dbuff and _outlen is greater than the number of bytes available in that dbuff, the copy operation will fail.

Note
_out will not be advanced. If this is required fr_dbuff_move should be used.
Parameters
[in]_outeither a buffer, or another dbuff/marker to copy data to.
[in]_dbuff_or_markerto copy data from.
[in]_outlenHow much data we need to copy. If _out is fr_dbuff_t * and SIZE_MAX is passed, then _inlen will be substituted for the length of the buffer.
Returns
  • 0 no data copied.
  • >0 the number of bytes copied.
  • <0 the number of bytes we would have needed to complete the copy operation.

Definition at line 1744 of file dbuff.h.

◆ FR_DBUFF_OUT_RETURN

#define FR_DBUFF_OUT_RETURN (   _out,
  _dbuff_or_marker 
)    FR_DBUFF_RETURN(fr_dbuff_out, _out, _dbuff_or_marker)

Copy data from a dbuff or marker to a fixed sized C type returning if there is insufficient data.

Parameters
[out]_outWhere to write the data. If out is an integer type a byteswap will be performed if native endianness is not big endian.
[in]_dbuff_or_markerto copy data from. Will be advanced by the number of bytes consumed, i.e. if out is a uin16_t *, _dbuff_or_marker will be advanced by two bytes.
Returns
  • <0 the number of bytes we would have needed to complete the conversion.
  • >0 the number of bytes _in was advanced by.

Definition at line 1797 of file dbuff.h.

◆ fr_dbuff_out_uint64v

#define fr_dbuff_out_uint64v (   _num,
  _dbuff_or_marker,
  _len 
)     _fr_dbuff_out_uint64v(_num, _fr_dbuff_current_ptr(_dbuff_or_marker), fr_dbuff_ptr(_dbuff_or_marker), _len)

Read bytes from a dbuff or marker and interpret them as a network order unsigned integer.

Parameters
[in]_numpoints to a uint64_t to store the integer in
[in]_dbuff_or_markerdata to copy bytes from
[in]_lennumber of bytes to read (must be positive and less than eight)
Returns
  • 0 no data read.
  • >0 the number of bytes read.
  • <0 the number of bytes we would have needed to complete the read operation.

Definition at line 1827 of file dbuff.h.

◆ FR_DBUFF_OUT_UINT64V_RETURN

#define FR_DBUFF_OUT_UINT64V_RETURN (   _num,
  _dbuff_or_marker,
  _len 
)    FR_DBUFF_RETURN(fr_dbuff_out_uint64v, _num, _dbuff_or_marker, _len)

Read bytes from a dbuff or marker and interpret them as a network order unsigned integer.

Parameters
[in]_numpoints to a uint64_t to store the integer in
[in]_dbuff_or_markerdata to copy bytes from
[in]_lennumber of bytes to read (must be positive and less than eight)
Returns
  • 0 no data read.
  • >0 the number of bytes read.
  • <0 the number of bytes we would have needed to complete the read operation.

Definition at line 1834 of file dbuff.h.

◆ fr_dbuff_ptr

#define fr_dbuff_ptr (   _dbuff_or_marker)
Value:
_Generic((_dbuff_or_marker), \
fr_dbuff_t * : ((fr_dbuff_t *)(_dbuff_or_marker)), \
fr_dbuff_marker_t * : (((fr_dbuff_marker_t *)(_dbuff_or_marker))->parent) \
)

Return a pointer to the dbuff.

Parameters
[in]_dbuff_or_markerto return a pointer to.
Returns
A pointer to the dbuff.

Definition at line 853 of file dbuff.h.

◆ fr_dbuff_ptr_const

#define fr_dbuff_ptr_const (   _dbuff_or_marker)
Value:
_Generic((_dbuff_or_marker), \
fr_dbuff_t * : ((fr_dbuff_t const *)(_dbuff_or_marker)), \
fr_dbuff_t const * : ((fr_dbuff_t const *)(_dbuff_or_marker)), \
fr_dbuff_marker_t * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->parent), \
fr_dbuff_marker_t const * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->parent) \
)

Return a const pointer to the dbuff.

Parameters
[in]_dbuff_or_markerto return a pointer to.
Returns
A pointer to the dbuff.

Definition at line 864 of file dbuff.h.

◆ fr_dbuff_remaining

#define fr_dbuff_remaining (   _dbuff_or_marker)
Value:
((size_t)(fr_dbuff_end(_dbuff_or_marker) < fr_dbuff_current(_dbuff_or_marker) ? \
0 : (fr_dbuff_end(_dbuff_or_marker) - fr_dbuff_current(_dbuff_or_marker))))
#define fr_dbuff_end(_dbuff_or_marker)
Return the current 'end' position of a dbuff or marker.
Definition: dbuff.h:933
unsigned long int size_t
Definition: merged_model.c:25

Return the number of bytes remaining between the dbuff or marker and the end of the buffer.

Note
Do not use this in functions that may be used for stream decoding unless you're sure you know what you're doing. The value return does not reflect the number of bytes that may be potentially read from the stream, only the number of bytes until the end of the current chunk.
Parameters
[in]_dbuff_or_markerto return the number of bytes remaining for.
Returns
  • >0 the number of bytes remaining before we reach the end of the buffer.
  • -0 we're at the end of the buffer.

Definition at line 738 of file dbuff.h.

◆ FR_DBUFF_REMAINING_RETURN

#define FR_DBUFF_REMAINING_RETURN (   _dbuff_or_marker,
  _len 
)     if ((_len) > fr_dbuff_remaining(_dbuff_or_marker)) return -((_len) - fr_dbuff_remaining(_dbuff_or_marker))

Check if _len bytes are available in the dbuff and if not return the number of bytes we'd need.

Note
Do not use this in functions that may be used for stream decoding unless you're sure you know what you're doing. The value return does not reflect the number of bytes that may be potentially read from the stream, only the number of bytes until the end of the current chunk.
Parameters
[in]_dbuff_or_markerto return the number of bytes remaining for.
[in]_lenMinimum remaining bytes.
Returns
  • >0 the number of bytes remaining before we reach the end of the buffer.
  • -0 we're at the end of the buffer.

Definition at line 756 of file dbuff.h.

◆ FR_DBUFF_RETURN

#define FR_DBUFF_RETURN (   _func,
  ... 
)
Value:
do { \
ssize_t _slen = _func(__VA_ARGS__ ); \
if (_slen < 0) return _slen; \
} while (0)
while(1)
Definition: acutest.h:856
return
Definition: module.c:186

Generic wrapper macro to return if there's insufficient memory to satisfy the request on the dbuff.

Definition at line 184 of file dbuff.h.

◆ fr_dbuff_set

#define fr_dbuff_set (   _dst,
  _src 
)
Value:
_fr_dbuff_set(\
_fr_dbuff_current_ptr(_dst), fr_dbuff_ptr(_dst), \
_Generic((_src), \
fr_dbuff_t * : fr_dbuff_current((fr_dbuff_t const *)(_src)), \
uint8_t const * : (uint8_t const *)(_src), \
uint8_t * : (uint8_t const *)(_src), \
size_t : (fr_dbuff_start(_dst) + (uintptr_t)(_src)), \
long : (fr_dbuff_start(_dst) + (uintptr_t)(_src)), \
int : (fr_dbuff_start(_dst) + (uintptr_t)(_src)) \
) \
)
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition: dbuff.h:893

Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer, or a length value.

Parameters
[in]_dstdbuff or marker to set the position for.
[in]_srcVariable to glean new position from. Behaviour here depends on the type of the variable.
  • dbuff, the current position of the dbuff.
  • marker, the current position of the marker.
  • pointer, the position of the pointer.
  • size_t, _dst->start + _src.
Returns
  • 0 not advanced.
  • >0 the number of bytes the dbuff was advanced by.
  • <0 the number of bytes required to complete the advancement

Definition at line 999 of file dbuff.h.

◆ fr_dbuff_set_end

#define fr_dbuff_set_end (   _dst,
  _end 
)
Value:
_fr_dbuff_set_end(\
fr_dbuff_ptr(_dst), \
_Generic((_end), \
fr_dbuff_t * : fr_dbuff_current((fr_dbuff_t const *)(_end)), \
uint8_t const * : (uint8_t const *)(_end), \
uint8_t * : (uint8_t const *)(_end) \
) \
)

Set a new 'end' position in a dbuff or marker.

Parameters
[out]_dstdbuff to use for constraints checks.
[in]_endPosition to set.
Returns
  • 0 not advanced (p before dbuff start) or after dbuff end.
  • >0 the number of bytes the dbuff was trimmed by.

Definition at line 1046 of file dbuff.h.

◆ FR_DBUFF_SET_RETURN

#define FR_DBUFF_SET_RETURN (   _dst,
  _src 
)    FR_DBUFF_RETURN(fr_dbuff_set, _dst, _src)

Set the 'current' position in a dbuff or marker returning if _src is out of range.

Definition at line 1017 of file dbuff.h.

◆ fr_dbuff_set_to_end

#define fr_dbuff_set_to_end (   _dbuff_or_marker)     fr_dbuff_set(_dbuff_or_marker, fr_dbuff_end(_dbuff_or_marker))

Reset the 'current' position of the dbuff or marker to the 'end' of the buffer.

Definition at line 1156 of file dbuff.h.

◆ fr_dbuff_set_to_start

#define fr_dbuff_set_to_start (   _dbuff_or_marker)     fr_dbuff_set(_dbuff_or_marker, fr_dbuff_start(_dbuff_or_marker))

Reset the 'current' position of the dbuff or marker to the 'start' of the buffer.

Definition at line 1150 of file dbuff.h.

◆ fr_dbuff_start

#define fr_dbuff_start (   _dbuff_or_marker)
Value:
(_Generic((_dbuff_or_marker), \
fr_dbuff_t * : (((fr_dbuff_t const *)(_dbuff_or_marker))->start), \
fr_dbuff_t const * : (((fr_dbuff_t const *)(_dbuff_or_marker))->start), \
fr_dbuff_marker_t * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->parent->start), \
fr_dbuff_marker_t const * : (((fr_dbuff_marker_t const *)(_dbuff_or_marker))->parent->start) \
))

Return the 'start' position of a dbuff or marker.

The start position is not necessarily the start of the buffer, and is advanced every time a dbuff is copied.

Parameters
[in]_dbuff_or_markerto return the start position of.
Returns
A pointer to the start position of the buffer.

Definition at line 893 of file dbuff.h.

◆ FR_DBUFF_TALLOC_THREAD_LOCAL

#define FR_DBUFF_TALLOC_THREAD_LOCAL (   _out,
  _init,
  _max 
)
Value:
do { \
static _Thread_local fr_dbuff_thread_local_t *_dbuff_t_local; \
if (!_dbuff_t_local) { \
fr_dbuff_thread_local_t *dbtl = talloc_zero(NULL, fr_dbuff_thread_local_t); \
fr_dbuff_init_talloc(dbtl, &dbtl->dbuff, &dbtl->tctx, _init, _max); \
fr_atexit_thread_local(_dbuff_t_local, _dbuff_thread_local_free, dbtl); \
*(_out) = &_dbuff_t_local->dbuff; \
} else { \
fr_dbuff_reset_talloc(&_dbuff_t_local->dbuff); \
*(_out) = &_dbuff_t_local->dbuff; \
} \
} while (0)
static int _dbuff_thread_local_free(void *dbtl)
Definition: dbuff.h:540
Structure to encapsulate a thread local dbuff information.
Definition: dbuff.h:535

Create a function local and thread local extensible dbuff.

Parameters
[out]_outWhere to write a pointer to the thread local dbuff
[in]_initInitial size for the dbuff buffer.
[in]_maxMaximum size of the dbuff buffer.

Definition at line 551 of file dbuff.h.

◆ FR_DBUFF_TMP

#define FR_DBUFF_TMP (   _start,
  _len_or_end 
)
Value:
.buff_i = (uint8_t const *)(_start), \
.start_i = (uint8_t const *)(_start), \
.end_i = _Generic((_len_or_end), \
size_t : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
long : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
int : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
unsigned int : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
uint8_t * : (uint8_t const *)(_len_or_end), \
uint8_t const * : (uint8_t const *)(_len_or_end), \
char * : (uint8_t const *)(_len_or_end), \
char const * : (uint8_t const *)(_len_or_end) \
), \
.p_i = _start, \
.is_const = _Generic((_start), \
uint8_t * : false, \
uint8_t const * : true, \
char * : false, \
char const * : true \
) \
}
struct fr_dbuff_s fr_dbuff_t
A dbuff.
Definition: dbuff.h:65

Creates a compound literal to pass into functions which accept a dbuff.

Note
The return value of the function should be used to determine how much data was written to the buffer.
Parameters
[in]_startof the buffer.
[in]_len_or_endLength of the buffer or the end pointer.

Definition at line 509 of file dbuff.h.

◆ fr_dbuff_used

#define fr_dbuff_used (   _dbuff_or_marker)
Value:
((size_t)(fr_dbuff_start(_dbuff_or_marker) > fr_dbuff_current(_dbuff_or_marker) ? \
0 : (fr_dbuff_current(_dbuff_or_marker) - fr_dbuff_start(_dbuff_or_marker))))

Return the number of bytes remaining between the start of the dbuff or marker and the current position.

Definition at line 762 of file dbuff.h.

◆ fr_dbuff_was_extended

#define fr_dbuff_was_extended (   _status)    ((_status) & FR_DBUFF_FLAG_EXTENDED)

Check if the dbuff was extended during the last extend call.

Definition at line 609 of file dbuff.h.

Typedef Documentation

◆ fr_dbuff_extend_t

typedef size_t(* fr_dbuff_extend_t) (fr_dbuff_t *dbuff, size_t req_extension)

dbuff extension callback

This callback is used to extend the underlying buffer.

  • Where the buffer is being used to aggregate data, this callback will usually call realloc to extend the buffer.
  • Where the buffer is being used for stream decoding, this callback will usually shift the existing data in the buffer to the left, and read in more data from the stream.

After performing an operation on the underlying buffer, this callback should call fr_dbuff_update to fix position pointers in the current dbuff and its parents and markers.

Generally the caller will request the minimum amount the buffer should be extended by. This callback may choose to ignore the request and extend the buffer by more than the requested amount.

Parameters
[in]dbuffto extend.
[in]req_extensionHow much the caller wants to extend the buffer by.
Returns
How much the buffer was extended by.
See also
fr_dbuff_update

Definition at line 108 of file dbuff.h.

◆ fr_dbuff_marker_t

typedef struct fr_dbuff_marker_s fr_dbuff_marker_t

A position marker associated with a dbuff.

Markers are used whenever the caller needs to access part of the underlying buffer other than the 'start', 'current' or 'end' positions described by a fr_dbuff_t.

Markers are needed because if a fr_dbuff_t is extended, pointers into the underlying buffer may be invalidated by a realloc or memmove.

Markers are intended to be allocated on the stack and associated with a stack-frame-local fr_dbuff_t. Using a stack-frame-local dbuff ensures markers are automatically released when the stack frame is popped so that markers are not leaked.

Definition at line 1 of file dbuff.h.

◆ fr_dbuff_t

typedef struct fr_dbuff_s fr_dbuff_t

A dbuff.

dbuffs wrap an underlying buffer, maintaining 'start', 'current', and 'end' position pointers.

dbuffs also contain information on if and how the underlying buffer can be extended.

For encoding extending means reallocing the underlying buffer so that there's addition space to write data to.

For stream decoding extending means shifting out existing data and refilling the underlying buffer from a data source.

dbuffs are intended to be organised into hierarchies, with one dbuff per stack frame, initialised from a parent in a higher stack frame.

Each time a dbuff is copied (using one of the provided FR_DBUFF_BIND_CURRENT_* macros), the copy's 'start' position is updated to be the 'current' position of its parent. This ensures length macros report only spaced used/available in the new dbuff and not its parent. Other copy macros may move the 'end' position, to artificially limit the amount of data available.

Definition at line 1 of file dbuff.h.

Enumeration Type Documentation

◆ fr_dbuff_extend_status_t

Whether the buffer is currently extendable and whether it was extended.

Enumerator
FR_DBUFF_NOT_EXTENDABLE 

dbuff cannot be extended

FR_DBUFF_EXTENDABLE 

dbuff can be extended

FR_DBUFF_EXTENDABLE_EXTENDED 

dbuff was extended in the last extend call and may be extended again

FR_DBUFF_EXTENDED 

dbuff was extended in the last extend call but cannot be extended again

Definition at line 585 of file dbuff.h.

Function Documentation

◆ _dbuff_thread_local_free()

static int _dbuff_thread_local_free ( void *  dbtl)
inlinestatic

Definition at line 540 of file dbuff.h.

+ Here is the call graph for this function:

◆ _fr_dbuff_extend_fd()

size_t _fr_dbuff_extend_fd ( fr_dbuff_t dbuff,
size_t  extension 
)

Refresh the buffer with more data from the file.

Check for errors

Definition at line 175 of file dbuff.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _fr_dbuff_extend_talloc()

size_t _fr_dbuff_extend_talloc ( fr_dbuff_t dbuff,
size_t  extension 
)

Reallocate the current buffer.

Parameters
[in]dbuffto be extended.
[in]extensionHow many additional bytes should be allocated in the buffer.
Returns
  • 0 the extension operation failed.
  • >0 the number of bytes the buffer was extended by.

Definition at line 235 of file dbuff.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_dbuff_free_talloc()

static void fr_dbuff_free_talloc ( fr_dbuff_t dbuff)
inlinestatic

Free the talloc buffer associated with a dbuff.

Definition at line 448 of file dbuff.h.

◆ fr_dbuff_init_fd()

static fr_dbuff_t* fr_dbuff_init_fd ( fr_dbuff_t dbuff,
fr_dbuff_uctx_fd_t *  fctx,
uint8_t buff,
size_t  len,
int  fd,
size_t  max 
)
inlinestatic

Initialise a special dbuff which automatically reads in more data as the buffer is exhausted.

Parameters
[out]dbuffto initialise.
[out]fctxto initialise. Must have a lifetime >= to the dbuff.
[in]buffTemporary buffer to use for storing file contents.
[in]lenLength of the temporary buffer.
[in]fddescriptor of an open file to read from.
[in]maxThe maximum length of data to read from the file.
Returns
  • The passed dbuff on success.
  • NULL on failure.

< Store the real end

< Starts with 0 bytes available

Definition at line 480 of file dbuff.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_dbuff_init_talloc()

static fr_dbuff_t* fr_dbuff_init_talloc ( TALLOC_CTX *  ctx,
fr_dbuff_t dbuff,
fr_dbuff_uctx_talloc_t *  tctx,
size_t  init,
size_t  max 
)
inlinestatic

Initialise a special dbuff which automatically extends as additional data is written.

Parameters
[in]ctxto allocate buffer in.
[out]dbuffto initialise.
[out]tctxto initialise. Must have a lifetime >= to the dbuff.
[in]initThe length of the initial buffer.
[in]maxThe maximum length of the buffer.
Returns
  • The passed dbuff on success.
  • NULL on failure.

Definition at line 406 of file dbuff.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_dbuff_marker()

static uint8_t* fr_dbuff_marker ( fr_dbuff_marker_t m,
fr_dbuff_t dbuff 
)
inlinestatic

Initialises a new marker pointing to the 'current' position of the dbuff.

Parameters
[out]mto initialise.
[in]dbuffto associate marker with.
Returns
The position the marker was set to.

Definition at line 1187 of file dbuff.h.

+ Here is the caller graph for this function:

◆ fr_dbuff_marker_release()

static void fr_dbuff_marker_release ( fr_dbuff_marker_t m)
inlinestatic

Releases the specified marker and any markers added before it.

Pointers should be released in the inverse order to allocation.

Parameters
[in]mto release.

Definition at line 1205 of file dbuff.h.

+ Here is the caller graph for this function:

◆ fr_dbuff_marker_release_ahead()

static size_t fr_dbuff_marker_release_ahead ( fr_dbuff_marker_t m)
inlinestatic

Trims the linked list back to the specified pointer and return how many bytes marker was ahead of p.

Pointers should be released in the inverse order to allocation.

Alternatively the oldest pointer can be released, resulting in any newer pointer also being removed from the list.

Parameters
[in]mto release.
Returns
  • 0 marker is ahead of p.
  • >0 the number of bytes the marker is behind p.

Definition at line 1245 of file dbuff.h.

+ Here is the call graph for this function:

◆ fr_dbuff_marker_release_behind()

static size_t fr_dbuff_marker_release_behind ( fr_dbuff_marker_t m)
inlinestatic

Trims the linked list back to the specified pointer and return how many bytes marker was behind p.

Pointers should be released in the inverse order to allocation.

Alternatively the oldest pointer can be released, resulting in any newer pointer also being removed from the list.

Parameters
[in]mto release.
Returns
  • 0 marker is ahead of p.
  • >0 the number of bytes the marker is behind p.

Definition at line 1226 of file dbuff.h.

+ Here is the call graph for this function:

◆ fr_dbuff_reset_talloc()

int fr_dbuff_reset_talloc ( fr_dbuff_t dbuff)

Reset a talloced buffer to its initial length, clearing any data stored.

Parameters
[in]dbuffto reset.
Returns
  • 0 on success.
  • -1 on failure - markers present pointing past the end of string data.

Definition at line 332 of file dbuff.c.

+ Here is the call graph for this function:

◆ fr_dbuff_shift()

size_t fr_dbuff_shift ( fr_dbuff_t dbuff,
size_t  shift 
)

Shift the contents of the dbuff, returning the number of bytes we managed to shift.

Parameters
[in]dbuffto shift.
[in]shiftthe number of bytes to shift (not necessarily the amount actually shifted, because one can't move pointers outside the buffer)
Returns
  • 0 the shift failed due to constraining pointers.
  • >0 the number of bytes we managed to shift pointers in the dbuff.

Definition at line 116 of file dbuff.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_dbuff_trim_talloc()

int fr_dbuff_trim_talloc ( fr_dbuff_t dbuff,
size_t  len 
)

Trim a talloced dbuff to the minimum length required to represent the contained string.

Parameters
[in]dbuffto trim.
[in]lenLength to trim to. Passing SIZE_MAX will result in the buffer being trimmed to the length of the content.
Returns
  • 0 on success.
  • -1 on failure - markers present pointing past the end of string data.

Definition at line 297 of file dbuff.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_dbuff_update()

void fr_dbuff_update ( fr_dbuff_t dbuff,
uint8_t new_buff,
size_t  new_len 
)

Update all markers and pointers in the set of dbuffs to point to new_buff.

This function should be used if the underlying buffer is realloced.

Parameters
[in]dbuffto update.
[in]new_buffto assign to to sbuff.
[in]new_lenLength of the new buffer.

Definition at line 79 of file dbuff.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function: