Marshall Library

A library to marshall data in and out of character buffers.

Summary
Marshall LibraryA library to marshall data in and out of character buffers.
IntroductionThe following library marshalls and unmarshalls primitive data types into and out of buffers.
AlignmentAlthough the packing functions take care of most alignment issues transparently, it’s important that you understand the basics of alignment before using these functions to avoid some common pitfalls.
Format StringsVarious functions take a format string as a parameter to marshall multiple values into a buffer at one time.
ExamplesPacking Data into a File
Library Functions
Packing FunctionsThe following function pack primitive data types into a buffer in an aligned fashion.
Pack_Int8Packs a 8-bit signed integer into a buffer.
Pack_UInt8Packs a 8-bit unsigned integer into a buffer.
Pack_Int16Packs a 16-bit signed integer into a buffer.
Pack_UInt16Packs a 16-bit unsigned integer into a buffer.
Pack_Int32Packs a 32-bit signed integer into a buffer.
Pack_UInt32Packs a 32-bit unsigned integer into a buffer.
Pack_Int64Packs a 64-bit signed integer into a buffer.
Pack_UInt64Packs a 64-bit unsigned integer into a buffer.
Pack_FloatPacks a single precision, 32-bit floating point number into a buffer.
Pack_DoublePacks a double precision, 64-bit floating point number into a buffer.
Pack_StringPacks a length prepended string into a buffer.
Pack_Array1Packs an array of 1-byte aligned values into a buffer.
Pack_Array2Packs an array of 2-byte aligned values into a buffer.
Pack_Array4Packs an array of 4-byte aligned values into a buffer.
Pack_Array8Packs an array of 8-byte aligned values into a buffer.
Pack_BufferPacks values into a buffer according to the format string parameter.
Pack_BufferNewPacks values into a malloc’ed buffer according to the format string parameter.
Pack_BufferArgsPacks values into a buffer according to the format string parameter.
Unpacking Functions
Unpack_Int8Unpacks a 8-bit signed integer from a buffer.
Unpack_UInt8Unpacks a 8-bit unsigned integer from a buffer.
Unpack_Int16Unpacks a 16-bit signed integer from a buffer.
Unpack_UInt16Unpacks a 16-bit unsigned integer from a buffer.
Unpack_Int32Unpacks a 32-bit signed integer from a buffer.
Unpack_UInt32Unpacks a 32-bit unsigned integer from a buffer.
Unpack_Int64Unpacks a 64-bit signed integer from a buffer.
Unpack_UInt64Unpacks a 64-bit unsigned integer from a buffer.
Unpack_FloatUnpacks a single precision, 32-bit floating point number from a buffer.
Unpack_DoubleUnpacks a double precision, 64-bit floating point number from a buffer.
Unpack_StringUnpacks a length prepended string from a buffer.
Unpack_StringNewUnpacks a length prepended string from a buffer.
Unpack_Array1Unpacks an array of 1-byte aligned values from a buffer.
Unpack_Array2Unpacks an array of 2-byte aligned values from a buffer.
Unpack_Array4Unpacks an array of 4-byte aligned values from a buffer.
Unpack_Array8Unpacks an array of 8-byte aligned values from a buffer.
Unpack_BufferUnpacks values from a buffer according to the format string parameter.
Unpack_BufferArgsUnpacks values from a buffer according to the format string parameter.
Packing/Unpacking Utilities
Pack_CalcSizeCalculates the size of a buffer needed to pack or unpack the data described by the format string.
Pack_CalcSizeArgsCalculates the size of a buffer needed to pack or unpack the data described by the format string.
Stuffing FunctionsThe following function stuff primitive data types into a buffer, ignoring alignment within the buffer.
Stuff_Int8Stuffs a 8-bit signed integer into a buffer.
Stuff_UInt8Stuffs a 8-bit unsigned integer into a buffer.
Stuff_Int16Stuffs a 16-bit signed integer into a buffer.
Stuff_UInt16Stuffs a 16-bit unsigned integer into a buffer.
Stuff_Int32Stuffs a 32-bit signed integer into a buffer.
Stuff_UInt32Stuffs a 32-bit unsigned integer into a buffer.
Stuff_Int64Stuffs a 64-bit signed integer into a buffer.
Stuff_UInt64Stuffs a 64-bit unsigned integer into a buffer.
Stuff_FloatStuffs a single precision, 32-bit floating point number into a buffer.
Stuff_DoubleStuffs a double precision, 64-bit floating point number into a buffer.
Stuff_StringStuffs a length prepended string into a buffer.
Stuff_ArrayStuffs an array of bytes into a buffer.
Stuff_BufferStuffs values into a buffer according to the format string parameter.
Stuff_BufferNewStuffs values into a malloc’ed buffer according to the format string parameter.
Stuff_BufferArgsStuffs values into a buffer according to the format string parameter.
Unstuffing Functions
Unstuff_Int8Unstuffs a 8-bit signed integer from a buffer.
Unstuff_UInt8Unstuffs a 8-bit unsigned integer from a buffer.
Unstuff_Int16Unstuffs a 16-bit signed integer from a buffer.
Unstuff_UInt16Unstuffs a 16-bit unsigned integer from a buffer.
Unstuff_Int32Unstuffs a 32-bit signed integer from a buffer.
Unstuff_UInt32Unstuffs a 32-bit unsigned integer from a buffer.
Unstuff_Int64Unstuffs a 64-bit signed integer from a buffer.
Unstuff_UInt64Unstuffs a 64-bit unsigned integer from a buffer.
Unstuff_FloatUnstuffs a single precision, 32-bit floating point number from a buffer.
Unstuff_DoubleUnstuffs a double precision, 64-bit floating point number from a buffer.
Unstuff_StringUnstuffs a length prepended string from a buffer.
Unstuff_StringNewUnstuffs a length prepended string from a buffer.
Unstuff_ArrayUnstuffs an array of values from a buffer.
Unstuff_BufferUnstuffs values from a buffer according to the format string parameter.
Unstuff_BufferArgsUnstuffs values from a buffer according to the format string parameter.
Stuffing/Unstuffing Utilities
Stuff_CalcSizeCalculates the size of a buffer needed to stuff or unstuff the data described by the format string.
Stuff_CalcSizeArgsCalculates the size of a buffer needed to stuff or unstuff the data described by the format string.

