cbitcoin
 All Data Structures Files Functions Variables Typedefs
src/structures/CBObject/CBByteArray/CBByteArray.h File Reference

Provides an interface to a memory block as bytes. Many CBByteArrays can refer to the same memory block with different offsets and lengths. Inherits CBObject. More...

#include "CBObject.h"
#include <stdbool.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  CBSharedData
 Stores byte data that can be shared amongst many CBByteArrays. More...
struct  CBByteArray
 Structure for CBByteArray objects. More...

Functions

CBByteArrayCBNewByteArrayFromString (char *string, bool terminator, void(*onErrorReceived)(CBError error, char *,...))
 Creates a CBByteArray object from a C string. The termination character is not included in the new CBByteArray.
CBByteArrayCBNewByteArrayOfSize (uint32_t size, void(*onErrorReceived)(CBError error, char *,...))
 Creates an empty CBByteArray object.
CBByteArrayCBNewByteArraySubReference (CBByteArray *ref, uint32_t offset, uint32_t length)
 Creates a CBByteArray object referencing another CBByteArrayObject.
CBByteArrayCBNewByteArrayWithData (uint8_t *data, uint32_t size, void(*onErrorReceived)(CBError error, char *,...))
 Creates a new CBByteArray using data.
CBByteArrayCBNewByteArrayWithDataCopy (uint8_t *data, uint32_t size, void(*onErrorReceived)(CBError error, char *,...))
 Creates a new CBByteArray using data which is copied.
CBByteArrayCBGetByteArray (void *self)
 Gets a CBByteArray from another object. Use this to avoid casts.
bool CBInitByteArrayFromString (CBByteArray *self, char *string, bool terminator, void(*onErrorReceived)(CBError error, char *,...))
 Initialises a CBByteArray object from a C string. The termination character is not included in the new CBByteArray.
bool CBInitByteArrayOfSize (CBByteArray *self, uint32_t size, void(*onErrorReceived)(CBError error, char *,...))
 Initialises an empty CBByteArray object.
bool CBInitByteArraySubReference (CBByteArray *self, CBByteArray *ref, uint32_t offset, uint32_t length)
 Initialises a reference CBByteArray to a subsection of an CBByteArray.
bool CBInitByteArrayWithData (CBByteArray *self, uint8_t *data, uint32_t size, void(*onErrorReceived)(CBError error, char *,...))
 Creates a new CBByteArray using data.
bool CBInitByteArrayWithDataCopy (CBByteArray *self, uint8_t *data, uint32_t size, void(*onErrorReceived)(CBError error, char *,...))
 Creates a new CBByteArray using data which is copied.
void CBFreeByteArray (void *self)
 Frees a CBByteArray object.
void CBByteArrayReleaseSharedData (CBByteArray *self)
 Releases a reference to shared byte data and frees the data if necessary.
CBCompare CBByteArrayCompare (CBByteArray *self, CBByteArray *second)
 Compares a CBByteArray to another CBByteArray and returns with a CBCompare value.
CBByteArrayCBByteArrayCopy (CBByteArray *self)
 Copies a CBByteArray.
void CBByteArrayCopyByteArray (CBByteArray *self, uint32_t writeOffset, CBByteArray *source)
 Copies another byte array to this byte array.
void CBByteArrayCopySubByteArray (CBByteArray *self, uint32_t writeOffset, CBByteArray *source, uint32_t readOffset, uint32_t length)
 Copies a section of another byte array to this byte array.
uint8_t CBByteArrayGetByte (CBByteArray *self, uint32_t index)
 Get a byte from the CBByteArray object. A byte will be returned from self->offset+index in the underlying data.
uint8_t * CBByteArrayGetData (CBByteArray *self)
 Get a pointer to the underlying data starting at self->offset.
uint8_t CBByteArrayGetLastByte (CBByteArray *self)
 Get the last byte from the CBByteArray object. A byte will be returned from self->offset+self->length in the underlying data.
bool CBByteArrayIsNull (CBByteArray *self)
 Determines if a CBByteArray is null.
void CBByteArraySetByte (CBByteArray *self, uint32_t index, uint8_t byte)
 Set a byte into the array. This will be set at self->offset+index in the underlying data.
void CBByteArraySetBytes (CBByteArray *self, uint32_t index, uint8_t *bytes, uint32_t length)
 Copies a length of bytes into the array. This will be set at self->offset+index in the underlying data.
void CBByteArraySetInt16 (CBByteArray *self, uint32_t offset, uint16_t integer)
 Writes a 16 bit integer to a CBByteArray as little-endian.
