libre  0.6.0
Data Structures | Macros | Functions
re_mbuf.h File Reference

Interface to memory buffers. More...

#include <stdarg.h>
Include dependency graph for re_mbuf.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  mbuf
 Defines a memory buffer. More...
 

Macros

#define MBUF_DEBUG   1
 Mbuf debugging (0 or 1)
 
#define MBUF_CHECK_POS(mb)
 Check that mbuf position does not exceed end.
 
#define MBUF_CHECK_END(mb)
 Check that mbuf end does not exceed size.
 

Functions

struct mbufmbuf_alloc (size_t size)
 Allocate a new memory buffer. More...
 
struct mbufmbuf_alloc_ref (struct mbuf *mbr)
 Allocate a new memory buffer with a reference to another mbuf. More...
 
void mbuf_init (struct mbuf *mb)
 Initialize a memory buffer. More...
 
void mbuf_reset (struct mbuf *mb)
 Reset a memory buffer. More...
 
int mbuf_resize (struct mbuf *mb, size_t size)
 Resize a memory buffer. More...
 
void mbuf_trim (struct mbuf *mb)
 Trim unused trailing bytes in a memory buffer, resize if necessary. More...
 
int mbuf_shift (struct mbuf *mb, ssize_t shift)
 Shift mbuf content position. More...
 
int mbuf_write_mem (struct mbuf *mb, const uint8_t *buf, size_t size)
 Write a block of memory to a memory buffer. More...
 
int mbuf_write_u8 (struct mbuf *mb, uint8_t v)
 Write an 8-bit value to a memory buffer. More...
 
int mbuf_write_u16 (struct mbuf *mb, uint16_t v)
 Write a 16-bit value to a memory buffer. More...
 
int mbuf_write_u32 (struct mbuf *mb, uint32_t v)
 Write a 32-bit value to a memory buffer. More...
 
int mbuf_write_u64 (struct mbuf *mb, uint64_t v)
 Write a 64-bit value to a memory buffer. More...
 
int mbuf_write_str (struct mbuf *mb, const char *str)
 Write a null-terminated string to a memory buffer. More...
 
int mbuf_write_pl (struct mbuf *mb, const struct pl *pl)
 Write a pointer-length string to a memory buffer. More...
 
int mbuf_read_mem (struct mbuf *mb, uint8_t *buf, size_t size)
 Read a block of memory from a memory buffer. More...
 
uint8_t mbuf_read_u8 (struct mbuf *mb)
 Read an 8-bit value from a memory buffer. More...
 
uint16_t mbuf_read_u16 (struct mbuf *mb)
 Read a 16-bit value from a memory buffer. More...
 
uint32_t mbuf_read_u32 (struct mbuf *mb)
 Read a 32-bit value from a memory buffer. More...
 
uint64_t mbuf_read_u64 (struct mbuf *mb)
 Read a 64-bit value from a memory buffer. More...
 
int mbuf_read_str (struct mbuf *mb, char *str, size_t size)
 Read a string from a memory buffer. More...
 
int mbuf_strdup (struct mbuf *mb, char **strp, size_t len)
 Duplicate a null-terminated string from a memory buffer. More...
 
int mbuf_vprintf (struct mbuf *mb, const char *fmt, va_list ap)
 Print a formatted variable argument list to a memory buffer. More...
 
int mbuf_printf (struct mbuf *mb, const char *fmt,...)
 Print a formatted string to a memory buffer. More...
 
int mbuf_write_pl_skip (struct mbuf *mb, const struct pl *pl, const struct pl *skip)
 Write a pointer-length string to a memory buffer, excluding a section. More...
 
int mbuf_fill (struct mbuf *mb, uint8_t c, size_t n)
 Write n bytes of value 'c' to a memory buffer. More...
 
int mbuf_debug (struct re_printf *pf, const struct mbuf *mb)
 Debug the memory buffer. More...
 

Detailed Description

Interface to memory buffers.

Copyright (C) 2010 Creytiv.com

Definition in file re_mbuf.h.

Function Documentation

◆ mbuf_alloc()

struct mbuf* mbuf_alloc ( size_t  size)

Allocate a new memory buffer.

Parameters
sizeInitial buffer size
Returns
New memory buffer, NULL if no memory

Definition at line 36 of file mbuf.c.

References mem_zalloc().

Referenced by bfcp_reply(), http_creply(), rtmp_control(), sdp_encode(), stun_ereply(), stun_indication(), stun_reply(), and tls_set_ciphers().

◆ mbuf_alloc_ref()

struct mbuf* mbuf_alloc_ref ( struct mbuf mbr)

Allocate a new memory buffer with a reference to another mbuf.

Parameters
mbrMemory buffer to reference
Returns
New memory buffer, NULL if no memory

Definition at line 58 of file mbuf.c.

References mem_zalloc(), and NULL.

