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 inputValue to pack.
uint8_t* outputDestination buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal 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:
voidNo 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 inputValue to pack.
uint8_t* outputDestination buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal 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:
voidNo 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 inputValue to pack.
uint8_t* outputDestination buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal 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:
voidNo 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 inputValue to pack.
uint8_t* outputDestination buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal 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* inputSource array.
int32_t countNumber of elements to write.
uint8_t* outputDestination buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal 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:
voidNo 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 inputValue to pack.
uint8_t* outputDestination buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal 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:
voidNo 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 inputValue to pack.
uint8_t* outputDestination buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal 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:
voidNo 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 inputValue to pack.
uint8_t* outputDestination buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal 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:
voidNo 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* inputString to pack or NULL.
uint8_t* outputDestination buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal 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* inputString 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* inputSource buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal buffer length.
bool* valueDestination 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* inputSource buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal buffer length.
uint8_t* valueDestination 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* inputSource buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal buffer length.
double* valueDestination 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* inputSource buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal buffer length.
float* valueDestination 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* inputSource buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal buffer length.
float** valueReceives 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* inputSource buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal buffer length.
int16_t* valueDestination 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* inputSource buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal buffer length.
int32_t* valueDestination 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* inputSource buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal buffer length.
int64_t* valueDestination 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* inputSource buffer.
int32_t offsetStart position in the buffer.
int32_t buflenTotal buffer length.
char** valueReceives a heap-allocated duplicate on success.
Return: int32_t Number of bytes read or a negative value on error.