libisdn
msgb.c File Reference
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "msgb.h"

Go to the source code of this file.

Enumerations

enum  { MSGBF_NONE = 0, MSGBF_BUFFER_OWNED = (1 << 0) }

Functions

int msgb_init (struct msgb *msgb, const uint8_t *buf, const int size)
struct msgbmsgb_alloc (const int size)
struct msgbmsgb_alloc_reserve (const int size, const int reserve)
int msgb_destroy (struct msgb *msgb)
int msgb_size (const struct msgb *msgb)
int msgb_length (const struct msgb *msgb)
int msgb_space (const struct msgb *msgb)
int msgb_reserved_space (const struct msgb *msgb)
int msgb_empty (const struct msgb *msgb)
int msgb_full (const struct msgb *msgb)
int msgb_left (const struct msgb *msgb)
int msgb_tell (const struct msgb *msgb)
uint8_t * msgb_head_ptr (const struct msgb *msgb)
uint8_t * msgb_tail_ptr (const struct msgb *msgb)
int msgb_reserve (struct msgb *msgb, const int reserve)
int msgb_unreserve (struct msgb *msgb, const int size)
int msgb_rewind (struct msgb *msgb)
int msgb_seek (struct msgb *msgb, const msgb_seek_t type, const int offset)
int msgb_clear (struct msgb *msgb)
int msgb_reset (struct msgb *msgb)
uint8_t * msgb_put (struct msgb *msgb, const int size)
uint8_t * msgb_get (struct msgb *msgb, const int size)
int msgb_write (struct msgb *msgb, const void *buf, const int size)
int msgb_read (struct msgb *msgb, void *buf, const int size)
int msgb_fill (struct msgb *msgb, const int c, const int size)
int msgb_chain_append (struct msgb *chain, struct msgb *msgb)
struct msgbmsgb_chain_split (struct msgb *chain, struct msgb *where)
struct msgbmsgb_chain_split_at (struct msgb *chain, const int pos)
struct msgbmsgb_linearize (struct msgb *msgb)
struct msgbmsgb_linearize_reserve (struct msgb *msgb, const int reserve)
int msgb_merge (struct msgb *msgb, struct msgb *outb)
struct msgbmsgb_split (struct msgb *msgb, const int size)
struct msgbmsgb_split_reserve (struct msgb *msgb, const int size, const int reserve)
int msgb_chain_count (const struct msgb *chain)
int msgb_chain_length (const struct msgb *chain)
int msgb_chain_size (const struct msgb *chain)

Enumeration Type Documentation

anonymous enum
Enumerator:
MSGBF_NONE 
MSGBF_BUFFER_OWNED 

Definition at line 15 of file msgb.c.


Function Documentation

struct msgb* msgb_alloc ( const int  size) [read]

Allocate message buffer of given size

Parameters:
sizeSize of buffer to allocate
Returns:
Newly allocated message buffer on success, NULL otherwise

Definition at line 50 of file msgb.c.

References MSGBF_BUFFER_OWNED.

Referenced by msgb_alloc_reserve().

struct msgb* msgb_alloc_reserve ( const int  size,
const int  reserve 
) [read]

Allocate message buffer of given size and reserve space at the beginning

Note:
Same as manually calling msgb_alloc() + msgb_reserve()
Parameters:
sizeSize of buffer to allocate
reserveNumber of bytes to reserve
Returns:
Newly allocated message buffer on success, NULL otherwise

Definition at line 69 of file msgb.c.

References msgb_alloc(), and msgb_reserve().

int msgb_chain_append ( struct msgb chain,
struct msgb msgb 
)

Append msgb to chain

Parameters:
[in]chainChain of msg buffers
[in]msgbBuffer to append (can be a chain too)
Returns:
0 on success, -1 on error

Definition at line 381 of file msgb.c.

References msgb::next.

int msgb_chain_count ( const struct msgb chain)

Number of msgbs in chain

