Skip to content

File syn_pack.h

FileList > src > syntropic > util > syn_pack.h

Go to the source code of this file

Binary message packer / unpacker (header-only). More...

  • #include <stddef.h>
  • #include <stdint.h>
  • #include <string.h>

Public Static Functions

Type Name
void syn_pack_bytes (uint8_t * buf, size_t * pos, const uint8_t * data, size_t len)
Pack a raw byte array.
void syn_pack_i16 (uint8_t * buf, size_t * pos, int16_t val)
Pack an int16_t value (Big Endian).
void syn_pack_i16_le (uint8_t * buf, size_t * pos, int16_t val)
Pack an int16_t value (Little Endian).
void syn_pack_i32 (uint8_t * buf, size_t * pos, int32_t val)
Pack an int32_t value (Big Endian).
void syn_pack_i32_le (uint8_t * buf, size_t * pos, int32_t val)
Pack an int32_t value (Little Endian).
void syn_pack_i8 (uint8_t * buf, size_t * pos, int8_t val)
Pack an int8_t value (Big Endian).
void syn_pack_u16 (uint8_t * buf, size_t * pos, uint16_t val)
Pack a uint16_t value (Big Endian).
void syn_pack_u16_le (uint8_t * buf, size_t * pos, uint16_t val)
Pack a uint16_t value (Little Endian).
void syn_pack_u32 (uint8_t * buf, size_t * pos, uint32_t val)
Pack a uint32_t value (Big Endian).
void syn_pack_u32_le (uint8_t * buf, size_t * pos, uint32_t val)
Pack a uint32_t value (Little Endian).
void syn_pack_u64 (uint8_t * buf, size_t * pos, uint64_t val)
Pack a uint64_t value (Big Endian).
void syn_pack_u64_le (uint8_t * buf, size_t * pos, uint64_t val)
Pack a uint64_t value (Little Endian).
void syn_pack_u8 (uint8_t * buf, size_t * pos, uint8_t val)
Pack a uint8_t value (Big Endian).
uint16_t syn_peek_u16 (const uint8_t * buf, size_t pos)
Peek a uint16_t value (Big Endian) without advancing position.
uint16_t syn_peek_u16_le (const uint8_t * buf, size_t pos)
Peek a uint16_t value (Little Endian) without advancing position.
uint32_t syn_peek_u32 (const uint8_t * buf, size_t pos)
Peek a uint32_t value (Big Endian) without advancing position.
uint32_t syn_peek_u32_le (const uint8_t * buf, size_t pos)
Peek a uint32_t value (Little Endian) without advancing position.
uint8_t syn_peek_u8 (const uint8_t * buf, size_t pos)
Peek a uint8_t value (Big Endian) without advancing position.
void syn_poke_u16 (uint16_t val, uint8_t * buf, size_t pos)
Poke a uint16_t value (Big Endian) at a fixed offset.
void syn_poke_u16_le (uint16_t val, uint8_t * buf, size_t pos)
Poke a uint16_t value (Little Endian) at a fixed offset.
void syn_poke_u32 (uint32_t val, uint8_t * buf, size_t pos)
Poke a uint32_t value (Big Endian) at a fixed offset.
void syn_poke_u32_le (uint32_t val, uint8_t * buf, size_t pos)
Poke a uint32_t value (Little Endian) at a fixed offset.
void syn_unpack_bytes (const uint8_t * buf, size_t * pos, uint8_t * out, size_t len)
Unpack a raw byte array.
int16_t syn_unpack_i16 (const uint8_t * buf, size_t * pos)
Unpack an int16_t value (Big Endian).
int16_t syn_unpack_i16_le (const uint8_t * buf, size_t * pos)
Unpack an int16_t value (Little Endian).
int32_t syn_unpack_i32 (const uint8_t * buf, size_t * pos)
Unpack an int32_t value (Big Endian).
int32_t syn_unpack_i32_le (const uint8_t * buf, size_t * pos)
Unpack an int32_t value (Little Endian).
int8_t syn_unpack_i8 (const uint8_t * buf, size_t * pos)
Unpack an int8_t value (Big Endian).
uint16_t syn_unpack_u16 (const uint8_t * buf, size_t * pos)
Unpack a uint16_t value (Big Endian).
uint16_t syn_unpack_u16_le (const uint8_t * buf, size_t * pos)
Unpack a uint16_t value (Little Endian).
uint32_t syn_unpack_u32 (const uint8_t * buf, size_t * pos)
Unpack a uint32_t value (Big Endian).
uint32_t syn_unpack_u32_le (const uint8_t * buf, size_t * pos)
Unpack a uint32_t value (Little Endian).
uint64_t syn_unpack_u64 (const uint8_t * buf, size_t * pos)
Unpack a uint64_t value (Big Endian).
uint64_t syn_unpack_u64_le (const uint8_t * buf, size_t * pos)
Unpack a uint64_t value (Little Endian).
uint8_t syn_unpack_u8 (const uint8_t * buf, size_t * pos)
Unpack a uint8_t value (Big Endian).