Introduction

The following library marshalls and unmarshalls primitive data types into and out of buffers.  They can be used for data storage, transmission, etc. in a consistant and portable way.

In context of this library, “packing” is used to mean storing values in the buffer with alignment, while “stuffing” ignores alignment.  Packing is generally faster while stuffing is more space efficient.  The packing and stuffing functions can be used together but great care must be taken and is generally discouraged.

Alignment

Although the packing functions take care of most alignment issues transparently, it’s important that you understand the basics of alignment before using these functions to avoid some common pitfalls.

Format Strings

Various functions take a format string as a parameter to marshall multiple values into a buffer at one time.  The values that get marshalled are passed in through a variable argument list.  The following table is an explanation of the various tokens in the format string with their C datatype in parentheses:

’c’Signed 8-bit integer (int8_t, char)
’C’Unsigned 8-bit integer (uint8_t)
’h’Signed 16-bit integer (int16_t)
’H’Unsigned 16-bit integer (uint16_t)
’l’Signed 32-bit integer (int32_t)
’L’Unsigned 32-bit integer (uint32_t)
’x’Signed 64-bit integer (int64_t)
’X’Unsigned 64-bit integer (uint64_t)
’f’Single precision, 32-bit floating point (float)
’d’Double precision, 64-bit floating point (double)
’s’String, length prepended (char *)
’S’String (malloc’ed), length prepended (char *)
’a’Array, size in bytes
’A’Array, size in elements
’1’Align buffer pointer to a 1-byte boundry
’2’Align buffer pointer to a 2-byte boundry
’4’Align buffer pointer to a 4-byte boundry
’8’Align buffer pointer to a 8-byte boundry
’=’Marshall in native endian-ness
’<’Marshall in little endian-ness
’>’Marshall in big endian-ness
’+’Advance buffer pointer

Examples

Packing Data into a File

#include <stdio.h>
#include <stdlib.h>

#include "marshall.h"

int main(void)
{
    int size;
    char *buf;
    FILE *myfile;

    if((myfile = fopen("pack.dat", "w")) == NULL)
    {
        printf("Failed to open file for writing!\n");
        return -1;
    }

    size = Pack_BufferNew(&buf, "cHfs", 'q', 45, 1.23, "Hello world!");
    if(size < 0)
    {
        printf("An error occured packing data!\n");
        return -1;
    }

    fwrite(buf, sizeof(char), size, myfile);

    fclose(myfile);

    free(buf);

    return 0;
}

Library Functions

Summary
Packing FunctionsThe following function pack primitive data types into a buffer in an aligned fashion.
Pack_Int8Packs a 8-bit signed integer into a buffer.
Pack_UInt8Packs a 8-bit unsigned integer into a buffer.
Pack_Int16Packs a 16-bit signed integer into a buffer.
Pack_UInt16Packs a 16-bit unsigned integer into a buffer.
Pack_Int32Packs a 32-bit signed integer into a buffer.
Pack_UInt32Packs a 32-bit unsigned integer into a buffer.
Pack_Int64Packs a 64-bit signed integer into a buffer.
Pack_UInt64Packs a 64-bit unsigned integer into a buffer.
Pack_FloatPacks a single precision, 32-bit floating point number into a buffer.
Pack_DoublePacks a double precision, 64-bit floating point number into a buffer.
Pack_StringPacks a length prepended string into a buffer.
Pack_Array1Packs an array of 1-byte aligned values into a buffer.
Pack_Array2Packs an array of 2-byte aligned values into a buffer.
Pack_Array4Packs an array of 4-byte aligned values into a buffer.
Pack_Array8Packs an array of 8-byte aligned values into a buffer.
Pack_BufferPacks values into a buffer according to the format string parameter.
Pack_BufferNewPacks values into a malloc’ed buffer according to the format string parameter.
Pack_BufferArgsPacks values into a buffer according to the format string parameter.
Unpacking Functions
Unpack_Int8Unpacks a 8-bit signed integer from a buffer.
Unpack_UInt8Unpacks a 8-bit unsigned integer from a buffer.
Unpack_Int16Unpacks a 16-bit signed integer from a buffer.
Unpack_UInt16Unpacks a 16-bit unsigned integer from a buffer.
Unpack_Int32Unpacks a 32-bit signed integer from a buffer.
Unpack_UInt32Unpacks a 32-bit unsigned integer from a buffer.
Unpack_Int64Unpacks a 64-bit signed integer from a buffer.
Unpack_UInt64Unpacks a 64-bit unsigned integer from a buffer.
Unpack_FloatUnpacks a single precision, 32-bit floating point number from a buffer.
Unpack_DoubleUnpacks a double precision, 64-bit floating point number from a buffer.
Unpack_StringUnpacks a length prepended string from a buffer.
Unpack_StringNewUnpacks a length prepended string from a buffer.
Unpack_Array1Unpacks an array of 1-byte aligned values from a buffer.
Unpack_Array2Unpacks an array of 2-byte aligned values from a buffer.
Unpack_Array4Unpacks an array of 4-byte aligned values from a buffer.
Unpack_Array8Unpacks an array of 8-byte aligned values from a buffer.
Unpack_BufferUnpacks values from a buffer according to the format string parameter.
Unpack_BufferArgsUnpacks values from a buffer according to the format string parameter.
Packing/Unpacking Utilities
Pack_CalcSizeCalculates the size of a buffer needed to pack or unpack the data described by the format string.
Pack_CalcSizeArgsCalculates the size of a buffer needed to pack or unpack the data described by the format string.
Stuffing FunctionsThe following function stuff primitive data types into a buffer, ignoring alignment within the buffer.
Stuff_Int8Stuffs a 8-bit signed integer into a buffer.
Stuff_UInt8Stuffs a 8-bit unsigned integer into a buffer.
Stuff_Int16Stuffs a 16-bit signed integer into a buffer.
Stuff_UInt16Stuffs a 16-bit unsigned integer into a buffer.
Stuff_Int32Stuffs a 32-bit signed integer into a buffer.
Stuff_UInt32Stuffs a 32-bit unsigned integer into a buffer.
Stuff_Int64Stuffs a 64-bit signed integer into a buffer.
Stuff_UInt64Stuffs a 64-bit unsigned integer into a buffer.
Stuff_FloatStuffs a single precision, 32-bit floating point number into a buffer.
Stuff_DoubleStuffs a double precision, 64-bit floating point number into a buffer.
Stuff_StringStuffs a length prepended string into a buffer.
Stuff_ArrayStuffs an array of bytes into a buffer.
Stuff_BufferStuffs values into a buffer according to the format string parameter.
Stuff_BufferNewStuffs values into a malloc’ed buffer according to the format string parameter.
Stuff_BufferArgsStuffs values into a buffer according to the format string parameter.
Unstuffing Functions
Unstuff_Int8Unstuffs a 8-bit signed integer from a buffer.
Unstuff_UInt8Unstuffs a 8-bit unsigned integer from a buffer.
Unstuff_Int16Unstuffs a 16-bit signed integer from a buffer.
Unstuff_UInt16Unstuffs a 16-bit unsigned integer from a buffer.
Unstuff_Int32Unstuffs a 32-bit signed integer from a buffer.
Unstuff_UInt32Unstuffs a 32-bit unsigned integer from a buffer.
Unstuff_Int64Unstuffs a 64-bit signed integer from a buffer.
Unstuff_UInt64Unstuffs a 64-bit unsigned integer from a buffer.
Unstuff_FloatUnstuffs a single precision, 32-bit floating point number from a buffer.
Unstuff_DoubleUnstuffs a double precision, 64-bit floating point number from a buffer.
Unstuff_StringUnstuffs a length prepended string from a buffer.
Unstuff_StringNewUnstuffs a length prepended string from a buffer.
Unstuff_ArrayUnstuffs an array of values from a buffer.
Unstuff_BufferUnstuffs values from a buffer according to the format string parameter.
Unstuff_BufferArgsUnstuffs values from a buffer according to the format string parameter.
Stuffing/Unstuffing Utilities
Stuff_CalcSizeCalculates the size of a buffer needed to stuff or unstuff the data described by the format string.
Stuff_CalcSizeArgsCalculates the size of a buffer needed to stuff or unstuff the data described by the format string.

