Haka: include/haka/vbuffer.h File Reference

Haka

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
vbuffer.h File Reference
`#include <haka/types.h>`
`#include <haka/container/list2.h>`
`#include <haka/lua/object.h>`

Go to the source code of this file.

## Data Structures
struct | vbuffer_data_ops
struct | vbuffer_data
struct | vbuffer
struct | vbuffer_iterator
struct | vbuffer_sub
struct | vbuffer_sub_mmap

## Macros
#define | ALL (size_t)-1

## Typedefs
typedef uint32 | vbsize_t

## Enumerations

## Functions
bool | vbuffer_isvalid (const struct vbuffer *buf)
bool | vbuffer_isempty (const struct vbuffer *buf)
bool | vbuffer_create_empty (struct vbuffer *buf)
bool | vbuffer_create_new (struct vbuffer *buf, size_t size, bool zero)
bool | vbuffer_create_from (struct vbuffer *buf, const char *str, size_t len)
void | vbuffer_clear (struct vbuffer *buf)
void | vbuffer_release (struct vbuffer *buf)
void | vbuffer_position (const struct vbuffer *buf, struct vbuffer_iterator *position, size_t offset)
INLINE void | vbuffer_begin (const struct vbuffer *buf, struct vbuffer_iterator *position)
INLINE void | vbuffer_end (const struct vbuffer *buf, struct vbuffer_iterator *position)
void | vbuffer_last (const struct vbuffer *buf, struct vbuffer_iterator *position)
void | vbuffer_setwritable (struct vbuffer *buf, bool writable)
bool | vbuffer_iswritable (struct vbuffer *buf)
bool | vbuffer_ismodified (struct vbuffer *buf)
void | vbuffer_clearmodified (struct vbuffer *buf)
bool | vbuffer_append (struct vbuffer *buf, struct vbuffer *buffer)
size_t | vbuffer_size (struct vbuffer *buf)
INLINE bool | vbuffer_check_size (struct vbuffer *buf, size_t minsize, size_t *size)
INLINE bool | vbuffer_isflat (struct vbuffer *buf)
INLINE const uint8 * | vbuffer_flatten (struct vbuffer *buf, size_t *size)
INLINE bool | vbuffer_clone (struct vbuffer *data, struct vbuffer *buffer, bool copy)
void | vbuffer_swap (struct vbuffer *a, struct vbuffer *b)
bool | vbuffer_iterator_isvalid (const struct vbuffer_iterator *position)
void | vbuffer_iterator_copy (const struct vbuffer_iterator *src, struct vbuffer_iterator *dst)
void | vbuffer_iterator_move (struct vbuffer_iterator *iter, const struct vbuffer_iterator *pos)
void | vbuffer_iterator_clear (struct vbuffer_iterator *position)
size_t | vbuffer_iterator_available (struct vbuffer_iterator *position)
bool | vbuffer_iterator_check_available (struct vbuffer_iterator *position, size_t minsize, size_t *size)
bool | vbuffer_iterator_register (struct vbuffer_iterator *position)
bool | vbuffer_iterator_unregister (struct vbuffer_iterator *position)
bool | vbuffer_iterator_insert (struct vbuffer_iterator *position, struct vbuffer *buffer, struct vbuffer_sub *sub)
size_t | vbuffer_iterator_advance (struct vbuffer_iterator *position, size_t len)
void | vbuffer_iterator_skip_empty (struct vbuffer_iterator *position)
bool | vbuffer_iterator_isend (struct vbuffer_iterator *position)
bool | vbuffer_iterator_iseof (struct vbuffer_iterator *position)
bool | vbuffer_iterator_split (struct vbuffer_iterator *position)
size_t | vbuffer_iterator_sub (struct vbuffer_iterator *position, size_t len, struct vbuffer_sub *sub, bool split)
uint8 | vbuffer_iterator_getbyte (struct vbuffer_iterator *position)
bool | vbuffer_iterator_setbyte (struct vbuffer_iterator *position, uint8 byte)
uint8 * | vbuffer_iterator_mmap (struct vbuffer_iterator *position, size_t maxsize, size_t *size, bool write)
bool | vbuffer_iterator_mark (struct vbuffer_iterator *position, bool readonly)
bool | vbuffer_iterator_unmark (struct vbuffer_iterator *position)
bool | vbuffer_iterator_isinsertable (struct vbuffer_iterator *position, struct vbuffer *buffer)
void | vbuffer_sub_clear (struct vbuffer_sub *data)
bool | vbuffer_sub_register (struct vbuffer_sub *data)
bool | vbuffer_sub_unregister (struct vbuffer_sub *data)
void | vbuffer_sub_create (struct vbuffer_sub *data, struct vbuffer *buffer, size_t offset, size_t length)
bool | vbuffer_sub_create_from_position (struct vbuffer_sub *data, struct vbuffer_iterator *position, size_t length)
bool | vbuffer_sub_create_between_position (struct vbuffer_sub *data, struct vbuffer_iterator *begin, struct vbuffer_iterator *end)
void | vbuffer_sub_begin (struct vbuffer_sub *data, struct vbuffer_iterator *begin)
void | vbuffer_sub_end (struct vbuffer_sub *data, struct vbuffer_iterator *end)
bool | vbuffer_sub_position (struct vbuffer_sub *data, struct vbuffer_iterator *iter, size_t offset)
bool | vbuffer_sub_sub (struct vbuffer_sub *data, size_t offset, size_t length, struct vbuffer_sub *buffer)
size_t | vbuffer_sub_size (struct vbuffer_sub *data)
bool | vbuffer_sub_check_size (struct vbuffer_sub *data, size_t minsize, size_t *size)
size_t | vbuffer_sub_read (struct vbuffer_sub *data, uint8 *ptr, size_t size)
size_t | vbuffer_sub_write (struct vbuffer_sub *data, const uint8 *ptr, size_t size)
const uint8 * | vbuffer_sub_flatten (struct vbuffer_sub *data, size_t *size)
bool | vbuffer_sub_compact (struct vbuffer_sub *data)
bool | vbuffer_sub_isflat (struct vbuffer_sub *data)
bool | vbuffer_sub_clone (struct vbuffer_sub *data, struct vbuffer *buffer, clone_mode mode)
uint8 * | vbuffer_mmap (struct vbuffer_sub *data, size_t *len, bool write, struct vbuffer_sub_mmap *mmap_iter, struct vbuffer_iterator *iter)
bool | vbuffer_zero (struct vbuffer_sub *data)
bool | vbuffer_extract (struct vbuffer_sub *data, struct vbuffer *buffer)
bool | vbuffer_select (struct vbuffer_sub *data, struct vbuffer *buffer, struct vbuffer_iterator *ref)
bool | vbuffer_restore (struct vbuffer_iterator *position, struct vbuffer *data, bool clone)
bool | vbuffer_erase (struct vbuffer_sub *data)
bool | vbuffer_replace (struct vbuffer_sub *data, struct vbuffer *buffer)
int64 | vbuffer_asnumber (struct vbuffer_sub *data, bool bigendian)
bool | vbuffer_setnumber (struct vbuffer_sub *data, bool bigendian, int64 num)
int64 | vbuffer_asbits (struct vbuffer_sub *data, size_t offset, size_t bits, bool bigendian)
bool | vbuffer_setbits (struct vbuffer_sub *data, size_t offset, size_t bits, bool bigendian, int64 num)
size_t | vbuffer_asstring (struct vbuffer_sub *data, char *str, size_t len)
size_t | vbuffer_setfixedstring (struct vbuffer_sub *data, const char *str, size_t len)
bool | vbuffer_setstring (struct vbuffer_sub *data, const char *str, size_t len)
uint8 | vbuffer_getbyte (struct vbuffer_sub *data, size_t offset)
bool | vbuffer_setbyte (struct vbuffer_sub *data, size_t offset, uint8 byte)