Parameters:
[in]chainmsgb chain
Returns:
number of buffers in chain

Definition at line 560 of file msgb.c.

References msgb::next.

int msgb_chain_length ( const struct msgb chain)

Total length of data in chain

Parameters:
[in]chainmsgb chain
Returns:
Sum of msgb_length() of all msgbs on success, -1 on error

Definition at line 569 of file msgb.c.

int msgb_chain_size ( const struct msgb chain)

Total size of msgbs in chain

Parameters:
[in]chainmsgb chain
Returns:
Sum of msgb_size() of all msgbs on success, -1 on error

Definition at line 574 of file msgb.c.

struct msgb* msgb_chain_split ( struct msgb chain,
struct msgb where 
) [read]

Split chain at indicated element

Parameters:
[in]chainChain of msg buffers
[in]msgbBuffer to append (can be a chain too)
Returns:
Remaining part of the chain or NULL on error

Definition at line 394 of file msgb.c.

References msgb::next.

struct msgb* msgb_chain_split_at ( struct msgb chain,
const int  pos 
) [read]

Split chain at indicated element (index)

Parameters:
[in]chainChain of msg buffers
[in]msgbBuffer to append (can be a chain too)
Returns:
Remaining part of the chain or NULL on error

Definition at line 410 of file msgb.c.

References msgb::next.

int msgb_clear ( struct msgb msgb)

Clear data, move read, tail and end pointer to position of head

Note:
Does not release reserved space
Parameters:
msgbMessage buffer
Returns:
0 on success, < 0 on error

Definition at line 270 of file msgb.c.

References msgb::buf, msgb::end, msgb::head, msgb::size, and msgb::tail.

int msgb_destroy ( struct msgb msgb)

Destroy message buffer

Note:
Buffer content is zeroed
Parameters:
msgbMessage buffer
Returns:
0 on success, <0 on error

Definition at line 88 of file msgb.c.

References msgb::next.

int msgb_empty ( const struct msgb msgb)

Is message buffer empty?

Parameters:
msgbMessage buffer
Returns:
1 if buffer is empty, 0 otherwise

Definition at line 134 of file msgb.c.

References msgb::buf, msgb::end, msgb::head, and msgb::size.

int msgb_fill ( struct msgb msgb,
const int  c,
const int  size 
)

Fill buffer with character, starting at current write position

Note:
Updates tail pointer
Parameters:
[in]msgbBuffer to fill
[in]cCharacter
[in]sizeNumber of characters to write
Returns:
Number of bytes filled, < 0 on error

Definition at line 355 of file msgb.c.

References msgb_put(), msgb_space(), and msgb::size.

int msgb_full ( const struct msgb msgb)

Is message buffer full?

Parameters:
msgbMessage buffer
Returns:
1 if buffer is full, 0 otherwise

Definition at line 142 of file msgb.c.

References msgb::buf, msgb::end, and msgb::size.

uint8_t* msgb_get ( struct msgb msgb,
const int  size 
)

Move read pointer

Parameters:
msgbMessage buffer
sizeNumber of bytes to move read pointer
Returns:
Pointer to old position of read pointer on success, NULL otherwise

Definition at line 307 of file msgb.c.

References msgb::buf, msgb::end, msgb::size, and msgb::tail.

Referenced by msgb_read().

uint8_t* msgb_head_ptr ( const struct msgb msgb)

Get pointer to head of buffer

Parameters:
msgbMessage buffer
Returns:
Pointer to head of buffer on success, NULL otherwise

Definition at line 166 of file msgb.c.

References msgb::buf, msgb::head, and msgb::size.

Referenced by msgb_merge().

int msgb_init ( struct msgb msgb,
const uint8_t *  buf,
const int  size 
)

Initialize custom message buffer

Note:
Buffer content is _not_ zeroed, use msgb_clear()
Parameters:
msgbMessage buffer
bufExternal buffer space
sizeSize of buffer space
Returns:
0 on success, <0 on error