Packing Functions

The following function pack primitive data types into a buffer in an aligned fashion.

Pack_Int8

int Pack_Int8(int8_t value,
char *buf,
enum PackOrder order)

Packs a 8-bit signed integer into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_UInt8

int Pack_UInt8(uint8_t value,
char *buf,
enum PackOrder order)

Packs a 8-bit unsigned integer into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Int16

int Pack_Int16(int16_t value,
char *buf,
enum PackOrder order)

Packs a 16-bit signed integer into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_UInt16

int Pack_UInt16(uint16_t value,
char *buf,
enum PackOrder order)

Packs a 16-bit unsigned integer into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Int32

int Pack_Int32(int32_t value,
char *buf,
enum PackOrder order)

Packs a 32-bit signed integer into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_UInt32

int Pack_UInt32(uint32_t value,
char *buf,
enum PackOrder order)

Packs a 32-bit unsigned integer into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Int64

int Pack_Int64(int64_t value,
char *buf,
enum PackOrder order)

Packs a 64-bit signed integer into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_UInt64

int Pack_UInt64(uint64_t value,
char *buf,
enum PackOrder order)

Packs a 64-bit unsigned integer into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Float

int Pack_Float(float value,
char *buf,
enum PackOrder order)

Packs a single precision, 32-bit floating point number into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Double

int Pack_Double(double value,
char *buf,
enum PackOrder order)

Packs a double precision, 64-bit floating point number into a buffer.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_String

int Pack_String(char *string,
char *buf,
enum PackOrder order)

Packs a length prepended string into a buffer.  An unsigned 32-bit integer length is packed into the buffer first with order endian-ness.  If you do not want the string to be length prepended, use Pack_Array1 instead.

Parameters

valueThe value to pack into the buffer.
bufPointer to the buffer to pack the value into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Array1

int Pack_Array1(void *value,
int size,
char *buf,
enum PackOrder order)

Packs an array of 1-byte aligned values into a buffer.

Parameters

valueA pointer to an array of values to pack into the buffer.
sizeThe size of the array of 1-byte aligned values to pack as returned by the sizeof operator.
bufPointer to the buffer to pack the values into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Array2

int Pack_Array2(void *value,
int size,
char *buf,
enum PackOrder order)

Packs an array of 2-byte aligned values into a buffer.

Parameters

valueA pointer to an array of values to pack into the buffer.
sizeThe size of the array of 2-byte aligned values to pack as returned by the sizeof operator.
bufPointer to the buffer to pack the values into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Array4

int Pack_Array4(void *value,
int size,
char *buf,
enum PackOrder order)

Packs an array of 4-byte aligned values into a buffer.

Parameters

valueA pointer to an array of values to pack into the buffer.
sizeThe size of the array of 4-byte aligned values to pack as returned by the sizeof operator.
bufPointer to the buffer to pack the values into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Array8

int Pack_Array8(void *value,
int size,
char *buf,
enum PackOrder order)

Packs an array of 8-byte aligned values into a buffer.

Parameters