## Variables
const struct vbuffer | vbuffer_init
const struct vbuffer_iterator | vbuffer_iterator_init
const struct vbuffer_sub | vbuffer_sub_init
const struct vbuffer_sub_mmap | vbuffer_mmap_init

## Detailed Description

Virtual buffer abstraction.

## Macro Definition Documentation

#define ALL (size_t)-1

Define for vbuffer size.

## Typedef Documentation

typedef uint32 vbsize_t

vbuffer size type.

## Enumeration Type Documentation

enum clone_mode

Clone mode.

Enumerator
CLONE_COPY | Copy the buffer.
CLONE_RW | Clone the buffer keeping the writable flag.
CLONE_RO_ORIG | Clone the buffer and mark the original buffer as read-only.
CLONE_RO_CLONE | Clone the buffer in read-only mode.

## Function Documentation

bool vbuffer_append | ( | struct vbuffer * | buf,
|  | struct vbuffer * | buffer
| ) |  |

Insert some data at the end of the buffer.

int64 vbuffer_asbits | ( | struct vbuffer_sub * | data,
|  | size_t | offset,
|  | size_t | bits,
|  | bool | bigendian
| ) |  |

Convert bits of the content of the buffer to a number.

Note
  The function will raise an error if the buffer size is not supported.

int64 vbuffer_asnumber | ( | struct vbuffer_sub * | data,
|  | bool | bigendian
| ) |  |

