Add serial module — little-endian binary serialization primitives

put_u8/16/32/64 and get_u8/16/32/64 (plus signed variants) for packing
and unpacking values at explicit byte offsets in a buffer. Each value is
encoded byte-by-byte with explicit shift operations — no struct casting,
no alignment assumptions, correct on any host endianness.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-25 22:15:41 +00:00
parent e1b848333b
commit b56dfae672
3 changed files with 141 additions and 0 deletions

32
include/serial.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <stdint.h>
/*
* Binary serialization primitives.
*
* All values are encoded little-endian. Fields are placed and read
* explicitly by position — never by casting a struct to bytes.
*/
/* -- put: write a value into a buffer at a given byte offset -- */
void put_u8 (uint8_t *buf, uint32_t offset, uint8_t value);
void put_u16(uint8_t *buf, uint32_t offset, uint16_t value);
void put_u32(uint8_t *buf, uint32_t offset, uint32_t value);
void put_u64(uint8_t *buf, uint32_t offset, uint64_t value);
void put_i8 (uint8_t *buf, uint32_t offset, int8_t value);
void put_i16(uint8_t *buf, uint32_t offset, int16_t value);
void put_i32(uint8_t *buf, uint32_t offset, int32_t value);
void put_i64(uint8_t *buf, uint32_t offset, int64_t value);
/* -- get: read a value from a buffer at a given byte offset -- */
uint8_t get_u8 (const uint8_t *buf, uint32_t offset);
uint16_t get_u16(const uint8_t *buf, uint32_t offset);
uint32_t get_u32(const uint8_t *buf, uint32_t offset);
uint64_t get_u64(const uint8_t *buf, uint32_t offset);
int8_t get_i8 (const uint8_t *buf, uint32_t offset);
int16_t get_i16(const uint8_t *buf, uint32_t offset);
int32_t get_i32(const uint8_t *buf, uint32_t offset);
int64_t get_i64(const uint8_t *buf, uint32_t offset);