valueA pointer to an array of values to pack into the buffer.
sizeThe size of the array of 8-byte aligned values to pack as returned by the sizeof operator.
bufPointer to the buffer to pack the values into.
orderThe endian byte order to pack the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.

Pack_Buffer

int Pack_Buffer(char *buf,
char *format,
 ...)

Packs values into a buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  Data to be packed is passed in through the variable argument list.

Parameters

bufPointer to the buffer to pack the values into.
formatA format string describing the data to pack.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.  Returns -1 if an error occurs.

Example

int size;
char *buf = calloc(100, sizeof(char));
size = Pack_Buffer(buf, "cHfs", 'q', 45, 1.23, "Hello world!");

Pack_BufferNew

int Pack_BufferNew(char **buf,
char *format,
 ...)

Packs values into a malloc’ed buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  Data to be packed is passed in through the variable argument list.  The buffer must be freed with the free function when you are done with the buffer unless an error occurs.

Parameters

bufA pointer to a pointer that will point to the malloc’ed buffer.
formatA format string describing the data to pack.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.  Returns -1 if an error occurs.

Example

int size;
char *buf;
size = Pack_BufferNew(&buf, "cHfs", 'q', 45, 1.23, "Hello world!");

Pack_BufferArgs

int Pack_BufferArgs(char *buf,
char *format,
va_list args)

Packs values into a buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  The function takes a va_list for use within wrapping functions that take a variable number of parameters.

Parameters

bufPointer to the buffer to pack the value into.
formatA format string describing the data to pack.
argsA va_list of arguments.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for packing further data.  Returns -1 if an error occurs.

Example

int MyFunction(char* buf, char* format, ...)
{
    int buf_size;
    va_list args;

    // Do our own thing here

    va_start(args, format);
    buf_size = Pack_BufferArgs(buf, format, args);
    va_end(args);

    return buf_size;
}

...
size = MyFunction(buf, "cHfs", 'q', 45, 1.23, "Hello world!");
...

Unpacking Functions

Unpack_Int8

int Unpack_Int8(int8_t *value,
char *buf,
enum PackOrder order)

Unpacks a 8-bit signed integer from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_UInt8

int Unpack_UInt8(uint8_t *value,
char *buf,
enum PackOrder order)

Unpacks a 8-bit unsigned integer from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Int16

int Unpack_Int16(int16_t *value,
char *buf,
enum PackOrder order)

Unpacks a 16-bit signed integer from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_UInt16

int Unpack_UInt16(uint16_t *value,
char *buf,
enum PackOrder order)

Unpacks a 16-bit unsigned integer from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Int32

int Unpack_Int32(int32_t *value,
char *buf,
enum PackOrder order)

Unpacks a 32-bit signed integer from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_UInt32

int Unpack_UInt32(uint32_t *value,
char *buf,
enum PackOrder order)

Unpacks a 32-bit unsigned integer from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Int64

int Unpack_Int64(int64_t *value,
char *buf,
enum PackOrder order)

Unpacks a 64-bit signed integer from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_UInt64

int Unpack_UInt64(uint64_t *value,
char *buf,
enum PackOrder order)

Unpacks a 64-bit unsigned integer from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Float

int Unpack_Float(float *value,
char *buf,
enum PackOrder order)

Unpacks a single precision, 32-bit floating point number from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Double

int Unpack_Double(double *value,
char *buf,
enum PackOrder order)

Unpacks a double precision, 64-bit floating point number from a buffer.

Parameters

valueA pointer to where the value will be unpacked.
bufPointer to the buffer to unpack from.
orderThe endian byte order to unpack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_String

int Unpack_String(char *string,
char *buf,
enum PackOrder order)

Unpacks a length prepended string from a buffer.  An unsigned 32-bit integer length is unpacked from the buffer first with order endian-ness that tells how many characters are in the string.  If the string is not length prepended, use Unpack_Array1 instead.

Parameters

stringA pointer to a buffer that will hold the unpacked string.
bufA pointer to the buffer to unpack from.
orderThe endian byte order to unpack the prepended length.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_StringNew

int Unpack_StringNew(char **string,
char *buf,
enum PackOrder order)

Unpacks a length prepended string from a buffer.  An unsigned 32-bit integer length is unpacked from the buffer first with order endian-ness that tells how many characters are in the string.  A buffer of sufficent length will be malloc’ed to hold the string.  If the string is not length prepended, use Unpack_Array1 instead.

Parameters

stringA pointer to a buffer that will hold the unpacked string.
bufA pointer to a pointer that will point to the malloc’ed string buffer.
orderThe endian byte order to unpack the prepended length.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Array1

int Unpack_Array1(void *value,
int size,
char *buf,
enum PackOrder order)

Unpacks an array of 1-byte aligned values from a buffer.

Parameters

valueA pointer to an array where the unpacked values will be written.
sizeThe size of the array of 1-byte aligned values to unpack as returned by the sizeof operator.
bufPointer to the buffer to unpack the values from.
orderThe endian byte order to pack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Array2

int Unpack_Array2(void *value,
int size,
char *buf,
enum PackOrder order)

Unpacks an array of 2-byte aligned values from a buffer.

Parameters

valueA pointer to an array where the unpacked values will be written.
sizeThe size of the array of 2-byte aligned values to unpack as returned by the sizeof operator.
bufPointer to the buffer to unpack the values from.
orderThe endian byte order to pack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Array4

int Unpack_Array4(void *value,
int size,
char *buf,
enum PackOrder order)

Unpacks an array of 4-byte aligned values from a buffer.

Parameters

valueA pointer to an array where the unpacked values will be written.
sizeThe size of the array of 4-byte aligned values to unpack as returned by the sizeof operator.
bufPointer to the buffer to unpack the values from.
orderThe endian byte order to pack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Array8

int Unpack_Array8(void *value,
int size,
char *buf,
enum PackOrder order)

Unpacks an array of 8-byte aligned values from a buffer.

Parameters