Definition at line 45 of file msgb.c.

References MSGBF_NONE.

int msgb_left ( const struct msgb msgb)

Get number of remaining bytes to read in message buffer

Parameters:
msgbMessage buffer
Returns:
Number of remaining bytes on success, < 0 on error

Definition at line 150 of file msgb.c.

References msgb::buf, msgb::end, msgb::size, and msgb::tail.

Referenced by msgb_merge(), and msgb_read().

int msgb_length ( const struct msgb msgb)

Get length of data in message buffer

Parameters:
msgbMessage buffer
Returns:
Size of data on success, < 0 on error

Definition at line 110 of file msgb.c.

References msgb::buf, msgb::end, msgb::head, and msgb::size.

Referenced by msgb_merge().

struct msgb* msgb_linearize ( struct msgb msgb) [read]

Allocate new msgb and merge content of input (chain) into new buffer

Parameters:
[in]msgbInput (chain of msg) buffer(s)
Returns:
Liniearized output buffer on success, NULL on error

Definition at line 484 of file msgb.c.

struct msgb* msgb_linearize_reserve ( struct msgb msgb,
const int  reserve 
) [read]

Allocate new msgb with reserved headroom and merge content of input (chain) into new buffer

Note:
Output buffer will be <size of="" input>=""> + <reserve> bytes large
Parameters:
[in]msgbInput (chain of msg) buffer(s)
[in]reserveSpace to reserve in bytes
Returns:
Liniearized output buffer on success, NULL on error

Definition at line 489 of file msgb.c.

int msgb_merge ( struct msgb msgb,
struct msgb outb 
)

Merge chain of input buffers into output buffer

Note:
Output buffer should have enough free space or this operation will fail
Read pointer of input buffers is reset
Parameters:
[in]msgbInput (chain of) msg buffer(s)
[in]outbOutput msg buffer
Returns:
0 on success, -1 on error (e.g. not enough space in output buffer)

Definition at line 494 of file msgb.c.

References msgb_head_ptr(), msgb_left(), msgb_length(), msgb_write(), msgb::next, and msgb::size.

uint8_t* msgb_put ( struct msgb msgb,
const int  size 
)

Move tail pointer

Parameters:
msgbMessage buffer
sizeNumber of bytes to move tail pointer
Returns:
Pointer to old position of tail on success, NULL otherwise

Definition at line 292 of file msgb.c.

References msgb::buf, msgb::end, MAX, msgb::size, and msgb::tail.

Referenced by msgb_fill(), and msgb_write().

int msgb_read ( struct msgb msgb,
void *  buf,
const int  size 
)

Copy data from buffer

Note:
Returns 0 if no data is left (this emulates read(2) behaviour)
Parameters:
msgbMessage buffer
bufBuffer to copy into
sizeNumber of bytes to copy from message buffer
Returns:
0 on success, < 0 otherwise

Definition at line 339 of file msgb.c.

References msgb_get(), msgb_left(), and msgb::size.

int msgb_reserve ( struct msgb msgb,
const int  reserve 
)

Reserve space at the beginning of the buffer

Note:
Updates head and tail pointer, existing data at the beginning may become inaccessible
Parameters:
msgbMessage buffer
reserveNumber of bytes to reserve
Returns:
0 on success, <0 on error

Definition at line 187 of file msgb.c.

References msgb::end, msgb::head, msgb::size, and msgb::tail.

Referenced by msgb_alloc_reserve().

int msgb_reserved_space ( const struct msgb msgb)

Get reserved space of message buffer (= space before head pointer)

Parameters:
msgbMessage buffer
Returns:
Size of reserved buffer space on success, < 0 on error

Definition at line 126 of file msgb.c.

References msgb::buf, msgb::head, and msgb::size.

int msgb_reset ( struct msgb msgb)

Completely reset buffer

Note:
Clears data and releases reserved space
Parameters:
msgbMessage buffer
Returns:
0 on success, < 0 on error

