EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
pack (Buffer Packing and Unpacking)

Overview

Platform- and endianness-independent buffer packing and unpacking.

For 16- and 32-bit integer packing and unpacking:

#include "etcpal/pack.h"

For 64-bit integer packing and unpacking:

#include "etcpal/pack64.h"

This module defines macros for packing and unpacking integer types to/from a byte buffer. They are architected in such a way that the endianness of the integer in the buffer is always known, regardless of the endianness of your platform.

For example:

uint8_t buf[4];
etcpal_pack_u32b(buf, 0x11223344); // Pack 32-bit value, big-endian
// buf now contains { 0x11, 0x22, 0x33, 0x44 }
uint32_t val = etcpal_unpack_u32b(&buf); // Unpack 32-bit value, big-endian
etcpal_pack_u32l(&buf, val); // Pack 32-bit value, little-endian
// buf now contains { 0x44, 0x33, 0x22, 0x11 }
void etcpal_pack_u32l(uint8_t *buf, uint32_t val)
Pack a uint32_t to a known little-endian buffer.
Definition: pack.c:226
uint32_t etcpal_unpack_u32b(const uint8_t *buf)
Unpack a uint32_t from a known big-endian buffer.
Definition: pack.c:174
void etcpal_pack_u32b(uint8_t *buf, uint32_t val)
Pack a uint32_t to a known big-endian buffer.
Definition: pack.c:192

Functions

int16_t etcpal_unpack_i16b (const uint8_t *buf)
 Unpack an int16_t from a known big-endian buffer. More...
 
void etcpal_pack_i16b (uint8_t *buf, int16_t val)
 Pack an int16_t to a known big-endian buffer. More...
 
int16_t etcpal_unpack_i16l (const uint8_t *buf)
 Unpack an int16_t from a known little-endian buffer. More...
 
void etcpal_pack_i16l (uint8_t *buf, int16_t val)
 Pack an int16_t to a known little-endian buffer. More...
 
uint16_t etcpal_unpack_u16b (const uint8_t *buf)
 Unpack a uint16_t from a known big-endian buffer. More...
 
void etcpal_pack_u16b (uint8_t *buf, uint16_t val)
 Pack a uint16_t to a known big-endian buffer. More...
 
uint16_t etcpal_unpack_u16l (const uint8_t *buf)
 Unpack a uint16_t from a known little-endian buffer. More...
 
void etcpal_pack_u16l (uint8_t *buf, uint16_t val)
 Pack a uint16_t to a known little-endian buffer. More...
 
int32_t etcpal_unpack_i32b (const uint8_t *buf)
 Unpack an int32_t from a known big-endian buffer. More...
 
void etcpal_pack_i32b (uint8_t *buf, int32_t val)
 Pack an int32_t to a known big-endian buffer. More...
 
int32_t etcpal_unpack_i32l (const uint8_t *buf)
 Unpack an int32_t from a known little-endian buffer. More...
 
void etcpal_pack_i32l (uint8_t *buf, int32_t val)
 Pack an int32_t to a known little-endian buffer. More...
 
uint32_t etcpal_unpack_u32b (const uint8_t *buf)
 Unpack a uint32_t from a known big-endian buffer. More...
 
void etcpal_pack_u32b (uint8_t *buf, uint32_t val)
 Pack a uint32_t to a known big-endian buffer. More...
 
uint32_t etcpal_unpack_u32l (const uint8_t *buf)
 Unpack a uint32_t from a known little-endian buffer. More...
 
void etcpal_pack_u32l (uint8_t *buf, uint32_t val)
 Pack a uint32_t to a known little-endian buffer. More...
 
int64_t etcpal_unpack_i64b (const uint8_t *buf)
 Unpack an int64_t from a known big-endian buffer. More...
 
void etcpal_pack_i64b (uint8_t *buf, int64_t val)
 Pack an int64_t to a known big-endian buffer. More...
 
int64_t etcpal_unpack_i64l (const uint8_t *buf)
 Unpack an int64_t from a known little-endian buffer. More...
 
void etcpal_pack_i64l (uint8_t *buf, int64_t val)
 Pack an int64_t to a known little-endian buffer. More...
 
uint64_t etcpal_unpack_u64b (const uint8_t *buf)
 Unpack a uint64_t from a known big-endian buffer. More...
 