valueA pointer to an array where the unpacked values will be written.
sizeThe size of the array of 8-byte aligned values to unpack as returned by the sizeof operator.
bufPointer to the buffer to unpack the values from.
orderThe endian byte order to pack the value.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.

Unpack_Buffer

int Unpack_Buffer(char *buf,
char *format,
 ...)

Unpacks values from a buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  Pointers for where the data will be unpacked into are passed in through the variable argument list.

Parameters

bufPointer to the buffer to unpack the values from.
formatA format string describing the data to unpack.

Returns

The number of bytes read from the buffer to unpack the value.  You can use this value to add to a pointer to the buffer for unpacking further data.  Returns -1 if an error occurs.

Example

...
char c;
uint16_t foo[10];
float bar;
char string[20];

Unpack_Buffer(buf, "caHfs", &c, sizeof(foo), &foo, &bar, string);
...

Unpack_BufferArgs

int Unpack_BufferArgs(char *buf,
char *format,
va_list args)

Unpacks values from a buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  The function takes a va_list for use within wrapping functions that take a variable number of parameters.

Parameters

bufPointer to the buffer to unpack the values from.
formatA format string describing the data to unpack.
argsA va_list of arguments.

Returns

The number of bytes read from the buffer to unpack the values.  You can use this value to add to a pointer to the buffer for unpacking further data.  Returns -1 if an error occurs.

Example

int MyFunction(char* buf, char* format, ...)
{
    int buf_size;
    va_list args;

    // Do our own thing here

    va_start(args, format);
    buf_size = Unpack_BufferArgs(buf, format, args);
    va_end(args);

    return buf_size;
}

...
char c;
uint16_t foo[10];
float bar;
char string[20];

MyFunction(buf, "caHfs", &c, sizeof(foo), &foo, &bar, string);
...

Packing/Unpacking Utilities

Pack_CalcSize

int Pack_CalcSize(char *format,
 ...)

Calculates the size of a buffer needed to pack or unpack the data described by the format string.  Data of of variable size (strings, arrays, etc.) should have there sizes, as returned by the sizeof operator, passed in through the variable parameter list.

Parameters

formatA format string describing the data to pack or unpack that will be used to calculate the needed buffer size.

Returns

The size of a buffer needed to contain the data described in the format string.  Returns -1 if an error occurs.

Example

...
int size;
float farray[10];

size = CalcPackSize("cHafX", sizeof(farray));
...

Pack_CalcSizeArgs

int Pack_CalcSizeArgs(char *format,
va_list args)

Calculates the size of a buffer needed to pack or unpack the data described by the format string.  This function is useful with your own wrapping functions and will accept a va_list with all the data present.  Data of of variable size (strings, arrays, etc.) should have there sizes, as returned by the sizeof operator, passed in through the variable parameter list before the pointer to the data.

Parameters

formatA format string describing the data to pack or unpack that will be used to calculate the needed buffer size.
argsA va_list of arguments.

Returns

The size of a buffer needed to contain the data described in the format string.  Returns -1 if an error occurs.

Example

int MyFunction(char* format, ...)
{
    char *buf;
    int buf_size;
    va_list args;

    va_start(args, format);
    buf_size = CalcPackSizeArgs(format, args);
    va_end(args);

    buf = calloc(buf_size, sizeof(char));

    // Do our own thing here like pack with
    // the <Pack_BufferArgs> function.

    return buf_size;
}

Stuffing Functions

The following function stuff primitive data types into a buffer, ignoring alignment within the buffer.

Stuff_Int8

int Stuff_Int8(int8_t value,
char *buf,
enum PackOrder order)

Stuffs a 8-bit signed integer into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_UInt8

int Stuff_UInt8(uint8_t value,
char *buf,
enum PackOrder order)

Stuffs a 8-bit unsigned integer into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_Int16

int Stuff_Int16(int16_t value,
char *buf,
enum PackOrder order)

Stuffs a 16-bit signed integer into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_UInt16

int Stuff_UInt16(uint16_t value,
char *buf,
enum PackOrder order)

Stuffs a 16-bit unsigned integer into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_Int32

int Stuff_Int32(int32_t value,
char *buf,
enum PackOrder order)

Stuffs a 32-bit signed integer into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_UInt32

int Stuff_UInt32(uint32_t value,
char *buf,
enum PackOrder order)

Stuffs a 32-bit unsigned integer into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_Int64

int Stuff_Int64(int64_t value,
char *buf,
enum PackOrder order)

Stuffs a 64-bit signed integer into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_UInt64

int Stuff_UInt64(uint64_t value,
char *buf,
enum PackOrder order)

Stuffs a 64-bit unsigned integer into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_Float

int Stuff_Float(float value,
char *buf,
enum PackOrder order)

Stuffs a single precision, 32-bit floating point number into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_Double

int Stuff_Double(double value,
char *buf,
enum PackOrder order)

Stuffs a double precision, 64-bit floating point number into a buffer.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_String

int Stuff_String(char *string,
char *buf,
enum PackOrder order)

Stuffs a length prepended string into a buffer.  An unsigned 32-bit integer length is stuffed into the buffer first with order endian-ness.  If you do not want the string to be length prepended, use Stuff_Array instead.

Parameters

valueThe value to stuff into the buffer.
bufPointer to the buffer to stuff the value into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_Array

int Stuff_Array(void *value,
int size,
char *buf,
enum PackOrder order)

Stuffs an array of bytes into a buffer.

Parameters

valueA pointer to an array of values to stuff into the buffer.
sizeThe size of the array of values to stuff as returned by the sizeof operator.
bufPointer to the buffer to stuff the values into.
orderThe endian byte order to stuff the value.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.

Stuff_Buffer

int Stuff_Buffer(char *buf,
char *format,
 ...)

Stuffs values into a buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  Data to be stuffed is passed in through the variable argument list.

Parameters

bufPointer to the buffer to stuff the values into.
formatA format string describing the data to stuff.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.  Returns -1 if an error occurs.