Definition at line 279 of file msgb.c.

References msgb::buf, msgb::end, msgb::head, msgb::size, and msgb::tail.

int msgb_rewind ( struct msgb msgb)

Set read and tail pointer to beginning of data

Parameters:
msgbMessage buffer
Returns:
0 on success, < 0 on error

Definition at line 234 of file msgb.c.

References msgb::buf, msgb::head, and msgb::tail.

int msgb_seek ( struct msgb msgb,
const msgb_seek_t  type,
const int  offset 
)

Move read and tail pointer to desired location

Parameters:
msgbMessage buffer
typeType of seek (starting point and direction)
offsetNumber of bytes to seek
Returns:
0 on success, < 0 on error

Definition at line 242 of file msgb.c.

References msgb::buf, msgb::end, msgb::head, MSGB_SEEK_CUR, MSGB_SEEK_END, MSGB_SEEK_SET, and msgb::tail.

int msgb_size ( const struct msgb msgb)

Get allocated size of message buffer

Parameters:
msgbMessage buffer
Returns:
Size of buffer on success, < 0 on error

Definition at line 105 of file msgb.c.

References msgb::buf, and msgb::size.

int msgb_space ( const struct msgb msgb)

Get remaining free space in message buffer for writing (= free space after tail pointer)

Parameters:
msgbMessage buffer
Returns:
Size of buffer on success, < 0 on error

Definition at line 118 of file msgb.c.

References msgb::buf, msgb::size, and msgb::tail.

Referenced by msgb_fill(), and msgb_write().

struct msgb* msgb_split ( struct msgb msgb,
const int  size 
) [read]

Split msgb into chain of <size> each

Parameters:
[in]msgbInput msg buffer
[in]sizeSize of fragments to split input buffer into
Returns:
Chain of msg buffers on succes, NULL on error

Definition at line 550 of file msgb.c.

struct msgb* msgb_split_reserve ( struct msgb msgb,
const int  size,
const int  reserve 
) [read]

Split msgb into chain of <size> each with additional headroom of <reserve> bytes

Note:
Output buffers will be <size> + <reserve> bytes large
Parameters:
[in]msgbInput msg buffer
[in]msgbInput msg buffer
[in]sizeSize of fragments to split input buffer into
[in]reserveSize of headroom to reserve
Returns:
Chain of msg buffers on succes, NULL on error

Definition at line 555 of file msgb.c.

uint8_t* msgb_tail_ptr ( const struct msgb msgb)

Get pointer to tail of buffer

Parameters:
msgbMessage buffer
Returns:
Pointer to tail of buffer on success, NULL otherwise

Definition at line 174 of file msgb.c.

References msgb::buf, msgb::size, and msgb::tail.

int msgb_tell ( const struct msgb msgb)

Get current tail pointer position

Parameters:
[in]msgbMessage buffer
Returns:
>= 0 on success, < 0 on error
Note:
Returns the position relative to head pointer

Definition at line 158 of file msgb.c.

References msgb::buf, msgb::head, msgb::size, and msgb::tail.

int msgb_unreserve ( struct msgb msgb,
const int  size 
)

Make reserved space at the beginning available

Note:
Moves buffer read and/or tail pointer to new head position if either pointed to the head before
Parameters:
msgbMessage buffer
sizeNumber of bytes to unreserve, <= 0 for everything
Returns:
0 on success, <0 on error

Definition at line 204 of file msgb.c.

References msgb::end, msgb::head, msgb::size, and msgb::tail.

int msgb_write ( struct msgb msgb,
const void *  buf,
const int  size 
)

Copy data into buffer

Note:
No space left is also an error condition (this emulates write(2) behaviour)
Parameters:
msgbMessage buffer
bufBuffer to copy from
sizeNumber of bytes to copy into message buffer
Returns:
0 on success, < 0 otherwise

Definition at line 321 of file msgb.c.

References msgb_put(), msgb_space(), and msgb::size.

Referenced by msgb_merge().