Convert the content of the buffer to a number.

Note
  The function will raise an error if the buffer size is not supported.

size_t vbuffer_asstring | ( | struct vbuffer_sub * | data,
|  | char * | str,
|  | size_t | len
| ) |  |

Convert the content of the buffer to a string.

INLINE void vbuffer_begin | ( | const struct vbuffer * | buf,
|  | struct vbuffer_iterator * | position
| ) |  |

Get an iterator at the beginning.

INLINE bool vbuffer_check_size | ( | struct vbuffer * | buf,
|  | size_t | minsize,
|  | size_t * | size
| ) |  |

Check if the buffer is larger than a given size.

void vbuffer_clear | ( | struct vbuffer * | buf | ) |

Clean all data in the vbuffer.

void vbuffer_clearmodified | ( | struct vbuffer * | buf | ) |

Clear the modified flag on the whole buffer.

INLINE bool vbuffer_clone | ( | struct vbuffer * | data,
|  | struct vbuffer * | buffer,
|  | bool | copy
| ) |  |

Create a clone of the buffer.

bool vbuffer_create_empty | ( | struct vbuffer * | buf | ) |

Create a new empty vbuffer.

bool vbuffer_create_from | ( | struct vbuffer * | buf,
|  | const char * | str,
|  | size_t | len
| ) |  |

Create a new vbuffer from a memory block. The memory will be copied.

bool vbuffer_create_new | ( | struct vbuffer * | buf,
|  | size_t | size,
|  | bool | zero
| ) |  |

Create a new vbuffer and allocate a block of memory for it.

INLINE void vbuffer_end | ( | const struct vbuffer * | buf,
|  | struct vbuffer_iterator * | position
| ) |  |

Get an iterator at the end.

bool vbuffer_erase | ( | struct vbuffer_sub * | data | ) |

Erase the data in the sub buffer.

bool vbuffer_extract | ( | struct vbuffer_sub * | data,
|  | struct vbuffer * | buffer
| ) |  |

Extract part of a buffer. The data are removed from the original buffer and moved to a new vbuffer.

INLINE const uint8 * vbuffer_flatten | ( | struct vbuffer * | buf,
|  | size_t * | size
| ) |  |

Flatten a buffer if needed.

uint8 vbuffer_getbyte | ( | struct vbuffer_sub * | data,
|  | size_t | offset
| ) |  |

Get the value of a byte at the given position in the buffer.