Example

int size;
char *buf = calloc(100, sizeof(char));
size = Stuff_Buffer(buf, "cHfs", 'q', 45, 1.23, "Hello world!");

Stuff_BufferNew

int Stuff_BufferNew(char **buf,
char *format,
 ...)

Stuffs values into a malloc’ed buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  Data to be stuffed is passed in through the variable argument list.  The buffer must be freed with the free function when you are done with the buffer unless an error occurs.

Parameters

bufA pointer to a pointer that will point to the malloc’ed buffer.
formatA format string describing the data to stuff.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.  Returns -1 if an error occurs.

Example

int size;
char *buf;
size = Stuff_BufferNew(&buf, "cHfs", 'q', 45, 1.23, "Hello world!");

Stuff_BufferArgs

int Stuff_BufferArgs(char *buf,
char *format,
va_list args)

Stuffs values into a buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  The function takes a va_list for use within wrapping functions that take a variable number of parameters.

Parameters

bufPointer to the buffer to stuff the value into.
formatA format string describing the data to stuff.
argsA va_list of arguments.

Returns

The number of bytes written to the buffer.  You can use this value to add to a pointer to the buffer for stuffing further data.  Returns -1 if an error occurs.

Example

int MyFunction(char* buf, char* format, ...)
{
    int buf_size;
    va_list args;

    // Do our own thing here

    va_start(args, format);
    buf_size = Stuff_BufferArgs(buf, format, args);
    va_end(args);

    return buf_size;
}

...
size = MyFunction(buf, "cHfs", 'q', 45, 1.23, "Hello world!");
...

Unstuffing Functions

Unstuff_Int8

int Unstuff_Int8(int8_t *value,
char *buf,
enum PackOrder order)

Unstuffs a 8-bit signed integer from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_UInt8

int Unstuff_UInt8(uint8_t *value,
char *buf,
enum PackOrder order)

Unstuffs a 8-bit unsigned integer from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_Int16

int Unstuff_Int16(int16_t *value,
char *buf,
enum PackOrder order)

Unstuffs a 16-bit signed integer from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_UInt16

int Unstuff_UInt16(uint16_t *value,
char *buf,
enum PackOrder order)

Unstuffs a 16-bit unsigned integer from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_Int32

int Unstuff_Int32(int32_t *value,
char *buf,
enum PackOrder order)

Unstuffs a 32-bit signed integer from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_UInt32

int Unstuff_UInt32(uint32_t *value,
char *buf,
enum PackOrder order)

Unstuffs a 32-bit unsigned integer from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_Int64

int Unstuff_Int64(int64_t *value,
char *buf,
enum PackOrder order)

Unstuffs a 64-bit signed integer from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_UInt64

int Unstuff_UInt64(uint64_t *value,
char *buf,
enum PackOrder order)

Unstuffs a 64-bit unsigned integer from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_Float

int Unstuff_Float(float *value,
char *buf,
enum PackOrder order)

Unstuffs a single precision, 32-bit floating point number from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_Double

int Unstuff_Double(double *value,
char *buf,
enum PackOrder order)

Unstuffs a double precision, 64-bit floating point number from a buffer.

Parameters

valueA pointer to where the value will be unstuffed.
bufPointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_String

int Unstuff_String(char *string,
char *buf,
enum PackOrder order)

Unstuffs a length prepended string from a buffer.  An unsigned 32-bit integer length is unstuffed from the buffer first with order endian-ness that tells how many characters are in the string.  If the string is not length prepended, use Unstuff_Array instead.

Parameters

stringA pointer to a buffer that will hold the unstuffed string.
bufA pointer to the buffer to unstuff from.
orderThe endian byte order to unstuff the prepended length.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_StringNew

int Unstuff_StringNew(char **string,
char *buf,
enum PackOrder order)

Unstuffs a length prepended string from a buffer.  An unsigned 32-bit integer length is unstuffed from the buffer first with order endian-ness that tells how many characters are in the string.  A buffer of sufficent length will be malloc’ed to hold the string.  If the string is not length prepended, use Unstuff_Array instead.

Parameters

stringA pointer to a buffer that will hold the unstuffed string.
bufA pointer to a pointer that will point to the malloc’ed string buffer.
orderThe endian byte order to unstuff the prepended length.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_Array

int Unstuff_Array(void *value,
int size,
char *buf,
enum PackOrder order)

Unstuffs an array of values from a buffer.

Parameters

valueA pointer to an array where the unstuffed values will be written.
sizeThe size of the array of values to unstuff as returned by the sizeof operator.
bufPointer to the buffer to unstuff the values from.
orderThe endian byte order to stuff the value.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.

Unstuff_Buffer

int Unstuff_Buffer(char *buf,
char *format,
 ...)

Unstuffs values from a buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  Pointers for where the data will be unstuffed into are passed in through the variable argument list.

Parameters

bufPointer to the buffer to unstuff the values from.
formatA format string describing the data to unstuff.

Returns

The number of bytes read from the buffer to unstuff the value.  You can use this value to add to a pointer to the buffer for unstuffing further data.  Returns -1 if an error occurs.

Example

...
char c;
uint16_t foo[10];
float bar;
char string[20];

Unstuff_Buffer(buf, "caHfs", &c, sizeof(foo), &foo, &bar, string);
...

Unstuff_BufferArgs

int Unstuff_BufferArgs(char *buf,
char *format,
va_list args)

Unstuffs values from a buffer according to the format string parameter.  The tokens allowed in the format string are described in the Format Strings section.  The function takes a va_list for use within wrapping functions that take a variable number of parameters.

Parameters

bufPointer to the buffer to unstuff the values from.
formatA format string describing the data to unstuff.
argsA va_list of arguments.

Returns

The number of bytes read from the buffer to unstuff the values.  You can use this value to add to a pointer to the buffer for unstuffing further data.  Returns -1 if an error occurs.