◆ mbuf_debug()

int mbuf_debug ( struct re_printf pf,
const struct mbuf mb 
)

Debug the memory buffer.

Parameters
pfPrint handler
mbMemory buffer
Returns
0 if success, otherwise errorcode

Definition at line 601 of file mbuf.c.

References mbuf::buf, mbuf::end, mbuf::pos, re_hprintf(), and mbuf::size.

◆ mbuf_fill()

int mbuf_fill ( struct mbuf mb,
uint8_t  c,
size_t  n 
)

Write n bytes of value 'c' to a memory buffer.

Parameters
mbMemory buffer
cValue to write
nNumber of bytes to write
Returns
0 if success, otherwise errorcode

Definition at line 565 of file mbuf.c.

References mbuf::pos, and mbuf::size.

◆ mbuf_init()

void mbuf_init ( struct mbuf mb)

Initialize a memory buffer.

Parameters
mbMemory buffer to initialize

Definition at line 83 of file mbuf.c.

References mbuf::buf, mbuf::end, NULL, mbuf::pos, and mbuf::size.

Referenced by mbuf_reset(), and md5_printf().

◆ mbuf_printf()

int mbuf_printf ( struct mbuf mb,
const char *  fmt,
  ... 
)

Print a formatted string to a memory buffer.

Parameters
mbMemory buffer
fmtFormatted string
Returns
0 if success, otherwise errorcode

Definition at line 506 of file mbuf.c.

References re_vhprintf().

Referenced by sdp_encode(), and tls_set_ciphers().

◆ mbuf_read_mem()

int mbuf_read_mem ( struct mbuf mb,
uint8_t *  buf,
size_t  size 
)

Read a block of memory from a memory buffer.

Parameters
mbMemory buffer
bufBuffer to read data to
sizeSize of buffer
Returns
0 if success, otherwise errorcode

Definition at line 334 of file mbuf.c.

Referenced by mbuf_read_u16(), mbuf_read_u32(), mbuf_read_u64(), mbuf_read_u8(), and mbuf_strdup().

◆ mbuf_read_str()

int mbuf_read_str ( struct mbuf mb,
char *  str,
size_t  size 
)

Read a string from a memory buffer.

Parameters
mbMemory buffer
strBuffer to read string to
sizeSize of buffer
Returns
0 if success, otherwise errorcode

Definition at line 422 of file mbuf.c.

References mbuf_read_u8().

◆ mbuf_read_u16()

uint16_t mbuf_read_u16 ( struct mbuf mb)

Read a 16-bit value from a memory buffer.

Parameters
mbMemory buffer
Returns
16-bit value

Definition at line 375 of file mbuf.c.

References mbuf_read_mem().

◆ mbuf_read_u32()

uint32_t mbuf_read_u32 ( struct mbuf mb)

Read a 32-bit value from a memory buffer.

Parameters
mbMemory buffer
Returns
32-bit value

Definition at line 390 of file mbuf.c.

References mbuf_read_mem().

◆ mbuf_read_u64()

uint64_t mbuf_read_u64 ( struct mbuf mb)

Read a 64-bit value from a memory buffer.

Parameters
mbMemory buffer
Returns
64-bit value

Definition at line 405 of file mbuf.c.

References mbuf_read_mem().

◆ mbuf_read_u8()

uint8_t mbuf_read_u8 ( struct mbuf mb)

Read an 8-bit value from a memory buffer.

Parameters
mbMemory buffer
Returns
8-bit value

Definition at line 360 of file mbuf.c.

References mbuf_read_mem().

Referenced by mbuf_read_str().

◆ mbuf_reset()

void mbuf_reset ( struct mbuf mb)

Reset a memory buffer.

Parameters
mbMemory buffer to reset

Definition at line 100 of file mbuf.c.

References mbuf::buf, mbuf_init(), and mem_deref().

Referenced by md5_printf().

◆ mbuf_resize()

int mbuf_resize ( struct mbuf mb,
size_t  size 
)

Resize a memory buffer.

Parameters
mbMemory buffer to resize
sizeNew buffer size
Returns
0 if success, otherwise errorcode

Definition at line 118 of file mbuf.c.

References mbuf::buf, mem_alloc(), mem_realloc(), NULL, and mbuf::size.

Referenced by mbuf_shift(), and mbuf_trim().

◆ mbuf_shift()

int mbuf_shift ( struct mbuf mb,
ssize_t  shift 
)

Shift mbuf content position.

Parameters
mbMemory buffer to shift
shiftShift offset count
Returns
0 if success, otherwise errorcode

Definition at line 164 of file mbuf.c.

References mbuf::end, mbuf_resize(), mbuf::pos, and mbuf::size.

◆ mbuf_strdup()

int mbuf_strdup ( struct mbuf mb,
char **  strp,
size_t  len 
)

Duplicate a null-terminated string from a memory buffer.