bool vbuffer_isempty | ( | const struct vbuffer * | buf | ) |

Check if a vbuffer is empty.

INLINE bool vbuffer_isflat | ( | struct vbuffer * | buf | ) |

Check if the buffer is only made of contiguous memory block.

bool vbuffer_ismodified | ( | struct vbuffer * | buf | ) |

Check if some memory blocks have been modified.

bool vbuffer_isvalid | ( | const struct vbuffer * | buf | ) |

Check if a vbuffer is valid.

bool vbuffer_iswritable | ( | struct vbuffer * | buf | ) |

Check if the buffer is writable.

Note
  This function does not check that every memory block are writable.

size_t vbuffer_iterator_advance | ( | struct vbuffer_iterator * | position,
|  | size_t | len
| ) |  |

Advance the iterator on the data.

Returns
  The number of bytes advanced. This value might be lower than the given length if the buffer does not have enough data.

size_t vbuffer_iterator_available | ( | struct vbuffer_iterator * | position | ) |

Get the available byte from this iterator.

Note
  This function needs to process all memory block.

bool vbuffer_iterator_check_available | ( | struct vbuffer_iterator * | position,
|  | size_t | minsize,
|  | size_t * | size
| ) |  |

Check if the available bytes are larger than a minimal size.

void vbuffer_iterator_clear | ( | struct vbuffer_iterator * | position | ) |

Clear an iterator.

void vbuffer_iterator_copy | ( | const struct vbuffer_iterator * | src,
|  | struct vbuffer_iterator * | dst
| ) |  |

Create a copy of an iterator.

uint8 vbuffer_iterator_getbyte | ( | struct vbuffer_iterator * | position | ) |

Get the current byte.

Note
  The iterator will not move.

bool vbuffer_iterator_insert | ( | struct vbuffer_iterator * | position,
|  | struct vbuffer * | buffer,
|  | struct vbuffer_sub * | sub
| ) |  |

Insert some data at the iterator position.

bool vbuffer_iterator_isend | ( | struct vbuffer_iterator * | position | ) |

Check if the iterator is at the end of the buffer.

bool vbuffer_iterator_iseof | ( | struct vbuffer_iterator * | position | ) |

Check if the iterator is at the end of the buffer and that this end is marked as `eof`. This is used by vbuffer_stream.

bool vbuffer_iterator_isinsertable | ( | struct vbuffer_iterator * | position,
|  | struct vbuffer * | buffer
| ) |  |

Check if a buffer could be inserted. This verify that the insert will not create loops.

bool vbuffer_iterator_isvalid | ( | const struct vbuffer_iterator * | position | ) |

Check if an iterator is valid.

bool vbuffer_iterator_mark | ( | struct vbuffer_iterator * | position,
|  | bool | readonly
| ) |  |

Insert a mark at the iterator position.

The resulting buffer will look like this:

uint8* vbuffer_iterator_mmap | ( | struct vbuffer_iterator * | position,
|  | size_t | maxsize,
|  | size_t * | size,
|  | bool | write
| ) |  |

Mmap each block one by one.

void vbuffer_iterator_move | ( | struct vbuffer_iterator * | iter,
|  | const struct vbuffer_iterator * | pos
| ) |  |

Move an iterator to the position of another one.

bool vbuffer_iterator_register | ( | struct vbuffer_iterator * | position | ) |

Register the iterator to make it safe. When this is done, if the buffer behind the iterator is released, the iterator will raise an error when trying to do operations with it. If it is not registered, the program will probably segfault.

bool vbuffer_iterator_setbyte | ( | struct vbuffer_iterator * | position,
|  | uint8 | byte
| ) |  |

Set the current byte.

Note
  The iterator will not move.

void vbuffer_iterator_skip_empty | ( | struct vbuffer_iterator * | position | ) |

Skip empty vbuffer chunk at the iterator position.

bool vbuffer_iterator_split | ( | struct vbuffer_iterator * | position | ) |

Split the current memory block at the iterator position.