Example

int MyFunction(char* buf, char* format, ...)
{
    int buf_size;
    va_list args;

    // Do our own thing here

    va_start(args, format);
    buf_size = Unstuff_BufferArgs(buf, format, args);
    va_end(args);

    return buf_size;
}

...
char c;
uint16_t foo[10];
float bar;
char string[20];

MyFunction(buf, "caHfs", &c, sizeof(foo), &foo, &bar, string);
...

Stuffing/Unstuffing Utilities

Stuff_CalcSize

int Stuff_CalcSize(char *format,
 ...)

Calculates the size of a buffer needed to stuff or unstuff the data described by the format string.  Data of of variable size (strings, arrays, etc.) should have there sizes, as returned by the sizeof operator, passed in through the variable parameter list.

Parameters

formatA format string describing the data to stuff or unstuff that will be used to calculate the needed buffer size.

Returns

The size of a buffer needed to contain the data described in the format string.  Returns -1 if an error occurs.

Example

...
int size;
float farray[10];

size = Stuff_CalcSize("cHafX", sizeof(farray));
...

Stuff_CalcSizeArgs

int Stuff_CalcSizeArgs(char *format,
va_list args)

Calculates the size of a buffer needed to stuff or unstuff the data described by the format string.  This function is useful with your own wrapping functions and will accept a va_list with all the data present.  Data of of variable size (strings, arrays, etc.) should have there sizes, as returned by the sizeof operator, passed in through the variable parameter list before the pointer to the data.

Parameters

formatA format string describing the data to stuff or unstuff that will be used to calculate the needed buffer size.
argsA va_list of arguments.

Returns

The size of a buffer needed to contain the data described in the format string.  Returns -1 if an error occurs.

Example

int MyFunction(char* format, ...)
{
    char *buf;
    int buf_size;
    va_list args;

    va_start(args, format);
    buf_size = Stuff_CalcSizeArgs(format, args);
    va_end(args);

    buf = calloc(buf_size, sizeof(char));

    // Do our own thing here like stuff with
    // the <Stuff_BufferArgs> function.

    return buf_size;
}
int Pack_Int8(int8_t value,
char *buf,
enum PackOrder order)
Packs a 8-bit signed integer into a buffer.
int Pack_UInt8(uint8_t value,
char *buf,
enum PackOrder order)
Packs a 8-bit unsigned integer into a buffer.
int Pack_Int16(int16_t value,
char *buf,
enum PackOrder order)
Packs a 16-bit signed integer into a buffer.
int Pack_UInt16(uint16_t value,
char *buf,
enum PackOrder order)
Packs a 16-bit unsigned integer into a buffer.
int Pack_Int32(int32_t value,
char *buf,
enum PackOrder order)
Packs a 32-bit signed integer into a buffer.
int Pack_UInt32(uint32_t value,
char *buf,
enum PackOrder order)
Packs a 32-bit unsigned integer into a buffer.
int Pack_Int64(int64_t value,
char *buf,
enum PackOrder order)
Packs a 64-bit signed integer into a buffer.
int Pack_UInt64(uint64_t value,
char *buf,
enum PackOrder order)
Packs a 64-bit unsigned integer into a buffer.
int Pack_Float(float value,
char *buf,
enum PackOrder order)
Packs a single precision, 32-bit floating point number into a buffer.
int Pack_Double(double value,
char *buf,
enum PackOrder order)
Packs a double precision, 64-bit floating point number into a buffer.
int Pack_String(char *string,
char *buf,
enum PackOrder order)
Packs a length prepended string into a buffer.
int Pack_Array1(void *value,
int size,
char *buf,
enum PackOrder order)
Packs an array of 1-byte aligned values into a buffer.
int Pack_Array2(void *value,
int size,
char *buf,
enum PackOrder order)
Packs an array of 2-byte aligned values into a buffer.
int Pack_Array4(void *value,
int size,
char *buf,
enum PackOrder order)
Packs an array of 4-byte aligned values into a buffer.
int Pack_Array8(void *value,
int size,
char *buf,
enum PackOrder order)
Packs an array of 8-byte aligned values into a buffer.
int Pack_Buffer(char *buf,
char *format,
 ...)
Packs values into a buffer according to the format string parameter.
int Pack_BufferNew(char **buf,
char *format,
 ...)
Packs values into a malloc’ed buffer according to the format string parameter.
int Pack_BufferArgs(char *buf,
char *format,
va_list args)
Packs values into a buffer according to the format string parameter.
int Unpack_Int8(int8_t *value,
char *buf,
enum PackOrder order)
Unpacks a 8-bit signed integer from a buffer.
int Unpack_UInt8(uint8_t *value,
char *buf,
enum PackOrder order)
Unpacks a 8-bit unsigned integer from a buffer.
int Unpack_Int16(int16_t *value,
char *buf,
enum PackOrder order)
Unpacks a 16-bit signed integer from a buffer.
int Unpack_UInt16(uint16_t *value,
char *buf,
enum PackOrder order)
Unpacks a 16-bit unsigned integer from a buffer.
int Unpack_Int32(int32_t *value,
char *buf,
enum PackOrder order)
Unpacks a 32-bit signed integer from a buffer.
int Unpack_UInt32(uint32_t *value,
char *buf,
enum PackOrder order)
Unpacks a 32-bit unsigned integer from a buffer.
int Unpack_Int64(int64_t *value,
char *buf,
enum PackOrder order)
Unpacks a 64-bit signed integer from a buffer.
int Unpack_UInt64(uint64_t *value,
char *buf,
enum PackOrder order)
Unpacks a 64-bit unsigned integer from a buffer.
int Unpack_Float(float *value,
char *buf,
enum PackOrder order)
Unpacks a single precision, 32-bit floating point number from a buffer.
int Unpack_Double(double *value,
char *buf,
enum PackOrder order)
Unpacks a double precision, 64-bit floating point number from a buffer.
int Unpack_String(char *string,
char *buf,
enum PackOrder order)
Unpacks a length prepended string from a buffer.
int Unpack_StringNew(char **string,
char *buf,
enum PackOrder order)
Unpacks a length prepended string from a buffer.
int Unpack_Array1(void *value,
int size,
char *buf,
enum PackOrder order)
Unpacks an array of 1-byte aligned values from a buffer.
int Unpack_Array2(void *value,
int size,
char *buf,
enum PackOrder order)
Unpacks an array of 2-byte aligned values from a buffer.
int Unpack_Array4(void *value,
int size,
char *buf,
enum PackOrder order)
Unpacks an array of 4-byte aligned values from a buffer.
int Unpack_Array8(void *value,
int size,
char *buf,
enum PackOrder order)
Unpacks an array of 8-byte aligned values from a buffer.
int Unpack_Buffer(char *buf,
char *format,
 ...)