void CBByteArraySetInt32 (CBByteArray *self, uint32_t offset, uint32_t integer)
 Writes a 32 bit integer to a CBByteArray as little-endian.
void CBByteArraySetInt64 (CBByteArray *self, uint32_t offset, uint64_t integer)
 Writes a 64 bit integer to a CBByteArray as little-endian.
void CBByteArraySetPort (CBByteArray *self, uint32_t offset, uint16_t integer)
 Writes a 16 bit integer to a CBByteArray as big-endian for port numbers.
uint16_t CBByteArrayReadInt16 (CBByteArray *self, uint32_t offset)
 Reads a 16 bit integer from a CBByteArray as little-endian.
uint32_t CBByteArrayReadInt32 (CBByteArray *self, uint32_t offset)
 Reads a 32 bit integer from a CBByteArray as little-endian.
uint64_t CBByteArrayReadInt64 (CBByteArray *self, uint32_t offset)
 Reads a 64 bit integer from the CBByteArray as little-endian.
uint16_t CBByteArrayReadPort (CBByteArray *self, uint32_t offset)
 Reads a 16 bit integer from the CBByteArray as big-endian for port numbers.
void CBByteArrayReverseBytes (CBByteArray *self)
 Reverses the bytes.
void CBByteArrayChangeReference (CBByteArray *self, CBByteArray *ref, uint32_t offset)
 Changes the reference of this CBByteArray object to reference the underlying data of another CBByteArray. Useful for moving byte data into single underlying data by copying the data into a larger CBByteArray and then changing the reference to this new larger CBByteArray.
CBByteArrayCBByteArraySubCopy (CBByteArray *self, uint32_t offset, uint32_t length)
 Copies a subsection of a CBByteArray.
CBByteArrayCBByteArraySubReference (CBByteArray *self, uint32_t offset, uint32_t length)
 References a subsection of a CBByteArray.

Detailed Description

Provides an interface to a memory block as bytes. Many CBByteArrays can refer to the same memory block with different offsets and lengths. Inherits CBObject.


Function Documentation

void CBByteArrayChangeReference ( CBByteArray self,
CBByteArray ref,
uint32_t  offset 
)

Changes the reference of this CBByteArray object to reference the underlying data of another CBByteArray. Useful for moving byte data into single underlying data by copying the data into a larger CBByteArray and then changing the reference to this new larger CBByteArray.

Parameters:
selfThe CBByteArray object to change the reference for.
refThe CBByteArray object to get the reference from.
offsetThe offset to start the reference.
Returns:
The new CBByteArray.
CBCompare CBByteArrayCompare ( CBByteArray self,
CBByteArray second 
)

Compares a CBByteArray to another CBByteArray and returns with a CBCompare value.

Parameters:
selfThe CBByteArray object to compare
secondAnother CBByteArray object to compare with
Returns:
If the lengths are different, CB_COMPARE_MORE_THAN if "self" if longer, else CB_COMPARE_LESS_THAN. If the bytes are equal CB_COMPARE_EQUAL, else CB_COMPARE_MORE_THAN if the first different byte if higher in "self", otherwise CB_COMPARE_LESS_THAN. The return value can be treated like the return value to memcmp.

Copies a CBByteArray.

Parameters:
selfThe CBByteArray object to copy
Returns:
The new CBByteArray.
void CBByteArrayCopyByteArray ( CBByteArray self,
uint32_t  writeOffset,
CBByteArray source 
)

Copies another byte array to this byte array.

Parameters:
selfThe CBByteArray object.
writeOffsetThe offset to begin writing
sourceThe CBByteArray to copy from.
void CBByteArrayCopySubByteArray ( CBByteArray self,
uint32_t  writeOffset,
CBByteArray source,
uint32_t  readOffset,
uint32_t  length 
)

Copies a section of another byte array to this byte array.

Parameters:
selfThe CBByteArray object.
writeOffsetThe offset to begin writing
sourceThe CBByteArray to copy from.
readOffsetThe offset of the source array to begin reading.
lengthThe length to copy.
uint8_t CBByteArrayGetByte ( CBByteArray self,
uint32_t  index 
)

Get a byte from the CBByteArray object. A byte will be returned from self->offset+index in the underlying data.

Parameters:
selfThe CBByteArray object.
indexThe index in the array to get the byte from
Returns:
The byte
uint8_t* CBByteArrayGetData ( CBByteArray self)

