S2OPC OPCUA Toolkit
Data Structures | Functions
sopc_buffer.h File Reference

A buffer of bytes with a maximum size, length and position. More...

#include <stdbool.h>
#include <stdint.h>
#include "sopc_enums.h"

Go to the source code of this file.

Data Structures

struct  SOPC_Buffer
 Bytes buffer structure. More...
 

Functions

SOPC_BufferSOPC_Buffer_Create (uint32_t size)
 Allocate a buffer and its data bytes of the given definitive size and returns it. More...
 
SOPC_BufferSOPC_Buffer_CreateResizable (uint32_t initial_size, uint32_t maximum_size)
 Allocate a resizable buffer and its initial and maximum data bytes. More...
 
SOPC_BufferSOPC_Buffer_Attach (uint8_t *data, uint32_t size)
 Wraps a raw memory area into an SOPC_Buffer. More...
 
void SOPC_Buffer_Clear (SOPC_Buffer *buffer)
 Deallocate buffer data bytes content. More...
 
void SOPC_Buffer_Delete (SOPC_Buffer *buffer)
 Deallocate buffer and its data bytes content (Clear + deallocate pointer) More...
 
void SOPC_Buffer_Reset (SOPC_Buffer *buffer)
 Reset length, position and data bytes to zero value of an allocated buffer. More...
 
SOPC_ReturnStatus SOPC_Buffer_ResetAfterPosition (SOPC_Buffer *buffer, uint32_t position)
 reset data bytes after position (>=) to zero and set buffer position and length to given position More...
 
SOPC_ReturnStatus SOPC_Buffer_GetPosition (SOPC_Buffer *buffer, uint32_t *position)
 Get buffer current position. More...
 
SOPC_ReturnStatus SOPC_Buffer_SetPosition (SOPC_Buffer *buffer, uint32_t position)
 Set buffer to the given position. More...
 
SOPC_ReturnStatus SOPC_Buffer_SetDataLength (SOPC_Buffer *buffer, uint32_t length)
 Set buffer to the given length. More...
 
SOPC_ReturnStatus SOPC_Buffer_Write (SOPC_Buffer *buffer, const uint8_t *data_src, uint32_t count)
 Write the given bytes into the buffer data bytes from the buffer position (adapting buffer position and length if necessary) More...
 
SOPC_ReturnStatus SOPC_Buffer_Read (uint8_t *data_dest, SOPC_Buffer *buffer, uint32_t count)
 Read the given bytes of the buffer data bytes from the buffer position (adapting buffer position to next position to read) More...
 
SOPC_ReturnStatus SOPC_Buffer_Copy (SOPC_Buffer *dest, SOPC_Buffer *src)
 Copy the data bytes and properties from the source buffer to the destination buffer. More...
 
SOPC_ReturnStatus SOPC_Buffer_CopyWithLength (SOPC_Buffer *dest, SOPC_Buffer *src, uint32_t limitedLength)
 Copy the data bytes and properties for the given length from the source buffer to the destination buffer. More...
 
uint32_t SOPC_Buffer_Remaining (SOPC_Buffer *buffer)
 Returns the remaining number of unread bytes in the buffer. More...
 
int64_t SOPC_Buffer_ReadFrom (SOPC_Buffer *buffer, SOPC_Buffer *src, uint32_t n)
 Reads n bytes from src into buffer. More...
 
SOPC_ReturnStatus SOPC_Buffer_ReadFile (const char *path, SOPC_Buffer **buf)
 Reads the contents of a file into a SOPC_Buffer. More...
 
SOPC_ReturnStatus SOPC_Buffer_PrintU32 (SOPC_Buffer *buf, const uint32_t value)
 Print the value into the buffer data bytes from the buffer position (adapting buffer position and length if necessary) There is no terminating NULL character added at the end of the printed value. More...
 
SOPC_ReturnStatus SOPC_Buffer_PrintI32 (SOPC_Buffer *buf, const int32_t value)
 Print the value into the buffer data bytes from the buffer position (adapting buffer position and length if necessary) The I32 format written in the buffer does not take into account the spacing for '-' in the case of a positive value. There is no terminating NULL character added at the end of the printed value. More...
 
SOPC_ReturnStatus SOPC_Buffer_PrintFloatDouble (SOPC_Buffer *buf, const double value)
 Print the value into the buffer data bytes from the buffer position (adapting buffer position and length if necessary) The float/double format written in the buffer does not take into account the spacing for '-' in the case of a positive value. The output is equivalent to '%.*g' print format with SOPC_PRECISION_PRINTING_FLOAT_NUMBERS as precision. There is no terminating NULL character added at the end of the printed value. More...
 

Detailed Description

A buffer of bytes with a maximum size, length and position.

Function Documentation

◆ SOPC_Buffer_Create()

SOPC_Buffer* SOPC_Buffer_Create ( uint32_t  size)

Allocate a buffer and its data bytes of the given definitive size and returns it.

