Skip to content

dtpackx

Packing helpers for wire buffers

This group of functions provides little-endian packing and unpacking for basic C types. Use it for serializing and deserializing byte-oriented wire buffers with explicit bounds checking.

Mini-guide

  • Use the pack functions to append values to a buffer by tracking an explicit offset.
  • Use the unpack functions to read values in the same order by reusing the returned length.
  • Check for negative return values to detect bounds or allocation failures early.
  • Pre-compute space requirements with the length helpers before allocating buffers.

Example

int32_t offset = 0;
uint8_t buffer[64];

offset += dtpackx_pack_int32(42, buffer, offset, sizeof buffer);
offset += dtpackx_pack_bool(true, buffer, offset, sizeof buffer);

int32_t value = 0;
bool flag = false;
int32_t read = 0;

read += dtpackx_unpack_int32(buffer, read, sizeof buffer, &value);
read += dtpackx_unpack_bool(buffer, read, sizeof buffer, &flag);

Data structures

Macros

DTPACKX_PACK

DTPACKX_PACK(call, p, length)

Wraps a packing call and advances the position while converting overflow into an error jump.

DTPACKX_UNPACK

DTPACKX_UNPACK(call, p, length)

Wraps an unpacking call and advances the position while converting overflow into an error jump.

Functions

dtpackx_pack_bool

Writes a boolean value as a single byte in little-endian form.

Params:

bool input Value to pack.
uint8_t* output Destination buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.

Return: int32_t Number of bytes written or a negative value on error.

dtpackx_pack_bool_length

Returns the number of bytes required to pack a boolean value.

Params:

void No parameters.

Return: int32_t Size in bytes of the packed value.

dtpackx_pack_byte

Writes an unsigned byte value to the buffer.

Params:

uint8_t input Value to pack.
uint8_t* output Destination buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.

Return: int32_t Number of bytes written or a negative value on error.

dtpackx_pack_byte_length

Returns the number of bytes required to pack a byte value.

Params:

void No parameters.

Return: int32_t Size in bytes of the packed value.

dtpackx_pack_double

Writes a double value as an IEEE-754 bit pattern in little-endian order.

Params:

double input Value to pack.
uint8_t* output Destination buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.

Return: int32_t Number of bytes written or a negative value on error.

dtpackx_pack_double_length

Returns the number of bytes required to pack a double value.

Params:

void No parameters.

Return: int32_t Size in bytes of the packed value.

dtpackx_pack_float

Writes a float value as an IEEE-754 bit pattern in little-endian order.

Params:

float input Value to pack.
uint8_t* output Destination buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.

Return: int32_t Number of bytes written or a negative value on error.

dtpackx_pack_float_array

Writes a counted array of float values using a fixed little-endian layout.

Params:

const float* input Source array.
int32_t count Number of elements to write.
uint8_t* output Destination buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.

Return: int32_t Number of bytes written or a negative value on error.

dtpackx_pack_float_length

Returns the number of bytes required to pack a float value.

Params:

void No parameters.

Return: int32_t Size in bytes of the packed value.

dtpackx_pack_int16

Writes a 16-bit integer value in little-endian order.

Params:

int16_t input Value to pack.
uint8_t* output Destination buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.

Return: int32_t Number of bytes written or a negative value on error.

dtpackx_pack_int16_length

Returns the number of bytes required to pack a 16-bit integer value.

Params:

void No parameters.

Return: int32_t Size in bytes of the packed value.

dtpackx_pack_int32

Writes a 32-bit integer value in little-endian order.

Params:

int32_t input Value to pack.
uint8_t* output Destination buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.

Return: int32_t Number of bytes written or a negative value on error.

dtpackx_pack_int32_length

Returns the number of bytes required to pack a 32-bit integer value.

Params:

void No parameters.

Return: int32_t Size in bytes of the packed value.

dtpackx_pack_int64

Writes a 64-bit integer value in little-endian order.

Params:

int64_t input Value to pack.
uint8_t* output Destination buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.

Return: int32_t Number of bytes written or a negative value on error.

dtpackx_pack_int64_length

Returns the number of bytes required to pack a 64-bit integer value.

Params:

void No parameters.

Return: int32_t Size in bytes of the packed value.

dtpackx_pack_string

Writes a NUL-terminated string including the terminator.

Params:

const char* input String to pack or NULL.
uint8_t* output Destination buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.

Return: int32_t Number of bytes written or a negative value on error.

dtpackx_pack_string_length

Returns the number of bytes required to pack a string including its terminator.

Params:

const char* input String to measure or NULL.

Return: int32_t Size in bytes of the packed value.

dtpackx_unpack_bool

Reads a boolean value from a single byte.

Params:

const uint8_t* input Source buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.
bool* value Destination for the unpacked value.

Return: int32_t Number of bytes read or a negative value on error.

dtpackx_unpack_byte

Reads an unsigned byte value from the buffer.

Params:

const uint8_t* input Source buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.
uint8_t* value Destination for the unpacked value.

Return: int32_t Number of bytes read or a negative value on error.

dtpackx_unpack_double

Reads a double value from an IEEE-754 little-endian bit pattern.

Params:

const uint8_t* input Source buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.
double* value Destination for the unpacked value.

Return: int32_t Number of bytes read or a negative value on error.

dtpackx_unpack_float

Reads a float value from an IEEE-754 little-endian bit pattern.

Params:

const uint8_t* input Source buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.
float* value Destination for the unpacked value.

Return: int32_t Number of bytes read or a negative value on error.

dtpackx_unpack_float_array

Reads a counted array of float values and allocates storage for the result.

Params:

const uint8_t* input Source buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.
float** value Receives a heap-allocated array on success.

Return: int32_t Number of bytes read or a negative value on error.

dtpackx_unpack_int16

Reads a 16-bit integer value from little-endian storage.

Params:

const uint8_t* input Source buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.
int16_t* value Destination for the unpacked value.

Return: int32_t Number of bytes read or a negative value on error.

dtpackx_unpack_int32

Reads a 32-bit integer value from little-endian storage.

Params:

const uint8_t* input Source buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.
int32_t* value Destination for the unpacked value.

Return: int32_t Number of bytes read or a negative value on error.

dtpackx_unpack_int64

Reads a 64-bit integer value from little-endian storage.

Params:

const uint8_t* input Source buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.
int64_t* value Destination for the unpacked value.

Return: int32_t Number of bytes read or a negative value on error.

dtpackx_unpack_string

Reads a NUL-terminated string and duplicates it to heap storage.

Params:

const uint8_t* input Source buffer.
int32_t offset Start position in the buffer.
int32_t buflen Total buffer length.
char** value Receives a heap-allocated duplicate on success.

Return: int32_t Number of bytes read or a negative value on error.