Get a pointer to the underlying data starting at self->offset.

Parameters:
selfThe CBByteArray object.
Returns:
The pointer
uint8_t CBByteArrayGetLastByte ( CBByteArray self)

Get the last byte from the CBByteArray object. A byte will be returned from self->offset+self->length in the underlying data.

Parameters:
selfThe CBByteArray object.
Returns:
The last byte
bool CBByteArrayIsNull ( CBByteArray self)

Determines if a CBByteArray is null.

Parameters:
selfThe CBByteArray object.
Returns:
true if all bytes are zero, else false.
uint16_t CBByteArrayReadInt16 ( CBByteArray self,
uint32_t  offset 
)

Reads a 16 bit integer from a CBByteArray as little-endian.

Parameters:
selfThe CBByteArray object
offsetOffset to where to start the read
Returns:
A 16 bit integer. This can be cast to a signed integer if reading integer as a signed value.
uint32_t CBByteArrayReadInt32 ( CBByteArray self,
uint32_t  offset 
)

Reads a 32 bit integer from a CBByteArray as little-endian.

Parameters:
selfThe CBByteArray object
offsetOffset to where to start the read
Returns:
A 32 bit integer. This can be cast to a signed integer if reading integer as a signed value
uint64_t CBByteArrayReadInt64 ( CBByteArray self,
uint32_t  offset 
)

Reads a 64 bit integer from the CBByteArray as little-endian.

Parameters:
selfThe CBByteArray object
offsetOffset to where to start the read
Returns:
A 64 bit integer. This can be cast to a signed integer if reading integer as a signed value
uint16_t CBByteArrayReadPort ( CBByteArray self,
uint32_t  offset 
)

Reads a 16 bit integer from the CBByteArray as big-endian for port numbers.

Parameters:
selfThe CBByteArray object
offsetOffset to where to start the read
Returns:
A 16 bit integer.

Releases a reference to shared byte data and frees the data if necessary.

Parameters:
selfThe CBByteArray object with the CBSharedData

Reverses the bytes.

Parameters:
selfThe CBByteArray object to reverse
void CBByteArraySetByte ( CBByteArray self,
uint32_t  index,
uint8_t  byte 
)

Set a byte into the array. This will be set at self->offset+index in the underlying data.

Parameters:
selfThe CBByteArray object.
indexThe index in the array to set the byte
byteThe byte to be set.
void CBByteArraySetBytes ( CBByteArray self,
uint32_t  index,
uint8_t *  bytes,
uint32_t  length 
)

Copies a length of bytes into the array. This will be set at self->offset+index in the underlying data.

Parameters:
selfThe CBByteArray object.
indexThe index in the array to start writing.
bytesThe pointer to the bytes to be copied.
lengthThe number of bytes to copy.
void CBByteArraySetInt16 ( CBByteArray self,
uint32_t  offset,
uint16_t  integer 
)

Writes a 16 bit integer to a CBByteArray as little-endian.

Parameters:
selfThe CBByteArray object
offsetOffset to where to start the write
integerThe 16 bit integer to set. The argument is an unsigned integer but signed or unsigned integers are OK to pass.
void CBByteArraySetInt32 ( CBByteArray self,
uint32_t  offset,
uint32_t  integer 
)

Writes a 32 bit integer to a CBByteArray as little-endian.

Parameters:
selfThe CBByteArray object
offsetOffset to where to start the write
integerThe 32 bit integer to set. The argument is an unsigned integer but signed or unsigned integers are OK to pass.
void CBByteArraySetInt64 ( CBByteArray self,
uint32_t  offset,
uint64_t  integer 
)

Writes a 64 bit integer to a CBByteArray as little-endian.

Parameters:
selfThe CBByteArray object
offsetOffset to where to start the write
integerThe 64 bit integer to set. The argument is an unsigned integer but signed or unsigned integers are OK to pass.
void CBByteArraySetPort ( CBByteArray self,
uint32_t  offset,
uint16_t  integer 
)

Writes a 16 bit integer to a CBByteArray as big-endian for port numbers.

Parameters:
selfThe CBByteArray object
offsetOffset to where to start the write
integerThe 16 bit integer to set.
CBByteArray* CBByteArraySubCopy ( CBByteArray self,
uint32_t  offset,
uint32_t  length 
)

Copies a subsection of a CBByteArray.

Parameters:
selfThe CBByteArray object to copy from.
offsetThe offset to the start of the copy.
lengthThe length of the copy.
Returns:
The new CBByteArray.
CBByteArray* CBByteArraySubReference ( CBByteArray self,
uint32_t  offset,
uint32_t  length 
)