Unpacks values from a buffer according to the format string parameter.
int Unpack_BufferArgs(char *buf,
char *format,
va_list args)
Unpacks values from a buffer according to the format string parameter.
int Pack_CalcSize(char *format,
 ...)
Calculates the size of a buffer needed to pack or unpack the data described by the format string.
int Pack_CalcSizeArgs(char *format,
va_list args)
Calculates the size of a buffer needed to pack or unpack the data described by the format string.
int Stuff_Int8(int8_t value,
char *buf,
enum PackOrder order)
Stuffs a 8-bit signed integer into a buffer.
int Stuff_UInt8(uint8_t value,
char *buf,
enum PackOrder order)
Stuffs a 8-bit unsigned integer into a buffer.
int Stuff_Int16(int16_t value,
char *buf,
enum PackOrder order)
Stuffs a 16-bit signed integer into a buffer.
int Stuff_UInt16(uint16_t value,
char *buf,
enum PackOrder order)
Stuffs a 16-bit unsigned integer into a buffer.
int Stuff_Int32(int32_t value,
char *buf,
enum PackOrder order)
Stuffs a 32-bit signed integer into a buffer.
int Stuff_UInt32(uint32_t value,
char *buf,
enum PackOrder order)
Stuffs a 32-bit unsigned integer into a buffer.
int Stuff_Int64(int64_t value,
char *buf,
enum PackOrder order)
Stuffs a 64-bit signed integer into a buffer.
int Stuff_UInt64(uint64_t value,
char *buf,
enum PackOrder order)
Stuffs a 64-bit unsigned integer into a buffer.
int Stuff_Float(float value,
char *buf,
enum PackOrder order)
Stuffs a single precision, 32-bit floating point number into a buffer.
int Stuff_Double(double value,
char *buf,
enum PackOrder order)
Stuffs a double precision, 64-bit floating point number into a buffer.
int Stuff_String(char *string,
char *buf,
enum PackOrder order)
Stuffs a length prepended string into a buffer.
int Stuff_Array(void *value,
int size,
char *buf,
enum PackOrder order)
Stuffs an array of bytes into a buffer.
int Stuff_Buffer(char *buf,
char *format,
 ...)
Stuffs values into a buffer according to the format string parameter.
int Stuff_BufferNew(char **buf,
char *format,
 ...)
Stuffs values into a malloc’ed buffer according to the format string parameter.
int Stuff_BufferArgs(char *buf,
char *format,
va_list args)
Stuffs values into a buffer according to the format string parameter.
int Unstuff_Int8(int8_t *value,
char *buf,
enum PackOrder order)
Unstuffs a 8-bit signed integer from a buffer.
int Unstuff_UInt8(uint8_t *value,
char *buf,
enum PackOrder order)
Unstuffs a 8-bit unsigned integer from a buffer.
int Unstuff_Int16(int16_t *value,
char *buf,
enum PackOrder order)
Unstuffs a 16-bit signed integer from a buffer.
int Unstuff_UInt16(uint16_t *value,
char *buf,
enum PackOrder order)
Unstuffs a 16-bit unsigned integer from a buffer.
int Unstuff_Int32(int32_t *value,
char *buf,
enum PackOrder order)
Unstuffs a 32-bit signed integer from a buffer.
int Unstuff_UInt32(uint32_t *value,
char *buf,
enum PackOrder order)
Unstuffs a 32-bit unsigned integer from a buffer.
int Unstuff_Int64(int64_t *value,
char *buf,
enum PackOrder order)
Unstuffs a 64-bit signed integer from a buffer.
int Unstuff_UInt64(uint64_t *value,
char *buf,
enum PackOrder order)
Unstuffs a 64-bit unsigned integer from a buffer.
int Unstuff_Float(float *value,
char *buf,
enum PackOrder order)
Unstuffs a single precision, 32-bit floating point number from a buffer.
int Unstuff_Double(double *value,
char *buf,
enum PackOrder order)
Unstuffs a double precision, 64-bit floating point number from a buffer.
int Unstuff_String(char *string,
char *buf,
enum PackOrder order)
Unstuffs a length prepended string from a buffer.
int Unstuff_StringNew(char **string,
char *buf,
enum PackOrder order)
Unstuffs a length prepended string from a buffer.
int Unstuff_Array(void *value,
int size,
char *buf,
enum PackOrder order)
Unstuffs an array of values from a buffer.
int Unstuff_Buffer(char *buf,
char *format,
 ...)
Unstuffs values from a buffer according to the format string parameter.
int Unstuff_BufferArgs(char *buf,
char *format,
va_list args)
Unstuffs values from a buffer according to the format string parameter.
int Stuff_CalcSize(char *format,
 ...)
Calculates the size of a buffer needed to stuff or unstuff the data described by the format string.
int Stuff_CalcSizeArgs(char *format,
va_list args)
Calculates the size of a buffer needed to stuff or unstuff the data described by the format string.
Various functions take a format string as a parameter to marshall multiple values into a buffer at one time.
Close