Starting from an iterator on the buffer:

The chunk pointed by the iterator will be split into two parts like this:

size_t vbuffer_iterator_sub | ( | struct vbuffer_iterator * | position,
|  | size_t | len,
|  | struct vbuffer_sub * | sub,
|  | bool | split
| ) |  |

Create a sub buffer from the iterator position. The iterator will advance accordingly.

bool vbuffer_iterator_unmark | ( | struct vbuffer_iterator * | position | ) |

Unmark the buffer.

Note
  The iterator must be placed on a mark created with vbuffer_iterator_mark()

bool vbuffer_iterator_unregister | ( | struct vbuffer_iterator * | position | ) |

Unregister an iterator.

void vbuffer_last | ( | const struct vbuffer * | buf,
|  | struct vbuffer_iterator * | position
| ) |  |

Get an iterator at tha last position of the buffer. This iterator will be different if the vbuffer is not empty. The end is always at the end even if some data are appended, but the last position is attached to the last byte in the buffer. So if some data are added, those data will be after this last position.

uint8* vbuffer_mmap | ( | struct vbuffer_sub * | data,
|  | size_t * | len,
|  | bool | write,
|  | struct vbuffer_sub_mmap * | mmap_iter,
|  | struct vbuffer_iterator * | iter
| ) |  |

Mmap every memory block of the buffer.

void vbuffer_position | ( | const struct vbuffer * | buf,
|  | struct vbuffer_iterator * | position,
|  | size_t | offset
| ) |  |

Get an iterator at the given offset.

void vbuffer_release | ( | struct vbuffer * | buf | ) |

Release the vbuffer internal memory.

bool vbuffer_replace | ( | struct vbuffer_sub * | data,
|  | struct vbuffer * | buffer
| ) |  |

Replace the data in the sub buffer.

bool vbuffer_restore | ( | struct vbuffer_iterator * | position,
|  | struct vbuffer * | data,
|  | bool | clone
| ) |  |

Restore data extracted using vbuffer_select().

bool vbuffer_select | ( | struct vbuffer_sub * | data,
|  | struct vbuffer * | buffer,
|  | struct vbuffer_iterator * | ref
| ) |  |

Extract part of a buffer. The data are removed from the original buffer and moved to a new vbuffer. Unlike vbuffer_extract(), this operator does not mark the buffer as modified. It is needed to restore the data using the function vbuffer_restore().

bool vbuffer_setbits | ( | struct vbuffer_sub * | data,
|  | size_t | offset,
|  | size_t | bits,
|  | bool | bigendian,
|  | int64 | num
| ) |  |

Set bits of the content of the buffer from a number.

Note
  The function will raise an error if the buffer size is not supported.

bool vbuffer_setbyte | ( | struct vbuffer_sub * | data,
|  | size_t | offset,
|  | uint8 | byte
| ) |  |

Set the value of a byte at the given position in the buffer.

size_t vbuffer_setfixedstring | ( | struct vbuffer_sub * | data,
|  | const char * | str,
|  | size_t | len
| ) |  |

Set the content of the buffer from a string. This function will change the data inplace without modifying the buffer size.

bool vbuffer_setnumber | ( | struct vbuffer_sub * | data,
|  | bool | bigendian,
|  | int64 | num
| ) |  |

Set the content of the buffer from a number.

Note
  The function will raise an error if the buffer size is not supported.

bool vbuffer_setstring | ( | struct vbuffer_sub * | data,
|  | const char * | str,
|  | size_t | len
| ) |  |

Set the content of the buffer from a string. This function will change the size of the buffer to match the one given by the arguments.

void vbuffer_setwritable | ( | struct vbuffer * | buf,
|  | bool | writable
| ) |  |

Change the writable flag on the whole buffer.

size_t vbuffer_size | ( | struct vbuffer * | buf | ) |

Compute the size of the buffer.

Note
  This function needs to process all memory block.