Parameters
mbMemory buffer
strpPointer to destination string; allocated and set
lenLength of string
Returns
0 if success, otherwise errorcode

Definition at line 447 of file mbuf.c.

References mbuf_read_mem(), mem_alloc(), mem_deref(), and NULL.

◆ mbuf_trim()

void mbuf_trim ( struct mbuf mb)

Trim unused trailing bytes in a memory buffer, resize if necessary.

Parameters
mbMemory buffer to trim

Definition at line 141 of file mbuf.c.

References DEBUG_WARNING, mbuf::end, mbuf_resize(), and mbuf::size.

◆ mbuf_vprintf()

int mbuf_vprintf ( struct mbuf mb,
const char *  fmt,
va_list  ap 
)

Print a formatted variable argument list to a memory buffer.

Parameters
mbMemory buffer
fmtFormatted string
apVariable argument list
Returns
0 if success, otherwise errorcode

Definition at line 492 of file mbuf.c.

References re_vhprintf().

Referenced by http_creply(), and md5_printf().

◆ mbuf_write_mem()

int mbuf_write_mem ( struct mbuf mb,
const uint8_t *  buf,
size_t  size 
)

Write a block of memory to a memory buffer.

Parameters
mbMemory buffer
bufMemory block to write
sizeNumber of bytes to write
Returns
0 if success, otherwise errorcode

Definition at line 207 of file mbuf.c.

References mbuf::pos, and mbuf::size.

Referenced by conf_alloc_buf(), dns_cstr_encode(), mbuf_write_pl(), mbuf_write_pl_skip(), mbuf_write_str(), mbuf_write_u16(), mbuf_write_u32(), mbuf_write_u64(), mbuf_write_u8(), rtcp_sdes_encode(), and rtp_hdr_encode().

◆ mbuf_write_pl()

int mbuf_write_pl ( struct mbuf mb,
const struct pl pl 
)

Write a pointer-length string to a memory buffer.

Parameters
mbMemory buffer
plPointer-length string
Returns
0 if success, otherwise errorcode

Definition at line 316 of file mbuf.c.

References pl::l, mbuf_write_mem(), and pl::p.

◆ mbuf_write_pl_skip()

int mbuf_write_pl_skip ( struct mbuf mb,
const struct pl pl,
const struct pl skip 
)

Write a pointer-length string to a memory buffer, excluding a section.

Parameters
mbMemory buffer
plPointer-length string
skipPart of pl to exclude
Returns
0 if success, otherwise errorcode
Todo:
: create substf variante

Definition at line 530 of file mbuf.c.

References pl::l, mbuf_write_mem(), and pl::p.

◆ mbuf_write_str()

int mbuf_write_str ( struct mbuf mb,
const char *  str 
)

Write a null-terminated string to a memory buffer.

Parameters
mbMemory buffer
strNull terminated string to write
Returns
0 if success, otherwise errorcode

Definition at line 299 of file mbuf.c.

References mbuf_write_mem().

◆ mbuf_write_u16()

int mbuf_write_u16 ( struct mbuf mb,
uint16_t  v 
)

Write a 16-bit value to a memory buffer.

Parameters
mbMemory buffer
v16-bit value to write
Returns
0 if success, otherwise errorcode

Definition at line 257 of file mbuf.c.

References mbuf_write_mem().

Referenced by dns_rr_encode(), rtcp_hdr_encode(), rtcp_rtpfb_gnack_encode(), rtmp_control(), and rtp_hdr_encode().

◆ mbuf_write_u32()

int mbuf_write_u32 ( struct mbuf mb,
uint32_t  v 
)

Write a 32-bit value to a memory buffer.

Parameters
mbMemory buffer
v32-bit value to write
Returns
0 if success, otherwise errorcode

Definition at line 271 of file mbuf.c.

References mbuf_write_mem().

Referenced by dns_rr_encode(), rtcp_psfb_sli_encode(), rtcp_sdes_encode(), rtmp_control(), and rtp_hdr_encode().

◆ mbuf_write_u64()

int mbuf_write_u64 ( struct mbuf mb,
uint64_t  v 
)

Write a 64-bit value to a memory buffer.

Parameters
mbMemory buffer
v64-bit value to write
Returns
0 if success, otherwise errorcode

Definition at line 285 of file mbuf.c.

References mbuf_write_mem().

◆ mbuf_write_u8()

int mbuf_write_u8 ( struct mbuf mb,
uint8_t  v 
)

Write an 8-bit value to a memory buffer.

Parameters
mbMemory buffer
v8-bit value to write
Returns
0 if success, otherwise errorcode

Definition at line 243 of file mbuf.c.

References mbuf_write_mem().

Referenced by dns_cstr_encode(), rtcp_hdr_encode(), rtcp_sdes_encode(), rtmp_control(), and tls_set_ciphers().