Buffer

This utility class provides method to store and retrieve binary data.

Function Reference

Command to construct an empty buffer

`Buffer()`

Usage in C/C++

vsi::runtime::Buffer();

Usage in Python

vsi_runtime.Buffer()

Command to construct a buffer of specific size

`Buffer(size_t len)`

Parameters

  • len: Size of the buffer to construct.

Usage in C/C++

vsi::runtime::Buffer(256);

Usage in Python

vsi_runtime.Buffer(256)

Command to construct a buffer using a null terminated string

The length and content are same as string’s. Buffer(const char *string)

Parameters

  • string: The input string.

Usage in C/C++

vsi::runtime::Buffer("abcd");

Usage in Python

vsi_runtime.Buffer('abcd')

Command to construct a buffer using a data pointer and given length

`Buffer(void *_data, size_t len, bool direct = false)`

Parameters

  • _data: Pointer of the data to copy from.
  • len: Size of the buffer to construct.
  • direct: Set to true to avoid allocating a new buffer such as when using it with language binding.

Usage in C/C++

void copy_buff(void *data, size_t d_size) {
	vsi::runtime::Buffer buf(data, d_size);
	// Do something with the buffer
}

Usage in Python

N/A

Command to copy a single byte into the buffer at the given offset

`put(const unsigned char _byte, int _offset)`

Parameters

  • _byte: Byte to copy into the buffer.
  • _offset: This will be used as the target offset into the buffer for the copy operation.

Usage in C/C++

Usage in Python

Command to copy another Buffer’s contents into this buffer at the given offset

`put(Buffer *src, int _offset = 0)`

Parameters

  • src: Pointer to the source buffer.
  • _offset: This will be used as the target offset into the buffer for the copy operation.

Usage in C/C++

Usage in Python

Command to return an integer value and forwards the current offset by it’s size

`getInt()`

Parameters

return: The integer value stored at current offset.

Usage in C/C++

Usage in Python

Command to compare the content of this buffer with another Buffer

`compare(Buffer &buf)`

Parameters

  • buf: Pointer to another Buffer to compare against.
  • return: true if the contents are identical. False otherwise.

Usage in C/C++

Usage in Python

Command to store an integer at the current offset and forwards the offset by integer’s size

`putInt(int i)`

Parameters

  • i: Integer value to store in the buffer.

Usage in C/C++

Usage in Python

Command to fill the buffer with the given pattern

The pattern is repeated to fill the whole buffer. If the pattern is bigger than the buffer then only the portion that it is clipped. fill(const char *pattern)

Parameters

  • pattern: A pattern to fill in the buffer. A null terminated string is required.

Usage in C/C++

Usage in Python

Command to return a short value at current offset and forward the offset by short size

`getShort()`

Parameters

  • return: The short value that is at current offset.

Usage in C/C++

Usage in Python

Command to write the short value at current offset and forward the offset by short size

`putShort(short i)`

Parameters

  • i: Short value to write.

Usage in C/C++

Usage in Python

Command to read a char value at current offset and forward the offset by char size

`getChar()`

Parameters

  • return: The char value that is at current offset.

Usage in C/C++

Usage in Python

Command to write the char value at current offset and forward the offset by char size

`putChar(char i)`

Parameters

  • i: Char value to write.

Usage in C/C++

Usage in Python

Command to return the size of the buffer minus current offset

Note that internal size might be larger than the value return if offset is > 0;

  • return: Readable size of the buffer (size - offset). size()

Usage in C/C++

Usage in Python

Command to return the underlying raw pointer of stored data at current offset

`get()`

Parameters

  • return: Pointer to the data.

Usage in C/C++

Usage in Python

Command to rewind the buffer, setting the offset at zero

`rewind()`

Usage in C/C++

Usage in Python

Command to return the underlying raw pointer of stored data at current offset

`data()`

Usage in C/C++

Usage in Python

Command to return the total size of the underlying buffer

Note that this is the overall memory size that is used by the buffer, not affected by the current offset.

  • return: total size of the underlying buffer. base_size()

Usage in C/C++

Usage in Python

Command to return the raw pointer of the underlying buffer

`base_data()`

The pointer is to the base and is not affected by current offset.

  • return: raw pointer to the underlying buffer.

Usage in C/C++

Usage in Python

Command to return a part of buffer clipped to the specified size

`substr(size_t size)`

Parameters

  • size: Only return these many bytes.
  • return: std::string containing the clipped buffer. If buffer size was larger than passed size, it is appended by ... to indicate clipping status.

Usage in C/C++

Usage in Python

Command to return a string formatted as hexadecimal and separated by the provided separator character.

`bytes(size_t size, const char *separator)`

Parameters

  • size: limit the return string to this size.
  • separator: user this character as the separator.
  • return: std::string formated as hexadecimal string.

Usage in C/C++

Usage in Python

Command to return the current offset of the buffer.

`offset()`
  • return: integer specifying the current offset.

Usage in C/C++

Usage in Python

Command to set the buffer contents. The underlying buffer will resize to accommodate the new size.

`set(const void *_data, size_t len)`

Parameters

  • _data: Pointer to raw content to copy.
  • len: Size of the content to copy. Also used as the new size for this buffer.

Usage in C/C++

Usage in Python

Command to set the buffer contents to a null terminated string. The underlying buffer will resize to accommodate the new size.

`set(const char *string)`

Parameters

  • string: the null terminated string to use as the content and to calculate the new size of the buffer.

Usage in C/C++

Usage in Python

Command to change the buffer to use the provided raw buffer as the underlying buffer

`direct_set(void *_data, size_t _size)`

The provided raw buffer should be allocated using malloc or new and will be freed by this Buffer when it’s destructor is called.

Parameters

  • _data: Pointer to the raw buffer.
  • _size: Size of the raw buffer;

Usage in C/C++

Usage in Python

Command to append data to the current buffer

`add(const void *_data, size_t len)`

The underlying buffer will be resized to accommodate the new size.

Parameters

  • _data: Pointer to the data.
  • _size: Size of the raw buffer;

Usage in C/C++

Usage in Python

Command to append another Buffer’s contents to the current buffer

`add(Buffer *buf)`

The underlying buffer will be resized to accommodate the new size.

Parameters

  • buf: Pointer to the Buffer to copy the data from.

Usage in C/C++

Usage in Python

Command to set the current offset to provided value

`offset(size_t _offset)`

Parameters

  • _offset: New offset value. It should not be larger than the underlying buffer’s size.

Usage in C/C++

Usage in Python

Command to specify the target offset

`dev_offset(size_t _offset)`

An auxiliary piece of integer that is used to specify the target offset to write the data once it reaches it’s destination.

Parameters

  • _offset: Offset to write the data to. The offset is only enforced for memory devices.

Usage in C/C++

Usage in Python

Command to return the memory offset where the buffer should be written to.

`dev_offset()`

Parameters

  • return: Returns the auxiliary offset.

Usage in C/C++

Usage in Python

Command to truncate the buffer by provided size

`reduce(size_t size)`

The truncated bytes are permanently lost.

Parameters

  • size: Truncate the underlying buffer by this size. The overall size of the buffer must be larger or equal to the provided size.

Usage in C/C++

Usage in Python

Command to allocate the given size for the underlying buffer

`alloc(size_t len)`

If there was an existing buffer than it is freed.

Parameters

  • len: The new size of the underlying buffer.

Usage in C/C++

Usage in Python