Parameters
sizeThe size of the data bytes allocated (static size).
Returns
Pointer on the allocated buffer or NULL if allocation failed

◆ SOPC_Buffer_CreateResizable()

SOPC_Buffer* SOPC_Buffer_CreateResizable ( uint32_t  initial_size,
uint32_t  maximum_size 
)

Allocate a resizable buffer and its initial and maximum data bytes.

Parameters
initial_sizeThe size of the data bytes allocated initially (also used as increment step)
maximum_sizeThe size of the maximum data bytes finally allocated
Returns
Pointer on the allocated buffer or NULL if allocation failed

◆ SOPC_Buffer_Attach()

SOPC_Buffer* SOPC_Buffer_Attach ( uint8_t *  data,
uint32_t  size 
)

Wraps a raw memory area into an SOPC_Buffer.

Parameters
dataA pointer the memory zone.
sizeThe size of the memory zone.
Returns
The created SOPC_Buffer, or NULL on memory allocation failure.

The ownership of the memory area is transferred to the SOPC_Buffer. In other words, the memory will be freed when SOPC_Buffer_Clear or SOPC_Buffer_Delete is called.

◆ SOPC_Buffer_Clear()

void SOPC_Buffer_Clear ( SOPC_Buffer buffer)

Deallocate buffer data bytes content.

Parameters
bufferPointer to the buffer in which data bytes content must be deallocated.

◆ SOPC_Buffer_Delete()

void SOPC_Buffer_Delete ( SOPC_Buffer buffer)

Deallocate buffer and its data bytes content (Clear + deallocate pointer)

Parameters
bufferPointer to the buffer to deallocate (pointer must not be used anymore after operation)

◆ SOPC_Buffer_Reset()

void SOPC_Buffer_Reset ( SOPC_Buffer buffer)

Reset length, position and data bytes to zero value of an allocated buffer.

Parameters
bufferPointer to the buffer to reset

◆ SOPC_Buffer_ResetAfterPosition()

SOPC_ReturnStatus SOPC_Buffer_ResetAfterPosition ( SOPC_Buffer buffer,
uint32_t  position 
)

reset data bytes after position (>=) to zero and set buffer position and length to given position

Parameters
bufferPointer to the buffer to reset to the given position
positionNew position of the reset buffer (position <= buffer->length)
Returns
SOPC_STATUS_OK if succeeded, SOPC_STATUS_INVALID_PARAMETERS otherwise (NULL pointer, non allocated buffer content, invalid position)

◆ SOPC_Buffer_GetPosition()

SOPC_ReturnStatus SOPC_Buffer_GetPosition ( SOPC_Buffer buffer,
uint32_t *  position 
)

Get buffer current position.

Parameters
bufferPointer to the buffer from which to get current position
positionNon-NULL pointer to store the current position of the buffer
Returns
SOPC_STATUS_OK if succeeded, SOPC_STATUS_INVALID_PARAMETERS otherwise (NULL pointer, non allocated buffer content)

◆ SOPC_Buffer_SetPosition()

SOPC_ReturnStatus SOPC_Buffer_SetPosition ( SOPC_Buffer buffer,
uint32_t  position 
)

Set buffer to the given position.

Parameters
bufferPointer to the buffer to set to the given position
positionNew position of the buffer (<= buffer->length)
Returns
SOPC_STATUS_OK if succeeded, SOPC_STATUS_INVALID_PARAMETERS otherwise (NULL pointer, non allocated buffer content, invalid position)

◆ SOPC_Buffer_SetDataLength()

SOPC_ReturnStatus SOPC_Buffer_SetDataLength ( SOPC_Buffer buffer,
uint32_t  length 
)

Set buffer to the given length.

Parameters
bufferPointer to the buffer to set to the given length
lengthNew length of the buffer (<= buffer->maxsize && >= buffer->position)
Returns
SOPC_STATUS_OK if succeeded, SOPC_STATUS_INVALID_PARAMETERS otherwise (NULL pointer, non allocated buffer content, invalid length)

◆ SOPC_Buffer_Write()

SOPC_ReturnStatus SOPC_Buffer_Write ( SOPC_Buffer buffer,
const uint8_t *  data_src,
uint32_t  count 
)

Write the given bytes into the buffer data bytes from the buffer position (adapting buffer position and length if necessary)

Parameters
bufferPointer to the buffer to write into
data_srcPointer to the bytes to be written in the buffer (HYP: nb bytes >= count bytes)
countNumber of bytes to write in the buffer (count + buffer->position <= buffer->maxsize)
Returns
SOPC_STATUS_OK if succeeded, an error code otherwise (NULL pointer, non allocated buffer content, full buffer avoiding operation)

◆ SOPC_Buffer_Read()

SOPC_ReturnStatus SOPC_Buffer_Read ( uint8_t *  data_dest,
SOPC_Buffer buffer,
uint32_t  count 
)

Read the given bytes of the buffer data bytes from the buffer position (adapting buffer position to next position to read)