References a subsection of a CBByteArray.

Parameters:
selfThe CBByteArray object to reference.
offsetThe offset to the start of the reference.
lengthThe length of the reference.
Returns:
The new CBByteArray.
void CBFreeByteArray ( void *  self)

Frees a CBByteArray object.

Parameters:
selfThe CBByteArray object to free.
CBByteArray* CBGetByteArray ( void *  self)

Gets a CBByteArray from another object. Use this to avoid casts.

Parameters:
selfThe object to obtain the CBByteArray from.
Returns:
The CBByteArray object.
bool CBInitByteArrayFromString ( CBByteArray self,
char *  string,
bool  terminator,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Initialises a CBByteArray object from a C string. The termination character is not included in the new CBByteArray.

Parameters:
selfThe CBByteArray object to initialise
stringThe string to put into a CBByteArray.
terminatorIf tru, include the termination character.
onErrorReceivedCBEngine for errors.
Returns:
true on success, false on failure.
bool CBInitByteArrayOfSize ( CBByteArray self,
uint32_t  size,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Initialises an empty CBByteArray object.

Parameters:
selfThe CBByteArray object to initialise
sizeSize in bytes for the new array.
onErrorReceivedCBEngine for errors.
Returns:
true on success, false on failure.
bool CBInitByteArraySubReference ( CBByteArray self,
CBByteArray ref,
uint32_t  offset,
uint32_t  length 
)

Initialises a reference CBByteArray to a subsection of an CBByteArray.

Parameters:
selfThe CBByteArray object to initialise.
refThe CBByteArray object to reference.
offsetThe offset to the start of the reference.
lengthThe length of the reference. If 0 the length is set to be the same as the reference CBByteArray.
Returns:
true on success, false on failure.
bool CBInitByteArrayWithData ( CBByteArray self,
uint8_t *  data,
uint32_t  size,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates a new CBByteArray using data.

Parameters:
selfThe CBByteArray object to initialise
dataThe data. This should be dynamically allocated. The new CBByteArray object will take care of it's memory management so do not free this data once passed into this constructor.
sizeSize in bytes for the new array.
onErrorReceivedCBEngine for errors.
Returns:
true on success, false on failure.
bool CBInitByteArrayWithDataCopy ( CBByteArray self,
uint8_t *  data,
uint32_t  size,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates a new CBByteArray using data which is copied.

Parameters:
selfThe CBByteArray object to initialise
dataThe data. This data is copied.
sizeSize in bytes for the new array.
onErrorReceivedCBEngine for errors.
Returns:
true on success, false on failure.
CBByteArray* CBNewByteArrayFromString ( char *  string,
bool  terminator,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates a CBByteArray object from a C string. The termination character is not included in the new CBByteArray.

Parameters:
stringThe string to put into a CBByteArray.
terminatorIf true, include the termination character.
onErrorReceivedCBEngine for errors.
Returns:
A new CBByteArray object.
CBByteArray* CBNewByteArrayOfSize ( uint32_t  size,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates an empty CBByteArray object.

Parameters:
sizeSize in bytes for the new array.
onErrorReceivedCBEngine for errors.
Returns:
An empty CBByteArray object.
CBByteArray* CBNewByteArraySubReference ( CBByteArray ref,
uint32_t  offset,
uint32_t  length 
)

Creates a CBByteArray object referencing another CBByteArrayObject.

Parameters:
refThe CBByteArray to reference.
offsetThe offset to the start of the reference in the reference CBByteArray.
lengthThe length of the new CBByteArray. If 0 the length is set to be the same as the reference CBByteArray.
Returns:
An empty CBByteArray object.
CBByteArray* CBNewByteArrayWithData ( uint8_t *  data,
uint32_t  size,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates a new CBByteArray using data.

Parameters:
dataThe data. This should be dynamically allocated. The new CBByteArray object will take care of it's memory management so do not free this data once passed into this constructor.
sizeSize in bytes for the new array.
onErrorReceivedCBEngine for errors.
Returns:
The new CBByteArray object.
CBByteArray* CBNewByteArrayWithDataCopy ( uint8_t *  data,
uint32_t  size,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates a new CBByteArray using data which is copied.

Parameters:
dataThe data. This data is copied.
sizeSize in bytes for the new array.
onErrorReceivedCBEngine for errors.
Returns:
The new CBByteArray object.