Lightweight low-level binary read/write helpers for C.
net_rw.h provides endian-safe primitive serialization functions for working with binary protocols, packet formats, file formats, embedded systems, SDR/radio protocols, and raw byte buffers.
The library is header-only, allocation-free, alignment-safe, and designed for explicit manual buffer management.
- Header-only implementation
- Little-endian serialization format
- Alignment-safe memory access using
memcpy - Portable fixed-width integer encoding
- IEEE754 float/double serialization
- No hidden state or allocations
- Minimal and low-overhead API
- Suitable for embedded and systems programming
u8u16u32u64
i8i16i32i64
f32f64
- byte arrays
- binary blobs
net_rw.h is intentionally low-level.
The library only performs primitive binary serialization/deserialization operations.
It does NOT provide:
- automatic buffer management
- bounds checking
- stream abstractions
- offset tracking
- dynamic allocations
- protocol definitions
The caller is responsible for managing offsets, buffer sizes, and protocol structures.
Example:
uint8_t buffer[256];
size_t offset = 0;
write_u32(buffer + offset, 0x12345678);
offset += 4;
write_f32(buffer + offset, 3.14f);
offset += 4;This explicit design keeps the library:
- lightweight
- deterministic
- flexible
- easy to integrate into low-level systems
All serialized values are encoded in little-endian format.
This ensures deterministic and portable binary encoding across architectures.
Writing raw C structs directly to files or networks is often unsafe because of:
- compiler padding
- alignment differences
- endianness issues
- compiler/platform ABI differences
net_rw.h avoids these issues by explicitly encoding primitive values into raw byte buffers.
#include "net_rw.h"#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "net_rw.h"
int main(void)
{
uint8_t buffer[256];
size_t offset = 0;
write_u32(buffer + offset, 0xABCD1234);
offset += 4;
write_u16(buffer + offset, 1337);
offset += 2;
write_f32(buffer + offset, 3.14f);
offset += 4;
write_bytes(buffer + offset, "hello", 5);
offset += 5;
return 0;
}#include <stdio.h>
#include <stdint.h>
#include "net_rw.h"
int main(void)
{
uint8_t buffer[256];
size_t offset = 0;
uint32_t magic = read_u32(buffer + offset);
offset += 4;
uint16_t port = read_u16(buffer + offset);
offset += 2;
float value = read_f32(buffer + offset);
offset += 4;
return 0;
}write_u8()
write_u16()
write_u32()
write_u64()
write_i8()
write_i16()
write_i32()
write_i64()
write_f32()
write_f64()
write_bytes()read_u8()
read_u16()
read_u32()
read_u64()
read_i8()
read_i16()
read_i32()
read_i64()
read_f32()
read_f64()
read_bytes()net_rw.h is suitable for:
- TCP/UDP protocols
- SDR/radio packet formats
- custom binary protocols
- binary file formats
- game networking
- embedded firmware protocols
- IPC/shared memory
- serialization systems
- packet framing systems
- custom archive/container formats
The library assumes:
- valid pointers
- sufficient buffer size
- proper offset management by caller
No bounds checking is performed internally.
- C11 compatible compiler
- Compiler support for:
__builtin_memcpy__builtin_bswap16__builtin_bswap32__builtin_bswap64
Tested with:
- GCC
- Clang
MIT License