void etcpal_pack_u64b (uint8_t *buf, uint64_t val)
 Pack a uint64_t to a known big-endian buffer. More...
 
uint64_t etcpal_unpack_u64l (const uint8_t *buf)
 Unpack a uint64_t from a known little-endian buffer. More...
 
void etcpal_pack_u64l (uint8_t *buf, uint64_t val)
 Pack a uint64_t to a known little-endian buffer. More...
 

Function Documentation

◆ etcpal_pack_i16b()

void etcpal_pack_i16b ( uint8_t *  buf,
int16_t  val 
)

Pack an int16_t to a known big-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 2 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_i16l()

void etcpal_pack_i16l ( uint8_t *  buf,
int16_t  val 
)

Pack an int16_t to a known little-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 2 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_i32b()

void etcpal_pack_i32b ( uint8_t *  buf,
int32_t  val 
)

Pack an int32_t to a known big-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 4 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_i32l()

void etcpal_pack_i32l ( uint8_t *  buf,
int32_t  val 
)

Pack an int32_t to a known little-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 4 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_i64b()

void etcpal_pack_i64b ( uint8_t *  buf,
int64_t  val 
)

Pack an int64_t to a known big-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 8 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_i64l()

void etcpal_pack_i64l ( uint8_t *  buf,
int64_t  val 
)

Pack an int64_t to a known little-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 8 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_u16b()

void etcpal_pack_u16b ( uint8_t *  buf,
uint16_t  val 
)

Pack a uint16_t to a known big-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 2 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_u16l()

void etcpal_pack_u16l ( uint8_t *  buf,
uint16_t  val 
)

Pack a uint16_t to a known little-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 2 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_u32b()

void etcpal_pack_u32b ( uint8_t *  buf,
uint32_t  val 
)

Pack a uint32_t to a known big-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 4 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_u32l()

void etcpal_pack_u32l ( uint8_t *  buf,
uint32_t  val 
)

Pack a uint32_t to a known little-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 4 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_u64b()

void etcpal_pack_u64b ( uint8_t *  buf,
uint64_t  val 
)

Pack a uint64_t to a known big-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 8 bytes in size.
valValue to pack into the buffer.

◆ etcpal_pack_u64l()

void etcpal_pack_u64l ( uint8_t *  buf,
uint64_t  val 
)

Pack a uint64_t to a known little-endian buffer.

Parameters
bufPointer to the buffer into which to pack a value; must be at least 8 bytes in size.
valValue to pack into the buffer.

◆ etcpal_unpack_i16b()

int16_t etcpal_unpack_i16b ( const uint8_t *  buf)

Unpack an int16_t from a known big-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 2 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_i16l()

int16_t etcpal_unpack_i16l ( const uint8_t *  buf)

Unpack an int16_t from a known little-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 2 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_i32b()

int32_t etcpal_unpack_i32b ( const uint8_t *  buf)

Unpack an int32_t from a known big-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 4 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_i32l()

int32_t etcpal_unpack_i32l ( const uint8_t *  buf)

Unpack an int32_t from a known little-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 4 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_i64b()

int64_t etcpal_unpack_i64b ( const uint8_t *  buf)

Unpack an int64_t from a known big-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 8 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_i64l()

int64_t etcpal_unpack_i64l ( const uint8_t *  buf)

Unpack an int64_t from a known little-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 8 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_u16b()

uint16_t etcpal_unpack_u16b ( const uint8_t *  buf)

Unpack a uint16_t from a known big-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 2 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_u16l()

uint16_t etcpal_unpack_u16l ( const uint8_t *  buf)

Unpack a uint16_t from a known little-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 2 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_u32b()

uint32_t etcpal_unpack_u32b ( const uint8_t *  buf)

Unpack a uint32_t from a known big-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 4 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_u32l()

uint32_t etcpal_unpack_u32l ( const uint8_t *  buf)

Unpack a uint32_t from a known little-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 4 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_u64b()

uint64_t etcpal_unpack_u64b ( const uint8_t *  buf)

Unpack a uint64_t from a known big-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 8 bytes in size.
Returns
Unpacked value.

◆ etcpal_unpack_u64l()

uint64_t etcpal_unpack_u64l ( const uint8_t *  buf)

Unpack a uint64_t from a known little-endian buffer.

Parameters
bufPointer to the buffer from which to unpack a value; must be at least 8 bytes in size.
Returns
Unpacked value.