void vbuffer_sub_begin | ( | struct vbuffer_sub * | data,
|  | struct vbuffer_iterator * | begin
| ) |  |

Get an iterator at the beginning of the sub buffer.

bool vbuffer_sub_check_size | ( | struct vbuffer_sub * | data,
|  | size_t | minsize,
|  | size_t * | size
| ) |  |

Check if the sub buffer is larger than a given minimal size.

void vbuffer_sub_clear | ( | struct vbuffer_sub * | data | ) |

Clear a sub buffer.

bool vbuffer_sub_clone | ( | struct vbuffer_sub * | data,
|  | struct vbuffer * | buffer,
|  | clone_mode | mode
| ) |  |

Clone the content of the sub buffer.

bool vbuffer_sub_compact | ( | struct vbuffer_sub * | data | ) |

Compact the sub buffer by aggregating memory blocks when possible.

void vbuffer_sub_create | ( | struct vbuffer_sub * | data,
|  | struct vbuffer * | buffer,
|  | size_t | offset,
|  | size_t | length
| ) |  |

Create a new sub buffer.

bool vbuffer_sub_create_between_position | ( | struct vbuffer_sub * | data,
|  | struct vbuffer_iterator * | begin,
|  | struct vbuffer_iterator * | end
| ) |  |

Create a new sub buffer between two iterator positions.

bool vbuffer_sub_create_from_position | ( | struct vbuffer_sub * | data,
|  | struct vbuffer_iterator * | position,
|  | size_t | length
| ) |  |

Create a new sub buffer from an iterator position.

void vbuffer_sub_end | ( | struct vbuffer_sub * | data,
|  | struct vbuffer_iterator * | end
| ) |  |

Get an iterator at the end of the sub buffer.

const uint8* vbuffer_sub_flatten | ( | struct vbuffer_sub * | data,
|  | size_t * | size
| ) |  |

Flatten the sub buffer if needed.

bool vbuffer_sub_isflat | ( | struct vbuffer_sub * | data | ) |

Check if the sub buffer is only made of one memory block.

bool vbuffer_sub_position | ( | struct vbuffer_sub * | data,
|  | struct vbuffer_iterator * | iter,
|  | size_t | offset
| ) |  |

Get an iterator at the given offset.

size_t vbuffer_sub_read | ( | struct vbuffer_sub * | data,
|  | uint8 * | ptr,
|  | size_t | size
| ) |  |

Copy the data in the sub buffer.

bool vbuffer_sub_register | ( | struct vbuffer_sub * | data | ) |

Register a sub buffer.

See also
  vbuffer_iterator_register()

size_t vbuffer_sub_size | ( | struct vbuffer_sub * | data | ) |

Compute the size of the sub buffer.

Note
  This function needs to process all memory block.

bool vbuffer_sub_sub | ( | struct vbuffer_sub * | data,
|  | size_t | offset,
|  | size_t | length,
|  | struct vbuffer_sub * | buffer
| ) |  |

Create a new sub buffer.

bool vbuffer_sub_unregister | ( | struct vbuffer_sub * | data | ) |

Unregister a sub buffer.

size_t vbuffer_sub_write | ( | struct vbuffer_sub * | data,
|  | const uint8 * | ptr,
|  | size_t | size
| ) |  |

Copy and replace the data in the sub buffer.

void vbuffer_swap | ( | struct vbuffer * | a,
|  | struct vbuffer * | b
| ) |  |

Swap the content of two buffers.

bool vbuffer_zero | ( | struct vbuffer_sub * | data | ) |

Set each byte of the buffer to zero.

## Variable Documentation

const struct vbuffer vbuffer_init

Initializer for vbuffer.

const struct vbuffer_iterator vbuffer_iterator_init

Initializer for vbuffer_iterator.

const struct vbuffer_sub_mmap vbuffer_mmap_init

Initializer for vbuffer_sub_mmap.

const struct vbuffer_sub vbuffer_sub_init

Initializer for vbuffer_sub.