Parameters
data_destPointer to the bytes to set to read bytes value of the buffer (HYP: nb bytes >= count bytes) If NULL ,then data are simply skipped
bufferPointer to the buffer to read from
countNumber of bytes to read from the buffer (count + buffer->position <= buffer->length)
Returns
SOPC_STATUS_OK if succeeded, an error code otherwise (NULL pointer, non allocated buffer content, empty buffer avoiding operation)

◆ SOPC_Buffer_Copy()

SOPC_ReturnStatus SOPC_Buffer_Copy ( SOPC_Buffer dest,
SOPC_Buffer src 
)

Copy the data bytes and properties from the source buffer to the destination buffer.

Parameters
destPointer to the destination buffer of the copy operation (dest->maxsize >= src->length)
srcPointer to the source buffer of the copy operation
Returns
SOPC_STATUS_OK if succeeded, an error code otherwise (NULL pointer, non allocated buffer content, incompatible size)

◆ SOPC_Buffer_CopyWithLength()

SOPC_ReturnStatus SOPC_Buffer_CopyWithLength ( SOPC_Buffer dest,
SOPC_Buffer src,
uint32_t  limitedLength 
)

Copy the data bytes and properties for the given length from the source buffer to the destination buffer.

Parameters
destPointer to the destination buffer of the copy operation (dest->maxsize >= limitedLength)
srcPointer to the source buffer of the copy operation (src->length >= limitedLength, src->position <= limitedLength)
limitedLengthThe length to use for the copy, number of bytes copied and length set in destination buffer
Returns
SOPC_STATUS_OK if succeeded, an error code otherwise (NULL pointer, non allocated buffer content, incompatible size)

◆ SOPC_Buffer_Remaining()

uint32_t SOPC_Buffer_Remaining ( SOPC_Buffer buffer)

Returns the remaining number of unread bytes in the buffer.

Parameters
bufferThe buffer.
Returns
The number of unread bytes in the buffer.

◆ SOPC_Buffer_ReadFrom()

int64_t SOPC_Buffer_ReadFrom ( SOPC_Buffer buffer,
SOPC_Buffer src,
uint32_t  n 
)

Reads n bytes from src into buffer.

Parameters
bufferThe buffer to read the bytes into.
srcThe source buffer.
nThe maximum number of bytes to read.
Returns
The number of bytes read in case of success, -1 in case of failure.

◆ SOPC_Buffer_ReadFile()

SOPC_ReturnStatus SOPC_Buffer_ReadFile ( const char *  path,
SOPC_Buffer **  buf 
)

Reads the contents of a file into a SOPC_Buffer.

Parameters
pathThe path to the file.
bufOut parameter, points to a new buffer holding the contents of the file. This buffer should be freed by the caller.
Returns
SOPC_STATUS_OK on success, an error code in case of failure.

◆ SOPC_Buffer_PrintU32()

SOPC_ReturnStatus SOPC_Buffer_PrintU32 ( SOPC_Buffer buf,
const uint32_t  value 
)

Print the value into the buffer data bytes from the buffer position (adapting buffer position and length if necessary) There is no terminating NULL character added at the end of the printed value.

Parameters
valueuint32_t to print into the buffer
bufPointer to the buffer to write into
Returns
SOPC_STATUS_OK if succeeded, an error code otherwise (NULL pointer, non allocated buffer content, full buffer avoiding operation)

◆ SOPC_Buffer_PrintI32()

SOPC_ReturnStatus SOPC_Buffer_PrintI32 ( SOPC_Buffer buf,
const int32_t  value 
)

Print the value into the buffer data bytes from the buffer position (adapting buffer position and length if necessary) The I32 format written in the buffer does not take into account the spacing for '-' in the case of a positive value. There is no terminating NULL character added at the end of the printed value.

Parameters
valueint32_t to print into the buffer
bufPointer to the buffer to write into
Returns
SOPC_STATUS_OK if succeeded, an error code otherwise (NULL pointer, non allocated buffer content, full buffer avoiding operation)

◆ SOPC_Buffer_PrintFloatDouble()

SOPC_ReturnStatus SOPC_Buffer_PrintFloatDouble ( SOPC_Buffer buf,
const double  value 
)

Print the value into the buffer data bytes from the buffer position (adapting buffer position and length if necessary) The float/double format written in the buffer does not take into account the spacing for '-' in the case of a positive value. The output is equivalent to '%.*g' print format with SOPC_PRECISION_PRINTING_FLOAT_NUMBERS as precision. There is no terminating NULL character added at the end of the printed value.

For exemple : '42', '-4.3e+111'

About special values, the syntax is as follows (including double quote) :

  • Not a Number : '"NaN"'
  • Positive infinity : '"Infinity"'
  • Negative infinity : '"-Infinity"'

This format is JSON compatible for standard values.

Parameters
valuedouble to print into the buffer
bufPointer to the buffer to write into
Returns
SOPC_STATUS_OK if succeeded, an error code otherwise (NULL pointer, non allocated buffer content, full buffer avoiding operation)