Detailed Description

Endianness-safe serialization for packing sensor telemetry, config blobs, register maps, and protocol payloads.

All functions take a buffer and a position pointer that auto-advances.

Usage:

uint8_t buf[32];
size_t pos = 0;

// Pack (big-endian)
syn_pack_u8(buf, &pos, 0x42);
syn_pack_u16(buf, &pos, 0x1234);
syn_pack_i32(buf, &pos, -12345);
syn_pack_bytes(buf, &pos, payload, 4);

// Unpack
pos = 0;
uint8_t  a = syn_unpack_u8(buf, &pos);
uint16_t b = syn_unpack_u16(buf, &pos);
int32_t  c = syn_unpack_i32(buf, &pos);

Public Static Functions Documentation

function syn_pack_bytes

Pack a raw byte array.

static inline void syn_pack_bytes (
    uint8_t * buf,
    size_t * pos,
    const uint8_t * data,
    size_t len
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • data Array to copy.
  • len Number of bytes to copy.

function syn_pack_i16

Pack an int16_t value (Big Endian).

static inline void syn_pack_i16 (
    uint8_t * buf,
    size_t * pos,
    int16_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_i16_le

Pack an int16_t value (Little Endian).

static inline void syn_pack_i16_le (
    uint8_t * buf,
    size_t * pos,
    int16_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_i32

Pack an int32_t value (Big Endian).

static inline void syn_pack_i32 (
    uint8_t * buf,
    size_t * pos,
    int32_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_i32_le

Pack an int32_t value (Little Endian).

static inline void syn_pack_i32_le (
    uint8_t * buf,
    size_t * pos,
    int32_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_i8

Pack an int8_t value (Big Endian).

static inline void syn_pack_i8 (
    uint8_t * buf,
    size_t * pos,
    int8_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_u16

Pack a uint16_t value (Big Endian).

static inline void syn_pack_u16 (
    uint8_t * buf,
    size_t * pos,
    uint16_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_u16_le

Pack a uint16_t value (Little Endian).

static inline void syn_pack_u16_le (
    uint8_t * buf,
    size_t * pos,
    uint16_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_u32

Pack a uint32_t value (Big Endian).

static inline void syn_pack_u32 (
    uint8_t * buf,
    size_t * pos,
    uint32_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_u32_le

Pack a uint32_t value (Little Endian).

static inline void syn_pack_u32_le (
    uint8_t * buf,
    size_t * pos,
    uint32_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_u64

Pack a uint64_t value (Big Endian).

static inline void syn_pack_u64 (
    uint8_t * buf,
    size_t * pos,
    uint64_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_u64_le

Pack a uint64_t value (Little Endian).

static inline void syn_pack_u64_le (
    uint8_t * buf,
    size_t * pos,
    uint64_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_pack_u8

Pack a uint8_t value (Big Endian).

static inline void syn_pack_u8 (
    uint8_t * buf,
    size_t * pos,
    uint8_t val
) 

Parameters:

  • buf Target buffer.
  • pos Cursor position (auto-advanced).
  • val Value to pack.

function syn_peek_u16

Peek a uint16_t value (Big Endian) without advancing position.

static inline uint16_t syn_peek_u16 (
    const uint8_t * buf,
    size_t pos
) 

Parameters:

  • buf Source buffer.
  • pos Source offset byte index.

Returns:

Value at the offset index.


function syn_peek_u16_le

Peek a uint16_t value (Little Endian) without advancing position.

static inline uint16_t syn_peek_u16_le (
    const uint8_t * buf,
    size_t pos
) 

Parameters:

  • buf Source buffer.
  • pos Source offset byte index.

Returns:

Value at the offset index.


function syn_peek_u32

Peek a uint32_t value (Big Endian) without advancing position.

static inline uint32_t syn_peek_u32 (
    const uint8_t * buf,
    size_t pos
) 

Parameters:

  • buf Source buffer.
  • pos Source offset byte index.

Returns:

Value at the offset index.


function syn_peek_u32_le

Peek a uint32_t value (Little Endian) without advancing position.

static inline uint32_t syn_peek_u32_le (
    const uint8_t * buf,
    size_t pos
) 

Parameters:

  • buf Source buffer.
  • pos Source offset byte index.

Returns:

Value at the offset index.


function syn_peek_u8

Peek a uint8_t value (Big Endian) without advancing position.

static inline uint8_t syn_peek_u8 (
    const uint8_t * buf,
    size_t pos
) 

Parameters:

  • buf Source buffer.
  • pos Source offset byte index.

Returns:

Value at the offset index.


function syn_poke_u16

Poke a uint16_t value (Big Endian) at a fixed offset.

static inline void syn_poke_u16 (
    uint16_t val,
    uint8_t * buf,
    size_t pos
) 

Parameters:

  • val Value to write.
  • buf Target buffer.
  • pos Destination offset byte index.

function syn_poke_u16_le

Poke a uint16_t value (Little Endian) at a fixed offset.

static inline void syn_poke_u16_le (
    uint16_t val,
    uint8_t * buf,
    size_t pos
) 

Parameters:

  • val Value to write.
  • buf Target buffer.
  • pos Destination offset byte index.

function syn_poke_u32

Poke a uint32_t value (Big Endian) at a fixed offset.

static inline void syn_poke_u32 (
    uint32_t val,
    uint8_t * buf,
    size_t pos
) 

Parameters:

  • val Value to write.
  • buf Target buffer.
  • pos Destination offset byte index.

function syn_poke_u32_le

Poke a uint32_t value (Little Endian) at a fixed offset.

static inline void syn_poke_u32_le (
    uint32_t val,
    uint8_t * buf,
    size_t pos
) 

Parameters:

  • val Value to write.
  • buf Target buffer.
  • pos Destination offset byte index.

function syn_unpack_bytes

Unpack a raw byte array.

static inline void syn_unpack_bytes (
    const uint8_t * buf,
    size_t * pos,
    uint8_t * out,
    size_t len
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).
  • out Output buffer to copy into.
  • len Number of bytes to copy.

function syn_unpack_i16

Unpack an int16_t value (Big Endian).

static inline int16_t syn_unpack_i16 (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_i16_le

Unpack an int16_t value (Little Endian).

static inline int16_t syn_unpack_i16_le (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_i32

Unpack an int32_t value (Big Endian).

static inline int32_t syn_unpack_i32 (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_i32_le

Unpack an int32_t value (Little Endian).

static inline int32_t syn_unpack_i32_le (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_i8

Unpack an int8_t value (Big Endian).

static inline int8_t syn_unpack_i8 (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_u16

Unpack a uint16_t value (Big Endian).

static inline uint16_t syn_unpack_u16 (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_u16_le

Unpack a uint16_t value (Little Endian).

static inline uint16_t syn_unpack_u16_le (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_u32

Unpack a uint32_t value (Big Endian).

static inline uint32_t syn_unpack_u32 (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_u32_le

Unpack a uint32_t value (Little Endian).

static inline uint32_t syn_unpack_u32_le (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_u64

Unpack a uint64_t value (Big Endian).

static inline uint64_t syn_unpack_u64 (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_u64_le

Unpack a uint64_t value (Little Endian).

static inline uint64_t syn_unpack_u64_le (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.


function syn_unpack_u8

Unpack a uint8_t value (Big Endian).

static inline uint8_t syn_unpack_u8 (
    const uint8_t * buf,
    size_t * pos
) 

Parameters:

  • buf Source buffer.
  • pos Cursor position (auto-advanced).

Returns:

Unpacked value.



The documentation for this class was generated from the following file src/syntropic/util/syn_pack.h